From 2a170480ea3248ff282c972b03b4020369d2b851 Mon Sep 17 00:00:00 2001 From: emeroad Date: Fri, 13 Sep 2024 18:16:52 +0900 Subject: [PATCH] [#11503] Remove duplicate code in SimpleCache --- .../profiler/test/MockApiMetaDataService.java | 2 +- .../pinpoint/profiler/cache/AtomicIntId.java | 14 +++++ .../pinpoint/profiler/cache/Result.java | 5 ++ .../pinpoint/profiler/cache/SimpleCache.java | 58 +++++++++++-------- .../pinpoint/profiler/cache/UidCache.java | 42 -------------- .../metadata/ApiMetaDataServiceProvider.java | 2 +- .../provider/metadata/SimpleCacheFactory.java | 8 +-- .../metadata/SqlMetadataServiceProvider.java | 2 +- .../StringMetadataServiceProvider.java | 2 +- .../metadata/DefaultApiMetaDataService.java | 4 +- .../DefaultStringMetaDataService.java | 4 +- .../metadata/UidCachingSqlNormalizer.java | 4 +- .../profiler/cache/SimpleCacheTest.java | 5 +- ...t.java => SimpleCacheUidFunctionTest.java} | 6 +- .../DefaultApiMetaDataServiceTest.java | 2 +- .../DefaultStringMetaDataServiceTest.java | 2 +- .../SimpleCachingSqlNormalizerTest.java | 8 +-- .../metadata/SqlCacheServiceTest.java | 2 +- 18 files changed, 80 insertions(+), 92 deletions(-) create mode 100644 agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/AtomicIntId.java delete mode 100644 agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/UidCache.java rename agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/cache/{UidCacheTest.java => SimpleCacheUidFunctionTest.java} (91%) diff --git a/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/MockApiMetaDataService.java b/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/MockApiMetaDataService.java index f8f4dde8ed377..a23489bca94fe 100644 --- a/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/MockApiMetaDataService.java +++ b/agent-module/profiler-test/src/main/java/com/navercorp/pinpoint/profiler/test/MockApiMetaDataService.java @@ -31,7 +31,7 @@ */ public class MockApiMetaDataService implements ApiMetaDataService { - private final SimpleCache apiCache = new SimpleCache<>(); + private final SimpleCache apiCache = SimpleCache.newIdCache(); private final EnhancedDataSender enhancedDataSender; diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/AtomicIntId.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/AtomicIntId.java new file mode 100644 index 0000000000000..b59812eb84698 --- /dev/null +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/AtomicIntId.java @@ -0,0 +1,14 @@ +package com.navercorp.pinpoint.profiler.cache; + +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Function; + +public class AtomicIntId implements Function { + + private final AtomicInteger idGen = new AtomicInteger(0); + + @Override + public Integer apply(K key) { + return idGen.incrementAndGet(); + } +} \ No newline at end of file diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/Result.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/Result.java index 6c579352ab345..8a1c7b64c29a0 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/Result.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/Result.java @@ -38,4 +38,9 @@ public boolean isNewValue() { public ID getId() { return id; } + + @Override + public String toString() { + return "Result{" + newValue +", " + id + '}'; + } } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/SimpleCache.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/SimpleCache.java index 509617aca4679..0353f5e723807 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/SimpleCache.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/SimpleCache.java @@ -18,51 +18,61 @@ import com.github.benmanes.caffeine.cache.Caffeine; +import java.util.Objects; import java.util.concurrent.ConcurrentMap; -import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Function; /** * @author emeroad */ -public class SimpleCache implements Cache> { +public class SimpleCache implements Cache> { // zero means not exist. - private final ConcurrentMap> cache; - private final AtomicInteger idGen = new AtomicInteger(); + private final ConcurrentMap cache; + private final Function idFunction; - public SimpleCache() { - this(1024); + public static SimpleCache newIdCache() { + return newIdCache(1024); } - public SimpleCache(int cacheSize) { + public static SimpleCache newIdCache(int cacheSize) { + return new SimpleCache<>(cacheSize, new AtomicIntId<>()); + } + + public SimpleCache(Function idFunction) { + this(1024, idFunction); + } + + public SimpleCache(int cacheSize, Function idFunction) { this.cache = createCache(cacheSize); + this.idFunction = Objects.requireNonNull(idFunction, "idFunction"); } - private ConcurrentMap> createCache(int maxCacheSize) { + private ConcurrentMap createCache(int maxCacheSize) { final Caffeine cacheBuilder = CaffeineBuilder.newBuilder(); cacheBuilder.initialCapacity(maxCacheSize); cacheBuilder.maximumSize(maxCacheSize); - com.github.benmanes.caffeine.cache.Cache> localCache = cacheBuilder.build(); + com.github.benmanes.caffeine.cache.Cache localCache = cacheBuilder.build(); return localCache.asMap(); } @Override - public Result put(T value) { - final Result find = this.cache.get(value); - if (find != null) { - return find; - } + public Result put(K value) { + final PutIfAbsent putIfAbsent = new PutIfAbsent(); + final V id = this.cache.computeIfAbsent(value, putIfAbsent); + return new Result<>(putIfAbsent.called, id); + } + + private class PutIfAbsent implements Function { + + private boolean called; - // Use negative values too to reduce data size - final int newId = nextId(); - final Result result = new Result<>(false, newId); - final Result before = this.cache.putIfAbsent(value, result); - if (before != null) { - return before; + public PutIfAbsent() { } - return new Result<>(true, newId); - } - private int nextId() { - return this.idGen.incrementAndGet(); + @Override + public V apply(K key) { + this.called = true; + return idFunction.apply(key); + } } } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/UidCache.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/UidCache.java deleted file mode 100644 index 3439a2f54c720..0000000000000 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/cache/UidCache.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.navercorp.pinpoint.profiler.cache; - -import com.github.benmanes.caffeine.cache.Caffeine; - -import java.util.concurrent.ConcurrentMap; -import java.util.function.Function; - -public class UidCache implements Cache> { - // zero means not exist. - private final ConcurrentMap> cache; - - private final Function uidFunction; - - public UidCache(int cacheSize, Function uidFunction) { - this.cache = createCache(cacheSize); - this.uidFunction = uidFunction; - } - - private ConcurrentMap> createCache(int maxCacheSize) { - final Caffeine cacheBuilder = CaffeineBuilder.newBuilder(); - cacheBuilder.initialCapacity(maxCacheSize); - cacheBuilder.maximumSize(maxCacheSize); - com.github.benmanes.caffeine.cache.Cache> localCache = cacheBuilder.build(); - return localCache.asMap(); - } - - @Override - public Result put(String value) { - final Result find = this.cache.get(value); - if (find != null) { - return find; - } - - final byte[] uid = uidFunction.apply(value); - final Result result = new Result<>(false, uid); - final Result before = this.cache.putIfAbsent(value, result); - if (before != null) { - return before; - } - return new Result<>(true, uid); - } -} diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/metadata/ApiMetaDataServiceProvider.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/metadata/ApiMetaDataServiceProvider.java index f07e1e6749853..9b2f80e79cdf1 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/metadata/ApiMetaDataServiceProvider.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/metadata/ApiMetaDataServiceProvider.java @@ -45,7 +45,7 @@ public ApiMetaDataServiceProvider(@MetadataDataSender Provider enhancedDataSender = this.enhancedDataSenderProvider.get(); - final SimpleCache simpleCache = simpleCacheFactory.newSimpleCache(); + final SimpleCache simpleCache = simpleCacheFactory.newSimpleCache(); return new DefaultApiMetaDataService(enhancedDataSender, simpleCache); } } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/metadata/SimpleCacheFactory.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/metadata/SimpleCacheFactory.java index 2a03fdbd76cce..c6fb0a1949d0d 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/metadata/SimpleCacheFactory.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/metadata/SimpleCacheFactory.java @@ -26,12 +26,12 @@ public class SimpleCacheFactory { public SimpleCacheFactory() { } - public SimpleCache newSimpleCache() { - return new SimpleCache<>(); + public SimpleCache newSimpleCache() { + return SimpleCache.newIdCache(); } - public SimpleCache newSimpleCache(int cacheSize) { - return new SimpleCache<>(cacheSize); + public SimpleCache newSimpleCache(int cacheSize) { + return SimpleCache.newIdCache(cacheSize); } } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/metadata/SqlMetadataServiceProvider.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/metadata/SqlMetadataServiceProvider.java index 397e31ca8cf8f..7308d7d32d6c4 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/metadata/SqlMetadataServiceProvider.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/metadata/SqlMetadataServiceProvider.java @@ -65,7 +65,7 @@ public SqlMetaDataService get() { SqlCacheService sqlCacheService = new SqlCacheService<>(enhancedDataSender, simpleCachingSqlNormalizer, maxSqlLength); return new SqlUidMetaDataService(sqlCacheService); } else { - final SimpleCache stringCache = simpleCacheFactory.newSimpleCache(jdbcSqlCacheSize); + final SimpleCache stringCache = simpleCacheFactory.newSimpleCache(jdbcSqlCacheSize); SimpleCachingSqlNormalizer simpleCachingSqlNormalizer = new SimpleCachingSqlNormalizer(stringCache); SqlCacheService sqlCacheService = new SqlCacheService<>(enhancedDataSender, simpleCachingSqlNormalizer, maxSqlLength); return new DefaultSqlMetaDataService(sqlCacheService); diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/metadata/StringMetadataServiceProvider.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/metadata/StringMetadataServiceProvider.java index 57f4e81be97dd..a8316491494a3 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/metadata/StringMetadataServiceProvider.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/context/provider/metadata/StringMetadataServiceProvider.java @@ -44,7 +44,7 @@ public StringMetadataServiceProvider(@MetadataDataSender EnhancedDataSender stringCache = simpleCacheFactory.newSimpleCache(); + final SimpleCache stringCache = simpleCacheFactory.newSimpleCache(); return new DefaultStringMetaDataService(enhancedDataSender, stringCache); } } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/DefaultApiMetaDataService.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/DefaultApiMetaDataService.java index ae237335e82b5..bc4391aabc2ab 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/DefaultApiMetaDataService.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/DefaultApiMetaDataService.java @@ -28,11 +28,11 @@ */ public class DefaultApiMetaDataService implements ApiMetaDataService { - private final SimpleCache apiCache; + private final SimpleCache apiCache; private final EnhancedDataSender enhancedDataSender; - public DefaultApiMetaDataService(EnhancedDataSender enhancedDataSender, SimpleCache apiCache) { + public DefaultApiMetaDataService(EnhancedDataSender enhancedDataSender, SimpleCache apiCache) { this.enhancedDataSender = Objects.requireNonNull(enhancedDataSender, "enhancedDataSender"); this.apiCache = Objects.requireNonNull(apiCache, "apiCache"); } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/DefaultStringMetaDataService.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/DefaultStringMetaDataService.java index 260fd62052c62..eb65c65d03852 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/DefaultStringMetaDataService.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/DefaultStringMetaDataService.java @@ -27,11 +27,11 @@ */ public class DefaultStringMetaDataService implements StringMetaDataService { - private final SimpleCache stringCache; + private final SimpleCache stringCache; private final EnhancedDataSender enhancedDataSender; - public DefaultStringMetaDataService(EnhancedDataSender enhancedDataSender, SimpleCache stringCache) { + public DefaultStringMetaDataService(EnhancedDataSender enhancedDataSender, SimpleCache stringCache) { this.enhancedDataSender = Objects.requireNonNull(enhancedDataSender, "enhancedDataSender"); this.stringCache = Objects.requireNonNull(stringCache, "stringCache"); diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/UidCachingSqlNormalizer.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/UidCachingSqlNormalizer.java index 61247902ce9d6..2107b76ef2c20 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/UidCachingSqlNormalizer.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/UidCachingSqlNormalizer.java @@ -6,7 +6,7 @@ import com.navercorp.pinpoint.common.profiler.sql.SqlNormalizer; import com.navercorp.pinpoint.profiler.cache.Cache; import com.navercorp.pinpoint.profiler.cache.Result; -import com.navercorp.pinpoint.profiler.cache.UidCache; +import com.navercorp.pinpoint.profiler.cache.SimpleCache; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -22,7 +22,7 @@ public class UidCachingSqlNormalizer implements CachingSqlNormalizer(cacheSize, hashFunction); this.sqlNormalizer = new DefaultSqlNormalizer(); this.lengthLimit = lengthLimit; } diff --git a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/cache/SimpleCacheTest.java b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/cache/SimpleCacheTest.java index af5d578d939a0..5dadff75da584 100644 --- a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/cache/SimpleCacheTest.java +++ b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/cache/SimpleCacheTest.java @@ -26,9 +26,10 @@ public class SimpleCacheTest { @Test public void startKey0() { - SimpleCache cache = new SimpleCache<>(); + SimpleCache cache = SimpleCache.newIdCache(); Result test1 = cache.put("test1"); Result test2 = cache.put("test2"); + Assertions.assertEquals(1, test1.getId()); Assertions.assertEquals(2, test2.getId()); } @@ -36,7 +37,7 @@ public void startKey0() { @Test public void put() { - SimpleCache cache = new SimpleCache<>(); + SimpleCache cache = SimpleCache.newIdCache();; Result test = cache.put("test"); Assertions.assertEquals(1, test.getId()); Assertions.assertTrue(test.isNewValue()); diff --git a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/cache/UidCacheTest.java b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/cache/SimpleCacheUidFunctionTest.java similarity index 91% rename from agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/cache/UidCacheTest.java rename to agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/cache/SimpleCacheUidFunctionTest.java index a4b8bf0003830..732db9162bc7a 100644 --- a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/cache/UidCacheTest.java +++ b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/cache/SimpleCacheUidFunctionTest.java @@ -16,15 +16,15 @@ import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) -class UidCacheTest { - UidCache sut; +class SimpleCacheUidFunctionTest { + SimpleCache sut; @Mock Function uidFunction; @BeforeEach void setUp() { - sut = new UidCache(1024, uidFunction); + sut = new SimpleCache<>(1024, uidFunction); when(uidFunction.apply(any())) .thenReturn(new byte[]{}); diff --git a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/metadata/DefaultApiMetaDataServiceTest.java b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/metadata/DefaultApiMetaDataServiceTest.java index 0e22fcb855af4..e5465bd0f099a 100644 --- a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/metadata/DefaultApiMetaDataServiceTest.java +++ b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/metadata/DefaultApiMetaDataServiceTest.java @@ -35,7 +35,7 @@ public class DefaultApiMetaDataServiceTest { @Test public void cacheApi() { EnhancedDataSender dataSender = mock(EnhancedDataSender.class); - SimpleCache cache = new SimpleCache<>(); + SimpleCache cache = SimpleCache.newIdCache(); ApiMetaDataService apiMetaDataService = new DefaultApiMetaDataService(dataSender, cache); MethodDescriptor methodDescriptor = new DefaultMethodDescriptor("clazz", "method", diff --git a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/metadata/DefaultStringMetaDataServiceTest.java b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/metadata/DefaultStringMetaDataServiceTest.java index 1218eb4f8de7a..f82bea194bbe0 100644 --- a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/metadata/DefaultStringMetaDataServiceTest.java +++ b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/metadata/DefaultStringMetaDataServiceTest.java @@ -33,7 +33,7 @@ public class DefaultStringMetaDataServiceTest { @Test public void cacheString() { EnhancedDataSender dataSender = mock(EnhancedDataSender.class); - SimpleCache stringCache = new SimpleCache<>(); + SimpleCache stringCache = SimpleCache.newIdCache(); StringMetaDataService stringMetaDataService = new DefaultStringMetaDataService(dataSender, stringCache); String str = "test"; diff --git a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/metadata/SimpleCachingSqlNormalizerTest.java b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/metadata/SimpleCachingSqlNormalizerTest.java index 7a37e9fc26574..aae8d1dcd4b7f 100644 --- a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/metadata/SimpleCachingSqlNormalizerTest.java +++ b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/metadata/SimpleCachingSqlNormalizerTest.java @@ -27,7 +27,7 @@ public class SimpleCachingSqlNormalizerTest { @Test public void testNormalizedSql() { - SimpleCache cache = newCache(1); + SimpleCache cache = newCache(1); SimpleCachingSqlNormalizer normalizer = new SimpleCachingSqlNormalizer(cache); ParsingResultInternal parsingResult = new DefaultParsingResult("select * from dual"); @@ -45,7 +45,7 @@ public void testNormalizedSql() { @Test public void testNormalizedSql_cache_expire() { - SimpleCache cache = newCache(1); + SimpleCache cache = newCache(1); SimpleCachingSqlNormalizer normalizer = new SimpleCachingSqlNormalizer(cache); ParsingResultInternal parsingResult = new DefaultParsingResult("select * from table1"); boolean newCache = normalizer.normalizedSql(parsingResult); @@ -61,7 +61,7 @@ public void testNormalizedSql_cache_expire() { Assertions.assertTrue(newCache_parsingResult1_recached); } - private SimpleCache newCache(int size) { - return new SimpleCache<>(size); + private SimpleCache newCache(int cacheSize) { + return SimpleCache.newIdCache(cacheSize); } } \ No newline at end of file diff --git a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/metadata/SqlCacheServiceTest.java b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/metadata/SqlCacheServiceTest.java index a8a33d4a22fbb..306106fe13b7a 100644 --- a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/metadata/SqlCacheServiceTest.java +++ b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/metadata/SqlCacheServiceTest.java @@ -44,7 +44,7 @@ public class SqlCacheServiceTest { @BeforeEach public void setUp() { - SimpleCache sqlCache = new SimpleCache<>(100); + SimpleCache sqlCache = SimpleCache.newIdCache(100); SimpleCachingSqlNormalizer cachingSqlNormalizer = new SimpleCachingSqlNormalizer(sqlCache); sut = new SqlCacheService<>(dataSender, cachingSqlNormalizer, MAX_LENGTH); }