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] Check replication cluster before starting the replicator #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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 @@ -38,7 +38,9 @@
import java.io.ByteArrayOutputStream;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
Expand All @@ -52,6 +54,7 @@
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;
import lombok.Cleanup;
import lombok.extern.slf4j.Slf4j;
import org.apache.bookkeeper.client.LedgerHandle;
import org.apache.bookkeeper.mledger.ManagedCursor;
import org.apache.bookkeeper.mledger.ManagedLedger;
Expand Down Expand Up @@ -83,6 +86,7 @@
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

@Slf4j
@Test(groups = "broker")
public class PersistentTopicTest extends BrokerTestBase {

Expand Down Expand Up @@ -558,10 +562,10 @@ public void testCreateTopicWithZombieReplicatorCursor(boolean topicLevelPolicy)
admin.tenants().updateTenant("prop", tenantInfo);

if (topicLevelPolicy) {
admin.topics().setReplicationClusters(topicName, Collections.singletonList(remoteCluster));
admin.topics().setReplicationClusters(topicName, Arrays.asList("test", remoteCluster));
} else {
admin.namespaces().setNamespaceReplicationClustersAsync(
namespace, Collections.singleton(remoteCluster)).get();
namespace, Sets.newHashSet("test", remoteCluster)).get();
}

final PersistentTopic topic = (PersistentTopic) pulsar.getBrokerService().getTopic(topicName, false)
Expand All @@ -576,16 +580,27 @@ public void testCreateTopicWithZombieReplicatorCursor(boolean topicLevelPolicy)
};
assertEquals(getCursors.get(), Collections.singleton(conf.getReplicatorPrefix() + "." + remoteCluster));

// PersistentTopics#onPoliciesUpdate might happen in different threads, so there might be a race between two
// updates of the replication clusters. So here we sleep for a while to reduce the flakiness.
Thread.sleep(100);

// Configure the local cluster to avoid the topic being deleted in PersistentTopics#checkReplication
if (topicLevelPolicy) {
admin.topics().setReplicationClusters(topicName, Collections.emptyList());
admin.topics().setReplicationClusters(topicName, Collections.singletonList("test"));
} else {
admin.namespaces().setNamespaceReplicationClustersAsync(namespace, Collections.emptySet()).get();
admin.namespaces().setNamespaceReplicationClustersAsync(namespace, Collections.singleton("test")).get();
}
admin.clusters().deleteCluster(remoteCluster);
// Now the cluster and its related policy has been removed but the replicator cursor still exists

topic.initialize().get(3, TimeUnit.SECONDS);
Awaitility.await().atMost(3, TimeUnit.SECONDS)
.until(() -> !topic.getManagedLedger().getCursors().iterator().hasNext());
Awaitility.await().atMost(Duration.ofSeconds(10)).until(() -> {
log.info("Before initialize...");
try {
topic.initialize().get(3, TimeUnit.SECONDS);
} catch (ExecutionException e) {
log.warn("Failed to initialize: {}", e.getCause().getMessage());
}
return !topic.getManagedLedger().getCursors().iterator().hasNext();
});
}
}