Search in sources :

Example 36 with PropertyStoreException

use of com.linkedin.d2.discovery.stores.PropertyStoreException 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;
}
Also used : ServicePropertiesJsonSerializer(com.linkedin.d2.balancer.properties.ServicePropertiesJsonSerializer) HashMap(java.util.HashMap) DegraderLoadBalancerStrategyFactoryV3(com.linkedin.d2.balancer.strategies.degrader.DegraderLoadBalancerStrategyFactoryV3) UriPropertiesMerger(com.linkedin.d2.balancer.properties.UriPropertiesMerger) ZooKeeperTogglingStore(com.linkedin.d2.discovery.stores.zk.ZooKeeperTogglingStore) UriPropertiesJsonSerializer(com.linkedin.d2.balancer.properties.UriPropertiesJsonSerializer) JmxManager(com.linkedin.d2.jmx.JmxManager) ClusterPropertiesJsonSerializer(com.linkedin.d2.balancer.properties.ClusterPropertiesJsonSerializer) UriProperties(com.linkedin.d2.balancer.properties.UriProperties) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SimpleLoadBalancerState(com.linkedin.d2.balancer.simple.SimpleLoadBalancerState) SimpleLoadBalancer(com.linkedin.d2.balancer.simple.SimpleLoadBalancer) ZKConnection(com.linkedin.d2.discovery.stores.zk.ZKConnection) LoadBalancerStrategyFactory(com.linkedin.d2.balancer.strategies.LoadBalancerStrategyFactory) NamedThreadFactory(com.linkedin.r2.util.NamedThreadFactory) LoadBalancerStrategy(com.linkedin.d2.balancer.strategies.LoadBalancerStrategy) FileStore(com.linkedin.d2.discovery.stores.file.FileStore) ServiceProperties(com.linkedin.d2.balancer.properties.ServiceProperties) PropertyEventBusImpl(com.linkedin.d2.discovery.event.PropertyEventBusImpl) ClusterProperties(com.linkedin.d2.balancer.properties.ClusterProperties) File(java.io.File) PropertyEventThread(com.linkedin.d2.discovery.event.PropertyEventThread)

Example 37 with PropertyStoreException

use of com.linkedin.d2.discovery.stores.PropertyStoreException in project rest.li by linkedin.

the class ZooKeeperChildrenDataPublisherTest method testPublishInitialize.

@Test
public void testPublishInitialize() throws InterruptedException, IOException, PropertyStoreException, ExecutionException {
    ZKConnection client = new ZKConnection("localhost:" + _port, 5000);
    client.start();
    final ZooKeeperChildrenDataPublisher<Map<String, String>, String> publisher = new ZooKeeperChildrenDataPublisher<Map<String, String>, String>(client, new PropertyStringSerializer(), "/");
    final CountDownLatch initLatch = new CountDownLatch(1);
    final CountDownLatch startLatch = new CountDownLatch(1);
    final PropertyEventSubscriber<Map<String, String>> subscriber = new PropertyEventSubscriber<Map<String, String>>() {

        @Override
        public void onInitialize(String propertyName, Map<String, String> propertyValue) {
            _outputData = propertyValue;
            initLatch.countDown();
        }

        @Override
        public void onAdd(String propertyName, Map<String, String> propertyValue) {
        }

        @Override
        public void onRemove(String propertyName) {
        }
    };
    publisher.start(new Callback<None>() {

        @Override
        public void onError(Throwable e) {
            Assert.fail("publisher start onError called", e);
        }

        @Override
        public void onSuccess(None result) {
            _eventBus = new PropertyEventBusImpl<Map<String, String>>(_executor, publisher);
            _eventBus.register(Collections.singleton("bucket"), subscriber);
            startLatch.countDown();
        }
    });
    if (!startLatch.await(60, TimeUnit.SECONDS)) {
        Assert.fail("unable to start ZookeeperChildrenDataPublisher");
    }
    if (!initLatch.await(60, TimeUnit.SECONDS)) {
        Assert.fail("unable to publish initial property value");
    }
    assertEquals(_outputData, _testData);
    _eventBus.unregister(Collections.singleton("bucket"), subscriber);
    client.shutdown();
}
Also used : PropertyEventSubscriber(com.linkedin.d2.discovery.event.PropertyEventSubscriber) CountDownLatch(java.util.concurrent.CountDownLatch) PropertyEventBusImpl(com.linkedin.d2.discovery.event.PropertyEventBusImpl) HashMap(java.util.HashMap) Map(java.util.Map) None(com.linkedin.common.util.None) Test(org.testng.annotations.Test)

Example 38 with PropertyStoreException

use of com.linkedin.d2.discovery.stores.PropertyStoreException in project rest.li by linkedin.

the class ZooKeeperEphemeralStoreStrawMan method main.

public static void main(String[] args) throws IOException, InterruptedException, PropertyStoreException {
    ZKConnection zkClient = new ZKConnection("localhost:2181", 30000);
    PropertyStringMerger merger = new PropertyStringMerger();
    Set<String> listenTos = new HashSet<String>();
    ZooKeeperEphemeralStore<String> zk = new ZooKeeperEphemeralStore<String>(zkClient, new PropertyStringSerializer(), merger, "/test/lb/test-property-ephemeral", false, true);
    listenTos.add("foo12");
    ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
    PropertyEventBus<String> bus = new PropertyEventBusImpl<String>(executorService, zk);
    bus.register(listenTos, new PropertyEventSubscriber<String>() {

        @Override
        public void onAdd(String propertyName, String propertyValue) {
            System.err.println("onAdd: " + propertyName + "\t" + propertyValue);
        }

        @Override
        public void onInitialize(String propertyName, String propertyValue) {
            System.err.println("onInitialize: " + propertyName + "\t" + propertyValue);
        }

        @Override
        public void onRemove(String propertyName) {
            System.err.println("onRemove: " + propertyName);
        }
    });
    zk.put("foo12", "TEST1");
    zk.put("foo12", "TEST2");
    zk.put("foo12", "TEST3");
    zk.put("foo12", "TEST4");
    zk.put("foo12", "TEST5");
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    zk.remove("foo12");
    try {
        Thread.sleep(5000);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    zkClient.getZooKeeper().close();
    executorService.shutdown();
}
Also used : PropertyStringMerger(com.linkedin.d2.discovery.stores.zk.ZooKeeperEphemeralStoreTest.PropertyStringMerger) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) PropertyStringSerializer(com.linkedin.d2.discovery.stores.PropertyStringSerializer) PropertyEventBusImpl(com.linkedin.d2.discovery.event.PropertyEventBusImpl) HashSet(java.util.HashSet)

Example 39 with PropertyStoreException

use of com.linkedin.d2.discovery.stores.PropertyStoreException in project rest.li by linkedin.

the class ZooKeeperEphemeralStoreTest method testShutdown.

@Test(groups = { "small", "back-end" })
public void testShutdown() throws InterruptedException, IOException, PropertyStoreException, ExecutionException {
    PropertyStore<String> store = getStore();
    final CountDownLatch latch = new CountDownLatch(1);
    store.shutdown(new PropertyEventShutdownCallback() {

        @Override
        public void done() {
            latch.countDown();
        }
    });
    if (!latch.await(5, TimeUnit.SECONDS)) {
        fail("unable to shut down store");
    }
}
Also used : PropertyEventShutdownCallback(com.linkedin.d2.discovery.event.PropertyEventThread.PropertyEventShutdownCallback) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 40 with PropertyStoreException

use of com.linkedin.d2.discovery.stores.PropertyStoreException in project rest.li by linkedin.

the class ZooKeeperEphemeralStoreTest method getStore.

public ZooKeeperEphemeralStore<String> getStore() throws IOException, PropertyStoreException, InterruptedException, ExecutionException {
    ZKConnection client = new ZKConnection("localhost:" + _port, 5000);
    client.start();
    ZooKeeperEphemeralStore<String> store = new ZooKeeperEphemeralStore<String>(client, new PropertyStringSerializer(), new PropertyStringMerger(), "/test-path", false, true);
    FutureCallback<None> callback = new FutureCallback<None>();
    store.start(callback);
    callback.get();
    return store;
}
Also used : None(com.linkedin.common.util.None) PropertyStringSerializer(com.linkedin.d2.discovery.stores.PropertyStringSerializer) FutureCallback(com.linkedin.common.callback.FutureCallback)

Aggregations

None (com.linkedin.common.util.None)19 FutureCallback (com.linkedin.common.callback.FutureCallback)18 HashMap (java.util.HashMap)18 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)15 Test (org.testng.annotations.Test)14 PartitionData (com.linkedin.d2.balancer.properties.PartitionData)9 PropertyStoreException (com.linkedin.d2.discovery.stores.PropertyStoreException)9 ServiceProperties (com.linkedin.d2.balancer.properties.ServiceProperties)8 URI (java.net.URI)8 ClusterProperties (com.linkedin.d2.balancer.properties.ClusterProperties)7 ServicePropertiesJsonSerializer (com.linkedin.d2.balancer.properties.ServicePropertiesJsonSerializer)7 UriPropertiesJsonSerializer (com.linkedin.d2.balancer.properties.UriPropertiesJsonSerializer)7 UriPropertiesMerger (com.linkedin.d2.balancer.properties.UriPropertiesMerger)7 IOException (java.io.IOException)7 HashSet (java.util.HashSet)6 PropertyEventBusImpl (com.linkedin.d2.discovery.event.PropertyEventBusImpl)5 ZooKeeperEphemeralStore (com.linkedin.d2.discovery.stores.zk.ZooKeeperEphemeralStore)5 ExecutionException (java.util.concurrent.ExecutionException)5 ClusterPropertiesJsonSerializer (com.linkedin.d2.balancer.properties.ClusterPropertiesJsonSerializer)4 PropertyStringSerializer (com.linkedin.d2.discovery.stores.PropertyStringSerializer)4