Browse Source

Create a sha-256 hash of the shard request cache key (#74877)

We currently use the plaintext body of a shard request as the key to the 
request cache.  This has the disadvantage that very large requests can
quickly fill up the cache due to the size of their keys.  With this commit, 
we instead use a sha-256 hash of the shard request as the cache key, 
which will use a constant (and much smaller) number of bytes.
Johan Nilsson Hansen 4 years ago
parent
commit
553e8dcb07

+ 1 - 1
docs/reference/modules/indices/request_cache.asciidoc

@@ -105,7 +105,7 @@ query-string parameter detailed here.
 [discrete]
 ==== Cache key
 
-The whole JSON body is used as the cache key. This means that if the JSON
+A hash of the whole JSON body is used as the cache key. This means that if the JSON
 changes -- for instance if keys are output in a different order -- then the
 cache key will not be recognised.
 

+ 3 - 2
server/src/main/java/org/elasticsearch/search/internal/ShardSearchRequest.java

@@ -21,8 +21,10 @@ import org.elasticsearch.common.CheckedBiConsumer;
 import org.elasticsearch.core.CheckedFunction;
 import org.elasticsearch.core.Nullable;
 import org.elasticsearch.common.Strings;
+import org.elasticsearch.common.bytes.BytesArray;
 import org.elasticsearch.common.bytes.BytesReference;
 import org.elasticsearch.common.collect.ImmutableOpenMap;
+import org.elasticsearch.common.hash.MessageDigests;
 import org.elasticsearch.common.io.stream.BytesStreamOutput;
 import org.elasticsearch.common.io.stream.StreamInput;
 import org.elasticsearch.common.io.stream.StreamOutput;
@@ -404,8 +406,7 @@ public class ShardSearchRequest extends TransportRequest implements IndicesReque
             if (differentiator != null) {
                 differentiator.accept(this, out);
             }
-            // copy it over since we don't want to share the thread-local bytes in #scratch
-            return out.copyBytes();
+            return new BytesArray(MessageDigests.digest(out.bytes(), MessageDigests.sha256()));
         } finally {
             out.reset();
         }