Index: metastore/src/test/org/apache/hadoop/hive/metastore/TestMetaStoreEndFunctionListener.java =================================================================== --- metastore/src/test/org/apache/hadoop/hive/metastore/TestMetaStoreEndFunctionListener.java (revision 1419361) +++ metastore/src/test/org/apache/hadoop/hive/metastore/TestMetaStoreEndFunctionListener.java (working copy) @@ -28,29 +28,16 @@ import org.apache.hadoop.hive.metastore.api.NoSuchObjectException; import org.apache.hadoop.hive.ql.Driver; import org.apache.hadoop.hive.ql.session.SessionState; +import org.apache.hadoop.hive.shims.ShimLoader; /** * TestMetaStoreEventListener. Test case for * {@link org.apache.hadoop.hive.metastore.MetaStoreEndFunctionListener} */ public class TestMetaStoreEndFunctionListener extends TestCase { - private static final String msPort = "20002"; private HiveConf hiveConf; private HiveMetaStoreClient msc; private Driver driver; - private static class RunMS implements Runnable { - - @Override - public void run() { - try { - HiveMetaStore.main(new String[]{msPort}); - } catch (Throwable e) { - e.printStackTrace(System.err); - assert false; - } - } - } - @Override protected void setUp() throws Exception { @@ -61,11 +48,11 @@ DummyPreListener.class.getName()); System.setProperty("hive.metastore.end.function.listeners", DummyEndFunctionListener.class.getName()); - Thread t = new Thread(new RunMS()); - t.start(); - Thread.sleep(40000); + int port = MetaStoreUtils.findFreePort(); + MetaStoreUtils.startMetaStore(port, ShimLoader.getHadoopThriftAuthBridge()); + hiveConf = new HiveConf(this.getClass()); - hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, "thrift://localhost:" + msPort); + hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, "thrift://localhost:" + port); hiveConf.setIntVar(HiveConf.ConfVars.METASTORETHRIFTCONNECTIONRETRIES, 3); hiveConf.set(HiveConf.ConfVars.PREEXECHOOKS.varname, ""); hiveConf.set(HiveConf.ConfVars.POSTEXECHOOKS.varname, ""); Index: metastore/src/test/org/apache/hadoop/hive/metastore/TestMetaStoreEventListener.java =================================================================== --- metastore/src/test/org/apache/hadoop/hive/metastore/TestMetaStoreEventListener.java (revision 1419361) +++ metastore/src/test/org/apache/hadoop/hive/metastore/TestMetaStoreEventListener.java (working copy) @@ -27,7 +27,6 @@ import org.apache.hadoop.hive.cli.CliSessionState; import org.apache.hadoop.hive.conf.HiveConf; -import org.apache.hadoop.hive.conf.HiveConf.ConfVars; import org.apache.hadoop.hive.metastore.api.Database; import org.apache.hadoop.hive.metastore.api.Partition; import org.apache.hadoop.hive.metastore.api.PartitionEventType; @@ -54,6 +53,7 @@ import org.apache.hadoop.hive.metastore.events.PreLoadPartitionDoneEvent; import org.apache.hadoop.hive.ql.Driver; import org.apache.hadoop.hive.ql.session.SessionState; +import org.apache.hadoop.hive.shims.ShimLoader; /** * TestMetaStoreEventListener. Test case for @@ -61,7 +61,6 @@ * {@link org.apache.hadoop.hive.metastore.MetaStorePreEventListener} */ public class TestMetaStoreEventListener extends TestCase { - private static final String msPort = "20001"; private HiveConf hiveConf; private HiveMetaStoreClient msc; private Driver driver; @@ -70,19 +69,6 @@ private static final String tblName = "tmptbl"; private static final String renamed = "tmptbl2"; - private static class RunMS implements Runnable { - - @Override - public void run() { - try { - HiveMetaStore.main(new String[]{msPort}); - } catch (Throwable e) { - e.printStackTrace(System.err); - assert false; - } - } - } - @Override protected void setUp() throws Exception { @@ -93,12 +79,11 @@ System.setProperty("hive.metastore.pre.event.listeners", DummyPreListener.class.getName()); - Thread t = new Thread(new RunMS()); - t.start(); - Thread.sleep(40000); + int port = MetaStoreUtils.findFreePort(); + MetaStoreUtils.startMetaStore(port, ShimLoader.getHadoopThriftAuthBridge()); hiveConf = new HiveConf(this.getClass()); - hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, "thrift://localhost:" + msPort); + hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, "thrift://localhost:" + port); hiveConf.setIntVar(HiveConf.ConfVars.METASTORETHRIFTCONNECTIONRETRIES, 3); hiveConf.set(HiveConf.ConfVars.PREEXECHOOKS.varname, ""); hiveConf.set(HiveConf.ConfVars.POSTEXECHOOKS.varname, ""); Index: metastore/src/test/org/apache/hadoop/hive/metastore/TestSetUGIOnOnlyServer.java =================================================================== --- metastore/src/test/org/apache/hadoop/hive/metastore/TestSetUGIOnOnlyServer.java (revision 1419361) +++ metastore/src/test/org/apache/hadoop/hive/metastore/TestSetUGIOnOnlyServer.java (working copy) @@ -21,8 +21,8 @@ public class TestSetUGIOnOnlyServer extends TestSetUGIOnBothClientServer { @Override - protected void createClient(boolean setugi) throws Exception { + protected void createClient(boolean setugi, int port) throws Exception { // It is turned on for both client and server because of super class. Turn it off for client. - super.createClient(false); + super.createClient(false, port); } } Index: metastore/src/test/org/apache/hadoop/hive/metastore/TestRemoteHiveMetaStoreIpAddress.java =================================================================== --- metastore/src/test/org/apache/hadoop/hive/metastore/TestRemoteHiveMetaStoreIpAddress.java (revision 1419361) +++ metastore/src/test/org/apache/hadoop/hive/metastore/TestRemoteHiveMetaStoreIpAddress.java (working copy) @@ -18,14 +18,11 @@ package org.apache.hadoop.hive.metastore; -import java.io.IOException; -import java.net.ServerSocket; - import junit.framework.TestCase; import org.apache.hadoop.hive.conf.HiveConf; -import org.apache.hadoop.hive.conf.HiveConf.ConfVars; import org.apache.hadoop.hive.metastore.api.Database; +import org.apache.hadoop.hive.shims.ShimLoader; import org.apache.hadoop.util.StringUtils; /** @@ -40,50 +37,23 @@ private static HiveConf hiveConf; private static HiveMetaStoreClient msc; - private String port; - - private static class RunMS implements Runnable { - String port; - - public RunMS(String port) { - this.port = port; - } - - @Override - public void run() { - try { - System.setProperty(ConfVars.METASTORE_EVENT_LISTENERS.varname, - IpAddressListener.class.getName()); - HiveMetaStore.main(new String[] { port }); - } catch (Throwable e) { - e.printStackTrace(System.err); - assert false; - } - } - - } - @Override protected void setUp() throws Exception { super.setUp(); hiveConf = new HiveConf(this.getClass()); - port = findFreePort(); if (isServerStarted) { assertNotNull("Unable to connect to the MetaStore server", msc); return; } + int port = MetaStoreUtils.findFreePort(); System.out.println("Starting MetaStore Server on port " + port); - Thread t = new Thread(new RunMS(port)); - t.start(); + MetaStoreUtils.startMetaStore(port, ShimLoader.getHadoopThriftAuthBridge()); isServerStarted = true; - // Wait a little bit for the metastore to start. Should probably have - // a better way of detecting if the metastore has started? - Thread.sleep(5000); // This is default case with setugi off for both client and server - createClient(); + createClient(port); } public void testIpAddress() throws Exception { @@ -100,15 +70,8 @@ } } - protected void createClient() throws Exception { + protected void createClient(int port) throws Exception { hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, "thrift://localhost:" + port); msc = new HiveMetaStoreClient(hiveConf); } - - private String findFreePort() throws IOException { - ServerSocket socket= new ServerSocket(0); - int port = socket.getLocalPort(); - socket.close(); - return String.valueOf(port); - } } Index: metastore/src/test/org/apache/hadoop/hive/metastore/TestRemoteHiveMetaStore.java =================================================================== --- metastore/src/test/org/apache/hadoop/hive/metastore/TestRemoteHiveMetaStore.java (revision 1419361) +++ metastore/src/test/org/apache/hadoop/hive/metastore/TestRemoteHiveMetaStore.java (working copy) @@ -20,10 +20,10 @@ import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.conf.HiveConf.ConfVars; +import org.apache.hadoop.hive.shims.ShimLoader; public class TestRemoteHiveMetaStore extends TestHiveMetaStore { - protected static final String METASTORE_PORT = "29083"; private static boolean isServerStarted = false; public TestRemoteHiveMetaStore() { @@ -31,20 +31,6 @@ isThriftClient = true; } - private static class RunMS implements Runnable { - - @Override - public void run() { - try { - HiveMetaStore.main(new String[] { METASTORE_PORT }); - } catch (Throwable e) { - e.printStackTrace(System.err); - assert false; - } - } - - } - @Override protected void setUp() throws Exception { super.setUp(); @@ -54,20 +40,17 @@ return; } - System.out.println("Starting MetaStore Server on port " + METASTORE_PORT); - Thread t = new Thread(new RunMS()); - t.start(); + int port = MetaStoreUtils.findFreePort(); + System.out.println("Starting MetaStore Server on port " + port); + MetaStoreUtils.startMetaStore(port, ShimLoader.getHadoopThriftAuthBridge()); isServerStarted = true; - // Wait a little bit for the metastore to start. Should probably have - // a better way of detecting if the metastore has started? - Thread.sleep(5000); // This is default case with setugi off for both client and server - createClient(false); + createClient(false, port); } - protected void createClient(boolean setugi) throws Exception { - hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, "thrift://localhost:" + METASTORE_PORT); + protected void createClient(boolean setugi, int port) throws Exception { + hiveConf.setVar(HiveConf.ConfVars.METASTOREURIS, "thrift://localhost:" + port); hiveConf.setBoolVar(ConfVars.METASTORE_EXECUTE_SET_UGI,setugi); client = new HiveMetaStoreClient(hiveConf); } Index: metastore/src/test/org/apache/hadoop/hive/metastore/TestSetUGIOnOnlyClient.java =================================================================== --- metastore/src/test/org/apache/hadoop/hive/metastore/TestSetUGIOnOnlyClient.java (revision 1419361) +++ metastore/src/test/org/apache/hadoop/hive/metastore/TestSetUGIOnOnlyClient.java (working copy) @@ -21,8 +21,8 @@ public class TestSetUGIOnOnlyClient extends TestRemoteHiveMetaStore{ @Override - protected void createClient(boolean setugi) throws Exception { + protected void createClient(boolean setugi, int port) throws Exception { // turn it on for client. - super.createClient(true); + super.createClient(true, port); } } Index: metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreUtils.java =================================================================== --- metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreUtils.java (revision 1419361) +++ metastore/src/java/org/apache/hadoop/hive/metastore/MetaStoreUtils.java (working copy) @@ -22,6 +22,7 @@ import java.io.IOException; import java.lang.reflect.Constructor; import java.net.InetSocketAddress; +import java.net.ServerSocket; import java.net.Socket; import java.util.ArrayList; import java.util.HashMap; @@ -40,12 +41,12 @@ import org.apache.hadoop.fs.Path; import org.apache.hadoop.hive.common.JavaUtils; import org.apache.hadoop.hive.conf.HiveConf; -import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants; import org.apache.hadoop.hive.metastore.api.FieldSchema; import org.apache.hadoop.hive.metastore.api.MetaException; import org.apache.hadoop.hive.metastore.api.SerDeInfo; import org.apache.hadoop.hive.metastore.api.StorageDescriptor; import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.hadoop.hive.metastore.api.hive_metastoreConstants; import org.apache.hadoop.hive.serde2.Deserializer; import org.apache.hadoop.hive.serde2.SerDeException; import org.apache.hadoop.hive.serde2.SerDeUtils; @@ -801,6 +802,19 @@ } /** + * Finds a free port on the machine. + * + * @return + * @throws IOException + */ + public static int findFreePort() throws IOException { + ServerSocket socket= new ServerSocket(0); + int port = socket.getLocalPort(); + socket.close(); + return port; + } + + /** * Catches exceptions that can't be handled and bundles them to MetaException * * @param e