You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "Demogorgon314 (via GitHub)" <gi...@apache.org> on 2023/02/11 04:18:24 UTC
[GitHub] [pulsar] Demogorgon314 commented on a diff in pull request #19477: [improve][broker] PIP-192 Added namespace unload scheduler
Demogorgon314 commented on code in PR #19477:
URL: https://github.com/apache/pulsar/pull/19477#discussion_r1103511808
##########
pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/scheduler/NamespaceUnloadScheduler.java:
##########
@@ -0,0 +1,155 @@
+/*
+ * 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 com.google.common.annotations.VisibleForTesting;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
+import java.util.concurrent.TimeUnit;
+import lombok.extern.slf4j.Slf4j;
+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.UnloadDecision;
+import org.apache.pulsar.common.util.FutureUtil;
+import org.apache.pulsar.common.util.Reflections;
+
+@Slf4j
+public class NamespaceUnloadScheduler implements LoadManagerScheduler {
+
+ private final NamespaceUnloadStrategy namespaceUnloadStrategy;
+
+ private final ScheduledExecutorService loadManagerExecutor;
+
+ private final LoadManagerContext context;
+
+ private final ServiceUnitStateChannel channel;
+
+ private final ServiceConfiguration conf;
+
+ private volatile ScheduledFuture<?> loadSheddingTask;
+
+ private final Map<String, Long> recentlyUnloadedBundles;
+
+ private final Map<String, Long> recentlyUnloadedBrokers;
+
+ private volatile CompletableFuture<Void> currentRunningFuture = null;
+
+ public NamespaceUnloadScheduler(ScheduledExecutorService loadManagerExecutor,
+ LoadManagerContext context,
+ ServiceUnitStateChannel channel) {
+ this(loadManagerExecutor, context, channel, createNamespaceUnloadStrategy(context.brokerConfiguration()));
+ }
+
+ @VisibleForTesting
+ protected NamespaceUnloadScheduler(ScheduledExecutorService loadManagerExecutor,
+ LoadManagerContext context,
+ ServiceUnitStateChannel channel,
+ NamespaceUnloadStrategy strategy) {
+ this.namespaceUnloadStrategy = strategy;
+ this.recentlyUnloadedBundles = new ConcurrentHashMap<>();
+ this.recentlyUnloadedBrokers = new ConcurrentHashMap<>();
+ this.loadManagerExecutor = loadManagerExecutor;
+ this.context = context;
+ this.conf = context.brokerConfiguration();
+ this.channel = channel;
+ }
+
+ @Override
+ public synchronized void execute() {
+ boolean debugMode = conf.isLoadBalancerDebugModeEnabled() || log.isDebugEnabled();
+ if (debugMode) {
+ log.info("Load balancer enabled: {}, Shedding enabled: {}.",
+ conf.isLoadBalancerEnabled(), conf.isLoadBalancerSheddingEnabled());
+ }
+ if (currentRunningFuture != null && !currentRunningFuture.isDone()
+ || !(conf.isLoadBalancerEnabled() && conf.isLoadBalancerSheddingEnabled())) {
+ return;
+ }
+ this.currentRunningFuture = channel.isChannelOwnerAsync().thenCompose(isChannelOwner -> {
+ if (!isChannelOwner) {
+ return CompletableFuture.completedFuture(null);
+ }
+ return context.brokerRegistry().getAvailableBrokersAsync().thenCompose(availableBrokers -> {
+ if (debugMode) {
+ log.info("Available brokers: {}", availableBrokers);
+ }
+ if (availableBrokers.size() <= 1) {
+ log.info("Only 1 broker available: no load shedding will be performed");
+ return CompletableFuture.completedFuture(null);
+ }
+ final UnloadDecision unloadDecision = namespaceUnloadStrategy
+ .findBundlesForUnloading(context, recentlyUnloadedBundles, recentlyUnloadedBrokers);
+ if (debugMode) {
+ log.info("[{}] Unload decision result: {}",
+ namespaceUnloadStrategy.getClass().getSimpleName(), unloadDecision.toString());
+ }
+ if (unloadDecision.getUnloads().isEmpty()) {
+ return CompletableFuture.completedFuture(null);
+ }
+ List<CompletableFuture<Void>> futures = new ArrayList<>();
+ unloadDecision.getUnloads().forEach((broker, unload) -> {
+ log.info("[{}] Unloading bundle: {}", namespaceUnloadStrategy.getClass().getSimpleName(), unload);
+ futures.add(channel.publishUnloadEventAsync(unload).thenAccept(__ -> {
+ recentlyUnloadedBundles.put(unload.serviceUnit(), System.currentTimeMillis());
+ }));
+ });
+ return FutureUtil.waitForAll(futures).exceptionally(ex -> {
+ log.error("[{}] Namespace unload has exception.", namespaceUnloadStrategy, ex);
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: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org