fix: guard NPE in TopicRouteWrapper when brokerName is absent from the route - #10744
fix: guard NPE in TopicRouteWrapper when brokerName is absent from the route#10744yyqdbngt wants to merge 1 commit into
Conversation
…e route getMasterAddr and getMasterAddrPrefer called brokerNameRouteData.get(brokerName).getBrokerAddrs() without a null check, so a brokerName not present in the current route snapshot (e.g. during broker failover or a stale MessageQueue) threw NullPointerException. This contradicts the contract the callers rely on: MessageQueueSelector (buildRead/buildWrite) and ClusterTopicRouteService all check the return value for null and skip the broker. Return null when the broker is missing so the existing caller handling works as intended. Compiled and verified on the build server (mvn -pl proxy -am compile).
RockteMQ-AI
left a comment
There was a problem hiding this comment.
Review by github-manager-bot
Summary
This PR adds null guards in TopicRouteWrapper.getMasterAddr() and getMasterAddrPrefer() to prevent NPE when brokerName is not present in the current route snapshot.
Findings
-
[Info]
TopicRouteWrapper.java:46-50— The null guard forbrokerNameRouteData.get(brokerName)is correct and aligns with the caller contract (callers already check fornullreturn). This is a valid defensive fix for scenarios like broker failover or staleMessageQueuereferences. -
[Info]
TopicRouteWrapper.java:53-57— Same null guard applied togetMasterAddrPrefer(). Correct. -
[Warning]
TopicRouteWrapper.java:58— After the null guard,brokerData.getBrokerAddrs()could theoretically also returnnull(depending onBrokerDataconstruction). Consider whether a secondary null check or aCollections.emptyMap()fallback is warranted here. This is a pre-existing concern, not introduced by this PR, but worth noting since you are already hardening this class. -
[Info] No unit test is included. A simple test case covering the
brokerNameabsent scenario (verifyingnullis returned instead of NPE) would strengthen this fix and prevent regression.
Suggestions
- Consider adding a test in a new
TopicRouteWrapperTestclass:
@Test
public void testGetMasterAddr_missingBroker_returnsNull() {
TopicRouteData routeData = new TopicRouteData();
routeData.setBrokerDatas(new ArrayList<>()); // empty
TopicRouteWrapper wrapper = new TopicRouteWrapper(routeData, "testTopic");
assertNull(wrapper.getMasterAddr("nonExistentBroker"));
assertNull(wrapper.getMasterAddrPrefer("nonExistentBroker"));
}Verdict
The fix is correct, minimal, and addresses a real NPE risk. LGTM with the suggestion to add test coverage.
Automated review by github-manager-bot
Motivation
TopicRouteWrapper.getMasterAddrandgetMasterAddrPrefercalledbrokerNameRouteData.get(brokerName).getBrokerAddrs()without a null check. WhenbrokerNameis not present in the current route snapshot (e.g. during broker failover, or a staleMessageQueuereferencing a broker no longer in the route),get(brokerName)returnsnulland the chainedgetBrokerAddrs()throwsNullPointerException.This contradicts the contract the callers rely on. In
MessageQueueSelector.buildRead/buildWriteandClusterTopicRouteService, every call site checks the return value fornulland skips the broker:So these methods are supposed to return
nullfor a missing broker, but instead they throw. The fix guards the lookup and returnsnull, making the existing caller handling work as intended.Verification
mvn -pl proxy -am compilepasses on the build server.Diff
1 file changed, +10 / -2.