From f7a07425e9f5d366b283409f85d2f16a2474d99e Mon Sep 17 00:00:00 2001 From: zhouyifan279 Date: Mon, 11 Mar 2024 20:07:35 +0800 Subject: [PATCH 1/4] Fix flaky test KyuubiBeeLineTest.testKyuubiBeelineComment --- .../hive/beeline/KyuubiBeeLineTest.java | 51 +++++++++++++++---- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java b/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java index 9c7aec35a42..2d688b133a6 100644 --- a/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java +++ b/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java @@ -89,16 +89,47 @@ public void testKyuubiBeeLinePythonMode() { } @Test - public void testKyuubiBeelineComment() { - KyuubiBeeLine kyuubiBeeLine = new KyuubiBeeLine(); - int result = kyuubiBeeLine.initArgsFromCliVars(new String[] {"-e", "--comment show database;"}); - assertEquals(0, result); - result = kyuubiBeeLine.initArgsFromCliVars(new String[] {"-e", "--comment\n show database;"}); - assertEquals(1, result); - result = - kyuubiBeeLine.initArgsFromCliVars( - new String[] {"-e", "--comment line 1 \n --comment line 2 \n show database;"}); - assertEquals(1, result); + public void testKyuubiBeelineComment() throws NoSuchFieldException { + KyuubiBeeLine kyuubiBeeLine = + new KyuubiBeeLine() { + @Override + boolean dispatch(String line) { + // Pretend connection is established + if (line != null && line.startsWith("!connect")) { + return true; + } + return super.dispatch(line); + } + }; + + String[] cmd = new String[] {""}; + KyuubiCommands commands = + new KyuubiCommands(kyuubiBeeLine) { + @Override + public boolean sql(String line, boolean entireLineAsCommand) { + cmd[0] = line; + return true; + } + }; + DynFields.builder() + .hiddenImpl(BeeLine.class, "commands") + .buildChecked(kyuubiBeeLine) + .set(commands); + + kyuubiBeeLine.initArgs(new String[] {"-u", "dummy_url", "-e", "--comment show database;"}); + assertEquals(0, cmd[0].length()); + + // Beeline#exit must be false to execute sql + kyuubiBeeLine.setExit(false); + kyuubiBeeLine.initArgs(new String[] {"-u", "dummy_url", "-e", "--comment\n show database;"}); + assertEquals("show database;", cmd[0]); + + kyuubiBeeLine.setExit(false); + kyuubiBeeLine.initArgs( + new String[] { + "-u", "dummy_url", "-e", "--comment line 1 \n --comment line 2 \n show database;" + }); + assertEquals("show database;", cmd[0]); } static class BufferPrintStream extends PrintStream { From 29bf670ddba862e3b475172110fa9182400a8c90 Mon Sep 17 00:00:00 2001 From: zhouyifan279 Date: Mon, 11 Mar 2024 20:54:50 +0800 Subject: [PATCH 2/4] Fix flaky test KyuubiBeeLineTest.testKyuubiBeelineComment --- .../src/main/java/org/apache/hive/beeline/BeeLine.java | 6 +++++- .../main/java/org/apache/hive/beeline/KyuubiBeeLine.java | 7 +------ .../java/org/apache/hive/beeline/KyuubiBeeLineTest.java | 5 +---- 3 files changed, 7 insertions(+), 11 deletions(-) diff --git a/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/BeeLine.java b/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/BeeLine.java index e3b29206c05..b66dca26c1a 100644 --- a/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/BeeLine.java +++ b/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/BeeLine.java @@ -134,7 +134,7 @@ public class BeeLine implements Closeable { private final BeeLineOpts opts = new BeeLineOpts(this, System.getProperties()); private String lastProgress = null; private final Map seenWarnings = new HashMap(); - private final Commands commands = new Commands(this); + private Commands commands = new Commands(this); private OutputFile scriptOutputFile = null; private OutputFile recordOutputFile = null; private PrintStream outputStream = new PrintStream(System.out, true); @@ -2382,4 +2382,8 @@ void setIsTestMode(boolean isTestMode) { boolean isTestMode() { return isTestMode; } + + protected void setCommands(Commands commands) { + this.commands = commands; + } } diff --git a/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiBeeLine.java b/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiBeeLine.java index c786da35f24..878c95d86dc 100644 --- a/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiBeeLine.java +++ b/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiBeeLine.java @@ -48,7 +48,6 @@ public class KyuubiBeeLine extends BeeLine { public static final String KYUUBI_BEELINE_DEFAULT_JDBC_DRIVER = "org.apache.kyuubi.jdbc.KyuubiHiveDriver"; - protected KyuubiCommands commands = new KyuubiCommands(this); private Driver defaultDriver; // copied from org.apache.hive.beeline.BeeLine @@ -66,11 +65,7 @@ public KyuubiBeeLine() { @SuppressWarnings("deprecation") public KyuubiBeeLine(boolean isBeeLine) { super(isBeeLine); - try { - DynFields.builder().hiddenImpl(BeeLine.class, "commands").buildChecked(this).set(commands); - } catch (Throwable t) { - throw new ExceptionInInitializerError("Failed to inject kyuubi commands"); - } + setCommands(new KyuubiCommands(this)); try { defaultDriver = DynConstructors.builder() diff --git a/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java b/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java index 2d688b133a6..532fd1237fa 100644 --- a/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java +++ b/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java @@ -111,10 +111,7 @@ public boolean sql(String line, boolean entireLineAsCommand) { return true; } }; - DynFields.builder() - .hiddenImpl(BeeLine.class, "commands") - .buildChecked(kyuubiBeeLine) - .set(commands); + kyuubiBeeLine.setCommands(commands); kyuubiBeeLine.initArgs(new String[] {"-u", "dummy_url", "-e", "--comment show database;"}); assertEquals(0, cmd[0].length()); From 935aa371b51a7ba6bd144f732a18938dff764402 Mon Sep 17 00:00:00 2001 From: zhouyifan279 Date: Mon, 11 Mar 2024 21:00:48 +0800 Subject: [PATCH 3/4] Fix flaky test KyuubiBeeLineTest.testKyuubiBeelineComment --- .../hive/beeline/KyuubiBeeLineTest.java | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java b/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java index 532fd1237fa..e61724434b0 100644 --- a/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java +++ b/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java @@ -89,8 +89,8 @@ public void testKyuubiBeeLinePythonMode() { } @Test - public void testKyuubiBeelineComment() throws NoSuchFieldException { - KyuubiBeeLine kyuubiBeeLine = + public void testKyuubiBeelineComment() { + KyuubiBeeLine interceptedKyuubiBeeLine = new KyuubiBeeLine() { @Override boolean dispatch(String line) { @@ -103,26 +103,28 @@ boolean dispatch(String line) { }; String[] cmd = new String[] {""}; - KyuubiCommands commands = - new KyuubiCommands(kyuubiBeeLine) { + KyuubiCommands interceptedCommands = + new KyuubiCommands(interceptedKyuubiBeeLine) { @Override public boolean sql(String line, boolean entireLineAsCommand) { cmd[0] = line; return true; } }; - kyuubiBeeLine.setCommands(commands); + interceptedKyuubiBeeLine.setCommands(interceptedCommands); - kyuubiBeeLine.initArgs(new String[] {"-u", "dummy_url", "-e", "--comment show database;"}); + interceptedKyuubiBeeLine.initArgs( + new String[] {"-u", "dummy_url", "-e", "--comment show database;"}); assertEquals(0, cmd[0].length()); // Beeline#exit must be false to execute sql - kyuubiBeeLine.setExit(false); - kyuubiBeeLine.initArgs(new String[] {"-u", "dummy_url", "-e", "--comment\n show database;"}); + interceptedKyuubiBeeLine.setExit(false); + interceptedKyuubiBeeLine.initArgs( + new String[] {"-u", "dummy_url", "-e", "--comment\n show database;"}); assertEquals("show database;", cmd[0]); - kyuubiBeeLine.setExit(false); - kyuubiBeeLine.initArgs( + interceptedKyuubiBeeLine.setExit(false); + interceptedKyuubiBeeLine.initArgs( new String[] { "-u", "dummy_url", "-e", "--comment line 1 \n --comment line 2 \n show database;" }); From c250af07fd1bbe939c3385172437fff0365004dd Mon Sep 17 00:00:00 2001 From: zhouyifan279 Date: Mon, 11 Mar 2024 21:17:03 +0800 Subject: [PATCH 4/4] Fix beeline test KyuubiBeeLineTest.testKyuubiBeelineComment --- .../java/org/apache/hive/beeline/KyuubiBeeLineTest.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java b/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java index e61724434b0..8b87b13a06c 100644 --- a/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java +++ b/kyuubi-hive-beeline/src/test/java/org/apache/hive/beeline/KyuubiBeeLineTest.java @@ -26,8 +26,13 @@ import java.io.PrintStream; import org.apache.kyuubi.util.reflect.DynFields; import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class KyuubiBeeLineTest { + + private static final Logger LOG = LoggerFactory.getLogger(KyuubiBeeLineTest.class); + @Test public void testKyuubiBeelineWithoutArgs() { KyuubiBeeLine kyuubiBeeLine = new KyuubiBeeLine(); @@ -94,8 +99,8 @@ public void testKyuubiBeelineComment() { new KyuubiBeeLine() { @Override boolean dispatch(String line) { - // Pretend connection is established if (line != null && line.startsWith("!connect")) { + LOG.info("Return true for command: {} to pretend connection is established.", line); return true; } return super.dispatch(line); @@ -107,6 +112,7 @@ boolean dispatch(String line) { new KyuubiCommands(interceptedKyuubiBeeLine) { @Override public boolean sql(String line, boolean entireLineAsCommand) { + LOG.info("Return true for sql {} to pretend sql is executed successfully.", line); cmd[0] = line; return true; }