mxsm commented on code in PR #6100:
URL: https://github.com/apache/rocketmq/pull/6100#discussion_r1112738599


##########
broker/src/main/java/org/apache/rocketmq/broker/controller/ReplicasManager.java:
##########
@@ -108,10 +124,23 @@ public long getConfirmOffset() {
     enum State {
         INITIAL,
         FIRST_TIME_SYNC_CONTROLLER_METADATA_DONE,
+
+        FIRST_TIME_REGISTER_TO_CONTROLLER_DONE,
+
         RUNNING,
         SHUTDOWN,
     }

Review Comment:
   Could the enum name be changed to make it more semantically expressive



##########
store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/TempBrokerMetadata.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.store.ha.autoswitch;
+
+import org.apache.commons.lang3.StringUtils;
+
+public class TempBrokerMetadata extends MetadataFile {
+
+    private String clusterName;
+

Review Comment:
   How about  TempBrokerMetadata  extends BrokerMetadata 



##########
broker/src/main/java/org/apache/rocketmq/broker/controller/ReplicasManager.java:
##########
@@ -296,24 +347,89 @@ private void handleSlaveSynchronize(final BrokerRole 
role) {
         }
     }
 
+    private boolean brokerElect() {
+        // Broker try to elect itself as a master in broker set.
+        try {
+            ElectMasterResponseHeader tryElectResponse = 
this.brokerOuterAPI.brokerElect(this.controllerLeaderAddress, 
this.brokerConfig.getBrokerClusterName(),
+                this.brokerConfig.getBrokerName(), this.brokerId);
+            final String masterAddress = tryElectResponse.getMasterAddress();
+            final Long masterBrokerId = tryElectResponse.getMasterBrokerId();
+            if (StringUtils.isEmpty(masterAddress) || masterBrokerId == null) {
+                LOGGER.warn("Now no master in broker set");
+                return false;
+            }
+
+            if (masterBrokerId.equals(this.brokerId)) {
+                changeToMaster(tryElectResponse.getMasterEpoch(), 
tryElectResponse.getSyncStateSetEpoch());
+            } else {
+                changeToSlave(masterAddress, 
tryElectResponse.getMasterEpoch(), tryElectResponse.getMasterBrokerId());
+            }
+            brokerController.setIsolated(false);
+            return true;
+        } catch (Exception e) {
+            LOGGER.error("Failed to try elect", e);
+            return false;
+        }
+    }
+
+    public void sendHeartbeatToController() {
+        final List<String> controllerAddresses = 
this.getAvailableControllerAddresses();
+        for (String controllerAddress : controllerAddresses) {
+            if (StringUtils.isNotEmpty(controllerAddress)) {
+                this.brokerOuterAPI.sendHeartbeatToController(
+                        controllerAddress,
+                        this.brokerConfig.getBrokerClusterName(),
+                        this.localAddress,
+                        this.brokerConfig.getBrokerName(),
+                        this.brokerId,

Review Comment:
   How about `this.localAddress`  rename `this.brokerAddress`?



##########
broker/src/main/java/org/apache/rocketmq/broker/controller/ReplicasManager.java:
##########
@@ -322,35 +438,150 @@ private boolean registerBrokerToController() {
         }
     }
 
+    /**
+     * Send GetNextBrokerRequest to controller for getting next assigning 
brokerId in this broker-set
+     * @return next brokerId in this broker-set
+     */
+    private Long getNextBrokerId() {
+        try {
+            GetNextBrokerIdResponseHeader nextBrokerIdResp = 
this.brokerOuterAPI.getNextBrokerId(this.brokerConfig.getBrokerClusterName(), 
this.brokerConfig.getBrokerName(), this.controllerLeaderAddress);
+            return nextBrokerIdResp.getNextBrokerId();
+        } catch (Exception e) {
+            LOGGER.error("fail to get next broker id from controller", e);
+            return null;
+        }
+    }
+
+    /**
+     * Create temp metadata file in local file system, records the brokerId 
and registerCheckCode
+     * @param brokerId the brokerId that is expected to be assigned
+     * @return whether the temp meta file is created successfully
+     */
+
+    private boolean createTempMetadataFile(Long brokerId) {
+        // generate register check code, format like that: 
$ipAddress;$timestamp
+        String registerCheckCode = this.localAddress + ";" + 
System.currentTimeMillis();
+        try {
+            
this.tempBrokerMetadata.updateAndPersist(brokerConfig.getBrokerClusterName(), 
brokerConfig.getBrokerName(), brokerId, registerCheckCode);
+            return true;
+        } catch (Exception e) {
+            LOGGER.error("update and persist temp broker metadata file 
failed", e);
+            this.tempBrokerMetadata.clear();
+            return false;
+        }
+    }
+
+    /**
+     * Send applyBrokerId request to controller
+     * @return whether controller has assigned this brokerId for this broker
+     */
+    private boolean applyBrokerId() {
+        try {
+            ApplyBrokerIdResponseHeader response = 
this.brokerOuterAPI.applyBrokerId(brokerConfig.getBrokerClusterName(), 
brokerConfig.getBrokerName(),
+                    tempBrokerMetadata.getBrokerId(), 
tempBrokerMetadata.getRegisterCheckCode(), this.controllerLeaderAddress);
+            return true;
+
+        } catch (Exception e) {
+            LOGGER.error("fail to apply broker id: {}", e, 
tempBrokerMetadata.getBrokerId());
+            return false;
+        }
+    }

Review Comment:
   response whether to determine?



##########
broker/src/main/java/org/apache/rocketmq/broker/controller/ReplicasManager.java:
##########
@@ -322,35 +438,150 @@ private boolean registerBrokerToController() {
         }
     }
 
+    /**
+     * Send GetNextBrokerRequest to controller for getting next assigning 
brokerId in this broker-set
+     * @return next brokerId in this broker-set
+     */
+    private Long getNextBrokerId() {
+        try {
+            GetNextBrokerIdResponseHeader nextBrokerIdResp = 
this.brokerOuterAPI.getNextBrokerId(this.brokerConfig.getBrokerClusterName(), 
this.brokerConfig.getBrokerName(), this.controllerLeaderAddress);
+            return nextBrokerIdResp.getNextBrokerId();
+        } catch (Exception e) {
+            LOGGER.error("fail to get next broker id from controller", e);
+            return null;
+        }
+    }
+
+    /**
+     * Create temp metadata file in local file system, records the brokerId 
and registerCheckCode
+     * @param brokerId the brokerId that is expected to be assigned
+     * @return whether the temp meta file is created successfully
+     */
+
+    private boolean createTempMetadataFile(Long brokerId) {
+        // generate register check code, format like that: 
$ipAddress;$timestamp
+        String registerCheckCode = this.localAddress + ";" + 
System.currentTimeMillis();
+        try {
+            
this.tempBrokerMetadata.updateAndPersist(brokerConfig.getBrokerClusterName(), 
brokerConfig.getBrokerName(), brokerId, registerCheckCode);
+            return true;
+        } catch (Exception e) {
+            LOGGER.error("update and persist temp broker metadata file 
failed", e);
+            this.tempBrokerMetadata.clear();
+            return false;
+        }
+    }
+
+    /**
+     * Send applyBrokerId request to controller
+     * @return whether controller has assigned this brokerId for this broker
+     */
+    private boolean applyBrokerId() {
+        try {
+            ApplyBrokerIdResponseHeader response = 
this.brokerOuterAPI.applyBrokerId(brokerConfig.getBrokerClusterName(), 
brokerConfig.getBrokerName(),
+                    tempBrokerMetadata.getBrokerId(), 
tempBrokerMetadata.getRegisterCheckCode(), this.controllerLeaderAddress);
+            return true;
+
+        } catch (Exception e) {
+            LOGGER.error("fail to apply broker id: {}", e, 
tempBrokerMetadata.getBrokerId());
+            return false;
+        }
+    }
+
+    /**
+     * Create metadata file and delete temp metadata file
+     * @return whether process success
+     */
+    private boolean createMetadataFileAndDeleteTemp() {
+        // create metadata file and delete temp metadata file
+        try {
+            
this.brokerMetadata.updateAndPersist(brokerConfig.getBrokerClusterName(), 
brokerConfig.getBrokerName(), tempBrokerMetadata.getBrokerId());
+            this.tempBrokerMetadata.clear();
+            this.brokerId = this.brokerMetadata.getBrokerId();
+            return true;
+        } catch (Exception e) {
+            LOGGER.error("fail to create metadata file", e);
+            this.brokerMetadata.clear();
+            return false;
+        }
+    }
+
+    /**
+     * Send registerSuccess request to inform controller that now broker has 
been registered successfully and controller should update broker ipAddress if 
changed
+     * @return whether request success
+     */
+    private boolean registerSuccess() {
+        try {
+            RegisterSuccessResponseHeader response = 
this.brokerOuterAPI.registerSuccess(brokerConfig.getBrokerClusterName(), 
brokerConfig.getBrokerName(), brokerId, localAddress, controllerLeaderAddress);
+            final Long masterBrokerId = response.getMasterBrokerId();
+            final String masterAddress = response.getMasterAddress();
+            if (masterBrokerId == null) {
+                return true;
+            }
+            if (this.brokerId.equals(masterBrokerId)) {
+                changeToMaster(response.getMasterEpoch(), 
response.getSyncStateSetEpoch());
+            } else {
+                changeToSlave(masterAddress, response.getMasterEpoch(), 
masterBrokerId);
+            }
+            brokerController.setIsolated(false);
+            return true;
+        } catch (Exception e) {
+            LOGGER.error("fail to send registerSuccess request to controller", 
e);
+            return false;
+        }
+    }

Review Comment:
   registerSuccess method name、this.brokerOuterAPI.registerSuccess、 and request 
code  REGISTER_SUCCESS can rename. These names give me the understanding that 
the registration is successful
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to