From b2ad63430bf9851e92e4d6bfcbcb7d96a6dcc6d7 Mon Sep 17 00:00:00 2001 From: lsyf Date: Wed, 19 Feb 2020 12:10:00 +0800 Subject: [PATCH] =?UTF-8?q?Fix=20ehcache:=C2=A0performance=20issue?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../v2/EhcacheConstructorInterceptor.java | 18 +++------- .../EhcachePrivateConstructorInterceptor.java | 35 +++++++++++++++++++ .../define/EhcachePluginInstrumentation.java | 15 ++++++-- .../ehcache/v2/EhcacheInterceptorTest.java | 7 ++++ 4 files changed, 59 insertions(+), 16 deletions(-) create mode 100644 apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcachePrivateConstructorInterceptor.java diff --git a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheConstructorInterceptor.java b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheConstructorInterceptor.java index dd848d68a6..1d4868aaf2 100644 --- a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheConstructorInterceptor.java +++ b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheConstructorInterceptor.java @@ -18,7 +18,6 @@ package org.apache.skywalking.apm.plugin.ehcache.v2; -import net.sf.ehcache.Cache; import net.sf.ehcache.config.CacheConfiguration; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; @@ -26,20 +25,11 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceC public class EhcacheConstructorInterceptor implements InstanceConstructorInterceptor { @Override public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { - try { - CacheConfiguration cacheConfiguration = (CacheConfiguration) allArguments[0]; + CacheConfiguration cacheConfiguration = (CacheConfiguration) allArguments[0]; - // get cache name - if (cacheConfiguration != null) { - objInst.setSkyWalkingDynamicField(new EhcacheEnhanceInfo(cacheConfiguration.getName())); - } - } catch (ClassCastException e) { - Cache cache = (Cache) allArguments[0]; - - // get cache name - if (cache != null && cache.getCacheConfiguration() != null) { - objInst.setSkyWalkingDynamicField(new EhcacheEnhanceInfo(cache.getCacheConfiguration().getName())); - } + // get cache name + if (cacheConfiguration != null) { + objInst.setSkyWalkingDynamicField(new EhcacheEnhanceInfo(cacheConfiguration.getName())); } } } diff --git a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcachePrivateConstructorInterceptor.java b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcachePrivateConstructorInterceptor.java new file mode 100644 index 0000000000..6610332bff --- /dev/null +++ b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcachePrivateConstructorInterceptor.java @@ -0,0 +1,35 @@ +/* + * 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.skywalking.apm.plugin.ehcache.v2; + +import net.sf.ehcache.Cache; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance; +import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor; + +public class EhcachePrivateConstructorInterceptor implements InstanceConstructorInterceptor { + @Override + public void onConstruct(EnhancedInstance objInst, Object[] allArguments) { + Cache cache = (Cache) allArguments[0]; + + // get cache name + if (cache != null && cache.getCacheConfiguration() != null) { + objInst.setSkyWalkingDynamicField(new EhcacheEnhanceInfo(cache.getCacheConfiguration().getName())); + } + } +} diff --git a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/define/EhcachePluginInstrumentation.java b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/define/EhcachePluginInstrumentation.java index bb741a1759..1ef702a9ab 100644 --- a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/define/EhcachePluginInstrumentation.java +++ b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/ehcache/v2/define/EhcachePluginInstrumentation.java @@ -38,6 +38,7 @@ public class EhcachePluginInstrumentation extends ClassInstanceMethodsEnhancePlu public static final String INTERCEPT_CLASS = "net.sf.ehcache.Cache"; public static final String CONSTRUCTOR_CLASS_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.ehcache.v2.EhcacheConstructorInterceptor"; + public static final String PRIVATE_CONSTRUCTOR_CLASS_INTERCEPT_CLASS = "org.apache.skywalking.apm.plugin.ehcache.v2.EhcachePrivateConstructorInterceptor"; // get and put value public static final String PUT_CACHE_ENHANCE_METHOD = "put"; @@ -80,14 +81,24 @@ public class EhcachePluginInstrumentation extends ClassInstanceMethodsEnhancePlu new ConstructorInterceptPoint() { @Override public ElementMatcher getConstructorMatcher() { - return takesArgument(0, named("net.sf.ehcache.config.CacheConfiguration")) - .or(isPrivate().and(takesArgument(0, named("net.sf.ehcache.Cache")))); + return takesArgument(0, named("net.sf.ehcache.config.CacheConfiguration")); } @Override public String getConstructorInterceptor() { return CONSTRUCTOR_CLASS_INTERCEPT_CLASS; } + }, + new ConstructorInterceptPoint() { + @Override + public ElementMatcher getConstructorMatcher() { + return isPrivate().and(takesArgument(0, named("net.sf.ehcache.Cache"))); + } + + @Override + public String getConstructorInterceptor() { + return PRIVATE_CONSTRUCTOR_CLASS_INTERCEPT_CLASS; + } } }; } diff --git a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheInterceptorTest.java b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheInterceptorTest.java index dba2c08edd..a612b9c3e0 100644 --- a/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheInterceptorTest.java +++ b/apm-sniffer/apm-sdk-plugin/ehcache-2.x-plugin/src/test/java/org/apache/skywalking/apm/plugin/ehcache/v2/EhcacheInterceptorTest.java @@ -66,6 +66,7 @@ public class EhcacheInterceptorTest { private EhcacheOperateAllInterceptor operateAllInterceptor; private EhcacheLockInterceptor lockInterceptor; private EhcacheConstructorInterceptor constructorInterceptor; + private EhcachePrivateConstructorInterceptor privateConstructorInterceptor; private EhcacheCacheNameInterceptor cacheNameInterceptor; private Object[] operateObjectArguments; private Object[] operateElementArguments; @@ -106,6 +107,7 @@ public class EhcacheInterceptorTest { operateElementInterceptor = new EhcacheOperateElementInterceptor(); operateAllInterceptor = new EhcacheOperateAllInterceptor(); constructorInterceptor = new EhcacheConstructorInterceptor(); + privateConstructorInterceptor = new EhcachePrivateConstructorInterceptor(); cacheNameInterceptor = new EhcacheCacheNameInterceptor(); lockInterceptor = new EhcacheLockInterceptor(); @@ -139,6 +141,11 @@ public class EhcacheInterceptorTest { constructorInterceptor.onConstruct(enhancedInstance, new Object[] {new CacheConfiguration(CACHE_NAME, 20)}); } + @Test + public void assertPrivateConstruct() throws Throwable { + privateConstructorInterceptor.onConstruct(enhancedInstance, new Object[] {new Cache(new CacheConfiguration(CACHE_NAME, 20))}); + } + @Test public void assertSetNameSuccess() throws Throwable { cacheNameInterceptor.beforeMethod(enhancedInstance, setNameMethod, cacheNameArguments, null, null); -- GitLab