@@ -53,18 +53,20 @@ class HandleFactoryImpl implements HandleFactory, LedgerDeletionListener {
5353 @ Override
5454 public LedgerDescriptor getHandle (final long ledgerId , final byte [] masterKey , boolean journalReplay )
5555 throws IOException , BookieException {
56- if (!ledgers .containsKey (ledgerId )) {
57- synchronized (ledgers ) {
58- if (!ledgers .containsKey (ledgerId )) {
59- if (!journalReplay && recentlyFencedAndDeletedLedgers .getIfPresent (ledgerId ) != null ) {
60- throw BookieException .create (BookieException .Code .LedgerFencedAndDeletedException );
61- }
62- LedgerDescriptor handle = LedgerDescriptor .create (masterKey , ledgerId , ledgerStorage );
63- ledgers .putIfAbsent (ledgerId , handle );
64- }
56+ LedgerDescriptor handle = ledgers .get (ledgerId );
57+
58+ if (handle == null ) {
59+ if (!journalReplay && recentlyFencedAndDeletedLedgers .getIfPresent (ledgerId ) != null ) {
60+ throw BookieException .create (BookieException .Code .LedgerFencedAndDeletedException );
61+ }
62+ handle = LedgerDescriptor .create (masterKey , ledgerId , ledgerStorage );
63+ LedgerDescriptor handlePut = ledgers .putIfAbsent (ledgerId , handle );
64+ // If it has been modified by other thread, use the previous one.
65+ if (handlePut != handle ) {
66+ return getHandle (ledgerId , masterKey , journalReplay );
6567 }
6668 }
67- LedgerDescriptor handle = ledgers . get ( ledgerId );
69+
6870 handle .checkAccess (masterKey );
6971 return handle ;
7072 }
0 commit comments