com-poorbar commented on a change in pull request #12792: URL: https://github.com/apache/pulsar/pull/12792#discussion_r795015008
########## File path: pulsar-broker/src/test/java/org/apache/pulsar/broker/service/KeySharedE2ETest.java ########## @@ -0,0 +1,242 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pulsar.broker.service; + +import static org.testng.Assert.fail; +import java.lang.reflect.Field; +import java.util.NavigableMap; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Predicate; +import org.apache.pulsar.client.api.Consumer; +import org.apache.pulsar.client.api.ConsumerBuilder; +import org.apache.pulsar.client.api.ConsumerEventListener; +import org.apache.pulsar.client.api.KeySharedPolicy; +import org.apache.pulsar.client.api.Range; +import org.apache.pulsar.client.api.SubscriptionType; +import org.apache.pulsar.client.impl.StickyKeyConsumerPredicate; +import org.testng.Assert; +import org.testng.annotations.AfterClass; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; +import org.apache.pulsar.client.impl.StickyKeyConsumerPredicate.Predicate4HashRangeAutoSplitStickyKeyConsumerSelector; +import org.apache.pulsar.client.impl.StickyKeyConsumerPredicate.Predicate4HashRangeExclusiveStickyKeyConsumerSelector; + +@Test(groups = "broker") +public class KeySharedE2ETest extends BrokerTestBase { + + @BeforeClass + @Override + protected void setup() throws Exception { + super.baseSetup(); + } + + @AfterClass(alwaysRun = true) + @Override + protected void cleanup() throws Exception { + super.internalCleanup(); + } + + protected static final int CONSUMER_ADD_OR_REMOVE_WAIT_TIME = 100; + + protected static class TestConsumerStateEventListener implements ConsumerEventListener { + + String name = ""; + + Predicate<String> keyPredicate; + + AtomicInteger trigCount = new AtomicInteger(); + + public TestConsumerStateEventListener(String name) { + this.name = name; + } + + @Override + public void becameActive(Consumer<?> consumer, int partitionId) { + } + + @Override + public void becameInactive(Consumer<?> consumer, int partitionId) { + + } + + @Override + public void keySharedRuleChanged(Consumer<?> consumer, Predicate<String> keyPredicate) { + this.keyPredicate = keyPredicate; + trigCount.incrementAndGet(); + } + } + + protected void clearEventListener(TestConsumerStateEventListener eventListener){ + eventListener.keyPredicate = null; + eventListener.trigCount.set(0); + } + + @Test + public void testConsumerEventsWithAutoHashRangeImpl() throws Exception { + final String topicName = "persistent://prop/use/ns-abc/key-shared-topic_01-" + System.currentTimeMillis(); + final String subName = "sub_key_shared_01"; + ConsumerBuilder<byte[]> consumerBuilder = pulsarClient.newConsumer().topic(topicName).subscriptionName(subName) + .subscriptionType(SubscriptionType.Key_Shared); + int rangeSize = 0; + // 1.One subscription. + TestConsumerStateEventListener listener1 = new TestConsumerStateEventListener("key_shared_listener_01"); + ConsumerBuilder<byte[]> consumerBuilder1 = consumerBuilder.clone().consumerName("key_shared_consumer_01") + .consumerEventListener(listener1); + Consumer<byte[]> consumer1 = consumerBuilder1.subscribe(); + Thread.sleep(CONSUMER_ADD_OR_REMOVE_WAIT_TIME); + Assert.assertEquals(listener1.trigCount.get(), 1); + Assert.assertTrue(listener1.keyPredicate != null); + Assert.assertTrue(listener1.keyPredicate instanceof Predicate4HashRangeAutoSplitStickyKeyConsumerSelector); + Predicate4HashRangeAutoSplitStickyKeyConsumerSelector predicate4ConsistentHashingStickyKeyConsumerSelector1 = + (Predicate4HashRangeAutoSplitStickyKeyConsumerSelector) listener1.keyPredicate; + rangeSize = predicate4ConsistentHashingStickyKeyConsumerSelector1.getRangeSize(); + Assert.assertTrue(predicate4ConsistentHashingStickyKeyConsumerSelector1.getRangeSize() + == predicate4ConsistentHashingStickyKeyConsumerSelector1.getHighHash()); + Assert.assertEquals(predicate4ConsistentHashingStickyKeyConsumerSelector1.getLowHash(), -1); + // 2.Two subscription. + TestConsumerStateEventListener listener2 = new TestConsumerStateEventListener("key_shared_listener_02"); + ConsumerBuilder<byte[]> consumerBuilder2 = consumerBuilder.clone().consumerName("key_shared_consumer_02") + .consumerEventListener(listener2); + Consumer<byte[]> consumer2 = consumerBuilder2.subscribe(); + Thread.sleep(CONSUMER_ADD_OR_REMOVE_WAIT_TIME); Review comment: Good idea ! Thanks for your guidance. I'm so sorry that I'm so busy these days -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
