User-zero-awareness database master-slave switching method and device
By detecting the completion of master-slave database switching and session synchronization in the database driver, cache and resend session requests, the problem of client connection disconnection and request loss during master-slave database switching is solved, and the unaware switching process is realized, improving the system availability and user experience.
Patent Information
- Application Number
- CN202411696619.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-11-25
- Publication Date
- 2025-03-25
- Estimated Expiration
- 2044-11-25
AI Technical Summary
In the prior art, when the master-slave database is switched, the client connection is disconnected and the request is lost, resulting in poor user experience.
The database driver detects whether the master and slave database have completed the switch and realizes session synchronization, caches session requests during the switch, and sends them to the new master database after the switch is completed, thereby ensuring the continuity of session requests.
It realizes the unaware experience of the client during the master-slave database switching process, improves the availability and stability of the system, and improves the user experience and application performance and reliability.
Smart Images

Figure CN119690940A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of database technology, and in particular to a method for switching a database master-slave with zero user perception, a device for switching a database master-slave with zero user perception, and an electronic device. Background Art
[0002] With the widespread use of databases in modern applications, the requirements for the availability and fault tolerance of database systems are becoming increasingly higher. Among the many database architectures, the master-slave database architecture is widely used to provide high availability and disaster tolerance of data. In a master-slave database, the master database is responsible for receiving and processing write operations, while the slave database provides read services by replicating the data of the master database and completing the failover takeover of the business when the master database fails.
[0003] Although the master-slave database can provide overall system availability, when the master database fails or maintenance switch occurs, even if the lower-level database completes the master-slave switch, the client connection still needs to be manually or automatically switched to the new master database node. This master-slave switching process is usually perceptible to applications and clients, which can cause connection disconnection and request loss. For a large number of upper-level clients, the user experience is very poor. Therefore, it is necessary to provide a solution for imperceptible master-slave switching to improve the reliability of the system and the convenience of master-slave switching. Summary of the invention
[0004] The embodiment of the present invention provides a method and device for master-slave switching of a database with zero user perception, so as to solve the problem in the prior art that client connection is disconnected, request is lost, and experience is poor when master-slave switching occurs.
[0005] The embodiment of the present invention discloses a method for master-slave switching of a database with zero user perception, comprising:
[0006] When the database driver detects that the master database and the slave database have switched, it determines whether the master database and the slave database have completed the master-slave switch and achieved session synchronization;
[0007] If so, the database driver sends the session request cached during the master-slave switch to the slave database that is the current master database, so that the slave database processes the session request;
[0008] The database driver receives the response returned from the database according to the session request, and sends the response to the client to which the session request belongs.
[0009] Optionally, before the database driver detects that the master database and the slave database are switched, the method further includes:
[0010] The database driver is connected to the master database and the slave database according to the multi-data source configuration information, and determines whether the master database is switched through heartbeat detection and the master database status and the slave database status;
[0011] If no switch occurs, the client's session request is sent to the primary database;
[0012] If the database driver does not receive a response to the session request returned by the master database, the master database state and the slave database state are judged to determine whether a switch between the master database and the slave database occurs;
[0013] If so, cache the client's session requests during the switch between the primary and secondary databases.
[0014] Optionally, determining whether the master database and the slave database have completed the master-slave switch and achieved session synchronization includes:
[0015] The database driver determines whether the master-slave switch is completed through heartbeat detection and the status of the master database and the status of the slave database. If not, it waits, and if the cached session request times out during the waiting period, it sends a failure response to the client of the timed-out session request;
[0016] The connection list and session information in the master database are synchronized to the slave database so that the slave database can parse and load, and the session continuity is achieved when a session request sent by the database driver is received.
[0017] Optionally, the database driver sends the session request cached during the master-slave switch to the slave database that is the current master database, including:
[0018] The database driver obtains all session requests cached by the current database driver from the memory, and sends the session requests to the slave database which is the current master database.
[0019] The embodiment of the present invention also provides a method for master-slave switching of a database with zero user perception, including:
[0020] When the master database receives a connection request from the data driver, it verifies the connection. If the verification passes, it connects to the data driver and synchronizes the connection information, context information, and log information to the slave database.
[0021] The master database receives a response from the slave database indicating that synchronization information processing is successful, and then sends the information of the master database and the information of the slave database to the data driver program so that the data driver program is stored in the memory;
[0022] The main database receives the client's session request sent by the data driver, and after processing, the main database returns a response to the session request to the data driver, so that the data driver will respond to the client that sent the session request.
[0023] Optionally, it also includes:
[0024] When the master-slave switch occurs, the hook function in the master database captures the failure event and writes the session information in the master database into the specified WAL file, distinguishing it from the WAL file that records the log. The WAL file that records the session information and the WAL file that records the log are sent to the slave database through streaming replication.
[0025] Optionally, it also includes:
[0026] The slave database that serves as the new primary database receives the WAL file that records the log, parses it, obtains the connection list, reads the connection events, reads the session information into the memory, and waits for the session request sent by the data driver to achieve session continuity.
[0027] The embodiment of the present invention also discloses a database master-slave switching system with zero user perception, including:
[0028] A master database, slave databases, database drivers, and multiple clients;
[0029] When the database driver detects that the master database and the slave database have switched, it determines whether the master database and the slave database have completed the master-slave switch and achieved session synchronization;
[0030] If so, the database driver sends the session request cached during the master-slave switch to the slave database that is the current master database, so that the slave database processes the session request;
[0031] The database driver receives the response returned from the database according to the session request, and sends the response to the client to which the session request belongs.
[0032] The embodiment of the present invention also discloses a database master-slave switching device with zero user perception, including:
[0033] A detection unit, used to determine whether the master database and the slave database have completed the master-slave switch and achieved session synchronization when detecting that the master database and the slave database have switched;
[0034] The switching unit is used to send the session request cached during the master-slave switching to the slave database as the current master database when the detection unit determines that the master-slave switching is completed, so that the slave database processes the session request;
[0035] The receiving unit is used to receive the response returned from the database according to the session request, and send the response to the client to which the session request belongs.
[0036] The embodiment of the present invention further discloses an electronic device, comprising a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other via the communication bus;
[0037] The memory is used to store computer programs;
[0038] The processor is used to implement the method described in the embodiment of the present invention when executing the program stored in the memory.
[0039] The embodiment of the present invention further discloses one or more computer-readable media on which instructions are stored. When executed by one or more processors, the processors are enabled to execute the method described in the embodiment of the present invention.
[0040] The embodiments of the present invention include the following advantages:
[0041] The user-free master-slave switching method of the database in the embodiment of the present invention provides a safe and fast way to handle the connection interruption and request loss problems during the master-slave switching process. Specifically, the client's session request is cached during the master-slave switching through the data driver, and then after the master-slave database switching is completed and the session information is synchronized, the cached session request is sent to the current new master database, thereby improving the availability and stability of the entire master-slave switching system.
[0042] During the master-slave switching process, connection events and session information are synchronized between the master and slave databases, and the data driver then sends the session request to the new master database, ensuring session continuity. This greatly improves the user experience in the master-slave database failover scenario, allowing users to continuously access and operate the database without having to pay attention to the details of the master-slave switching, ultimately improving the performance and reliability of the application.
[0043] The method of the embodiment of the present invention can achieve a client-impactless experience during the master-slave switching process, and provides an innovative solution for the database architecture in modern applications. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] Figure 1 It is a structural block diagram of a database master-slave switching system with zero user perception provided in an embodiment of the present invention;
[0045] Figure 2 It is a flowchart of the steps of a method for master-slave switching of a database with zero user perception provided in an embodiment of the present invention;
[0046] Figure 3 It is a flowchart of the steps of a method for master-slave switching of a database with zero user perception provided in an embodiment of the present invention;
[0047] Figure 4 It is a flowchart of the steps of a method for master-slave switching of a database with zero user perception provided in an embodiment of the present invention;
[0048] Figure 5 It is a flowchart of the steps of a method for master-slave switching of a database with zero user perception provided in an embodiment of the present invention;
[0049] Figure 6 It is a structural block diagram of a database master-slave switching device with zero user perception provided in an embodiment of the present invention;
[0050] Figure 7 is a block diagram of an electronic device provided in an embodiment of the present invention;
[0051] Figure 8 is a schematic diagram of a computer-readable medium provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0052] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, the present invention is further described in detail below with reference to the accompanying drawings and specific embodiments.
[0053] In view of the technical problems of poor user experience and low reliability in the prior art, an embodiment of the present invention provides a PostgreSQL master-slave switching method with zero user perception, which realizes a client-unaware experience during the master-slave switching process through a customized database driver and WAL log replication capability.
[0054] PostgreSQL is a free software object-relational database management system with a very complete set of features. PostgreSQL supports most of the SQL standard and provides many other modern features, such as complex queries, foreign keys, triggers, views, transaction integrity, multi-version concurrency control, etc. Similarly, PostgreSQL can also be extended in many ways, such as by adding new data types, functions, operators, aggregate functions, index methods, procedural languages, etc. In addition, because of the flexibility of the license, anyone can use, modify and distribute PostgreSQL for any purpose.
[0055] The user-free database master-slave switching system of the embodiment of the present invention mainly includes two parts:
[0056] (1) Add a database driver that can implement customized processing, record all node IP information of the master and slave databases, monitor in real time whether a master-slave switch occurs between the master and slave databases, and when a master-slave switch occurs, perceive the master-slave relationship, cache SQL records during the failover, and send requests to the new master database to achieve automatic retransmission of database traffic.
[0057] (2) Both the master database and the slave database have the ability to replicate WAL log files (i.e., WAL files), and synchronously replicate the connection information and session information of the master database to the slave database. The slave database parses the WAL log file and applies the connection information to the memory, realizing automatic synchronization of the connection information between the master and slave databases without manual intervention or reconnection to the database.
[0058] The imperceptible master-slave switching system in the embodiment of the present invention provides a safe and fast way to handle the connection interruption and request loss problems during the master-slave switching process, thereby improving the availability and stability of the system. At the same time, it greatly improves the user experience in the master-slave database failure switching scenario, so that users do not need to pay attention to the details of the master-slave switching, and can continue to access and operate the database, ultimately improving the performance and reliability of the application.
[0059] In this embodiment, the master library, master database, and master device all refer to the master database, and the slave library, slave database, slave device, and standby database all refer to the slave database. The master-slave switch in this embodiment refers to the current master database switching to the slave database.
[0060] WAL (Write Ahead Log) is a common method in database systems to ensure the atomicity and persistence of data operations.
[0061] Session: A session is a set of interactive communication activities, usually between a client and a server. It can be used to store user authentication information, user session data, and other related information, thereby providing a persistent connection.
[0062] RTO (Recovery Time Objective): Data recovery time refers to the time period from the moment the IT system crashes and causes business suspension to the moment the IT system recovers to the point where it can support the operation of various departments and business resumes operations after a disaster occurs.
[0063] Reference Figure 1 , showing a structural block diagram of a database master-slave switching system with zero user perception provided in an embodiment of the present invention, which may specifically include: multiple clients, a database driver, a master database and a slave database.
[0064] Each client in this embodiment connects to the database driver, sends a session request to the database driver, and the database driver sends the session request to the current master database. At the same time, when the master database does not switch between the master and slave databases, data synchronization is achieved between the master database and the slave database.
[0065] Specifically, the database driver can be customized by the operator before initialization to configure the data source configuration information of the master and slave databases, such as port information, specific connection information, etc., so that the database driver can establish connection information with the master database and the slave database.
[0066] The data driver of this embodiment is also used to connect to the client, receive the client's session request and then forward it to the master database, so as to realize the function of recording the master-slave database relationship and all node IP information. The database driver in this embodiment realizes the connection and verification with the master database and the slave database, while the client connects to the database driver, and the database driver sends the client's session request to the current master database.
[0067] By customizing the database driver, it supports master-slave multi-data source configuration, and can realize the function of recording the master-slave database and all node IP information.
[0068] When the database driver connects to the master database, the data driver will automatically obtain the node IP information of the master and slave databases and save it in the memory of the database driver in preparation for connection when the database is switched.
[0069] The database driver receives the session request sent by the client and forwards it to the master database. When a failure of the master database is detected, it determines whether the master and slave databases have completed the master-slave switch and achieved session synchronization.
[0070] If yes, the database driver sends the session request cached during the master-slave switch to the slave database that is the current master database, so that the slave database processes the session request;
[0071] The database driver receives the response returned from the database according to the session request, and sends the response to the client to which the session request belongs.
[0072] The data driver implements master database anomaly detection and switching through heartbeat detection and anomaly judgment; the data driver periodically sends heartbeat signals to the master database to detect the status of the master database. If the data driver cannot receive the heartbeat response from the master database, or receives a specific return for master-slave switching, it is determined that the master database is abnormal.
[0073] The data driver program periodically sends a heartbeat signal to the master database. The heartbeat signal is essentially a special message or request, which is intended to verify whether the master database is in normal operation. The frequency of sending the heartbeat signal is a carefully designed parameter, which needs to find a balance between timely detection of the master database status and avoiding excessive occupation of network resources. For example, it may be set to send a heartbeat signal every few seconds (such as 5 seconds).
[0074] After sending the heartbeat signal, the data driver will wait for the response from the master database. It will set a reasonable timeout period. If it does not receive the heartbeat response from the master database within this timeout period, it will start the abnormal judgment process. The setting of this timeout period needs to take into account factors such as network delay and the processing capacity of the master database. It may generally be set to several times the heartbeat signal sending cycle (such as 3 times the heartbeat cycle, i.e. 15 seconds).
[0075] When the master database receives the heartbeat signal, it will respond accordingly based on its own operating status. Under normal circumstances, the master database will return a normal heartbeat response. This response also contains some necessary information, such as the current load of the master database (such as the number of transactions currently being processed), the timestamp of the master database, etc. This information can be used by the data driver for further analysis and monitoring.
[0076] The data driver automatically switches to the new master node based on the saved master-slave connection information and re-issues the SQL request. This ensures that the client can switch to an available master node without noticing when the master fails.
[0077] After the master-slave switch occurs, the data driver determines that a switch has occurred, caches the request, and resends it to the new master after the master-slave switch is completed. Usually, the master-slave switch takes a certain amount of time. The data driver needs to determine that a master-slave switch has occurred, and needs to cache the SQL within the uncompleted time. After determining that the switch is complete, resend the previously uncompleted SQL request to the new master node.
[0078] Both the master database and the slave database implement the connection information synchronization function.
[0079] If hook functions are set in both the master database and the slave database, the connection information of the master database can be synchronously copied to the slave database. That is, the ability to copy WAL log files is realized, and the connection information of the master database is synchronously copied to the slave database.
[0080] The hook function writes the connection information of the master database to the WAL log file and replicates it to the slave database. The slave database reads and loads the session synchronously. Ensure that the slave database has the same connection information as the master database so that the client can connect to the correct master database node when the master-slave switch occurs.
[0081] The information synchronization between the master database and the slave database can be understood as data replication technology, which is used to improve the availability and reliability of the system and ensure the consistency of data. In this embodiment, the session information synchronization is mainly to maintain the consistency of the session information so that the client does not perceive the session disconnection and needs to re-establish the connection.
[0082] A hook function is a function that is called when a specific event or action occurs. It is a callback mechanism that allows programmers to insert custom code snippets into the execution flow of a program.
[0083] The above hook function can monitor events and trigger the synchronization of data such as log files and session information after the event occurs. When the replication is completed, the slave database (as the new master database) can notify the data driver that the parsing and preparation are complete.
[0084] The above-mentioned master-slave switching system realizes the client's imperceptible experience during the master-slave switching process, solves the problems of client connection interruption, request loss and user experience degradation in traditional master-slave switching, thereby improving the availability of the database system and the user experience.
[0085] Figure 1 The database driver shown in supports configuration of multiple data sources, i.e., clients, supports master database status detection, and supports judgment of whether a database master-slave switch has occurred and whether the switch is completed (i.e., the session request has been synchronized). It supports SQL caching and retry. When a master-slave switch occurs, it waits for the master-slave switch to be completed, and SQL is resent to the new master database to retry the service.
[0086] The database status detection in this embodiment may include: checking the most recent error log to see if any serious errors have occurred, or checking whether the main database service is running, or monitoring the usage of resources such as CPU, memory, disk, etc. to determine whether the threshold is reached, whether the database is in a locked state, etc., whether the read and write throughput is normal, etc.
[0087] The corresponding detection of the database status may include: checking whether the log is recorded normally, whether the database backup is completed successfully, whether it can be restored, etc.
[0088] In actual applications, database status detection can use the database's own command line tools (such as PostgreSQL's psql) to obtain status information. Or use professional database monitoring tools (such as Prometheus+Grafana, Zabbix, Nagios, etc.) to monitor various indicators in real time, or use custom scripts to automatically check.
[0089] PostgreSQL's psql provides a rich set of commands to query database status information. These commands can help administrators view database version, connection status, system table information, database and table sizes, etc. For example, you can use the \dt command to view all tables in the current database, use the \l command to list all databases, and use the pg_stat_activity system view to view active sessions in the current database.
[0090] Prometheus is an open source system monitoring and alarm toolkit. Grafana is a popular open source visualization platform. By integrating Prometheus with Grafana, the database indicators collected by Prometheus can be displayed in an intuitive chart form.
[0091] Zabbix is a powerful enterprise-level distributed monitoring system. For database monitoring, you first need to configure the monitoring items for the database on the Zabbix server. You can obtain database status information through Zabbix Agent (installed on the database server) or using the database's native protocol (for example, you can use the libpq library for PostgreSQL).
[0092] Nagios is a popular open source network and system monitoring tool. For database status monitoring, it is mainly implemented through plug-ins and check scripts. For example, there are Nagios plug-ins specifically for PostgreSQL database monitoring. These plug-ins can check various database states, such as whether the database is running, whether the database connection is normal, etc.
[0093] Figure 1 The master database described in the above writes the session information into the WAL log file for persistent storage when the data driver establishes a connection, and sends it to the slave database through master-slave synchronous replication. At the same time, when the session changes, such as interruption, reconnection, etc., it is written into the WAL log file and sent synchronously to the slave database.
[0094] Figure 1 The slave database described in the above can parse the session information after receiving the WAL log file sent by the master database, and load it into the memory, so as to quickly execute SQL requests when the master-slave switch occurs, without having to re-establish the client connection.
[0095] like Figures 2 to 4 As shown, Figures 2 to 4 Both show the step flow charts of a method for master-slave switching of a database with zero user perception provided in an embodiment of the present invention.
[0096] Part 1: Database connection process, such as Figure 3 As shown:
[0097] 101: The hook function of the main database is used to capture the events of connection establishment or disconnection when the main database connection changes.
[0098] 102: Capture the database connection establishment event.
[0099] 103: Analyze the specific client information of the connection establishment, including the client IP address, user name, connection time, session validity time, etc.
[0100] 104: Write the session information in JSON format to the specified WAL file. In order to distinguish it from the native WAL log, write it to a different file, such as WAL_SESSION.
[0101] 105: Send WAL log files to the slave database through streaming replication.
[0102] The slave database is responsible for receiving, parsing, and loading the session information synchronized with the master database. The specific process is as follows: Figure 4 shown.
[0103] 201: Receive the WAL log file sent by the master database from the database.
[0104] 202: Parse the WAL log file from the database, obtain the connection information list, and read the connection events therein, such as new session, session expiration, session disconnection, etc.
[0105] 203: Apply the WAL log file from the database and read the session information into the memory to achieve session continuity. The client SQL can be directly received without re-establishing the connection.
[0106] Part 2, master-slave switching process:
[0107] If the SQL initiated by the client encounters an exception, the data driver will perform fault diagnosis and switch, so that the client does not need to be aware of the underlying database switch, such as Figure 5 shown.
[0108] 301: After receiving the session request, the data driver sends the session request to the configured master database according to the configured multi-data source (ie, database information).
[0109] 302: The data driver determines the database status based on the database response. If it fails normally, it ends directly. If there is no timeout and no response or a "failure switch" error is returned, the automatic switch process begins.
[0110] 303: The data driver determines the state of the master database and whether a master-slave switch occurs through heartbeat detection and the master-slave state query interface.
[0111] 304: Enter the automatic switching process. The data driver caches the SQL requests received later into an ordered queue and prepares to retry to the new primary database.
[0112] 305: The data driver determines whether the new master database is ready to receive new connections through heartbeat detection and the master-slave status query interface. If not, it enters a loop waiting until the client fails after a timeout or the switch is completed, and SQL is re-initiated.
[0113] 306: The data driver re-sends the SQL requests cached during the failover to the new primary database.
[0114] When determining that the master database fails, the database driver program caches the session requests received from the client, and sends the cached session requests to the slave database that is the current master database.
[0115] That is, the database driver receives session requests from all clients and sends the session requests to the current primary database for processing.
[0116] The method of this embodiment implements parsing the connection information of the master database and persisting it in the WAL log, using the master-slave synchronization capability of the WAL log to send the connection information to the slave database, and re-parse and apply it to the slave database to implement the connection information synchronization capability.
[0117] The data driver program of this embodiment realizes the fault switching scenario, automatically identifies fault switching, automatically caches SQL requests, and automatically determines fault recovery and requests resending capabilities, thereby realizing automatic switching of driver layer database failures. There is no need for users to manually establish new database connections and resend SQL requests, ultimately achieving user-imperceptible fault switching.
[0118] In a specific implementation, a method for switching a database master-slave with zero user awareness in this embodiment includes:
[0119] When the database driver detects that the master database and the slave database have switched, it determines whether the master database and the slave database have completed the master-slave switch and achieved session synchronization;
[0120] If so, the database driver sends the session request cached during the master-slave switch to the slave database that is the current master database, so that the slave database processes the session request;
[0121] The database driver receives the response returned from the database according to the session request, and sends the response to the client to which the session request belongs.
[0122] It is understandable that the data driver program can detect that the master database has a fault or a master-slave switch has occurred in the following two ways:
[0123] The first is: the database driver obtains the information of the master database and the slave database, and determines whether the master database fails and the master-slave switch occurs through heartbeat detection and the status of the master database and the slave database;
[0124] The second is: the database driver receives session requests from all clients and sends the session requests to the current primary database for processing;
[0125] If the database driver does not receive a response to the session request returned by the current master database within a specified time, it is determined that the current master database fails and whether a master-slave switch occurs by detecting the master database state and the slave database state.
[0126] The database driver of this embodiment can cache the session information of the client in the memory during the master-slave switching.
[0127] In this embodiment, after the master-slave switch is completed and the session is synchronized, the database driver sends the cached session request from the memory to the slave database that is the current master database.
[0128] In a second specific implementation, a method for master-slave switching of a database with zero user perception in this embodiment includes:
[0129] When the master database receives a connection request from the data driver, it verifies the connection. If the verification passes, it connects to the data driver and synchronizes the connection information, context information, and log information to the slave database.
[0130] The master database receives a response from the slave database indicating that synchronization information processing is successful, and then sends the information of the master database and the information of the slave database to the data driver program so that the data driver program is stored in the memory;
[0131] The main database receives the session request of the client sent by the data driver, and after processing, the main database returns a response to the session request to the data driver, so that the data driver will respond to the client that sent the session request.
[0132] For example, when a failure occurs in the master database, the hook function in the master database captures the failure event, writes the session information in the master database into the specified WAL file, and distinguishes it from the WAL file that records the log. The WAL file that records the session information and the WAL file that records the log are sent to the slave database through streaming replication.
[0133] At this time, the slave database serving as the new master database receives the WAL file, parses it, obtains the connection list, reads the connection event, reads the session information into the memory, and waits for the session request sent by the data driver to achieve session continuity.
[0134] This embodiment utilizes the database WAL log synchronization capability, through customized development, to parse and persist the connection information of the master database into the WAL log, utilizes the WAL log master-slave synchronization capability, sends the connection information to the slave database, and reparses and applies it to the slave database, thereby realizing the connection information synchronization capability.
[0135] In addition, through customized development of database drivers, we can achieve automatic identification of fault switching, automatic caching of SQL requests, automatic fault recovery judgment, and request resending capabilities in fault switching scenarios, thereby realizing automatic switching of driver-layer database failures. This eliminates the need for users to manually establish new database connections and resend SQL requests, ultimately enabling user-imperceptible fault switching.
[0136] The method of this embodiment can achieve a client-impactless experience during the master-slave switching process, and provides an innovative solution for the database architecture in modern applications.
[0137] It should be noted that, for the sake of simplicity, the method embodiments are described as a series of action combinations, but those skilled in the art should be aware that the embodiments of the present invention are not limited by the order of the actions described, because according to the embodiments of the present invention, certain steps can be performed in other orders or simultaneously. Secondly, those skilled in the art should also be aware that the embodiments described in the specification are all preferred embodiments, and the actions involved are not necessarily required by the embodiments of the present invention.
[0138] Reference Figure 6 , shows a structural block diagram of a database master-slave switching device with zero user perception provided in an embodiment of the present invention, which may specifically include the following modules:
[0139] The detection unit is used to determine whether the master database and the slave database have completed the master-slave switch and achieved session synchronization when detecting that the master database and the slave database have switched. Session synchronization here means that the connection list and session information in the master database are synchronized to the slave database so that the slave database can parse and load, and achieve session continuity when receiving a session request sent by the database driver;
[0140] The switching unit is used to send the session request cached during the master-slave switching to the slave database as the current master database when the detection unit determines that the master-slave switching is completed, so that the slave database processes the session request;
[0141] The receiving unit is used to receive the response returned from the database according to the session request, and send the response to the client to which the session request belongs.
[0142] For example, the detection unit may be specifically configured to connect to the master database and the slave database according to the multi-data source configuration information, and determine whether the master database is switched through heartbeat detection and the master database status and the slave database status.
[0143] The switching unit is specifically used to obtain all session requests cached by the current database driver from the memory, and send the session requests to the slave database which is the current master database.
[0144] The device of this embodiment is a newly added device in the master-slave switching system. It caches the session request when the master-slave switching is detected, and then sends the cached session request to the new master database when it is determined that the master-slave switching is completed, that is, after the session information is synchronized, thereby ensuring the continuity of the session.
[0145] At the same time, it greatly improves the user experience in the master-slave database failover scenario, so that users do not need to pay attention to the details of the master-slave switch and can continuously access and operate the database, ultimately improving the performance and reliability of the application.
[0146] As for the device embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0147] In addition, an embodiment of the present invention further provides an electronic device, such as Figure 7 As shown, it includes a processor 1301, a communication interface 1302, a memory 1303 and a communication bus 1304, wherein the processor 1301, the communication interface 1302, and the memory 1303 communicate with each other through the communication bus 1304.
[0148] Memory 1303, used for storing computer programs;
[0149] The processor 1301 is used to implement the following steps when executing the program stored in the memory 1303:
[0150] When the database driver detects that the master database and the slave database have switched, it determines whether the master database and the slave database have completed the master-slave switch and achieved session synchronization;
[0151] If so, the database driver sends the session request cached during the master-slave switch to the slave database that is the current master database, so that the slave database processes the session request;
[0152] The database driver receives the response returned from the database according to the session request, and sends the response to the client to which the session request belongs.
[0153] Alternatively, implement the following steps:
[0154] When the master database receives a connection request from the data driver, it verifies the connection. If the verification passes, it connects to the data driver and synchronizes the connection information, context information, and log information to the slave database.
[0155] The master database receives a response from the slave database indicating that synchronization information processing is successful, and then sends the information of the master database and the information of the slave database to the data driver program so that the data driver program is stored in the memory;
[0156] The main database receives the client's session request sent by the data driver, and after processing, the main database returns a response to the session request to the data driver, so that the data driver will respond to the client that sent the session request.
[0157] The communication bus mentioned in the above terminal can be a Peripheral Component Interconnect (PCI) bus or an Extended Industry Standard Architecture (EISA) bus, etc. The communication bus can be divided into an address bus, a data bus, a control bus, etc. For ease of representation, only one thick line is used in the figure, but it does not mean that there is only one bus or one type of bus.
[0158] The communication interface is used for communication between the above terminal and other devices.
[0159] The memory may include a random access memory (RAM) or a non-volatile memory, such as at least one disk memory. Optionally, the memory may also be at least one storage device located away from the aforementioned processor.
[0160] The above-mentioned processor can be a general-purpose processor, including a central processing unit (CPU), a network processor (NP), etc.; it can also be a digital signal processor (DSP), an application specific integrated circuit (ASIC), a field programmable gate array (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components.
[0161] like Figure 8 As shown, in another embodiment provided by the present invention, a computer-readable storage medium 1401 is also provided, in which instructions are stored. When the computer-readable storage medium 1401 is run on a computer, the computer executes the user-zero-perception database master-slave switching method described in the above embodiment.
[0162] In another embodiment of the present invention, a computer program product including instructions is provided. When the computer is run on a computer, the computer executes the database master-slave switching method with zero user perception described in the above embodiment.
[0163] In the above embodiments, it can be implemented in whole or in part by software, hardware, firmware or any combination thereof. When implemented by software, it can be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When the computer program instructions are loaded and executed on a computer, the process or function described in the embodiment of the present invention is generated in whole or in part. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium. For example, the computer instructions can be transmitted from a website site, computer, server or data center to another website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integrated. The available medium can be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium (e.g., a DVD), or a semiconductor medium (e.g., a solid-state hard disk Solid State Disk (SSD)), etc.
[0164] 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 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.
[0165] Each embodiment in this specification is described in a related manner, and the same or similar parts between the embodiments can be referred to each other, and each embodiment focuses on the differences from other embodiments. In particular, for the system embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant parts can be referred to the partial description of the method embodiment.
[0166] The above description is only a preferred embodiment of the present invention and is not intended to limit the protection scope of the present invention. Any modification, equivalent replacement, improvement, etc. made within the spirit and principle of the present invention are included in the protection scope of the present invention.
Claims
1. A method for master-slave switching of a database with zero user awareness, characterized in that: include: When the database driver detects that the master database and the slave database have switched, it determines whether the master database and the slave database have completed the master-slave switch and achieved session synchronization; If so, the database driver sends the session request cached during the master-slave switch to the slave database that is the current master database, so that the slave database processes the session request; The database driver receives the response returned from the database according to the session request, and sends the response to the client to which the session request belongs.
2. The method according to claim 1, characterized in that: Before the database driver detects that the master database and the slave database are switched, the method further includes: The database driver is connected to the master database and the slave database according to the multi-data source configuration information, and determines whether the master database is switched through heartbeat detection and the master database status and the slave database status; If no switch occurs, the client's session request is sent to the primary database; If the database driver does not receive a response to the session request returned by the master database, the master database state and the slave database state are judged to determine whether a switch between the master database and the slave database occurs; If so, cache the client's session requests during the switch between the primary and secondary databases.
3. The method according to claim 1, characterized in that: Determine whether the master database and the slave database have completed the master-slave switch and achieved session synchronization, including: The database driver determines whether the master-slave switch is completed through heartbeat detection and the status of the master database and the status of the slave database. If not, it waits, and if the cached session request times out during the waiting period, it sends a failure response to the client of the timed-out session request; The connection list and session information in the master database are synchronized to the slave database so that the slave database can parse and load, and the session continuity is achieved when a session request sent by the database driver is received.
4. The method according to claim 1, characterized in that The database driver sends the session requests cached during the master-slave switch to the slave database that is the current master database, including: The database driver obtains all session requests cached by the current database driver from the memory, and sends the session requests to the slave database which is the current master database.
5. A method for switching between master and slave databases without user awareness, characterized in that: include: When the master database receives a connection request from the data driver, it verifies the connection. If the verification passes, it connects to the data driver and synchronizes the connection information, context information, and log information to the slave database. The master database receives a response from the slave database indicating that synchronization information processing is successful, and then sends the information of the master database and the information of the slave database to the data driver program so that the data driver program is stored in the memory; The main database receives the session request of the client sent by the data driver, and after processing, the main database returns a response to the session request to the data driver, so that the data driver will respond to the client that sent the session request.
6. The method according to claim 5, characterized in that Also includes: When the master-slave switch occurs, the hook function in the master database captures the failure event and writes the session information in the master database into the specified WAL file, distinguishing it from the WAL file that records the log. The WAL file that records the session information and the WAL file that records the log are sent to the slave database through streaming replication.
7. The method according to claim 5, characterized in that Also includes: The slave database that serves as the new primary database receives the WAL file that records the log, parses it, obtains the connection list, reads the connection events, reads the session information into the memory, and waits for the session request sent by the data driver to achieve session continuity.
8. A database master-slave switching system with zero user awareness, characterized in that: include: Master database, slave database, database driver and multiple clients; When the database driver detects that the master database and the slave database have switched, it determines whether the master database and the slave database have completed the master-slave switch and achieved session synchronization; If so, the database driver sends the session request cached during the master-slave switch to the slave database that is the current master database, so that the slave database processes the session request; The database driver receives the response returned from the database according to the session request, and sends the response to the client to which the session request belongs.
9. A database master-slave switching device with zero user perception, characterized in that: include: A detection unit, used to determine whether the master database and the slave database have completed the master-slave switch and achieved session synchronization when detecting that the master database and the slave database have switched; The switching unit is used to send the session request cached during the master-slave switching to the slave database as the current master database when the detection unit determines that the master-slave switching is completed, so that the slave database processes the session request; The receiving unit is used to receive the response returned from the database according to the session request, and send the response to the client to which the session request belongs.
10. An electronic device, characterized in that: It includes a processor, a communication interface, a memory and a communication bus, wherein the processor, the communication interface and the memory communicate with each other through the communication bus; The memory is used to store computer programs; The processor is used to implement the method according to any one of claims 1 to 7 when executing the program stored in the memory.
Citation Information
Patent Citations
High-elasticity high availability and load balancing realization method of PostgreSQL (Structured Query Language)
CN104503965A
Automatic transaction retry after session failure
CN104508663A
Master-slave switching method and device for database cluster nodes, equipment and medium
CN111200532A
Master-slave synchronization and switching method and system for two databases, computer and storage medium
CN114138902A
Database switching method, system and equipment based on server downtime
CN116594845A
Cited By
User-unaware database master-slave switching method and apparatus
WO2026108869A1