Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #6662] Optimize the process of HA's confirmOffset calculation #6663

Merged
merged 5 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
Expand Up @@ -600,6 +600,7 @@ private void confirmNowRegisteringState() {
if (this.brokerMetadata.isLoaded()) {
this.registerState = RegisterState.CREATE_METADATA_FILE_DONE;
this.brokerControllerId = brokerMetadata.getBrokerId();
this.haService.setBrokerControllerId(this.brokerControllerId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it is the first time to start (without brokerIdentity file), the brokerControllerId will be forgotten to be set to HAService.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you are right.
I have modify the code to set brokerControllerId when it starts without brokerIdentity file.

return;
}
// 2. check if temp metadata exist
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
Expand All @@ -50,6 +51,7 @@
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
* SwitchAble ha service, support switch role to master or slave.
Expand All @@ -72,6 +74,8 @@ public class AutoSwitchHAService extends DefaultHAService {
private EpochFileCache epochCache;
private AutoSwitchHAClient haClient;

private Long brokerControllerId = null;

public AutoSwitchHAService() {
}

Expand Down Expand Up @@ -427,14 +431,25 @@ public void updateConfirmOffset(long confirmOffset) {

private long computeConfirmOffset() {
final Set<Long> currentSyncStateSet = getSyncStateSet();
long confirmOffset = this.defaultMessageStore.getMaxPhyOffset();
long newConfirmOffset = this.defaultMessageStore.getMaxPhyOffset();
List<Long> idList = this.connectionList.stream().map(connection -> ((AutoSwitchHAConnection)connection).getSlaveId()).collect(Collectors.toList());

// To avoid the syncStateSet is not consistent with connectionList.
// Fix issue: https://github.com/apache/rocketmq/issues/6662
for (Long syncId : currentSyncStateSet) {
if (!idList.contains(syncId) && this.brokerControllerId != null && !Objects.equals(syncId, this.brokerControllerId)) {
LOGGER.warn("Slave {} is still in syncStateSet, but has lost its connection. So new offset can't be compute.", syncId);
return this.confirmOffset;
}
}

for (HAConnection connection : this.connectionList) {
final Long slaveId = ((AutoSwitchHAConnection) connection).getSlaveId();
if (currentSyncStateSet.contains(slaveId)) {
confirmOffset = Math.min(confirmOffset, connection.getSlaveAckOffset());
newConfirmOffset = Math.min(newConfirmOffset, connection.getSlaveAckOffset());
}
}
return confirmOffset;
return newConfirmOffset;
}

public void setSyncStateSet(final Set<Long> syncStateSet) {
Expand Down Expand Up @@ -545,6 +560,14 @@ public List<EpochEntry> getEpochEntries() {
return this.epochCache.getAllEntries();
}

public Long getBrokerControllerId() {
return brokerControllerId;
}

public void setBrokerControllerId(Long brokerControllerId) {
this.brokerControllerId = brokerControllerId;
}

class AutoSwitchAcceptSocketService extends AcceptSocketService {

public AutoSwitchAcceptSocketService(final MessageStoreConfig messageStoreConfig) {
Expand Down