Skip to content

Commit 2239523

Browse files
BewareMyPowerTechnoboy-
authored andcommitted
[fix][test] Fix flaky testCreateTopicWithZombieReplicatorCursor (#20037)
1 parent 9948f89 commit 2239523

1 file changed

Lines changed: 22 additions & 7 deletions

File tree

pulsar-broker/src/test/java/org/apache/pulsar/broker/service/persistent/PersistentTopicTest.java

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
import java.io.ByteArrayOutputStream;
3636
import java.lang.reflect.Field;
3737
import java.nio.charset.StandardCharsets;
38+
import java.time.Duration;
3839
import java.util.ArrayList;
40+
import java.util.Arrays;
3941
import java.util.Collection;
4042
import java.util.Collections;
4143
import java.util.HashSet;
@@ -51,6 +53,7 @@
5153
import java.util.concurrent.atomic.AtomicBoolean;
5254
import java.util.function.Supplier;
5355
import lombok.Cleanup;
56+
import lombok.extern.slf4j.Slf4j;
5457
import org.apache.bookkeeper.client.LedgerHandle;
5558
import org.apache.bookkeeper.mledger.ManagedCursor;
5659
import org.apache.bookkeeper.mledger.ManagedLedger;
@@ -81,6 +84,7 @@
8184
import org.testng.annotations.DataProvider;
8285
import org.testng.annotations.Test;
8386

87+
@Slf4j
8488
@Test(groups = "broker")
8589
public class PersistentTopicTest extends BrokerTestBase {
8690

@@ -443,10 +447,10 @@ public void testCreateTopicWithZombieReplicatorCursor(boolean topicLevelPolicy)
443447
admin.tenants().updateTenant("prop", tenantInfo);
444448

445449
if (topicLevelPolicy) {
446-
admin.topics().setReplicationClusters(topicName, Collections.singletonList(remoteCluster));
450+
admin.topics().setReplicationClusters(topicName, Arrays.asList("test", remoteCluster));
447451
} else {
448452
admin.namespaces().setNamespaceReplicationClustersAsync(
449-
namespace, Collections.singleton(remoteCluster)).get();
453+
namespace, Sets.newHashSet("test", remoteCluster)).get();
450454
}
451455

452456
final PersistentTopic topic = (PersistentTopic) pulsar.getBrokerService().getTopic(topicName, false)
@@ -461,16 +465,27 @@ public void testCreateTopicWithZombieReplicatorCursor(boolean topicLevelPolicy)
461465
};
462466
assertEquals(getCursors.get(), Collections.singleton(conf.getReplicatorPrefix() + "." + remoteCluster));
463467

468+
// PersistentTopics#onPoliciesUpdate might happen in different threads, so there might be a race between two
469+
// updates of the replication clusters. So here we sleep for a while to reduce the flakiness.
470+
Thread.sleep(100);
471+
472+
// Configure the local cluster to avoid the topic being deleted in PersistentTopics#checkReplication
464473
if (topicLevelPolicy) {
465-
admin.topics().setReplicationClusters(topicName, Collections.emptyList());
474+
admin.topics().setReplicationClusters(topicName, Collections.singletonList("test"));
466475
} else {
467-
admin.namespaces().setNamespaceReplicationClustersAsync(namespace, Collections.emptySet()).get();
476+
admin.namespaces().setNamespaceReplicationClustersAsync(namespace, Collections.singleton("test")).get();
468477
}
469478
admin.clusters().deleteCluster(remoteCluster);
470479
// Now the cluster and its related policy has been removed but the replicator cursor still exists
471480

472-
topic.initialize().get(3, TimeUnit.SECONDS);
473-
Awaitility.await().atMost(3, TimeUnit.SECONDS)
474-
.until(() -> !topic.getManagedLedger().getCursors().iterator().hasNext());
481+
Awaitility.await().atMost(Duration.ofSeconds(10)).until(() -> {
482+
log.info("Before initialize...");
483+
try {
484+
topic.initialize().get(3, TimeUnit.SECONDS);
485+
} catch (ExecutionException e) {
486+
log.warn("Failed to initialize: {}", e.getCause().getMessage());
487+
}
488+
return !topic.getManagedLedger().getCursors().iterator().hasNext();
489+
});
475490
}
476491
}

0 commit comments

Comments
 (0)