提交 04638c51 编写于 作者: wu-sheng's avatar wu-sheng

Rename and refactor SimapleObjectFirstInvokeInterceptor to NoCocurrencyAceessObject

上级 7e640311
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();
}
}
}
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;
}
}
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");
}
}
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;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册