Skip to content

Commit eb378da

Browse files
authored
KAFKA-19462: Count fetch size when remote fetch (#20088)
Estimate the fetch size for remote fetch to avoid to exceed the `fetch.max.bytes` config. We don't want to query the remoteLogMetadata during API handling, thus we assume the remote fetch can get `max.partition.fetch.bytes` size. Tests added. Reviewers: Kamal Chandraprakash <[email protected]>
1 parent 7cb370b commit eb378da

File tree

2 files changed

+44
-23
lines changed

2 files changed

+44
-23
lines changed

core/src/main/scala/kafka/server/ReplicaManager.scala

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1852,7 +1852,11 @@ class ReplicaManager(val config: KafkaConfig,
18521852
// Once we read from a non-empty partition, we stop ignoring request and partition level size limits
18531853
if (recordBatchSize > 0)
18541854
minOneMessage = false
1855-
limitBytes = math.max(0, limitBytes - recordBatchSize)
1855+
// Because we don't know how much data will be retrieved in remote fetch yet, and we don't want to block the API call
1856+
// to query remoteLogMetadata, assume it will fetch the max bytes size of data to avoid to exceed the "fetch.max.bytes" setting.
1857+
val estimatedRecordBatchSize = if (recordBatchSize == 0 && readResult.info.delayedRemoteStorageFetch.isPresent)
1858+
readResult.info.delayedRemoteStorageFetch.get.fetchMaxBytes else recordBatchSize
1859+
limitBytes = math.max(0, limitBytes - estimatedRecordBatchSize)
18561860
result += (tp -> readResult)
18571861
}
18581862
result

core/src/test/scala/unit/kafka/server/ReplicaManagerTest.scala

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ object ReplicaManagerTest {
112112
class ReplicaManagerTest {
113113

114114
private val topic = "test-topic"
115+
private val topic2 = "test-topic2"
115116
private val topicId = Uuid.fromString("YK2ed2GaTH2JpgzUaJ8tgg")
117+
private val topicId2 = Uuid.randomUuid()
116118
private val topicIds = scala.Predef.Map("test-topic" -> topicId)
117119
private val topicNames = topicIds.map(_.swap)
118120
private val topicPartition = new TopicPartition(topic, 0)
@@ -3294,38 +3296,53 @@ class ReplicaManagerTest {
32943296
@ValueSource(booleans = Array(true, false))
32953297
def testOffsetOutOfRangeExceptionWhenReadFromLog(isFromFollower: Boolean): Unit = {
32963298
val replicaId = if (isFromFollower) 1 else -1
3299+
val fetchMaxBytes = 150
3300+
val partitionMaxBytes = 100
32973301
val tp0 = new TopicPartition(topic, 0)
3302+
val tp02 = new TopicPartition(topic2, 0)
32983303
val tidp0 = new TopicIdPartition(topicId, tp0)
3304+
val tidp02 = new TopicIdPartition(topicId2, tp02)
32993305
// create a replicaManager with remoteLog enabled
33003306
val replicaManager = setupReplicaManagerWithMockedPurgatories(new MockTimer(time), aliveBrokerIds = Seq(0, 1, 2), enableRemoteStorage = true, shouldMockLog = true, remoteFetchQuotaExceeded = Some(false))
33013307
try {
33023308
val offsetCheckpoints = new LazyOffsetCheckpoints(replicaManager.highWatermarkCheckpoints.asJava)
33033309
replicaManager.createPartition(tp0).createLogIfNotExists(isNew = false, isFutureReplica = false, offsetCheckpoints, None)
3310+
replicaManager.createPartition(tp02).createLogIfNotExists(isNew = false, isFutureReplica = false, offsetCheckpoints, None)
33043311
val partition0Replicas = Seq[Integer](0, 1).asJava
3305-
val topicIds = Map(tp0.topic -> topicId).asJava
3312+
val topicIds = Map(tp0.topic -> topicId, tp02.topic -> topicId2).asJava
33063313
val leaderEpoch = 0
33073314
val delta = createLeaderDelta(topicIds.get(topic), tp0, partition0Replicas.get(0), partition0Replicas, partition0Replicas)
3315+
val delta2 = createLeaderDelta(topicIds.get(topic2), tp02, partition0Replicas.get(0), partition0Replicas, partition0Replicas)
33083316
val leaderMetadataImage = imageFromTopics(delta.apply())
3317+
val leaderMetadataImage2 = imageFromTopics(delta2.apply())
33093318
replicaManager.applyDelta(delta, leaderMetadataImage)
3310-
3311-
val params = new FetchParams(replicaId, 1, 1000, 0, 100, FetchIsolation.LOG_END, Optional.empty)
3312-
// when reading log, it'll throw OffsetOutOfRangeException, which will be handled separately
3313-
val result = replicaManager.readFromLog(params, Seq(tidp0 -> new PartitionData(topicId, 1, 0, 100000, Optional.of[Integer](leaderEpoch), Optional.of[Integer](leaderEpoch))), UNBOUNDED_QUOTA, false)
3314-
3315-
if (isFromFollower) {
3316-
// expect OFFSET_MOVED_TO_TIERED_STORAGE error returned if it's from follower, since the data is already available in remote log
3317-
assertEquals(Errors.OFFSET_MOVED_TO_TIERED_STORAGE, result.head._2.error)
3318-
} else {
3319-
assertEquals(Errors.NONE, result.head._2.error)
3320-
}
3321-
assertEquals(startOffset, result.head._2.leaderLogStartOffset)
3322-
assertEquals(endOffset, result.head._2.leaderLogEndOffset)
3323-
assertEquals(highHW, result.head._2.highWatermark)
3324-
if (isFromFollower) {
3325-
assertFalse(result.head._2.info.delayedRemoteStorageFetch.isPresent)
3326-
} else {
3327-
// for consumer fetch, we should return a delayedRemoteStorageFetch to wait for remote fetch
3328-
assertTrue(result.head._2.info.delayedRemoteStorageFetch.isPresent)
3319+
replicaManager.applyDelta(delta2, leaderMetadataImage2)
3320+
3321+
val params = new FetchParams(replicaId, 1, 100, 0, fetchMaxBytes, FetchIsolation.LOG_END, Optional.empty)
3322+
// when reading logs from 2 partitions, they'll throw OffsetOutOfRangeException, which will be handled separately
3323+
val results = replicaManager.readFromLog(params, Seq(
3324+
tidp0 -> new PartitionData(topicId, 1, 0, partitionMaxBytes, Optional.of[Integer](leaderEpoch), Optional.of[Integer](leaderEpoch)),
3325+
tidp02 -> new PartitionData(topicId2, 1, 0, partitionMaxBytes, Optional.of[Integer](leaderEpoch), Optional.of[Integer](leaderEpoch))), UNBOUNDED_QUOTA, false)
3326+
3327+
results.foreach { case (tidp, partitionData) =>
3328+
assertEquals(startOffset, partitionData.leaderLogStartOffset)
3329+
assertEquals(endOffset, partitionData.leaderLogEndOffset)
3330+
assertEquals(highHW, partitionData.highWatermark)
3331+
if (isFromFollower) {
3332+
// expect OFFSET_MOVED_TO_TIERED_STORAGE error returned if it's from follower, since the data is already available in remote log
3333+
assertEquals(Errors.OFFSET_MOVED_TO_TIERED_STORAGE, partitionData.error)
3334+
assertFalse(partitionData.info.delayedRemoteStorageFetch.isPresent)
3335+
} else {
3336+
assertEquals(Errors.NONE, partitionData.error)
3337+
// for consumer fetch, we should return a delayedRemoteStorageFetch to wait for remote fetch
3338+
assertTrue(partitionData.info.delayedRemoteStorageFetch.isPresent)
3339+
// verify the 1st partition will set the fetchMaxBytes to partitionMaxBytes,
3340+
// and the 2nd one will set to the remaining (fetchMaxBytes - partitionMaxBytes) to meet the "fetch.max.bytes" config.
3341+
if (tidp.topic == topic)
3342+
assertEquals(partitionMaxBytes, partitionData.info.delayedRemoteStorageFetch.get().fetchMaxBytes)
3343+
else
3344+
assertEquals(fetchMaxBytes - partitionMaxBytes, partitionData.info.delayedRemoteStorageFetch.get().fetchMaxBytes)
3345+
}
33293346
}
33303347
} finally {
33313348
replicaManager.shutdown(checkpointHW = false)
@@ -3723,8 +3740,8 @@ class ReplicaManagerTest {
37233740
partitionDir.mkdir()
37243741
when(mockLog.dir).thenReturn(partitionDir)
37253742
when(mockLog.parentDir).thenReturn(path)
3726-
when(mockLog.topicId).thenReturn(Optional.of(topicId))
3727-
when(mockLog.topicPartition).thenReturn(new TopicPartition(topic, 0))
3743+
when(mockLog.topicId).thenReturn(Optional.of(topicId)).thenReturn(Optional.of(topicId2))
3744+
when(mockLog.topicPartition).thenReturn(new TopicPartition(topic, 0)).thenReturn(new TopicPartition(topic2, 0))
37283745
when(mockLog.highWatermark).thenReturn(highHW)
37293746
when(mockLog.updateHighWatermark(anyLong())).thenReturn(0L)
37303747
when(mockLog.logEndOffsetMetadata).thenReturn(new LogOffsetMetadata(10))

0 commit comments

Comments
 (0)