@Override public String toString() { return "ZookeeperServer{" + getConnection() + "}"; } }
/** * Perform the supplied function on each directory used by this server. * * @param consumer the consumer function; may not be null */ void onEachDirectory(Consumer<File> consumer) { consumer.accept(getSnapshotDirectory()); consumer.accept(getLogDirectory()); }
/** * Get the Zookeeper port. * * @return the Zookeeper port */ public int zkPort() { return zkServer.getPort(); }
@Test public void shouldStartServerAndRemoveData() throws Exception { Testing.debug("Running 1"); server.startup(); server.onEachDirectory(this::assertValidDataDirectory); server.shutdown(true); server.onEachDirectory(this::assertDoesNotExist); }
@Before public void beforeEach() { dataDir = Testing.Files.createTestingDirectory("zk"); Testing.Files.delete(dataDir); server = new ZookeeperServer(); server.setStateDirectory(dataDir); }
zkServer.setStateDirectory(zkDir); // does error checking this.dataDir = dataDir; File kafkaDir = new File(dataDir, "kafka"); kafkaServers.values().forEach(server -> server.setStateDirectory(new File(kafkaDir, "broker" + server.brokerId()))); zkServer.startup(); LOGGER.debug("Starting {} brokers", kafkaServers.size()); kafkaServers.values().forEach(KafkaServer::startup);
/** * Shutdown the embedded Zookeeper server and delete all data. */ public void shutdown() { shutdown(true); }
/** * Set the port numbers for Zookeeper and the Kafka brokers. * * @param zkPort the port number that Zookeeper should use; may be -1 if an available port should be discovered * @param firstKafkaPort the port number for the first Kafka broker (additional brokers will use subsequent port numbers); * may be -1 if available ports should be discovered * @return this instance to allow chaining methods; never null * @throws IllegalStateException if the cluster is running */ public KafkaCluster withPorts(int zkPort, int firstKafkaPort) { if (running) throw new IllegalStateException("Unable to add a broker when the cluster is already running"); this.zkServer.setPort(zkPort); this.startingKafkaPort = firstKafkaPort; if (this.startingKafkaPort >= 0) { this.nextKafkaPort.set(this.startingKafkaPort); kafkaServers.values().forEach(kafka -> kafka.setPort((int) this.nextKafkaPort.getAndIncrement())); } return this; }
@Test public void shouldStartServerAndLeaveData() throws Exception { Testing.debug("Running 2"); server.startup(); server.onEachDirectory(this::assertValidDataDirectory); server.shutdown(false); server.onEachDirectory(this::assertValidDataDirectory); }
} finally { try { zkServer.shutdown(deleteDataUponShutdown); } catch (Throwable t) { LOGGER.error("Error while shutting down {}", zkServer, t);
/** * Perform the supplied function on each directory used by this cluster. * * @param consumer the consumer function; may not be null */ void onEachDirectory(java.util.function.Consumer<File> consumer) { consumer.accept(zkServer.getSnapshotDirectory()); consumer.accept(zkServer.getLogDirectory()); kafkaServers.values().forEach(server -> consumer.accept(server.getStateDirectory())); }