Data source determination method, device and equipment and computer readable storage medium
By using annotation information in the target function to obtain the data source rule expression and delay information, and dynamically switching databases, it solves the problem in the existing technology of being unable to automatically judge the delay from the database and dynamically calculate the data source, and improves the stability and flexibility of the system. .
Patent Information
- Application Number
- CN202410016185.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-01-04
- Publication Date
- 2024-09-17
- Estimated Expiration
- Not applicable · inactive patent
AI Technical Summary
The existing technology cannot automatically determine whether the slave database is delayed in a master-slave database switching scenario, and switch to the master database according to dynamic rules, and cannot dynamically calculate the target data source based on business request parameters, which affects system stability and continuity.
By using the annotation information in the target function to obtain the data source rule expression, determine the candidate data source, and determine whether to switch to the main database based on the delay information of the candidate data source, realize dynamic data source switching, and dynamically use multiple parameters according to the business request parameters Determine the target data source.
It improves the stability and continuity of the system, adapts to system changes with flexibility, avoids abnormal application operation caused by data delays, and improves operation and maintenance support capabilities.
Smart Images

Figure CN118656356A
Abstract
Description
Technical Field
[0001] The present application relates to database switching technology, and more particularly to a data source determination method, device, equipment and computer-readable storage medium. Background Art
[0002] A database is an ordered collection of structured information or data, generally stored in a computer system in electronic form. As the underlying resource of a system, a database is usually the bottleneck of a system. For different scenarios, different databases can be used according to business needs to improve system performance. Therefore, it is necessary to switch the current database to another database. Summary of the invention
[0003] The embodiments of the present application provide a data source determination method, device, and computer-readable storage medium, which can dynamically detect and switch data sources through annotation information and data source rule expressions, realize real-time dynamic changes in data sources, and improve system operation and maintenance assurance capabilities.
[0004] The technical solution of the embodiment of the present application is implemented as follows:
[0005] The present application provides a method for determining a data source, the method comprising:
[0006] During the running of the application, when a target function with annotation information is executed, a data source rule expression is obtained from the annotation information; when the data source rule expression is not empty, a candidate data source is determined based on the data source rule expression; the main database information is obtained from the annotation information, and when the candidate data source is different from the main database and meets the data delay detection condition, the data delay information corresponding to the candidate data source is obtained; when the data delay information indicates that there is no data delay in the candidate data source, the candidate data source is determined as a target data source, and the target data source is used to execute the target function.
[0007] The present application provides a data source determination device, including:
[0008] An acquisition module is used to acquire a data source rule expression from the annotation information when executing a target function with annotation information during the running of the application; a determination module is used to determine a candidate data source based on the data source rule expression when the data source rule expression is not empty; the acquisition module is also used to acquire the main database information from the annotation information, and when the candidate data source is different from the main database and satisfies the data delay detection condition, acquire the data delay information corresponding to the candidate data source; the determination module is also used to determine the candidate data source as the target data source when the data delay information indicates that the candidate data source has no data delay, and the target data source is used to execute the target function.
[0009] An embodiment of the present application provides an electronic device, the electronic device comprising:
[0010] The memory is used to store computer executable instructions; the processor is used to implement the data source determination method provided in the embodiment of the present application when executing the computer executable instructions stored in the memory.
[0011] An embodiment of the present application provides a computer-readable storage medium storing a computer program or computer-executable instructions for implementing the data source determination method provided in the embodiment of the present application when executed by a processor.
[0012] The embodiments of the present application have the following beneficial effects:
[0013] During the application operation, the candidate data source of the target function is determined by obtaining the annotation information when the target function is running. When the candidate data source is different from the main database and there is no data delay between the candidate data source and the main database, the target function is executed through the candidate data source, otherwise the target function is executed through the main database. That is to say, in the embodiment of the present application, the target data source is determined by the annotation information. Since the modification and setting of the annotation information are more flexible, the target data source determined by the annotation information can adapt to various changes in the system or application and has higher flexibility. By detecting the delay between the candidate data source and the main database, it is possible to switch to the main database when the candidate data source is delayed, thereby avoiding abnormal application operation caused by data delay and improving system stability and continuity. BRIEF DESCRIPTION OF THE DRAWINGS
[0014] Figure 1 is a schematic diagram of the architecture of a data source determination system 100 provided in an embodiment of the present application;
[0015] Figure 2 is a schematic diagram of the structure of the server 200 provided in an embodiment of the present application;
[0016] Figure 3It is a schematic diagram of the implementation process of the data source determination method provided in the embodiment of the present application;
[0017] Figure 4 It is a schematic diagram of an implementation flow of determining the delay duration of a data table to be checked provided in an embodiment of the present application;
[0018] Figure 5 It is a schematic diagram of an implementation flow of determining the delay duration of a system monitoring table provided in an embodiment of the present application;
[0019] Figure 6 It is a schematic diagram of the implementation process of obtaining annotation information corresponding to the target function provided in an embodiment of the present application;
[0020] Figure 7 It is a schematic diagram of the implementation process of updating annotation parameter values based on grouping dimensions provided by an embodiment of the present application;
[0021] Figure 8 is a flow chart of a method for detecting table data delay provided by an embodiment of the present application;
[0022] Fig. 9 is a flow chart of a timing detection method provided by an embodiment of the present application;
[0023] Fig.10 A schematic diagram of the reporting and execution process of the data source determination method provided in an embodiment of the present application. DETAILED DESCRIPTION
[0024] In order to make the purpose, technical solutions and advantages of the present application clearer, the present application will be further described in detail below in conjunction with the accompanying drawings. The described embodiments should not be regarded as limiting the present application. All other embodiments obtained by ordinary technicians in the field without making creative work are within the scope of protection of this application.
[0025] In the following description, reference is made to “some embodiments”, which describe a subset of all possible embodiments, but it can be understood that “some embodiments” may be the same subset or different subsets of all possible embodiments and may be combined with each other without conflict.
[0026] In the following description, the terms "first\second\third" involved are merely used to distinguish similar objects and do not represent a specific ordering of the objects. It can be understood that "first\second\third" can be interchanged with a specific order or sequence where permitted, so that the embodiments of the present application described herein can be implemented in an order other than that illustrated or described herein.
[0027] Unless otherwise defined, all technical and scientific terms used in the embodiments of the present application have the same meanings as those commonly understood by those skilled in the art. The terms used in the embodiments of the present application are only for the purpose of describing the embodiments of the present application and are not intended to limit the present application.
[0028] Before further describing the embodiments of the present application in detail, the nouns and terms involved in the embodiments of the present application are explained. The nouns and terms involved in the embodiments of the present application are subject to the following interpretations.
[0029] 1. Data source: refers to the component that provides data storage services, such as MySQL database, Oracle database, etc.
[0030] 2. Transaction: A series of user-defined operations that execute Structured Query Language (SQL). These operations are either executed completely or not executed at all, and are an indivisible work execution unit.
[0031] 3. Spring Framework: A lightweight Java development framework that aims to solve the coupling problem between the business logic layer and other layers of enterprise-level application development. It is a layered, one-stop, lightweight open source framework that provides comprehensive infrastructure support for the development of Java applications.
[0032] In the related art, the switching of the database includes the following methods:
[0033] Solution 1: Determine the target data source based on the data source identifier input by the user and the preset data source mapping rule.
[0034] Solution 2: Intercept the business request, read the key of the data source to be operated corresponding to the business request; obtain the key of each data source from the context of each data source, and determine the data source to be operated corresponding to the business request by comparing the key of the data source to be operated with the key of each data source. Switch the current data source to the data source to be operated corresponding to the business request, and switch the current data source to the initial data source after the business request processing is completed.
[0035] Solution 3: Monitor the preset operation method of the data access object (DAO) layer, and determine the category of the preset operation method according to the preset annotation carried by the preset operation method; when the preset operation method is determined to be a query operation, obtain the delayed data difference threshold corresponding to the query operation from the preset annotation; obtain the largest first data record identifier in the current primary database and the standby database, calculate the difference between the two largest first data record identifiers obtained, and compare it with the delayed data difference threshold; if the difference is greater than or equal to the delayed data difference threshold, route the corresponding query operation to the primary database.
[0036] By analyzing the above methods, it is found that the following problems exist in the related technologies:
[0037] 1. In the master-slave database switching scenario, the ideal situation is to use the slave database for query and the master database for writing. However, if the slave database is delayed and some business scenarios do not allow data delay, it is necessary to automatically switch back to the master database for query. Therefore, it is necessary to determine whether the slave database data is delayed or how long it is delayed, and to automatically determine the target data source based on dynamic rules. However, the above methods cannot solve this problem.
[0038] 2. In the above solutions 1 and 2, the data source is determined according to the identifier clearly specified in the business request, and it is impossible to dynamically calculate the data source to be accessed according to multiple parameters of the business request parameters.
[0039] Since the master-slave replication of the database takes a certain amount of time, the data queried from the database may not be the latest data. Different business scenarios have different tolerances for data delays. Therefore, it is necessary to formulate corresponding rules according to different scenarios and determine whether the database currently queried by the business is the master database or the slave database based on the database status.
[0040] To solve the above problems, the embodiments of the present application provide a data source determination method, device, equipment and computer-readable storage medium. On the one hand, when the embodiments of the present application execute to the target function with annotation information, the candidate data source can be determined according to the data source rule expression carried in the annotation information, and further based on the main database information in the annotation information and the data delay information of the candidate data source, when it is determined that the candidate data source does not have data delay, the candidate data source is determined as the target data source, and the target data source can be automatically determined according to the data source rule expression. On the other hand, the embodiments of the present application dynamically determine the target data source to be accessed according to multiple parameters of the business request parameters through the data source rule expression, the main database information in the annotation information, and the data delay information corresponding to the candidate data source, solving the problem in the related art that the data source can only be determined according to the identifier clearly specified by the business request. Therefore, the system stability and continuity are improved, and the system operation and maintenance guarantee capability is improved.
[0041] The following describes an exemplary application of the electronic device provided by the embodiment of the present application. The device provided by the embodiment of the present application can be implemented as a laptop, a tablet computer, a desktop computer, a set-top box, a mobile device (for example, a mobile phone, a portable music player, a personal digital assistant, a dedicated messaging device, a portable gaming device), a smart phone, a smart speaker, a smart watch, a smart TV, a vehicle-mounted terminal and other types of user terminals, and can also be implemented as a server. The following describes an exemplary application when the device is implemented as a server.
[0042] See also Figure 1 , Figure 1 is a schematic diagram of the architecture of a data source determination system 100 provided in an embodiment of the present application, Figure 1 The server 200, network 300, terminal 400, candidate data source 500 and main database 600 are involved. Terminal 400 includes application 401. Server 200 and terminal 400 are connected through network 300. Network 300 can be a wide area network or a local area network, or a combination of the two.
[0043] In some embodiments, during the operation of the application 401 of the terminal 400, a communication connection is established with the server 200 through the network 300. When the target function with annotation information is executed during the operation of the application, the server 200 obtains the data source rule expression from the annotation information, and when the data source rule expression is not empty, the candidate data source is determined based on the data source rule expression, and the main database information is obtained from the annotation information. When the candidate data source is different from the main database and data delay needs to be detected, the data delay information corresponding to the candidate data source is obtained. When there is no data delay in the candidate data source, the candidate data source 500 is used to execute the target function. Otherwise, the server 200 will use the main database 600 to execute the target function.
[0044] See also Figure 2 , Figure 2 is a schematic diagram of the structure of the server 200 provided in an embodiment of the present application, Figure 2 The server 200 shown includes: at least one processor 210, a memory 230 and at least one network interface 220. The various components in the server 200 are coupled together via a bus system 240. It is understood that the bus system 240 is used to achieve connection and communication between these components. In addition to the data bus, the bus system 240 also includes a power bus, a control bus and a status signal bus. However, for the sake of clarity, the bus system 240 is not described in detail. Figure 2 Various buses are labeled as bus system 240 .
[0045] The processor 210 can be an integrated circuit chip with signal processing capabilities, such as a general-purpose processor, a digital signal processor (DSP), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc., where the general-purpose processor can be a microprocessor or any conventional processor, etc.
[0046] The memory 230 may be removable, non-removable, or a combination thereof. Exemplary hardware devices include solid-state memory, hard disk drives, optical disk drives, etc. The memory 230 may optionally include one or more storage devices that are physically remote from the processor 210.
[0047] The memory 230 includes a volatile memory or a non-volatile memory, and may also include both volatile and non-volatile memories. The non-volatile memory may be a read-only memory (ROM), and the volatile memory may be a random access memory (RAM). The memory 230 described in the embodiments of the present application is intended to include any suitable type of memory.
[0048] In some embodiments, memory 230 can store data to support various operations, examples of which include programs, modules, and data structures, or a subset or superset thereof, as exemplarily described below.
[0049] Operating system 231, including system programs for processing various basic system services and performing hardware-related tasks, such as a framework layer, a core library layer, a driver layer, etc., for implementing various basic services and processing hardware-based tasks;
[0050] The network communication module 232 is used to reach other electronic devices via one or more (wired or wireless) network interfaces 220. Exemplary network interfaces 220 include: Bluetooth, Wireless Authentication (WiFi), and Universal Serial Bus (USB).
[0051] In some embodiments, the device provided in the embodiments of the present application can be implemented in software. Figure 2 The data source determination device 233 stored in the memory 230 is shown to include the following software modules: an acquisition module 2331 and a determination module 2332. These modules are logical, and thus can be arbitrarily combined or further split according to the functions implemented. The functions of each module will be described below.
[0052] In other embodiments, the device provided in the embodiments of the present application can be implemented in hardware. As an example, the device provided in the embodiments of the present application can be a processor in the form of a hardware decoding processor, which is programmed to execute the data source determination method provided in the embodiments of the present application. For example, the processor in the form of a hardware decoding processor can adopt one or more application specific integrated circuits (Application Specific Integrated Circuit, ASIC), digital signal processors (Digital Signal Processor, DSP), programmable logic devices (Programmable Logic Device, PLD), complex programmable logic devices (Complex Programmable Logic Device, CPLD), field programmable gate arrays (Field-Programmable Gate Array, FPGA) or other electronic components.
[0053] The data source determination method provided in the embodiment of the present application will be explained in combination with the exemplary application and implementation of the server provided in the embodiment of the present application.
[0054] The following describes the data source determination method provided in the embodiment of the present application. As mentioned above, the electronic device implementing the data source determination method in the embodiment of the present application may be a terminal, a server, or a combination of the two. Therefore, the execution subject of each step will not be repeatedly described below.
[0055] It should be noted that in the example of the data source determination method below, the data source switching scenario during application operation is used as an example. Based on the understanding of the following text, those skilled in the art can apply the data source determination method provided in the embodiment of the present application to the processing of other scenarios including the need to switch data sources.
[0056] See also Figure 3 , Figure 3 is a schematic diagram of the implementation flow of the data source determination method provided in the embodiment of the present application, which will be combined with Figure 3 The steps shown are explained.
[0057] In step 101, during the running of the application, when a target function with annotation information is executed, a data source rule expression is obtained from the annotation information.
[0058] Here, the annotation information includes all annotated parameters of the target function and the parameter values of each parameter. The annotation parameters in the annotation information may include: masterKey, the parameter corresponding to the masterKey parameter to the master database identifier (masterKey), the target data source identifier (value), whether to detect data delay flag (isCheckDataDelay), the table identifier of the data table to be detected (checkTables), the group identifier (group) and the data source rule expression (exp). The data source rule expression is pre-set and is used to determine the expression of the data source according to the annotation information.
[0059] In some embodiments, the table identifier of the data table to be detected includes the table to be detected. The table here can be the table name involved in the embodiments of the present application, or it can be a library-level monitoring table, that is, detecting whether the library-level data has a delay.
[0060] Exemplarily, the annotation information of the target function may be: {“value”: “salve”, “masterKey”: “master”, “isCheckDataDelay”: true, “checkTables”: “sys_monitor_data”, “group”: “testGroup}, where “salve” indicates the database corresponding to the candidate database identifier, and “master” indicates the database corresponding to the master database identifier.
[0061] In step 102, when the data source rule expression is not empty, a candidate data source is determined based on the data source rule expression.
[0062] Here, by executing the data source rule expression, the candidate data source identifier of the data source to which the objective function needs to be switched can be determined by calculation according to the annotation information. Therefore, the candidate data source identifier can be used to determine the candidate data source to which the objective function needs to be switched.
[0063] In step 103, the main database information is obtained from the annotation information, and when the candidate data source is different from the main database and satisfies the data delay detection condition, the data delay information corresponding to the candidate data source is obtained.
[0064] Here, by comparing the main database identifier and the candidate data source identifier, it is determined whether the candidate data source and the main database are the same. If they are not the same, it means that the objective function needs to switch to a candidate data source different from the main database. In some embodiments, the annotation information includes whether to detect a data delay flag parameter (corresponding to the isCheckDataDelay parameter in other embodiments). The parameter is a Boolean type, that is, the logical value of the parameter can be true (true) or false (false). It is determined whether the data delay detection condition is met by judging whether the logical value of the data delay flag parameter is true. If the logical value of the data delay flag parameter is true, it means that the data delay needs to be detected. At this time, it is determined that the data delay detection condition is met, and the data delay information corresponding to the candidate data source is obtained. The data delay information of the candidate data source includes at least one delay duration corresponding to the data table to be detected. In some embodiments, if the logical value of the data delay flag parameter is false, it means that the data delay does not need to be detected. At this time, it is determined that the data delay detection condition is not met.
[0065] In some embodiments, obtaining the data delay information corresponding to the candidate data source in step 103 may be achieved by the following steps:
[0066] The first step is to obtain a table identifier of at least one data table to be checked from the annotation information.
[0067] The second step is to obtain the delay duration corresponding to each data table to be checked based on the table identifier.
[0068] Here, the delay duration corresponding to each data table to be checked is stored in the local cache space of the server, and each delay duration corresponds to a detection time. When obtaining the delay duration corresponding to each data table to be checked based on the table identifier, the delay duration corresponding to the latest detection time can be obtained from the local cache space of the server.
[0069] In the third step, when the delay durations corresponding to the data tables to be checked are greater than or equal to 0, it is determined that there is no data delay in the candidate data source.
[0070] In some embodiments, the delay duration corresponding to the data table to be detected is obtained by subtracting the latest update time of the data table to be detected in the main database from the latest update time of the data table to be detected in the candidate data source. If the delay duration is greater than or equal to 0, it means that the latest update time of the data table to be detected in the candidate data source is later than the latest update time of the data table to be detected in the main database, that is, there is no delay in the data of the data table to be checked stored in the candidate data source. When the delay duration corresponding to each data table to be checked is greater than or equal to 0, it means that there is no delay in the data in each data table to be checked in the candidate data source, that is, it is determined that there is no data delay in the candidate data source.
[0071] In some embodiments, when at least one of the delay durations corresponding to the data tables to be checked is less than 0, it is determined that there is data delay in the candidate data source; and the main database is determined as the target data source.
[0072] Here, when there is a delay in at least one of the data tables to be checked, it means that the data in at least one of the data tables to be checked in the candidate data sources is delayed compared to the main database. Therefore, in order to avoid affecting the operation of the target function, it is necessary to determine the main database as the target data source to avoid system operation failures caused by data source delays.
[0073] Continue to see Figure 3 In step 104, when the data delay information indicates that the candidate data source has no data delay, the candidate data source is determined as a target data source, and the target data source is used to execute the target function.
[0074] Here, if the data delay information indicates that there is no data delay between the candidate data source and the primary database, the candidate data source may be determined as the target data source.
[0075] In an embodiment of the present application, by obtaining the table identifier of the data table to be checked from the annotation information, the delay duration corresponding to each data table to be checked is detected. When the delay durations corresponding to all data tables to be checked are greater than or equal to 0, it is determined that there is no delay in the candidate data source, and the candidate data source is determined to be the target data source. If there is a delay in at least one data table to be checked, it means that there is a delay in the candidate data source, and the main data source is determined to be the target data source. In this way, it is possible to avoid the impact of the delay in the candidate data source on the operation of the target function, flexibly switch back to the main database to be determined as the target data source, and avoid system operation failures caused by data source delays.
[0076] In some embodiments, if there is currently no transaction in the local thread, the following steps are performed:
[0077] The first step is to save the identifier of the target data source into the local thread variable corresponding to the local thread when it is determined that no transaction currently exists in the local thread.
[0078] Here, the identifier of the target data source is saved in a local thread variable. For example, the transaction uses the capabilities of the spring framework. When a transaction is opened, a local thread of spring will record whether the current request has opened a transaction. Therefore, it is determined whether the transaction currently exists by judging whether the local thread has opened a transaction. Similarly, in other frameworks, a status value will be recorded when a transaction is opened, and it is determined whether a transaction currently exists based on the status. For example, when a transaction is not opened, the value saved by the local thread is false. When a transaction is opened, the value is set to true. By judging whether the value is true, it can be known whether a transaction currently exists.
[0079] The second step is to obtain the identifier of the target data source from the local thread variable before the target function establishes a connection with the target data source.
[0080] In some embodiments, when executing into the target function and executing a statement that needs to obtain data from the database (for example, executing a database query statement), it is determined that the target function needs to establish a connection with the target data source, and at this time, the target data source identifier is obtained from the local thread variable to obtain the target data source. Exemplarily, the target data source identifier can be: salve.
[0081] The third step is to establish a connection between the target function and the target data source to obtain data from the target database when executing the target function.
[0082] Here, by obtaining the target data source identifier from the local thread variable and determining the corresponding target database according to the target data source identifier, a database connection can be obtained from the target database, thereby obtaining data from the target database when executing the target function.
[0083] The related art has the following problem: since there is only one database connection, that is, one data source, in a transaction, in some cases, different SQL statements need to access different data sources in one transaction, and the related art does not have a solution to this problem.
[0084] Based on this, in other embodiments, when there is a current transaction and the data source used by the current transaction is different from the target data source, it indicates that the data source needs to be switched, and the following steps are performed:
[0085] The first step is to obtain the data source used by the current transaction when it is determined that there is a transaction in the local thread.
[0086] In some embodiments, the data source used by the current transaction may be determined by obtaining a data source identifier used by the current transaction from a local thread variable.
[0087] In the second step, when the target data source is different from the data source used by the current transaction, the identifier of the data source used by the current transaction is stored in a temporary variable.
[0088] Here, an identifier is used to determine whether the target data source is the same as the data source used by the current transaction. When a transaction already exists and the data source used by the transaction is different from the target data source, the data source identifier used by the current transaction is stored in a temporary variable, so that the data source used by the current transaction can be stored in a temporary variable so that the data source used by the current transaction can be called subsequently.
[0089] The third step is to delete the identifier of the data source used by the current transaction from the local thread variable and suspend the current transaction.
[0090] The fourth step is to save the target data source identifier to a local thread variable and start a new transaction.
[0091] Step 5: Execute the target function in a new transaction based on the target data source.
[0092] Here, since there will be only one database connection, that is, one data source, in a transaction, in order to switch the data source in a transaction, when the data source used by the current transaction is different from the target data source, a new transaction is started, and the target data source identifier is saved in the local thread variable before starting a new transaction, so that the data in the target data source can be used when executing the target function in the new transaction.
[0093] Among them, after executing the target function, the following steps can also be performed:
[0094] The sixth step is to save the identifier of the data source used by the current transaction stored in the temporary variable to the local thread variable, and execute the current transaction.
[0095] In some embodiments, when the target function is executed, the newly opened transaction can be ended, the target data source in the local thread variable is deleted, and the data source previously stored in the temporary variable and used by the current transaction is saved to the local thread variable. After the target function is executed, the data source used by the current transaction is switched back to achieve the purpose of switching the data source, so as to avoid the failure of the original current transaction due to the execution of the target function.
[0096] In the embodiment of the present application, since there is only one database connection, i.e., one data source, in a transaction, in order to switch the data source in a transaction, if the data source used by the current transaction is different from the target data source, a new transaction is started, and the target function is executed in the new transaction. After the target function is executed, the data source is switched back to the data source of the original transaction, so that the consistency of the transaction and the data source can be ensured, thereby ensuring that the function can be correctly executed in the transaction.
[0097] In some embodiments, when it is determined that a transaction currently exists and the target data source is the same as the data source used by the current transaction, it indicates that data source switching is not required, and the following steps are performed:
[0098] The first step is to obtain the identifier of the data source used by the current transaction from the local thread variable when the target data source is the same as the data source used by the current transaction.
[0099] Since the target data source is the same as the data source used by the current transaction, the data source used by the current transaction obtained from the local thread variable is also the target data source.
[0100] The second step is to establish a connection between the target function and the data source used by the current transaction to obtain data from the target data source when executing the target function.
[0101] Here, since the target data source is the same as the data source used by the current transaction, the target function can directly call the data source used by the current transaction.
[0102] In an embodiment of the present application, when it is determined that a transaction currently exists and the target data source is the same as the data source used by the current transaction, there is no need to switch the data source, and a connection can be directly established between the target function and the data source used by the current transaction, so that the target function can directly call the data source used by the current transaction, thereby saving system computing resources and improving the application running speed.
[0103] In some embodiments, when the data source rule expression is empty, or the candidate data source is different from the primary database and does not meet the data delay detection condition, the target data source is determined based on the annotation information.
[0104] Here, if the data source rule expression of the target function is empty, it means that the data source rule expression is not set. In this case, the target data source identifier can be obtained from the annotation information to determine the target data source and execute the target function based on the target data source. If the candidate data source in the data source rule expression is different from the primary database but there is no need to detect data delay, the target data source can also be directly obtained from the annotation information and the target function can be executed based on the target data source.
[0105] In the embodiment of the present application, when the data source rule expression is empty, or when the candidate data source is different from the main database and does not need to detect data delay, the target data source is obtained from the annotation information, and the target function is executed through the target data source. In this way, it is possible to avoid excessive detection of functions with high tolerance to data delay, avoid wasting system computing resources, and improve the efficiency and flexibility of data source determination.
[0106] In some embodiments, before the above step 101, the following steps may be performed: Figure 4 Steps 201 to 204 shown in the figure determine the delay time of the data table to be checked. Figure 4 The steps shown are explained.
[0107] In step 201, when it is detected that a data update occurs in a to-be-checked data table in a main database, a first update index and a first update time of the update are recorded in the main database.
[0108] Here, a preset data table to be checked is obtained, and the data table to be checked is set according to the actual business scenario requirements. An index can be established for the update time field of the data table to be checked in the main database. When a data update is detected in the data table to be checked in the main database, the first update index of this update is determined based on the first historical update index of the last update, and the current time when the data is updated is determined as the first update time.
[0109] In some embodiments, each time a data update occurs, the index value corresponding to the update time field is increased by 1, and the index value corresponding to the update time field indicates the number of updates this time. For example, if the first historical update index of the last update of the data table to be checked in the main database is 8, then the first update index of this update is 9, and the first update time is the current time when the data is updated, which is assumed to be 12:00:00 on November 28, 2023.
[0110] In step 202, when it is detected that a data update occurs in the to-be-checked data table in the slave database, a second update index and a second update time of the update are recorded in the slave database.
[0111] Here, similar to the implementation process of step 201, an index can be established for the update time field of the data table to be checked in the subordinate database. When a data update is detected in the data table to be checked in the subordinate database, the second update index of this update is determined based on the second historical update index of the last update, and the current time when the data is updated is determined as the second update time.
[0112] Exemplarily, the second historical update index of the last update of the to-be-checked data table of the main database is 8, then the second update index of this update is 9, and the second update time is the current time when the data is updated this time, which is assumed to be 12:00:02 on November 28, 2023.
[0113] In step 203, when the data delay detection opportunity is reached, the first maximum update time of the data table to be checked is obtained from the master database, and the second maximum update time of the data table to be checked is obtained from the slave database.
[0114] Here, the data delay detection condition can be a detection condition triggered by a preset timed task, such as a timed detection at regular intervals (such as 1 second, 10 seconds, etc.). The interval time can be determined according to the accuracy of the delayed data required by the business scenario. If the accuracy requirement is high, the time interval of the timed task is set as small as possible to achieve rapid detection. The first maximum update time can be the first update time corresponding to the maximum first update index. Similarly, the second maximum update time can be the second update time corresponding to the maximum second update index.
[0115] In step 204, the difference between the second maximum update time and the first maximum update time is determined as the delay time, and the delay time and the detection time corresponding to the data table to be checked are stored.
[0116] Exemplarily, a difference obtained by subtracting the first maximum update time from the second maximum update time is used as the delay duration, and the detection time corresponds to the time for data delay detection.
[0117] In an embodiment of the present application, when a data update is detected in the data table to be checked in the master database, the first update index and the first update time of this update are recorded, and the second update index and the second update time of this update are also recorded in the slave database. When the data delay detection condition is met, the delay duration is determined according to the difference between the second maximum update time and the first maximum update time, and the delay duration and the detection time corresponding to the data table to be checked are stored. In this way, the delay duration of the data to be checked can be obtained regularly, and the delay duration of the data to be checked can be updated in real time, thereby improving the detection efficiency and accuracy of the delay duration.
[0118] In some embodiments, before the above step 101, the following steps may be performed: Figure 5 Steps 301 to 306 shown in the figure determine the delay time of the system monitoring table. Figure 5 The steps shown are explained.
[0119] In step 301, in response to the received system monitoring table creation instruction, system monitoring tables are created in the master database and the slave database respectively.
[0120] Here, when a system monitoring table creation instruction is received, a system monitoring table will be established in the master database and the slave database respectively according to the system monitoring table creation instruction. By periodically detecting the system monitoring tables in the master database and the slave database, it is possible to determine at the library level whether there is data delay in the slave database.
[0121] In step 302, in response to the received data field adding instruction, the data field in the system monitoring table is obtained, and the data field at least includes a primary key identifier and an update time.
[0122] Here, the primary key identifier (primary key id) is used to represent the update record, the value of the primary key identifier can be 1, and the update time is used to represent the update time of the master database and the slave database at the database level.
[0123] In step 303, when it is determined that the update timing of the main database has arrived, the current time is determined as the first database update time of the main database.
[0124] Here, the update timing of the main database can be a pre-set scheduled task. The scheduled task is executed once every certain time (such as 1 second, 10 seconds, etc.). The interval time is determined according to the needs (for example, if the accuracy requirement is high, the scheduled task interval time is set as small as possible) to regularly reach the update timing of the main database, and update the update time corresponding to the primary key identifier. When implementing, the update time statement can be executed: (id=1, update sys_data set update_time=now() where id=1) to achieve the purpose of determining the current time as the first database update time of the main database. In other words, a record is always maintained in the system monitoring table of the main database. The value corresponding to the primary key identifier field of the record is 1, and the value corresponding to the update time field (that is, the first database update time) is the time when the update time statement was last executed. Exemplarily, the first database update time is 14:00:00 on November 28, 2023.
[0125] In step 304, when it is determined that the update timing of the subordinate database has been reached, the current time is determined as the second database update time of the subordinate database.
[0126] Here, the update timing of the subordinate database can be a pre-set scheduled task, which is executed once at a certain interval (such as 1 second, 10 seconds, etc.). The interval time is determined according to the needs (for example, if the accuracy requirement is high, the scheduled task interval time is set as small as possible). When the update timing of the subordinate database is reached on time, the subordinate database executes the update time statement and also determines the current time as the second database update time of the subordinate database.
[0127] Exemplarily, the master database and the slave database can be updated regularly through a scheduled task. The scheduled task is executed once every certain period of time (such as 1 second, 10 seconds, etc.), and the interval time is determined according to needs (for example, if the accuracy requirement is high, the scheduled task interval time is set as small as possible).
[0128] In step 305, when the data delay detection opportunity is reached, the first database update time of the system monitoring table is obtained from the master database, and the second database update time of the system monitoring table is obtained from the slave database.
[0129] In step 306, the difference between the second database update time and the first database update time is determined as the delay time corresponding to the system monitoring table, and the delay time and detection time corresponding to the system monitoring table are stored.
[0130] Here, the difference obtained by subtracting the first database update time from the second database update time is used as the delay duration corresponding to the system monitoring table, the corresponding time of the data delay detection is used as the detection time, and the delay duration and the detection time are stored.
[0131] In an embodiment of the present application, a system monitoring table is created in the master database and the slave database respectively, and the data fields in the system monitoring table are obtained. When it is determined that the update timing has arrived, the first database update time of the master database and the second database update time of the slave database are determined. When data delay detection is performed, the delay duration corresponding to the system monitoring table is determined and stored according to the first database update time and the second database update time. In this way, the delay duration of the system monitoring table can be obtained regularly, and the delay duration of the system monitoring table can be updated in real time, thereby improving the detection efficiency and accuracy of the delay duration of the system monitoring table.
[0132] In some embodiments, before the above step 101, the following steps may be performed: Figure 6 Steps 401 and 402 shown in the figure obtain annotation information corresponding to the objective function. Figure 6 The steps shown are explained.
[0133] In step 401, an objective function that satisfies a condition for switching a data source is obtained.
[0134] Here, the objective function that satisfies the condition for switching the data source is the objective function that requires switching the data source.
[0135] In step 402, in response to the received annotation input instruction, annotation information corresponding to the target function is obtained.
[0136] The annotation information includes: the main database identifier, the target data source identifier, the flag of whether to detect data delay, the table identifier of the data table to be detected, the group identifier and the data source rule expression.
[0137] Here, when the annotation input instruction is received, the annotation information is obtained: the main database identifier, the target data source identifier, the flag of whether to detect data delay, the table identifier of the data table to be detected, the group identifier and the data source rule expression, so as to annotate the target function using the obtained annotation information.
[0138] In the embodiment of the present application, by obtaining the annotation information corresponding to the target function: the main database identifier, the target data source identifier, whether to detect data delay flag, the table identifier of the data table to be detected, the group identifier and the data source rule expression, when executing the data source rule expression, the target data source identifier is dynamically determined according to the parameters in the annotation information, and the target data source identifier corresponding to the target function is calculated in real time, which improves the flexibility and stability of the target function when switching data sources.
[0139] The related technology has the following problem: In the scenario of using a master-slave database, the query operation on the same table or the same SQL statement can allow the slave database data to be delayed in some scenarios, but not in other scenarios, and requires classification or group control. However, the above solution 3 detects the underlying data and cannot achieve classification or group control based on the scenario.
[0140] Based on this, in some embodiments, after the above step 402, when an application start instruction is received, the following steps are also included:
[0141] The first step is to obtain the function signature and annotation information of the target function with annotation information in the application in response to the received application startup instruction.
[0142] Here, the function signature includes information such as the full path of the class where the target function is located, the target function name, and the target function parameters. The function signature can uniquely identify the target function in the application and is unique.
[0143] The second step is to store the function signature and annotation information of the target function into the local cache space.
[0144] Here, the annotation information is stored in the local cache space in the dimension of function signature, which can record the parameter values of the annotation information configured at the code level of the target function. The parameter values in the annotation information can be modified through the configuration center, and the changed parameter values can be pushed to all functions of the application. The parameter values in the annotation information of the specified function in the application instance cache are updated to achieve the purpose of dynamically modifying the annotation information.
[0145] In some embodiments, the data source rule expression is configured based on the function signature dimension, that is, the data source rule expression is configured based on the target function context parameters (including target function parameters, application parameters, and thread level parameters), and the target data source identifier is determined based on the result of executing the data source rule expression.
[0146] The third step is to upload the function signature and annotation information of the target function to the configuration center server; the configuration center server is used to store the function signature and annotation information of the target function, and update the annotation information according to the annotation information update instruction.
[0147] In some embodiments, the function signature and annotation information of the target function may be modified in the configuration center server, and the function signature and annotation information of the target function may also be pushed to a cache for storage through the configuration center server.
[0148] In an embodiment of the present application, the target functions that can uniformly control the switching of data sources can be divided into the same group according to the needs of functions or scenarios. In this way, the data source rule expression can be modified in the group dimension, and the modified data source rule expression can be pushed to the application instance in the configuration center. The application updates the data source rule expression of all functions under each group according to this grouping, avoiding the need to modify the data source rule expression for each target function in the configuration center, reducing system resource consumption and improving system efficiency.
[0149] In some embodiments, see Figure 7 , Figure 7 This is a schematic diagram of the implementation process of updating the annotation parameter value by grouping dimension provided by the embodiment of the present application, which will be combined with Figure 7 The steps shown are explained.
[0150] In step 501, the configuration center server stores the received function signature and annotation information of the target function, and establishes a mapping relationship between the group identifier and the function signature based on the group identifier included in the annotation information.
[0151] Here, the group identifier included in the annotation information is used to determine the function signature associated with the group identifier and establish a mapping relationship between the group identifier and the function signature so that the group identifier and the function signature are associated with each other. In some embodiments, the group identifier and the function signature can be a one-to-one mapping relationship or a one-to-many mapping relationship.
[0152] For example, for query operations on the same table or the same structured query language, in some scenarios, data delay from the database may be allowed, while in other scenarios, data delay from the database is not allowed, so classification or grouping is required to control the objective function.
[0153] In step 502, the configuration center server responds to the received annotation information update instruction for the target group identifier and obtains the target function signature corresponding to the target group identifier based on the mapping relationship between the group identifier and the function signature.
[0154] Here, through the mapping relationship between the group identifier and the function signature, the target function signature corresponding to the target group identifier is determined in the mapping relationship.
[0155] Here, the target function signature includes the full path of the class where the target function is located, the target function name, the target function parameters, etc. The target function signature can uniquely identify the target function in the application and has uniqueness in the application dimension.
[0156] In step 503, the annotation parameter to be updated and the target parameter value of the annotation parameter to be updated carried in the annotation information update instruction are obtained.
[0157] Here, the annotation parameter to be modified is determined by the annotation parameter to be updated, and the annotation parameter to be modified is updated by the target parameter value of the annotation parameter to be updated.
[0158] Exemplarily, the annotation parameters to be updated include: isCheckDataDelay, and the target parameter value of the annotation parameters to be updated includes: flash, then the parameter value of isCheckDataDelay in the annotation parameters can be updated to flash.
[0159] In step 504, the parameter value of the annotation parameter to be updated in the annotation information corresponding to the target function signature is updated to the target parameter value.
[0160] In step 505 , the target function signature, the annotation parameter to be updated, and the target parameter value of the annotation parameter to be updated are sent to the application instance.
[0161] Here, the parameter value corresponding to the target function signature is modified through the configuration center, the changed parameter value is pushed to all application instances, and the annotation parameter value of the specified method in the application instance cache is updated to realize the modification of the annotation parameter under the grouping dimension.
[0162] In an embodiment of the present application, by adding annotation information to the target function, the data source switching of the target function is dynamically controlled through the data source rule expression, and the implementation method of switching the data source is accurately controlled. In this way, the candidate data source can be determined by annotation information to adapt to various changes in the system, and the flexibility is higher. By detecting the delay between the candidate data source and the main database, it is possible to switch to the main database when the candidate data source is delayed, avoiding abnormal application operation caused by the delay of the candidate data source, and improving the stability and continuity of the system.
[0163] The method provided in the embodiments of the present invention achieves the following effects:
[0164] 1. By adjusting the annotation parameters of data source switching through the configuration center dynamic change method, the data source of the target function can be changed dynamically in real time, thus improving the system operation and maintenance guarantee capability.
[0165] 2. By switching the data source by grouping the target functions, the data source can be switched quickly, which shortens the switching time and improves the system operation and maintenance guarantee capability.
[0166] 3. The target data source for executing the target function is determined by the data source rule expression, which can adapt to various changes in the system and is more flexible. In addition, when the business corresponding to the target function changes, there is no need to upgrade the code, so that it can quickly adapt to the business.
[0167] 4. By detecting the delay between the candidate data source and the main database, the system can automatically switch back to the main database when the candidate data source is delayed, thus avoiding problems with the target function caused by data delays in the candidate data source, improving system stability, and ensuring the continuity of application operation.
[0168] 5. Implemented the switch to another data source in the current transaction, solving the problem that switching data source in a transaction is invalid or ineffective.
[0169] The following will describe an exemplary application of the present application embodiment in an actual application scenario. Taking the master-slave database switching scenario as an example (where the master database corresponds to the above master database, and the slave database corresponds to the above candidate data source), in order to relieve the pressure on the database server, the master-slave mode is usually used to build the database. Figure 8 , Figure 8 This is a flow chart of a method for detecting table data delays provided by an embodiment of the present application. A scheduled task at the table level detects whether there is a delay in the data of a specified table in the database, wherein, before the detection, the update time field of the table to be detected needs to be indexed (corresponding to the above-mentioned first update index and second update index), and each time the data is updated, its value is determined to be the time of the update (corresponding to the above-mentioned first update time and second update time). The detection is performed at regular intervals (such as 1 second, 10 seconds, etc.), and the interval time is determined according to the data accuracy required by the business scenario. If the accuracy requirement is high, the scheduled task interval time is set as small as possible to achieve rapid detection. Detecting data delays specifically includes the following steps 801 to 804:
[0170] The process of detecting table data delay is started by triggering a scheduled task.
[0171] In step 801, the latest update time of a specified table in the main database is queried.
[0172] Here, the maximum update time masterMaxUpdateTime (corresponding to the first maximum update time mentioned above) of the specified table table is queried in the master database.
[0173] In step 802, the latest update time of the specified table is queried from the database.
[0174] Here, the maximum update time slaveMaxUpdateTime (corresponding to the second maximum update time mentioned above) of the specified table table is queried from the database.
[0175] In step 803, the time difference (corresponding to the above-mentioned delay time) is calculated.
[0176] Here, by subtracting the latest update time of the master database from the latest update time of the slave database, the time difference val (in milliseconds) is obtained. If val is greater than or equal to 0, there is no delay in the table. Otherwise, there is a delay, and the delay time is the absolute value of val.
[0177] In step 804, the time difference is saved with a table name tag.
[0178] Here, the delay duration and detection time of the label table of the specified table are stored in a cache, such as a remote dictionary server (REmote DIctionary Server, Redis), with the table name label table as the key, and the saved values include the detection time (in the format of yyyyMMddHHmmssSSS) and the time difference val (in milliseconds).
[0179] Finally, the method flow for detecting table data delay ends.
[0180] See also Fig. 9 , Fig. 9 This is a flow chart of a method for timing detection provided by an embodiment of the present application. The method for controlling the timing detection starts with a timing task trigger 901, and the update time is set to the current time 902 during each detection, and is sent to the master database 903 and then synchronized to the slave database 904, completing a timing detection after the end.
[0181] In some embodiments, a scheduled task at the library level detects whether there is a delay from the database. First, a library-level monitoring table is established (such as sys_monitor_data, the master and slave databases will establish this table, corresponding to the above-mentioned system monitoring table), the fields include the primary key id (corresponding to the above-mentioned primary key identifier), update time update_time, etc., and a record is inserted by default (such as id=1). Then, a scheduled task is used to update the default record regularly (such as id=1, update sys_data setupdate_time=now()where id=1) to achieve the purpose of updating the time field. The scheduled task is executed once at a certain interval (such as 1 second, 10 seconds, etc.), and the interval time is determined according to needs. If the accuracy requirement is high, the interval time of the scheduled task is set as small as possible. Among them, at the library level, a scheduled task is used to detect whether there is a delay in the slave database data, and then the following is executed. Figure 8The steps shown are not repeated here.
[0182] See also Fig.10 , Fig.10 A schematic diagram of the reporting and execution flow of the data source determination method provided in the embodiment of the present application. Define the data source annotation DrRoute (corresponding to the above annotation information), and the parameters include masterKey (the main database data source name key, corresponding to the above main database identifier), value (the target data source key, corresponding to the above target data source identifier), isCheckDataDelay (whether to detect data delay, corresponding to the above whether to detect data delay flag), checkTables (which tables to detect, corresponding to the table identifier of the above data table to be detected), group (group code, corresponding to the above group identifier), exp (rule expression, corresponding to the above data source rule expression), etc. Among them, checkTables represents the table to be detected, and the table can be the table name involved in the embodiment of the present application, or it can be the library-level monitoring table sys_monitor_data (i.e., whether there is a delay at the library level). Add an annotation to the method that needs to switch the data source, intercept the annotation through the aspect-oriented programming (Spring Aspect Orient Programming, Spring AOP) technology, and parse the parameter value of the annotation and the parameter value of the corresponding method. Specifically include the following steps of reporting process and execution process, wherein the reporting process includes the following steps 1001 to 1004:
[0183] In step 1001, application startup is completed.
[0184] In step 1002, the application annotation method is scanned.
[0185] Here, after the application is started, all methods annotated with DrRoute in the current application are scanned by Spring (corresponding to the above target function).
[0186] In step 1003, the method signature and annotation parameters are obtained.
[0187] Here, by scanning all the methods in the current application that are configured with annotations, the corresponding method signature (corresponding to the above function signature) and annotation parameters (corresponding to the above annotation information) are obtained. The content of the method signature includes the full path of the class where the method is located, the method name, the method parameters, etc. This method signature can uniquely identify the method in the application and has the uniqueness of the application dimension. The annotation parameters include all the parameters of the annotated DrRoute.
[0188] At the same time, the obtained information is reported to the configuration center 101 (corresponding to the above-mentioned configuration center server), the annotation parameters can be modified in the configuration center 101, and then the annotation parameters in the configuration center are pushed to the method switching data source cache 102, specifically including: reporting in a lightweight data exchange format (JavaScript Object Notation, json), saving the corresponding annotation parameter values in the method signature dimension in the configuration center, and saving the mapping relationship between the group code (code) and the method signature.
[0189] The purpose of saving the annotation parameter values in the method signature dimension is to record the annotation parameter values configured at the code level, and to modify the annotation parameter values through the configuration center, push the changed annotation parameter values to all application instances, and update the annotation parameter values of the specified method in the application instance cache to achieve the purpose of dynamically modifying the annotation parameters. At the same time, the data source rule expression is configured in the method signature dimension, that is, according to the method context parameters (including: method parameters, application parameters, thread-level parameters), the data source rule expression (expression that complies with the spring el specification) is configured, and the data source to be used is determined according to the result of executing the data source rule expression.
[0190] The specific process of configuring the data source rule expression with the method signature dimension includes: using the method signature as the key and the value as the data source rule (the json format data of the parameter in the data source annotation DrRoute) configured in the configuration center (e.g., the method signature com.msxf.mbp.ApplyService.exeute={}). The specific content of the data source rule includes: the json format data of the parameter in the data source annotation DrRoute.
[0191] The purpose of maintaining the mapping relationship between group code and method signature is: in the configuration center, change the annotation parameters in the group dimension, push the changes to all application instances, and complete the annotation parameter changes of all methods under the group. For example, switching the data source can achieve the purpose of fast switching of categories. That is, in the group dimension, change the annotation value parameter value of all methods under the group to the specified data source key, push the changes to all application instances to complete the data source switch.
[0192] In some embodiments, positioning can also be performed in other ways, such as reporting all or part of the methods under the class to the configuration center based on the class dimension, and directly configuring the parameters for switching the data source in the configuration center (i.e., these parameters are not configured by default in the code).
[0193] In step 1004, the method signature and annotation parameters are cached locally.
[0194] Here, the information reported to the configuration center 101 is synchronously cached locally and written into the switching data source cache 102 .
[0195] The execution process includes the following steps 1005 to 1009:
[0196] In step 1005, the annotation method is intercepted.
[0197] Here, when the method with the annotation DrRoute (corresponding to the above target function) is executed, the method can be intercepted through the SpringAOP mechanism.
[0198] In step 1006, the annotated method of this application is scanned to obtain the method signature.
[0199] In step 1007, the annotation parameters of the method signature are obtained from the cache.
[0200] Here, the annotation parameters corresponding to the method signature are obtained in the method switching data cache 102 according to the method signature, including: the latest annotation parameters and the data source rule expression.
[0201] In step 1008, it is determined whether to execute the primary data source.
[0202] Here, if the data source rule expression is empty, it means that the target data source is the value of the parameter value in the annotation parameter; if the expression is not empty, it means that the target data source needs to be determined by executing the data source rule expression. By judging whether the value of the target data source (corresponding to the above candidate database identifier) is equal to the value of the annotation parameter masterKey (corresponding to the above master database identifier), if they are equal, it means that the current method uses the master data source, and the judgment result is yes, and step 10013 is executed. If the value of the target data source is not equal to the value of the annotation parameter masterKey, the judgment result is no, and step 1009 is executed.
[0203] In step 1009, it is determined whether a delay is detected.
[0204] Here, it is determined whether the value of the annotation parameter isCheckDataDelay is logically true (true). If it is not true, it means that delay detection is not required. At this time, the data source specified by value (corresponding to the database corresponding to the above target data source identifier) is executed, and step 10012 is executed. If it is true, it means that delay detection is required, and step 10010 is executed.
[0205] In step 10010, the time difference val of the corresponding table is obtained from the cache.
[0206] Here, the time difference val (corresponding to the above delay duration) of the table specified by the annotation parameter checkTables is obtained from the cache.
[0207] In step 10011, determine whether there is data delay.
[0208] Here, if the time difference val of all tables is greater than or equal to 0, it means that there is no data delay, the judgment result is no, and step 10012 is executed. Otherwise, it means that there is data delay, the judgment result is yes, and step 10013 is executed.
[0209] In step 10012, the target data source specified by the annotation parameter value is switched.
[0210] In step 10013, the master data source specified by the annotation parameter masterKey is switched.
[0211] Here, the specific implementation method of switching the data source is: save the target data source to the local thread variable; when the target method establishes a connection, obtain the target data source key from the local thread, and obtain the actual data source object according to the key, and then obtain a database connection from the data source, thereby realizing the switching of the data source.
[0212] The implementation method of switching data sources in a transaction is as follows: take the data source of the current transaction from the local thread variable and save it in a temporary variable; set the target data source of the current method to the local thread variable, start a new transaction, and let the target method execute in the new transaction environment; after the target method is executed, reset the data source of the original transaction in the temporary variable to the local thread variable, and the switch is completed.
[0213] In some embodiments, when switching data sources, it is also necessary to determine whether there is a transaction currently. If there is no transaction, it can be directly set as the target data source; if there is a transaction, it is determined whether the data source used by the current transaction is the target data source. If it is, it can be set as the target data source. If not, it is necessary to start a new transaction and let the target method execute in the new transaction to achieve the purpose of using the target data source in the target method.
[0214] The implementation method for determining whether there is a transaction is as follows: Since transactions use the capabilities of the spring framework, when a transaction is enabled, spring will have a local thread record whether the current request has opened a transaction. Therefore, it is determined whether there is a transaction by determining whether the local thread has opened a transaction. Similar to other frameworks, a status value is recorded when a transaction is enabled, and it is determined whether there is a transaction based on the status. For example, when a transaction is not enabled, the value saved by the local thread is false. When a transaction is enabled, the value is set to true. By determining whether the value is true, it is determined whether there is a transaction.
[0215] In the embodiments of the present application, the following effects are achieved by the above method:
[0216] 1. By dynamically changing the data source switching parameters of the configuration center, the purpose of real-time dynamic change can be achieved, thus improving the system operation and maintenance guarantee capability.
[0217] 2. Switching data sources in the grouping dimension can achieve fast switching, greatly shorten the switching time, and improve the system operation and maintenance guarantee capability. Among them, the grouping method includes: according to the needs of the business or specific scenarios, the methods that can be uniformly controlled and switched are divided into the same group, so that the data rules of the group dimension can be modified. The configuration center pushes the modified rules to the application instance. The instance updates the rules of all methods under this group according to this group, avoiding the need to modify the rules in the method dimension in the configuration center, reducing system resource consumption and improving system efficiency. Dynamic calculation of target data source rules includes: according to the exp (rule expression, i.e., spring el expression) in the data source annotation parameter, using the ability of spring, executing the expression through the parameters passed in when executing the method, and obtaining a result (i.e., the target data source). That is, when executing the expression, the target data source is dynamically determined according to the parameters of the method. Here, the priority of the target data source obtained by executing the expression is higher than the priority of the target data source specified in the annotation parameter.
[0218] 3. Using regular expressions to determine the data source can adapt to various changes in the system with greater flexibility, achieving the purpose of quickly responding to business changes without upgrading the code.
[0219] 4. The delay between the target data source and the main data source is regularly detected through scheduled tasks. When the target data source is delayed, it can automatically switch back to the main data source, avoiding business problems caused by data delays in the target data source, improving system stability, and ensuring business continuity.
[0220] 5. It is possible to switch to another data source in the current transaction, solving the problem that switching data source in the transaction is invalid or ineffective.
[0221] The following is a description of an exemplary structure of the data source determining device 233 provided in the embodiment of the present application implemented as a software module. In some embodiments, Figure 2 As shown, the software modules stored in the data source determining device 233 of the memory 230 may include:
[0222] The acquisition module 2331 is used to acquire a data source rule expression from the annotation information when executing a target function with annotation information during the running of the application.
[0223] The determination module 2332 is used to determine a candidate data source based on the data source rule expression when the data source rule expression is not empty.
[0224] The acquisition module 2331 is further configured to acquire the primary database information from the annotation information, and when the candidate data source is different from the primary database and satisfies a data delay detection condition, acquire data delay information corresponding to the candidate data source.
[0225] The determination module 2332 is further configured to determine the candidate data source as a target data source when the data delay information indicates that the candidate data source has no data delay, and the target data source is used to execute the objective function.
[0226] In some embodiments, the acquisition module 2331 is further used to determine the target data source based on the annotation information when the data source rule expression is empty or the candidate data source is different from the primary database and does not meet the data delay detection condition.
[0227] In some embodiments, the acquisition module 2331 is further used to obtain the table identifier of at least one data table to be checked from the annotation information; based on the table identifier, obtain the delay duration corresponding to each of the data tables to be checked; correspondingly, the determination module 2332 is further used to determine that there is no data delay in the candidate data source when the delay durations corresponding to each of the data tables to be checked are greater than or equal to 0; when at least one of the delay durations corresponding to each of the data tables to be checked is less than 0, determine that there is data delay in the candidate data source, and determine the primary database as the target data source.
[0228] In some embodiments, the determination module 2332 is further used to save the identifier of the target data source to a local thread variable corresponding to the local thread when it is determined that no transaction currently exists in the local thread; obtain the identifier of the target data source from the local thread variable before the target function establishes a connection with the target data source; and establish a connection between the target function and the target data source to obtain data from the target database when executing the target function.
[0229] In some embodiments, the determination module 2332 is further used to, when it is determined that a transaction currently exists in the local thread, obtain a data source used by the current transaction; when the target data source is different from the data source used by the current transaction, store an identifier of the data source used by the current transaction in a temporary variable; delete the data source used by the current transaction from the local thread variable and suspend the current transaction; save the target data source to the local thread variable and start a new transaction; and execute the target function in the new transaction based on the target data source.
[0230] In some embodiments, the determination module 2332 is further used to save the identifier of the data source used by the current transaction stored in the temporary variable to the local thread variable after executing the target function, and execute the current transaction.
[0231] In some embodiments, the determination module 2332 is further used to obtain an identifier of the data source used by the current transaction from the local thread variable when the target data source is the same as the data source used by the current transaction; and establish a connection between the target function and the data source used by the current transaction to obtain data from the target data source when executing the target function.
[0232] In some embodiments, the determination module 2332 is also used to, when it is detected that a data update occurs in the data table to be checked in the master database, record the first update index and the first update time of this update in the master database; when it is detected that a data update occurs in the data table to be checked in the slave database, record the second update index and the second update time of this update in the slave database; when the data delay detection timing is reached, obtain the first maximum update time of the data table to be checked from the master database, and obtain the second maximum update time of the data table to be checked from the slave database; determine the difference between the second maximum update time and the first maximum update time as the delay duration, and store the delay duration and detection time corresponding to the data table to be checked.
[0233] In some embodiments, the determination module 2332 is also used to create a system monitoring table in the master database and the slave database respectively in response to a received system monitoring table creation instruction; in response to a received data field addition instruction, obtain the data field in the system monitoring table, the data field including at least a primary key identifier and an update time; when it is determined that the update timing of the master database has been reached, determine the current time as the first library update time of the master database; when it is determined that the update timing of the slave database has been reached, determine the current time as the second library update time of the slave database; when it is determined that the data delay detection timing has been reached, obtain the first library update time of the system monitoring table from the master database, and obtain the second library update time of the system monitoring table from the slave database; determine the difference between the second library update time and the first library update time as the delay duration corresponding to the system monitoring table, and store the delay duration and detection time corresponding to the system monitoring table.
[0234] In some embodiments, the acquisition module 2331 is also used to obtain a target function that meets the conditions for switching data sources; in response to a received annotation input instruction, obtain annotation information corresponding to the target function, the annotation information including: a primary database identifier, a target data source identifier, a flag for detecting data delays, a table identifier of a data table to be detected, a group identifier, and a data source rule expression.
[0235] In some embodiments, the acquisition module 2331 is also used to obtain the function signature and annotation information of the target function with annotation information in the application in response to the received application startup instruction; store the function signature and annotation information of the target function in the local cache space; and upload the function signature and annotation information of the target function to the configuration center server.
[0236] In some embodiments, the determination module 2332 is also used for the configuration center server to store the received function signature and annotation information of the target function, and to establish a mapping relationship between the group identifier and the function signature based on the group identifier included in the annotation information; the configuration center server responds to the received annotation information update instruction for the target group identifier, and based on the mapping relationship between the group identifier and the function signature, obtains the target function signature corresponding to the target group identifier; obtains the annotation parameters to be updated and the target parameter values of the annotation parameters to be updated carried by the annotation information update instruction; updates the parameter values of the annotation parameters to be updated in the annotation information corresponding to the target function signature to the target parameter value; and sends the target function signature, the annotation parameters to be updated and the target parameters of the annotation parameters to be updated to the application instance.
[0237] The embodiment of the present application provides a computer program product, which includes a computer program or a computer executable instruction, and the computer program or the computer executable instruction is stored in a computer-readable storage medium. The processor of the electronic device reads the computer executable instruction from the computer-readable storage medium, and the processor executes the computer executable instruction, so that the electronic device executes the data source determination method described in the embodiment of the present application.
[0238] The embodiment of the present application provides a computer-readable storage medium storing computer-executable instructions, wherein the computer-executable instructions or computer programs are stored. When the computer-executable instructions or computer programs are executed by a processor, the processor will be caused to execute the data source determination method provided in the embodiment of the present application, for example, Figure 3 The data source determination method shown.
[0239] In some embodiments, the computer-readable storage medium may be a memory such as RAM, ROM, flash memory, magnetic surface memory, optical disk, or CD-ROM; or may be various devices including one or any combination of the above memories.
[0240] In some embodiments, computer executable instructions may be in the form of a program, software, software module, script or code, written in any form of programming language (including compiled or interpreted languages, or declarative or procedural languages), and may be deployed in any form, including as a stand-alone program or as a module, component, subroutine or other unit suitable for use in a computing environment.
[0241] As an example, computer-executable instructions may, but need not, correspond to a file in a file system, may be stored as part of a file that stores other programs or data, such as in one or more scripts in a HyperText Markup Language (HTML) document, in a single file dedicated to the program in question, or in multiple coordinated files (e.g., files storing one or more modules, subroutines, or code portions).
[0242] As an example, computer executable instructions may be deployed to be executed on one electronic device, or on multiple electronic devices located at one site, or on multiple electronic devices distributed at multiple sites and interconnected by a communication network.
[0243] In summary, through the embodiments of the present application, it is possible to determine whether a candidate data source is delayed or how long the delay is, automatically switch the data source according to dynamic rules, classify or group the switching of data sources according to scenarios, and dynamically calculate the data source that needs to be accessed according to multiple parameters of the business request parameters. It also realizes switching to another data source in the current transaction, solves the problem of invalid or ineffective switching of data sources in transactions, and improves the system operation and maintenance guarantee capability.
[0244] The above is only an embodiment of the present application and is not intended to limit the protection scope of the present application. Any modifications, equivalent substitutions and improvements made within the spirit and scope of the present application are included in the protection scope of the present application.
Claims
1. A method for determining a data source, characterized in that: The method comprises: During the application running process, when a target function with annotation information is executed, a data source rule expression is obtained from the annotation information; When the data source rule expression is not empty, determining a candidate data source based on the data source rule expression; Obtaining the main database information from the annotation information, and obtaining the data delay information corresponding to the candidate data source when the candidate data source is different from the main database and satisfies the data delay detection condition; When the data delay information indicates that the candidate data source has no data delay, the candidate data source is determined as a target data source, and the target data source is used to execute the target function.
2. The method according to claim 1, characterized in that The method further comprises: When the data source rule expression is empty, or the candidate data source is different from the primary database and does not meet the data delay detection condition, the target data source is determined based on the annotation information.
3. The method according to claim 1, characterized in that The obtaining the data delay information corresponding to the candidate data source includes: Acquire at least one table identifier of a data table to be checked from the annotation information; Obtaining the delay time corresponding to each of the to-be-checked data tables based on the table identifier; When the delay durations corresponding to the to-be-checked data tables are all greater than or equal to 0, it is determined that there is no data delay in the candidate data source; When at least one of the delay durations corresponding to the to-be-checked data tables is less than 0, it is determined that the candidate data source has data delay, and the primary database is determined as the target data source.
4. The method according to claim 1, characterized in that The method further comprises: When it is determined that no transaction currently exists in the local thread, the identifier of the target data source is saved in a local thread variable corresponding to the local thread; Before the target function establishes a connection with the target data source, obtaining an identifier of the target data source from the local thread variable; A connection is established between the target function and the target data source based on the identifier of the target data source, so as to obtain data from the target database when executing the target function.
5. The method according to claim 4, characterized in that The method further comprises: When it is determined that a transaction currently exists in the local thread, obtaining a data source used by the current transaction; In the case where the target data source is different from the data source used by the current transaction, storing the identifier of the data source used by the current transaction in a temporary variable; Deleting the identifier of the data source used by the current transaction from the local thread variable, and suspending the current transaction; The identifier of the target data source is saved in a local thread variable, and a new transaction is started; Based on the target data source, the target function is executed in the new transaction.
6. The method according to claim 5, characterized in that The method further comprises: When the target function is executed, the identifier of the data source used by the current transaction stored in the temporary variable is saved in the local thread variable, and the current transaction is executed.
7. The method according to claim 5, characterized in that The method further comprises: When the target data source is the same as the data source used by the current transaction, obtaining an identifier of the data source used by the current transaction from the local thread variable; A connection is established between the target function and the data source used by the current transaction based on the identifier of the data source used by the current transaction, so as to obtain data from the target data source when executing the target function.
8. The method according to any one of claims 1 to 7, characterized in that: In the case where a target function with annotation information is executed during the application running process, before obtaining a data source rule expression from the annotation information, the method further includes: When it is detected that data in the to-be-checked data table in the master database is updated, a first update index and a first update time of the update are recorded in the master database; When it is detected that data in the to-be-checked data table in the subordinate database is updated, a second update index and a second update time of the update are recorded in the subordinate database; When the data delay detection opportunity is reached, obtaining a first maximum update time of the data table to be checked from the master database, and obtaining a second maximum update time of the data table to be checked from the slave database; The difference between the second maximum update time and the first maximum update time is determined as the delay time, and the delay time and the detection time corresponding to the to-be-checked data table are stored.
9. The method according to any one of claims 1 to 7, characterized in that: In the case where a target function with annotation information is executed during the application running process, before obtaining a data source rule expression from the annotation information, the method further includes: In response to the received system monitoring table creation instruction, create system monitoring tables in the master database and the slave database respectively; In response to the received data field adding instruction, obtaining the data field in the system monitoring table, wherein the data field at least includes a primary key identifier and an update time; When it is determined that the update timing of the main database has been reached, determining the current time as the first database update time of the main database; In the case of determining that the update timing of the subordinate database has been reached, determining the current time as the second database update time of the subordinate database; When the data delay detection opportunity is reached, obtaining a first database update time of the system monitoring table from the master database, and obtaining a second database update time of the system monitoring table from the slave database; The difference between the update time of the second database and the update time of the first database is determined as the delay duration corresponding to the system monitoring table, and the delay duration and detection time corresponding to the system monitoring table are stored.
10. The method according to any one of claims 1 to 7, characterized in that: In the case where a target function with annotation information is executed during the application running process, before obtaining a data source rule expression from the annotation information, the method further includes: Obtain the objective function that satisfies the conditions for switching data sources; In response to the received annotation input instruction, annotation information corresponding to the target function is obtained, the annotation information including: a main database identifier, a target data source identifier, a flag indicating whether to detect data delay, a table identifier of a data table to be detected, a group identifier and a data source rule expression.
11. The method according to claim 10, characterized in that After obtaining the annotation information corresponding to the target function in response to the received annotation input instruction, the method further includes: In response to the received application startup instruction, obtaining a function signature and annotation information of a target function with annotation information in the application; Storing the function signature and annotation information of the target function in a local cache space; The function signature and annotation information of the target function are uploaded to the configuration center server; the configuration center server is used to store the function signature and annotation information of the target function, and update the annotation information according to the annotation information update instruction.
12. A data source determination device, characterized in that: The device comprises: An acquisition module, used for acquiring a data source rule expression from the annotation information when executing a target function with annotation information during the running of the application; A determination module, configured to determine a candidate data source based on the data source rule expression when the data source rule expression is not empty; The acquisition module is further configured to acquire the primary database information from the annotation information, and acquire the data delay information corresponding to the candidate data source when the candidate data source is different from the primary database and satisfies the data delay detection condition; The determination module is further configured to determine the candidate data source as a target data source when the data delay information indicates that the candidate data source has no data delay, and the target data source is used to execute the objective function.
13. An electronic device, characterized in that: The electronic device comprises: A memory for storing computer executable instructions; A processor, configured to implement the method according to any one of claims 1 to 11 when executing the computer executable instructions stored in the memory.
14. A computer-readable storage medium storing computer-executable instructions or a computer program, characterized in that: When the computer executable instructions or computer program are executed by a processor, the method according to any one of claims 1 to 11 is implemented.
Citation Information
Patent Citations
Main and standby database delay time processing method and device, medium and electronic equipment
CN110019257A
Database master-slave switching method, terminal and storage medium
CN111506560A
Read-write separation method, electronic equipment and storage medium
CN116701416A
Cited By
Data source changing method and system, intelligent terminal and storage medium
CN119066059A
