use of com.alipay.sofa.jraft.closure.LoadSnapshotClosure in project sofa-jraft by sofastack.
the class SnapshotExecutorTest method testInstallSnapshot.
@Test
public void testInstallSnapshot() throws Exception {
final RpcRequests.InstallSnapshotRequest.Builder irb = RpcRequests.InstallSnapshotRequest.newBuilder();
irb.setGroupId("test");
irb.setPeerId(this.addr.toString());
irb.setServerId("localhost:8080");
irb.setUri("remote://localhost:8080/99");
irb.setTerm(0);
irb.setMeta(RaftOutter.SnapshotMeta.newBuilder().setLastIncludedIndex(1).setLastIncludedTerm(2));
Mockito.when(this.raftClientService.connect(new Endpoint("localhost", 8080))).thenReturn(true);
final FutureImpl<Message> future = new FutureImpl<>();
final RpcRequests.GetFileRequest.Builder rb = RpcRequests.GetFileRequest.newBuilder().setReaderId(99).setFilename(Snapshot.JRAFT_SNAPSHOT_META_FILE).setCount(Integer.MAX_VALUE).setOffset(0).setReadPartly(true);
// mock get metadata
ArgumentCaptor<RpcResponseClosure> argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
Mockito.when(this.raftClientService.getFile(eq(new Endpoint("localhost", 8080)), eq(rb.build()), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
Utils.runInThread(new Runnable() {
@Override
public void run() {
SnapshotExecutorTest.this.executor.installSnapshot(irb.build(), RpcRequests.InstallSnapshotResponse.newBuilder(), new RpcRequestClosure(SnapshotExecutorTest.this.asyncCtx));
}
});
Thread.sleep(500);
RpcResponseClosure<RpcRequests.GetFileResponse> closure = argument.getValue();
final ByteBuffer metaBuf = this.table.saveToByteBufferAsRemote();
closure.setResponse(RpcRequests.GetFileResponse.newBuilder().setReadSize(metaBuf.remaining()).setEof(true).setData(ByteString.copyFrom(metaBuf)).build());
// mock get file
argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
rb.setFilename("testFile");
rb.setCount(this.raftOptions.getMaxByteCountPerRpc());
Mockito.when(this.raftClientService.getFile(eq(new Endpoint("localhost", 8080)), eq(rb.build()), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
closure.run(Status.OK());
Thread.sleep(500);
closure = argument.getValue();
closure.setResponse(RpcRequests.GetFileResponse.newBuilder().setReadSize(100).setEof(true).setData(ByteString.copyFrom(new byte[100])).build());
final ArgumentCaptor<LoadSnapshotClosure> loadSnapshotArg = ArgumentCaptor.forClass(LoadSnapshotClosure.class);
Mockito.when(this.fSMCaller.onSnapshotLoad(loadSnapshotArg.capture())).thenReturn(true);
closure.run(Status.OK());
Thread.sleep(500);
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());
this.executor.join();
assertEquals(2, this.executor.getLastSnapshotTerm());
assertEquals(1, this.executor.getLastSnapshotIndex());
}
use of com.alipay.sofa.jraft.closure.LoadSnapshotClosure in project sofa-jraft by sofastack.
the class SnapshotExecutorTest method testRetryInstallSnapshot.
@Test
public void testRetryInstallSnapshot() throws Exception {
final RpcRequests.InstallSnapshotRequest.Builder irb = RpcRequests.InstallSnapshotRequest.newBuilder();
irb.setGroupId("test");
irb.setPeerId(this.addr.toString());
irb.setServerId("localhost:8080");
irb.setUri("remote://localhost:8080/99");
irb.setTerm(0);
irb.setMeta(RaftOutter.SnapshotMeta.newBuilder().setLastIncludedIndex(1).setLastIncludedTerm(2));
Mockito.when(this.raftClientService.connect(new Endpoint("localhost", 8080))).thenReturn(true);
final FutureImpl<Message> future = new FutureImpl<>();
final RpcRequests.GetFileRequest.Builder rb = RpcRequests.GetFileRequest.newBuilder().setReaderId(99).setFilename(Snapshot.JRAFT_SNAPSHOT_META_FILE).setCount(Integer.MAX_VALUE).setOffset(0).setReadPartly(true);
// mock get metadata
ArgumentCaptor<RpcResponseClosure> argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
final CountDownLatch retryLatch = new CountDownLatch(1);
final CountDownLatch answerLatch = new CountDownLatch(1);
Mockito.when(this.raftClientService.getFile(eq(new Endpoint("localhost", 8080)), eq(rb.build()), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenAnswer(new Answer<Future<Message>>() {
AtomicInteger count = new AtomicInteger(0);
@Override
public Future<Message> answer(InvocationOnMock invocation) throws Throwable {
if (count.incrementAndGet() == 1) {
retryLatch.countDown();
answerLatch.await();
Thread.sleep(1000);
return future;
} else {
throw new IllegalStateException("shouldn't be called more than once");
}
}
});
final MockAsyncContext installContext = new MockAsyncContext();
final MockAsyncContext retryInstallContext = new MockAsyncContext();
Utils.runInThread(new Runnable() {
@Override
public void run() {
SnapshotExecutorTest.this.executor.installSnapshot(irb.build(), RpcRequests.InstallSnapshotResponse.newBuilder(), new RpcRequestClosure(installContext));
}
});
Thread.sleep(500);
retryLatch.await();
Utils.runInThread(new Runnable() {
@Override
public void run() {
answerLatch.countDown();
SnapshotExecutorTest.this.executor.installSnapshot(irb.build(), RpcRequests.InstallSnapshotResponse.newBuilder(), new RpcRequestClosure(retryInstallContext));
}
});
RpcResponseClosure<RpcRequests.GetFileResponse> closure = argument.getValue();
final ByteBuffer metaBuf = this.table.saveToByteBufferAsRemote();
closure.setResponse(RpcRequests.GetFileResponse.newBuilder().setReadSize(metaBuf.remaining()).setEof(true).setData(ByteString.copyFrom(metaBuf)).build());
// mock get file
argument = ArgumentCaptor.forClass(RpcResponseClosure.class);
rb.setFilename("testFile");
rb.setCount(this.raftOptions.getMaxByteCountPerRpc());
Mockito.when(this.raftClientService.getFile(eq(new Endpoint("localhost", 8080)), eq(rb.build()), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
closure.run(Status.OK());
Thread.sleep(500);
closure = argument.getValue();
closure.setResponse(RpcRequests.GetFileResponse.newBuilder().setReadSize(100).setEof(true).setData(ByteString.copyFrom(new byte[100])).build());
final ArgumentCaptor<LoadSnapshotClosure> loadSnapshotArg = ArgumentCaptor.forClass(LoadSnapshotClosure.class);
Mockito.when(this.fSMCaller.onSnapshotLoad(loadSnapshotArg.capture())).thenReturn(true);
closure.run(Status.OK());
Thread.sleep(2000);
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());
this.executor.join();
assertEquals(2, this.executor.getLastSnapshotTerm());
assertEquals(1, this.executor.getLastSnapshotIndex());
assertNotNull(installContext.getResponseObject());
assertNotNull(retryInstallContext.getResponseObject());
assertEquals(installContext.as(RpcRequests.ErrorResponse.class).getErrorCode(), RaftError.EINTR.getNumber());
assertTrue(retryInstallContext.as(RpcRequests.InstallSnapshotResponse.class).hasSuccess());
}
use of com.alipay.sofa.jraft.closure.LoadSnapshotClosure in project sofa-jraft by sofastack.
the class FSMCallerTest method testOnSnapshotLoadStale.
@Test
public void testOnSnapshotLoadStale() throws Exception {
final SnapshotReader reader = Mockito.mock(SnapshotReader.class);
final SnapshotMeta meta = SnapshotMeta.newBuilder().setLastIncludedIndex(5).setLastIncludedTerm(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);
}
use of com.alipay.sofa.jraft.closure.LoadSnapshotClosure in project sofa-jraft by sofastack.
the class FSMCallerTest method testOnSnapshotLoad.
@Test
public void testOnSnapshotLoad() throws Exception {
final SnapshotReader reader = Mockito.mock(SnapshotReader.class);
final SnapshotMeta meta = SnapshotMeta.newBuilder().setLastIncludedIndex(12).setLastIncludedTerm(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());
}
use of com.alipay.sofa.jraft.closure.LoadSnapshotClosure in project sofa-jraft by sofastack.
the class FSMCallerTest method testOnSnapshotLoadFSMError.
@Test
public void testOnSnapshotLoadFSMError() throws Exception {
final SnapshotReader reader = Mockito.mock(SnapshotReader.class);
final SnapshotMeta meta = SnapshotMeta.newBuilder().setLastIncludedIndex(12).setLastIncludedTerm(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);
}
Aggregations