From 255e5103116bda422978b1b3e0181545c3756c71 Mon Sep 17 00:00:00 2001 From: Ohjun Kwon Date: Mon, 1 Jul 2024 16:01:08 +0900 Subject: [PATCH] [#11166] Bypass cache for long SQL queries --- .../config/DefaultProfilerConfig.java | 17 ++++- .../bootstrap/config/ProfilerConfig.java | 2 + .../pinpoint/profiler/cache/UidCache.java | 18 ++--- .../metadata/SqlMetadataServiceProvider.java | 13 ++-- .../metadata/DefaultSqlMetaDataService.java | 2 + ...r.java => SimpleCachingSqlNormalizer.java} | 15 ++-- .../metadata/SqlUidMetaDataService.java | 2 + .../metadata/UidCachingSqlNormalizer.java | 69 +++++++++++++++++++ .../pinpoint/profiler/cache/UidCacheTest.java | 55 +++++++++------ .../SimpleCachingSqlNormalizerTest.java | 4 +- .../metadata/SqlCacheServiceTest.java | 2 +- .../metadata/SqlUidMetaDataServiceTest.java | 49 +++++++++---- 12 files changed, 180 insertions(+), 68 deletions(-) rename agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/{DefaultCachingSqlNormalizer.java => SimpleCachingSqlNormalizer.java} (80%) create mode 100644 agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/UidCachingSqlNormalizer.java diff --git a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/DefaultProfilerConfig.java b/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/DefaultProfilerConfig.java index c78bd1ab934d..5030330543dc 100644 --- a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/DefaultProfilerConfig.java +++ b/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/DefaultProfilerConfig.java @@ -71,6 +71,8 @@ public class DefaultProfilerConfig implements ProfilerConfig { private boolean traceSqlBindValue = false; @Value("${profiler.jdbc.maxsqlbindvaluesize}") private int maxSqlBindValueSize = 1024; + @Value("${profiler.jdbc.sqlcachelengthlimit}") + private int maxSqlLength = 2048; @Value("${profiler.transport.grpc.stats.logging.period}") private String grpcStatLoggingPeriod = "PT1M"; @@ -134,6 +136,11 @@ public int getMaxSqlBindValueSize() { return maxSqlBindValueSize; } + @Override + public int getMaxSqlLength() { + return maxSqlLength; + } + @Override public String getGrpcStatLoggingPeriod() { return grpcStatLoggingPeriod; @@ -273,16 +280,22 @@ public Map readPattern(String propertyNamePatternRegex) { @Override public String toString() { - return "DefaultProfilerConfig{" + "pinpointDisable='" + pinpointDisable + '\'' + - ", activeProfile=" + activeProfile + + return "DefaultProfilerConfig{" + + "properties=" + properties + + ", pinpointDisable='" + pinpointDisable + '\'' + ", logDirMaxBackupSize=" + logDirMaxBackupSize + + ", activeProfile='" + activeProfile + '\'' + ", staticResourceCleanup=" + staticResourceCleanup + + ", transportModule=" + transportModule + ", jdbcSqlCacheSize=" + jdbcSqlCacheSize + ", traceSqlBindValue=" + traceSqlBindValue + ", maxSqlBindValueSize=" + maxSqlBindValueSize + + ", maxSqlLength=" + maxSqlLength + + ", grpcStatLoggingPeriod='" + grpcStatLoggingPeriod + '\'' + ", httpStatusCodeErrors=" + httpStatusCodeErrors + ", injectionModuleFactoryClazzName='" + injectionModuleFactoryClazzName + '\'' + ", applicationNamespace='" + applicationNamespace + '\'' + + ", agentClassloaderAdditionalLibs='" + agentClassloaderAdditionalLibs + '\'' + '}'; } } \ No newline at end of file diff --git a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/ProfilerConfig.java b/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/ProfilerConfig.java index 01774e5456ac..1285ebfc5724 100644 --- a/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/ProfilerConfig.java +++ b/agent-module/bootstraps/bootstrap-core/src/main/java/com/navercorp/pinpoint/bootstrap/config/ProfilerConfig.java @@ -44,6 +44,8 @@ public interface ProfilerConfig { int getMaxSqlBindValueSize(); + int getMaxSqlLength(); + String getGrpcStatLoggingPeriod(); @InterfaceAudience.Private 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 index ec8b02343450..3439a2f54c72 100644 --- 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 @@ -1,21 +1,19 @@ package com.navercorp.pinpoint.profiler.cache; import com.github.benmanes.caffeine.cache.Caffeine; -import com.google.common.hash.HashFunction; -import com.google.common.hash.Hashing; -import java.nio.charset.StandardCharsets; 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 HashFunction hashFunction = Hashing.murmur3_128(); + private final Function uidFunction; - public UidCache(int cacheSize) { + public UidCache(int cacheSize, Function uidFunction) { this.cache = createCache(cacheSize); + this.uidFunction = uidFunction; } private ConcurrentMap> createCache(int maxCacheSize) { @@ -33,7 +31,7 @@ public Result put(String value) { return find; } - final byte[] uid = calculateUid(value); + final byte[] uid = uidFunction.apply(value); final Result result = new Result<>(false, uid); final Result before = this.cache.putIfAbsent(value, result); if (before != null) { @@ -41,10 +39,4 @@ public Result put(String value) { } return new Result<>(true, uid); } - - private byte[] calculateUid(String value) { - return hashFunction - .hashString(value, StandardCharsets.UTF_8) - .asBytes(); - } } 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 44a2cfea6ca3..b20289a565b2 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 @@ -22,17 +22,15 @@ import com.navercorp.pinpoint.common.profiler.message.EnhancedDataSender; import com.navercorp.pinpoint.io.ResponseMessage; import com.navercorp.pinpoint.profiler.cache.SimpleCache; -import com.navercorp.pinpoint.profiler.cache.UidCache; import com.navercorp.pinpoint.profiler.context.module.MetadataDataSender; import com.navercorp.pinpoint.profiler.context.monitor.config.MonitorConfig; -import com.navercorp.pinpoint.profiler.metadata.CachingSqlNormalizer; -import com.navercorp.pinpoint.profiler.metadata.DefaultCachingSqlNormalizer; import com.navercorp.pinpoint.profiler.metadata.DefaultSqlMetaDataService; import com.navercorp.pinpoint.profiler.metadata.MetaDataType; -import com.navercorp.pinpoint.profiler.metadata.ParsingResultInternal; +import com.navercorp.pinpoint.profiler.metadata.SimpleCachingSqlNormalizer; import com.navercorp.pinpoint.profiler.metadata.SqlCacheService; import com.navercorp.pinpoint.profiler.metadata.SqlMetaDataService; import com.navercorp.pinpoint.profiler.metadata.SqlUidMetaDataService; +import com.navercorp.pinpoint.profiler.metadata.UidCachingSqlNormalizer; import java.util.Objects; @@ -61,13 +59,14 @@ public SqlMetaDataService get() { final int jdbcSqlCacheSize = profilerConfig.getJdbcSqlCacheSize(); if (monitorConfig.isSqlStatEnable()) { - final UidCache stringCache = new UidCache(jdbcSqlCacheSize); - CachingSqlNormalizer> simpleCachingSqlNormalizer = new DefaultCachingSqlNormalizer<>(stringCache); + final int maxSqlLength = profilerConfig.getMaxSqlLength(); + + UidCachingSqlNormalizer simpleCachingSqlNormalizer = new UidCachingSqlNormalizer(jdbcSqlCacheSize, maxSqlLength); SqlCacheService sqlCacheService = new SqlCacheService<>(enhancedDataSender, simpleCachingSqlNormalizer); return new SqlUidMetaDataService(sqlCacheService); } else { final SimpleCache stringCache = simpleCacheFactory.newSimpleCache(jdbcSqlCacheSize); - CachingSqlNormalizer> simpleCachingSqlNormalizer = new DefaultCachingSqlNormalizer<>(stringCache); + SimpleCachingSqlNormalizer simpleCachingSqlNormalizer = new SimpleCachingSqlNormalizer(stringCache); SqlCacheService sqlCacheService = new SqlCacheService<>(enhancedDataSender, simpleCachingSqlNormalizer); return new DefaultSqlMetaDataService(sqlCacheService); } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/DefaultSqlMetaDataService.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/DefaultSqlMetaDataService.java index 22d75557c2dc..411f0bb298f1 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/DefaultSqlMetaDataService.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/DefaultSqlMetaDataService.java @@ -17,6 +17,7 @@ package com.navercorp.pinpoint.profiler.metadata; import com.navercorp.pinpoint.bootstrap.context.ParsingResult; +import com.navercorp.pinpoint.common.annotations.VisibleForTesting; import com.navercorp.pinpoint.common.trace.AnnotationKey; import com.navercorp.pinpoint.common.util.IntStringStringValue; import com.navercorp.pinpoint.common.util.StringUtils; @@ -65,6 +66,7 @@ public Annotation newSqlAnnotation(ParsingResult parsingResult, String bindVa return Annotations.of(AnnotationKey.SQL_ID.getCode(), sqlValue); } + @VisibleForTesting static MetaDataType newSqlMetaData(ParsingResultInternal parsingResult) { return new SqlMetaData(parsingResult.getId(), parsingResult.getSql()); } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/DefaultCachingSqlNormalizer.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/SimpleCachingSqlNormalizer.java similarity index 80% rename from agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/DefaultCachingSqlNormalizer.java rename to agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/SimpleCachingSqlNormalizer.java index b339d4de2c56..d35f5fd9f27b 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/DefaultCachingSqlNormalizer.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/SimpleCachingSqlNormalizer.java @@ -29,21 +29,19 @@ /** * @author emeroad */ -public class DefaultCachingSqlNormalizer implements CachingSqlNormalizer> { +public class SimpleCachingSqlNormalizer implements CachingSqlNormalizer> { + private final Logger logger = LogManager.getLogger(this.getClass()); - protected final Logger logger = LogManager.getLogger(this.getClass()); - - private final Cache> sqlCache; + private final Cache> sqlCache; private final SqlNormalizer sqlNormalizer; - public DefaultCachingSqlNormalizer(Cache> sqlCache) { + public SimpleCachingSqlNormalizer(Cache> sqlCache) { this.sqlCache = Objects.requireNonNull(sqlCache, "sqlCache"); this.sqlNormalizer = new DefaultSqlNormalizer(); } - @Override - public boolean normalizedSql(ParsingResultInternal parsingResult) { + public boolean normalizedSql(ParsingResultInternal parsingResult) { if (parsingResult == null) { return false; } @@ -55,7 +53,7 @@ public boolean normalizedSql(ParsingResultInternal parsingResult) { final String originalSql = parsingResult.getOriginalSql(); final NormalizedSql normalizedSql = this.sqlNormalizer.normalizeSql(originalSql); - final Result cachingResult = this.sqlCache.put(normalizedSql.getNormalizedSql()); + final Result cachingResult = this.sqlCache.put(normalizedSql.getNormalizedSql()); boolean success = parsingResult.setId(cachingResult.getId()); if (!success) { @@ -68,5 +66,4 @@ public boolean normalizedSql(ParsingResultInternal parsingResult) { return cachingResult.isNewValue(); } - } diff --git a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/SqlUidMetaDataService.java b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/SqlUidMetaDataService.java index 40d7e03f42b0..2d0192b741ab 100644 --- a/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/SqlUidMetaDataService.java +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/SqlUidMetaDataService.java @@ -1,6 +1,7 @@ package com.navercorp.pinpoint.profiler.metadata; import com.navercorp.pinpoint.bootstrap.context.ParsingResult; +import com.navercorp.pinpoint.common.annotations.VisibleForTesting; import com.navercorp.pinpoint.common.trace.AnnotationKey; import com.navercorp.pinpoint.common.util.BytesStringStringValue; import com.navercorp.pinpoint.common.util.StringUtils; @@ -45,6 +46,7 @@ public Annotation newSqlAnnotation(ParsingResult parsingResult, String bindVa return Annotations.of(AnnotationKey.SQL_UID.getCode(), sqlValue); } + @VisibleForTesting static MetaDataType newSqlUidMetaData(ParsingResultInternal parsingResult) { return new SqlUidMetaData(parsingResult.getId(), parsingResult.getSql()); } 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 new file mode 100644 index 000000000000..61247902ce9d --- /dev/null +++ b/agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/UidCachingSqlNormalizer.java @@ -0,0 +1,69 @@ +package com.navercorp.pinpoint.profiler.metadata; + +import com.google.common.hash.Hashing; +import com.navercorp.pinpoint.common.profiler.sql.DefaultSqlNormalizer; +import com.navercorp.pinpoint.common.profiler.sql.NormalizedSql; +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 org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; + +import java.nio.charset.StandardCharsets; +import java.util.function.Function; + +public class UidCachingSqlNormalizer implements CachingSqlNormalizer> { + private final Logger logger = LogManager.getLogger(this.getClass()); + private final Function hashFunction = x -> Hashing.murmur3_128().hashString(x, StandardCharsets.UTF_8).asBytes(); + + private final Cache> sqlCache; + private final SqlNormalizer sqlNormalizer; + private final int lengthLimit; + + public UidCachingSqlNormalizer(int cacheSize, int lengthLimit) { + this.sqlCache = new UidCache(cacheSize, hashFunction); + this.sqlNormalizer = new DefaultSqlNormalizer(); + this.lengthLimit = lengthLimit; + } + + @Override + public boolean normalizedSql(ParsingResultInternal parsingResult) { + if (parsingResult == null) { + return false; + } + if (parsingResult.getId() != null) { + // already cached + return false; + } + + final String originalSql = parsingResult.getOriginalSql(); + final NormalizedSql normalizedSql = this.sqlNormalizer.normalizeSql(originalSql); + + byte[] uid; + boolean isNewValue; + if (lengthLimit == -1 || normalizedSql.getNormalizedSql().length() <= lengthLimit) { + final Result cachingResult = this.sqlCache.put(normalizedSql.getNormalizedSql()); + uid = cachingResult.getId(); + isNewValue = cachingResult.isNewValue(); + } else { + // bypass cache + uid = hashFunction.apply(normalizedSql.getNormalizedSql()); + isNewValue = true; + } + + setParsingResult(parsingResult, uid, normalizedSql); + return isNewValue; + } + + private void setParsingResult(ParsingResultInternal parsingResult, byte[] uid, NormalizedSql normalizedSql) { + boolean success = parsingResult.setId(uid); + if (!success) { + if (logger.isWarnEnabled()) { + logger.warn("invalid state. setSqlUid fail setUid:{}, ParsingResultInternal:{}", uid, parsingResult); + } + } + parsingResult.setSql(normalizedSql.getNormalizedSql()); + parsingResult.setOutput(normalizedSql.getParseParameter()); + } +} 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/UidCacheTest.java index 452d9d73e171..a4b8bf000383 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/UidCacheTest.java @@ -1,40 +1,55 @@ package com.navercorp.pinpoint.profiler.cache; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; -import java.util.Arrays; +import java.util.function.Function; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; -public class UidCacheTest { - @Test - public void sameValue() { - UidCache cache1 = newCache(); - UidCache cache2 = newCache(); +@ExtendWith(MockitoExtension.class) +class UidCacheTest { + UidCache sut; - Result result1 = cache1.put("test"); - Result result2 = cache2.put("test"); + @Mock + Function uidFunction; - assertTrue(result1.isNewValue()); - assertTrue(result2.isNewValue()); - assertArrayEquals(result1.getId(), result2.getId()); + @BeforeEach + void setUp() { + sut = new UidCache(1024, uidFunction); + + when(uidFunction.apply(any())) + .thenReturn(new byte[]{}); } @Test - public void differentValue() { - UidCache cache = newCache(); + void sameValue() { + Result result1 = sut.put("test"); + Result result2 = sut.put("test"); + + assertTrue(result1.isNewValue()); + assertFalse(result2.isNewValue()); - Result result1 = cache.put("test"); - Result result2 = cache.put("different"); + verify(uidFunction, times(1)).apply("test"); + } + + @Test + void differentValue() { + Result result1 = sut.put("test"); + Result result2 = sut.put("different"); assertTrue(result1.isNewValue()); assertTrue(result2.isNewValue()); - assertFalse(Arrays.equals(result1.getId(), result2.getId())); - } - private UidCache newCache() { - return new UidCache(1024); + verify(uidFunction, times(1)).apply("test"); + verify(uidFunction, times(1)).apply("different"); } } 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 6b103c4fe897..5a27d3b6c419 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 @@ -29,7 +29,7 @@ public class SimpleCachingSqlNormalizerTest { @Test public void testNormalizedSql() { SimpleCache cache = newCache(1); - CachingSqlNormalizer> normalizer = new DefaultCachingSqlNormalizer<>(cache); + SimpleCachingSqlNormalizer normalizer = new SimpleCachingSqlNormalizer(cache); ParsingResultInternal parsingResult = new DefaultParsingResult("select * from dual"); boolean newCache = normalizer.normalizedSql(parsingResult); @@ -47,7 +47,7 @@ public void testNormalizedSql() { @Test public void testNormalizedSql_cache_expire() { SimpleCache cache = newCache(1); - CachingSqlNormalizer> normalizer = new DefaultCachingSqlNormalizer<>(cache); + SimpleCachingSqlNormalizer normalizer = new SimpleCachingSqlNormalizer(cache); ParsingResultInternal parsingResult = new DefaultParsingResult("select * from table1"); boolean newCache = normalizer.normalizedSql(parsingResult); Assertions.assertTrue(newCache, "newCacheState"); 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 a324acb77507..daad6867d258 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 @@ -36,7 +36,7 @@ public class SqlCacheServiceTest { public void cacheSql() { final EnhancedDataSender dataSender = mock(EnhancedDataSender.class); SimpleCache sqlCache = new SimpleCache<>(new IdAllocator.ZigZagAllocator(), 100); - CachingSqlNormalizer> simpleCachingSqlNormalizer = new DefaultCachingSqlNormalizer<>(sqlCache); + SimpleCachingSqlNormalizer simpleCachingSqlNormalizer = new SimpleCachingSqlNormalizer(sqlCache); final SqlCacheService sqlMetaDataService = new SqlCacheService<>(dataSender, simpleCachingSqlNormalizer); final String sql = "select * from A"; diff --git a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/metadata/SqlUidMetaDataServiceTest.java b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/metadata/SqlUidMetaDataServiceTest.java index 7b83e52e2dc3..740b2adf0b87 100644 --- a/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/metadata/SqlUidMetaDataServiceTest.java +++ b/agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/metadata/SqlUidMetaDataServiceTest.java @@ -2,7 +2,6 @@ import com.navercorp.pinpoint.common.profiler.message.EnhancedDataSender; import com.navercorp.pinpoint.io.ResponseMessage; -import com.navercorp.pinpoint.profiler.cache.UidCache; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -19,18 +18,20 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoMoreInteractions; -public class SqlUidMetaDataServiceTest { - private SqlUidMetaDataService sut; +class SqlUidMetaDataServiceTest { + SqlUidMetaDataService sut; @Mock - private EnhancedDataSender dataSender; + EnhancedDataSender dataSender; AutoCloseable autoCloseable; - private SqlCacheService sqlCacheService; + SqlCacheService sqlCacheService; + + static int LENGTH_LIMIT = 100; @BeforeEach - public void setUp() { + void setUp() { autoCloseable = MockitoAnnotations.openMocks(this); this.sqlCacheService = newSqlCacheService(); @@ -38,19 +39,18 @@ public void setUp() { sut = new SqlUidMetaDataService(sqlCacheService); } - private SqlCacheService newSqlCacheService() { - UidCache uidCache = new UidCache(100); - CachingSqlNormalizer> simpleCachingSqlNormalizer = new DefaultCachingSqlNormalizer<>(uidCache); + SqlCacheService newSqlCacheService() { + UidCachingSqlNormalizer simpleCachingSqlNormalizer = new UidCachingSqlNormalizer(100, LENGTH_LIMIT); return new SqlCacheService<>(dataSender, simpleCachingSqlNormalizer); } @AfterEach - public void tearDown() throws Exception { + void tearDown() throws Exception { autoCloseable.close(); } @Test - public void sendDataOnce() { + void sendDataOnce() { String sql = "select * from A"; UidParsingResult parsingResult = (UidParsingResult) sut.wrapSqlResult(sql); @@ -63,7 +63,7 @@ public void sendDataOnce() { } @Test - public void sameSql() { + void sameSql() { String sql = "select * from A"; UidParsingResult parsingResult1 = (UidParsingResult) sut.wrapSqlResult(sql); @@ -77,7 +77,7 @@ public void sameSql() { } @Test - public void sameSql_clearCache() { + void sameSql_clearCache() { String sql = "select * from A"; UidParsingResult parsingResult1 = (UidParsingResult) sut.wrapSqlResult(sql); @@ -92,7 +92,7 @@ public void sameSql_clearCache() { } @Test - public void differentSql() { + void differentSql() { String sql1 = "select * from A"; String sql2 = "select * from B"; @@ -104,4 +104,25 @@ public void differentSql() { assertFalse(Arrays.equals(parsingResult1.getId(), parsingResult2.getId())); } + + @Test + void bypassCache() { + String veryLongSql = veryLongSql(); + + UidParsingResult parsingResult1 = (UidParsingResult) sut.wrapSqlResult(veryLongSql); + UidParsingResult parsingResult2 = (UidParsingResult) sut.wrapSqlResult(veryLongSql); + + assertTrue(sqlCacheService.cacheSql(parsingResult1, SqlUidMetaDataService::newSqlUidMetaData)); + assertTrue(sqlCacheService.cacheSql(parsingResult2, SqlUidMetaDataService::newSqlUidMetaData)); + + assertArrayEquals(parsingResult1.getId(), parsingResult2.getId()); + } + + String veryLongSql() { + StringBuilder a = new StringBuilder(); + for (int i = 0; i < LENGTH_LIMIT + 1; i++) { + a.append("a"); + } + return a.toString(); + } } \ No newline at end of file