Search in sources :

Example 11 with NodeId

use of io.atomix.cluster.NodeId in project atomix by atomix.

the class MemberSelector method next.

@Override
public NodeId next() {
    if (selectionsIterator == null) {
        selectionsIterator = selections.iterator();
    }
    NodeId selection = selectionsIterator.next();
    this.selection = selection;
    return selection;
}
Also used : NodeId(io.atomix.cluster.NodeId)

Example 12 with NodeId

use of io.atomix.cluster.NodeId in project atomix by atomix.

the class MemberSelectorManager method resetAll.

/**
 * Resets all child selectors.
 *
 * @param leader  The current cluster leader.
 * @param members The collection of all active members.
 */
public void resetAll(NodeId leader, Collection<NodeId> members) {
    NodeId oldLeader = this.leader;
    this.leader = leader;
    this.members = Lists.newLinkedList(members);
    selectors.forEach(s -> s.reset(leader, this.members));
    if (!Objects.equals(oldLeader, leader)) {
        leaderChangeListeners.forEach(l -> l.accept(leader));
    }
}
Also used : NodeId(io.atomix.cluster.NodeId)

Example 13 with NodeId

use of io.atomix.cluster.NodeId in project atomix by atomix.

the class RaftProxyConnection method sendRequest.

/**
 * Sends the given request attempt to the cluster.
 */
protected <T extends RaftRequest, U extends RaftResponse> void sendRequest(T request, BiFunction<NodeId, T, CompletableFuture<U>> sender, int count, CompletableFuture<U> future) {
    NodeId node = next();
    if (node != null) {
        log.trace("Sending {} to {}", request, node);
        int selectionId = this.selectionId;
        sender.apply(node, request).whenCompleteAsync((r, e) -> {
            if (e != null || r != null) {
                handleResponse(request, sender, count, selectionId, node, r, e, future);
            } else {
                future.complete(null);
            }
        }, context);
    } else {
        future.completeExceptionally(new ConnectException("Failed to connect to the cluster"));
    }
}
Also used : NodeId(io.atomix.cluster.NodeId) ConnectException(java.net.ConnectException)

Example 14 with NodeId

use of io.atomix.cluster.NodeId 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)

Example 15 with NodeId

use of io.atomix.cluster.NodeId in project atomix by atomix.

the class RaftServiceContext method installSnapshot.

/**
 * Installs a snapshot.
 */
public void installSnapshot(SnapshotReader reader) {
    log.debug("Installing snapshot {}", reader.snapshot().index());
    // Skip the service ID
    reader.skip(Bytes.LONG);
    PrimitiveType primitiveType = raft.getPrimitiveTypes().get(reader.readString());
    String serviceName = reader.readString();
    int sessionCount = reader.readInt();
    for (int i = 0; i < sessionCount; i++) {
        SessionId sessionId = SessionId.from(reader.readLong());
        NodeId node = NodeId.from(reader.readString());
        ReadConsistency readConsistency = ReadConsistency.valueOf(reader.readString());
        long minTimeout = reader.readLong();
        long maxTimeout = reader.readLong();
        long sessionTimestamp = reader.readLong();
        // Only create a new session if one does not already exist. This is necessary to ensure only a single session
        // is ever opened and exposed to the state machine.
        RaftSession session = raft.getSessions().addSession(new RaftSession(sessionId, node, serviceName, primitiveType, readConsistency, minTimeout, maxTimeout, sessionTimestamp, this, raft, threadContextFactory));
        session.setRequestSequence(reader.readLong());
        session.setCommandSequence(reader.readLong());
        session.setEventIndex(reader.readLong());
        session.setLastCompleted(reader.readLong());
        session.setLastApplied(reader.snapshot().index());
        session.setLastUpdated(sessionTimestamp);
        sessions.openSession(session);
    }
    currentIndex = reader.snapshot().index();
    currentTimestamp = reader.snapshot().timestamp().unixTimestamp();
    service.restore(reader);
}
Also used : ReadConsistency(io.atomix.protocols.raft.ReadConsistency) RaftSession(io.atomix.protocols.raft.session.RaftSession) NodeId(io.atomix.cluster.NodeId) PrimitiveType(io.atomix.primitive.PrimitiveType) SessionId(io.atomix.primitive.session.SessionId)

Aggregations

NodeId (io.atomix.cluster.NodeId)17 CompletableFuture (java.util.concurrent.CompletableFuture)7 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)5 Collection (java.util.Collection)5 MoreObjects.toStringHelper (com.google.common.base.MoreObjects.toStringHelper)4 Futures (io.atomix.utils.concurrent.Futures)4 ArrayList (java.util.ArrayList)4 Collections (java.util.Collections)4 Map (java.util.Map)4 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4 Logger (org.slf4j.Logger)4 RaftError (io.atomix.protocols.raft.RaftError)3 RaftServer (io.atomix.protocols.raft.RaftServer)3 RaftCluster (io.atomix.protocols.raft.cluster.RaftCluster)3 RaftClusterEvent (io.atomix.protocols.raft.cluster.RaftClusterEvent)3 RaftClusterEventListener (io.atomix.protocols.raft.cluster.RaftClusterEventListener)3 RaftMember (io.atomix.protocols.raft.cluster.RaftMember)3 RaftContext (io.atomix.protocols.raft.impl.RaftContext)3 JoinRequest (io.atomix.protocols.raft.protocol.JoinRequest)3