use of com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse in project sofa-jraft by sofastack.
the class NodeRequestProcessorTest method testInvalidPeerId.
@Test
public void testInvalidPeerId() {
this.processor = new MockRequestProcessor("localhost", "test");
this.processor.handleRequest(asyncContext, TestUtils.createPingRequest());
ErrorResponse resp = (ErrorResponse) asyncContext.getResponseObject();
assertNotNull(resp);
assertEquals(RaftError.EINVAL.getNumber(), resp.getErrorCode());
assertEquals("Fail to parse peerId: localhost", resp.getErrorMsg());
}
use of com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse in project sofa-jraft by sofastack.
the class AbstractClientServiceTest method testInvokeWithDoneOK.
@Test
public void testInvokeWithDoneOK() throws Exception {
ArgumentCaptor<InvokeCallback> callbackArg = ArgumentCaptor.forClass(InvokeCallback.class);
PingRequest request = TestUtils.createPingRequest();
MockRpcResponseClosure<ErrorResponse> done = new MockRpcResponseClosure<>();
Future<Message> future = this.clientService.invokeWithDone(this.endpoint, request, done, -1);
Mockito.verify(this.rpcClient).invokeAsync(eq(this.endpoint), eq(request), Mockito.any(), callbackArg.capture(), eq((long) this.rpcOptions.getRpcDefaultTimeout()));
InvokeCallback cb = callbackArg.getValue();
assertNotNull(cb);
assertNotNull(future);
assertNull(done.getResponse());
assertNull(done.status);
assertFalse(future.isDone());
ErrorResponse response = (ErrorResponse) this.rpcResponseFactory.newResponse(null, Status.OK());
cb.complete(response, null);
Message msg = future.get();
assertNotNull(msg);
assertTrue(msg instanceof ErrorResponse);
assertSame(msg, response);
done.latch.await();
assertNotNull(done.status);
assertEquals(0, done.status.getCode());
}
use of com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse in project sofa-jraft by sofastack.
the class AbstractClientServiceTest method testCancel.
@Test
public void testCancel() throws Exception {
ArgumentCaptor<InvokeCallback> callbackArg = ArgumentCaptor.forClass(InvokeCallback.class);
PingRequest request = TestUtils.createPingRequest();
MockRpcResponseClosure<ErrorResponse> done = new MockRpcResponseClosure<>();
Future<Message> future = this.clientService.invokeWithDone(this.endpoint, request, done, -1);
Mockito.verify(this.rpcClient).invokeAsync(eq(this.endpoint), eq(request), Mockito.any(), callbackArg.capture(), eq((long) this.rpcOptions.getRpcDefaultTimeout()));
InvokeCallback cb = callbackArg.getValue();
assertNotNull(cb);
assertNotNull(future);
assertNull(done.getResponse());
assertNull(done.status);
assertFalse(future.isDone());
future.cancel(true);
ErrorResponse response = (ErrorResponse) this.rpcResponseFactory.newResponse(null, Status.OK());
cb.complete(response, null);
// The closure should be notified with ECANCELED error code.
done.latch.await();
assertNotNull(done.status);
assertEquals(RaftError.ECANCELED.getNumber(), done.status.getCode());
}
use of com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse in project sofa-jraft by sofastack.
the class CliServiceImpl method getLeader.
@Override
public Status getLeader(final String groupId, final Configuration conf, final PeerId leaderId) {
Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
Requires.requireNonNull(leaderId, "Null leader id");
if (conf == null || conf.isEmpty()) {
return new Status(RaftError.EINVAL, "Empty group configuration");
}
final Status st = new Status(-1, "Fail to get leader of group %s", groupId);
for (final PeerId peer : conf) {
if (!this.cliClientService.connect(peer.getEndpoint())) {
LOG.error("Fail to connect peer {} to get leader for group {}.", peer, groupId);
continue;
}
final GetLeaderRequest.Builder rb = //
GetLeaderRequest.newBuilder().setGroupId(//
groupId).setPeerId(peer.toString());
final Future<Message> result = this.cliClientService.getLeader(peer.getEndpoint(), rb.build(), null);
try {
final Message msg = result.get(this.cliOptions.getTimeoutMs() <= 0 ? this.cliOptions.getRpcDefaultTimeout() : this.cliOptions.getTimeoutMs(), TimeUnit.MILLISECONDS);
if (msg instanceof ErrorResponse) {
if (st.isOk()) {
st.setError(-1, ((ErrorResponse) msg).getErrorMsg());
} else {
final String savedMsg = st.getErrorMsg();
st.setError(-1, "%s, %s", savedMsg, ((ErrorResponse) msg).getErrorMsg());
}
} else {
final GetLeaderResponse response = (GetLeaderResponse) msg;
if (leaderId.parse(response.getLeaderId())) {
break;
}
}
} catch (final Exception e) {
if (st.isOk()) {
st.setError(-1, e.getMessage());
} else {
final String savedMsg = st.getErrorMsg();
st.setError(-1, "%s, %s", savedMsg, e.getMessage());
}
}
}
if (leaderId.isEmpty()) {
return st;
}
return Status.OK();
}
use of com.alipay.sofa.jraft.rpc.RpcRequests.ErrorResponse in project sofa-jraft by sofastack.
the class CliServiceImpl method getPeers.
private List<PeerId> getPeers(final String groupId, final Configuration conf, final boolean returnLearners, final boolean onlyGetAlive) {
Requires.requireTrue(!StringUtils.isBlank(groupId), "Blank group id");
Requires.requireNonNull(conf, "Null conf");
final PeerId leaderId = new PeerId();
final Status st = getLeader(groupId, conf, leaderId);
if (!st.isOk()) {
throw new IllegalStateException(st.getErrorMsg());
}
if (!this.cliClientService.connect(leaderId.getEndpoint())) {
throw new IllegalStateException("Fail to init channel to leader " + leaderId);
}
final GetPeersRequest.Builder rb = //
GetPeersRequest.newBuilder().setGroupId(//
groupId).setLeaderId(//
leaderId.toString()).setOnlyAlive(onlyGetAlive);
try {
final Message result = this.cliClientService.getPeers(leaderId.getEndpoint(), rb.build(), null).get(this.cliOptions.getTimeoutMs() <= 0 ? this.cliOptions.getRpcDefaultTimeout() : this.cliOptions.getTimeoutMs(), TimeUnit.MILLISECONDS);
if (result instanceof GetPeersResponse) {
final GetPeersResponse resp = (GetPeersResponse) result;
final List<PeerId> peerIdList = new ArrayList<>();
final ProtocolStringList responsePeers = returnLearners ? resp.getLearnersList() : resp.getPeersList();
for (final String peerIdStr : responsePeers) {
final PeerId newPeer = new PeerId();
newPeer.parse(peerIdStr);
peerIdList.add(newPeer);
}
return peerIdList;
} else {
final ErrorResponse resp = (ErrorResponse) result;
throw new JRaftException(resp.getErrorMsg());
}
} catch (final JRaftException e) {
throw e;
} catch (final Exception e) {
throw new JRaftException(e);
}
}
Aggregations