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

[fix][ml] Fix thread safe issue with RangeCache.put and RangeCache.clear #21302

Merged
merged 1 commit into from
Oct 7, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.commons.lang3.mutable.MutableBoolean;
import org.apache.commons.lang3.tuple.Pair;

/**
Expand Down Expand Up @@ -74,13 +73,18 @@ public RangeCache(Weighter<Value> weighter, TimestampExtractor<Value> timestampE
* @return whether the entry was inserted in the cache
*/
public boolean put(Key key, Value value) {
MutableBoolean flag = new MutableBoolean();
entries.computeIfAbsent(key, (k) -> {
size.addAndGet(weighter.getSize(value));
flag.setValue(true);
return value;
});
return flag.booleanValue();
// retain value so that it's not released before we put it in the cache and calculate the weight
value.retain();
try {
if (entries.putIfAbsent(key, value) == null) {
size.addAndGet(weighter.getSize(value));
return true;
} else {
return false;
}
} finally {
value.release();
}
}

public boolean exists(Key key) {
Expand Down Expand Up @@ -242,7 +246,6 @@ public synchronized Pair<Integer, Long> clear() {
value.release();
}

entries.clear();
size.getAndAdd(-removedSize);
return Pair.of(removedCount, removedSize);
}
Expand Down