-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<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; | ||
Check warning on line 33 in agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/UidCachingSqlNormalizer.java Codecov / codecov/patchagent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/UidCachingSqlNormalizer.java#L33
|
||
} | ||
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 || originalSql.length() <= lengthLimit) { | ||
final Result<byte[]> cachingResult = this.sqlCache.put(normalizedSql.getNormalizedSql()); | ||
uid = cachingResult.getId(); | ||
isNewValue = cachingResult.isNewValue(); | ||
} else { | ||
// bypass cache | ||
uid = hashFunction.apply(normalizedSql.getNormalizedSql()); | ||
isNewValue = true; | ||
Check warning on line 52 in agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/UidCachingSqlNormalizer.java Codecov / codecov/patchagent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/UidCachingSqlNormalizer.java#L51-L52
|
||
} | ||
|
||
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); | ||
Check warning on line 63 in agent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/UidCachingSqlNormalizer.java Codecov / codecov/patchagent-module/profiler/src/main/java/com/navercorp/pinpoint/profiler/metadata/UidCachingSqlNormalizer.java#L63
|
||
} | ||
} | ||
parsingResult.setSql(normalizedSql.getNormalizedSql()); | ||
parsingResult.setOutput(normalizedSql.getParseParameter()); | ||
} | ||
} |
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"); | ||
} | ||
} |