Search in sources :

Example 6 with Endpoint

use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.

the class SnapshotExecutorTest method testInterruptInstallaling.

@Test
public void testInterruptInstallaling() 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(1));
    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
    final 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));
        }
    });
    this.executor.interruptDownloadingSnapshots(1);
    this.executor.join();
    assertEquals(0, this.executor.getLastSnapshotTerm());
    assertEquals(0, this.executor.getLastSnapshotIndex());
}
Also used : Message(com.google.protobuf.Message) RpcResponseClosure(com.alipay.sofa.jraft.rpc.RpcResponseClosure) RpcRequestClosure(com.alipay.sofa.jraft.rpc.RpcRequestClosure) Endpoint(com.alipay.sofa.jraft.util.Endpoint) FutureImpl(com.alipay.sofa.jraft.rpc.impl.FutureImpl) Test(org.junit.Test)

Example 7 with Endpoint

use of com.alipay.sofa.jraft.util.Endpoint 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());
}
Also used : Message(com.google.protobuf.Message) RpcResponseClosure(com.alipay.sofa.jraft.rpc.RpcResponseClosure) RpcRequestClosure(com.alipay.sofa.jraft.rpc.RpcRequestClosure) LoadSnapshotClosure(com.alipay.sofa.jraft.closure.LoadSnapshotClosure) Endpoint(com.alipay.sofa.jraft.util.Endpoint) MockAsyncContext(com.alipay.sofa.jraft.test.MockAsyncContext) RpcRequests(com.alipay.sofa.jraft.rpc.RpcRequests) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) FutureImpl(com.alipay.sofa.jraft.rpc.impl.FutureImpl) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Future(java.util.concurrent.Future) LocalSnapshotReader(com.alipay.sofa.jraft.storage.snapshot.local.LocalSnapshotReader) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) Test(org.junit.Test)

Example 8 with Endpoint

use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.

the class LocalSnapshotCopierTest method testStartJoinFinishOK.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testStartJoinFinishOK() throws Exception {
    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", 8081)), eq(rb.build()), eq(this.copyOpts.getTimeoutMs()), argument.capture())).thenReturn(future);
    this.copier.start();
    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", 8081)), 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());
    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(com.google.protobuf.Message) FutureImpl(com.alipay.sofa.jraft.rpc.impl.FutureImpl) Endpoint(com.alipay.sofa.jraft.util.Endpoint) RpcResponseClosure(com.alipay.sofa.jraft.rpc.RpcResponseClosure) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Example 9 with Endpoint

use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.

the class LocalSnapshotCopierTest method setup.

@Override
@Before
public void setup() throws Exception {
    super.setup();
    this.timerManager = new TimerManager(5);
    this.raftOptions = new RaftOptions();
    this.writer = new LocalSnapshotWriter(this.path, this.snapshotStorage, this.raftOptions);
    this.reader = new LocalSnapshotReader(this.snapshotStorage, null, new Endpoint("localhost", 8081), this.raftOptions, this.path);
    Mockito.when(this.snapshotStorage.open()).thenReturn(this.reader);
    Mockito.when(this.snapshotStorage.create(true)).thenReturn(this.writer);
    this.table = new LocalSnapshotMetaTable(this.raftOptions);
    this.table.addFile("testFile", LocalFileMetaOutter.LocalFileMeta.newBuilder().setChecksum("test").build());
    this.table.setMeta(RaftOutter.SnapshotMeta.newBuilder().setLastIncludedIndex(1).setLastIncludedTerm(1).build());
    this.uri = "remote://" + this.hostPort + "/" + this.readerId;
    this.copier = new LocalSnapshotCopier();
    this.copyOpts = new CopyOptions();
    Mockito.when(this.raftClientService.connect(new Endpoint("localhost", 8081))).thenReturn(true);
    assertTrue(this.copier.init(this.uri, new SnapshotCopierOptions(this.raftClientService, this.timerManager, this.raftOptions, new NodeOptions())));
    this.copier.setStorage(this.snapshotStorage);
}
Also used : RaftOptions(com.alipay.sofa.jraft.option.RaftOptions) SnapshotCopierOptions(com.alipay.sofa.jraft.option.SnapshotCopierOptions) Endpoint(com.alipay.sofa.jraft.util.Endpoint) CopyOptions(com.alipay.sofa.jraft.option.CopyOptions) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) TimerManager(com.alipay.sofa.jraft.core.TimerManager) Before(org.junit.Before)

Example 10 with Endpoint

use of com.alipay.sofa.jraft.util.Endpoint in project sofa-jraft by sofastack.

the class LocalSnapshotCopierTest method testCancelByRemote.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void testCancelByRemote() throws Exception {
    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
    final 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();
    Thread.sleep(500);
    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);
    Assert.assertEquals(RaftError.ECANCELED.getNumber(), this.copier.getCode());
    Assert.assertEquals("test cancel", this.copier.getErrorMsg());
}
Also used : Status(com.alipay.sofa.jraft.Status) Message(com.google.protobuf.Message) FutureImpl(com.alipay.sofa.jraft.rpc.impl.FutureImpl) Endpoint(com.alipay.sofa.jraft.util.Endpoint) RpcResponseClosure(com.alipay.sofa.jraft.rpc.RpcResponseClosure) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Aggregations

Endpoint (com.alipay.sofa.jraft.util.Endpoint)80 Test (org.junit.Test)34 PeerId (com.alipay.sofa.jraft.entity.PeerId)28 Node (com.alipay.sofa.jraft.Node)23 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)18 Configuration (com.alipay.sofa.jraft.conf.Configuration)12 CountDownLatch (java.util.concurrent.CountDownLatch)11 Status (com.alipay.sofa.jraft.Status)8 File (java.io.File)7 ByteBuffer (java.nio.ByteBuffer)7 RaftOptions (com.alipay.sofa.jraft.option.RaftOptions)6 Region (com.alipay.sofa.jraft.rhea.metadata.Region)6 PlacementDriverClient (com.alipay.sofa.jraft.rhea.client.pd.PlacementDriverClient)5 RheaKVStoreOptions (com.alipay.sofa.jraft.rhea.options.RheaKVStoreOptions)5 RpcResponseClosure (com.alipay.sofa.jraft.rpc.RpcResponseClosure)5 RpcServer (com.alipay.sofa.jraft.rpc.RpcServer)5 FutureImpl (com.alipay.sofa.jraft.rpc.impl.FutureImpl)5 SnapshotReader (com.alipay.sofa.jraft.storage.snapshot.SnapshotReader)5 Message (com.google.protobuf.Message)5 RaftGroupService (com.alipay.sofa.jraft.RaftGroupService)4