Skip to content

Commit 15a5b49

Browse files
SingleDirectoryDbLedgerStorage skip optimistic cache put sometimes (#4306)
* SingleDirectoryDbLedgerStorage skip optimistic cache put When tryOptimisticRead returns 0, the lock is acquired by another thread and we will fail the validation step. We can therefore skip the eager cache insertion.
1 parent 3c5123d commit 15a5b49

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/storage/ldb/SingleDirectoryDbLedgerStorage.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,12 @@ public long addEntry(ByteBuf entry) throws IOException, BookieException {
484484
long stamp = writeCacheRotationLock.tryOptimisticRead();
485485
boolean inserted = false;
486486

487-
inserted = writeCache.put(ledgerId, entryId, entry);
488-
if (!writeCacheRotationLock.validate(stamp)) {
487+
// If the stamp is 0, the lock was exclusively acquired, validation will fail, and we can skip this put.
488+
if (stamp != 0) {
489+
inserted = writeCache.put(ledgerId, entryId, entry);
490+
}
491+
492+
if (stamp == 0 || !writeCacheRotationLock.validate(stamp)) {
489493
// The write cache was rotated while we were inserting. We need to acquire the proper read lock and repeat
490494
// the operation because we might have inserted in a write cache that was already being flushed and cleared,
491495
// without being sure about this last entry being flushed or not.

0 commit comments

Comments
 (0)