use of com.alibaba.nacos.consistency.entity.Response in project nacos by alibaba.
the class JRaftServer method invokeToLeader.
private void invokeToLeader(final String group, final Message request, final int timeoutMillis, FailoverClosure closure) {
try {
final Endpoint leaderIp = Optional.ofNullable(getLeader(group)).orElseThrow(() -> new NoLeaderException(group)).getEndpoint();
cliClientService.getRpcClient().invokeAsync(leaderIp, request, new InvokeCallback() {
@Override
public void complete(Object o, Throwable ex) {
if (Objects.nonNull(ex)) {
closure.setThrowable(ex);
closure.run(new Status(RaftError.UNKNOWN, ex.getMessage()));
return;
}
if (!((Response) o).getSuccess()) {
closure.setThrowable(new IllegalStateException(((Response) o).getErrMsg()));
closure.run(new Status(RaftError.UNKNOWN, ((Response) o).getErrMsg()));
return;
}
closure.setResponse((Response) o);
closure.run(Status.OK());
}
@Override
public Executor executor() {
return RaftExecutor.getRaftCliServiceExecutor();
}
}, timeoutMillis);
} catch (Exception e) {
closure.setThrowable(e);
closure.run(new Status(RaftError.UNKNOWN, e.toString()));
}
}
use of com.alibaba.nacos.consistency.entity.Response in project nacos by alibaba.
the class JRaftServer method get.
CompletableFuture<Response> get(final ReadRequest request) {
final String group = request.getGroup();
CompletableFuture<Response> future = new CompletableFuture<>();
final RaftGroupTuple tuple = findTupleByGroup(group);
if (Objects.isNull(tuple)) {
future.completeExceptionally(new NoSuchRaftGroupException(group));
return future;
}
final Node node = tuple.node;
final RequestProcessor processor = tuple.processor;
try {
node.readIndex(BytesUtil.EMPTY_BYTES, new ReadIndexClosure() {
@Override
public void run(Status status, long index, byte[] reqCtx) {
if (status.isOk()) {
try {
Response response = processor.onRequest(request);
future.complete(response);
} catch (Throwable t) {
MetricsMonitor.raftReadIndexFailed();
future.completeExceptionally(new ConsistencyException("The conformance protocol is temporarily unavailable for reading", t));
}
return;
}
MetricsMonitor.raftReadIndexFailed();
Loggers.RAFT.error("ReadIndex has error : {}, go to Leader read.", status.getErrorMsg());
MetricsMonitor.raftReadFromLeader();
readFromLeader(request, future);
}
});
return future;
} catch (Throwable e) {
MetricsMonitor.raftReadFromLeader();
Loggers.RAFT.warn("Raft linear read failed, go to Leader read logic : {}", e.toString());
// run raft read
readFromLeader(request, future);
return future;
}
}
use of com.alibaba.nacos.consistency.entity.Response in project nacos by alibaba.
the class AbstractProcessor method execute.
protected void execute(JRaftServer server, final RpcContext asyncCtx, final Message message, final JRaftServer.RaftGroupTuple tuple) {
FailoverClosure closure = new FailoverClosure() {
Response data;
Throwable ex;
@Override
public void setResponse(Response data) {
this.data = data;
}
@Override
public void setThrowable(Throwable throwable) {
this.ex = throwable;
}
@Override
public void run(Status status) {
if (Objects.nonNull(ex)) {
Loggers.RAFT.error("execute has error : ", ex);
asyncCtx.sendResponse(Response.newBuilder().setErrMsg(ex.toString()).setSuccess(false).build());
} else {
asyncCtx.sendResponse(data);
}
}
};
server.applyOperation(tuple.getNode(), message, closure);
}
use of com.alibaba.nacos.consistency.entity.Response in project nacos by alibaba.
the class PersistentClientOperationServiceImplTest method testOnApply.
@Test
public void testOnApply() {
PersistentClientOperationServiceImpl.InstanceStoreRequest request = new PersistentClientOperationServiceImpl.InstanceStoreRequest();
Service service1 = Service.newService("A", "B", "C");
request.setService(service1);
request.setClientId("xxxx");
request.setInstance(new Instance());
Mockito.when(serializer.deserialize(Mockito.any())).thenReturn(request);
Mockito.when(clientManager.contains(Mockito.anyString())).thenReturn(true);
IpPortBasedClient ipPortBasedClient = Mockito.mock(IpPortBasedClient.class);
Mockito.when(clientManager.getClient(Mockito.anyString())).thenReturn(ipPortBasedClient);
WriteRequest writeRequest = WriteRequest.newBuilder().setOperation(DataOperation.ADD.name()).build();
Response response = persistentClientOperationServiceImpl.onApply(writeRequest);
Assert.assertTrue(response.getSuccess());
writeRequest = WriteRequest.newBuilder().setOperation(DataOperation.DELETE.name()).build();
response = persistentClientOperationServiceImpl.onApply(writeRequest);
Assert.assertTrue(response.getSuccess());
ServiceManager.getInstance().removeSingleton(service1);
writeRequest = WriteRequest.newBuilder().setOperation(DataOperation.VERIFY.name()).build();
response = persistentClientOperationServiceImpl.onApply(writeRequest);
Assert.assertFalse(response.getSuccess());
}
Aggregations