use of com.alibaba.nacos.core.remote.Connection 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);
}
use of com.alibaba.nacos.core.remote.Connection 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());
}
use of com.alibaba.nacos.core.remote.Connection 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);
}
use of com.alibaba.nacos.core.remote.Connection in project nacos by alibaba.
the class ClientMetricsControllerTest method testGetCurrentMetric.
@Test
public void testGetCurrentMetric() throws Exception {
ClientConfigMetricResponse response = new ClientConfigMetricResponse();
response.putMetric("test", "test");
Connection connection = Mockito.mock(Connection.class);
when(connection.request(any(), anyLong())).thenReturn(response);
List<Connection> connections = new ArrayList<>();
connections.add(connection);
when(connectionManager.getConnectionByIp(eq("127.0.0.1"))).thenReturn(connections);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.METRICS_CONTROLLER_PATH + "/current").param("ip", "127.0.0.1").param("tenant", "test").param("dataId", "test").param("group", "test");
String actualValue = mockMvc.perform(builder).andReturn().getResponse().getContentAsString();
Assert.assertEquals("{\"test\":\"test\"}", actualValue);
}
use of com.alibaba.nacos.core.remote.Connection 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());
}
Aggregations