Kafka source code analysis: the running process of the server side

​​​​​​​​Abstract: The server side of the Kafka network module, introduces the process of starting, receiving and processing requests on the server side.

This article is shared from Huawei Cloud Community "Kafka Network Module - Server" , the original author: middleware brother.

SocketServer is a module used by the Kafka server to process requests. It is created, initialized, and started during the Kafka startup process.

SocketServer startup process:

  • Initialize the Acceptor in the order of endpoints, each endpoint corresponds to an Acceptor, create a Processor for each Acceptor (the number is determined by the num.network.threads configuration item), and start the Acceptor. After the Acceptor starts, it will monitor the connection through the selector, and the newly established The connection is handed over to the Processor (polling to select the Processor)

  • start all processors

  • Acceptor starts and listens for the connection process:

  • After the Acceptor starts, it will create a serverSocketChannel, listen on the endpoint corresponding to the acceptor, and register OP_ACCEPT on the selector, and then enter an infinite loop. In each loop, the ready key (that is, the previously registered serverSocketChannel) is obtained through the selector, indicating that there are When the connection arrives, create a socketChannel corresponding to the connection through accept(), then poll to select one from the processors responsible for the acceptor, and hand the socketChannel to the selected processor for processing, that is, hand the connection to the processor.

  • The Acceptor hands the connection to the processor for processing, which is to add the socketChannel to the processor's connection queue newConnection, and the processor will continuously obtain and process it in the run method.

  • After the Processor obtains the socketChannel from newConnection, it registers OP_READ on the selector and creates the corresponding KafkaChannel.

The process of receiving and processing requests on the server side:

  • After the Processor receives the OP_READ event and is ready, check and try to complete the SSL handshake and SASL verification (the handshake may not be completed at this time, so after the Processor receives the OP_READ event and is ready, it must first check and ensure that the handshake has been completed, SSL/SASL Related reference (Section 9.4)

  • After the SSL handshake and SASL verification are completed, read data from the channel, construct a NetworkReceive object, and queue it into stagedReceives

  • Take out the first element of the stagedReceives team (remove) and add completedReceives

  • Take out (do not remove) the elements in completedReceives, construct a Request object, join the requestQueue, remove the event registration for OP_READ, set the corresponding KafkaChannel to MUTED, and then set it to MUTED_AND_RESPONSE_PENDING

  • KafkaRequestHandler takes elements (removes) from the requestQueue and hands them over to the KafkaApi module to process the request

  • After KafkaApi processes the request, it puts the response into the responseQueue and inflightResponses of the corresponding processor, and wakes up its selector

  • Processor takes the response (removes) from the responseQueue. If the response needs to be sent back to the client, it assigns the send of the response to KafkaChannel and registers the OP_WRITE event

  • When the channel is ready to write, write send to the channel's write buffer, when send is finished, remove the registration for the OP_WRITE event, and add send to completedSends

  • Remove the corresponding response from inflightResponses, execute the response callback, set the KafkaChannel to MUTED, then from MUTED to NOT_MUTED, and re-add the OP_READ event registration

Click Follow to learn about HUAWEI CLOUD's new technologies for the first time~

Guess you like

Origin juejin.im/post/6977178302414864398