[Redis study notes (eight)] detailed explanation of events in Redis

This article is published by the official account [Developing Pigeon]! Welcome to follow! ! !


Old Rules-Sister Town House:

One. event

(I. Overview

       The Redis server is an event-driven program, and the server needs to handle two types of events:

1. File events

       The Redis server connects with the client through a socket, and the file event is the abstraction of the server's socket operation. The communication between the server and the client will generate corresponding file events, and the server completes network communication operations by monitoring and processing these events.


2. Event time

       Some operations in the server, such as the serverCron function, need to be executed at a given point in time. Time events are the abstraction of the server for such timing operations.


(2) Document incident

1 Overview

       Redis has developed its own network time processor based on the Reactor model, called a file event processor. It uses an I/O multiplexing program to monitor multiple sockets at the same time, and is a socket based on the task currently performed by the socket. Words are associated with different event handlers. When the monitored socket is ready to perform connection response, read, write, close and other operations, the file event corresponding to the operation will be generated, and then the file event handler will call the previously associated socket Event handlers handle these events.

       The file event processor runs in a single-threaded manner, and uses I/O multiplexing programs to monitor multiple sockets, which implements a high-performance network communication model and maintains the simplicity of single-threaded design.


2. The composition of the file event handler

       The file event consists of four parts: socket, I/O multiplexing program, file event dispatcher, and event handler.

(1) Socket

       File events are an abstraction of socket operations. Whenever a socket is ready to perform an operation, a file event will be generated. Because a server will connect to multiple sockets, multiple file events may appear concurrently.


(2) Multiplexing

       Monitor multiple sockets, and pass those sockets that have generated events to the file event dispatcher. Although file events will be generated concurrently, the multiplexer will put these events in a queue for orderly distribution. When the event generated by the previous socket has been processed, it will continue to send the next socket to the dispatcher.


(3) Dispatcher

       Accept the socket, according to the type of event generated by the socket, call the corresponding event handler.


(4) Processor

       The server associates different event handlers for sockets that perform different tasks. These handlers are functions that define the operations that should be performed.


3. The realization of I/O multiplexing

       It is realized by packaging common I/O multiplexing function libraries such as select, epoll, evport and kqueue.


4. Types of events

(1) AE_READABLE event

       When the socket becomes readable, or when a new answerable socket appears, the socket generates an AE_READABLE event.


(2) AE_WRITEABLE event

       This event is generated when the socket becomes writable.

       The I/O multiplexing program monitors these two events at the same time, and prioritizes the read event.


5. Handler for file events

(1) Connect the response processor

       Used to respond to the client connecting to the server's listening socket. When the server is initialized, it will associate the connection response processor with the read event of the server's listening socket. As long as there is a client connecting to the server's listening socket through the connect function Socket, the socket will generate a read event, causing the connection response processor to execute.


(2) Command request processor

       Responsible for reading the command request content sent by the client from the socket. During the entire process of the client connecting to the server, the server will always request the processor for the command request processor associated with the read event of the client socket.


(3) Command reply processor

       Responsible for returning the command reply obtained by the server after executing the command to the client through the socket.


(3) Time events

1 Overview

       Time events are divided into two categories, one is timed time, and the other is periodic event. A time event is composed of three attributes: a
       globally unique ID, a UNIX timestamp with millisecond precision, a record of the arrival time of a time event, and a time event handler.

2. Realization

       The server puts all time events in an unordered linked list. When the time event executor executes, it traverses the entire linked list, finds all the time events that have arrived, and calls the corresponding event handler.


(4) Event scheduling

       For file events and time events, the server needs to schedule these two events. Because time events are fixed at an event, and file events are executed randomly, in order to avoid frequent polling operations for time events, the total The file event is processed first, and then the time event is processed. The processing of the two events is synchronized.

Guess you like

Origin blog.csdn.net/Mrwxxxx/article/details/114238741