Jason918 commented on a change in pull request #13796:
URL: https://github.com/apache/pulsar/pull/13796#discussion_r816392179



##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/common/naming/NamespaceBundleFactory.java
##########
@@ -306,20 +307,22 @@ private NamespaceBundles getBundles(NamespaceName nsname, 
Optional<Pair<LocalPol
      *            {@link NamespaceBundle} needs to be split
      * @param argNumBundles
      *            split into numBundles
-     * @param splitBoundary
+     * @param splitBoundaries

Review comment:
       This description needs update too. 

##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v2/Namespaces.java
##########
@@ -570,18 +572,34 @@ public void splitNamespaceBundle(
             @PathParam("bundle") String bundleRange,
             @QueryParam("authoritative") @DefaultValue("false") boolean 
authoritative,
             @QueryParam("unload") @DefaultValue("false") boolean unload,
-            @QueryParam("splitAlgorithmName") String splitAlgorithmName) {
-
+            @QueryParam("splitAlgorithmName") String splitAlgorithmName,
+            @ApiParam("splitBoundaries") List<Long> splitBoundaries) {
         try {
             validateNamespaceName(tenant, namespace);
-            internalSplitNamespaceBundle(asyncResponse, bundleRange, 
authoritative, unload, splitAlgorithmName);
+            internalSplitNamespaceBundle(asyncResponse,
+                    bundleRange, authoritative, unload, splitAlgorithmName, 
splitBoundaries);
         } catch (WebApplicationException wae) {
             asyncResponse.resume(wae);
         } catch (Exception e) {
             asyncResponse.resume(new RestException(e));
         }
     }
 
+    @GET
+    @Path("/{tenant}/{namespace}/{bundle}/topicHashPositions")
+    @ApiOperation(value = "Get hash positions for topics")
+    @ApiResponses(value = {
+            @ApiResponse(code = 403, message = "Don't have admin permission"),
+            @ApiResponse(code = 404, message = "Namespace does not exist")})
+    public TopicHashPositions getTopicHashPositions(
+            @PathParam("tenant") String tenant,
+            @PathParam("namespace") String namespace,
+            @PathParam("bundle") String bundleRange,
+            @QueryParam("topicList") List<String> topicList) {
+            validateNamespaceName(tenant, namespace);
+            return internalGetTopicHashPositions(bundleRange, new 
ArrayList<>(topicList));

Review comment:
       Any reason to add `new ArrayList<>`?

##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/broker/admin/v1/Namespaces.java
##########
@@ -651,18 +652,34 @@ public void splitNamespaceBundle(
             @PathParam("namespace") String namespace,
             @PathParam("bundle") String bundleRange,
             @QueryParam("authoritative") @DefaultValue("false") boolean 
authoritative,
-            @QueryParam("unload") @DefaultValue("false") boolean unload) {
+            @QueryParam("unload") @DefaultValue("false") boolean unload,
+            @QueryParam("splitBoundaries") @DefaultValue("") List<Long> 
splitBoundaries) {

Review comment:
       I think we don't need to add support for v1 APIs.

##########
File path: 
pulsar-broker/src/main/java/org/apache/pulsar/common/naming/SpecifiedPositionsBundleSplitAlgorithm.java
##########
@@ -0,0 +1,57 @@
+/**
+ * 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.common.naming;
+
+
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import java.util.stream.Collectors;
+import org.apache.pulsar.broker.namespace.NamespaceService;
+
+/**
+ * This algorithm divides the bundle into several parts by the specified 
positions.
+ */
+public class SpecifiedPositionsBundleSplitAlgorithm implements 
NamespaceBundleSplitAlgorithm{
+    @Override
+    public CompletableFuture<List<Long>> getSplitBoundary(BundleSplitOption 
bundleSplitOption) {
+        NamespaceService service = bundleSplitOption.getService();
+        NamespaceBundle bundle = bundleSplitOption.getBundle();
+        List<Long> positions = bundleSplitOption.getPositions();
+        if (positions == null || positions.size() == 0) {
+            return CompletableFuture.completedFuture(null);
+        }
+        // sort all positions
+        Collections.sort(positions);
+        return 
service.getOwnedTopicListForNamespaceBundle(bundle).thenCompose(topics -> {
+            if (topics == null || topics.size() <= 1) {
+                return CompletableFuture.completedFuture(null);
+            }
+            List<Long> splitBoundaries = positions
+                    .stream()
+                    .filter(position -> position > bundle.getLowerEndpoint() 
&& position < bundle.getUpperEndpoint())

Review comment:
       position >= bundle.getLowerEndpoint() ?

##########
File path: 
pulsar-client-admin-api/src/main/java/org/apache/pulsar/client/admin/Namespaces.java
##########
@@ -2061,10 +2062,11 @@ void setBookieAffinityGroup(String namespace, 
BookieAffinityGroupData bookieAffi
      * @param bundle range of bundle to split
      * @param unloadSplitBundles
      * @param splitAlgorithmName
+     * @param splitBoundaries
      * @throws PulsarAdminException
      */
-    void splitNamespaceBundle(String namespace, String bundle, boolean 
unloadSplitBundles, String splitAlgorithmName)
-            throws PulsarAdminException;
+    void splitNamespaceBundle(String namespace, String bundle, boolean 
unloadSplitBundles,

Review comment:
       Does the value in `splitBoundaries` in the bundle range? 

##########
File path: 
pulsar-broker/src/test/java/org/apache/pulsar/common/naming/SpecifiedPositionsBundleSplitAlgorithmTest.java
##########
@@ -0,0 +1,87 @@
+/**
+ * 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.common.naming;
+
+import com.google.common.collect.Lists;
+import java.util.Arrays;
+import org.apache.pulsar.broker.namespace.NamespaceService;
+import org.testng.annotations.Test;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.concurrent.CompletableFuture;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
+
+public class SpecifiedPositionsBundleSplitAlgorithmTest {
+    @Test
+    public void testNullBoundaries() throws Exception {
+        SpecifiedPositionsBundleSplitAlgorithm algorithm = new 
SpecifiedPositionsBundleSplitAlgorithm();
+        assertNull(algorithm.getSplitBoundary(new BundleSplitOption()).get());
+    }
+
+    @Test
+    public void testTotalTopicsSizeLessThan1() {
+        SpecifiedPositionsBundleSplitAlgorithm algorithm = new 
SpecifiedPositionsBundleSplitAlgorithm();
+        NamespaceService mockNamespaceService = mock(NamespaceService.class);
+        NamespaceBundle mockNamespaceBundle = mock(NamespaceBundle.class);
+        doReturn(CompletableFuture.completedFuture(Lists.newArrayList("a")))
+                
.when(mockNamespaceService).getOwnedTopicListForNamespaceBundle(mockNamespaceBundle);
+        assertNull(algorithm.getSplitBoundary(new 
BundleSplitOption(mockNamespaceService, mockNamespaceBundle,
+                Arrays.asList(1L, 2L))).join());
+    }
+
+    @Test
+    public void testSpecifiedPositionsLessThan1() {
+        SpecifiedPositionsBundleSplitAlgorithm algorithm = new 
SpecifiedPositionsBundleSplitAlgorithm();
+        NamespaceService mockNamespaceService = mock(NamespaceService.class);
+        NamespaceBundle mockNamespaceBundle = mock(NamespaceBundle.class);
+        assertNull(algorithm.getSplitBoundary(
+                new BundleSplitOption(mockNamespaceService, 
mockNamespaceBundle, null)).join());
+        assertNull(algorithm.getSplitBoundary(
+                new BundleSplitOption(mockNamespaceService, 
mockNamespaceBundle, new ArrayList<>())).join());
+    }
+
+    @SuppressWarnings("UnstableApiUsage")
+    @Test
+    public void testAlgorithmReturnCorrectResult() {
+        // -- algorithm
+        SpecifiedPositionsBundleSplitAlgorithm algorithm = new 
SpecifiedPositionsBundleSplitAlgorithm();
+        // -- calculate the mock result
+        NamespaceService mockNamespaceService = mock(NamespaceService.class);
+        NamespaceBundle mockNamespaceBundle = mock(NamespaceBundle.class);
+        doReturn(1L).when(mockNamespaceBundle).getLowerEndpoint();
+        doReturn(1000L).when(mockNamespaceBundle).getUpperEndpoint();
+        doReturn(CompletableFuture.completedFuture(Lists.newArrayList("topic", 
"topic2")))
+                .when(mockNamespaceService)
+                .getOwnedTopicListForNamespaceBundle(mockNamespaceBundle);
+
+        List<Long> positions = Arrays.asList(100L, 200L, 500L, 800L);

Review comment:
       Please add some more corner case, like negative value, LowerEndpoint, 
UpperEndpoint, out of range value, etc.

##########
File path: 
pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdNamespaces.java
##########
@@ -892,7 +897,38 @@ void run() throws PulsarAdminException {
                 throw new ParameterException("--bundle and --bundle-type are 
mutually exclusive");
             }
             bundle = bundleType != null ? bundleType.toString() : bundle;
-            getAdmin().namespaces().splitNamespaceBundle(namespace, bundle, 
unload, splitAlgorithmName);
+            getAdmin().namespaces().splitNamespaceBundle(
+                    namespace, bundle, unload, splitAlgorithmName, 
splitBoundaries);
+        }
+    }
+
+    @Parameters(commandDescription = "Get the positions for one or more 
topic(s) in a namespace bundle")
+    private class GetTopicHashPositions extends CliCommand {
+        @Parameter(description = "tenant/namespace", required = true)
+        private java.util.List<String> params;
+
+        @Parameter(
+                names = { "--bundle", "-b" },
+                description = "{start-boundary}_{end-boundary} format 
namespace bundle",
+                required = false)
+        private String bundle;
+
+        @Parameter(
+                names = { "--topic-list",  "-tl" },
+                description = "The list of topics to get posisions in this 
bunel",

Review comment:
       Can these topic be partitioned topics?

##########
File path: 
pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdNamespaces.java
##########
@@ -878,10 +878,15 @@ void run() throws PulsarAdminException {
         private boolean unload;
 
         @Parameter(names = { "--split-algorithm-name", "-san" }, description = 
"Algorithm name for split "
-                + "namespace bundle. Valid options are: [range_equally_divide, 
topic_count_equally_divide]."
-                + " Use broker side config if absent", required = false)
+                + "namespace bundle. Valid options are: [range_equally_divide, 
topic_count_equally_divide, "
+                + "specified_positions_divide]. Use broker side config if 
absent", required = false)
         private String splitAlgorithmName;
 
+        @Parameter(names = { "--split-boundaries",

Review comment:
       What's format of  list<long> to provide from cli? comma separated ? 
   I think we should add an example here? 

##########
File path: 
pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdNamespaces.java
##########
@@ -892,7 +897,38 @@ void run() throws PulsarAdminException {
                 throw new ParameterException("--bundle and --bundle-type are 
mutually exclusive");
             }
             bundle = bundleType != null ? bundleType.toString() : bundle;
-            getAdmin().namespaces().splitNamespaceBundle(namespace, bundle, 
unload, splitAlgorithmName);
+            getAdmin().namespaces().splitNamespaceBundle(
+                    namespace, bundle, unload, splitAlgorithmName, 
splitBoundaries);
+        }
+    }
+
+    @Parameters(commandDescription = "Get the positions for one or more 
topic(s) in a namespace bundle")
+    private class GetTopicHashPositions extends CliCommand {
+        @Parameter(description = "tenant/namespace", required = true)
+        private java.util.List<String> params;
+
+        @Parameter(
+                names = { "--bundle", "-b" },
+                description = "{start-boundary}_{end-boundary} format 
namespace bundle",
+                required = false)
+        private String bundle;
+
+        @Parameter(
+                names = { "--topic-list",  "-tl" },
+                description = "The list of topics to get posisions in this 
bunel",

Review comment:
       ```suggestion
                   description = "The list of topics to get position in this 
bundle",
   ```




-- 
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: commits-unsubscr...@pulsar.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to