Search in sources :

Example 11 with RaftSession

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);
    }
}
Also used : QueryEntry(io.atomix.protocols.raft.storage.log.entry.QueryEntry) RaftSession(io.atomix.protocols.raft.session.RaftSession) Indexed(io.atomix.storage.journal.Indexed)

Example 12 with RaftSession

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);
}
Also used : RaftSession(io.atomix.protocols.raft.session.RaftSession)

Example 13 with RaftSession

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);
}
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)

Example 14 with RaftSession

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);
        }
    }
}
Also used : RaftSession(io.atomix.protocols.raft.session.RaftSession)

Example 15 with RaftSession

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));
    }
}
Also used : RaftSession(io.atomix.protocols.raft.session.RaftSession)

Aggregations

RaftSession (io.atomix.protocols.raft.session.RaftSession)17 RaftServiceContext (io.atomix.protocols.raft.service.RaftServiceContext)3 PrimitiveType (io.atomix.primitive.PrimitiveType)2 SessionId (io.atomix.primitive.session.SessionId)2 QueryEntry (io.atomix.protocols.raft.storage.log.entry.QueryEntry)2 Indexed (io.atomix.storage.journal.Indexed)2 NodeId (io.atomix.cluster.NodeId)1 SessionMetadata (io.atomix.primitive.session.SessionMetadata)1 ReadConsistency (io.atomix.protocols.raft.ReadConsistency)1 OperationResult (io.atomix.protocols.raft.impl.OperationResult)1 PendingCommand (io.atomix.protocols.raft.impl.PendingCommand)1 RaftContext (io.atomix.protocols.raft.impl.RaftContext)1 RaftServiceManager (io.atomix.protocols.raft.impl.RaftServiceManager)1 CommandResponse (io.atomix.protocols.raft.protocol.CommandResponse)1 QueryResponse (io.atomix.protocols.raft.protocol.QueryResponse)1 RaftServerProtocol (io.atomix.protocols.raft.protocol.RaftServerProtocol)1 TestPrimitiveType (io.atomix.protocols.raft.proxy.impl.TestPrimitiveType)1 RaftSessionRegistry (io.atomix.protocols.raft.session.RaftSessionRegistry)1 ThreadContext (io.atomix.utils.concurrent.ThreadContext)1 ThreadContextFactory (io.atomix.utils.concurrent.ThreadContextFactory)1