Search in sources :

Example 6 with SnapshotReader

use of org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader in project ignite-3 by apache.

the class FSMCallerTest method testOnSnapshotLoadFSMError.

@Test
public void testOnSnapshotLoadFSMError() 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(false);
    final CountDownLatch latch = new CountDownLatch(1);
    this.fsmCaller.onSnapshotLoad(new LoadSnapshotClosure() {

        @Override
        public void run(final Status status) {
            assertFalse(status.isOk());
            assertEquals(-1, status.getCode());
            assertEquals("StateMachine onSnapshotLoad failed", status.getErrorMsg());
            latch.countDown();
        }

        @Override
        public SnapshotReader start() {
            return reader;
        }
    });
    latch.await();
    assertEquals(this.fsmCaller.getLastAppliedIndex(), 10);
}
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)

Example 7 with SnapshotReader

use of org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader in project ignite-3 by apache.

the class SnapshotExecutorTest method testInstallSnapshot.

@Test
public void testInstallSnapshot() throws Exception {
    RaftMessagesFactory msgFactory = raftOptions.getRaftMessagesFactory();
    final RpcRequests.InstallSnapshotRequest irb = msgFactory.installSnapshotRequest().groupId("test").peerId(addr.toString()).serverId("localhost:8080").uri("remote://localhost:8080/99").term(0).meta(msgFactory.snapshotMeta().lastIncludedIndex(1).lastIncludedTerm(2).build()).build();
    Mockito.when(raftClientService.connect(new Endpoint("localhost", 8080))).thenReturn(true);
    final CompletableFuture<Message> fut = new CompletableFuture<>();
    final GetFileRequestBuilder rb = msgFactory.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(raftClientService.getFile(eq(new Endpoint("localhost", 8080)), eq(rb.build()), eq(copyOpts.getTimeoutMs()), argument.capture())).thenReturn(fut);
    Future<?> snapFut = Utils.runInThread(ForkJoinPool.commonPool(), () -> executor.installSnapshot(irb, msgFactory.installSnapshotResponse(), new RpcRequestClosure(asyncCtx, msgFactory)));
    assertTrue(TestUtils.waitForArgumentCapture(argument, 5_000));
    RpcResponseClosure<RpcRequests.GetFileResponse> closure = argument.getValue();
    final ByteBuffer metaBuf = table.saveToByteBufferAsRemote();
    closure.setResponse(msgFactory.getFileResponse().readSize(metaBuf.remaining()).eof(true).data(new ByteString(metaBuf)).build());
    // Mock get file
    argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
    rb.filename("testFile");
    rb.count(raftOptions.getMaxByteCountPerRpc());
    Mockito.when(raftClientService.getFile(eq(new Endpoint("localhost", 8080)), eq(rb.build()), eq(copyOpts.getTimeoutMs()), argument.capture())).thenReturn(fut);
    closure.run(Status.OK());
    assertTrue(TestUtils.waitForArgumentCapture(argument, 5_000));
    closure = argument.getValue();
    closure.setResponse(msgFactory.getFileResponse().readSize(100).eof(true).data(new ByteString(new byte[100])).build());
    ArgumentCaptor<LoadSnapshotClosure> loadSnapshotArg = ArgumentCaptor.forClass(LoadSnapshotClosure.class);
    Mockito.when(fSMCaller.onSnapshotLoad(loadSnapshotArg.capture())).thenReturn(true);
    closure.run(Status.OK());
    assertTrue(TestUtils.waitForArgumentCapture(loadSnapshotArg, 5_000));
    final LoadSnapshotClosure done = loadSnapshotArg.getValue();
    final SnapshotReader reader = done.start();
    assertNotNull(reader);
    assertEquals(1, reader.listFiles().size());
    assertTrue(reader.listFiles().contains("testFile"));
    done.run(Status.OK());
    executor.join();
    assertTrue(snapFut.isDone());
    assertEquals(2, executor.getLastSnapshotTerm());
    assertEquals(1, executor.getLastSnapshotIndex());
}
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) RaftMessagesFactory(org.apache.ignite.raft.jraft.RaftMessagesFactory) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) ByteBuffer(java.nio.ByteBuffer) RpcRequestClosure(org.apache.ignite.raft.jraft.rpc.RpcRequestClosure) LoadSnapshotClosure(org.apache.ignite.raft.jraft.closure.LoadSnapshotClosure) 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) LocalSnapshotReader(org.apache.ignite.raft.jraft.storage.snapshot.local.LocalSnapshotReader) Test(org.junit.jupiter.api.Test)

Example 8 with SnapshotReader

use of org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader in project ignite-3 by apache.

the class LocalSnapshotCopierTest method testInterrupt.

@Test
@Disabled("https://issues.apache.org/jira/browse/IGNITE-16620")
public void testInterrupt() 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();
    Utils.runInThread(ForkJoinPool.commonPool(), () -> LocalSnapshotCopierTest.this.copier.cancel());
    this.copier.join();
    // start timer
    final SnapshotReader reader = this.copier.getReader();
    assertNull(reader);
    assertEquals(RaftError.ECANCELED.getNumber(), this.copier.getCode());
    assertEquals("Cancel the copier manually.", this.copier.getErrorMsg());
}
Also used : 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) Disabled(org.junit.jupiter.api.Disabled)

Example 9 with SnapshotReader

use of org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader in project ignite-3 by apache.

the class ReplicatorTest method testInstallSnapshot.

@Test
public void testInstallSnapshot() {
    final Replicator r = getReplicator();
    this.id.unlock();
    final Future<Message> rpcInFly = r.getRpcInFly();
    assertNotNull(rpcInFly);
    final SnapshotReader reader = Mockito.mock(SnapshotReader.class);
    Mockito.when(this.snapshotStorage.open()).thenReturn(reader);
    final String uri = "remote://localhost:8081/99";
    Mockito.when(reader.generateURIForCopy()).thenReturn(uri);
    final RaftOutter.SnapshotMeta meta = raftOptions.getRaftMessagesFactory().snapshotMeta().lastIncludedIndex(11).lastIncludedTerm(1).build();
    Mockito.when(reader.load()).thenReturn(meta);
    assertEquals(0, r.statInfo.lastLogIncluded);
    assertEquals(0, r.statInfo.lastTermIncluded);
    final RpcRequests.InstallSnapshotRequest req = raftOptions.getRaftMessagesFactory().installSnapshotRequest().term(this.opts.getTerm()).groupId(this.opts.getGroupId()).serverId(this.opts.getServerId().toString()).peerId(this.opts.getPeerId().toString()).meta(meta).uri(uri).build();
    Mockito.when(this.rpcService.installSnapshot(eq(this.opts.getPeerId().getEndpoint()), eq(req), Mockito.any())).thenReturn(new CompletableFuture<>());
    r.installSnapshot();
    assertNotNull(r.getRpcInFly());
    assertNotSame(r.getRpcInFly(), rpcInFly);
    assertEquals(Replicator.RunningState.INSTALLING_SNAPSHOT, r.statInfo.runningState);
    assertEquals(11, r.statInfo.lastLogIncluded);
    assertEquals(1, r.statInfo.lastTermIncluded);
}
Also used : Message(org.apache.ignite.raft.jraft.rpc.Message) RaftOutter(org.apache.ignite.raft.jraft.entity.RaftOutter) SnapshotReader(org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader) RpcRequests(org.apache.ignite.raft.jraft.rpc.RpcRequests) ByteString(org.apache.ignite.raft.jraft.util.ByteString) Test(org.junit.jupiter.api.Test)

Example 10 with SnapshotReader

use of org.apache.ignite.raft.jraft.storage.snapshot.SnapshotReader in project ignite-3 by apache.

the class FSMCallerTest method testOnSnapshotLoadStale.

@Test
public void testOnSnapshotLoadStale() throws Exception {
    final SnapshotReader reader = Mockito.mock(SnapshotReader.class);
    final SnapshotMeta meta = opts.getRaftMessagesFactory().snapshotMeta().lastIncludedIndex(5).lastIncludedTerm(1).build();
    Mockito.when(reader.load()).thenReturn(meta);
    final CountDownLatch latch = new CountDownLatch(1);
    this.fsmCaller.onSnapshotLoad(new LoadSnapshotClosure() {

        @Override
        public void run(final Status status) {
            assertFalse(status.isOk());
            assertEquals(RaftError.ESTALE, status.getRaftError());
            latch.countDown();
        }

        @Override
        public SnapshotReader start() {
            return reader;
        }
    });
    latch.await();
    assertEquals(this.fsmCaller.getLastAppliedIndex(), 10);
}
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