|
| 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.scheduler; |
| 20 | + |
| 21 | +import static org.mockito.ArgumentMatchers.any; |
| 22 | +import static org.mockito.ArgumentMatchers.eq; |
| 23 | +import static org.mockito.Mockito.doAnswer; |
| 24 | +import static org.mockito.Mockito.doReturn; |
| 25 | +import static org.mockito.Mockito.mock; |
| 26 | +import static org.mockito.Mockito.times; |
| 27 | +import static org.mockito.Mockito.verify; |
| 28 | + |
| 29 | +import com.google.common.collect.Lists; |
| 30 | +import org.apache.pulsar.broker.ServiceConfiguration; |
| 31 | +import org.apache.pulsar.broker.loadbalance.extensions.BrokerRegistry; |
| 32 | +import org.apache.pulsar.broker.loadbalance.extensions.LoadManagerContext; |
| 33 | +import org.apache.pulsar.broker.loadbalance.extensions.channel.ServiceUnitStateChannel; |
| 34 | +import org.apache.pulsar.broker.loadbalance.extensions.models.Unload; |
| 35 | +import org.apache.pulsar.broker.loadbalance.extensions.models.UnloadDecision; |
| 36 | +import org.apache.pulsar.client.util.ExecutorProvider; |
| 37 | +import org.testng.annotations.AfterMethod; |
| 38 | +import org.testng.annotations.BeforeMethod; |
| 39 | +import org.testng.annotations.Test; |
| 40 | +import java.util.concurrent.CompletableFuture; |
| 41 | +import java.util.concurrent.CountDownLatch; |
| 42 | +import java.util.concurrent.ExecutorService; |
| 43 | +import java.util.concurrent.Executors; |
| 44 | +import java.util.concurrent.ScheduledExecutorService; |
| 45 | +import java.util.concurrent.TimeUnit; |
| 46 | + |
| 47 | +@Test(groups = "broker") |
| 48 | +public class NamespaceUnloadSchedulerTest { |
| 49 | + |
| 50 | + private ScheduledExecutorService loadManagerExecutor; |
| 51 | + |
| 52 | + public LoadManagerContext setupContext(){ |
| 53 | + var ctx = getContext(); |
| 54 | + ctx.brokerConfiguration().setLoadBalancerDebugModeEnabled(true); |
| 55 | + return ctx; |
| 56 | + } |
| 57 | + |
| 58 | + @BeforeMethod |
| 59 | + public void setUp() { |
| 60 | + this.loadManagerExecutor = Executors |
| 61 | + .newSingleThreadScheduledExecutor(new ExecutorProvider.ExtendedThreadFactory("pulsar-load-manager")); |
| 62 | + } |
| 63 | + |
| 64 | + @AfterMethod |
| 65 | + public void tearDown() { |
| 66 | + this.loadManagerExecutor.shutdown(); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testExecuteSuccess() { |
| 71 | + LoadManagerContext context = setupContext(); |
| 72 | + BrokerRegistry registry = context.brokerRegistry(); |
| 73 | + ServiceUnitStateChannel channel = mock(ServiceUnitStateChannel.class); |
| 74 | + NamespaceUnloadStrategy unloadStrategy = mock(NamespaceUnloadStrategy.class); |
| 75 | + doReturn(CompletableFuture.completedFuture(true)).when(channel).isChannelOwnerAsync(); |
| 76 | + doReturn(CompletableFuture.completedFuture(Lists.newArrayList("broker-1", "broker-2"))) |
| 77 | + .when(registry).getAvailableBrokersAsync(); |
| 78 | + doReturn(CompletableFuture.completedFuture(null)).when(channel).publishUnloadEventAsync(any()); |
| 79 | + UnloadDecision decision = new UnloadDecision(); |
| 80 | + Unload unload = new Unload("broker-1", "bundle-1"); |
| 81 | + decision.getUnloads().put("broker-1", unload); |
| 82 | + doReturn(decision).when(unloadStrategy).findBundlesForUnloading(any(), any(), any()); |
| 83 | + |
| 84 | + NamespaceUnloadScheduler scheduler = |
| 85 | + new NamespaceUnloadScheduler(loadManagerExecutor, context, channel, unloadStrategy); |
| 86 | + |
| 87 | + scheduler.execute(); |
| 88 | + |
| 89 | + verify(channel, times(1)).publishUnloadEventAsync(eq(unload)); |
| 90 | + |
| 91 | + // Test empty unload. |
| 92 | + UnloadDecision emptyUnload = new UnloadDecision(); |
| 93 | + doReturn(emptyUnload).when(unloadStrategy).findBundlesForUnloading(any(), any(), any()); |
| 94 | + |
| 95 | + scheduler.execute(); |
| 96 | + |
| 97 | + verify(channel, times(1)).publishUnloadEventAsync(eq(unload)); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testExecuteMoreThenOnceWhenFirstNotDone() throws InterruptedException { |
| 102 | + LoadManagerContext context = setupContext(); |
| 103 | + BrokerRegistry registry = context.brokerRegistry(); |
| 104 | + ServiceUnitStateChannel channel = mock(ServiceUnitStateChannel.class); |
| 105 | + NamespaceUnloadStrategy unloadStrategy = mock(NamespaceUnloadStrategy.class); |
| 106 | + doReturn(CompletableFuture.completedFuture(true)).when(channel).isChannelOwnerAsync(); |
| 107 | + doAnswer(__ -> CompletableFuture.supplyAsync(() -> { |
| 108 | + try { |
| 109 | + // Delay 5 seconds to finish. |
| 110 | + TimeUnit.SECONDS.sleep(5); |
| 111 | + } catch (InterruptedException e) { |
| 112 | + throw new RuntimeException(e); |
| 113 | + } |
| 114 | + return Lists.newArrayList("broker-1", "broker-2"); |
| 115 | + }, Executors.newFixedThreadPool(1))).when(registry).getAvailableBrokersAsync(); |
| 116 | + NamespaceUnloadScheduler scheduler = |
| 117 | + new NamespaceUnloadScheduler(loadManagerExecutor, context, channel, unloadStrategy); |
| 118 | + |
| 119 | + ExecutorService executorService = Executors.newFixedThreadPool(10); |
| 120 | + CountDownLatch latch = new CountDownLatch(10); |
| 121 | + for (int i = 0; i < 10; i++) { |
| 122 | + executorService.execute(() -> { |
| 123 | + scheduler.execute(); |
| 124 | + latch.countDown(); |
| 125 | + }); |
| 126 | + } |
| 127 | + latch.await(); |
| 128 | + |
| 129 | + verify(registry, times(1)).getAvailableBrokersAsync(); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testDisableLoadBalancer() { |
| 134 | + LoadManagerContext context = setupContext(); |
| 135 | + context.brokerConfiguration().setLoadBalancerEnabled(false); |
| 136 | + ServiceUnitStateChannel channel = mock(ServiceUnitStateChannel.class); |
| 137 | + NamespaceUnloadStrategy unloadStrategy = mock(NamespaceUnloadStrategy.class); |
| 138 | + NamespaceUnloadScheduler scheduler = |
| 139 | + new NamespaceUnloadScheduler(loadManagerExecutor, context, channel, unloadStrategy); |
| 140 | + |
| 141 | + scheduler.execute(); |
| 142 | + |
| 143 | + verify(channel, times(0)).isChannelOwnerAsync(); |
| 144 | + |
| 145 | + context.brokerConfiguration().setLoadBalancerEnabled(true); |
| 146 | + context.brokerConfiguration().setLoadBalancerSheddingEnabled(false); |
| 147 | + scheduler.execute(); |
| 148 | + |
| 149 | + verify(channel, times(0)).isChannelOwnerAsync(); |
| 150 | + } |
| 151 | + |
| 152 | + @Test |
| 153 | + public void testNotChannelOwner() { |
| 154 | + LoadManagerContext context = setupContext(); |
| 155 | + context.brokerConfiguration().setLoadBalancerEnabled(false); |
| 156 | + ServiceUnitStateChannel channel = mock(ServiceUnitStateChannel.class); |
| 157 | + NamespaceUnloadStrategy unloadStrategy = mock(NamespaceUnloadStrategy.class); |
| 158 | + NamespaceUnloadScheduler scheduler = |
| 159 | + new NamespaceUnloadScheduler(loadManagerExecutor, context, channel, unloadStrategy); |
| 160 | + doReturn(CompletableFuture.completedFuture(false)).when(channel).isChannelOwnerAsync(); |
| 161 | + |
| 162 | + scheduler.execute(); |
| 163 | + |
| 164 | + verify(context.brokerRegistry(), times(0)).getAvailableBrokersAsync(); |
| 165 | + } |
| 166 | + |
| 167 | + public LoadManagerContext getContext(){ |
| 168 | + var ctx = mock(LoadManagerContext.class); |
| 169 | + var registry = mock(BrokerRegistry.class); |
| 170 | + var conf = new ServiceConfiguration(); |
| 171 | + doReturn(conf).when(ctx).brokerConfiguration(); |
| 172 | + doReturn(registry).when(ctx).brokerRegistry(); |
| 173 | + return ctx; |
| 174 | + } |
| 175 | +} |
0 commit comments