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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to