Search in sources :

Example 1 with LoadSheddingStrategy

use of org.apache.pulsar.broker.loadbalance.LoadSheddingStrategy in project incubator-pulsar by apache.

the class ModularLoadManagerImpl method doLoadShedding.

/**
 * As the leader broker, select bundles for the namespace service to unload so that they may be reassigned to new
 * brokers.
 */
@Override
public synchronized void doLoadShedding() {
    if (!LoadManagerShared.isLoadSheddingEnabled(pulsar)) {
        return;
    }
    if (getAvailableBrokers().size() <= 1) {
        log.info("Only 1 broker available: no load shedding will be performed");
        return;
    }
    // Remove bundles who have been unloaded for longer than the grace period from the recently unloaded
    // map.
    final long timeout = System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(conf.getLoadBalancerSheddingGracePeriodMinutes());
    final Map<String, Long> recentlyUnloadedBundles = loadData.getRecentlyUnloadedBundles();
    recentlyUnloadedBundles.keySet().removeIf(e -> recentlyUnloadedBundles.get(e) < timeout);
    for (LoadSheddingStrategy strategy : loadSheddingPipeline) {
        final Map<String, String> bundlesToUnload = strategy.findBundlesForUnloading(loadData, conf);
        if (bundlesToUnload != null && !bundlesToUnload.isEmpty()) {
            try {
                for (Map.Entry<String, String> entry : bundlesToUnload.entrySet()) {
                    final String broker = entry.getKey();
                    final String bundle = entry.getValue();
                    final String namespaceName = LoadManagerShared.getNamespaceNameFromBundleName(bundle);
                    final String bundleRange = LoadManagerShared.getBundleRangeFromBundleName(bundle);
                    if (!shouldAntiAffinityNamespaceUnload(namespaceName, bundleRange, broker)) {
                        continue;
                    }
                    log.info("Unloading bundle: {} from broker {}", bundle, broker);
                    pulsar.getAdminClient().namespaces().unloadNamespaceBundle(namespaceName, bundleRange);
                    loadData.getRecentlyUnloadedBundles().put(bundle, System.currentTimeMillis());
                }
            } catch (Exception e) {
                log.warn("Error when trying to perform load shedding", e);
            }
            return;
        }
    }
}
Also used : LoadSheddingStrategy(org.apache.pulsar.broker.loadbalance.LoadSheddingStrategy) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) BrokerFilterException(org.apache.pulsar.broker.loadbalance.BrokerFilterException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) PulsarServerException(org.apache.pulsar.broker.PulsarServerException)

Aggregations

IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 PulsarServerException (org.apache.pulsar.broker.PulsarServerException)1 BrokerFilterException (org.apache.pulsar.broker.loadbalance.BrokerFilterException)1 LoadSheddingStrategy (org.apache.pulsar.broker.loadbalance.LoadSheddingStrategy)1 KeeperException (org.apache.zookeeper.KeeperException)1 NoNodeException (org.apache.zookeeper.KeeperException.NoNodeException)1