use of org.apache.curator.framework.state.ConnectionStateListener 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);
}
}
use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.
the class TestPersistentEphemeralNode method testNoServerAtStart.
@Test
public void testNoServerAtStart() throws Exception {
server.stop();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
PersistentEphemeralNode node = new PersistentEphemeralNode(client, PersistentEphemeralNode.Mode.EPHEMERAL, "/abc/node", "hello".getBytes());
node.start();
final CountDownLatch connectedLatch = new CountDownLatch(1);
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.CONNECTED) {
connectedLatch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(listener);
timing.sleepABit();
server.restart();
Assert.assertTrue(timing.awaitLatch(connectedLatch));
timing.sleepABit();
Assert.assertTrue(node.waitForInitialCreate(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.
the class TestPersistentEphemeralNode method testListenersReconnectedIsFast.
@Test
public void testListenersReconnectedIsFast() throws Exception {
server.stop();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
PersistentEphemeralNode node = new PersistentEphemeralNode(client, PersistentEphemeralNode.Mode.EPHEMERAL, "/abc/node", "hello".getBytes());
node.start();
final CountDownLatch connectedLatch = new CountDownLatch(1);
final CountDownLatch reconnectedLatch = new CountDownLatch(1);
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.CONNECTED) {
connectedLatch.countDown();
}
if (newState == ConnectionState.RECONNECTED) {
reconnectedLatch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(listener);
timing.sleepABit();
server.restart();
Assert.assertTrue(timing.awaitLatch(connectedLatch));
timing.sleepABit();
Assert.assertTrue(node.waitForInitialCreate(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS));
server.stop();
timing.sleepABit();
server.restart();
timing.sleepABit();
Assert.assertTrue(timing.awaitLatch(reconnectedLatch));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.
the class TestPersistentEphemeralNodeListener method testListenersReconnectedIsOK.
@Test
public void testListenersReconnectedIsOK() throws Exception {
server.stop();
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
PersistentEphemeralNode node = new PersistentEphemeralNode(client, PersistentEphemeralNode.Mode.EPHEMERAL, "/abc/node", "hello".getBytes());
node.start();
final CountDownLatch connectedLatch = new CountDownLatch(1);
final CountDownLatch reconnectedLatch = new CountDownLatch(1);
final AtomicReference<ConnectionState> lastState = new AtomicReference<ConnectionState>();
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
lastState.set(newState);
if (newState == ConnectionState.CONNECTED) {
connectedLatch.countDown();
}
if (newState == ConnectionState.RECONNECTED) {
reconnectedLatch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(listener);
timing.sleepABit();
server.restart();
Assert.assertTrue(timing.awaitLatch(connectedLatch));
timing.sleepABit();
Assert.assertTrue(node.waitForInitialCreate(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS));
server.restart();
timing.sleepABit();
Assert.assertTrue(timing.awaitLatch(reconnectedLatch));
timing.sleepABit();
Assert.assertEquals(lastState.get(), ConnectionState.RECONNECTED);
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.
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