use of io.atomix.core.election.AsyncLeaderElector in project atomix by atomix.
the class LeaderElectorProxyBuilder method buildAsync.
@Override
@SuppressWarnings("unchecked")
public CompletableFuture<LeaderElector<T>> buildAsync() {
PrimitiveProtocol protocol = protocol();
PartitionGroup<?> partitions = managementService.getPartitionService().getPartitionGroup(protocol);
Map<PartitionId, CompletableFuture<AsyncLeaderElector<T>>> electors = Maps.newConcurrentMap();
for (Partition partition : partitions.getPartitions()) {
electors.put(partition.id(), newLeaderElector(partition.getPrimitiveClient().newProxy(name(), primitiveType(), protocol)));
}
Partitioner<String> partitioner = topic -> partitions.getPartition(topic).id();
return Futures.allOf(new ArrayList<>(electors.values())).thenApply(e -> new PartitionedAsyncLeaderElector<T>(name(), Maps.transformValues(electors, v -> v.getNow(null)), partitioner).sync());
}
use of io.atomix.core.election.AsyncLeaderElector in project atomix by atomix.
the class LeaderElectorTest method testWithdraw.
@Test
public void testWithdraw() throws Throwable {
AsyncLeaderElector<NodeId> elector1 = atomix().<NodeId>leaderElectorBuilder("test-elector-withdraw", protocol()).build().async();
elector1.run("foo", node1).join();
AsyncLeaderElector<NodeId> elector2 = atomix().<NodeId>leaderElectorBuilder("test-elector-withdraw", protocol()).build().async();
elector2.run("foo", node2).join();
LeaderEventListener listener1 = new LeaderEventListener();
elector1.addListener("foo", listener1).join();
LeaderEventListener listener2 = new LeaderEventListener();
elector2.addListener("foo", listener2).join();
LeaderEventListener listener3 = new LeaderEventListener();
elector1.addListener("bar", listener3);
elector2.addListener("bar", listener3);
elector1.withdraw("foo", node1).join();
listener1.nextEvent().thenAccept(result -> {
assertEquals(node2, result.newLeadership().leader().id());
assertEquals(2, result.newLeadership().leader().term());
assertEquals(1, result.newLeadership().candidates().size());
assertEquals(node2, result.newLeadership().candidates().get(0));
}).join();
listener2.nextEvent().thenAccept(result -> {
assertEquals(node2, result.newLeadership().leader().id());
assertEquals(2, result.newLeadership().leader().term());
assertEquals(1, result.newLeadership().candidates().size());
assertEquals(node2, result.newLeadership().candidates().get(0));
}).join();
assertFalse(listener3.hasEvent());
Leadership leadership1 = elector1.getLeadership("foo").join();
assertEquals(node2, leadership1.leader().id());
assertEquals(1, leadership1.candidates().size());
Leadership leadership2 = elector2.getLeadership("foo").join();
assertEquals(node2, leadership2.leader().id());
assertEquals(1, leadership2.candidates().size());
}
Aggregations