This is an automated email from the ASF dual-hosted git repository. chengpan pushed a commit to branch branch-1.7 in repository https://gitbox.apache.org/repos/asf/kyuubi.git
commit 4dff8ee373612c8b21b490dfe575f9e4b90d704b Author: fwang12 <[email protected]> AuthorDate: Thu Jun 15 10:09:01 2023 +0800 [KYUUBI #4962][1.7] Backport HIVE-19048: Initscript errors are ignored ### _Why are the changes needed?_ FYI: For hive-3.1.3, if failed to get connection, the return code of `beeline -f` is 0, the correct one should be non-zero. And it works well for hive-4.0.0-alpha-2.  And the hive-beeline dependency version for kyuubi is 3.1.3. This pr backports HIVE-19048: Initscript errors are ignored. https://issues.apache.org/jira/browse/HIVE-19048 https://github.com/apache/hive/commit/006bf8a1a49aabf9930744c977fb0b9c91a5ef6c ### _How was this patch tested?_ - [x] Add some test cases that check the changes thoroughly including negative and positive cases if possible - [ ] Add screenshots for manual tests if appropriate - [x] [Run test](https://kyuubi.readthedocs.io/en/master/develop_tools/testing.html#running-tests) locally before make a pull request Closes #4962 from turboFei/beeline_exit. Closes #4962 85f497ad2 [fwang12] add ut 21e22cf6f [fwang12] save 49432c256 [fwang12] save 69f90ae37 [fwang12] save b1afa9e26 [fwang12] backport Authored-by: fwang12 <[email protected]> Signed-off-by: Cheng Pan <[email protected]> --- .../org/apache/hive/beeline/KyuubiBeeLine.java | 58 ++++++++++++++++++++++ .../org/apache/hive/beeline/KyuubiCommands.java | 11 +++- .../org/apache/hive/beeline/KyuubiBeeLineTest.java | 18 +++++++ kyuubi-hive-beeline/src/test/resources/test.sql | 17 +++++++ 4 files changed, 103 insertions(+), 1 deletion(-) 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 7ca767148..3908a44f1 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 @@ -36,6 +36,11 @@ public class KyuubiBeeLine extends BeeLine { protected KyuubiCommands commands = new KyuubiCommands(this); private Driver defaultDriver = null; + // copied from org.apache.hive.beeline.BeeLine + private static final int ERRNO_OK = 0; + private static final int ERRNO_ARGS = 1; + private static final int ERRNO_OTHER = 2; + public KyuubiBeeLine() { this(true); } @@ -160,6 +165,11 @@ public class KyuubiBeeLine extends BeeLine { } } + // see HIVE-19048 : InitScript errors are ignored + if (exit) { + return 1; + } + int code = 0; if (cl.getOptionValues('e') != null) { commands = Arrays.asList(cl.getOptionValues('e')); @@ -192,4 +202,52 @@ public class KyuubiBeeLine extends BeeLine { } return code; } + + // see HIVE-19048 : Initscript errors are ignored + @Override + int runInit() { + String[] initFiles = getOpts().getInitFiles(); + + // executionResult will be ERRNO_OK only if all initFiles execute successfully + int executionResult = ERRNO_OK; + boolean exitOnError = !getOpts().getForce(); + Field exitField = null; + + if (initFiles != null && initFiles.length != 0) { + for (String initFile : initFiles) { + info("Running init script " + initFile); + try { + int currentResult; + try { + Method executeFileMethod = BeeLine.class.getDeclaredMethod("executeFile", String.class); + executeFileMethod.setAccessible(true); + currentResult = (int) executeFileMethod.invoke(null, initFile); + exitField = BeeLine.class.getDeclaredField("exit"); + exitField.setAccessible(true); + } catch (Exception t) { + error(t.getMessage()); + currentResult = ERRNO_OTHER; + } + + if (currentResult != ERRNO_OK) { + executionResult = currentResult; + + if (exitOnError) { + return executionResult; + } + } + } finally { + // exit beeline if there is initScript failure and --force is not set + boolean exit = exitOnError && executionResult != ERRNO_OK; + try { + exitField.set(this, exit); + } catch (Exception t) { + error(t.getMessage()); + return ERRNO_OTHER; + } + } + } + } + return executionResult; + } } diff --git a/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiCommands.java b/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiCommands.java index 9557f2567..6580d2490 100644 --- a/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiCommands.java +++ b/kyuubi-hive-beeline/src/main/java/org/apache/hive/beeline/KyuubiCommands.java @@ -489,7 +489,16 @@ public class KyuubiCommands extends Commands { if (!beeLine.isBeeLine()) { beeLine.updateOptsForCli(); } - beeLine.runInit(); + + // see HIVE-19048 : Initscript errors are ignored + int initScriptExecutionResult = beeLine.runInit(); + + // if execution of the init script(s) return anything other than ERRNO_OK from beeline + // exit beeline with error unless --force is set + if (initScriptExecutionResult != 0 && !beeLine.getOpts().getForce()) { + return beeLine.error("init script execution failed."); + } + if (beeLine.getOpts().getInitFiles() != null) { beeLine.initializeConsoleReader(null); } 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 b144c95c6..96b76373d 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 @@ -29,4 +29,22 @@ public class KyuubiBeeLineTest { int result = kyuubiBeeLine.initArgs(new String[0]); assertEquals(0, result); } + + @Test + public void testKyuubiBeelineExitCodeWithoutConnection() { + KyuubiBeeLine kyuubiBeeLine = new KyuubiBeeLine(); + String scriptFile = getClass().getClassLoader().getResource("test.sql").getFile(); + + String[] args1 = {"-u", "badUrl", "-e", "show tables"}; + int result1 = kyuubiBeeLine.initArgs(args1); + assertEquals(1, result1); + + String[] args2 = {"-u", "badUrl", "-f", scriptFile}; + int result2 = kyuubiBeeLine.initArgs(args2); + assertEquals(1, result2); + + String[] args3 = {"-u", "badUrl", "-i", scriptFile}; + int result3 = kyuubiBeeLine.initArgs(args3); + assertEquals(1, result3); + } } diff --git a/kyuubi-hive-beeline/src/test/resources/test.sql b/kyuubi-hive-beeline/src/test/resources/test.sql new file mode 100644 index 000000000..c7c3ee2f9 --- /dev/null +++ b/kyuubi-hive-beeline/src/test/resources/test.sql @@ -0,0 +1,17 @@ +-- Licensed to the Apache Software Foundation (ASF) under one or more +-- contributor license agreements. See the NOTICE file distributed with +-- this work for additional information regarding copyright ownership. +-- The ASF licenses this file to You under the Apache License, Version 2.0 +-- (the "License"); you may not use this file except in compliance with +-- the License. You may obtain a copy of the License at +-- +-- http://www.apache.org/licenses/LICENSE-2.0 +-- +-- Unless required by applicable law or agreed to in writing, software +-- distributed under the License is distributed on an "AS IS" BASIS, +-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +-- See the License for the specific language governing permissions and +-- limitations under the License. +-- + +show tables;
