Search in sources :

Example 1 with SnapshotReader

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());
}
Also used : Status(org.apache.ignite.raft.jraft.Status) RaftException(org.apache.ignite.raft.jraft.error.RaftException) Configuration(org.apache.ignite.raft.jraft.conf.Configuration) RaftOutter(org.apache.ignite.raft.jraft.entity.RaftOutter) SnapshotReader(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader) LogId(org.apache.ignite.raft.jraft.entity.LogId) PeerId(org.apache.ignite.raft.jraft.entity.PeerId)

Example 2 with SnapshotReader

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());
}
Also used : RaftOutter(org.apache.ignite.raft.jraft.entity.RaftOutter) SnapshotWriter(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotWriter) SnapshotReader(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader) BaseStorageTest(org.apache.ignite.raft.jraft.storage.BaseStorageTest) Test(org.junit.jupiter.api.Test)

Example 3 with SnapshotReader

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());
}
Also used : Status(org.apache.ignite.raft.jraft.Status) CompletableFuture(java.util.concurrent.CompletableFuture) Message(org.apache.ignite.raft.jraft.rpc.Message) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) RpcResponseClosure(org.apache.ignite.raft.jraft.rpc.RpcResponseClosure) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) SnapshotReader(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader) BaseStorageTest(org.apache.ignite.raft.jraft.storage.BaseStorageTest) Test(org.junit.jupiter.api.Test)

Example 4 with SnapshotReader

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"));
}
Also used : Message(org.apache.ignite.raft.jraft.rpc.Message) ByteString(org.apache.ignite.raft.jraft.util.ByteString) RpcResponseClosure(org.apache.ignite.raft.jraft.rpc.RpcResponseClosure) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) ByteBuffer(java.nio.ByteBuffer) CompletableFuture(java.util.concurrent.CompletableFuture) GetFileRequestBuilder(org.apache.ignite.raft.jraft.rpc.GetFileRequestBuilder) Endpoint(org.apache.ignite.raft.jraft.util.Endpoint) SnapshotReader(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader) BaseStorageTest(org.apache.ignite.raft.jraft.storage.BaseStorageTest) Test(org.junit.jupiter.api.Test)

Example 5 with SnapshotReader

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());
}
Also used : LoadSnapshotClosure(org.apache.ignite.raft.jraft.closure.LoadSnapshotClosure) Status(org.apache.ignite.raft.jraft.Status) SnapshotReader(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader) SnapshotMeta(org.apache.ignite.raft.jraft.entity.RaftOutter.SnapshotMeta) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.jupiter.api.Test)

Aggregations

SnapshotReader (org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader)13 Test (org.junit.jupiter.api.Test)9 Status (org.apache.ignite.raft.jraft.Status)5 Message (org.apache.ignite.raft.jraft.rpc.Message)5 RpcRequests (org.apache.ignite.raft.jraft.rpc.RpcRequests)5 CompletableFuture (java.util.concurrent.CompletableFuture)4 LoadSnapshotClosure (org.apache.ignite.raft.jraft.closure.LoadSnapshotClosure)4 RpcResponseClosure (org.apache.ignite.raft.jraft.rpc.RpcResponseClosure)4 BaseStorageTest (org.apache.ignite.raft.jraft.storage.BaseStorageTest)4 Endpoint (org.apache.ignite.raft.jraft.util.Endpoint)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 RaftOutter (org.apache.ignite.raft.jraft.entity.RaftOutter)3 SnapshotMeta (org.apache.ignite.raft.jraft.entity.RaftOutter.SnapshotMeta)3 ByteString (org.apache.ignite.raft.jraft.util.ByteString)3 ByteBuffer (java.nio.ByteBuffer)2 GetFileRequestBuilder (org.apache.ignite.raft.jraft.rpc.GetFileRequestBuilder)2 RaftMessagesFactory (org.apache.ignite.raft.jraft.RaftMessagesFactory)1 Configuration (org.apache.ignite.raft.jraft.conf.Configuration)1 LogId (org.apache.ignite.raft.jraft.entity.LogId)1 PeerId (org.apache.ignite.raft.jraft.entity.PeerId)1