Kafka Source Code Analysis (ii) Metadata data structure and reading and updating strategies

Source: Internet
Author: User
Tags message queue

First, the basic idea

The basic idea of asynchronous send is: When send, Kafkaproducer put the message to the local message queue recordaccumulator, then a background thread sender keeps looping and sends the message to the Kafka cluster.

To achieve this, there must be a precondition: that is, kafkaproducer/sender need to get the configuration information of the cluster metadata. The so-called metadata, that is, in the previous article, topic/partion and broker mapping relationship: Every Topic each partion, to know its corresponding broker list is what, leader is who, follower who.

Two, 2 data streams

So in, there are 2 data streams:

Metadata Stream (A1,A2,A3): Sender obtains information from the cluster and then updates metadata; Kafkaproducer reads metadata first and then puts the message in the queue.

Message Flow (B1, B2, B3)

As can be seen, metadata is read by multiple producer threads, a sender thread is updated, so it must be thread safe

III. Thread Safety of metadata

As you can see from the code below, all of its public methods are synchronized:

1  PublicFinalclassMetadata {2 ..... 3      Publicsynchronized Cluster Fetch () {4         return  This. cluster;5     }6      PublicSynchronizedLongTimetonextupdate (Longnowms) {7 ..... 8     }9      PublicSynchronizedintrequestupdate () {Ten .....  One     } A .....  -}

IV. Data structure of metadata

1  PublicFinalclassMetadata {2 ...3     PrivateFinalLongREFRESHBACKOFFMS;//In the case of an update failure, the compensation time for the next 1 updates (this variable is not too significant in the code)4     PrivateFinalLongMetadataexpirems;//key values: How often, updates. The default is 600*1000, which is 10 minutes.5     Private intVersion//each update succeeds 1 times, and version increments by 1. This variable is mainly used in the while loop, wait time, as a cyclic judging condition6     Private LongLastrefreshms;//Last Update time (also contains the case of an update failure)7     Private LongLastsuccessfulrefreshms;//The time of the last successful update (2 equals if each succeeds). Otherwise, lastsuccessulrefreshms < LASTREFRESHMS)8     PrivateCluster Cluster;//Cluster configuration information9     PrivateBoolean needupdate;//whether to force refreshTen , One   ... A}

Kafka Source Code Analysis (ii) Metadata data structure and reading and updating strategies

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.