Search in sources :

Example 1 with ExecuteOperation

use of io.atomix.protocols.backup.protocol.ExecuteOperation in project atomix by atomix.

the class PrimaryRole method executeQuery.

private CompletableFuture<ExecuteResponse> executeQuery(ExecuteRequest request) {
    // If the session doesn't exist, create and replicate a new session before applying the query.
    Session session = context.getSession(request.session());
    if (session == null) {
        Session newSession = context.createSession(request.session(), request.node());
        long index = context.nextIndex();
        long timestamp = System.currentTimeMillis();
        return replicator.replicate(new ExecuteOperation(index, timestamp, newSession.sessionId().id(), newSession.nodeId(), null)).thenApply(v -> {
            context.setIndex(index);
            context.setTimestamp(timestamp);
            return applyQuery(request, newSession);
        });
    } else {
        return CompletableFuture.completedFuture(applyQuery(request, session));
    }
}
Also used : ExecuteOperation(io.atomix.protocols.backup.protocol.ExecuteOperation) PrimaryBackupSession(io.atomix.protocols.backup.impl.PrimaryBackupSession) Session(io.atomix.primitive.session.Session)

Example 2 with ExecuteOperation

use of io.atomix.protocols.backup.protocol.ExecuteOperation in project atomix by atomix.

the class PrimaryRole method executeCommand.

private CompletableFuture<ExecuteResponse> executeCommand(ExecuteRequest request) {
    PrimaryBackupSession session = context.getOrCreateSession(request.session(), request.node());
    long index = context.nextIndex();
    long timestamp = System.currentTimeMillis();
    return replicator.replicate(new ExecuteOperation(index, timestamp, session.sessionId().id(), session.nodeId(), request.operation())).thenApply(v -> {
        try {
            byte[] result = context.service().apply(new DefaultCommit<>(context.setIndex(index), request.operation().id(), request.operation().value(), context.setSession(session), context.setTimestamp(timestamp)));
            return ExecuteResponse.ok(result);
        } catch (Exception e) {
            return ExecuteResponse.error();
        } finally {
            context.setSession(null);
        }
    });
}
Also used : PrimaryBackupSession(io.atomix.protocols.backup.impl.PrimaryBackupSession) ExecuteOperation(io.atomix.protocols.backup.protocol.ExecuteOperation)

Aggregations

PrimaryBackupSession (io.atomix.protocols.backup.impl.PrimaryBackupSession)2 ExecuteOperation (io.atomix.protocols.backup.protocol.ExecuteOperation)2 Session (io.atomix.primitive.session.Session)1