Demogorgon314 commented on code in PR #21085:
URL: https://github.com/apache/pulsar/pull/21085#discussion_r1310027075


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/models/TopKBundles.java:
##########
@@ -132,6 +133,42 @@ static void partitionSort(List<Map.Entry<String, ? extends 
Comparable>> arr, int
         Collections.sort(arr.subList(0, end), (a, b) -> 
b.getValue().compareTo(a.getValue()));
     }
 
+    public static <T> void partitionSort(List<Map.Entry<String, T>> arr, int 
k, Comparator<T> comparator) {
+        int start = 0;
+        int end = arr.size() - 1;
+        int target = k - 1;
+        while (start < end) {
+            int lo = start;
+            int hi = end;
+            int mid = lo;
+            var pivot = arr.get(hi).getValue();
+            while (mid <= hi) {
+                int cmp = comparator.compare(pivot, arr.get(mid).getValue());
+                if (cmp < 0) {
+                    var tmp = arr.get(lo);
+                    arr.set(lo++, arr.get(mid));
+                    arr.set(mid++, tmp);
+                } else if (cmp > 0) {
+                    var tmp = arr.get(mid);
+                    arr.set(mid, arr.get(hi));
+                    arr.set(hi--, tmp);
+                } else {
+                    mid++;
+                }
+            }
+            if (lo <= target && target < mid) {
+                end = lo;
+                break;
+            }
+            if (target < lo) {
+                end = lo - 1;
+            } else {
+                start = mid;
+            }
+        }
+        Collections.sort(arr.subList(0, end), (a, b) -> 
comparator.compare(b.getValue(), a.getValue()));

Review Comment:
   ```suggestion
           arr.subList(0, end).sort((a, b) -> comparator.compare(b.getValue(), 
a.getValue()));
   ```



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/ModularLoadManagerImpl.java:
##########
@@ -1139,6 +1145,30 @@ public void writeBrokerDataOnZooKeeper(boolean force) {
         }
     }
 
+
+    /**
+     * sort bundles by load and select topK bundles for each broker.
+     * @return the number of bundles selected
+     */
+    private CompletableFuture<Integer> selectTopKBundle() {
+        CompletableFuture completableFuture = new CompletableFuture();

Review Comment:
   ```suggestion
           CompletableFuture<Integer> completableFuture = new 
CompletableFuture<>();
   ```



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/impl/ModularLoadManagerImpl.java:
##########
@@ -1139,6 +1145,30 @@ public void writeBrokerDataOnZooKeeper(boolean force) {
         }
     }
 
+
+    /**
+     * sort bundles by load and select topK bundles for each broker.
+     * @return the number of bundles selected
+     */
+    private CompletableFuture<Integer> selectTopKBundle() {
+        CompletableFuture completableFuture = new CompletableFuture();
+
+        executors.execute(() -> {
+            // make the bundle-data update and sorting executed in single 
thread
+            bundleArr.clear();
+            bundleArr.addAll(loadData.getBundleData().entrySet());
+
+            // select topK bundle for each broker, so select topK * 
brokerCount bundle in total
+            int brokerCount = Math.max(1, loadData.getBrokerData().size());
+            int updateBundleCount = Math.min(pulsar.getConfiguration()
+                    .getLoadBalancerMaxNumberOfBundlesInBundleLoadReport() * 
brokerCount, bundleArr.size());
+
+            TopKBundles.partitionSort(bundleArr, updateBundleCount, 
bundleDataComparator);

Review Comment:
   Should we select the topK bundles for each broker?



##########
pulsar-common/src/main/java/org/apache/pulsar/common/util/BundleDataComparator.java:
##########
@@ -0,0 +1,36 @@
+/*
+ * 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.util;
+
+import java.io.Serializable;
+import java.util.Comparator;
+import org.apache.pulsar.policies.data.loadbalancer.BundleData;
+
+public class BundleDataComparator implements Comparator<BundleData>, 
Serializable {

Review Comment:
   Can we implement the `Comparable<BundleData>` interface for `BundleData` 
class?



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to