diff --git a/ql/src/test/org/apache/hadoop/hive/ql/session/TestSessionState.java b/ql/src/test/org/apache/hadoop/hive/ql/session/TestSessionState.java index 45ba07e..45ad22a 100644 --- a/ql/src/test/org/apache/hadoop/hive/ql/session/TestSessionState.java +++ b/ql/src/test/org/apache/hadoop/hive/ql/session/TestSessionState.java @@ -20,13 +20,16 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import java.io.BufferedReader; import java.io.File; import java.io.IOException; +import java.io.InputStreamReader; import java.lang.reflect.Method; import java.util.Arrays; import java.util.Collection; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.apache.hadoop.hive.conf.HiveConf; @@ -48,13 +51,21 @@ */ @RunWith(value = Parameterized.class) public class TestSessionState { - private final boolean prewarm; - private final static String clazzDistFileName = "SessionStateTest.jar.v1"; - private final static String clazzV2FileName = "SessionStateTest.jar.v2"; - private final static String reloadClazzFileName = "reloadingClazz.jar"; - private final static String reloadClazzName = "org.apache.test.RefreshedJarClass"; + private final static String clazzDistFileName = "RefreshedJarClass.jar.V1"; + private final static String clazzV2FileName = "RefreshedJarClass.jar.V2"; + private final static String reloadClazzFileName = "RefreshedJarClass.jar"; private final static String versionMethodName = "version"; + private final static String RELOADED_CLAZZ_PREFIX_NAME = "RefreshedJarClass"; + private final static String JAVA_FILE_EXT = ".java"; + private final static String CLAZZ_FILE_EXT = ".class"; + private final static String JAR_FILE_EXT = ".jar"; + private final static String TXT_FILE_EXT = ".txt"; + private final static String V1 = "V1"; + private final static String V2 = "V2"; + private final String clazzFile = RELOADED_CLAZZ_PREFIX_NAME + CLAZZ_FILE_EXT; + private final String jarFile = RELOADED_CLAZZ_PREFIX_NAME + JAR_FILE_EXT; + private final String javaFile = RELOADED_CLAZZ_PREFIX_NAME + JAVA_FILE_EXT; private static String hiveReloadPath; private File reloadFolder; public static final Log LOG = LogFactory.getLog(TestSessionState.class); @@ -83,6 +94,13 @@ public void setUp() { reloadFolder.mkdir(); } + try { + generateRefreshJarFiles(V2); + generateRefreshJarFiles(V1); + } catch (Throwable e) { + Assert.fail("fail to generate refresh jar file due to the error " + e); + } + if (prewarm) { HiveConf.setBoolVar(conf, ConfVars.HIVE_PREWARM_ENABLED, true); HiveConf.setIntVar(conf, ConfVars.HIVE_PREWARM_NUM_CONTAINERS, 1); @@ -167,11 +185,43 @@ public void testClassLoaderEquality() throws Exception { } private String getReloadedClazzVersion(ClassLoader cl) throws Exception { - Class addedClazz = Class.forName(reloadClazzName, true, cl); + Class addedClazz = Class.forName(RELOADED_CLAZZ_PREFIX_NAME, true, cl); Method versionMethod = addedClazz.getMethod(versionMethodName); return (String) versionMethod.invoke(addedClazz.newInstance()); } + private void generateRefreshJarFiles(String version) throws IOException, InterruptedException { + String u = HiveTestUtils.getFileFromClasspath( + RELOADED_CLAZZ_PREFIX_NAME + version + TXT_FILE_EXT); + File dir = new File(u); + File parentDir = dir.getParentFile(); + File f = new File(parentDir, javaFile); + Files.copy(dir, f); + executeCmd(new String[]{"javac", javaFile}, parentDir); + executeCmd(new String[]{"jar", "cf", jarFile, clazzFile}, parentDir); + Files.move(new File(parentDir, jarFile), new File(parentDir, jarFile + "." + version)); + f.delete(); + new File(parentDir, clazzFile).delete(); + } + + private void executeCmd(String[] cmdArr, File dir) throws IOException, InterruptedException { + final Process p1 = Runtime.getRuntime().exec(cmdArr, null, dir); + new Thread(new Runnable() { + public void run() { + BufferedReader input = new BufferedReader(new InputStreamReader(p1.getErrorStream())); + String line; + try { + while ((line = input.readLine()) != null) { + System.out.println(line); + } + } catch (IOException e) { + LOG.error("Failed to execute the command due the exception " + e); + } + } + }).start(); + p1.waitFor(); + } + @Test public void testReloadAuxJars2() { HiveConf conf = new HiveConf(); diff --git a/ql/src/test/resources/RefreshedJarClassV1.txt b/ql/src/test/resources/RefreshedJarClassV1.txt new file mode 100644 index 0000000..036b998 --- /dev/null +++ b/ql/src/test/resources/RefreshedJarClassV1.txt @@ -0,0 +1,26 @@ +/** + * 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. + */ +public class RefreshedJarClass { + public String version(){ + return "version1"; + } + + public static void main(String [] args){ + System.out.println(new RefreshedJarClass().version()); + } +} \ No newline at end of file diff --git a/ql/src/test/resources/RefreshedJarClassV2.txt b/ql/src/test/resources/RefreshedJarClassV2.txt new file mode 100644 index 0000000..c965265 --- /dev/null +++ b/ql/src/test/resources/RefreshedJarClassV2.txt @@ -0,0 +1,26 @@ +/** + * 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. + */ +public class RefreshedJarClass { + public String version(){ + return "version2"; + } + + public static void main(String [] args){ + System.out.println(new RefreshedJarClass().version()); + } +} \ No newline at end of file