Data query method, device, computer equipment and storage medium

By judging and downgrading query requests for abnormal cache shards when querying in full cache scenarios, the problem of database system pressure caused by full cache exceptions is solved, and database stability and data loading efficiency are improved.

CN113220722BActive Publication Date: 2024-10-29SHENZHEN YUNWANG WANDIAN TECH CO LTD
View PDF 2 Cites 0 Cited by

Patent Information

Application Number
CN202110453019.1
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-04-26
Publication Date
2024-10-29
Estimated Expiration
2041-04-26

AI Technical Summary

Technical Problem

When an exception occurs in a query in a full cache scenario, traditional technology will increase the pressure on the database system and threaten the stability of the database. A method is needed to reduce the pressure on the database system and improve its stability.

Method used

By receiving the query request, query the data in the cache according to the query request. If it cannot be queried, calculate the target cache fragmentation, and when the target cache fragmentation is an abnormal fragmentation, query the data from the database and reload the data if necessary. to cache to avoid full reloading.

Benefits of technology

It realizes query degradation for a single abnormal shard when an exception occurs in the full cache, reduces the pressure on the database system, improves the stability of the database system, and optimizes data loading efficiency.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN113220722B_ABST
    Figure CN113220722B_ABST
Patent Text Reader

Abstract

The present application relates to a data query method, device, computer equipment and storage medium. The method comprises: receiving a query request, querying corresponding data in a cache according to the query request, the cache comprising at least one cache shard; when the corresponding data cannot be queried from the cache, calculating the target cache shard to which the query request is routed; when the target cache shard is an abnormal shard, querying corresponding data from a database according to the query request. The use of this method can reduce the pressure on the database system and improve the stability of the database system when an abnormality occurs in a full cache scenario.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present application relates to the field of data processing technology, and in particular to a data query method, device, computer equipment and storage medium. Background Art

[0002] With the development of data processing technology, data caching technology has emerged. The principle of caching technology is that when the system needs to read a piece of data, it first searches from the cache. If it is found, it reads it immediately. If it is not found, it reads the data from the database or other areas at a relatively slow speed.

[0003] Generally, only hot data is stored in the cache. However, in industries such as e-commerce, all data may be hot data at a certain point in time, and the entire data needs to be cached to meet system performance requirements.

[0004] In traditional technologies, when an exception occurs in a query in a full cache scenario, the full cache is reloaded or the query logic is switched, so that all requests are downgraded to logic that can penetrate the cache and enter the database for query. This increases the pressure on the database system and threatens the stability of the database system. Summary of the invention

[0005] Based on this, it is necessary to provide a data query method, device, computer equipment and storage medium that can reduce the pressure on the database system and improve the stability of the database system when anomalies occur in queries in a full cache scenario in response to the above technical problems.

[0006] A data query method, the method comprising:

[0007] receiving a query request, and querying corresponding data in a cache according to the query request, where the cache includes at least one cache shard;

[0008] When the corresponding data cannot be found in the cache, the target cache shard to which the query request is routed is calculated;

[0009] When the target cache shard is an abnormal shard, the corresponding data is queried from the database according to the query request.

[0010] In one embodiment, the abnormal shard is at least one of a cache shard whose cache detection key is inactivated, a cache shard where a primary shard instance and a slave shard instance are switched, and a cache shard where a node is changed.

[0011] In one embodiment, before querying the corresponding data from the database according to the query request, the method further includes:

[0012] The number of concurrent query threads currently in the database is obtained. When the number is less than a preset threshold, the step of querying corresponding data from the database according to the query request is entered.

[0013] In one embodiment, after querying the corresponding data from the database according to the query request, the method further includes:

[0014] Reload the data corresponding to the query request in the queried database into the cache.

[0015] In one embodiment, the method further includes:

[0016] When the corresponding data is queried from the cache, the expiration time of the data corresponding to the query request in the cache is calculated;

[0017] If the data corresponding to the query request in the cache has expired, the asynchronous thread is called to reload the data corresponding to the query request in the database into the cache.

[0018] In one embodiment, the method further includes:

[0019] When the target cache shard is not an abnormal shard, a prompt message indicating that there is no query result is returned to the request initiator.

[0020] In one embodiment, the method further includes:

[0021] Periodically obtaining a cache detection key of each cache shard; wherein each cache shard is respectively set to have a corresponding unique cache detection key;

[0022] When it is detected that at least one cache detection key is deleted, the cache shard corresponding to the deleted cache detection key is determined as an abnormal shard.

[0023] In one embodiment, the method further includes:

[0024] When switching occurs between the primary shard instance and the secondary shard instance of a cache shard, the cache detection key of the cache shard where the switching occurs is deleted, and the cache shard where the switching occurs is determined as an abnormal shard.

[0025] In one embodiment, the method further includes:

[0026] When an operation of expanding or shrinking the cache is detected, the cache shard whose node has changed is determined as an abnormal shard.

[0027] A data query device, comprising:

[0028] A cache query module, used to receive a query request and query corresponding data in the cache according to the query request, wherein the cache includes at least one cache shard;

[0029] A shard calculation module is used to calculate the target cache shard to which the query request is routed when the corresponding data cannot be queried from the cache;

[0030] The query degradation module is used to query the corresponding data from the database according to the query request when the target cache shard is an abnormal shard.

[0031] A computer device includes a memory, a processor, and a computer program stored in the memory and executable on the processor. When the processor executes the computer program, the steps of the above-mentioned data query method are implemented.

[0032] A computer-readable storage medium stores a computer program, which implements the steps of the above-mentioned data query method when executed by a processor.

[0033] The above-mentioned data query method, device, computer equipment and storage medium can determine whether the cache shard to which the query request is routed is an abnormal shard when the corresponding data cannot be queried from the cache according to the query request. If the cache shard is an abnormal shard, the query request routed to the cache shard will be downgraded, that is, the query request will be allowed to enter the database to query the corresponding data, thereby realizing the downgrading of the query request for a single abnormal shard. In the case of an abnormality in the full cache, there is no need to reload the data in full and there is no need to downgrade the entire cache, thereby reducing the pressure on the database system and improving the stability of the database system. BRIEF DESCRIPTION OF THE DRAWINGS

[0034] Figure 1 An application environment diagram of a data query method in an embodiment;

[0035] Figure 2 A schematic diagram of a flow chart of a data query method in an embodiment;

[0036] Figure 3 The figure is a flowchart of data query in an application example;

[0037] Figure 4 A flowchart of using an asynchronous task to detect whether a cache shard is inactive in an application instance;

[0038] Figure 5 A flowchart for monitoring configuration changes of abnormal shards in a configuration center in an application instance;

[0039] Figure 6A flow chart of asynchronously modifying abnormal shard configuration during master-slave switching for a cache system client in an application instance;

[0040] Figure 7 is a structural block diagram of a data query device in an embodiment;

[0041] Figure 8 FIG. 4 is a diagram showing the internal structure of a computer device in one embodiment. DETAILED DESCRIPTION

[0042] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.

[0043] The data query method provided in this application can be applied to Figure 1 In the application environment shown. The application server 102 receives a query request, and according to the query request, the cache server 104 can be called to query the corresponding data in the cache. The cache includes at least one cache shard. When the corresponding data cannot be queried from the cache, the application server 102 calculates the target cache shard to which the query request is routed. When the target cache shard is an abnormal shard, the database server 106 can be called according to the query request to query the corresponding data from the database. The server can be implemented as an independent server or a server cluster composed of multiple servers.

[0044] In one embodiment, Figure 2 As shown, a data query method is provided, which is applied to Figure 1 The following steps are used as an example to illustrate the application server in the example:

[0045] Step S202: receiving a query request, and querying corresponding data in a cache according to the query request, where the cache includes at least one cache shard.

[0046] The cache refers to the buffer for data exchange. The cache here can be regarded as a buffer between the business system and the database system. The cache can be implemented based on a distributed system. For example, the cache can be deployed based on a redis server cluster. Cache sharding refers to the data sharding of data stored in the cache. In the distributed cache data storage solution, in order to enable the cluster to scale horizontally, the full amount of data is distributed to multiple nodes for shard storage according to certain rules. The cache of the full amount of data includes at least one cache shard. A query request is a request to instruct a data query.

[0047] Specifically, the application server may receive a query request sent by the terminal, and call the cache server according to the query request to query the data corresponding to the query request in the entire cache. For example, in a distributed cache based on a redis cluster, the data storage structure of redis is generally in the form of a key-value pair. Therefore, the query request may include a cache query key, and the corresponding value is queried in the cache according to the cache query key.

[0048] Step S204: When the corresponding data cannot be found in the cache, the target cache shard to which the query request is routed is calculated.

[0049] The cache includes at least one cache shard. The target cache shard refers to the cache shard where the data corresponding to the query request in the cache is stored.

[0050] Specifically, when the corresponding data cannot be found in the cache according to the query request, that is, when an exception occurs in which the data cannot be found in the scenario where the data is fully cached, the cache shard to which the query request is routed can be calculated. The calculation method is not limited, and a consistent hash algorithm can be used to determine the target cache shard through calculation.

[0051] Step S206: When the target cache shard is an abnormal shard, corresponding data is queried from the database according to the query request.

[0052] Among them, abnormal shards refer to cache shards that are determined to be abnormal. Abnormal shards may include cache shards whose cache detection keys have been deleted, cache shards that have switched between primary and secondary shard instances, or cache shards whose nodes have changed, etc. In addition to the cache shards in the above cases that can be determined as abnormal shards, the determination of abnormal shards can also be customized according to business needs.

[0053] Specifically, after determining the target cache shard, it is possible to further determine whether the target cache shard is an abnormal shard. For example, each cache shard has its own shard name. The shard name of the abnormal shard configured by the configuration center can be matched with the shard name of the target cache shard to determine whether the target cache shard is an abnormal shard. If so, the query request routed to the target shard can be downgraded so that the query request can penetrate the cache and query the corresponding data from the database.

[0054] The above-mentioned data query method, when the corresponding data cannot be queried from the cache according to the query request, can determine whether the cache shard to which the query request is routed is an abnormal shard. If the cache shard is an abnormal shard, the query request routed to the cache shard will be downgraded, that is, the query request is allowed to enter the database to query the corresponding data, thereby realizing the downgrading of the query request for a single abnormal shard. In the case of an abnormality in the full cache, there is no need to reload the data in full or downgrade the entire cache, thereby reducing the pressure on the database system and improving the stability of the database system.

[0055] In one embodiment, the abnormal shard is at least one of a cache shard whose cache detection key is inactivated, a cache shard where a primary shard instance and a slave shard instance are switched, and a cache shard where a node is changed.

[0056] For cache shards whose cache detection keys are inactivated, after all data is loaded into the cache, the system will load a unique corresponding cache detection key for each cache shard of the cache. When a downtime or other operation causes the cache detection key to be deleted, that is, when the cache detection key is inactivated, the system can determine the cache shard whose cache detection key is inactivated as an abnormal shard. Furthermore, the system can also record the shard name of the cache shard whose cache detection key is inactivated in the abnormal shard column in the configuration center.

[0057] The cache detection key can be generated in the following way: After the cache is loaded, the system will save a cache detection key in all cache shards. This cache detection key can be calculated according to the consistent hash algorithm of the cache shard, so as to ensure that each cache detection key can be accurately routed to the corresponding cache shard regardless of whether the cache is expanded or shrunk. The expiration time of the cache detection key can be set to never expire. The generation rule of the cache detection key can be the node name plus the asterisk plus the node weight plus the virtual node index. That is, if the shard name is shardName, the weight is 1, and the virtual node index is 0 (the virtual node index can be any integer from 0 to 159), the cache detection key corresponding to the cache shard is shardName*10.

[0058] In one embodiment, the above method also includes: periodically obtaining the cache detection key of each cache shard; wherein each cache shard is respectively set to correspond to a unique cache detection key; when it is detected that at least one cache detection key is deleted, the cache shard corresponding to the deleted cache detection key is determined as an abnormal shard.

[0059] In this embodiment, an asynchronous task may be set to periodically detect the activity of the cache detection key of each cache shard, so that when a cache shard with an inactive cache detection key is detected, the detected cache shard is promptly and accurately determined as an abnormal shard.

[0060] More specifically, the step of the asynchronous task detecting the cache detection key activity of each cache shard may include:

[0061] 1. After the full cache is loaded, the cache detection key is loaded in all cache shards.

[0062] 2. At regular intervals, the asynchronous task queries all cache detection keys to determine whether the cache detection keys and their corresponding fields exist, so as to determine whether there is complete data loss in each cache shard, that is, the scenario in which all master and slave shards that are not persisted are down.

[0063] 3. If a cache detection key is detected to be inactive, the information of the abnormal shard in the configuration center is updated.

[0064] For the cache shard where the master shard instance and the slave shard instance switch occurs, generally, the cache shard includes the master shard instance and the slave shard instance, and the data stored in the master and slave shard instances will be synchronized. However, the switch of the master and slave shard instances will cause the loss of cache data that has not been synchronized. Therefore, when the master and slave shard instance switch occurs, it may cause the loss of cache data. Therefore, the cache shard where the master shard instance and the slave shard instance switch occurs can be determined as an abnormal shard.

[0065] In one embodiment, the above method also includes: when a switching operation is detected between the primary shard instance and the slave shard instance of a cache shard, the cache detection key of the cache shard where the switching between the primary shard instance and the slave shard instance occurs is deleted, and the cache shard where the switching between the primary shard instance and the slave shard instance occurs is determined as the abnormal shard.

[0066] In this embodiment, by detecting the operation of switching between the master and slave shards, the cache shard where the master and slave shard switching occurs can be promptly determined as an abnormal shard.

[0067] More specifically, a new interface can be added to the client of the cache system. If a specific business operation needs to be performed after the master-slave shard switch, it can be passed to the client of the cache server through this interface. The cache server is instructed to execute a callback after the master-slave shard switch is completed, and the cache shard where the master shard instance and the slave shard instance switch occurred is determined as the abnormal shard, and the cache detection key of the cache shard where the master shard instance and the slave shard instance switch occurred is deleted.

[0068] For cache shards with changed nodes, when the cache is expanded or reduced, the node set corresponding to the cache shard will change. Therefore, the cache shards with changed nodes can be determined as abnormal cache shards.

[0069] In one embodiment, the method further includes: when an operation of expanding or shrinking the cache is detected, determining the cache shard whose node has changed as an abnormal shard. In this embodiment, by monitoring the changes in the node set configuration of the cache shard in the configuration center, the cache shard whose node set has changed (i.e., expansion or shrinkage has occurred) can be promptly determined as an abnormal shard.

[0070] In one embodiment, before querying the corresponding data from the database according to the query request, the above method also includes: obtaining the current number of concurrent query threads of the database, and when the number is less than a preset threshold, entering the step of querying the corresponding data from the database according to the query request.

[0071] In this embodiment, it can be determined whether the number of concurrent threads in the database reaches the threshold of the database flow control. If not, the step of querying the corresponding data from the database can be continued. Otherwise, an error can be reported or the thread of the query request can be suspended, etc., and the query can be performed again when the flow is sufficient. The stability of the database system can be further improved by performing flow control.

[0072] In one embodiment, after querying the corresponding data from the database according to the query request, the method further includes: reloading the data corresponding to the query request in the queried database into the cache.

[0073] In this embodiment, by reloading the data corresponding to the query request retrieved from the database into the cache, the missing data in the cache can be filled and the speed of the next query can be improved. Moreover, this scheme only reloads the missing data in the abnormal shard into the cache, making the data reloading more targeted, thereby reducing the waste of system resources.

[0074] In one embodiment, the above method also includes: when the corresponding data is queried from the cache, calculating the expiration time of the data corresponding to the query request in the cache; if the data corresponding to the query request in the cache has expired, calling an asynchronous thread to reload the data corresponding to the query request in the database into the cache.

[0075] The purpose of calculating data expiration time is to improve the consistency between the cache and the database. Because when the data is fully cached, if the operation of the database and the cache causes inconsistency in the stored data due to some reasons, the erroneous cached data cannot be modified. Therefore, when the system is unstable and there is a high possibility of problems, the logical expiration time of the data can be calculated. If the data has expired, an asynchronous thread can be set to reload the data into the cache after a certain interval to ensure that the contents of the cache are consistent with those of the database. If the system is stable and the functions are mature, the logical time can be omitted to improve system performance.

[0076] In one embodiment, the method further includes: when the target cache shard is not an abnormal shard, returning prompt information indicating that there is no query result to the request initiator.

[0077] In this embodiment, when the target cache shard is not an abnormal shard, query downgrade processing may not be performed on the target cache shard, that is, query requests routed to the shard will not penetrate the cache, thereby achieving targeted query downgrade, further reducing the pressure on the database system and improving the stability of the database system.

[0078] The data query method involved in this application is further described in detail below in conjunction with application examples. Figure 3 As shown, Figure 3 The following is a flow chart of a data query method in an application example. The specific details of the process can be as follows:

[0079] 1. The client queries the full cache data based on the full cache key.

[0080] 2. Determine whether the redis server returns a value.

[0081] 3. If the redis server returns no value.

[0082] 3.1. Determine whether there are abnormal fragments in the cache.

[0083] 3.2. If there is no abnormal shard in the cache, return no value directly.

[0084] 3.3. If the cache has abnormal fragmentation.

[0085] 3.3.1. Calculate which shard the current full cache Key is routed to based on the consistent hash algorithm.

[0086] 3.3.2. Determine whether the route fragment is an abnormal fragment.

[0087] 3.3.3. If the routed shard is not an abnormal shard, return no value directly.

[0088] 3.3.4. If the routed fragment is an abnormal fragment.

[0089] 3.3.4.1. Determine whether the flow control threshold is reached.

[0090] 3.3.4.2. If the flow control threshold is reached, an error will be reported directly.

[0091] 3.3.4.3. If the flow control threshold is not reached, query the database.

[0092] 3.3.4.4. Determine whether the data is found.

[0093] 3.3.4.5. If the data is found.

[0094] 3.3.4.5.1. Load the result into cache value.

[0095] 3.3.4.5.2. The cache key expiration time is set to never expire.

[0096] 3.3.4.6. If the data is not found.

[0097] 3.3.4.6.1. Load the NOTEXIST string into the cache value.

[0098] 3.3.4.6.2. Get the expiration time when the value does not exist from the configuration center and set it as the expiration time of the cache key.

[0099] 3.3.4.7. Load the current time into the cache loadTime.

[0100] 3.3.4.8. Return the cached value to the business. If the value is NOTEXIST, it is converted to null when returned.

[0101] in, Figure 4 A schematic diagram of a process of using an asynchronous task to detect whether a cache slice is inactivated in an application example is shown. The specific steps may be as follows:

[0102] Asynchronous Job monitoring process (executed every 10 seconds in each data center):

[0103] 1. Generate all cache detection keys according to the node name configured by redis in the configuration center

[0104] 2. Traverse the fields corresponding to the full cache

[0105] 3. Query the full hash cache based on the cache detection key and field

[0106] 4. Calculate the abnormal shards corresponding to the full cache based on the cache results

[0107] 5. Compare the abnormal sharding configuration of the full cache in the public configuration library and the abnormal sharding information calculated by the redis cache to see if they are consistent

[0108] 6. If they are consistent, exit this inspection.

[0109] 7. If there is inconsistency

[0110] a) Update the abnormal shard corresponding to the full cache in the public configuration library

[0111] b) Clear the Ehcache cache configured in the public configuration library

[0112] c) Alarm notification system personnel

[0113] Figure 5 A schematic diagram of a process for monitoring configuration changes of abnormal shards in a configuration center in an application example is shown. The specific steps may be as follows:

[0114] Detection configuration center redis node configuration change process (redis expansion and contraction to modify shardName node set)

[0115] 1. Configuration changes of redis nodes in the configuration center

[0116] 2. Send data changes to the application server

[0117] 3. Determine whether the shardName node set has changed

[0118] 4. If there is no change, exit

[0119] 5. If there are any changes

[0120] a) Delete all cache detection key caches

[0121] b) Modify the abnormal shard configuration corresponding to the full cache in the public configuration Ehcache to all

[0122] Figure 6 The schematic diagram of the process of asynchronously modifying abnormal shard configuration when the cache system client completes the master-slave switch in an application example is shown. The specific steps can be as follows:

[0123] Redis client monitors in real time, and Sentinel completes the master-slave switch and notifies the client process:

[0124] 1. Sentinel completes master-slave switching

[0125] 2. Sentinel sends master-slave failover message to redis client

[0126] 3. Redis client adjusts master-slave cache information

[0127] 4. Redis client asynchronous thread pool calls notification business system method

[0128] a) Modify the abnormal sharding of all full cache types in the Ehcache cache

[0129] b) Delete the cache detection key corresponding to the abnormal shard

[0130] Flow control logic:

[0131] 1. When the application is initialized, a ConcurrentHashMap is constructed with the time accurate to seconds as the key and the LongAdder object as the value.

[0132] 2. The next time you query the database, you build a LongAdder object and assign a value to the map through the putIfAbsent method of the map. If a LongAdder object for that second already exists, use the existing object directly; otherwise, use the newly built object.

[0133] 3. Use the sum method of the object and the threshold in the configuration center to determine whether flow control is triggered.

[0134] 4. If flow control is triggered, an error is reported.

[0135] 5. If flow control is not triggered, call the increment method of the LongAdder object to add one.

[0136] 6. When the application is initialized, start another thread to regularly remove expired keys from this Map collection.

[0137] It should be understood that although Figure 2-6 The steps in the flowchart are shown in sequence as indicated by the arrows, but these steps are not necessarily executed in the order indicated by the arrows. Unless otherwise specified in this document, there is no strict order restriction for the execution of these steps, and these steps can be executed in other orders. Moreover, Figure 2-6 At least part of the steps may include multiple sub-steps or multiple stages. These sub-steps or stages are not necessarily executed at the same time, but can be executed at different times. The execution order of these sub-steps or stages is not necessarily sequential, but can be executed in turn or alternately with other steps or at least part of the sub-steps or stages of other steps.

[0138] In one embodiment, Figure 7 As shown, a data query device is provided, including: a cache query module 702, a shard calculation module 704 and a query degradation module 706, wherein:

[0139] A cache query module 702, configured to receive a query request and query corresponding data in a cache according to the query request, wherein the cache includes at least one cache shard;

[0140] The shard calculation module 704 is used to calculate the target cache shard to which the query request is routed when the corresponding data cannot be found from the cache;

[0141] The query degradation module 706 is used to query the corresponding data from the database according to the query request when the target cache segment is an abnormal segment.

[0142] In one embodiment, before querying the corresponding data from the database according to the query request, the query downgrade module 706 is also used to obtain the current number of concurrent query threads in the database. When the number is less than a preset threshold, the step of querying the corresponding data from the database according to the query request is entered.

[0143] In one embodiment, after querying corresponding data from the database according to the query request, the query degradation module 706 is further configured to reload the data corresponding to the query request in the queried database into the cache.

[0144] In one embodiment, the cache query module 702 is also used to calculate the expiration time of the data corresponding to the query request in the cache when the corresponding data is queried from the cache; if the data corresponding to the query request in the cache has expired, call the asynchronous thread to reload the data corresponding to the query request in the database into the cache.

[0145] In one embodiment, the query degradation module 706 is further configured to return a prompt message indicating that there is no query result to the request initiator when the target cache shard is not an abnormal shard.

[0146] In one embodiment, the above-mentioned device includes an exception monitoring module 708, which is used to periodically obtain the cache detection key of each cache shard; wherein each cache shard is respectively set to correspond to a unique cache detection key; when it is detected that at least one cache detection key is deleted, the cache shard corresponding to the deleted cache detection key is determined as an abnormal shard.

[0147] In one embodiment, the exception monitoring module 708 is used to delete the cache detection key of the cache shard where the switch between the primary shard instance and the slave shard instance occurs, and determine the cache shard where the switch between the primary shard instance and the slave shard instance occurs as an abnormal shard.

[0148] In one embodiment, the abnormality monitoring module 708 is used to determine the cache shard with a changed node as an abnormal shard when detecting an operation of expanding or shrinking the cache.

[0149] For the specific definition of the data query device, please refer to the definition of the data query method above, which will not be repeated here. Each module in the above-mentioned data query device can be implemented in whole or in part by software, hardware and a combination thereof. The above-mentioned modules can be embedded in or independent of the processor in the computer device in the form of hardware, or can be stored in the memory of the computer device in the form of software, so that the processor can call and execute the operations corresponding to the above modules.

[0150] In one embodiment, a computer device is provided. The computer device may be a server, and its internal structure diagram may be as follows: Figure 8 As shown. The computer device includes a processor, a memory, a network interface and a database connected through a system bus. Among them, the processor of the computer device is used to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system, a computer program and a database. The internal memory provides an environment for the operation of the operating system and the computer program in the non-volatile storage medium. The database of the computer device is used to store business data. The network interface of the computer device is used to communicate with an external terminal through a network connection. When the computer program is executed by the processor, a data query method is implemented.

[0151] Those skilled in the art will understand that Figure 8 The structure shown in the figure is only a block diagram of a part of the structure related to the solution of the present application, and does not constitute a limitation on the computer device to which the solution of the present application is applied. The specific computer device may include more or fewer components than those shown in the figure, or combine certain components, or have a different arrangement of components.

[0152] In one embodiment, a computer device is provided, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor implements the following steps when executing the computer program: receiving a query request, querying corresponding data in a cache according to the query request, the cache including at least one cache shard; when the corresponding data cannot be queried from the cache, calculating a target cache shard to which the query request is routed; when the target cache shard is an abnormal shard, querying corresponding data from a database according to the query request.

[0153] In one embodiment, before the processor executes a computer program to query corresponding data from a database according to a query request, it also implements the following steps: obtaining the current number of concurrent query threads in the database, and when the number is less than a preset threshold, entering the step of querying corresponding data from the database according to the query request.

[0154] In one embodiment, after the processor executes the computer program to query the corresponding data from the database according to the query request, the processor further implements the following steps: reloading the data corresponding to the query request in the queried database into the cache.

[0155] In one embodiment, when the processor executes the computer program, the following steps are also implemented: when the corresponding data is queried from the cache, the expiration time of the data corresponding to the query request in the cache is calculated; if the data corresponding to the query request in the cache has expired, the asynchronous thread is called to reload the data corresponding to the query request in the database into the cache.

[0156] In one embodiment, when the processor executes the computer program, the following steps are further implemented: when the target cache shard is not an abnormal shard, a prompt message indicating that there is no query result is returned to the request initiator.

[0157] In one embodiment, when the processor executes the computer program, the following steps are also implemented: periodically obtaining the cache detection key of each cache shard; wherein each cache shard is respectively set to correspond to a unique cache detection key; when it is detected that at least one cache detection key is deleted, the cache shard corresponding to the deleted cache detection key is determined as an abnormal shard.

[0158] In one embodiment, when the processor executes the computer program, the following steps are also implemented: when a switch occurs between a primary shard instance and a secondary shard instance of a cache shard, the cache detection key of the cache shard where the switch between the primary shard instance and the secondary shard instance occurs is deleted, and the cache shard where the switch between the primary shard instance and the secondary shard instance occurs is determined as an abnormal shard.

[0159] In one embodiment, when the processor executes the computer program, the following steps are further implemented: when an operation of expanding or shrinking the cache is detected, a cache shard in which a node has changed is determined as an abnormal shard.

[0160] In one embodiment, a computer-readable storage medium is provided, on which a computer program is stored, and when the computer program is executed by a processor, the following steps are implemented: receiving a query request, querying corresponding data in a cache according to the query request, the cache including at least one cache shard; when the corresponding data cannot be queried from the cache, calculating the target cache shard to which the query request is routed; when the target cache shard is an abnormal shard, querying corresponding data from a database according to the query request.

[0161] In one embodiment, before the computer program is executed by the processor to query the corresponding data from the database according to the query request, it also implements the following steps: obtaining the current number of concurrent query threads in the database, and when the number is less than a preset threshold, entering the step of querying the corresponding data from the database according to the query request.

[0162] In one embodiment, after the computer program is executed by the processor to query the corresponding data from the database according to the query request, the following steps are also implemented: reloading the data corresponding to the query request in the queried database into the cache.

[0163] In one embodiment, when the computer program is executed by the processor, the following steps are also implemented: when the corresponding data is queried from the cache, the expiration time of the data corresponding to the query request in the cache is calculated; if the data corresponding to the query request in the cache has expired, the asynchronous thread is called to reload the data corresponding to the query request in the database into the cache.

[0164] In one embodiment, when the computer program is executed by the processor, the following steps are further implemented: when the target cache shard is not an abnormal shard, a prompt message indicating that there is no query result is returned to the request initiator.

[0165] In one embodiment, when the computer program is executed by the processor, the following steps are also implemented: periodically obtaining the cache detection key of each cache shard; wherein each cache shard is respectively set to correspond to a unique cache detection key; when it is detected that at least one cache detection key is deleted, the cache shard corresponding to the deleted cache detection key is determined as an abnormal shard.

[0166] In one embodiment, when the computer program is executed by the processor, the following steps are also implemented: when a switch occurs between the primary shard instance and the slave shard instance of a cache shard, the cache detection key of the cache shard where the switch between the primary shard instance and the slave shard instance occurs is deleted, and the cache shard where the switch between the primary shard instance and the slave shard instance occurs is determined as an abnormal shard.

[0167] In one embodiment, when the computer program is executed by the processor, the following steps are further implemented: when an operation of expanding or shrinking the cache is detected, the cache shard in which the node has changed is determined as an abnormal shard.

[0168] Those skilled in the art can understand that all or part of the processes in the above-mentioned embodiment methods can be completed by instructing the relevant hardware through a computer program, and the computer program can be stored in a non-volatile computer-readable storage medium. When the computer program is executed, it can include the processes of the embodiments of the above-mentioned methods. Among them, any reference to memory, storage, database or other media used in the embodiments provided in this application can include non-volatile and / or volatile memory. Non-volatile memory can include read-only memory (ROM), programmable ROM (PROM), electrically programmable ROM (EPROM), electrically erasable programmable ROM (EEPROM) or flash memory. Volatile memory can include random access memory (RAM) or external cache memory. As an illustration and not limitation, RAM is available in many forms, such as static RAM (SRAM), dynamic RAM (DRAM), synchronous DRAM (SDRAM), double data rate SDRAM (DDRSDRAM), enhanced SDRAM (ESDRAM), synchronous link (Synchlink) DRAM (SLDRAM), memory bus (Rambus) direct RAM (RDRAM), direct memory bus dynamic RAM (DRDRAM), and memory bus dynamic RAM (RDRAM).

[0169] The technical features of the above embodiments may be arbitrarily combined. To make the description concise, not all possible combinations of the technical features in the above embodiments are described. However, as long as there is no contradiction in the combination of these technical features, they should be considered to be within the scope of this specification.

[0170] The above-mentioned embodiments only express several implementation methods of the present application, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of the invention patent. It should be pointed out that, for a person of ordinary skill in the art, several variations and improvements can be made without departing from the concept of the present application, and these all belong to the protection scope of the present application. Therefore, the protection scope of the patent of the present application shall be subject to the attached claims.

Claims

1. A data query method, the method include: Receiving a query request, and querying corresponding data in a cache according to the query request, the cache including at least one cache shard; When the corresponding data cannot be queried from the cache, calculating the target cache shard to which the query request is routed; When the target cache shard is an abnormal shard, query the corresponding data from the database according to the query request; wherein the abnormal shard is at least one of a cache shard whose cache detection key is inactivated, a cache shard whose primary shard instance and secondary shard instance are switched, and a cache shard whose node is changed; The method further comprises: Periodically obtaining a cache detection key of each cache shard; wherein each cache shard is respectively set to correspond to a unique cache detection key; when it is detected that at least one cache detection key is deleted, determining the cache shard corresponding to the deleted cache detection key as the abnormal shard; and / or, When switching occurs between the primary shard instance and the secondary shard instance of the cache shard, the cache detection key of the cache shard where the switching occurs is deleted, and the cache shard where the switching occurs is determined as the abnormal shard; and / or, When an operation of expanding or shrinking the cache is detected, the cache shard in which the node is changed is determined as the abnormal shard.

2. The method according to claim 1, It is characterized in that Before querying the corresponding data from the database according to the query request, the method further includes: The current number of concurrent query threads of the database is obtained, and when the number is less than a preset threshold, the step of querying corresponding data from the database according to the query request is entered.

3. The method according to claim 1, It is characterized in that After querying the corresponding data from the database according to the query request, the method further includes: The data corresponding to the query request in the queried database is reloaded into the cache.

4. The method according to claim 1, It is characterized in that The method further comprises: When the corresponding data is queried from the cache, calculating the expiration time of the data corresponding to the query request in the cache; If the data corresponding to the query request in the cache has expired, the asynchronous thread is called to reload the data corresponding to the query request in the database into the cache.

5. The method according to claim 1, It is characterized in that The method further comprises: When the target cache segment is not an abnormal segment, a prompt message indicating that there is no query result is returned to the request initiator.

6. A data query device, It is characterized in that The device comprises: A cache query module, configured to receive a query request and query corresponding data in a cache according to the query request, wherein the cache includes at least one cache shard; A shard calculation module, used to calculate the target cache shard to which the query request is routed when the corresponding data cannot be queried from the cache; A query degradation module, used for querying corresponding data from the database according to the query request when the target cache shard is an abnormal shard; wherein the abnormal shard is at least one of a cache shard whose cache detection key is inactivated, a cache shard whose primary shard instance and slave shard instance are switched, and a cache shard whose node is changed; An abnormality monitoring module is used to periodically obtain the cache detection key of each cache shard; wherein each cache shard is respectively set to have a corresponding unique cache detection key; when it is detected that at least one cache detection key is deleted, the cache shard corresponding to the deleted cache detection key is determined as the abnormal shard; and / or, when switching occurs between the primary shard instance and the secondary shard instance of the cache shard, the cache detection key of the cache shard in which the primary shard instance and the secondary shard instance are switched is deleted, and the cache shard in which the primary shard instance and the secondary shard instance are switched is determined as the abnormal shard; and / or, when an operation of expanding or shrinking the cache is detected, the cache shard in which the node is changed is determined as the abnormal shard.

7. A computer device comprising a memory, a processor and a computer program stored in the memory and executable on the processor, It is characterized in that When the processor executes the computer program, the steps of the method according to any one of claims 1 to 5 are implemented.

8. A computer-readable storage medium having a computer program stored thereon, It is characterized in that When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 5 are implemented.

Citation Information

Patent Citations

  • Data processing optimization device and method based on distributed system

    CN108182213A

  • Data caching processing method and device, computer device and storage medium

    CN110795457A