A Redis cluster proxy mode instruction response method, device and electronic equipment

By using a private connection between the proxy node and the Redis node, the target command is located and sent, solving the command response problem of the publish-subscribe mechanism in the Redis cluster proxy mode, and realizing effective command execution and result feedback.

CN114281410BActive Publication Date: 2026-01-02BEIJING KINGSOFT CLOUD NETWORK TECH CO LTD
View PDF 1 Cites 0 Cited by

Patent Information

Application Number
CN202111403198.4
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-11-24
Publication Date
2026-01-02
Estimated Expiration
2041-11-24

AI Technical Summary

Technical Problem

In Redis cluster proxy mode, existing technologies cannot effectively implement the command response of the publish-subscribe mechanism, especially Codis, which lacks support for related commands such as publish, subscribe, unsubscribe, modal subscription, and unmodal subscription.

Method used

The proxy node uses a private connection with the target Redis node to locate the target channel on the target Redis node and sends target commands to execute corresponding operations, including subscribing, unsubscribing, publishing messages, and pattern subscriptions. It uses hash calculations to determine the slot value and finds the target node according to the mapping relationship, ensuring the response and result feedback of the commands.

Benefits of technology

It implements command response to the publish/subscribe mechanism in Redis cluster proxy mode, ensuring effective communication and operation execution between the client and Redis nodes, and provides a feedback mechanism for operation results.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114281410B_ABST
    Figure CN114281410B_ABST
Patent Text Reader

Abstract

The embodiment of the application provides a Redis cluster proxy mode instruction response method and device and electronic equipment, and relates to the technical field of computers. The specific implementation scheme is as follows: when receiving a target instruction for a publish / subscribe mechanism sent by any client, determining a target channel indicated by the target instruction; finding a target Redis node where the target channel is located; sending the target instruction to the target Redis node through a private connection with the target Redis node having a specified state, so that the target Redis node executes an operation corresponding to the target instruction after receiving the target instruction. Thus, through the present scheme, the instruction response about the publish / subscribe mechanism in the Redis cluster proxy mode can be realized.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of computer technology, and in particular to a command response method, apparatus, and electronic device for a Redis cluster proxy mode. Background Technology

[0002] Redis (Remote Dictionary Server) is a non-relational database, belonging to the key-value database storage system.

[0003] Redis's publish / subscribe (pub / sub) mechanism consists of three parts: publishers, subscribers, and channels. Publishers and subscribers are clients, while channels are the Redis server. Specifically, a publisher sends a message to a channel, and subscribers to that channel can receive the message.

[0004] With the development of Internet technology, the cluster proxy mode of using Codis (a distributed Redis solution) to manage Redis distributed clusters is becoming increasingly popular. Codis acts as an intermediary proxy, and for upper-layer applications, there is no significant difference between connecting to Codis-Proxy (the proxy node) and connecting to the native Redis node.

[0005] Therefore, how to implement command responses related to the publish / subscribe mechanism in Redis cluster proxy mode is an urgent problem to be solved. Summary of the Invention

[0006] The purpose of this invention is to provide a command response method, apparatus, and electronic device for Redis cluster proxy mode, so as to realize command response regarding the publish-subscribe mechanism in Redis cluster proxy mode. The specific technical solution is as follows:

[0007] In a first aspect, embodiments of the present invention provide a command response method for a Redis cluster proxy mode, applied to a proxy node, wherein the proxy node communicates with both the client and each Redis node; the method includes:

[0008] When a target instruction for a publish-subscribe mechanism is received from any client, the target channel indicated by the target instruction is determined.

[0009] Locate the target Redis node where the target channel is located;

[0010] sending the target instruction to the target Redis node through a private connection with the target Redis node in a specified state, so that the target Redis node performs an operation corresponding to the target instruction after receiving the target instruction.

[0011] Optionally, the finding of the target Redis node where the target channel is located comprises: performing hash calculation on a key of the target channel to obtain a target slot value.

[0012] The target Redis node corresponding to the target slot value is determined as the target Redis node from a pre-established mapping relationship between each slot value and a Redis node.

[0013] In the mapping relationship, the Redis node corresponding to any slot value is a node where a channel corresponding to the slot value exists, and the channel corresponding to the slot value is a channel whose key can be hashed to obtain the slot value.

[0014] Optionally, the target Redis node performing the operation corresponding to the target instruction comprises:

[0015] If the target instruction is a subscription instruction, a client sending the target instruction is subscribed to the target channel.

[0016] If the target instruction is a message publishing instruction, a message carried in the message publishing instruction is published to a client that has subscribed to the target channel.

[0017] If the target instruction is a cancel subscription instruction, the subscription to the target channel is canceled for the client sending the target instruction.

[0018] Optionally, the method further comprises:

[0019] receiving an operation result of the target instruction fed back by the target Redis node;

[0020] After receiving the operation result, the operation result is transmitted to the client sending the target instruction.

[0021] Optionally, before sending the target instruction to the target Redis node through a private connection with the target Redis node in a specified state, the method further comprises:

[0022] detecting whether there is a private connection with the target Redis node in a specified state;

[0023] If not, a private connection between the proxy node and the target Redis node is established, and the established private connection is set to the specified state.

[0024] In a second aspect, the embodiments of the present application provide a Redis cluster proxy mode instruction response apparatus, applied to a proxy node, the proxy node being in communication with a client and each Redis node respectively; the apparatus comprises:

[0025] a receiving module, configured to determine a target channel indicated by a target instruction sent by any client for a publish / subscribe mechanism when the target instruction is received;

[0026] a searching module, configured to search for a target Redis node where the target channel is located;

[0027] a sending module, configured to send the target instruction to the target Redis node through a private connection with the target Redis node having a specified state, so that the target Redis node performs an operation corresponding to the target instruction after receiving the target instruction.

[0028] Optionally, the searching module comprises:

[0029] a calculating sub-module, configured to perform hash calculation on a key of the target channel to obtain a target slot value;

[0030] a determining sub-module, configured to determine a Redis node corresponding to the target slot value as the target Redis node from a mapping relationship between each slot value and a Redis node established in advance;

[0031] In the mapping relationship, a Redis node corresponding to any slot value is a node where a channel corresponding to the slot value exists, and the channel corresponding to the slot value is a channel where the slot value can be obtained by performing hash calculation on a key.

[0032] Optionally, the target Redis node performing the operation corresponding to the target instruction comprises:

[0033] if the target instruction is a subscribe instruction, subscribing to the target channel for the client sending the target instruction;

[0034] if the target instruction is a message publish instruction, publishing a message carried in the message publish instruction to the client having subscribed to the target channel;

[0035] if the target instruction is a cancel subscribe instruction, canceling the subscription to the target channel for the client sending the target instruction.

[0036] Optionally, the apparatus further comprises:

[0037] a feedback module, configured to receive an operation result of the target instruction fed back by the target Redis node;

[0038] a transmission module, configured to transmit the operation result to a client sending the target instruction after receiving the operation result.

[0039] Optionally, the apparatus further comprises:

[0040] a detection module, configured to detect whether a private connection with a specified state exists between the target Redis node before the sending module sends the target instruction to the target Redis node through the private connection with the specified state between the target Redis node and the proxy node;

[0041] a establishing module, configured to establish a private connection between the proxy node and the target Redis node if the private connection does not exist, and set the established private connection as the specified state.

[0042] In a third aspect, an electronic device is provided, comprising a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory complete mutual communication through the communication bus;

[0043] the memory is configured to store a computer program;

[0044] the processor is configured to execute the program stored on the memory, and implement the steps of the instruction response method of the Redis cluster proxy mode according to any of the above embodiments.

[0045] In a fourth aspect, a computer readable storage medium is provided, and the computer readable storage medium stores a computer program, and the computer program is executed by a processor to implement the steps of the instruction response method of the Redis cluster proxy mode according to any of the above embodiments.

[0046] In a fifth aspect, a computer program product containing instructions is provided, and when the computer program product is executed on a computer, the computer is caused to execute the steps of the instruction response method of the Redis cluster proxy mode according to any of the above embodiments.

[0047] Embodiments of the present application have the following beneficial effects:

[0048] It can be seen that, in the scheme, for the target instruction of the publish / subscribe mechanism, the characteristics of the cluster proxy mode are considered, the proxy node first finds the target Redis node where the target channel indicated by the target instruction is located, and then sends the target instruction to the target Redis node through the private connection with the target Redis node having a specified state, so that the target Redis node executes the operation corresponding to the target instruction. Therefore, through the scheme, the instruction response of the publish / subscribe mechanism in the Redis cluster proxy mode can be realized.

[0049] Of course, implementing any product or method of the present application does not necessarily require all the advantages described above to be achieved simultaneously. BRIEF DESCRIPTION OF DRAWINGS

[0050] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the drawings needed to be used in the embodiments or prior art description will be briefly introduced as follows. Obviously, the drawings in the following description are only some embodiments of the present application, and other embodiments can also be obtained by those skilled in the art based on these drawings.

[0051] Figure 1 An architecture diagram of a Redis cluster proxy mode provided by an embodiment of the present application;

[0052] Figure 2 A flowchart of an instruction response method of a Redis cluster proxy mode provided by an embodiment of the present application;

[0053] Figure 3 Another flowchart of an instruction response method of a Redis cluster proxy mode provided by an embodiment of the present application;

[0054] Figure 4 Still another flowchart of an instruction response method of a Redis cluster proxy mode provided by an embodiment of the present application;

[0055] Figure 5 A structural schematic diagram of an instruction response device of a Redis cluster proxy mode provided by an embodiment of the present application;

[0056] Figure 6 A structural schematic diagram of an electronic device provided by an embodiment of the present application. DETAILED DESCRIPTION

[0057] With reference to the accompanying drawings, the technical solutions in the embodiments of the present application will be described clearly and completely. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments of the present application. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art based on the present application are within the scope of the present application.

[0058] In the related art, in the publish / subscribe mechanism of Redis, when a client subscribes to a certain channel, the Redis node needs to bind the channel and the client subscribing to the channel, so that the client subscribing to the channel can receive the message published through the channel. When the client sends a message to a certain channel, Redis first finds the node with the channel as the key in the pubsub_channels dictionary in the structure Redis-Server (Redis service, i.e. Redis node), traverses the values of the node, i.e. traverses all clients subscribing to the channel, and then sends the message to the clients.

[0059] With the development of Internet technology, the cluster proxy mode of using Codis to manage Redis distributed clusters is becoming more and more popular. Codis is a distributed Redis solution. There is no obvious difference between connecting to Codis-Proxy and connecting to the original Redis-Server for the upper-layer client. Codis handles the forwarding of requests, non-stop data migration and other work. All the things behind are transparent to the front client, which can be simply considered as connecting to an infinite Redis service in memory. However, there is no support for publish, subscribe, unsubscribe, pattern subscribe and unsubscribe in Codis, which leads to the fact that users cannot implement the instruction response of the publish / subscribe mechanism in this cluster proxy mode.

[0060] Based on the above, in order to implement the instruction response of the publish / subscribe mechanism in the Redis cluster proxy mode, the embodiments of the present application provide a method and device for responding to instructions in the Redis cluster proxy mode and an electronic device.

[0061] Next, a method for responding to instructions in the Redis cluster proxy mode provided by the embodiments of the present application will be introduced.

[0062] The instruction response method of the Redis cluster proxy mode provided by the embodiment of the application is applied to a proxy node, which communicates with a client and respective Redis nodes. The main function of the proxy node is to receive a redis command submitted by the client, distribute the command to a back-end Redis-Server cluster, collect execution results of the respective commands, and finally return the execution results to the client. In a specific application, as shown in Figure 1 The client accesses each Codis-Proxy through a virtual IP (Virtual IP), and forwards a request to the back-end Redis-Server through the Codis-Proxy.

[0063] In addition, for a user publishing and subscribing scenario, the client serves as a medium for a user to publish and / or subscribe to a message, and communicates with a publishing and subscribing server. The client can be in a publisher identity and / or a subscriber identity, and publishes and / or subscribes to a message through a channel. As a publisher, the client can publish a message to multiple channels; as a subscriber, the client can subscribe to multiple channels and receive messages from the channels.

[0064] In a structure body client representing the client, there is an attribute pubsub_channels, which indicates all channels to which the client subscribes, and is a dictionary type, which is implemented by a hash table. Each element in the hash table contains a key-value pair and a pointer to the next element. A node is inserted into the hash table each time a subscription is made, the key indicates a subscribed channel, and the value is empty. Then, in a structure body Redis-Server representing the server end, there is also an attribute pubsub_channels, which indicates all channels in the server end and clients that subscribe to the channel, and is also a dictionary type. When a node is inserted, the key indicates a channel, and the value is a linked list of all clients that subscribe to the channel.

[0065] The instruction response method of the Redis cluster proxy mode provided by the embodiment of the application can include the following steps:

[0066] When a target instruction for a publishing and subscribing mechanism sent by any client is received, a target channel indicated by the target instruction is determined;

[0067] A target Redis node in which the target channel is located is found;

[0068] The target instruction is sent to the target Redis node through a private connection with the target Redis node in a specified state, so that the target Redis node performs an operation corresponding to the target instruction after receiving the target instruction.

[0069] In the scheme provided by the embodiment of the present application, for the target instruction of the publish / subscribe mechanism, the proxy node first finds the target Redis node where the target channel indicated by the target instruction is located, and then sends the target instruction to the target Redis node through the private connection with the target Redis node having a specified state, so that the target Redis node performs an operation corresponding to the target instruction. Therefore, the instruction response about the publish / subscribe mechanism in the Redis cluster proxy mode can be implemented through the scheme.

[0070] The instruction response method of the Redis cluster proxy mode provided by the embodiment of the present application will be described below with reference to the accompanying drawings.

[0071] As shown in the figure, the instruction response method of the Redis cluster proxy mode provided by the embodiment of the present application can include the following steps: Figure 2

[0072] S201, when receiving a target instruction for the publish / subscribe mechanism sent by any client, determining a target channel indicated by the target instruction;

[0073] In the embodiment, the target instruction for the publish / subscribe mechanism can include a subscribe instruction, an unsubscribe instruction, a message publish instruction, a psubscribe instruction and a punsubscribe instruction, etc. The subscribe instruction is used to subscribe a channel for a client; the unsubscribe instruction is used to cancel the subscription of a channel for a client; the message publish instruction is used to publish a message through a channel to a client subscribed to the channel; the psubscribe instruction is used to filter out a channel matching a mode for a client to subscribe according to the mode; and the punsubscribe instruction is used to filter out a channel matching a mode for a client to cancel the subscription according to the mode. It should be noted that the psubscribe instruction can also belong to the subscribe instruction in a broad sense, that is, the instruction for subscribing to a type of channel.

[0074] It should be noted that the mode matching function allows a client to subscribe to a channel conforming to a certain mode, and Redis uses the psubscribe instruction to subscribe to all channels conforming to a certain mode, and “*” represents the mode, which can be replaced by any value.

[0075] ​The manner of determining the target channel indicated by the target instruction can include: determining the target channel indicated by the target instruction according to a parameter carried in the instruction. For example, if the instruction is "subscirbe mongo", the instruction is a subscription instruction, and the target channel indicated by the instruction is mongo. For example, if the instruction is "psubscirbe b*", the instruction is a pattern subscription instruction, and the target channel indicated by the instruction is all channels matched with "b*", such as bK, bn and all channels starting with "b".

[0076] S202, searching for a target Redis node where the target channel is located;

[0077] It can be understood that, since there are multiple redis nodes in Codis, and at least one channel can exist in each redis node, after the target channel indicated by the target instruction is determined through step S201, the target Redis node where the target channel is located needs to be searched, so that the received target instruction can be sent to the target Redis node where the target channel is located, so that the target Redis node can perform related operations on the target channel. Different channels can represent different message types. Redis refers to message types as channels, i.e., channels. When a publisher sends a specific type of message to Redis-Server through a publish command, all clients subscribing to the message type will receive the message.

[0078] Optionally, in an implementation manner, the searching for the target Redis node where the target channel is located includes steps A1-A2:

[0079] A1, performing hash calculation on a key of the target channel to obtain a target slot value;

[0080] In Codis, the number of slots can be set according to the number of Redis nodes, for example, set to 1024, 2048 or 4096. Since Codis maintains the mapping relationship between slot values and Redis nodes in memory, which slot value is managed by which Redis node is determined after the redis cluster is started, therefore, in order to find the Redis node where the target channel is located, the target slot value can be calculated first. Since the data in the Redis database is stored in the form of key-value pairs, in the structure body client representing the client, the dictionary with the attribute pubsub_channels records all the channels subscribed by the client, the key represents the subscribed channel, and the value is empty. Therefore, the key of the target channel is hashed to obtain the key value of the target channel in the dictionary with the attribute pubsub_channels, and then the key value is hashed.

[0081] For example, if there are 16384 slots in Codis, these slots are distributed in all Redis nodes, the key of the target channel is hashed, that is, the crc16 algorithm is executed, and then 16384 is taken as the remainder, so that a number less than 16384 is obtained, which is the slot value.

[0082] A2, determine the Redis node corresponding to the target slot value as the target Redis node from the pre-established mapping relationship between the slot values and the Redis nodes; wherein in the mapping relationship, the Redis node corresponding to any slot value is the node where the channel corresponding to the slot value exists, and the channel corresponding to the slot value is the channel whose key can be hashed to obtain the slot value.

[0083] Since Codis is a proxy middleware, the mapping relationship between the slot values and the Redis nodes is saved in memory, therefore, after the target slot value is calculated through step A1, the target Redis node can be determined according to the mapping relationship between the slot value and the Redis node, and the target Redis node is the node where the target channel is located.

[0084] S203, send the target instruction to the target Redis node through a private connection with the target Redis node having a specified state, so that the target Redis node executes the operation corresponding to the target instruction after receiving the target instruction.

[0085] The private connection is an independent communication connection between the proxy node and the Redis node for the publish / subscribe instruction. The specified state is a publish / subscribe state of the private connection after the private connection for the publish / subscribe instruction is established. After the target Redis node where the target channel is located is found, the proxy node can forward the target instruction to the target Redis node through the private connection with the specified state, so that the target Redis node can perform an operation corresponding to the target instruction.

[0086] In addition, it should be noted that the number of target channels indicated by the target instruction can be one or multiple, which is reasonable. If the number of target channels indicated by the target instruction is multiple, the proxy node will calculate the slot value for each target channel after receiving the target instruction sent by the client, so as to obtain multiple target Redis nodes where the multiple target channels are located. At this time, if the multiple target Redis nodes where the multiple target channels are located are different, the target instruction will be split according to different channels when the target instruction is sent to each target Redis node through the private connection between the proxy node and each target Redis node, so that each target Redis node can subscribe to the corresponding target channel for the client.

[0087] For example, if the target instruction is "subscirbe mongo redis", the keys of the target channels "mongo" and "redis" will be hashed to obtain the slot value. If the values are different, the target instruction will be split into two instructions and routed to different target Redis nodes on the server side. The split instruction is, for example, "subscribe mongo" and "subscribe redis". The target Redis node where the target channel "mongo" is located can accept the subscribe instruction "subscribe mongo", so as to subscribe to the target channel "mongo" for the client. The target Redis node where the target channel "redis" is located can accept the subscribe instruction "subscribe redis", so as to subscribe to the target channel "redis" for the client.

[0088] The target Redis node performs an operation corresponding to the target instruction, including steps B1-B3:

[0089] B1, if the target instruction is a subscribe instruction, the target channel is subscribed to for the client sending the target instruction;

[0090] Exemplarily, the manner of subscribing the target channel for the client sending the target instruction can include: inserting a node into a dictionary with the attribute of pubsub_channels in the target redis node, wherein the key represents the channel and the value is the client subscribing to the channel. Thus, the binding between the target channel and the client sending the target instruction is achieved, i.e., the target channel is subscribed for the client sending the target instruction.

[0091] B2, if the target instruction is a message publishing instruction, publishing the message carried in the message publishing instruction to the client subscribing to the target channel;

[0092] Exemplarily, the manner of publishing the message carried in the message publishing instruction to the client subscribing to the target channel can include: after finding the target redis node where the target channel is located, first finding the node with the key of the target channel in the dictionary with the attribute of pubsub_channels in the target Redis node, traversing the value of the node, i.e., traversing all the clients subscribing to the target channel, and then sending the message carried in the message publishing instruction to the clients.

[0093] B3, if the target instruction is a cancel subscription instruction, canceling the subscription about the target channel for the client sending the target instruction.

[0094] Exemplarily, the manner of canceling the subscription about the target channel for the client sending the target instruction can include: deleting the node with the key of the target channel and the value of the client sending the target instruction in the dictionary with the attribute of pubsub_channels in the target redis node. Thus, the unbinding between the target channel and the client sending the target instruction is achieved, i.e., the subscription about the target channel is canceled for the client sending the target instruction.

[0095] In addition, the target instruction can also be a pattern subscription instruction and a cancel pattern subscription instruction. When the target instruction is the pattern subscription instruction, the operation performed is similar to the operation performed by the subscription instruction in step B1; when the target instruction is the cancel pattern subscription instruction, the operation performed is similar to the operation performed by the cancel subscription instruction in step B3; the difference lies in that the subscription and cancel subscription of the target channel in batches are achieved by traversing the node containing the pattern of the channel in the dictionary with the attribute of pubsub_patterns (publish / subscribe pattern). Here, no longer be described.

[0096] In the scheme provided by the embodiment of the application, for a target instruction of a publish / subscribe mechanism, considering the characteristics of the cluster proxy mode, the proxy node first finds a target Redis node where a target channel indicated by the target instruction is located, and then sends the target instruction to the target Redis node through a private connection with the target Redis node having a specified state, so that the target Redis node performs an operation corresponding to the target instruction. Therefore, the scheme can realize instruction response of the publish / subscribe mechanism in the Redis cluster proxy mode.

[0097] Optionally, in another embodiment of the application, in the step of Figure 2 Based on the embodiment, as Figure 3 indicated, the method further includes steps S301-S302:

[0098] S301, receiving an operation result of the target instruction fed back by the target Redis node;

[0099] It can be understood that after the target Redis node performs an operation corresponding to the target instruction, the operation result can be fed back to the client sending the target instruction to notify the client whether the relevant instruction is successfully executed. The operation result can include information such as a subscribed channel name and a channel number.

[0100] For example, if the target instruction is a subscribe instruction, after the target Redis node performs the subscribe instruction for the client sending the target instruction to subscribe to a target channel indicated by the target instruction, the target Redis node feeds back the operation result of the subscription to the proxy node through the private connection between the target Redis node and the proxy node. The operation result received by the proxy node includes a currently subscribed channel name and a channel number.

[0101] For example, if the target instruction is a cancel subscription instruction, after the target Redis node performs the cancel subscription instruction for the client sending the target instruction to cancel subscription to a target channel indicated by the target instruction, the target Redis node feeds back the operation result of the cancel subscription to the proxy node through the private connection between the target Redis node and the proxy node. The operation result received by the proxy node includes a currently subscribed channel name and a channel number.

[0102] For example, if the target instruction is a message publish instruction, after the target Redis node performs the message publish instruction for the client subscribed to the target channel to publish a message carried in the message publish instruction, the target Redis node feeds back the operation result of the message publish to the proxy node through the private connection between the target Redis node and the proxy node. The operation result received by the proxy node includes a client name and a number of subscribers receiving the message.

[0103] S302, after receiving the operation result, transmitting the operation result to the client sending the target instruction.

[0104] It can be understood that, after receiving the operation result of the target instruction fed back by the target redis node, the agent node can make the user who operates the client sending the publish / subscribe related instruction to view the operation result, so as to know the execution of the related instruction, by transmitting the result to the client sending the target instruction.

[0105] In addition, after receiving the operation result sent by the agent node, the client can also record the result of the related subscription operation in the client.

[0106] For example, if the target instruction is a subscription instruction, after receiving the message that the target redis node successfully subscribes to the target channel for the client, the client inserts a node (key-value pair) into the dictionary with the local attribute pubsub_channels, the key represents the subscribed channel, and the value is empty. Thus, the binding between the client and the target channel is achieved, which facilitates subsequent reception of the message published by the target channel.

[0107] For example, if the target instruction is an unsubscription instruction, after receiving the message that the target redis node successfully unsubscribes the target channel for the client, the client deletes the node with the target channel as the key in the dictionary with the attribute pubsub_channels in the client sending the target instruction. Thus, the unbinding between the client and the target channel is achieved, and the subsequent message published by the target channel is no longer received.

[0108] It can be seen that, through the embodiment, after the target redis node executes the operation corresponding to the target instruction, the operation result of the target instruction is fed back to the client sending the target instruction through the agent node, so that the user who operates the client sending the target instruction can view the corresponding operation result to know the execution of the target instruction.

[0109] Optionally, in another embodiment of the application, as shown in Figure 4 Before the target instruction is sent to the target Redis node through the private connection with the target Redis node having a specified state, the method further includes steps S401-S402:

[0110] S401, detecting whether there is a private connection with the target Redis node having a specified state;

[0111] In the embodiment, the specified state is a publish / subscribe state of the private connection established for the publish / subscribe instruction. Since the subscribe operation of Redis is blocking, once the client subscribes to a certain channel or pattern through the proxy node, the private connection between the proxy node and the target redis node will be in the publish / subscribe state until the exit.

[0112] When the private connection between the proxy node and the target redis node has the publish / subscribe state, the proxy node can send the target instruction to the target redis node through the private connection. Therefore, before the proxy node sends the target instruction to the target Redis node, it can be detected whether the private connection with the specified state exists between the proxy node and the target Redis node.

[0113] For example, the way to detect whether the private connection with the specified state exists between the proxy node and the target Redis node can be to find the session_flag of the private connection. If the state of the session_flag is the publish / subscribe state, the private connection with the specified state exists between the proxy node and the target Redis node.

[0114] S402, if not, establish the private connection between the proxy node and the target Redis node, and set the established private connection to the specified state.

[0115] If the private connection with the specified state does not exist between the proxy node and the target redis, the private connection between the proxy node and the target Redis node needs to be established. The private connection is a special instruction, i.e. a connection used independently by the publish / subscribe instruction. After the private connection is established, the established private connection is set to the specified state. When the publish / subscribe instruction sent by the client is received subsequently, the target instruction can be sent to the target Redis node through the private connection with the specified state between the proxy node and the target redis node.

[0116] For example, the way to set the established private connection to the specified state can be to create a session after establishing the private connection between the proxy node and the target Redis node, and set the session_flag of the private connection to the publish / subscribe state, i.e. to set the established private connection to the specified state.

[0117] In addition, the session also records the channel name and the number of channels subscribed through the private connection. When the number of channels subscribed through the private connection becomes 0, the private connection will be closed.

[0118] It can be seen that the scheme provided in the embodiment can ensure that the agent node can send the target instruction to the target Redis node through the private connection with the target Redis node having a specified state, thereby providing an implementation basis for the response of the instruction about the publish / subscribe mechanism.

[0119] In order to better illustrate the content of the embodiment of the application, the response process of the instruction response method of the Redis cluster agent mode is introduced below in combination with five specific examples.

[0120] Example 1: The agent node receives the subscribe instruction "subscribe redis mongo" sent by the client, and processes as follows:

[0121] 1. According to the channels redis and mongo indicated by the subscribe instruction, the keys of the channels redis and mongo are respectively hashed to obtain corresponding slot values, so that the redis nodes where the channels redis and mongo are located are found according to the mapping relationship between the slot values and the redis nodes. If the redis nodes where the two channels are located are the same, a private connection is established between the agent node and the redis node where the channels redis and mongo are located.

[0122] If the redis nodes where the two channels are different, the subscribe instruction is split into "subscribe mongo" and "subscribe redis", and then a private connection is established with the redis node where each channel is located. So that the Redis node where the channel mongo is located can accept the subscribe instruction "subscribe mongo", thereby subscribing the channel mongo for the client; the Redis node where the channel "redis" is located can accept the subscribe instruction "subscribe redis", thereby subscribing the channel redis for the client.

[0123] After the private connection is established, a session is created, the session_flag of the private connection is marked as a publish / subscribe state, and the subscribe instruction or the split subscribe instruction is sent to the corresponding redis node through the private connection.

[0124] 2. Taking the example that the channels redis and mongo are located in the same redis node, the foregoing redis node returns the result to the client after executing the subscribe instruction:

[0125] "subscribe"

[0126] "redis"

[0127] (integer)1

[0128] “subscribe”

[0129] “mongo”

[0130] (integer)2

[0131] Wherein, the number after integer indicates the number of currently subscribed channels. If there is a failure in the returned result, the private connection is closed; otherwise, the channel name and the number of channels subscribed in the session are recorded, and when the number of channels recorded in the session is 0, the private connection is closed.

[0132] Example two: on the basis of example one, the proxy node receives the unsubscribe instruction “unsubscribe redis” sent by the client, and processes as follows:

[0133] 1. Determine whether the session_flag is a publish / subscribe state.

[0134] 2. If yes, the proxy node sends the unsubscribe instruction to the redis node where the channel redis is located through the private connection, so that the aforementioned redis node returns the result to the client after executing the unsubscribe instruction:

[0135] “unsubscribe”

[0136] “redis”

[0137] (integer)1

[0138] Example three: on the basis of example one, the proxy node receives the unsubscribe instruction “unsubscribe” sent by the client, and processes as follows:

[0139] 1. Determine whether the session_flag is a publish / subscribe state.

[0140] 2. If yes, since there is no channel indicated in the unsubscribe instruction, the slot value of the key of each channel is calculated according to the channel name cached in the session, i.e. “redis” and “mongo”, each redis node where each channel is located is found through each slot value, and then the unsubscribe instruction is forwarded to each redis node where each channel is located.

[0141] 3. Each redis node returns the result to the client after executing the unsubscribe instruction:

[0142] “unsubscribe”

[0143] “redis”

[0144] (integer) 1

[0145] “unsubscribe”

[0146] “mongo”

[0147] (integer) 0

[0148] At this time, the number of channels recorded in the session is 0, the private connection is closed, and the session_flag flag is released.

[0149] Example Four: Based on example one, the proxy node receives the unsubscribe instruction “unsubscribe redis kk mongo” sent by the client, and processes as follows:

[0150] 1. Determine whether the session_flag is in the publish / subscribe state.

[0151] 2. If so, the proxy node sends the unsubscribe instruction to the redis node where the channel redis, the channel kk, and the channel mongo are located through the private connection described above, so that the aforementioned redis node returns the result to the client after executing the unsubscribe instruction:

[0152] “unsubscribe”

[0153] “redis”

[0154] (integer) 1

[0155] “unsubscribe”

[0156] “kk”

[0157] (integer) 1

[0158] “unsubscribe”

[0159] “mongo”

[0160] (integer) 0

[0161] It can be understood that since there is no subscription channel kk in the current session, the number of subscribed channels does not change after executing the instruction “unsubscribe kk”. When the number of channels recorded in the session is 0, the private connection is closed, and the session_flag flag is released.

[0162] Example five: on the basis of example one, the agent node receives the message publishing instruction "publish redis hello" sent by the client, and is processed as follows:

[0163] 1. The agent node regularly acquires information of all channels and pattern channels subscribed by the clients.

[0164] 2. The information of all channels and pattern channels subscribed by the clients is matched with the channel indicated by the message publishing instruction, so as to find all clients subscribing to the channel redis.

[0165] 3. The agent node publishes the message "hello" to all clients subscribing to the channel redis through the redis node where the channel redis is located, and returns the client name and quantity receiving the message to the client sending the message publishing instruction.

[0166] It can be seen that through the scheme, the instruction response about the publish / subscribe mechanism in the Redis cluster agent mode can be realized.

[0167] Corresponding to the above method embodiment, the embodiment of the application further provides a Redis cluster agent mode instruction response device, applied to an agent node, the agent node being in communication with a client and each Redis node; as shown in the figure, the device comprises: Figure 5

[0168] The receiving module 510 is configured to determine a target channel indicated by a target instruction sent by any client and directed to the publish / subscribe mechanism when the target instruction is received.

[0169] The finding module 520 is configured to find a target Redis node where the target channel is located.

[0170] The sending module 530 is configured to send the target instruction to the target Redis node through a private connection with the target Redis node having a specified state, so that the target Redis node performs an operation corresponding to the target instruction after receiving the target instruction.

[0171] Optionally, the finding module comprises:

[0172] The computing sub-module is configured to perform hash calculation on the key of the target channel to obtain a target slot value.

[0173] The determining sub-module is configured to determine a Redis node corresponding to the target slot value as the target Redis node from a pre-established mapping relationship between each slot value and Redis node.

[0174] ​The mapping relationship is that any slot value corresponds to a Redis node that has a channel corresponding to the slot value, and the channel corresponding to the slot value is a channel that can obtain the slot value by hashing a key.

[0175] Optionally, the target Redis node performs an operation corresponding to the target instruction, and the operation includes:

[0176] If the target instruction is a subscription instruction, the target channel is subscribed by a client that sends the target instruction.

[0177] If the target instruction is a message publishing instruction, a message carried in the message publishing instruction is published to a client that has subscribed to the target channel.

[0178] If the target instruction is a cancel subscription instruction, the subscription to the target channel is canceled for a client that sends the target instruction.

[0179] Optionally, the apparatus further includes:

[0180] a feedback module configured to receive an operation result of the target instruction fed back by the target Redis node;

[0181] a transmission module configured to transmit the operation result to a client that sends the target instruction after receiving the operation result.

[0182] Optionally, the apparatus further includes:

[0183] a detection module configured to detect whether a private connection with a specified state exists between the target Redis node before the sending module sends the target instruction to the target Redis node through the private connection with the specified state between the target Redis node and the target Redis node;

[0184] a establishing module configured to establish a private connection between the proxy node and the target Redis node if the private connection does not exist, and set the established private connection to the specified state.

[0185] Embodiments of the application also provide an electronic device, as shown in the accompanying drawings, including a processor 601, a communication interface 602, a memory 603 and a communication bus 604, wherein the processor 601, the communication interface 602 and the memory 603 complete mutual communication through the communication bus 604, Figure 6

[0186] the memory 603 is configured to store a computer program.

[0187] ​The processor 601 is configured to implement the steps of the instruction response method of any of the Redis cluster proxy modes provided by the embodiments of the application when executing the program stored in the memory 603.

[0188] The communication bus mentioned above can be a Peripheral Component Interconnect (PCI) bus, an Extended Industry Standard Architecture (EISA) bus or the like. The communication bus can be divided into an address bus, a data bus, a control bus and the like. For the convenience of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or only one type of bus.

[0189] The communication interface is configured to communicate between the electronic device and other devices.

[0190] The memory can include a Random Access Memory (RAM) and can also include a Non-Volatile Memory (NVM), for example, at least one disk memory. Optionally, the memory can also be at least one storage device located away from the aforementioned processor.

[0191] The processor mentioned above can be a general-purpose processor, including a Central Processing Unit (CPU), a Network Processor (NP) and the like; can also be a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), a Field-Programmable Gate Array (FPGA) or other programmable logic device, a discrete gate or transistor logic device, a discrete hardware component.

[0192] In another embodiment provided by the application, a computer readable storage medium is also provided, and the computer readable storage medium stores a computer program. The computer program is executed by a processor to implement the steps of the instruction response method of any of the Redis cluster proxy modes.

[0193] In another embodiment provided by the application, a computer program product containing instructions is also provided, and when the computer program product is run on a computer, the computer is caused to execute the instruction response method of any of the Redis cluster proxy modes in the above embodiments.

[0194] In the embodiments described above, all or some of the steps can be implemented by software, hardware, firmware or any combination thereof. When implemented by software, all or some of the steps can be implemented in the form of one or more computer programs. The computer program is stored in a computer readable medium, which can be any data storage device that can store computer programs used for or produced by electronic devices. The computer readable medium includes magnetic disk, optical disk, read-only memory (ROM), flash memory and electrical carrier signal, and the like. The computer program is loaded into and executed by one or more computers to produce the functions as described in the embodiments according to the present application. The computer can be a general purpose computer, a special purpose computer, a computer network, or other programmable apparatuses. The computer program can be stored in a computer readable medium, or transmitted from one computer readable medium to another computer readable medium, such as from a website, a computer, a server or a data center to another website, computer, server or data center through wired (such as coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (such as infrared, wireless, microwave, etc.) manner. The computer readable medium can be any available medium that can be accessed by a computer or data storage device such as a server, data center, etc. that includes one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., Solid State Disk (SSD)), etc.

[0195] It should be noted that the relative terms, such as first and second, etc., are used only to differentiate one entity or operation from another entity or operation, and do not necessarily require or imply that there is any such actual relationship or order between these entities or operations. Also, the terms "comprising", "including" or any other variant thereof are intended to cover non-exclusive inclusion, so that a process, method, article or apparatus including a list of elements does not only include those elements, but also includes other elements not explicitly listed, or further includes elements inherent in such process, method, article or apparatus. Without more limitations, the element defined by the statement "comprising a" does not exclude the presence of additional identical elements in the process, method, article or apparatus including the element.

[0196] Each of the embodiments in the specification is described in a related manner, and the same or similar parts between the embodiments can be referred to each other. Each embodiment focuses on the difference from other embodiments. In particular, for the device embodiments, since they are basically similar to the method embodiments, the description is relatively simple, and the relevant parts can be referred to the part of the method embodiments.

[0197] The above merely describes the preferred embodiments of the present application, but is not used to limit the protection scope of the present application. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present application shall be included in the protection scope of the present application.

Claims

1. A method for responding to instructions in a Redis cluster proxy mode, characterized in that, The method is applied to a proxy node, which respectively communicates with a client and each Redis node, and comprises the following steps: When receiving a target instruction for a publish / subscribe mechanism sent by any client, determining a target channel indicated by the target instruction; Finding a target Redis node where the target channel is located; Sending the target instruction to the target Redis node through a private connection with the target Redis node in a specified state, so that the target Redis node performs an operation corresponding to the target instruction after receiving the target instruction; the specified state is a publish / subscribe state of the private connection after being established for a publish / subscribe instruction; When the number of channels subscribed through a private connection is 0, closing the private connection.

2. The method of claim 1, wherein, The finding of the target Redis node where the target channel is located comprises the following steps: Hashing a key of the target channel to obtain a target slot value; Determining a Redis node corresponding to the target slot value as the target Redis node from a pre-established mapping relationship between each slot value and Redis node; In the mapping relationship, a Redis node corresponding to any slot value is a node where a channel corresponding to the slot value exists, and the channel corresponding to the slot value is a channel whose key can be hashed to obtain the slot value.

3. The method according to claim 1 or 2, characterized in that, The target Redis node performing an operation corresponding to the target instruction comprises the following steps: If the target instruction is a subscribe instruction, subscribing the target channel for a client sending the target instruction; If the target instruction is a message publish instruction, publishing a message carried in the message publish instruction to a client that has subscribed to the target channel; If the target instruction is a cancel subscription instruction, canceling the subscription for the target channel for a client sending the target instruction.

4. The method according to claim 1 or 2, characterized in that, The method further comprises the following steps: Receiving an operation result of the target instruction fed back by the target Redis node; After receiving the operation result, transmitting the operation result to the client sending the target instruction.

5. The method according to claim 1 or 2, characterized in that, Before sending the target instruction to the target Redis node through a private connection with the target Redis node in a specified state, the method further comprises the following steps: Detecting whether there is a private connection with the target Redis node in the specified state; If not, establishing a private connection between the proxy node and the target Redis node, and setting the established private connection to the specified state.

6. An instruction response apparatus of a Redis cluster proxy mode, characterized by, The device is applied to a proxy node, which respectively communicates with a client and each Redis node, and comprises the following modules: A receiving module, configured to determine a target channel indicated by a target instruction for a publish / subscribe mechanism when receiving the target instruction sent by any client; A finding module, configured to find a target Redis node where the target channel is located; The sending module is configured to send the target instruction to the target Redis node through a private connection with the target Redis node in a specified state, so that the target Redis node performs an operation corresponding to the target instruction after receiving the target instruction; the specified state is a publish / subscribe state of the private connection after the private connection is established for a publish / subscribe instruction; and when the number of channels subscribed through one private connection is 0, the private connection is closed.

7. The apparatus of claim 6, wherein, The searching module comprises: The computing sub-module is configured to perform hash calculation on the key of the target channel to obtain a target slot value; The determining sub-module is configured to determine, from a pre-established mapping relationship between each slot value and a Redis node, a Redis node corresponding to the target slot value as a target Redis node; The mapping relationship is that a Redis node corresponding to any slot value is a node in which a channel corresponding to the slot value exists, and the channel corresponding to the slot value is a channel in which the key is capable of being hashed to obtain the slot value.

8. The apparatus of claim 6 or 7, wherein, The target Redis node performs an operation corresponding to the target instruction, which comprises: If the target instruction is a subscribe instruction, the target channel is subscribed by a client sending the target instruction; If the target instruction is a message publish instruction, a message carried in the message publish instruction is published to a client that has subscribed to the target channel; If the target instruction is a cancel subscribe instruction, the subscription about the target channel is canceled for the client sending the target instruction.

9. The apparatus of claim 6 or 7, wherein, The apparatus further comprises: The feedback module is configured to receive an operation result of the target instruction fed back by the target Redis node; The transmission module is configured to transmit the operation result to the client sending the target instruction after receiving the operation result.

10. The apparatus of claim 6 or 7, wherein, The apparatus further comprises: The detection module is configured to detect, before the sending module performs sending the target instruction to the target Redis node through a private connection with the target Redis node in a specified state, whether there is a private connection with the target Redis node in the specified state; The establishment module is configured to, if not, establish a private connection between the proxy node and the target Redis node, and set the established private connection to the specified state.

11. An electronic device, comprising: The apparatus comprises a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other through the communication bus; The memory is configured to store a computer program; The processor is configured to execute the program stored in the memory to implement the method steps in any one of claims 1-5.

12. A computer-readable storage medium, characterized in that, The computer readable storage medium stores a computer program, and the computer program is executed by the processor to implement the method steps in any one of claims 1-5.

Citation Information

Patent Citations

  • Method and system for implementing Redis proxy client, storage medium, and electronic equipment

    CN109347936A