davsclaus commented on code in PR #27495:
URL: https://github.com/apache/camel/pull/27495#discussion_r4210388229


##########
components/camel-kafka-share/src/main/java/org/apache/camel/component/kafka/share/KafkaShareFetchRecords.java:
##########
@@ -0,0 +1,438 @@
+/*
+ * 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.camel.component.kafka.share;
+
+import java.time.Duration;
+import java.util.ArrayDeque;
+import java.util.Arrays;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Optional;
+import java.util.Properties;
+import java.util.Set;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.locks.ReentrantLock;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Message;
+import org.apache.camel.component.kafka.KafkaClientConfiguration;
+import org.apache.camel.component.kafka.KafkaConsumerFatalException;
+import org.apache.camel.component.kafka.KafkaReconnectSupport;
+import org.apache.camel.component.kafka.PollOnError;
+import org.apache.camel.component.kafka.TaskHealthState;
+import org.apache.camel.component.kafka.consumer.support.KafkaRecordProcessor;
+import org.apache.camel.support.BridgeExceptionHandlerToErrorHandler;
+import org.apache.camel.support.task.BackgroundTask;
+import org.apache.camel.support.task.TaskRunFailureException;
+import org.apache.camel.util.ObjectHelper;
+import org.apache.camel.util.TimeUtils;
+import org.apache.kafka.clients.CommonClientConfigs;
+import org.apache.kafka.clients.consumer.AcknowledgeType;
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.apache.kafka.clients.consumer.ConsumerRecords;
+import org.apache.kafka.clients.consumer.ShareConsumer;
+import org.apache.kafka.common.KafkaException;
+import org.apache.kafka.common.TopicIdPartition;
+import org.apache.kafka.common.errors.AuthenticationException;
+import org.apache.kafka.common.errors.AuthorizationException;
+import org.apache.kafka.common.errors.InterruptException;
+import org.apache.kafka.common.errors.WakeupException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Polls one Kafka share consumer and routes its records. Every record of a 
poll is acknowledged on this thread, before
+ * the next poll, as the share consumer uses explicit acknowledgement.
+ */
+class KafkaShareFetchRecords implements Runnable {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(KafkaShareFetchRecords.class);
+
+    private final KafkaShareConsumer kafkaShareConsumer;
+    private final KafkaShareConfiguration configuration;
+    private final BridgeExceptionHandlerToErrorHandler bridge;
+    private final List<String> topics;
+    private final String threadId;
+    private final Properties kafkaProps;
+    private final RecordProcessor recordProcessor = new RecordProcessor();
+    // held while processing the records of a poll, so that stop() waits for 
the records in flight
+    private final ReentrantLock lock = new ReentrantLock();
+
+    private ShareConsumer<Object, Object> consumer;
+    private ScheduledExecutorService reconnectPool;
+
+    // read from other threads, such as the health check
+    private volatile String clientId;
+    private volatile boolean connected;
+    private volatile boolean terminated;
+    private volatile Exception lastError;
+    private volatile long currentBackoffInterval;
+
+    KafkaShareFetchRecords(KafkaShareConsumer kafkaShareConsumer, 
BridgeExceptionHandlerToErrorHandler bridge, String id,
+                           Properties kafkaProps) {
+        this.kafkaShareConsumer = kafkaShareConsumer;
+        this.configuration = 
kafkaShareConsumer.getEndpoint().getConfiguration();
+        this.bridge = bridge;
+        this.topics = 
Arrays.stream(configuration.getTopic().split(",")).map(String::trim).filter(t 
-> !t.isEmpty())
+                .toList();
+        this.threadId = configuration.getTopic() + "-Thread " + id;
+        this.kafkaProps = kafkaProps;
+    }
+
+    @Override
+    public void run() {
+        try {
+            while (isRunAllowed() && !terminated) {
+                if (!connected && !connect()) {
+                    // gave up creating the share consumer
+                    terminated = true;
+                    break;
+                }
+                lastError = null;
+                pollAndProcess();

Review Comment:
   When the consumer is stopped while `connect()` is still retrying, 
`createConsumer()` returns `true` without creating a consumer (`isRunAllowed()` 
is false). `connect()` then reports success and `pollAndProcess()` calls `poll` 
on a `null` `consumer`. The resulting `NullPointerException` is logged as a 
poll error and, with the default `pollOnError=ERROR_HANDLER`, handed to the 
route error handler during shutdown. (In the `RECONNECT` case it polls the 
previous consumer instead.) `KafkaFetchRecords` only polls when 
`isConnected()`, so the same guard here avoids this:
   
   ```suggestion
                   if (!connected && !connect()) {
                       // gave up creating the share consumer
                       terminated = true;
                       break;
                   }
                   if (!connected) {
                       // stopped while connecting, there is no share consumer 
to poll
                       break;
                   }
                   lastError = null;
                   pollAndProcess();
   ```



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