use of com.alibaba.nacos.api.remote.DefaultRequestFuture in project nacos by alibaba.
the class NamingGrpcClientProxyTest method testServerListChanged.
@Test
public void testServerListChanged() throws Exception {
RpcClient rpc = new RpcClient("testServerListHasChanged", factory) {
@Override
public ConnectionType getConnectionType() {
return ConnectionType.GRPC;
}
@Override
public int rpcPortOffset() {
return 0;
}
@Override
public Connection connectToServer(ServerInfo serverInfo) throws Exception {
return new Connection(serverInfo) {
@Override
public Response request(Request request, long timeoutMills) throws NacosException {
Response response = new Response() {
};
response.setRequestId(request.getRequestId());
return response;
}
@Override
public RequestFuture requestFuture(Request request) throws NacosException {
return new DefaultRequestFuture("test", request.getRequestId());
}
@Override
public void asyncRequest(Request request, RequestCallBack requestCallBack) throws NacosException {
}
@Override
public void close() {
}
};
}
};
Field rpcClient = NamingGrpcClientProxy.class.getDeclaredField("rpcClient");
rpcClient.setAccessible(true);
rpcClient.set(client, rpc);
rpc.serverListFactory(factory);
rpc.registerServerRequestHandler(new NamingPushRequestHandler(holder));
Field listenerField = NamingGrpcClientProxy.class.getDeclaredField("redoService");
listenerField.setAccessible(true);
NamingGrpcRedoService listener = (NamingGrpcRedoService) listenerField.get(client);
rpc.registerConnectionListener(listener);
rpc.start();
int retry = 10;
while (!rpc.isRunning()) {
TimeUnit.SECONDS.sleep(1);
if (--retry < 0) {
Assert.fail("rpc is not running");
}
}
Assert.assertEquals(ORIGIN_SERVER, rpc.getCurrentServer().getServerIp());
String newServer = "www.aliyun.com";
when(factory.genNextServer()).thenReturn(newServer);
when(factory.getServerList()).thenReturn(Stream.of(newServer, "anotherServer").collect(Collectors.toList()));
NotifyCenter.publishEvent(new ServerListChangedEvent());
retry = 10;
while (ORIGIN_SERVER.equals(rpc.getCurrentServer().getServerIp())) {
TimeUnit.SECONDS.sleep(1);
if (--retry < 0) {
Assert.fail("failed to auth switch server");
}
}
Assert.assertEquals(newServer, rpc.getCurrentServer().getServerIp());
}
use of com.alibaba.nacos.api.remote.DefaultRequestFuture in project nacos by alibaba.
the class GrpcConnection method sendRequestInner.
private DefaultRequestFuture sendRequestInner(Request request, RequestCallBack callBack) throws NacosException {
final String requestId = String.valueOf(PushAckIdGenerator.getNextId());
request.setRequestId(requestId);
DefaultRequestFuture defaultPushFuture = new DefaultRequestFuture(getMetaInfo().getConnectionId(), requestId, callBack, () -> RpcAckCallbackSynchronizer.clearFuture(getMetaInfo().getConnectionId(), requestId));
RpcAckCallbackSynchronizer.syncCallback(getMetaInfo().getConnectionId(), requestId, defaultPushFuture);
sendRequestNoAck(request);
return defaultPushFuture;
}
use of com.alibaba.nacos.api.remote.DefaultRequestFuture in project nacos by alibaba.
the class RpcAckCallbackSynchronizer method ackNotify.
/**
* notify ack.
*/
public static void ackNotify(String connectionId, Response response) {
Map<String, DefaultRequestFuture> stringDefaultPushFutureMap = CALLBACK_CONTEXT.get(connectionId);
if (stringDefaultPushFutureMap == null) {
Loggers.REMOTE_DIGEST.warn("Ack receive on a outdated connection ,connection id={},requestId={} ", connectionId, response.getRequestId());
return;
}
DefaultRequestFuture currentCallback = stringDefaultPushFutureMap.remove(response.getRequestId());
if (currentCallback == null) {
Loggers.REMOTE_DIGEST.warn("Ack receive on a outdated request ,connection id={},requestId={} ", connectionId, response.getRequestId());
return;
}
if (response.isSuccess()) {
currentCallback.setResponse(response);
} else {
currentCallback.setFailResult(new NacosException(response.getErrorCode(), response.getMessage()));
}
}
Aggregations