41
41
import java .util .Map ;
42
42
import java .util .Optional ;
43
43
import java .util .Set ;
44
+ import java .util .concurrent .atomic .AtomicInteger ;
44
45
import java .util .concurrent .locks .Lock ;
45
46
import java .util .function .BiConsumer ;
46
47
import java .util .stream .Collectors ;
@@ -125,7 +126,7 @@ private void completeShareFetchRequest(LinkedHashMap<TopicIdPartition, FetchRequ
125
126
try {
126
127
LinkedHashMap <TopicIdPartition , LogReadResult > responseData ;
127
128
if (partitionsAlreadyFetched .isEmpty ())
128
- responseData = readFromLog (topicPartitionData );
129
+ responseData = readFromLog (topicPartitionData , shareFetch . fetchParams (). maxBytes / topicPartitionData . size () );
129
130
else
130
131
// There shouldn't be a case when we have a partitionsAlreadyFetched value here and this variable is getting
131
132
// updated in a different tryComplete thread.
@@ -207,7 +208,6 @@ LinkedHashMap<TopicIdPartition, FetchRequest.PartitionData> acquirablePartitions
207
208
LinkedHashMap <TopicIdPartition , FetchRequest .PartitionData > topicPartitionData = new LinkedHashMap <>();
208
209
209
210
sharePartitions .forEach ((topicIdPartition , sharePartition ) -> {
210
- int partitionMaxBytes = shareFetch .partitionMaxBytes ().getOrDefault (topicIdPartition , 0 );
211
211
// Add the share partition to the list of partitions to be fetched only if we can
212
212
// acquire the fetch lock on it.
213
213
if (sharePartition .maybeAcquireFetchLock ()) {
@@ -220,7 +220,6 @@ LinkedHashMap<TopicIdPartition, FetchRequest.PartitionData> acquirablePartitions
220
220
topicIdPartition .topicId (),
221
221
sharePartition .nextFetchOffset (),
222
222
0 ,
223
- partitionMaxBytes ,
224
223
Optional .empty ()
225
224
)
226
225
);
@@ -251,7 +250,7 @@ private LinkedHashMap<TopicIdPartition, LogReadResult> maybeReadFromLog(LinkedHa
251
250
return new LinkedHashMap <>();
252
251
}
253
252
// We fetch data from replica manager corresponding to the topic partitions that have missing fetch offset metadata.
254
- return readFromLog (partitionsNotMatchingFetchOffsetMetadata );
253
+ return readFromLog (partitionsNotMatchingFetchOffsetMetadata , shareFetch . fetchParams (). maxBytes / topicPartitionData . size () );
255
254
}
256
255
257
256
private void maybeUpdateFetchOffsetMetadata (
@@ -312,7 +311,7 @@ private boolean isMinBytesSatisfied(LinkedHashMap<TopicIdPartition, FetchRequest
312
311
return true ;
313
312
} else if (fetchOffsetMetadata .onSameSegment (endOffsetMetadata )) {
314
313
// we take the partition fetch size as upper bound when accumulating the bytes.
315
- long bytesAvailable = Math .min (endOffsetMetadata .positionDiff (fetchOffsetMetadata ), partitionData . maxBytes );
314
+ long bytesAvailable = Math .min (endOffsetMetadata .positionDiff (fetchOffsetMetadata ), shareFetch . fetchParams (). maxBytes / topicPartitionData . size () );
316
315
accumulatedSize += bytesAvailable ;
317
316
}
318
317
}
@@ -335,12 +334,13 @@ else if (isolationType == FetchIsolation.HIGH_WATERMARK)
335
334
336
335
}
337
336
338
- private LinkedHashMap <TopicIdPartition , LogReadResult > readFromLog (LinkedHashMap <TopicIdPartition , FetchRequest .PartitionData > topicPartitionData ) {
337
+ private LinkedHashMap <TopicIdPartition , LogReadResult > readFromLog (LinkedHashMap <TopicIdPartition , FetchRequest .PartitionData > topicPartitionData , int partitionMaxBytes ) {
339
338
// Filter if there already exists any erroneous topic partition.
340
339
Set <TopicIdPartition > partitionsToFetch = shareFetch .filterErroneousTopicPartitions (topicPartitionData .keySet ());
341
340
if (partitionsToFetch .isEmpty ()) {
342
341
return new LinkedHashMap <>();
343
342
}
343
+ topicPartitionData .values ().forEach (partitionData -> partitionData .updateMaxBytes (partitionMaxBytes ));
344
344
345
345
Seq <Tuple2 <TopicIdPartition , LogReadResult >> responseLogResult = replicaManager .readFromLog (
346
346
shareFetch .fetchParams (),
@@ -393,15 +393,21 @@ private void handleFetchException(
393
393
LinkedHashMap <TopicIdPartition , LogReadResult > combineLogReadResponse (LinkedHashMap <TopicIdPartition , FetchRequest .PartitionData > topicPartitionData ,
394
394
LinkedHashMap <TopicIdPartition , LogReadResult > existingFetchedData ) {
395
395
LinkedHashMap <TopicIdPartition , FetchRequest .PartitionData > missingLogReadTopicPartitions = new LinkedHashMap <>();
396
+ AtomicInteger totalPartitionMaxBytesUsed = new AtomicInteger ();
396
397
topicPartitionData .forEach ((topicIdPartition , partitionData ) -> {
397
398
if (!existingFetchedData .containsKey (topicIdPartition )) {
398
399
missingLogReadTopicPartitions .put (topicIdPartition , partitionData );
400
+ } else {
401
+ totalPartitionMaxBytesUsed .addAndGet (partitionData .maxBytes );
399
402
}
400
403
});
401
404
if (missingLogReadTopicPartitions .isEmpty ()) {
402
405
return existingFetchedData ;
403
406
}
404
- LinkedHashMap <TopicIdPartition , LogReadResult > missingTopicPartitionsLogReadResponse = readFromLog (missingLogReadTopicPartitions );
407
+ LinkedHashMap <TopicIdPartition , LogReadResult > missingTopicPartitionsLogReadResponse = readFromLog (
408
+ missingLogReadTopicPartitions ,
409
+ (shareFetch .fetchParams ().maxBytes - totalPartitionMaxBytesUsed .get ()) / missingLogReadTopicPartitions .size ()
410
+ );
405
411
missingTopicPartitionsLogReadResponse .putAll (existingFetchedData );
406
412
return missingTopicPartitionsLogReadResponse ;
407
413
}
0 commit comments