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

[improve][ml] RangeCache refactoring follow-up: use StampedLock instead of synchronized #22818

Merged
merged 1 commit into from
Jun 3, 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
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.util.concurrent.ConcurrentNavigableMap;
import java.util.concurrent.ConcurrentSkipListMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.StampedLock;
import lombok.extern.slf4j.Slf4j;
import org.apache.bookkeeper.mledger.util.RangeCache.ValueWithKeyValidation;
import org.apache.commons.lang3.tuple.Pair;
Expand Down Expand Up @@ -75,6 +76,7 @@ protected EntryWrapper newObject(Handle<EntryWrapper> recyclerHandle) {
return new EntryWrapper(recyclerHandle);
}
};
private final StampedLock lock = new StampedLock();
private K key;
private V value;
long size;
Expand All @@ -85,27 +87,50 @@ private EntryWrapper(Handle<EntryWrapper> recyclerHandle) {

static <K, V> EntryWrapper<K, V> create(K key, V value, long size) {
EntryWrapper<K, V> entryWrapper = RECYCLER.get();
synchronized (entryWrapper) {
entryWrapper.key = key;
entryWrapper.value = value;
entryWrapper.size = size;
}
long stamp = entryWrapper.lock.writeLock();
entryWrapper.key = key;
entryWrapper.value = value;
entryWrapper.size = size;
entryWrapper.lock.unlockWrite(stamp);
return entryWrapper;
}

synchronized K getKey() {
return key;
K getKey() {
long stamp = lock.tryOptimisticRead();
K localKey = key;
if (!lock.validate(stamp)) {
stamp = lock.readLock();
localKey = key;
lock.unlockRead(stamp);
}
return localKey;
}

synchronized V getValue(K key) {
if (this.key != key) {
V getValue(K key) {
long stamp = lock.tryOptimisticRead();
K localKey = this.key;
V localValue = this.value;
if (!lock.validate(stamp)) {
stamp = lock.readLock();
localKey = this.key;
localValue = this.value;
lock.unlockRead(stamp);
}
if (localKey != key) {
return null;
}
return value;
return localValue;
}

synchronized long getSize() {
return size;
long getSize() {
long stamp = lock.tryOptimisticRead();
long localSize = size;
if (!lock.validate(stamp)) {
stamp = lock.readLock();
localSize = size;
lock.unlockRead(stamp);
}
return localSize;
}

void recycle() {
Expand Down
Loading