Search in sources :

Example 61 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project coprhd-controller by CoprHD.

the class DistributedDoubleBarrierTest method testEnterTimeout.

/**
 * Test return value of enter() in case of timeout
 *
 * If not all members enter the barrier as expected, it should return false. Subsequent enter() should return false until
 * it meets the barrier criteria
 */
@Test
public void testEnterTimeout() throws Exception {
    final Timing timing = new Timing(2, TimeUnit.SECONDS);
    final AtomicInteger count = new AtomicInteger(0);
    final ExecutorService service = Executors.newCachedThreadPool();
    final List<Future<Void>> futures = Lists.newArrayList();
    for (int i = 1; i < QTY - 1; ++i) {
        Future<Void> worker = service.submit(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                ZkConnection zkConnection = createConnection(60 * 1000);
                CuratorFramework client = zkConnection.curator();
                try {
                    zkConnection.connect();
                    DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(client, "/barrier/testEnterTimeout", QTY);
                    log.info("Entering with timeout {} seconds", timing.seconds());
                    Assert.assertFalse("Return value of enter()", barrier.enter(timing.seconds(), TimeUnit.SECONDS));
                    count.incrementAndGet();
                    log.info("Entering again with timeout {} seconds", timing.seconds());
                    Assert.assertFalse("Return value of enter()", barrier.enter(timing.seconds(), TimeUnit.SECONDS));
                    count.decrementAndGet();
                } finally {
                    CloseableUtils.closeQuietly(client);
                }
                return null;
            }
        });
        futures.add(worker);
    }
    for (Future<Void> f : futures) {
        f.get();
    }
    Assert.assertEquals("enter/leave count", count.get(), 0);
    futures.clear();
    for (int i = 0; i < QTY; ++i) {
        Future<Void> worker = service.submit(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                ZkConnection zkConnection = createConnection(60 * 1000);
                CuratorFramework client = zkConnection.curator();
                try {
                    zkConnection.connect();
                    DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(client, "/barrier/testEnterTimeout", QTY);
                    log.info("Entering with timeout {} seconds", timing.seconds());
                    Assert.assertTrue("Return value of enter()", barrier.enter(timing.seconds(), TimeUnit.SECONDS));
                    count.incrementAndGet();
                    log.info("Leaving with timeout {} seconds", timing.seconds());
                    Assert.assertTrue("Return value of enter()", barrier.leave(timing.seconds(), TimeUnit.SECONDS));
                    count.decrementAndGet();
                } finally {
                    CloseableUtils.closeQuietly(client);
                }
                return null;
            }
        });
        futures.add(worker);
    }
    for (Future<Void> f : futures) {
        f.get();
    }
    Assert.assertEquals("enter/leave count", count.get(), 0);
}
Also used : ZkConnection(com.emc.storageos.coordinator.common.impl.ZkConnection) CuratorFramework(org.apache.curator.framework.CuratorFramework) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Timing(org.apache.curator.test.Timing) Test(org.junit.Test)

Example 62 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project coprhd-controller by CoprHD.

the class DistributedDoubleBarrierTest method testBasic.

/**
 * Test case port from Apache Curator
 */
@Test
public void testBasic() throws Exception {
    final Timing timing = new Timing();
    final List<Closeable> closeables = Lists.newArrayList();
    ZkConnection zkConnection = createConnection(10 * 1000);
    CuratorFramework client = zkConnection.curator();
    try {
        closeables.add(client);
        client.start();
        final CountDownLatch postEnterLatch = new CountDownLatch(QTY);
        final CountDownLatch postLeaveLatch = new CountDownLatch(QTY);
        final AtomicInteger count = new AtomicInteger(0);
        final AtomicInteger max = new AtomicInteger(0);
        List<Future<Void>> futures = Lists.newArrayList();
        ExecutorService service = Executors.newCachedThreadPool();
        for (int i = 0; i < QTY; ++i) {
            Future<Void> future = service.submit(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(client, "/barrier/testBasic", QTY);
                    Assert.assertTrue(barrier.enter(timing.seconds(), TimeUnit.SECONDS));
                    synchronized (DistributedDoubleBarrierTest.this) {
                        int thisCount = count.incrementAndGet();
                        if (thisCount > max.get()) {
                            max.set(thisCount);
                        }
                    }
                    postEnterLatch.countDown();
                    Assert.assertTrue(timing.awaitLatch(postEnterLatch));
                    Assert.assertEquals(count.get(), QTY);
                    Assert.assertTrue(barrier.leave(10, TimeUnit.SECONDS));
                    count.decrementAndGet();
                    postLeaveLatch.countDown();
                    Assert.assertTrue(timing.awaitLatch(postLeaveLatch));
                    return null;
                }
            });
            futures.add(future);
        }
        for (Future<Void> f : futures) {
            f.get();
        }
        Assert.assertEquals(count.get(), 0);
        Assert.assertEquals(max.get(), QTY);
    } finally {
        for (Closeable c : closeables) {
            CloseableUtils.closeQuietly(c);
        }
    }
}
Also used : Closeable(java.io.Closeable) ZkConnection(com.emc.storageos.coordinator.common.impl.ZkConnection) CuratorFramework(org.apache.curator.framework.CuratorFramework) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Timing(org.apache.curator.test.Timing) Test(org.junit.Test)

Example 63 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project coprhd-controller by CoprHD.

the class CoordinatorClientImpl method start.

@Override
public void start() throws IOException {
    if (_zkConnection.curator().isStarted()) {
        return;
    }
    _zkConnection.curator().getConnectionStateListenable().addListener(new org.apache.curator.framework.state.ConnectionStateListener() {

        @Override
        public void stateChanged(CuratorFramework client, final ConnectionState newState) {
            log.info("Entering stateChanged method : {}", newState);
            _connectionStateWorker.submit(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    Iterator<ConnectionStateListener> it = _listener.iterator();
                    while (it.hasNext()) {
                        ConnectionStateListener listener = it.next();
                        try {
                            switch(newState) {
                                case RECONNECTED:
                                case CONNECTED:
                                    {
                                        listener.connectionStateChanged(ConnectionStateListener.State.CONNECTED);
                                        break;
                                    }
                                case LOST:
                                case SUSPENDED:
                                    {
                                        listener.connectionStateChanged(ConnectionStateListener.State.DISCONNECTED);
                                        break;
                                    }
                            }
                        } catch (Exception e) {
                            log.warn("Connection listener threw", e);
                        }
                    }
                    return null;
                }
            });
        }
    });
    _zkConnection.connect();
    // writing local node to zk
    initInetAddressEntry();
    try {
        checkAndCreateSiteSpecificSection();
        String servicePath = getServicePath();
        EnsurePath path = new EnsurePath(servicePath);
        path.ensure(_zkConnection.curator().getZookeeperClient());
    } catch (Exception e) {
        // desired
        if ((_connectionStateWorker != null) && (!_connectionStateWorker.isShutdown())) {
            _connectionStateWorker.shutdownNow();
        }
        if (nodeChangeWorker != null && !nodeChangeWorker.isShutdown()) {
            nodeChangeWorker.shutdownNow();
        }
        throw CoordinatorException.fatals.errorConnectingCoordinatorService(e);
    }
}
Also used : PropertyInfoMapper.decodeFromString(com.emc.storageos.coordinator.mapper.PropertyInfoMapper.decodeFromString) Callable(java.util.concurrent.Callable) CoordinatorException(com.emc.storageos.coordinator.exceptions.CoordinatorException) RetryableCoordinatorException(com.emc.storageos.coordinator.exceptions.RetryableCoordinatorException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) CuratorFramework(org.apache.curator.framework.CuratorFramework) EnsurePath(org.apache.curator.utils.EnsurePath) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(com.emc.storageos.coordinator.client.service.ConnectionStateListener)

Example 64 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project metron by apache.

the class SensorParserConfigServiceImplTest method setUp.

@Before
public void setUp() throws Exception {
    objectMapper = mock(ObjectMapper.class);
    curatorFramework = mock(CuratorFramework.class);
    Environment environment = mock(Environment.class);
    Authentication authentication = mock(Authentication.class);
    when(authentication.getName()).thenReturn(user);
    SecurityContextHolder.getContext().setAuthentication(authentication);
    when(environment.getProperty(GROK_TEMP_PATH_SPRING_PROPERTY)).thenReturn("./target");
    grokService = new GrokServiceImpl(environment, mock(Grok.class), new HdfsServiceImpl(new Configuration()));
    cache = mock(ConfigurationsCache.class);
    sensorParserConfigService = new SensorParserConfigServiceImpl(objectMapper, curatorFramework, grokService, cache);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Configuration(org.apache.hadoop.conf.Configuration) Authentication(org.springframework.security.core.Authentication) Environment(org.springframework.core.env.Environment) ConfigurationsCache(org.apache.metron.common.zookeeper.ConfigurationsCache) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Before(org.junit.Before)

Example 65 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project metron by apache.

the class ConfigurationsUtils method uploadConfigsToZookeeper.

public static void uploadConfigsToZookeeper(String globalConfigPath, String parsersConfigPath, String enrichmentsConfigPath, String indexingConfigPath, String profilerConfigPath, String zookeeperUrl) throws Exception {
    try (CuratorFramework client = getClient(zookeeperUrl)) {
        client.start();
        uploadConfigsToZookeeper(globalConfigPath, parsersConfigPath, enrichmentsConfigPath, indexingConfigPath, profilerConfigPath, client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework)

Aggregations

CuratorFramework (org.apache.curator.framework.CuratorFramework)824 Test (org.testng.annotations.Test)290 RetryOneTime (org.apache.curator.retry.RetryOneTime)267 Test (org.junit.Test)168 Timing (org.apache.curator.test.Timing)147 CountDownLatch (java.util.concurrent.CountDownLatch)120 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)98 KeeperException (org.apache.zookeeper.KeeperException)84 IOException (java.io.IOException)75 ConnectionState (org.apache.curator.framework.state.ConnectionState)70 CuratorEvent (org.apache.curator.framework.api.CuratorEvent)52 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)50 RetryNTimes (org.apache.curator.retry.RetryNTimes)48 ExecutorService (java.util.concurrent.ExecutorService)47 ArrayList (java.util.ArrayList)44 RetryPolicy (org.apache.curator.RetryPolicy)36 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)35 Stat (org.apache.zookeeper.data.Stat)35 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)33 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)32