From 75ddbc6c72da605cf4c69402a8df96a86e7a2f36 Mon Sep 17 00:00:00 2001 From: Shashi Madappa Date: Mon, 19 Oct 2020 11:11:43 -0700 Subject: [PATCH 1/2] Fixed a bug with hashedkey where we were dueting it by default. --- .../java/com/netflix/evcache/EVCacheKey.java | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/evcache-core/src/main/java/com/netflix/evcache/EVCacheKey.java b/evcache-core/src/main/java/com/netflix/evcache/EVCacheKey.java index 83b3ffd0..648bfcee 100644 --- a/evcache-core/src/main/java/com/netflix/evcache/EVCacheKey.java +++ b/evcache-core/src/main/java/com/netflix/evcache/EVCacheKey.java @@ -1,12 +1,17 @@ package com.netflix.evcache; +import java.util.HashMap; +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import com.netflix.archaius.api.Property; import com.netflix.evcache.util.KeyHasher; import com.netflix.evcache.util.KeyHasher.HashingAlgorithm; -import java.util.HashMap; -import java.util.Map; public class EVCacheKey { + private static final Logger log = LoggerFactory.getLogger(EVCacheKey.class); private final String appName; private final HashingAlgorithm hashingAlgorithmAtAppLevel; private final Property shouldEncodeHashKeyAtAppLevel; @@ -58,6 +63,7 @@ private String getCanonicalKeyForDuet() { if (null == canonicalKeyForDuet) { final int duetKeyLength = appName.length() + 1 + canonicalKey.length(); canonicalKeyForDuet = new StringBuilder(duetKeyLength).append(appName).append(':').append(canonicalKey).toString(); + if (log.isDebugEnabled()) log.debug("canonicalKeyForDuet : " + canonicalKeyForDuet); } return canonicalKeyForDuet; @@ -94,7 +100,9 @@ public String getHashKey(boolean isDuet, HashingAlgorithm hashingAlgorithm, Bool baseEnoder = encoder; } - return isDuet ? getHashKeyForDuet(hashingAlgorithm, shouldEncodeHashKey, maxDigestBytes, maxHashLength, baseEnoder) : getHashKey(hashingAlgorithm, shouldEncodeHashKey, maxDigestBytes, maxHashLength, baseEnoder); + final String rKey = isDuet ? getHashKeyForDuet(hashingAlgorithm, shouldEncodeHashKey, maxDigestBytes, maxHashLength, baseEnoder) : getHashKey(hashingAlgorithm, shouldEncodeHashKey, maxDigestBytes, maxHashLength, baseEnoder); + if (log.isDebugEnabled()) log.debug("Key : " + rKey); + return rKey; } // overlays app level hashing algorithm and client level hashing algorithm @@ -104,7 +112,9 @@ public String getDerivedKey(boolean isDuet, HashingAlgorithm hashingAlgorithm, B hashingAlgorithm = hashingAlgorithmAtAppLevel; } - return null == hashingAlgorithm || hashingAlgorithm == HashingAlgorithm.NO_HASHING ? getCanonicalKey(isDuet) : getHashKey(isDuet, hashingAlgorithm, shouldEncodeHashKey, maxDigestBytes, maxHashLength, baseEnoder); + final String derivedKey = null == hashingAlgorithm || hashingAlgorithm == HashingAlgorithm.NO_HASHING ? getCanonicalKey(isDuet) : getHashKey(isDuet, hashingAlgorithm, shouldEncodeHashKey, maxDigestBytes, maxHashLength, baseEnoder); + if (log.isDebugEnabled()) log.debug("derivedKey : " + derivedKey); + return derivedKey; } private String getHashKey(HashingAlgorithm hashingAlgorithm, Boolean shouldEncodeHashKey, Integer maxDigestBytes, Integer maxHashLength, String encoder) { @@ -115,9 +125,10 @@ private String getHashKey(HashingAlgorithm hashingAlgorithm, Boolean shouldEncod final String key = hashingAlgorithm.toString()+ maxDigestBytes != null ? maxDigestBytes.toString() : "-" + maxHashLength != null ? maxHashLength.toString() : "-" + encoder != null ? encoder : "-"; String val = hashedKeysByAlgorithm.get(key); if(val == null) { - val = KeyHasher.getHashedKeyEncoded(getCanonicalKeyForDuet(), hashingAlgorithm, maxDigestBytes, maxHashLength, encoder); + val = KeyHasher.getHashedKeyEncoded(canonicalKey, hashingAlgorithm, maxDigestBytes, maxHashLength, encoder); hashedKeysByAlgorithm.put(key , val); } + if (log.isDebugEnabled()) log.debug("getHashKey : " + val); // TODO: Once the issue around passing hashedKey in bytes[] is figured, we will start using (nullable) shouldEncodeHashKey, and call KeyHasher.getHashedKeyInBytes() accordingly return val; } @@ -133,6 +144,7 @@ private String getHashKeyForDuet(HashingAlgorithm hashingAlgorithm, Boolean shou val = KeyHasher.getHashedKeyEncoded(getCanonicalKeyForDuet(), hashingAlgorithm, maxDigestBytes, maxHashLength, encoder); hashedKeysByAlgorithmForDuet.put(key , val); } + if (log.isDebugEnabled()) log.debug("getHashKeyForDuet : " + val); // TODO: Once the issue around passing hashedKey in bytes[] is figured, we will start using (nullable) shouldEncodeHashKey, and call KeyHasher.getHashedKeyInBytes() accordingly return val; } From b6f86e1bfef938c95355d6702294a1253f14250f Mon Sep 17 00:00:00 2001 From: Shashi Madappa Date: Mon, 19 Oct 2020 11:21:10 -0700 Subject: [PATCH 2/2] Changed canonicalKey ref from variable to method --- evcache-core/src/main/java/com/netflix/evcache/EVCacheKey.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evcache-core/src/main/java/com/netflix/evcache/EVCacheKey.java b/evcache-core/src/main/java/com/netflix/evcache/EVCacheKey.java index 648bfcee..e8744f0f 100644 --- a/evcache-core/src/main/java/com/netflix/evcache/EVCacheKey.java +++ b/evcache-core/src/main/java/com/netflix/evcache/EVCacheKey.java @@ -125,7 +125,7 @@ private String getHashKey(HashingAlgorithm hashingAlgorithm, Boolean shouldEncod final String key = hashingAlgorithm.toString()+ maxDigestBytes != null ? maxDigestBytes.toString() : "-" + maxHashLength != null ? maxHashLength.toString() : "-" + encoder != null ? encoder : "-"; String val = hashedKeysByAlgorithm.get(key); if(val == null) { - val = KeyHasher.getHashedKeyEncoded(canonicalKey, hashingAlgorithm, maxDigestBytes, maxHashLength, encoder); + val = KeyHasher.getHashedKeyEncoded(getCanonicalKey(false), hashingAlgorithm, maxDigestBytes, maxHashLength, encoder); hashedKeysByAlgorithm.put(key , val); } if (log.isDebugEnabled()) log.debug("getHashKey : " + val);