/** * Resumes ZooKeeper (if it had previously been suspended). * * @throws Exception If an exception got thrown. */ public void resumeZooKeeper() throws Exception { val zk = new ZooKeeperServiceRunner(this.zkPort, this.secureZK, this.tLSKeyStore, this.tLSKeyStorePasswordPath, this.tlsTrustStore); if (this.zkServer.compareAndSet(null, zk)) { // Initialize ZK runner (since nobody else did it for us). zk.initialize(); log.info("ZooKeeper initialized."); } else { zk.close(); } // Start or resume ZK. this.zkServer.get().start(); log.info("ZooKeeper resumed."); }
public static boolean waitForServerUp(int zkPort) { return waitForServerUp(zkPort, false, "", "", "", ""); }
@Override public void close() throws Exception { stop(); File t = this.tmpDir.getAndSet(null); if (t != null) { log.info("Cleaning up " + t); FileUtils.deleteDirectory(t); } }
private void startLocalZK() throws Exception { zkService = new ZooKeeperServiceRunner(zkPort, secureZK, jksKeyFile, keyPasswordFile, jksTrustFile); zkService.initialize(); zkService.start(); }
@Override public void close() throws Exception { try { this.servers.stream().filter(Objects::nonNull).forEach(BookieServer::shutdown); if (this.zkServer.get() != null) { this.zkServer.get().close(); } } finally { cleanupDirectories(); } Thread c = this.cleanup.getAndSet(null); if (c != null) { Runtime.getRuntime().removeShutdownHook(c); } }
try { SSLContext context = SSLContext.getInstance("TLS"); TrustManagerFactory trustManager = getTrustManager(trustStore, trustStorePasswordPath); KeyManagerFactory keyFactory = getKeyManager(keyStore, keyStorePasswdPath); context.init(keyFactory.getKeyManagers(), trustManager.getTrustManagers(), null);
/** * Blocks the current thread and awaits ZooKeeper to start running locally on the given port. * * @param zkPort The ZooKeeper Port. * @param secureZk Flag to notify whether the ZK is secure. * @param trustStore Location of the trust store. * @param keyStore Location of the key store. * @param keyStorePasswordPath Location of password path for key store. * Empty string if `secureZk` is false or a password does not exist. * @param trustStorePasswordPath Location of password path for trust store. * Empty string if `secureZk` is false or a password does not exist. * @return True if ZooKeeper started within a specified timeout, false otherwise. */ public static boolean waitForServerUp(int zkPort, boolean secureZk, String trustStore, String keyStore, String keyStorePasswordPath, String trustStorePasswordPath) { val address = LOOPBACK_ADDRESS + ":" + zkPort; if (secureZk) { return waitForSSLServerUp(address, LocalBookKeeper.CONNECTION_TIMEOUT, trustStore, keyStore, keyStorePasswordPath, trustStorePasswordPath); } else { return LocalBookKeeper.waitForServerUp(address, LocalBookKeeper.CONNECTION_TIMEOUT); } }
ZooKeeperServiceRunner runner = new ZooKeeperServiceRunner(zkPort, secureZK, zkKeyStore, zkKeyStorePasswd, zkTrustStore); runner.initialize(); runner.start(); Thread.sleep(Long.MAX_VALUE);
@Override @Synchronized public void close() throws Exception { if (isInProcSegmentStore) { for ( ServiceStarter starter : this.nodeServiceStarter ) { starter.shutdown(); } } if (isInProcController) { for ( ControllerServiceMain controller : this.controllerServers ) { controller.stopAsync(); } } if (this.zkService != null) { this.zkService.close(); this.zkService = null; } } }
/** * Starts the ZooKeeper Service in process. * * @throws Exception If an exception occurred. */ public void start() throws Exception { Preconditions.checkState(this.tmpDir.get() != null, "Not Initialized."); val s = new ZooKeeperServer(this.tmpDir.get(), this.tmpDir.get(), ZooKeeperServer.DEFAULT_TICK_TIME); if (!this.server.compareAndSet(null, s)) { s.shutdown(); throw new IllegalStateException("Already started."); } this.serverFactory.set(NettyServerCnxnFactory.createFactory()); val address = LOOPBACK_ADDRESS + ":" + this.zkPort; log.info("Starting Zookeeper server at " + address + " ..."); this.serverFactory.get().configure(new InetSocketAddress(LOOPBACK_ADDRESS, this.zkPort), 1000, secureZK); this.serverFactory.get().startup(s); if (!waitForServerUp(this.zkPort, this.secureZK, this.trustStore, this.keyStore, this.keyStorePassword, this.keyStorePassword)) { throw new IllegalStateException("ZooKeeper server failed to start"); } }
/** * Suspends (stops) ZooKeeper, without destroying its underlying data (so a subsequent resume can pick up from the * state where it left off). */ public void suspendZooKeeper() { val zk = this.zkServer.get(); Preconditions.checkState(zk != null, "ZooKeeper not started."); // Stop, but do not close, the ZK runner. zk.stop(); log.info("ZooKeeper suspended."); }
private void startZooKeeper() throws Exception { Preconditions.checkState(this.zooKeeperProcess.get() == null, "ZooKeeper is already started."); this.zooKeeperProcess.set(ProcessStarter .forClass(ZooKeeperServiceRunner.class) .sysProp(ZooKeeperServiceRunner.PROPERTY_ZK_PORT, this.testConfig.getZkPort()) .stdOut(ProcessBuilder.Redirect.to(new File(this.testConfig.getComponentOutLogPath("zk", 0)))) .stdErr(ProcessBuilder.Redirect.to(new File(this.testConfig.getComponentErrLogPath("zk", 0)))) .start()); if (!ZooKeeperServiceRunner.waitForServerUp(this.testConfig.getZkPort())) { throw new RuntimeException("Unable to start ZooKeeper at port " + this.testConfig.getZkPort()); } log("ZooKeeper started (Port = %s).", this.testConfig.getZkPort()); }
/** * Starts a BookKeeper (using a number of bookies) along with a ZooKeeper out-of-process. * * @param config The Test Config to use. This indicates the BK Port(s), ZK Port, as well as Bookie counts. * @param logId A String to use for logging purposes. * @return A Process referring to the newly started Bookie process. * @throws IOException If an error occurred. */ static Process startBookKeeperOutOfProcess(TestConfig config, String logId) throws IOException { int bookieCount = config.getBookieCount(); Process p = ProcessStarter .forClass(BookKeeperServiceRunner.class) .sysProp(BookKeeperServiceRunner.PROPERTY_BASE_PORT, config.getBkPort(0)) .sysProp(BookKeeperServiceRunner.PROPERTY_BOOKIE_COUNT, bookieCount) .sysProp(BookKeeperServiceRunner.PROPERTY_ZK_PORT, config.getZkPort()) .sysProp(BookKeeperServiceRunner.PROPERTY_LEDGERS_PATH, TestConfig.BK_LEDGER_PATH) .sysProp(BookKeeperServiceRunner.PROPERTY_START_ZK, true) .stdOut(ProcessBuilder.Redirect.to(new File(config.getComponentOutLogPath("bk", 0)))) .stdErr(ProcessBuilder.Redirect.to(new File(config.getComponentErrLogPath("bk", 0)))) .start(); ZooKeeperServiceRunner.waitForServerUp(config.getZkPort()); TestLogger.log(logId, "Zookeeper (Port %s) and BookKeeper (Ports %s-%s) started.", config.getZkPort(), config.getBkPort(0), config.getBkPort(bookieCount - 1)); return p; } }