Data synchronization method, device, server and storage medium
By generating a log index identifier on the slave database side and sending the target operation log and subsequent operation logs to the master database, partial data synchronization of the slave database is achieved, which solves the problem of low full synchronization efficiency after the slave database fails and restarts, and improves data synchronization efficiency. .
Patent Information
- Application Number
- CN202111155826.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-09-29
- Publication Date
- 2024-08-27
- Estimated Expiration
- 2041-09-29
AI Technical Summary
After the secondary database fails and is restarted, the backup data in the secondary database needs to be fully synchronized, resulting in reduced data synchronization efficiency.
By generating a data synchronization request from the log index identifier on the slave database side, the target operation log and the operation log after the corresponding execution time are sent to the master database, thereby achieving partial data synchronization of the slave database.
The data synchronization efficiency is improved, so that the slave database only synchronizes part of the data when restarting after a failure, saving synchronization time.
Smart Images

Figure CN113806301B
Abstract
Description
Technical Field
[0001] The present application relates to the field of artificial intelligence technology, and in particular to a data synchronization method, device, server and storage medium. Background Art
[0002] With the advent of the big data era, the surge in data processing volume has driven the development of databases. Generally, a master database and a slave database are used to jointly ensure the high reliability of the database. In order to ensure data security and prevent data loss, the master database and the slave database of the database often use a synchronization mode for data backup. During the master-slave database synchronization process, the data changes in the master database need to be synchronized to the slave database.
[0003] In the related art, after a slave database fails and restarts, it is usually necessary to fully synchronize the backup data in the slave database, which will consume a lot of time to synchronize the data on the master database to the slave database, resulting in reduced data synchronization efficiency. Summary of the invention
[0004] In view of this, an embodiment of the present application provides a data synchronization method, device, server and storage medium to solve the problem in the related art that after a slave database fails and restarts, it is usually necessary to fully synchronize the backup data in the slave database, which will consume a lot of time to synchronize the data on the master database to the slave database, resulting in reduced data synchronization efficiency.
[0005] A first aspect of an embodiment of the present application provides a data synchronization method, including:
[0006] Receiving a data synchronization request including a log index identifier of a target operation log, which is sent by a communication-connected slave database, wherein the data synchronization request is generated by the slave database based on a log index identifier corresponding to the target operation log in a first log file, and the first log file is used to write the operation log executed by the slave database;
[0007] Searching for a target operation log corresponding to a log index identifier from a second log file of the primary database, where the second log file is used to write the operation log of the primary database;
[0008] The target operation log and the operation log whose corresponding execution time is after the target operation log in the second log file are sent to the slave database, so that the slave database performs partial data synchronization based on the received operation log.
[0009] Furthermore, the method further comprises:
[0010] In response to detecting the data operation instruction, executing the data operation indicated by the data operation instruction, and storing the target data corresponding to the data operation to the first storage device;
[0011] Generate a log index identifier for the operation log corresponding to the data operation, and write the log index identifier and the operation log into a second log file accordingly;
[0012] The corresponding operation log and log index identifier are sent to the slave database to trigger the slave database to back up the target data to the second storage device based on the received operation log and write the corresponding operation log and log index identifier into the first log file.
[0013] Further, storing target data corresponding to the data operation in the first storage device includes:
[0014] The target data corresponding to the data operation is stored in the first storage device in the form of a key-value pair, wherein the key of the key-value pair is the data identifier and data type of the target data, and the value of the key-value pair is the value of the target data.
[0015] Further, the corresponding operation log and log index identifier are sent to the slave database, including:
[0016] When the slave database is in a read-only state, the corresponding operation log and log index identifier are sent to the slave database.
[0017] Furthermore, the first storage device is a solid state drive, and the second storage device is a solid state drive, the second log file is cached in the solid state drive on the master database side, and the first log file is cached in the solid state drive on the slave database side.
[0018] Furthermore, when the primary database writes the operation log into the second log file, it also writes the operation log into the recovery log; and the method further includes:
[0019] In response to the operation log being synchronized to the slave database and being processed by the slave database, deleting the operation log from the recovery log;
[0020] In response to detecting that the primary database is started, a recovery log is loaded, and a data operation corresponding to an operation log in the recovery log is executed.
[0021] Furthermore, the slave database has multiple working threads, and if the slave database is in a non-read-only state, the slave database can be used to receive at least one data operation instruction sent by the user terminal, generate an instruction identifier for each data operation instruction, and distribute each data operation instruction to each working thread according to the instruction identifier for parallel execution.
[0022] A second aspect of an embodiment of the present application provides a data synchronization device, including:
[0023] a request receiving unit, configured to receive a data synchronization request including a log index identifier of a target operation log, sent by a communication-connected slave database, wherein the data synchronization request is generated by the slave database based on a log index identifier corresponding to the target operation log in a first log file, and the first log file is used to write the operation log executed by the slave database;
[0024] A log search unit, used to search for a target operation log corresponding to a log index identifier from a second log file of the main database, the second log file being used to write the operation log of the main database;
[0025] The log sending unit is used to send the target operation log and the operation log whose corresponding execution time is after the target operation log in the second log file to the slave database, so that the slave database performs partial data synchronization based on the received operation log.
[0026] Furthermore, the device also includes an instruction detection unit, a log storage unit, and a log synchronization unit;
[0027] An instruction detection unit, configured to, in response to detecting a data operation instruction, execute the data operation indicated by the data operation instruction, and store target data corresponding to the data operation to the first storage device;
[0028] The log storage unit is used to generate a log index identifier for the operation log corresponding to the data operation, and write the log index identifier and the operation log into a second log file accordingly;
[0029] A log synchronization unit is used to send the corresponding operation log and log index identifier to the slave database to trigger the slave database to back up the target data to the second storage device based on the received operation log and write the corresponding operation log and log index identifier into the first log file, and the first log file is used to write the operation log of the slave database.
[0030] Furthermore, the instruction detection unit is specifically used to store the target data corresponding to the data operation in the form of a key-value pair to the first storage device, wherein the key of the key-value pair is the data identifier and data type of the target data, and the value of the key-value pair is the value of the target data.
[0031] Furthermore, the log synchronization unit is specifically used to send the corresponding operation log and log index identifier to the slave database when the slave database is in a read-only state.
[0032] Furthermore, the first storage device is a solid state drive, and the second storage device is a solid state drive, the second log file is cached in the solid state drive on the master database side, and the first log file is cached in the solid state drive on the slave database side.
[0033] Further, if the primary database writes the operation log into the recovery log when writing the operation log into the second log file, the device further includes a log deletion unit and a data recovery unit;
[0034] a log deleting unit, configured to delete the operation log from the recovery log in response to the operation log being synchronized to the slave database and being processed by the slave database;
[0035] The data recovery unit is used to load the recovery log and execute the data operation corresponding to the operation log in the recovery log in response to detecting the startup of the primary database.
[0036] Furthermore, the slave database has multiple working threads, and if the slave database is in a non-read-only state, the slave database can be used to receive at least one data operation instruction sent by the user terminal, generate an instruction identifier for each data operation instruction, and distribute each data operation instruction to each working thread according to the instruction identifier for parallel execution.
[0037] A third aspect of an embodiment of the present application provides a server, including a memory, a processor, and a computer program stored in the memory and executable on the server, wherein the processor implements the steps of the data synchronization method provided in the first aspect when executing the computer program.
[0038] A fourth aspect of an embodiment of the present application provides a storage medium, which stores a computer program. When the computer program is executed by a processor, each step of the data synchronization method provided in the first aspect is implemented.
[0039] The implementation of a data synchronization method, device, server and storage medium provided by the embodiment of the present application has the following beneficial effects: when a slave database fails and restarts, the slave database can generate a data synchronization request based on the log index identifier in the first log file on the slave database side, and send the data synchronization request to the master database, so that the master database sends the operation log corresponding to the log index identifier in the second log file on the master database side and the operation log with the corresponding execution time after the operation log to the slave database, so that the slave database can perform partial data synchronization based on the received operation log. When a slave database fails and restarts, the slave database only needs to synchronize part of the data, which is equivalent to synchronizing all the data in the slave database, which helps to improve the efficiency of data synchronization. BRIEF DESCRIPTION OF THE DRAWINGS
[0040] In order to more clearly illustrate the technical solutions in the embodiments of the present application, the drawings required for use in the embodiments or related technical descriptions will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present application. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.
[0041] Figure 1 It is a flow chart of an implementation of a data synchronization method provided in an embodiment of the present application;
[0042] Figure 2 is a flowchart of another data synchronization method provided in an embodiment of the present application;
[0043] Figure 3 is a structural block diagram of a data synchronization device provided in an embodiment of the present application;
[0044] Figure 4 It is a structural block diagram of a server provided in an embodiment of the present application. DETAILED DESCRIPTION
[0045] In order to make the purpose, technical solution and advantages of the present application more clearly understood, the present application is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present application and are not used to limit the present application.
[0046] The embodiments of the present application can acquire and process relevant data based on artificial intelligence technology. Among them, artificial intelligence (AI) is the theory, method, technology and application system that uses digital computers or machines controlled by digital computers to simulate, extend and expand human intelligence, perceive the environment, acquire knowledge and use knowledge to obtain the best results.
[0047] AI basic technologies generally include sensors, dedicated AI chips, cloud computing, distributed storage, big data processing technology, operation / interaction systems, mechatronics, etc. AI software technologies mainly include computer vision technology, robotics technology, biometrics technology, speech processing technology, natural language processing technology, and machine learning / deep learning.
[0048] In the embodiment of the present application, artificial intelligence technology is used to achieve data synchronization between the master and slave databases.
[0049] The data synchronization method involved in the embodiment of the present application can be executed by a server. When the data synchronization method is executed by a server, the execution subject is the server.
[0050] It should be noted that the above-mentioned servers may include but are not limited to servers, mobile phones, tablets or wearable smart devices, etc. In addition, the above-mentioned servers may be independent servers, or cloud servers that provide basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communications, middleware services, domain name services, security services, content delivery networks (CDN), and big data and artificial intelligence platforms.
[0051] See also Figure 1 , Figure 1 A flowchart of a data synchronization method provided in an embodiment of the present application is shown, including:
[0052] Step 101: Receive a data synchronization request including a log index identifier of a target operation log, which is sent from a database via a communication connection.
[0053] The data synchronization request is generated by the slave database based on the log index identifier corresponding to the target operation log in the first log file, and the first log file is used to write the operation log executed by the slave database. In practice, the slave database can directly use the log index identifier to generate the data synchronization request, or it can use the combination of the log index identifier and other information to generate the data synchronization request.
[0054] The data synchronization request is usually information for requesting data synchronization, and the data synchronization request may include a log index identifier of a target operation log. This embodiment does not limit the specific form of the data synchronization request.
[0055] In addition, in practice, each operation log in the first log file on the database side is usually written in the order of execution time from the earliest to the latest.
[0056] The target operation log may be any operation log in the first log file. In practice, the target operation log is usually the last operation log written in the first log file.
[0057] In this embodiment, the execution subject of the above data synchronization method is usually a master database. The master database is usually a server for storing data. Here, the slave database can generate a data synchronization request including the log index identifier based on the log index identifier of the target operation log in the first log file on the slave database side, and then send the data synchronization request to the master database through the network. In this way, the master database can receive the data synchronization request sent by the slave database through the network.
[0058] Step 102: Search the second log file of the primary database for the target operation log corresponding to the log index identifier.
[0059] The second log file is used to write the operation log of the main database. The second log file is usually a log file used to record the operation log of the main database. In practice, the second log file usually records each operation log generated by the main database in the order of execution time.
[0060] Here, after receiving the data synchronization request sent by the slave database, the master database can use the log index identifier in the data synchronization request to find the operation log corresponding to the log index identifier from the second log file on the master database side. For ease of description, the found operation log can be recorded as the target operation log.
[0061] In practice, the second log file is usually cached in the solid state drive of the master database, and the first log file is cached in the solid state drive of the slave database. It should be noted that since the storage is performed in the form of a solid state drive, it is convenient to expand the storage space and the cost is low. The first log file and the second log file are both stored in the solid state drive, which can save costs.
[0062] Step 103: Send the target operation log and the operation logs whose corresponding execution time is after the target operation log in the second log file to the slave database, so that the slave database performs partial data synchronization based on the received operation logs.
[0063] Here, the master database can send the target operation log in the second log file and the operation log whose corresponding execution time is after the target operation log to the slave database. In this way, the slave database can receive at least one operation log and perform partial data synchronization based on the received operation log.
[0064] The method provided in this embodiment is that when a slave database fails and restarts, the slave database can generate a data synchronization request based on the log index identifier in the first log file on the slave database side, and send the data synchronization request to the master database, so that the master database sends the operation log corresponding to the log index identifier in the second log file on the master database side and the operation log with the corresponding execution time after the operation log to the slave database, so that the slave database can perform partial data synchronization based on the received operation log. When a slave database fails and restarts, the slave database only needs to synchronize part of the data, rather than synchronizing all the data in the slave database, which helps to improve data synchronization efficiency.
[0065] See also Figure 2 , Figure 2 The data synchronization method provided in this embodiment may include the following steps:
[0066] Step 201, in response to detecting a data operation instruction, executing a data operation indicated by the data operation instruction, and storing target data corresponding to the data operation in a first storage device.
[0067] The data operation instruction is usually an instruction for indicating data operation. The data operation indicated by the data operation instruction can be a data definition language (DDL) operation, a data manipulation language (DML) operation, etc. The above target data is usually the data generated by executing the data operation.
[0068] The first storage device is usually used to store data corresponding to the main database.
[0069] Here, the user can send a data operation instruction to the main database through the user terminal. In this way, the main database can detect the data operation instruction sent by the user terminal, and then execute the data operation indicated by the data operation instruction. In addition, the main database can store the data generated by executing the data operation in the first storage device.
[0070] Step 202: Generate a log index identifier for the operation log corresponding to the data operation, and write the log index identifier and the operation log into a second log file accordingly.
[0071] Here, the main database may generate a corresponding log index identifier for each operation log, and then write the log index identifier and the operation log into the second log file in correspondence.
[0072] Step 203: Send the corresponding operation log and log index identifier to the slave database to trigger the slave database to back up the target data to the second storage device based on the received operation log and write the corresponding operation log and log index identifier into the first log file.
[0073] The first log file is used to write the operation log of the slave database. The first log file is usually a log file used to record the operation log of the slave database. In practice, the first log file usually records the operation logs executed by the slave database in chronological order.
[0074] The second storage device is usually used to store data corresponding to the slave database.
[0075] Here, the master database can send the corresponding operation log and log index identifier to the slave database. In this way, the slave database can perform the data operation pointed to by the operation log, and store the target data corresponding to the data operation in the second storage device, and at the same time, the slave database can write the corresponding operation log and log index identifier into the first log file.
[0076] Step 204: Receive a data synchronization request including a log index identifier of a target operation log, which is sent from a database via a communication connection.
[0077] The data synchronization request is generated from the database based on a log index identifier in a first log file corresponding to a target operation log, and the first log file is used to write operation logs executed from the database.
[0078] Step 205: Search the second log file of the primary database for the target operation log corresponding to the log index identifier.
[0079] The second log file is used to write the operation log of the main database.
[0080] Step 206: Send the target operation log and the operation logs whose corresponding execution time is after the target operation log in the second log file to the slave database, so that the slave database performs partial data synchronization based on the received operation logs.
[0081] In this embodiment, the specific operations of steps 204-206 are the same as Figure 1 The operations of steps 101 - 103 in the illustrated embodiment are substantially the same and will not be described in detail herein.
[0082] In this embodiment, the operation log in the master database is written into the second log file corresponding to the log index identifier, and then the operation log and the log index identifier are written into the first log file of the slave database correspondingly. The slave database can generate a data synchronization request including the log index identifier based on the log index identifier in the first log file, and send the data synchronization request including the log index identifier to the master database to obtain the operation log corresponding to the log index identifier and subsequent operation logs, so as to synchronize part of the data in the slave database.
[0083] In some optional implementations, storing the target data corresponding to the data operation in the first storage device may include: storing the target data corresponding to the data operation in the first storage device in the form of a key-value pair.
[0084] The key of the key-value pair is the data identifier and data type of the target data, and the value of the key-value pair is the value of the target data.
[0085] Here, the purpose of storing the target data in the form of key-value pairs is to achieve compatibility with the Redis protocol and to improve the efficiency of querying the data in the database.
[0086] In some optional implementations, the first storage device is a solid state drive, and the second storage device is a solid state drive, the second log file is cached in the solid state drive on the master database side, and the first log file is cached in the solid state drive on the slave database side.
[0087] It should be pointed out that using a solid state drive for storage can facilitate the expansion of storage space, which can greatly reduce costs compared to using memory for storage in related technologies.
[0088] In some optional implementations of this embodiment, sending the corresponding operation log and log index identifier to the slave database may include:
[0089] When the slave database is in a read-only state, the corresponding operation log and log index identifier are sent to the slave database.
[0090] Here, when the slave database is in a read-only state, it indicates that the slave database is currently in a state suitable for data backup. At this time, the operation log and the log index identifier are sent to the slave database, so that the slave database can effectively process the operation log.
[0091] In an optional implementation of each embodiment of the present application, the slave database has multiple working threads, and if the slave database is in a non-read-only state, the slave database can be used to receive at least one data operation instruction sent by the user terminal, generate an instruction identifier for each data operation instruction, and distribute each data operation instruction to each working thread according to the instruction identifier for parallel execution.
[0092] Here, in some cases, such as when the master database fails, the slave database may be in a non-read-only state. At this time, the slave database may act as the master database, receive data operation instructions sent by the user end, and process the data operation instructions.
[0093] Here, the slave database can switch roles when needed and become the master database, which helps to respond to the user's data operation instructions in a timely manner and has high practicality. In addition, since the slave database has multiple working threads, it can execute multiple data operation instructions in parallel, which helps to improve data processing efficiency.
[0094] In an optional implementation of each embodiment of the present application, if the primary database writes the operation log to the recovery log when writing the operation log to the second log file, the above data synchronization method may also include at least one of the following steps 1 and 2.
[0095] The recovery log is usually a log file for recording operation logs that have not been backed up. Here, when the operation log is written into the second log file, the primary database can simultaneously write the operation log into the recovery log.
[0096] Step 1: In response to the operation log being synchronized to the slave database and being processed by the slave database, the operation log is deleted from the recovery log.
[0097] Here, when the operation log is processed by the slave database, the master database can delete the operation log from the recovery log. The recovery log always records the operation log that has not been processed by the slave database.
[0098] Step 2: In response to detecting that the primary database is started, loading the recovery log, and executing the data operation corresponding to the operation log in the recovery log.
[0099] Here, the primary database can automatically load the recovery log when it is started, and execute the data operations corresponding to the operation log in the recovery log one by one.
[0100] It should be pointed out that when the primary database is restarted, it only needs to load the recovery log and process a small amount of operation logs recorded in the recovery log. The time required is relatively short. When the primary database uses the entire second log file for data recovery, the data recovery efficiency of the primary database can be greatly improved.
[0101] In some optional implementations, the above data synchronization method may further include the following step three.
[0102] Step three: if the master database is a newly switched master database, the offset of the second log file corresponding to the master database is sent to the slave database, so that the slave database performs partial data synchronization based on the received offset.
[0103] Here, after the master database is changed, the second log file of the original master database is directly used to record the operation log of the new master database. When the master database is the newly switched master database, the newly switched master database can use the offset of the operation log in the original second log file to perform a partial data synchronization with the slave database.
[0104] Here, the slave database may perform partial data synchronization based on the received offset, which may further improve the data synchronization efficiency between the master database and the slave database.
[0105] It should be pointed out that if changes occur in both the slave database and the master database, full data synchronization is required between the master and slave databases.
[0106] See also Figure 3 , Figure 3 3 is a structural block diagram of a data synchronization device 300 provided in an embodiment of the present application. In this embodiment, the data synchronization device includes various units for executing Figure 1-Figure 2For details, please refer to the steps in the corresponding embodiment. Figure 1-Figure 2 as well as Figure 1-Figure 2 For the convenience of explanation, only the parts related to this embodiment are shown. Figure 3 , the data synchronization device 300 includes:
[0107] A request receiving unit 301 is used to receive a data synchronization request including a log index identifier of a target operation log, which is sent by a communication-connected slave database, wherein the data synchronization request is generated by the slave database based on a log index identifier corresponding to the target operation log in a first log file, and the first log file is used to write the operation log executed by the slave database;
[0108] A log search unit 302 is used to search for a target operation log corresponding to a log index identifier from a second log file of the primary database, where the second log file is used to write the operation log of the primary database;
[0109] The log sending unit 303 is used to send the target operation log and the operation log whose corresponding execution time is after the target operation log in the second log file to the slave database, so that the slave database performs partial data synchronization based on the received operation log.
[0110] Here, the slave database can generate a data synchronization request including the log index identifier based on the log index identifier of the target operation log in the first log file on the slave database side, and then send the data synchronization request to the master database through the network. In this way, the master database can receive the data synchronization request sent by the slave database through the network. Then, the master database can use the log index identifier in the data synchronization request to find the operation log corresponding to the log index identifier from the second log file. For ease of description, the found operation log can be recorded as the target operation log. The master database can send the target operation log in the second log file and the operation log whose corresponding execution time is after the target operation log to the slave database. In this way, the slave database can receive at least one operation log, and perform partial data synchronization based on the received operation log.
[0111] As an embodiment of the present application, the device further includes an instruction detection unit, a log storage unit, and a log synchronization unit (not shown in the figure).
[0112] An instruction detection unit, configured to, in response to detecting a data operation instruction, execute the data operation indicated by the data operation instruction, and store target data corresponding to the data operation to the first storage device;
[0113] The log storage unit is used to generate a log index identifier for the operation log corresponding to the data operation, and write the log index identifier and the operation log into a second log file accordingly;
[0114] The log synchronization unit is used to send the corresponding operation log and log index identifier to the slave database to trigger the slave database to back up the target data to the second storage device based on the received operation log and write the corresponding operation log and log index identifier into the first log file.
[0115] Here, the user can send a data operation instruction to the master database through the user terminal. In this way, the master database can detect the data operation instruction sent by the user terminal, and then execute the data operation indicated by the data operation instruction. In addition, the master database can store the data generated by executing the data operation to the first storage device. Afterwards, the master database can generate a corresponding log index identifier for each operation log. Then, the log index identifier and the operation log are written into the second log file accordingly. Finally, the master database can send the corresponding operation log and log index identifier to the slave database together. In this way, the slave database can execute the data operation pointed to by the operation log, and store the target data backup corresponding to the data operation to the second storage device. At the same time, the slave database can write the corresponding operation log and log index identifier into the first log file.
[0116] As an embodiment of the present application, the instruction detection unit is specifically used to store the target data corresponding to the data operation in the form of a key-value pair to a first storage device, wherein the key of the key-value pair is the data identifier and data type of the target data, and the value of the key-value pair is the value of the target data.
[0117] Here, the purpose of storing the target data in the form of key-value pairs is to achieve compatibility with the Redis protocol and to improve the efficiency of querying the data in the database.
[0118] As an embodiment of the present application, the log synchronization unit is specifically used to send the corresponding operation log and log index identifier to the slave database when the slave database is in a read-only state.
[0119] Here, when the slave database is in a read-only state, it indicates that the slave database is currently in a state suitable for data backup. At this time, the operation log and the log index identifier are sent to the slave database, so that the slave database can effectively process the operation log.
[0120] As an embodiment of the present application, the first storage device is a solid state drive, and the second storage device is a solid state drive. The second log file is cached in the solid state drive on the master database side, and the first log file is cached in the solid state drive on the slave database side.
[0121] Here, using a solid state drive for storage can facilitate the expansion of storage space, which can greatly reduce costs compared to using memory for storage in related technologies.
[0122] As an embodiment of the present application, if the main database writes the operation log into the recovery log when writing the operation log into the second log file, the device further includes a log deletion unit and a data recovery unit (not shown in the figure).
[0123] a log deleting unit, configured to delete the operation log from the recovery log in response to the operation log being synchronized to the slave database and being processed by the slave database;
[0124] The data recovery unit is used to load the recovery log and execute the data operation corresponding to the operation log in the recovery log in response to detecting the startup of the primary database.
[0125] Here, when the operation log is processed by the slave database, the master database can delete the operation log from the recovery log. The recovery log always records the operation logs that have not been processed by the slave database. Then, here, the master database can automatically load the recovery log at startup, and execute the data operations corresponding to the operation logs in the recovery log one by one. It should be pointed out that when the master database is restarted, it only needs to load the recovery log and process a small amount of operation logs recorded in the recovery log. The time consumed is short, and the data recovery efficiency of the master database can be greatly improved when the master database uses the entire second log file for data recovery.
[0126] As an embodiment of the present application, the slave database has multiple working threads, and if the slave database is in a non-read-only state, the slave database can be used to receive at least one data operation instruction sent by the user terminal, generate an instruction identifier for each data operation instruction, and distribute each data operation instruction to each working thread according to the instruction identifier for parallel execution.
[0127] Here, in some cases, such as when the master database fails, the slave database can be in a non-read-only state. At this time, the slave database can act as the master database, receive data operation instructions sent by the user end, and process the data operation instructions. The slave database can switch roles when necessary and convert to the master database, which helps to respond to the user's data operation instructions in a timely manner and has high practicality. In addition, since the slave database has multiple working threads, it can execute multiple data operation instructions in parallel, which helps to improve data processing efficiency.
[0128] As an embodiment of the present application, the device further includes an offset synchronization unit (not shown in the figure). The offset synchronization unit is used to: if the master database is a newly switched master database, send the offset of the second log file corresponding to the master database to the slave database, so that the slave database performs partial data synchronization based on the received offset.
[0129] Here, after the master database is changed, the second log file of the original master database is directly used to record the operation log of the new master database. When the master database is the newly switched master database, the newly switched master database can use the offset of the operation log in the original second log file to perform a partial data synchronization with the slave database. The slave database can perform partial data synchronization based on the received offset, which can further improve the data synchronization efficiency between the master database and the slave database. It should be pointed out that if both the slave database and the master database are changed, full data synchronization is required between the master database and the slave database.
[0130] The device provided in this embodiment, when the slave database fails and restarts, the slave database can generate a data synchronization request based on the log index identifier in the first log file on the slave database side, and send the data synchronization request to the master database, so that the master database sends the operation log corresponding to the log index identifier in the second log file on the master database side and the operation log with the corresponding execution time after the operation log to the slave database, so that the slave database can perform partial data synchronization based on the received operation log. When the slave database fails and restarts, the slave database only needs to synchronize part of the data, rather than synchronizing all the data in the slave database, which helps to improve data synchronization efficiency.
[0131] It should be understood that Figure 3 In the structural block diagram of the data synchronization device shown in FIG. 1 , each unit is used to execute Figure 1-Figure 2 The steps in the corresponding embodiments, and for Figure 1-Figure 2 Each step in the corresponding embodiment has been explained in detail in the above embodiment. Figure 1-Figure 2 as well as Figure 1-Figure 2 The relevant descriptions in the corresponding embodiments are not repeated here.
[0132] Figure 4 FIG. 1 is a structural block diagram of a server provided by another embodiment of the present application. Figure 4 As shown, the server 400 of this embodiment includes: a processor 401, a memory 402, and a computer program 403 stored in the memory 402 and executable on the processor 401, such as a program of a data synchronization method. When the processor 401 executes the computer program 403, the steps in each embodiment of the above-mentioned data synchronization method are implemented, such as Figure 1Steps 101 to 103 shown, or Figure 2 Alternatively, the processor 401 executes the computer program 403 to implement the above Figure 3 The functions of each unit in the corresponding embodiment are, for example, Figure 3 For details on the functions of the units 301 to 303 shown, please refer to Figure 3 The relevant descriptions in the corresponding embodiments are not repeated here.
[0133] Exemplarily, the computer program 403 may be divided into one or more units, one or more units are stored in the memory 402, and are executed by the processor 401 to complete the present application. One or more units may be a series of computer program instruction segments that can complete specific functions, and the instruction segments are used to describe the execution process of the computer program 403 in the server 400. For example, the computer program 403 may be divided into a request receiving unit, a log search unit, and a log sending unit, and the specific functions of each unit are as described above.
[0134] The server may include, but is not limited to, a processor 401 and a memory 402. Those skilled in the art will appreciate that Figure 4 It is only an example of server 400 and does not constitute a limitation of server 400. It may include more or fewer components than shown in the figure, or a combination of certain components, or different components. For example, the turntable device may also include input and output devices, network access devices, buses, etc.
[0135] The processor 401 may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor, etc.
[0136] The memory 402 may be an internal storage unit of the server 400, such as a hard disk or memory of the server 400. The memory 402 may also be an external storage device of the server 400, such as a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card, etc. equipped on the server 400. Further, the memory 402 may also include both an internal storage unit of the server 400 and an external storage device. The memory 402 is used to store computer programs and other programs and data required by the turntable device. The memory 402 may also be used to temporarily store data that has been output or is to be output.
[0137] In addition, each functional unit in each embodiment of the present application may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.
[0138] If the integrated module is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Among them, the computer-readable storage medium can be non-volatile or volatile. Based on this understanding, the present application implements all or part of the processes in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium, and the computer program can implement the steps of the above-mentioned various method embodiments when executed by the processor. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable storage medium may include: any entity or device capable of carrying computer program code, recording medium, U disk, mobile hard disk, disk, optical disk, computer memory, read-only memory (ROM, Read-Only Memory), random access memory (RAM, Random Access Memory), electric carrier signal, telecommunication signal and software distribution medium, etc. It should be noted that the content contained in computer-readable storage media can be appropriately increased or decreased according to the requirements of legislation and patent practices in the jurisdiction. For example, in some jurisdictions, according to legislation and patent practices, computer-readable storage media do not include electrical carrier signals and telecommunication signals.
[0139] The above embodiments are only used to illustrate the technical solutions of the present application, rather than to limit them. Although the present application has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or make equivalent replacements for some of the technical features therein. These modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application, and should all be included in the protection scope of the present application.
Claims
1. A data synchronization method, characterized in that: Applied to the main database, the method comprises: When a slave database fails and restarts, receiving a data synchronization request including a log index identifier of a target operation log sent by the slave database in communication connection, wherein the data synchronization request is generated by the slave database based on a log index identifier corresponding to the target operation log in a first log file, and the first log file is used to write the operation log executed by the slave database; searching a target operation log corresponding to the log index identifier from a second log file of the primary database, where the second log file is used to write the operation log of the primary database; Sending the target operation log and the operation logs with corresponding execution times after the target operation log in the second log file to the slave database, so that the slave database performs partial data synchronization based on the received operation logs; Among them, when the master database is switched to a new master database, the new master database continues to use the second log file of the master database to record the operation log of the new master database, and the new master database responds to the data synchronization request of the slave database with part of the operation log recorded in the second log file, and the responded part of the operation log is adapted to the data synchronization request.
2. The data synchronization method according to claim 1, characterized in that: The method further comprises: In response to detecting a data operation instruction, executing the data operation indicated by the data operation instruction, and storing target data corresponding to the data operation in a first storage device; Generate a log index identifier for the operation log corresponding to the data operation, and write the log index identifier and the operation log into the second log file accordingly; The corresponding operation log and the log index identifier are sent to the slave database to trigger the slave database to back up the target data to the second storage device based on the received operation log and write the corresponding operation log and the log index identifier into the first log file.
3. The data synchronization method according to claim 2, characterized in that: The storing the target data corresponding to the data operation into the first storage device includes: The target data corresponding to the data operation is stored in the first storage device in the form of a key-value pair, wherein the key of the key-value pair is the data identifier and data type of the target data, and the value of the key-value pair is the value of the target data.
4. The data synchronization method according to claim 2, characterized in that: The sending the corresponding operation log and the log index identifier to the slave database includes: When the slave database is in a read-only state, the corresponding operation log and the log index identifier are sent to the slave database.
5. The data synchronization method according to claim 2, characterized in that: The first storage device is a solid state drive, and the second storage device is a solid state drive. The second log file is cached in the solid state drive on the master database side, and the first log file is cached in the solid state drive on the slave database side.
6. The data synchronization method according to claim 1, characterized in that: When the primary database writes the operation log into the second log file, the primary database also writes the operation log into the recovery log; And the method further comprises: In response to the operation log being synchronized to the slave database and being processed by the slave database, deleting the operation log from the recovery log; In response to detecting that the primary database is started, the recovery log is loaded, and data operations corresponding to the operation log in the recovery log are executed.
7. The data synchronization method according to any one of claims 1 to 6, characterized in that: The slave database has multiple working threads, and if the slave database is in a non-read-only state, the slave database can be used to receive at least one data operation instruction sent by the user terminal, generate an instruction identifier for each data operation instruction, and distribute each data operation instruction to each working thread according to the instruction identifier for parallel execution.
8. A data synchronization device, characterized in that: Applied to the main database, including: a request receiving unit, configured to receive a data synchronization request including a log index identifier of a target operation log, sent by the slave database in communication connection when the slave database fails and restarts, wherein the data synchronization request is generated by the slave database based on the log index identifier corresponding to the target operation log in a first log file, and the first log file is used to write the operation log executed by the slave database; A log search unit, used to search for a target operation log corresponding to the log index identifier from a second log file of the main database, wherein the second log file is used to write the operation log of the main database; a log sending unit, configured to send the target operation log and the operation log corresponding to the execution time after the target operation log in the second log file to the slave database, so that the slave database performs partial data synchronization based on the received operation log; Among them, when the master database is switched to a new master database, the new master database continues to use the second log file of the master database to record the operation log of the new master database, and the new master database responds to the data synchronization request of the slave database with part of the operation log recorded in the second log file, and the responded part of the operation log is adapted to the data synchronization request.
9. A server comprising a memory, a processor, and a computer program stored in the memory and executable on the processor, characterized in that: When the processor executes the computer program, the steps of the method according to any one of claims 1 to 7 are implemented.
10. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the method according to any one of claims 1 to 7 are implemented.
