You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2021/01/14 04:15:37 UTC

[GitHub] [kafka] ijuma commented on a change in pull request #9884: MINOR: remove unused flag 'hasIdempotentRecords'

ijuma commented on a change in pull request #9884:
URL: https://github.com/apache/kafka/pull/9884#discussion_r557033490



##########
File path: clients/src/main/java/org/apache/kafka/common/requests/RequestUtils.java
##########
@@ -38,41 +37,23 @@ private RequestUtils() {}
             Optional.empty() : Optional.of(leaderEpoch);
     }
 
-    // visible for testing
-    public static boolean hasIdempotentRecords(ProduceRequest request) {
-        return flags(request).getKey();
-    }
-
-    // visible for testing
     public static boolean hasTransactionalRecords(ProduceRequest request) {
-        return flags(request).getValue();
+        return flag(request, RecordBatch::isTransactional);
     }
 
     /**
-     * Get both hasIdempotentRecords flag and hasTransactionalRecords flag from produce request.
-     * Noted that we find all flags at once to avoid duplicate loop and record batch construction.
-     * @return first flag is "hasIdempotentRecords" and another is "hasTransactionalRecords"
+     * find a flag from all records of a produce request.
+     * @param request produce request
+     * @param predicate used to predicate the record
+     * @return true if there is any matched flag in the produce request. Otherwise, false
      */
-    public static AbstractMap.SimpleEntry<Boolean, Boolean> flags(ProduceRequest request) {
-        boolean hasIdempotentRecords = false;
-        boolean hasTransactionalRecords = false;
-        for (ProduceRequestData.TopicProduceData tpd : request.data().topicData()) {
-            for (ProduceRequestData.PartitionProduceData ppd : tpd.partitionData()) {
-                BaseRecords records = ppd.records();
-                if (records instanceof Records) {
-                    Iterator<? extends RecordBatch> iterator = ((Records) records).batches().iterator();
-                    if (iterator.hasNext()) {
-                        RecordBatch batch = iterator.next();
-                        hasIdempotentRecords = hasIdempotentRecords || batch.hasProducerId();
-                        hasTransactionalRecords = hasTransactionalRecords || batch.isTransactional();
-                    }
-                }
-                // return early
-                if (hasIdempotentRecords && hasTransactionalRecords)
-                    return new AbstractMap.SimpleEntry<>(true, true);
-            }
-        }
-        return new AbstractMap.SimpleEntry<>(hasIdempotentRecords, hasTransactionalRecords);
+    static boolean flag(ProduceRequest request, Predicate<RecordBatch> predicate) {
+        return request.data().topicData().stream().flatMap(tpd -> tpd.partitionData().stream())
+                .map(ProduceRequestData.PartitionProduceData::records)
+                .filter(records -> records instanceof Records)
+                .map(r -> ((Records) r).batches().iterator())
+                .filter(Iterator::hasNext)
+                .anyMatch(iter -> predicate.test(iter.next()));

Review comment:
       Have we checked if this is not much slower than the for loop? Since this is in the produce path, we should be careful about unnecessary overhead.




----------------------------------------------------------------
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.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org