use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.
the class TestInterProcessSemaphore method testAcquireAfterLostServer.
@Test
public void testAcquireAfterLostServer() throws Exception {
// CURATOR-335
final String SEMAPHORE_PATH = "/test";
final int MAX_SEMAPHORES = 1;
final int NUM_CLIENTS = 10;
ExecutorService executor = Executors.newFixedThreadPool(NUM_CLIENTS);
final Timing timing = new Timing();
// long session time on purpose
final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.forWaiting().milliseconds(), timing.connection(), new RetryOneTime(1));
try {
client.start();
// cause one of the semaphores to create its node and then wait
InterProcessSemaphoreV2.debugAcquireLatch = new CountDownLatch(1);
// semaphore will notify when getChildren() fails
InterProcessSemaphoreV2.debugFailedGetChildrenLatch = new CountDownLatch(1);
final CountDownLatch isReadyLatch = new CountDownLatch(NUM_CLIENTS);
final BlockingQueue<Boolean> acquiredQueue = Queues.newLinkedBlockingQueue();
Runnable runner = new Runnable() {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, SEMAPHORE_PATH, MAX_SEMAPHORES);
Lease lease = null;
try {
isReadyLatch.countDown();
lease = semaphore.acquire();
acquiredQueue.add(true);
timing.sleepABit();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
} catch (KeeperException e) {
try {
timing.sleepABit();
} catch (InterruptedException e2) {
Thread.currentThread().interrupt();
break;
}
} catch (Exception ignore) {
// ignore
} finally {
if (lease != null) {
semaphore.returnLease(lease);
}
}
}
}
};
for (int i = 0; i < NUM_CLIENTS; ++i) {
executor.execute(runner);
}
Assert.assertTrue(timing.awaitLatch(isReadyLatch));
timing.sleepABit();
final CountDownLatch lostLatch = new CountDownLatch(1);
final CountDownLatch restartedLatch = new CountDownLatch(1);
client.getConnectionStateListenable().addListener(new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.LOST) {
lostLatch.countDown();
} else if (newState == ConnectionState.RECONNECTED) {
restartedLatch.countDown();
}
}
});
timing.sleepABit();
server.stop();
Assert.assertTrue(timing.awaitLatch(lostLatch));
// the waiting semaphore proceeds to getChildren - which should fail
InterProcessSemaphoreV2.debugAcquireLatch.countDown();
// wait until getChildren fails
Assert.assertTrue(timing.awaitLatch(InterProcessSemaphoreV2.debugFailedGetChildrenLatch));
server.restart();
Assert.assertTrue(timing.awaitLatch(restartedLatch));
for (int i = 0; i < NUM_CLIENTS; ++i) {
// acquires should continue as normal after server restart
Boolean polled = acquiredQueue.poll(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS);
if ((polled == null) || !polled) {
Assert.fail("Semaphores not reacquired after restart");
}
}
} finally {
executor.shutdownNow();
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.
the class TestInterProcessSemaphoreCluster method testCluster.
@Test
public void testCluster() throws Exception {
final int QTY = 20;
final int OPERATION_TIME_MS = 1000;
final String PATH = "/foo/bar/lock";
ExecutorService executorService = Executors.newFixedThreadPool(QTY);
ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<Void>(executorService);
final Timing timing = new Timing();
TestingCluster cluster = new TestingCluster(3);
List<SemaphoreClient> semaphoreClients = Lists.newArrayList();
try {
cluster.start();
final AtomicInteger opCount = new AtomicInteger(0);
for (int i = 0; i < QTY; ++i) {
SemaphoreClient semaphoreClient = new SemaphoreClient(cluster.getConnectString(), PATH, new Callable<Void>() {
@Override
public Void call() throws Exception {
opCount.incrementAndGet();
Thread.sleep(OPERATION_TIME_MS);
return null;
}
});
completionService.submit(semaphoreClient);
semaphoreClients.add(semaphoreClient);
}
timing.forWaiting().sleepABit();
Assert.assertNotNull(SemaphoreClient.getActiveClient());
final CountDownLatch latch = new CountDownLatch(1);
CuratorFramework client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.LOST) {
latch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(listener);
client.start();
try {
client.getZookeeperClient().blockUntilConnectedOrTimedOut();
cluster.stop();
latch.await();
} finally {
CloseableUtils.closeQuietly(client);
}
long startTicks = System.currentTimeMillis();
for (; ; ) {
int thisOpCount = opCount.get();
Thread.sleep(2 * OPERATION_TIME_MS);
if (thisOpCount == opCount.get()) {
// checking that the op count isn't increasing
break;
}
Assert.assertTrue((System.currentTimeMillis() - startTicks) < timing.forWaiting().milliseconds());
}
int thisOpCount = opCount.get();
Iterator<InstanceSpec> iterator = cluster.getInstances().iterator();
cluster = new TestingCluster(iterator.next(), iterator.next());
cluster.start();
timing.forWaiting().sleepABit();
startTicks = System.currentTimeMillis();
for (; ; ) {
Thread.sleep(2 * OPERATION_TIME_MS);
if (opCount.get() > thisOpCount) {
// checking that semaphore has started working again
break;
}
Assert.assertTrue((System.currentTimeMillis() - startTicks) < timing.forWaiting().milliseconds());
}
} finally {
for (SemaphoreClient semaphoreClient : semaphoreClients) {
CloseableUtils.closeQuietly(semaphoreClient);
}
CloseableUtils.closeQuietly(cluster);
executorService.shutdownNow();
}
}
use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.
the class TestSharedCount method testDisconnectEventOnWatcherDoesNotRetry.
@Test
public void testDisconnectEventOnWatcherDoesNotRetry() throws Exception {
final CountDownLatch gotSuspendEvent = new CountDownLatch(1);
CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryNTimes(10, 1000));
curatorFramework.start();
curatorFramework.blockUntilConnected();
SharedCount sharedCount = new SharedCount(curatorFramework, "/count", 10);
sharedCount.start();
curatorFramework.getConnectionStateListenable().addListener(new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.SUSPENDED) {
gotSuspendEvent.countDown();
}
}
});
try {
server.stop();
// if watcher goes into 10second retry loop we won't get timely notification
Assert.assertTrue(gotSuspendEvent.await(5, TimeUnit.SECONDS));
} finally {
CloseableUtils.closeQuietly(sharedCount);
CloseableUtils.closeQuietly(curatorFramework);
}
}
use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.
the class TestPathChildrenCache method testWithBadConnect.
@Test
public void testWithBadConnect() throws Exception {
final int serverPort = server.getPort();
server.close();
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), 1000, 1000, new RetryOneTime(1));
try {
client.start();
final CountDownLatch ensurePathLatch = new CountDownLatch(1);
PathChildrenCache cache = new PathChildrenCache(client, "/", true) {
@Override
protected void ensurePath() throws Exception {
try {
super.ensurePath();
} catch (Exception e) {
ensurePathLatch.countDown();
throw e;
}
}
};
final CountDownLatch addedLatch = new CountDownLatch(1);
PathChildrenCacheListener listener = new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
if (event.getType() == PathChildrenCacheEvent.Type.CHILD_ADDED && event.getData().getPath().equals("/baz")) {
addedLatch.countDown();
}
}
};
cache.getListenable().addListener(listener);
cache.start();
Assert.assertTrue(timing.awaitLatch(ensurePathLatch));
final CountDownLatch connectedLatch = new CountDownLatch(1);
client.getConnectionStateListenable().addListener(new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.CONNECTED) {
connectedLatch.countDown();
}
}
});
server = new TestingServer(serverPort, true);
Assert.assertTrue(timing.awaitLatch(connectedLatch));
client.create().creatingParentContainersIfNeeded().forPath("/baz", new byte[] { 1, 2, 3 });
assertNotNull("/baz does not exist", client.checkExists().forPath("/baz"));
Assert.assertTrue(timing.awaitLatch(addedLatch));
assertNotNull("cache doesn't see /baz", cache.getCurrentData("/baz"));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.state.ConnectionStateListener in project fabric8 by jboss-fuse.
the class CuratorFrameworkImpl method start.
@Override
public void start() {
log.info("Starting");
if (!state.compareAndSet(CuratorFrameworkState.LATENT, CuratorFrameworkState.STARTED)) {
throw new IllegalStateException("Cannot be started more than once");
}
try {
// ordering dependency - must be called before client.start()
connectionStateManager.start();
final ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (ConnectionState.CONNECTED == newState || ConnectionState.RECONNECTED == newState) {
logAsErrorConnectionErrors.set(true);
}
}
};
this.getConnectionStateListenable().addListener(listener);
client.start();
executorService = Executors.newSingleThreadScheduledExecutor(threadFactory);
executorService.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
backgroundOperationsLoop();
return null;
}
});
} catch (Exception e) {
ThreadUtils.checkInterrupted(e);
handleBackgroundOperationException(null, e);
}
}
Aggregations