-
Notifications
You must be signed in to change notification settings - Fork 25.8k
Allow fast blob-cache introspection by shard-id #138282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
henningandersen
merged 8 commits into
elastic:main
from
henningandersen:blob_cache_by_shard
Dec 3, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
850bc61
Allow fast blob-cache introspection by shard-id
henningandersen 15fd0f1
Update docs/changelog/138282.yaml
henningandersen 544a784
Fix race-condition and make key mapping testable separately.
henningandersen 7515470
Add comment for getFreq.
henningandersen 8f2f947
Fix force evict by shard (and add assert)
henningandersen 768e037
Merge branch 'main' into blob_cache_by_shard
henningandersen fe1c2f9
Fix race condition
henningandersen daddec8
Merge branch 'main' into blob_cache_by_shard
henningandersen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,5 @@ | ||
| pr: 138282 | ||
| summary: Allow fast blob-cache introspection by shard-id | ||
| area: Searchable Snapshots | ||
| type: enhancement | ||
| issues: [] |
78 changes: 78 additions & 0 deletions
78
x-pack/plugin/blob-cache/src/main/java/org/elasticsearch/blobcache/shared/KeyMapping.java
This file contains hidden or 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,78 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.blobcache.shared; | ||
|
|
||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.function.BiConsumer; | ||
| import java.util.function.Function; | ||
|
|
||
| /** | ||
| * A 2 layer key mapping for the shared cache. | ||
| * @param <Key1> The outer layer key type | ||
| * @param <Key2> The inner key type | ||
| * @param <Value> The value type | ||
| */ | ||
| class KeyMapping<Key1, Key2, Value> { | ||
| private final ConcurrentHashMap<Key1, ConcurrentHashMap<Key2, Value>> mapping = new ConcurrentHashMap<>(); | ||
|
|
||
| public Value get(Key1 key1, Key2 key2) { | ||
| ConcurrentHashMap<Key2, Value> inner = mapping.get(key1); | ||
| if (inner != null) { | ||
| return inner.get(key2); | ||
| } else { | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Compute a key if absent. Notice that unlike CHM#computeIfAbsent, locking will be done also when present | ||
| * @param key1 The key1 part | ||
| * @param key2 The key2 part | ||
| * @param function the function to get from key2 to the value | ||
| * @return the resulting value. | ||
| */ | ||
| public Value computeIfAbsent(Key1 key1, Key2 key2, Function<Key2, Value> function) { | ||
| AtomicReference<Value> result = new AtomicReference<>(); | ||
| mapping.compute(key1, (k, current) -> { | ||
| ConcurrentHashMap<Key2, Value> map = current == null ? new ConcurrentHashMap<>() : current; | ||
| result.setPlain(map.computeIfAbsent(key2, function)); | ||
| return map; | ||
| }); | ||
| return result.getPlain(); | ||
| } | ||
|
|
||
| public boolean remove(Key1 key1, Key2 key2, Value value) { | ||
| ConcurrentHashMap<Key2, Value> inner = mapping.get(key1); | ||
| if (inner != null) { | ||
| boolean removed = inner.remove(key2, value); | ||
| if (removed && inner.isEmpty()) { | ||
| mapping.computeIfPresent(key1, (k, v) -> v.isEmpty() ? null : v); | ||
| } | ||
| return removed; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| Iterable<Key1> key1s() { | ||
| return mapping.keySet(); | ||
| } | ||
|
|
||
| void forEach(Key1 key1, BiConsumer<Key2, Value> consumer) { | ||
| ConcurrentHashMap<Key2, Value> map = mapping.get(key1); | ||
| if (map != null) { | ||
| map.forEach(consumer); | ||
| } | ||
| } | ||
|
|
||
| void forEach(BiConsumer<Key2, Value> consumer) { | ||
| for (ConcurrentHashMap<Key2, Value> map : mapping.values()) { | ||
| map.forEach(consumer); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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
113 changes: 113 additions & 0 deletions
113
...k/plugin/blob-cache/src/test/java/org/elasticsearch/blobcache/shared/KeyMappingTests.java
This file contains hidden or 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,113 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the Elastic License | ||
| * 2.0; you may not use this file except in compliance with the Elastic License | ||
| * 2.0. | ||
| */ | ||
|
|
||
| package org.elasticsearch.blobcache.shared; | ||
|
|
||
| import org.elasticsearch.test.ESTestCase; | ||
|
|
||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| public class KeyMappingTests extends ESTestCase { | ||
|
|
||
| public void testBasics() { | ||
| final String k1 = randomAlphanumericOfLength(10); | ||
| final String k2 = randomAlphanumericOfLength(10); | ||
| final String value = randomAlphanumericOfLength(10); | ||
| KeyMapping<String, String, String> mapping = new KeyMapping<>(); | ||
| assertNull(mapping.get(k1, k2)); | ||
|
|
||
| assertEquals(value, mapping.computeIfAbsent(k1, k2, (kx) -> value)); | ||
| assertEquals(value, mapping.get(k1, k2)); | ||
|
|
||
| mapping.computeIfAbsent(k1, k2, (kx) -> { throw new AssertionError(); }); | ||
|
|
||
| assertEquals(value, mapping.get(k1, k2)); | ||
|
|
||
| final String k12 = randomValueOtherThan(k1, () -> randomAlphanumericOfLength(10)); | ||
| mapping.computeIfAbsent(k12, k2, (kx) -> randomAlphanumericOfLength(10)); | ||
|
|
||
| assertEquals(value, mapping.get(k1, k2)); | ||
|
|
||
| assertEquals(Set.of(k1, k12), mapping.key1s()); | ||
|
|
||
| Set<String> values = new HashSet<>(); | ||
| mapping.forEach(k1, (ak2, result) -> { assertTrue(values.add(result)); }); | ||
| assertEquals(Set.of(value), values); | ||
|
|
||
| assertTrue(mapping.remove(k1, k2, value)); | ||
|
|
||
| assertEquals(Set.of(k12), mapping.key1s()); | ||
|
|
||
| assertNull(mapping.get(k1, k2)); | ||
| assertNotNull(mapping.get(k12, k2)); | ||
|
|
||
| assertFalse(mapping.remove(k1, k2, value)); | ||
| } | ||
|
|
||
| public void testMultiThreaded() { | ||
| final String k1 = randomAlphanumericOfLength(10); | ||
| KeyMapping<String, String, Integer> mapping = new KeyMapping<>(); | ||
|
|
||
| List<Thread> threads = IntStream.range(0, 10).mapToObj(i -> new Thread(() -> { | ||
| final String k2 = Integer.toString(i); | ||
| logger.info(k2); | ||
|
|
||
| for (int j = 0; j < 1000; ++j) { | ||
| Integer finalJ = j; | ||
| assertNull(mapping.get(k1, k2)); | ||
| assertSame(finalJ, mapping.computeIfAbsent(k1, k2, (kx) -> finalJ)); | ||
| assertEquals(finalJ, mapping.get(k1, k2)); | ||
| assertTrue(mapping.remove(k1, k2, finalJ)); | ||
| if ((j & 1) == 0) { | ||
| assertFalse(mapping.remove(k1, k2, finalJ)); | ||
| } | ||
|
|
||
| } | ||
| assertNull(mapping.get(k1, k2)); | ||
| }, "test-thread-" + i)).toList(); | ||
|
|
||
| threads.forEach(Thread::start); | ||
| threads.forEach(t -> { | ||
| try { | ||
| t.join(10000); | ||
| } catch (InterruptedException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| }); | ||
|
|
||
| assertEquals(Set.of(), mapping.key1s()); | ||
| } | ||
|
|
||
| public void testMultiThreadedSameKey() { | ||
| final String k1 = randomAlphanumericOfLength(10); | ||
| KeyMapping<String, String, Integer> mapping = new KeyMapping<>(); | ||
|
|
||
| List<Thread> threads = IntStream.range(0, 10).mapToObj(i -> new Thread(() -> { | ||
| for (int j = 0; j < 1000; ++j) { | ||
| Integer computeValue = i * 1000 + j; | ||
| Integer value = mapping.computeIfAbsent(k1, k1, (kx) -> computeValue); | ||
| assertNotNull(value); | ||
| // either our value or another threads value. | ||
| assertTrue(value == computeValue || value / 1000 != i); | ||
| mapping.remove(k1, k1, value); | ||
| } | ||
| })).toList(); | ||
| threads.forEach(Thread::start); | ||
| threads.forEach(t -> { | ||
| try { | ||
| t.join(10000); | ||
| } catch (InterruptedException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| }); | ||
|
|
||
| assertEquals(Set.of(), mapping.key1s()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we make this private?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cannot, it is used in
forceEvicthere (an unchanged line).