|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | +package org.apache.pulsar.client.api; |
| 20 | + |
| 21 | +import com.carrotsearch.hppc.ObjectSet; |
| 22 | +import java.time.Duration; |
| 23 | +import java.util.List; |
| 24 | +import java.util.concurrent.TimeUnit; |
| 25 | +import java.util.concurrent.atomic.AtomicInteger; |
| 26 | +import lombok.extern.slf4j.Slf4j; |
| 27 | +import org.apache.pulsar.broker.BrokerTestUtil; |
| 28 | +import org.apache.pulsar.broker.service.Dispatcher; |
| 29 | +import org.apache.pulsar.common.naming.TopicName; |
| 30 | +import org.awaitility.Awaitility; |
| 31 | +import org.awaitility.reflect.WhiteboxImpl; |
| 32 | +import org.testng.Assert; |
| 33 | +import org.testng.annotations.AfterClass; |
| 34 | +import org.testng.annotations.BeforeClass; |
| 35 | +import org.testng.annotations.Test; |
| 36 | + |
| 37 | +@Slf4j |
| 38 | +@Test(groups = "broker-api") |
| 39 | +public class SimpleProducerConsumerMLInitializeDelayTest extends ProducerConsumerBase { |
| 40 | + |
| 41 | + @BeforeClass(alwaysRun = true) |
| 42 | + @Override |
| 43 | + protected void setup() throws Exception { |
| 44 | + super.internalSetup(); |
| 45 | + super.producerBaseSetup(); |
| 46 | + } |
| 47 | + |
| 48 | + @AfterClass(alwaysRun = true) |
| 49 | + @Override |
| 50 | + protected void cleanup() throws Exception { |
| 51 | + super.internalCleanup(); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + protected void doInitConf() throws Exception { |
| 56 | + super.doInitConf(); |
| 57 | + conf.setTopicLoadTimeoutSeconds(60 * 5); |
| 58 | + } |
| 59 | + |
| 60 | + @Test(timeOut = 30 * 1000) |
| 61 | + public void testConsumerListMatchesConsumerSet() throws Exception { |
| 62 | + final String topicName = BrokerTestUtil.newUniqueName("persistent://public/default/tp"); |
| 63 | + final String subName = "sub"; |
| 64 | + final int clientOperationTimeout = 3; |
| 65 | + final int loadMLDelayMillis = clientOperationTimeout * 3 * 1000; |
| 66 | + final int clientMaxBackoffSeconds = clientOperationTimeout * 2; |
| 67 | + admin.topics().createNonPartitionedTopic(topicName); |
| 68 | + // Create a client with a low operation timeout. |
| 69 | + PulsarClient client = PulsarClient.builder() |
| 70 | + .serviceUrl(lookupUrl.toString()) |
| 71 | + .operationTimeout(clientOperationTimeout, TimeUnit.SECONDS) |
| 72 | + .maxBackoffInterval(clientMaxBackoffSeconds, TimeUnit.SECONDS) |
| 73 | + .build(); |
| 74 | + Consumer consumer = client.newConsumer() |
| 75 | + .topic(topicName) |
| 76 | + .subscriptionName(subName) |
| 77 | + .subscriptionType(SubscriptionType.Shared) |
| 78 | + .subscribe(); |
| 79 | + // Inject a delay for the initialization of ML, to make the consumer to register twice. |
| 80 | + // Consumer register twice: the first will be timeout, and try again. |
| 81 | + AtomicInteger delayTimes = new AtomicInteger(); |
| 82 | + mockZooKeeper.delay(loadMLDelayMillis, (op, s) -> { |
| 83 | + if (op.toString().equals("GET") && s.contains(TopicName.get(topicName).getPersistenceNamingEncoding())) { |
| 84 | + return delayTimes.incrementAndGet() == 1; |
| 85 | + } |
| 86 | + return false; |
| 87 | + }); |
| 88 | + admin.topics().unload(topicName); |
| 89 | + // Verify: at last, "dispatcher.consumers.size" equals "dispatcher.consumerList.size". |
| 90 | + Awaitility.await().atMost(Duration.ofSeconds(loadMLDelayMillis * 3)) |
| 91 | + .ignoreExceptions().untilAsserted(() -> { |
| 92 | + Dispatcher dispatcher = pulsar.getBrokerService() |
| 93 | + .getTopic(topicName, false).join().get() |
| 94 | + .getSubscription(subName).getDispatcher(); |
| 95 | + ObjectSet consumerSet = WhiteboxImpl.getInternalState(dispatcher, "consumerSet"); |
| 96 | + List consumerList = WhiteboxImpl.getInternalState(dispatcher, "consumerList"); |
| 97 | + log.info("consumerSet_size: {}, consumerList_size: {}", consumerSet.size(), consumerList.size()); |
| 98 | + Assert.assertEquals(consumerList.size(), 1); |
| 99 | + Assert.assertEquals(consumerSet.size(), 1); |
| 100 | + }); |
| 101 | + |
| 102 | + // Verify: the topic can be deleted. |
| 103 | + consumer.close(); |
| 104 | + admin.topics().delete(topicName); |
| 105 | + // cleanup. |
| 106 | + client.close(); |
| 107 | + } |
| 108 | +} |
0 commit comments