-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#11166] Bypass cache for long SQL queries
- Loading branch information
Showing
12 changed files
with
145 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...filer/src/main/java/com/navercorp/pinpoint/profiler/metadata/UidCachingSqlNormalizer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
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<ParsingResultInternal<byte[]>> { | ||
private final Logger logger = LogManager.getLogger(this.getClass()); | ||
private final Function<String, byte[]> hashFunction = x -> Hashing.murmur3_128().hashString(x, StandardCharsets.UTF_8).asBytes(); | ||
|
||
private final Cache<String, Result<byte[]>> 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<byte[]> 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 (originalSql.length() > lengthLimit) { | ||
uid = hashFunction.apply(normalizedSql.getNormalizedSql()); | ||
isNewValue = true; | ||
} else { | ||
final Result<byte[]> cachingResult = this.sqlCache.put(normalizedSql.getNormalizedSql()); | ||
uid = cachingResult.getId(); | ||
isNewValue = cachingResult.isNewValue(); | ||
} | ||
|
||
setParsingResult(parsingResult, uid, normalizedSql); | ||
return isNewValue; | ||
} | ||
|
||
private void setParsingResult(ParsingResultInternal<byte[]> 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()); | ||
} | ||
} |
55 changes: 35 additions & 20 deletions
55
agent-module/profiler/src/test/java/com/navercorp/pinpoint/profiler/cache/UidCacheTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<byte[]> result1 = cache1.put("test"); | ||
Result<byte[]> result2 = cache2.put("test"); | ||
@Mock | ||
Function<String, byte[]> 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<byte[]> result1 = sut.put("test"); | ||
Result<byte[]> result2 = sut.put("test"); | ||
|
||
assertTrue(result1.isNewValue()); | ||
assertFalse(result2.isNewValue()); | ||
|
||
Result<byte[]> result1 = cache.put("test"); | ||
Result<byte[]> result2 = cache.put("different"); | ||
verify(uidFunction, times(1)).apply("test"); | ||
} | ||
|
||
@Test | ||
void differentValue() { | ||
Result<byte[]> result1 = sut.put("test"); | ||
Result<byte[]> 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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.