当前位置:网站首页>Design and implementation of redis (4): what is the event driver of redis
Design and implementation of redis (4): what is the event driver of redis
2022-04-23 03:30:00 【The view of extraordinary is often far away】
6、 ... and 、 event
Redis The server is a Event driver , There are two main types :
- Document events :Redis The server connects with the client through socket , File events are the server's abstraction of socket operations . The communication between the server and the client will generate corresponding file Events , The server completes a series of network communication operations by listening to these events .
- Time event :Redis The server has some operations that need to be performed within a given time , Time event is the abstraction of this kind of timing operation .
Simply speaking , File events are Events related to socket operation ; Time events are Timed operation related events .
6.1 Document events
Redis be based on Reactor Network event processor developed by pattern , File event handler (file event handler):
- Use I/O Multiplexing program Listen to multiple sockets at the same time , According to the socket, the current task is socket Associate different event handlers ;
- When the monitored socket is ready The reply , Read , write in , close When waiting for operation , The corresponding file event will generate , The file event handler will call the event handler associated with the socket to handle the event .
Use IO Multiplexing listening to multiple sockets , Although it runs as a single thread , But the file event processor implements a high-performance network communication model , And can be very good with Redis Docking of other modules in the server , Keep the design simplicity .
The composition of file Events
By socket ,I/O Multiplexing program , File event dispatcher , The event processor consists of .
File events are right Socket operations The abstraction of , When the socket is ready to execute The reply , Read , write in , close A file event will be generated during operation . A server usually listens to multiple sockets , Therefore, file events may occur concurrently . but IO The multiplexer will put all the events in one queue , In order , Sync , One socket at a time To the file event dispatcher . Only when the event generated by the last socket is executed by the event handler , Will continue to send the next socket .
The dispatcher executes the corresponding event handler according to the received socket call .
6.1.1 Implementation of multiplexing
Redis Yes select、epoll、evport and kqueue And other multiplexed function libraries , Each multiplex function library corresponds to a separate file :ae_select.c,ae_epoll.c,ae_kqueue.c etc. . Each multiplexing function implements the same API, Therefore, the underlying implementation of multiplexing program is interchangeable .Redis In the multiplexer source code, the corresponding rules are defined with macros , Make the program automatically select the highest performance in the system when compiling I/O Multiplex function library .
The socket events that the multiplexer can listen for can be divided into ae.h/AE_READABLE Events and ae.h/AE_WRITABLE event
- When the socket becomes Can be read when ( The client performs... On the socket write,close,accept after ), Socket generation AE_READABLE event .
- When the socket becomes Can write when ( The client performs... On the socket read After the operation ), Socket generation AE_WRITABLE event .
I/O The multiplexer allows the server to listen for two events at the same time , If a socket generates two events at the same time , File event dispatch will take priority AE_READABLE, To deal with AE_WRITABLE.
6.1.2 File event handler
According to the needs of the client , The event processor is divided into connection response processor , Command request processor , Command reply processor , Copy processor , The first three processors are most commonly used .
- Connect to answer processor
networking.c/acceptTcpHandler The function is Redis The connection response processor of , Used to connect to The server listens to the socket The client replies .
Redis The server On initialization , The program will connect the response processor and the server to listen for the socket AE_READABLE Event correlation , When the client calls sys/socket.h/connect Function when connecting to the server to listen on the socket , Socket will produce AE_READABLE event , Causes the connection answering processor to execute , And perform the corresponding socket answer operation .
Simply put, when the client connects to the socket that the server is listening to , The server socket generates and triggers a read event , The connection response processor will execute .

- Command request processor
networking.c/readQueryFromClient The function is Redis Command request processor , It is mainly responsible for reading the command request content sent by the client from the socket .
When the client successfully connects to the server , The server will Client socket Of AE_READABLE The event is associated with the command request processor . This association will be in the client There has always been a problem during the connection .
When the client sends a command request to the server , Socket generation AE_READABLE event , Causes a command request to be executed by the processor , Execute the read operation of the corresponding socket .
Simply put, when the client sends a command request , The client socket generates and triggers a read event , The command request processor will execute .
- Command reply processor
networking.c/sendReplyToClient The function is Redis Command reply processor , It is responsible for returning the command reply obtained after the server executes the command to the client through the socket .
When you need to reply to the command result , The server will Client socket Of AE_WRITEBLE Events are associated with the command reply processor , When the client is ready to receive a reply, a AE_WRITABLE event , Cause command reply processor to execute **. end of execution , The server will disarm The command replies to the socket between the processor and the client AE_WRITABLE Between events relation **.
Simply put, when the server sends a command reply , The client socket generates and triggers a write event , The command reply processor will execute .
- Copy processor
Used for replication between master and slave servers , Let's not introduce .
A complete interaction between server and client based on file Events , The processing process of the relevant processor :
1. Client initiates connection ,** The server listens to the socket ** produce AE_READABLE event , Trigger connection reply processor to execute . Then create the client socket , The client state and the status of the client socket AE_READABLE Event associated with command request processor , The client can then send commands to the server .
1. Client sends command ,** Client socket ** produce AE_READABLE event , Trigger command request processor , The actuator reads the execution command , The specific operation is carried out by relevant procedures . After generating the corresponding command reply , Server will ** Client socket ** Of AE_WRITEBLE Event associated with command reply processor .
1. The client tried to read the command reply , produce AE_WRITEBLE event , Trigger command reply processor to execute . Write reply to socket , Disassociate the read event from the command reply processor .
6.2 Time event
Time events can be divided into Timing events and Periodic events :
- Timing events : Only when the specified event arrives . Such as xx Execute once after time .
- Periodic events : Execute at regular intervals . Every time xx Once per second .
A time event mainly consists of the following three properties :
- id: The global uniqueness created by the server for time events ID ( Identification number ). ID Number press From small to large In increasing order , New events ID The number is bigger than the old event .
- when: Millisecond precision UNIX Time stamp , The arrival of time events (arrive) Time .
- timeProc: Time event handler function . When the time event arrives , The server will call the corresponding processor to handle the event .
Whether a time event is a timed event or a periodic event depends on the return value of the time event processor :
- Event handler returns ae.h/AE_NOMORE, For timed events : This event is deleted after reaching one , No longer arrive .
- The event handler returned a non AE_NOMORE The integer value , Is periodic time . When a time event arrives , The server will use the value returned by the event handler , Of time events when Property update , Let this event arrive again after a period of time , And keep updating and running in this way .
redis Only cycle time events are used in , Timed events are not used .
Realization
The server places all time events in an unordered list , Whenever the time event executor runs , Traverse the entire list , Find the time event that has arrived , Call the corresponding event handler .
An unordered linked list refers to a list that does not follow the when Sort by time , It doesn't mean not to press id Size sorting .
serverCron function
In many cases ,Redis Regular resource checks are required , State synchronization and other operations , You need to operate regularly , And regular operations are carried out by serverCron Functions are responsible for , It is also an application example of time events . Default every 100ms Do it once , The specific work includes :
- Update all kinds of statistics of the server , Such as time 、 Memory footprint 、 Database usage, etc .
- Clean up expired key value pairs in the database .
- Shut down and clean up clients with invalid connections .
- Try to do AOF or RDB Persistence operation .
- If the server is the primary server , Then synchronize the slave server regularly .
- If in cluster mode , Perform regular synchronization and connectivity tests on the cluster .
Let's start from several aspects , Introduce serverCron My job :
- Update server time cache , Default 100 Millisecond update once unixtime and mstime
- to update LRU The clock , Every Redis Objects also have lru attribute , Record the last time the command was accessed . If you want to calculate the idle time of a key , Will be through lrulock The recorded time minus the... Of the object lru Attribute record time .
- Number of commands executed by update server per second , This estimate will be put into redisServer Of ops_sec_samples Array .
- Update server memory peak record ,stat_peak_memory Record the memory peak .
- Manage client resources , call clientsCron Function to check the client : If it has timed out, close ; If the input buffer size exceeds a certain length, recreate the input buffer of the default size .
- Manage database resources ,serverCron Called every time databaseCron function , Some databases of the server will be checked , Delete expiration key ; Shrink the dictionary .
- Execution is delayed BGREWRITEAOF,aof_rewrite_shceduled The flag bit decides ,BGSAVE During the execution of the order ,BGREWRITEAOF Will be delayed until BGSAVE After execution .
- Check the running state of the persistence operation
6.3 Scheduling of events
When there are both time events and file events on the server , The event is scheduled by ae.c/aeProcessEvents The function is responsible for . For each event cycle , The main process is :
- Get the latest time event and calculate how many milliseconds are left .
- Create time task structure ; Blocking waiting for file events to occur , Maximum blocking time Determined by the number of milliseconds the most recent event has reached .
- Deal with the generated first Document events Then process the arrived Time event .
redis Server running process
The processing of file events and time events is Sync 、 Orderly 、 Atomic execution Of , The server will not interrupt event handling , And there will be no preemption of events , therefore , Whether it's the file event handler , Or the processor of time events , They all reduce the blocking time of the program as much as possible , And give up the executive power when necessary ( For example, the execution time of time events is too long to call subprocesses , The amount of file event operation data is too large break, The rest will be executed next time ), So as to reduce the possibility of causing incident hunger .
7、 ... and 、 client
redis Server is a typical one to many server program , A server can establish connections with multiple clients , Each client can send commands to the server . The server is based on IO Event processor implemented by multiplexing technology , Single threaded processing commands .
Redis The state structure of the server clients The attribute is Linked list , All client structures connected to the server are recorded , Perform batch operation or search operation on the client , Both can pass clients Linked list complete .
struct redisServer{
// A linked list , Saved all client states
list *clents
...
};
The structure of each client is
typedef struct redisClient{
// socket descriptor
int fd;
// sign
int flags;
// Input buffer
sds querybuf;
// An array split by a single command
robj **argv;
//argv Length of array
int argc;
// Command function , Point to specific redisCommand structure
struct redisCommand *cmd;
// Fixed size output buffer , Default 16K
char buf[REDIS_REPLY_CHUNK_BYTES];
//buf Number of bytes used
int bufpos;
// Variable size output buffer
list *reply
// When the client was created
time_t ctime;
// The last time to interact with the server
time_t lastinteraction;
// Soft limit time
time_t obuf_soft_limit_reached_time;
...
}redis client;
socket descriptor fd
Depending on the client type :
- fd by -1 Represents a pseudo client .
- fd For more than -1 An integer representing a normal client .
Pseudo client is the command request used for processing, which comes from AOF or Lua Script , No socket connection required , You don't need a socket recorder . Ordinary clients are all clients that need socket connection from the network .
sign flags
sign flags The role of the client is recorded . There are master-slave signs ,Lua Pseudo client flag , perform MONITOR sign … Flags can be spliced in binary :flags:||…
Input buffer querybuf
The input buffer stores instructions entered by the client , The size is dynamically reduced and expanded according to the input content , No more than 1G, Otherwise, the server will shut down the client .
Commands and parameters (argv,argc)
The commands entered by the client will be stored in the array first argv in , The data structure is like this :
When the client enters a command , Server according to argv[0] Find the value in the command table ( Commands are not case sensitive ) The function corresponding to the command and give cmd assignment ,cmd Is the operation information related to the corresponding command function .
Output buffer (buf,bufpos,reply)
There are two output buffers , A fixed size , A variable size . Fixed storage size and small reply length , such as OK, Error return, etc . A variable size buffer holds replies with a large length , Like a long list , Large set .
The variable size buffer consists of reply Linked list implementation , Use the linked list structure to store several string objects , So that the length is not limited . The data structure is as follows :
Time (ctime,lastinteraction,obuf_soft_limit_reached_time)
The server uses two modes to limit the size of the client output buffer :
- Hard limits ( hard limit): If the size of the output buffer exceeds the size set by the hard limit , Then the server immediately shuts down the client .
- Soft limits (softlimit): The soft limit is smaller than the hard limit , The server will decide whether to close the client according to the time when the output buffer size is between the soft and hard limits .
8、 ... and 、 Server side
8.1 Initialize server
First of all Redis Server initialization operation , The server needs to perform the following steps from Startup to being able to process command requests from the client :
- Initialize server state .
- Load server configuration .
- Initialize the server data structure .
- Restore database state .
- Execute the event loop .
Initialize the server state structure
It's mainly about redisServer Initialization of structure , Including setting up the server to run ID, Operating frequency , Set profile path , Set persistence conditions , Command table creation, etc .
Load configuration options
According to the configuration set by the user , Yes redisServer Modify the value of the relevant variable , Such as port number , Number of databases ,RDB Whether the compression of is turned on, etc . Other properties still use the default values .
Initialize the server data structure
The server must first load the user configuration , In order to accurately initialize other data structures . Other data structures include client linked lists ,db Array , Subscription information ,Lua Script execution environment , Slow query log related properties and so on .
Restore database state
load RDB or AOF File data recovery process .
Execute the event loop
thus , The server can receive client requests and send information .
8.2 Command execution process
With SET key value For example , The execution process of the command is :
- Client sends command .
- The server receives and processes requests , Operation on Database , reply OK.
- The server replies the command to the client .
- The client receives the command and prints the result .
Next, it will be disassembled into sending , Read lookup , Perform preliminary operations , Call the implementation function , Carry out follow-up work , reply , Explanation of printing operation .
send out
When the client receives a command request , The command will be converted into a fixed format according to the protocol and then sent to the server .
Read
When the socket becomes readable due to writing from the client , The server will first read the content in the protocol format and save it to the input buffer . Command analysis , Extract parameters and number , Deposit in argv and argc attribute . Finally, call the command executor. Base note .
Command actuators - Find the implementation of the command
The command table is a Dictionaries , The key is the command name , The value is redisCommand structure . Several important properties are as follows :
- name: Command name .
- proc: Point to the command implementation function .
- arity: Number of command parameters , Include command name .
- sflags: Command properties .
The process of looking up the command table is to find redisCommand, Point the pointer to it :
Command actuators - Perform preliminary operations
Before the command is actually executed, there needs to be preparatory operation to ensure that the command can be correctly executed , Carry out smoothly . This link is equivalent to a layer of filtration , For example, check whether the command is correct , Is the parameter correct , Whether the authentication has passed , Whether there is enough memory, etc . Ensure that the configuration is effective , Accurately execute .
Command actuators - Call the implementation function of the command
The execution process is to call the execution function found and pointed to before . adopt client->cmd->proc(client); call . Then save the reply in the output buffer of the client state , The command reply processor associated with the socket .
Command actuators - Carry out follow-up work
Some aftercare work will continue , For example, slow query log records , Record the execution time ,AOF Persistence , The master server passes the command to the slave server . When all this is done , The server continues to fetch the file from the time processor and execute the next command request .
Send command reply to client
When the client socket becomes writable , The server executes the command reply processor , Send the reply from the output buffer to the client .
The client receives and prints the command reply
Turn the reply into a human readable format , Print to the user .
notes : The content is transferred from the learning notes on YuQue , The main reference is from 《Redis Design and implementation 》 Some reference sources are no longer traceable , Infringement private deletion .
版权声明
本文为[The view of extraordinary is often far away]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220616492716.html
边栏推荐
- Chapter 8 of C language programming (fifth edition of Tan Haoqiang) is good at using pointer exercises to analyze and answer
- Problem a: face recognition
- L3-011 direct attack Huanglong (30 points)
- If statement format flow
- C-10 program error correction (recursive function): number to character
- Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
- POI create and export Excel based on data
- Three types of jump statements
- 淺學一下I/O流和File類文件操作
- C-11 problem I: find balloon
猜你喜欢
Un aperçu des flux d'E / s et des opérations de fichiers de classe de fichiers
2022 团体程序设计天梯赛 模拟赛 L2-1 盲盒包装流水线 (25 分)
Fiddler use
Learn about I / O flow and file operations
2022 group programming ladder simulation match 1-8 are prime numbers (20 points)
Idea view history [file history and project history]
Database - MySQL -- Navicat import SQL error 1067 - invalid default value for 'paydate‘
Chapter 8 of C language programming (fifth edition of Tan Haoqiang) is good at using pointer exercises to analyze and answer
Redis (17) -- redis cache related problem solving
2022 团体程序设计天梯赛 模拟赛 1-8 均是素数 (20 分)
随机推荐
poi根据数据创建导出excel
Unity basics 2
Query stored procedures in PostgreSQL
Section 2 map and structure in Chapter 6
通过 zxing 生成二维码
Quartz. Www. 18fu Used in net core
7-2 Tushare
JS implementation of new
Initial experience of talent plan learning camp: communication + adhering to the only way to learn open source collaborative courses
Docker拉取mysql并连接
There is no index in the database table. When inserting data, SQL statements are used to prevent repeated addition (Reprint)
C-11 problem I: find balloon
浅学一下I/O流和File类文件操作
Unity Basics
L3-011 直捣黄龙 (30 分)
Visual programming - Experiment 1
[microservices] (x) -- Unified gateway
PyMOL usage
全新的ORM框架——BeetlSQL介绍
Why is bi so important to enterprises?