diff --git a/core/src/main/java/com/alibaba/nacos/core/monitor/NacosMeterRegistryCenter.java b/core/src/main/java/com/alibaba/nacos/core/monitor/NacosMeterRegistryCenter.java index 889630c4a3c..7618d73a96b 100644 --- a/core/src/main/java/com/alibaba/nacos/core/monitor/NacosMeterRegistryCenter.java +++ b/core/src/main/java/com/alibaba/nacos/core/monitor/NacosMeterRegistryCenter.java @@ -49,7 +49,9 @@ public final class NacosMeterRegistryCenter { // control plugin registeres. public static final String CONTROL_DENIED_REGISTRY = "CONTROL_DENIED_REGISTRY"; - + + public static final String LOCK_STABLE_REGISTRY = "LOCK_STABLE_REGISTRY"; + private static final ConcurrentHashMap METER_REGISTRIES = new ConcurrentHashMap<>(); private static CompositeMeterRegistry METER_REGISTRY = null; @@ -61,7 +63,7 @@ public final class NacosMeterRegistryCenter { Loggers.CORE.warn("Metrics init failed :", t); } registry(CORE_STABLE_REGISTRY, CONFIG_STABLE_REGISTRY, NAMING_STABLE_REGISTRY, TOPN_CONFIG_CHANGE_REGISTRY, - TOPN_SERVICE_CHANGE_REGISTRY, CONTROL_DENIED_REGISTRY); + TOPN_SERVICE_CHANGE_REGISTRY, CONTROL_DENIED_REGISTRY, LOCK_STABLE_REGISTRY); } diff --git a/lock/src/main/java/com/alibaba/nacos/lock/aspect/RequestLockAspect.java b/lock/src/main/java/com/alibaba/nacos/lock/aspect/RequestLockAspect.java new file mode 100644 index 00000000000..9d9a09d72e4 --- /dev/null +++ b/lock/src/main/java/com/alibaba/nacos/lock/aspect/RequestLockAspect.java @@ -0,0 +1,59 @@ +/* + * Copyright 1999-2023 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.lock.aspect; + +import com.alibaba.nacos.api.lock.remote.request.LockOperationRequest; +import com.alibaba.nacos.api.lock.remote.response.LockOperationResponse; +import com.alibaba.nacos.api.remote.request.RequestMeta; +import com.alibaba.nacos.lock.monitor.LockMetricsMonitor; +import org.aspectj.lang.ProceedingJoinPoint; +import org.aspectj.lang.annotation.Around; +import org.aspectj.lang.annotation.Aspect; +import org.springframework.stereotype.Component; + +import java.util.concurrent.TimeUnit; + +/** + * RequestLockAspect. + * @author goumang.zh@alibaba-inc.com + */ +@Aspect +@Component +public class RequestLockAspect { + + + /** + * count metrics and get handler time. + */ + @SuppressWarnings("checkstyle:linelength") + @Around(value = "execution(* com.alibaba.nacos.core.remote.RequestHandler.handleRequest(..)) && target(com.alibaba.nacos.lock.remote.rpc.handler.LockRequestHandler) && args(request, meta)", argNames = "pjp,request,meta") + public Object lockMeterPoint(ProceedingJoinPoint pjp, LockOperationRequest request, RequestMeta meta) + throws Throwable { + long st = System.currentTimeMillis(); + try { + LockMetricsMonitor.getTotalMeter(request.getLockOperationEnum()).incrementAndGet(); + LockOperationResponse result = (LockOperationResponse) pjp.proceed(); + if (result.isSuccess()) { + LockMetricsMonitor.getSuccessMeter(request.getLockOperationEnum()).incrementAndGet(); + } + return result; + } finally { + long rt = System.currentTimeMillis() - st; + LockMetricsMonitor.getLockHandlerTimer().record(rt, TimeUnit.MILLISECONDS); + } + } +} diff --git a/lock/src/main/java/com/alibaba/nacos/lock/monitor/LockMemoryMonitor.java b/lock/src/main/java/com/alibaba/nacos/lock/monitor/LockMemoryMonitor.java new file mode 100644 index 00000000000..64e4587548a --- /dev/null +++ b/lock/src/main/java/com/alibaba/nacos/lock/monitor/LockMemoryMonitor.java @@ -0,0 +1,40 @@ +/* + * Copyright 1999-2023 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.lock.monitor; + +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Service; + +/** + * memory monitor. + * + * @author goumang.zh@alibaba-inc.com + */ +@Service +public class LockMemoryMonitor { + + /** + * auto clean metrics per-day. + */ + @Scheduled(cron = "0 0 0 * * ?") + public void clear() { + LockMetricsMonitor.getGrpcLockTotal().set(0); + LockMetricsMonitor.getGrpcLockSuccess().set(0); + LockMetricsMonitor.getGrpcUnLockTotal().set(0); + LockMetricsMonitor.getGrpcUnLockSuccess().set(0); + } +} diff --git a/lock/src/main/java/com/alibaba/nacos/lock/monitor/LockMetricsMonitor.java b/lock/src/main/java/com/alibaba/nacos/lock/monitor/LockMetricsMonitor.java new file mode 100644 index 00000000000..507b2802e05 --- /dev/null +++ b/lock/src/main/java/com/alibaba/nacos/lock/monitor/LockMetricsMonitor.java @@ -0,0 +1,111 @@ +/* + * Copyright 1999-2023 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.lock.monitor; + +import com.alibaba.nacos.api.lock.remote.LockOperationEnum; +import com.alibaba.nacos.core.monitor.NacosMeterRegistryCenter; +import io.micrometer.core.instrument.ImmutableTag; +import io.micrometer.core.instrument.Tag; +import io.micrometer.core.instrument.Timer; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +/** + * MetricsMonitor. + * @author goumang.zh@alibaba-inc.com + */ +public class LockMetricsMonitor { + + private static final String METER_REGISTRY = NacosMeterRegistryCenter.LOCK_STABLE_REGISTRY; + + private static AtomicInteger grpcLockSuccess = new AtomicInteger(); + + private static AtomicInteger grpcUnLockSuccess = new AtomicInteger(); + + private static AtomicInteger grpcLockTotal = new AtomicInteger(); + + private static AtomicInteger grpcUnLockTotal = new AtomicInteger(); + + private static AtomicInteger aliveLockCount = new AtomicInteger(); + + static { + ImmutableTag immutableTag = new ImmutableTag("module", "lock"); + List tags = new ArrayList<>(); + tags.add(immutableTag); + tags.add(new ImmutableTag("name", "grpcLockTotal")); + NacosMeterRegistryCenter.gauge(METER_REGISTRY, "nacos_monitor", tags, grpcLockTotal); + + tags = new ArrayList<>(); + tags.add(immutableTag); + tags.add(new ImmutableTag("name", "grpcLockSuccess")); + NacosMeterRegistryCenter.gauge(METER_REGISTRY, "nacos_monitor", tags, grpcLockSuccess); + + tags = new ArrayList<>(); + tags.add(immutableTag); + tags.add(new ImmutableTag("name", "grpcUnLockTotal")); + NacosMeterRegistryCenter.gauge(METER_REGISTRY, "nacos_monitor", tags, grpcUnLockTotal); + + tags = new ArrayList<>(); + tags.add(immutableTag); + tags.add(new ImmutableTag("name", "grpcUnLockSuccess")); + NacosMeterRegistryCenter.gauge(METER_REGISTRY, "nacos_monitor", tags, grpcUnLockSuccess); + + tags = new ArrayList<>(); + tags.add(immutableTag); + tags.add(new ImmutableTag("name", "aliveLockCount")); + NacosMeterRegistryCenter.gauge(METER_REGISTRY, "nacos_monitor", tags, aliveLockCount); + } + + public static AtomicInteger getGrpcLockSuccess() { + return grpcLockSuccess; + } + + public static AtomicInteger getGrpcUnLockSuccess() { + return grpcUnLockSuccess; + } + + public static AtomicInteger getGrpcLockTotal() { + return grpcLockTotal; + } + + public static AtomicInteger getGrpcUnLockTotal() { + return grpcUnLockTotal; + } + + public static Timer getLockHandlerTimer() { + return NacosMeterRegistryCenter + .timer(METER_REGISTRY, "nacos_timer", "module", "lock", "name", "lockHandlerRt"); + } + + public static AtomicInteger getSuccessMeter(LockOperationEnum lockOperationEnum) { + if (lockOperationEnum == LockOperationEnum.ACQUIRE) { + return grpcLockSuccess; + } else { + return grpcUnLockSuccess; + } + } + + public static AtomicInteger getTotalMeter(LockOperationEnum lockOperationEnum) { + if (lockOperationEnum == LockOperationEnum.ACQUIRE) { + return grpcLockTotal; + } else { + return grpcUnLockTotal; + } + } +} diff --git a/lock/src/main/java/com/alibaba/nacos/lock/service/impl/LockOperationServiceImpl.java b/lock/src/main/java/com/alibaba/nacos/lock/service/impl/LockOperationServiceImpl.java index 932c10e3d03..4b8f1b182d4 100644 --- a/lock/src/main/java/com/alibaba/nacos/lock/service/impl/LockOperationServiceImpl.java +++ b/lock/src/main/java/com/alibaba/nacos/lock/service/impl/LockOperationServiceImpl.java @@ -156,6 +156,7 @@ public Boolean lock(LockInstance lockInstance) { lockInstance.getLockType(), paramSize, e.getMessage()); throw e; } catch (Exception e) { + LOGGER.error("lock fail.", e); throw new NacosLockException("tryLock error.", e); } }