Search in sources :

Example 1 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project elastic-job by dangdangdotcom.

the class JobNodeStorageTest method assertExecuteInTransactionFailure.

@Test(expected = RuntimeException.class)
public void assertExecuteInTransactionFailure() throws Exception {
    CuratorFramework client = mock(CuratorFramework.class);
    CuratorTransaction curatorTransaction = mock(CuratorTransaction.class);
    TransactionCheckBuilder transactionCheckBuilder = mock(TransactionCheckBuilder.class);
    CuratorTransactionBridge curatorTransactionBridge = mock(CuratorTransactionBridge.class);
    CuratorTransactionFinal curatorTransactionFinal = mock(CuratorTransactionFinal.class);
    when(regCenter.getRawClient()).thenReturn(client);
    when(client.inTransaction()).thenReturn(curatorTransaction);
    when(curatorTransaction.check()).thenReturn(transactionCheckBuilder);
    when(transactionCheckBuilder.forPath("/")).thenReturn(curatorTransactionBridge);
    when(curatorTransactionBridge.and()).thenReturn(curatorTransactionFinal);
    TransactionCreateBuilder transactionCreateBuilder = mock(TransactionCreateBuilder.class);
    when(curatorTransactionFinal.create()).thenReturn(transactionCreateBuilder);
    when(transactionCreateBuilder.forPath("/test_transaction")).thenReturn(curatorTransactionBridge);
    when(curatorTransactionBridge.and()).thenThrow(new RuntimeException());
    jobNodeStorage.executeInTransaction(new TransactionExecutionCallback() {

        @Override
        public void execute(final CuratorTransactionFinal curatorTransactionFinal) throws Exception {
            curatorTransactionFinal.create().forPath("/test_transaction").and();
        }
    });
    verify(regCenter).getRawClient();
    verify(client).inTransaction();
    verify(curatorTransaction).check();
    verify(transactionCheckBuilder).forPath("/");
    verify(curatorTransactionBridge, times(2)).and();
    verify(curatorTransactionFinal).create();
    verify(transactionCreateBuilder).forPath("/test_transaction");
    verify(curatorTransactionFinal, times(0)).commit();
}
Also used : TransactionCreateBuilder(org.apache.curator.framework.api.transaction.TransactionCreateBuilder) CuratorTransactionBridge(org.apache.curator.framework.api.transaction.CuratorTransactionBridge) CuratorFramework(org.apache.curator.framework.CuratorFramework) TransactionCheckBuilder(org.apache.curator.framework.api.transaction.TransactionCheckBuilder) CuratorTransaction(org.apache.curator.framework.api.transaction.CuratorTransaction) CuratorTransactionFinal(org.apache.curator.framework.api.transaction.CuratorTransactionFinal) Test(org.junit.Test)

Example 2 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project druid by druid-io.

the class RemoteTaskRunner method start.

@Override
@LifecycleStart
public void start() {
    try {
        if (started) {
            return;
        }
        final MutableInt waitingFor = new MutableInt(1);
        final Object waitingForMonitor = new Object();
        // Add listener for creation/deletion of workers
        workerPathCache.getListenable().addListener(new PathChildrenCacheListener() {

            @Override
            public void childEvent(CuratorFramework client, final PathChildrenCacheEvent event) throws Exception {
                final Worker worker;
                switch(event.getType()) {
                    case CHILD_ADDED:
                        worker = jsonMapper.readValue(event.getData().getData(), Worker.class);
                        synchronized (waitingForMonitor) {
                            waitingFor.increment();
                        }
                        Futures.addCallback(addWorker(worker), new FutureCallback<ZkWorker>() {

                            @Override
                            public void onSuccess(ZkWorker zkWorker) {
                                synchronized (waitingForMonitor) {
                                    waitingFor.decrement();
                                    waitingForMonitor.notifyAll();
                                }
                            }

                            @Override
                            public void onFailure(Throwable throwable) {
                                synchronized (waitingForMonitor) {
                                    waitingFor.decrement();
                                    waitingForMonitor.notifyAll();
                                }
                            }
                        });
                        break;
                    case CHILD_UPDATED:
                        worker = jsonMapper.readValue(event.getData().getData(), Worker.class);
                        updateWorker(worker);
                        break;
                    case CHILD_REMOVED:
                        worker = jsonMapper.readValue(event.getData().getData(), Worker.class);
                        removeWorker(worker);
                        break;
                    case INITIALIZED:
                        // Schedule cleanup for task status of the workers that might have disconnected while overlord was not running
                        List<String> workers;
                        try {
                            workers = cf.getChildren().forPath(indexerZkConfig.getStatusPath());
                        } catch (KeeperException.NoNodeException e) {
                            // statusPath doesn't exist yet; can occur if no middleManagers have started.
                            workers = ImmutableList.of();
                        }
                        for (String workerId : workers) {
                            final String workerAnnouncePath = JOINER.join(indexerZkConfig.getAnnouncementsPath(), workerId);
                            final String workerStatusPath = JOINER.join(indexerZkConfig.getStatusPath(), workerId);
                            if (!zkWorkers.containsKey(workerId) && cf.checkExists().forPath(workerAnnouncePath) == null) {
                                try {
                                    scheduleTasksCleanupForWorker(workerId, cf.getChildren().forPath(workerStatusPath));
                                } catch (Exception e) {
                                    log.warn(e, "Could not schedule cleanup for worker[%s] during startup (maybe someone removed the status znode[%s]?). Skipping.", workerId, workerStatusPath);
                                }
                            }
                        }
                        synchronized (waitingForMonitor) {
                            waitingFor.decrement();
                            waitingForMonitor.notifyAll();
                        }
                    default:
                        break;
                }
            }
        });
        workerPathCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
        synchronized (waitingForMonitor) {
            while (waitingFor.intValue() > 0) {
                waitingForMonitor.wait();
            }
        }
        scheduleBlackListedNodesCleanUp();
        resourceManagement.startManagement(this);
        started = true;
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) KeeperException(org.apache.zookeeper.KeeperException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) CuratorFramework(org.apache.curator.framework.CuratorFramework) MutableInt(org.apache.commons.lang.mutable.MutableInt) Worker(io.druid.indexing.worker.Worker) List(java.util.List) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ImmutableList(com.google.common.collect.ImmutableList) FutureCallback(com.google.common.util.concurrent.FutureCallback) LifecycleStart(io.druid.java.util.common.lifecycle.LifecycleStart)

Example 3 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project elastic-job by dangdangdotcom.

the class ZookeeperRegistryCenterForAuthTest method assertInitWithDigestSuccess.

@Test
public void assertInitWithDigestSuccess() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.builder().connectString(EmbedTestingServer.getConnectionString()).retryPolicy(new RetryOneTime(2000)).authorization("digest", "digest:password".getBytes()).build();
    client.start();
    client.blockUntilConnected();
    assertThat(client.getData().forPath("/" + ZookeeperRegistryCenterForAuthTest.class.getName() + "/test/deep/nested"), is("deepNested".getBytes()));
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Test(org.junit.Test)

Example 4 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project elastic-job by dangdangdotcom.

the class ZookeeperRegistryCenterForAuthTest method assertInitWithDigestFailure.

@Test(expected = NoAuthException.class)
public void assertInitWithDigestFailure() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient(EmbedTestingServer.getConnectionString(), new RetryOneTime(2000));
    client.start();
    client.blockUntilConnected();
    client.getData().forPath("/" + ZookeeperRegistryCenterForAuthTest.class.getName() + "/test/deep/nested");
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Test(org.junit.Test)

Example 5 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project elastic-job by dangdangdotcom.

the class ZookeeperRegistryCenterModifyTest method assertPersistEphemeral.

@Test
public void assertPersistEphemeral() throws Exception {
    zkRegCenter.persist("/persist", "persist_value");
    zkRegCenter.persistEphemeral("/ephemeral", "ephemeral_value");
    assertThat(zkRegCenter.get("/persist"), is("persist_value"));
    assertThat(zkRegCenter.get("/ephemeral"), is("ephemeral_value"));
    zkRegCenter.close();
    CuratorFramework client = CuratorFrameworkFactory.newClient(EmbedTestingServer.getConnectionString(), new RetryOneTime(2000));
    client.start();
    client.blockUntilConnected();
    assertThat(client.getData().forPath("/" + ZookeeperRegistryCenterModifyTest.class.getName() + "/persist"), is("persist_value".getBytes()));
    assertNull(client.checkExists().forPath("/" + ZookeeperRegistryCenterModifyTest.class.getName() + "/ephemeral"));
    zkRegCenter.init();
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) Test(org.junit.Test)

Aggregations

CuratorFramework (org.apache.curator.framework.CuratorFramework)851 Test (org.testng.annotations.Test)290 RetryOneTime (org.apache.curator.retry.RetryOneTime)268 Test (org.junit.Test)184 Timing (org.apache.curator.test.Timing)147 CountDownLatch (java.util.concurrent.CountDownLatch)121 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)101 KeeperException (org.apache.zookeeper.KeeperException)90 IOException (java.io.IOException)76 ConnectionState (org.apache.curator.framework.state.ConnectionState)70 CuratorEvent (org.apache.curator.framework.api.CuratorEvent)56 ExecutorService (java.util.concurrent.ExecutorService)53 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)50 RetryNTimes (org.apache.curator.retry.RetryNTimes)49 ArrayList (java.util.ArrayList)46 RetryPolicy (org.apache.curator.RetryPolicy)36 Stat (org.apache.zookeeper.data.Stat)36 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)35 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)35 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)33