Skip to content
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

close oldest indexes before opening new if over capacity #5095

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
close oldest indexes before opening new if over capacity
  • Loading branch information
rnewson committed Jun 19, 2024
commit 58f4d960399ee49abccca3e3f40c0982245e7ecd
Original file line number Diff line number Diff line change
Expand Up @@ -56,28 +56,8 @@ public final class IndexManager implements Managed {

private class LRUMap extends LinkedHashMap<String, IndexHolder> {

private int capacity;

private LRUMap(final int capacity) {
super(capacity, 0.75f, true);
this.capacity = capacity;
}

@Override
protected boolean removeEldestEntry(final java.util.Map.Entry<String, IndexHolder> eldest) {
if (size() > capacity) {
schedulerExecutorService.execute(() -> {
try {
unload(eldest.getKey(), false);
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
LOGGER.warn("Interrupted while evicting eldest entry {}", eldest.getKey(), e);
} catch (final IOException e) {
LOGGER.warn("I/O exception while evicting eldest entry {}", eldest.getKey(), e);
}
});
}
return false;
}
}

Expand Down Expand Up @@ -124,11 +104,14 @@ private static class IndexHolder {

public <R> R with(final String name, final IndexFunction<Index, R> indexFun)
throws IOException, InterruptedException {
evictIfOverCapacity();

retry:
while (true) {
if (!exists(name)) {
throw new WebApplicationException("Index does not exist", Status.NOT_FOUND);
}

final IndexHolder holder;
synchronized (cache) {
holder = cache.computeIfAbsent(name, (k) -> new IndexHolder());
Expand Down Expand Up @@ -172,6 +155,19 @@ public <R> R with(final String name, final IndexFunction<Index, R> indexFun)
}
}

private void evictIfOverCapacity() throws IOException, InterruptedException {
while (true) {
final String candidate;
synchronized (cache) {
if (cache.size() <= maxIndexesOpen) {
return;
}
candidate = cache.entrySet().iterator().next().getKey();
}
unload(candidate, false);
}
}

public void unload(final String name, final boolean forceDelete) throws IOException, InterruptedException {
final IndexHolder holder;
synchronized (cache) {
Expand Down