use of io.atomix.protocols.raft.storage.snapshot.SnapshotReader 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;
}
use of io.atomix.protocols.raft.storage.snapshot.SnapshotReader in project atomix by atomix.
the class RaftServiceManager method install.
/**
* Prepares sessions for the given index.
*
* @param index the index for which to install snapshots
*/
private void install(long index) {
Snapshot snapshot = raft.getSnapshotStore().getSnapshot(index - 1);
// If snapshots exist for the prior index, iterate through snapshots and populate services/sessions.
if (snapshot != null) {
logger.debug("Installing snapshot {}", snapshot);
try (SnapshotReader reader = snapshot.openReader()) {
while (reader.hasRemaining()) {
int length = reader.readInt();
if (length > 0) {
SnapshotReader serviceReader = new SnapshotReader(reader.buffer().slice(length), reader.snapshot());
installService(serviceReader);
reader.skip(length);
}
}
}
}
}
Aggregations