Search in sources :

Example 6 with FutureImpl

use of com.alipay.sofa.jraft.rpc.impl.FutureImpl in project sofa-jraft by sofastack.

the class CopySessionTest method testOnRpcReturnedRetry.

@Test
public void testOnRpcReturnedRetry() throws Exception {
    assertNull(this.session.getTimer());
    assertNull(this.session.getRpcCall());
    final ByteBufferCollector bufRef = ByteBufferCollector.allocate(0);
    this.session.setDestBuf(bufRef);
    final FutureImpl<Message> future = new FutureImpl<>();
    final RpcRequests.GetFileRequest.Builder rb = RpcRequests.GetFileRequest.newBuilder().setReaderId(99).setFilename("data").setCount(Integer.MAX_VALUE).setOffset(0).setReadPartly(true);
    Mockito.when(this.rpcService.getFile(this.address, rb.build(), this.copyOpts.getTimeoutMs(), session.getDone())).thenReturn(future);
    this.session.onRpcReturned(new Status(RaftError.EINTR, "test"), null);
    assertNotNull(this.session.getTimer());
    Thread.sleep(this.copyOpts.getRetryIntervalMs() + 100);
    assertNotNull(this.session.getRpcCall());
    assertSame(future, this.session.getRpcCall());
    assertNull(this.session.getTimer());
}
Also used : Status(com.alipay.sofa.jraft.Status) ByteBufferCollector(com.alipay.sofa.jraft.util.ByteBufferCollector) Message(com.google.protobuf.Message) FutureImpl(com.alipay.sofa.jraft.rpc.impl.FutureImpl) Test(org.junit.Test)

Example 7 with FutureImpl

use of com.alipay.sofa.jraft.rpc.impl.FutureImpl in project sofa-jraft by sofastack.

the class CopySessionTest method testOnRpcReturnedOK.

@Test
public void testOnRpcReturnedOK() {
    assertNull(this.session.getRpcCall());
    final ByteBufferCollector bufRef = ByteBufferCollector.allocate(0);
    this.session.setDestBuf(bufRef);
    final FutureImpl<Message> future = new FutureImpl<>();
    final RpcRequests.GetFileRequest.Builder rb = RpcRequests.GetFileRequest.newBuilder().setReaderId(99).setFilename("data").setCount(Integer.MAX_VALUE).setOffset(100).setReadPartly(true);
    Mockito.when(this.rpcService.getFile(this.address, rb.build(), this.copyOpts.getTimeoutMs(), session.getDone())).thenReturn(future);
    this.session.onRpcReturned(Status.OK(), RpcRequests.GetFileResponse.newBuilder().setReadSize(100).setEof(false).setData(ByteString.copyFrom(new byte[100])).build());
    assertEquals(100, bufRef.capacity());
    assertEquals(100, bufRef.getBuffer().position());
    assertNotNull(this.session.getRpcCall());
    // send next request
    assertSame(future, this.session.getRpcCall());
}
Also used : ByteBufferCollector(com.alipay.sofa.jraft.util.ByteBufferCollector) Message(com.google.protobuf.Message) FutureImpl(com.alipay.sofa.jraft.rpc.impl.FutureImpl) Test(org.junit.Test)

Example 8 with FutureImpl

use of com.alipay.sofa.jraft.rpc.impl.FutureImpl in project sofa-jraft by sofastack.

the class DefaultRaftClientService method onConnectionFail.

// fail-fast when no connection
private Future<Message> onConnectionFail(final Endpoint endpoint, final Message request, Closure done, final Executor executor) {
    final FutureImpl<Message> future = new FutureImpl<>();
    executor.execute(() -> {
        final String fmt = "Check connection[%s] fail and try to create new one";
        if (done != null) {
            try {
                done.run(new Status(RaftError.EINTERNAL, fmt, endpoint));
            } catch (final Throwable t) {
                LOG.error("Fail to run RpcResponseClosure, the request is {}.", request, t);
            }
        }
        if (!future.isDone()) {
            future.failure(new RemotingException(String.format(fmt, endpoint)));
        }
    });
    return future;
}
Also used : Status(com.alipay.sofa.jraft.Status) Message(com.google.protobuf.Message) FutureImpl(com.alipay.sofa.jraft.rpc.impl.FutureImpl) RemotingException(com.alipay.sofa.jraft.error.RemotingException)

Example 9 with FutureImpl

use of com.alipay.sofa.jraft.rpc.impl.FutureImpl in project sofa-jraft by sofastack.

the class LocalSnapshotCopierTest method testInterrupt.

@Test
public void testInterrupt() 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(10);
    Utils.runInThread(new Runnable() {

        @Override
        public void run() {
            LocalSnapshotCopierTest.this.copier.cancel();
        }
    });
    this.copier.join();
    // start timer
    final SnapshotReader reader = this.copier.getReader();
    assertNull(reader);
    Assert.assertEquals(RaftError.ECANCELED.getNumber(), this.copier.getCode());
    Assert.assertEquals("Cancel the copier manually.", this.copier.getErrorMsg());
}
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) Test(org.junit.Test) BaseStorageTest(com.alipay.sofa.jraft.storage.BaseStorageTest)

Aggregations

FutureImpl (com.alipay.sofa.jraft.rpc.impl.FutureImpl)9 Message (com.google.protobuf.Message)9 Test (org.junit.Test)8 RpcResponseClosure (com.alipay.sofa.jraft.rpc.RpcResponseClosure)6 Endpoint (com.alipay.sofa.jraft.util.Endpoint)6 SnapshotReader (com.alipay.sofa.jraft.storage.snapshot.SnapshotReader)5 Status (com.alipay.sofa.jraft.Status)3 RpcRequestClosure (com.alipay.sofa.jraft.rpc.RpcRequestClosure)3 BaseStorageTest (com.alipay.sofa.jraft.storage.BaseStorageTest)3 ByteBuffer (java.nio.ByteBuffer)3 LoadSnapshotClosure (com.alipay.sofa.jraft.closure.LoadSnapshotClosure)2 LocalSnapshotReader (com.alipay.sofa.jraft.storage.snapshot.local.LocalSnapshotReader)2 ByteBufferCollector (com.alipay.sofa.jraft.util.ByteBufferCollector)2 RemotingException (com.alipay.sofa.jraft.error.RemotingException)1 RpcRequests (com.alipay.sofa.jraft.rpc.RpcRequests)1 MockAsyncContext (com.alipay.sofa.jraft.test.MockAsyncContext)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Future (java.util.concurrent.Future)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1