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

Clear thread local values on UTF8TaxonomyWriterCache.close() #12013

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.BytesRefBuilder;
import org.apache.lucene.util.BytesRefHash;
import org.apache.lucene.util.CloseableThreadLocal;
import org.apache.lucene.util.Counter;
import org.apache.lucene.util.RamUsageEstimator;
import org.apache.lucene.util.UnicodeUtil;

/** A "cache" that never frees memory, and stores labels in a BytesRefHash (utf-8 encoding). */
public final class UTF8TaxonomyWriterCache implements TaxonomyWriterCache, Accountable {
private final ThreadLocal<BytesRefBuilder> bytes =
new ThreadLocal<BytesRefBuilder>() {
private final CloseableThreadLocal<BytesRefBuilder> bytes =
new CloseableThreadLocal<>() {
@Override
protected BytesRefBuilder initialValue() {
return new BytesRefBuilder();
Expand Down Expand Up @@ -153,7 +154,10 @@ public synchronized long ramBytesUsed() {
}

@Override
public void close() {}
public void close() {
map.clear();
this.bytes.close();
}

private static final byte DELIM_CHAR = (byte) 0x1F;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,16 @@ public void testRandom() throws Exception {
}
}

public void testCacheClose() {
UTF8TaxonomyWriterCache cache = new UTF8TaxonomyWriterCache();
FacetLabel testLabel = new FacetLabel("test/label");
int testOrd = 7;
cache.put(testLabel, testOrd);
assertEquals(testOrd, cache.get(testLabel));
cache.close();
assertThrows(NullPointerException.class, () -> cache.get(testLabel));
}

private static class LabelToOrdinalMap extends LabelToOrdinal {
private Map<FacetLabel, Integer> map = new HashMap<>();

Expand Down