Depth Redis command execution

Depth Redis command execution

Redis servers: server implementation Redis side connected to a plurality of clients, and a processing request sent by the client, the client while preserving the data generated by execution command to the database. Redis server relies Explorer to maintain its operation, its main role is to manage Redis service.

Process server processing command

We sent a command to the end customer: SET city "beijing"

The first step the user command SET city "beijing" Enter the client, the client receives this command.
Step client first command received into the server can identify the protocol format, and then use the socket to the server, the command to convert protocol format legitimate requests to the server.

file

When the server receives the third step the protocol data transmission over a client, a socket connection between the client and server will become readable, this time, the server will invoke a command requesting a processor to perform the following procedure.

3.1 socket server protocol format read request command, then the read request command to the client state stored in the input buffer.

3.2 pairs of input buffer command analyzes the request, obtain the number of parameters and parameter request command, are stored in the client status attributes argc and argv.

3.3 call command execution, an execution command sent by the client's request.


Command executor will first [0] parameter lookup during command execution request sent by the client according to the client state in the argv table command (Command Table) specified in the command parameter, and the found save command to the client state cmd attribute and related determination, for example, is determined cmd pointer client state is pointing to NULL, or check the identity of the client, determines whether or not verified the like, the last call to realize the function execution of the commands related to command, which is the command execution actuator.


Command table is a dictionary for storing Redis commands, the dictionary key is the name of a command, such as "set", "sadd" "zadd" and so on; and the value of the dictionary is one redisCommand structure, and each redisCommand structure of the recording information corresponding command implemented. redisCommand structure having a plurality of attributes, as follows.

● name attribute: Indicates the name of the command, such as SET, GET, which is of type char *.

● proc properties: it is a function pointer, the function used to implement point command, for example, point to implement the function setCommand SET command, which is redisCommandProc type, and the type of redisCommandProc redisCommandProc void defined as typedef (redisClient C).

● arity properties: it is an integer type int indicates the number of command parameters, the command request for determining the correct format. If the value is a negative attribute arity -N , represents the number of the command argument is greater than N . Please note that here the number of parameters contains the command name itself, for example, SET city "beijing", the number of parameters of this command is 3, namely the "SET", "city", "beijing."

● sflags properties: it is a char * string type identification value, having a plurality of identifiers for the recording command has the property.

● flags properties: it is an integer type int, binary identification of the identification sflags drawn from the analysis, the binary identification generated automatically by the program. When the server identification command checks using the flags attribute.

● calls attribute: This attribute is used to count how many times the server. Run this command, it is an integer type long long.

● milliseconds attribute: This attribute statistics server performing the total duration of this command consumed, it is an integer type long long.

Each command has redisCommand Redis corresponding structure, has the above related attributes.

sflags attribute has the following specific identifier.

● a: attribute value a, this indicates a command Redis management command. The associated commands are SAVE, BGSAVE, SHUTDOWN and so on.

● l: attribute value l, refers to a command used to load data in the server process. The associated commands are INFO, PUBLISH, SUBSCRIBE and so on.

● m: attribute value m, the process described in the order of execution may consume large amounts of memory. Prior to the implementation, we need to determine the server's memory size and usage, server memory resources if insufficient, will refuse to execute the command. The associated commands are SET, SADD, APPEND, RPUSH, LPUSH and so on.

● M: attribute value M, that represents a command is not automatically propagated Redis in monitor mode. The associated commands are EXEC.

● p: property value of p, and Redis Description This command functions related to the news release subscription. The associated commands are PUBLISH, PUBSUB, PSUBSCRIBE, PUNSUBSCRIBE, SUBSCRIBE, UNSUBSCRIBE so on.

● r: attribute value r, read-only, read-only stated that this is a command for acquiring relevant data, it does not modify the database. The associated commands are GET, STRLEN, EXISTS and so on.

● R: attribute value R, which is a random command description, when processing the same data sets and the same parameters, the result is random. The associated commands are SPOP, SSCAN, RANDOMKEY and so on.

● s: attribute value of s, that they can not use the command script in Lua. The associated commands are BLPOP, BRPOP, SPOP, BRPOPLPUSH and so on.

● S: attribute value S, this represents a command script used in Lua. When you use this command in Lua script, the resulting output will be sorted, that is, the resulting output is ordered. The associated commands are KEYS, SUNION, SDIFF, SINTER, SMEMBERS and so on.

● t: attribute value is t, refers to a command from the server while allowing use with outdated data. The associated commands are PING, INFO, SLAVEOF and so on.

● w: attribute value w, can be written, a write command which is described, it can modify the database. The associated commands are SET, DEL, RPUSH and so on.


The fourth step command is executed after the completion of implementation of the relevant function, the server will then do some follow-up work, and then the command returns the results to the client.

(1) during the execution of the command may take some time, the structure needs to be synchronized to redisCommand corresponding command. Modify the value of the attribute milliseconds, while the value redisCommand structure calls counter is incremented.

(2) If this server started slow query log function, so slow query log module will determine whether you need to add a slow query log for the command just executed finished.

(3) If this server enabled AOF persistence function, then the AOF persistence module will complete execution of this command requests write AOF buffer.

(4) If there are other servers from this server is the current data synchronization backup, the command this server will just executing the request forwarded from the server to give it connected.

After the completion of the processing server-related follow-up, will call the command processor reply, then the client socket becomes writable state. Return result server processor will invoke the command responses stored in the client protocol format output buffer is sent to the client

When the command processor returns results successfully reply sent to the client, it will remove the client state of the output buffer, the request for the next command to free up space

Step five client receives the command result returned by the server OK, then display the results to the user.

Guess you like

Origin www.cnblogs.com/undefined22/p/12591422.html