use of org.apache.curator.retry.RetryNTimes 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.retry.RetryNTimes in project xian by happyyangyuan.
the class TestLockCleanlinessWithFaults method testNodeDeleted.
@Test
public void testNodeDeleted() throws Exception {
final String PATH = "/foo/bar";
CuratorFramework client = null;
try {
client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryNTimes(0, 0));
client.start();
client.create().creatingParentsIfNeeded().forPath(PATH);
Assert.assertEquals(client.checkExists().forPath(PATH).getNumChildren(), 0);
LockInternals internals = new LockInternals(client, new StandardLockInternalsDriver(), PATH, "lock-", 1) {
@Override
List<String> getSortedChildren() throws Exception {
throw new KeeperException.NoNodeException();
}
};
try {
internals.attemptLock(0, null, null);
Assert.fail();
} catch (KeeperException.NoNodeException dummy) {
// expected
}
// make sure no nodes are left lying around
Assert.assertEquals(client.checkExists().forPath(PATH).getNumChildren(), 0);
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.retry.RetryNTimes in project xian by happyyangyuan.
the class TestLeaderLatch method testNoServerAtStart.
@Test
public void testNoServerAtStart() {
CloseableUtils.closeQuietly(server);
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryNTimes(5, 1000));
client.start();
final LeaderLatch leader = new LeaderLatch(client, PATH_NAME);
final CountDownLatch leaderCounter = new CountDownLatch(1);
final AtomicInteger leaderCount = new AtomicInteger(0);
final AtomicInteger notLeaderCount = new AtomicInteger(0);
leader.addListener(new LeaderLatchListener() {
@Override
public void isLeader() {
leaderCounter.countDown();
leaderCount.incrementAndGet();
}
@Override
public void notLeader() {
notLeaderCount.incrementAndGet();
}
});
try {
leader.start();
timing.sleepABit();
// Start the new server
server = new TestingServer(server.getPort(), server.getTempDirectory());
Assert.assertTrue(timing.awaitLatch(leaderCounter), "Not elected leader");
Assert.assertEquals(leaderCount.get(), 1, "Elected too many times");
Assert.assertEquals(notLeaderCount.get(), 0, "Unelected too many times");
} catch (Exception e) {
Assert.fail("Unexpected exception", e);
} finally {
CloseableUtils.closeQuietly(leader);
CloseableUtils.closeQuietly(client);
CloseableUtils.closeQuietly(server);
}
}
use of org.apache.curator.retry.RetryNTimes in project xian by happyyangyuan.
the class TestLeaderSelectorEdges method createProtectedNodeInBackgroundTest.
/**
* Create a protected node in background with a retry policy
*/
@Test
public void createProtectedNodeInBackgroundTest() throws Exception {
final CuratorFramework client = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(new RetryNTimes(2, 1)).connectionTimeoutMs(100).sessionTimeoutMs(60000).build();
final CountDownLatch latch = new CountDownLatch(1);
client.start();
try {
client.create().forPath(ChaosMonkeyCnxnFactory.CHAOS_ZNODE);
client.create().withProtection().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).inBackground(new BackgroundCallback() {
public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
log.info("Receive event {}", event.toString());
if (event.getResultCode() == KeeperException.Code.CONNECTIONLOSS.intValue()) {
latch.countDown();
}
}
}).forPath(ChaosMonkeyCnxnFactory.CHAOS_ZNODE_PREFIX + "foo-");
Assert.assertTrue(latch.await(30, TimeUnit.SECONDS), "Callback has not been called");
// Wait for the znode to be deleted
Thread.sleep(ChaosMonkeyCnxnFactory.LOCKOUT_DURATION_MS * 2);
// Check that there is no znode
final int children = client.getChildren().forPath(ChaosMonkeyCnxnFactory.CHAOS_ZNODE).size();
Assert.assertEquals(children, 0, "Still " + children + " znodes under " + ChaosMonkeyCnxnFactory.CHAOS_ZNODE + " lock");
} finally {
client.close();
}
}
use of org.apache.curator.retry.RetryNTimes in project drill by axbaretto.
the class TestEphemeralStore method setUp.
@Before
public void setUp() throws Exception {
ZookeeperTestUtil.setZookeeperSaslTestConfigProps();
server = new TestingServer();
final RetryPolicy policy = new RetryNTimes(2, 1000);
curator = CuratorFrameworkFactory.newClient(server.getConnectString(), policy);
config = Mockito.mock(TransientStoreConfig.class);
Mockito.when(config.getName()).thenReturn(root);
Mockito.when(config.getSerializer()).thenReturn(new InstanceSerializer<String>() {
@Override
public byte[] serialize(final String instance) throws IOException {
if (instance == null) {
return null;
}
return instance.getBytes();
}
@Override
public String deserialize(final byte[] raw) throws IOException {
if (raw == null) {
return null;
}
return new String(raw);
}
});
store = new ZkEphemeralStore<>(config, curator);
server.start();
curator.start();
store.start();
}
Aggregations