Browse Source

AMBARI-13806. Multi statement queries with errors after the first statement cause orphaned AMs. (DIPAYAN BHOWMICK via Jaimin)

Jaimin Jetly 9 years ago
parent
commit
69db84a684

+ 1 - 1
contrib/views/hive/src/main/java/org/apache/ambari/view/hive/client/Connection.java

@@ -577,7 +577,7 @@ public class Connection {
   public TOperationHandle execute(final TSessionHandle session, final String cmd, final boolean async) throws HiveClientException {
   public TOperationHandle execute(final TSessionHandle session, final String cmd, final boolean async) throws HiveClientException {
     TOperationHandle handle = null;
     TOperationHandle handle = null;
 
 
-    String[] commands = cmd.split(";");
+    String[] commands = Utils.removeEmptyStrings(cmd.split(";"));
     for(int i=0; i<commands.length; i++) {
     for(int i=0; i<commands.length; i++) {
       final String oneCmd = commands[i];
       final String oneCmd = commands[i];
       final boolean lastCommand = i == commands.length-1;
       final boolean lastCommand = i == commands.length-1;

+ 14 - 0
contrib/views/hive/src/main/java/org/apache/ambari/view/hive/client/Utils.java

@@ -23,6 +23,7 @@ import org.apache.hive.service.cli.thrift.TStatusCode;
 import org.apache.http.client.CookieStore;
 import org.apache.http.client.CookieStore;
 import org.apache.http.cookie.Cookie;
 import org.apache.http.cookie.Cookie;
 
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.List;
 
 
 public class Utils {
 public class Utils {
@@ -60,6 +61,19 @@ public class Utils {
     return true;
     return true;
   }
   }
 
 
+  /**
+   * Removes the empty strings and returns back only the strings with content
+   */
+  static String[] removeEmptyStrings(String[] strs) {
+    List<String> nonEmptyStrings = new ArrayList<>();
+    for(String str : strs) {
+      if (!(str == null || str.trim().isEmpty())) {
+        nonEmptyStrings.add(str);
+      }
+    }
+    return nonEmptyStrings.toArray(new String[] {});
+  }
+
   public static class HiveAuthenticationParams {
   public static class HiveAuthenticationParams {
     public static final String AUTH_TYPE = "auth";
     public static final String AUTH_TYPE = "auth";
     // We're deprecating this variable's name.
     // We're deprecating this variable's name.

+ 35 - 0
contrib/views/hive/src/test/java/org/apache/ambari/view/hive/client/UtilsTest.java

@@ -0,0 +1,35 @@
+/**
+ * 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.
+ */
+package org.apache.ambari.view.hive.client;
+
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+public class UtilsTest {
+
+  @Test
+  public void testRemoveEmptyStrings() throws Exception {
+    String[] arrayWithSomeEmptyStrings = new String[] { "", null, "string1", null, "", "string2", "" };
+    String[] expectedStrings = Utils.removeEmptyStrings(arrayWithSomeEmptyStrings);
+
+    assertEquals(2, expectedStrings.length);
+    assertEquals("string1", expectedStrings[0]);
+    assertEquals("string2", expectedStrings[1]);
+  }
+}