You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@druid.apache.org by GitBox <gi...@apache.org> on 2018/12/21 02:38:10 UTC

[GitHub] gianm commented on a change in pull request #6431: Add Kinesis Indexing Service to core Druid

gianm commented on a change in pull request #6431: Add Kinesis Indexing Service to core Druid
URL: https://github.com/apache/incubator-druid/pull/6431#discussion_r243475321
 
 

 ##########
 File path: extensions-core/kafka-indexing-service/src/main/java/org/apache/druid/indexing/kafka/KafkaRecordSupplier.java
 ##########
 @@ -0,0 +1,214 @@
+/*
+ * 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.druid.indexing.kafka;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.google.common.collect.ImmutableList;
+import org.apache.druid.indexing.kafka.supervisor.KafkaSupervisorIOConfig;
+import org.apache.druid.indexing.seekablestream.common.OrderedPartitionableRecord;
+import org.apache.druid.indexing.seekablestream.common.RecordSupplier;
+import org.apache.druid.indexing.seekablestream.common.StreamPartition;
+import org.apache.druid.indexing.seekablestream.utils.RandomIdUtils;
+import org.apache.druid.java.util.common.ISE;
+import org.apache.druid.java.util.common.StringUtils;
+import org.apache.druid.metadata.PasswordProvider;
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.apache.kafka.clients.consumer.KafkaConsumer;
+import org.apache.kafka.common.PartitionInfo;
+import org.apache.kafka.common.TopicPartition;
+import org.apache.kafka.common.serialization.ByteArrayDeserializer;
+
+import javax.annotation.Nonnull;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public class KafkaRecordSupplier implements RecordSupplier<Integer, Long>
+{
+  private final KafkaConsumer<byte[], byte[]> consumer;
+  private final Map<String, Object> consumerProperties;
+  private final ObjectMapper sortingMapper;
+  private boolean closed;
+
+  public KafkaRecordSupplier(
+      Map<String, Object> consumerProperties,
+      ObjectMapper sortingMapper
+  )
+  {
+    this.consumerProperties = consumerProperties;
+    this.sortingMapper = sortingMapper;
+    this.consumer = getKafkaConsumer();
+  }
+
+  @Override
+  public void assign(Set<StreamPartition<Integer>> streamPartitions)
+  {
+    consumer.assign(streamPartitions
+                        .stream()
+                        .map(x -> new TopicPartition(x.getStream(), x.getPartitionId()))
+                        .collect(Collectors.toSet()));
+    seekToEarliest(streamPartitions);
+  }
+
+  @Override
+  public void seek(StreamPartition<Integer> partition, Long sequenceNumber)
+  {
+    consumer.seek(new TopicPartition(partition.getStream(), partition.getPartitionId()), sequenceNumber);
+  }
+
+  @Override
+  public void seekToEarliest(Set<StreamPartition<Integer>> partitions)
+  {
+    consumer.seekToBeginning(partitions
+                                 .stream()
+                                 .map(e -> new TopicPartition(e.getStream(), e.getPartitionId()))
+                                 .collect(Collectors.toList()));
+  }
+
+  @Override
+  public void seekToLatest(Set<StreamPartition<Integer>> partitions)
+  {
+    consumer.seekToEnd(partitions
+                           .stream()
+                           .map(e -> new TopicPartition(e.getStream(), e.getPartitionId()))
+                           .collect(Collectors.toList()));
+  }
+
+  @Override
+  public Set<StreamPartition<Integer>> getAssignment()
+  {
+    Set<TopicPartition> topicPartitions = consumer.assignment();
+    return topicPartitions
+        .stream()
+        .map(e -> new StreamPartition<>(e.topic(), e.partition()))
+        .collect(Collectors.toSet());
+  }
+
+  @Nonnull
+  @Override
+  public List<OrderedPartitionableRecord<Integer, Long>> poll(long timeout)
+  {
+    List<OrderedPartitionableRecord<Integer, Long>> polledRecords = new ArrayList<>();
+    for (ConsumerRecord<byte[], byte[]> record : consumer.poll(timeout)) {
+      polledRecords.add(new OrderedPartitionableRecord<>(
+          record.topic(),
+          record.partition(),
+          record.offset(),
+          record.value() == null ? null : ImmutableList.of(record.value())
+      ));
+    }
+    return polledRecords;
+  }
+
+  @Override
+  public Long getLatestSequenceNumber(StreamPartition<Integer> partition)
+  {
+    Long currPos = consumer.position(new TopicPartition(partition.getStream(), partition.getPartitionId()));
 
 Review comment:
   There's another PR in the works -- #6496 -- to upgrade to Kafka 2.x, from @surekhasaharan, that we were planning to do this in after this current PR has been merged (to avoid conflicts). There were other changes needed too in order to make the upgrade work smoothly (check out that PR). So IMO it makes sense to leave this as is, & we can make the new Kafka API changes over in #6496.

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

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@druid.apache.org
For additional commands, e-mail: commits-help@druid.apache.org