Instance switching method and device for cloud database, equipment and medium
By using arbitration storage areas and control components in cloud databases, monitoring and managing database instance status, and using K8S resource locks to achieve atomic switching of master and spare instances, it solves the problems of high demand for traditional database resources and complex deployment, and improves operation and maintenance Efficiency and database reliability.
Patent Information
- Application Number
- CN202410986664.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-23
- Publication Date
- 2024-11-12
AI Technical Summary
The resource demand of traditional databases is too high, the deployment is difficult, and the operation and maintenance efficiency is low. Especially when implementing database failure tolerance in a cloud database environment, complex management and additional resources are required, which affects the reliability and efficiency of database services.
By introducing arbitration storage areas and control components into cloud databases, monitoring and managing the status of database instances, using K8S resource locks and Informer mechanisms to achieve atomic switching of master and spare instances, reducing resource requirements and deployment complexity, and improving operation and maintenance efficiency.
It realizes efficient switching of the status of the main and spare instances in the cloud database, reduces resource requirements and deployment difficulties, improves operation and maintenance efficiency, and enhances the reliability and adaptability of the database cluster.
Smart Images

Figure CN118939477A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of database technology, and in particular to an instance switching method, device, equipment and medium for a cloud database. Background Art
[0002] At present, databases are important software infrastructures that carry a large amount of critical data. In addition, in the field of national economy and people's livelihood, database services need to work continuously 24 hours a day, 7 days a week. When the database service goes down, it is necessary to rely on the database's own high-availability services to ensure that the database traffic can be quickly switched to the normal host and minimize the impact of the downtime on external services. For a multi-instance master-slave database cluster, the key point to achieve database fault tolerance is to seamlessly switch the master instance to the available slave instance to ensure the normal operation of the entire database cluster.
[0003] In the traditional database active-standby deployment framework, instances need to be managed uniformly through management instances. Management instances not only need to ensure that they have sufficient resources, but also need to ensure their high availability (deployed on at least two nodes) in production environment deployment. At the same time, additional databases or storage spaces need to be deployed to record the status data of each instance to ensure that the instance can accurately perform failover based on the previous status. This will undoubtedly increase the deployment difficulty, workload, operation and maintenance costs of database services and the user's usage costs. Summary of the invention
[0004] The purpose of the present invention is to provide an instance switching method, device, equipment and medium for a cloud database, which is used to solve the problems of excessively high resource requirements, greater deployment difficulty and low operation and maintenance efficiency of traditional databases.
[0005] In order to solve the above technical problems, an embodiment of the present invention provides an instance switching method for a cloud database, which is applied to a first control component. The method includes:
[0006] Acquire a first state of a first instance in a cloud database where the first control component is located, and / or acquire a second state of at least one second instance in the cloud database associated with the first instance;
[0007] The instance state of the first instance is determined according to the instance information of the primary instance and the standby instance stored in the arbitration storage area, the first state and / or the second state, wherein the instance state is the primary instance or the standby instance.
[0008] Optionally, determining the instance state of the first instance according to the instance information of the primary instance and the backup instance stored in the arbitration storage area, the first state and / or the second state includes one or more of the following:
[0009] When both the first state and the second state are instances started for the first time and the arbitration storage area does not include instance information of the primary instance, determining that the instance state of the first instance is the primary instance;
[0010] When both the first state and the second state are the first startup of the instance and the arbitration storage area includes instance information of the primary instance, it is determined that the instance state of the first instance is a standby instance.
[0011] Optionally, determining the instance state of the first instance according to the instance information of the primary instance and the backup instance stored in the arbitration storage area, the first state and / or the second state includes one or more of the following:
[0012] When the first state is instance restart and the instance information of the primary instance stored in the arbitration storage area matches the instance information of the first instance, determining that the instance state of the first instance is the primary instance;
[0013] When the first state is instance restart and the instance information of the primary instance stored in the arbitration storage area does not match the instance information of the first instance, it is determined that the instance state of the first instance is a standby instance.
[0014] Optionally, when the instance information of the primary instance stored in the arbitration storage area does not match the instance information of the first instance, determining that the instance state of the first instance is a standby instance includes:
[0015] In a case where the instance information of the primary instance stored in the arbitration storage area does not match the instance information of the first instance, monitoring whether the primary instance stored in the arbitration storage area is started;
[0016] In the case where it is monitored that the primary instance stored in the arbitration storage area has been started, it is determined that the instance state of the first instance is a standby instance.
[0017] Optionally, determining the instance state of the first instance according to the instance information of the primary instance and the backup instance stored in the arbitration storage area, the first state and / or the second state includes:
[0018] When the second state of at least one of the second instances is an instance failure, and the second instance with the instance failure is a primary instance, acquiring instance information of the standby instance stored in the arbitration storage area, the instance information including a state timestamp;
[0019] Compare the state timestamp of the first instance with the state timestamp of the standby instance;
[0020] When the state timestamp of the first instance is greater than the state timestamp of any of the standby instances, determining that the instance state of the first instance is a primary instance;
[0021] When the state timestamp of the first instance is less than or equal to the state timestamp of at least one of the standby instances, it is determined that the instance state of the first instance is a standby instance.
[0022] Optionally, when the second instance of the instance failure is a primary instance, obtaining instance information of the standby instance stored in the arbitration storage area includes:
[0023] In the case where the second instance of the instance failure is the primary instance, atomically updating the instance information of the first instance in the arbitration storage area and monitoring whether the instance information of the standby instance stored in the arbitration storage area has completed the atomic update;
[0024] After monitoring that the instance information of the standby instance stored in the arbitration storage area has completed atomic update, the updated instance information of the standby instance stored in the arbitration storage area is obtained.
[0025] Optionally, before obtaining a first state of a first instance in a cloud database where the first control component is located and obtaining a second state of at least one second instance associated with the first instance in the cloud database, the method further includes:
[0026] In the case of monitoring that the first instance is started, determining whether the arbitration storage area exists;
[0027] If the arbitration storage area does not exist, the arbitration storage area is created and it is determined that both the first state and the second state are the first startup of the instance.
[0028] Optionally, before obtaining the first state of the first instance where the first control component is located in the cloud database, the method further includes:
[0029] In the case of monitoring that the first instance is started, determining whether the arbitration storage area exists;
[0030] In a case where the arbitration storage area exists, it is determined that the first state is the instance restarting.
[0031] Optionally, the arbitration storage area includes a primary instance arbitration storage area and a backup instance arbitration storage area;
[0032] The method further comprises:
[0033] When it is determined that the instance state of the first instance is a master instance, storing the instance information of the first instance in the master instance arbitration storage area;
[0034] When it is determined that the instance state of the first instance is a standby instance, the instance information of the first instance is stored in the standby instance arbitration storage area.
[0035] The embodiment of the present invention further provides an instance switching method for a cloud database, which is applied to a second control component, and the method includes:
[0036] Monitor the instance status and fault status of each instance in the cloud database, and monitor the instance status and fault status of each instance in the arbitration storage area, wherein the instance status is a primary instance or a backup instance;
[0037] When it is detected that the instance state of at least one instance in the cloud database is different from the instance state stored in the arbitration storage area, the instance state stored in the arbitration storage area is modified according to the instance state of the instance;
[0038] When it is detected that the fault state of at least one instance in the cloud database is different from the fault state stored in the arbitration storage area, the fault state stored in the arbitration storage area is corrected according to the fault state of the instance.
[0039] Optionally, the fault state is instance fault or instance normal;
[0040] When it is detected that the fault state of at least one instance in the cloud database is different from the fault state stored in the arbitration storage area, the fault state stored in the arbitration storage area is corrected according to the fault state of the instance, including:
[0041] When it is monitored that the fault state of at least one instance in the cloud database is an instance fault and is different from the fault state stored in the arbitration storage area, determining whether the faulty instance is a primary instance or a backup instance;
[0042] In the case where the failed instance is a primary instance, correcting the failure state of the failed primary instance to an instance failure in the primary instance arbitration storage area;
[0043] If the failed instance is a standby instance, restart the failed standby instance;
[0044] When the failed standby instance fails to restart and the number of restarts is greater than a first threshold, the failure state of the failed standby instance is corrected to instance failure in the standby instance arbitration storage area.
[0045] The embodiment of the present invention further provides an instance switching device for a cloud database, which is applied to a first control component and includes:
[0046] A first acquisition module, configured to acquire a first state of a first instance in a cloud database where the first control component is located, and / or acquire a second state of at least one second instance in the cloud database associated with the first instance;
[0047] A first determination module is configured to determine an instance state of the first instance according to instance information of the first instance and the second instance stored in an arbitration storage area, the first state, and / or the second state, wherein the instance state is a primary instance or a backup instance, and the arbitration storage area includes a primary instance arbitration storage area and a backup instance arbitration storage area.
[0048] The embodiment of the present invention further provides an instance switching device for a cloud database, which is applied to a second control component and includes:
[0049] A first monitoring module is used to monitor the instance state and fault state of each instance in the cloud database, and to monitor the instance state and fault state of each instance in the arbitration storage area, wherein the state includes a primary instance or a standby instance, and the arbitration storage area includes a primary instance arbitration storage area and a standby instance arbitration storage area;
[0050] A first correction module is used to correct the instance state stored in the arbitration storage area according to the instance state of at least one instance in the cloud database when it is detected that the instance state of the instance is different from the instance state stored in the arbitration storage area;
[0051] The second correction module is used to correct the fault state stored in the arbitration storage area according to the fault state of at least one instance in the cloud database when it is detected that the fault state of the instance is different from the fault state stored in the arbitration storage area.
[0052] An embodiment of the present invention further provides a network device, comprising: a processor, a memory, and a program stored in the memory and executable on the processor, wherein the program, when executed by the processor, implements the instance switching method for a cloud database as described in any one of the above items.
[0053] An embodiment of the present invention further provides a readable storage medium, comprising: a program is stored on the readable storage medium, and when the program is executed by a processor, the steps of the instance switching method for a cloud database as described in any one of the above items are implemented.
[0054] An embodiment of the present invention further provides a computer program product, comprising computer instructions, which, when executed by a processor, implement the steps of the instance switching method for a cloud database as described in any one of the above items.
[0055] At least one of the above technical solutions of the present invention has the following beneficial effects:
[0056] In the above scheme, the first control component is integrated with the instance, wherein one first control component arbitrates one instance and monitors the operation of other instances at the same time; the first control component determines the instance state of the first instance according to the instance information of the primary instance and the backup instance stored in the arbitration storage area, the first state of the first instance corresponding to the arbitration and / or the second state of at least one second instance associated with the first instance, thereby realizing the switching of the primary and backup instance states, reducing resource requirements and deployment difficulty, and effectively improving operation and maintenance efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0057] Figure 1 A schematic diagram of a deployment architecture of a database cluster according to an embodiment of the present invention;
[0058] Figure 2 A schematic diagram of a flow chart of an instance switching method for a cloud database according to one embodiment of the present invention;
[0059] Figure 3 A schematic diagram of a flow chart of an instance switching method for a cloud database according to another embodiment of the present invention;
[0060] Figure 4 It is a structural schematic diagram of an instance switching device for a cloud database according to one embodiment of the present invention;
[0061] Figure 5 It is a structural schematic diagram of an instance switching device for a cloud database according to another embodiment of the present invention. DETAILED DESCRIPTION
[0062] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0063] As cloud-native technology develops rapidly, it has brought great changes to the deployment, operation and high availability of databases, and has also continuously changed people's perception of the rational use of computer resources. If the instance failover solution of the traditional database cluster is simply applied to the cloud-native database cluster, it may not only increase the difficulty of database cluster deployment and increase the consumption of cloud computing resources, but also may not perfectly achieve the expected high availability requirements of the database cluster.
[0064] In cloud-native database clusters with multiple instances, mainstream technologies basically use custom resource definitions (CRDs) and controller technologies to implement the definition and management of database instances. As stateful individuals, the state of each database instance will change dynamically with the environment and internal operating status. In order to ensure the successful switching of instance states in a database cluster, a more complex and detailed database instance state switching solution is required.
[0065] like Figure 1 The embodiment of the present invention provides a deployment architecture of a database cluster, which includes the following components:
[0066] Database instance: A database instance is a stateful resource of a StatefulSet. In production database scenarios, database instances are generally deployed in a distributed manner to ensure high availability. A single master and multiple backup instances are used to ensure business continuity.
[0067] The first control component is responsible for the instance state switching of the database instance. The first control component is mainly implemented based on the resource lock of the open source system K8S (kubernetes). The resource lock is grabbed through each database instance. If the resource lock is successfully grabbed (atomic update of resources), it can be upgraded to the main instance. Among them, the types of resource locks include but are not limited to Endpoints, Configmaps and Leases. When the resource lock is applied, the atomicity of resource update is guaranteed by K8S internally;
[0068] The arbitration storage area is used to save the instance information of the database instance after each stateful database instance arbitration, so that the client can correctly establish a request connection with the primary instance (here it mainly relies on the Configmap resources of the cloud native environment, which is convenient and fast). Among them, the arbitration storage area includes the primary instance arbitration storage area and the backup instance arbitration storage area. The primary instance arbitration storage area is used to store the instance information of the primary instance, and the backup instance arbitration storage area is used to store the instance information of the backup instance;
[0069] The second control component, based on the K8S Informer mechanism, monitors the changes in the dependent resources of the database instance to grasp the real-time dynamic update of the database instance status, and promptly refreshes the database instance information to the arbitration storage area for persistent storage; due to the dynamic expansion and contraction of the database computing instance, a component is required to dynamically update the instance information of the instance. Therefore, the logic of the management node is reused in the second control component for dynamic update, without the need to introduce additional management nodes to increase the complexity of database cluster management and the cost of operation and maintenance.
[0070] It should be noted that the standby instance needs to wait until the primary instance is started before switching states. All resources in the database cluster are created and managed by the second control component.
[0071] like Figure 2 As shown, an embodiment of the present invention provides an instance switching method for a cloud database, which is applied to a first control component, and the method includes:
[0072] Step S201, obtaining a first state of a first instance in a cloud database where the first control component is located, and / or obtaining a second state of at least one second instance in the cloud database associated with the first instance;
[0073] In step S201, one first control component arbitrates one database instance, and for each first control component, a first state of the first instance corresponding to the arbitration is obtained, and a second state of a second instance (other database instances except the first instance) is obtained through monitoring, wherein the first state includes but is not limited to any one of the instance first startup, instance restart, and instance failure, and the second state includes but is not limited to any one of the instance first startup, instance restart, and instance failure;
[0074] Step S202: Determine the instance state of the first instance according to the instance information of the primary instance and the backup instance stored in the arbitration storage area, the first state and / or the second state, wherein the instance state is the primary instance or the backup instance.
[0075] In step S202, the instance information of the primary instance includes but is not limited to the instance name, Internet Protocol (IP) address or domain name, the node where the instance is located, the instance status timestamp and the time when the instance is promoted to the primary instance, etc.; the instance information of the backup instance includes but is not limited to the instance name, IP address or domain name, the node where the instance is located, the instance status timestamp and the time when the backup instance is arbitrated, etc.;
[0076] Due to the business pressure in the production environment, the reliability of the external environment and the influence of human factors, different scenarios may occur in the database cluster (the first state and the second state are different in different scenarios). Different instance state determination schemes are required for different scenarios. Therefore, the first control component first determines the instance state determination scheme according to the first state and / or the second state, and then finally determines the instance state of the first instance in combination with the instance information stored in the arbitration storage area.
[0077] In an embodiment of the present invention, the first control component is integrated with the database instance. One first control component arbitrates the instance status of one instance and monitors the operation of other instances, thereby realizing the switching of the primary and standby instance status, reducing resource requirements and deployment difficulty, and effectively improving operation and maintenance efficiency.
[0078] Optionally, determining the instance state of the first instance according to the instance information of the primary instance and the backup instance stored in the arbitration storage area, the first state and / or the second state includes one or more of the following:
[0079] When both the first state and the second state are instances started for the first time and the arbitration storage area does not include instance information of the primary instance, determining that the instance state of the first instance is the primary instance;
[0080] When both the first state and the second state are the first startup of the instance and the arbitration storage area includes instance information of the primary instance, it is determined that the instance state of the first instance is a standby instance.
[0081] In an embodiment of the present invention, when both the first state and the second state are the first startup of the instance, it means that the entire database cluster is started for the first time, and the first started instance is determined as the primary instance. Therefore, the first control component determines whether the arbitration storage area includes instance information of the primary instance. If included, it means that the primary instance has been determined, and the instance state of the first instance is set to the standby instance. If not included, it means that the first instance is the first started instance, and the instance state of the first instance is set to the primary instance.
[0082] Optionally, determining the instance state of the first instance according to the instance information of the primary instance and the backup instance stored in the arbitration storage area, the first state and / or the second state includes one or more of the following:
[0083] When the first state is instance restart and the instance information of the primary instance stored in the arbitration storage area matches the instance information of the first instance, determining that the instance state of the first instance is the primary instance;
[0084] When the first state is instance restart and the instance information of the primary instance stored in the arbitration storage area does not match the instance information of the first instance, it is determined that the instance state of the first instance is a standby instance.
[0085] In an embodiment of the present invention, when the first state is instance restart, the instance information of the main instance before the restart stored in the arbitration storage area is obtained, and the instance information of the main instance before the restart is matched with the instance information of the first instance. If the match is successful, it means that the first instance is the main instance before the restart, and the instance state of the first instance is determined as the main instance. If the match fails, it means that the first instance is not the main instance before the restart, but a backup instance.
[0086] Optionally, when the instance information of the primary instance stored in the arbitration storage area does not match the instance information of the first instance, determining that the instance state of the first instance is a standby instance includes:
[0087] In a case where the instance information of the primary instance stored in the arbitration storage area does not match the instance information of the first instance, monitoring whether the primary instance stored in the arbitration storage area is started;
[0088] In the case where it is monitored that the primary instance stored in the arbitration storage area has been started, it is determined that the instance state of the first instance is a standby instance.
[0089] In the embodiment of the present invention, the situation that the first instance is not the main instance before the restart but the standby instance is further explained. After determining that the first instance is the standby instance, the instance state of the first instance is not switched first, but waits and monitors whether the main instance stored in the arbitration storage area is started and determines. When it is monitored that the main instance stored in the arbitration storage area has been started, the instance state of the first instance is immediately switched to the standby instance.
[0090] It should be noted that when an instance is restarted, multiple instances are started in sequence. The first control component corresponding to the first restarted instance needs to determine whether only the corresponding first instance is restarted or the entire database cluster is restarted. Therefore, when it is determined that the instance information of the primary instance stored in the arbitration storage area does not match the instance information of the first instance, first try to connect to the second instance. If the connection with the second instance fails, it means that the second instance is also restarting and has not started successfully, then it is determined that the entire database cluster is restarted, that is, the first state and the second state are both instance restarts. If the connection with the second instance is successful, it means that only the corresponding first instance is restarted.
[0091] Optionally, determining the instance state of the first instance according to the instance information of the primary instance and the backup instance stored in the arbitration storage area, the first state and / or the second state includes:
[0092] When the second state of at least one of the second instances is an instance failure, and the second instance with the instance failure is a primary instance, acquiring instance information of the standby instance stored in the arbitration storage area, the instance information including a state timestamp;
[0093] Compare the state timestamp of the first instance with the state timestamp of the standby instance;
[0094] When the state timestamp of the first instance is greater than the state timestamp of any of the standby instances, determining that the instance state of the first instance is a primary instance;
[0095] When the state timestamp of the first instance is less than or equal to the state timestamp of at least one of the standby instances, it is determined that the instance state of the first instance is a standby instance.
[0096] In an embodiment of the present invention, when the first instance is a standby instance, the primary instance is periodically accessed. If the primary instance cannot be accessed, it is considered that the primary instance fails, and it is necessary to determine an instance as a new primary instance from multiple standby instances. First, the instance information of all standby instances stored in the arbitration storage area is obtained, and the instance information includes a status timestamp. The status timestamp of the first instance and the status timestamps of the other standby instances are compared. If the status timestamp of the first instance is greater than the status timestamp of any standby instance, the instance status of the first instance is determined to be the primary instance, otherwise it is determined to be the standby instance.
[0097] It should be noted that the instance status timestamp records the completeness of the database in each instance. The larger the status timestamp, the more complete and reliable the data on the instance is, and it should be promoted to the primary instance first.
[0098] It should be noted that the standby instance needs to wait until the main instance is started before it can switch its state. Therefore, when the first instance is not the main instance, it is necessary to poll whether the main instance in the arbitration storage area has been determined and started. When it is determined that the main instance in the arbitration storage area has been determined and started, the instance state of the first instance is determined as a standby instance.
[0099] Optionally, when the second instance of the instance failure is a primary instance, obtaining instance information of the standby instance stored in the arbitration storage area includes:
[0100] In the case where the second instance of the instance failure is the primary instance, atomically updating the instance information of the first instance in the arbitration storage area and monitoring whether the instance information of the standby instance stored in the arbitration storage area has completed the atomic update;
[0101] After monitoring that the instance information of the standby instance stored in the arbitration storage area has completed atomic update, the updated instance information of the standby instance stored in the arbitration storage area is obtained.
[0102] In an embodiment of the present invention, in the event of a master instance failure, it is necessary to determine an instance from multiple backup instances as a new master instance, obtain instance information of all backup instances stored in an arbitration storage area for comparison, wherein the obtained instance information of the backup instances is updated information, and when a master instance failure is detected, first, each first control component atomically updates the instance information of the first instance to the arbitration storage area, and after monitoring that the instance information of all instances has been atomically updated, obtain instance information of all backup instances stored in the arbitration storage area for comparison.
[0103] Optionally, before obtaining a first state of a first instance in a cloud database where the first control component is located and obtaining a second state of at least one second instance associated with the first instance in the cloud database, the method further includes:
[0104] In the case of monitoring that the first instance is started, determining whether the arbitration storage area exists;
[0105] If the arbitration storage area does not exist, the arbitration storage area is created and it is determined that both the first state and the second state are the first startup of the instance.
[0106] In the embodiment of the present invention, when an instance is started, multiple instances are started in sequence. The first control component corresponding to the first started instance needs to first determine whether the arbitration storage area exists. If it does not exist, it means that the entire database cluster is started for the first time. The first state and the second state are determined as the first start of the instance, and the arbitration storage area is created. It should be noted that after the first control component corresponding to the first started instance determines the first state and the second state, the subsequently started instances do not need to determine them again.
[0107] Optionally, before obtaining the first state of the first instance where the first control component is located in the cloud database, the method further includes:
[0108] In the case of monitoring that the first instance is started, determining whether the arbitration storage area exists;
[0109] In a case where the arbitration storage area exists, it is determined that the first state is the instance restarting.
[0110] In an embodiment of the present invention, when an instance is restarted, multiple instances are started in sequence. When the instance is started, the corresponding first control component needs to first determine whether the arbitration storage area exists. If it exists, it means that the corresponding first instance is restarted, and the first state is determined as the instance restart. It should be noted that the premise of determining the first state as the instance restart is to determine that the first state is not the first startup of the instance.
[0111] Optionally, the arbitration storage area includes a primary instance arbitration storage area and a backup instance arbitration storage area;
[0112] The method further comprises:
[0113] When it is determined that the instance state of the first instance is a master instance, storing the instance information of the first instance in the master instance arbitration storage area;
[0114] When it is determined that the instance state of the first instance is a standby instance, the instance information of the first instance is stored in the standby instance arbitration storage area.
[0115] In the embodiment of the present invention, the arbitration storage area is divided into a primary instance arbitration storage area and a backup instance arbitration storage area. The primary instance arbitration storage area is used to store instance information of the primary instance, and the backup instance arbitration storage area is used to store instance information of the backup instance, thereby reducing frequent resource update conflicts between instances caused by too many instances, facilitating quick query and improving operation and maintenance efficiency.
[0116] When it is determined that the instance state of the first instance is the primary instance, the instance information of the first instance is stored or updated in the primary instance arbitration storage area; when it is determined that the instance state of the first instance is the backup instance, the instance information of the first instance is stored or updated in the backup instance arbitration storage area.
[0117] like Figure 3 As shown, an embodiment of the present invention further provides an instance switching method for a cloud database, which is applied to a second control component, and the method includes:
[0118] Step S301, monitoring the instance state and fault state of each instance in the cloud database, and monitoring the instance state and fault state of each instance in the arbitration storage area, wherein the instance state is a primary instance or a backup instance;
[0119] Step S302, when it is monitored that the instance state of at least one instance in the cloud database is different from the instance state stored in the arbitration storage area, modify the instance state stored in the arbitration storage area according to the instance state of the instance;
[0120] Step S303: when it is detected that the fault state of at least one instance in the cloud database is different from the fault state stored in the arbitration storage area, the fault state stored in the arbitration storage area is corrected according to the fault state of the instance.
[0121] In the embodiment of the present invention, the second control component has an instance state repair function, which monitors the instance state and fault state of each instance in the cloud database and the instance state and fault state of each instance in the arbitration storage area in real time, and when the instance state of the instance in the arbitration storage area is different from the actual instance state of the instance, corrects the instance state stored in the arbitration storage area, and when the fault state of the instance in the arbitration storage area is different from the actual fault state of the instance, corrects the fault state stored in the arbitration storage area.
[0122] Optionally, the fault state is instance fault or instance normal;
[0123] When it is detected that the fault state of at least one instance in the cloud database is different from the fault state stored in the arbitration storage area, the fault state stored in the arbitration storage area is corrected according to the fault state of the instance, including:
[0124] When it is monitored that the fault state of at least one instance in the cloud database is an instance fault and is different from the fault state stored in the arbitration storage area, determining whether the faulty instance is a primary instance or a backup instance;
[0125] In the case where the failed instance is a primary instance, correcting the failure state of the failed primary instance to an instance failure in the primary instance arbitration storage area;
[0126] If the failed instance is a standby instance, restart the failed standby instance;
[0127] When the failed standby instance fails to restart and the number of restarts is greater than a first threshold, the failure state of the failed standby instance is corrected to instance failure in the standby instance arbitration storage area.
[0128] In an embodiment of the present invention, when at least one instance failure is monitored, it is determined whether it is a primary instance failure or a standby instance failure. If it is a primary instance failure, the impact on the entire database is relatively large, and the failure state of the primary instance is directly corrected to an instance failure in the primary instance arbitration storage area, which is conducive to the promotion of other standby instances to primary instances. If it is a standby instance failure, the impact on the entire database is relatively small, and the second control component continuously attempts to restart the failed standby instance. When the number of restarts is greater than the first threshold and the restart still fails, the failure state of the failed standby instance is corrected to an instance failure in the standby instance arbitration storage area to prevent other instances from establishing invalid connections with the instance.
[0129] Optionally, the method further comprises:
[0130] Receive a first instruction sent by the client;
[0131] Creating a database cluster resource according to the first instruction, wherein the database cluster resource includes a plurality of database instances;
[0132] After monitoring that the database cluster resource is created, create external access resources.
[0133] In an embodiment of the present invention, a second control component receives a first instruction sent by a customer, where the first instruction is used to create a database cluster; the second control component creates database cluster resources and external access resources in sequence according to the first instruction, wherein when creating the database cluster resources, database instances are started in sequence, and at this time, the first state and the second state are the first startup of the instance.
[0134] To summarize, in the deployment scenario of a cloud-native database cluster, a first control component is created inside each instance, which can monitor the status of the corresponding instance in real time and efficiently, reduce resource requirements, and eliminate the impact of external network anomalies on the node; the arbitration storage area is divided into a primary instance arbitration storage area and a backup instance arbitration storage area. The primary instance arbitration storage area is used to store the instance information of the primary instance, and the backup instance arbitration storage area is used to store the instance information of the backup instance, thereby reducing frequent resource update conflicts between instances caused by too many instances, facilitating quick query and improving operation and maintenance efficiency; the K8S resource lock is used to perform periodic maintenance and updates on the primary instance, wherein, according to the principle of the resource lock, the heartbeat detection of the primary instance can be lightweight simulated, which can play the same role and reduce the deployment complexity and the resources required for deployment; the second control component has a state repair function, which monitors the instance status and fault status of the instance in real time and promptly corrects the corresponding instance status and fault status in the arbitration storage area.
[0135] In the four situations of instance initial startup, instance restart, primary instance failure, and backup instance failure, the instance status can be switched accurately and efficiently to ensure the normal operation of the database and increase the reliability and adaptability of the database cluster.
[0136] like Figure 4 As shown, an embodiment of the present invention further provides an instance switching device for a cloud database, which is applied to a first control component and includes:
[0137] A first acquisition module 401 is used to acquire a first state of a first instance in a cloud database where the first control component is located, and / or acquire a second state of at least one second instance in the cloud database associated with the first instance;
[0138] The first determination module 402 is used to determine the instance state of the first instance according to the instance information of the first instance and the second instance stored in the arbitration storage area, the first state and / or the second state, wherein the instance state is a primary instance or a backup instance, and the arbitration storage area includes a primary instance arbitration storage area and a backup instance arbitration storage area.
[0139] Optionally, the first determining module 402 includes:
[0140] A first determining unit is configured to determine that the instance state of the first instance is the primary instance when both the first state and the second state are the first startup of the instance and the arbitration storage area does not include the instance information of the primary instance;
[0141] The second determining unit is configured to determine that the instance state of the first instance is a standby instance when both the first state and the second state are initialization of the instance and the arbitration storage area includes instance information of the primary instance.
[0142] Optionally, the first determining module 402 further includes:
[0143] a third determining unit, configured to determine that the instance state of the first instance is the primary instance when the first state is instance restart and the instance information of the primary instance stored in the arbitration storage area matches the instance information of the first instance;
[0144] The fourth determining unit is configured to determine that the instance state of the first instance is a standby instance when the first state is instance restart and the instance information of the primary instance stored in the arbitration storage area does not match the instance information of the first instance.
[0145] Optionally, the fourth determining unit includes:
[0146] A first monitoring unit, configured to monitor whether the primary instance stored in the arbitration storage area is started when the instance information of the primary instance stored in the arbitration storage area does not match the instance information of the first instance;
[0147] The fifth determining unit is configured to determine that the instance state of the first instance is a standby instance when it is detected that the primary instance stored in the arbitration storage area has been started.
[0148] Optionally, the first determining module 402 further includes:
[0149] A first acquisition unit is configured to acquire instance information of a standby instance stored in the arbitration storage area when the second state of at least one second instance is instance failure and the second instance of instance failure is a primary instance, wherein the instance information includes a state timestamp;
[0150] A first comparison unit, configured to compare the state timestamp of the first instance with the state timestamp of the standby instance;
[0151] A sixth determining unit, configured to determine that the instance state of the first instance is a primary instance when the state timestamp of the first instance is greater than the state timestamp of any of the standby instances;
[0152] The seventh determining unit is configured to determine that the instance state of the first instance is a standby instance when the state timestamp of the first instance is less than or equal to the state timestamp of at least one of the standby instances.
[0153] Optionally, the first acquiring unit includes:
[0154] A first updating unit, configured to atomically update instance information of the first instance in the arbitration storage area and monitor whether the instance information of the standby instance stored in the arbitration storage area has been atomically updated when the second instance of the instance failure is the primary instance;
[0155] The second acquisition unit is configured to acquire the updated instance information of the standby instance stored in the arbitration storage area after monitoring that the instance information of the standby instance stored in the arbitration storage area has completed atomic update.
[0156] Optionally, the device further comprises:
[0157] A first judgment module is used to judge whether the arbitration storage area exists when monitoring that the first instance is started;
[0158] The second determining module is configured to create the arbitration storage area if the arbitration storage area does not exist and determine that both the first state and the second state are the first startup of the instance.
[0159] Optionally, the device further comprises:
[0160] A second judgment module is used to judge whether the arbitration storage area exists when monitoring that the first instance is started;
[0161] The third determining module is configured to determine, if the arbitration storage area exists, that the first state is the restart of the instance.
[0162] Optionally, the arbitration storage area in the first determination module 402 includes a primary instance arbitration storage area and a backup instance arbitration storage area;
[0163] The device also includes:
[0164] A first storage module is used to store instance information of the first instance in the master instance arbitration storage area when it is determined that the instance state of the first instance is a master instance;
[0165] The second storage module is configured to store instance information of the first instance in the standby instance arbitration storage area when it is determined that the instance state of the first instance is a standby instance.
[0166] It should be noted that the embodiment of the device is a device corresponding to the embodiment of the above method, and all implementation methods in the embodiment of the above method are applicable to the embodiment of the device and can achieve the same technical effect.
[0167] like Figure 5 As shown, an embodiment of the present invention further provides an instance switching device for a cloud database, which is applied to a second control component and includes:
[0168] The first monitoring module 501 is used to monitor the instance state and fault state of each instance in the cloud database, and monitor the instance state and fault state of each instance in the arbitration storage area, wherein the state includes the primary instance or the backup instance, and the arbitration storage area includes the primary instance arbitration storage area and the backup instance arbitration storage area;
[0169] A first correction module 502 is used to correct the instance state stored in the arbitration storage area according to the instance state of at least one instance in the cloud database when it is detected that the instance state of the instance is different from the instance state stored in the arbitration storage area;
[0170] The second correction module 503 is used to correct the fault state stored in the arbitration storage area according to the fault state of at least one instance in the cloud database when it is detected that the fault state of the instance is different from the fault state stored in the arbitration storage area.
[0171] Optionally, the fault state monitored by the first monitoring module 501 is instance fault or instance normal;
[0172] The second correction module 503 includes:
[0173] A first judgment unit is used to judge whether the failed instance is a primary instance or a backup instance when it is detected that the failure state of at least one instance in the cloud database is an instance failure and is different from the failure state stored in the arbitration storage area;
[0174] A first correction unit is used to correct the fault state of the failed primary instance to an instance fault in the primary instance arbitration storage area when the failed instance is a primary instance;
[0175] A first restart unit, configured to restart the failed standby instance when the failed instance is a standby instance;
[0176] The second correction unit is configured to correct the fault state of the faulty standby instance to an instance fault in the standby instance arbitration storage area when the faulty standby instance fails to restart and the number of restarts is greater than a first threshold.
[0177] It should be noted that the embodiment of the device is a device corresponding to the embodiment of the above method, and all implementation methods in the embodiment of the above method are applicable to the embodiment of the device and can achieve the same technical effect.
[0178] An embodiment of the present invention also provides a network device, comprising: a processor, a memory, and a program stored in the memory and executable on the processor. When the program is executed by the processor, the program implements the instance switching method for a cloud database as described in any of the above items, and can achieve the same technical effect. To avoid repetition, it will not be described here.
[0179] The embodiment of the present invention also provides a readable storage medium, including: a program is stored on the readable storage medium, and when the program is executed by the processor, the steps of the instance switching method for a cloud database as described in any of the above items are implemented, and the same technical effect can be achieved. To avoid repetition, it is not repeated here. The computer-readable storage medium is, for example, a read-only memory (ROM), a random access memory (RAM), a disk or an optical disk, etc.
[0180] An embodiment of the present invention further provides a computer program product, including computer instructions. When the computer instructions are executed by a processor, the steps of the instance switching method for a cloud database as described in any of the above items are implemented, and the same technical effect can be achieved. To avoid repetition, they are not described here.
[0181] It should be noted that, in this article, relational terms such as first and second, etc. are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or terminal device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.
[0182] Obviously, those skilled in the art can make various changes and modifications to the present invention without departing from the spirit and scope of the present invention. Thus, if these modifications and variations of the present invention fall within the scope of the claims of the present invention and their equivalents, the present invention is also intended to include these modifications and variations.
Claims
1. A method for switching instances of a cloud database, characterized in that: Applied to the first control component, the method comprises: Acquire a first state of a first instance in a cloud database where the first control component is located, and / or acquire a second state of at least one second instance in the cloud database associated with the first instance; The instance state of the first instance is determined according to the instance information of the primary instance and the standby instance stored in the arbitration storage area, the first state and / or the second state, wherein the instance state is the primary instance or the standby instance.
2. The instance switching method for a cloud database according to claim 1, characterized in that: Determining the instance state of the first instance according to the instance information of the primary instance and the backup instance stored in the arbitration storage area, the first state, and / or the second state includes one or more of the following: When both the first state and the second state are instances started for the first time and the arbitration storage area does not include instance information of the primary instance, determining that the instance state of the first instance is the primary instance; When both the first state and the second state are the first startup of the instance and the arbitration storage area includes instance information of the primary instance, it is determined that the instance state of the first instance is a standby instance.
3. The instance switching method for a cloud database according to claim 1, characterized in that: Determining the instance state of the first instance according to the instance information of the primary instance and the backup instance stored in the arbitration storage area, the first state, and / or the second state includes one or more of the following: When the first state is instance restart and the instance information of the primary instance stored in the arbitration storage area matches the instance information of the first instance, determining that the instance state of the first instance is the primary instance; When the first state is instance restart and the instance information of the primary instance stored in the arbitration storage area does not match the instance information of the first instance, it is determined that the instance state of the first instance is a standby instance.
4. The instance switching method for a cloud database according to claim 3, characterized in that: When the instance information of the primary instance stored in the arbitration storage area does not match the instance information of the first instance, determining that the instance state of the first instance is a standby instance includes: In a case where the instance information of the primary instance stored in the arbitration storage area does not match the instance information of the first instance, monitoring whether the primary instance stored in the arbitration storage area is started; In the case where it is monitored that the primary instance stored in the arbitration storage area has been started, it is determined that the instance state of the first instance is a standby instance.
5. The instance switching method for a cloud database according to claim 1, characterized in that: Determining the instance state of the first instance according to the instance information of the primary instance and the backup instance stored in the arbitration storage area, the first state, and / or the second state includes: When the second state of at least one of the second instances is an instance failure, and the second instance with the instance failure is a primary instance, acquiring instance information of the standby instance stored in the arbitration storage area, the instance information including a state timestamp; Compare the state timestamp of the first instance with the state timestamp of the standby instance; When the state timestamp of the first instance is greater than the state timestamp of any of the standby instances, determining that the instance state of the first instance is a primary instance; When the state timestamp of the first instance is less than or equal to the state timestamp of at least one of the standby instances, it is determined that the instance state of the first instance is a standby instance.
6. The instance switching method for a cloud database according to claim 5, characterized in that: When the second instance of the instance failure is the primary instance, obtaining the instance information of the standby instance stored in the arbitration storage area includes: In the case where the second instance of the instance failure is the primary instance, atomically updating the instance information of the first instance in the arbitration storage area and monitoring whether the instance information of the standby instance stored in the arbitration storage area has completed the atomic update; After monitoring that the instance information of the standby instance stored in the arbitration storage area has completed atomic update, the updated instance information of the standby instance stored in the arbitration storage area is obtained.
7. The instance switching method for a cloud database according to claim 2, characterized in that: Before obtaining a first state of a first instance in a cloud database where the first control component is located and obtaining a second state of at least one second instance in the cloud database associated with the first instance, the method further includes: In the case of monitoring that the first instance is started, determining whether the arbitration storage area exists; If the arbitration storage area does not exist, the arbitration storage area is created and it is determined that both the first state and the second state are the first startup of the instance.
8. The instance switching method for a cloud database according to claim 3, characterized in that: Before obtaining the first state of the first instance where the first control component is located in the cloud database, the method further includes: In the case of monitoring that the first instance is started, determining whether the arbitration storage area exists; In a case where the arbitration storage area exists, it is determined that the first state is the instance restarting.
9. The instance switching method for a cloud database according to claim 1, characterized in that: The arbitration storage area includes a primary instance arbitration storage area and a backup instance arbitration storage area; The method further comprises: When it is determined that the instance state of the first instance is a master instance, storing the instance information of the first instance in the master instance arbitration storage area; When it is determined that the instance state of the first instance is a standby instance, the instance information of the first instance is stored in the standby instance arbitration storage area.
10. A method for switching instances of a cloud database, characterized in that: Applied to the second control component, the method comprises: Monitor the instance status and fault status of each instance in the cloud database, and monitor the instance status and fault status of each instance in the arbitration storage area, wherein the instance status is a primary instance or a backup instance; When it is detected that the instance state of at least one instance in the cloud database is different from the instance state stored in the arbitration storage area, the instance state stored in the arbitration storage area is modified according to the instance state of the instance; When it is detected that the fault state of at least one instance in the cloud database is different from the fault state stored in the arbitration storage area, the fault state stored in the arbitration storage area is corrected according to the fault state of the instance.
11. The instance switching method for a cloud database according to claim 10, characterized in that: The fault status is instance fault or instance normal; When it is detected that the fault state of at least one instance in the cloud database is different from the fault state stored in the arbitration storage area, the fault state stored in the arbitration storage area is corrected according to the fault state of the instance, including: When it is monitored that the fault state of at least one instance in the cloud database is an instance fault and is different from the fault state stored in the arbitration storage area, determining whether the faulty instance is a primary instance or a backup instance; In the case where the failed instance is a primary instance, correcting the failure state of the failed primary instance to an instance failure in the primary instance arbitration storage area; If the failed instance is a standby instance, restart the failed standby instance; When the failed standby instance fails to restart and the number of restarts is greater than a first threshold, the failure state of the failed standby instance is corrected to instance failure in the standby instance arbitration storage area.
12. An instance switching device for a cloud database, characterized in that: Applicable to the first control component, comprising: A first acquisition module, configured to acquire a first state of a first instance in a cloud database where the first control component is located, and / or acquire a second state of at least one second instance in the cloud database associated with the first instance; A first determination module is configured to determine an instance state of the first instance according to instance information of the first instance and the second instance stored in an arbitration storage area, the first state, and / or the second state, wherein the instance state is a primary instance or a backup instance, and the arbitration storage area includes a primary instance arbitration storage area and a backup instance arbitration storage area.
13. An instance switching device for a cloud database, characterized in that: Applicable to the second control component, including: A first monitoring module is used to monitor the instance state and fault state of each instance in the cloud database, and to monitor the instance state and fault state of each instance in the arbitration storage area, wherein the state includes a primary instance or a standby instance, and the arbitration storage area includes a primary instance arbitration storage area and a standby instance arbitration storage area; A first correction module is used to correct the instance state stored in the arbitration storage area according to the instance state of at least one instance in the cloud database when it is detected that the instance state of the instance is different from the instance state stored in the arbitration storage area; The second correction module is used to correct the fault state stored in the arbitration storage area according to the fault state of at least one instance in the cloud database when it is detected that the fault state of the instance is different from the fault state stored in the arbitration storage area.
14. A network device, characterized in that: include: A processor, a memory, and a program stored in the memory and executable on the processor, wherein the program, when executed by the processor, implements the instance switching method for a cloud database as described in any one of claims 1 to 9 or implements the instance switching method for a cloud database as described in any one of claims 10 to 11.
15. A readable storage medium, characterized in that: include: The readable storage medium stores a program, and when the program is executed by the processor, the program implements the steps of the instance switching method for a cloud database as described in any one of claims 1 to 9 or the steps of the instance switching method for a cloud database as described in any one of claims 10 to 11.
16. A computer program product, characterized in that The method comprises computer instructions, which, when executed by a processor, implement the steps of the instance switching method for a cloud database as claimed in any one of claims 1 to 9 or implement the steps of the instance switching method for a cloud database as claimed in any one of claims 10 to 11.
