use of org.apache.curator.retry.ExponentialBackoffRetry in project Mycat_plus by coderczp.
the class DistributedSequenceHandler method initializeZK.
public void initializeZK(String zkAddress) {
this.client = CuratorFrameworkFactory.newClient(zkAddress, new ExponentialBackoffRetry(1000, 3));
this.client.start();
try {
if (client.checkExists().forPath(PATH.concat(INSTANCE_PATH)) == null) {
client.create().creatingParentContainersIfNeeded().forPath(PATH.concat(INSTANCE_PATH));
}
} catch (Exception e) {
// do nothing
}
this.leaderSelector = new LeaderSelector(client, PATH.concat(LEADER_PATH), this);
this.leaderSelector.autoRequeue();
this.leaderSelector.start();
Runnable runnable = new Runnable() {
@Override
public void run() {
try {
while (leaderSelector.getLeader() == null) {
Thread.currentThread().yield();
}
if (!leaderSelector.hasLeadership()) {
isLeader = false;
if (slavePath != null && client.checkExists().forPath(slavePath) != null) {
return;
}
slavePath = client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(PATH.concat("/instance/node"), "ready".getBytes());
while ("ready".equals(new String(client.getData().forPath(slavePath)))) {
Thread.currentThread().yield();
}
instanceId = Long.parseLong(new String(client.getData().forPath(slavePath)));
ready = true;
}
} catch (Exception e) {
LOGGER.warn("Caught exception while handling zk!", e);
}
}
};
timerExecutor.scheduleAtFixedRate(runnable, 1L, 10L, TimeUnit.SECONDS);
}
use of org.apache.curator.retry.ExponentialBackoffRetry in project xian by happyyangyuan.
the class TestReadOnly method testReadOnly.
@Test
public void testReadOnly() throws Exception {
Timing timing = new Timing();
CuratorFramework client = null;
TestingCluster cluster = new TestingCluster(2);
try {
cluster.start();
client = CuratorFrameworkFactory.builder().connectString(cluster.getConnectString()).canBeReadOnly(true).connectionTimeoutMs(timing.connection()).sessionTimeoutMs(timing.session()).retryPolicy(new ExponentialBackoffRetry(100, 3)).build();
client.start();
client.create().forPath("/test");
final CountDownLatch readOnlyLatch = new CountDownLatch(1);
final CountDownLatch reconnectedLatch = new CountDownLatch(1);
ConnectionStateListener listener = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
if (newState == ConnectionState.READ_ONLY) {
readOnlyLatch.countDown();
} else if (newState == ConnectionState.RECONNECTED) {
reconnectedLatch.countDown();
}
}
};
client.getConnectionStateListenable().addListener(listener);
InstanceSpec ourInstance = cluster.findConnectionInstance(client.getZookeeperClient().getZooKeeper());
Iterator<InstanceSpec> iterator = cluster.getInstances().iterator();
InstanceSpec killInstance = iterator.next();
if (killInstance.equals(ourInstance)) {
// kill the instance we're not connected to
killInstance = iterator.next();
}
cluster.killServer(killInstance);
Assert.assertEquals(reconnectedLatch.getCount(), 1);
Assert.assertTrue(timing.awaitLatch(readOnlyLatch));
Assert.assertEquals(reconnectedLatch.getCount(), 1);
cluster.restartServer(killInstance);
Assert.assertTrue(timing.awaitLatch(reconnectedLatch));
} finally {
CloseableUtils.closeQuietly(client);
CloseableUtils.closeQuietly(cluster);
}
}
use of org.apache.curator.retry.ExponentialBackoffRetry 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.retry.ExponentialBackoffRetry in project xian by happyyangyuan.
the class TestWithCluster method testSessionSurvives.
@Test
public void testSessionSurvives() throws Exception {
Timing timing = new Timing();
CuratorFramework client = null;
TestingCluster cluster = new TestingCluster(3);
cluster.start();
try {
client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
client.start();
client.create().withMode(CreateMode.EPHEMERAL).forPath("/temp", "value".getBytes());
Assert.assertNotNull(client.checkExists().forPath("/temp"));
for (InstanceSpec spec : cluster.getInstances()) {
cluster.killServer(spec);
timing.forWaiting().sleepABit();
cluster.restartServer(spec);
timing.sleepABit();
}
timing.sleepABit();
Assert.assertNotNull(client.checkExists().forPath("/temp"));
} finally {
CloseableUtils.closeQuietly(client);
CloseableUtils.closeQuietly(cluster);
}
}
use of org.apache.curator.retry.ExponentialBackoffRetry in project xian by happyyangyuan.
the class TestLeaderAcls method testAclErrorWithLeader.
@Test(description = "Validation test for CURATOR-365")
public void testAclErrorWithLeader() throws Exception {
ACLProvider provider = new ACLProvider() {
@Override
public List<ACL> getDefaultAcl() {
return ZooDefs.Ids.OPEN_ACL_UNSAFE;
}
@Override
public List<ACL> getAclForPath(String path) {
if (path.equals("/base")) {
try {
String testDigest = DigestAuthenticationProvider.generateDigest("test:test");
return Collections.singletonList(new ACL(ZooDefs.Perms.ALL, new Id("digest", testDigest)));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}
return getDefaultAcl();
}
};
RetryPolicy retryPolicy = new ExponentialBackoffRetry(timing.milliseconds(), 3);
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(retryPolicy).aclProvider(provider).authorization("digest", "test:test".getBytes());
CuratorFramework client = builder.build();
LeaderLatch latch = null;
try {
client.start();
latch = new LeaderLatch(client, "/base");
latch.start();
Assert.assertTrue(latch.await(timing.forWaiting().seconds(), TimeUnit.SECONDS));
latch.close();
latch = null;
CuratorFramework noAuthClient = CuratorFrameworkFactory.newClient(server.getConnectString(), retryPolicy);
try {
noAuthClient.start();
final CountDownLatch noAuthLatch = new CountDownLatch(1);
UnhandledErrorListener listener = new UnhandledErrorListener() {
@Override
public void unhandledError(String message, Throwable e) {
if (e instanceof KeeperException.NoAuthException) {
noAuthLatch.countDown();
}
}
};
noAuthClient.getUnhandledErrorListenable().addListener(listener);
// use a path below "base" as noAuthClient is not authorized to create nodes in "/base"
// but also making sure that the code goes through the backgroundCreateParentsThenNode() codepath
latch = new LeaderLatch(noAuthClient, "/base/second");
latch.start();
Assert.assertTrue(timing.awaitLatch(noAuthLatch));
} finally {
CloseableUtils.closeQuietly(noAuthClient);
}
} finally {
CloseableUtils.closeQuietly(latch);
CloseableUtils.closeQuietly(client);
}
}
Aggregations