Search in sources :

Example 1 with ServerReloadResponse

use of com.alibaba.nacos.api.remote.response.ServerReloadResponse in project nacos by alibaba.

the class ServerReloaderRequestHandlerTest method testHandle.

@Test
public void testHandle() {
    Mockito.when(connectionManager.currentClientsCount(Mockito.any())).thenReturn(2);
    ServerReloadRequest reloadRequest = new ServerReloadRequest();
    reloadRequest.setReloadCount(2);
    reloadRequest.setReloadServer("test");
    RequestMeta meta = new RequestMeta();
    meta.setClientIp("1.1.1.1");
    try {
        ServerReloadResponse reloadResponse = handler.handle(reloadRequest, meta);
        Assert.assertEquals("ignore", reloadResponse.getMessage());
    } catch (NacosException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
    reloadRequest.setReloadCount(1);
    try {
        ServerReloadResponse reloadResponse = handler.handle(reloadRequest, meta);
        Assert.assertEquals("ok", reloadResponse.getMessage());
    } catch (NacosException e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : ServerReloadResponse(com.alibaba.nacos.api.remote.response.ServerReloadResponse) RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) ServerReloadRequest(com.alibaba.nacos.api.remote.request.ServerReloadRequest) NacosException(com.alibaba.nacos.api.exception.NacosException) Test(org.junit.Test)

Example 2 with ServerReloadResponse

use of com.alibaba.nacos.api.remote.response.ServerReloadResponse in project nacos by alibaba.

the class ServerReloaderRequestHandler method handle.

@Override
public ServerReloadResponse handle(ServerReloadRequest request, RequestMeta meta) throws NacosException {
    ServerReloadResponse response = new ServerReloadResponse();
    Loggers.REMOTE.info("server reload request receive,reload count={},redirectServer={},requestIp={}", request.getReloadCount(), request.getReloadServer(), meta.getClientIp());
    int reloadCount = request.getReloadCount();
    Map<String, String> filter = new HashMap<String, String>(2);
    filter.put(RemoteConstants.LABEL_SOURCE, RemoteConstants.LABEL_SOURCE_SDK);
    int sdkCount = connectionManager.currentClientsCount(filter);
    if (sdkCount <= reloadCount) {
        response.setMessage("ignore");
    } else {
        reloadCount = (int) Math.max(reloadCount, sdkCount * (1 - RemoteUtils.LOADER_FACTOR));
        connectionManager.loadCount(reloadCount, request.getReloadServer());
        response.setMessage("ok");
    }
    return response;
}
Also used : ServerReloadResponse(com.alibaba.nacos.api.remote.response.ServerReloadResponse) HashMap(java.util.HashMap)

Aggregations

ServerReloadResponse (com.alibaba.nacos.api.remote.response.ServerReloadResponse)2 NacosException (com.alibaba.nacos.api.exception.NacosException)1 RequestMeta (com.alibaba.nacos.api.remote.request.RequestMeta)1 ServerReloadRequest (com.alibaba.nacos.api.remote.request.ServerReloadRequest)1 HashMap (java.util.HashMap)1 Test (org.junit.Test)1