forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat][broker] PIP-145: Notifications for faster topic discovery (apa…
…che#16062) * PIP-145: Notifications for faster topic discovery This commit introduces topic list watchers. By using these objects clients can observe the creation or deletion of topics closer to real-time. This reduces latency in consuming the first messages published to a topic when using a pattern-based subscription. Modifications: - New commands were added to the binary protocol to enable registering and deregistering watchers. - Pattern-based consumers create TopicListWatcher objects if the broker supports this feature. Otherwise, they fall back to polling only. - The watchers use ConnectionHandler to obtain a connection to a broker. - Once connected, watchers register and wait for updates. - ServerCnx uses the newly created TopicListService to manage watchers. - TopicListService listens to metadata notifications and sends updates. * Fix checkstyle violation * Fix cpp client compile error * Remove unused code, remove failed watchers * Rename command Unwatch, extract fields from command to avoid concurrent modification * Fix cpp client compile error * Fix cpp client compile error Co-authored-by: Matteo Merli <[email protected]>
- Loading branch information
Showing
30 changed files
with
1,555 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
...ar-broker-common/src/test/java/org/apache/pulsar/broker/resources/TopicResourcesTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/** | ||
* 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.resources; | ||
|
||
import org.apache.pulsar.common.naming.NamespaceName; | ||
import org.apache.pulsar.metadata.api.MetadataStore; | ||
import org.apache.pulsar.metadata.api.Notification; | ||
import org.apache.pulsar.metadata.api.NotificationType; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
import static org.mockito.Mockito.any; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoInteractions; | ||
import static org.mockito.Mockito.verifyNoMoreInteractions; | ||
import java.util.function.BiConsumer; | ||
|
||
public class TopicResourcesTest { | ||
|
||
private MetadataStore metadataStore; | ||
private TopicResources topicResources; | ||
|
||
@BeforeMethod | ||
public void setup() { | ||
metadataStore = mock(MetadataStore.class); | ||
topicResources = new TopicResources(metadataStore); | ||
} | ||
|
||
@Test | ||
public void testConstructorRegistersAsListener() { | ||
verify(metadataStore).registerListener(any()); | ||
} | ||
|
||
@Test | ||
public void testListenerInvokedWhenTopicCreated() { | ||
BiConsumer<String, NotificationType> listener = mock(BiConsumer.class); | ||
topicResources.registerPersistentTopicListener(NamespaceName.get("tenant/namespace"), listener); | ||
topicResources.handleNotification(new Notification(NotificationType.Created, "/managed-ledgers/tenant/namespace/persistent/topic")); | ||
verify(listener).accept("persistent://tenant/namespace/topic", NotificationType.Created); | ||
} | ||
|
||
@Test | ||
public void testListenerInvokedWhenTopicV1Created() { | ||
BiConsumer<String, NotificationType> listener = mock(BiConsumer.class); | ||
topicResources.registerPersistentTopicListener(NamespaceName.get("tenant/cluster/namespace"), listener); | ||
topicResources.handleNotification(new Notification(NotificationType.Created, "/managed-ledgers/tenant/cluster/namespace/persistent/topic")); | ||
verify(listener).accept("persistent://tenant/cluster/namespace/topic", NotificationType.Created); | ||
} | ||
|
||
@Test | ||
public void testListenerInvokedWhenTopicDeleted() { | ||
BiConsumer<String, NotificationType> listener = mock(BiConsumer.class); | ||
topicResources.registerPersistentTopicListener(NamespaceName.get("tenant/namespace"), listener); | ||
topicResources.handleNotification(new Notification(NotificationType.Deleted, "/managed-ledgers/tenant/namespace/persistent/topic")); | ||
verify(listener).accept("persistent://tenant/namespace/topic", NotificationType.Deleted); | ||
} | ||
|
||
@Test | ||
public void testListenerNotInvokedWhenSubscriptionCreated() { | ||
BiConsumer<String, NotificationType> listener = mock(BiConsumer.class); | ||
topicResources.registerPersistentTopicListener(NamespaceName.get("tenant/namespace"), listener); | ||
topicResources.handleNotification(new Notification(NotificationType.Created, "/managed-ledgers/tenant/namespace/persistent/topic/subscription")); | ||
verifyNoInteractions(listener); | ||
} | ||
|
||
@Test | ||
public void testListenerNotInvokedWhenTopicCreatedInOtherNamespace() { | ||
BiConsumer<String, NotificationType> listener = mock(BiConsumer.class); | ||
topicResources.registerPersistentTopicListener(NamespaceName.get("tenant/namespace"), listener); | ||
topicResources.handleNotification(new Notification(NotificationType.Created, "/managed-ledgers/tenant/namespace2/persistent/topic")); | ||
verifyNoInteractions(listener); | ||
} | ||
|
||
@Test | ||
public void testListenerNotInvokedWhenTopicModified() { | ||
BiConsumer<String, NotificationType> listener = mock(BiConsumer.class); | ||
topicResources.registerPersistentTopicListener(NamespaceName.get("tenant/namespace"), listener); | ||
topicResources.handleNotification(new Notification(NotificationType.Modified, "/managed-ledgers/tenant/namespace/persistent/topic")); | ||
verifyNoInteractions(listener); | ||
} | ||
|
||
@Test | ||
public void testListenerNotInvokedAfterDeregistered() { | ||
BiConsumer<String, NotificationType> listener = mock(BiConsumer.class); | ||
topicResources.registerPersistentTopicListener(NamespaceName.get("tenant/namespace"), listener); | ||
topicResources.handleNotification(new Notification(NotificationType.Created, "/managed-ledgers/tenant/namespace/persistent/topic")); | ||
verify(listener).accept("persistent://tenant/namespace/topic", NotificationType.Created); | ||
topicResources.deregisterPersistentTopicListener(listener); | ||
topicResources.handleNotification(new Notification(NotificationType.Created, "/managed-ledgers/tenant/namespace/persistent/topic2")); | ||
verifyNoMoreInteractions(listener); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.