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/07 05:32:59 UTC

[GitHub] [kafka] vvcephei opened a new pull request #9840: KAFKA-10867: Improved task idling

vvcephei opened a new pull request #9840:
URL: https://github.com/apache/kafka/pull/9840


   Use the new ConsumerRecords.metadata() API to implement
   improved task idling as described in KIP-695
   
   ### Committer Checklist (excluded from commit message)
   - [ ] Verify design and implementation 
   - [ ] Verify test coverage and CI build status
   - [ ] Verify documentation (including upgrade notes)
   


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



[GitHub] [kafka] guozhangwang commented on a change in pull request #9840: KAFKA-10867: Improved task idling

Posted by GitBox <gi...@apache.org>.
guozhangwang commented on a change in pull request #9840:
URL: https://github.com/apache/kafka/pull/9840#discussion_r563952224



##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/PartitionGroup.java
##########
@@ -123,9 +123,9 @@ public boolean readyToProcess(final long wallClockTime) {
             }
         }
         // Log-level strategy:
-        //  TRACE for messages that don't wait for fetches, since these may be logged at extremely high frequency
-        //  DEBUG when we waited for a fetch and decided to wait some more, as configured
-        //  DEBUG when we are ready for processing and didn't have to enforce processing
+        //  TRACE for messages that don't wait for fetches

Review comment:
       I'm +1 on demoting these log entries! :)




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



[GitHub] [kafka] vvcephei commented on a change in pull request #9840: KAFKA-10867: Improved task idling

Posted by GitBox <gi...@apache.org>.
vvcephei commented on a change in pull request #9840:
URL: https://github.com/apache/kafka/pull/9840#discussion_r562089223



##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/PartitionGroup.java
##########
@@ -52,15 +58,20 @@
  * (i.e., it increases or stays the same over time).
  */
 public class PartitionGroup {
+    private static final Logger LOG = LoggerFactory.getLogger(PartitionGroup.class);

Review comment:
       I can pass in the log context. I wouldn't pass the actual logger, though, because it would mess up common log4j usage patterns.

##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/PartitionGroup.java
##########
@@ -78,15 +89,149 @@ RecordQueue queue() {
         }
     }
 
-    PartitionGroup(final Map<TopicPartition, RecordQueue> partitionQueues, final Sensor recordLatenessSensor) {
+    PartitionGroup(final TaskId id,
+                   final Map<TopicPartition, RecordQueue> partitionQueues,
+                   final Sensor recordLatenessSensor,
+                   final Sensor enforcedProcessingSensor,
+                   final long maxTaskIdleMs) {
+        this.id = id;
         nonEmptyQueuesByTime = new PriorityQueue<>(partitionQueues.size(), Comparator.comparingLong(RecordQueue::headRecordTimestamp));
         this.partitionQueues = partitionQueues;
+        this.enforcedProcessingSensor = enforcedProcessingSensor;
+        this.maxTaskIdleMs = maxTaskIdleMs;
         this.recordLatenessSensor = recordLatenessSensor;
         totalBuffered = 0;
         allBuffered = false;
         streamTime = RecordQueue.UNKNOWN;
     }
 
+    public void addFetchedMetadata(final TopicPartition partition, final ConsumerRecords.Metadata metadata) {
+        final Long lag = metadata.lag();
+        if (lag != null) {
+            LOG.debug("[{}] added fetched lag {}: {}", id, partition, lag);
+            fetchedLags.put(partition, lag);
+        }
+    }
+
+    public boolean readyToProcess(final long wallClockTime) {
+        if (LOG.isTraceEnabled()) {
+            for (final Map.Entry<TopicPartition, RecordQueue> entry : partitionQueues.entrySet()) {
+                LOG.trace(
+                    "[{}] buffered/lag {}: {}/{}",
+                    id,
+                    entry.getKey(),
+                    entry.getValue().size(),
+                    fetchedLags.get(entry.getKey())
+                );
+            }
+        }
+        // Log-level strategy:
+        //  TRACE for messages that don't wait for fetches, since these may be logged at extremely high frequency
+        //  DEBUG when we waited for a fetch and decided to wait some more, as configured
+        //  DEBUG when we are ready for processing and didn't have to enforce processing
+        //  INFO  when we enforce processing, since this has to wait for fetches AND may result in disorder
+
+        if (maxTaskIdleMs == StreamsConfig.MAX_TASK_IDLE_MS_DISABLED) {
+            if (LOG.isTraceEnabled() && !allBuffered && totalBuffered > 0) {
+                final Set<TopicPartition> bufferedPartitions = new HashSet<>();
+                final Set<TopicPartition> emptyPartitions = new HashSet<>();
+                for (final Map.Entry<TopicPartition, RecordQueue> entry : partitionQueues.entrySet()) {
+                    if (entry.getValue().isEmpty()) {
+                        emptyPartitions.add(entry.getKey());
+                    } else {
+                        bufferedPartitions.add(entry.getKey());
+                    }
+                }
+                LOG.trace("[{}] Ready for processing because max.task.idle.ms is disabled." +
+                              "\n\tThere may be out-of-order processing for this task as a result." +
+                              "\n\tBuffered partitions: {}" +
+                              "\n\tNon-buffered partitions: {}",
+                          id,
+                          bufferedPartitions,
+                          emptyPartitions);
+            }
+            return true;

Review comment:
       Glad we agree ;) 

##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamTask.java
##########
@@ -910,6 +900,11 @@ public void addRecords(final TopicPartition partition, final Iterable<ConsumerRe
         }
     }
 
+    @Override
+    public void addFetchedMetadata(final TopicPartition partition, final ConsumerRecords.Metadata metadata) {

Review comment:
       Wouldn't it be good to avoid taking on unrelated refactoring TODOs in this PR? It seems better to me to leave it to whoever decides to pick up that TODO and instead just keep this PR focused on the feature.

##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java
##########
@@ -510,9 +516,7 @@ public StreamThread(final Time time,
         this.nextProbingRebalanceMs = nextProbingRebalanceMs;
 
         this.pollTime = Duration.ofMillis(config.getLong(StreamsConfig.POLL_MS_CONFIG));
-        final int dummyThreadIdx = 1;
-        this.maxPollTimeMs = new InternalConsumerConfig(config.getMainConsumerConfigs("dummyGroupId", "dummyClientId", dummyThreadIdx))
-            .getInt(ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG);
+        this.maxPollTimeMs = maxPollTimeMs;

Review comment:
       Ah, this was leftover from when I was using the poll interval as a heuristic. I'll revert it.

##########
File path: streams/src/test/java/org/apache/kafka/streams/processor/internals/PartitionGroupTest.java
##########
@@ -73,28 +82,22 @@
     private final byte[] recordKey = intSerializer.serialize(null, 1);
 
     private final Metrics metrics = new Metrics();
+    private final Sensor enforcedProcessingSensor = metrics.sensor(UUID.randomUUID().toString());
     private final MetricName lastLatenessValue = new MetricName("record-lateness-last-value", "", "", mkMap());
 
-    private PartitionGroup group;
 
     private static Sensor getValueSensor(final Metrics metrics, final MetricName metricName) {
         final Sensor lastRecordedValue = metrics.sensor(metricName.name());
         lastRecordedValue.add(metricName, new Value());
         return lastRecordedValue;
     }
 
-    @Before

Review comment:
       Thanks!




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



[GitHub] [kafka] vvcephei commented on a change in pull request #9840: KAFKA-10867: Improved task idling

Posted by GitBox <gi...@apache.org>.
vvcephei commented on a change in pull request #9840:
URL: https://github.com/apache/kafka/pull/9840#discussion_r562190618



##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java
##########
@@ -510,9 +516,7 @@ public StreamThread(final Time time,
         this.nextProbingRebalanceMs = nextProbingRebalanceMs;
 
         this.pollTime = Duration.ofMillis(config.getLong(StreamsConfig.POLL_MS_CONFIG));
-        final int dummyThreadIdx = 1;
-        this.maxPollTimeMs = new InternalConsumerConfig(config.getMainConsumerConfigs("dummyGroupId", "dummyClientId", dummyThreadIdx))
-            .getInt(ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG);
+        this.maxPollTimeMs = maxPollTimeMs;

Review comment:
       Ah, this was leftover from when I was using the poll interval as a heuristic. I'll revert it.




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



[GitHub] [kafka] vvcephei commented on pull request #9840: KAFKA-10867: Improved task idling

Posted by GitBox <gi...@apache.org>.
vvcephei commented on pull request #9840:
URL: https://github.com/apache/kafka/pull/9840#issuecomment-768599882


   There was a merge conflict with trunk. Rebased and pushed.


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



[GitHub] [kafka] vvcephei commented on pull request #9840: KAFKA-10867: Improved task idling

Posted by GitBox <gi...@apache.org>.
vvcephei commented on pull request #9840:
URL: https://github.com/apache/kafka/pull/9840#issuecomment-768782409


   Hmm, the Java 8 build appears to have hung after an hour and 58 minutes. It's been running for 3 hours and 30 minutes now. This is now the 16th build, and there have been multiple Java 8 successes to date, so I think it's environmental.
   
   I'll go ahead with the merge.


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



[GitHub] [kafka] vvcephei commented on a change in pull request #9840: KAFKA-10867: Improved task idling

Posted by GitBox <gi...@apache.org>.
vvcephei commented on a change in pull request #9840:
URL: https://github.com/apache/kafka/pull/9840#discussion_r562090811



##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamTask.java
##########
@@ -910,6 +900,11 @@ public void addRecords(final TopicPartition partition, final Iterable<ConsumerRe
         }
     }
 
+    @Override
+    public void addFetchedMetadata(final TopicPartition partition, final ConsumerRecords.Metadata metadata) {

Review comment:
       Wouldn't it be good to avoid taking on unrelated refactoring TODOs in this PR? It seems better to me to leave it to whoever decides to pick up that TODO and instead just keep this PR focused on the feature.




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



[GitHub] [kafka] vvcephei commented on a change in pull request #9840: KAFKA-10867: Improved task idling

Posted by GitBox <gi...@apache.org>.
vvcephei commented on a change in pull request #9840:
URL: https://github.com/apache/kafka/pull/9840#discussion_r562089518



##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/PartitionGroup.java
##########
@@ -78,15 +89,149 @@ RecordQueue queue() {
         }
     }
 
-    PartitionGroup(final Map<TopicPartition, RecordQueue> partitionQueues, final Sensor recordLatenessSensor) {
+    PartitionGroup(final TaskId id,
+                   final Map<TopicPartition, RecordQueue> partitionQueues,
+                   final Sensor recordLatenessSensor,
+                   final Sensor enforcedProcessingSensor,
+                   final long maxTaskIdleMs) {
+        this.id = id;
         nonEmptyQueuesByTime = new PriorityQueue<>(partitionQueues.size(), Comparator.comparingLong(RecordQueue::headRecordTimestamp));
         this.partitionQueues = partitionQueues;
+        this.enforcedProcessingSensor = enforcedProcessingSensor;
+        this.maxTaskIdleMs = maxTaskIdleMs;
         this.recordLatenessSensor = recordLatenessSensor;
         totalBuffered = 0;
         allBuffered = false;
         streamTime = RecordQueue.UNKNOWN;
     }
 
+    public void addFetchedMetadata(final TopicPartition partition, final ConsumerRecords.Metadata metadata) {
+        final Long lag = metadata.lag();
+        if (lag != null) {
+            LOG.debug("[{}] added fetched lag {}: {}", id, partition, lag);
+            fetchedLags.put(partition, lag);
+        }
+    }
+
+    public boolean readyToProcess(final long wallClockTime) {
+        if (LOG.isTraceEnabled()) {
+            for (final Map.Entry<TopicPartition, RecordQueue> entry : partitionQueues.entrySet()) {
+                LOG.trace(
+                    "[{}] buffered/lag {}: {}/{}",
+                    id,
+                    entry.getKey(),
+                    entry.getValue().size(),
+                    fetchedLags.get(entry.getKey())
+                );
+            }
+        }
+        // Log-level strategy:
+        //  TRACE for messages that don't wait for fetches, since these may be logged at extremely high frequency
+        //  DEBUG when we waited for a fetch and decided to wait some more, as configured
+        //  DEBUG when we are ready for processing and didn't have to enforce processing
+        //  INFO  when we enforce processing, since this has to wait for fetches AND may result in disorder
+
+        if (maxTaskIdleMs == StreamsConfig.MAX_TASK_IDLE_MS_DISABLED) {
+            if (LOG.isTraceEnabled() && !allBuffered && totalBuffered > 0) {
+                final Set<TopicPartition> bufferedPartitions = new HashSet<>();
+                final Set<TopicPartition> emptyPartitions = new HashSet<>();
+                for (final Map.Entry<TopicPartition, RecordQueue> entry : partitionQueues.entrySet()) {
+                    if (entry.getValue().isEmpty()) {
+                        emptyPartitions.add(entry.getKey());
+                    } else {
+                        bufferedPartitions.add(entry.getKey());
+                    }
+                }
+                LOG.trace("[{}] Ready for processing because max.task.idle.ms is disabled." +
+                              "\n\tThere may be out-of-order processing for this task as a result." +
+                              "\n\tBuffered partitions: {}" +
+                              "\n\tNon-buffered partitions: {}",
+                          id,
+                          bufferedPartitions,
+                          emptyPartitions);
+            }
+            return true;

Review comment:
       Glad we agree ;) 




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



[GitHub] [kafka] vvcephei commented on a change in pull request #9840: KAFKA-10867: Improved task idling

Posted by GitBox <gi...@apache.org>.
vvcephei commented on a change in pull request #9840:
URL: https://github.com/apache/kafka/pull/9840#discussion_r562192140



##########
File path: streams/src/test/java/org/apache/kafka/streams/processor/internals/PartitionGroupTest.java
##########
@@ -73,28 +82,22 @@
     private final byte[] recordKey = intSerializer.serialize(null, 1);
 
     private final Metrics metrics = new Metrics();
+    private final Sensor enforcedProcessingSensor = metrics.sensor(UUID.randomUUID().toString());
     private final MetricName lastLatenessValue = new MetricName("record-lateness-last-value", "", "", mkMap());
 
-    private PartitionGroup group;
 
     private static Sensor getValueSensor(final Metrics metrics, final MetricName metricName) {
         final Sensor lastRecordedValue = metrics.sensor(metricName.name());
         lastRecordedValue.add(metricName, new Value());
         return lastRecordedValue;
     }
 
-    @Before

Review comment:
       Thanks!




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



[GitHub] [kafka] guozhangwang commented on a change in pull request #9840: KAFKA-10867: Improved task idling

Posted by GitBox <gi...@apache.org>.
guozhangwang commented on a change in pull request #9840:
URL: https://github.com/apache/kafka/pull/9840#discussion_r562313027



##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/PartitionGroup.java
##########
@@ -52,15 +58,20 @@
  * (i.e., it increases or stays the same over time).
  */
 public class PartitionGroup {
+    private static final Logger LOG = LoggerFactory.getLogger(PartitionGroup.class);

Review comment:
       Makes sense.




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



[GitHub] [kafka] guozhangwang commented on pull request #9840: KAFKA-10867: Improved task idling

Posted by GitBox <gi...@apache.org>.
guozhangwang commented on pull request #9840:
URL: https://github.com/apache/kafka/pull/9840#issuecomment-767024409


   I've just kicked another test. Please feel free to merge afterwards.


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



[GitHub] [kafka] guozhangwang commented on a change in pull request #9840: KAFKA-10867: Improved task idling

Posted by GitBox <gi...@apache.org>.
guozhangwang commented on a change in pull request #9840:
URL: https://github.com/apache/kafka/pull/9840#discussion_r562057043



##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/PartitionGroup.java
##########
@@ -78,15 +89,149 @@ RecordQueue queue() {
         }
     }
 
-    PartitionGroup(final Map<TopicPartition, RecordQueue> partitionQueues, final Sensor recordLatenessSensor) {
+    PartitionGroup(final TaskId id,
+                   final Map<TopicPartition, RecordQueue> partitionQueues,
+                   final Sensor recordLatenessSensor,
+                   final Sensor enforcedProcessingSensor,
+                   final long maxTaskIdleMs) {
+        this.id = id;
         nonEmptyQueuesByTime = new PriorityQueue<>(partitionQueues.size(), Comparator.comparingLong(RecordQueue::headRecordTimestamp));
         this.partitionQueues = partitionQueues;
+        this.enforcedProcessingSensor = enforcedProcessingSensor;
+        this.maxTaskIdleMs = maxTaskIdleMs;
         this.recordLatenessSensor = recordLatenessSensor;
         totalBuffered = 0;
         allBuffered = false;
         streamTime = RecordQueue.UNKNOWN;
     }
 
+    public void addFetchedMetadata(final TopicPartition partition, final ConsumerRecords.Metadata metadata) {
+        final Long lag = metadata.lag();
+        if (lag != null) {
+            LOG.debug("[{}] added fetched lag {}: {}", id, partition, lag);
+            fetchedLags.put(partition, lag);
+        }
+    }
+
+    public boolean readyToProcess(final long wallClockTime) {
+        if (LOG.isTraceEnabled()) {
+            for (final Map.Entry<TopicPartition, RecordQueue> entry : partitionQueues.entrySet()) {
+                LOG.trace(
+                    "[{}] buffered/lag {}: {}/{}",
+                    id,
+                    entry.getKey(),
+                    entry.getValue().size(),
+                    fetchedLags.get(entry.getKey())
+                );
+            }
+        }
+        // Log-level strategy:
+        //  TRACE for messages that don't wait for fetches, since these may be logged at extremely high frequency
+        //  DEBUG when we waited for a fetch and decided to wait some more, as configured
+        //  DEBUG when we are ready for processing and didn't have to enforce processing
+        //  INFO  when we enforce processing, since this has to wait for fetches AND may result in disorder
+
+        if (maxTaskIdleMs == StreamsConfig.MAX_TASK_IDLE_MS_DISABLED) {
+            if (LOG.isTraceEnabled() && !allBuffered && totalBuffered > 0) {
+                final Set<TopicPartition> bufferedPartitions = new HashSet<>();
+                final Set<TopicPartition> emptyPartitions = new HashSet<>();
+                for (final Map.Entry<TopicPartition, RecordQueue> entry : partitionQueues.entrySet()) {
+                    if (entry.getValue().isEmpty()) {
+                        emptyPartitions.add(entry.getKey());
+                    } else {
+                        bufferedPartitions.add(entry.getKey());
+                    }
+                }
+                LOG.trace("[{}] Ready for processing because max.task.idle.ms is disabled." +
+                              "\n\tThere may be out-of-order processing for this task as a result." +
+                              "\n\tBuffered partitions: {}" +
+                              "\n\tNon-buffered partitions: {}",
+                          id,
+                          bufferedPartitions,
+                          emptyPartitions);
+            }
+            return true;

Review comment:
       Actually on a second thought.. if users configures `-1` it means they probably do not care about enforced processing, while on the other side the INFO entry may flood the logs here. So NVM.




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



[GitHub] [kafka] vvcephei commented on a change in pull request #9840: KAFKA-10867: Improved task idling

Posted by GitBox <gi...@apache.org>.
vvcephei commented on a change in pull request #9840:
URL: https://github.com/apache/kafka/pull/9840#discussion_r562089223



##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/PartitionGroup.java
##########
@@ -52,15 +58,20 @@
  * (i.e., it increases or stays the same over time).
  */
 public class PartitionGroup {
+    private static final Logger LOG = LoggerFactory.getLogger(PartitionGroup.class);

Review comment:
       I can pass in the log context. I wouldn't pass the actual logger, though, because it would mess up common log4j usage patterns.




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



[GitHub] [kafka] guozhangwang commented on pull request #9840: KAFKA-10867: Improved task idling

Posted by GitBox <gi...@apache.org>.
guozhangwang commented on pull request #9840:
URL: https://github.com/apache/kafka/pull/9840#issuecomment-767024409


   I've just kicked another test. Please feel free to merge afterwards.


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



[GitHub] [kafka] guozhangwang commented on pull request #9840: KAFKA-10867: Improved task idling

Posted by GitBox <gi...@apache.org>.
guozhangwang commented on pull request #9840:
URL: https://github.com/apache/kafka/pull/9840#issuecomment-765055878


   LGTM! Once we have perf numbers quantifying its impact I think we can merge.


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



[GitHub] [kafka] guozhangwang commented on pull request #9840: KAFKA-10867: Improved task idling

Posted by GitBox <gi...@apache.org>.
guozhangwang commented on pull request #9840:
URL: https://github.com/apache/kafka/pull/9840#issuecomment-762621580


   Trying to understand the dependency here: should I only review this after #9836 is merged?


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



[GitHub] [kafka] guozhangwang commented on pull request #9840: KAFKA-10867: Improved task idling

Posted by GitBox <gi...@apache.org>.
guozhangwang commented on pull request #9840:
URL: https://github.com/apache/kafka/pull/9840#issuecomment-765055878


   LGTM! Once we have perf numbers quantifying its impact I think we can merge.


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



[GitHub] [kafka] vvcephei merged pull request #9840: KAFKA-10867: Improved task idling

Posted by GitBox <gi...@apache.org>.
vvcephei merged pull request #9840:
URL: https://github.com/apache/kafka/pull/9840


   


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



[GitHub] [kafka] guozhangwang commented on a change in pull request #9840: KAFKA-10867: Improved task idling

Posted by GitBox <gi...@apache.org>.
guozhangwang commented on a change in pull request #9840:
URL: https://github.com/apache/kafka/pull/9840#discussion_r561459260



##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/PartitionGroup.java
##########
@@ -78,15 +89,149 @@ RecordQueue queue() {
         }
     }
 
-    PartitionGroup(final Map<TopicPartition, RecordQueue> partitionQueues, final Sensor recordLatenessSensor) {
+    PartitionGroup(final TaskId id,
+                   final Map<TopicPartition, RecordQueue> partitionQueues,
+                   final Sensor recordLatenessSensor,
+                   final Sensor enforcedProcessingSensor,
+                   final long maxTaskIdleMs) {
+        this.id = id;
         nonEmptyQueuesByTime = new PriorityQueue<>(partitionQueues.size(), Comparator.comparingLong(RecordQueue::headRecordTimestamp));
         this.partitionQueues = partitionQueues;
+        this.enforcedProcessingSensor = enforcedProcessingSensor;
+        this.maxTaskIdleMs = maxTaskIdleMs;
         this.recordLatenessSensor = recordLatenessSensor;
         totalBuffered = 0;
         allBuffered = false;
         streamTime = RecordQueue.UNKNOWN;
     }
 
+    public void addFetchedMetadata(final TopicPartition partition, final ConsumerRecords.Metadata metadata) {
+        final Long lag = metadata.lag();
+        if (lag != null) {
+            LOG.debug("[{}] added fetched lag {}: {}", id, partition, lag);
+            fetchedLags.put(partition, lag);
+        }
+    }
+
+    public boolean readyToProcess(final long wallClockTime) {
+        if (LOG.isTraceEnabled()) {
+            for (final Map.Entry<TopicPartition, RecordQueue> entry : partitionQueues.entrySet()) {
+                LOG.trace(
+                    "[{}] buffered/lag {}: {}/{}",
+                    id,
+                    entry.getKey(),
+                    entry.getValue().size(),
+                    fetchedLags.get(entry.getKey())
+                );
+            }
+        }
+        // Log-level strategy:
+        //  TRACE for messages that don't wait for fetches, since these may be logged at extremely high frequency
+        //  DEBUG when we waited for a fetch and decided to wait some more, as configured
+        //  DEBUG when we are ready for processing and didn't have to enforce processing
+        //  INFO  when we enforce processing, since this has to wait for fetches AND may result in disorder
+
+        if (maxTaskIdleMs == StreamsConfig.MAX_TASK_IDLE_MS_DISABLED) {
+            if (LOG.isTraceEnabled() && !allBuffered && totalBuffered > 0) {
+                final Set<TopicPartition> bufferedPartitions = new HashSet<>();
+                final Set<TopicPartition> emptyPartitions = new HashSet<>();
+                for (final Map.Entry<TopicPartition, RecordQueue> entry : partitionQueues.entrySet()) {
+                    if (entry.getValue().isEmpty()) {
+                        emptyPartitions.add(entry.getKey());
+                    } else {
+                        bufferedPartitions.add(entry.getKey());
+                    }
+                }
+                LOG.trace("[{}] Ready for processing because max.task.idle.ms is disabled." +
+                              "\n\tThere may be out-of-order processing for this task as a result." +
+                              "\n\tBuffered partitions: {}" +
+                              "\n\tNon-buffered partitions: {}",
+                          id,
+                          bufferedPartitions,
+                          emptyPartitions);
+            }
+            return true;

Review comment:
       Should we log INFO if we are indeed enforcing processing? I.e. there are some empty partitions.

##########
File path: streams/src/main/java/org/apache/kafka/streams/StreamsConfig.java
##########
@@ -134,6 +134,8 @@
 @SuppressWarnings("deprecation")
 public class StreamsConfig extends AbstractConfig {
 
+    public static final long MAX_TASK_IDLE_MS_DISABLED = -1;

Review comment:
       nit: move this down below to 147?

##########
File path: streams/src/test/java/org/apache/kafka/streams/processor/internals/PartitionGroupTest.java
##########
@@ -73,28 +82,22 @@
     private final byte[] recordKey = intSerializer.serialize(null, 1);
 
     private final Metrics metrics = new Metrics();
+    private final Sensor enforcedProcessingSensor = metrics.sensor(UUID.randomUUID().toString());
     private final MetricName lastLatenessValue = new MetricName("record-lateness-last-value", "", "", mkMap());
 
-    private PartitionGroup group;
 
     private static Sensor getValueSensor(final Metrics metrics, final MetricName metricName) {
         final Sensor lastRecordedValue = metrics.sensor(metricName.name());
         lastRecordedValue.add(metricName, new Value());
         return lastRecordedValue;
     }
 
-    @Before

Review comment:
       Good refactoring!

##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/PartitionGroup.java
##########
@@ -52,15 +58,20 @@
  * (i.e., it increases or stays the same over time).
  */
 public class PartitionGroup {
+    private static final Logger LOG = LoggerFactory.getLogger(PartitionGroup.class);

Review comment:
       Is it more convienent to pass in the `log` object from AbstractTask to the PartitionGroup constructor? It is created with the logContext including the task-type / task-id.

##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java
##########
@@ -510,9 +516,7 @@ public StreamThread(final Time time,
         this.nextProbingRebalanceMs = nextProbingRebalanceMs;
 
         this.pollTime = Duration.ofMillis(config.getLong(StreamsConfig.POLL_MS_CONFIG));
-        final int dummyThreadIdx = 1;
-        this.maxPollTimeMs = new InternalConsumerConfig(config.getMainConsumerConfigs("dummyGroupId", "dummyClientId", dummyThreadIdx))
-            .getInt(ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG);
+        this.maxPollTimeMs = maxPollTimeMs;

Review comment:
       What's the rationale of this refactoring?

##########
File path: streams/src/main/java/org/apache/kafka/streams/StreamsConfig.java
##########
@@ -134,6 +134,8 @@
 @SuppressWarnings("deprecation")
 public class StreamsConfig extends AbstractConfig {
 
+    public static final long MAX_TASK_IDLE_MS_DISABLED = -1;

Review comment:
       Also nit, the line below should be
   
   ```
   private static final Logger
   ```

##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamTask.java
##########
@@ -910,6 +900,11 @@ public void addRecords(final TopicPartition partition, final Iterable<ConsumerRe
         }
     }
 
+    @Override
+    public void addFetchedMetadata(final TopicPartition partition, final ConsumerRecords.Metadata metadata) {

Review comment:
       The only reason that we need to add this function at `Task` seems to be `tasks.activeTasksForInputPartition(partition)` at `TaskManager`. and there's a TODO to convert its return to `StreamTask` anyways. So let's just move this function to `StreamTask` only and in `TaskManager` force convert the `task` to `StreamTask`. And then we can remove it from `StandbyTask`.




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



[GitHub] [kafka] guozhangwang commented on a change in pull request #9840: KAFKA-10867: Improved task idling

Posted by GitBox <gi...@apache.org>.
guozhangwang commented on a change in pull request #9840:
URL: https://github.com/apache/kafka/pull/9840#discussion_r563952224



##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/PartitionGroup.java
##########
@@ -123,9 +123,9 @@ public boolean readyToProcess(final long wallClockTime) {
             }
         }
         // Log-level strategy:
-        //  TRACE for messages that don't wait for fetches, since these may be logged at extremely high frequency
-        //  DEBUG when we waited for a fetch and decided to wait some more, as configured
-        //  DEBUG when we are ready for processing and didn't have to enforce processing
+        //  TRACE for messages that don't wait for fetches

Review comment:
       I'm +1 on demoting these log entries! :)




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



[GitHub] [kafka] guozhangwang commented on a change in pull request #9840: KAFKA-10867: Improved task idling

Posted by GitBox <gi...@apache.org>.
guozhangwang commented on a change in pull request #9840:
URL: https://github.com/apache/kafka/pull/9840#discussion_r561459260



##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/PartitionGroup.java
##########
@@ -78,15 +89,149 @@ RecordQueue queue() {
         }
     }
 
-    PartitionGroup(final Map<TopicPartition, RecordQueue> partitionQueues, final Sensor recordLatenessSensor) {
+    PartitionGroup(final TaskId id,
+                   final Map<TopicPartition, RecordQueue> partitionQueues,
+                   final Sensor recordLatenessSensor,
+                   final Sensor enforcedProcessingSensor,
+                   final long maxTaskIdleMs) {
+        this.id = id;
         nonEmptyQueuesByTime = new PriorityQueue<>(partitionQueues.size(), Comparator.comparingLong(RecordQueue::headRecordTimestamp));
         this.partitionQueues = partitionQueues;
+        this.enforcedProcessingSensor = enforcedProcessingSensor;
+        this.maxTaskIdleMs = maxTaskIdleMs;
         this.recordLatenessSensor = recordLatenessSensor;
         totalBuffered = 0;
         allBuffered = false;
         streamTime = RecordQueue.UNKNOWN;
     }
 
+    public void addFetchedMetadata(final TopicPartition partition, final ConsumerRecords.Metadata metadata) {
+        final Long lag = metadata.lag();
+        if (lag != null) {
+            LOG.debug("[{}] added fetched lag {}: {}", id, partition, lag);
+            fetchedLags.put(partition, lag);
+        }
+    }
+
+    public boolean readyToProcess(final long wallClockTime) {
+        if (LOG.isTraceEnabled()) {
+            for (final Map.Entry<TopicPartition, RecordQueue> entry : partitionQueues.entrySet()) {
+                LOG.trace(
+                    "[{}] buffered/lag {}: {}/{}",
+                    id,
+                    entry.getKey(),
+                    entry.getValue().size(),
+                    fetchedLags.get(entry.getKey())
+                );
+            }
+        }
+        // Log-level strategy:
+        //  TRACE for messages that don't wait for fetches, since these may be logged at extremely high frequency
+        //  DEBUG when we waited for a fetch and decided to wait some more, as configured
+        //  DEBUG when we are ready for processing and didn't have to enforce processing
+        //  INFO  when we enforce processing, since this has to wait for fetches AND may result in disorder
+
+        if (maxTaskIdleMs == StreamsConfig.MAX_TASK_IDLE_MS_DISABLED) {
+            if (LOG.isTraceEnabled() && !allBuffered && totalBuffered > 0) {
+                final Set<TopicPartition> bufferedPartitions = new HashSet<>();
+                final Set<TopicPartition> emptyPartitions = new HashSet<>();
+                for (final Map.Entry<TopicPartition, RecordQueue> entry : partitionQueues.entrySet()) {
+                    if (entry.getValue().isEmpty()) {
+                        emptyPartitions.add(entry.getKey());
+                    } else {
+                        bufferedPartitions.add(entry.getKey());
+                    }
+                }
+                LOG.trace("[{}] Ready for processing because max.task.idle.ms is disabled." +
+                              "\n\tThere may be out-of-order processing for this task as a result." +
+                              "\n\tBuffered partitions: {}" +
+                              "\n\tNon-buffered partitions: {}",
+                          id,
+                          bufferedPartitions,
+                          emptyPartitions);
+            }
+            return true;

Review comment:
       Should we log INFO if we are indeed enforcing processing? I.e. there are some empty partitions.

##########
File path: streams/src/main/java/org/apache/kafka/streams/StreamsConfig.java
##########
@@ -134,6 +134,8 @@
 @SuppressWarnings("deprecation")
 public class StreamsConfig extends AbstractConfig {
 
+    public static final long MAX_TASK_IDLE_MS_DISABLED = -1;

Review comment:
       nit: move this down below to 147?

##########
File path: streams/src/test/java/org/apache/kafka/streams/processor/internals/PartitionGroupTest.java
##########
@@ -73,28 +82,22 @@
     private final byte[] recordKey = intSerializer.serialize(null, 1);
 
     private final Metrics metrics = new Metrics();
+    private final Sensor enforcedProcessingSensor = metrics.sensor(UUID.randomUUID().toString());
     private final MetricName lastLatenessValue = new MetricName("record-lateness-last-value", "", "", mkMap());
 
-    private PartitionGroup group;
 
     private static Sensor getValueSensor(final Metrics metrics, final MetricName metricName) {
         final Sensor lastRecordedValue = metrics.sensor(metricName.name());
         lastRecordedValue.add(metricName, new Value());
         return lastRecordedValue;
     }
 
-    @Before

Review comment:
       Good refactoring!

##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/PartitionGroup.java
##########
@@ -52,15 +58,20 @@
  * (i.e., it increases or stays the same over time).
  */
 public class PartitionGroup {
+    private static final Logger LOG = LoggerFactory.getLogger(PartitionGroup.class);

Review comment:
       Is it more convienent to pass in the `log` object from AbstractTask to the PartitionGroup constructor? It is created with the logContext including the task-type / task-id.

##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamThread.java
##########
@@ -510,9 +516,7 @@ public StreamThread(final Time time,
         this.nextProbingRebalanceMs = nextProbingRebalanceMs;
 
         this.pollTime = Duration.ofMillis(config.getLong(StreamsConfig.POLL_MS_CONFIG));
-        final int dummyThreadIdx = 1;
-        this.maxPollTimeMs = new InternalConsumerConfig(config.getMainConsumerConfigs("dummyGroupId", "dummyClientId", dummyThreadIdx))
-            .getInt(ConsumerConfig.MAX_POLL_INTERVAL_MS_CONFIG);
+        this.maxPollTimeMs = maxPollTimeMs;

Review comment:
       What's the rationale of this refactoring?

##########
File path: streams/src/main/java/org/apache/kafka/streams/StreamsConfig.java
##########
@@ -134,6 +134,8 @@
 @SuppressWarnings("deprecation")
 public class StreamsConfig extends AbstractConfig {
 
+    public static final long MAX_TASK_IDLE_MS_DISABLED = -1;

Review comment:
       Also nit, the line below should be
   
   ```
   private static final Logger
   ```

##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/StreamTask.java
##########
@@ -910,6 +900,11 @@ public void addRecords(final TopicPartition partition, final Iterable<ConsumerRe
         }
     }
 
+    @Override
+    public void addFetchedMetadata(final TopicPartition partition, final ConsumerRecords.Metadata metadata) {

Review comment:
       The only reason that we need to add this function at `Task` seems to be `tasks.activeTasksForInputPartition(partition)` at `TaskManager`. and there's a TODO to convert its return to `StreamTask` anyways. So let's just move this function to `StreamTask` only and in `TaskManager` force convert the `task` to `StreamTask`. And then we can remove it from `StandbyTask`.

##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/PartitionGroup.java
##########
@@ -78,15 +89,149 @@ RecordQueue queue() {
         }
     }
 
-    PartitionGroup(final Map<TopicPartition, RecordQueue> partitionQueues, final Sensor recordLatenessSensor) {
+    PartitionGroup(final TaskId id,
+                   final Map<TopicPartition, RecordQueue> partitionQueues,
+                   final Sensor recordLatenessSensor,
+                   final Sensor enforcedProcessingSensor,
+                   final long maxTaskIdleMs) {
+        this.id = id;
         nonEmptyQueuesByTime = new PriorityQueue<>(partitionQueues.size(), Comparator.comparingLong(RecordQueue::headRecordTimestamp));
         this.partitionQueues = partitionQueues;
+        this.enforcedProcessingSensor = enforcedProcessingSensor;
+        this.maxTaskIdleMs = maxTaskIdleMs;
         this.recordLatenessSensor = recordLatenessSensor;
         totalBuffered = 0;
         allBuffered = false;
         streamTime = RecordQueue.UNKNOWN;
     }
 
+    public void addFetchedMetadata(final TopicPartition partition, final ConsumerRecords.Metadata metadata) {
+        final Long lag = metadata.lag();
+        if (lag != null) {
+            LOG.debug("[{}] added fetched lag {}: {}", id, partition, lag);
+            fetchedLags.put(partition, lag);
+        }
+    }
+
+    public boolean readyToProcess(final long wallClockTime) {
+        if (LOG.isTraceEnabled()) {
+            for (final Map.Entry<TopicPartition, RecordQueue> entry : partitionQueues.entrySet()) {
+                LOG.trace(
+                    "[{}] buffered/lag {}: {}/{}",
+                    id,
+                    entry.getKey(),
+                    entry.getValue().size(),
+                    fetchedLags.get(entry.getKey())
+                );
+            }
+        }
+        // Log-level strategy:
+        //  TRACE for messages that don't wait for fetches, since these may be logged at extremely high frequency
+        //  DEBUG when we waited for a fetch and decided to wait some more, as configured
+        //  DEBUG when we are ready for processing and didn't have to enforce processing
+        //  INFO  when we enforce processing, since this has to wait for fetches AND may result in disorder
+
+        if (maxTaskIdleMs == StreamsConfig.MAX_TASK_IDLE_MS_DISABLED) {
+            if (LOG.isTraceEnabled() && !allBuffered && totalBuffered > 0) {
+                final Set<TopicPartition> bufferedPartitions = new HashSet<>();
+                final Set<TopicPartition> emptyPartitions = new HashSet<>();
+                for (final Map.Entry<TopicPartition, RecordQueue> entry : partitionQueues.entrySet()) {
+                    if (entry.getValue().isEmpty()) {
+                        emptyPartitions.add(entry.getKey());
+                    } else {
+                        bufferedPartitions.add(entry.getKey());
+                    }
+                }
+                LOG.trace("[{}] Ready for processing because max.task.idle.ms is disabled." +
+                              "\n\tThere may be out-of-order processing for this task as a result." +
+                              "\n\tBuffered partitions: {}" +
+                              "\n\tNon-buffered partitions: {}",
+                          id,
+                          bufferedPartitions,
+                          emptyPartitions);
+            }
+            return true;

Review comment:
       Actually on a second thought.. if users configures `-1` it means they probably do not care about enforced processing, while on the other side the INFO entry may flood the logs here. So NVM.

##########
File path: streams/src/main/java/org/apache/kafka/streams/processor/internals/PartitionGroup.java
##########
@@ -52,15 +58,20 @@
  * (i.e., it increases or stays the same over time).
  */
 public class PartitionGroup {
+    private static final Logger LOG = LoggerFactory.getLogger(PartitionGroup.class);

Review comment:
       Makes sense.




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