Skip to content

Commit

Permalink
Feat/calculate ip count when use batchregister (alibaba#8858)
Browse files Browse the repository at this point in the history
Feat/calculate ip count when use batchregister
  • Loading branch information
chenhao26-nineteen authored Aug 3, 2022
1 parent de5ec35 commit 0b428e0
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ public long getLastUpdatedTime() {
@Override
public boolean addServiceInstance(Service service, InstancePublishInfo instancePublishInfo) {
if (null == publishers.put(service, instancePublishInfo)) {
MetricsMonitor.incrementInstanceCount();
if (instancePublishInfo instanceof BatchInstancePublishInfo) {
MetricsMonitor.incrementIpCountWithBatchRegister(instancePublishInfo);
} else {
MetricsMonitor.incrementInstanceCount();
}
}
NotifyCenter.publishEvent(new ClientEvent.ClientChangedEvent(this));
Loggers.SRV_LOG.info("Client change for service {}, {}", service, getClientId());
Expand All @@ -73,7 +77,11 @@ public boolean addServiceInstance(Service service, InstancePublishInfo instanceP
public InstancePublishInfo removeServiceInstance(Service service) {
InstancePublishInfo result = publishers.remove(service);
if (null != result) {
MetricsMonitor.decrementInstanceCount();
if (result instanceof BatchInstancePublishInfo) {
MetricsMonitor.decrementIpCountWithBatchRegister(result);
} else {
MetricsMonitor.decrementInstanceCount();
}
NotifyCenter.publishEvent(new ClientEvent.ClientChangedEvent(this));
}
Loggers.SRV_LOG.info("Client remove for service {}, {}", service, getClientId());
Expand Down Expand Up @@ -160,7 +168,15 @@ private static BatchInstanceData buildBatchInstanceData(BatchInstanceData batch

@Override
public void release() {
MetricsMonitor.getIpCountMonitor().addAndGet(-1 * publishers.size());
MetricsMonitor.getSubscriberCount().addAndGet(-1 * subscribers.size());
Collection<InstancePublishInfo> instancePublishInfos = publishers.values();
for (InstancePublishInfo instancePublishInfo : instancePublishInfos) {
if (instancePublishInfo instanceof BatchInstancePublishInfo) {
MetricsMonitor.decrementIpCountWithBatchRegister(instancePublishInfo);
} else {
MetricsMonitor.getIpCountMonitor().addAndGet(-1 * publishers.size());
}
}
MetricsMonitor.getIpCountMonitor().addAndGet(-1 * subscribers.size());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.alibaba.nacos.naming.monitor;

import com.alibaba.nacos.naming.core.v2.pojo.BatchInstancePublishInfo;
import com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo;
import com.alibaba.nacos.naming.misc.Loggers;
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.ImmutableTag;
Expand Down Expand Up @@ -176,6 +178,26 @@ public static Counter getLeaderSendBeatFailedException() {
return Metrics.counter("nacos_exception", "module", "naming", "name", "leaderSendBeatFailed");
}

/**
* increment IpCount when use batchRegister instance.
* @param instancePublishInfo must be BatchInstancePublishInfo
*/
public static void incrementIpCountWithBatchRegister(InstancePublishInfo instancePublishInfo) {
BatchInstancePublishInfo batchInstancePublishInfo = (BatchInstancePublishInfo) instancePublishInfo;
List<InstancePublishInfo> instancePublishInfos = batchInstancePublishInfo.getInstancePublishInfos();
getIpCountMonitor().addAndGet(instancePublishInfos.size());
}

/**
* decrement IpCount when use batchRegister instance.
* @param instancePublishInfo must be BatchInstancePublishInfo
*/
public static void decrementIpCountWithBatchRegister(InstancePublishInfo instancePublishInfo) {
BatchInstancePublishInfo batchInstancePublishInfo = (BatchInstancePublishInfo) instancePublishInfo;
List<InstancePublishInfo> instancePublishInfos = batchInstancePublishInfo.getInstancePublishInfos();
getIpCountMonitor().addAndGet(-1 * instancePublishInfos.size());
}

/**
* Reset all metrics.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.naming.core.v2.client;

import com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.pojo.Subscriber;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;

import java.util.Collection;

/**
* AbstractClient test.
* @ClassName: AbstractClientTest
* @Author: ChenHao26
* @Date: 2022/8/3 16:22
* @Description: TODO
*/
@RunWith(MockitoJUnitRunner.class)
public class AbstractClientTest {

private AbstractClient abstractClient;

private Service service;

private InstancePublishInfo instancePublishInfo;

private Subscriber subscriber;

@Before
public void setUp() {
abstractClient = new MockAbstractClient();
service = Service.newService("ns1", "group1", "serviceName001");
instancePublishInfo = new InstancePublishInfo("127.0.0.1", 8890);
subscriber = new Subscriber("127.0.0.1:8848", "agent1", "appName", "127.0.0.1",
"ns1", "serviceName001", 9090);
addServiceInstance();
addServiceSubscriber();
}

private void addServiceInstance() {
boolean result = abstractClient.addServiceInstance(service, instancePublishInfo);
Assert.assertTrue(result);
}

private void addServiceSubscriber() {
Assert.assertTrue(abstractClient.addServiceSubscriber(service, subscriber));
}

@Test
public void testGetLastUpdatedTime() {
Assert.assertNotNull(abstractClient.getLastUpdatedTime());
}

@Test
public void removeServiceInstanceSuccess() {
InstancePublishInfo publishInfo = abstractClient.removeServiceInstance(service);
Assert.assertNotNull(publishInfo);
}

@Test
public void getInstancePublishInfo() {
InstancePublishInfo publishInfo = abstractClient.getInstancePublishInfo(service);
Assert.assertNotNull(publishInfo);
}

@Test
public void getAllPublishedService() {
Collection<Service> allPublishedService = abstractClient.getAllPublishedService();
Assert.assertNotNull(allPublishedService);
}

@Test
public void removeServiceSubscriber() {
boolean result = abstractClient.removeServiceSubscriber(service);
Assert.assertTrue(result);
}

@Test
public void getSubscriber() {
Subscriber subscriber1 = abstractClient.getSubscriber(service);
Assert.assertNotNull(subscriber1);
}

@Test
public void getAllSubscribeService() {
Collection<Service> allSubscribeService = abstractClient.getAllSubscribeService();
Assert.assertNotNull(allSubscribeService);
}

@Test
public void generateSyncData() {
ClientSyncData clientSyncData = abstractClient.generateSyncData();
Assert.assertNotNull(clientSyncData);
}

@Test
public void release() {
try {
abstractClient.release();
} catch (Exception e) {
e.printStackTrace();
Assert.assertNotNull(e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.naming.core.v2.client;

/**
* Mock AbstractClient.
* @ClassName: MockAbstractClient
* @Author: ChenHao26
* @Date: 2022/8/3 16:25
* @Description: TODO
*/
public class MockAbstractClient extends AbstractClient {

@Override
public String getClientId() {
return "-1";
}

@Override
public boolean isEphemeral() {
return false;
}

@Override
public boolean isExpire(long currentTime) {
return false;
}
}

0 comments on commit 0b428e0

Please sign in to comment.