|
| 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.loadbalance.extensions.scheduler; |
| 20 | + |
| 21 | +import com.google.common.annotations.VisibleForTesting; |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.HashMap; |
| 24 | +import java.util.List; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.concurrent.CompletableFuture; |
| 27 | +import java.util.concurrent.ScheduledExecutorService; |
| 28 | +import java.util.concurrent.ScheduledFuture; |
| 29 | +import java.util.concurrent.TimeUnit; |
| 30 | +import lombok.extern.slf4j.Slf4j; |
| 31 | +import org.apache.pulsar.broker.ServiceConfiguration; |
| 32 | +import org.apache.pulsar.broker.loadbalance.extensions.LoadManagerContext; |
| 33 | +import org.apache.pulsar.broker.loadbalance.extensions.channel.ServiceUnitStateChannel; |
| 34 | +import org.apache.pulsar.broker.loadbalance.extensions.models.UnloadDecision; |
| 35 | +import org.apache.pulsar.common.util.FutureUtil; |
| 36 | +import org.apache.pulsar.common.util.Reflections; |
| 37 | + |
| 38 | +@Slf4j |
| 39 | +public class UnloadScheduler implements LoadManagerScheduler { |
| 40 | + |
| 41 | + private final NamespaceUnloadStrategy namespaceUnloadStrategy; |
| 42 | + |
| 43 | + private final ScheduledExecutorService loadManagerExecutor; |
| 44 | + |
| 45 | + private final LoadManagerContext context; |
| 46 | + |
| 47 | + private final ServiceUnitStateChannel channel; |
| 48 | + |
| 49 | + private final ServiceConfiguration conf; |
| 50 | + |
| 51 | + private volatile ScheduledFuture<?> task; |
| 52 | + |
| 53 | + private final Map<String, Long> recentlyUnloadedBundles; |
| 54 | + |
| 55 | + private final Map<String, Long> recentlyUnloadedBrokers; |
| 56 | + |
| 57 | + private volatile CompletableFuture<Void> currentRunningFuture = null; |
| 58 | + |
| 59 | + public UnloadScheduler(ScheduledExecutorService loadManagerExecutor, |
| 60 | + LoadManagerContext context, |
| 61 | + ServiceUnitStateChannel channel) { |
| 62 | + this(loadManagerExecutor, context, channel, createNamespaceUnloadStrategy(context.brokerConfiguration())); |
| 63 | + } |
| 64 | + |
| 65 | + @VisibleForTesting |
| 66 | + protected UnloadScheduler(ScheduledExecutorService loadManagerExecutor, |
| 67 | + LoadManagerContext context, |
| 68 | + ServiceUnitStateChannel channel, |
| 69 | + NamespaceUnloadStrategy strategy) { |
| 70 | + this.namespaceUnloadStrategy = strategy; |
| 71 | + this.recentlyUnloadedBundles = new HashMap<>(); |
| 72 | + this.recentlyUnloadedBrokers = new HashMap<>(); |
| 73 | + this.loadManagerExecutor = loadManagerExecutor; |
| 74 | + this.context = context; |
| 75 | + this.conf = context.brokerConfiguration(); |
| 76 | + this.channel = channel; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public synchronized void execute() { |
| 81 | + boolean debugMode = conf.isLoadBalancerDebugModeEnabled() || log.isDebugEnabled(); |
| 82 | + if (debugMode) { |
| 83 | + log.info("Load balancer enabled: {}, Shedding enabled: {}.", |
| 84 | + conf.isLoadBalancerEnabled(), conf.isLoadBalancerSheddingEnabled()); |
| 85 | + } |
| 86 | + if (!isLoadBalancerSheddingEnabled()) { |
| 87 | + if (debugMode) { |
| 88 | + log.info("The load balancer or load balancer shedding already disabled. Skipping."); |
| 89 | + } |
| 90 | + return; |
| 91 | + } |
| 92 | + if (currentRunningFuture != null && !currentRunningFuture.isDone()) { |
| 93 | + if (debugMode) { |
| 94 | + log.info("Auto namespace unload is running. Skipping."); |
| 95 | + } |
| 96 | + return; |
| 97 | + } |
| 98 | + // Remove bundles who have been unloaded for longer than the grace period from the recently unloaded map. |
| 99 | + final long timeout = System.currentTimeMillis() |
| 100 | + - TimeUnit.MINUTES.toMillis(conf.getLoadBalancerSheddingGracePeriodMinutes()); |
| 101 | + recentlyUnloadedBundles.keySet().removeIf(e -> recentlyUnloadedBundles.get(e) < timeout); |
| 102 | + |
| 103 | + this.currentRunningFuture = channel.isChannelOwnerAsync().thenCompose(isChannelOwner -> { |
| 104 | + if (!isChannelOwner) { |
| 105 | + if (debugMode) { |
| 106 | + log.info("Current broker is not channel owner. Skipping."); |
| 107 | + } |
| 108 | + return CompletableFuture.completedFuture(null); |
| 109 | + } |
| 110 | + return context.brokerRegistry().getAvailableBrokersAsync().thenCompose(availableBrokers -> { |
| 111 | + if (debugMode) { |
| 112 | + log.info("Available brokers: {}", availableBrokers); |
| 113 | + } |
| 114 | + if (availableBrokers.size() <= 1) { |
| 115 | + log.info("Only 1 broker available: no load shedding will be performed. Skipping."); |
| 116 | + return CompletableFuture.completedFuture(null); |
| 117 | + } |
| 118 | + final UnloadDecision unloadDecision = namespaceUnloadStrategy |
| 119 | + .findBundlesForUnloading(context, recentlyUnloadedBundles, recentlyUnloadedBrokers); |
| 120 | + if (debugMode) { |
| 121 | + log.info("[{}] Unload decision result: {}", |
| 122 | + namespaceUnloadStrategy.getClass().getSimpleName(), unloadDecision.toString()); |
| 123 | + } |
| 124 | + if (unloadDecision.getUnloads().isEmpty()) { |
| 125 | + if (debugMode) { |
| 126 | + log.info("[{}] Unload decision unloads is empty. Skipping.", |
| 127 | + namespaceUnloadStrategy.getClass().getSimpleName()); |
| 128 | + } |
| 129 | + return CompletableFuture.completedFuture(null); |
| 130 | + } |
| 131 | + List<CompletableFuture<Void>> futures = new ArrayList<>(); |
| 132 | + unloadDecision.getUnloads().forEach((broker, unload) -> { |
| 133 | + log.info("[{}] Unloading bundle: {}", namespaceUnloadStrategy.getClass().getSimpleName(), unload); |
| 134 | + futures.add(channel.publishUnloadEventAsync(unload).thenAccept(__ -> { |
| 135 | + recentlyUnloadedBundles.put(unload.serviceUnit(), System.currentTimeMillis()); |
| 136 | + recentlyUnloadedBrokers.put(unload.sourceBroker(), System.currentTimeMillis()); |
| 137 | + })); |
| 138 | + }); |
| 139 | + return FutureUtil.waitForAll(futures).exceptionally(ex -> { |
| 140 | + log.error("[{}] Namespace unload has exception.", |
| 141 | + namespaceUnloadStrategy.getClass().getSimpleName(), ex); |
| 142 | + return null; |
| 143 | + }); |
| 144 | + }); |
| 145 | + }); |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public void start() { |
| 150 | + long loadSheddingInterval = TimeUnit.MINUTES |
| 151 | + .toMillis(conf.getLoadBalancerSheddingIntervalMinutes()); |
| 152 | + this.task = loadManagerExecutor.scheduleAtFixedRate( |
| 153 | + this::execute, loadSheddingInterval, loadSheddingInterval, TimeUnit.MILLISECONDS); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void close() { |
| 158 | + if (this.task != null) { |
| 159 | + this.task.cancel(false); |
| 160 | + } |
| 161 | + this.recentlyUnloadedBundles.clear(); |
| 162 | + this.recentlyUnloadedBrokers.clear(); |
| 163 | + } |
| 164 | + |
| 165 | + private static NamespaceUnloadStrategy createNamespaceUnloadStrategy(ServiceConfiguration conf) { |
| 166 | + try { |
| 167 | + return Reflections.createInstance(conf.getLoadBalancerLoadSheddingStrategy(), NamespaceUnloadStrategy.class, |
| 168 | + Thread.currentThread().getContextClassLoader()); |
| 169 | + } catch (Exception e) { |
| 170 | + log.error("Error when trying to create namespace unload strategy: {}", |
| 171 | + conf.getLoadBalancerLoadPlacementStrategy(), e); |
| 172 | + } |
| 173 | + log.error("create namespace unload strategy failed. using TransferShedder instead."); |
| 174 | + return new TransferShedder(); |
| 175 | + } |
| 176 | + |
| 177 | + private boolean isLoadBalancerSheddingEnabled() { |
| 178 | + return conf.isLoadBalancerEnabled() && conf.isLoadBalancerSheddingEnabled(); |
| 179 | + } |
| 180 | +} |
0 commit comments