Search in sources :

Example 1 with GetTopicsResult

use of org.apache.pulsar.common.lookup.GetTopicsResult in project pulsar by yahoo.

the class PatternMultiTopicsConsumerImplTest method testChangedUnfilteredResponse.

@Test
public void testChangedUnfilteredResponse() {
    PatternMultiTopicsConsumerImpl.updateSubscriptions(Pattern.compile("tenant/my-ns/name-.*"), mockTopicsHashSetter, new GetTopicsResult(Arrays.asList("persistent://tenant/my-ns/name-1", "persistent://tenant/my-ns/name-2", "persistent://tenant/my-ns/non-matching"), null, false, true), mockListener, Collections.emptyList());
    verify(mockListener).onTopicsAdded(Sets.newHashSet("persistent://tenant/my-ns/name-1", "persistent://tenant/my-ns/name-2"));
    verify(mockListener).onTopicsRemoved(Collections.emptySet());
    verify(mockTopicsHashSetter).accept(null);
}
Also used : GetTopicsResult(org.apache.pulsar.common.lookup.GetTopicsResult) Test(org.testng.annotations.Test)

Example 2 with GetTopicsResult

use of org.apache.pulsar.common.lookup.GetTopicsResult in project pulsar by yahoo.

the class PatternMultiTopicsConsumerImplTest method testUnchangedResponse.

@Test
public void testUnchangedResponse() {
    PatternMultiTopicsConsumerImpl.updateSubscriptions(Pattern.compile("tenant/my-ns/name-.*"), mockTopicsHashSetter, new GetTopicsResult(Arrays.asList("persistent://tenant/my-ns/name-0", "persistent://tenant/my-ns/name-1", "persistent://tenant/my-ns/name-2"), "TOPICS_HASH", true, false), mockListener, Arrays.asList("persistent://tenant/my-ns/name-0"));
    verify(mockListener, never()).onTopicsAdded(any());
    verify(mockListener, never()).onTopicsRemoved(any());
    verify(mockTopicsHashSetter).accept("TOPICS_HASH");
}
Also used : GetTopicsResult(org.apache.pulsar.common.lookup.GetTopicsResult) Test(org.testng.annotations.Test)

Example 3 with GetTopicsResult

use of org.apache.pulsar.common.lookup.GetTopicsResult in project pulsar by yahoo.

the class PatternMultiTopicsConsumerImplTest method testChangedFilteredResponse.

@Test
public void testChangedFilteredResponse() {
    PatternMultiTopicsConsumerImpl.updateSubscriptions(Pattern.compile("tenant/my-ns/name-.*"), mockTopicsHashSetter, new GetTopicsResult(Arrays.asList("persistent://tenant/my-ns/name-0", "persistent://tenant/my-ns/name-1", "persistent://tenant/my-ns/name-2"), "TOPICS_HASH", true, true), mockListener, Arrays.asList("persistent://tenant/my-ns/name-0"));
    verify(mockListener).onTopicsAdded(Sets.newHashSet("persistent://tenant/my-ns/name-1", "persistent://tenant/my-ns/name-2"));
    verify(mockListener).onTopicsRemoved(Collections.emptySet());
    verify(mockTopicsHashSetter).accept("TOPICS_HASH");
}
Also used : GetTopicsResult(org.apache.pulsar.common.lookup.GetTopicsResult) Test(org.testng.annotations.Test)

Example 4 with GetTopicsResult

use of org.apache.pulsar.common.lookup.GetTopicsResult in project pulsar by yahoo.

the class PulsarClientImplTest method testConsumerIsClosed.

@Test
public void testConsumerIsClosed() throws Exception {
    // mock client connection
    LookupService lookup = mock(LookupService.class);
    when(lookup.getTopicsUnderNamespace(any(NamespaceName.class), any(CommandGetTopicsOfNamespace.Mode.class), nullable(String.class), nullable(String.class))).thenReturn(CompletableFuture.completedFuture(new GetTopicsResult(Collections.emptyList(), null, false, true)));
    when(lookup.getPartitionedTopicMetadata(any(TopicName.class))).thenReturn(CompletableFuture.completedFuture(new PartitionedTopicMetadata()));
    when(lookup.getBroker(any(TopicName.class))).thenReturn(CompletableFuture.completedFuture(Pair.of(mock(InetSocketAddress.class), mock(InetSocketAddress.class))));
    ConnectionPool pool = mock(ConnectionPool.class);
    ClientCnx cnx = mock(ClientCnx.class);
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    Channel channel = mock(Channel.class);
    when(channel.remoteAddress()).thenReturn(mock(SocketAddress.class));
    when(ctx.channel()).thenReturn(channel);
    when(ctx.writeAndFlush(any(), any(ChannelPromise.class))).thenReturn(mock(ChannelFuture.class));
    when(ctx.voidPromise()).thenReturn(mock(ChannelPromise.class));
    when(cnx.channel()).thenReturn(channel);
    when(cnx.ctx()).thenReturn(ctx);
    when(cnx.sendRequestWithId(any(ByteBuf.class), anyLong())).thenReturn(CompletableFuture.completedFuture(mock(ProducerResponse.class)));
    when(pool.getConnection(any(InetSocketAddress.class), any(InetSocketAddress.class))).thenReturn(CompletableFuture.completedFuture(cnx));
    Whitebox.setInternalState(clientImpl, "cnxPool", pool);
    Whitebox.setInternalState(clientImpl, "lookup", lookup);
    List<ConsumerBase<byte[]>> consumers = new ArrayList<>();
    /**
     * {@link org.apache.pulsar.client.impl.PulsarClientImpl#patternTopicSubscribeAsync}
     */
    ConsumerConfigurationData<byte[]> consumerConf0 = new ConsumerConfigurationData<>();
    consumerConf0.setSubscriptionName("test-subscription0");
    consumerConf0.setTopicsPattern(Pattern.compile("test-topic"));
    consumers.add((ConsumerBase) clientImpl.subscribeAsync(consumerConf0).get());
    /**
     * {@link org.apache.pulsar.client.impl.PulsarClientImpl#singleTopicSubscribeAsync}
     */
    ConsumerConfigurationData<byte[]> consumerConf1 = new ConsumerConfigurationData<>();
    consumerConf1.setSubscriptionName("test-subscription1");
    consumerConf1.setTopicNames(Collections.singleton("test-topic"));
    consumers.add((ConsumerBase) clientImpl.subscribeAsync(consumerConf1).get());
    /**
     * {@link org.apache.pulsar.client.impl.PulsarClientImpl#multiTopicSubscribeAsync}
     */
    ConsumerConfigurationData<byte[]> consumerConf2 = new ConsumerConfigurationData<>();
    consumerConf2.setSubscriptionName("test-subscription2");
    consumers.add((ConsumerBase) clientImpl.subscribeAsync(consumerConf2).get());
    consumers.forEach(consumer -> assertNotSame(consumer.getState(), HandlerState.State.Closed));
    clientImpl.close();
    consumers.forEach(consumer -> assertSame(consumer.getState(), HandlerState.State.Closed));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) GetTopicsResult(org.apache.pulsar.common.lookup.GetTopicsResult) InetSocketAddress(java.net.InetSocketAddress) Channel(io.netty.channel.Channel) ArrayList(java.util.ArrayList) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) TopicName(org.apache.pulsar.common.naming.TopicName) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) ConsumerConfigurationData(org.apache.pulsar.client.impl.conf.ConsumerConfigurationData) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) PartitionedTopicMetadata(org.apache.pulsar.common.partition.PartitionedTopicMetadata) Test(org.testng.annotations.Test)

Example 5 with GetTopicsResult

use of org.apache.pulsar.common.lookup.GetTopicsResult in project pulsar by yahoo.

the class PatternMultiTopicsConsumerImpl method run.

// TimerTask to recheck topics change, and trigger subscribe/unsubscribe based on the change.
@Override
public void run(Timeout timeout) throws Exception {
    if (timeout.isCancelled()) {
        return;
    }
    client.getLookup().getTopicsUnderNamespace(namespaceName, subscriptionMode, topicsPattern.pattern(), topicsHash).thenCompose(getTopicsResult -> {
        if (log.isDebugEnabled()) {
            log.debug("Get topics under namespace {}, topics.size: {}, topicsHash: {}, filtered: {}", namespaceName, getTopicsResult.getTopics().size(), getTopicsResult.getTopicsHash(), getTopicsResult.isFiltered());
            getTopicsResult.getTopics().forEach(topicName -> log.debug("Get topics under namespace {}, topic: {}", namespaceName, topicName));
        }
        final List<String> oldTopics = new ArrayList<>(getPartitionedTopics());
        for (String partition : getPartitions()) {
            TopicName topicName = TopicName.get(partition);
            if (!topicName.isPartitioned() || !oldTopics.contains(topicName.getPartitionedTopicName())) {
                oldTopics.add(partition);
            }
        }
        return updateSubscriptions(topicsPattern, this::setTopicsHash, getTopicsResult, topicsChangeListener, oldTopics);
    }).exceptionally(ex -> {
        log.warn("[{}] Failed to recheck topics change: {}", topic, ex.getMessage());
        return null;
    }).thenAccept(__ -> {
        // schedule the next re-check task
        this.recheckPatternTimeout = client.timer().newTimeout(PatternMultiTopicsConsumerImpl.this, Math.max(1, conf.getPatternAutoDiscoveryPeriod()), TimeUnit.SECONDS);
    });
}
Also used : Timeout(io.netty.util.Timeout) TopicName(org.apache.pulsar.common.naming.TopicName) Logger(org.slf4j.Logger) ConsumerConfigurationData(org.apache.pulsar.client.impl.conf.ConsumerConfigurationData) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) Schema(org.apache.pulsar.client.api.Schema) Mode(org.apache.pulsar.common.api.proto.CommandGetTopicsOfNamespace.Mode) TimeUnit(java.util.concurrent.TimeUnit) Consumer(org.apache.pulsar.client.api.Consumer) GetTopicsResult(org.apache.pulsar.common.lookup.GetTopicsResult) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) List(java.util.List) TopicList(org.apache.pulsar.common.topics.TopicList) FutureUtil(org.apache.pulsar.common.util.FutureUtil) Lists(com.google.common.collect.Lists) ExecutorProvider(org.apache.pulsar.client.util.ExecutorProvider) NamespaceName(org.apache.pulsar.common.naming.NamespaceName) VisibleForTesting(com.google.common.annotations.VisibleForTesting) TimerTask(io.netty.util.TimerTask) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) ArrayList(java.util.ArrayList) TopicName(org.apache.pulsar.common.naming.TopicName)

Aggregations

GetTopicsResult (org.apache.pulsar.common.lookup.GetTopicsResult)14 Test (org.testng.annotations.Test)8 CompletableFuture (java.util.concurrent.CompletableFuture)6 ArrayList (java.util.ArrayList)4 ConsumerConfigurationData (org.apache.pulsar.client.impl.conf.ConsumerConfigurationData)4 NamespaceName (org.apache.pulsar.common.naming.NamespaceName)4 TopicName (org.apache.pulsar.common.naming.TopicName)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 Lists (com.google.common.collect.Lists)2 ByteBuf (io.netty.buffer.ByteBuf)2 Channel (io.netty.channel.Channel)2 ChannelFuture (io.netty.channel.ChannelFuture)2 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)2 ChannelPromise (io.netty.channel.ChannelPromise)2 Timeout (io.netty.util.Timeout)2 TimerTask (io.netty.util.TimerTask)2 InetSocketAddress (java.net.InetSocketAddress)2 SocketAddress (java.net.SocketAddress)2 Collection (java.util.Collection)2