|
| 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; |
| 20 | + |
| 21 | +import static org.mockito.Mockito.reset; |
| 22 | +import static org.mockito.Mockito.spy; |
| 23 | +import com.google.common.collect.Sets; |
| 24 | +import java.util.concurrent.CompletableFuture; |
| 25 | +import org.apache.commons.lang3.reflect.FieldUtils; |
| 26 | +import org.apache.commons.lang3.tuple.Pair; |
| 27 | +import org.apache.pulsar.broker.PulsarService; |
| 28 | +import org.apache.pulsar.broker.ServiceConfiguration; |
| 29 | +import org.apache.pulsar.broker.auth.MockedPulsarServiceBaseTest; |
| 30 | +import org.apache.pulsar.broker.loadbalance.extensions.channel.ServiceUnitStateChannelImpl; |
| 31 | +import org.apache.pulsar.broker.loadbalance.extensions.scheduler.TransferShedder; |
| 32 | +import org.apache.pulsar.broker.testcontext.PulsarTestContext; |
| 33 | +import org.apache.pulsar.client.admin.PulsarAdminException; |
| 34 | +import org.apache.pulsar.client.impl.LookupService; |
| 35 | +import org.apache.pulsar.common.naming.NamespaceBundle; |
| 36 | +import org.apache.pulsar.common.naming.SystemTopicNames; |
| 37 | +import org.apache.pulsar.common.naming.TopicName; |
| 38 | +import org.apache.pulsar.common.policies.data.ClusterData; |
| 39 | +import org.apache.pulsar.common.policies.data.TenantInfoImpl; |
| 40 | +import org.apache.pulsar.common.policies.data.TopicType; |
| 41 | +import org.testng.annotations.AfterClass; |
| 42 | +import org.testng.annotations.BeforeClass; |
| 43 | +import org.testng.annotations.BeforeMethod; |
| 44 | + |
| 45 | +public abstract class ExtensibleLoadManagerImplBaseTest extends MockedPulsarServiceBaseTest { |
| 46 | + |
| 47 | + protected PulsarService pulsar1; |
| 48 | + protected PulsarService pulsar2; |
| 49 | + |
| 50 | + protected PulsarTestContext additionalPulsarTestContext; |
| 51 | + |
| 52 | + protected ExtensibleLoadManagerImpl primaryLoadManager; |
| 53 | + |
| 54 | + protected ExtensibleLoadManagerImpl secondaryLoadManager; |
| 55 | + |
| 56 | + protected ServiceUnitStateChannelImpl channel1; |
| 57 | + protected ServiceUnitStateChannelImpl channel2; |
| 58 | + |
| 59 | + protected final String defaultTestNamespace; |
| 60 | + |
| 61 | + protected LookupService lookupService; |
| 62 | + |
| 63 | + protected ExtensibleLoadManagerImplBaseTest(String defaultTestNamespace) { |
| 64 | + this.defaultTestNamespace = defaultTestNamespace; |
| 65 | + } |
| 66 | + |
| 67 | + protected ServiceConfiguration initConfig(ServiceConfiguration conf) { |
| 68 | + // Set the inflight state waiting time and ownership monitor delay time to 5 seconds to avoid |
| 69 | + // stuck when doing unload. |
| 70 | + conf.setLoadBalancerInFlightServiceUnitStateWaitingTimeInMillis(5 * 1000); |
| 71 | + conf.setLoadBalancerServiceUnitStateMonitorIntervalInSeconds(1); |
| 72 | + conf.setForceDeleteNamespaceAllowed(true); |
| 73 | + conf.setAllowAutoTopicCreationType(TopicType.NON_PARTITIONED); |
| 74 | + conf.setAllowAutoTopicCreation(true); |
| 75 | + conf.setLoadManagerClassName(ExtensibleLoadManagerImpl.class.getName()); |
| 76 | + conf.setLoadBalancerLoadSheddingStrategy(TransferShedder.class.getName()); |
| 77 | + conf.setLoadBalancerSheddingEnabled(false); |
| 78 | + conf.setLoadBalancerDebugModeEnabled(true); |
| 79 | + conf.setTopicLevelPoliciesEnabled(true); |
| 80 | + return conf; |
| 81 | + } |
| 82 | + |
| 83 | + @Override |
| 84 | + @BeforeClass(alwaysRun = true) |
| 85 | + protected void setup() throws Exception { |
| 86 | + initConfig(conf); |
| 87 | + super.internalSetup(conf); |
| 88 | + pulsar1 = pulsar; |
| 89 | + var conf2 = initConfig(getDefaultConf()); |
| 90 | + additionalPulsarTestContext = createAdditionalPulsarTestContext(conf2); |
| 91 | + pulsar2 = additionalPulsarTestContext.getPulsarService(); |
| 92 | + |
| 93 | + setPrimaryLoadManager(); |
| 94 | + setSecondaryLoadManager(); |
| 95 | + |
| 96 | + admin.clusters().createCluster(this.conf.getClusterName(), |
| 97 | + ClusterData.builder().serviceUrl(pulsar.getWebServiceAddress()).build()); |
| 98 | + admin.tenants().createTenant("public", |
| 99 | + new TenantInfoImpl(Sets.newHashSet("appid1", "appid2"), |
| 100 | + Sets.newHashSet(this.conf.getClusterName()))); |
| 101 | + admin.namespaces().createNamespace("public/default"); |
| 102 | + admin.namespaces().setNamespaceReplicationClusters("public/default", |
| 103 | + Sets.newHashSet(this.conf.getClusterName())); |
| 104 | + |
| 105 | + admin.namespaces().createNamespace(defaultTestNamespace, 128); |
| 106 | + admin.namespaces().setNamespaceReplicationClusters(defaultTestNamespace, |
| 107 | + Sets.newHashSet(this.conf.getClusterName())); |
| 108 | + lookupService = (LookupService) FieldUtils.readDeclaredField(pulsarClient, "lookup", true); |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + @AfterClass(alwaysRun = true) |
| 113 | + protected void cleanup() throws Exception { |
| 114 | + this.additionalPulsarTestContext.close(); |
| 115 | + super.internalCleanup(); |
| 116 | + } |
| 117 | + |
| 118 | + @BeforeMethod(alwaysRun = true) |
| 119 | + protected void initializeState() throws PulsarAdminException, IllegalAccessException { |
| 120 | + admin.namespaces().unload(defaultTestNamespace); |
| 121 | + reset(primaryLoadManager, secondaryLoadManager); |
| 122 | + FieldUtils.writeDeclaredField(pulsarClient, "lookup", lookupService, true); |
| 123 | + pulsar1.getConfig().setLoadBalancerMultiPhaseBundleUnload(true); |
| 124 | + pulsar2.getConfig().setLoadBalancerMultiPhaseBundleUnload(true); |
| 125 | + } |
| 126 | + |
| 127 | + protected void setPrimaryLoadManager() throws IllegalAccessException { |
| 128 | + ExtensibleLoadManagerWrapper wrapper = |
| 129 | + (ExtensibleLoadManagerWrapper) pulsar1.getLoadManager().get(); |
| 130 | + primaryLoadManager = spy((ExtensibleLoadManagerImpl) |
| 131 | + FieldUtils.readField(wrapper, "loadManager", true)); |
| 132 | + FieldUtils.writeField(wrapper, "loadManager", primaryLoadManager, true); |
| 133 | + channel1 = (ServiceUnitStateChannelImpl) |
| 134 | + FieldUtils.readField(primaryLoadManager, "serviceUnitStateChannel", true); |
| 135 | + } |
| 136 | + |
| 137 | + private void setSecondaryLoadManager() throws IllegalAccessException { |
| 138 | + ExtensibleLoadManagerWrapper wrapper = |
| 139 | + (ExtensibleLoadManagerWrapper) pulsar2.getLoadManager().get(); |
| 140 | + secondaryLoadManager = spy((ExtensibleLoadManagerImpl) |
| 141 | + FieldUtils.readField(wrapper, "loadManager", true)); |
| 142 | + FieldUtils.writeField(wrapper, "loadManager", secondaryLoadManager, true); |
| 143 | + channel2 = (ServiceUnitStateChannelImpl) |
| 144 | + FieldUtils.readField(secondaryLoadManager, "serviceUnitStateChannel", true); |
| 145 | + } |
| 146 | + |
| 147 | + protected CompletableFuture<NamespaceBundle> getBundleAsync(PulsarService pulsar, TopicName topic) { |
| 148 | + return pulsar.getNamespaceService().getBundleAsync(topic); |
| 149 | + } |
| 150 | + |
| 151 | + protected Pair<TopicName, NamespaceBundle> getBundleIsNotOwnByChangeEventTopic(String topicNamePrefix) |
| 152 | + throws Exception { |
| 153 | + TopicName changeEventsTopicName = |
| 154 | + TopicName.get(defaultTestNamespace + "/" + SystemTopicNames.NAMESPACE_EVENTS_LOCAL_NAME); |
| 155 | + NamespaceBundle changeEventsBundle = getBundleAsync(pulsar1, changeEventsTopicName).get(); |
| 156 | + int i = 0; |
| 157 | + while(true) { |
| 158 | + TopicName topicName = TopicName.get(defaultTestNamespace + "/" + topicNamePrefix + "-" + i); |
| 159 | + NamespaceBundle bundle = getBundleAsync(pulsar1, topicName).get(); |
| 160 | + if (!bundle.equals(changeEventsBundle)) { |
| 161 | + return Pair.of(topicName, bundle); |
| 162 | + } |
| 163 | + i++; |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments