Search in sources :

Example 1 with InstallRequest

use of io.atomix.protocols.raft.protocol.InstallRequest in project atomix by atomix.

the class AbstractAppender method buildInstallRequest.

/**
 * Builds an install request for the given member.
 */
protected InstallRequest buildInstallRequest(RaftMemberContext member, Snapshot snapshot) {
    if (member.getNextSnapshotIndex() != snapshot.index()) {
        member.setNextSnapshotIndex(snapshot.index());
        member.setNextSnapshotOffset(0);
    }
    InstallRequest request;
    synchronized (snapshot) {
        // Open a new snapshot reader.
        try (SnapshotReader reader = snapshot.openReader()) {
            // Skip to the next batch of bytes according to the snapshot chunk size and current offset.
            reader.skip(member.getNextSnapshotOffset() * MAX_BATCH_SIZE);
            byte[] data = new byte[Math.min(MAX_BATCH_SIZE, reader.remaining())];
            reader.read(data);
            // Create the install request, indicating whether this is the last chunk of data based on the number
            // of bytes remaining in the buffer.
            DefaultRaftMember leader = raft.getLeader();
            request = InstallRequest.builder().withTerm(raft.getTerm()).withLeader(leader != null ? leader.nodeId() : null).withIndex(snapshot.index()).withOffset(member.getNextSnapshotOffset()).withData(data).withComplete(!reader.hasRemaining()).build();
        }
    }
    return request;
}
Also used : DefaultRaftMember(io.atomix.protocols.raft.cluster.impl.DefaultRaftMember) InstallRequest(io.atomix.protocols.raft.protocol.InstallRequest) SnapshotReader(io.atomix.protocols.raft.storage.snapshot.SnapshotReader)

Aggregations

DefaultRaftMember (io.atomix.protocols.raft.cluster.impl.DefaultRaftMember)1 InstallRequest (io.atomix.protocols.raft.protocol.InstallRequest)1 SnapshotReader (io.atomix.protocols.raft.storage.snapshot.SnapshotReader)1