Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* 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.broker.controller;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;
import org.apache.rocketmq.common.MixAll;
import org.apache.rocketmq.common.UtilAll;

/**
* Advances once before registering a recovered store. A per-process startup ID, sent alongside the
* generation, prevents a restored metadata file from being mistaken for a registration retry.
*/
final class BrokerStartupMetadata {
private BrokerStartupMetadata() {
}

static long nextGeneration(Path file) throws IOException {
final Path target = file.toAbsolutePath();
long previous = 0;
try {
final String[] lines = new String(Files.readAllBytes(target), StandardCharsets.UTF_8).split("\n", -1);
if (lines.length != 4 || !"1".equals(lines[0]) || !lines[3].isEmpty()) {
throw new IOException("Invalid broker startup metadata: " + target);
}
final String payload = lines[0] + "\n" + lines[1] + "\n";
if (Integer.parseInt(lines[2]) != UtilAll.crc32(payload.getBytes(StandardCharsets.UTF_8))) {
throw new IOException("Broker startup metadata checksum mismatch: " + target);
}
previous = Long.parseLong(lines[1]);
if (previous < 1 || previous == Long.MAX_VALUE) {
throw new IOException("Invalid broker startup generation: " + previous);
}
} catch (NoSuchFileException ignored) {
// First upgraded startup. The controller still checks its own persisted generation.
} catch (NumberFormatException e) {
throw new IOException("Invalid broker startup metadata: " + target, e);
}

final long next = previous + 1;
final String payload = "1\n" + next + "\n";
final byte[] data = (payload + UtilAll.crc32(payload.getBytes(StandardCharsets.UTF_8)) + "\n")
.getBytes(StandardCharsets.UTF_8);
final Path temporary = Files.createTempFile(target.getParent(), target.getFileName() + ".", ".tmp");
try {
try (FileChannel channel = FileChannel.open(temporary, StandardOpenOption.WRITE)) {
final ByteBuffer buffer = ByteBuffer.wrap(data);
while (buffer.hasRemaining()) {
channel.write(buffer);
}
channel.force(true);
}
Files.move(temporary, target, StandardCopyOption.ATOMIC_MOVE, StandardCopyOption.REPLACE_EXISTING);
MixAll.fsyncDirectory(target.getParent());
} finally {
Files.deleteIfExists(temporary);
}
return next;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,15 @@

package org.apache.rocketmq.broker.controller;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
Expand Down Expand Up @@ -100,6 +103,13 @@ public class ReplicasManager {
private int syncStateSetEpoch = 0;
private String masterAddress = "";
private int masterEpoch = 0;
private final String brokerStartupId = UUID.randomUUID().toString();
private Long brokerStartupGeneration;
private volatile boolean startupRegistered;
private int startupMasterEpoch = -1;
private int recoveredEpoch = -1;
private boolean becameMasterThisStartup;
private volatile boolean roleReady;
private long lastSyncTimeMs = System.currentTimeMillis();
private Random random = new Random();

Expand Down Expand Up @@ -135,6 +145,8 @@ enum RegisterState {
}

public void start() {
setFenced(true);
this.recoveredEpoch = this.haService.getLastEpoch();
this.state = State.INITIAL;
updateControllerAddr();
scanAvailableControllerAddresses();
Expand Down Expand Up @@ -197,10 +209,9 @@ private boolean startBasicService() {
if (this.state == State.REGISTER_TO_CONTROLLER_DONE) {
// The scheduled task for heartbeat sending is not starting now, so we should manually send heartbeat request
this.sendHeartbeatToController();
if (this.masterBrokerId != null || brokerElect()) {
if (brokerElect()) {
LOGGER.info("Master in this broker set is elected, masterBrokerId: {}, masterBrokerAddr: {}", this.masterBrokerId, this.masterAddress);
this.state = State.RUNNING;
setFenced(false);
LOGGER.info("All register process has been done, change state to: {}", this.state);
} else {
return false;
Expand All @@ -225,6 +236,12 @@ public void shutdown() {
public synchronized void changeBrokerRole(final Long newMasterBrokerId, final String newMasterAddress,
final Integer newMasterEpoch,
final Integer syncStateSetEpoch, final Set<Long> syncStateSet) throws Exception {
// Startup uses the election response; periodic metadata sync reconciles any
// notification that crossed startup before this broker was ready.
if (this.state != State.RUNNING || !this.startupRegistered || newMasterEpoch < this.startupMasterEpoch) {
LOGGER.info("Ignore role notification before startup completes or below its epoch: {}", newMasterEpoch);
return;
}
if (newMasterBrokerId != null && newMasterEpoch > this.masterEpoch) {
if (newMasterBrokerId.equals(this.brokerControllerId)) {
changeToMaster(newMasterEpoch, syncStateSetEpoch, syncStateSet);
Expand All @@ -236,15 +253,28 @@ public synchronized void changeBrokerRole(final Long newMasterBrokerId, final St

public void changeToMaster(final int newMasterEpoch, final int syncStateSetEpoch, final Set<Long> syncStateSet) throws Exception {
synchronized (this) {
if (!this.startupRegistered || newMasterEpoch < this.startupMasterEpoch) {
throw new IllegalStateException("Master grant does not belong to the registered broker startup");
}
if (!this.becameMasterThisStartup && newMasterEpoch <= this.recoveredEpoch) {
throw new IllegalStateException("Recovered store requires a master epoch newer than " + this.recoveredEpoch);
}
if (newMasterEpoch > this.masterEpoch) {
LOGGER.info("Begin to change to master, brokerName:{}, replicas:{}, new Epoch:{}", this.brokerConfig.getBrokerName(), this.brokerAddress, newMasterEpoch);
this.masterEpoch = newMasterEpoch;
this.roleReady = false;
setFenced(true);
if (this.masterBrokerId != null && this.masterBrokerId.equals(this.brokerControllerId) && this.brokerController.getBrokerConfig().getBrokerId() == MixAll.MASTER_ID) {
// Change SyncStateSet
final HashSet<Long> newSyncStateSet = new HashSet<>(syncStateSet);
changeSyncStateSet(newSyncStateSet, syncStateSetEpoch);
// if master doesn't change
this.haService.changeToMasterWhenLastRoleIsMaster(newMasterEpoch);
if (!this.haService.changeToMasterWhenLastRoleIsMaster(newMasterEpoch)) {
throw new IllegalStateException("Failed to persist master epoch " + newMasterEpoch);
}
this.masterEpoch = newMasterEpoch;
this.becameMasterThisStartup = true;
this.roleReady = true;
setFenced(false);
this.brokerController.getTopicConfigManager().getDataVersion().nextVersion(newMasterEpoch);
this.executorService.submit(this::checkSyncStateSetAndDoReport);
registerBrokerWhenRoleChange();
Expand All @@ -255,11 +285,12 @@ public void changeToMaster(final int newMasterEpoch, final int syncStateSetEpoch
final HashSet<Long> newSyncStateSet = new HashSet<>(syncStateSet);
changeSyncStateSet(newSyncStateSet, syncStateSetEpoch);

// Handle the slave synchronise
handleSlaveSynchronize(BrokerRole.SYNC_MASTER);

// Notify ha service, change to master
this.haService.changeToMaster(newMasterEpoch);
if (!this.haService.changeToMaster(newMasterEpoch)) {
throw new IllegalStateException("Failed to change HA role to master at epoch " + newMasterEpoch);
}

handleSlaveSynchronize(BrokerRole.SYNC_MASTER);

this.brokerController.getBrokerConfig().setBrokerId(MixAll.MASTER_ID);
this.brokerController.getMessageStoreConfig().setBrokerRole(BrokerRole.SYNC_MASTER);
Expand All @@ -268,6 +299,10 @@ public void changeToMaster(final int newMasterEpoch, final int syncStateSetEpoch
// Change record
this.masterAddress = this.brokerAddress;
this.masterBrokerId = this.brokerControllerId;
this.masterEpoch = newMasterEpoch;
this.becameMasterThisStartup = true;
this.roleReady = true;
setFenced(false);

schedulingCheckSyncStateSet();

Expand All @@ -280,14 +315,26 @@ public void changeToMaster(final int newMasterEpoch, final int syncStateSetEpoch

public void changeToSlave(final String newMasterAddress, final int newMasterEpoch, Long newMasterBrokerId) {
synchronized (this) {
if (!this.startupRegistered || newMasterEpoch < this.startupMasterEpoch) {
LOGGER.warn("Ignore slave role before startup registration or below its epoch: {}", newMasterEpoch);
return;
}
if (newMasterEpoch > this.masterEpoch) {
LOGGER.info("Begin to change to slave, brokerName={}, brokerId={}, newMasterBrokerId={}, newMasterAddress={}, newMasterEpoch={}",
this.brokerConfig.getBrokerName(), this.brokerControllerId, newMasterBrokerId, newMasterAddress, newMasterEpoch);

this.masterEpoch = newMasterEpoch;
this.roleReady = false;
setFenced(true);
if (newMasterBrokerId.equals(this.masterBrokerId)) {
// if master doesn't change
this.haService.changeToSlaveWhenMasterNotChange(newMasterAddress, newMasterEpoch);
if (!this.haService.changeToSlaveWhenMasterNotChange(newMasterAddress, newMasterEpoch)) {
return;
}
this.masterAddress = newMasterAddress;
this.brokerController.getSlaveSynchronize().setMasterAddr(newMasterAddress);
this.masterEpoch = newMasterEpoch;
this.roleReady = true;
setFenced(false);
this.brokerController.getTopicConfigManager().getDataVersion().nextVersion(newMasterEpoch);
registerBrokerWhenRoleChange();
return;
Expand All @@ -302,15 +349,17 @@ public void changeToSlave(final String newMasterAddress, final int newMasterEpoc
// The brokerId in brokerConfig just means its role(master[0] or slave[>=1])
this.brokerConfig.setBrokerId(brokerControllerId);

// Change record
// Notify ha service, change to slave
if (!this.haService.changeToSlave(newMasterAddress, newMasterEpoch, brokerControllerId)) {
return;
}

this.masterAddress = newMasterAddress;
this.masterBrokerId = newMasterBrokerId;

// Handle the slave synchronise
this.masterEpoch = newMasterEpoch;
handleSlaveSynchronize(BrokerRole.SLAVE);

// Notify ha service, change to slave
this.haService.changeToSlave(newMasterAddress, newMasterEpoch, brokerControllerId);
this.roleReady = true;
setFenced(false);

this.brokerController.getTopicConfigManager().getDataVersion().nextVersion(newMasterEpoch);
registerBrokerWhenRoleChange();
Expand Down Expand Up @@ -391,7 +440,8 @@ private boolean brokerElect() {
} else {
changeToSlave(masterAddress, tryElectResponse.getMasterEpoch(), tryElectResponse.getMasterBrokerId());
}
return true;
return this.roleReady && Objects.equals(this.masterBrokerId, masterBrokerId)
&& this.masterEpoch == tryElectResponse.getMasterEpoch();
} catch (Exception e) {
LOGGER.error("Failed to try elect", e);
return false;
Expand Down Expand Up @@ -558,21 +608,30 @@ private boolean createMetadataFileAndDeleteTemp() {
*/
private boolean registerBrokerToController() {
try {
Pair<RegisterBrokerToControllerResponseHeader, Set<Long>> responsePair = this.brokerOuterAPI.registerBrokerToController(brokerConfig.getBrokerClusterName(), brokerConfig.getBrokerName(), brokerControllerId, brokerAddress, controllerLeaderAddress);
if (this.brokerStartupGeneration == null) {
this.brokerStartupGeneration = BrokerStartupMetadata.nextGeneration(Paths.get(
this.brokerController.getMessageStoreConfig().getStorePathBrokerIdentity() + "-startup"));
}
Pair<RegisterBrokerToControllerResponseHeader, Set<Long>> responsePair = this.brokerOuterAPI.registerBrokerToController(
brokerConfig.getBrokerClusterName(), brokerConfig.getBrokerName(), brokerControllerId, brokerAddress,
controllerLeaderAddress, this.brokerStartupGeneration, this.brokerStartupId);
if (responsePair == null)
return false;
RegisterBrokerToControllerResponseHeader response = responsePair.getObject1();
Set<Long> syncStateSet = responsePair.getObject2();
final Long masterBrokerId = response.getMasterBrokerId();
final String masterAddress = response.getMasterAddress();
if (masterBrokerId == null) {
return true;
}
if (this.brokerControllerId.equals(masterBrokerId)) {
changeToMaster(response.getMasterEpoch(), response.getSyncStateSetEpoch(), syncStateSet);
} else {
changeToSlave(masterAddress, response.getMasterEpoch(), masterBrokerId);
synchronized (this) {
if (!Objects.equals(this.brokerStartupGeneration, response.getBrokerStartupGeneration())
|| !this.brokerStartupId.equals(response.getBrokerStartupId()) || response.getMasterEpoch() == null) {
LOGGER.error("Controller did not acknowledge this broker startup; upgrade the controller before starting this broker");
this.startupRegistered = false;
this.roleReady = false;
setFenced(true);
return false;
}
this.startupMasterEpoch = Math.max(this.startupMasterEpoch, response.getMasterEpoch());
this.startupRegistered = true;
}
// The reply acknowledges this process and sets the minimum epoch. It is not
// authorization to publish a role; startup needs the controller's election response.
return true;
} catch (Exception e) {
LOGGER.error("fail to send registerBrokerToController request to controller", e);
Expand Down Expand Up @@ -673,7 +732,10 @@ private void schedulingSyncBrokerMetadata() {
LOGGER.warn("Error happen when get broker {}'s metadata", this.brokerConfig.getBrokerName(), exception);
if (exception.getResponseCode() == CONTROLLER_BROKER_METADATA_NOT_EXIST) {
try {
registerBrokerToController();
if (registerBrokerToController()) {
sendHeartbeatToController();
brokerElect();
}
TimeUnit.SECONDS.sleep(2);
} catch (InterruptedException ignore) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1435,7 +1435,16 @@ public ApplyBrokerIdResponseHeader applyBrokerId(final String clusterName, final
public Pair<RegisterBrokerToControllerResponseHeader, Set<Long>> registerBrokerToController(
final String clusterName, final String brokerName, final Long brokerId, final String brokerAddress,
final String controllerAddress) throws Exception {
return registerBrokerToController(clusterName, brokerName, brokerId, brokerAddress, controllerAddress,
null, null);
}

public Pair<RegisterBrokerToControllerResponseHeader, Set<Long>> registerBrokerToController(
final String clusterName, final String brokerName, final Long brokerId, final String brokerAddress,
final String controllerAddress, final Long startupGeneration, final String startupId) throws Exception {
final RegisterBrokerToControllerRequestHeader requestHeader = new RegisterBrokerToControllerRequestHeader(clusterName, brokerName, brokerId, brokerAddress);
requestHeader.setBrokerStartupGeneration(startupGeneration);
requestHeader.setBrokerStartupId(startupId);
final RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.CONTROLLER_REGISTER_BROKER, requestHeader);
final RemotingCommand response = this.remotingClient.invokeSync(controllerAddress, request, 3000);
assert response != null;
Expand Down
Loading