Skip to content

Commit

Permalink
Fix flaky ManagedLedgerFactory shutdown (apache#10365)
Browse files Browse the repository at this point in the history
  • Loading branch information
lhotari authored Apr 25, 2021
1 parent 4e1e06a commit c4e1b8b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@

import static com.google.common.base.Preconditions.checkArgument;
import static org.apache.bookkeeper.mledger.ManagedLedgerException.getManagedLedgerException;

import com.google.common.base.Predicates;
import com.google.common.collect.Maps;

import io.netty.util.concurrent.DefaultThreadFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -41,7 +38,6 @@
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import org.apache.bookkeeper.client.BKException;
import org.apache.bookkeeper.client.BookKeeper;
import org.apache.bookkeeper.common.util.OrderedExecutor;
Expand Down Expand Up @@ -78,8 +74,8 @@
import org.apache.bookkeeper.stats.NullStatsLogger;
import org.apache.bookkeeper.stats.StatsLogger;
import org.apache.bookkeeper.zookeeper.ZooKeeperClient;
import org.apache.pulsar.common.util.DateFormatter;
import org.apache.pulsar.common.policies.data.EnsemblePlacementPolicyConfig;
import org.apache.pulsar.common.util.DateFormatter;
import org.apache.pulsar.metadata.api.MetadataStore;
import org.apache.pulsar.metadata.api.Stat;
import org.apache.pulsar.metadata.impl.ZKMetadataStore;
Expand Down Expand Up @@ -498,11 +494,13 @@ public void shutdown() throws InterruptedException, ManagedLedgerException {
statsTask.cancel(true);
flushCursorsTask.cancel(true);

// take a snapshot of ledgers currently in the map to prevent race conditions
List<CompletableFuture<ManagedLedgerImpl>> ledgers = new ArrayList<>(this.ledgers.values());
int numLedgers = ledgers.size();
final CountDownLatch latch = new CountDownLatch(numLedgers);
log.info("Closing {} ledgers", numLedgers);

for (CompletableFuture<ManagedLedgerImpl> ledgerFuture : ledgers.values()) {
for (CompletableFuture<ManagedLedgerImpl> ledgerFuture : ledgers) {
ManagedLedgerImpl ledger = ledgerFuture.getNow(null);
if (ledger == null) {
latch.countDown();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,13 @@ public CompletableFuture<Void> closeAsync() {
* getConfiguration()
.getBrokerShutdownTimeoutMs())));

// shutdown loadmanager before shutting down the broker
executorServicesShutdown.shutdown(loadManagerExecutor);
LoadManager loadManager = this.loadManager.get();
if (loadManager != null) {
loadManager.stop();
}

List<CompletableFuture<Void>> asyncCloseFutures = new ArrayList<>();
if (this.brokerService != null) {
asyncCloseFutures.add(this.brokerService.closeAsync());
Expand All @@ -392,8 +399,6 @@ public CompletableFuture<Void> closeAsync() {
this.leaderElectionService = null;
}

executorServicesShutdown.shutdown(loadManagerExecutor);

if (globalZkCache != null) {
globalZkCache.close();
globalZkCache = null;
Expand Down Expand Up @@ -434,10 +439,7 @@ public CompletableFuture<Void> closeAsync() {
executorServicesShutdown.shutdown(orderedExecutor);
executorServicesShutdown.shutdown(cacheExecutor);

LoadManager loadManager = this.loadManager.get();
if (loadManager != null) {
loadManager.stop();
}


if (schemaRegistryService != null) {
schemaRegistryService.close();
Expand Down

0 comments on commit c4e1b8b

Please sign in to comment.