Skip to content

Commit

Permalink
Merge pull request #5095 from apache/nouveau-lru-max
Browse files Browse the repository at this point in the history
close oldest indexes before opening new if over capacity
  • Loading branch information
rnewson authored Jun 19, 2024
2 parents 031e762 + 58f4d96 commit bc8de17
Showing 1 changed file with 16 additions and 20 deletions.
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

0 comments on commit bc8de17

Please sign in to comment.