|
| 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.broker.resources; |
| 20 | + |
| 21 | +import static org.testng.Assert.assertEquals; |
| 22 | +import static org.testng.Assert.assertTrue; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.HashSet; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.Set; |
| 28 | +import org.apache.pulsar.common.naming.NamespaceName; |
| 29 | +import org.apache.pulsar.common.naming.TopicName; |
| 30 | +import org.apache.pulsar.metadata.api.MetadataStoreConfig; |
| 31 | +import org.apache.pulsar.metadata.api.extended.MetadataStoreExtended; |
| 32 | +import org.apache.pulsar.metadata.impl.LocalMemoryMetadataStore; |
| 33 | +import org.testng.annotations.AfterMethod; |
| 34 | +import org.testng.annotations.BeforeMethod; |
| 35 | +import org.testng.annotations.Test; |
| 36 | + |
| 37 | +/** |
| 38 | + * Coverage for the {@code findScalableTopicsByPropertyAsync} entry point on |
| 39 | + * {@link ScalableTopicResources}: verifies that topic properties registered at |
| 40 | + * create/update time are queryable via the secondary-index API, that updates |
| 41 | + * refresh the index, and that the filter is correctly scoped to a namespace. |
| 42 | + * |
| 43 | + * <p>Uses {@link LocalMemoryMetadataStore} which does not implement native |
| 44 | + * secondary indexes, so this exercises the fallback scan + per-record property |
| 45 | + * predicate path. The Oxia-native path (where the index is consulted directly) |
| 46 | + * is covered by the metadata-store-level secondary index tests. |
| 47 | + */ |
| 48 | +public class ScalableTopicPropertyIndexTest { |
| 49 | + |
| 50 | + private MetadataStoreExtended store; |
| 51 | + private ScalableTopicResources resources; |
| 52 | + |
| 53 | + @BeforeMethod |
| 54 | + public void setUp() throws Exception { |
| 55 | + store = new LocalMemoryMetadataStore("memory:local", |
| 56 | + MetadataStoreConfig.builder().build()); |
| 57 | + resources = new ScalableTopicResources(store, 30); |
| 58 | + } |
| 59 | + |
| 60 | + @AfterMethod(alwaysRun = true) |
| 61 | + public void tearDown() throws Exception { |
| 62 | + if (store != null) { |
| 63 | + store.close(); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private TopicName topicIn(NamespaceName ns, String localName) { |
| 68 | + return TopicName.get("topic://" + ns + "/" + localName); |
| 69 | + } |
| 70 | + |
| 71 | + private ScalableTopicMetadata metaWithProps(Map<String, String> props) { |
| 72 | + return ScalableTopicMetadata.builder() |
| 73 | + .epoch(0) |
| 74 | + .nextSegmentId(1) |
| 75 | + .properties(new HashMap<>(props)) |
| 76 | + .build(); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void findsTopicByExactPropertyValue() throws Exception { |
| 81 | + NamespaceName ns = NamespaceName.get("tenant/ns-a"); |
| 82 | + |
| 83 | + resources.createScalableTopicAsync(topicIn(ns, "t-alice"), |
| 84 | + metaWithProps(Map.of("owner", "alice", "team", "platform"))).get(); |
| 85 | + resources.createScalableTopicAsync(topicIn(ns, "t-bob"), |
| 86 | + metaWithProps(Map.of("owner", "bob", "team", "platform"))).get(); |
| 87 | + resources.createScalableTopicAsync(topicIn(ns, "t-carol"), |
| 88 | + metaWithProps(Map.of("owner", "carol", "team", "data"))).get(); |
| 89 | + |
| 90 | + // Filter by owner=alice — only t-alice matches. |
| 91 | + List<String> aliceOwned = resources |
| 92 | + .findScalableTopicsByPropertyAsync(ns, "owner", "alice").get(); |
| 93 | + assertEquals(aliceOwned, List.of("topic://tenant/ns-a/t-alice")); |
| 94 | + |
| 95 | + // Filter by team=platform — both alice and bob. |
| 96 | + Set<String> platform = new HashSet<>(resources |
| 97 | + .findScalableTopicsByPropertyAsync(ns, "team", "platform").get()); |
| 98 | + assertEquals(platform, Set.of( |
| 99 | + "topic://tenant/ns-a/t-alice", |
| 100 | + "topic://tenant/ns-a/t-bob")); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void findIsScopedToNamespace() throws Exception { |
| 105 | + NamespaceName nsA = NamespaceName.get("tenant/ns-a"); |
| 106 | + NamespaceName nsB = NamespaceName.get("tenant/ns-b"); |
| 107 | + |
| 108 | + // Same property in two namespaces — find must return only the one we asked for. |
| 109 | + resources.createScalableTopicAsync(topicIn(nsA, "t1"), |
| 110 | + metaWithProps(Map.of("owner", "alice"))).get(); |
| 111 | + resources.createScalableTopicAsync(topicIn(nsB, "t2"), |
| 112 | + metaWithProps(Map.of("owner", "alice"))).get(); |
| 113 | + |
| 114 | + List<String> inNsA = resources |
| 115 | + .findScalableTopicsByPropertyAsync(nsA, "owner", "alice").get(); |
| 116 | + assertEquals(inNsA, List.of("topic://tenant/ns-a/t1")); |
| 117 | + |
| 118 | + List<String> inNsB = resources |
| 119 | + .findScalableTopicsByPropertyAsync(nsB, "owner", "alice").get(); |
| 120 | + assertEquals(inNsB, List.of("topic://tenant/ns-b/t2")); |
| 121 | + } |
| 122 | + |
| 123 | + @Test |
| 124 | + public void noMatchReturnsEmptyList() throws Exception { |
| 125 | + NamespaceName ns = NamespaceName.get("tenant/ns-empty"); |
| 126 | + resources.createScalableTopicAsync(topicIn(ns, "t1"), |
| 127 | + metaWithProps(Map.of("owner", "alice"))).get(); |
| 128 | + |
| 129 | + // Wrong value |
| 130 | + assertTrue(resources.findScalableTopicsByPropertyAsync(ns, "owner", "bob") |
| 131 | + .get().isEmpty()); |
| 132 | + |
| 133 | + // Wrong key |
| 134 | + assertTrue(resources.findScalableTopicsByPropertyAsync(ns, "team", "alice") |
| 135 | + .get().isEmpty()); |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + public void updateRefreshesIndex() throws Exception { |
| 140 | + NamespaceName ns = NamespaceName.get("tenant/ns-update"); |
| 141 | + TopicName tn = topicIn(ns, "t-mutating"); |
| 142 | + |
| 143 | + resources.createScalableTopicAsync(tn, |
| 144 | + metaWithProps(Map.of("owner", "alice"))).get(); |
| 145 | + assertEquals(resources.findScalableTopicsByPropertyAsync(ns, "owner", "alice").get(), |
| 146 | + List.of(tn.toString())); |
| 147 | + |
| 148 | + // Reassign owner via update — the new owner must be queryable, and the |
| 149 | + // old owner's entry must no longer match this topic. |
| 150 | + resources.updateScalableTopicAsync(tn, current -> { |
| 151 | + current.getProperties().put("owner", "bob"); |
| 152 | + return current; |
| 153 | + }).get(); |
| 154 | + |
| 155 | + assertEquals(resources.findScalableTopicsByPropertyAsync(ns, "owner", "bob").get(), |
| 156 | + List.of(tn.toString())); |
| 157 | + assertTrue(resources.findScalableTopicsByPropertyAsync(ns, "owner", "alice") |
| 158 | + .get().isEmpty()); |
| 159 | + } |
| 160 | + |
| 161 | + @Test |
| 162 | + public void topicWithoutPropertiesIsNotMatched() throws Exception { |
| 163 | + NamespaceName ns = NamespaceName.get("tenant/ns-noprops"); |
| 164 | + resources.createScalableTopicAsync(topicIn(ns, "t-anon"), |
| 165 | + metaWithProps(Map.of())).get(); |
| 166 | + resources.createScalableTopicAsync(topicIn(ns, "t-tagged"), |
| 167 | + metaWithProps(Map.of("owner", "alice"))).get(); |
| 168 | + |
| 169 | + // Filtering by any property must skip the un-tagged record. |
| 170 | + List<String> matches = resources |
| 171 | + .findScalableTopicsByPropertyAsync(ns, "owner", "alice").get(); |
| 172 | + assertEquals(matches, List.of("topic://tenant/ns-noprops/t-tagged")); |
| 173 | + } |
| 174 | +} |
0 commit comments