Jafka Source Analysis--processor

Source: Internet
Author: User
Tags rewind

Jafka Acceptor accepts the client and establishes a connection request,acceptor will give the Socket to Processor be processed. Processor handles the client request with the following processing steps:

1. Read the client request.

2. Depending on the client request type, the corresponding handler function is called for processing.

Processor reading a client request is a more interesting thing to consider two things: first, the request rule (Processor need to follow a certain rule to resolve the request). Second, how to determine that the read of a request has ended (due to a non-clogging connection, it is possible that the first read operation read a portion of the requested data, the second to nth reading ability to read the entire client request). Here we specifically parse the format of the client request.

The client request first includes aint, theintindicates the size of this client request(size). The request then includes a twobyte (short)types of requests (Request types include:createrrequest,deleterrequest,fetchrequest,multifetchrequest,multiproducerrequest,offsetrequestand theproducerrequest. Then there is a fixed format for each type of request. Specifically explained theproducerrequestThe format:



Knowing the format above, problem two (how to determine that a request has been read) is very easy to conquer.

First, assign the request length to a4byteof theBytebuffer, until theBufferRead full, otherwise the description length has not been read finished. After the request length has been read, assign a request length size to the requestBytebuffer, until theBufferA full read indicates that a request was read. After reading, the corresponding handler function is called according to the request type (Handler) for processing. In theJafka, the above twoBufferin the classboundedbytebufferreceiveto be declared and managed in the Processorreceived aAcceptorassigned bySocketafter the connection. will provideSockeConnect to create aboundedbytebufferreceiveand associate it withSocketthe connection is bound. Whenever theSocketWhen the connection is "readable". Will beboundedbytebufferreceivetake it out and continue reading from the last read. Until a request is completely read, the detailed procedure is as follows (processor.read)what you see:

private void Read (Selectionkey key) throws IOException {Socketchannel Socketchannel = channelfor (key); Receive request = Null;request = new Boundedbytebufferreceive (maxrequestsize); Key.attach (request); else {request = (Receive) key.attachment ();} int read = Request.readfrom (Socketchannel); Stats.recordbytesread (read); if (read < 0) {close (key);} else if ( Request.complete ()) {Send mayberesponse = handle (key, request); Key.attach (null);//If there is a response, send it, otherw Ise do nothingif (mayberesponse! = null) {Key.attach (mayberesponse); Key.interestops (Selectionkey.op_write);}} else {//More reading to be donekey.interestops (Selectionkey.op_read); Getselector (). Wakeup (); if ( Logger.istraceenabled ()) {Logger.trace ("Reading request not been done." + request);}}


Boundedbytebufferreceive.readfrom Implementation details such as the following: The main is to apply two Buffer and constantly read data.

public int Readfrom (Readablebytechannel channel) throws IOException {Expectincomplete ();        int read = 0;        if (sizebuffer.remaining () > 0) {read + = Utils.read (channel, Sizebuffer);            } if (Contentbuffer = = null &&!sizebuffer.hasremaining ()) {sizebuffer.rewind ();            int size = Sizebuffer.getint ();            if (size <= 0) {throw new Invalidrequestexception (...); if (Size > Maxrequestsize) {final String msg = "Request of length%d is not valid, it is L                Arger than the maximum size of%d bytes. ";            throw new Invalidrequestexception (Format (msg, size, maxrequestsize));        } Contentbuffer = bytebufferallocate (size);            }//if (contentbuffer! = null) {read = Utils.read (channel, Contentbuffer);      if (!contentbuffer.hasremaining ()) {contentbuffer.rewind ();          Setcompleted ();    }} return read; }


After reading,Processor resolves the " request Type", depending on the request type, different Handler processing corresponds to the request.

Copyright notice: This article Bo Master original articles, blogs, without consent may not be reproduced.

Jafka Source Analysis--processor

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.