Data query method and device, electronic equipment, storage medium and product
By decoding the SCAN command in the service request, finding the hash value, concurrently scanning the redis shards, and merging the return values, it solves the cross-shard query performance bottleneck and blocking problems in the redis cluster and achieves efficient data query.
Patent Information
- Application Number
- CN202410421938.4
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-04-09
- Publication Date
- 2024-10-18
AI Technical Summary
Existing distributed system data query methods have performance bottlenecks and blocking problems in redis clusters. In particular, the KEYS command cannot query across shards, and the SCAN command can only operate one shard, making it impossible to achieve smooth query of cross-shard data.
By receiving the service request from the terminal, decoding the SCAN command, searching for the hash value in the hash mapping relationship table, using the hash value as the index of the redis shard, concurrently scanning each shard, merging the return values, and realizing cross-shard data. Query.
It improves the efficiency of data query, realizes the logical processing of SCAN commands at the agent level without using additional commands, realizes the connection to the redis shard cluster, achieves the purpose of smoothly querying data on different redis shards, and avoids performance bottlenecks. and blocking.
Smart Images

Figure CN118796910A_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of database technology, and in particular to a data query method, device, electronic device, storage medium and product. Background Art
[0002] At present, the data query method of distributed systems mainly uses the sharding algorithm to specify the specific redis shard for query, and then aggregates it at the proxy layer. Although the KEYS command of redis can query data at the specified node and aggregate all the queried data, the characteristic that the KEYS command cannot query across shards during execution will affect the performance of the redis cluster and even cause blocking. The SCAN command can only operate the data on one redis shard and cannot be easily achieved through aggregation. Therefore, the relevant distributed system data query method usually requires the use of additional commands to achieve the purpose of smoothly querying data on different redis shards specified in the redis cluster. For example, codis implements redis shard query by adding the command SLOTSSCAN, and Tencent Cloud's redis cluster implements redis shard query by providing the CLUSTER NODEA command and adding parameters to the KEYS command. Summary of the invention
[0003] The present disclosure provides a data query method, device, electronic device, storage medium and product to solve the problems in the related art.
[0004] A first aspect of the present disclosure provides a data query method, the method comprising:
[0005] Receiving a service request sent by a terminal;
[0006] Decoding a first cursor of a SCAN command in the service request to obtain a key value corresponding to the first cursor;
[0007] Searching for a hash value corresponding to the key value from a hash mapping relationship table to obtain a hash value linked list;
[0008] Using the sorting sequence number of the value in the hash value linked list as the index of the redis shard in the redis shard cluster, using the value in the hash value linked list as the second cursor of the redis shard with the same sorting sequence number, and concurrently scanning the data of each redis shard in the redis shard cluster to obtain the scanning result of each redis shard, wherein the scanning result includes a return value;
[0009] Merge the return value of each redis shard in the redis shard cluster according to the index order of the redis shards;
[0010] The combined return value is sent to the terminal.
[0011] In some embodiments of the present disclosure, searching a hash value corresponding to the key value from a hash mapping relationship table includes:
[0012] Obtain the number of the redis shards in the redis shard cluster, the number of proxies equipped with the redis shard cluster, and the performance evaluation value of the redis shard in the redis shard cluster;
[0013] Determine the module of the hash mapping relationship table based on the number of the redis shards in the redis shard cluster, the number of proxies equipped with the redis shard cluster, and the performance evaluation values of the redis shards in the redis shard cluster;
[0014] Based on the key value and the modulus of the hash mapping relationship table, a hash value corresponding to the key value is obtained.
[0015] In some embodiments of the present disclosure, obtaining the number of redis shards in the redis shard cluster, the number of proxies configured in the redis shard cluster, and the performance evaluation value of the redis shard in the redis shard cluster includes:
[0016] Obtain the number of redis shards in the redis shard cluster, the number of proxies equipped with the redis shard cluster, the performance evaluation of each redis shard, the amount of memory allocated to each redis shard, and the number of cores of the central processing unit CPU allocated to each redis shard;
[0017] Based on the performance evaluation of each redis shard, the amount of memory allocated to each redis shard, and the number of CPU cores allocated to each redis shard, determine the average performance evaluation, average memory, and average CPU cores of the redis shards in the redis shard cluster;
[0018] Based on the average performance evaluation, average memory number and average CPU core number of the redis shards in the redis shard cluster, the performance evaluation value of the redis shards in the redis shard cluster is determined.
[0019] In some embodiments of the present disclosure, before performing data scanning on each redis shard in the redis shard cluster, the data query method provided by the present disclosure further includes:
[0020] If the hash value corresponding to the key value cannot be read from the hash mapping relationship table, the preset cursor value is determined as the second cursor of each redis shard.
[0021] In some embodiments of the present disclosure, the service request includes a timestamp, the timestamp is used to indicate the order in which the service requests enter a task queue, the task queue being a queue in which the agent processes the service requests of the terminal;
[0022] The decoding of the first cursor of the SCAN command in the service request includes:
[0023] Obtain a first cursor of a SCAN command in the service request with the smallest timestamp in the service request.
[0024] In some embodiments of the present disclosure, after merging the return values of all the redis shards, the data query method provided by the present disclosure further includes:
[0025] Generate a random number with a fixed number of digits using the time random number method;
[0026] The random number with the fixed number of bits is determined as a third cursor of the service request.
[0027] In some embodiments of the present disclosure, the scan result further includes a fourth cursor;
[0028] After the random number with a fixed number of bits is generated by the time random number method, the data query method provided by the present disclosure further includes:
[0029] According to the index order of redis shards, concatenate the fourth cursors of redis shards into a linked list;
[0030] The random number with a fixed number of bits is determined as a key value, and the fourth cursors are serially connected into a linked list as a hash value to update the hash mapping relationship table.
[0031] A second aspect of the present disclosure provides a data query device, the device comprising:
[0032] A receiving unit, configured to receive a service request sent by a terminal;
[0033] A decoding unit, used for decoding a first cursor of a SCAN command in the service request to obtain a key value corresponding to the first cursor;
[0034] A search unit, used to search the hash value corresponding to the key value from the hash mapping relationship table to obtain a hash value linked list;
[0035] A data scanning unit, used to use the sorting sequence number of the value in the hash value linked list as the index of the redis shard in the redis shard cluster, use the value in the hash value linked list as the second cursor of the redis shard with the same sorting sequence number, and concurrently scan the data of each redis shard in the redis shard cluster to obtain the scanning result of each redis shard, wherein the scanning result includes a return value;
[0036] A merging unit, used to merge the return value of each redis shard in the redis shard cluster according to the index order of the redis shard;
[0037] A sending unit is used to send the combined return value to the terminal.
[0038] A third aspect of the present disclosure provides an electronic device, including:
[0039] At least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the method described in the embodiment of the first aspect of the present disclosure.
[0040] The fourth aspect embodiment of the present disclosure proposes a non-transitory computer-readable storage medium storing computer instructions, wherein the computer instructions are used to enable a computer to execute the method described in the first aspect embodiment of the present disclosure.
[0041] The fifth aspect embodiment of the present disclosure provides a computer program product, including a computer program, which implements the method described in the first aspect embodiment of the present disclosure when executed by a processor.
[0042] In summary, the present disclosure proposes a data query method, device, electronic device, storage medium and product, the method comprising: receiving a service request sent by a terminal; decoding a first cursor of a SCAN command in the service request to obtain a key value corresponding to the first cursor; searching a hash value corresponding to the key value from a hash mapping relationship table to obtain a hash value linked list; using the sorting sequence number of the value in the hash value linked list as the index of the redis shard in a redis shard cluster, using the value in the hash value linked list as a second cursor of the redis shard with the same sorting sequence number, and concurrently scanning data for each redis shard in the redis shard cluster to obtain a scan result for each of the redis shards, the scan result including a return value; merging the return value of each of the redis shards in the redis shard cluster according to the index order of the redis shards; and sending the merged return value to the terminal.
[0043] Through the scheme provided by the present disclosure, a service request sent by a terminal is received; the first cursor of the SCAN command in the service request is decoded by a proxy to obtain the key value corresponding to the first cursor; the hash value corresponding to the key value is found from the hash mapping relationship table to obtain a hash value chain table, the sorting number of the value in the hash value chain table is used as the index of the redis shard in the redis shard cluster, the value in the hash value chain table is used as the second cursor of the redis shard with the same sorting number, and data is scanned for each redis shard in the redis shard cluster concurrently, and the return value of each redis shard can be obtained, thereby improving data query efficiency. By merging the return values of all redis shards according to the index order of the redis shards, and sending the merged return value to the terminal, the SCAN command can be logically processed at the proxy level without using additional commands, and the connection of each redis shard in the redis shard cluster can be realized, thereby achieving the purpose of smoothly using the SCAN command to query data on different redis shards in the redis cluster.
[0044] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] The drawings herein are incorporated into and constitute a part of the specification, illustrate embodiments consistent with the present disclosure, and together with the description are used to explain the principles of the present disclosure, and do not constitute improper limitations on the present disclosure.
[0046] Figure 1 A schematic diagram of an application scenario provided by an embodiment of the present disclosure;
[0047] Figure 2 A flowchart of a data query method provided by an embodiment of the present disclosure;
[0048] Figure 3 A schematic diagram of the logical structure of a data query method provided in an embodiment of the present disclosure;
[0049] Figure 4 A flowchart of a method for obtaining a hash value corresponding to a key value provided in an embodiment of the present disclosure;
[0050] Figure 5 A flowchart of a method for determining a performance evaluation value of a redis shard in a redis shard cluster provided for an application example of the present disclosure;
[0051] Figure 6 A flowchart of a data query method provided for an application example of the present disclosure;
[0052] Figure 7A schematic diagram of the structure of a data query device provided in an embodiment of the present disclosure;
[0053] Figure 8 A schematic diagram of the hardware structure of an electronic device provided in an embodiment of the present disclosure. DETAILED DESCRIPTION
[0054] Embodiments of the present disclosure are described in detail below, and examples of the embodiments are shown in the accompanying drawings, wherein the same or similar reference numerals throughout represent the same or similar elements or elements having the same or similar functions. The embodiments described below with reference to the accompanying drawings are exemplary and are intended to be used to explain the present disclosure, and should not be construed as limiting the present disclosure.
[0055] At present, the data query method of distributed systems mainly uses the sharding algorithm to specify the specific redis shard for query, and then aggregates it at the proxy layer. Although the KEYS command of redis can query data at the specified node and aggregate all the queried data, the characteristic that the KEYS command cannot query across shards during execution will affect the performance of the redis cluster and even cause blocking. The SCAN command can only operate the data on one redis shard and cannot be easily achieved through aggregation. Therefore, the relevant distributed system data query method usually requires the use of additional commands to achieve the purpose of smoothly querying data on different redis shards specified in the redis cluster. For example, codis implements redis shard query by adding the command SLOTSSCAN, and Tencent Cloud's redis cluster implements redis shard query by providing the CLUSTER NODEA command and adding parameters to the KEYS command.
[0056] The following briefly introduces two solutions of data query methods in related technologies:
[0057] Solution A: Use a stand-alone redis data query method. As the most popular in-memory database, redis supports the use of wildcards when querying data, such as "?" or "*". For a stand-alone redis, when we want to obtain all the data in a certain DB in redis, we can use a command such as "keys*" to achieve it.
[0058] Solution B: Use the distributed cluster redis data query method. There are two types of redis distributed clusters. One is the decentralized redis cluster provided by the open source community, and the other is the proxy sharding cluster, such as the most commonly used codis cluster.
[0059] There is a concept of slots in the decentralized redis cluster provided by the open source community. If redis is a single node, it only has 16384 slots. However, in cluster mode, multiple nodes share 16384 slots. If the cluster has three nodes, then the three nodes will share the 16384 slots equally. When a piece of data is put in, the key will be hashed and then the corresponding slot will be found according to the hashed value and put into the corresponding node. However, if SCAN is used at this time, problems will be found, because the SCAN command needs to establish a connection, but the key to be operated may be on different shards, so the request needs to be forwarded at this time, but this is not supported in cluster mode. Different keys may be on different nodes, but SCAN can only operate data on one node. At this time, users can only go to SCAN shard by shard. Usually, users need to modify the application to obtain the connection addresses of all nodes of the open source decentralized redis cluster. If there are N nodes in the redis cluster, when controlling the connection and SCAN one by one in the code, it is necessary to reconnect N times, which is very inconvenient.
[0060] Proxy sharding cluster, taking codis as an example, also has a slot concept. The proxy gets the corresponding slot based on the crc32 algorithm % 1024. The slot is the so-called logical shard. At the same time, codis will map the corresponding logical shard to the corresponding virtual node. Each virtual node is composed of 1 master and multiple slave redis nodes. Similarly, the redis nodes of the proxy sharding cluster do not interact with each other, and requests cannot be forwarded. SCAN can only operate the data on one node. Therefore, codis has developed a new command to provide the SLOTSSCAN instruction, which can scan all keys on the specified slot, "slotsscan slotId cursor[count]". Compared with the open source SCAN command, there is one more slotId, and it is necessary to query and traverse data according to the shard id. The changes to the user code are very large, and most of the redis application clients cannot support it. At the same time, the proxy cluster of Alibaba Cloud in the industry also adopts the same method. In cluster mode, iscan is used to execute the traversal command "iscan db_idx cursor[MATCH pattern][COUNT count]" on the specified db node. Based on the SCAN command, a parameter is extended to specify db_idx. The range of db_idx is [0, nodecount]. Nodecount can be obtained through the info command or by viewing the instance topology map from the console.
[0061] The above solution has the following defects:
[0062] First, in the above solution, when the amount of data is large, the performance of using "keys*" to match queries is not ideal. In-memory databases often store massive amounts of data. Using "keys*" to match queries will traverse all the data, which takes a very long time and will block business operations. Keys operations will be prohibited in production applications.
[0063] Second, when using the two clusters in the distributed cluster redis data query method to traverse data, it is not as convenient as using the SCAN command of a single machine, and application code modification is required to continue using redis.
[0064] In order to solve the problems existing in the related art, the present disclosure proposes a data query method, receiving a service request sent by a terminal; decoding the first cursor of the SCAN command in the service request by proxy to obtain the key value corresponding to the first cursor; finding the hash value corresponding to the key value from the hash mapping relationship table to obtain a hash value chain list; using the sorting sequence number of the value in the hash value chain list as the index of the redis shard in the redis shard cluster, using the value in the hash value chain list as the second cursor of the redis shard with the same sorting sequence number, and concurrently scanning the data of each redis shard in the redis shard cluster, the return value of each redis shard can be obtained, and the data query efficiency is improved. By merging the return values of all redis shards according to the index order of the redis shards, and sending the merged return value to the terminal, the SCAN command can be logically processed at the proxy level without using additional commands, and the connection of each redis shard in the redis shard cluster can be realized, so as to achieve the purpose of smoothly using the SCAN command to query data on different redis shards in the redis cluster.
[0065] This method is applied to Figure 1 The application scenarios shown are as follows: Figure 1As shown, the application scenario includes several node devices, and the number of node devices is not limited. The node device can be a server or a terminal. This application uses the node device as a terminal as an example for introduction. The client is deployed on the terminal, wherein the client can run on the terminal in the form of a browser, or can run on the terminal in the form of an independent application (application, APP), etc. The specific presentation form of the client is not limited here. The server involved in this application can be an independent physical server, or a server cluster or distributed system composed of multiple physical servers, or a cloud server that provides basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, content delivery networks (Content Delivery Network, CDN), and big data and artificial intelligence platforms. The terminal can be a mobile phone, a computer, an intelligent voice interaction device, a smart home appliance, a vehicle-mounted terminal and an aircraft, etc., but is not limited thereto. The embodiments of this application can be applied to various scenarios, including but not limited to cloud technology, artificial intelligence, smart transportation, assisted driving, etc.
[0066] The present disclosure is further described in detail below with reference to the accompanying drawings and specific embodiments.
[0067] like Figure 2 As shown, Figure 2 The data query method provided by the embodiment of the present disclosure comprises the following steps:
[0068] Step 201, receiving a service request sent by a terminal;
[0069] In one embodiment, the number of terminals may be one, two, or more, which is not limited in the present disclosure.
[0070] In one embodiment, the terminal refers to a client.
[0071] In one embodiment, the service request refers to a request sent by the client and including a SCAN command.
[0072] In one embodiment, the accept coroutine loop may be used to receive the service request sent by the client.
[0073] In one embodiment, the receive waiting queue lock may also be used to control the service request to initialize placeholder information in the receive waiting queue, and after obtaining the result, the result information is completed and returned to the client.
[0074] In one embodiment, the receive waiting queue lock can ensure that each service request can return a result in the case of a timeout exception, such as returning an empty result in the case of a crash.
[0075] Step 202, decoding the first cursor of the SCAN command in the service request, and obtaining a key value corresponding to the first cursor;
[0076] In one embodiment, the first cursor is used to indicate the current position during the iteration process.
[0077] In one embodiment, when the first cursor parameter of the SCAN command is set to 0, it indicates that the proxy server will start a new iteration.
[0078] In one embodiment, when the cursor value returned by the proxy server to the client is 0, it indicates that the current iteration has ended.
[0079] In one embodiment, the key value corresponding to the first cursor may be obtained according to the decoding rule of the first cursor.
[0080] Specifically,
[0081] In one embodiment, the first few bits of the first cursor may be used as placeholders, and the last few bits of the first cursor may be calculated to obtain the key value corresponding to the first cursor.
[0082] Step 203, searching the hash value corresponding to the key value from the hash mapping relationship table to obtain a hash value linked list;
[0083] In one embodiment, the hash mapping relationship table maps key values to hash values through a hash function.
[0084] In one embodiment, data can be quickly accessed based on the hash mapping relationship between the hash value and the key value.
[0085] In one embodiment, the hash value linked list is a linked list obtained by serially connecting key values with the same hash value in the form of a linked list.
[0086] In one embodiment, the key value is first mapped to a hash value through a hash function, and then the hash value corresponding to the key value is searched from the hash mapping relationship table.
[0087] Step 204, using the sorting sequence number of the value in the hash value linked list as the index of the redis shard in the redis shard cluster, using the value in the hash value linked list as the second cursor of the redis shard with the same sorting sequence number, and concurrently scanning data for each redis shard in the redis shard cluster to obtain a scan result for each redis shard, wherein the scan result includes a return value;
[0088] In one embodiment, the second cursor refers to a cursor of each redis shard.
[0089] In one embodiment, different Redis shards in a Redis shard cluster store different data.
[0090] In one embodiment, if Figure 3 As shown, the information of redis shards in the redis shard cluster can be obtained from the distributed unified key-value storage (etcd).
[0091] In one embodiment, the watcher technology of Kubernetes (K8S for short) can be used to monitor newly created or deleted redis shards in real time.
[0092] In one embodiment, the proxy and all redis shards in the redis cluster maintain continuous communication through a persistent connection without multiple reconnections. When a redis shard is created or deleted, the proxy can automatically obtain and load it.
[0093] In one embodiment, once a new redis shard is created or deleted, the IP address of the node is automatically obtained, and the latest IP and role are updated in etcd in real time.
[0094] In one embodiment, the roles include a master node and a slave node.
[0095] In one embodiment, when a redis shard is created or deleted, etcd triggers the manager to update the running state information, so that updates are loaded into the cache within seconds.
[0096] In one embodiment, when the proxy restarts abnormally, etcd can quickly initialize and obtain the IP and role, and load them into the running cache.
[0097] In one embodiment, the sorting sequence number of the values in the hash value linked list is used as the index of the redis shard in the redis shard cluster, and the value in the hash value linked list is used as the second cursor of the redis shard with the same sorting sequence number, and data is scanned concurrently on each redis shard in the redis shard cluster to obtain the scanning result of each redis shard.
[0098] In one embodiment, the scan result includes a return value and a return cursor. The return value is the data scanned by the second cursor according to the current redis shard, and the return cursor is used to indicate the position of the next iteration.
[0099] Step 205, merging the return value of each redis shard in the redis shard cluster according to the index order of the redis shards;
[0100] In one embodiment, the return value of the service request may be obtained by merging the return values of all redis shards according to the index order of the redis shards.
[0101] Step 206: Send the combined return value to the terminal.
[0102] In one embodiment, the return value refers to the merged return value.
[0103] In one embodiment, by sending the merged return value to the client, the SCAN command can be logically processed at the proxy layer without using additional commands, thereby achieving the connection of the redis shards in the redis cluster.
[0104] In one embodiment, if Figure 4 As shown, searching the hash value corresponding to the key value from the hash mapping relationship table includes:
[0105] Step 401, obtaining the number of the redis shards in the redis shard cluster, the number of proxies equipped with the redis shard cluster, and the performance evaluation value of the redis shard in the redis shard cluster;
[0106] In one embodiment, the number of redis shards in the redis shard cluster can be obtained from etcd.
[0107] In one embodiment, the number of proxies equipped in the redis shard cluster can be determined according to the size of the redis shard in the redis shard cluster. For example, if the query per second (QPS) of a redis shard is 80,000, and the maximum QPS supported by a proxy is 200,000, then a proxy can support at most two redis shards.
[0108] In one embodiment, the performance evaluation value of the redis shards in the redis shard cluster can be determined by monitoring multiple performance indicators such as CPU utilization, memory usage, command execution time, network bandwidth and latency of each redis shard.
[0109] Step 402, determining the module of the hash mapping relationship table based on the number of the redis shards in the redis shard cluster, the number of proxies equipped with the redis shard cluster, and the performance evaluation values of the redis shards in the redis shard cluster;
[0110] In one embodiment, the modulus of the hash mapping relationship table can be determined by calculating the sum of the product of the number of redis shards in the redis shard cluster and the performance evaluation value of the redis shard in the redis shard cluster and the number of proxies equipped in the redis shard cluster.
[0111] In one embodiment, the size of the modulus of the hash mapping relationship table is used to indicate the number of elements that can be stored in each bucket. For example, if the modulus of the hash mapping relationship table is 5, it means that at most 4 elements can be stored in each bucket.
[0112] In one embodiment, the larger the modulus of the hash mapping relationship table, the more elements a bucket can store, and the more redis shards that can be scanned concurrently.
[0113] Step 403: Based on the key value and the modulus of the hash mapping relationship table, obtain the hash value corresponding to the key value.
[0114] In one embodiment, based on the key value and the modulus of the hash mapping relationship table, the hash value corresponding to the key value is obtained using the division and remainder method. For example, if the key value is 5 and the modulus of the hash mapping relationship table is 7, then the remainder 5 is determined as the hash value corresponding to the key value. For example, if the key value is 27 and the modulus of the hash mapping relationship table is 7, then the remainder 6 is determined as the hash value corresponding to the key value.
[0115] In one embodiment, if Figure 5 As shown, step 401 includes:
[0116] Step 501, obtaining the number of redis shards in the redis shard cluster, the number of proxies equipped in the redis shard cluster, the performance evaluation of each redis shard, the amount of memory allocated to each redis shard, and the number of cores of the central processing unit CPU allocated to each redis shard;
[0117] In one embodiment, the INFO command may be used to obtain the amount of memory allocated to each redis shard and the number of cores of the central processing unit CPU allocated to each redis shard.
[0118] Specifically, you can use the INFO MEMORY command to return the memory usage of the Redis shard, and you can use the INFOUSED_CPU_SYS command to obtain the number of CPU cores allocated to the Redis shard.
[0119] Step 502, based on the performance evaluation of each redis shard, the amount of memory allocated to each redis shard, and the number of CPU cores allocated to each redis shard, determine the average performance evaluation, average amount of memory, and average number of CPU cores of the redis shards in the redis shard cluster;
[0120] In one embodiment, the sum of the performance evaluations of all redis shards in the redis shard cluster, the sum of the memory count, and the sum of the CPU core count are determined respectively.
[0121] In one embodiment, the ratio of the sum of performance evaluations of all redis shards in the redis shard cluster to the number of redis shards in the redis shard cluster, the ratio of the sum of memory numbers to the number of redis shards in the redis shard cluster, and the ratio of the sum of CPU core numbers to the number of redis shards in the redis shard cluster are determined respectively to obtain the average performance evaluation, average memory number, and average CPU core number of the redis shards in the redis shard cluster.
[0122] Step 503: Determine the performance evaluation value of the redis shards in the redis shard cluster based on the average performance evaluation, average memory number, and average CPU core number of the redis shards in the redis shard cluster.
[0123] In one embodiment, a weighted sum method may be used to determine the performance evaluation value of a redis shard in a redis shard cluster.
[0124] In one embodiment, the calculation formula of the performance evaluation value of the redis shard in the redis shard cluster is as follows:
[0125]
[0126] Among them, C n is the number of CPU cores, C MEAN is the average number of CPU cores in the redis shard cluster, M n is the number of redis shards in the redis shard cluster, M MEAN is the average memory size of the redis shard cluster, G MEAN is the average performance evaluation of the redis shard cluster, N is the number of proxies equipped with the redis shard cluster, and G is the performance evaluation value of the redis shard in the redis shard cluster.
[0127] In one embodiment, the weighting factors 0.3 and 0.7 in the above formula (1) can be adjusted according to different requirements. For example, if the performance evaluation of the redis shard in the redis shard cluster is mainly related to the amount of memory, the memory impact factor can be increased and the CPU impact factor can be reduced. For example, the memory impact factor is set to 0.6 and the CPU impact factor is set to 0.4 respectively. The specific values of the weighting factors are not limited in the present disclosure.
[0128] In one embodiment, the sum of the weighting factors in the above formula (1) is 1.
[0129] In one embodiment, before performing data scanning on each redis shard in the redis shard cluster, the data query method further includes:
[0130] If the hash value corresponding to the key value cannot be read from the hash mapping relationship table, the preset cursor value is determined as the second cursor of each redis shard.
[0131] In one embodiment, the preset cursor value is a preset initial value used for data scanning. Preferably, the preset cursor value may be 0, that is, each redis shard starts scanning from 0.
[0132] In one embodiment, the ratio of the count value to the number of redis shards is determined as the amount of query data for each redis shard.
[0133] In one embodiment, the count value may be obtained from a SCAN command in a service request, and the default value is 10.
[0134] In one embodiment, the count value means that a specified number of elements need to be returned from the data set, but it may not necessarily return the number specified by the user, such as 0 elements may be returned.
[0135] In one embodiment, the service request includes a timestamp, and the timestamp is used to indicate the order in which the service requests enter a task queue, and the task queue refers to a queue in which the agent processes the service requests of the terminal.
[0136] In one embodiment, the order of processing the service requests is determined according to the timestamps of the service requests, for example, the service request with a later timestamp can be executed only after the service request with an earlier timestamp is processed.
[0137] In one embodiment, decoding the first cursor of the SCAN command in the service request includes:
[0138] Obtain a first cursor of a SCAN command in the service request with the smallest timestamp in the service request.
[0139] In one embodiment, the timestamps of all service requests are obtained, and the service request with the smallest timestamp among the service requests is determined.
[0140] In one embodiment, the accuracy of the return value can be ensured by obtaining the service request with the smallest timestamp among the service requests.
[0141] In one embodiment, the first cursor of the SCAN command in the service request with the smallest timestamp is obtained by parsing the service request with the smallest timestamp.
[0142] In one embodiment, after merging the return values of all the redis shards, the data query method further includes:
[0143] Generate a random number with a fixed number of digits using the time random number method;
[0144] In one embodiment, the uniqueness of the random number can be guaranteed by using a time random number method to generate a random number with a fixed number of bits.
[0145] The random number with the fixed number of bits is determined as a third cursor of the service request.
[0146] In one embodiment, the third cursor refers to a return cursor of the service request.
[0147] In one embodiment, the third cursor is used to indicate the position of the next iteration.
[0148] In one embodiment, if the random number with a fixed number of bits is 8271833, 8271833 is determined as the third cursor of the service request.
[0149] In one embodiment, the scan result further includes a fourth cursor.
[0150] In one embodiment, the fourth cursor refers to the return cursor of each redis shard.
[0151] In one embodiment, after the random number with a fixed number of bits is generated by using the time random number method, the data query method further includes:
[0152] According to the index order of redis shards, concatenate the fourth cursors of redis shards into a linked list;
[0153] In one embodiment, if the current redis cluster is a cluster with 1024 redis shards, the cursor returned on shard 1 is 223, with values of 1, 2, 3, 4, 5, 6, 7, 8.9, the cursor returned on shard 2 is 3882, with values of 10, 11, 21, 333, 33, 12......., the cursor returned on shard 1024 is 9920, with values of 999, 22, 32, and the generated random number is 8271833. According to the index order of the redis shards, the fourth cursors of the redis shards are connected in series into a linked list, i.e. 223->3882->......->9920, a total of 1024.
[0154] The random number with a fixed number of bits is determined as a key value, and the fourth cursors are serially connected into a linked list as a hash value to update the hash mapping relationship table.
[0155] In one embodiment, as in the redis cluster in the aforementioned embodiment, 8271833 is determined as the key value, 223->3882->......->9920 is determined as the hash value, and the hash mapping relationship table is updated.
[0156] In one embodiment, by updating the hash mapping relationship table, it can be used by the client for the next data query.
[0157] In one embodiment, when all the fourth cursors obtained are 0, all the data in the hash mapping table is cleared while returning the data, and the last returned data is merged and sent to the client, and the third cursor is determined to be 0.
[0158] The data query method provided by the present disclosure is further described below with an application example:
[0159] like Figure 6 As shown, Figure 6 A flow chart of a data query method provided by an application example of the present disclosure. The data query method provided by an application example of the present disclosure comprises the following steps:
[0160] Step 601, receiving a service request sent by a terminal;
[0161] In one embodiment, the service request includes a timestamp, and the timestamp is used to indicate the order in which the service requests enter a task queue, and the task queue refers to a queue in which the agent processes the service requests of the terminal;
[0162] Step 602, obtaining the first cursor of the SCAN command in the service request with the smallest timestamp in the service request, and obtaining the key value corresponding to the first cursor;
[0163] Step 603, obtaining the number of redis shards in the redis shard cluster, the number of proxies configured in the redis shard cluster, and the performance evaluation value of the redis shards in the redis shard cluster;
[0164] Step 604, determining the module of the hash mapping relationship table based on the number of redis shards in the redis shard cluster, the number of proxies equipped with the redis shard cluster, and the performance evaluation value of the redis shards in the redis shard cluster;
[0165] Step 605, based on the key value and the modulus of the hash mapping relationship table, obtain a hash value linked list;
[0166] Step 606, using the sorting sequence number of the value in the hash value linked list as the index of the redis shard in the redis shard cluster, using the value in the hash value linked list as the second cursor of the redis shard with the same sorting sequence number, and concurrently scanning data for each redis shard in the redis shard cluster to obtain a scan result for each redis shard, the scan result including a return value;
[0167] In one embodiment, if the hash value corresponding to the key value cannot be read from the hash mapping relationship table, the preset cursor value is determined as the second cursor of each redis shard.
[0168] Step 607, merging the return values of all redis shards according to the index order of the redis shards;
[0169] Step 608, generating a random number of fixed digits using a time random number method;
[0170] Step 609, determining a random number of fixed digits as a third cursor of the service request;
[0171] In one embodiment, the scan result further includes a fourth cursor;
[0172] Step 610, according to the index order of the redis shards, the fourth cursors of the redis shards are serially connected into a linked list;
[0173] Step 611, determine a random number with a fixed number of bits as a key value, determine a fourth cursor serially connected into a linked list as a hash value, and update the hash mapping relationship table.
[0174] In one embodiment, the execution order of step 609 and step 610 to step 611 may be adjusted, which is not limited in the present disclosure.
[0175] Step 612: Send the combined return value to the terminal.
[0176] In summary, the solution provided by the present disclosure is:
[0177] First, a service request sent by a terminal is received; the first cursor of the SCAN command in the service request is decoded by a proxy to obtain the key value corresponding to the first cursor; the hash value corresponding to the key value is searched from the hash mapping relationship table to obtain a hash value chain list; the sorting number of the value in the hash value chain list is used as the index of the redis shard in the redis shard cluster, and the value in the hash value chain list is used as the second cursor of the redis shard with the same sorting number, and data is scanned for each redis shard in the redis shard cluster concurrently, so that the return value of each redis shard can be obtained, thereby improving data query efficiency. By merging the return values of all redis shards according to the index order of the redis shards, and sending the merged return values to the terminal, the SCAN command can be logically processed at the proxy level without using additional commands, so as to achieve the connection of each redis shard in the redis shard cluster, thereby achieving the purpose of smoothly using the SCAN command to query data on different redis shards in the redis cluster.
[0178] Secondly, the proxy and all Redis shards in the Redis cluster maintain continuous communication through long connections, which can reduce the overhead of establishing and disconnecting connections. In addition, when creating or deleting Redis shards in the Redis cluster, the proxy can automatically obtain and load them, thereby enabling migration from stand-alone Redis to cluster Redis without modifying any business logic.
[0179] Again, by using the time random number method to generate a random number with a fixed number of bits, the uniqueness of the random number can be guaranteed.
[0180] In order to implement the data query method provided by the embodiment of the present disclosure, the embodiment of the present disclosure also provides a data query device, such as Figure 7 shown. Figure 7 The data query device 700 is a schematic diagram of the structure of a data query device provided in an embodiment of the present disclosure, and includes:
[0181] The receiving unit 701 is used to receive a service request sent by a terminal;
[0182] A decoding unit 702, configured to decode a first cursor of a SCAN command in the service request to obtain a key value corresponding to the first cursor;
[0183] A search unit 703 is used to search the hash value corresponding to the key value from the hash mapping relationship table to obtain a hash value linked list;
[0184] The data scanning unit 704 is used to use the sorting sequence number of the value in the hash value linked list as the index of the redis shard in the redis shard cluster, use the value in the hash value linked list as the second cursor of the redis shard with the same sorting sequence number, and concurrently scan the data of each redis shard in the redis shard cluster to obtain the scanning result of each redis shard, and the scanning result includes a return value;
[0185] A merging unit 705 is used to merge the return value of each redis shard in the redis shard cluster according to the index order of the redis shard;
[0186] The sending unit 706 is configured to send the combined return value to the terminal.
[0187] In one embodiment, the searching unit 703 is specifically configured to:
[0188] Obtain the number of the redis shards in the redis shard cluster, the number of proxies equipped with the redis shard cluster, and the performance evaluation value of the redis shard in the redis shard cluster;
[0189] Determine the module of the hash mapping relationship table based on the number of the redis shards in the redis shard cluster, the number of proxies equipped with the redis shard cluster, and the performance evaluation values of the redis shards in the redis shard cluster;
[0190] Based on the key value and the modulus of the hash mapping relationship table, a hash value corresponding to the key value is obtained.
[0191] In one embodiment, the searching unit 703 is specifically configured to:
[0192] Obtain the number of redis shards in the redis shard cluster, the number of proxies equipped with the redis shard cluster, the performance evaluation of each redis shard, the amount of memory allocated to each redis shard, and the number of cores of the central processing unit CPU allocated to each redis shard;
[0193] Based on the performance evaluation of each redis shard, the amount of memory allocated to each redis shard, and the number of CPU cores allocated to each redis shard, determine the average performance evaluation, average memory, and average CPU cores of the redis shards in the redis shard cluster;
[0194] Based on the average performance evaluation, average memory number and average CPU core number of the redis shards in the redis shard cluster, the performance evaluation value of the redis shards in the redis shard cluster is determined.
[0195] In one embodiment, the data query device 700 further includes a first cursor determining unit, and the first cursor determining unit is used to:
[0196] If the hash value corresponding to the key value cannot be read from the hash mapping relationship table, the preset cursor value is determined as the second cursor of each redis shard.
[0197] In one embodiment, the service request includes a timestamp, and the timestamp is used to indicate the order in which the service requests enter a task queue, and the task queue refers to a queue in which the agent processes the service requests of the terminal.
[0198] In one embodiment, the decoding unit 702 is specifically configured to:
[0199] Obtain a first cursor of a SCAN command in the service request with the smallest timestamp in the service request.
[0200] In one embodiment, the data query device 700 further includes a third cursor determining unit, and the third cursor determining unit is used to:
[0201] Generate a random number with a fixed number of digits using the time random number method;
[0202] The random number with the fixed number of bits is determined as a third cursor of the service request.
[0203] In one embodiment, the scan result further includes a fourth cursor;
[0204] In one embodiment, the data query device 700 further includes an updating unit, which is used to:
[0205] According to the index order of redis shards, concatenate the fourth cursors of redis shards into a linked list;
[0206] The random number with a fixed number of bits is determined as a key value, and the fourth cursors are serially connected into a linked list as a hash value to update the hash mapping relationship table.
[0207] It should be noted that: the data query device 700 provided in the above embodiment only uses the division of the above program modules as an example when performing data query. In actual applications, the above processing can be assigned to different program modules as needed, that is, the internal structure of the data query device 700 is divided into different program modules to complete all or part of the processing described above. In addition, the data query device 700 provided in the above embodiment and the data query method embodiment provided in the embodiment of the present disclosure belong to the same concept. The specific implementation process is detailed in the method embodiment and will not be repeated here.
[0208] Figure 8 A schematic diagram of the hardware structure of an electronic device provided in an embodiment of the present disclosure is shown in FIG. Figure 8 As shown, the electronic device 800 includes at least one processor 802; and a memory 801 that is communicatively connected to the at least one processor 802; wherein the memory 801 stores instructions that can be executed by the at least one processor 802, and the instructions are executed by the at least one processor 802 to implement the steps of the data query method provided in the embodiment of the present disclosure.
[0209] Optionally, the electronic device may specifically be a data query device in an embodiment of the present application, and the electronic device may implement the corresponding processes implemented by the data query device in each method in the embodiment of the present application, which will not be described in detail here for the sake of brevity.
[0210] It is understood that the electronic device also includes a communication interface 803. The various components in the electronic device are coupled together through a bus system 804. It is understood that the bus system 804 is used to realize the connection and communication between these components. In addition to the data bus, the bus system 804 also includes a power bus, a control bus, and a status signal bus. However, for the sake of clarity, Figure 8 Various buses are labeled as bus system 804 .
[0211] It can be understood that the memory 801 can be a volatile memory or a non-volatile memory, and can also include both volatile and non-volatile memories. Among them, the non-volatile memory can be a read-only memory (ROM), a programmable read-only memory (PROM), an erasable programmable read-only memory (EPROM), an electrically erasable programmable read-only memory (EEPROM), a magnetic random access memory (FRAM), a ferromagnetic random access memory, a flash memory, a magnetic surface memory, an optical disc, or a compact disc read-only memory (CD-ROM); the magnetic surface memory can be a disk memory or a tape memory. The volatile memory can be a random access memory (RAM), which is used as an external cache. By way of example and not limitation, many forms of RAM are available, such as static random access memory (SRAM), synchronous static random access memory (SSRAM), dynamic random access memory (DRAM), synchronous dynamic random access memory (SDRAM), double data rate synchronous dynamic random access memory (DDRSDRAM), enhanced synchronous dynamic random access memory (ESDRAM), synchronous link dynamic random access memory (SLDRAM, SyncLink Dynamic Random Access Memory), and direct RAM bus random access memory (DRRAM, Direct Rambus Random Access Memory).The memory 801 described in the embodiment of the present invention is intended to include but is not limited to these and any other suitable types of memories.
[0212] The method disclosed in the above embodiment of the present disclosure can be applied to the processor 802, or implemented by the processor 802. The processor 802 may be an integrated circuit chip with signal processing capabilities. In the implementation process, each step of the above method can be completed by the hardware integrated logic circuit in the processor 802 or the instruction in the form of software. The above processor 802 may be a general-purpose processor, a DSP, or other programmable logic devices, discrete gates or transistor logic devices, discrete hardware components, etc. The processor 802 can implement or execute the methods, steps and logic block diagrams disclosed in the embodiments of the present invention. The general-purpose processor may be a microprocessor or any conventional processor, etc. In combination with the steps of the method disclosed in the embodiment of the present invention, it can be directly embodied as a hardware decoding processor to execute, or it can be executed by a combination of hardware and software modules in the decoding processor. The software module can be located in a storage medium, which is located in the memory 801, and the processor 802 reads the information in the memory 801 and completes the steps of the above method in combination with its hardware.
[0213] In an exemplary embodiment, the electronic device may be implemented by one or more application specific integrated circuits (ASICs), DSPs, programmable logic devices (PLDs), complex programmable logic devices (CPLDs), FPGAs, general purpose processors, controllers, MCUs, microprocessors, or other electronic components to execute the aforementioned method.
[0214] The embodiment of the present disclosure also provides a non-transitory computer-readable storage medium storing computer instructions, where the computer instructions are used to enable a computer to implement the steps of the data query method provided in the embodiment of the present disclosure when executed.
[0215] Optionally, the computer-readable storage medium can be applied to the data query device in the embodiments of the present application, and the computer instructions enable the computer to execute the corresponding processes implemented by the data query device in the various methods of the embodiments of the present application. For the sake of brevity, they are not repeated here.
[0216] The embodiment of the present disclosure also provides a computer program product, including a computer program, which implements the steps of the data query method provided by the embodiment of the present invention when executed by a processor.
[0217] In the several embodiments provided in the present application, it should be understood that the disclosed devices and methods can be implemented in other ways. The device embodiments described above are only schematic. For example, the division of units is only a logical function division. There may be other division methods in actual implementation, such as: multiple units or components can be combined, or can be integrated into another system, or some features can be ignored or not executed. In addition, the coupling, direct coupling, or communication connection between the components shown or discussed can be through some interfaces, and the indirect coupling or communication connection of devices or units can be electrical, mechanical or other forms.
[0218] The units described above as separate components may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units; some or all of the units may be selected according to actual needs to achieve the purpose of the present embodiment.
[0219] In addition, all functional units in the embodiments of the present invention may be integrated into one processing unit, or each unit may be separately used as a unit, or two or more units may be integrated into one unit; the above-mentioned integrated units may be implemented in the form of hardware or in the form of hardware plus software functional units.
[0220] A person of ordinary skill in the art can understand that: all or part of the steps of implementing the above-mentioned method embodiment can be completed by hardware related to program instructions, and the aforementioned program can be stored in a computer-readable storage medium, which, when executed, executes the steps of the above-mentioned method embodiment; and the aforementioned storage medium includes: various media that can store program codes, such as mobile storage devices, ROM, RAM, disks or optical disks.
[0221] Alternatively, if the above-mentioned integrated unit of the present invention is implemented in the form of a software function module and sold or used as an independent product, it can also be stored in a computer-readable storage medium. Based on this understanding, the technical solution of the embodiment of the present invention can be essentially or partly reflected in the form of a software product that contributes to the prior art. The computer software product is stored in a storage medium and includes several instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute all or part of the methods of each embodiment of the present invention. The aforementioned storage medium includes: various media that can store program codes, such as mobile storage devices, ROM, RAM, magnetic disks or optical disks.
[0222] The above are only specific embodiments of the present invention, but the protection scope of the present invention is not limited thereto. Any person skilled in the art who is familiar with the technical field can easily think of changes or substitutions within the technical scope disclosed by the present invention, which should be included in the protection scope of the present invention. Therefore, the protection scope of the present invention should be based on the protection scope of the claims.
Claims
1. A data query method, characterized in that: include: Receiving a service request sent by a terminal; Decoding a first cursor of a SCAN command in the service request to obtain a key value corresponding to the first cursor; Searching for a hash value corresponding to the key value from a hash mapping relationship table to obtain a hash value linked list; Using the sorting sequence number of the value in the hash value linked list as the index of the redis shard in the redis shard cluster, using the value in the hash value linked list as the second cursor of the redis shard with the same sorting sequence number, and concurrently scanning the data of each redis shard in the redis shard cluster to obtain the scanning result of each redis shard, wherein the scanning result includes a return value; Merge the return value of each redis shard in the redis shard cluster according to the index order of the redis shards; The combined return value is sent to the terminal.
2. The method according to claim 1, characterized in that The searching for a hash value corresponding to the key value from the hash mapping relationship table includes: Obtain the number of the redis shards in the redis shard cluster, the number of proxies equipped with the redis shard cluster, and the performance evaluation value of the redis shard in the redis shard cluster; Determine the module of the hash mapping relationship table based on the number of the redis shards in the redis shard cluster, the number of proxies equipped with the redis shard cluster, and the performance evaluation values of the redis shards in the redis shard cluster; Based on the key value and the modulus of the hash mapping relationship table, a hash value corresponding to the key value is obtained.
3. The method according to claim 2, characterized in that The obtaining the number of the redis shards in the redis shard cluster, the number of proxies equipped with the redis shard cluster, and the performance evaluation value of the redis shard in the redis shard cluster includes: Obtain the number of redis shards in the redis shard cluster, the number of proxies equipped with the redis shard cluster, the performance evaluation of each redis shard, the amount of memory allocated to each redis shard, and the number of cores of the central processing unit CPU allocated to each redis shard; Based on the performance evaluation of each redis shard, the amount of memory allocated to each redis shard, and the number of CPU cores allocated to each redis shard, determine the average performance evaluation, average memory, and average CPU cores of the redis shards in the redis shard cluster; Based on the average performance evaluation, average memory number and average CPU core number of the redis shards in the redis shard cluster, the performance evaluation value of the redis shards in the redis shard cluster is determined.
4. The method according to claim 1, characterized in that Before scanning data on each redis shard in the redis shard cluster, the method further includes: If the hash value corresponding to the key value cannot be read from the hash mapping relationship table, the preset cursor value is determined as the second cursor of each redis shard.
5. The method according to claim 1, characterized in that The service request includes a timestamp, and the timestamp is used to indicate the order in which the service requests enter a task queue, and the task queue refers to a queue in which the agent processes the service requests of the terminal; The decoding of the first cursor of the SCAN command in the service request includes: Obtain a first cursor of a SCAN command in the service request with the smallest timestamp in the service request.
6. The method according to claim 1, characterized in that After merging the return values of all the redis shards, the method further includes: Generate a random number with a fixed number of digits using the time random number method; The random number with the fixed number of bits is determined as a third cursor of the service request.
7. The method according to claim 6, characterized in that The scanning result also includes a fourth cursor; After the random number with a fixed number of bits is generated by the time random number method, the method further comprises: According to the index order of redis shards, concatenate the fourth cursors of redis shards into a linked list; The random number with a fixed number of bits is determined as a key value, and the fourth cursors are serially connected into a linked list as a hash value to update the hash mapping relationship table.
8. A data query device, characterized in that: include: A receiving unit, configured to receive a service request sent by a terminal; A decoding unit, used for decoding a first cursor of a SCAN command in the service request to obtain a key value corresponding to the first cursor; A search unit, used to search the hash value corresponding to the key value from the hash mapping relationship table to obtain a hash value linked list; A data scanning unit, used to use the sorting sequence number of the value in the hash value linked list as the index of the redis shard in the redis shard cluster, use the value in the hash value linked list as the second cursor of the redis shard with the same sorting sequence number, and concurrently scan the data of each redis shard in the redis shard cluster to obtain the scanning result of each redis shard, wherein the scanning result includes a return value; A merging unit, used to merge the return value of each redis shard in the redis shard cluster according to the index order of the redis shards; A sending unit is used to send the combined return value to the terminal.
9. An electronic device, characterized in that: include: at least one processor; as well as a memory communicatively connected to the at least one processor; wherein, The memory stores instructions that can be executed by the at least one processor, and the instructions are executed by the at least one processor to enable the at least one processor to perform the method according to any one of claims 1 to 7.
10. A non-transitory computer-readable storage medium storing computer instructions, characterized in that: The computer instructions are used to cause the computer to execute the method according to any one of claims 1 to 7.
11. A computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the computer program implements the method according to any one of claims 1 to 7.
