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
7 changes: 6 additions & 1 deletion core/src/main/scala/kafka/cluster/Partition.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1088,7 +1088,12 @@ class Partition(val topicPartition: TopicPartition,
leaderLogIfLocal.exists { leaderLog =>
val followerEndOffset = followerReplica.stateSnapshot.logEndOffset
val replicationStop = sealedReplicationStopOffset
followerEndOffset >= capAt(leaderLog.highWatermark, replicationStop) &&
// Inkless: `ReplicaManager` fences a phase 1 leader whose log ends below the committed seal, but only after the
// become-leader transition. A follower fetch in between must not expand the ISR, or the controller records a
// full ISR and a recovered leader for a partition that the leader can't serve. The check reads the stop offset
// directly because `seal()` runs after `makeLeader` on a restarted leader.
!replicationStopOffset().exists(leaderLog.logEndOffset < _) &&
followerEndOffset >= capAt(leaderLog.highWatermark, replicationStop) &&
leaderEpochStartOffsetOpt.exists(epochStart => followerEndOffset >= capAt(epochStart, replicationStop))
}
}
Expand Down
37 changes: 37 additions & 0 deletions core/src/test/scala/unit/kafka/cluster/PartitionTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,43 @@ class PartitionTest extends AbstractPartitionTest {
assertEquals(alterPartitionListener.failures.get, 0)
}

@Test
def testIsrNotExpandedWhileLeaderLogEndsBelowSeal(): Unit = {
val log = logManager.getOrCreateLog(topicPartition, topicId = topicId.toJava)
seedLogData(log, numRecords = 10, leaderEpoch = 4)

val remoteBrokerId = brokerId + 1
val replicas = Array(brokerId, remoteBrokerId)
addBrokerEpochToMockMetadataCache(metadataCache, replicas)

partition.createLogIfNotExists(isNew = false, isFutureReplica = false, offsetCheckpoints, None)
val partitionRegistration = new PartitionRegistration.Builder()
.setLeader(brokerId)
.setLeaderRecoveryState(LeaderRecoveryState.RECOVERING)
.setLeaderEpoch(5)
.setIsr(Array(brokerId))
.setPartitionEpoch(1)
.setReplicas(replicas)
.setDirectories(DirectoryId.unassignedArray(replicas.length))
.build()
assertTrue(partition.makeLeader(partitionRegistration, isNew = true, offsetCheckpoints, None))

// The committed seal is past the leader's log end offset, as after a last-known-leader election that lost records.
// The partition isn't sealed yet, as on a restarted leader between `makeLeader` and `seal`.
partition.replicationStopOffset = () => Some(log.logEndOffset + 5)
assertFalse(partition.isSealed)

fetchFollower(partition, replicaId = remoteBrokerId, fetchOffset = log.logEndOffset)
assertEquals(0, alterPartitionManager.isrUpdates.size)
assertEquals(util.Set.of(brokerId), partition.partitionState.maximalIsr)

// Once the seal sits at the leader's log end offset, the same fetch expands the ISR.
partition.replicationStopOffset = () => Some(log.logEndOffset)
fetchFollower(partition, replicaId = remoteBrokerId, fetchOffset = log.logEndOffset)
assertEquals(1, alterPartitionManager.isrUpdates.size)
assertEquals(util.Set.of(brokerId, remoteBrokerId), partition.partitionState.maximalIsr)
}

@Test
def testIsrNotExpandedIfUpdateFails(): Unit = {
val log = logManager.getOrCreateLog(topicPartition, topicId = topicId.toJava)
Expand Down
Loading