-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[fix][broker] Fix failed topic future not removed before the deduplication recovery is done #24500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
BewareMyPower
wants to merge
19
commits into
apache:master
from
BewareMyPower:bewaremypower/load-topic-failure-2
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
0f05b36
[fix][broker] Fix failed topic future not removed before the Persiste…
BewareMyPower db2c9f5
Fix checkstyle
BewareMyPower f2c206d
Fix wrong tests
BewareMyPower db3aeef
Add testOrphanManagedLedgerRemovedAfterUnload
BewareMyPower b9d29f2
Take snapshot when the recovery is interrupted
BewareMyPower 6ad4dd6
Speed up tests
BewareMyPower 2c8c0fc
Fix checkstyle
BewareMyPower aa01fc4
Refactor MessageDeduplication
BewareMyPower 5c41fee
Fix failed testDeduplicationReplayStuck due to snapshot
BewareMyPower 9c60866
Speed up tests
BewareMyPower 7e2d957
Fix testTopicLoadAndDeleteAtTheSameTime
BewareMyPower 2c74ff2
Fix testCloseTransactionBufferWhenTimeout
BewareMyPower 0428f9e
Adjust logs
BewareMyPower bbeb9a9
Merge branch 'master' into bewaremypower/load-topic-failure-2
BewareMyPower 7048aaf
Don't close managed ledger if there is another pending topic future
BewareMyPower d483e67
Improve takeSnapshot
BewareMyPower cd72268
Merge branch 'master' into bewaremypower/load-topic-failure-2
BewareMyPower 4644bed
Improve testDuplicatedTopics
BewareMyPower 50eb747
Merge branch 'master' into bewaremypower/load-topic-failure-2
BewareMyPower File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
managed-ledger/src/main/java/org/apache/bookkeeper/mledger/util/ManagedLedgerAsyncUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, | ||
| * software distributed under the License is distributed on an | ||
| * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| * KIND, either express or implied. See the License for the | ||
| * specific language governing permissions and limitations | ||
| * under the License. | ||
| */ | ||
| package org.apache.bookkeeper.mledger.util; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.CompletableFuture; | ||
| import org.apache.bookkeeper.mledger.AsyncCallbacks; | ||
| import org.apache.bookkeeper.mledger.Entry; | ||
| import org.apache.bookkeeper.mledger.ManagedCursor; | ||
| import org.apache.bookkeeper.mledger.ManagedLedger; | ||
| import org.apache.bookkeeper.mledger.ManagedLedgerException; | ||
| import org.apache.bookkeeper.mledger.Position; | ||
| import org.apache.pulsar.common.classification.InterfaceStability; | ||
|
|
||
| /** | ||
| * This util class contains some future-based methods to replace callback-based APIs. With a callback-based API, if any | ||
| * exception is thrown in the callback, the callback will never have a chance to be called. While with a future-based | ||
| * API, if any exception is thrown in future's callback (e.g. `thenApply`), the future will eventually be completed | ||
| * exceptionally. In addition, future-based API is easier for users to switch a different executor to execute the | ||
| * callback (e.g. `thenApplyAsync`). | ||
| */ | ||
| @InterfaceStability.Evolving | ||
| public class ManagedLedgerAsyncUtils { | ||
|
|
||
| public static CompletableFuture<ManagedCursor> openCursor(ManagedLedger ml, String cursorName) { | ||
| final var future = new CompletableFuture<ManagedCursor>(); | ||
| ml.asyncOpenCursor(cursorName, new AsyncCallbacks.OpenCursorCallback() { | ||
| @Override | ||
| public void openCursorComplete(ManagedCursor cursor, Object ctx) { | ||
| future.complete(cursor); | ||
| } | ||
|
|
||
| @Override | ||
| public void openCursorFailed(ManagedLedgerException exception, Object ctx) { | ||
| future.completeExceptionally(exception); | ||
| } | ||
| }, null); | ||
| return future; | ||
| } | ||
|
|
||
| public static CompletableFuture<List<Entry>> readEntries(ManagedCursor cursor, int numberOfEntriesToRead, | ||
| Position maxPosition) { | ||
| final var future = new CompletableFuture<List<Entry>>(); | ||
| cursor.asyncReadEntries(numberOfEntriesToRead, new AsyncCallbacks.ReadEntriesCallback() { | ||
| @Override | ||
| public void readEntriesComplete(List<Entry> entries, Object ctx) { | ||
| future.complete(entries); | ||
| } | ||
|
|
||
| @Override | ||
| public void readEntriesFailed(ManagedLedgerException exception, Object ctx) { | ||
| future.completeExceptionally(exception); | ||
| } | ||
| }, null, maxPosition); | ||
| return future; | ||
| } | ||
|
|
||
| public static CompletableFuture<Void> markDelete(ManagedCursor cursor, Position position, | ||
| Map<String, Long> properties) { | ||
| final var future = new CompletableFuture<Void>(); | ||
| cursor.asyncMarkDelete(position, properties, new AsyncCallbacks.MarkDeleteCallback() { | ||
| @Override | ||
| public void markDeleteComplete(Object ctx) { | ||
| future.complete(null); | ||
| } | ||
|
|
||
| @Override | ||
| public void markDeleteFailed(ManagedLedgerException exception, Object ctx) { | ||
| future.completeExceptionally(exception); | ||
| } | ||
| }, new Object()); | ||
| return future; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -132,6 +132,7 @@ | |
| import org.apache.pulsar.broker.service.persistent.DispatchRateLimiter; | ||
| import org.apache.pulsar.broker.service.persistent.DispatchRateLimiterFactory; | ||
| import org.apache.pulsar.broker.service.persistent.DispatchRateLimiterFactoryClassic; | ||
| import org.apache.pulsar.broker.service.persistent.MessageDeduplication; | ||
| import org.apache.pulsar.broker.service.persistent.PersistentTopic; | ||
| import org.apache.pulsar.broker.service.persistent.SystemTopic; | ||
| import org.apache.pulsar.broker.service.plugin.EntryFilterProvider; | ||
|
|
@@ -1657,16 +1658,20 @@ public PulsarAdmin getClusterPulsarAdmin(String cluster, Optional<ClusterData> c | |
| * @return CompletableFuture<Topic> | ||
| * @throws RuntimeException | ||
| */ | ||
| protected CompletableFuture<Optional<Topic>> loadOrCreatePersistentTopic(final String topic, | ||
| @VisibleForTesting | ||
| public CompletableFuture<Optional<Topic>> loadOrCreatePersistentTopic(final String topic, | ||
| boolean createIfMissing, Map<String, String> properties) { | ||
| final CompletableFuture<Optional<Topic>> topicFuture = FutureUtil.createFutureWithTimeout( | ||
| Duration.ofSeconds(pulsar.getConfiguration().getTopicLoadTimeoutSeconds()), executor(), | ||
| () -> FAILED_TO_LOAD_TOPIC_TIMEOUT_EXCEPTION); | ||
|
|
||
| topicFuture.exceptionally(t -> { | ||
| topicFuture.exceptionallyAsync(e -> { | ||
| pulsarStats.recordTopicLoadFailed(); | ||
| return null; | ||
| }); | ||
| if (topics.remove(topic, topicFuture)) { | ||
| log.info("Removed topic {} for: {}", topic, e.getMessage()); | ||
| } | ||
| return Optional.empty(); | ||
| }, executor()); | ||
|
|
||
| checkTopicNsOwnership(topic) | ||
| .thenRun(() -> { | ||
|
|
@@ -1781,7 +1786,6 @@ public void createPersistentTopic0(final String topic, boolean createIfMissing, | |
| if (isTransactionInternalName(topicName)) { | ||
| String msg = String.format("Can not create transaction system topic %s", topic); | ||
| log.warn(msg); | ||
| pulsar.getExecutor().execute(() -> topics.remove(topic, topicFuture)); | ||
| topicFuture.completeExceptionally(new NotAllowedException(msg)); | ||
| return; | ||
| } | ||
|
|
@@ -1842,6 +1846,12 @@ public void createPersistentTopic0(final String topic, boolean createIfMissing, | |
| @Override | ||
| public void openLedgerComplete(ManagedLedger ledger, Object ctx) { | ||
| try { | ||
| if (topicFuture.isCompletedExceptionally()) { | ||
| // Don't close the managed ledger because next time the topic is accessed, the | ||
| // managed ledger will be created again. The managed ledger will be removed if the | ||
| // ownership has been changed. | ||
| return; | ||
| } | ||
| PersistentTopic persistentTopic = isSystemTopic(topic) | ||
| ? new SystemTopic(topic, ledger, BrokerService.this) | ||
| : newTopic(topic, ledger, BrokerService.this, PersistentTopic.class); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe we can |
||
|
|
@@ -1870,20 +1880,15 @@ public void openLedgerComplete(ManagedLedger ledger, Object ctx) { | |
| log.error("{} future is already completed by another thread, " | ||
| + "which is not expected. Closing the current one", topic); | ||
| } | ||
| executor().submit(() -> { | ||
| persistentTopic.close().whenComplete((ignore, ex) -> { | ||
| topics.remove(topic, topicFuture); | ||
| if (ex != null) { | ||
| log.warn("[{}] Get an error when closing topic.", | ||
| topic, ex); | ||
| } | ||
| }); | ||
| }); | ||
| } else { | ||
| addTopicToStatsMaps(topicName, persistentTopic); | ||
| } | ||
| }) | ||
| .exceptionally((ex) -> { | ||
| if (MessageDeduplication.RECOVERY_FAILURE.equals(ex.getCause())) { | ||
| log.info("Deduplication recovery of {} is cancelled", topic); | ||
| return null; | ||
| } | ||
| log.warn("Replication or dedup check failed." | ||
| + " Removing topic from topics list {}, {}", topic, ex); | ||
| executor().submit(() -> { | ||
|
|
||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The primary change of #22860 is to remove the failed Future case by case instead of removing a failed topic creation future here.
@shibd @codelipenghui Could you also review the current PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I explained all details in the PR description.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Key point:
#24497 is the best example. #23184 is another example before.
The main concern is that if an orphan pending
PersistentTopiccould affect the new pendingPersistentTopic, mainly due to:Even if it could make the new
PersistentTopicfail to load, it will be the same with the existing case, with better exception message (e.g.replicator create failure) rather than the misleading timeout exception (Failed to load topic within timeout).