Skip to content

Commit 4928f00

Browse files
author
Peter Alfonsi
committed
address sagar's comments
Signed-off-by: Peter Alfonsi <petealft@amazon.com>
1 parent 8994de6 commit 4928f00

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

server/src/main/java/org/opensearch/indices/IndicesService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1877,7 +1877,7 @@ public void run() {
18771877
logger.trace("running periodic field data cache cleanup");
18781878
}
18791879
try {
1880-
this.cache.clearMarkedKeys();
1880+
this.cache.clear();
18811881
} catch (Exception e) {
18821882
logger.warn("Exception during periodic field data cache cleanup:", e);
18831883
}

server/src/main/java/org/opensearch/indices/fielddata/cache/IndicesFieldDataCache.java

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import java.io.IOException;
6464
import java.util.ArrayList;
6565
import java.util.Collections;
66+
import java.util.HashMap;
6667
import java.util.Iterator;
6768
import java.util.List;
6869
import java.util.Map;
@@ -154,23 +155,37 @@ public void clear(Index index, String field) {
154155
fieldsOfIndex.add(field);
155156
}
156157

157-
// This method can stay synchronized to avoid having multiple simultaneous cache iterators removing keys.
158-
// This shouldn't cause performance impact as it's called only from the scheduled cache clear thread
159-
// and is not blocking search traffic.
160-
public void clearMarkedKeys() {
158+
// The synchronized block is to avoid having multiple simultaneous cache iterators removing keys.
159+
public void clear() {
161160
if (!(indicesToClear.isEmpty() && fieldsToClear.isEmpty())) {
161+
// Copy marked indices/fields before iteration, and only remove keys matching the copies
162+
// in case new entries are marked for cleanup mid-iteration
163+
Set<Index> indicesToClearCopy = Set.copyOf(indicesToClear);
164+
Map<Index, Set<String>> fieldsToClearCopy = new HashMap<>();
165+
for (Map.Entry<Index, Set<String>> entry : fieldsToClear.entrySet()) {
166+
fieldsToClearCopy.put(entry.getKey(), Set.copyOf(entry.getValue()));
167+
}
168+
// remove this way instead of clearing all, in case a new entry has been marked since copying
169+
indicesToClear.removeAll(indicesToClearCopy);
170+
for (Map.Entry<Index, Set<String>> entry : fieldsToClearCopy.entrySet()) {
171+
Set<String> fieldsForIndex = fieldsToClear.get(entry.getKey());
172+
if (fieldsForIndex != null) {
173+
fieldsForIndex.removeAll(entry.getValue());
174+
}
175+
}
176+
162177
synchronized (this) {
163178
for (Iterator<Key> iterator = getCache().keys().iterator(); iterator.hasNext();) {
164179
Key key = iterator.next();
165-
if (indicesToClear.contains(key.indexCache.index)) {
180+
if (indicesToClearCopy.contains(key.indexCache.index)) {
166181
try {
167182
iterator.remove();
168183
} catch (Exception e) {
169184
logger.warn("Exception occurred while removing key from cache", e);
170185
}
171186
continue;
172187
}
173-
Set<String> fieldsOfIndexToClear = fieldsToClear.get(key.indexCache.index);
188+
Set<String> fieldsOfIndexToClear = fieldsToClearCopy.get(key.indexCache.index);
174189
if (fieldsOfIndexToClear != null && fieldsOfIndexToClear.contains(key.indexCache.fieldName)) {
175190
try {
176191
iterator.remove();
@@ -180,8 +195,6 @@ public void clearMarkedKeys() {
180195
}
181196
}
182197
}
183-
indicesToClear.clear();
184-
fieldsToClear.clear();
185198
}
186199
cache.refresh();
187200
}

server/src/test/java/org/opensearch/index/fielddata/IndexFieldDataServiceTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -544,7 +544,7 @@ public void remove() {
544544
// Clear the cache for both fields of the index, we expect 1 key will remain afterwards
545545
ifdService.clear();
546546
// Manually run the cache's clear keys loop
547-
fdCacheSpy.clearMarkedKeys();
547+
fdCacheSpy.clear();
548548
assertEquals(1, internalCacheSpy.count());
549549

550550
reader.close();

0 commit comments

Comments
 (0)