Skip to content
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

[fix][broker] Remove failed future when topic load error #20561

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1039,8 +1039,10 @@ public CompletableFuture<Optional<Topic>> getTopic(final TopicName topicName, bo
}
}
final boolean isPersistentTopic = topicName.getDomain().equals(TopicDomain.persistent);
AtomicBoolean topicFutureCreatedByCurrentRequest = new AtomicBoolean();
if (isPersistentTopic) {
return topics.computeIfAbsent(topicName.toString(), (tpName) -> {
topicFuture = topics.computeIfAbsent(topicName.toString(), (tpName) -> {
topicFutureCreatedByCurrentRequest.set(true);
if (topicName.isPartitioned()) {
return fetchPartitionedTopicMetadataAsync(TopicName.get(topicName.getPartitionedTopicName()))
.thenCompose((metadata) -> {
Expand All @@ -1055,7 +1057,8 @@ public CompletableFuture<Optional<Topic>> getTopic(final TopicName topicName, bo
return loadOrCreatePersistentTopic(tpName, createIfMissing, properties);
});
} else {
return topics.computeIfAbsent(topicName.toString(), (name) -> {
topicFuture = topics.computeIfAbsent(topicName.toString(), (name) -> {
topicFutureCreatedByCurrentRequest.set(true);
topicEventsDispatcher.notify(topicName.toString(), TopicEvent.LOAD, EventStage.BEFORE);
if (topicName.isPartitioned()) {
final TopicName partitionedTopicName = TopicName.get(topicName.getPartitionedTopicName());
Expand Down Expand Up @@ -1091,6 +1094,15 @@ public CompletableFuture<Optional<Topic>> getTopic(final TopicName topicName, bo
}
});
}
if (topicFutureCreatedByCurrentRequest.get()) {
final CompletableFuture<Optional<Topic>> topicFutureToRemove = topicFuture;
topicFutureToRemove.whenComplete((tpOptional, ex) -> {
if (ex != null || !tpOptional.isPresent()) {
topics.remove(topicName.toString(), topicFutureToRemove);
}
});
}
return topicFuture;
} catch (IllegalArgumentException e) {
log.warn("[{}] Illegalargument exception when loading topic", topicName, e);
return FutureUtil.failedFuture(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.pulsar.broker.BrokerTestUtil.newUniqueName;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
Expand Down Expand Up @@ -48,6 +49,8 @@
import java.util.TreeSet;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import javax.ws.rs.NotAcceptableException;
import javax.ws.rs.core.Response.Status;
Expand All @@ -59,6 +62,7 @@
import org.apache.bookkeeper.mledger.impl.ManagedCursorImpl;
import org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl;
import org.apache.commons.lang3.reflect.FieldUtils;
import org.apache.pulsar.broker.BrokerTestUtil;
import org.apache.pulsar.broker.PulsarService;
import org.apache.pulsar.broker.ServiceConfiguration;
import org.apache.pulsar.broker.admin.AdminApiTest.MockedPulsarService;
Expand Down Expand Up @@ -97,6 +101,7 @@
import org.apache.pulsar.client.api.SubscriptionType;
import org.apache.pulsar.client.api.TopicMetadata;
import org.apache.pulsar.client.impl.MessageIdImpl;
import org.apache.pulsar.common.naming.NamespaceBundle;
import org.apache.pulsar.common.naming.NamespaceName;
import org.apache.pulsar.common.naming.TopicDomain;
import org.apache.pulsar.common.naming.TopicName;
Expand All @@ -123,6 +128,7 @@
import org.apache.pulsar.common.policies.data.TopicType;
import org.apache.pulsar.common.policies.data.impl.BacklogQuotaImpl;
import org.awaitility.Awaitility;
import org.awaitility.reflect.WhiteboxImpl;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
Expand Down Expand Up @@ -3290,4 +3296,45 @@ private void testDeleteNamespaceForciblyWithManyTopics() throws Exception {
admin.namespaces().deleteNamespace(ns, true);
Assert.assertFalse(admin.namespaces().getNamespaces(defaultTenant).contains(ns));
}

@Test
private void testUnloadNamespaceAfterLoadTopicFailed() throws Exception {
final String tpNameStr = BrokerTestUtil.newUniqueName("persistent://" + defaultNamespace + "/tp_");
final TopicName tpName = TopicName.get(tpNameStr);
final String ledgerName = tpName.getPersistenceNamingEncoding();
ManagedLedgerImpl ml = (ManagedLedgerImpl) pulsar.getManagedLedgerFactory().open(ledgerName);

// Make topic created timeout.
final CountDownLatch timeoutController = new CountDownLatch(1);
ManagedLedgerImpl spyMl = spy(ml);
doAnswer(invocation -> {
timeoutController.wait();
return ml.getCursors();
}).when(spyMl).getCursors();
doAnswer(invocation -> {
timeoutController.wait();
return ml.getProperties();
}).when(spyMl).getProperties();
ConcurrentHashMap<String, CompletableFuture<ManagedLedgerImpl>> ledgers =
WhiteboxImpl.getInternalState(pulsar.getManagedLedgerFactory(), "ledgers");
ledgers.put(ledgerName, CompletableFuture.completedFuture(spyMl));

// Verify the topic load will fail by timeout.
try {
admin.topics().createNonPartitionedTopic(tpNameStr);
fail("Expected load topic timeout.");
} catch (Exception ex){
assertTrue(ex.getMessage().contains("timeout"));
}
timeoutController.countDown();

// Verify unload bundle success.
NamespaceBundle namespaceBundle = pulsar.getNamespaceService().getBundle(tpName);
Awaitility.await().untilAsserted(() -> {
pulsar.getBrokerService().unloadServiceUnit(namespaceBundle, true, 30, TimeUnit.SECONDS).join();
});

// cleanup.
admin.topics().delete(tpNameStr, false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,29 @@
*/
package org.apache.pulsar.broker.service;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.spy;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.bookkeeper.mledger.AsyncCallbacks;
import org.apache.bookkeeper.mledger.ManagedLedgerException;
import org.apache.bookkeeper.mledger.impl.ManagedLedgerImpl;
import org.apache.pulsar.broker.BrokerTestUtil;
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.MessageId;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.common.naming.NamespaceBundle;
import org.apache.pulsar.common.naming.TopicName;
import org.apache.pulsar.common.policies.data.TopicStats;
import org.awaitility.Awaitility;
import org.awaitility.reflect.WhiteboxImpl;
import org.junit.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
Expand Down Expand Up @@ -75,4 +91,48 @@ public void testReplicatorProducerStatInTopic() throws Exception {
admin2.topics().delete(topicName);
});
}

@Test
private void testUnloadNamespaceAfterStartReplicatorFailed() throws Exception {
final String tpNameStr = BrokerTestUtil.newUniqueName("persistent://" + defaultNamespace + "/tp_");
final TopicName tpName = TopicName.get(tpNameStr);
final String ledgerName = tpName.getPersistenceNamingEncoding();
final String replicatorCursorName = "pulsar.repl.r2";
ManagedLedgerImpl ml = (ManagedLedgerImpl) pulsar1.getManagedLedgerFactory().open(ledgerName);

// Make replicator start failed.
final AtomicBoolean replicatorStartController = new AtomicBoolean(true);
ManagedLedgerImpl spyMl = spy(ml);
doAnswer(invocation -> {
String cursorName = (String) invocation.getArguments()[0];
AsyncCallbacks.OpenCursorCallback cb = (AsyncCallbacks.OpenCursorCallback) invocation.getArguments()[1];
Object ctx = invocation.getArguments()[2];
if (replicatorCursorName.equals(cursorName)) {
cb.openCursorFailed(new ManagedLedgerException.MetaStoreException("Mocked error"), ctx);
}
ml.asyncOpenCursor(cursorName, cb, ctx);
return null;
}).when(spyMl).asyncOpenCursor(anyString(), any(AsyncCallbacks.OpenCursorCallback.class), any());
ConcurrentHashMap<String, CompletableFuture<ManagedLedgerImpl>> ledgers =
WhiteboxImpl.getInternalState(pulsar1.getManagedLedgerFactory(), "ledgers");
ledgers.put(ledgerName, CompletableFuture.completedFuture(spyMl));

// Verify the topic load will fail by mocked error.
try {
admin1.topics().createNonPartitionedTopic(tpNameStr);
fail("Expected load topic timeout.");
} catch (Exception ex){
assertTrue(ex.getMessage().contains("Mocked error"));
}

// Verify unload bundle success.
NamespaceBundle namespaceBundle = pulsar1.getNamespaceService().getBundle(tpName);
Awaitility.await().untilAsserted(() -> {
pulsar1.getBrokerService().unloadServiceUnit(namespaceBundle, true, 30, TimeUnit.SECONDS).join();
});

// cleanup.
replicatorStartController.set(false);
admin1.topics().delete(tpNameStr, false);
}
}
Loading