in zookeeper-server/src/main/java/org/apache/zookeeper/server/embedded/ZooKeeperServerEmbeddedImpl.java [87:191]
public void start(long startupTimeout) throws Exception {
switch (exitHandler) {
case EXIT:
ServiceUtils.setSystemExitProcedure(ServiceUtils.SYSTEM_EXIT);
break;
case LOG_ONLY:
ServiceUtils.setSystemExitProcedure(ServiceUtils.LOG_ONLY);
break;
default:
ServiceUtils.setSystemExitProcedure(ServiceUtils.SYSTEM_EXIT);
break;
}
final CompletableFuture<String> started = new CompletableFuture<>();
if (config.getServers().size() > 1 || config.isDistributed()) {
LOG.info("Running ZK Server in single Quorum MODE");
maincluster = new QuorumPeerMain() {
protected QuorumPeer getQuorumPeer() throws SaslException {
return new QuorumPeer() {
@Override
public void start() {
super.start();
boundClientPort = getClientPort();
boundSecureClientPort = getSecureClientPort();
LOG.info("ZK Server {} started", this);
started.complete(null);
}
};
}
};
// Start and schedule the purge task
purgeMgr = new DatadirCleanupManager(config
.getDataDir(), config.getDataLogDir(), config
.getSnapRetainCount(), config.getPurgeIntervalInMs());
purgeMgr.start();
thread = new Thread("zkservermainrunner") {
@Override
public void run() {
try {
maincluster.runFromConfig(config);
maincluster.close();
LOG.info("ZK server died. Requesting stop on JVM");
if (!stopping) {
ServiceUtils.requestSystemExit(ExitCode.EXECUTION_FINISHED.getValue());
}
} catch (Throwable t) {
LOG.error("error during server lifecycle", t);
maincluster.close();
if (!stopping) {
ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue());
}
}
}
};
thread.start();
} else {
LOG.info("Running ZK Server in single STANDALONE MODE");
mainsingle = new ZooKeeperServerMain() {
@Override
public void serverStarted() {
LOG.info("ZK Server started");
boundClientPort = getClientPort();
boundSecureClientPort = getSecureClientPort();
started.complete(null);
}
};
purgeMgr = new DatadirCleanupManager(config
.getDataDir(), config.getDataLogDir(), config
.getSnapRetainCount(), config.getPurgeIntervalInMs());
purgeMgr.start();
thread = new Thread("zkservermainrunner") {
@Override
public void run() {
try {
ServerConfig cc = new ServerConfig();
cc.readFrom(config);
LOG.info("ZK server starting");
mainsingle.runFromConfig(cc);
LOG.info("ZK server died. Requesting stop on JVM");
if (!stopping) {
ServiceUtils.requestSystemExit(ExitCode.EXECUTION_FINISHED.getValue());
}
} catch (Throwable t) {
LOG.error("error during server lifecycle", t);
mainsingle.close();
if (!stopping) {
ServiceUtils.requestSystemExit(ExitCode.INVALID_INVOCATION.getValue());
}
}
}
};
thread.start();
}
try {
started.get(startupTimeout, TimeUnit.MILLISECONDS);
} catch (TimeoutException err) {
LOG.info("Startup timed out, trying to close");
close();
throw err;
}
}