Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does Redis execute orders?

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "how to execute orders in Redis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Handle new connections

Redis creates a loop event in the initServer () function and calls the acceptTcpHandler and acceptUnixHandler functions (both in the networking.c file) to handle the received TCP connection and Unix's Sockets connection. These two functions also call the acceptCommonHandler () function, where the createClient () function is called to create a new client object that represents a new client connection.

What exactly does the createClient () function do?

First, memory is allocated for the variable c, then the Socket connection is set to non-blocking, and TCP is set to have no latency. Then a File loop event (aeCreateFileEvent) is created to call the readQueryFromClient function. By default, the newly created client connects to the first database of the server (coded as 0). Finally, you need to set various properties and states of the client.

Read a client command

We just mentioned the readQueryFromClient function, which is used to read commands from the client, as can be seen from the name. Let's take a look at the specific implementation of the function.

Redis reads the command into the buffer first, and the maximum read size is PROTO_IOBUF_LEN (1024-16) bit. The processInputBufferAndReplicate () function handles both cases where the client sends a command to the server and the master node sends a command to the slave node, but in the end you need to call the processInputBuffer () function.

The processInputBuffer () function will first determine whether the client is normal, and if the connection is broken or the client is blocked, it will immediately stop processing the command without doing useless work. Then the commands (including parameters) that can be executed by the corresponding Redis are generated according to the read request. The processInlineBuffer () and processMultibulkBuffer () functions are called for different request types. After the command is generated, it is executed by the processCommand () (in the server.c file) function, and if C_OK is returned, the client is reset and wait for the next command. If C_ERR is returned, the client is destroyed (such as executing the QUIT command).

The processCommand () function looks for a command from the command table loaded when Redis starts, and then checks the execution permissions of the command.

If it is cluster, it will determine whether key belongs to the current master and does not belong to the redirection information that needs to be returned.

If there is not enough memory, you also need to determine whether there is any memory that can be freed. If not, you cannot execute the command and return an error message.

Next, we will determine some situations in which write commands cannot be received:

The server cannot be persisted

As a master, there is not enough slave available

This server is a read-only slave, and only its master can receive write commands

In subscription mode, only the specified command can be received: (P) SUBSCRIBE / (P) UNSUBSCRIBE / PING / QUIT.

When slave and master are lost, only commands with flag "t" can be received, such as INFO,SLAVEOF, etc.

If the command does not have a CMD_LOADING flag and the current server is loading data, you cannot receive this command.

Limit the length of the lua script.

After the above various conditions have been determined, you can actually start calling the call () function to execute the command.

Execute the command and return

The argument to the call () function is of type client, and the cmd member is taken out for execution.

1 * Call the command. , /

2dirty = server.dirty

3start = ustime ()

4C-> cmd- > proc (c)

5duration = ustime ()-start

6dirty = server.dirty-dirty

7if (dirty < 0) dirty = 0

If you write a command, it will make the server "dirty", that is, the server needs to mark that some pages in memory have changed. This is very important for Redis persistence because it knows how many key are affected by this command. The command does not end after execution, and the call function does some other operations as well. Such as logging, writing AOF files, synchronizing commands to slave nodes, and so on.

As for the return value, each command has its own processing method, which we will describe later.

At this point, the process of processing commands by Redis is complete.

Later, we will give a clearer introduction to this process through specific commands.

This is the end of "how to execute orders in Redis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report