pulsar-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] sijie commented on a change in pull request #3310: Add a running flag for Kafka source connector
Date Tue, 08 Jan 2019 15:13:17 GMT
sijie commented on a change in pull request #3310: Add a running flag for Kafka source connector
URL: https://github.com/apache/pulsar/pull/3310#discussion_r246031948
 
 

 ##########
 File path: pulsar-io/kafka/src/main/java/org/apache/pulsar/io/kafka/KafkaAbstractSource.java
 ##########
 @@ -106,32 +107,42 @@ public void close() throws InterruptedException {
 
     public void start() {
         runnerThread = new Thread(() -> {
-            LOG.info("Starting kafka source");
-            consumer = new KafkaConsumer<>(beforeCreateConsumer(props));
-            consumer.subscribe(Arrays.asList(kafkaSourceConfig.getTopic()));
-            LOG.info("Kafka source started.");
-            ConsumerRecords<String, byte[]> consumerRecords;
-            while(true){
-                consumerRecords = consumer.poll(1000);
-                CompletableFuture<?>[] futures = new CompletableFuture<?>[consumerRecords.count()];
-                int index = 0;
-                for (ConsumerRecord<String, byte[]> consumerRecord : consumerRecords)
{
-                    LOG.debug("Record received from kafka, key: {}. value: {}", consumerRecord.key(),
consumerRecord.value());
-                    KafkaRecord<V> record = new KafkaRecord<>(consumerRecord,
extractValue(consumerRecord));
-                    consume(record);
-                    futures[index] = record.getCompletableFuture();
-                    index++;
-                }
-                if (!kafkaSourceConfig.isAutoCommitEnabled()) {
-                    try {
-                        CompletableFuture.allOf(futures).get();
-                        consumer.commitSync();
-                    } catch (ExecutionException | InterruptedException ex) {
-                        break;
+            try {
+                consumer = new KafkaConsumer<>(beforeCreateConsumer(props));
+            } catch (Exception ex) {
+                LOG.error("Unable to instantiate Kafka consumer", ex);
+                return;
+            }
+            while (running) {
+                LOG.info("Starting kafka source");
+                try {
+                    consumer.subscribe(Collections.singletonList(kafkaSourceConfig.getTopic()));
+                    LOG.info("Kafka source started.");
+                    ConsumerRecords<String, byte[]> consumerRecords;
+                    while (running) {
+                        consumerRecords = consumer.poll(1000);
+                        CompletableFuture<?>[] futures = new CompletableFuture<?>[consumerRecords.count()];
+                        int index = 0;
+                        for (ConsumerRecord<String, byte[]> consumerRecord : consumerRecords)
{
+                            LOG.debug("Record received from kafka, key: {}. value: {}", consumerRecord.key(),
consumerRecord.value());
+                            KafkaRecord<V> record = new KafkaRecord<>(consumerRecord,
extractValue(consumerRecord));
+                            consume(record);
+                            futures[index] = record.getCompletableFuture();
+                            index++;
+                        }
+                        if (!kafkaSourceConfig.isAutoCommitEnabled()) {
+                            try {
+                                CompletableFuture.allOf(futures).get();
+                                consumer.commitSync();
+                            } catch (InterruptedException ex) {
+                                break;
+                            }
+                        }
                     }
+                } catch (Exception ex) {
+                    LOG.error("Error while consuming data from Kafka", ex);
 
 Review comment:
   I am not a Kafka expert, so I am not sure how Kafka handles that. If Kafka client is able
to handle that successfully, that's fine for me. However don't need to close the consumer
when exceptions are caught, because in the next retry loop, a new consumer will be created,
no?
   
   another question, how can we ensure your fix work as expected? e.g. can we add any unit
tests or integration tests?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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


With regards,
Apache Git Services

Mime
View raw message