|
| 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 java.util.ArrayList; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Optional; |
| 25 | +import lombok.extern.slf4j.Slf4j; |
| 26 | +import org.apache.pulsar.broker.PulsarService; |
| 27 | +import org.apache.pulsar.broker.ServiceConfiguration; |
| 28 | +import org.apache.pulsar.client.admin.PulsarAdmin; |
| 29 | +import org.apache.pulsar.client.api.PulsarClient; |
| 30 | +import org.apache.pulsar.common.policies.data.ClusterData; |
| 31 | +import org.apache.pulsar.common.policies.data.TenantInfo; |
| 32 | +import org.apache.pulsar.zookeeper.LocalBookkeeperEnsemble; |
| 33 | +import org.testng.Assert; |
| 34 | +import org.testng.annotations.AfterClass; |
| 35 | +import org.testng.annotations.BeforeClass; |
| 36 | +import org.testng.annotations.Test; |
| 37 | + |
| 38 | +@Slf4j |
| 39 | +public class ExtensibleLoadManagerCloseTest { |
| 40 | + |
| 41 | + private static final String clusterName = "test"; |
| 42 | + private final LocalBookkeeperEnsemble bk = new LocalBookkeeperEnsemble(1, 0, () -> 0); |
| 43 | + private final List<PulsarService> brokers = new ArrayList<>(); |
| 44 | + private PulsarAdmin admin; |
| 45 | + |
| 46 | + @BeforeClass(alwaysRun = true) |
| 47 | + public void setup() throws Exception { |
| 48 | + bk.start(); |
| 49 | + for (int i = 0; i < 3; i++) { |
| 50 | + final var broker = new PulsarService(brokerConfig()); |
| 51 | + broker.start(); |
| 52 | + brokers.add(broker); |
| 53 | + } |
| 54 | + admin = brokers.get(0).getAdminClient(); |
| 55 | + admin.clusters().createCluster(clusterName, ClusterData.builder().build()); |
| 56 | + admin.tenants().createTenant("public", TenantInfo.builder() |
| 57 | + .allowedClusters(Collections.singleton(clusterName)).build()); |
| 58 | + admin.namespaces().createNamespace("public/default"); |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + @AfterClass(alwaysRun = true, timeOut = 30000) |
| 63 | + public void cleanup() throws Exception { |
| 64 | + bk.stop(); |
| 65 | + } |
| 66 | + |
| 67 | + private ServiceConfiguration brokerConfig() { |
| 68 | + final var config = new ServiceConfiguration(); |
| 69 | + config.setClusterName(clusterName); |
| 70 | + config.setAdvertisedAddress("localhost"); |
| 71 | + config.setBrokerServicePort(Optional.of(0)); |
| 72 | + config.setWebServicePort(Optional.of(0)); |
| 73 | + config.setMetadataStoreUrl("zk:127.0.0.1:" + bk.getZookeeperPort()); |
| 74 | + config.setManagedLedgerDefaultWriteQuorum(1); |
| 75 | + config.setManagedLedgerDefaultAckQuorum(1); |
| 76 | + config.setManagedLedgerDefaultEnsembleSize(1); |
| 77 | + config.setDefaultNumberOfNamespaceBundles(16); |
| 78 | + config.setLoadBalancerAutoBundleSplitEnabled(false); |
| 79 | + config.setLoadManagerClassName(ExtensibleLoadManagerImpl.class.getName()); |
| 80 | + config.setLoadBalancerDebugModeEnabled(true); |
| 81 | + config.setBrokerShutdownTimeoutMs(100); |
| 82 | + return config; |
| 83 | + } |
| 84 | + |
| 85 | + |
| 86 | + @Test |
| 87 | + public void testCloseAfterLoadingBundles() throws Exception { |
| 88 | + final var topic = "test"; |
| 89 | + admin.topics().createPartitionedTopic(topic, 20); |
| 90 | + admin.lookups().lookupPartitionedTopic(topic); |
| 91 | + final var client = PulsarClient.builder().serviceUrl(brokers.get(0).getBrokerServiceUrl()).build(); |
| 92 | + final var producer = client.newProducer().topic(topic).create(); |
| 93 | + producer.close(); |
| 94 | + client.close(); |
| 95 | + |
| 96 | + final var closeTimeMsList = new ArrayList<Long>(); |
| 97 | + for (var broker : brokers) { |
| 98 | + final var startTimeMs = System.currentTimeMillis(); |
| 99 | + broker.close(); |
| 100 | + closeTimeMsList.add(System.currentTimeMillis() - startTimeMs); |
| 101 | + } |
| 102 | + log.info("Brokers close time: {}", closeTimeMsList); |
| 103 | + for (var closeTimeMs : closeTimeMsList) { |
| 104 | + Assert.assertTrue(closeTimeMs < 5000L); |
| 105 | + } |
| 106 | + } |
| 107 | +} |
0 commit comments