heesung-sn commented on code in PR #19592: URL: https://github.com/apache/pulsar/pull/19592#discussion_r1120552209
########## pulsar-broker/src/main/java/org/apache/pulsar/broker/loadbalance/extensions/policies/IsolationPoliciesHelper.java: ########## @@ -0,0 +1,159 @@ +/* + * 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.policies; + +import io.netty.util.concurrent.FastThreadLocal; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; +import lombok.extern.slf4j.Slf4j; +import org.apache.pulsar.broker.loadbalance.extensions.data.BrokerLookupData; +import org.apache.pulsar.broker.loadbalance.impl.SimpleResourceAllocationPolicies; +import org.apache.pulsar.common.naming.NamespaceBundle; +import org.apache.pulsar.common.naming.NamespaceName; +import org.apache.pulsar.common.naming.ServiceUnitId; + +@Slf4j +public class IsolationPoliciesHelper { + + private final SimpleResourceAllocationPolicies policies; + + public IsolationPoliciesHelper(SimpleResourceAllocationPolicies policies) { + this.policies = policies; + } + + // Cache for primary brokers according to policies. + private static final FastThreadLocal<Set<String>> localPrimariesCache = new FastThreadLocal<>() { + @Override + protected Set<String> initialValue() { + return new HashSet<>(); + } + }; + + // Cache for shard brokers according to policies. + private static final FastThreadLocal<Set<String>> localSecondaryCache = new FastThreadLocal<>() { + @Override + protected Set<String> initialValue() { + return new HashSet<>(); + } + }; + + private static final FastThreadLocal<Set<String>> localBrokerCandidateCache = new FastThreadLocal<>() { + @Override + protected Set<String> initialValue() { + return new HashSet<>(); + } + }; + + public Set<String> applyIsolationPolicies(Map<String, BrokerLookupData> availableBrokers, + ServiceUnitId serviceUnit) { + Set<String> primariesCache = localPrimariesCache.get(); + primariesCache.clear(); + + Set<String> secondaryCache = localSecondaryCache.get(); + secondaryCache.clear(); + + NamespaceName namespace = serviceUnit.getNamespaceObject(); + boolean isIsolationPoliciesPresent = policies.areIsolationPoliciesPresent(namespace); + boolean isNonPersistentTopic = serviceUnit instanceof NamespaceBundle + && ((NamespaceBundle) serviceUnit).hasNonPersistentTopic(); + if (isIsolationPoliciesPresent) { + log.debug("Isolation Policies Present for namespace - [{}]", namespace.toString()); + } + + availableBrokers.forEach((broker, lookupData) -> { + final String brokerUrlString = String.format("http://%s", broker); Review Comment: nit: It would be great if we could reuse the code from LoadManagerShared. We can create a static function, `LoadManagerShared.doApplyIsolationPolicies(brokerUrlString, nonPersistentTopicsEnabled, persistentTopicsEnabled...)` and use them both for the extension and the existing load manager. -- 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]
