Search in sources :

Example 1 with BackupOperation

use of io.atomix.protocols.log.protocol.BackupOperation in project atomix by atomix.

the class FollowerRole method backup.

@Override
public CompletableFuture<BackupResponse> backup(BackupRequest request) {
    logRequest(request);
    // If the term is greater than the node's current term, update the term.
    if (request.term() > context.currentTerm()) {
        context.resetTerm(request.term(), request.leader());
    } else // If the term is less than the node's current term, ignore the backup message.
    if (request.term() < context.currentTerm()) {
        return CompletableFuture.completedFuture(BackupResponse.error());
    }
    JournalWriter<LogEntry> writer = context.writer();
    JournalReader<LogEntry> reader = context.reader();
    // Iterate through all operations in the batch and append entries.
    for (BackupOperation operation : request.batch()) {
        // If the reader's next index does not align with the operation index, reset the reader.
        if (reader.getNextIndex() != operation.index()) {
            reader.reset(operation.index());
        }
        // If the reader has no next entry, append the entry to the journal.
        if (!reader.hasNext()) {
            try {
                writer.append(new LogEntry(operation.term(), operation.timestamp(), operation.value()));
            } catch (StorageException e) {
                return CompletableFuture.completedFuture(BackupResponse.error());
            }
        } else // If the next entry's term does not match the operation term, append the entry to the journal.
        if (reader.next().entry().term() != operation.term()) {
            writer.truncate(operation.index());
            try {
                writer.append(new LogEntry(operation.term(), operation.timestamp(), operation.value()));
            } catch (StorageException e) {
                return CompletableFuture.completedFuture(BackupResponse.error());
            }
        }
    }
    return CompletableFuture.completedFuture(logResponse(BackupResponse.ok()));
}
Also used : StorageException(io.atomix.storage.StorageException) LogEntry(io.atomix.protocols.log.protocol.LogEntry) BackupOperation(io.atomix.protocols.log.protocol.BackupOperation)

Aggregations

BackupOperation (io.atomix.protocols.log.protocol.BackupOperation)1 LogEntry (io.atomix.protocols.log.protocol.LogEntry)1 StorageException (io.atomix.storage.StorageException)1