use of io.atomix.protocols.raft.session.RaftSession in project atomix by atomix.
the class PassiveRole method onQuery.
@Override
public CompletableFuture<QueryResponse> onQuery(QueryRequest request) {
raft.checkThread();
logRequest(request);
// doesn't exist if the follower hasn't had a chance to see the session's registration entry.
if (raft.getState() != RaftContext.State.READY || raft.getLastApplied() < request.session()) {
log.trace("State out of sync, forwarding query to leader");
return queryForward(request);
}
// Look up the client's session.
RaftSession session = raft.getSessions().getSession(request.session());
if (session == null) {
log.trace("State out of sync, forwarding query to leader");
return queryForward(request);
}
// If the session's consistency level is SEQUENTIAL, handle the request here, otherwise forward it.
if (session.readConsistency() == ReadConsistency.SEQUENTIAL) {
// Forward the request to the leader.
if (raft.getLogWriter().getLastIndex() < raft.getCommitIndex()) {
log.trace("State out of sync, forwarding query to leader");
return queryForward(request);
}
final Indexed<QueryEntry> entry = new Indexed<>(request.index(), new QueryEntry(raft.getTerm(), System.currentTimeMillis(), request.session(), request.sequenceNumber(), request.operation()), 0);
return applyQuery(entry).thenApply(this::logResponse);
} else {
return queryForward(request);
}
}
use of io.atomix.protocols.raft.session.RaftSession in project atomix by atomix.
the class RaftServiceContext method takeSnapshot.
/**
* Takes a snapshot of the service state.
*/
public void takeSnapshot(SnapshotWriter writer) {
log.debug("Taking snapshot {}", writer.snapshot().index());
// Serialize sessions to the in-memory snapshot and request a snapshot from the state machine.
writer.writeLong(primitiveId.id());
writer.writeString(primitiveType.id());
writer.writeString(serviceName);
writer.writeInt(sessions.getSessions().size());
for (RaftSession session : sessions.getSessions()) {
writer.writeLong(session.sessionId().id());
writer.writeString(session.nodeId().id());
writer.writeString(session.readConsistency().name());
writer.writeLong(session.minTimeout());
writer.writeLong(session.maxTimeout());
writer.writeLong(session.getLastUpdated());
writer.writeLong(session.getRequestSequence());
writer.writeLong(session.getCommandSequence());
writer.writeLong(session.getEventIndex());
writer.writeLong(session.getLastCompleted());
}
service.backup(writer);
}
use of io.atomix.protocols.raft.session.RaftSession 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);
}
use of io.atomix.protocols.raft.session.RaftSession in project atomix by atomix.
the class RaftServiceContext method expireSessions.
/**
* Expires sessions that have timed out.
*/
private void expireSessions(long timestamp) {
// Iterate through registered sessions.
for (RaftSession session : sessions.getSessions()) {
if (session.isTimedOut(timestamp)) {
log.debug("Session expired in {} milliseconds: {}", timestamp - session.getLastUpdated(), session);
sessions.expireSession(session);
}
}
}
use of io.atomix.protocols.raft.session.RaftSession in project atomix by atomix.
the class RaftSessions method closeSession.
/**
* Closes and removes a session from the sessions list.
*
* @param session The session to remove.
*/
void closeSession(RaftSession session) {
final RaftSession singletonSession = sessionManager.removeSession(session.sessionId());
if (singletonSession != null) {
singletonSession.close();
listeners.forEach(l -> l.onClose(singletonSession));
}
}
Aggregations