-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[fix][broker] Fix broker load manager class filter NPE (#20350)
PIP: #16691 ### Motivation When upgrading the pulsar version and changing the pulsar load manager to `ExtensibleLoadManagerImpl` it might cause NPE. The root cause is the old version of pulsar does not contain the `loadManagerClassName` field. ``` 2023-05-18T05:42:50,557+0000 [pulsar-io-4-1] INFO org.apache.pulsar.broker.service.ServerCnx - [/127.0.0.6:51345] connected with role=[[email protected]](mailto:[email protected]) using authMethod=token, clientVersion=Pulsar Go 0.9.0, clientProtocolVersion=18, proxyVersion=null 2023-05-18T05:42:50,558+0000 [pulsar-io-4-1] WARN org.apache.pulsar.broker.lookup.TopicLookupBase - Failed to lookup [[email protected]](mailto:[email protected]) for topic persistent://xxx with error java.lang.NullPointerException: Cannot invoke “String.equals(Object)” because the return value of “org.apache.pulsar.broker.loadbalance.extensions.data.BrokerLookupData.getLoadManagerClassName()” is null java.util.concurrent.CompletionException: java.lang.NullPointerException: Cannot invoke “String.equals(Object)” because the return value of “org.apache.pulsar.broker.loadbalance.extensions.data.BrokerLookupData.getLoadManagerClassName()” is null at java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:315) ~[?:?] at java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:1194) ~[?:?] at java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2309) ~[?:?] at org.apache.pulsar.broker.loadbalance.extensions.ExtensibleLoadManagerImpl.selectAsync(ExtensibleLoadManagerImpl.java:385) ~[io.streamnative-pulsar-broker-3.0.0.1.jar:3.0.0.1] at org.apache.pulsar.broker.loadbalance.extensions.ExtensibleLoadManagerImpl.lambda$assign$6(ExtensibleLoadManagerImpl.java:336) ~[io.streamnative-pulsar-broker-3.0.0.1.jar:3.0.0.1] at java.util.concurrent.CompletableFuture.uniComposeStage(CompletableFuture.java:1187) ~[?:?] at java.util.concurrent.CompletableFuture.thenCompose(CompletableFuture.java:2309) ~[?:?] at org.apache.pulsar.broker.loadbalance.extensions.ExtensibleLoadManagerImpl.lambda$assign$10(ExtensibleLoadManagerImpl.java:333) ~[io.streamnative-pulsar-broker-3.0.0.1.jar:3.0.0.1] at org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap$Section.put(ConcurrentOpenHashMap.java:409) ~[io.streamnative-pulsar-common-3.0.0.1.jar:3.0.0.1] at org.apache.pulsar.common.util.collections.ConcurrentOpenHashMap.computeIfAbsent(ConcurrentOpenHashMap.java:243) ~[io.streamnative-pulsar-common-3.0.0.1.jar:3.0.0.1] at org.apache.pulsar.broker.loadbalance.extensions.ExtensibleLoadManagerImpl.assign(ExtensibleLoadManagerImpl.java:327) ~[io.streamnative-pulsar-broker-3.0.0.1.jar:3.0.0.1] at org.apache.pulsar.broker.loadbalance.extensions.ExtensibleLoadManagerWrapper.findBrokerServiceUrl(ExtensibleLoadManagerWrapper.java:66) ~[io.streamnative-pulsar-broker-3.0.0.1.jar:3.0.0.1] at org.apache.pulsar.broker.namespace.NamespaceService.lambda$getBrokerServiceUrlAsync$0(NamespaceService.java:191) ~[io.streamnative-pulsar-broker-3.0.0.1.jar:3.0.0.1] ``` ### Modifications * Add null check when using`getLoadManagerClassName`. * Add test to cover this case. * Add `RedirectManager` unit test.
- Loading branch information
1 parent
a9f2f28
commit b7f0004
Showing
6 changed files
with
139 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
...est/java/org/apache/pulsar/broker/loadbalance/extensions/manager/RedirectManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.broker.loadbalance.extensions.manager; | ||
|
||
import static org.mockito.Mockito.doReturn; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.spy; | ||
import static org.mockito.Mockito.when; | ||
import static org.testng.Assert.assertFalse; | ||
import static org.testng.Assert.assertTrue; | ||
|
||
import org.apache.pulsar.broker.PulsarService; | ||
import org.apache.pulsar.broker.ServiceConfiguration; | ||
import org.apache.pulsar.broker.loadbalance.extensions.ExtensibleLoadManagerImpl; | ||
import org.apache.pulsar.broker.loadbalance.extensions.data.BrokerLookupData; | ||
import org.apache.pulsar.broker.loadbalance.impl.ModularLoadManagerImpl; | ||
import org.apache.pulsar.broker.lookup.LookupResult; | ||
import org.apache.pulsar.policies.data.loadbalancer.AdvertisedListener; | ||
import org.testng.annotations.Test; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ExecutionException; | ||
|
||
|
||
/** | ||
* Unit test {@link RedirectManager}. | ||
*/ | ||
public class RedirectManagerTest { | ||
|
||
@Test | ||
public void testFindRedirectLookupResultAsync() throws ExecutionException, InterruptedException { | ||
PulsarService pulsar = mock(PulsarService.class); | ||
ServiceConfiguration configuration = new ServiceConfiguration(); | ||
when(pulsar.getConfiguration()).thenReturn(configuration); | ||
RedirectManager redirectManager = spy(new RedirectManager(pulsar, null)); | ||
|
||
configuration.setLoadManagerClassName(ModularLoadManagerImpl.class.getName()); | ||
configuration.setLoadBalancerDebugModeEnabled(true); | ||
|
||
// Test 1: No load manager class name found. | ||
doReturn(CompletableFuture.completedFuture( | ||
new HashMap<>(){{ | ||
put("broker-1", getLookupData("broker-1", null, 10)); | ||
put("broker-2", getLookupData("broker-2", ModularLoadManagerImpl.class.getName(), 1)); | ||
}} | ||
)).when(redirectManager).getAvailableBrokerLookupDataAsync(); | ||
|
||
// Should redirect to broker-1, since broker-1 has the latest load manager, even though the class name is null. | ||
Optional<LookupResult> lookupResult = redirectManager.findRedirectLookupResultAsync().get(); | ||
assertTrue(lookupResult.isPresent()); | ||
assertTrue(lookupResult.get().getLookupData().getBrokerUrl().contains("broker-1")); | ||
|
||
// Test 2: Should redirect to broker-1, since the latest broker are using ExtensibleLoadManagerImpl | ||
doReturn(CompletableFuture.completedFuture( | ||
new HashMap<>(){{ | ||
put("broker-1", getLookupData("broker-1", ExtensibleLoadManagerImpl.class.getName(), 10)); | ||
put("broker-2", getLookupData("broker-2", ModularLoadManagerImpl.class.getName(), 1)); | ||
}} | ||
)).when(redirectManager).getAvailableBrokerLookupDataAsync(); | ||
|
||
lookupResult = redirectManager.findRedirectLookupResultAsync().get(); | ||
assertTrue(lookupResult.isPresent()); | ||
assertTrue(lookupResult.get().getLookupData().getBrokerUrl().contains("broker-1")); | ||
|
||
|
||
// Test 3: Should not redirect, since current broker are using ModularLoadManagerImpl | ||
doReturn(CompletableFuture.completedFuture( | ||
new HashMap<>(){{ | ||
put("broker-1", getLookupData("broker-1", ExtensibleLoadManagerImpl.class.getName(), 10)); | ||
put("broker-2", getLookupData("broker-2", ModularLoadManagerImpl.class.getName(), 100)); | ||
}} | ||
)).when(redirectManager).getAvailableBrokerLookupDataAsync(); | ||
|
||
lookupResult = redirectManager.findRedirectLookupResultAsync().get(); | ||
assertFalse(lookupResult.isPresent()); | ||
} | ||
|
||
|
||
public BrokerLookupData getLookupData(String broker, String loadManagerClassName, long startTimeStamp) { | ||
String webServiceUrl = "http://" + broker + ":8080"; | ||
String webServiceUrlTls = "https://" + broker + ":8081"; | ||
String pulsarServiceUrl = "pulsar://" + broker + ":6650"; | ||
String pulsarServiceUrlTls = "pulsar+ssl://" + broker + ":6651"; | ||
Map<String, AdvertisedListener> advertisedListeners = new HashMap<>(); | ||
Map<String, String> protocols = new HashMap<>(){{ | ||
put("kafka", "9092"); | ||
}}; | ||
return new BrokerLookupData( | ||
webServiceUrl, webServiceUrlTls, pulsarServiceUrl, | ||
pulsarServiceUrlTls, advertisedListeners, protocols, true, true, | ||
loadManagerClassName, startTimeStamp, "3.0.0"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters