fvaleri commented on code in PR #24034:
URL: https://github.com/apache/kafka/pull/24034#discussion_r4526628424
##########
metadata/src/main/java/org/apache/kafka/controller/PartitionChangeBuilder.java:
##########
@@ -458,13 +460,30 @@ public Optional<ApiMessageAndVersion> build() {
record.setLeaderRecoveryState(targetLeaderRecoveryState.value());
}
+ maybeSetLeaderEpoch(record);
+
if (changeRecordIsNoOp(record)) {
return Optional.empty();
} else {
return Optional.of(new ApiMessageAndVersion(record,
metadataVersion.partitionChangeRecordVersion()));
}
}
+ /**
+ * Set the new leader epoch in the record if the metadata version supports
it.
+ *
+ * Setting the leader field signals a leader epoch bump, so this must be
called after every
+ * step that may set the leader field.
+ */
+ private void maybeSetLeaderEpoch(PartitionChangeRecord record) {
+ if (!metadataVersion.isClusterMirrorSupported()) {
+ return;
+ }
+ if (record.leader() != NO_LEADER_CHANGE) {
+ record.setLeaderEpoch(partition.leaderEpoch + 1);
+ }
+ }
Review Comment:
Here we should handle a new `targetLeaderEpoch` field , that will be set by
the `ReplicationControlManager` when handling `BumpLeaderEpochs` request.
```sh
if (targetLeaderEpoch >= 0) {
record.setLeaderEpoch(Math.max(targetLeaderEpoch,
partition.leaderEpoch));
} else if (record.leader() != NO_LEADER_CHANGE) {
record.setLeaderEpoch(partition.leaderEpoch + 1);
}
```
Max ensures the epoch never decreases. If the partition already moved past
the bump target, we keep the current epoch.
##########
metadata/src/main/java/org/apache/kafka/metadata/PartitionRegistration.java:
##########
@@ -249,14 +250,16 @@ public PartitionRegistration merge(PartitionChangeRecord
record) {
int[] newAddingReplicas = (record.addingReplicas() == null) ?
addingReplicas : Replicas.toArray(record.addingReplicas());
- int newLeader;
+ int newLeader = (record.leader() == NO_LEADER_CHANGE) ? leader :
record.leader();
int newLeaderEpoch;
- if (record.leader() == NO_LEADER_CHANGE) {
- newLeader = leader;
- newLeaderEpoch = leaderEpoch;
- } else {
- newLeader = record.leader();
+ if (record.leaderEpoch() != NO_LEADER_EPOCH_CHANGE) {
+ // Since version 3, the controller sets the new leader epoch in
the record.
+ newLeaderEpoch = record.leaderEpoch();
Review Comment:
I also think this check belongs to the controller. The builder runs in the
controller (decides what to write). The merge runs on every node replaying the
log (applies the record to get the new state).
--
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]