Skip to content

Commit cb22cb0

Browse files
authored
Check CircuitBreaker before put/compute in FileCache to avoid entry removal (#18661)
Signed-off-by: Shreyansh Ray <[email protected]>
1 parent 6f9e1f8 commit cb22cb0

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

server/src/main/java/org/opensearch/index/store/remote/filecache/FileCache.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public long capacity() {
6969

7070
@Override
7171
public CachedIndexInput put(Path filePath, CachedIndexInput indexInput) {
72+
checkParentBreaker();
7273
CachedIndexInput cachedIndexInput = theCache.put(filePath, indexInput);
73-
checkParentBreaker(filePath);
7474
return cachedIndexInput;
7575
}
7676

@@ -79,8 +79,8 @@ public CachedIndexInput compute(
7979
Path key,
8080
BiFunction<? super Path, ? super CachedIndexInput, ? extends CachedIndexInput> remappingFunction
8181
) {
82+
checkParentBreaker();
8283
CachedIndexInput cachedIndexInput = theCache.compute(key, remappingFunction);
83-
checkParentBreaker(key);
8484
return cachedIndexInput;
8585
}
8686

@@ -201,13 +201,11 @@ public void closeIndexInputReferences() {
201201

202202
/**
203203
* Ensures that the PARENT breaker is not tripped when an entry is added to the cache
204-
* @param filePath the path key for which entry is added
205204
*/
206-
private void checkParentBreaker(Path filePath) {
205+
private void checkParentBreaker() {
207206
try {
208207
circuitBreaker.addEstimateBytesAndMaybeBreak(0, "filecache_entry");
209208
} catch (CircuitBreakingException ex) {
210-
theCache.remove(filePath);
211209
throw new CircuitBreakingException(
212210
"Unable to create file cache entries",
213211
ex.getBytesWanted(),

server/src/test/java/org/opensearch/index/store/remote/filecache/FileCacheTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ private FileCache createFileCache(long capacity) {
5050
return FileCacheFactory.createConcurrentLRUFileCache(capacity, CONCURRENCY_LEVEL, new NoopCircuitBreaker(CircuitBreaker.REQUEST));
5151
}
5252

53+
private FileCache createFileCache(long capacity, CircuitBreaker circuitBreaker) {
54+
return FileCacheFactory.createConcurrentLRUFileCache(capacity, CONCURRENCY_LEVEL, circuitBreaker);
55+
}
56+
5357
private FileCache createCircuitBreakingFileCache(long capacity) {
5458
TestCircuitBreaker testCircuitBreaker = new TestCircuitBreaker();
5559
testCircuitBreaker.startBreaking();
@@ -204,6 +208,20 @@ public void testComputeThrowCircuitBreakingException() {
204208
assertNull(fileCache.get(path));
205209
}
206210

211+
public void testEntryNotRemovedCircuitBreaker() {
212+
TestCircuitBreaker circuitBreaker = new TestCircuitBreaker();
213+
FileCache fileCache = createFileCache(MEGA_BYTES, circuitBreaker);
214+
Path path = createPath("0");
215+
fileCache.put(path, new StubCachedIndexInput(8 * MEGA_BYTES));
216+
// put should succeed since circuit breaker hasn't tripped yet
217+
assertEquals(fileCache.get(path).length(), 8 * MEGA_BYTES);
218+
circuitBreaker.startBreaking();
219+
// compute should throw CircuitBreakingException but shouldn't remove entry already present
220+
assertThrows(CircuitBreakingException.class, () -> fileCache.compute(path, (p, i) -> new StubCachedIndexInput(2 * MEGA_BYTES)));
221+
assertNotNull(fileCache.get(path));
222+
assertEquals(fileCache.get(path).length(), 8 * MEGA_BYTES);
223+
}
224+
207225
public void testRemove() {
208226
FileCache fileCache = createFileCache(MEGA_BYTES);
209227
for (int i = 0; i < 4; i++) {

0 commit comments

Comments
 (0)