Skip to content

Commit 770d2e0

Browse files
Put project ID args first, as Niels suggested
1 parent 7798133 commit 770d2e0

File tree

3 files changed

+25
-24
lines changed

3 files changed

+25
-24
lines changed

x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichCache.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,24 @@ private Cache<CacheKey, CacheValue> createCache(long maxWeight, ToLongBiFunction
8484
* This method notifies the given listener of the value in this cache for the given search parameters. If there is no value in the cache
8585
* for these search parameters, then the new cache value is computed using searchResponseFetcher.
8686
*
87-
* @param enrichIndex The enrich index from which the results will be retrieved
88-
* @param lookupValue The value that will be used in the search
89-
* @param maxMatches The max number of matches that the search will return
87+
* @param projectId The ID of the project
88+
* @param enrichIndex The enrich index from which the results will be retrieved
89+
* @param lookupValue The value that will be used in the search
90+
* @param maxMatches The max number of matches that the search will return
9091
* @param searchResponseFetcher The function used to compute the value to be put in the cache, if there is no value in the cache already
91-
* @param listener A listener to be notified of the value in the cache
92+
* @param listener A listener to be notified of the value in the cache
9293
*/
9394
public void computeIfAbsent(
94-
String enrichIndex,
9595
ProjectId projectId,
96+
String enrichIndex,
9697
Object lookupValue,
9798
int maxMatches,
9899
Consumer<ActionListener<SearchResponse>> searchResponseFetcher,
99100
ActionListener<List<Map<?, ?>>> listener
100101
) {
101102
// intentionally non-locking for simplicity...it's OK if we re-put the same key/value in the cache during a race condition.
102103
long cacheStart = relativeNanoTimeProvider.getAsLong();
103-
var cacheKey = new CacheKey(enrichIndex, projectId, lookupValue, maxMatches);
104+
var cacheKey = new CacheKey(projectId, enrichIndex, lookupValue, maxMatches);
104105
List<Map<?, ?>> response = get(cacheKey);
105106
long cacheRequestTime = relativeNanoTimeProvider.getAsLong() - cacheStart;
106107
if (response != null) {
@@ -200,11 +201,11 @@ private static Object innerDeepCopy(Object value, boolean unmodifiable) {
200201
*
201202
* @param enrichIndex The enrich <i>index</i> (i.e. not the alias, but the concrete index that the alias points to)
202203
* @param lookupValue The value that is used to find matches in the enrich index
203-
* @param maxMatches The max number of matches that the enrich lookup should return. This changes the size of the search response and
204-
* should thus be included in the cache key
204+
* @param maxMatches The max number of matches that the enrich lookup should return. This changes the size of the search response and
205+
* should thus be included in the cache key
205206
*/
206207
// Visibility for testing
207-
record CacheKey(String enrichIndex, ProjectId projectId, Object lookupValue, int maxMatches) {
208+
record CacheKey(ProjectId projectId, String enrichIndex, Object lookupValue, int maxMatches) {
208209
/**
209210
* In reality, the size in bytes of the cache key is a function of the {@link CacheKey#lookupValue} field plus some constant for
210211
* the object itself, the string reference for the enrich index (but not the string itself because it's taken from the metadata),

x-pack/plugin/enrich/src/main/java/org/elasticsearch/xpack/enrich/EnrichProcessorFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ private SearchRunner createSearchRunner(ProjectMetadata project, String indexAli
138138
return (value, maxMatches, reqSupplier, handler) -> {
139139
// intentionally non-locking for simplicity...it's OK if we re-put the same key/value in the cache during a race condition.
140140
enrichCache.computeIfAbsent(
141-
getEnrichIndexKey(project, indexAlias),
142141
project.id(),
142+
getEnrichIndexKey(project, indexAlias),
143143
value,
144144
maxMatches,
145145
(searchResponseActionListener) -> originClient.execute(

x-pack/plugin/enrich/src/test/java/org/elasticsearch/xpack/enrich/EnrichCacheTests.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ public void testCaching() {
4040
// Emulated search requests that an enrich processor could generate:
4141
// (two unique searches for two enrich policies)
4242
var projectId = randomProjectIdOrDefault();
43-
var cacheKey1 = new EnrichCache.CacheKey("policy1-1", projectId, "1", 1);
44-
var cacheKey2 = new EnrichCache.CacheKey("policy1-1", projectId, "2", 1);
45-
var cacheKey3 = new EnrichCache.CacheKey("policy2-1", projectId, "1", 1);
46-
var cacheKey4 = new EnrichCache.CacheKey("policy2-1", projectId, "2", 1);
43+
var cacheKey1 = new EnrichCache.CacheKey(projectId, "policy1-1", "1", 1);
44+
var cacheKey2 = new EnrichCache.CacheKey(projectId, "policy1-1", "2", 1);
45+
var cacheKey3 = new EnrichCache.CacheKey(projectId, "policy2-1", "1", 1);
46+
var cacheKey4 = new EnrichCache.CacheKey(projectId, "policy2-1", "2", 1);
4747
// Emulated search response (content doesn't matter, since it isn't used, it just a cache entry)
4848
EnrichCache.CacheValue searchResponse = new EnrichCache.CacheValue(List.of(Map.of("test", "entry")), 1L);
4949

@@ -77,10 +77,10 @@ public void testCaching() {
7777
assertThat(cacheStats.evictions(), equalTo(1L));
7878
assertThat(cacheStats.cacheSizeInBytes(), equalTo(3L));
7979

80-
cacheKey1 = new EnrichCache.CacheKey("policy1-2", projectId, "1", 1);
81-
cacheKey2 = new EnrichCache.CacheKey("policy1-2", projectId, "2", 1);
82-
cacheKey3 = new EnrichCache.CacheKey("policy2-2", projectId, "1", 1);
83-
cacheKey4 = new EnrichCache.CacheKey("policy2-2", projectId, "2", 1);
80+
cacheKey1 = new EnrichCache.CacheKey(projectId, "policy1-2", "1", 1);
81+
cacheKey2 = new EnrichCache.CacheKey(projectId, "policy1-2", "2", 1);
82+
cacheKey3 = new EnrichCache.CacheKey(projectId, "policy2-2", "1", 1);
83+
cacheKey4 = new EnrichCache.CacheKey(projectId, "policy2-2", "2", 1);
8484

8585
// Because enrich index has changed, cache can't serve cached entries
8686
assertThat(enrichCache.get(cacheKey1), nullValue());
@@ -123,7 +123,7 @@ public void testComputeIfAbsent() throws InterruptedException {
123123
// Do initial computeIfAbsent, assert that it is a cache miss and the search is performed:
124124
CountDownLatch queriedDatabaseLatch = new CountDownLatch(1);
125125
CountDownLatch notifiedOfResultLatch = new CountDownLatch(1);
126-
enrichCache.computeIfAbsent("policy1-1", projectId, "1", 1, (searchResponseActionListener) -> {
126+
enrichCache.computeIfAbsent(projectId, "policy1-1", "1", 1, (searchResponseActionListener) -> {
127127
SearchResponse searchResponse = convertToSearchResponse(searchResponseMap);
128128
searchResponseActionListener.onResponse(searchResponse);
129129
searchResponse.decRef();
@@ -146,7 +146,7 @@ public void testComputeIfAbsent() throws InterruptedException {
146146
{
147147
// Do the same call, assert that it is a cache hit and no search is performed:
148148
CountDownLatch notifiedOfResultLatch = new CountDownLatch(1);
149-
enrichCache.computeIfAbsent("policy1-1", projectId, "1", 1, (searchResponseActionListener) -> {
149+
enrichCache.computeIfAbsent(projectId, "policy1-1", "1", 1, (searchResponseActionListener) -> {
150150
fail("Expected no call to the database because item should have been in the cache");
151151
}, assertNoFailureListener(r -> notifiedOfResultLatch.countDown()));
152152
assertThat(notifiedOfResultLatch.await(5, TimeUnit.SECONDS), equalTo(true));
@@ -163,7 +163,7 @@ public void testComputeIfAbsent() throws InterruptedException {
163163
// Do a computeIfAbsent with a different index, assert that it is a cache miss and the search is performed:
164164
CountDownLatch queriedDatabaseLatch = new CountDownLatch(1);
165165
CountDownLatch notifiedOfResultLatch = new CountDownLatch(1);
166-
enrichCache.computeIfAbsent("policy1-2", projectId, "1", 1, (searchResponseActionListener) -> {
166+
enrichCache.computeIfAbsent(projectId, "policy1-2", "1", 1, (searchResponseActionListener) -> {
167167
SearchResponse searchResponse = convertToSearchResponse(searchResponseMap);
168168
searchResponseActionListener.onResponse(searchResponse);
169169
searchResponse.decRef();
@@ -182,7 +182,7 @@ public void testComputeIfAbsent() throws InterruptedException {
182182
// Do a computeIfAbsent with a different project, assert that it is a cache miss and the search is performed:
183183
CountDownLatch queriedDatabaseLatch = new CountDownLatch(1);
184184
CountDownLatch notifiedOfResultLatch = new CountDownLatch(1);
185-
enrichCache.computeIfAbsent("policy1-1", randomUniqueProjectId(), "1", 1, (searchResponseActionListener) -> {
185+
enrichCache.computeIfAbsent(randomUniqueProjectId(), "policy1-1", "1", 1, (searchResponseActionListener) -> {
186186
SearchResponse searchResponse = convertToSearchResponse(searchResponseMap);
187187
searchResponseActionListener.onResponse(searchResponse);
188188
searchResponse.decRef();
@@ -201,7 +201,7 @@ public void testComputeIfAbsent() throws InterruptedException {
201201
// Do a computeIfAbsent with a different lookup value, assert that it is a cache miss and the search is performed:
202202
CountDownLatch queriedDatabaseLatch = new CountDownLatch(1);
203203
CountDownLatch notifiedOfResultLatch = new CountDownLatch(1);
204-
enrichCache.computeIfAbsent("policy1-1", projectId, "2", 1, (searchResponseActionListener) -> {
204+
enrichCache.computeIfAbsent(projectId, "policy1-1", "2", 1, (searchResponseActionListener) -> {
205205
SearchResponse searchResponse = convertToSearchResponse(searchResponseMap);
206206
searchResponseActionListener.onResponse(searchResponse);
207207
searchResponse.decRef();
@@ -220,7 +220,7 @@ public void testComputeIfAbsent() throws InterruptedException {
220220
// Do a computeIfAbsent with a different max matches, assert that it is a cache miss and the search is performed:
221221
CountDownLatch queriedDatabaseLatch = new CountDownLatch(1);
222222
CountDownLatch notifiedOfResultLatch = new CountDownLatch(1);
223-
enrichCache.computeIfAbsent("policy1-1", projectId, "1", 3, (searchResponseActionListener) -> {
223+
enrichCache.computeIfAbsent(projectId, "policy1-1", "1", 3, (searchResponseActionListener) -> {
224224
SearchResponse searchResponse = convertToSearchResponse(searchResponseMap);
225225
searchResponseActionListener.onResponse(searchResponse);
226226
searchResponse.decRef();

0 commit comments

Comments
 (0)