高吞吐量的分散式發佈訂閱消息系統Kafka之Producer源碼分析

来源:https://www.cnblogs.com/MonsterJ/archive/2020/05/30/12994418.html
-Advertisement-
Play Games

引言 Kafka是一款很棒的消息系統,今天我們就來深入瞭解一下它的實現細節,首先關註Producer這一方。 要使用kafka首先要實例化一個KafkaProducer,需要有brokerIP、序列化器等必要Properties以及acks(0、1、n)、compression、retries、ba ...


引言

Kafka是一款很棒的消息系統,今天我們就來深入瞭解一下它的實現細節,首先關註Producer這一方。

要使用kafka首先要實例化一個KafkaProducer,需要有brokerIP、序列化器等必要Properties以及acks(0、1、n)、compression、retries、batch.size等非必要Properties,通過這個簡單的介面可以控制Producer大部分行為,實例化後就可以調用send方法發送消息了。

核心實現是這個方法:

public Future<RecordMetadata> send(ProducerRecord<K, V> record, Callback callback) {
    // intercept the record, which can be potentially modified; this method does not throw exceptions
    ProducerRecord<K, V> interceptedRecord = this.interceptors.onSend(record);//①
    return doSend(interceptedRecord, callback);//②
}

通過不同的模式可以實現發送即忘(忽略返回結果)、同步發送(獲取返回的future對象,回調函數置為null)、非同步發送(設置回調函數)三種消息模式。

我們來看看消息類ProducerRecord有哪些屬性:

private final String topic;//主題
private final Integer partition;//分區
private final Headers headers;//頭
private final K key;//鍵
private final V value;//值
private final Long timestamp;//時間戳

它有多個構造函數,可以適應不同的消息類型:比如有無分區、有無key等。

①中ProducerInterceptors(有0 ~ 無窮多個,形成一個攔截鏈)對ProducerRecord進行攔截處理(比如打上時間戳,進行審計與統計等操作)

public ProducerRecord<K, V> onSend(ProducerRecord<K, V> record) {
    ProducerRecord<K, V> interceptRecord = record;
    for (ProducerInterceptor<K, V> interceptor : this.interceptors) {
        try {
            interceptRecord = interceptor.onSend(interceptRecord);
        } catch (Exception e) {
            // 不拋出異常,繼續執行下一個攔截器
            if (record != null)
                log.warn("Error executing interceptor onSend callback for topic: {}, partition: {}", record.topic(), record.partition(), e);
            else
                log.warn("Error executing interceptor onSend callback", e);
        }
    }
    return interceptRecord;
}

如果用戶有定義就進行處理並返回處理後的ProducerRecord,否則直接返回本身。
然後②中doSend真正發送消息,並且是非同步的(源碼太長只保留關鍵):

private Future<RecordMetadata> doSend(ProducerRecord<K, V> record, Callback callback) {
    TopicPartition tp = null;
    try {
        // 序列化 key 和 value
        byte[] serializedKey;
        try {
            serializedKey = keySerializer.serialize(record.topic(), record.headers(), record.key());
        } catch (ClassCastException cce) {
        }
        byte[] serializedValue;
        try {
            serializedValue = valueSerializer.serialize(record.topic(), record.headers(), record.value());
        } catch (ClassCastException cce) {
        }
        // 計算分區獲得主題與分區
        int partition = partition(record, serializedKey, serializedValue, cluster);
        tp = new TopicPartition(record.topic(), partition);
        // 回調與事務處理省略。
        Header[] headers = record.headers().toArray();
        // 消息追加到RecordAccumulator中
        RecordAccumulator.RecordAppendResult result = accumulator.append(tp, timestamp, serializedKey,
                serializedValue, headers, interceptCallback, remainingWaitMs);
        // 該批次滿了或者創建了新的批次就要喚醒IO線程發送該批次了,也就是sender的wakeup方法
        if (result.batchIsFull || result.newBatchCreated) {
            log.trace("Waking up the sender since topic {} partition {} is either full or getting a new batch", record.topic(), partition);
            this.sender.wakeup();
        }
        return result.future;
    } catch (Exception e) {
        // 攔截異常並拋出
        this.interceptors.onSendError(record, tp, e);
        throw e;
    }
}

下麵是計算分區的方法:

private int partition(ProducerRecord<K, V> record, 
byte[] serializedKey, byte[] serializedValue, Cluster cluster) {
    Integer partition = record.partition();
    // 消息有分區就直接使用,否則就使用分區器計算
    return partition != null ?
            partition :
            partitioner.partition(
                    record.topic(), record.key(), serializedKey,
                     record.value(), serializedValue, cluster);
}

預設的分區器DefaultPartitioner實現方式是如果partition存在就直接使用,否則根據key計算partition,如果key也不存在就使用round robin演算法分配partition。

/**
 * The default partitioning strategy:
 * <ul>
 * <li>If a partition is specified in the record, use it
 * <li>If no partition is specified but a key is present choose a partition based on a hash of the key
 * <li>If no partition or key is present choose a partition in a round-robin fashion
 */
public class DefaultPartitioner implements Partitioner {

    private final ConcurrentMap<String, AtomicInteger> topicCounterMap = new ConcurrentHashMap<>();
    
    public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
        List<PartitionInfo> partitions = cluster.partitionsForTopic(topic);
        int numPartitions = partitions.size();
        if (keyBytes == null) {//key為空 
            int nextValue = nextValue(topic);
            List<PartitionInfo> availablePartitions = cluster.availablePartitionsForTopic(topic);//可用的分區
            if (availablePartitions.size() > 0) {//有分區,取模就行
                int part = Utils.toPositive(nextValue) % availablePartitions.size();
                return availablePartitions.get(part).partition();
            } else {// 無分區,
                return Utils.toPositive(nextValue) % numPartitions;
            }
        } else {// key 不為空,計算key的hash並取模獲得分區
            return Utils.toPositive(Utils.murmur2(keyBytes)) % numPartitions;
        }
    }

    private int nextValue(String topic) {
        AtomicInteger counter = topicCounterMap.get(topic);
        if (null == counter) {
            counter = new AtomicInteger(ThreadLocalRandom.current().nextInt());
            AtomicInteger currentCounter = topicCounterMap.putIfAbsent(topic, counter);
            if (currentCounter != null) {
                counter = currentCounter;
            }
        }
        return counter.getAndIncrement();//返回並加一,在取模的配合下就是round robin
    }
}

以上就是發送消息的邏輯處理,接下來我們再看看消息發送的物理處理。

Sender(是一個Runnable,被包含在一個IO線程ioThread中,該線程不斷從RecordAccumulator隊列中的讀取消息並通過Selector將數據發送給Broker)的wakeup方法,實際上是KafkaClient介面的wakeup方法,由NetworkClient類實現,採用了NIO,也就是java.nio.channels.Selector.wakeup()方法實現。

Sender的run中主要邏輯是不停執行準備消息和等待消息:

long pollTimeout = sendProducerData(now);//③
client.poll(pollTimeout, now);//④

③完成消息設置並保存到通道中,然後監聽感興趣的key,由KafkaChannel實現。

public void setSend(Send send) {
    if (this.send != null)
        throw new IllegalStateException("Attempt to begin a send operation with prior send operation still in progress, connection id is " + id);
    this.send = send;
    this.transportLayer.addInterestOps(SelectionKey.OP_WRITE);
}

// transportLayer的一種實現中的相關方法
public void addInterestOps(int ops) {
    key.interestOps(key.interestOps() | ops);
}

④主要是Selector的poll,其select被wakeup喚醒:

public void poll(long timeout) throws IOException {
    /* check ready keys */
    long startSelect = time.nanoseconds();
    int numReadyKeys = select(timeout);//wakeup使其停止阻塞
    long endSelect = time.nanoseconds();
    this.sensors.selectTime.record(endSelect - startSelect, time.milliseconds());

    if (numReadyKeys > 0 || !immediatelyConnectedKeys.isEmpty() || dataInBuffers) {
        Set<SelectionKey> readyKeys = this.nioSelector.selectedKeys();

        // Poll from channels that have buffered data (but nothing more from the underlying socket)
        if (dataInBuffers) {
            keysWithBufferedRead.removeAll(readyKeys); //so no channel gets polled twice
            Set<SelectionKey> toPoll = keysWithBufferedRead;
            keysWithBufferedRead = new HashSet<>(); //poll() calls will repopulate if needed
            pollSelectionKeys(toPoll, false, endSelect);
        }

        // Poll from channels where the underlying socket has more data
        pollSelectionKeys(readyKeys, false, endSelect);
        // Clear all selected keys so that they are included in the ready count for the next select
        readyKeys.clear();

        pollSelectionKeys(immediatelyConnectedKeys, true, endSelect);
        immediatelyConnectedKeys.clear();
    } else {
        madeReadProgressLastPoll = true; //no work is also "progress"
    }

    long endIo = time.nanoseconds();
    this.sensors.ioTime.record(endIo - endSelect, time.milliseconds());
}

其中pollSelectionKeys方法會調用如下方法完成消息發送:

public Send write() throws IOException {
    Send result = null;
    if (send != null && send(send)) {
        result = send;
        send = null;
    }
    return result;
}
private boolean send(Send send) throws IOException {
    send.writeTo(transportLayer);
    if (send.completed())
        transportLayer.removeInterestOps(SelectionKey.OP_WRITE);
    return send.completed();
}

Send是一次數據發包,一般由ByteBufferSend或者MultiRecordsSend實現,其writeTo調用transportLayer的write方法,一般由PlaintextTransportLayer或者SslTransportLayer實現,區分是否使用ssl:

public long writeTo(GatheringByteChannel channel) throws IOException {
    long written = channel.write(buffers);
    if (written < 0)
        throw new EOFException("Wrote negative bytes to channel. This shouldn't happen.");
    remaining -= written;
    pending = TransportLayers.hasPendingWrites(channel);
    return written;
}

public int write(ByteBuffer src) throws IOException {
    return socketChannel.write(src);
}

到此就把Producer的業務相關邏輯處理和非業務相關的網路 2方面的主要流程梳理清楚了。其他額外的功能是通過一些配置保證的。

比如順序保證就是max.in.flight.requests.per.connection,InFlightRequests的doSend會進行判斷(由NetworkClient的canSendRequest調用),只要該參數設為1即可保證當前包未確認就不能發送下一個包從而實現有序性

public boolean canSendMore(String node) {
    Deque<NetworkClient.InFlightRequest> queue = requests.get(node);
    return queue == null || queue.isEmpty() ||
           (queue.peekFirst().send.completed() && queue.size() < this.maxInFlightRequestsPerConnection);
}

再比如可靠性,通過設置acks,Sender中sendProduceRequest的clientRequest加入了回調函數:

  RequestCompletionHandler callback = new RequestCompletionHandler() {
        public void onComplete(ClientResponse response) {
            handleProduceResponse(response, recordsByPartition, time.milliseconds());//調用completeBatch
        }
    };
    
     /**
     * 完成或者重試投遞,這裡如果acks不對就會重試
     *
     * @param batch The record batch
     * @param response The produce response
     * @param correlationId The correlation id for the request
     * @param now The current POSIX timestamp in milliseconds
     */
    private void completeBatch(ProducerBatch batch, ProduceResponse.PartitionResponse response, long correlationId,
                               long now, long throttleUntilTimeMs) {
    }
    
    public class ProduceResponse extends AbstractResponse {
      /**
         * Possible error code:
         * INVALID_REQUIRED_ACKS (21)
         */
    }

kafka源碼一層一層包裝很多,錯綜複雜,如有錯誤請大家不吝賜教。


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 圖中是暗黑領域,非常牛逼的技能。 背景 DDD中出現的名詞: 領域,子領域,核心域,通用域,支撐域,限界上下文,聚合,聚合根,實體,值對象 都是關鍵概念,但是又比較晦澀,在開始DDD之前,搞清楚這些關鍵概念名詞非常的重要。 那它們作用體現在哪裡呢? 領域-子領域 領域是: 從事專門活動或者事業的範圍 ...
  • #include<vector> // 包含頭文件vector ... using namespace std; // vector包含在std中,因此必須包含std::vector vector <int> vi; // create a zero-size array of int int n; ...
  • python的變數是存在作用域的,在代碼中不同位置的變數作用的範圍會有所不同,比如有的變數在整段代碼中都可以使用,有的變數卻只在函數內部使用。python中把能夠在整段代碼任意位置有效的變數稱為全局變數,只在函數內部使用的變數稱作局部變數。 全局變數: a = 520 #此時a作為全局變數 def ...
  • 1. 應用測試的介紹 一般我們在寫完代碼之後,代碼的測試是會給專門的測試人員來測試的,如果一個測試跑到你的工位上對你說,你的代碼好像有Bug,你肯定會不爽,反正我就是這樣的🙃。所以為了顯示自己的代碼質量高一點,在功能提交給測試之前,我們會自己測試一下,接下來給大家介紹一下 Spring Boot ...
  • 前言 本文的文字及圖片來源於網路,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯繫我們以作處理。 當需要進行大規模查詢時(比如目前遇到的情形:查詢某個省所有發債企業的YY評級分數),人工查詢顯然太過費時,那就寫個爬蟲吧。 由於該爬蟲實在過於簡單,就只簡單概述下。 一、請求 ...
  • 幾乎所有語言的第一個程式都是"HelloWorld" 就像所有單片機初學者一樣,點亮第一個LED燈開始 而起初我們編寫/學習Java程式,都是通過記事本來編寫的,這裡推薦一個Editplus(提取碼:qq1t)記事本文件給大家 這裡要分清楚一個概念,所有Java源程式的尾碼都是*.Java,可以新建 ...
  • 對於動漫愛好者來說,海賊王、火影、死神三大動漫神作你肯定肯定不陌生了。小編身邊很多的同事仍然深愛著這些經典神作,可見“中毒”至深。今天小編利用Python大法帶大家分析一下這些神作,看看這些神作到底在講些神馬。 人生苦短,我用Python。小編利用Python網路爬蟲爬取了豆瓣網,將網站上關於這三部 ...
  • Linux命令之top命令介紹 top命令是Linux下常用的性能分析工具,能夠實時顯示系統中各個進程的資源占用狀況,類似於Windows的任務管理器。下麵詳細介紹它的使用方法。 top是一個動態顯示過程,即可以通過用戶按鍵來不斷刷新當前狀態.如果在前臺執行該命令,它將獨占前臺,直到用戶終止該程式為 ...
一周排行
    -Advertisement-
    Play Games
  • 前言 在我們開發過程中基本上不可或缺的用到一些敏感機密數據,比如SQL伺服器的連接串或者是OAuth2的Secret等,這些敏感數據在代碼中是不太安全的,我們不應該在源代碼中存儲密碼和其他的敏感數據,一種推薦的方式是通過Asp.Net Core的機密管理器。 機密管理器 在 ASP.NET Core ...
  • 新改進提供的Taurus Rpc 功能,可以簡化微服務間的調用,同時可以不用再手動輸出模塊名稱,或調用路徑,包括負載均衡,這一切,由框架實現並提供了。新的Taurus Rpc 功能,將使得服務間的調用,更加輕鬆、簡約、高效。 ...
  • 順序棧的介面程式 目錄順序棧的介面程式頭文件創建順序棧入棧出棧利用棧將10進位轉16進位數驗證 頭文件 #include <stdio.h> #include <stdbool.h> #include <stdlib.h> 創建順序棧 // 指的是順序棧中的元素的數據類型,用戶可以根據需要進行修改 ...
  • 前言 整理這個官方翻譯的系列,原因是網上大部分的 tomcat 版本比較舊,此版本為 v11 最新的版本。 開源項目 從零手寫實現 tomcat minicat 別稱【嗅虎】心有猛虎,輕嗅薔薇。 系列文章 web server apache tomcat11-01-官方文檔入門介紹 web serv ...
  • C總結與剖析:關鍵字篇 -- <<C語言深度解剖>> 目錄C總結與剖析:關鍵字篇 -- <<C語言深度解剖>>程式的本質:二進位文件變數1.變數:記憶體上的某個位置開闢的空間2.變數的初始化3.為什麼要有變數4.局部變數與全局變數5.變數的大小由類型決定6.任何一個變數,記憶體賦值都是從低地址開始往高地 ...
  • 如果讓你來做一個有狀態流式應用的故障恢復,你會如何來做呢? 單機和多機會遇到什麼不同的問題? Flink Checkpoint 是做什麼用的?原理是什麼? ...
  • C++ 多級繼承 多級繼承是一種面向對象編程(OOP)特性,允許一個類從多個基類繼承屬性和方法。它使代碼更易於組織和維護,並促進代碼重用。 多級繼承的語法 在 C++ 中,使用 : 符號來指定繼承關係。多級繼承的語法如下: class DerivedClass : public BaseClass1 ...
  • 前言 什麼是SpringCloud? Spring Cloud 是一系列框架的有序集合,它利用 Spring Boot 的開發便利性簡化了分散式系統的開發,比如服務註冊、服務發現、網關、路由、鏈路追蹤等。Spring Cloud 並不是重覆造輪子,而是將市面上開發得比較好的模塊集成進去,進行封裝,從 ...
  • class_template 類模板和函數模板的定義和使用類似,我們已經進行了介紹。有時,有兩個或多個類,其功能是相同的,僅僅是數據類型不同。類模板用於實現類所需數據的類型參數化 template<class NameType, class AgeType> class Person { publi ...
  • 目錄system v IPC簡介共用記憶體需要用到的函數介面shmget函數--獲取對象IDshmat函數--獲得映射空間shmctl函數--釋放資源共用記憶體實現思路註意 system v IPC簡介 消息隊列、共用記憶體和信號量統稱為system v IPC(進程間通信機制),V是羅馬數字5,是UNI ...