You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by ji...@apache.org on 2022/12/17 16:22:11 UTC
[shardingsphere] branch master updated: Refactor TimeRecorder (#22942)
This is an automated email from the ASF dual-hosted git repository.
jianglongtao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 052a35b583c Refactor TimeRecorder (#22942)
052a35b583c is described below
commit 052a35b583cc6a25ca0f3c3ccdbef6eb8542b701
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Dec 18 00:22:05 2022 +0800
Refactor TimeRecorder (#22942)
---
.../agent/core/util/TimeRecorder.java} | 16 +++----
.../base/advice/MetaDataContextsFactoryAdvice.java | 7 +--
.../api/advice/CommandExecutorTaskAdvice.java | 11 ++---
.../api/threadlocal/ElapsedTimeThreadLocal.java | 56 ----------------------
4 files changed, 16 insertions(+), 74 deletions(-)
diff --git a/agent/plugins/logging/base/src/main/java/org/apache/shardingsphere/agent/plugin/logging/base/advice/LogTimeRecorder.java b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/util/TimeRecorder.java
similarity index 81%
rename from agent/plugins/logging/base/src/main/java/org/apache/shardingsphere/agent/plugin/logging/base/advice/LogTimeRecorder.java
rename to agent/core/src/main/java/org/apache/shardingsphere/agent/core/util/TimeRecorder.java
index 538531a893f..a425d12b9b0 100644
--- a/agent/plugins/logging/base/src/main/java/org/apache/shardingsphere/agent/plugin/logging/base/advice/LogTimeRecorder.java
+++ b/agent/core/src/main/java/org/apache/shardingsphere/agent/core/util/TimeRecorder.java
@@ -15,24 +15,22 @@
* limitations under the License.
*/
-package org.apache.shardingsphere.agent.plugin.logging.base.advice;
+package org.apache.shardingsphere.agent.core.util;
/**
- * Log time recorder.
+ * Time recorder.
*/
-public enum LogTimeRecorder {
+public enum TimeRecorder {
INSTANCE;
private static final ThreadLocal<Long> CURRENT_RECORDER = new ThreadLocal<>();
/**
- * Record time.
- *
- * @param time time
+ * Record now.
*/
- public void record(final long time) {
- CURRENT_RECORDER.set(time);
+ public void record() {
+ CURRENT_RECORDER.set(System.currentTimeMillis());
}
/**
@@ -47,7 +45,7 @@ public enum LogTimeRecorder {
/**
* Clean recorded time.
*/
- public void cleanRecordedTime() {
+ public void clean() {
CURRENT_RECORDER.remove();
}
}
diff --git a/agent/plugins/logging/base/src/main/java/org/apache/shardingsphere/agent/plugin/logging/base/advice/MetaDataContextsFactoryAdvice.java b/agent/plugins/logging/base/src/main/java/org/apache/shardingsphere/agent/plugin/logging/base/advice/MetaDataContextsFactoryAdvice.java
index 4e85744ef87..d779b409152 100644
--- a/agent/plugins/logging/base/src/main/java/org/apache/shardingsphere/agent/plugin/logging/base/advice/MetaDataContextsFactoryAdvice.java
+++ b/agent/plugins/logging/base/src/main/java/org/apache/shardingsphere/agent/plugin/logging/base/advice/MetaDataContextsFactoryAdvice.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.agent.plugin.logging.base.advice;
import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.agent.advice.type.StaticMethodAdvice;
import org.apache.shardingsphere.agent.advice.MethodInvocationResult;
+import org.apache.shardingsphere.agent.core.util.TimeRecorder;
import java.lang.reflect.Method;
@@ -31,12 +32,12 @@ public final class MetaDataContextsFactoryAdvice implements StaticMethodAdvice {
@Override
public void beforeMethod(final Class<?> clazz, final Method method, final Object[] args, final MethodInvocationResult result) {
- LogTimeRecorder.INSTANCE.record(System.currentTimeMillis());
+ TimeRecorder.INSTANCE.record();
}
@Override
public void afterMethod(final Class<?> clazz, final Method method, final Object[] args, final MethodInvocationResult result) {
- log.info("Build meta data contexts finished, cost {} milliseconds.", LogTimeRecorder.INSTANCE.getElapsedTime());
- LogTimeRecorder.INSTANCE.cleanRecordedTime();
+ log.info("Build meta data contexts finished, cost {} milliseconds.", TimeRecorder.INSTANCE.getElapsedTime());
+ TimeRecorder.INSTANCE.clean();
}
}
diff --git a/agent/plugins/metrics/api/src/main/java/org/apache/shardingsphere/agent/metrics/api/advice/CommandExecutorTaskAdvice.java b/agent/plugins/metrics/api/src/main/java/org/apache/shardingsphere/agent/metrics/api/advice/CommandExecutorTaskAdvice.java
index 4891a429e4c..a6773cffe56 100644
--- a/agent/plugins/metrics/api/src/main/java/org/apache/shardingsphere/agent/metrics/api/advice/CommandExecutorTaskAdvice.java
+++ b/agent/plugins/metrics/api/src/main/java/org/apache/shardingsphere/agent/metrics/api/advice/CommandExecutorTaskAdvice.java
@@ -17,13 +17,13 @@
package org.apache.shardingsphere.agent.metrics.api.advice;
+import org.apache.shardingsphere.agent.advice.MethodInvocationResult;
import org.apache.shardingsphere.agent.advice.TargetAdviceObject;
import org.apache.shardingsphere.agent.advice.type.InstanceMethodAdvice;
-import org.apache.shardingsphere.agent.advice.MethodInvocationResult;
+import org.apache.shardingsphere.agent.core.util.TimeRecorder;
import org.apache.shardingsphere.agent.metrics.api.MetricsPool;
import org.apache.shardingsphere.agent.metrics.api.MetricsWrapper;
import org.apache.shardingsphere.agent.metrics.api.constant.MetricIds;
-import org.apache.shardingsphere.agent.metrics.api.threadlocal.ElapsedTimeThreadLocal;
import java.lang.reflect.Method;
@@ -44,7 +44,7 @@ public final class CommandExecutorTaskAdvice implements InstanceMethodAdvice {
@Override
public void beforeMethod(final TargetAdviceObject target, final Method method, final Object[] args, final MethodInvocationResult result) {
if (COMMAND_EXECUTOR_RUN.equals(method.getName())) {
- ElapsedTimeThreadLocal.INSTANCE.set(System.currentTimeMillis());
+ TimeRecorder.INSTANCE.record();
}
}
@@ -52,10 +52,9 @@ public final class CommandExecutorTaskAdvice implements InstanceMethodAdvice {
public void afterMethod(final TargetAdviceObject target, final Method method, final Object[] args, final MethodInvocationResult result) {
if (COMMAND_EXECUTOR_RUN.equals(method.getName())) {
try {
- long elapsedTime = System.currentTimeMillis() - ElapsedTimeThreadLocal.INSTANCE.get();
- MetricsPool.get(MetricIds.PROXY_EXECUTE_LATENCY_MILLIS).ifPresent(optional -> optional.observe(elapsedTime));
+ MetricsPool.get(MetricIds.PROXY_EXECUTE_LATENCY_MILLIS).ifPresent(optional -> optional.observe(TimeRecorder.INSTANCE.getElapsedTime()));
} finally {
- ElapsedTimeThreadLocal.INSTANCE.remove();
+ TimeRecorder.INSTANCE.clean();
}
} else if (COMMAND_EXECUTOR_EXCEPTION.equals(method.getName())) {
MetricsPool.get(MetricIds.PROXY_EXECUTE_ERROR).ifPresent(MetricsWrapper::inc);
diff --git a/agent/plugins/metrics/api/src/main/java/org/apache/shardingsphere/agent/metrics/api/threadlocal/ElapsedTimeThreadLocal.java b/agent/plugins/metrics/api/src/main/java/org/apache/shardingsphere/agent/metrics/api/threadlocal/ElapsedTimeThreadLocal.java
deleted file mode 100644
index 58c217669ef..00000000000
--- a/agent/plugins/metrics/api/src/main/java/org/apache/shardingsphere/agent/metrics/api/threadlocal/ElapsedTimeThreadLocal.java
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You 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 org.apache.shardingsphere.agent.metrics.api.threadlocal;
-
-/**
- * The enum Elapsed time thread local.
- */
-public enum ElapsedTimeThreadLocal {
-
- /**
- * Instance elapsed time thread local.
- */
- INSTANCE;
-
- private static final ThreadLocal<Long> CURRENT_LOCAL = new ThreadLocal<>();
-
- /**
- * Set.
- *
- * @param time the time
- */
- public void set(final long time) {
- CURRENT_LOCAL.set(time);
- }
-
- /**
- * Get long.
- *
- * @return the long
- */
- public Long get() {
- return CURRENT_LOCAL.get();
- }
-
- /**
- * Remove.
- */
- public void remove() {
- CURRENT_LOCAL.remove();
- }
-}