handleClientsWithPendingReadsUsingThreads method in the main thread
....
while(listLength(server.clients_pending_read)) {
ln = listFirst(server.clients_pending_read);
client *c = listNodeValue(ln);
c->flags &= ~CLIENT_PENDING_READ;
listDelNode(server.clients_pending_read,ln);
if (c->flags & CLIENT_PENDING_COMMAND) {
c->flags &= ~CLIENT_PENDING_COMMAND;
if (processCommandAndResetClient(c) == C_ERR) {
/* If the client is no longer valid, we avoid
* processing the client later. So we just go
* to the next. */
continue;
}
}
//There is a conflict between the processing here and the processing in the io thread
processInputBuffer(c);
}
IOThreadMain method in io thread
...
//This method also contains the 'processInputBuffer' method
readQueryFromClient(c->conn);
...
What if the io thread and the main thread handle the same client at the same time?After all, when the main thread processInputBuffer is all clients.
Comment From: starsskye
@antirez hi, Is it that I misunderstood or didn't express it clearly ? Looking forward to your answer. Thank you very much .
Comment From: trevor211
Hi, @starsskye , processInputBuffer is guaranteed to be called on main thread as handleClientsWithPendingReadsUsingThreads is, you can search for its callers to convince yourself.
Take a look at here.
https://github.com/redis/redis/blob/4ac1f9ac55b841094861e84cdec5da0f4a294a86/src/networking.c#L1914
If it returns here, io work is delayed for io threads by adding client c to list server.clients_pending_read. And handleClientsWithPendingReadsUsingThreads is later called by main thread.
Note that handleClientsWithPendingReadsUsingThreads will wait for io threads to complete before it comes to processInputBuffer.
See https://github.com/redis/redis/blob/4ac1f9ac55b841094861e84cdec5da0f4a294a86/src/networking.c#L3185-L3191