|
| 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.manager; |
| 20 | + |
| 21 | +import static org.mockito.Mockito.doReturn; |
| 22 | +import static org.mockito.Mockito.mock; |
| 23 | +import static org.mockito.Mockito.spy; |
| 24 | +import static org.mockito.Mockito.when; |
| 25 | +import static org.testng.Assert.assertFalse; |
| 26 | +import static org.testng.Assert.assertTrue; |
| 27 | + |
| 28 | +import org.apache.pulsar.broker.PulsarService; |
| 29 | +import org.apache.pulsar.broker.ServiceConfiguration; |
| 30 | +import org.apache.pulsar.broker.loadbalance.extensions.ExtensibleLoadManagerImpl; |
| 31 | +import org.apache.pulsar.broker.loadbalance.extensions.data.BrokerLookupData; |
| 32 | +import org.apache.pulsar.broker.loadbalance.impl.ModularLoadManagerImpl; |
| 33 | +import org.apache.pulsar.broker.lookup.LookupResult; |
| 34 | +import org.apache.pulsar.policies.data.loadbalancer.AdvertisedListener; |
| 35 | +import org.testng.annotations.Test; |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.Map; |
| 38 | +import java.util.Optional; |
| 39 | +import java.util.concurrent.CompletableFuture; |
| 40 | +import java.util.concurrent.ExecutionException; |
| 41 | + |
| 42 | + |
| 43 | +/** |
| 44 | + * Unit test {@link RedirectManager}. |
| 45 | + */ |
| 46 | +public class RedirectManagerTest { |
| 47 | + |
| 48 | + @Test |
| 49 | + public void testFindRedirectLookupResultAsync() throws ExecutionException, InterruptedException { |
| 50 | + PulsarService pulsar = mock(PulsarService.class); |
| 51 | + ServiceConfiguration configuration = new ServiceConfiguration(); |
| 52 | + when(pulsar.getConfiguration()).thenReturn(configuration); |
| 53 | + RedirectManager redirectManager = spy(new RedirectManager(pulsar, null)); |
| 54 | + |
| 55 | + configuration.setLoadManagerClassName(ModularLoadManagerImpl.class.getName()); |
| 56 | + configuration.setLoadBalancerDebugModeEnabled(true); |
| 57 | + |
| 58 | + // Test 1: No load manager class name found. |
| 59 | + doReturn(CompletableFuture.completedFuture( |
| 60 | + new HashMap<>(){{ |
| 61 | + put("broker-1", getLookupData("broker-1", null, 10)); |
| 62 | + put("broker-2", getLookupData("broker-2", ModularLoadManagerImpl.class.getName(), 1)); |
| 63 | + }} |
| 64 | + )).when(redirectManager).getAvailableBrokerLookupDataAsync(); |
| 65 | + |
| 66 | + // Should redirect to broker-1, since broker-1 has the latest load manager, even though the class name is null. |
| 67 | + Optional<LookupResult> lookupResult = redirectManager.findRedirectLookupResultAsync().get(); |
| 68 | + assertTrue(lookupResult.isPresent()); |
| 69 | + assertTrue(lookupResult.get().getLookupData().getBrokerUrl().contains("broker-1")); |
| 70 | + |
| 71 | + // Test 2: Should redirect to broker-1, since the latest broker are using ExtensibleLoadManagerImpl |
| 72 | + doReturn(CompletableFuture.completedFuture( |
| 73 | + new HashMap<>(){{ |
| 74 | + put("broker-1", getLookupData("broker-1", ExtensibleLoadManagerImpl.class.getName(), 10)); |
| 75 | + put("broker-2", getLookupData("broker-2", ModularLoadManagerImpl.class.getName(), 1)); |
| 76 | + }} |
| 77 | + )).when(redirectManager).getAvailableBrokerLookupDataAsync(); |
| 78 | + |
| 79 | + lookupResult = redirectManager.findRedirectLookupResultAsync().get(); |
| 80 | + assertTrue(lookupResult.isPresent()); |
| 81 | + assertTrue(lookupResult.get().getLookupData().getBrokerUrl().contains("broker-1")); |
| 82 | + |
| 83 | + |
| 84 | + // Test 3: Should not redirect, since current broker are using ModularLoadManagerImpl |
| 85 | + doReturn(CompletableFuture.completedFuture( |
| 86 | + new HashMap<>(){{ |
| 87 | + put("broker-1", getLookupData("broker-1", ExtensibleLoadManagerImpl.class.getName(), 10)); |
| 88 | + put("broker-2", getLookupData("broker-2", ModularLoadManagerImpl.class.getName(), 100)); |
| 89 | + }} |
| 90 | + )).when(redirectManager).getAvailableBrokerLookupDataAsync(); |
| 91 | + |
| 92 | + lookupResult = redirectManager.findRedirectLookupResultAsync().get(); |
| 93 | + assertFalse(lookupResult.isPresent()); |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | + public BrokerLookupData getLookupData(String broker, String loadManagerClassName, long startTimeStamp) { |
| 98 | + String webServiceUrl = "http://" + broker + ":8080"; |
| 99 | + String webServiceUrlTls = "https://" + broker + ":8081"; |
| 100 | + String pulsarServiceUrl = "pulsar://" + broker + ":6650"; |
| 101 | + String pulsarServiceUrlTls = "pulsar+ssl://" + broker + ":6651"; |
| 102 | + Map<String, AdvertisedListener> advertisedListeners = new HashMap<>(); |
| 103 | + Map<String, String> protocols = new HashMap<>(){{ |
| 104 | + put("kafka", "9092"); |
| 105 | + }}; |
| 106 | + return new BrokerLookupData( |
| 107 | + webServiceUrl, webServiceUrlTls, pulsarServiceUrl, |
| 108 | + pulsarServiceUrlTls, advertisedListeners, protocols, true, true, |
| 109 | + loadManagerClassName, startTimeStamp, "3.0.0"); |
| 110 | + } |
| 111 | +} |
0 commit comments