From 878fde6d0166df3182956d94858d7e80124b251e Mon Sep 17 00:00:00 2001 From: RongtongJin Date: Wed, 15 Jun 2022 15:56:54 +0800 Subject: [PATCH] Polish the logic of change to master --- .../broker/hacontroller/ReplicasManager.java | 25 +++++++++++-------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/broker/src/main/java/org/apache/rocketmq/broker/hacontroller/ReplicasManager.java b/broker/src/main/java/org/apache/rocketmq/broker/hacontroller/ReplicasManager.java index c11129ca705..d4ac86760b1 100644 --- a/broker/src/main/java/org/apache/rocketmq/broker/hacontroller/ReplicasManager.java +++ b/broker/src/main/java/org/apache/rocketmq/broker/hacontroller/ReplicasManager.java @@ -163,8 +163,6 @@ public void changeToMaster(final int newMasterEpoch, final int syncStateSetEpoch if (newMasterEpoch > this.masterEpoch) { LOGGER.info("Begin to change to master, brokerName:{}, replicas:{}, new Epoch:{}", this.brokerConfig.getBrokerName(), this.localAddress, newMasterEpoch); - // Change record - this.masterAddress = this.localAddress; this.masterEpoch = newMasterEpoch; // Change sync state set @@ -172,17 +170,24 @@ public void changeToMaster(final int newMasterEpoch, final int syncStateSetEpoch newSyncStateSet.add(this.localAddress); changeSyncStateSet(newSyncStateSet, syncStateSetEpoch); - // Handle the slave synchronise - handleSlaveSynchronize(BrokerRole.SYNC_MASTER); + if (this.localAddress.equals(masterAddress) && brokerController.getBrokerConfig().getBrokerId() == MixAll.MASTER_ID) { + LOGGER.warn("The broker role is already master"); + } else { + // Change record + this.masterAddress = this.localAddress; - // Notify ha service, change to master - this.haService.changeToMaster(newMasterEpoch); + // Handle the slave synchronise + handleSlaveSynchronize(BrokerRole.SYNC_MASTER); - this.brokerController.getBrokerConfig().setBrokerId(MixAll.MASTER_ID); - this.brokerController.getMessageStoreConfig().setBrokerRole(BrokerRole.SYNC_MASTER); - this.brokerController.changeSpecialServiceStatus(true); + // Notify ha service, change to master + this.haService.changeToMaster(newMasterEpoch); - schedulingCheckSyncStateSet(); + this.brokerController.getBrokerConfig().setBrokerId(MixAll.MASTER_ID); + this.brokerController.getMessageStoreConfig().setBrokerRole(BrokerRole.SYNC_MASTER); + this.brokerController.changeSpecialServiceStatus(true); + + schedulingCheckSyncStateSet(); + } this.executorService.submit(() -> { // Register broker to name-srv