Search in sources :

Example 1 with AsyncLeaderElector

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());
}
Also used : LeaderElectorBuilder(io.atomix.core.election.LeaderElectorBuilder) AsyncLeaderElector(io.atomix.core.election.AsyncLeaderElector) LeaderElector(io.atomix.core.election.LeaderElector) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) CompletableFuture(java.util.concurrent.CompletableFuture) Maps(com.google.common.collect.Maps) PartitionGroup(io.atomix.primitive.partition.PartitionGroup) PartitionId(io.atomix.primitive.partition.PartitionId) ArrayList(java.util.ArrayList) Partition(io.atomix.primitive.partition.Partition) PrimitiveProxy(io.atomix.primitive.proxy.PrimitiveProxy) PrimitiveManagementService(io.atomix.primitive.PrimitiveManagementService) PrimitiveProtocol(io.atomix.primitive.PrimitiveProtocol) Map(java.util.Map) Partitioner(io.atomix.primitive.partition.Partitioner) Futures(io.atomix.utils.concurrent.Futures) Partition(io.atomix.primitive.partition.Partition) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) PartitionId(io.atomix.primitive.partition.PartitionId) PrimitiveProtocol(io.atomix.primitive.PrimitiveProtocol)

Example 2 with AsyncLeaderElector

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());
}
Also used : NodeId(io.atomix.cluster.NodeId) AsyncLeaderElector(io.atomix.core.election.AsyncLeaderElector) LeadershipEvent(io.atomix.core.election.LeadershipEvent) LeadershipEventListener(io.atomix.core.election.LeadershipEventListener) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) CompletableFuture(java.util.concurrent.CompletableFuture) Ignore(org.junit.Ignore) Assert.assertFalse(org.junit.Assert.assertFalse) Queue(java.util.Queue) LinkedList(java.util.LinkedList) AbstractPrimitiveTest(io.atomix.core.AbstractPrimitiveTest) Leadership(io.atomix.core.election.Leadership) Assert.assertEquals(org.junit.Assert.assertEquals) Leadership(io.atomix.core.election.Leadership) NodeId(io.atomix.cluster.NodeId) Test(org.junit.Test) AbstractPrimitiveTest(io.atomix.core.AbstractPrimitiveTest)

Aggregations

AsyncLeaderElector (io.atomix.core.election.AsyncLeaderElector)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)1 Maps (com.google.common.collect.Maps)1 NodeId (io.atomix.cluster.NodeId)1 AbstractPrimitiveTest (io.atomix.core.AbstractPrimitiveTest)1 LeaderElector (io.atomix.core.election.LeaderElector)1 LeaderElectorBuilder (io.atomix.core.election.LeaderElectorBuilder)1 Leadership (io.atomix.core.election.Leadership)1 LeadershipEvent (io.atomix.core.election.LeadershipEvent)1 LeadershipEventListener (io.atomix.core.election.LeadershipEventListener)1 PrimitiveManagementService (io.atomix.primitive.PrimitiveManagementService)1 PrimitiveProtocol (io.atomix.primitive.PrimitiveProtocol)1 Partition (io.atomix.primitive.partition.Partition)1 PartitionGroup (io.atomix.primitive.partition.PartitionGroup)1 PartitionId (io.atomix.primitive.partition.PartitionId)1 Partitioner (io.atomix.primitive.partition.Partitioner)1 PrimitiveProxy (io.atomix.primitive.proxy.PrimitiveProxy)1 Futures (io.atomix.utils.concurrent.Futures)1 ArrayList (java.util.ArrayList)1