Skip to content

Commit

Permalink
LedgerHandle: eliminate unnecessasary synchronization on LedgerHandle…
Browse files Browse the repository at this point in the history
….getLength()
  • Loading branch information
eolivelli committed Oct 19, 2024
1 parent f148f63 commit 376b9dd
Showing 1 changed file with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import org.apache.bookkeeper.client.AsyncCallback.AddCallback;
import org.apache.bookkeeper.client.AsyncCallback.AddCallbackWithLatency;
import org.apache.bookkeeper.client.AsyncCallback.CloseCallback;
Expand Down Expand Up @@ -137,7 +138,7 @@ private enum HandleState {
*/
private int stickyBookieIndex;

long length;
final AtomicLong length;
final DigestManager macManager;
final DistributionSchedule distributionSchedule;
final RateLimiter throttler;
Expand Down Expand Up @@ -188,10 +189,10 @@ private enum HandleState {
LedgerMetadata metadata = versionedMetadata.getValue();
if (metadata.isClosed()) {
lastAddConfirmed = lastAddPushed = metadata.getLastEntryId();
length = metadata.getLength();
length = new AtomicLong(metadata.getLength());
} else {
lastAddConfirmed = lastAddPushed = INVALID_ENTRY_ID;
length = 0;
length = new AtomicLong();
}

this.pendingAddsSequenceHead = lastAddConfirmed;
Expand Down Expand Up @@ -365,7 +366,7 @@ boolean setLedgerMetadata(Versioned<LedgerMetadata> expected, Versioned<LedgerMe
LedgerMetadata metadata = versionedMetadata.getValue();
if (metadata.isClosed()) {
lastAddConfirmed = lastAddPushed = metadata.getLastEntryId();
length = metadata.getLength();
length.set(metadata.getLength());
}
return true;
} else {
Expand Down Expand Up @@ -422,9 +423,8 @@ DigestManager getDigestManager() {
* @param delta
* @return the length of the ledger after the addition
*/
synchronized long addToLength(long delta) {
this.length += delta;
return this.length;
long addToLength(long delta) {
return length.addAndGet(delta);
}

/**
Expand All @@ -433,8 +433,8 @@ synchronized long addToLength(long delta) {
* @return the length of the ledger in bytes
*/
@Override
public synchronized long getLength() {
return this.length;
public long getLength() {
return this.length.get();
}

/**
Expand Down Expand Up @@ -559,7 +559,7 @@ void doAsyncCloseInternal(final CloseCallback cb, final Object ctx, final int rc

// taking the length must occur after draining, as draining changes the length
lastEntry = lastAddPushed = LedgerHandle.this.lastAddConfirmed;
finalLength = LedgerHandle.this.length;
finalLength = LedgerHandle.this.length.get();
handleState = HandleState.CLOSED;
}

Expand Down Expand Up @@ -1649,7 +1649,7 @@ synchronized void updateLastConfirmed(long lac, long len) {
lacUpdateMissesCounter.inc();
}
lastAddPushed = Math.max(lastAddPushed, lac);
length = Math.max(length, len);
length.accumulateAndGet(len, (current, value) -> Math.max(current, value));
}

/**
Expand Down Expand Up @@ -1985,7 +1985,7 @@ public void asyncReadExplicitLastConfirmed(final ReadLastConfirmedCallback cb, f
isClosed = metadata.isClosed();
if (isClosed) {
lastAddConfirmed = metadata.getLastEntryId();
length = metadata.getLength();
length.set(metadata.getLength());
}
}
if (isClosed) {
Expand Down

0 comments on commit 376b9dd

Please sign in to comment.