use of com.linkedin.d2.balancer.LoadBalancer in project rest.li by linkedin.
the class TestD2ZKQuorumFailover method setup.
private void setup() throws IOException, Exception {
// Start _quorum
_quorum = new ZKQuorum(QUORUM_SIZE);
_quorum.startAll();
_quorum.assertAllPeersUp();
_zkUriString = "zk://" + _quorum.getHosts();
_zkHosts = _quorum.getHosts().split(",");
// Register clusters/services with zookeeper _quorum
LoadBalancerClientCli.runDiscovery(_quorum.getHosts(), "/d2", D2_CONFIG_DATA);
// Echo servers startup
startAllEchoServers();
assertAllEchoServersRunning(_echoServers);
// Get LoadBalancer Client
_cli = new LoadBalancerClientCli(_quorum.getHosts(), "/d2");
_client = _cli.createZKFSTogglingLBClient(_quorum.getHosts(), "/d2", null);
assertAllEchoServersRegistered(_cli.getZKClient(), _zkUriString, _echoServers);
assertQuorumProcessAllRequests(D2_CONFIG_DATA);
}
use of com.linkedin.d2.balancer.LoadBalancer in project rest.li by linkedin.
the class ZKFSLoadBalancerWithFacilitiesFactory method create.
@Override
public LoadBalancerWithFacilities create(D2ClientConfig config) {
LOG.info("Creating D2 LoadBalancer based on ZKFSLoadBalancerWithFacilitiesFactory");
ZKFSLoadBalancer zkfsLoadBalancer = new ZKFSLoadBalancer(config.zkHosts, (int) config.zkSessionTimeoutInMs, (int) config.zkStartupTimeoutInMs, createLoadBalancerFactory(config), config.flagFile, config.basePath, config.shutdownAsynchronously, config.isSymlinkAware, config._executorService, config.zooKeeperDecorator);
LoadBalancerWithFacilities balancer = zkfsLoadBalancer;
if (config.warmUp) {
balancer = new WarmUpLoadBalancer(balancer, zkfsLoadBalancer, config.startUpExecutorService, config.fsBasePath, config.d2ServicePath, config.downstreamServicesFetcher, config.warmUpTimeoutSeconds, config.warmUpConcurrentRequests);
}
return balancer;
}
use of com.linkedin.d2.balancer.LoadBalancer in project rest.li by linkedin.
the class LastSeenBalancerWithFacilitiesFactory method create.
@Override
public LoadBalancerWithFacilities create(D2ClientConfig config) {
LOG.info("Creating D2 LoadBalancer based on LastSeenLoadBalancerWithFacilities");
D2ClientJmxManager d2ClientJmxManager = new D2ClientJmxManager(config.d2JmxManagerPrefix, config.jmxManager);
// init connection
ZKConnectionBuilder zkConnectionBuilder = new ZKConnectionBuilder(config.zkHosts);
zkConnectionBuilder.setShutdownAsynchronously(config.shutdownAsynchronously).setIsSymlinkAware(config.isSymlinkAware).setTimeout((int) config.zkSessionTimeoutInMs);
ZKPersistentConnection zkPersistentConnection;
if (config.zkConnectionToUseForLB != null) {
LOG.info("LastSeenLoadBalancer using shared connection to zookeeper");
zkPersistentConnection = config.zkConnectionToUseForLB;
} else {
LOG.info("LastSeenLoadBalancer using its own connection to zookeeper");
zkPersistentConnection = new ZKPersistentConnection(zkConnectionBuilder);
}
// init all the stores
LastSeenZKStore<ClusterProperties> lsClusterStore = getClusterPropertiesLastSeenZKStore(config, zkPersistentConnection, d2ClientJmxManager, config._executorService, config.zookeeperReadWindowMs);
PropertyEventBus<ClusterProperties> clusterBus = new PropertyEventBusImpl<>(config._executorService);
clusterBus.setPublisher(lsClusterStore);
LastSeenZKStore<ServiceProperties> lsServiceStore = getServicePropertiesLastSeenZKStore(config, zkPersistentConnection, d2ClientJmxManager, config._executorService, config.zookeeperReadWindowMs);
PropertyEventBus<ServiceProperties> serviceBus = new PropertyEventBusImpl<>(config._executorService);
serviceBus.setPublisher(lsServiceStore);
LastSeenZKStore<UriProperties> lsUrisStore = getUriPropertiesLastSeenZKStore(config, zkPersistentConnection, d2ClientJmxManager, config._executorService, config.zookeeperReadWindowMs);
PropertyEventBus<UriProperties> uriBus = new PropertyEventBusImpl<>(config._executorService);
uriBus.setPublisher(lsUrisStore);
// create the simple load balancer
SimpleLoadBalancerState state = new SimpleLoadBalancerState(config._executorService, uriBus, clusterBus, serviceBus, config.clientFactories, config.loadBalancerStrategyFactories, config.sslContext, config.sslParameters, config.isSSLEnabled, config.partitionAccessorRegistry, config.sslSessionValidatorFactory, config.deterministicSubsettingMetadataProvider);
d2ClientJmxManager.setSimpleLoadBalancerState(state);
SimpleLoadBalancer simpleLoadBalancer = new SimpleLoadBalancer(state, config.lbWaitTimeout, config.lbWaitUnit, config._executorService);
d2ClientJmxManager.setSimpleLoadBalancer(simpleLoadBalancer);
// add facilities
LastSeenLoadBalancerWithFacilities lastSeenLoadBalancer = new LastSeenLoadBalancerWithFacilities(simpleLoadBalancer, config.basePath, config.d2ServicePath, zkPersistentConnection, lsClusterStore, lsServiceStore, lsUrisStore);
LoadBalancerWithFacilities balancer = lastSeenLoadBalancer;
if (config.warmUp) {
balancer = new WarmUpLoadBalancer(balancer, lastSeenLoadBalancer, config.startUpExecutorService, config.fsBasePath, config.d2ServicePath, config.downstreamServicesFetcher, config.warmUpTimeoutSeconds, config.warmUpConcurrentRequests);
}
return balancer;
}
use of com.linkedin.d2.balancer.LoadBalancer in project rest.li by linkedin.
the class ZKFSLoadBalancer method start.
@Override
public void start(final Callback<None> callback) {
LOG.info("Starting ZKFSLoadBalancer");
LOG.info("ZK connect string: {}", _connectString);
LOG.info("ZK session timeout: {}ms", _sessionTimeout);
LOG.info("ZK initial connect timeout: {}ms", _initialZKTimeout);
if (_connectString == null || _connectString.isEmpty()) {
callback.onError(new IllegalArgumentException("ZooKeeper connection string is null or empty"));
return;
}
if (_zkFlagFile == null) {
LOG.info("ZK flag file not specified");
} else {
LOG.info("ZK flag file: {}", _zkFlagFile.getAbsolutePath());
LOG.info("ZK currently suppressed by flag file: {}", suppressZK());
}
_zkConnection = _zkConnectionBuilder.build();
final TogglingLoadBalancer balancer = _loadBalancerFactory.createLoadBalancer(_zkConnection, _executor);
// 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);
}
});
}
use of com.linkedin.d2.balancer.LoadBalancer in project rest.li by linkedin.
the class LoadBalancerSimulator method shutdown.
public void shutdown() throws Exception {
_clockedExecutor.shutdown();
final CountDownLatch latch = new CountDownLatch(1);
PropertyEventShutdownCallback callback = () -> latch.countDown();
_loadBalancer.shutdown(callback);
if (!latch.await(60, TimeUnit.SECONDS)) {
Assert.fail("unable to shutdown state");
}
_log.info("LoadBalancer Shutdown @ {}", _clockedExecutor.currentTimeMillis());
}
Aggregations