From 04638c51776dfdc1f48f4b59ed976996beecaa26 Mon Sep 17 00:00:00 2001 From: wusheng Date: Sun, 19 Feb 2017 08:24:58 +0800 Subject: [PATCH] Rename and refactor SimapleObjectFirstInvokeInterceptor to NoCocurrencyAceessObject --- .../assist/NoCocurrencyAceessObject.java | 46 +++++++++++++++++++ .../SimpleObjectFirstInvokeInterceptor.java | 42 ----------------- .../java/test/a/eye/cloud/api/TimeTest.java | 25 ---------- .../jedis/v2/JedisMethodInterceptor.java | 41 +++++++++-------- 4 files changed, 68 insertions(+), 86 deletions(-) create mode 100644 skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/plugin/interceptor/assist/NoCocurrencyAceessObject.java delete mode 100644 skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/plugin/interceptor/assist/SimpleObjectFirstInvokeInterceptor.java delete mode 100644 skywalking-sniffer/skywalking-api/src/test/java/test/a/eye/cloud/api/TimeTest.java diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/plugin/interceptor/assist/NoCocurrencyAceessObject.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/plugin/interceptor/assist/NoCocurrencyAceessObject.java new file mode 100644 index 0000000000..bfaedb9938 --- /dev/null +++ b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/plugin/interceptor/assist/NoCocurrencyAceessObject.java @@ -0,0 +1,46 @@ +package com.a.eye.skywalking.plugin.interceptor.assist; + +import com.a.eye.skywalking.plugin.interceptor.EnhancedClassInstanceContext; +import com.a.eye.skywalking.plugin.interceptor.InterceptorException; +import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; + +/** + * {@link NoCocurrencyAceessObject} is an abstract class, + * works for class's methods call each others, which these methods should be intercepted. + * + * At this scenario, only the first access should be intercepted. + * + * @author wusheng + */ +public abstract class NoCocurrencyAceessObject implements InstanceMethodsAroundInterceptor { + protected String invokeCounterKey = "__$invokeCounterKey"; + + protected Object invokeCounterInstLock = new Object(); + + public void whenEnter(EnhancedClassInstanceContext context, Runnable runnable) { + if (!context.isContain(invokeCounterKey)) { + synchronized (invokeCounterInstLock) { + if (!context.isContain(invokeCounterKey)) { + context.set(invokeCounterKey, 0); + } + } + } + int counter = context.get(invokeCounterKey, + Integer.class); + if(++counter == 1){ + runnable.run(); + } + } + + public void whenExist(EnhancedClassInstanceContext context, Runnable runnable) { + if (!context.isContain(invokeCounterKey)) { + throw new InterceptorException( + "key=invokeCounterKey not found is context. unexpected situation."); + } + int counter = context.get(invokeCounterKey, + Integer.class); + if(--counter == 0){ + runnable.run(); + } + } +} diff --git a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/plugin/interceptor/assist/SimpleObjectFirstInvokeInterceptor.java b/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/plugin/interceptor/assist/SimpleObjectFirstInvokeInterceptor.java deleted file mode 100644 index 3a69477179..0000000000 --- a/skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/plugin/interceptor/assist/SimpleObjectFirstInvokeInterceptor.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.a.eye.skywalking.plugin.interceptor.assist; - -import java.util.concurrent.atomic.AtomicInteger; - -import com.a.eye.skywalking.plugin.interceptor.EnhancedClassInstanceContext; -import com.a.eye.skywalking.plugin.interceptor.InterceptorException; -import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor; - -/** - * 用于首次拦截方法调用,避免方法内部的方法调用被多次拦截。 - * - * @author wusheng - * - */ -public abstract class SimpleObjectFirstInvokeInterceptor implements InstanceMethodsAroundInterceptor { - protected String invokeCounterKey = "__$invokeCounterKey"; - - protected Object invokeCounterInstLock = new Object(); - - public boolean isFirstBeforeMethod(EnhancedClassInstanceContext context) { - if (!context.isContain(invokeCounterKey)) { - synchronized (invokeCounterInstLock) { - if (!context.isContain(invokeCounterKey)) { - context.set(invokeCounterKey, new AtomicInteger(0)); - } - } - } - AtomicInteger counter = context.get(invokeCounterKey, - AtomicInteger.class); - return counter.incrementAndGet() == 1; - } - - public boolean isLastAfterMethod(EnhancedClassInstanceContext context) { - if (!context.isContain(invokeCounterKey)) { - throw new InterceptorException( - "key=invokeCounterKey not found is context. unexpected failue."); - } - AtomicInteger counter = context.get(invokeCounterKey, - AtomicInteger.class); - return counter.decrementAndGet() == 0; - } -} diff --git a/skywalking-sniffer/skywalking-api/src/test/java/test/a/eye/cloud/api/TimeTest.java b/skywalking-sniffer/skywalking-api/src/test/java/test/a/eye/cloud/api/TimeTest.java deleted file mode 100644 index 96bba25768..0000000000 --- a/skywalking-sniffer/skywalking-api/src/test/java/test/a/eye/cloud/api/TimeTest.java +++ /dev/null @@ -1,25 +0,0 @@ -package test.a.eye.cloud.api; - -import org.junit.Test; - -import com.a.eye.skywalking.invoke.monitor.RPCClientInvokeMonitor; -import com.a.eye.skywalking.model.Identification; -import com.a.eye.skywalking.model.Identification.IdentificationBuilder; - -public class TimeTest { - @Test - public void test(){ - RPCClientInvokeMonitor sender = new RPCClientInvokeMonitor(); - long start = System.currentTimeMillis(); - for (int i = 0; i < 100; i++) { - IdentificationBuilder builder = Identification - .newBuilder() - .viewPoint("1111"); - sender.beforeInvoke(builder.build()); - sender.afterInvoke(); - } - long end = System.currentTimeMillis(); - System.out.println(end - start + "ms"); - } - -} diff --git a/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisMethodInterceptor.java b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisMethodInterceptor.java index c43ead0245..518c9ada75 100644 --- a/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisMethodInterceptor.java +++ b/skywalking-sniffer/skywalking-sdk-plugin/jedis-2.x-plugin/src/main/java/com/a/eye/skywalking/plugin/jedis/v2/JedisMethodInterceptor.java @@ -1,44 +1,47 @@ package com.a.eye.skywalking.plugin.jedis.v2; -import com.a.eye.skywalking.invoke.monitor.RPCClientInvokeMonitor; import com.a.eye.skywalking.model.Identification; import com.a.eye.skywalking.plugin.interceptor.EnhancedClassInstanceContext; -import com.a.eye.skywalking.plugin.interceptor.assist.SimpleObjectFirstInvokeInterceptor; +import com.a.eye.skywalking.plugin.interceptor.assist.NoCocurrencyAceessObject; import com.a.eye.skywalking.plugin.interceptor.enhance.InstanceMethodInvokeContext; import com.a.eye.skywalking.plugin.interceptor.enhance.MethodInterceptResult; -public class JedisMethodInterceptor extends SimpleObjectFirstInvokeInterceptor { +public class JedisMethodInterceptor extends NoCocurrencyAceessObject { protected static final String REDIS_CONN_INFO_KEY = "redisClusterConnInfo"; - private static RPCClientInvokeMonitor rpcClientInvokeMonitor = new RPCClientInvokeMonitor(); @Override public void beforeMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, MethodInterceptResult result) { - if (this.isFirstBeforeMethod(context)) { - /** - * redis server wouldn't process rpc context. ignore the - * return(ContextData) of sender's beforeSend - */ - Identification.IdentificationBuilder builder = Identification + this.whenEnter(context, new Runnable() { + @Override public void run() { + /** + * redis server wouldn't process rpc context. ignore the + * return(ContextData) of sender's beforeSend + */ + Identification.IdentificationBuilder builder = Identification .newBuilder() .viewPoint( - context.get(REDIS_CONN_INFO_KEY, String.class) - + " " + interceptorContext.methodName()) + context.get(REDIS_CONN_INFO_KEY, String.class) + + " " + interceptorContext.methodName()) .spanType(RedisBuriedPointType.INSTANCE); - if (interceptorContext.allArguments().length > 0 + if (interceptorContext.allArguments().length > 0 && interceptorContext.allArguments()[0] instanceof String) { - builder.businessKey("key=" + builder.businessKey("key=" + interceptorContext.allArguments()[0]); + } + rpcClientInvokeMonitor.beforeInvoke(builder.build()); } - rpcClientInvokeMonitor.beforeInvoke(builder.build()); - } + }); } @Override public Object afterMethod(EnhancedClassInstanceContext context, InstanceMethodInvokeContext interceptorContext, Object ret) { - if (this.isLastAfterMethod(context)) { - rpcClientInvokeMonitor.afterInvoke(); - } + this.whenExist(context, new Runnable(){ + @Override public void run() { + rpcClientInvokeMonitor.afterInvoke(); + } + }); + return ret; } -- GitLab