use of org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader in project ignite-3 by apache.
the class FSMCallerImpl method doSnapshotLoad.
private void doSnapshotLoad(final LoadSnapshotClosure done) {
Requires.requireNonNull(done, "LoadSnapshotClosure is null");
final SnapshotReader reader = done.start();
if (reader == null) {
done.run(new Status(RaftError.EINVAL, "open SnapshotReader failed"));
return;
}
final RaftOutter.SnapshotMeta meta = reader.load();
if (meta == null) {
done.run(new Status(RaftError.EINVAL, "SnapshotReader load meta failed"));
if (reader.getRaftError() == RaftError.EIO) {
final RaftException err = new RaftException(ErrorType.ERROR_TYPE_SNAPSHOT, RaftError.EIO, "Fail to load snapshot meta");
setError(err);
}
return;
}
final LogId lastAppliedId = new LogId(this.lastAppliedIndex.get(), this.lastAppliedTerm);
final LogId snapshotId = new LogId(meta.lastIncludedIndex(), meta.lastIncludedTerm());
if (lastAppliedId.compareTo(snapshotId) > 0) {
done.run(new Status(RaftError.ESTALE, "Loading a stale snapshot last_applied_index=%d last_applied_term=%d snapshot_index=%d snapshot_term=%d", lastAppliedId.getIndex(), lastAppliedId.getTerm(), snapshotId.getIndex(), snapshotId.getTerm()));
return;
}
if (!this.fsm.onSnapshotLoad(reader)) {
done.run(new Status(-1, "StateMachine onSnapshotLoad failed"));
final RaftException e = new RaftException(ErrorType.ERROR_TYPE_STATE_MACHINE, RaftError.ESTATEMACHINE, "StateMachine onSnapshotLoad failed");
setError(e);
return;
}
if (meta.oldPeersList() == null) {
// Joint stage is not supposed to be noticeable by end users.
final Configuration conf = new Configuration();
if (meta.peersList() != null) {
for (String metaPeer : meta.peersList()) {
final PeerId peer = new PeerId();
Requires.requireTrue(peer.parse(metaPeer), "Parse peer failed");
conf.addPeer(peer);
}
}
this.fsm.onConfigurationCommitted(conf);
}
this.lastAppliedIndex.set(meta.lastIncludedIndex());
this.lastAppliedTerm = meta.lastIncludedTerm();
done.run(Status.OK());
}
use of org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader in project ignite-3 by apache.
the class LocalSnapshotStorageTest method testCreateOpen.
@Test
public void testCreateOpen() throws Exception {
SnapshotWriter writer = this.snapshotStorage.create();
assertNotNull(writer);
RaftOutter.SnapshotMeta wroteMeta = opts.getRaftMessagesFactory().snapshotMeta().lastIncludedIndex(this.lastSnapshotIndex + 1).lastIncludedTerm(1).build();
((LocalSnapshotWriter) writer).saveMeta(wroteMeta);
writer.addFile("data");
assertEquals(1, this.snapshotStorage.getRefs(this.lastSnapshotIndex).get());
writer.close();
// release old
assertEquals(0, this.snapshotStorage.getRefs(this.lastSnapshotIndex).get());
// ref new
assertEquals(1, this.snapshotStorage.getRefs(this.lastSnapshotIndex + 1).get());
SnapshotReader reader = this.snapshotStorage.open();
assertNotNull(reader);
assertTrue(reader.listFiles().contains("data"));
RaftOutter.SnapshotMeta readMeta = reader.load();
assertEquals(wroteMeta, readMeta);
assertEquals(2, this.snapshotStorage.getRefs(this.lastSnapshotIndex + 1).get());
reader.close();
assertEquals(1, this.snapshotStorage.getRefs(this.lastSnapshotIndex + 1).get());
}
use of org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader in project ignite-3 by apache.
the class LocalSnapshotCopierTest method testCancelByRemote.
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testCancelByRemote() throws Exception {
final CompletableFuture<Message> future = new CompletableFuture<>();
final RpcRequests.GetFileRequest rb = raftOptions.getRaftMessagesFactory().getFileRequest().readerId(99).filename(Snapshot.JRAFT_SNAPSHOT_META_FILE).count(Integer.MAX_VALUE).offset(0).readPartly(true).build();
// mock get metadata
ArgumentCaptor<RpcResponseClosure> argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
Mockito.when(this.raftClientService.getFile(eq(new Endpoint("localhost", 8081)), eq(rb), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
this.copier.start();
assertTrue(TestUtils.waitForArgumentCapture(argument, 5_000));
final RpcResponseClosure<RpcRequests.GetFileResponse> closure = argument.getValue();
closure.run(new Status(RaftError.ECANCELED, "test cancel"));
this.copier.join();
// start timer
final SnapshotReader reader = this.copier.getReader();
assertNull(reader);
assertEquals(RaftError.ECANCELED.getNumber(), this.copier.getCode());
assertEquals("test cancel", this.copier.getErrorMsg());
}
use of org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader in project ignite-3 by apache.
the class LocalSnapshotCopierTest method testStartJoinFinishOK.
@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testStartJoinFinishOK() throws Exception {
final CompletableFuture<Message> future = new CompletableFuture<>();
final GetFileRequestBuilder rb = raftOptions.getRaftMessagesFactory().getFileRequest().readerId(99).filename(Snapshot.JRAFT_SNAPSHOT_META_FILE).count(Integer.MAX_VALUE).offset(0).readPartly(true);
// mock get metadata
ArgumentCaptor<RpcResponseClosure> argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
Mockito.when(this.raftClientService.getFile(eq(new Endpoint("localhost", 8081)), eq(rb.build()), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
this.copier.start();
assertTrue(TestUtils.waitForArgumentCapture(argument, 5_000));
RpcResponseClosure<RpcRequests.GetFileResponse> closure = argument.getValue();
final ByteBuffer metaBuf = this.table.saveToByteBufferAsRemote();
RpcRequests.GetFileResponse response = raftOptions.getRaftMessagesFactory().getFileResponse().readSize(metaBuf.remaining()).eof(true).data(new ByteString(metaBuf)).build();
closure.setResponse(response);
// mock get file
argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
rb.filename("testFile");
rb.count(this.raftOptions.getMaxByteCountPerRpc());
Mockito.when(this.raftClientService.getFile(eq(new Endpoint("localhost", 8081)), eq(rb.build()), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
closure.run(Status.OK());
assertTrue(TestUtils.waitForArgumentCapture(argument, 5_000));
closure = argument.getValue();
response = raftOptions.getRaftMessagesFactory().getFileResponse().readSize(100).eof(true).data(new ByteString(new byte[100])).build();
closure.setResponse(response);
closure.run(Status.OK());
this.copier.join();
final SnapshotReader reader = this.copier.getReader();
assertSame(this.reader, reader);
assertEquals(1, this.writer.listFiles().size());
assertTrue(this.writer.listFiles().contains("testFile"));
}
use of org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader in project ignite-3 by apache.
the class FSMCallerTest method testOnSnapshotLoad.
@Test
public void testOnSnapshotLoad() throws Exception {
final SnapshotReader reader = Mockito.mock(SnapshotReader.class);
final SnapshotMeta meta = opts.getRaftMessagesFactory().snapshotMeta().lastIncludedIndex(12).lastIncludedTerm(1).build();
Mockito.when(reader.load()).thenReturn(meta);
Mockito.when(this.fsm.onSnapshotLoad(reader)).thenReturn(true);
final CountDownLatch latch = new CountDownLatch(1);
this.fsmCaller.onSnapshotLoad(new LoadSnapshotClosure() {
@Override
public void run(final Status status) {
assertTrue(status.isOk());
latch.countDown();
}
@Override
public SnapshotReader start() {
return reader;
}
});
latch.await();
assertEquals(this.fsmCaller.getLastAppliedIndex(), 12);
Mockito.verify(this.fsm).onConfigurationCommitted(Mockito.any());
}
Aggregations