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 #6728] Compute the confirmOffset without considering new connections #6729

Merged
merged 3 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 21 additions & 1 deletion store/src/main/java/org/apache/rocketmq/store/CommitLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -553,15 +553,35 @@ private void setBatchSizeIfNeeded(Map<String, String> propertiesMap, DispatchReq
}
}

// Fetch and compute the newest confirmOffset.
// Even if it is just inited.
public long getConfirmOffset() {
if (this.defaultMessageStore.getBrokerConfig().isEnableControllerMode()) {
if (this.defaultMessageStore.getMessageStoreConfig().getBrokerRole() != BrokerRole.SLAVE && !this.defaultMessageStore.getRunningFlags().isFenced()) {
if (((AutoSwitchHAService) this.defaultMessageStore.getHaService()).getLocalSyncStateSet().size() == 1) {
return this.defaultMessageStore.getMaxPhyOffset();
}
// First time compute confirmOffset.
// First time it will compute the confirmOffset.
if (this.confirmOffset <= 0) {
setConfirmOffset(((AutoSwitchHAService) this.defaultMessageStore.getHaService()).computeConfirmOffset());
log.info("Init the confirmOffset to {}.", this.confirmOffset);
}
}
return this.confirmOffset;
} else if (this.defaultMessageStore.getMessageStoreConfig().isDuplicationEnable()) {
return this.confirmOffset;
} else {
return getMaxOffset();
}
}

// Fetch the original confirmOffset's value.
// Without checking and re-computing.
public long getConfirmOffsetDirectly() {
if (this.defaultMessageStore.getBrokerConfig().isEnableControllerMode()) {
if (this.defaultMessageStore.getMessageStoreConfig().getBrokerRole() != BrokerRole.SLAVE && !this.defaultMessageStore.getRunningFlags().isFenced()) {
if (((AutoSwitchHAService) this.defaultMessageStore.getHaService()).getLocalSyncStateSet().size() == 1) {
return this.defaultMessageStore.getMaxPhyOffset();
}
}
return this.confirmOffset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1589,11 +1589,19 @@ public boolean resetWriteOffset(long phyOffset) {
}
}

// Fetch and compute the newest confirmOffset.
// Even if it is just inited.
@Override
public long getConfirmOffset() {
return this.commitLog.getConfirmOffset();
}

// Fetch the original confirmOffset's value.
// Without checking and re-computing.
public long getConfirmOffsetDirectly() {
return this.commitLog.getConfirmOffsetDirectly();
}

@Override
public void setConfirmOffset(long phyOffset) {
this.commitLog.setConfirmOffset(phyOffset);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,14 +421,21 @@ public long computeConfirmOffset() {
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.defaultMessageStore.getConfirmOffset();
// Without check and re-compute, return the confirmOffset's value directly.
return this.defaultMessageStore.getConfirmOffsetDirectly();
}
}

for (HAConnection connection : this.connectionList) {
final Long slaveId = ((AutoSwitchHAConnection) connection).getSlaveId();
if (currentSyncStateSet.contains(slaveId)) {
newConfirmOffset = Math.min(newConfirmOffset, connection.getSlaveAckOffset());
long slaveAckOffset = connection.getSlaveAckOffset();
if (slaveAckOffset <= 0) {
// Slave's connection is just inited, the ack hasn't been calculated.
// So skip this ackOffset.
continue;
}
Copy link
Contributor

@echooymxq echooymxq May 10, 2023

Choose a reason for hiding this comment

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

if (currentSyncStateSet.contains(slaveId) && connection.getSlaveAckOffset() > 0) {
 ...
}

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, I have optimized this logic as you said.

newConfirmOffset = Math.min(newConfirmOffset, slaveAckOffset);
}
}
return newConfirmOffset;
Expand Down