LOG.info("ZK flag file: {}", _zkFlagFile.getAbsolutePath());
LOG.info("ZK currently suppressed by flag file: {}", suppressZK());
}
_zkConnection = new ZKConnection(_connectString, _sessionTimeout, _shutdownAsynchronously, _isSymlinkAware);
final TogglingLoadBalancer balancer = _loadBalancerFactory.createLoadBalancer(_zkConnection, _executor);
// _currentLoadBalancer will never be null except the first time this method is called.
// In this case we want the not-yet-started load balancer to service client requests. In
// all other cases, we service requests from the old LoadBalancer until the new one is started
if (_currentLoadBalancer == null)
{
_currentLoadBalancer = balancer;
}
Callback<None> wrapped = new Callback<None>()
{
@Override
public void onSuccess(None none)
{
_currentLoadBalancer = balancer;
callback.onSuccess(none);
}
@Override
public void onError(Throwable e)
{
callback.onError(e);
}
};
if (!_startupCallback.compareAndSet(null, wrapped))
{
throw new IllegalStateException("Startup already in progress");
}
_executor.execute(new PropertyEventThread.PropertyEvent("startup")
{
@Override
public void innerRun()
{
_zkConnection.addStateListener(new ZKListener(balancer));
try
{
_zkConnection.start();
}
catch (Exception e)
{
LOG.error("Failed to start ZooKeeper (bad configuration?), enabling backup stores", e);
Callback<None> startupCallback = _startupCallback.getAndSet(null);
// TODO this should never be null
balancer.enableBackup(startupCallback);
return;
}
LOG.info("Started ZooKeeper");
_executor.schedule(new Runnable()
{
@Override
public void run()
{
Callback<None> startupCallback = _startupCallback.getAndSet(null);
if (startupCallback != null)
{
// Noone has enabled the stores yet either way
LOG.error("No response from ZooKeeper within {}ms, enabling backup stores", _initialZKTimeout);
balancer.enableBackup(startupCallback);
}
}
}, _initialZKTimeout, TimeUnit.MILLISECONDS);
}
});