use of org.apache.curator.framework.recipes.leader.LeaderSelector in project xian by happyyangyuan.
the class TestResetConnectionWithBackgroundFailure method testConnectionStateListener.
@Test
public void testConnectionStateListener() throws Exception {
server.stop();
LeaderSelector selector = null;
Timing timing = new Timing();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
try {
client.start();
timing.sleepABit();
LeaderSelectorListener listenerLeader = new LeaderSelectorListenerAdapter() {
@Override
public void takeLeadership(CuratorFramework client) throws Exception {
Thread.currentThread().join();
}
};
selector = new LeaderSelector(client, "/leader", listenerLeader);
selector.autoRequeue();
selector.start();
final BlockingQueue<ConnectionState> listenerSequence = Queues.newLinkedBlockingQueue();
ConnectionStateListener listener1 = new ConnectionStateListener() {
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
listenerSequence.add(newState);
}
};
Timing forWaiting = timing.forWaiting();
client.getConnectionStateListenable().addListener(listener1);
log.debug("Starting ZK server");
server.restart();
Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.CONNECTED);
log.debug("Stopping ZK server");
server.stop();
Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.SUSPENDED);
Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.LOST);
log.debug("Starting ZK server");
server.restart();
Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.RECONNECTED);
log.debug("Stopping ZK server");
server.close();
Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.SUSPENDED);
Assert.assertEquals(listenerSequence.poll(forWaiting.milliseconds(), TimeUnit.MILLISECONDS), ConnectionState.LOST);
} finally {
CloseableUtils.closeQuietly(selector);
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.recipes.leader.LeaderSelector in project xian by happyyangyuan.
the class TestReaper method testUsingManualLeader.
@Test
public void testUsingManualLeader() throws Exception {
final Timing timing = new Timing();
final CuratorFramework client = makeClient(timing, null);
final CountDownLatch latch = new CountDownLatch(1);
LeaderSelectorListener listener = new LeaderSelectorListener() {
@Override
public void takeLeadership(CuratorFramework client) throws Exception {
Reaper reaper = new Reaper(client, 1);
try {
reaper.addPath("/one/two/three", Reaper.Mode.REAP_UNTIL_DELETE);
reaper.start();
timing.sleepABit();
latch.countDown();
} finally {
CloseableUtils.closeQuietly(reaper);
}
}
@Override
public void stateChanged(CuratorFramework client, ConnectionState newState) {
}
};
LeaderSelector selector = new LeaderSelector(client, "/leader", listener);
try {
client.start();
client.create().creatingParentsIfNeeded().forPath("/one/two/three");
Assert.assertNotNull(client.checkExists().forPath("/one/two/three"));
selector.start();
timing.awaitLatch(latch);
Assert.assertNull(client.checkExists().forPath("/one/two/three"));
} finally {
CloseableUtils.closeQuietly(selector);
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.framework.recipes.leader.LeaderSelector in project spring-integration by spring-projects.
the class LeaderInitiator method start.
/**
* Start the registration of the {@link #candidate} for leader election.
*/
@Override
public void start() {
synchronized (this.lifecycleMonitor) {
if (!this.running) {
if (this.client.getState() != CuratorFrameworkState.STARTED) {
// we want to do curator start here because it needs to
// be started before leader selector and it gets a little
// complicated to control ordering via beans so that
// curator is fully started.
this.client.start();
}
this.leaderSelector = new LeaderSelector(this.client, buildLeaderPath(), new LeaderListener());
this.leaderSelector.setId(this.candidate.getId());
this.leaderSelector.autoRequeue();
this.leaderSelector.start();
this.running = true;
logger.debug("Started LeaderInitiator");
}
}
}
use of org.apache.curator.framework.recipes.leader.LeaderSelector in project coprhd-controller by CoprHD.
the class LeaderSelectionTest method leaderSelectionTest.
/**
* Simulates multiple clients accessing persistent lock API simultaneously.
*
* @throws Exception
*/
@Test
public void leaderSelectionTest() throws Exception {
logger.info("*** Leader Seleciton Test start");
ExecutorService clients = Executors.newFixedThreadPool(NUMCLIENTS);
for (int i = 0; i < NUMCLIENTS; i++) {
final int count = i;
clients.submit(new Runnable() {
@Override
public void run() {
String leaderName = LATCH_NAME + '_' + (count + 1);
LeaderSelector leader = null;
try {
TestProcessor processor = new TestProcessor(leaderName);
leader = connectClient().getLeaderSelector(LATCH_PATH, processor);
} catch (Exception e) {
logger.info(": {} leaderSelectionTest could not get coordinator client", e);
Assert.assertNull(e);
return;
}
logger.info(": ### Initialized LeaderSelector {} ###", leaderName);
leader.start();
leader.requeue();
synchronized (leaders) {
leaders.add(leader);
}
}
});
}
synchronized (leaderMonitor) {
while (leaderMonitor.size() < NUMCLIENTS * NUMRUN) {
leaderMonitor.wait();
}
}
synchronized (leaders) {
for (int i = 0; i < NUMCLIENTS; i++) {
leaders.get(i).close();
}
}
synchronized (leaderMonitor) {
for (int i = 0; i < NUMCLIENTS; i++) {
for (int j = 0; j < NUMRUN; j++) {
logger.info("Leadership : " + leaderMonitor.get(i * NUMRUN + j));
}
String first = leaderMonitor.get(0).substring(0, 17);
for (int j = 1; j < NUMRUN; j++) {
Assert.assertTrue(leaderMonitor.get(j).startsWith(first));
}
}
}
logger.info("*** LeaderSelector end");
}
Aggregations