apex-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gauravgopi123 <...@git.apache.org>
Subject [GitHub] incubator-apex-malhar pull request: New Kafka input operator using...
Date Wed, 23 Dec 2015 19:34:57 GMT
Github user gauravgopi123 commented on a diff in the pull request:

    https://github.com/apache/incubator-apex-malhar/pull/134#discussion_r48373387
  
    --- Diff: kafka/src/main/java/org/apache/apex/malhar/kafka/AbstractKafkaPartitioner.java
---
    @@ -0,0 +1,276 @@
    +/**
    + * 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.apex.malhar.kafka;
    +
    +import java.io.ByteArrayOutputStream;
    +import java.util.Collection;
    +import java.util.HashMap;
    +import java.util.Iterator;
    +import java.util.LinkedList;
    +import java.util.List;
    +import java.util.Map;
    +import java.util.Objects;
    +import java.util.Properties;
    +import java.util.Set;
    +
    +import org.slf4j.Logger;
    +import org.slf4j.LoggerFactory;
    +
    +import org.apache.kafka.clients.consumer.KafkaConsumer;
    +import org.apache.kafka.common.PartitionInfo;
    +import org.apache.kafka.common.TopicPartition;
    +
    +import com.esotericsoftware.kryo.Kryo;
    +import com.esotericsoftware.kryo.io.Input;
    +import com.esotericsoftware.kryo.io.Output;
    +import com.google.common.base.Joiner;
    +
    +import com.datatorrent.api.DefaultPartition;
    +import com.datatorrent.api.Partitioner;
    +import com.datatorrent.api.StatsListener;
    +
    +/**
    + * Abstract partitioner used to manage the partitions of kafka input operator.
    + * It use a number of kafka consumers(one for each cluster) to get the latest partition
metadata for topics that
    + * the consumer subscribes and expose those to subclass which implements the assign method
    + *
    + * The partitioner is always stateless.
    + */
    +public abstract class AbstractKafkaPartitioner implements Partitioner<AbstractKafkaInputOperator>,
StatsListener
    +{
    +
    +  private static final Logger logger = LoggerFactory.getLogger(AbstractKafkaPartitioner.class);
    +
    +  private static final String META_CONSUMER_GROUP_NAME = AbstractKafkaInputOperator.class.getName()
+ "META_GROUP";
    +
    +  protected String[] clusters;
    +
    +  protected String[] topics;
    +
    +  protected AbstractKafkaInputOperator prototypeOperator;
    +
    +  private KafkaConsumer[] metadataRefreshClients;
    +
    +
    +  private List<Set<AbstractKafkaPartitioner.PartitionMeta>> currentPartitions
= new LinkedList<>(); // prevent null
    +
    +  public AbstractKafkaPartitioner(String[] clusters, String[] topics, AbstractKafkaInputOperator
prototypeOperator)
    +  {
    +    this.clusters = clusters;
    +    this.topics = topics;
    +    this.prototypeOperator = prototypeOperator;
    +  }
    +
    +  abstract List<Set<PartitionMeta>> assign(Map<String, Map<String,List<PartitionInfo>>>
metadata);
    +
    +
    +
    +  @Override
    +  public Collection<Partition<AbstractKafkaInputOperator>> definePartitions(Collection<Partition<AbstractKafkaInputOperator>>
collection, PartitioningContext partitioningContext)
    +  {
    +
    +    initMetadataClients();
    +
    +    Map<String, Map<String, List<PartitionInfo>>> metadata = new HashMap<>();
    +
    +
    +    for (int i = 0; i < clusters.length; i++) {
    +      metadata.put(clusters[i], new HashMap<String, List<PartitionInfo>>());
    +      for (String topic : topics) {
    +        List<PartitionInfo> ptis = metadataRefreshClients[i].partitionsFor(topic);
    +        if (logger.isDebugEnabled()) {
    +          logger.debug("Partition metadata for topic {} : {}", topic, Joiner.on(';').join(ptis));
    +        }
    +        metadata.get(clusters[i]).put(topic, ptis);
    +      }
    +      metadataRefreshClients[i].close();
    +    }
    +
    +    metadataRefreshClients = null;
    +
    +    List<Set<AbstractKafkaPartitioner.PartitionMeta>> parts = assign(metadata);
    +
    +
    +    if (currentPartitions == parts || currentPartitions.equals(parts)) {
    +      logger.debug("No partition change found");
    +      return collection;
    +    } else {
    +      logger.info("Partition change detected: ");
    +      currentPartitions.clear();
    +      currentPartitions.addAll(parts);
    +      int i = 0;
    +      List<Partition<AbstractKafkaInputOperator>> result = new LinkedList<>();
    +      for (Iterator<Partition<AbstractKafkaInputOperator>> iter = collection.iterator();
iter.hasNext();) {
    +        Partition<AbstractKafkaInputOperator> nextPartition = iter.next();
    +        if (parts.remove(nextPartition.getPartitionedInstance().getAssignment())) {
    +          if (logger.isInfoEnabled()) {
    +            logger.info("[Existing] Partition {} with assignment {} ", i,
    +                Joiner.on(';').join(nextPartition.getPartitionedInstance().getAssignment()));
    +          }
    +          result.add(nextPartition);
    +          i++;
    +        }
    +      }
    +
    +      for (Set<AbstractKafkaPartitioner.PartitionMeta> partitionAssignment : parts)
{
    +        if (logger.isInfoEnabled()) {
    +          logger.info("[New] Partition {} with assignment {} ", i,
    +              Joiner.on(';').join(partitionAssignment));
    +        }
    +        result.add(createPartition(partitionAssignment));
    +        i++;
    +      }
    +
    +
    +      return result;
    +    }
    +  }
    +
    +  @Override
    +  public void partitioned(Map<Integer, Partition<AbstractKafkaInputOperator>>
map)
    +  {
    +
    +  }
    +
    +  @Override
    +  public Response processStats(BatchedOperatorStats batchedOperatorStats)
    +  {
    +    Response response = new Response();
    +    response.repartitionRequired = true;
    --- End diff --
    
    Do you always want to re-partition when processStats is called?? 


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message