Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add some metrics and logs #4649

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.Random;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -73,6 +74,9 @@ public class ServerListManager implements ServerListFactory, Closeable {

public ServerListManager(Properties properties) {
initServerAddr(properties);
if (!serverList.isEmpty()) {
currentIndex.set(new Random().nextInt(serverList.size()));
}
}

private void initServerAddr(Properties properties) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@ public Set<String> getAllNamespaces() {
* @return removed service
*/
public Service removeSingleton(Service service) {
namespaceSingletonMaps.get(service.getNamespace()).remove(service);
if (namespaceSingletonMaps.containsKey(service.getNamespace())) {
namespaceSingletonMaps.get(service.getNamespace()).remove(service);
}
return singletonRepository.remove(service);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import com.alibaba.nacos.naming.core.v2.event.client.ClientEvent;
import com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.monitor.MetricsMonitor;
import com.alibaba.nacos.naming.pojo.Subscriber;

import java.util.Collection;
Expand Down Expand Up @@ -57,15 +59,22 @@ public long getLastUpdatedTime() {

@Override
public boolean addServiceInstance(Service service, InstancePublishInfo instancePublishInfo) {
publishers.put(service, instancePublishInfo);
if (null == publishers.put(service, instancePublishInfo)) {
MetricsMonitor.getIpCountMonitor().incrementAndGet();
}
NotifyCenter.publishEvent(new ClientEvent.ClientChangedEvent(this));
Loggers.SRV_LOG.info("Client change for service {}, {}", service, getClientId());
return true;
}

@Override
public InstancePublishInfo removeServiceInstance(Service service) {
InstancePublishInfo result = publishers.remove(service);
if (null != result) {
MetricsMonitor.getIpCountMonitor().decrementAndGet();
}
NotifyCenter.publishEvent(new ClientEvent.ClientChangedEvent(this));
Loggers.SRV_LOG.info("Client remove for service {}, {}", service, getClientId());
return result;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.alibaba.nacos.consistency.entity.WriteRequest;
import com.alibaba.nacos.consistency.snapshot.SnapshotOperation;
import com.alibaba.nacos.core.distributed.ProtocolManager;
import com.alibaba.nacos.core.utils.Loggers;
import com.alibaba.nacos.naming.core.v2.ServiceManager;
import com.alibaba.nacos.naming.core.v2.pojo.Service;
import com.alibaba.nacos.naming.utils.Constants;
Expand Down Expand Up @@ -94,6 +95,7 @@ public Response onApply(WriteRequest request) {
}
return Response.newBuilder().setSuccess(true).build();
} catch (Exception e) {
Loggers.RAFT.error("apply service metadata error: ", e);
return Response.newBuilder().setSuccess(false).setErrMsg(e.getMessage()).build();
} finally {
readLock.unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ public static NamingExecuteTaskDispatcher getInstance() {
public void dispatchAndExecuteTask(Object dispatchTag, AbstractExecuteTask task) {
executeEngine.addTask(dispatchTag, task);
}

public String workersStatus() {
return executeEngine.workersStatus();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,14 @@ public static void incrementFailPush() {
INSTANCE.failedPush.incrementAndGet();
}

public static void incrementInstanceCount() {
INSTANCE.ipCount.incrementAndGet();
}

public static void decrementInstanceCount() {
INSTANCE.ipCount.decrementAndGet();
}

public static Counter getDiskException() {
return Metrics.counter("nacos_exception", "module", "naming", "name", "disk");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.alibaba.nacos.naming.core.ServiceManager;
import com.alibaba.nacos.naming.misc.GlobalExecutor;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.NamingExecuteTaskDispatcher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
Expand All @@ -47,7 +48,7 @@ public class PerformanceLoggerThread {
@Autowired
private ClusterVersionJudgement versionJudgement;

private static final long PERIOD = 5 * 60;
private static final long PERIOD = 1 * 60;

@PostConstruct
public void init() {
Expand All @@ -72,8 +73,9 @@ public void refreshMetrics() {
*/
@Scheduled(cron = "0/15 * * * * ?")
public void collectMetrics() {
MetricsMonitor.getDomCountMonitor().set(serviceManager.getServiceCount());
MetricsMonitor.getIpCountMonitor().set(serviceManager.getInstanceCount());
// MetricsMonitor.getDomCountMonitor().set(serviceManager.getServiceCount());
// MetricsMonitor.getIpCountMonitor().set(serviceManager.getInstanceCount());
MetricsMonitor.getDomCountMonitor().set(com.alibaba.nacos.naming.core.v2.ServiceManager.getInstance().size());
MetricsMonitor.getAvgPushCostMonitor().set(getAvgPushCost());
metricsRaftLeader();
}
Expand All @@ -96,18 +98,28 @@ private void metricsRaftLeader() {

class PerformanceLogTask implements Runnable {

private int logCount = 0;

@Override
public void run() {
try {
int serviceCount = serviceManager.getServiceCount();
int ipCount = serviceManager.getInstanceCount();
logCount %= 10;
if (logCount == 0) {
Loggers.PERFORMANCE_LOG
.info("PERFORMANCE:|serviceCount|ipCount|maxPushCost|avgPushCost|totalPushCount|failPushCount");
}
int serviceCount = com.alibaba.nacos.naming.core.v2.ServiceManager.getInstance().size();
int ipCount = MetricsMonitor.getIpCountMonitor().get();
long maxPushCost = MetricsMonitor.getMaxPushCostMonitor().get();
long avgPushCost = getAvgPushCost();
long totalPushCount = MetricsMonitor.getTotalPushMonitor().longValue();
long failPushCount = MetricsMonitor.getFailedPushMonitor().longValue();
Loggers.PERFORMANCE_LOG
.info("PERFORMANCE:|{}|{}|{}|{}|{}|{}", serviceCount, ipCount, maxPushCost, avgPushCost,
totalPushCount, failPushCount);
Loggers.PERFORMANCE_LOG
.info("Task worker status: \n" + NamingExecuteTaskDispatcher.getInstance().workersStatus());
logCount++;
MetricsMonitor.getTotalPushCountForAvg().set(0);
MetricsMonitor.getTotalPushCostForAvg().set(0);
MetricsMonitor.getMaxPushCostMonitor().set(-1);
Expand Down