After the jafka acceptor receives the client request and establishes a connection, the acceptor sends the socket connection to the processor for processing. Processor processes client requests by following these steps:
1. Read client requests.
2. Call corresponding processing functions for processing based on different client request types.
It is interesting for processor to read client requests. There are two things to consider: first, request rules (processor needs to parse requests according to certain rules); second, how to determine whether the read of a request has ended (because it is a non-blocking connection, it is very likely that the first read operation reads part of the data of the request, the entire client request can be read completely only from the second read to the nth read ). Next we will parse the format of the client request in detail. The client request contains an int that specifies the size of the client request ). Subsequently, the request contains one or two bytes (short) Request types (the request types include: createrrequest, deleterrequest, fetchrequest, multifetchrequest, multiproducerrequest, offsetrequest, and producerrequest. Each request type has a fixed format. The producerrequest format is described as follows:
After knowing the above format, question 2 (how to determine whether a request has been read) is easy to solve. First, allocate a 4-byte bytebuffer for the "Request Length" until the buffer is fully read. Otherwise, the length is not completely read. After the "Request Length" is read, a bytebuffer of the "Request Length" is allocated to the request until the buffer is fully read. After reading, call the corresponding processing function (handler) based on the request type. In jafka, the preceding two buffers are declared and managed in the boundedbytebufferreceive class. After the processor receives the socket connection allocated by the acceptor, it establishes a boundedbytebufferreceive for the socke connection and binds it to the socket connection. Every time the socket is connected to "readable", the boundedbytebufferreceive will be taken out and read from the last read until a request is completely read. The specific process is shown in the following code (processor. Read:
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, otherwise 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);}}}
The implementation of boundedbytebufferreceive. readfrom is as follows: it mainly applies for two buffers and constantly reads 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 larger 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 the read is complete, processor will parse the "request type" and call different handler to process the corresponding request according to different request types.