@SneakyThrows private void cleanupOnShutdown() { close(); }
@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); } }
/** * Starts the BookKeeper cluster in-process. * * @throws Exception If an exception occurred. */ public void startAll() throws Exception { // Make sure the child processes and any created files get killed/deleted if the process is terminated. this.cleanup.set(new Thread(this::cleanupOnShutdown)); Runtime.getRuntime().addShutdownHook(this.cleanup.get()); if (this.startZk) { resumeZooKeeper(); } initializeZookeeper(); runBookies(); }
private void runBookies() throws Exception { log.info("Starting Bookie(s) ..."); for (int bkPort : this.bookiePorts) { this.servers.add(runBookie(bkPort)); } }
@SneakyThrows private static void restartFirstBookie() { BK_SERVICE.get().startBookie(0); }
/** * Start BookKeeper once for the duration of this class. This is pretty strenuous, so in the interest of running time * we only do it once. */ public static void setUpBookKeeper(boolean secure) throws Exception { // Pick a random port to reduce chances of collisions during concurrent test executions. SECURE_BK.set(secure); BK_PORT.set(TestUtils.getAvailableListenPort()); val bookiePorts = new ArrayList<Integer>(); for (int i = 0; i < BOOKIE_COUNT; i++) { bookiePorts.add(TestUtils.getAvailableListenPort()); } val runner = BookKeeperServiceRunner.builder() .startZk(true) .zkPort(BK_PORT.get()) .ledgersPath("/pravega/bookkeeper/ledgers") .secureBK(isSecure()) .secureZK(isSecure()) .tlsTrustStore("../../../config/bookie.truststore.jks") .tLSKeyStore("../../../config/bookie.keystore.jks") .tLSKeyStorePasswordPath("../../../config/bookie.keystore.jks.passwd") .bookiePorts(bookiePorts) .build(); runner.startAll(); BK_SERVICE.set(runner); }
/** * Restarts the BookieService with the given index. * * @param bookieIndex The index of the bookie to restart. * @throws Exception If an exception occurred. */ public void startBookie(int bookieIndex) throws Exception { Preconditions.checkState(this.servers.size() > 0, "No Bookies initialized. Call startAll()."); Preconditions.checkState(this.servers.get(0) == null, "Bookie already running."); this.servers.set(bookieIndex, runBookie(this.bookiePorts.get(bookieIndex))); log.info("Bookie {} stopped.", bookieIndex); }
@Override public void close() throws Exception { // BookKeeper val bk = this.bkRunner; if (bk != null) { bk.close(); this.bkRunner = null; } val zk = this.zkClient; if (zk != null) { zk.close(); this.zkClient = null; } } }
@AfterClass public static void tearDownBookKeeper() throws Exception { val process = BK_SERVICE.getAndSet(null); if (process != null) { process.close(); } }