Search in sources :

Example 1 with ConnectionMeta

use of com.alibaba.nacos.core.remote.ConnectionMeta in project nacos by alibaba.

the class RpcConfigChangeNotifier method configDataChanged.

/**
 * adaptor to config module ,when server side config change ,invoke this method.
 *
 * @param groupKey groupKey
 */
public void configDataChanged(String groupKey, String dataId, String group, String tenant, boolean isBeta, List<String> betaIps, String tag) {
    Set<String> listeners = configChangeListenContext.getListeners(groupKey);
    if (CollectionUtils.isEmpty(listeners)) {
        return;
    }
    int notifyClientCount = 0;
    for (final String client : listeners) {
        Connection connection = connectionManager.getConnection(client);
        if (connection == null) {
            continue;
        }
        ConnectionMeta metaInfo = connection.getMetaInfo();
        // beta ips check.
        String clientIp = metaInfo.getClientIp();
        String clientTag = metaInfo.getTag();
        if (isBeta && betaIps != null && !betaIps.contains(clientIp)) {
            continue;
        }
        // tag check
        if (StringUtils.isNotBlank(tag) && !tag.equals(clientTag)) {
            continue;
        }
        ConfigChangeNotifyRequest notifyRequest = ConfigChangeNotifyRequest.build(dataId, group, tenant);
        RpcPushTask rpcPushRetryTask = new RpcPushTask(notifyRequest, 50, client, clientIp, metaInfo.getAppName());
        push(rpcPushRetryTask);
        notifyClientCount++;
    }
    Loggers.REMOTE_PUSH.info("push [{}] clients ,groupKey=[{}]", notifyClientCount, groupKey);
}
Also used : Connection(com.alibaba.nacos.core.remote.Connection) ConnectionMeta(com.alibaba.nacos.core.remote.ConnectionMeta) ConfigChangeNotifyRequest(com.alibaba.nacos.api.config.remote.request.ConfigChangeNotifyRequest) TpsMonitorPoint(com.alibaba.nacos.core.remote.control.TpsMonitorPoint)

Example 2 with ConnectionMeta

use of com.alibaba.nacos.core.remote.ConnectionMeta in project nacos by alibaba.

the class CommunicationControllerTest method testGetSubClientConfig2x.

@Test
public void testGetSubClientConfig2x() throws Exception {
    SampleResult result = new SampleResult();
    result.setLisentersGroupkeyStatus(new HashMap<>());
    when(longPollingService.getCollectSubscribleInfo("test", "test", "test")).thenReturn(result);
    String groupKey = GroupKey2.getKey("test", "test", "test");
    Set<String> listenersClients = new HashSet<>();
    String connectionId = "127.0.0.1";
    listenersClients.add(connectionId);
    when(configChangeListenContext.getListeners(groupKey)).thenReturn(listenersClients);
    ConnectionMeta connectionMeta = new ConnectionMeta(connectionId, connectionId, connectionId, 8888, 9848, "GRPC", "", "", new HashMap<>());
    Connection client = new GrpcConnection(connectionMeta, null, null);
    when(connectionManager.getConnection(connectionId)).thenReturn(client);
    when(configChangeListenContext.getListenKeyMd5(connectionId, groupKey)).thenReturn("md5");
    MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.COMMUNICATION_CONTROLLER_PATH + "/configWatchers").param("dataId", "test").param("group", "test").param("tenant", "test");
    String actualValue = mockMvc.perform(builder).andReturn().getResponse().getContentAsString();
    Assert.assertEquals("{\"127.0.0.1\":\"md5\"}", JacksonUtils.toObj(actualValue).get("lisentersGroupkeyStatus").toString());
}
Also used : GrpcConnection(com.alibaba.nacos.core.remote.grpc.GrpcConnection) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) GrpcConnection(com.alibaba.nacos.core.remote.grpc.GrpcConnection) Connection(com.alibaba.nacos.core.remote.Connection) SampleResult(com.alibaba.nacos.config.server.model.SampleResult) ConnectionMeta(com.alibaba.nacos.core.remote.ConnectionMeta) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with ConnectionMeta

use of com.alibaba.nacos.core.remote.ConnectionMeta in project nacos by alibaba.

the class GrpcRequestAcceptorTest method testHandleRequestSuccess.

@Test
public void testHandleRequestSuccess() {
    ApplicationUtils.setStarted(true);
    Mockito.when(requestHandlerRegistry.getByRequestType(Mockito.anyString())).thenReturn(mockHandler);
    Mockito.when(connectionManager.checkValid(Mockito.any())).thenReturn(true);
    String ip = "1.1.1.1";
    ConnectionMeta connectionMeta = new ConnectionMeta(connectId, ip, ip, 8888, 9848, "GRPC", "", "", new HashMap<>());
    Connection connection = new GrpcConnection(connectionMeta, null, null);
    Mockito.when(connectionManager.getConnection(Mockito.any())).thenReturn(connection);
    RequestMeta metadata = new RequestMeta();
    metadata.setClientIp("127.0.0.1");
    metadata.setConnectionId(connectId);
    HealthCheckRequest mockRequest = new HealthCheckRequest();
    Payload payload = GrpcUtils.convert(mockRequest, metadata);
    StreamObserver<Payload> streamObserver = new StreamObserver<Payload>() {

        @Override
        public void onNext(Payload payload) {
            System.out.println("Receive data from server: " + payload);
            Object res = GrpcUtils.parse(payload);
            Assert.assertTrue(res instanceof HealthCheckResponse);
        }

        @Override
        public void onError(Throwable throwable) {
            Assert.fail(throwable.getMessage());
        }

        @Override
        public void onCompleted() {
            System.out.println("complete");
        }
    };
    streamStub.request(payload, streamObserver);
    ApplicationUtils.setStarted(false);
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) HealthCheckResponse(com.alibaba.nacos.api.remote.response.HealthCheckResponse) RequestMeta(com.alibaba.nacos.api.remote.request.RequestMeta) Connection(com.alibaba.nacos.core.remote.Connection) ConnectionMeta(com.alibaba.nacos.core.remote.ConnectionMeta) Payload(com.alibaba.nacos.api.grpc.auto.Payload) HealthCheckRequest(com.alibaba.nacos.api.remote.request.HealthCheckRequest) Test(org.junit.Test)

Example 4 with ConnectionMeta

use of com.alibaba.nacos.core.remote.ConnectionMeta in project nacos by alibaba.

the class CommunicationControllerTest method testGetSubClientConfigByIp.

@Test
public void testGetSubClientConfigByIp() throws Exception {
    String ip = "127.0.0.1";
    SampleResult result = new SampleResult();
    result.setLisentersGroupkeyStatus(new HashMap<>());
    when(longPollingService.getCollectSubscribleInfoByIp(ip)).thenReturn(result);
    ConnectionMeta connectionMeta = new ConnectionMeta(ip, ip, ip, 8888, 9848, "GRPC", "", "", new HashMap<>());
    Connection connection = new GrpcConnection(connectionMeta, null, null);
    List<Connection> connectionList = new ArrayList<>();
    connectionList.add(connection);
    when(connectionManager.getConnectionByIp(ip)).thenReturn(connectionList);
    Map<String, String> map = new HashMap<>();
    map.put("test", "test");
    when(configChangeListenContext.getListenKeys(ip)).thenReturn(map);
    MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.COMMUNICATION_CONTROLLER_PATH + "/watcherConfigs").param("ip", ip);
    String actualValue = mockMvc.perform(builder).andReturn().getResponse().getContentAsString();
    Assert.assertEquals("{\"test\":\"test\"}", JacksonUtils.toObj(actualValue).get("lisentersGroupkeyStatus").toString());
}
Also used : HashMap(java.util.HashMap) GrpcConnection(com.alibaba.nacos.core.remote.grpc.GrpcConnection) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) GrpcConnection(com.alibaba.nacos.core.remote.grpc.GrpcConnection) Connection(com.alibaba.nacos.core.remote.Connection) ArrayList(java.util.ArrayList) SampleResult(com.alibaba.nacos.config.server.model.SampleResult) ConnectionMeta(com.alibaba.nacos.core.remote.ConnectionMeta) Test(org.junit.Test)

Example 5 with ConnectionMeta

use of com.alibaba.nacos.core.remote.ConnectionMeta in project nacos by alibaba.

the class GrpcBiStreamRequestAcceptor method requestBiStream.

@Override
public StreamObserver<Payload> requestBiStream(StreamObserver<Payload> responseObserver) {
    StreamObserver<Payload> streamObserver = new StreamObserver<Payload>() {

        final String connectionId = CONTEXT_KEY_CONN_ID.get();

        final Integer localPort = CONTEXT_KEY_CONN_LOCAL_PORT.get();

        final int remotePort = CONTEXT_KEY_CONN_REMOTE_PORT.get();

        String remoteIp = CONTEXT_KEY_CONN_REMOTE_IP.get();

        String clientIp = "";

        @Override
        public void onNext(Payload payload) {
            clientIp = payload.getMetadata().getClientIp();
            traceDetailIfNecessary(payload);
            Object parseObj;
            try {
                parseObj = GrpcUtils.parse(payload);
            } catch (Throwable throwable) {
                Loggers.REMOTE_DIGEST.warn("[{}]Grpc request bi stream,payload parse error={}", connectionId, throwable);
                return;
            }
            if (parseObj == null) {
                Loggers.REMOTE_DIGEST.warn("[{}]Grpc request bi stream,payload parse null ,body={},meta={}", connectionId, payload.getBody().getValue().toStringUtf8(), payload.getMetadata());
                return;
            }
            if (parseObj instanceof ConnectionSetupRequest) {
                ConnectionSetupRequest setUpRequest = (ConnectionSetupRequest) parseObj;
                Map<String, String> labels = setUpRequest.getLabels();
                String appName = "-";
                if (labels != null && labels.containsKey(Constants.APPNAME)) {
                    appName = labels.get(Constants.APPNAME);
                }
                ConnectionMeta metaInfo = new ConnectionMeta(connectionId, payload.getMetadata().getClientIp(), remoteIp, remotePort, localPort, ConnectionType.GRPC.getType(), setUpRequest.getClientVersion(), appName, setUpRequest.getLabels());
                metaInfo.setTenant(setUpRequest.getTenant());
                Connection connection = new GrpcConnection(metaInfo, responseObserver, CONTEXT_KEY_CHANNEL.get());
                connection.setAbilities(setUpRequest.getAbilities());
                boolean rejectSdkOnStarting = metaInfo.isSdkSource() && !ApplicationUtils.isStarted();
                if (rejectSdkOnStarting || !connectionManager.register(connectionId, connection)) {
                    // Not register to the connection manager if current server is over limit or server is starting.
                    try {
                        Loggers.REMOTE_DIGEST.warn("[{}]Connection register fail,reason:{}", connectionId, rejectSdkOnStarting ? " server is not started" : " server is over limited.");
                        connection.request(new ConnectResetRequest(), 3000L);
                        connection.close();
                    } catch (Exception e) {
                        // Do nothing.
                        if (connectionManager.traced(clientIp)) {
                            Loggers.REMOTE_DIGEST.warn("[{}]Send connect reset request error,error={}", connectionId, e);
                        }
                    }
                }
            } else if (parseObj instanceof Response) {
                Response response = (Response) parseObj;
                if (connectionManager.traced(clientIp)) {
                    Loggers.REMOTE_DIGEST.warn("[{}]Receive response of server request  ,response={}", connectionId, response);
                }
                RpcAckCallbackSynchronizer.ackNotify(connectionId, response);
                connectionManager.refreshActiveTime(connectionId);
            } else {
                Loggers.REMOTE_DIGEST.warn("[{}]Grpc request bi stream,unknown payload receive ,parseObj={}", connectionId, parseObj);
            }
        }

        @Override
        public void onError(Throwable t) {
            if (connectionManager.traced(clientIp)) {
                Loggers.REMOTE_DIGEST.warn("[{}]Bi stream on error,error={}", connectionId, t);
            }
            if (responseObserver instanceof ServerCallStreamObserver) {
                ServerCallStreamObserver serverCallStreamObserver = ((ServerCallStreamObserver) responseObserver);
                if (serverCallStreamObserver.isCancelled()) {
                // client close the stream.
                } else {
                    try {
                        serverCallStreamObserver.onCompleted();
                    } catch (Throwable throwable) {
                    // ignore
                    }
                }
            }
        }

        @Override
        public void onCompleted() {
            if (connectionManager.traced(clientIp)) {
                Loggers.REMOTE_DIGEST.warn("[{}]Bi stream on completed", connectionId);
            }
            if (responseObserver instanceof ServerCallStreamObserver) {
                ServerCallStreamObserver serverCallStreamObserver = ((ServerCallStreamObserver) responseObserver);
                if (serverCallStreamObserver.isCancelled()) {
                // client close the stream.
                } else {
                    try {
                        serverCallStreamObserver.onCompleted();
                    } catch (Throwable throwable) {
                    // ignore
                    }
                }
            }
        }
    };
    return streamObserver;
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) ServerCallStreamObserver(io.grpc.stub.ServerCallStreamObserver) Connection(com.alibaba.nacos.core.remote.Connection) ConnectResetRequest(com.alibaba.nacos.api.remote.request.ConnectResetRequest) Response(com.alibaba.nacos.api.remote.response.Response) Payload(com.alibaba.nacos.api.grpc.auto.Payload) ConnectionMeta(com.alibaba.nacos.core.remote.ConnectionMeta) ConnectionSetupRequest(com.alibaba.nacos.api.remote.request.ConnectionSetupRequest)

Aggregations

Connection (com.alibaba.nacos.core.remote.Connection)6 ConnectionMeta (com.alibaba.nacos.core.remote.ConnectionMeta)6 Test (org.junit.Test)4 GrpcConnection (com.alibaba.nacos.core.remote.grpc.GrpcConnection)3 Payload (com.alibaba.nacos.api.grpc.auto.Payload)2 SampleResult (com.alibaba.nacos.config.server.model.SampleResult)2 StreamObserver (io.grpc.stub.StreamObserver)2 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)2 ConfigChangeNotifyRequest (com.alibaba.nacos.api.config.remote.request.ConfigChangeNotifyRequest)1 ConnectResetRequest (com.alibaba.nacos.api.remote.request.ConnectResetRequest)1 ConnectionSetupRequest (com.alibaba.nacos.api.remote.request.ConnectionSetupRequest)1 HealthCheckRequest (com.alibaba.nacos.api.remote.request.HealthCheckRequest)1 RequestMeta (com.alibaba.nacos.api.remote.request.RequestMeta)1 HealthCheckResponse (com.alibaba.nacos.api.remote.response.HealthCheckResponse)1 Response (com.alibaba.nacos.api.remote.response.Response)1 TpsMonitorPoint (com.alibaba.nacos.core.remote.control.TpsMonitorPoint)1 ServerCallStreamObserver (io.grpc.stub.ServerCallStreamObserver)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1