Search in sources :

Example 1 with EnsembleProvider

use of org.apache.curator.ensemble.EnsembleProvider in project druid by druid-io.

the class CuratorModuleTest method defaultEnsembleProvider.

@Test
public void defaultEnsembleProvider() throws NoSuchFieldException, IllegalAccessException {
    Injector injector = newInjector(new Properties());
    // initialize related components
    injector.getInstance(CuratorFramework.class);
    EnsembleProvider ensembleProvider = injector.getInstance(EnsembleProvider.class);
    Assert.assertTrue("EnsembleProvider should be FixedEnsembleProvider", ensembleProvider instanceof FixedEnsembleProvider);
    Assert.assertEquals("The connectionString should be 'localhost'", "localhost", ensembleProvider.getConnectionString());
}
Also used : Injector(com.google.inject.Injector) Properties(java.util.Properties) FixedEnsembleProvider(org.apache.curator.ensemble.fixed.FixedEnsembleProvider) ExhibitorEnsembleProvider(org.apache.curator.ensemble.exhibitor.ExhibitorEnsembleProvider) EnsembleProvider(org.apache.curator.ensemble.EnsembleProvider) FixedEnsembleProvider(org.apache.curator.ensemble.fixed.FixedEnsembleProvider) Test(org.junit.Test)

Example 2 with EnsembleProvider

use of org.apache.curator.ensemble.EnsembleProvider in project druid by druid-io.

the class CuratorModuleTest method emptyExhibitorHosts.

@Test
public void emptyExhibitorHosts() {
    Properties props = new Properties();
    props.put(curatorHostKey, "hostB");
    props.put(exhibitorHostsKey, "[]");
    Injector injector = newInjector(props);
    // initialize related components
    injector.getInstance(CuratorFramework.class);
    EnsembleProvider ensembleProvider = injector.getInstance(EnsembleProvider.class);
    Assert.assertTrue("EnsembleProvider should be FixedEnsembleProvider", ensembleProvider instanceof FixedEnsembleProvider);
    Assert.assertEquals("The connectionString should be 'hostB'", "hostB", ensembleProvider.getConnectionString());
}
Also used : Injector(com.google.inject.Injector) Properties(java.util.Properties) FixedEnsembleProvider(org.apache.curator.ensemble.fixed.FixedEnsembleProvider) ExhibitorEnsembleProvider(org.apache.curator.ensemble.exhibitor.ExhibitorEnsembleProvider) EnsembleProvider(org.apache.curator.ensemble.EnsembleProvider) FixedEnsembleProvider(org.apache.curator.ensemble.fixed.FixedEnsembleProvider) Test(org.junit.Test)

Example 3 with EnsembleProvider

use of org.apache.curator.ensemble.EnsembleProvider in project xian by happyyangyuan.

the class TestInterProcessSemaphoreCluster method testKilledServerWithEnsembleProvider.

@Test
public void testKilledServerWithEnsembleProvider() throws Exception {
    final int CLIENT_QTY = 10;
    final Timing timing = new Timing();
    final String PATH = "/foo/bar/lock";
    ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_QTY);
    ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<Void>(executorService);
    TestingCluster cluster = new TestingCluster(3);
    try {
        cluster.start();
        final AtomicReference<String> connectionString = new AtomicReference<String>(cluster.getConnectString());
        final EnsembleProvider provider = new EnsembleProvider() {

            @Override
            public void start() throws Exception {
            }

            @Override
            public String getConnectionString() {
                return connectionString.get();
            }

            @Override
            public void close() throws IOException {
            }
        };
        final Semaphore acquiredSemaphore = new Semaphore(0);
        final AtomicInteger acquireCount = new AtomicInteger(0);
        final CountDownLatch suspendedLatch = new CountDownLatch(CLIENT_QTY);
        for (int i = 0; i < CLIENT_QTY; ++i) {
            completionService.submit(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    CuratorFramework client = CuratorFrameworkFactory.builder().ensembleProvider(provider).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new ExponentialBackoffRetry(100, 3)).build();
                    try {
                        final Semaphore suspendedSemaphore = new Semaphore(0);
                        client.getConnectionStateListenable().addListener(new ConnectionStateListener() {

                            @Override
                            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                                if ((newState == ConnectionState.SUSPENDED) || (newState == ConnectionState.LOST)) {
                                    suspendedLatch.countDown();
                                    suspendedSemaphore.release();
                                }
                            }
                        });
                        client.start();
                        InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, PATH, 1);
                        while (!Thread.currentThread().isInterrupted()) {
                            Lease lease = null;
                            try {
                                lease = semaphore.acquire();
                                acquiredSemaphore.release();
                                acquireCount.incrementAndGet();
                                suspendedSemaphore.acquire();
                            } catch (Exception e) {
                            // just retry
                            } finally {
                                if (lease != null) {
                                    acquireCount.decrementAndGet();
                                    CloseableUtils.closeQuietly(lease);
                                }
                            }
                        }
                    } finally {
                        CloseableUtils.closeQuietly(client);
                    }
                    return null;
                }
            });
        }
        Assert.assertTrue(timing.acquireSemaphore(acquiredSemaphore));
        Assert.assertEquals(1, acquireCount.get());
        cluster.close();
        timing.awaitLatch(suspendedLatch);
        timing.forWaiting().sleepABit();
        Assert.assertEquals(0, acquireCount.get());
        cluster = new TestingCluster(3);
        cluster.start();
        connectionString.set(cluster.getConnectString());
        timing.forWaiting().sleepABit();
        Assert.assertTrue(timing.acquireSemaphore(acquiredSemaphore));
        timing.forWaiting().sleepABit();
        Assert.assertEquals(1, acquireCount.get());
    } finally {
        executorService.shutdown();
        executorService.awaitTermination(10, TimeUnit.SECONDS);
        executorService.shutdownNow();
        CloseableUtils.closeQuietly(cluster);
    }
}
Also used : ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) AtomicReference(java.util.concurrent.atomic.AtomicReference) Semaphore(java.util.concurrent.Semaphore) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) EnsembleProvider(org.apache.curator.ensemble.EnsembleProvider) CuratorFramework(org.apache.curator.framework.CuratorFramework) TestingCluster(org.apache.curator.test.TestingCluster) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Example 4 with EnsembleProvider

use of org.apache.curator.ensemble.EnsembleProvider in project druid by druid-io.

the class CuratorModuleTest method exhibitorEnsembleProvider.

@Test
public void exhibitorEnsembleProvider() {
    Properties props = new Properties();
    props.setProperty(CURATOR_HOST_KEY, "hostA");
    props.setProperty(EXHIBITOR_HOSTS_KEY, "[\"hostB\"]");
    Injector injector = newInjector(props);
    // initialize related components
    injector.getInstance(CuratorFramework.class);
    EnsembleProvider ensembleProvider = injector.getInstance(EnsembleProvider.class);
    Assert.assertTrue("EnsembleProvider should be ExhibitorEnsembleProvider", ensembleProvider instanceof ExhibitorEnsembleProvider);
}
Also used : Injector(com.google.inject.Injector) ExhibitorEnsembleProvider(org.apache.curator.ensemble.exhibitor.ExhibitorEnsembleProvider) Properties(java.util.Properties) ExhibitorEnsembleProvider(org.apache.curator.ensemble.exhibitor.ExhibitorEnsembleProvider) EnsembleProvider(org.apache.curator.ensemble.EnsembleProvider) FixedEnsembleProvider(org.apache.curator.ensemble.fixed.FixedEnsembleProvider) Test(org.junit.Test)

Example 5 with EnsembleProvider

use of org.apache.curator.ensemble.EnsembleProvider in project druid by druid-io.

the class CuratorModuleTest method exhibitorEnsembleProvider.

@Test
public void exhibitorEnsembleProvider() {
    Properties props = new Properties();
    props.put(curatorHostKey, "hostA");
    props.put(exhibitorHostsKey, "[\"hostB\"]");
    Injector injector = newInjector(props);
    // initialize related components
    injector.getInstance(CuratorFramework.class);
    EnsembleProvider ensembleProvider = injector.getInstance(EnsembleProvider.class);
    Assert.assertTrue("EnsembleProvider should be ExhibitorEnsembleProvider", ensembleProvider instanceof ExhibitorEnsembleProvider);
}
Also used : Injector(com.google.inject.Injector) ExhibitorEnsembleProvider(org.apache.curator.ensemble.exhibitor.ExhibitorEnsembleProvider) Properties(java.util.Properties) ExhibitorEnsembleProvider(org.apache.curator.ensemble.exhibitor.ExhibitorEnsembleProvider) EnsembleProvider(org.apache.curator.ensemble.EnsembleProvider) FixedEnsembleProvider(org.apache.curator.ensemble.fixed.FixedEnsembleProvider) Test(org.junit.Test)

Aggregations

EnsembleProvider (org.apache.curator.ensemble.EnsembleProvider)10 FixedEnsembleProvider (org.apache.curator.ensemble.fixed.FixedEnsembleProvider)9 Injector (com.google.inject.Injector)8 Properties (java.util.Properties)8 ExhibitorEnsembleProvider (org.apache.curator.ensemble.exhibitor.ExhibitorEnsembleProvider)8 Test (org.junit.Test)8 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)2 IOException (java.io.IOException)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 ExecutorCompletionService (java.util.concurrent.ExecutorCompletionService)1 ExecutorService (java.util.concurrent.ExecutorService)1 Semaphore (java.util.concurrent.Semaphore)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 RetryPolicy (org.apache.curator.RetryPolicy)1 DefaultExhibitorRestClient (org.apache.curator.ensemble.exhibitor.DefaultExhibitorRestClient)1 ExhibitorRestClient (org.apache.curator.ensemble.exhibitor.ExhibitorRestClient)1 Exhibitors (org.apache.curator.ensemble.exhibitor.Exhibitors)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 ConnectionState (org.apache.curator.framework.state.ConnectionState)1