heesung-sn commented on code in PR #19622:
URL: https://github.com/apache/pulsar/pull/19622#discussion_r1128870204


##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/channel/ServiceUnitStateChannelImpl.java:
##########
@@ -847,7 +847,6 @@ protected void 
splitServiceUnitOnceAndRetry(NamespaceService namespaceService,
             return null;
         });
     }
-

Review Comment:
   Updated.



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/strategy/DefaultNamespaceBundleSplitStrategyImpl.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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.loadbalance.extensions.strategy;
+
+import static 
org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Label.Failure;
+import static 
org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Reason.Bandwidth;
+import static 
org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Reason.MsgRate;
+import static 
org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Reason.Sessions;
+import static 
org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Reason.Topics;
+import static 
org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Reason.Unknown;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.broker.PulsarService;
+import org.apache.pulsar.broker.ServiceConfiguration;
+import org.apache.pulsar.broker.loadbalance.extensions.LoadManagerContext;
+import org.apache.pulsar.broker.loadbalance.extensions.models.Split;
+import org.apache.pulsar.broker.loadbalance.extensions.models.SplitCounter;
+import org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision;
+import org.apache.pulsar.broker.loadbalance.impl.LoadManagerShared;
+import org.apache.pulsar.common.naming.NamespaceBundleFactory;
+import org.apache.pulsar.common.naming.NamespaceName;
+import org.apache.pulsar.policies.data.loadbalancer.NamespaceBundleStats;
+
+/**
+ * Determines which bundles should be split based on various thresholds.
+ *
+ * Migrate from {@link 
org.apache.pulsar.broker.loadbalance.impl.BundleSplitterTask}
+ */
+@Slf4j
+public class DefaultNamespaceBundleSplitStrategyImpl implements 
NamespaceBundleSplitStrategy {
+    private final Set<SplitDecision> decisionCache;
+    private final Map<String, Integer> namespaceBundleCount;
+    private final Map<String, Integer> bundleHighTrafficFrequency;
+    private final SplitCounter counter;
+
+    public DefaultNamespaceBundleSplitStrategyImpl(SplitCounter counter) {
+        decisionCache = new HashSet<>();
+        namespaceBundleCount = new HashMap<>();
+        bundleHighTrafficFrequency = new HashMap<>();
+        this.counter = counter;
+
+    }
+
+    @Override
+    public Set<SplitDecision> findBundlesToSplit(LoadManagerContext context, 
PulsarService pulsar) {
+        decisionCache.clear();
+        namespaceBundleCount.clear();
+        final ServiceConfiguration conf = pulsar.getConfiguration();
+        int maxBundleCount = conf.getLoadBalancerNamespaceMaximumBundles();
+        long maxBundleTopics = conf.getLoadBalancerNamespaceBundleMaxTopics();
+        long maxBundleSessions = 
conf.getLoadBalancerNamespaceBundleMaxSessions();
+        long maxBundleMsgRate = 
conf.getLoadBalancerNamespaceBundleMaxMsgRate();
+        long maxBundleBandwidth = 
conf.getLoadBalancerNamespaceBundleMaxBandwidthMbytes() * 
LoadManagerShared.MIBI;
+        long maxSplitCount = 
conf.getLoadBalancerMaxNumberOfBundlesToSplitPerCycle();
+        long splitConditionThreshold = 
conf.getLoadBalancerNamespaceBundleSplitConditionThreshold();
+        boolean debug = log.isDebugEnabled() || 
conf.isLoadBalancerDebugModeEnabled();
+
+        Map<String, NamespaceBundleStats> bundleStatsMap = 
pulsar.getBrokerService().getBundleStats();
+        NamespaceBundleFactory namespaceBundleFactory =
+                pulsar.getNamespaceService().getNamespaceBundleFactory();
+
+        // clean bundleHighTrafficFrequency
+        var bundleHighTrafficIterator =
+                bundleHighTrafficFrequency.entrySet().iterator();
+        while (bundleHighTrafficIterator.hasNext()) {
+            String bundle = bundleHighTrafficIterator.next().getKey();
+            if (!bundleStatsMap.containsKey(bundle)) {
+                bundleHighTrafficIterator.remove();
+            }
+        }

Review Comment:
   Updated.



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/scheduler/SplitScheduler.java:
##########
@@ -0,0 +1,172 @@
+/*
+ * 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.loadbalance.extensions.scheduler;
+
+import static 
org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Label.Failure;
+import static 
org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Label.Success;
+import static 
org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Reason.Unknown;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.broker.PulsarService;
+import org.apache.pulsar.broker.ServiceConfiguration;
+import org.apache.pulsar.broker.loadbalance.extensions.LoadManagerContext;
+import 
org.apache.pulsar.broker.loadbalance.extensions.channel.ServiceUnitStateChannel;
+import org.apache.pulsar.broker.loadbalance.extensions.models.SplitCounter;
+import org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision;
+import 
org.apache.pulsar.broker.loadbalance.extensions.strategy.DefaultNamespaceBundleSplitStrategyImpl;
+import 
org.apache.pulsar.broker.loadbalance.extensions.strategy.NamespaceBundleSplitStrategy;
+import org.apache.pulsar.common.stats.Metrics;
+import org.apache.pulsar.common.util.FutureUtil;
+
+/**
+ * Service Unit(e.g. bundles) Split scheduler.
+ */
+@Slf4j
+public class SplitScheduler implements LoadManagerScheduler {
+
+    private final PulsarService pulsar;
+
+    private final ScheduledExecutorService loadManagerExecutor;
+
+    private final LoadManagerContext context;
+
+    private final ServiceConfiguration conf;
+
+    private final ServiceUnitStateChannel serviceUnitStateChannel;
+
+    private final NamespaceBundleSplitStrategy bundleSplitStrategy;
+
+    private final SplitCounter counter;
+
+    private final AtomicReference<List<Metrics>> splitMetrics;
+
+    private volatile ScheduledFuture<?> task;
+
+    private long counterLastUpdatedAt = 0;
+
+    public SplitScheduler(PulsarService pulsar,
+                          ServiceUnitStateChannel serviceUnitStateChannel,
+                          SplitCounter counter,
+                          AtomicReference<List<Metrics>> splitMetrics,
+                          LoadManagerContext context,
+                          NamespaceBundleSplitStrategy bundleSplitStrategy) {
+        this.pulsar = pulsar;
+        this.loadManagerExecutor = pulsar.getLoadManagerExecutor();
+        this.counter = counter;
+        this.splitMetrics = splitMetrics;
+        this.context = context;
+        this.conf = pulsar.getConfiguration();
+        this.bundleSplitStrategy = bundleSplitStrategy;
+        this.serviceUnitStateChannel = serviceUnitStateChannel;
+    }
+
+    public SplitScheduler(PulsarService pulsar,
+                          ServiceUnitStateChannel serviceUnitStateChannel,
+                          SplitCounter counter,
+                          AtomicReference<List<Metrics>> splitMetrics,
+                          LoadManagerContext context) {
+        this(pulsar, serviceUnitStateChannel, counter, splitMetrics, context,
+                new DefaultNamespaceBundleSplitStrategyImpl(counter));
+    }
+
+    @Override
+    public void execute() {
+        boolean debugMode = conf.isLoadBalancerDebugModeEnabled() || 
log.isDebugEnabled();
+        if (debugMode) {
+            log.info("Load balancer enabled: {}, Split enabled: {}.",
+                    conf.isLoadBalancerEnabled(), 
conf.isLoadBalancerAutoBundleSplitEnabled());
+        }
+
+        if (!isLoadBalancerAutoBundleSplitEnabled()) {
+            if (debugMode) {
+                log.info("The load balancer or load balancer split already 
disabled. Skipping.");
+            }
+            return;
+        }
+
+        synchronized (bundleSplitStrategy) {
+            final Set<SplitDecision> decisions = 
bundleSplitStrategy.findBundlesToSplit(context, pulsar);
+            if (!decisions.isEmpty()) {
+                List<CompletableFuture<Void>> futures = new ArrayList<>();
+                for (SplitDecision decision : decisions) {
+                    if (decision.getLabel() == Success) {
+                        var split = decision.getSplit();
+                        
futures.add(serviceUnitStateChannel.publishSplitEventAsync(split)
+                                .whenComplete((__, e) -> {
+                                    if (e == null) {
+                                        counter.update(decision);
+                                        log.info("Published Split Event for 
{}", split);
+                                    } else {
+                                        counter.update(Failure, Unknown);
+                                        log.error("Failed to publish Split 
Event for {}", split);
+                                    }
+                                }));
+                    }
+                }
+                FutureUtil.waitForAll(futures).exceptionally(ex -> {
+                    log.error("Failed to wait for split events to persist.", 
ex);
+                    counter.update(Failure, Unknown);
+                    return null;
+                });
+            } else {
+                if (debugMode) {
+                    log.info("BundleSplitStrategy returned no bundles to 
split.");
+                }
+            }
+        }
+
+        if (counter.updatedAt() > counterLastUpdatedAt) {
+            splitMetrics.set(counter.toMetrics(pulsar.getAdvertisedAddress()));

Review Comment:
   To be consistent with other metrics code, here we use 
`pulsar.getAdvertisedAddress()`



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/strategy/DefaultNamespaceBundleSplitStrategyImpl.java:
##########
@@ -0,0 +1,178 @@
+/*
+ * 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.loadbalance.extensions.strategy;
+
+import static 
org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Label.Failure;
+import static 
org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Reason.Bandwidth;
+import static 
org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Reason.MsgRate;
+import static 
org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Reason.Sessions;
+import static 
org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Reason.Topics;
+import static 
org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Reason.Unknown;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.broker.PulsarService;
+import org.apache.pulsar.broker.ServiceConfiguration;
+import org.apache.pulsar.broker.loadbalance.extensions.LoadManagerContext;
+import org.apache.pulsar.broker.loadbalance.extensions.models.Split;
+import org.apache.pulsar.broker.loadbalance.extensions.models.SplitCounter;
+import org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision;
+import org.apache.pulsar.broker.loadbalance.impl.LoadManagerShared;
+import org.apache.pulsar.common.naming.NamespaceBundleFactory;
+import org.apache.pulsar.common.naming.NamespaceName;
+import org.apache.pulsar.policies.data.loadbalancer.NamespaceBundleStats;
+
+/**
+ * Determines which bundles should be split based on various thresholds.
+ *
+ * Migrate from {@link 
org.apache.pulsar.broker.loadbalance.impl.BundleSplitterTask}
+ */
+@Slf4j
+public class DefaultNamespaceBundleSplitStrategyImpl implements 
NamespaceBundleSplitStrategy {
+    private final Set<SplitDecision> decisionCache;
+    private final Map<String, Integer> namespaceBundleCount;
+    private final Map<String, Integer> bundleHighTrafficFrequency;
+    private final SplitCounter counter;
+
+    public DefaultNamespaceBundleSplitStrategyImpl(SplitCounter counter) {
+        decisionCache = new HashSet<>();
+        namespaceBundleCount = new HashMap<>();
+        bundleHighTrafficFrequency = new HashMap<>();
+        this.counter = counter;
+
+    }
+
+    @Override
+    public Set<SplitDecision> findBundlesToSplit(LoadManagerContext context, 
PulsarService pulsar) {
+        decisionCache.clear();
+        namespaceBundleCount.clear();
+        final ServiceConfiguration conf = pulsar.getConfiguration();
+        int maxBundleCount = conf.getLoadBalancerNamespaceMaximumBundles();
+        long maxBundleTopics = conf.getLoadBalancerNamespaceBundleMaxTopics();
+        long maxBundleSessions = 
conf.getLoadBalancerNamespaceBundleMaxSessions();
+        long maxBundleMsgRate = 
conf.getLoadBalancerNamespaceBundleMaxMsgRate();
+        long maxBundleBandwidth = 
conf.getLoadBalancerNamespaceBundleMaxBandwidthMbytes() * 
LoadManagerShared.MIBI;
+        long maxSplitCount = 
conf.getLoadBalancerMaxNumberOfBundlesToSplitPerCycle();
+        long splitConditionThreshold = 
conf.getLoadBalancerNamespaceBundleSplitConditionThreshold();
+        boolean debug = log.isDebugEnabled() || 
conf.isLoadBalancerDebugModeEnabled();
+
+        Map<String, NamespaceBundleStats> bundleStatsMap = 
pulsar.getBrokerService().getBundleStats();
+        NamespaceBundleFactory namespaceBundleFactory =
+                pulsar.getNamespaceService().getNamespaceBundleFactory();
+
+        // clean bundleHighTrafficFrequency
+        var bundleHighTrafficIterator =
+                bundleHighTrafficFrequency.entrySet().iterator();
+        while (bundleHighTrafficIterator.hasNext()) {
+            String bundle = bundleHighTrafficIterator.next().getKey();
+            if (!bundleStatsMap.containsKey(bundle)) {
+                bundleHighTrafficIterator.remove();
+            }
+        }
+
+        for (var entry : bundleStatsMap.entrySet()) {
+            final String bundle = entry.getKey();
+            final NamespaceBundleStats stats = entry.getValue();
+            if (stats.topics < 2) {
+                if (debug) {
+                    log.info("The count of topics on the bundle {} is less 
than 2, skip split!", bundle);
+                }
+                continue;
+            }
+
+            final String namespaceName = 
LoadManagerShared.getNamespaceNameFromBundleName(bundle);
+            final String bundleRange = 
LoadManagerShared.getBundleRangeFromBundleName(bundle);
+            if (!namespaceBundleFactory
+                    
.canSplitBundle(namespaceBundleFactory.getBundle(namespaceName, bundleRange))) {
+                if (debug) {
+                    log.info("Can't split the bundle:{}. invalid bundle 
range:{}. ", bundle, bundleRange);
+                }
+                counter.update(Failure, Unknown);
+                continue;
+            }
+
+            double totalMessageRate = stats.msgRateIn + stats.msgRateOut;
+            double totalMessageThroughput = stats.msgThroughputIn + 
stats.msgThroughputOut;
+            int totalSessionCount = stats.consumerCount + stats.producerCount;
+            SplitDecision.Reason reason = Unknown;
+            if (stats.topics > maxBundleTopics) {
+                reason = Topics;
+            } else if (maxBundleSessions > 0 && (totalSessionCount > 
maxBundleSessions)) {
+                reason = Sessions;
+            } else if (totalMessageRate > maxBundleMsgRate) {
+                reason = MsgRate;
+            } else if (totalMessageThroughput > maxBundleBandwidth) {
+                reason = Bandwidth;
+            }
+
+            if (reason != Unknown) {
+                bundleHighTrafficFrequency.put(bundle, 
bundleHighTrafficFrequency.getOrDefault(bundle, 0) + 1);
+            } else {
+                bundleHighTrafficFrequency.remove(bundle);
+            }
+
+            if (bundleHighTrafficFrequency.getOrDefault(bundle, 0) > 
splitConditionThreshold) {
+                final String namespace = 
LoadManagerShared.getNamespaceNameFromBundleName(bundle);
+                try {
+                    final int bundleCount = pulsar.getNamespaceService()
+                            .getBundleCount(NamespaceName.get(namespace));
+                    if ((bundleCount + 
namespaceBundleCount.getOrDefault(namespace, 0))
+                            < maxBundleCount) {
+                        if (debug) {
+                            log.info("The bundle {} is considered to split. 
Topics: {}/{}, Sessions: ({}+{})/{}, "
+                                            + "Message Rate: {}/{} (msgs/s), 
Message Throughput: {}/{} (MB/s)",
+                                    bundle, stats.topics, maxBundleTopics, 
stats.producerCount, stats.consumerCount,
+                                    maxBundleSessions, totalMessageRate, 
maxBundleMsgRate,
+                                    totalMessageThroughput / 
LoadManagerShared.MIBI,
+                                    maxBundleBandwidth / 
LoadManagerShared.MIBI);
+                        }
+                        var decision = new SplitDecision();
+                        decision.setSplit(new Split(bundle, 
context.brokerRegistry().getBrokerId(), new HashMap<>()));
+                        decision.succeed(reason);
+                        decisionCache.add(decision);
+                        int bundleNum = 
namespaceBundleCount.getOrDefault(namespace, 0);
+                        namespaceBundleCount.put(namespace, bundleNum + 1);
+                        bundleHighTrafficFrequency.remove(bundle);
+                        // Clear namespace bundle-cache
+                        
namespaceBundleFactory.invalidateBundleCache(NamespaceName.get(namespaceName));
+                        if (decisionCache.size() == maxSplitCount) {
+                            if (debug) {
+                                log.info("Too many bundles to split in this 
split cycle {} / {}. Stop.",
+                                        decisionCache.size(), maxSplitCount);
+                            }
+                            break;
+                        }
+                    } else {

Review Comment:
   No. `Failure` means some system failure.



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/scheduler/SplitScheduler.java:
##########
@@ -0,0 +1,172 @@
+/*
+ * 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.loadbalance.extensions.scheduler;
+
+import static 
org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Label.Failure;
+import static 
org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Label.Success;
+import static 
org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Reason.Unknown;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.broker.PulsarService;
+import org.apache.pulsar.broker.ServiceConfiguration;
+import org.apache.pulsar.broker.loadbalance.extensions.LoadManagerContext;
+import 
org.apache.pulsar.broker.loadbalance.extensions.channel.ServiceUnitStateChannel;
+import org.apache.pulsar.broker.loadbalance.extensions.models.SplitCounter;
+import org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision;
+import 
org.apache.pulsar.broker.loadbalance.extensions.strategy.DefaultNamespaceBundleSplitStrategyImpl;
+import 
org.apache.pulsar.broker.loadbalance.extensions.strategy.NamespaceBundleSplitStrategy;
+import org.apache.pulsar.common.stats.Metrics;
+import org.apache.pulsar.common.util.FutureUtil;
+
+/**
+ * Service Unit(e.g. bundles) Split scheduler.
+ */
+@Slf4j
+public class SplitScheduler implements LoadManagerScheduler {
+
+    private final PulsarService pulsar;
+
+    private final ScheduledExecutorService loadManagerExecutor;
+
+    private final LoadManagerContext context;
+
+    private final ServiceConfiguration conf;
+
+    private final ServiceUnitStateChannel serviceUnitStateChannel;
+
+    private final NamespaceBundleSplitStrategy bundleSplitStrategy;
+
+    private final SplitCounter counter;
+
+    private final AtomicReference<List<Metrics>> splitMetrics;
+
+    private volatile ScheduledFuture<?> task;
+
+    private long counterLastUpdatedAt = 0;
+
+    public SplitScheduler(PulsarService pulsar,
+                          ServiceUnitStateChannel serviceUnitStateChannel,
+                          SplitCounter counter,
+                          AtomicReference<List<Metrics>> splitMetrics,
+                          LoadManagerContext context,
+                          NamespaceBundleSplitStrategy bundleSplitStrategy) {
+        this.pulsar = pulsar;
+        this.loadManagerExecutor = pulsar.getLoadManagerExecutor();
+        this.counter = counter;
+        this.splitMetrics = splitMetrics;
+        this.context = context;
+        this.conf = pulsar.getConfiguration();
+        this.bundleSplitStrategy = bundleSplitStrategy;
+        this.serviceUnitStateChannel = serviceUnitStateChannel;
+    }
+
+    public SplitScheduler(PulsarService pulsar,
+                          ServiceUnitStateChannel serviceUnitStateChannel,
+                          SplitCounter counter,
+                          AtomicReference<List<Metrics>> splitMetrics,
+                          LoadManagerContext context) {
+        this(pulsar, serviceUnitStateChannel, counter, splitMetrics, context,
+                new DefaultNamespaceBundleSplitStrategyImpl(counter));
+    }
+
+    @Override
+    public void execute() {
+        boolean debugMode = conf.isLoadBalancerDebugModeEnabled() || 
log.isDebugEnabled();
+        if (debugMode) {
+            log.info("Load balancer enabled: {}, Split enabled: {}.",
+                    conf.isLoadBalancerEnabled(), 
conf.isLoadBalancerAutoBundleSplitEnabled());
+        }
+
+        if (!isLoadBalancerAutoBundleSplitEnabled()) {
+            if (debugMode) {
+                log.info("The load balancer or load balancer split already 
disabled. Skipping.");
+            }
+            return;
+        }
+
+        synchronized (bundleSplitStrategy) {
+            final Set<SplitDecision> decisions = 
bundleSplitStrategy.findBundlesToSplit(context, pulsar);
+            if (!decisions.isEmpty()) {
+                List<CompletableFuture<Void>> futures = new ArrayList<>();
+                for (SplitDecision decision : decisions) {
+                    if (decision.getLabel() == Success) {
+                        var split = decision.getSplit();
+                        
futures.add(serviceUnitStateChannel.publishSplitEventAsync(split)
+                                .whenComplete((__, e) -> {
+                                    if (e == null) {
+                                        counter.update(decision);
+                                        log.info("Published Split Event for 
{}", split);
+                                    } else {
+                                        counter.update(Failure, Unknown);
+                                        log.error("Failed to publish Split 
Event for {}", split);
+                                    }
+                                }));
+                    }
+                }
+                FutureUtil.waitForAll(futures).exceptionally(ex -> {
+                    log.error("Failed to wait for split events to persist.", 
ex);
+                    counter.update(Failure, Unknown);
+                    return null;
+                });
+            } else {
+                if (debugMode) {
+                    log.info("BundleSplitStrategy returned no bundles to 
split.");
+                }
+            }
+        }
+
+        if (counter.updatedAt() > counterLastUpdatedAt) {

Review Comment:
   Updated to make syncWaiting after FutureUtil.waitForAll .



##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/scheduler/SplitScheduler.java:
##########
@@ -0,0 +1,172 @@
+/*
+ * 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.loadbalance.extensions.scheduler;
+
+import static 
org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Label.Failure;
+import static 
org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Label.Success;
+import static 
org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision.Reason.Unknown;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Set;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+import lombok.extern.slf4j.Slf4j;
+import org.apache.pulsar.broker.PulsarService;
+import org.apache.pulsar.broker.ServiceConfiguration;
+import org.apache.pulsar.broker.loadbalance.extensions.LoadManagerContext;
+import 
org.apache.pulsar.broker.loadbalance.extensions.channel.ServiceUnitStateChannel;
+import org.apache.pulsar.broker.loadbalance.extensions.models.SplitCounter;
+import org.apache.pulsar.broker.loadbalance.extensions.models.SplitDecision;
+import 
org.apache.pulsar.broker.loadbalance.extensions.strategy.DefaultNamespaceBundleSplitStrategyImpl;
+import 
org.apache.pulsar.broker.loadbalance.extensions.strategy.NamespaceBundleSplitStrategy;
+import org.apache.pulsar.common.stats.Metrics;
+import org.apache.pulsar.common.util.FutureUtil;
+
+/**
+ * Service Unit(e.g. bundles) Split scheduler.
+ */
+@Slf4j
+public class SplitScheduler implements LoadManagerScheduler {
+
+    private final PulsarService pulsar;
+
+    private final ScheduledExecutorService loadManagerExecutor;
+
+    private final LoadManagerContext context;
+
+    private final ServiceConfiguration conf;
+
+    private final ServiceUnitStateChannel serviceUnitStateChannel;
+
+    private final NamespaceBundleSplitStrategy bundleSplitStrategy;
+
+    private final SplitCounter counter;
+
+    private final AtomicReference<List<Metrics>> splitMetrics;
+
+    private volatile ScheduledFuture<?> task;
+
+    private long counterLastUpdatedAt = 0;
+
+    public SplitScheduler(PulsarService pulsar,
+                          ServiceUnitStateChannel serviceUnitStateChannel,
+                          SplitCounter counter,
+                          AtomicReference<List<Metrics>> splitMetrics,
+                          LoadManagerContext context,
+                          NamespaceBundleSplitStrategy bundleSplitStrategy) {
+        this.pulsar = pulsar;
+        this.loadManagerExecutor = pulsar.getLoadManagerExecutor();
+        this.counter = counter;
+        this.splitMetrics = splitMetrics;
+        this.context = context;
+        this.conf = pulsar.getConfiguration();
+        this.bundleSplitStrategy = bundleSplitStrategy;
+        this.serviceUnitStateChannel = serviceUnitStateChannel;
+    }
+
+    public SplitScheduler(PulsarService pulsar,
+                          ServiceUnitStateChannel serviceUnitStateChannel,
+                          SplitCounter counter,
+                          AtomicReference<List<Metrics>> splitMetrics,
+                          LoadManagerContext context) {
+        this(pulsar, serviceUnitStateChannel, counter, splitMetrics, context,
+                new DefaultNamespaceBundleSplitStrategyImpl(counter));
+    }
+
+    @Override
+    public void execute() {
+        boolean debugMode = conf.isLoadBalancerDebugModeEnabled() || 
log.isDebugEnabled();
+        if (debugMode) {
+            log.info("Load balancer enabled: {}, Split enabled: {}.",
+                    conf.isLoadBalancerEnabled(), 
conf.isLoadBalancerAutoBundleSplitEnabled());
+        }
+
+        if (!isLoadBalancerAutoBundleSplitEnabled()) {
+            if (debugMode) {
+                log.info("The load balancer or load balancer split already 
disabled. Skipping.");
+            }
+            return;
+        }
+
+        synchronized (bundleSplitStrategy) {
+            final Set<SplitDecision> decisions = 
bundleSplitStrategy.findBundlesToSplit(context, pulsar);
+            if (!decisions.isEmpty()) {
+                List<CompletableFuture<Void>> futures = new ArrayList<>();
+                for (SplitDecision decision : decisions) {
+                    if (decision.getLabel() == Success) {
+                        var split = decision.getSplit();
+                        
futures.add(serviceUnitStateChannel.publishSplitEventAsync(split)
+                                .whenComplete((__, e) -> {
+                                    if (e == null) {
+                                        counter.update(decision);
+                                        log.info("Published Split Event for 
{}", split);
+                                    } else {
+                                        counter.update(Failure, Unknown);
+                                        log.error("Failed to publish Split 
Event for {}", split);
+                                    }
+                                }));
+                    }
+                }
+                FutureUtil.waitForAll(futures).exceptionally(ex -> {
+                    log.error("Failed to wait for split events to persist.", 
ex);
+                    counter.update(Failure, Unknown);

Review Comment:
   Updated.



-- 
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