TheR1sing3un commented on code in PR #6100:
URL: https://github.com/apache/rocketmq/pull/6100#discussion_r1113845676
##########
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:
>
Yep! I rename it to `registerBrokerToController`, only if this request is
successful will this register process is completely finished !
--
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]