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;
}
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));
}
}
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"));
}
}
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());
}
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);
}
Aggregations