use of com.linkedin.d2.balancer.properties.ClusterProperties in project rest.li by linkedin.
the class LoadBalancerClientCli method shutdown.
public void shutdown() throws Exception {
if (_zkClusterRegistry != null) {
try {
shutdownZKRegistry(_zkClusterRegistry);
} catch (Exception e) {
_log.error("Failed to shutdown ZooKeeperPermanentStore<ClusterProperties> zkClusterRegistry.");
}
}
if (_zkServiceRegistry != null) {
try {
shutdownZKRegistry(_zkServiceRegistry);
} catch (Exception e) {
_log.error("Failed to shutdown ZooKeeperPermanentStore<ServiceProperties> zkServiceRegistry.");
}
}
if (_zkUriRegistry != null) {
try {
shutdownZKRegistry(_zkUriRegistry);
} catch (Exception e) {
_log.error("Failed to shutdown ZooKeeperEphemeralStore<UriProperties> zkUriRegistry.");
}
}
try {
if (_client != null) {
LoadBalancerUtil.syncShutdownClient(_client, _log);
}
} catch (Exception e) {
_log.error("Failed to shutdown dynamic client.");
}
if (_zkfsLoadBalancer != null) {
try {
final CountDownLatch latch = new CountDownLatch(1);
_zkfsLoadBalancer.shutdown(new PropertyEventShutdownCallback() {
@Override
public void done() {
latch.countDown();
}
});
if (!latch.await(5, TimeUnit.SECONDS)) {
_log.error("unable to shut down store");
}
} catch (Exception e) {
_log.error("Failed to shutdown zkfsLoadBalancer.");
}
}
try {
deleteTempDir();
} catch (Exception e) {
_log.error("Failed to delete directory " + _tmpDir);
}
try {
_zkclient.shutdown();
} catch (Exception e) {
_log.error("Failed to shutdown zk client.");
}
}
use of com.linkedin.d2.balancer.properties.ClusterProperties in project rest.li by linkedin.
the class LoadBalancerClientCli method getLoadBalancer.
public static SimpleLoadBalancer getLoadBalancer(ZKConnection zkclient, String zkserver, String d2path, String service) throws IOException, IllegalStateException, URISyntaxException, PropertyStoreException, ExecutionException, TimeoutException, InterruptedException {
// zk stores
String clstoreString = zkserver + ZKFSUtil.clusterPath(d2path);
String scstoreString = zkserver + ZKFSUtil.servicePath(d2path);
String uristoreString = zkserver + ZKFSUtil.uriPath(d2path);
ZooKeeperPermanentStore<ClusterProperties> zkClusterRegistry = (ZooKeeperPermanentStore<ClusterProperties>) getStore(zkclient, clstoreString, new ClusterPropertiesJsonSerializer());
ZooKeeperPermanentStore<ServiceProperties> zkServiceRegistry = (ZooKeeperPermanentStore<ServiceProperties>) getStore(zkclient, scstoreString, new ServicePropertiesJsonSerializer());
ZooKeeperEphemeralStore<UriProperties> zkUriRegistry = (ZooKeeperEphemeralStore<UriProperties>) getEphemeralStore(zkclient, uristoreString, new UriPropertiesJsonSerializer(), new UriPropertiesMerger());
ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory("D2 PropertyEventExecutor"));
PropertyEventBus<ServiceProperties> serviceBus = new PropertyEventBusImpl<ServiceProperties>(executor, zkServiceRegistry);
PropertyEventBus<UriProperties> uriBus = new PropertyEventBusImpl<UriProperties>(executor, zkUriRegistry);
PropertyEventBus<ClusterProperties> clusterBus = new PropertyEventBusImpl<ClusterProperties>(executor, zkClusterRegistry);
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());
Map<String, TransportClientFactory> clientFactories = new HashMap<String, TransportClientFactory>();
clientFactories.put("http", new HttpClientFactory());
// create the state
SimpleLoadBalancerState state = new SimpleLoadBalancerState(executor, uriBus, clusterBus, serviceBus, clientFactories, loadBalancerStrategyFactories, null, null, false);
SimpleLoadBalancer balancer = new SimpleLoadBalancer(state, 5, TimeUnit.SECONDS);
FutureCallback<None> callback = new FutureCallback<None>();
balancer.start(callback);
callback.get(5, TimeUnit.SECONDS);
new JmxManager().registerLoadBalancer("balancer", balancer).registerLoadBalancerState("state", state).registerScheduledThreadPoolExecutor("executorService", executor).registerZooKeeperPermanentStore("zkClusterRegistry", zkClusterRegistry).registerZooKeeperPermanentStore("zkServiceRegistry", zkServiceRegistry).registerZooKeeperEphemeralStore("zkUriRegistry", zkUriRegistry);
return balancer;
}
use of com.linkedin.d2.balancer.properties.ClusterProperties in project rest.li by linkedin.
the class SimpleLoadBalancer method getRings.
public Map<Integer, Ring<URI>> getRings(URI serviceUri) throws ServiceUnavailableException {
ServiceProperties service = listenToServiceAndCluster(serviceUri);
String serviceName = service.getServiceName();
String clusterName = service.getClusterName();
ClusterProperties cluster = getClusterProperties(serviceName, clusterName);
LoadBalancerStateItem<UriProperties> uriItem = getUriItem(serviceName, clusterName, cluster);
UriProperties uris = uriItem.getProperty();
List<LoadBalancerState.SchemeStrategyPair> orderedStrategies = _state.getStrategiesForService(serviceName, service.getPrioritizedSchemes());
if (!orderedStrategies.isEmpty()) {
final LoadBalancerState.SchemeStrategyPair pair = orderedStrategies.get(0);
final PartitionAccessor accessor = getPartitionAccessor(serviceName, clusterName);
int maxPartitionId = accessor.getMaxPartitionId();
Map<Integer, Ring<URI>> ringMap = new HashMap<Integer, Ring<URI>>((maxPartitionId + 1) * 2);
for (int partitionId = 0; partitionId <= maxPartitionId; partitionId++) {
Set<URI> possibleUris = uris.getUriBySchemeAndPartition(pair.getScheme(), partitionId);
List<TrackerClient> trackerClients = getPotentialClients(serviceName, service, possibleUris);
Ring<URI> ring = pair.getStrategy().getRing(uriItem.getVersion(), partitionId, trackerClients);
// ring will never be null; it can be empty
ringMap.put(partitionId, ring);
}
return ringMap;
} else {
throw new ServiceUnavailableException(serviceName, "Unable to find a load balancer strategy");
}
}
use of com.linkedin.d2.balancer.properties.ClusterProperties in project rest.li by linkedin.
the class LoadBalancerEchoClient method getLoadBalancer.
public static SimpleLoadBalancer getLoadBalancer(String hostPort) throws IOException, PropertyStoreException {
// zk stores
ZooKeeperPermanentStore<ClusterProperties> zkClusterRegistry = null;
ZooKeeperPermanentStore<ServiceProperties> zkServiceRegistry = null;
ZooKeeperEphemeralStore<UriProperties> zkUriRegistry = null;
ZKConnection zkClient = new ZKConnection(hostPort, 10000);
zkClusterRegistry = new ZooKeeperPermanentStore<ClusterProperties>(zkClient, new ClusterPropertiesJsonSerializer(), _basePath + "/clusters");
zkServiceRegistry = new ZooKeeperPermanentStore<ServiceProperties>(zkClient, new ServicePropertiesJsonSerializer(), _basePath + "/services");
zkUriRegistry = new ZooKeeperEphemeralStore<UriProperties>(zkClient, new UriPropertiesJsonSerializer(), new UriPropertiesMerger(), _basePath + "/uris", false, true);
// fs stores
File testDirectory = LoadBalancerUtil.createTempDirectory("lb-degrader-witih-file-store-large");
testDirectory.deleteOnExit();
new File(testDirectory + File.separator + "cluster").mkdir();
new File(testDirectory + File.separator + "service").mkdir();
new File(testDirectory + File.separator + "uri").mkdir();
FileStore<ClusterProperties> fsClusterStore = new FileStore<ClusterProperties>(testDirectory + File.separator + "cluster", ".ini", new ClusterPropertiesJsonSerializer());
FileStore<ServiceProperties> fsServiceStore = new FileStore<ServiceProperties>(testDirectory + File.separator + "service", ".ini", new ServicePropertiesJsonSerializer());
FileStore<UriProperties> fsUriStore = new FileStore<UriProperties>(testDirectory + File.separator + "uri", ".ini", new UriPropertiesJsonSerializer());
// chains
PropertyEventThread thread = new PropertyEventThread("echo client event thread");
ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("echo client event thread"));
// start up the world
thread.start();
PropertyEventBus<ServiceProperties> serviceBus = new PropertyEventBusImpl<ServiceProperties>(executorService, zkServiceRegistry);
serviceBus.register(fsServiceStore);
new ZooKeeperTogglingStore<ServiceProperties>(zkServiceRegistry, fsServiceStore, serviceBus, true);
PropertyEventBus<UriProperties> uriBus = new PropertyEventBusImpl<UriProperties>(executorService, zkUriRegistry);
uriBus.register(fsUriStore);
new ZooKeeperTogglingStore<UriProperties>(zkUriRegistry, fsUriStore, uriBus, true);
PropertyEventBus<ClusterProperties> clusterBus = new PropertyEventBusImpl<ClusterProperties>(executorService, zkClusterRegistry);
clusterBus.register(fsClusterStore);
new ZooKeeperTogglingStore<ClusterProperties>(zkClusterRegistry, fsClusterStore, clusterBus, true);
Map<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>> loadBalancerStrategyFactories = new HashMap<String, LoadBalancerStrategyFactory<? extends LoadBalancerStrategy>>();
// strategy and scheme factories
loadBalancerStrategyFactories.put("degrader", new DegraderLoadBalancerStrategyFactoryV3());
Map<String, TransportClientFactory> clientFactories = new HashMap<String, TransportClientFactory>();
clientFactories.put("http", new HttpClientFactory());
// create the state
SimpleLoadBalancerState state = new SimpleLoadBalancerState(executorService, uriBus, clusterBus, serviceBus, clientFactories, loadBalancerStrategyFactories, null, null, false);
SimpleLoadBalancer balancer = new SimpleLoadBalancer(state, 5, TimeUnit.SECONDS);
new JmxManager().registerLoadBalancer("balancer", balancer).registerLoadBalancerState("state", state);
return balancer;
}
use of com.linkedin.d2.balancer.properties.ClusterProperties in project rest.li by linkedin.
the class ZKFSTest method testZKDown.
@Test
public void testZKDown() throws Exception {
final String TEST_SERVICE_NAME = "testingService";
final String TEST_CLUSTER_NAME = "someCluster";
startServer();
try {
ZKFSLoadBalancer balancer = getBalancer();
FutureCallback<None> callback = new FutureCallback<None>();
balancer.start(callback);
callback.get(30, TimeUnit.SECONDS);
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, TEST_CLUSTER_NAME, "/somePath", Arrays.asList("degrader"), Collections.<String, Object>emptyMap(), null, null, Arrays.asList("http"), null);
store.put(TEST_SERVICE_NAME, props);
ZooKeeperPermanentStore<ClusterProperties> clusterStore = new ZooKeeperPermanentStore<ClusterProperties>(conn, new ClusterPropertiesJsonSerializer(), ZKFSUtil.clusterPath(BASE_PATH));
callback = new FutureCallback<None>();
clusterStore.start(callback);
callback.get(30, TimeUnit.SECONDS);
ClusterProperties clusterProps = new ClusterProperties("someCluster");
clusterStore.put(TEST_CLUSTER_NAME, clusterProps);
ZKConnection serverConn = new ZKConnection("localhost:" + PORT, 30000);
serverConn.start();
ZooKeeperEphemeralStore<UriProperties> uriStore = new ZooKeeperEphemeralStore<UriProperties>(serverConn, new UriPropertiesJsonSerializer(), new UriPropertiesMerger(), ZKFSUtil.uriPath(BASE_PATH));
callback = new FutureCallback<None>();
uriStore.start(callback);
callback.get(30, TimeUnit.SECONDS);
ZooKeeperServer server = new ZooKeeperServer(uriStore);
callback = new FutureCallback<None>();
Map<Integer, PartitionData> partitionDataMap = new HashMap<Integer, PartitionData>();
partitionDataMap.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(1.0));
server.markUp(TEST_CLUSTER_NAME, URI.create("http://test.uri"), partitionDataMap, callback);
callback.get(30, TimeUnit.SECONDS);
URIRequest request = new URIRequest("d2://" + TEST_SERVICE_NAME + "/foo");
TransportClient client = balancer.getClient(request, new RequestContext());
// Stop the server to cause a disconnect event
stopServer();
// Sleep to ensure the disconnect has propagated; ideally the Toggle should expose
// some interface to allow detection that the toggle occurred
Thread.sleep(1000);
// Now see if it still works
client = balancer.getClient(request, new RequestContext());
} finally {
stopServer();
}
}
Aggregations