public static int startMetaStoreWithRetry(HadoopThriftAuthBridge bridge, Configuration conf, boolean keepJdbcUri,
      boolean keepWarehousePath, boolean withHouseKeepingThreads, boolean waitForHouseKeepers,
                                            boolean createTransactionalTables) throws Exception {
    Exception metaStoreException = null;
    String warehouseDir = MetastoreConf.getVar(conf, ConfVars.WAREHOUSE);

    for (int tryCount = 0; tryCount < MetaStoreTestUtils.RETRY_COUNT; tryCount++) {
      try {
        int metaStorePort = findFreePort();
        if (!keepWarehousePath) {
          // Setting metastore instance specific warehouse directory, postfixing with port
          Path postfixedWarehouseDir = new Path(warehouseDir, String.valueOf(metaStorePort));
          MetastoreConf.setVar(conf, ConfVars.WAREHOUSE, postfixedWarehouseDir.toString());
          warehouseDir = postfixedWarehouseDir.toString();
        }

        String jdbcUrl = MetastoreConf.getVar(conf, ConfVars.CONNECT_URL_KEY);
        if (!keepJdbcUri) {
          // Setting metastore instance specific jdbc url postfixed with port
          jdbcUrl = "jdbc:derby:memory:" + TMP_DIR + File.separator
              + MetaStoreServerUtils.JUNIT_DATABASE_PREFIX + "_" + metaStorePort + ";create=true";
          MetastoreConf.setVar(conf, ConfVars.CONNECT_URL_KEY, jdbcUrl);
        }

        // Setting metastore instance specific metastore uri, if dynamic metastore service
        // discovery is not enabled.
        if (MetastoreConf.getVar(conf, ConfVars.THRIFT_SERVICE_DISCOVERY_MODE).trim().isEmpty()) {
          MetastoreConf.setVar(conf, ConfVars.THRIFT_URIS, "thrift://localhost:" + metaStorePort);
        }

        if (createTransactionalTables) {
          // Some tests may have dummy txn manager. There is no harm in creating the transactional tables but
          // we should not change the test config in case they purposefully do not use transactions
          Configuration txnInitConf = new Configuration(conf);
          TestTxnDbUtil.setConfValues(txnInitConf);
          TestTxnDbUtil.prepDb(txnInitConf);
        }

        MetaStoreTestUtils.startMetaStore(metaStorePort, bridge, conf, withHouseKeepingThreads, waitForHouseKeepers);

        // Creating warehouse dir, if not exists
        Warehouse wh = new Warehouse(conf);
        if (!wh.isDir(wh.getWhRoot())) {
          FileSystem fs = wh.getWhRoot().getFileSystem(conf);
          fs.mkdirs(wh.getWhRoot());
          fs.setPermission(wh.getWhRoot(),
              new FsPermission(FsAction.ALL, FsAction.ALL, FsAction.ALL));
          LOG.info("MetaStore warehouse root dir ({}) is created", warehouseDir);
        }

        LOG.info("MetaStore Thrift Server started on port: {} with warehouse dir: {} with " +
            "jdbcUrl: {}", metaStorePort, warehouseDir, jdbcUrl);

        return metaStorePort;
      } catch (ConnectException ce) {
        metaStoreException = ce;
      }
    }
    throw metaStoreException;
  }
Starts a MetaStore instance with the given configuration and given bridge.
Tries to find a free port, and use it. If failed tries another port so the tests will not
fail if run parallel. Also adds the port to the warehouse dir, so the multiple MetaStore
instances will use different warehouse directories.
@param bridge The Thrift bridge to uses
@param conf The configuration to use
@param keepJdbcUri If set to true, then the JDBC url is not changed
@param keepWarehousePath If set to true, then the Warehouse directory is not changed
@param withHouseKeepingThreads Start the housekeeping background threads in the metastore
@param waitForHouseKeepers Wait until the background threads are scheduled
@return The port on which the MetaStore finally started
@throws Exception Timeout after 60 seconds
standalone-metastore/metastore-server/src/test/java/org/apache/hadoop/hive/metastore/MetaStoreTestUtils.java