use of com.alipay.sofa.jraft.rpc.RpcContext in project sofa-jraft by sofastack.
the class FileServiceTest method testGetFileData.
@Test
public void testGetFileData() throws IOException {
writeData();
long readerId = FileService.getInstance().addReader(this.fileReader);
RpcRequests.GetFileRequest request = RpcRequests.GetFileRequest.newBuilder().setCount(Integer.MAX_VALUE).setFilename("data").setOffset(0).setReaderId(readerId).build();
RpcContext asyncContext = Mockito.mock(RpcContext.class);
Message msg = FileService.getInstance().handleGetFile(request, new RpcRequestClosure(asyncContext));
assertTrue(msg instanceof RpcRequests.GetFileResponse);
RpcRequests.GetFileResponse response = (RpcRequests.GetFileResponse) msg;
assertTrue(response.getEof());
assertEquals("jraft is great!", new String(response.getData().toByteArray()));
assertEquals(-1, response.getReadSize());
}
use of com.alipay.sofa.jraft.rpc.RpcContext in project sofa-jraft by sofastack.
the class FileServiceTest method testGetFileNotFound.
@Test
public void testGetFileNotFound() {
long readerId = FileService.getInstance().addReader(this.fileReader);
RpcRequests.GetFileRequest request = RpcRequests.GetFileRequest.newBuilder().setCount(Integer.MAX_VALUE).setFilename("data").setOffset(0).setReaderId(readerId).build();
RpcContext asyncContext = Mockito.mock(RpcContext.class);
Message msg = FileService.getInstance().handleGetFile(request, new RpcRequestClosure(asyncContext));
assertTrue(msg instanceof RpcRequests.ErrorResponse);
RpcRequests.ErrorResponse response = (RpcRequests.ErrorResponse) msg;
assertEquals(RaftError.EIO.getNumber(), response.getErrorCode());
assertEquals(String.format("Fail to read from path=%s filename=data", this.path), response.getErrorMsg());
}
use of com.alipay.sofa.jraft.rpc.RpcContext in project sofa-jraft by sofastack.
the class AppendEntriesRequestProcessorTest method testSendSequenceResponse.
@Test
public void testSendSequenceResponse() {
mockNode();
final AppendEntriesRequestProcessor processor = (AppendEntriesRequestProcessor) newProcessor();
final PeerPair pair = processor.pairOf(this.peerIdStr, this.serverId);
processor.getOrCreatePeerRequestContext(this.groupId, pair, this.conn);
final PingRequest msg = TestUtils.createPingRequest();
final RpcContext asyncContext = Mockito.mock(RpcContext.class);
processor.sendSequenceResponse(this.groupId, pair, 1, asyncContext, msg);
Mockito.verify(asyncContext, Mockito.never()).sendResponse(msg);
processor.sendSequenceResponse(this.groupId, pair, 0, asyncContext, msg);
Mockito.verify(asyncContext, Mockito.times(2)).sendResponse(msg);
}
use of com.alipay.sofa.jraft.rpc.RpcContext in project sofa-jraft by sofastack.
the class GrpcServer method registerProcessor.
@SuppressWarnings("unchecked")
@Override
public void registerProcessor(final RpcProcessor processor) {
final String interest = processor.interest();
final Message reqIns = Requires.requireNonNull(this.parserClasses.get(interest), "null default instance: " + interest);
final MethodDescriptor<Message, Message> method = //
MethodDescriptor.<Message, Message>newBuilder().setType(//
MethodDescriptor.MethodType.UNARY).setFullMethodName(//
MethodDescriptor.generateFullMethodName(processor.interest(), GrpcRaftRpcFactory.FIXED_METHOD_NAME)).setRequestMarshaller(//
ProtoUtils.marshaller(reqIns)).setResponseMarshaller(//
ProtoUtils.marshaller(this.marshallerRegistry.findResponseInstanceByRequest(interest))).build();
final ServerCallHandler<Message, Message> handler = ServerCalls.asyncUnaryCall((request, responseObserver) -> {
final SocketAddress remoteAddress = RemoteAddressInterceptor.getRemoteAddress();
final Connection conn = ConnectionInterceptor.getCurrentConnection(this.closedEventListeners);
final RpcContext rpcCtx = new RpcContext() {
@Override
public void sendResponse(final Object responseObj) {
try {
responseObserver.onNext((Message) responseObj);
responseObserver.onCompleted();
} catch (final Throwable t) {
LOG.warn("[GRPC] failed to send response.", t);
}
}
@Override
public Connection getConnection() {
if (conn == null) {
throw new IllegalStateException("fail to get connection");
}
return conn;
}
@Override
public String getRemoteAddress() {
// Rely on GRPC's capabilities, not magic (netty channel)
return remoteAddress != null ? remoteAddress.toString() : null;
}
};
final RpcProcessor.ExecutorSelector selector = processor.executorSelector();
Executor executor;
if (selector != null && request instanceof RpcRequests.AppendEntriesRequest) {
final RpcRequests.AppendEntriesRequest req = (RpcRequests.AppendEntriesRequest) request;
final RpcRequests.AppendEntriesRequestHeader.Builder header = //
RpcRequests.AppendEntriesRequestHeader.newBuilder().setGroupId(//
req.getGroupId()).setPeerId(//
req.getPeerId()).setServerId(req.getServerId());
executor = selector.select(interest, header.build());
} else {
executor = processor.executor();
}
if (executor == null) {
executor = this.defaultExecutor;
}
if (executor != null) {
executor.execute(() -> processor.handleRequest(rpcCtx, request));
} else {
processor.handleRequest(rpcCtx, request);
}
});
final ServerServiceDefinition serviceDef = //
ServerServiceDefinition.builder(//
interest).addMethod(method, //
handler).build();
this.handlerRegistry.addService(ServerInterceptors.intercept(serviceDef, this.serverInterceptors.toArray(new ServerInterceptor[0])));
}
use of com.alipay.sofa.jraft.rpc.RpcContext in project nacos by alibaba.
the class AbstractProcessorTest method testErrorThroughRpc.
@Test
public void testErrorThroughRpc() {
final AtomicReference<Response> reference = new AtomicReference<>();
RpcContext context = new RpcContext() {
@Override
public void sendResponse(Object responseObj) {
reference.set((Response) responseObj);
}
@Override
public Connection getConnection() {
return null;
}
@Override
public String getRemoteAddress() {
return null;
}
};
AbstractProcessor processor = new NacosLogProcessor(server, SerializeFactory.getDefault());
processor.execute(server, context, WriteRequest.newBuilder().build(), new JRaftServer.RaftGroupTuple());
Response response = reference.get();
Assert.assertNotNull(response);
Assert.assertEquals("Error message transmission", response.getErrMsg());
Assert.assertFalse(response.getSuccess());
}
Aggregations