|
| 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.policies; |
| 20 | + |
| 21 | +import io.netty.util.concurrent.FastThreadLocal; |
| 22 | +import java.net.MalformedURLException; |
| 23 | +import java.net.URL; |
| 24 | +import java.util.HashSet; |
| 25 | +import java.util.Map; |
| 26 | +import java.util.Set; |
| 27 | +import lombok.extern.slf4j.Slf4j; |
| 28 | +import org.apache.pulsar.broker.loadbalance.extensions.data.BrokerLookupData; |
| 29 | +import org.apache.pulsar.broker.loadbalance.impl.SimpleResourceAllocationPolicies; |
| 30 | +import org.apache.pulsar.common.naming.NamespaceBundle; |
| 31 | +import org.apache.pulsar.common.naming.NamespaceName; |
| 32 | +import org.apache.pulsar.common.naming.ServiceUnitId; |
| 33 | + |
| 34 | +@Slf4j |
| 35 | +public class IsolationPoliciesHelper { |
| 36 | + |
| 37 | + private final SimpleResourceAllocationPolicies policies; |
| 38 | + |
| 39 | + public IsolationPoliciesHelper(SimpleResourceAllocationPolicies policies) { |
| 40 | + this.policies = policies; |
| 41 | + } |
| 42 | + |
| 43 | + // Cache for primary brokers according to policies. |
| 44 | + private static final FastThreadLocal<Set<String>> localPrimariesCache = new FastThreadLocal<>() { |
| 45 | + @Override |
| 46 | + protected Set<String> initialValue() { |
| 47 | + return new HashSet<>(); |
| 48 | + } |
| 49 | + }; |
| 50 | + |
| 51 | + // Cache for shard brokers according to policies. |
| 52 | + private static final FastThreadLocal<Set<String>> localSecondaryCache = new FastThreadLocal<>() { |
| 53 | + @Override |
| 54 | + protected Set<String> initialValue() { |
| 55 | + return new HashSet<>(); |
| 56 | + } |
| 57 | + }; |
| 58 | + |
| 59 | + private static final FastThreadLocal<Set<String>> localBrokerCandidateCache = new FastThreadLocal<>() { |
| 60 | + @Override |
| 61 | + protected Set<String> initialValue() { |
| 62 | + return new HashSet<>(); |
| 63 | + } |
| 64 | + }; |
| 65 | + |
| 66 | + public Set<String> applyIsolationPolicies(Map<String, BrokerLookupData> availableBrokers, |
| 67 | + ServiceUnitId serviceUnit) { |
| 68 | + Set<String> primariesCache = localPrimariesCache.get(); |
| 69 | + primariesCache.clear(); |
| 70 | + |
| 71 | + Set<String> secondaryCache = localSecondaryCache.get(); |
| 72 | + secondaryCache.clear(); |
| 73 | + |
| 74 | + NamespaceName namespace = serviceUnit.getNamespaceObject(); |
| 75 | + boolean isIsolationPoliciesPresent = policies.areIsolationPoliciesPresent(namespace); |
| 76 | + boolean isNonPersistentTopic = serviceUnit instanceof NamespaceBundle |
| 77 | + && ((NamespaceBundle) serviceUnit).hasNonPersistentTopic(); |
| 78 | + if (isIsolationPoliciesPresent) { |
| 79 | + log.debug("Isolation Policies Present for namespace - [{}]", namespace.toString()); |
| 80 | + } |
| 81 | + |
| 82 | + availableBrokers.forEach((broker, lookupData) -> { |
| 83 | + final String brokerUrlString = String.format("http://%s", broker); |
| 84 | + URL brokerUrl; |
| 85 | + try { |
| 86 | + brokerUrl = new URL(brokerUrlString); |
| 87 | + } catch (MalformedURLException e) { |
| 88 | + log.error("Unable to parse brokerUrl from ResourceUnitId", e); |
| 89 | + return; |
| 90 | + } |
| 91 | + // todo: in future check if the resource unit has resources to take the namespace |
| 92 | + if (isIsolationPoliciesPresent) { |
| 93 | + // note: serviceUnitID is namespace name and ResourceID is brokerName |
| 94 | + if (policies.isPrimaryBroker(namespace, brokerUrl.getHost())) { |
| 95 | + primariesCache.add(broker); |
| 96 | + if (log.isDebugEnabled()) { |
| 97 | + log.debug("Added Primary Broker - [{}] as possible Candidates for" |
| 98 | + + " namespace - [{}] with policies", brokerUrl.getHost(), namespace.toString()); |
| 99 | + } |
| 100 | + } else if (policies.isSecondaryBroker(namespace, brokerUrl.getHost())) { |
| 101 | + secondaryCache.add(broker); |
| 102 | + if (log.isDebugEnabled()) { |
| 103 | + log.debug( |
| 104 | + "Added Shared Broker - [{}] as possible " |
| 105 | + + "Candidates for namespace - [{}] with policies", |
| 106 | + brokerUrl.getHost(), namespace); |
| 107 | + } |
| 108 | + } else { |
| 109 | + if (log.isDebugEnabled()) { |
| 110 | + log.debug("Skipping Broker - [{}] not primary broker and not shared" + " for namespace - [{}] ", |
| 111 | + brokerUrl.getHost(), namespace); |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | + } else { |
| 116 | + // non-persistent topic can be assigned to only those brokers that enabled for non-persistent topic |
| 117 | + if (isNonPersistentTopic && !lookupData.nonPersistentTopicsEnabled()) { |
| 118 | + if (log.isDebugEnabled()) { |
| 119 | + log.debug("Filter broker- [{}] because it doesn't support non-persistent namespace - [{}]", |
| 120 | + brokerUrl.getHost(), namespace.toString()); |
| 121 | + } |
| 122 | + } else if (!isNonPersistentTopic && !lookupData.persistentTopicsEnabled()) { |
| 123 | + // persistent topic can be assigned to only brokers that enabled for persistent-topic |
| 124 | + if (log.isDebugEnabled()) { |
| 125 | + log.debug("Filter broker- [{}] because broker only supports persistent namespace - [{}]", |
| 126 | + brokerUrl.getHost(), namespace.toString()); |
| 127 | + } |
| 128 | + } else if (policies.isSharedBroker(brokerUrl.getHost())) { |
| 129 | + secondaryCache.add(broker); |
| 130 | + if (log.isDebugEnabled()) { |
| 131 | + log.debug("Added Shared Broker - [{}] as possible Candidates for namespace - [{}]", |
| 132 | + brokerUrl.getHost(), namespace.toString()); |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + }); |
| 137 | + |
| 138 | + Set<String> brokerCandidateCache = localBrokerCandidateCache.get(); |
| 139 | + brokerCandidateCache.clear(); |
| 140 | + if (isIsolationPoliciesPresent) { |
| 141 | + brokerCandidateCache.addAll(primariesCache); |
| 142 | + if (policies.shouldFailoverToSecondaries(namespace, primariesCache.size())) { |
| 143 | + log.debug( |
| 144 | + "Not enough of primaries [{}] available for namespace - [{}], " |
| 145 | + + "adding shared [{}] as possible candidate owners", |
| 146 | + primariesCache.size(), namespace, secondaryCache.size()); |
| 147 | + brokerCandidateCache.addAll(secondaryCache); |
| 148 | + } |
| 149 | + } else { |
| 150 | + log.debug( |
| 151 | + "Policies not present for namespace - [{}] so only " |
| 152 | + + "considering shared [{}] brokers for possible owner", |
| 153 | + namespace.toString(), secondaryCache.size()); |
| 154 | + brokerCandidateCache.addAll(secondaryCache); |
| 155 | + } |
| 156 | + return brokerCandidateCache; |
| 157 | + } |
| 158 | + |
| 159 | +} |
0 commit comments