use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.
the class TestFailedDeleteManager method testLostSession.
@Test
public void testLostSession() throws Exception {
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
try {
client.start();
client.create().forPath("/test-me");
final CountDownLatch latch = new CountDownLatch(1);
final Semaphore semaphore = new Semaphore(0);
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if ((newState == ConnectionState.LOST) || (newState == ConnectionState.SUSPENDED)) {
semaphore.release();
} else if (newState == ConnectionState.RECONNECTED) {
latch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(listener);
server.stop();
Assert.assertTrue(timing.acquireSemaphore(semaphore));
try {
client.delete().guaranteed().forPath("/test-me");
Assert.fail();
} catch (KeeperException.ConnectionLossException e) {
// expected
}
Assert.assertTrue(timing.acquireSemaphore(semaphore));
timing.sleepABit();
server.restart();
Assert.assertTrue(timing.awaitLatch(latch));
timing.sleepABit();
Assert.assertNull(client.checkExists().forPath("/test-me"));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.
the class TestWithCluster method testSplitBrain.
@Test
public void testSplitBrain() throws Exception {
Timing timing = new Timing();
CuratorFramework client = null;
TestingCluster cluster = new TestingCluster(3);
cluster.start();
try {
// make sure all instances are up
for (InstanceSpec instanceSpec : cluster.getInstances()) {
client = CuratorFrameworkFactory.newClient(instanceSpec.getConnectString(), new RetryOneTime(1));
client.start();
client.checkExists().forPath("/");
client.close();
client = null;
}
client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
client.start();
final CountDownLatch latch = new CountDownLatch(2);
client.getConnectionStateListenable().addListener(new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if ((newState == ConnectionState.SUSPENDED) || (newState == ConnectionState.LOST)) {
latch.countDown();
}
}
});
client.checkExists().forPath("/");
for (InstanceSpec instanceSpec : cluster.getInstances()) {
if (!instanceSpec.equals(cluster.findConnectionInstance(client.getZookeeperClient().getZooKeeper()))) {
Assert.assertTrue(cluster.killServer(instanceSpec));
}
}
Assert.assertTrue(timing.awaitLatch(latch));
} finally {
CloseableUtils.closeQuietly(client);
CloseableUtils.closeQuietly(cluster);
}
}
use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.
the class TestLeaderLatch method testLostConnection.
@Test
public void testLostConnection() throws Exception {
final int PARTICIPANT_QTY = 10;
List<LeaderLatch> latches = Lists.newArrayList();
final Timing timing = new Timing();
final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
final CountDownLatch countDownLatch = new CountDownLatch(1);
client.getConnectionStateListenable().addListener(new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.LOST) {
countDownLatch.countDown();
}
}
});
for (int i = 0; i < PARTICIPANT_QTY; ++i) {
LeaderLatch latch = new LeaderLatch(client, PATH_NAME);
latch.start();
latches.add(latch);
}
waitForALeader(latches, timing);
server.stop();
Assert.assertTrue(timing.awaitLatch(countDownLatch));
timing.forWaiting().sleepABit();
Assert.assertEquals(getLeaders(latches).size(), 0);
server.restart();
// should reconnect
Assert.assertEquals(waitForALeader(latches, timing).size(), 1);
} finally {
for (LeaderLatch latch : latches) {
CloseableUtils.closeQuietly(latch);
}
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.
the class TestLeaderSelector method testLeaderNodeDeleteOnInterrupt.
@Test
public void testLeaderNodeDeleteOnInterrupt() throws Exception {
Timing timing = new Timing();
LeaderSelector selector = null;
CuratorFramework client = null;
try {
client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
final CountDownLatch reconnectedLatch = new CountDownLatch(1);
ConnectionStateListener connectionStateListener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.RECONNECTED) {
reconnectedLatch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(connectionStateListener);
client.start();
final BlockingQueue<Thread> queue = new ArrayBlockingQueue<Thread>(1);
LeaderSelectorListener listener = new LeaderSelectorListener() {
@Override
public void takeLeadership(CuratorFramework client) throws Exception {
queue.add(Thread.currentThread());
try {
Thread.currentThread().join();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
}
};
selector = new LeaderSelector(client, "/leader", listener);
selector.start();
Thread leaderThread = queue.take();
server.stop();
leaderThread.interrupt();
server.restart();
Assert.assertTrue(timing.awaitLatch(reconnectedLatch));
timing.sleepABit();
Assert.assertEquals(client.getChildren().forPath("/leader").size(), 0);
} finally {
CloseableUtils.closeQuietly(selector);
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.state.ConnectionStateListener in project xian by happyyangyuan.
the class TestInterProcessMutexBase method testWaitingProcessKilledServer.
@Test
public void testWaitingProcessKilledServer() throws Exception {
final Timing timing = new Timing();
final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
try {
client.start();
final CountDownLatch latch = new CountDownLatch(1);
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.LOST) {
latch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(listener);
final AtomicBoolean isFirst = new AtomicBoolean(true);
ExecutorCompletionService<Object> service = new ExecutorCompletionService<Object>(Executors.newFixedThreadPool(2));
for (int i = 0; i < 2; ++i) {
service.submit(new Callable<Object>() {
@Override
public Object call() throws Exception {
InterProcessLock lock = makeLock(client);
lock.acquire();
try {
if (isFirst.compareAndSet(true, false)) {
timing.sleepABit();
server.stop();
Assert.assertTrue(timing.awaitLatch(latch));
server.restart();
}
} finally {
try {
lock.release();
} catch (Exception e) {
// ignore
}
}
return null;
}
});
}
for (int i = 0; i < 2; ++i) {
service.take().get(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS);
}
} finally {
CloseableUtils.closeQuietly(client);
}
}
Aggregations