@@ -206,7 +206,7 @@ static final class WaitingRead { |
206 | 206 | @VisibleForTesting |
207 | 207 | volatile LedgerHandle cursorLedger; |
208 | 208 |
|
209 | | - // Wether the current cursorLedger is read-only or writable |
| 209 | + // Guard updates to the handle and its read-only flag with the cursor monitor. |
210 | 210 | private boolean isCursorLedgerReadOnly = true; |
211 | 211 | private boolean ledgerForceRecovery; |
212 | 212 |
|
@@ -685,6 +685,7 @@ protected void recoverFromLedger(final ManagedCursorInfo info, final VoidCallbac |
685 | 685 | if (lastEntryInLedger < 0) { |
686 | 686 | log.warn("[{}] Error reading from metadata ledger {} for cursor {}: No entries in ledger", |
687 | 687 | ledger.getName(), ledgerId, name); |
| 688 | + ManagedLedgerImpl.closeReadHandleAsync(lh, log); |
688 | 689 | // Rewind to last cursor snapshot available |
689 | 690 | initialize(getRollbackPosition(info), rollbackProperties, cursorProperties, callback); |
690 | 691 | return; |
@@ -695,41 +696,47 @@ protected void recoverFromLedger(final ManagedCursorInfo info, final VoidCallbac |
695 | 696 | log.debug("[{}} readComplete rc={} entryId={}", ledger.getName(), rc1, lh1.getLastAddConfirmed()); |
696 | 697 | } |
697 | 698 | if (isBkErrorNotRecoverable(rc1) || (rc1 != BKException.Code.OK && ledgerForceRecovery)) { |
| 699 | + ManagedLedgerImpl.closeReadHandleAsync(lh, log); |
698 | 700 | log.error("[{}] Error reading from metadata ledger {} for cursor {}: {}", ledger.getName(), |
699 | 701 | ledgerId, name, BKException.getMessage(rc1)); |
700 | 702 | // Rewind to the oldest entry available |
701 | 703 | initialize(getRollbackPosition(info), rollbackProperties, cursorProperties, callback); |
702 | 704 | return; |
703 | 705 | } else if (rc1 != BKException.Code.OK) { |
| 706 | + ManagedLedgerImpl.closeReadHandleAsync(lh, log); |
704 | 707 | log.warn("[{}] Error reading from metadata ledger {} for cursor {}: {}", ledger.getName(), |
705 | 708 | ledgerId, name, BKException.getMessage(rc1)); |
706 | 709 |
|
707 | 710 | callback.operationFailed(createManagedLedgerException(rc1)); |
708 | 711 | return; |
709 | 712 | } |
710 | 713 |
|
711 | | - LedgerEntry entry = seq.nextElement(); |
712 | | - mbean.addReadCursorLedgerSize(entry.getLength()); |
713 | | - PositionInfo positionInfo = new PositionInfo(); |
714 | 714 | try { |
| 715 | + LedgerEntry entry = seq.nextElement(); |
| 716 | + mbean.addReadCursorLedgerSize(entry.getLength()); |
| 717 | + PositionInfo positionInfo = new PositionInfo(); |
715 | 718 | positionInfo.parseFrom(entry.getEntry()); |
| 719 | + |
| 720 | + Map<String, Long> recoveredProperties = |
| 721 | + recoverProperties(positionInfo.getPropertiesCount(), positionInfo::getPropertyAt); |
| 722 | + |
| 723 | + Position position = PositionFactory.create(positionInfo.getLedgerId(), positionInfo.getEntryId()); |
| 724 | + recoverIndividualDeletedMessages(positionInfo); |
| 725 | + if (getConfig().isDeletionAtBatchIndexLevelEnabled() |
| 726 | + && positionInfo.getBatchedEntryDeletionIndexInfosCount() > 0) { |
| 727 | + recoverBatchDeletedIndexes(positionInfo.getBatchedEntryDeletionIndexInfosCount(), |
| 728 | + positionInfo::getBatchedEntryDeletionIndexInfoAt); |
| 729 | + } |
| 730 | + recoverLastActive(positionInfo.getIdleSince()); |
| 731 | + recoveredCursor(position, recoveredProperties, cursorProperties, lh); |
716 | 732 | } catch (Exception e) { |
| 733 | + // Recovery owns the handle until recoveredCursor transfers it to the cursor. |
| 734 | + if (cursorLedger != lh) { |
| 735 | + ManagedLedgerImpl.closeReadHandleAsync(lh, log); |
| 736 | + } |
717 | 737 | callback.operationFailed(new ManagedLedgerException(e)); |
718 | 738 | return; |
719 | 739 | } |
720 | | - |
721 | | - Map<String, Long> recoveredProperties = |
722 | | - recoverProperties(positionInfo.getPropertiesCount(), positionInfo::getPropertyAt); |
723 | | - |
724 | | - Position position = PositionFactory.create(positionInfo.getLedgerId(), positionInfo.getEntryId()); |
725 | | - recoverIndividualDeletedMessages(positionInfo); |
726 | | - if (getConfig().isDeletionAtBatchIndexLevelEnabled() |
727 | | - && positionInfo.getBatchedEntryDeletionIndexInfosCount() > 0) { |
728 | | - recoverBatchDeletedIndexes(positionInfo.getBatchedEntryDeletionIndexInfosCount(), |
729 | | - positionInfo::getBatchedEntryDeletionIndexInfoAt); |
730 | | - } |
731 | | - recoverLastActive(positionInfo.getIdleSince()); |
732 | | - recoveredCursor(position, recoveredProperties, cursorProperties, lh); |
733 | 740 | callback.operationComplete(); |
734 | 741 | }, null); |
735 | 742 | }; |
@@ -911,8 +918,10 @@ private void recoveredCursor(Position position, Map<String, Long> properties, |
911 | 918 | ledger.onCursorReadPositionUpdated(this, readPosition); |
912 | 919 | lastMarkDeleteEntry = new MarkDeleteEntry(markDeletePosition, properties, null, null); |
913 | 920 | // assign cursor-ledger so, it can be deleted when new ledger will be switched |
914 | | - this.cursorLedger = recoveredFromCursorLedger; |
915 | | - this.isCursorLedgerReadOnly = true; |
| 921 | + synchronized (this) { |
| 922 | + this.cursorLedger = recoveredFromCursorLedger; |
| 923 | + this.isCursorLedgerReadOnly = true; |
| 924 | + } |
916 | 925 | changeStateIfNotClosed(State.NoLedger); |
917 | 926 | } |
918 | 927 |
|
@@ -3108,6 +3117,7 @@ public void operationFailed(ManagedLedgerException e) { |
3108 | 3117 | public void operationComplete(Void result, Stat stat) { |
3109 | 3118 | log.info("[{}][{}] Closed cursor at md-position={}", ledger.getName(), name, markDeletePosition); |
3110 | 3119 | // At this point the position had already been safely stored in the cursor z-node |
| 3120 | + closeReadOnlyCursorLedger(); |
3111 | 3121 | callback.closeComplete(ctx); |
3112 | 3122 | asyncDeleteLedger(cursorLedger); |
3113 | 3123 | } |
@@ -3116,6 +3126,9 @@ public void operationComplete(Void result, Stat stat) { |
3116 | 3126 | public void operationFailed(MetaStoreException e) { |
3117 | 3127 | log.warn("[{}][{}] Failed to update cursor info when closing: {}", ledger.getName(), name, |
3118 | 3128 | e.getMessage()); |
| 3129 | + // The cursor cannot be reused after a failed close. Release the local read handle, |
| 3130 | + // but preserve the ledger because its position is still needed for recovery. |
| 3131 | + closeReadOnlyCursorLedger(); |
3119 | 3132 | callback.closeFailed(e, ctx); |
3120 | 3133 | } |
3121 | 3134 | }, true); |
@@ -3125,8 +3138,7 @@ public void operationFailed(MetaStoreException e) { |
3125 | 3138 | private boolean shouldPersistUnackRangesToLedger() { |
3126 | 3139 | lock.readLock().lock(); |
3127 | 3140 | try { |
3128 | | - return cursorLedger != null |
3129 | | - && !isCursorLedgerReadOnly |
| 3141 | + return hasWritableCursorLedger() |
3130 | 3142 | && getConfig().getMaxUnackedRangesToPersist() > 0 |
3131 | 3143 | && individualDeletedMessages.size() > getConfig().getMaxUnackedRangesToPersistInMetadataStore(); |
3132 | 3144 | } finally { |
@@ -3800,9 +3812,17 @@ void switchToNewLedger(final LedgerHandle lh, final VoidCallback callback) { |
3800 | 3812 | public void operationComplete(Void result, Stat stat) { |
3801 | 3813 | log.info("[{}] Updated cursor {} with ledger id {} md-position={} rd-position={}", ledger.getName(), |
3802 | 3814 | name, lh.getId(), markDeletePosition, readPosition); |
3803 | | - final LedgerHandle oldLedger = cursorLedger; |
3804 | | - cursorLedger = lh; |
3805 | | - isCursorLedgerReadOnly = false; |
| 3815 | + final LedgerHandle oldLedger; |
| 3816 | + final boolean oldLedgerReadOnly; |
| 3817 | + synchronized (ManagedCursorImpl.this) { |
| 3818 | + oldLedger = cursorLedger; |
| 3819 | + oldLedgerReadOnly = isCursorLedgerReadOnly; |
| 3820 | + cursorLedger = lh; |
| 3821 | + isCursorLedgerReadOnly = false; |
| 3822 | + } |
| 3823 | + if (oldLedgerReadOnly && oldLedger != null) { |
| 3824 | + ManagedLedgerImpl.closeReadHandleAsync(oldLedger, log); |
| 3825 | + } |
3806 | 3826 |
|
3807 | 3827 | // At this point the position had already been safely markdeleted |
3808 | 3828 | callback.operationComplete(); |
@@ -3918,6 +3938,21 @@ void readOperationCompleted() { |
3918 | 3938 | } |
3919 | 3939 | } |
3920 | 3940 |
|
| 3941 | + private synchronized boolean hasWritableCursorLedger() { |
| 3942 | + return cursorLedger != null && !isCursorLedgerReadOnly; |
| 3943 | + } |
| 3944 | + |
| 3945 | + /** Releases the local recovery handle without touching cursor metadata or deleting its ledger. */ |
| 3946 | + void closeReadOnlyCursorLedger() { |
| 3947 | + final LedgerHandle lh; |
| 3948 | + synchronized (this) { |
| 3949 | + lh = isCursorLedgerReadOnly ? cursorLedger : null; |
| 3950 | + } |
| 3951 | + if (lh != null) { |
| 3952 | + ManagedLedgerImpl.closeReadHandleAsync(lh, log); |
| 3953 | + } |
| 3954 | + } |
| 3955 | + |
3921 | 3956 | void asyncDeleteLedger(final LedgerHandle lh) { |
3922 | 3957 | asyncDeleteLedger(lh, DEFAULT_LEDGER_DELETE_RETRIES); |
3923 | 3958 | } |
@@ -3949,6 +3984,7 @@ private void asyncDeleteLedger(final LedgerHandle lh, int retry) { |
3949 | 3984 | } |
3950 | 3985 |
|
3951 | 3986 | void asyncDeleteCursorLedger() { |
| 3987 | + closeReadOnlyCursorLedger(); |
3952 | 3988 | asyncDeleteCursorLedger(DEFAULT_LEDGER_DELETE_RETRIES); |
3953 | 3989 | } |
3954 | 3990 |
|
|