Data source dynamic switching method, electronic equipment and program product

By dynamically switching the database data source, based on the delay situation of the master-slave database and the information of the latest operation table, the problem of the database operation accuracy in the prior art cannot be improved when the master-slave database is delayed, and higher operation accuracy and master-slave separation effect are achieved.

CN119577026APending Publication Date: 2025-03-07KE COM (BEIJING) TECHNOLOGY CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202411600043.3
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-11-11
Publication Date
2025-03-07

AI Technical Summary

Technical Problem

The prior art is difficult to improve the accuracy of database operations when master-slave database delay occurs on the basis of master-slave database separation.

Method used

By receiving database operation requests, query the delay table to obtain the delay status of the master and slave database, and dynamically determine whether the master database data source or slave database data source is used to perform database operation requests based on whether the latest operation table contains information about the pending data table.

Benefits of technology

It realizes the accuracy of database operations when master-slave database delays are achieved on the basis of ensuring the separation effect of master-slave database.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119577026A_ABST
    Figure CN119577026A_ABST
Patent Text Reader

Abstract

The invention provides a data source dynamic switching method, electronic equipment and a program product. The method comprises the following steps: receiving a database operation request; wherein the database operation request comprises request content and a data table identifier of a to-be-processed data table; in response to the fact that the database operation request is determined as query operation according to the request content, the delay conditions of the master and slave databases are obtained by querying a delay condition table; determining whether the latest operation table contains information of a data table to be processed or not according to the data table identifier in response to the situation that the master-slave database has delay according to the delay condition of the master-slave database, and determining a data source for executing query operation according to the situation whether the latest operation table contains the information of the data table to be processed or not; responding to the situation that the master-slave database has no delay according to the delay condition of the master-slave database, and determining to execute the query operation by utilizing the slave database data source. On the basis of ensuring the effect of separating the master database from the slave database, the accuracy of database operation when the master database and the slave database are delayed is improved.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of computer technology, and in particular to a data source dynamic switching method, electronic equipment and program product. Background Art

[0002] During program execution, in order to reduce the pressure on the database, the master-slave configuration of the database is used to separate reads and writes. The master database performs write operations (additions, deletions, and modifications) and the slave database performs read operations (query operations). When there are large transaction operations (such as additions, deletions, and modifications of large quantities of data), there may be delays in the master-slave database, that is, the master database and the slave database fail to synchronize data after exceeding the preset time threshold. For this scenario, there are three mainstream solutions: 1. For queries on tables with large transactions, directly use the master database data source. The disadvantage is that the load on the master database will be relatively high, and the expected effect of master-slave separation is not achieved; 2. Use the master database data source for queries within a certain time range after a large transaction operation on the table, and use the slave database data source after the time threshold is exceeded. The disadvantage is that the operation time of the large transaction needs to be recorded for the table, and because the master-slave delay is uncontrollable, it may not achieve the desired effect; 3. Monitor the master-slave synchronization delay of the database. If there is a delay, use the master database data source. If there is no delay, use the slave database data source. The disadvantage is that the monitored information is the database dimension. The delay caused by a large transaction of a table may cause all queries in the entire database to use the master database data source, which is too coarse.

[0003] Therefore, the prior art has not yet been able to provide a solution for dynamic switching of data sources that can achieve the effect of separating the master and slave databases and improve the accuracy of database operations when a master and slave database delay occurs. Summary of the invention

[0004] The present invention provides a data source dynamic switching method, electronic device and program product, which are used to solve the defect in the prior art that it is impossible to achieve the effect of separating a master-slave database and improve the accuracy of database operation when a master-slave database delay occurs. The method can improve the accuracy of database operation when a master-slave database delay occurs while ensuring the effect of separating a master-slave database.

[0005] The present invention provides a method for dynamically switching a data source, comprising the following steps: receiving a database operation request; wherein the database operation request includes a request content and a data table identifier of a data table to be processed; in response to determining that the database operation request is a query operation according to the request content, obtaining the delay of a master-slave database by querying a delay situation table; in response to learning that the master-slave database has a delay according to the delay of the master-slave database, determining whether a recent operation table contains information of the data table to be processed according to the data table identifier, and determining a data source for executing the query operation according to whether the recent operation table contains information of the data table to be processed; wherein the recent operation table is used to store information of a data table corresponding to a database operation request received within a recent preset time range, the data table includes at least one of a first data table, a second data table and a third data table, the operation corresponding to the first data table has a risk of causing a delay in the master-slave database, the second data table is a data table corresponding to a database operation request received when there is a delay in the master-slave database, and the third data table is a data table corresponding to a database operation request received when the recent operation table is not empty; in response to learning that the master-slave database has no delay according to the delay of the master-slave database, determining to use a slave database data source to execute the query operation.

[0006] According to a data source dynamic switching method provided by the present invention, the data source for executing the query operation is determined according to whether the recent operation table contains information about the data table to be processed, comprising: in response to the recent operation table containing information about the data table to be processed, determining to use the master database data source to execute the query operation; in response to the recent operation table not containing information about the data table to be processed, determining to use the slave database data source to execute the query operation.

[0007] According to a method for dynamically switching data sources provided by the present invention, the method also includes: in response to determining that the database operation request is a new operation, a deletion operation or a modification operation according to the request content, using the master database data source to execute the database operation request; obtaining the delay status of the master-slave database by querying the delay status table; in response to the existence of a delay in the master-slave database, generating a monitoring expiration time of the data table to be processed, and adding the data table identifier of the data table to be processed and the monitoring expiration time to the recent operation table accordingly.

[0008] According to a method for dynamic switching of data sources provided by the present invention, after obtaining the delay status of the master-slave database by querying the delay status table, the method further includes: in response to there being no delay in the master-slave database and the record in the recent operation table is empty, further judging whether the number of affected rows of the database operation request exceeds a preset row number threshold according to the request content; in response to the number of affected rows of the database operation request exceeding the preset row number threshold, generating a monitoring expiration time of the data table to be processed, and adding the data table identifier and the monitoring expiration time of the data table to be processed to the recent operation table accordingly.

[0009] According to a method for dynamically switching data sources provided by the present invention, after obtaining the delay status of the master-slave database by querying the delay status table, the method further includes: in response to the absence of delay in the master-slave database and the record of the recent operation table is not empty, generating a monitoring expiration time of the data table to be processed, and adding the data table identifier of the data table to be processed and the monitoring expiration time to the recent operation table accordingly.

[0010] According to a data source dynamic switching method provided by the present invention, the generating of the monitoring expiration time of the data table to be processed includes: adding the time of receiving the database operation request to a preset waiting time to obtain the monitoring expiration time of the data table to be processed.

[0011] According to a method for dynamic switching of data sources provided by the present invention, the method also includes: monitoring the delay of the master-slave database through a scheduled task; in response to the existence of a delay in the master-slave database, writing information that the master-slave database has a delay into the delay situation table; in response to the absence of a delay in the master-slave database, writing information that the master-slave database has no delay into the delay situation table, and clearing the recent operation table.

[0012] According to a method for dynamically switching data sources provided by the present invention, after determining to use a master database data source to perform the query operation, after determining to use a slave database data source to perform the query operation, and / or after adding the data table identifier of the data table to be processed and the monitoring expiration time to the recent operation table, the method further includes: clearing information of the data table to be processed in the recent operation table whose monitoring expiration time is less than a preset time point.

[0013] The present invention also provides a data source dynamic switching device, comprising the following modules: a request receiving module, used to: receive a database operation request; wherein the database operation request includes the request content and the data table identifier of the data table to be processed; an acquisition module, used to: in response to determining that the database operation request is a query operation according to the request content, obtain the delay status of the master-slave database by querying the delay status table; a first determination module, used to: in response to knowing that the master-slave database has a delay according to the delay status of the master-slave database, determine whether the most recent operation table contains information about the data table to be processed according to the data table identifier, and determine whether to execute the query according to whether the most recent operation table contains information about the data table to be processed. a data source for query operations; wherein the recent operation table is used to store information of a data table corresponding to a database operation request received within a recent preset time range, the data table including at least one of a first data table, a second data table and a third data table, the operation corresponding to the first data table has a risk of causing delays in the master-slave database, the second data table is a data table corresponding to a database operation request received when there is a delay in the master-slave database, and the third data table is a data table corresponding to a database operation request received when the recent operation table is not empty; a second determination module, used to: in response to knowing that there is no delay in the master-slave database according to the delay situation of the master-slave database, determine to use the slave database data source to perform the query operation.

[0014] The present invention also provides an electronic device, comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein when the processor executes the computer program, any of the above-mentioned methods for dynamically switching data sources is implemented.

[0015] The present invention also provides a non-transitory computer-readable storage medium on which a computer program is stored. When the computer program is executed by a processor, the method for dynamically switching data sources as described above is implemented.

[0016] The present invention also provides a computer program product, comprising a computer program, wherein when the computer program is executed by a processor, the computer program implements any of the above-mentioned methods for dynamically switching data sources.

[0017] The data source dynamic switching method, electronic device and program product provided by the present invention receive a database operation request, the database operation request includes the request content and the data table identifier of the data table to be processed, in response to determining that the database operation request is a query operation according to the request content, obtaining the delay status of the master-slave database by querying the delay status table, in response to knowing that the master-slave database has a delay according to the delay status of the master-slave database, determining whether the most recent operation table contains information on the data table to be processed according to the data table identifier, and determining the data source for executing the query operation according to whether the most recent operation table contains information on the data table to be processed; wherein the most recent operation table is used to store information on the data table corresponding to the database operation request received within the most recent preset time range, and the data table includes a first data table, a second data table, and a third data table. At least one of the second data table and the third data table, the operation corresponding to the first data table has the risk of causing delays in the master-slave databases, the second data table is a data table corresponding to database operation requests received when there are delays in the master-slave databases, and the third data table is a data table corresponding to database operation requests received when the most recent operation table is not empty. In response to knowing that there is no delay in the master-slave database according to the delay situation of the master-slave database, it is determined to use the slave database data source to execute the query operation, and according to the delay situation of the master-slave database and whether the most recent operation table contains information of the data table to be processed, it is dynamically determined whether to use the master database data source or the slave database data source to execute the database operation request, thereby improving the accuracy of database operations when master-slave database delays occur while ensuring the effect of separation of the master and slave databases. BRIEF DESCRIPTION OF THE DRAWINGS

[0018] In order to more clearly illustrate the technical solutions in the present invention or the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.

[0019] Figure 1 This is one of the flow charts of the data source dynamic switching method provided by the present invention.

[0020] Figure 2 This is the second flow chart of the method for dynamic switching of data sources provided by the present invention.

[0021] Figure 3 It is a structural schematic diagram of the data source dynamic switching device provided by the present invention.

[0022] Figure 4 It is a structural schematic diagram of the electronic device provided by the present invention. DETAILED DESCRIPTION

[0023] In order to make the purpose, technical solution and advantages of the present invention clearer, the technical solution of the present invention will be clearly and completely described below in conjunction with the drawings of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.

[0024] Figure 1 This is one of the flow charts of the method for dynamically switching data sources provided by the present invention. Figure 1 As shown, the method includes: Step S1, receiving a database operation request; wherein the database operation request includes request content and a data table identifier of a data table to be processed.

[0025] The device that executes the data source dynamic switching method provided by the present invention can be called a data source dynamic switching device, which can be implemented as an interceptor for determining the data source for processing the database operation request after receiving the database operation request, and then using the corresponding data source to execute the corresponding database operation request.

[0026] The data source dynamic switching device receives a database operation request. The database operation request includes the request content and the data table identifier of the data table to be processed. The request content may include the request type and specific data processing information of the corresponding type of request. The data table to be processed is the data table for which the operation is requested, and the data table identifier is the unique identifier of the data table to be processed.

[0027] Step S2: in response to determining that the database operation request is a query operation according to the request content, obtaining the delay status of the master and slave databases by querying a delay status table.

[0028] If the database operation request is determined to be a query operation according to the request content, the delay situation of the master and slave databases is obtained by querying the delay situation table, wherein the delay situation table stores the information of the delay situation of the master and slave databases in the most recent time.

[0029] Step S3, in response to learning that there is a delay in the master-slave database according to the delay situation of the master-slave database, determining whether the most recent operation table contains information on the data table to be processed according to the data table identifier, and determining the data source for executing the query operation according to whether the most recent operation table contains information on the data table to be processed; wherein the most recent operation table is used to store information on data tables corresponding to database operation requests received within a recent preset time range, the data tables comprising at least one of a first data table, a second data table and a third data table, the operations corresponding to the first data table have the risk of causing delays in the master-slave database, the second data table is a data table corresponding to database operation requests received when there is a delay in the master-slave database, and the third data table is a data table corresponding to database operation requests received when the most recent operation table is not empty.

[0030] In addition to maintaining a delay situation table to store the most recent master-slave delay situation information of the master-slave database, a recent operation table is also maintained. First, in terms of time, the recent operation table is used to store the information of the data table corresponding to the database operation request received within the most recent preset time range. In addition, not all the information of the data table corresponding to the database operation request received recently is stored in the recent operation table, but is selectively stored. First, the recent operation table can store the information of the data table whose data table operation has the risk of causing delays in the master-slave database, such as the information of the data table involving large-scale data processing. This type of data table is called the first data table. Secondly, the recent operation table can also store the information of the data table corresponding to the database operation request received when there is a delay in the master-slave database. This type of table is called the second data table. In addition, the recent operation table can also store the information of the data table corresponding to the database operation request received when the recent operation table is not empty. This type of table is called the third data table.

[0031] If it is known that there is a delay in the master-slave database based on the delay situation of the master-slave database, it is determined based on the data table identifier whether the most recent operation table contains information about the data table to be processed, and the data source for executing the query operation is determined based on whether the most recent operation table contains information about the data table to be processed, thereby avoiding a one-size-fits-all approach of using the master database data source to execute the query operation when it is determined that there is a delay in the master-slave database.

[0032] Step S4: In response to learning that there is no delay in the master-slave database according to the delay situation of the master-slave database, determining to use the slave database data source to perform the query operation.

[0033] If it is known from the delay of the master-slave database that there is no delay in the master-slave database, it is determined to use the slave database data source to perform the query operation, thereby ensuring the effect of the master-slave separation configuration.

[0034] The data source dynamic switching method provided by the present invention receives a database operation request, the database operation request includes request content and a data table identifier of a data table to be processed, in response to determining that the database operation request is a query operation according to the request content, obtaining the delay status of the master-slave database by querying the delay status table, in response to knowing that the master-slave database has a delay according to the delay status of the master-slave database, determining whether the most recent operation table contains information on the data table to be processed according to the data table identifier, and determining the data source for executing the query operation according to whether the most recent operation table contains information on the data table to be processed; wherein the most recent operation table is used to store information on the data table corresponding to the database operation request received within a recently preset time range, and the data table includes a first data table, a second data table and At least one of the third data tables, the operation corresponding to the first data table has the risk of causing delays in the master-slave databases, the second data table is a data table corresponding to database operation requests received when there are delays in the master-slave databases, and the third data table is a data table corresponding to database operation requests received when the most recent operation table is not empty. In response to knowing that there is no delay in the master-slave databases based on the delay situation of the master-slave databases, it is determined to use the slave database data source to execute the query operation, and according to the delay situation of the master-slave databases and whether the most recent operation table contains information on the data table to be processed, it is dynamically determined whether to use the master database data source or the slave database data source to execute the database operation request, thereby improving the accuracy of database operations when master-slave database delays occur while ensuring the effect of separation of the master and slave databases.

[0035] According to a data source dynamic switching method provided by the present invention, the data source for executing the query operation is determined according to whether the recent operation table contains information about the data table to be processed, comprising: in response to the recent operation table containing information about the data table to be processed, determining to use the master database data source to execute the query operation; in response to the recent operation table not containing information about the data table to be processed, determining to use the slave database data source to execute the query operation.

[0036] The data table recorded in the recent operation table is a table that may cause master-slave delay, or a table operated later, or a table received when there is a delay. The data table recorded in the recent operation table has a high possibility of delay in the storage of the master-slave database, that is, the data table recorded in the recent operation table is largely inconsistent in the storage on the master-slave database. Therefore, when there is a master-slave delay, if the information of the data table to be processed is recorded in the recent operation table, the master database data source is selected to perform the corresponding query operation; if the information of the data table to be processed is not recorded in the recent operation table, the slave database data source is selected to perform the corresponding query operation.

[0037] Therefore, when determining the data source for executing the query operation based on whether the recent operation table contains information about the data table to be processed, if the recent operation table contains information about the data table to be processed, the query operation is performed using the main database data source; if the recent operation table does not contain information about the data table to be processed, the query operation is performed using the slave database data source.

[0038] The data source dynamic switching method provided by the present invention determines to use the main library data source to perform query operations in response to information that the data table to be processed is included in the most recent operation table, and determines to use the slave library data source to perform query operations in response to information that the data table to be processed is not included in the most recent operation table. This further improves the accuracy of database operations when master-slave database delays occur while ensuring the effect of separation of the master and slave databases.

[0039] According to a method for dynamically switching data sources provided by the present invention, the method also includes: in response to determining that the database operation request is a new operation, a deletion operation or a modification operation according to the request content, using the master database data source to execute the database operation request; obtaining the delay status of the master-slave database by querying the delay status table; in response to the existence of a delay in the master-slave database, generating a monitoring expiration time of the data table to be processed, and adding the data table identifier of the data table to be processed and the monitoring expiration time to the recent operation table accordingly.

[0040] If the database operation request is determined to be a new operation, a delete operation or a modification operation according to the request content of the database operation request, since the new operation, the delete operation or the modification operation is a write operation, which is an operation that may cause a master-slave delay, it is necessary to use the master database data source to execute the corresponding database operation request. After triggering the use of the master database data source to execute the corresponding database operation request, the delay situation of the master-slave database can be obtained by querying the delay situation table. If there is a delay in the master-slave database, it indicates that the current data table to be processed will be delayed to a large extent in the storage of the master-slave database, and then the monitoring expiration time of the data table to be processed is generated, and the data table identifier and the monitoring expiration time of the data table to be processed are added to the recent operation table accordingly. Since the recent operation table stores the information of the data table corresponding to the database operation request received within the latest preset time range, the monitoring expiration time of the data table to be processed is related to the reception time of the database operation request, and a certain waiting time can be set with reference to the latest preset time range. The monitoring expiration time is used as a consideration factor for whether the information of the data table to be processed continues to be retained in the recent operation table or deleted from the recent operation table.

[0041] The data source dynamic switching method provided by the present invention responds to determining that the database operation request is a new operation, a deletion operation or a modification operation according to the request content, uses the main database data source to execute the database operation request, obtains the delay situation of the master-slave database by querying the delay situation table, and generates the monitoring expiration time of the data table to be processed in response to the delay of the master-slave database, and adds the data table identifier and the monitoring expiration time of the data table to be processed to the recent operation table accordingly, so that the data table to be processed involved in the write operation is stored in the recent operation table when there is a delay between the master and slave databases, so as to reasonably determine the data source when processing the relevant data table to be processed later, and further improve the accuracy of database operation when the master-slave database delay occurs.

[0042] According to a method for dynamic switching of data sources provided by the present invention, after obtaining the delay status of the master-slave database by querying the delay status table, the method further includes: in response to there being no delay in the master-slave database and the record in the recent operation table is empty, further judging whether the number of affected rows of the database operation request exceeds a preset row number threshold according to the request content; in response to the number of affected rows of the database operation request exceeding the preset row number threshold, generating a monitoring expiration time of the data table to be processed, and adding the data table identifier and the monitoring expiration time of the data table to be processed to the recent operation table accordingly.

[0043] In response to determining that the database operation request is a new operation, a delete operation or a modify operation according to the request content, the database operation request is executed using the main database data source, and the delay status of the master-slave database is obtained by querying the delay status table. If there is no delay in the master-slave database, and the record in the recent operation table is empty, it is further determined according to the request content whether the number of rows affected by the database operation request exceeds the preset row number threshold. If the number of rows affected by the database operation request exceeds the preset row number threshold, it means that the database operation request has the risk of causing delays in the master-slave database, and then it is necessary to add the information of the data table to be processed to the recent operation table, then generate the monitoring expiration time of the data table to be processed, and add the data table identifier and the monitoring expiration time of the data table to be processed to the recent operation table accordingly.

[0044] The data source dynamic switching method provided by the present invention responds to the fact that there is no delay in the master-slave database and the record in the most recent operation table is empty, further judges whether the number of rows affected by the database operation request exceeds the preset row number threshold according to the request content, generates a monitoring expiration time of the data table to be processed in response to the fact that the number of rows affected by the database operation request exceeds the preset row number threshold, and adds the data table identifier and the monitoring expiration time of the data table to be processed to the most recent operation table accordingly, so that when there is no delay in the master-slave database and the most recent operation table is empty, but when large-scale data processing is involved, the data table to be processed is stored in the most recent operation table, so that the data source can be reasonably determined when the relevant data table to be processed is processed later, and the accuracy of database operation when the master-slave database delay occurs is further improved.

[0045] According to a method for dynamically switching data sources provided by the present invention, after obtaining the delay status of the master-slave database by querying the delay status table, the method further includes: in response to the absence of delay in the master-slave database and the record of the recent operation table is not empty, generating a monitoring expiration time of the data table to be processed, and adding the data table identifier of the data table to be processed and the monitoring expiration time to the recent operation table accordingly.

[0046] In response to determining that the database operation request is a new operation, a delete operation, or a modify operation based on the request content, the database operation request is executed using the main database data source, and the delay status of the master-slave database is obtained by querying the delay status table. If there is no delay in the master-slave database, and the record of the recent operation table is not empty, it is necessary to add the information of the current data table to be processed to the recent operation table, and then generate the monitoring expiration time of the data table to be processed, and add the data table identifier and monitoring expiration time of the data table to be processed to the recent operation table accordingly.

[0047] The data source dynamic switching method provided by the present invention generates a monitoring expiration time of a data table to be processed in response to the fact that there is no delay in the master-slave database and the record in the latest operation table is not empty, and adds the data table identifier and the monitoring expiration time of the data table to be processed to the latest operation table accordingly. This realizes that when there is no delay in the master-slave database and the record in the latest operation table is not empty, the data table to be processed is stored in the latest operation table, so that the data source can be reasonably determined when the relevant data table to be processed is processed later, and the accuracy of database operation when the master-slave database delay occurs is further improved.

[0048] According to a data source dynamic switching method provided by the present invention, the generating of the monitoring expiration time of the data table to be processed includes: adding the time of receiving the database operation request to a preset waiting time to obtain the monitoring expiration time of the data table to be processed.

[0049] When generating the monitoring expiration time of the data table to be processed, the monitoring expiration time of the data table to be processed can be obtained by adding the time of receiving the database operation request to the preset waiting time. Among them, since when the database operation request is received, the main database data source is triggered to perform the corresponding adding operation, deleting operation or modifying operation, the time of receiving the database operation request can also be regarded as the operation time of the main database data source on the data table to be processed. The preset waiting time can be set with reference to the most recent preset time range.

[0050] The data source dynamic switching method provided by the present invention improves the generation efficiency of the monitoring expiration time by obtaining the monitoring expiration time of the to-be-processed data table by adding the time of receiving the database operation request to the preset waiting time.

[0051] According to a method for dynamic switching of data sources provided by the present invention, the method also includes: monitoring the delay of the master-slave database through a scheduled task; in response to the existence of a delay in the master-slave database, writing information that the master-slave database has a delay into the delay situation table; in response to the absence of a delay in the master-slave database, writing information that the master-slave database has no delay into the delay situation table, and clearing the recent operation table.

[0052] A timed task for monitoring the delay of the master-slave database is set. The timed task can be executed at a preset time interval, and whether the delay of the master-slave database occurs is determined based on whether the delay time of the master-slave database exceeds the preset time threshold. If the delay time of the master-slave database exceeds the preset time threshold, it is determined that the delay of the master-slave database occurs; if the delay time of the master-slave database does not exceed the preset time threshold, it is determined that the delay of the master-slave database does not occur. When it is determined that there is a delay in the master-slave database, the relevant technical personnel can be notified to handle it.

[0053] When the scheduled task is executed, if it is determined that there is a delay in the master-slave database, the information about the delay in the master-slave database is written into the delay situation table. For example, if the delay situation information is set to 1, it means that there is a delay in the master-slave database; if it is determined that there is no delay in the master-slave database, the information about the delay in the master-slave database is written into the delay situation table. For example, if the delay situation information is set to 0, it means that there is no delay in the master-slave database. When it is determined that there is no delay in the master-slave database, the recent operation table is cleared at the same time. Before the next scheduled execution, if the received database operation request is a new operation, a delete operation, or a modification operation, and involves a large amount of processing, the information of the corresponding pending table will be added to the recent operation table. This corresponds to the situation where there is no delay in the master-slave database, but the recent operation table is not empty.

[0054] The data source dynamic switching method provided by the present invention monitors the delay of the master-slave database through a scheduled task. In response to the existence of a delay in the master-slave database, the information that the delay exists in the master-slave database is written into the delay situation table. In response to the absence of a delay in the master-slave database, the information that the delay does not exist in the master-slave database is written into the delay situation table, and the recent operation table is cleared, thereby realizing the update processing of the delay situation table and the recent operation table according to the delay of the master-slave database, and further improving the accuracy of database operations when a delay in the master-slave database occurs.

[0055] According to a method for dynamically switching data sources provided by the present invention, after determining to use a master database data source to perform the query operation, after determining to use a slave database data source to perform the query operation, and / or after adding the data table identifier of the data table to be processed and the monitoring expiration time to the recent operation table, the method further includes: clearing information of the data table to be processed in the recent operation table whose monitoring expiration time is less than a preset time point.

[0056] Since the delay of the master-slave database will not be too long unless a system failure occurs. Therefore, it is necessary to ensure the timeliness of the data table recorded in the recent operation table, which is specifically achieved by clearing the information of the pending data table whose monitoring expiration time is less than the preset time point in the recent operation table. Among them, when clearing the information of the pending data table whose monitoring expiration time is less than the preset time point in the recent operation table, the preset time point can be directly set to the current time, that is, the information of the pending data table whose monitoring expiration time is less than the current time is deleted from the recent operation table. The preset time point can also be set to a time point in the past, such as one minute before the current time, in which case the information of the pending data table whose monitoring expiration time is less than one minute before the current time is deleted from the recent operation table, thereby achieving the deletion of the information of the pending data table whose monitoring expiration time in the recent operation table is outside the specified time range.

[0057] Clearing the information of the pending data table in the recent operation table whose monitoring expiration time is less than the preset time point involves multiple process nodes, which may include: A database operation request is received, and in response to determining that the database operation request is a query operation based on the request content, the delay status of the master-slave database is obtained by querying the delay status table. If it is known that there is a delay in the master-slave database based on the delay status of the master-slave database, and the recent operation table contains information on the data table to be processed, it is determined to use the master database data source to perform the query operation, and the information on the data table to be processed whose monitoring expiration time is less than the preset time point in the recent operation table is cleared.

[0058] A database operation request is received, and in response to determining that the database operation request is a query operation based on the request content, the delay status of the master-slave database is obtained by querying the delay status table. If it is learned from the delay status of the master-slave database that there is a delay in the master-slave database, and the recent operation table does not contain information on the data table to be processed, it is determined to use the slave database data source to perform the query operation, and the information on the data table to be processed whose monitoring expiration time is less than a preset time point in the recent operation table is cleared.

[0059] A database operation request is received, and in response to determining that the database operation request is a query operation based on the request content, the delay status of the master-slave database is obtained by querying the delay status table. If it is determined that there is no delay in the master-slave database based on the delay status of the master-slave database, it is determined to use the slave database data source to perform the query operation, and the information of the data table to be processed whose monitoring expiration time is less than the preset time point in the recent operation table is cleared.

[0060] Receive a database operation request, in response to determining that the database operation request is a new operation, a delete operation, or a modify operation based on the request content, use the master database data source to execute the database operation request, obtain the delay status of the master-slave database by querying the delay status table, and in response to the existence of a delay in the master-slave database, generate a monitoring expiration time for the data table to be processed, add the data table identifier and the monitoring expiration time of the data table to be processed to the recent operation table, and clear the information of the data table to be processed whose monitoring expiration time is less than the preset time point in the recent operation table.

[0061] In response to determining that the database operation request is a new operation, a delete operation or a modify operation according to the request content, the database operation request is executed using the main database data source, and the delay status of the master-slave database is obtained by querying the delay status table. In response to there being no delay in the master-slave database and the record in the recent operation table being empty, it is further determined according to the request content whether the number of affected rows of the database operation request exceeds a preset row number threshold. In response to the number of affected rows of the database operation request exceeding the preset row number threshold, a monitoring expiration time of the data table to be processed is generated, the data table identifier and the monitoring expiration time of the data table to be processed are added to the recent operation table accordingly, and the information of the data table to be processed whose monitoring expiration time is less than the preset time point in the recent operation table is cleared.

[0062] In response to determining that the database operation request is a new operation, a delete operation or a modify operation according to the request content, the database operation request is executed using the main database data source, and the delay status of the master-slave database is obtained by querying the delay status table. In response to there being no delay in the master-slave database and the record in the recent operation table is not empty, a monitoring expiration time of the data table to be processed is generated, the data table identifier and the monitoring expiration time of the data table to be processed are added to the recent operation table accordingly, and the information of the data table to be processed whose monitoring expiration time is less than the preset time point in the recent operation table is cleared.

[0063] The data source dynamic switching method provided by the present invention ensures the timeliness of the data table recorded in the recent operation table by clearing the information of the to-be-processed data table whose monitoring expiration time is less than a preset time point in the recent operation table, and further improves the accuracy of database operations when a master-slave database delay occurs.

[0064] Figure 2 This is the second flow chart of the method for dynamically switching data sources provided by the present invention. Figure 2 As shown, a method for dynamic switching of data sources based on redis is provided, which specifically includes the following steps: 1. Use the redis string structure to record the delay of the database slave; 2. Use the zset structure of redis to record the status of the table operated within a certain time range (the most recently operated table). The score value is the timestamp of the operation time of the table + the preset waiting time (the default is half an hour); 3. Use scheduled tasks to monitor the delay of the database slave. If there is a delay, record the delay status in redis (the delay status is 1, indicating that there is a delay); if the delay time exceeds the set alarm threshold, notify the relevant technical personnel to follow up; if there is no delay, record the delay status in redis (the delay status is 0, indicating that there is no delay), and clear the zset data in redis; 4. Use the interceptor to intercept the operation processing on the database (the user's database operation can trigger the generation of database operation requests). If it is a add, modify, or delete operation, execute step 5. If it is a query operation, execute step 6. 5. Execute database operations to determine whether the delay status recorded by redis is a delay. If it is a delay, record the operated table to the zset queue of redis, and the score value is the timestamp of (current time + half an hour); if there is no delay, determine whether the zset queue of redis is empty. If it is not empty, record the table to the zset queue, and the score value is the timestamp of (current time + half an hour); if the zset queue is empty, determine whether the number of rows affected by the database operation exceeds the specified threshold. If it exceeds, record the table to the zset queue, and the score value is the timestamp of (current time + half an hour); 6. Determine whether the database delay status recorded by redis is delayed. If not, select the slave data source. If delayed, determine whether the zset queue of redis has the table. If yes, select the master data source. Otherwise, select the slave data source. 7. Delete the data whose score value in the redis queue is before the current timestamp to avoid the redis queue length being too long.

[0065] The data source dynamic switching method provided by the present invention uses a timed task to monitor the master-slave delay of the database, and selectively uses the master database data source when there is a delay, thereby reducing the possibility of misjudgment; when the master-slave delay time exceeds a threshold, the relevant technical personnel are notified to manually handle it; redis is used to maintain the recent operation table, and when there is a delay in the database, it is determined that it is a table recorded in the recent operation table, then the master database data source is selected, otherwise the slave database data source is selected, thereby reducing the granularity of using the master database data source.

[0066] The data source dynamic switching device provided by the present invention is described below. The data source dynamic switching device described below and the data source dynamic switching method described above can be referenced to each other.

[0067] Figure 3 Schematic diagram of the structure of the data source dynamic switching device provided by the present invention. Figure 3 As shown, the device includes a request receiving module 10, an acquisition module 20, a first determination module 30 and a second determination module 40, wherein: the request receiving module 10 is used to: receive a database operation request; wherein the database operation request includes request content and a data table identifier of a data table to be processed; the acquisition module 20 is used to: in response to determining that the database operation request is a query operation according to the request content, obtain the delay status of the master-slave database by querying the delay status table; the first determination module 30 is used to: in response to knowing that the master-slave database has a delay according to the delay status of the master-slave database, determine whether the most recent operation table contains information of the data table to be processed according to the data table identifier, and determine whether the most recent operation table contains information of the data table to be processed according to whether the most recent operation table contains the data table to be processed The data source for executing the query operation is determined based on the information in the table; wherein the recent operation table is used to store information on a data table corresponding to a database operation request received within a recent preset time range, the data table including at least one of a first data table, a second data table and a third data table, the operation corresponding to the first data table has a risk of causing delays in the master-slave database, the second data table is a data table corresponding to a database operation request received when there is a delay in the master-slave database, and the third data table is a data table corresponding to a database operation request received when the recent operation table is not empty; the second determination module 40 is used to: in response to knowing that there is no delay in the master-slave database based on the delay situation of the master-slave database, determine to use a slave database data source to execute the query operation.

[0068] The data source dynamic switching device provided by the present invention receives a database operation request, wherein the database operation request includes request content and a data table identifier of a data table to be processed, in response to determining that the database operation request is a query operation according to the request content, obtains the delay status of the master-slave database by querying the delay status table, in response to knowing that the master-slave database has a delay according to the delay status of the master-slave database, determines whether the most recent operation table contains information about the data table to be processed according to the data table identifier, and determines the data source for executing the query operation according to whether the most recent operation table contains information about the data table to be processed; wherein the most recent operation table is used to store information about the data table corresponding to the database operation request received within a recently preset time range, and the data table includes a first data table, a second data table, and a The method comprises: selecting at least one of a data table and a third data table, wherein the operation corresponding to the first data table has the risk of causing delay of the master-slave database, the second data table is a data table corresponding to the database operation request received when there is a delay in the master-slave database, and the third data table is a data table corresponding to the database operation request received when the most recent operation table is not empty, in response to knowing that there is no delay in the master-slave database according to the delay situation of the master-slave database, determining to use the slave database data source to execute the query operation, and dynamically determining whether to use the master database data source or the slave database data source to execute the database operation request according to the delay situation of the master-slave database and whether the most recent operation table contains information of the data table to be processed, thereby improving the accuracy of database operation when the master-slave database delay occurs on the basis of ensuring the effect of separation of the master and slave databases.

[0069] Figure 4 An example of a physical structure diagram of an electronic device is shown in FIG. Figure 4As shown, the electronic device may include: a processor 410, a communication interface 420, a memory 430 and a communication bus 440, wherein the processor 410, the communication interface 420 and the memory 430 communicate with each other through the communication bus 440. The processor 410 may call the logic instructions in the memory 430 to execute the data source dynamic switching method, which includes: receiving a database operation request; wherein the database operation request includes the request content and the data table identifier of the data table to be processed; in response to determining that the database operation request is a query operation according to the request content, obtaining the delay status of the master-slave database by querying the delay status table; in response to knowing that the master-slave database has a delay according to the delay status of the master-slave database, determining whether the most recent operation table contains the information of the data table to be processed according to the data table identifier, and determining to execute the query according to whether the most recent operation table contains the information of the data table to be processed. The data source of the operation; wherein the recent operation table is used to store information of a data table corresponding to a database operation request received within a recent preset time range, the data table includes at least one of a first data table, a second data table and a third data table, the operation corresponding to the first data table has a risk of causing delays in the master-slave database, the second data table is a data table corresponding to the database operation request received when there is a delay in the master-slave database, and the third data table is a data table corresponding to the database operation request received when the recent operation table is not empty; in response to knowing that there is no delay in the master-slave database according to the delay situation of the master-slave database, determining to use the slave database data source to execute the query operation.

[0070] In addition, the logic instructions in the above-mentioned memory 430 can be implemented in the form of a software functional unit and can be stored in a computer-readable storage medium when it is sold or used as an independent product. Based on this understanding, the technical solution of the present invention, in essence, or the part that contributes to the prior art or the part of the technical solution, can be embodied in the form of a software product, and the computer software product is stored in a storage medium, including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to perform all or part of the steps of the method described in each embodiment of the present invention. The aforementioned storage medium includes: U disk, mobile hard disk, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), disk or optical disk, etc. Various media that can store program codes.

[0071] On the other hand, the present invention further provides a computer program product, the computer program product includes a computer program, the computer program can be stored on a non-transitory computer-readable storage medium, when the computer program is executed by a processor, the computer can execute the data source dynamic switching method provided by the above methods, the method comprising: receiving a database operation request; wherein the database operation request includes request content and a data table identifier of a data table to be processed; in response to determining that the database operation request is a query operation according to the request content, obtaining the delay status of the master-slave database by querying the delay status table; in response to knowing that the master-slave database has a delay according to the delay status of the master-slave database, determining whether the most recent operation table contains information of the data table to be processed according to the data table identifier, The data source for executing the query operation is determined according to whether the recent operation table contains information about the data table to be processed; wherein the recent operation table is used to store information about a data table corresponding to a database operation request received within a recent preset time range, the data table comprising at least one of a first data table, a second data table and a third data table, the operation corresponding to the first data table has a risk of causing delays in the master-slave database, the second data table is a data table corresponding to a database operation request received when there is a delay in the master-slave database, and the third data table is a data table corresponding to a database operation request received when the recent operation table is not empty; in response to knowing that there is no delay in the master-slave database according to the delay situation of the master-slave database, it is determined to use a slave database data source to execute the query operation.

[0072] On the other hand, the present invention also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, is implemented to execute the data source dynamic switching method provided by the above-mentioned methods, the method comprising: receiving a database operation request; wherein the database operation request comprises a request content and a data table identifier of a data table to be processed; in response to determining that the database operation request is a query operation according to the request content, obtaining the delay status of the master-slave database by querying a delay status table; in response to knowing that the master-slave database has a delay according to the delay status of the master-slave database, determining whether the most recent operation table contains information of the data table to be processed according to the data table identifier, and determining whether the most recent operation table contains information of the data table to be processed according to whether the most recent operation table contains information of the data table to be processed. The information of the data table to be processed determines the data source for executing the query operation; wherein the recent operation table is used to store information of the data table corresponding to the database operation request received within the most recent preset time range, the data table includes at least one of a first data table, a second data table and a third data table, the operation corresponding to the first data table has the risk of causing delays in the master-slave database, the second data table is a data table corresponding to the database operation request received when there is a delay in the master-slave database, and the third data table is a data table corresponding to the database operation request received when the recent operation table is not empty; in response to knowing that there is no delay in the master-slave database according to the delay situation of the master-slave database, it is determined to use the slave database data source to execute the query operation.

[0073] The device embodiments described above are merely illustrative, wherein the units described 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 they may be distributed on multiple network units. Some or all of the modules may be selected according to actual needs to achieve the purpose of the scheme of this embodiment. Ordinary technicians in this field can understand and implement it without paying creative labor.

[0074] Through the description of the above implementation methods, those skilled in the art can clearly understand that each implementation method can be implemented by means of software plus a necessary general hardware platform, and of course, can also be implemented by hardware. Based on this understanding, the above technical solution is essentially or the part that contributes to the prior art can be embodied in the form of a software product, and the computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, a disk, an optical disk, etc., including a number of instructions for a computer device (which can be a personal computer, a server, or a network device, etc.) to execute the methods described in each embodiment or some parts of the embodiments.

[0075] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. However, these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention.

Claims

1. A method for dynamically switching data sources, characterized in that: include: Receive a database operation request; wherein the database operation request includes request content and a data table identifier of a data table to be processed; In response to determining that the database operation request is a query operation according to the request content, obtaining the delay status of the master and slave databases by querying the delay status table; In response to learning that the master-slave database has a delay according to the delay situation of the master-slave database, determining whether the most recent operation table contains information of the data table to be processed according to the data table identifier, and determining the data source for executing the query operation according to whether the most recent operation table contains information of the data table to be processed; wherein the most recent operation table is used to store information of a data table corresponding to a database operation request received within a recent preset time range, the data table includes at least one of a first data table, a second data table and a third data table, the operation corresponding to the first data table has a risk of causing a delay in the master-slave database, the second data table is a data table corresponding to a database operation request received when there is a delay in the master-slave database, and the third data table is a data table corresponding to a database operation request received when the most recent operation table is not empty; In response to learning that there is no delay in the master-slave database according to the delay conditions of the master-slave database, it is determined to use the slave database data source to perform the query operation.

2. The method for dynamically switching data sources according to claim 1, characterized in that: The step of determining the data source for executing the query operation according to whether the most recent operation table contains information about the data table to be processed includes: In response to the information of the to-be-processed data table included in the most recent operation table, determining to use the main database data source to execute the query operation; In response to the fact that the most recent operation table does not include information about the to-be-processed data table, it is determined to use a slave data source to perform the query operation.

3. The method for dynamically switching data sources according to claim 1, characterized in that: The method further comprises: In response to determining, according to the request content, that the database operation request is a adding operation, a deleting operation, or a modifying operation, executing the database operation request using a primary database data source; Obtaining the delay status of the master-slave database by querying the delay status table; In response to a delay in the master-slave database, a monitoring expiration time of the to-be-processed data table is generated, and a data table identifier of the to-be-processed data table and the monitoring expiration time are correspondingly added to the recent operation table.

4. The method for dynamically switching data sources according to claim 3, characterized in that: After obtaining the delay status of the master-slave database by querying the delay status table, the method further includes: In response to the master-slave database not having a delay and the record of the most recent operation table being empty, further determining, according to the request content, whether the number of affected rows of the database operation request exceeds a preset row number threshold; In response to the number of affected rows of the database operation request exceeding the preset row number threshold, a monitoring expiration time of the data table to be processed is generated, and the data table identifier and the monitoring expiration time of the data table to be processed are added to the recent operation table accordingly.

5. The method for dynamically switching data sources according to claim 4, characterized in that: After obtaining the delay status of the master-slave database by querying the delay status table, the method further includes: In response to the fact that there is no delay in the master-slave database and the record in the recent operation table is not empty, the monitoring expiration time of the data table to be processed is generated, and the data table identifier and the monitoring expiration time of the data table to be processed are added to the recent operation table accordingly.

6. The method for dynamically switching data sources according to any one of claims 3 to 5, characterized in that: The generating of the monitoring expiration time of the to-be-processed data table includes: The monitoring expiration time of the to-be-processed data table is obtained by adding the time of receiving the database operation request to the preset waiting time.

7. The method for dynamically switching data sources according to claim 5, characterized in that: The method further comprises: Monitor the delay of the master-slave database through a scheduled task; In response to a delay in the master-slave database, information about the delay in the master-slave database is written into the delay status table; In response to the fact that there is no delay in the master-slave database, information that there is no delay in the master-slave database is written into the delay situation table, and the recent operation table is cleared.

8. The method for dynamically switching data sources according to claim 5, characterized in that: After determining to use the master database data source to perform the query operation, after determining to use the slave database data source to perform the query operation, and / or after adding the data table identifier of the to-be-processed data table and the monitoring expiration time to the recent operation table, the method further includes: Clear the information of the to-be-processed data table in the recent operation table whose monitoring expiration time is less than the preset time point.

9. An electronic device comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein: When the processor executes the computer program, the method for dynamically switching data sources according to any one of claims 1 to 8 is implemented.

10. A computer program product, comprising a computer program, characterized in that When the computer program is executed by a processor, the method for dynamically switching data sources as claimed in any one of claims 1 to 8 is implemented.