From f28b69bb8965249d86f92562ba30746142dbc244 Mon Sep 17 00:00:00 2001 From: yuluo-yx Date: Sun, 9 Aug 2026 12:22:17 +0800 Subject: [PATCH] fix(controller): compare partial broker identities safely --- .../impl/heartbeat/BrokerIdentityInfo.java | 6 +-- .../heartbeat/BrokerIdentityInfoTest.java | 52 +++++++++++++++++++ 2 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 controller/src/test/java/org/apache/rocketmq/controller/impl/heartbeat/BrokerIdentityInfoTest.java diff --git a/controller/src/main/java/org/apache/rocketmq/controller/impl/heartbeat/BrokerIdentityInfo.java b/controller/src/main/java/org/apache/rocketmq/controller/impl/heartbeat/BrokerIdentityInfo.java index 8fc04957e68..cb6b8153ecf 100644 --- a/controller/src/main/java/org/apache/rocketmq/controller/impl/heartbeat/BrokerIdentityInfo.java +++ b/controller/src/main/java/org/apache/rocketmq/controller/impl/heartbeat/BrokerIdentityInfo.java @@ -17,9 +17,8 @@ package org.apache.rocketmq.controller.impl.heartbeat; import java.io.Serializable; -import org.apache.rocketmq.common.UtilAll; - import java.util.Objects; +import org.apache.rocketmq.common.UtilAll; public class BrokerIdentityInfo implements Serializable { @@ -63,7 +62,8 @@ public boolean equals(Object obj) { if (obj instanceof BrokerIdentityInfo) { BrokerIdentityInfo addr = (BrokerIdentityInfo) obj; - return clusterName.equals(addr.clusterName) && brokerName.equals(addr.brokerName) && brokerId.equals(addr.brokerId); + return Objects.equals(clusterName, addr.clusterName) && Objects.equals(brokerName, addr.brokerName) + && Objects.equals(brokerId, addr.brokerId); } return false; } diff --git a/controller/src/test/java/org/apache/rocketmq/controller/impl/heartbeat/BrokerIdentityInfoTest.java b/controller/src/test/java/org/apache/rocketmq/controller/impl/heartbeat/BrokerIdentityInfoTest.java new file mode 100644 index 00000000000..836ae5065d3 --- /dev/null +++ b/controller/src/test/java/org/apache/rocketmq/controller/impl/heartbeat/BrokerIdentityInfoTest.java @@ -0,0 +1,52 @@ +/* + * 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.rocketmq.controller.impl.heartbeat; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class BrokerIdentityInfoTest { + + @Test + public void testEqualsCompleteIdentity() { + BrokerIdentityInfo identity = new BrokerIdentityInfo("cluster", "broker", 1L); + BrokerIdentityInfo sameIdentity = new BrokerIdentityInfo("cluster", "broker", 1L); + + assertThat(identity).isEqualTo(sameIdentity); + assertThat(identity.hashCode()).isEqualTo(sameIdentity.hashCode()); + } + + @Test + public void testEqualsPartialIdentity() { + assertThat(new BrokerIdentityInfo(null, "broker", null)) + .isEqualTo(new BrokerIdentityInfo(null, "broker", null)); + assertThat(new BrokerIdentityInfo("cluster", null, null)) + .isEqualTo(new BrokerIdentityInfo("cluster", null, null)); + assertThat(new BrokerIdentityInfo("cluster", "broker", null)) + .isEqualTo(new BrokerIdentityInfo("cluster", "broker", null)); + } + + @Test + public void testEqualsDifferentIdentity() { + BrokerIdentityInfo identity = new BrokerIdentityInfo("cluster", "broker", 1L); + + assertThat(identity).isNotEqualTo(new BrokerIdentityInfo("other-cluster", "broker", 1L)); + assertThat(identity).isNotEqualTo(new BrokerIdentityInfo("cluster", "other-broker", 1L)); + assertThat(identity).isNotEqualTo(new BrokerIdentityInfo("cluster", "broker", 2L)); + } +}