You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@bookkeeper.apache.org by "poorbarcode (via GitHub)" <gi...@apache.org> on 2026/01/14 05:02:32 UTC

[PR] [fix]Fix entry loss due to incorrect lock of LedgerHandle [bookkeeper]

poorbarcode opened a new pull request, #4701:
URL: https://github.com/apache/bookkeeper/pull/4701

   
   ### Motivation
   
   **Background**
   - Ledger states management
     - In BK server, ledgers are managed by `HandleFactoryImpl`, which maintains the ledger state.
     - When fencing entries, BK change the state of the Ledger Handle to `fenced`
     - When adding entries, BK checks fencing state first, then apply writing.
   - Open ledger with recovery
     - BK fence ledger first, then read LAC to ensure no entries missed 
   - Threads design
     - Opening ledger with recovery works on the thread `BookieHighPriorityThread`
     - Adding entries works on the thread `BookieWriteThreadPool`
   
   To avoid the following multi-threading competition issue,BK uses a lock<sup>[1]</sup>
   
   | `(BookieWriteThreadPool)`Add entry | `(BookieHighPriorityThread)`Opening ledger with recovery |
   | --- | --- |
   | get `LedgerDescriptor` from `HandleFactoryImpl` |
   | check fenced status: `non-fenced` |
   | | get `LedgerDescriptor` from `HandleFactoryImpl` |
   | | fence ledger |
   | | read Lac from write cache |
   | add entry into write cache |
   | add entry into Journal Disk |
   | success |
   | Lac is `1` | Lac is `0` |
   
   - **[1]** Both fencing ledger and adding entry acquire the lock `synchronized(LedgerDescriptor.this)`
   - **[1-1]** https://github.com/apache/bookkeeper/blob/release-4.17.2/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/BookieImpl.java#L1098-L1105
   ```java
   public void addEntry(ByteBuf entry, boolean ackBeforeSync, WriteCallback cb, Object ctx, byte[] masterKey)
               throws IOException, BookieException, InterruptedException {
           try {
               LedgerDescriptor handle = getLedgerForEntry(entry, masterKey);
               synchronized (handle) {
                   if (handle.isFenced()) {
                       throw BookieException
                               .create(BookieException.Code.LedgerFencedException);
                   }
                   addEntryInternal(handle, entry, ackBeforeSync, cb, ctx, masterKey);
               }
           } 
   }
   ```
   
   - **[1-2]** https://github.com/apache/bookkeeper/blob/release-4.17.2/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/LedgerDescriptorImpl.java#L92-L93
   
   ```java
   synchronized CompletableFuture<Boolean> fenceAndLogInJournal(Journal journal) throws IOException {
       boolean success = this.setFenced();
       return logFenceEntryInJournal(journal);
   }
   ```
   
   ---
   
   Issue: there is a race condition breaks the lock above
   
   https://github.com/apache/bookkeeper/blob/release-4.17.2/bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/HandleFactoryImpl.java#L54-L68
   
   ```java
       public LedgerDescriptor getHandle(final long ledgerId, final byte[] masterKey, boolean journalReplay)
               throws IOException, BookieException {
           LedgerDescriptor handle = ledgers.get(ledgerId);
   
           if (handle == null) {
               handle = LedgerDescriptor.create(masterKey, ledgerId, ledgerStorage);
               ledgers.putIfAbsent(ledgerId, handle);
           }
   
           handle.checkAccess(masterKey);
           return handle;
       }
   ```
   
   | `(BookieWriteThreadPool)`Add entry | `(BookieHighPriorityThread)`Opening ledger with recovery |
   | --- | --- |
   | get `LedgerDescriptor` from `HandleFactoryImpl` |
   | check if created: `false` | check if created: `false` |
   | creates a new one | creates a new one |
   
   
   ### Changes
   
   Fix the bug
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@bookkeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [fix]Fix entry loss due to incorrect lock of LedgerHandle [bookkeeper]

Posted by "poorbarcode (via GitHub)" <gi...@apache.org>.
poorbarcode commented on code in PR #4701:
URL: https://github.com/apache/bookkeeper/pull/4701#discussion_r2696683839


##########
bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/HandleFactoryImpl.java:
##########
@@ -53,16 +53,18 @@ class HandleFactoryImpl implements HandleFactory, LedgerDeletionListener {
     @Override
     public LedgerDescriptor getHandle(final long ledgerId, final byte[] masterKey, boolean journalReplay)
             throws IOException, BookieException {
-        LedgerDescriptor handle = ledgers.get(ledgerId);
-
-        if (handle == null) {
-            if (!journalReplay && recentlyFencedAndDeletedLedgers.getIfPresent(ledgerId) != null) {
-                throw BookieException.create(BookieException.Code.LedgerFencedAndDeletedException);
+        if (!ledgers.containsKey(ledgerId)) {
+            synchronized (ledgers) {
+                if (!ledgers.containsKey(ledgerId)) {
+                    if (!journalReplay && recentlyFencedAndDeletedLedgers.getIfPresent(ledgerId) != null) {
+                        throw BookieException.create(BookieException.Code.LedgerFencedAndDeletedException);
+                    }
+                    LedgerDescriptor handle = LedgerDescriptor.create(masterKey, ledgerId, ledgerStorage);
+                    ledgers.putIfAbsent(ledgerId, handle);
+                }
             }
-            handle = LedgerDescriptor.create(masterKey, ledgerId, ledgerStorage);
-            ledgers.putIfAbsent(ledgerId, handle);

Review Comment:
   Improved, please review again



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@bookkeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [fix]Fix entry loss due to incorrect lock of LedgerHandle [bookkeeper]

Posted by "zymap (via GitHub)" <gi...@apache.org>.
zymap merged PR #4701:
URL: https://github.com/apache/bookkeeper/pull/4701


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@bookkeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [fix]Fix entry loss due to incorrect lock of LedgerHandle [bookkeeper]

Posted by "zymap (via GitHub)" <gi...@apache.org>.
zymap commented on code in PR #4701:
URL: https://github.com/apache/bookkeeper/pull/4701#discussion_r2693641807


##########
bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/HandleFactoryImpl.java:
##########
@@ -53,16 +53,18 @@ class HandleFactoryImpl implements HandleFactory, LedgerDeletionListener {
     @Override
     public LedgerDescriptor getHandle(final long ledgerId, final byte[] masterKey, boolean journalReplay)
             throws IOException, BookieException {
-        LedgerDescriptor handle = ledgers.get(ledgerId);
-
-        if (handle == null) {
-            if (!journalReplay && recentlyFencedAndDeletedLedgers.getIfPresent(ledgerId) != null) {
-                throw BookieException.create(BookieException.Code.LedgerFencedAndDeletedException);
+        if (!ledgers.containsKey(ledgerId)) {
+            synchronized (ledgers) {
+                if (!ledgers.containsKey(ledgerId)) {
+                    if (!journalReplay && recentlyFencedAndDeletedLedgers.getIfPresent(ledgerId) != null) {
+                        throw BookieException.create(BookieException.Code.LedgerFencedAndDeletedException);
+                    }
+                    LedgerDescriptor handle = LedgerDescriptor.create(masterKey, ledgerId, ledgerStorage);
+                    ledgers.putIfAbsent(ledgerId, handle);
+                }
             }
-            handle = LedgerDescriptor.create(masterKey, ledgerId, ledgerStorage);
-            ledgers.putIfAbsent(ledgerId, handle);

Review Comment:
   Looks like we can avoid the synchronization by handling the result of the putIfAbsent? I understand the main issue is that here we create two LedgerDescriptor, then used by different threads. But the putIfAbsent will reduce one. So we can handle the result to ensure the same handle is returned?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@bookkeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [fix]Fix entry loss due to incorrect lock of LedgerHandle [bookkeeper]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #4701:
URL: https://github.com/apache/bookkeeper/pull/4701#discussion_r2693668080


##########
bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/HandleFactoryImpl.java:
##########
@@ -53,16 +53,18 @@ class HandleFactoryImpl implements HandleFactory, LedgerDeletionListener {
     @Override
     public LedgerDescriptor getHandle(final long ledgerId, final byte[] masterKey, boolean journalReplay)
             throws IOException, BookieException {
-        LedgerDescriptor handle = ledgers.get(ledgerId);
-
-        if (handle == null) {
-            if (!journalReplay && recentlyFencedAndDeletedLedgers.getIfPresent(ledgerId) != null) {
-                throw BookieException.create(BookieException.Code.LedgerFencedAndDeletedException);
+        if (!ledgers.containsKey(ledgerId)) {
+            synchronized (ledgers) {
+                if (!ledgers.containsKey(ledgerId)) {
+                    if (!journalReplay && recentlyFencedAndDeletedLedgers.getIfPresent(ledgerId) != null) {
+                        throw BookieException.create(BookieException.Code.LedgerFencedAndDeletedException);
+                    }
+                    LedgerDescriptor handle = LedgerDescriptor.create(masterKey, ledgerId, ledgerStorage);
+                    ledgers.putIfAbsent(ledgerId, handle);

Review Comment:
   The `putIfAbsent` on line 63 is unnecessary and potentially wasteful. Since we're already inside a synchronized block with a double-check, the key is guaranteed not to exist. Use `put()` instead of `putIfAbsent()` to avoid the redundant check and potential creation of unused LedgerDescriptor instances.
   ```suggestion
                       ledgers.put(ledgerId, handle);
   ```



##########
bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/HandleFactoryImpl.java:
##########
@@ -53,16 +53,18 @@ class HandleFactoryImpl implements HandleFactory, LedgerDeletionListener {
     @Override
     public LedgerDescriptor getHandle(final long ledgerId, final byte[] masterKey, boolean journalReplay)
             throws IOException, BookieException {
-        LedgerDescriptor handle = ledgers.get(ledgerId);
-
-        if (handle == null) {
-            if (!journalReplay && recentlyFencedAndDeletedLedgers.getIfPresent(ledgerId) != null) {
-                throw BookieException.create(BookieException.Code.LedgerFencedAndDeletedException);
+        if (!ledgers.containsKey(ledgerId)) {
+            synchronized (ledgers) {
+                if (!ledgers.containsKey(ledgerId)) {
+                    if (!journalReplay && recentlyFencedAndDeletedLedgers.getIfPresent(ledgerId) != null) {
+                        throw BookieException.create(BookieException.Code.LedgerFencedAndDeletedException);
+                    }
+                    LedgerDescriptor handle = LedgerDescriptor.create(masterKey, ledgerId, ledgerStorage);
+                    ledgers.putIfAbsent(ledgerId, handle);
+                }
             }
-            handle = LedgerDescriptor.create(masterKey, ledgerId, ledgerStorage);
-            ledgers.putIfAbsent(ledgerId, handle);
         }
-
+        LedgerDescriptor handle = ledgers.get(ledgerId);

Review Comment:
   Potential null pointer exception: After the synchronized block exits, another thread could remove the ledger (via `ledgerDeleted`) before line 67 executes, causing `handle` to be null and resulting in a NullPointerException at line 68. Add a null check after line 67.
   ```suggestion
           LedgerDescriptor handle = ledgers.get(ledgerId);
           if (handle == null) {
               synchronized (ledgers) {
                   handle = ledgers.get(ledgerId);
                   if (handle == null) {
                       if (!journalReplay && recentlyFencedAndDeletedLedgers.getIfPresent(ledgerId) != null) {
                           throw BookieException.create(BookieException.Code.LedgerFencedAndDeletedException);
                       }
                       handle = LedgerDescriptor.create(masterKey, ledgerId, ledgerStorage);
                       ledgers.putIfAbsent(ledgerId, handle);
                   }
               }
           }
   ```



##########
bookkeeper-server/src/main/java/org/apache/bookkeeper/bookie/HandleFactoryImpl.java:
##########
@@ -53,16 +53,18 @@ class HandleFactoryImpl implements HandleFactory, LedgerDeletionListener {
     @Override
     public LedgerDescriptor getHandle(final long ledgerId, final byte[] masterKey, boolean journalReplay)
             throws IOException, BookieException {
-        LedgerDescriptor handle = ledgers.get(ledgerId);
-
-        if (handle == null) {
-            if (!journalReplay && recentlyFencedAndDeletedLedgers.getIfPresent(ledgerId) != null) {
-                throw BookieException.create(BookieException.Code.LedgerFencedAndDeletedException);
+        if (!ledgers.containsKey(ledgerId)) {

Review Comment:
   The `containsKey` method in ConcurrentLongHashMap internally calls `get(key) != null` (line 221 of ConcurrentLongHashMap.java). This means we're doing two lookups in the fast path - one for `containsKey` and another for `get` at line 67. Consider using `computeIfAbsent` instead of the double-checked locking pattern for better performance and cleaner code.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@bookkeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [fix]Fix entry loss due to incorrect lock of LedgerHandle [bookkeeper]

Posted by "hangc0276 (via GitHub)" <gi...@apache.org>.
hangc0276 closed pull request #4701: [fix]Fix entry loss due to incorrect lock of LedgerHandle
URL: https://github.com/apache/bookkeeper/pull/4701


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@bookkeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [fix]Fix entry loss due to incorrect lock of LedgerHandle [bookkeeper]

Posted by "poorbarcode (via GitHub)" <gi...@apache.org>.
poorbarcode commented on PR #4701:
URL: https://github.com/apache/bookkeeper/pull/4701#issuecomment-3766836155

   rerun failure checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@bookkeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [fix]Fix entry loss due to incorrect lock of LedgerHandle [bookkeeper]

Posted by "poorbarcode (via GitHub)" <gi...@apache.org>.
poorbarcode commented on PR #4701:
URL: https://github.com/apache/bookkeeper/pull/4701#issuecomment-3767174142

   rerun failure checks


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@bookkeeper.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org