You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by lh...@apache.org on 2026/09/23 12:37:03 UTC

(pulsar) 13/21: [fix][client] Enable TableView compacted reads for short topic names (#26626)

This is an automated email from the ASF dual-hosted git repository.

lhotari pushed a commit to branch branch-4.0
in repository https://gitbox.apache.org/repos/asf/pulsar.git

commit 220d5bd6cdd9a927270fe55dc61406a95c679b30
Author: Lari Hotari <lh...@users.noreply.github.com>
AuthorDate: Thu Sep 17 13:51:37 2026 +0300

    [fix][client] Enable TableView compacted reads for short topic names (#26626)
    
    (cherry picked from commit 6c8fc014a7322e2325add9805a531d96ef1274ea)
---
 .../apache/pulsar/client/impl/TableViewImpl.java   |  3 +-
 .../client/impl/TableViewBuilderImplTest.java      |  2 +
 .../pulsar/client/impl/TableViewImplTest.java      | 48 ++++++++++++++++++++++
 3 files changed, 52 insertions(+), 1 deletion(-)

diff --git a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/TableViewImpl.java b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/TableViewImpl.java
index 998dbafd73c3..b9ebf3f3bac7 100644
--- a/pulsar-client/src/main/java/org/apache/pulsar/client/impl/TableViewImpl.java
+++ b/pulsar-client/src/main/java/org/apache/pulsar/client/impl/TableViewImpl.java
@@ -44,6 +44,7 @@ import org.apache.pulsar.client.api.Schema;
 import org.apache.pulsar.client.api.TableView;
 import org.apache.pulsar.client.api.TopicMessageId;
 import org.apache.pulsar.common.naming.TopicDomain;
+import org.apache.pulsar.common.naming.TopicName;
 import org.apache.pulsar.common.topics.TopicCompactionStrategy;
 
 @Slf4j
@@ -83,7 +84,7 @@ public class TableViewImpl<T> implements TableView<T> {
 
     TableViewImpl(PulsarClientImpl client, Schema<T> schema, TableViewConfigurationData conf) {
         this.conf = conf;
-        this.isPersistentTopic = conf.getTopicName().startsWith(TopicDomain.persistent.toString());
+        this.isPersistentTopic = TopicName.get(conf.getTopicName()).getDomain() == TopicDomain.persistent;
         this.data = new ConcurrentHashMap<>();
         this.immutableData = Collections.unmodifiableMap(data);
         this.listeners = new ArrayList<>();
diff --git a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/TableViewBuilderImplTest.java b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/TableViewBuilderImplTest.java
index e2becec1105e..1860ea73eb8a 100644
--- a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/TableViewBuilderImplTest.java
+++ b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/TableViewBuilderImplTest.java
@@ -22,6 +22,7 @@ import static org.mockito.Mockito.any;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 import static org.testng.Assert.assertNotNull;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 import java.util.concurrent.CompletableFuture;
@@ -52,6 +53,7 @@ public class TableViewBuilderImplTest {
         Reader reader = mock(Reader.class);
         readNextFuture = new CompletableFuture();
         when(reader.readNextAsync()).thenReturn(readNextFuture);
+        when(reader.getLastMessageIdsAsync()).thenReturn(CompletableFuture.completedFuture(Collections.emptyList()));
         client = mock(PulsarClientImpl.class);
         ConnectionPool connectionPool = mock(ConnectionPool.class);
         when(client.getCnxPool()).thenReturn(connectionPool);
diff --git a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/TableViewImplTest.java b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/TableViewImplTest.java
index 02f4365e8a73..a2c8551de098 100644
--- a/pulsar-client/src/test/java/org/apache/pulsar/client/impl/TableViewImplTest.java
+++ b/pulsar-client/src/test/java/org/apache/pulsar/client/impl/TableViewImplTest.java
@@ -23,6 +23,8 @@ import static org.mockito.Mockito.any;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.mockStatic;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertFalse;
@@ -30,6 +32,7 @@ import static org.testng.Assert.assertNotNull;
 import static org.testng.Assert.assertNull;
 import static org.testng.Assert.assertTrue;
 import java.util.Collections;
+import java.util.List;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.ExecutorService;
@@ -74,6 +77,51 @@ public class TableViewImplTest {
 
         assertNotNull(tableView);
     }
+    @DataProvider
+    public Object[][] topicNames() {
+        return new Object[][]{
+                {"persistent://tenant/ns/topic", true},
+                {"tenant/ns/topic", true},
+                {"topic", true},
+                {"persistent-topic", true},
+                {"non-persistent://tenant/ns/topic", false}
+        };
+    }
+
+    @Test(timeOut = 10_000, dataProvider = "topicNames")
+    @SuppressWarnings("unchecked")
+    public void testTopicDomain(String topic, boolean persistent) throws Exception {
+        PulsarClientImpl client = mock(PulsarClientImpl.class);
+        ReaderBuilder<String> builder = mock(ReaderBuilder.class, RETURNS_SELF);
+        Reader<String> reader = mock(Reader.class);
+        when(client.newReader(Schema.STRING)).thenReturn(builder);
+        when(builder.createAsync()).thenReturn(CompletableFuture.completedFuture(reader));
+        when(reader.closeAsync()).thenReturn(CompletableFuture.completedFuture(null));
+        CompletableFuture<List<TopicMessageId>> lastMessageIds =
+                new CompletableFuture<>();
+        when(reader.getLastMessageIdsAsync()).thenReturn(lastMessageIds);
+        when(reader.readNextAsync()).thenReturn(new CompletableFuture<>());
+        TableViewConfigurationData conf = new TableViewConfigurationData();
+        conf.setTopicName(topic);
+        try (TableViewImpl<String> tableView = new TableViewImpl<>(client, Schema.STRING, conf)) {
+            if (persistent) {
+                verify(builder).readCompacted(true);
+            } else {
+                verify(builder, never()).readCompacted(true);
+            }
+            CompletableFuture<TableView<String>> start = tableView.start();
+            assertEquals(start.isDone(), !persistent,
+                    "Persistent topics must wait for the initial replay");
+            lastMessageIds.complete(Collections.emptyList());
+            assertEquals(start.get(5, TimeUnit.SECONDS), tableView);
+            if (persistent) {
+                verify(reader).getLastMessageIdsAsync();
+            } else {
+                verify(reader, never()).getLastMessageIdsAsync();
+            }
+        }
+    }
+
     @DataProvider
     public Object[][] skippedMessage() {
         return new Object[][]{{false}, {true}};