Search in sources :

Example 1 with CommandEntry

use of io.atomix.protocols.raft.storage.log.entry.CommandEntry in project atomix by atomix.

the class RaftServiceManagerTest method testInstallSnapshotOnApply.

@Test
public void testInstallSnapshotOnApply() throws Exception {
    RaftLogWriter writer = raft.getLogWriter();
    writer.append(new InitializeEntry(1, System.currentTimeMillis()));
    writer.append(new OpenSessionEntry(1, System.currentTimeMillis(), "test-1", "test", "test", null, ReadConsistency.LINEARIZABLE, 100, 1000));
    writer.commit(2);
    RaftServiceManager manager = raft.getServiceManager();
    manager.apply(2).join();
    Snapshot snapshot = manager.snapshot();
    assertEquals(2, snapshot.index());
    assertTrue(snapshotTaken.get());
    snapshot.complete();
    assertEquals(2, raft.getSnapshotStore().getCurrentSnapshot().index());
    writer.append(new CommandEntry(1, System.currentTimeMillis(), 2, 1, new PrimitiveOperation(RUN, new byte[0])));
    writer.commit(3);
    manager.apply(3).join();
    assertTrue(snapshotInstalled.get());
}
Also used : PrimitiveOperation(io.atomix.primitive.operation.PrimitiveOperation) Snapshot(io.atomix.protocols.raft.storage.snapshot.Snapshot) CommandEntry(io.atomix.protocols.raft.storage.log.entry.CommandEntry) OpenSessionEntry(io.atomix.protocols.raft.storage.log.entry.OpenSessionEntry) InitializeEntry(io.atomix.protocols.raft.storage.log.entry.InitializeEntry) RaftLogWriter(io.atomix.protocols.raft.storage.log.RaftLogWriter) Test(org.junit.Test)

Example 2 with CommandEntry

use of io.atomix.protocols.raft.storage.log.entry.CommandEntry in project atomix by atomix.

the class LeaderRole method commitCommand.

/**
 * Commits a command.
 *
 * @param request the command request
 * @param future  the command response future
 */
private void commitCommand(CommandRequest request, CompletableFuture<CommandResponse> future) {
    final long term = raft.getTerm();
    final long timestamp = System.currentTimeMillis();
    CommandEntry command = new CommandEntry(term, timestamp, request.session(), request.sequenceNumber(), request.operation());
    appendAndCompact(command).whenCompleteAsync((entry, error) -> {
        if (error != null) {
            Throwable cause = Throwables.getRootCause(error);
            if (Throwables.getRootCause(error) instanceof StorageException.TooLarge) {
                log.warn("Failed to append command {}", command, cause);
                future.complete(CommandResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(RaftError.Type.PROTOCOL_ERROR).build());
            } else {
                future.complete(CommandResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(RaftError.Type.COMMAND_FAILURE).build());
            }
            return;
        }
        // Replicate the command to followers.
        appender.appendEntries(entry.index()).whenComplete((commitIndex, commitError) -> {
            raft.checkThread();
            if (isRunning()) {
                // If the command was successfully committed, apply it to the state machine.
                if (commitError == null) {
                    raft.getServiceManager().<OperationResult>apply(entry.index()).whenComplete((r, e) -> {
                        completeOperation(r, CommandResponse.builder(), e, future);
                    });
                } else {
                    future.complete(CommandResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(RaftError.Type.COMMAND_FAILURE).build());
                }
            } else {
                future.complete(CommandResponse.builder().withStatus(RaftResponse.Status.ERROR).withError(RaftError.Type.COMMAND_FAILURE).build());
            }
        });
    }, raft.getThreadContext());
}
Also used : CommandEntry(io.atomix.protocols.raft.storage.log.entry.CommandEntry)

Aggregations

CommandEntry (io.atomix.protocols.raft.storage.log.entry.CommandEntry)2 PrimitiveOperation (io.atomix.primitive.operation.PrimitiveOperation)1 RaftLogWriter (io.atomix.protocols.raft.storage.log.RaftLogWriter)1 InitializeEntry (io.atomix.protocols.raft.storage.log.entry.InitializeEntry)1 OpenSessionEntry (io.atomix.protocols.raft.storage.log.entry.OpenSessionEntry)1 Snapshot (io.atomix.protocols.raft.storage.snapshot.Snapshot)1 Test (org.junit.Test)1