Methods, apparatus, and equipment for primary / standby failover based on database clusters
By assisting candidate standby components in electing a new master component and automating the switchover during master-slave database switching, the service interruption problem caused by the majority rule in existing technologies is solved, achieving fast and reliable database switching and improving system availability and stability.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- TIANJIN NANKAI UNIV GENERAL DATA TECH
- Filing Date
- 2026-01-26
- Publication Date
- 2026-05-26
Smart Images

Figure CN121579278B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of database technology, and more specifically to a method, apparatus, and device for master-slave failover based on a database cluster. Background Technology
[0002] In the field of computer data management, a primary-standby database architecture is a common choice for ensuring high availability. This architecture includes a primary database that handles the main business load and one or more standby databases that serve as failover backups. The two maintain data consistency through a data synchronization mechanism, enabling rapid service switching in the event of a primary database failure to avoid business interruption.
[0003] Related technologies typically implement database master-slave failover through available components. However, since the master-slave failover strategies in these technologies all rely on the majority principle, if the available component that needs to be switched is damaged, or if the remaining standby database does not meet the majority principle, it is difficult to perform the master-slave failover, resulting in long-term service interruption. Summary of the Invention
[0004] In view of the above problems, the present invention provides a method, apparatus and device for master-slave switching based on database cluster.
[0005] According to a first aspect of the present invention, a primary / standby failover method based on a database cluster is provided, characterized in that the database cluster includes multiple databases, a primary available component, and multiple standby available components; the primary / standby failover method includes: in response to a target standby available component among the multiple standby available components identifying an anomaly in the primary available component, and the number of candidate standby available components with a healthy operating state among the multiple standby available components being less than a first number threshold, sending a voting request to each candidate standby available component, and sending a network connectivity test command to a target network address, the target network address being used to assist the candidate standby available components and the target standby available component in electing a new primary available component; in response to receiving the response result of the network connectivity test command... Based on the response results and the voting results of each candidate standby available component, a master promotion judgment is made. If the master promotion is successful, the master promotion configuration is executed to switch the target standby available component to the new master available component. In response to the completion of the master promotion configuration and the receipt of the running status of multiple databases, the running status of multiple databases belonging to the same database center is aggregated and analyzed according to the center identifier assigned to each database to obtain the analysis results of each database center. If the running status of the master database in multiple databases is abnormal, the method of database master-slave switching for the database cluster is determined based on the analysis results of each database center and the switching parameters used to control the database cluster to perform database master-slave switching.
[0006] According to an embodiment of the present invention, multiple databases are divided according to their physical addresses and belong to a primary database center and a backup database center, respectively. The method for performing a primary-backup database switchover on the database cluster is determined based on the analysis results of each database center and the switching parameters used to control the database cluster to perform primary-backup switchover. This includes: when the analysis results indicate that the operating status of multiple first databases included in the primary database center is abnormal, and the number of candidate second databases with healthy operating status included in the backup database center is less than a second quantity threshold, obtaining the switching parameters used to control the database cluster to perform primary-backup switchover; and when the switching parameters are determined to be first type parameters, using the target second database determined from the multiple candidate second databases as the new primary database.
[0007] According to an embodiment of the present invention, selecting a target second database from multiple candidate second databases as a new primary database includes: obtaining the execution log of the primary database; in response to receiving the execution log, determining the target second database based on the execution results of each candidate second database on the execution log; and performing a configuration to switch the target second database to the new primary database.
[0008] According to an embodiment of the present invention, determining a target second database based on the execution results of each candidate second database on the execution log includes: sending the execution log to multiple candidate second databases so that the multiple candidate second databases execute the execution log and return the execution results;
[0009] Based on the execution results, the candidate second database with the fastest execution speed is selected as the target second database.
[0010] According to an embodiment of the present invention, the primary / backup switchover method further includes: in response to the target backup available component identifying an anomaly in the primary available component, sending a health check command to multiple backup available components to obtain health check results of multiple backup available components; and determining the number of candidate backup available components whose operating status is healthy based on the health check results of multiple backup available components.
[0011] According to an embodiment of the present invention, the primary / standby switching method further includes: when the switching parameter is determined to be a second type parameter, generating alarm information based on the analysis results and a preset alarm template; and sending the alarm information to the target object so that the target object can perform primary / standby switching of the database cluster based on the alarm information.
[0012] According to an embodiment of the present invention, the primary-backup switching method further includes: determining switching parameters for the database cluster based on the synchronization mode between the primary database center and the backup database center; or, determining switching parameters based on the configuration information of the database cluster for the target object.
[0013] According to an embodiment of the present invention, the first quantity threshold is determined based on a preset voting rule and the total number of primary and backup available components included in the database cluster; the second quantity threshold is determined based on a preset voting rule and the total number of databases included in the database cluster.
[0014] A second aspect of the present invention provides a primary / standby switchover device based on a database cluster. The database cluster includes multiple databases, a primary available component, and multiple standby available components. The primary / standby switchover device includes: a sending module, configured to, in response to a target standby available component among the multiple standby available components identifying an anomaly in the primary available component and the number of candidate standby available components with a healthy operating state being less than a first threshold, send a voting request to each candidate standby available component and send a network connectivity test command to a target network address, the target network address being used to assist the candidate standby available components and the target standby available component in electing a new primary available component; and a judging module, configured to, in response to receiving a response result to the network connectivity test command... Based on the response results and the voting results of each candidate standby component, a master upgrade decision is made. If the master upgrade is successful, the master upgrade configuration is executed to switch the target standby component to the new master component. A switching module, in response to the completed master upgrade configuration and receipt of the operating status of multiple databases, performs aggregated analysis of the operating status of multiple databases belonging to the same database center according to the center identifier assigned to each database, obtaining the analysis results of each database center. If the operating status of the master database in multiple databases is abnormal, based on the analysis results of each database center and the switching parameters used to control the database cluster master-slave switch, the method for performing the database master-slave switch for the database cluster is determined. A third aspect of the present invention provides an electronic device, including: one or more processors; and a memory for storing one or more computer programs, wherein the one or more processors execute the one or more computer programs to implement the steps of the above method.
[0015] A fourth aspect of the present invention also provides a computer-readable storage medium having a computer program or instructions stored thereon, wherein the computer program or instructions, when executed by a processor, implement the steps of the above-described method.
[0016] A fifth aspect of the present invention also provides a computer program product, including a computer program or instructions that, when executed by a processor, implement the steps of the above-described method.
[0017] According to the database cluster-based master-slave failover method of the present invention, by making the database master-slave failover dependent on the primary high-availability component, multiple backup available components can be pre-configured. When an anomaly is detected in the primary high-availability component, it is determined whether the number of healthy candidate backup available components is less than a first threshold, i.e., whether the majority election condition is met. If not, a target network address is identified to assist in the election of a new primary available component, thereby initiating the subsequent database master-slave failover process. When the new primary available component monitors the database status, it can analyze the database operating status by center to quickly determine the situation of each center. When it is determined that the primary database is operating abnormally, the master-slave failover method can be determined based on the situation of each center and in combination with the pre-configured failover parameters corresponding to the database cluster, thereby achieving automated master-slave failover. Therefore, the present invention at least partially solves the technical problem of long-term service interruption caused by the difficulty in performing database master-slave failover, realizes a full-process database master-slave failover control strategy, and reduces the probability of long-term service interruption. Attached Figure Description
[0018] The above-mentioned contents, as well as other objects, features and advantages of the present invention, will become clearer from the following description of embodiments of the present invention with reference to the accompanying drawings.
[0019] Figure 1 The diagram illustrates an application scenario of a database cluster-based master / slave failover method, apparatus, and device according to an embodiment of the present invention.
[0020] Figure 2 A flowchart of a database cluster-based master / slave failover method according to an embodiment of the present invention is shown.
[0021] Figure 3 A flowchart of a database cluster-based master / slave failover method according to another embodiment of the present invention is shown.
[0022] Figure 4 A schematic diagram illustrating a health check of multiple standby components according to an embodiment of the present invention is shown.
[0023] Figure 5 A structural block diagram of a database cluster-based master / slave failover device according to an embodiment of the present invention is shown.
[0024] Figure 6 A block diagram of an electronic device suitable for implementing a database cluster-based master-slave failover method is shown according to an embodiment of the present invention. Detailed Implementation
[0025] Hereinafter, embodiments of the present invention will be described with reference to the accompanying drawings. However, it should be understood that these descriptions are exemplary only and are not intended to limit the scope of the invention. In the following detailed description, numerous specific details are set forth to provide a thorough understanding of the embodiments of the invention for ease of explanation. However, it will be apparent that one or more embodiments may be practiced without these specific details. Furthermore, descriptions of well-known structures and techniques are omitted in the following description to avoid unnecessarily obscuring the concept of the invention.
[0026] The terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit the invention. The terms “comprising,” “including,” etc., as used herein indicate the presence of the stated features, steps, operations, and / or components, but do not exclude the presence or addition of one or more other features, steps, operations, or components.
[0027] All terms used herein (including technical and scientific terms) have the meanings commonly understood by those skilled in the art, unless otherwise defined. It should be noted that the terms used herein are to be interpreted in a manner consistent with the context of this specification, and not in an idealized or overly rigid way.
[0028] When using expressions such as "at least one of A, B and C", they should generally be interpreted in accordance with the meaning that is commonly understood by those skilled in the art (e.g., "a system having at least one of A, B and C" should include, but is not limited to, a system having A alone, a system having B alone, a system having C alone, a system having A and B, a system having A and C, a system having B and C, and / or a system having A, B and C, etc.).
[0029] First, the technical terms used in this article are explained and clarified as follows.
[0030] Majority rule: In an N-node cluster, a candidate node needs at least N / 2+1 votes to be elected as the master node. For example, a 5-node cluster needs 3 votes to be elected. In a database master election based on majority rule, a database node needs to obtain a majority of the locks to be allowed to become the master. For example, to ensure long-term stable application service, application service providers might set up two data centers for the database cluster, designated as the master and backup centers, expecting that if all database nodes in the master center fail, the database nodes in the backup center can become the master and continue providing service. If the number of database nodes in the master center exceeds half of the total number of nodes in the cluster, after all machines in the master center fail, the backup databases in the backup center will be unable to elect a master, and the cluster will lose its ability to autonomously elect a master.
[0031] This invention provides a database cluster master-slave failover method, comprising: responding to a target standby component among multiple standby components identifying an anomaly in the master component, and the number of candidate standby components with healthy operating status among the multiple standby components being less than a first threshold, sending a voting request to each candidate standby component and sending a network connectivity test command to a target network address, the target network address being used to assist the candidate standby components and the target standby component in electing a new master component; responding to receiving the response result to the network connectivity test command, performing a master promotion judgment based on the response result and the voting results of each candidate standby component, so as to execute a master promotion configuration to switch the target standby component to the new master component if the judgment result is successful; responding to the completion of the master promotion configuration and receiving the operating status of multiple databases, performing an aggregate analysis of the operating status of multiple databases belonging to the same database center according to the center identifier assigned to each database, obtaining the analysis results of each database center, so as to determine the database master-slave failover method for the database cluster based on the analysis results of each database center and the failover parameters used to control the database cluster to perform database master-slave failover if the operating status of the master database in multiple databases is abnormal.
[0032] Figure 1 The diagram illustrates an application scenario of a database cluster-based master / slave failover method, apparatus, and device according to an embodiment of the present invention.
[0033] like Figure 1 As shown, application scenario 100 according to this embodiment may include a primary available component 101, a first backup available component 102, a second backup available component 103, a network 104, a first database 105, a second database 106, and a third database 107. The network 104 serves as a medium for providing communication links between the first database 105, the second database 106, and the third database 107 and the primary available component 101. It may also serve as a medium for providing communication links between the first database 105, the second database 106, and the third database 107 and either the first backup available component 102 or the second backup available component 103. The network 104 may include various connection types, such as wired or wireless communication links, or fiber optic cables, etc.
[0034] The first backup component 102 and the second backup component 103 can be used to perform a master-slave switch when the operating status of the master component 101 is abnormal. Specifically, a new master component 101 is elected from the first backup component 102 and the second backup component 103. For example, the backup component from the first backup component 102 and the second backup component 103 that identifies an abnormality in the master component 101 is selected as the target backup component. The operating status of other backup components is obtained, and when conditions are met, a voting request is sent to each candidate backup component, and a network connectivity test command is sent to the target network address. A master promotion decision is made based on the response results and voting results, and if the master promotion is successful, it is determined whether a master-slave switch is needed. When necessary, a master-slave switch is performed according to the appropriate scenario.
[0035] The main available component 101 is used to receive the running status of the first database 105, the second database 106, and the third database 107, and to elect a new main database from the remaining databases when it is determined that there is an abnormality in the running status of the main database in the first database 105, the second database 106, and the third database 107.
[0036] Databases 105 (first database), 106 (second database), and 107 (third database) also employ a primary-backup architecture. Based on their usage in production scenarios, they are divided into a primary database and two backup databases. The primary database handles business read and write operations in production. The backup databases serve as a security safeguard for the primary database, retrieving data from the primary database through a synchronization mechanism.
[0037] It should be noted that the primary / standby failover method based on a database cluster provided in this embodiment of the invention can generally be executed by a server that includes a target standby available component. Correspondingly, the primary / standby failover device based on a database cluster provided in this embodiment of the invention can generally be located within the server.
[0038] It should be understood that Figure 1 The number of primary available components, first backup available components, second backup available components, network, first database, second database, and third database shown is merely illustrative. Depending on implementation needs, any number of primary available components, first backup available components, second backup available components, network, first database, second database, and third database can be included.
[0039] The following will be based on Figure 1 The described scene, through Figures 2-4 The master-slave failover method based on a database cluster, according to the embodiments of the invention, is described in detail.
[0040] Figure 2 A flowchart of a database cluster-based master / slave failover method according to an embodiment of the present invention is shown.
[0041] like Figure 2 As shown, the primary / standby switching method includes operations S210 to S230, and the primary / standby switching method can be executed by the target standby available component.
[0042] In operation S210, in response to the target backup component among multiple backup available components identifying an anomaly in the primary available component, and the number of candidate backup available components with a healthy running state among the multiple backup available components being less than a first quantity threshold, a voting request is sent to each candidate backup available component, and a network connectivity test command is sent to the target network address. The target network address is used to assist the candidate backup available components and the target backup available component in electing a new primary available component.
[0043] In operation S220, in response to the received network connectivity test command, a master upgrade decision is made based on the response result and the voting results of each candidate standby available component. If the decision result is that the master upgrade is successful, the master upgrade configuration is executed to switch the target standby available component to the new master available component.
[0044] In operation S230, in response to the completion of the primary configuration and the receipt of the operating status of multiple databases, the operating status of multiple databases belonging to the same database center is aggregated and analyzed according to the center identifier assigned to each database. The analysis results of each database center are obtained. In the case that the operating status of the primary database in multiple databases is abnormal, the method of switching the database cluster to primary or standby is determined based on the analysis results of each database center and the switching parameters used to control the database cluster to perform database primary or standby switching.
[0045] A database cluster can include multiple databases, a primary availability component, and multiple standby availability components. The primary availability component is used to implement the election and configuration of primary / standby failover for the database. The standby availability components are used to identify whether there is an anomaly in the primary database and, if so, to elect a new primary availability component. After the new primary availability component is elected, it performs the primary / standby failover election and configuration for the database.
[0046] In some embodiments, the primary available component may be referred to as the primary high-availability component, and similarly, the backup available component may be referred to as the backup high-availability component.
[0047] During implementation, multiple backup components can periodically send messages to the target backup component and count the number of times the primary backup component fails to receive a response to that message, thereby determining whether the primary backup component's operational status is abnormal. The message type is not limited; it can be the operational status of the corresponding database collected by the backup component or a health check command, etc.
[0048] If an anomaly is detected in the primary available component, a health check command can be sent to other backup available components (excluding itself) to determine the operational status of multiple backup available components and count the number of healthy candidate backup available components.
[0049] If the number of candidate backup components is less than the first threshold, i.e., the majority rule for promoting a backup component to the primary is not met, a voting request can be sent to each candidate backup component, and a network connectivity test command can be sent to the target network address.
[0050] The content of the voting request is not limited and can include the unique identifier of the target backup component, its network address, log index, term number, and whether the candidate agrees to appoint the target backup component as the new primary backup component. This allows candidate backup components to return voting results based on the network address and determine whether to agree to appoint the target backup component as the new primary backup component based on the log index and other information.
[0051] The target network address is not limited and can be a preset device used to assist candidate standby components and target standby components in electing a new primary standby component. For example, it can be the address of a gateway device or the address of other preset devices.
[0052] There are no restrictions on the network connectivity test command; it can be any command that can test the communication link connection between the target network address and the target standby component, such as the ping command.
[0053] If effective communication is possible between the target network address and the target standby component, a response to the network connectivity test command will be received. This response can be considered as one vote in favor of electing the target standby component as the new primary available component.
[0054] Furthermore, by counting the number of votes in the candidate backup components and combining this with the above response results, it is possible to determine whether the target backup component has successfully become the primary component.
[0055] For example: There is 1 primary available component and 3 backup available components. It can be determined that there are 2 healthy backup available components, including the target backup available component, meaning the majority rule is not met. Therefore, it can be determined whether a response to the network connectivity test command has been received. If so, the decision to promote the primary component can be made by combining the voting results of the target backup available component itself, the voting results of the other healthy backup available component, and the response result.
[0056] When the upgrade to master is successful, the database cluster status information can be updated, recording the unique identifier and network address of the new master available component. Simultaneously, the user's own role and responsibilities can be modified, and the upgrade configuration of standby available components and databases within the cluster can be statistically analyzed.
[0057] After the master configuration is completed, if the running status of multiple databases is received, the databases are aggregated and analyzed according to the pre-assigned central identifier to obtain the analysis results from the central perspective.
[0058] For example, the concept of Availability Zone (AZ) is introduced. When deploying a database cluster, the databases in the primary database center are set as AZ1, and the databases in the backup database center are set as AZ2, so as to distinguish the databases from the primary database center and the backup database center.
[0059] In some embodiments, AZ1 can have a higher priority than AZ2. That is, the aggregate analysis of the database operation status of the primary database center can be performed first, and when the health of the primary database center is determined, the analysis results can be output directly, and the analysis of other centers can be stopped.
[0060] The operational status of multiple databases can be sent by the databases themselves. Alternatively, each standby component can obtain its operational status from its corresponding database and forward it to the new primary availability component. The mapping between standby components and databases can be preset, for example, one standby component has one corresponding database. Alternatively, the new primary availability component can reallocate databases based on the operational status of the standby components. For example, the new primary availability component can reallocate corresponding databases to each candidate standby component based on the number of healthy candidate standby components and the communication distance between the candidate standby components and their databases.
[0061] By analyzing the results from each database center and using the switching parameters used to control the database cluster to perform database master-slave failover, the corresponding database master-slave failover method for the current database operating scenario can be determined.
[0062] Database operation scenarios include abnormal situations in each database center, such as: all databases in the primary database center are abnormal, while the backup database center has healthy databases.
[0063] According to the database cluster-based master-slave failover method of the present invention, by making the database master-slave failover dependent on the primary high-availability component, multiple backup available components can be pre-configured. When an anomaly is detected in the primary high-availability component, it is determined whether the number of healthy candidate backup available components is less than a first threshold, i.e., whether the majority election condition is met. If not, a target network address is identified to assist in the election of a new primary available component, thereby initiating the subsequent database master-slave failover process. When the new primary available component monitors the database status, it can analyze the database operating status by center to quickly determine the situation of each center. When it is determined that the primary database is operating abnormally, the master-slave failover method can be determined based on the situation of each center and in combination with the pre-configured failover parameters corresponding to the database cluster, thereby achieving automated master-slave failover. Therefore, the present invention at least partially solves the technical problem of service interruption caused by the difficulty in performing database master-slave failover, realizes a full-process database master-slave failover control strategy, and reduces the probability of service interruption.
[0064] According to an embodiment of the present invention, the first quantity threshold is determined based on a preset voting rule and the total number of primary and backup available components included in the database cluster; the second quantity threshold is determined based on a preset voting rule and the total number of databases included in the database cluster.
[0065] The calculation of the first and second quantity thresholds can be performed automatically using pre-configured algorithms without manual intervention. In practical applications, the thresholds can be pre-configured in the cluster to ensure that the thresholds match the cluster size and voting logic.
[0066] For the first quantity threshold, during the calculation process, the total number of primary and backup available components in the database cluster can be counted first, and then the specific value can be derived according to the preset voting rules, such as the majority principle.
[0067] For example, if the default voting rule is the majority principle and specifically "a majority vote passes", and the cluster contains 1 primary available component and 4 backup available components, with a total of 5, then the first quantity threshold of 3 means that at least 3 healthy components need to participate in the vote and reach a consensus in order to meet the election conditions.
[0068] The calculation logic for the second quantity threshold is the same as that for the first quantity threshold. The difference is that the statistical base is the total number of all databases in the database cluster, which is also determined according to the preset voting rules.
[0069] According to embodiments of the present invention, the setting of the first quantity threshold and the second quantity threshold is beneficial to realizing dynamic and more precise control of database cluster master-slave switching, thereby improving the availability and stability of the system.
[0070] According to an embodiment of the present invention, multiple databases are divided according to their physical addresses and belong to a primary database center and a backup database center respectively; wherein, based on the analysis results of each database center and the switching parameters used to control the database cluster to perform database primary-backup switching, the method for performing database primary-backup switching on the database cluster can be determined, which may include the following operations.
[0071] If the analysis results indicate that the operating status of multiple first databases included in the main database center is abnormal, and the number of candidate second databases with a healthy operating status included in the backup database center is less than the second quantity threshold, the switching parameters used to control the database cluster to perform database master-slave switching are obtained; if the switching parameters are determined to be first type parameters, the target second database determined from multiple candidate second databases is used as the new main database.
[0072] The primary database center can be a center that includes the primary database, meaning that the primary database is included in multiple primary databases.
[0073] Therefore, when the analysis results indicate that all the first databases included in the main database center are down (i.e., abnormal), it can be known that the main database has crashed and there are no available backup databases in the main database center. Thus, the pre-configured switching parameters used to control the database cluster to perform database master-slave switching can be obtained.
[0074] And when the switching parameter is determined to be the first type parameter, the target second database determined from multiple candidate second databases will be used as the new master database.
[0075] According to embodiments of the present invention, by combining the operational status analysis results of the primary and backup database centers with preset switching parameters, when the primary database center is abnormal as a whole and the available resources of the backup database center are insufficient to meet the majority rule, traditional limitations can be overcome, and a switchover can be triggered according to a preset strategy to avoid prolonged service interruptions. Simultaneously, after promoting the target second database to the primary database, data synchronization can be quickly restored based on the new master-slave relationship, achieving a smooth transition of business operations and data consistency. This enhances the adaptability and robustness of the database cluster under extreme failures, reduces operational complexity, and improves overall system availability.
[0076] According to an embodiment of the present invention, the primary / standby switching method may further include the following operations.
[0077] If the switching parameter is determined to be a second type parameter, an alarm message is generated based on the analysis results and the preset alarm template. The alarm message is then sent to the target object so that the target object can perform a database master-slave switch on the database cluster based on the alarm message.
[0078] If the switching parameter is determined to be a type II parameter, abnormal data can be summarized and alarm information can be generated. This alerts manual intervention to perform a primary / standby database switchover or to repair the anomaly.
[0079] Figure 3 A flowchart of a database cluster-based master / slave failover method according to another embodiment of the present invention is shown.
[0080] like Figure 3 As shown, the method includes operations S310 to S360.
[0081] In operation S310, an aggregate analysis of the running status of multiple databases belonging to the same database center is performed to obtain the analysis results of the primary database center and the backup database center.
[0082] In operation S320, the analysis results indicate whether the operating status of multiple first databases included in the primary database center is abnormal, and whether the number of candidate second databases with a healthy operating status included in the backup database center is less than a second quantity threshold. If yes, proceed to operation S330. If no, proceed to operation S360.
[0083] In operation S330, determine whether the switching parameter used to control the database cluster's master-slave failover is a type 1 parameter. If yes, execute operation S340. If not, execute operation S350.
[0084] The switching parameter can be `enable_auto_failover_for_az`. When it is the first type parameter (on), operation S340 is executed. When it is the second type parameter (off), operation S350 is executed.
[0085] In operation S340, the target second database determined from multiple candidate second databases will be used as the new master database.
[0086] When operating the S350, alarm information is generated and sent so that the database cluster can be switched between primary and backup databases based on the alarm information received by the target object.
[0087] When operating S360, the database cluster is switched between primary and backup based on the switching method corresponding to the current database operation scenario.
[0088] For example, if the number of candidate second databases that are all down but in a healthy state is greater than or equal to a second threshold, then a new primary database is determined and switched using a preset voting rule.
[0089] For example, in a database operation scenario where there are multiple candidate primary databases with a healthy running status, one of them is selected as the primary database and the process is switched.
[0090] According to embodiments of the present invention, in formal production scenarios, considering database cluster performance, backup database machines in the backup database center are often configured asynchronously. However, promoting an asynchronous backup to primary status carries the risk of data loss. If users cannot accept the risk of data loss, they can configure the switching parameter to "off." If all databases in the primary data center fail, the databases in the backup database center will not be able to become primary, and the user must decide whether to manually restart the backup center. If partial data loss has little impact on business operations, and users prioritize the long-term stable operation of services, they can configure the switching parameter to "on." If the primary data center fails, the primary available component will automatically promote the databases in the backup data center to primary status to continue providing services.
[0091] According to an embodiment of the present invention, the primary / standby switching method may further include the following operations.
[0092] The switching parameters for the database cluster are determined based on the synchronization mode between the primary and backup database centers; or, the switching parameters are determined based on the configuration information of the database cluster for the target object.
[0093] When the synchronization mode is real-time synchronization, the switching parameter can be automatically configured as the first type of parameter. Conversely, when the synchronization mode is asynchronous, the switching parameter can be automatically configured as the second type of parameter.
[0094] Alternatively, the switching parameters can be obtained by parsing the received user configuration information for the database cluster.
[0095] According to embodiments of the present invention, by configuring the automatic configuration of the synchronization mode and the user configuration of the configuration information, various switching parameter configurations can be implemented to meet the needs of different users and different scenarios.
[0096] According to an embodiment of the present invention, using a target second database determined from a plurality of candidate second databases as a new master database may include the following operations.
[0097] Obtain the execution log of the primary database; in response to receiving the execution log, determine the target secondary database based on the execution results of each candidate secondary database; execute the upgrade configuration to switch the target secondary database to the new primary database.
[0098] During the master-slave failover process, an execution log retrieval command can be sent to the master database or the corresponding standby component. This command can include parameters such as the required log time range and log type. Upon receiving the command, the master database or the corresponding standby component can extract the corresponding log data from its local transaction log storage area and return it to the new master component. The execution log is a crucial data structure in the database system that records all database operations.
[0099] In some embodiments, the execution log may also be stored in the target storage device, so that it can be obtained from the target storage device even if the primary database or the standby component corresponding to the primary database is unable to provide the execution log.
[0100] After receiving the returned execution logs, the new primary available component distributes the logs to each candidate secondary database. Each candidate secondary database then performs operations based on its stored data and the received execution logs, obtaining its own execution results. Execution results may include execution time, post-execution data, etc.
[0101] The new primary available component receives the execution results returned by each candidate secondary database or the backup available component corresponding to each candidate secondary database, and determines the target secondary database with the best execution result through preset evaluation algorithms such as log integrity comparison and data consistency verification.
[0102] Then, it sends a master upgrade configuration command to the target second database or the corresponding standby component, so that the target second database can update its own role identifier, synchronize the latest cluster metadata, re-establish data synchronization channels with other standby databases, and officially take over the various responsibilities of the master database.
[0103] According to an embodiment of the present invention, by obtaining the execution log, the operation records of the main database can be collected more comprehensively. The log is then distributed to each candidate second database, which executes the execution log. The target second database selected based on the execution result has the latest and most complete data and can seamlessly take over all the responsibilities of the main database, thereby reducing the risk of data inconsistency after the switch.
[0104] According to an embodiment of the present invention, determining the target second database based on the execution results of each candidate second database on the execution log may include the following operations.
[0105] The execution log is sent to multiple candidate second databases so that the candidate second databases can execute the execution log and return the execution results; based on the execution results, the candidate second database with the fastest execution speed is selected as the target second database.
[0106] After the execution log is sent to multiple candidate second databases, each candidate second database will independently execute the operation sequence in the log locally. This process includes parsing log entries one by one, simulating the execution of the corresponding database operations, and recording key indicators such as execution time and resource consumption in real time.
[0107] After execution, each candidate secondary database returns its execution result to the new primary available component. The new primary available component collects the execution results of all candidate secondary databases and, according to a preset evaluation strategy, determines the candidate secondary database with the fastest execution speed (i.e., the shortest execution time) as the target secondary database. It then sends a primary upgrade configuration command to the target secondary database, completing the primary / standby switchover.
[0108] According to embodiments of the present invention, each candidate second database executes the execution log in parallel, which can quickly complete data consistency verification and status assessment, shorten the time required for switching decisions, and improve switching efficiency. Furthermore, using the fastest execution speed as the selection criterion ensures that the target second database not only has the latest data status but can also take over the business load of the primary database with superior performance, reducing the impact on business operations during the switching process.
[0109] According to an embodiment of the present invention, the primary / standby switching method may further include the following operations.
[0110] In response to the target standby component identifying an anomaly in the primary standby component, a health check command is sent to multiple standby components to obtain the health check results of multiple standby components; based on the health check results of multiple standby components, the number of candidate standby components with a healthy running status is determined.
[0111] In a database cluster system, when a target standby component detects an anomaly in the primary standby component, the target standby component will send a health check command to all other standby components in the cluster.
[0112] This instruction typically includes preset checks, such as component process status, network connectivity, disk space, and memory usage. Each standby component that receives the instruction will independently perform a self-check and report the health check results, including its current running status (healthy or abnormal), to the target standby component.
[0113] It should be noted that if no response is received from the backup component within the preset waiting time, the health check result of the backup component can also be considered abnormal.
[0114] After the target standby component collects the health check results of all standby components, it will summarize and statistically analyze them to determine the number of candidate standby components that are currently in a healthy operating state.
[0115] Figure 4A schematic diagram illustrating a health check of multiple standby components according to an embodiment of the present invention is shown.
[0116] like Figure 4 As shown, the multiple backup components include a target backup component, a first backup component, a second backup component, and a third backup component. The target backup component can send health check commands to the first, second, and third backup components. Based on the responses from each backup component, the health check results of each backup component are obtained, thereby determining the number of candidate backup components with a healthy operating status.
[0117] For example, if the first backup component is healthy, the second backup component is abnormal, and the third backup component is also abnormal, then by combining the health check results of the target backup component, the number of healthy candidate backup components can be determined to be 2.
[0118] According to an embodiment of the present invention, by sending a unified health check command to all available backup components and collecting the results, the actual availability status of each backup component in the cluster can be more comprehensively evaluated, and the number of healthy candidate available backup components can be determined, so as to provide a more reliable data foundation for subsequent primary / backup switchover decisions.
[0119] Based on the above-described primary / standby failover method for database clusters, this invention also provides a primary / standby failover device for database clusters. The following will be combined with... Figure 5 The device is described in detail.
[0120] Figure 5 A structural block diagram of a database cluster-based master / slave failover device according to an embodiment of the present invention is shown.
[0121] like Figure 5 As shown, the primary / backup switching device 500 based on a database cluster in this embodiment includes a sending module 510, a judgment module 520, and a switching module 530.
[0122] The sending module 510 is used to respond to the situation where the target backup component among multiple backup components identifies an anomaly in the primary available component, and the number of candidate backup components with a healthy running state among the multiple backup available components is less than a first number threshold, by sending a voting request to each candidate backup available component and sending a network connectivity test instruction to the target network address. The target network address is used to assist the candidate backup available components and the target backup available component in electing a new primary available component.
[0123] The judgment module 520 is used to respond to the response result of the received network connectivity test command, and to make a master upgrade judgment based on the response result and the voting results of each candidate standby available component, so as to execute the master upgrade configuration to switch the target standby available component to the new master available component if the judgment result is successful.
[0124] The switching module 530 is used to respond to the completion of the primary configuration and the receipt of the running status of multiple databases. According to the center identifier assigned to each database, it performs aggregate analysis of the running status of multiple databases belonging to the same database center, obtains the analysis results of each database center, and determines the method of switching the database cluster to primary or standby based on the analysis results of each database center and the switching parameters used to control the database cluster to perform database primary or standby switching in the event that the running status of the primary database in multiple databases is abnormal.
[0125] According to an embodiment of the present invention, multiple databases are divided according to their physical addresses and belong to a primary database center and a backup database center, respectively. The switching module 530 includes the following sub-modules.
[0126] The acquisition submodule is used to acquire switching parameters for controlling the database cluster to perform database master-slave switching when the analysis results indicate that the operating status of multiple first databases included in the master database center is abnormal, and the number of candidate second databases with a healthy operating status included in the backup database center is less than a second quantity threshold.
[0127] The first switching submodule is used to select the target second database determined from multiple candidate second databases as the new master database when the switching parameter is determined to be a first type parameter.
[0128] According to an embodiment of the present invention, the first switching submodule includes the following units.
[0129] The acquisition unit is used to acquire the execution logs of the main database.
[0130] The determination unit is used to determine the target second database in response to receiving the execution log, based on the execution results of each candidate second database on the execution log.
[0131] The execution unit is used to execute the upgrade configuration that switches the target secondary database to the new primary database.
[0132] According to an embodiment of the present invention, the determining unit includes the following sub-units.
[0133] The sending subunit is used to send the execution log to multiple candidate second databases so that the multiple candidate second databases can execute the execution log and return the execution results.
[0134] The sub-unit is determined to select the candidate second database with the fastest execution speed as the target second database based on the execution results.
[0135] According to an embodiment of the present invention, the master / slave switching device 500 further includes the following modules.
[0136] The inspection module is used to send health check commands to multiple backup components in response to the target backup component identifying an anomaly in the primary available component, and obtain the health check results of multiple backup components.
[0137] The statistics module is used to determine the number of candidate standby components that are in a healthy operating state based on the health check results of multiple standby components.
[0138] According to an embodiment of the present invention, the switching module 530 further includes the following sub-modules.
[0139] The generation submodule is used to generate alarm information based on the analysis results and preset alarm templates when the switching parameter is determined to be a second type parameter.
[0140] The second switching submodule is used to send alarm information to the target object so that the target object can perform database master-slave switching on the database cluster based on the alarm information.
[0141] According to an embodiment of the present invention, the master / slave switching device 500 further includes the following modules.
[0142] The first parameter determination module is used to determine the switching parameters for the database cluster based on the synchronization mode between the primary database center and the backup database center.
[0143] The second parameter determination module is used to determine the switching parameters based on the configuration information of the database cluster for the target object.
[0144] According to embodiments of the present invention, any plurality of modules among the transmitting module 510, the judging module 520, and the switching module 530 may be combined into one module, or any one of these modules may be split into multiple modules. Alternatively, at least a portion of the functionality of one or more of these modules may be combined with at least a portion of the functionality of other modules and implemented in one module. According to embodiments of the present invention, at least one of the transmitting module 510, the judging module 520, and the switching module 530 may be at least partially implemented as hardware circuitry, such as a field-programmable gate array (FPGA), a programmable logic array (PLA), a system-on-a-chip, a system-on-a-substrate, a system-on-package, an application-specific integrated circuit (ASIC), or any other reasonable means of integrating or packaging circuitry, or implemented in software, hardware, or firmware, or in any suitable combination of any of these three implementation methods. Alternatively, at least one of the transmitting module 510, the judging module 520, and the switching module 530 may be at least partially implemented as a computer program module, which, when run, can perform corresponding functions.
[0145] Figure 6A block diagram of an electronic device suitable for implementing a database cluster-based master-slave failover method is shown according to an embodiment of the present invention.
[0146] like Figure 6 As shown, an electronic device 600 according to an embodiment of the present invention includes a processor 601, which can perform various appropriate actions and processes according to a program stored in a read-only memory ROM 602 or a program loaded from a storage portion 608 into a random access memory RAM 603. The processor 601 may include, for example, a general-purpose microprocessor (e.g., a CPU), an instruction set processor and / or an associated chipset and / or a special-purpose microprocessor (e.g., an application-specific integrated circuit (ASIC)), etc. The processor 601 may also include onboard memory for caching purposes. The processor 601 may include a single processing unit or multiple processing units for performing different actions of the method flow according to an embodiment of the present invention.
[0147] RAM 603 stores various programs and data required for the operation of electronic device 600. Processor 601, ROM 602, and RAM 603 are interconnected via bus 604. Processor 601 executes various operations of the method flow according to embodiments of the present invention by executing programs in ROM 602 and / or RAM 603. It should be noted that the programs may also be stored in one or more memories other than ROM 602 and RAM 603. Processor 601 may also execute various operations of the method flow according to embodiments of the present invention by executing programs stored in said one or more memories.
[0148] According to an embodiment of the present invention, the electronic device 600 may further include an input / output (I / O) interface 605, which is also connected to a bus 604. The electronic device 600 may also include one or more of the following components connected to the input / output (I / O) interface 605: an input section 606 including a keyboard, mouse, etc.; an output section 607 including a cathode ray tube (CRT), liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 608 including a hard disk, etc.; and a communication section 609 including a network interface card such as a LAN card, modem, etc. The communication section 609 performs communication processing via a network such as the Internet. A drive 610 is also connected to the input / output (I / O) interface 605 as needed. A removable medium 611, such as a disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed on the drive 610 as needed so that computer programs read from it can be installed into the storage section 608 as needed.
[0149] The present invention also provides a computer-readable storage medium, which may be included in the device / apparatus / system described in the above embodiments; or it may exist independently and not assembled into the device / apparatus / system. The computer-readable storage medium carries one or more programs, which, when executed, implement the method according to the embodiments of the present invention.
[0150] According to embodiments of the present invention, a computer-readable storage medium may be a non-volatile computer-readable storage medium, such as including, but not limited to: portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device. For example, according to embodiments of the present invention, a computer-readable storage medium may include ROM 602 and / or RAM 603 and / or one or more memories other than ROM 602 and RAM 603 described above.
[0151] Embodiments of the present invention also include a computer program product comprising a computer program containing program code for performing the methods shown in the flowchart. When the computer program product is run on a computer system, the program code enables the computer system to implement the master-slave failover method based on a database cluster provided in the embodiments of the present invention.
[0152] When the computer program is executed by the processor 601, it performs the functions defined in the system / apparatus of this invention. According to embodiments of the invention, the systems, apparatuses, modules, units, etc., described above can be implemented by computer program modules.
[0153] In one embodiment, the computer program may rely on a tangible storage medium such as an optical storage device or a magnetic storage device. In another embodiment, the computer program may also be transmitted and distributed in the form of signals over a network medium, and downloaded and installed via the communication section 609, and / or installed from the removable medium 611. The program code contained in the computer program can be transmitted using any suitable network medium, including but not limited to: wireless, wired, etc., or any suitable combination thereof.
[0154] In such an embodiment, the computer program can be downloaded and installed from a network via the communication section 609, and / or installed from the removable medium 611. When the computer program is executed by the processor 601, it performs the functions defined in the system of this embodiment of the invention. According to embodiments of the invention, the systems, devices, apparatuses, modules, units, etc., described above can be implemented by computer program modules.
[0155] According to embodiments of the present invention, program code for executing the computer programs provided in the embodiments of the present invention can be written in any combination of one or more programming languages. Specifically, these computational programs can be implemented using high-level procedural and / or object-oriented programming languages, and / or assembly / machine languages. Programming languages include, but are not limited to, languages such as Java, C++, Python, "C", or similar programming languages. The program code can be executed entirely on the user's computing device, partially on the user's device, partially on a remote computing device, or entirely on a remote computing device or server. In cases involving remote computing devices, the remote computing device can be connected to the user's computing device via any type of network, including a local area network (LAN) or a wide area network (WAN), or it can be connected to an external computing device (e.g., via the Internet using an Internet service provider).
[0156] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of the present invention. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in a block diagram or flowchart, and combinations of blocks in a block diagram or flowchart, may be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions.
[0157] Those skilled in the art will understand that the features described in the various embodiments of the present invention can be combined and / or combined in various ways, even if such combinations or combinations are not explicitly described in the present invention. In particular, the features described in the various embodiments of the present invention can be combined and / or combined in various ways without departing from the spirit and teachings of the present invention. All such combinations and / or combinations fall within the scope of the present invention.
[0158] The embodiments of the present invention have been described above. However, these embodiments are merely illustrative and not intended to limit the scope of the invention. Although various embodiments have been described above, this does not mean that the measures in the various embodiments cannot be used advantageously in combination. Various substitutions and modifications can be made by those skilled in the art without departing from the scope of the invention, and all such substitutions and modifications should fall within the scope of the invention.
Claims
1. A master-slave failover method based on a database cluster, characterized in that, The database cluster includes multiple databases, a primary available component, and multiple backup available components; the primary / backup failover method includes: In response to a target backup component among the multiple backup available components identifying an anomaly in the primary available component, and the number of candidate backup available components with a healthy operating status among the multiple backup available components being less than a first number threshold, a voting request is sent to each of the candidate backup available components, and a network connectivity test command is sent to a target network address, wherein the target network address is used to assist the candidate backup available components and the target backup available component in electing a new primary available component; In response to the received network connectivity test command, a primary master determination is made based on the response result and the voting results of each candidate backup available component, so as to execute the primary master configuration to switch the target backup available component to the new primary available component if the determination result is successful. In response to the completion of the primary database upgrade configuration and receipt of the operating status of multiple databases, the operating status of multiple databases belonging to the same database center is aggregated and analyzed according to the center identifier assigned to each database to obtain the analysis results of each database center. In the event that the operating status of the primary database in multiple databases is abnormal, the method for performing database primary-standby switchover on the database cluster is determined based on the analysis results of each database center and the switching parameters used to control the database cluster to perform database primary-standby switchover.
2. The method according to claim 1, characterized in that, The databases are divided according to their physical addresses and belong to the primary database center and the backup database center, respectively. The step of determining the method for performing database master-slave switching on the database cluster based on the analysis results of each of the database centers and the switching parameters used to control the database cluster to perform database master-slave switching includes: If the analysis results indicate that the operating status of multiple first databases included in the primary database center is abnormal, and the number of candidate second databases with a healthy operating status included in the backup database center is less than a second number threshold, then obtain the switching parameters used to control the database cluster to perform database primary / backup switching. If the switching parameter is determined to be a first type parameter, the target second database determined from the multiple candidate second databases will be used as the new master database.
3. The method according to claim 2, characterized in that, The step of using the target second database determined from the multiple candidate second databases as the new master database includes: Obtain the execution logs of the main database; In response to receiving the execution log, the target second database is determined based on the execution results of each of the candidate second databases on the execution log; Perform the upgrade configuration to switch the target second database to the new primary database.
4. The method according to claim 3, characterized in that, The step of determining the target second database based on the execution results of the execution logs of each of the candidate second databases includes: The execution log is sent to multiple candidate second databases, so that the multiple candidate second databases execute the execution log and return the execution result; Based on the execution results, the candidate second database with the fastest execution speed is selected as the target second database.
5. The method according to claim 1 or 2, characterized in that, The primary / standby switching method further includes: In response to the target backup available component identifying an anomaly in the primary available component, a health check command is sent to multiple backup available components to obtain the health check results of multiple backup available components; Based on the health check results of multiple available backup components, the number of candidate available backup components with a healthy operating status is determined.
6. The method according to claim 2, characterized in that, The primary / standby switching method further includes: If the switching parameter is determined to be a second type parameter, an alarm message is generated based on the analysis results and the preset alarm template. The alarm information is sent to the target object so that the target object can perform a database master-slave switch on the database cluster based on the alarm information.
7. The method according to claim 2, characterized in that, The primary / standby switching method further includes: The switching parameters for the database cluster are determined based on the synchronization mode between the primary database center and the backup database center; Alternatively, the switching parameters can be determined based on the configuration information of the target object for the database cluster.
8. The method according to claim 2, characterized in that, The first quantity threshold is determined based on a preset voting rule and the total number of primary and backup available components included in the database cluster; the second quantity threshold is determined based on the preset voting rule and the total number of databases included in the database cluster.
9. A master-slave failover device based on a database cluster, characterized in that, The database cluster includes multiple databases, a primary available component, and multiple backup available components; the primary / backup switching device includes: The sending module is configured to respond to a situation where a target backup component among a plurality of backup available components identifies an anomaly in the primary available component, and the number of candidate backup available components with a healthy operating status among the plurality of backup available components is less than a first number threshold, by sending a voting request to each of the candidate backup available components and sending a network connectivity test command to a target network address, wherein the target network address is used to assist the candidate backup available components and the target backup available component in electing a new primary available component; The judgment module is used to respond to the response result of the network connectivity test command received, and to make a master upgrade judgment based on the response result and the voting results of each candidate backup available component, so as to execute the master upgrade configuration to switch the target backup available component to the new master available component if the judgment result is successful. The switching module is used to respond to the completion of the primary configuration and the receipt of the operating status of multiple databases, and to perform aggregate analysis of the operating status of multiple databases belonging to the same database center according to the center identifier assigned to each database, to obtain the analysis results of each database center. In the event that the operating status of the primary database in multiple databases is abnormal, the module determines the method of performing database primary-standby switching of the database cluster based on the analysis results of each database center and the switching parameters used to control the database cluster to perform database primary-standby switching.
10. An electronic device, comprising: One or more processors; Memory, used to store one or more computer programs. The characteristic feature is that the one or more processors execute the one or more computer programs to implement the steps of the method according to any one of claims 1 to 8.
Citation Information
Patent Citations
Cluster database system
CN110727709A
Database system, and primary database election methods and apparatuses
US20250363129A1