use of com.linkedin.d2.balancer.zkfs.ZKFSLoadBalancer in project rest.li by linkedin.
the class LoadBalancerClientCli method getZKFSLoadBalancer.
public ZKFSLoadBalancer getZKFSLoadBalancer(String zkConnectString, String d2path, String d2ServicePath) throws Exception {
_tmpDir = createTempDirectory(_tmpdirName);
ZKFSComponentFactory componentFactory = new ZKFSComponentFactory();
if (d2ServicePath == null || d2ServicePath.isEmpty()) {
d2ServicePath = "services";
}
Map<String, TransportClientFactory> clientFactories = new HashMap<String, TransportClientFactory>();
clientFactories.put("http", new HttpClientFactory());
Map<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>> loadBalancerStrategyFactories = new HashMap<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>>();
loadBalancerStrategyFactories.put("random", new RandomLoadBalancerStrategyFactory());
loadBalancerStrategyFactories.put("degrader", new DegraderLoadBalancerStrategyFactoryV2());
loadBalancerStrategyFactories.put("degraderV2", new DegraderLoadBalancerStrategyFactoryV2());
loadBalancerStrategyFactories.put("degraderV3", new DegraderLoadBalancerStrategyFactoryV3());
loadBalancerStrategyFactories.put("degraderV2_1", new DegraderLoadBalancerStrategyFactoryV2_1());
ZKFSTogglingLoadBalancerFactoryImpl factory = new ZKFSTogglingLoadBalancerFactoryImpl(componentFactory, TIMEOUT, TimeUnit.MILLISECONDS, d2path, _tmpDir.getAbsolutePath(), clientFactories, loadBalancerStrategyFactories, d2ServicePath, null, null, false);
return new ZKFSLoadBalancer(zkConnectString, SESSION_TIMEOUT, (int) TIMEOUT, factory, null, d2path);
}
use of com.linkedin.d2.balancer.zkfs.ZKFSLoadBalancer 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 = new ZKConnection(_connectString, _sessionTimeout, _shutdownAsynchronously, _isSymlinkAware);
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.zkfs.ZKFSLoadBalancer in project rest.li by linkedin.
the class ZKFSTest method testKeyMapper.
@Test
public void testKeyMapper() throws Exception {
final String TEST_SERVICE_NAME = "test-service";
final String TEST_CLUSTER_NAME = "test-cluster";
final URI TEST_SERVER_URI1 = URI.create("http://test-host-1/");
final URI TEST_SERVER_URI2 = URI.create("http://test-host-2/");
final int NUM_ITERATIONS = 5;
startServer();
try {
ZKFSLoadBalancer balancer = getBalancer();
FutureCallback<None> callback = new FutureCallback<None>();
balancer.start(callback);
callback.get(30, TimeUnit.SECONDS);
ZKConnection conn = balancer.zkConnection();
ZooKeeperPermanentStore<ServiceProperties> serviceStore = new ZooKeeperPermanentStore<ServiceProperties>(conn, new ServicePropertiesJsonSerializer(), ZKFSUtil.servicePath(BASE_PATH));
ServiceProperties props = new ServiceProperties(TEST_SERVICE_NAME, TEST_CLUSTER_NAME, "/test", Arrays.asList("degrader"), Collections.<String, Object>emptyMap(), null, null, Arrays.asList("http"), null);
serviceStore.put(TEST_SERVICE_NAME, props);
ClusterProperties clusterProperties = new ClusterProperties(TEST_CLUSTER_NAME);
ZooKeeperPermanentStore<ClusterProperties> clusterStore = new ZooKeeperPermanentStore<ClusterProperties>(conn, new ClusterPropertiesJsonSerializer(), ZKFSUtil.clusterPath(BASE_PATH));
clusterStore.put(TEST_CLUSTER_NAME, clusterProperties);
ZooKeeperEphemeralStore<UriProperties> uriStore = new ZooKeeperEphemeralStore<UriProperties>(conn, new UriPropertiesJsonSerializer(), new UriPropertiesMerger(), ZKFSUtil.uriPath(BASE_PATH), false, true);
Map<URI, Map<Integer, PartitionData>> uriData = new HashMap<URI, Map<Integer, PartitionData>>();
Map<Integer, PartitionData> partitionData = new HashMap<Integer, PartitionData>(1);
partitionData.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(1.0d));
uriData.put(TEST_SERVER_URI1, partitionData);
uriData.put(TEST_SERVER_URI2, partitionData);
UriProperties uriProps = new UriProperties(TEST_CLUSTER_NAME, uriData);
callback = new FutureCallback<None>();
uriStore.start(callback);
callback.get(30, TimeUnit.SECONDS);
uriStore.put(TEST_CLUSTER_NAME, uriProps);
Set<Integer> keys = new HashSet<Integer>();
for (int ii = 0; ii < 100; ++ii) {
keys.add(ii);
}
for (int ii = 0; ii < NUM_ITERATIONS; ++ii) {
KeyMapper mapper = balancer.getKeyMapper();
MapKeyResult<URI, Integer> batches = mapper.mapKeysV2(URI.create("d2://" + TEST_SERVICE_NAME), keys);
Assert.assertEquals(batches.getMapResult().size(), 2);
for (Map.Entry<URI, Collection<Integer>> oneBatch : batches.getMapResult().entrySet()) {
Assert.assertTrue(oneBatch.getKey().toString().startsWith("http://test-host-"));
Assert.assertTrue(keys.containsAll(oneBatch.getValue()));
}
}
} finally {
stopServer();
}
}
use of com.linkedin.d2.balancer.zkfs.ZKFSLoadBalancer in project rest.li by linkedin.
the class ZKFSTest method testClientFactoryProvider.
@Test
public void testClientFactoryProvider() throws Exception {
startServer();
try {
ZKFSLoadBalancer balancer = getBalancer();
FutureCallback<None> callback = new FutureCallback<None>();
balancer.start(callback);
callback.get(30, TimeUnit.SECONDS);
Facilities facilities = balancer.getFacilities();
TransportClientFactory factory = facilities.getClientFactory("http");
Assert.assertNotNull(factory);
Assert.assertTrue(factory instanceof HttpClientFactory);
} finally {
stopServer();
}
}
use of com.linkedin.d2.balancer.zkfs.ZKFSLoadBalancer in project rest.li by linkedin.
the class ZKFSTest method testServiceDirectory.
@Test
public void testServiceDirectory() throws Exception {
final String TEST_SERVICE_NAME = "testingService";
startServer();
try {
ZKFSLoadBalancer balancer = getBalancer();
FutureCallback<None> callback = new FutureCallback<None>();
balancer.start(callback);
callback.get(30, TimeUnit.SECONDS);
Directory dir = balancer.getDirectory();
ZKConnection conn = new ZKConnection("localhost:" + PORT, 30000);
conn.start();
ZooKeeperPermanentStore<ServiceProperties> store = new ZooKeeperPermanentStore<ServiceProperties>(conn, new ServicePropertiesJsonSerializer(), ZKFSUtil.servicePath(BASE_PATH));
callback = new FutureCallback<None>();
store.start(callback);
callback.get(30, TimeUnit.SECONDS);
ServiceProperties props = new ServiceProperties(TEST_SERVICE_NAME, "someCluster", "/somePath", Arrays.asList("someStrategy"));
store.put(TEST_SERVICE_NAME, props);
FutureCallback<List<String>> serviceCallback = new FutureCallback<List<String>>();
dir.getServiceNames(serviceCallback);
Assert.assertEquals(serviceCallback.get(30, TimeUnit.SECONDS), Collections.singletonList(TEST_SERVICE_NAME));
} finally {
stopServer();
}
}
Aggregations