You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by su...@apache.org on 2023/01/16 16:01:11 UTC

[shardingsphere] branch master updated: Rename MethodTimeRecorder (#23594)

This is an automated email from the ASF dual-hosted git repository.

sunnianjun 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 10bdc9fa6da Rename MethodTimeRecorder (#23594)
10bdc9fa6da is described below

commit 10bdc9fa6da74a09d00e548a0709051c68eefbd5
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Tue Jan 17 00:01:01 2023 +0800

    Rename MethodTimeRecorder (#23594)
    
    * Rename MethodTimeRecorder
---
 .../{TimeRecorder.java => MethodTimeRecorder.java} | 39 ++++++++++++----------
 ...corderTest.java => MethodTimeRecorderTest.java} | 12 ++++---
 .../file/advice/MetaDataContextsFactoryAdvice.java |  8 +++--
 .../proxy/ExecuteLatencyHistogramAdvice.java       |  8 +++--
 4 files changed, 38 insertions(+), 29 deletions(-)

diff --git a/agent/plugins/core/src/main/java/org/apache/shardingsphere/agent/plugin/core/recorder/TimeRecorder.java b/agent/plugins/core/src/main/java/org/apache/shardingsphere/agent/plugin/core/recorder/MethodTimeRecorder.java
similarity index 56%
rename from agent/plugins/core/src/main/java/org/apache/shardingsphere/agent/plugin/core/recorder/TimeRecorder.java
rename to agent/plugins/core/src/main/java/org/apache/shardingsphere/agent/plugin/core/recorder/MethodTimeRecorder.java
index 2957fa745bc..2fc33dbfd67 100644
--- a/agent/plugins/core/src/main/java/org/apache/shardingsphere/agent/plugin/core/recorder/TimeRecorder.java
+++ b/agent/plugins/core/src/main/java/org/apache/shardingsphere/agent/plugin/core/recorder/MethodTimeRecorder.java
@@ -17,28 +17,30 @@
 
 package org.apache.shardingsphere.agent.plugin.core.recorder;
 
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
+import lombok.RequiredArgsConstructor;
+import org.apache.shardingsphere.agent.api.advice.AgentAdvice;
 
 import java.lang.reflect.Method;
 import java.util.HashMap;
 import java.util.Map;
 
 /**
- * Time recorder.
+ * Method time recorder.
  */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class TimeRecorder {
+@RequiredArgsConstructor
+public final class MethodTimeRecorder {
     
-    private static final ThreadLocal<Map<Method, Long>> CURRENT_RECORDER = ThreadLocal.withInitial(HashMap::new);
+    private static final ThreadLocal<Map<String, Long>> CURRENT_RECORDER = ThreadLocal.withInitial(HashMap::new);
+    
+    private final Class<? extends AgentAdvice> adviceClass;
     
     /**
      * Record now.
-     *
+     * 
      * @param method method to be recorded
      */
-    public static void record(final Method method) {
-        CURRENT_RECORDER.get().put(method, System.currentTimeMillis());
+    public void record(final Method method) {
+        CURRENT_RECORDER.get().put(getKey(method), System.currentTimeMillis());
     }
     
     /**
@@ -47,23 +49,24 @@ public final class TimeRecorder {
      * @param method method to be recorded
      * @return elapsed time
      */
-    public static long getElapsedTimeAndClean(final Method method) {
+    public long getElapsedTimeAndClean(final Method method) {
+        String key = getKey(method);
         try {
-            return getElapsedTime(method);
+            return getElapsedTime(key);
         } finally {
-            clean(method);
+            clean(key);
         }
     }
     
-    private static long getElapsedTime(final Method method) {
-        return isRecorded(method) ? System.currentTimeMillis() - CURRENT_RECORDER.get().get(method) : 0L;
+    private String getKey(final Method method) {
+        return String.format("%s@%s", adviceClass.getCanonicalName(), method.getName());
     }
     
-    private static boolean isRecorded(final Method method) {
-        return null != CURRENT_RECORDER.get().get(method);
+    private long getElapsedTime(final String key) {
+        return CURRENT_RECORDER.get().containsKey(key) ? System.currentTimeMillis() - CURRENT_RECORDER.get().get(key) : 0L;
     }
     
-    private static void clean(final Method method) {
-        CURRENT_RECORDER.get().remove(method);
+    private void clean(final String key) {
+        CURRENT_RECORDER.get().remove(key);
     }
 }
diff --git a/agent/plugins/core/src/test/java/org/apache/shardingsphere/agent/plugin/core/util/TimeRecorderTest.java b/agent/plugins/core/src/test/java/org/apache/shardingsphere/agent/plugin/core/util/MethodTimeRecorderTest.java
similarity index 67%
rename from agent/plugins/core/src/test/java/org/apache/shardingsphere/agent/plugin/core/util/TimeRecorderTest.java
rename to agent/plugins/core/src/test/java/org/apache/shardingsphere/agent/plugin/core/util/MethodTimeRecorderTest.java
index fd533552b49..94efd428945 100644
--- a/agent/plugins/core/src/test/java/org/apache/shardingsphere/agent/plugin/core/util/TimeRecorderTest.java
+++ b/agent/plugins/core/src/test/java/org/apache/shardingsphere/agent/plugin/core/util/MethodTimeRecorderTest.java
@@ -17,24 +17,26 @@
 
 package org.apache.shardingsphere.agent.plugin.core.util;
 
-import org.apache.shardingsphere.agent.plugin.core.recorder.TimeRecorder;
+import org.apache.shardingsphere.agent.api.advice.AgentAdvice;
+import org.apache.shardingsphere.agent.plugin.core.recorder.MethodTimeRecorder;
 import org.junit.Test;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.greaterThanOrEqualTo;
 
-public final class TimeRecorderTest {
+public final class MethodTimeRecorderTest {
     
     @Test
     public void assertGetElapsedTimeAndCleanWithRecorded() throws InterruptedException, NoSuchMethodException {
-        TimeRecorder.record(Object.class.getDeclaredMethod("toString"));
+        MethodTimeRecorder methodTimeRecorder = new MethodTimeRecorder(AgentAdvice.class);
+        methodTimeRecorder.record(Object.class.getDeclaredMethod("toString"));
         Thread.sleep(5L);
-        assertThat(TimeRecorder.getElapsedTimeAndClean(Object.class.getDeclaredMethod("toString")), greaterThanOrEqualTo(5L));
+        assertThat(methodTimeRecorder.getElapsedTimeAndClean(Object.class.getDeclaredMethod("toString")), greaterThanOrEqualTo(5L));
     }
     
     @Test
     public void assertGetElapsedTimeAndCleanWithoutRecorded() throws NoSuchMethodException {
-        assertThat(TimeRecorder.getElapsedTimeAndClean(Object.class.getDeclaredMethod("toString")), is(0L));
+        assertThat(new MethodTimeRecorder(AgentAdvice.class).getElapsedTimeAndClean(Object.class.getDeclaredMethod("toString")), is(0L));
     }
 }
diff --git a/agent/plugins/logging/type/file/src/main/java/org/apache/shardingsphere/agent/plugin/logging/file/advice/MetaDataContextsFactoryAdvice.java b/agent/plugins/logging/type/file/src/main/java/org/apache/shardingsphere/agent/plugin/logging/file/advice/MetaDataContextsFactoryAdvice.java
index 89c51c730d4..da0004eabbd 100644
--- a/agent/plugins/logging/type/file/src/main/java/org/apache/shardingsphere/agent/plugin/logging/file/advice/MetaDataContextsFactoryAdvice.java
+++ b/agent/plugins/logging/type/file/src/main/java/org/apache/shardingsphere/agent/plugin/logging/file/advice/MetaDataContextsFactoryAdvice.java
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.agent.plugin.logging.file.advice;
 
 import lombok.extern.slf4j.Slf4j;
 import org.apache.shardingsphere.agent.api.advice.type.StaticMethodAdvice;
-import org.apache.shardingsphere.agent.plugin.core.recorder.TimeRecorder;
+import org.apache.shardingsphere.agent.plugin.core.recorder.MethodTimeRecorder;
 
 import java.lang.reflect.Method;
 
@@ -29,13 +29,15 @@ import java.lang.reflect.Method;
 @Slf4j
 public final class MetaDataContextsFactoryAdvice implements StaticMethodAdvice {
     
+    private final MethodTimeRecorder methodTimeRecorder = new MethodTimeRecorder(MetaDataContextsFactoryAdvice.class);
+    
     @Override
     public void beforeMethod(final Class<?> clazz, final Method method, final Object[] args) {
-        TimeRecorder.record(method);
+        methodTimeRecorder.record(method);
     }
     
     @Override
     public void afterMethod(final Class<?> clazz, final Method method, final Object[] args, final Object result) {
-        log.info("Build meta data contexts finished, cost {} milliseconds.", TimeRecorder.getElapsedTimeAndClean(method));
+        log.info("Build meta data contexts finished, cost {} milliseconds.", methodTimeRecorder.getElapsedTimeAndClean(method));
     }
 }
diff --git a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/ExecuteLatencyHistogramAdvice.java b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/ExecuteLatencyHistogramAdvice.java
index 85843d3962c..e7cf0de8e8d 100644
--- a/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/ExecuteLatencyHistogramAdvice.java
+++ b/agent/plugins/metrics/core/src/main/java/org/apache/shardingsphere/agent/plugin/metrics/core/advice/proxy/ExecuteLatencyHistogramAdvice.java
@@ -19,7 +19,7 @@ package org.apache.shardingsphere.agent.plugin.metrics.core.advice.proxy;
 
 import org.apache.shardingsphere.agent.api.advice.TargetAdviceObject;
 import org.apache.shardingsphere.agent.api.advice.type.InstanceMethodAdvice;
-import org.apache.shardingsphere.agent.plugin.core.recorder.TimeRecorder;
+import org.apache.shardingsphere.agent.plugin.core.recorder.MethodTimeRecorder;
 import org.apache.shardingsphere.agent.plugin.metrics.core.MetricsWrapperRegistry;
 
 import java.lang.reflect.Method;
@@ -31,13 +31,15 @@ public final class ExecuteLatencyHistogramAdvice implements InstanceMethodAdvice
     
     private static final String PROXY_EXECUTE_LATENCY_MILLIS_METRIC_KEY = "proxy_execute_latency_millis";
     
+    private final MethodTimeRecorder methodTimeRecorder = new MethodTimeRecorder(ExecuteLatencyHistogramAdvice.class);
+    
     @Override
     public void beforeMethod(final TargetAdviceObject target, final Method method, final Object[] args) {
-        TimeRecorder.record(method);
+        methodTimeRecorder.record(method);
     }
     
     @Override
     public void afterMethod(final TargetAdviceObject target, final Method method, final Object[] args, final Object result) {
-        MetricsWrapperRegistry.get(PROXY_EXECUTE_LATENCY_MILLIS_METRIC_KEY).observe(TimeRecorder.getElapsedTimeAndClean(method));
+        MetricsWrapperRegistry.get(PROXY_EXECUTE_LATENCY_MILLIS_METRIC_KEY).observe(methodTimeRecorder.getElapsedTimeAndClean(method));
     }
 }