sjvanrossum commented on code in PR #32986:
URL: https://github.com/apache/beam/pull/32986#discussion_r1890188295


##########
sdks/java/io/kafka/src/main/java/org/apache/beam/sdk/io/kafka/ConcurrentConsumer.java:
##########
@@ -0,0 +1,231 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.beam.sdk.io.kafka;
+
+import static 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkState;
+
+import java.time.Duration;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Phaser;
+import java.util.function.Supplier;
+import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Suppliers;
+import 
org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.util.concurrent.AtomicLongMap;
+import org.apache.kafka.clients.consumer.Consumer;
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.apache.kafka.clients.consumer.ConsumerRecords;
+import org.apache.kafka.clients.consumer.OffsetAndTimestamp;
+import org.apache.kafka.common.Metric;
+import org.apache.kafka.common.MetricName;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.errors.WakeupException;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+final class ConcurrentConsumer<K, V> implements AutoCloseable {
+  private static final Logger LOG = 
LoggerFactory.getLogger(ConcurrentConsumer.class);
+
+  private final ConsumerPhaser phaser;
+  private final Consumer<K, V> consumer;
+  private final Duration pollDuration;
+  private final AtomicLongMap<TopicPartition> times;
+  private final AtomicLongMap<TopicPartition> positions;
+  private final Supplier<Metric> recordsLagMax;
+  private final Map<TopicPartition, Supplier<Metric>> partitionRecordsLag;
+  private ConsumerRecords<K, V> pollResult;
+  private Map<TopicPartition, OffsetAndTimestamp> offsetsForTimesResult;
+
+  private final class ConsumerPhaser extends Phaser {
+    @Override
+    protected boolean onAdvance(final int phase, final int registeredParties) {
+      try {
+        final Map<TopicPartition, Long> positionsView = positions.asMap();
+        final Set<TopicPartition> prevAssignment = consumer.assignment();
+        final Set<TopicPartition> nextAssignment = positionsView.keySet();
+
+        if (!times.isEmpty()) {
+          offsetsForTimesResult = consumer.offsetsForTimes(times.asMap());
+          times.clear();
+        }
+
+        if (!prevAssignment.equals(nextAssignment)) {
+          consumer.assign(nextAssignment);
+        }
+
+        positionsView.forEach(
+            (tp, o) -> {
+              if (o == Long.MIN_VALUE) {
+                consumer.pause(Collections.singleton(tp));

Review Comment:
   `Consumer.pause(Collection<TopicPartition>)` iterates over the input 
collection, looks up the assignment in its subscription state table and then 
mutates the state. There's virtually no difference between N invocations of 
pause/resume for singleton collections or a single invocation for a collection 
of size N.
   
   I reworked the phaser interaction a bit to deregister when a non-empty list 
of records for a partition is returned by `Consumer.poll()` so this can be 
rewritten as `consumer.pause(consumerRecords.partitions())` which returns the 
set of partitions with available records. That addresses the throughput 
regression observed on larger machine types along with the rate jumping from 
~2.5 GiB/s to ~3.1 GiB/s. I'm looking into a stall at the moment, likely a 
missed arrive or deregister at the phaser or a missed cache invalidation.



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

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to