All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.twitter.distributedlog.kafka.KafkaDistributedLogProducer Maven / Gradle / Ivy

The newest version!
/**
 * 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 com.twitter.distributedlog.kafka;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.twitter.distributedlog.DLSN;
import com.twitter.distributedlog.messaging.PartitionedMultiWriter;
import com.twitter.distributedlog.messaging.Partitioner;
import com.twitter.distributedlog.messaging.RRMultiWriter;
import com.twitter.distributedlog.service.DistributedLogClient;
import org.apache.kafka.clients.producer.Callback;
import org.apache.kafka.clients.producer.Producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.apache.kafka.common.Metric;
import org.apache.kafka.common.MetricName;
import org.apache.kafka.common.PartitionInfo;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

/**
 * It is a kafka producer that uses dl streams
 */
public class KafkaDistributedLogProducer implements Producer, Partitioner {

    private final DistributedLogClient client;
    private final int numPartitions;
    private final ConcurrentMap> partitionedWriters;
    private final ConcurrentMap> unpartitionedWriters;

    // Assume all streams have same partitions
    public KafkaDistributedLogProducer(DistributedLogClient client,
                                       int numPartitions) {
        this.client = client;
        this.numPartitions = numPartitions;
        this.partitionedWriters = new ConcurrentHashMap>();
        this.unpartitionedWriters = new ConcurrentHashMap>();
    }

    @Override
    public int partition(K k, int totalPartitions) {
        if (null != k) {
            return k.hashCode() % totalPartitions;
        }
        return -1;
    }

    private String[] getStreamsForTopic(String topic) {
        String[] streams = new String[numPartitions];
        for (int i = 0; i < numPartitions; i++) {
            streams[i] = String.format("%s-%d", topic, i);
        }
        return streams;
    }

    private PartitionedMultiWriter getPartitionedMultiWriter(String topic) {
        PartitionedMultiWriter writer = partitionedWriters.get(topic);
        if (null == writer) {
            PartitionedMultiWriter newWriter = new PartitionedMultiWriter(
                    getStreamsForTopic(topic), this, client);
            PartitionedMultiWriter oldWriter = partitionedWriters.putIfAbsent(topic, newWriter);
            if (null != oldWriter) {
                writer = oldWriter;
            } else {
                writer = newWriter;
            }
        }
        return writer;
    }

    private RRMultiWriter getUnpartitionedMultiWriter(String topic) {
        RRMultiWriter writer = unpartitionedWriters.get(topic);
        if (null == writer) {
            RRMultiWriter newWriter = new RRMultiWriter(
                    getStreamsForTopic(topic), client);
            RRMultiWriter oldWriter = unpartitionedWriters.putIfAbsent(topic, newWriter);
            if (null != oldWriter) {
                writer = oldWriter;
            } else {
                writer = newWriter;
            }
        }
        return writer;
    }

    @Override
    public Future send(ProducerRecord producerRecord) {
        return send(producerRecord, null);
    }

    @Override
    public Future send(ProducerRecord producerRecord, Callback callback) {
        com.twitter.util.Future dlsnFuture;
        if (null == producerRecord.key()) {
            dlsnFuture = getUnpartitionedMultiWriter(producerRecord.topic()).write(producerRecord.value());
        } else {
            // TODO: be able to publish to a specific partition
            dlsnFuture = getPartitionedMultiWriter(producerRecord.topic()).write(producerRecord.key(),
                    producerRecord.value());
        }
        return new DLFutureRecordMetadata(producerRecord.topic(), dlsnFuture, callback);
    }

    @Override
    public void flush() {
        // no-op
    }

    @Override
    public List partitionsFor(String s) {
        String[] streams = getStreamsForTopic(s);
        List partitions = Lists.newArrayListWithExpectedSize(streams.length);
        for (int i = 0; i < streams.length; i++) {
            // TODO: maybe add getOwner from dl write proxy to return the owner of the partition
            partitions.add(new PartitionInfo(s, i, null, null, null));
        }
        return partitions;
    }

    @Override
    public Map metrics() {
        // no-op
        return Maps.newHashMap();
    }

    @Override
    public void close() {
        partitionedWriters.clear();
        unpartitionedWriters.clear();
    }

    @Override
    public void close(long l, TimeUnit timeUnit) {
        partitionedWriters.clear();
        unpartitionedWriters.clear();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy