Database synchronization method and device
By obtaining application operation logs and database logs for merging and processing, the problem of data synchronization accuracy between different databases is solved, accurate synchronization of the source database and the target database is achieved, system coupling is reduced and data consistency is improved.
Patent Information
- Application Number
- CN202011363440.5
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-11-27
- Publication Date
- 2024-10-18
- Estimated Expiration
- 2040-11-27
AI Technical Summary
There are accuracy problems in data synchronization between different databases, especially because the source data end and the target data end have different database types or business systems, which makes synchronization difficult.
By obtaining the application operation log and the database log of the source database, and using the same data index and version number for merge processing, the synchronization operation of the target database is achieved, reducing system coupling and ensuring data consistency.
It achieves precise synchronization between the source database and the target database, improves the accuracy and timeliness of data synchronization, reduces system coupling, and facilitates expansion and maintenance.
Smart Images

Figure CN113761052B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of computer technology, and in particular to a database synchronization method and device. Background Art
[0002] Different databases have different functional characteristics, and different databases can be used to manage different aspects of data. Due to the needs of various applications, data synchronization between different databases is often required. However, due to the different database types of the source data end and the target data end or the large differences in business systems, the two sides are often unable to synchronize accurately. Summary of the invention
[0003] In view of this, an embodiment of the present invention provides a database synchronization method and device, which can achieve more accurate synchronization between a target database and a source database.
[0004] In a first aspect, an embodiment of the present invention provides a database synchronization method, including:
[0005] Obtaining an application operation log of the application, wherein the application operation log includes one or more first data records, wherein the first data record has a data index and a first version number;
[0006] Obtaining a database log of a source database of the application, wherein the database log includes one or more second data records, and the second data record has a data index and a second version number;
[0007] A synchronization operation is performed on a target database according to the first data record and the second data record, wherein the first data record and the second data record have the same data index.
[0008] Optionally,
[0009] The step of performing synchronization operation on the target database according to the first data record and the second data record includes:
[0010] Merging the first data record and the second data record having the same data index to obtain a merged data record, wherein the data index of the merged data record is the same as the data index of the first data record or the second data record, and determining a base version number of the merged data record according to a first version number of the first data record and a second version number of the second data record;
[0011] A synchronization operation is performed on the target database according to the merged data record.
[0012] Optionally,
[0013] The merging the first data record and the second data record having the same data index to obtain a merged data record includes:
[0014] For each of the second data records to be merged, determine a target second data record in the second data record whose second version number is greater than or equal to the base version number of the merged data record;
[0015] The merged data record is updated according to the target second data record, and the base version number of the data merge queue is updated to the second version number of the target second data record.
[0016] Optionally,
[0017] The merging the first data record and the second data record having the same data index to obtain a merged data record includes:
[0018] Determine the maximum second version number of each of the second data records to be merged;
[0019] When the maximum second version number is greater than or equal to the base version number of the merged data record, the merged data record is updated according to the second data record with the maximum second version number, and the base version number of the data merge queue is updated to the maximum second version number.
[0020] Optionally,
[0021] After updating the base version number of the data merging queue according to the second version number, the method further includes:
[0022] For each of the first data records to be merged, determine a target first data record in the first data record whose first version number is greater than a base version number of the merged data record;
[0023] According to the order of the first version numbers of the target first data records, the merged data records are updated with the target first data records, and the base version number of the data merge queue is updated to the first version number of the target first data records.
[0024] Optionally,
[0025] After updating the base version number of the data merging queue according to the second version number, the method further includes:
[0026] Determine the maximum first version number of each of the first data records to be merged;
[0027] When the maximum first version number is greater than the base version number of the merged data record, the merged data record is updated according to the first data record with the maximum first version number, and the base version number of the data merge queue is updated to the maximum first version number.
[0028] Optionally,
[0029] receiving a query request for the merged data record;
[0030] Determine whether there is a first data record to be merged, wherein the first version number of the first data record to be merged is greater than the base version number of the merged data record;
[0031] If it exists, the merged data record is updated according to the first data record, and the base version number of the data merge queue is updated to the largest first version number of the first data record to be merged and processed.
[0032] Optionally,
[0033] The merging the first data record and the second data record having the same data index to obtain a merged data record includes:
[0034] Obtaining a task of merging data records using one or more queue records, wherein the task is represented in the queue by the data index;
[0035] A task to be executed is determined from the queue to merge the first data record and the second data record having the same data index to obtain a merged data record.
[0036] In a second aspect, an embodiment of the present invention provides another database synchronization device, including:
[0037] After the operation on the source database is successful, an application operation log is generated, wherein the application operation log includes one or more first data records, and the first data record has a data index and a first version number.
[0038] In a third aspect, an embodiment of the present invention provides a database synchronization device, including:
[0039] A first acquisition unit, configured to acquire an application operation log of an application, wherein the application operation log includes one or more first data records, and the first data record has a data index and a first version number;
[0040] A second acquisition unit, configured to acquire a database log of a source database of the application, wherein the database log includes one or more second data records, and the second data record has a data index and a second version number;
[0041] A synchronization unit is used to perform a synchronization operation on a target database according to the first data record and the second data record, wherein the first data record and the second data record have the same data index.
[0042] In a fourth aspect, an embodiment of the present invention provides another database synchronization device, including:
[0043] The log generation unit is used to generate an application operation log after the operation on the source database is successful, wherein the application operation log includes one or more first data records, and the first data record has a data index and a first version number.
[0044] In a fifth aspect, an embodiment of the present invention provides an electronic device, including:
[0045] one or more processors;
[0046] a storage device for storing one or more programs,
[0047] When the one or more programs are executed by the one or more processors, the one or more processors implement the method described in any of the above embodiments.
[0048] In a sixth aspect, an embodiment of the present invention provides a computer-readable medium having a computer program stored thereon, wherein the program, when executed by a processor, implements the method described in any of the above embodiments.
[0049] An embodiment of the above invention has the following advantages or beneficial effects: data synchronization between the source database and the target database is achieved through the application operation log and the database log of the source database. The application operation log and the database log have the same data index, which is used to identify data records in the source database log and in the application operation log. The database log records the data change information of the source database, and its accuracy is good. Compared with the database log, the application operation log saves the operation record of the application on the source database, and its timeliness is higher. The method of the embodiment of the present invention uses two logs to quickly and accurately record the data changes in the source database. Therefore, accurate synchronization between the source database and the target database can be achieved.
[0050] In addition, a synchronization center can be set up between the source database and the target database. The synchronization center is used to obtain application operation logs and database logs of the source database, and synchronize the target database according to the application operation logs and database logs. Compared with the synchronization method in which the source database directly sends data change messages to the target database, each business system on the source database and the target database only needs to focus on its own business logic and does not need to pay attention to data synchronization issues, which reduces the system coupling between the source database and the target database, and facilitates the expansion and maintenance of the entire synchronization system.
[0051] In addition, each data record in the application operation log and database log carries a version number, and the data records in the log can be processed according to the version number to prevent data inconsistency problems caused by concurrency problems, further ensuring the accuracy of synchronization.
[0052] The further effects of the above-mentioned non-conventional optional manner will be described below in conjunction with the specific implementation manner. BRIEF DESCRIPTION OF THE DRAWINGS
[0053] The accompanying drawings are used to better understand the present invention and do not constitute an improper limitation of the present invention.
[0054] Figure 1 is an exemplary system architecture diagram to which embodiments of the present invention may be applied;
[0055] Figure 2 It is a schematic diagram of a process of a database synchronization method provided by an embodiment of the present invention;
[0056] Figure 3 is a schematic diagram of another scenario of database synchronization provided by an embodiment of the present invention;
[0057] Figure 4 is a schematic diagram of the process of another database synchronization method provided by an embodiment of the present invention;
[0058] Figure 5 It is a schematic diagram of a process of a data query method provided by an embodiment of the present invention;
[0059] Figure 6 is a schematic diagram of a process of another database synchronization method provided by an embodiment of the present invention;
[0060] Figure 7 is a schematic diagram of a process of yet another database synchronization method provided by an embodiment of the present invention;
[0061] Figure 8 is a structural diagram of a database synchronization device provided by an embodiment of the present invention;
[0062] Fig. 9 is a schematic diagram of the structure of another database synchronization device provided by an embodiment of the present invention;
[0063] Fig.10 It is a schematic diagram of the structure of a computer system of a terminal device or a server suitable for implementing an embodiment of the present invention. DETAILED DESCRIPTION
[0064] The following is a description of exemplary embodiments of the present invention in conjunction with the accompanying drawings, including various details of the embodiments of the present invention to facilitate understanding, which should be considered as merely exemplary. Therefore, it should be recognized by those of ordinary skill in the art that various changes and modifications may be made to the embodiments described herein without departing from the scope and spirit of the present invention. Similarly, for clarity and conciseness, the description of well-known functions and structures is omitted in the following description.
[0065] Figure 1 is an exemplary system architecture diagram to which the embodiments of the present invention can be applied. Figure 1 As shown, the method of the embodiment of the present invention mainly involves three ends: a source database end, a synchronization center, and a target database end. The source database end is deployed with a server on which the source database is installed (referred to as a source database server in this disclosure) and an application server that uses the source database. Among them, at least one source database is deployed on the source database server, and at least one application that needs to operate the source database is deployed on the application server. The same application can call at least one source database, and the same source database can also be called by multiple different applications. The source database server and the application server can be deployed on multiple servers or clusters respectively, or on the same server or cluster.
[0066] The synchronization center is used to obtain the application operation log of the application and the database log of the source database, and perform synchronization operations on the target database according to the application operation log and the database log. The data records in the application operation log and the database log have the same index record.
[0067] The target database side is deployed with at least one database that needs to be synchronized with the source database. The at least one target database of the target database side can be deployed on multiple servers or clusters, or on the same server or cluster. The method of the embodiment of the present invention does not limit the specific deployment form of the source database side and the target database side.
[0068] In one embodiment, in the initial stage, the target database end may send a subscription request to the synchronization center. As shown in Table 1, the subscription request information includes a source database identifier and a target database identifier. The subscription request information in Table 1 is used to inform the synchronization center that the database corresponding to the target database identifier needs to be synchronized with the database corresponding to the source database identifier.
[0069] Table 1 Subscription request information
[0070] Source database / source application identifier Target database / target application identifier Y0001 M0001 Y0002 M0002
[0071] Table 2 provides another format of subscription request information. As shown in Table 2, the subscription message may include information such as source database identifier and source table identifier. Among them, the table corresponding to the source table identifier is a data table in the source database, and the table corresponding to the target identifier is a data table in the target database. The subscription request information in Table 2 is used to inform the synchronization center that the target table corresponding to the target table identifier needs to be synchronized with the table corresponding to the source table identifier.
[0072] Table 2 Subscription request information
[0073]
[0074] like Figure 1 As shown, the application on the source database completes the business logic processing and persists the data in the database (for example, adding, modifying, and deleting). After the database operation is completed, an application operation log is generated and submitted to the synchronization center, which completes the application operation log processing. This stage ensures real-time data synchronization.
[0075] The database log of the source database will also be sent to the synchronization center, which will process the changes. This stage ensures that the data can reach final consistency. When the real-time synchronization request is abnormal, the data can also be guaranteed to be final consistent in this way, thereby supporting the downgrade of real-time synchronization.
[0076] Both application operation logs and database logs will be recorded as modification logs and merged regularly.
[0077] It should be understood that Figure 1 The number of databases and applications on the source database side and the target database side is only for illustration. Different numbers of databases and applications can be deployed according to implementation requirements.
[0078] The method of the embodiment of the present invention configures a synchronization center on both the source database end and the target database end, which can reduce the coupling degree of each business system in the source data end and the target data end. Each business system only needs to focus on its own business logic and does not need to pay attention to data synchronization issues. The embodiment of the present invention designs a set of general technical solutions, which can be applied to a variety of data synchronization scenarios and are easy to promote and use. In a distributed environment with multiple data sources and a large number of concurrent requests, data consistency can also be ensured.
[0079] Figure 2 FIG. 1 is a flow chart of a database synchronization method provided by an embodiment of the present invention. Figure 2As shown, the method includes:
[0080] Step 201: Acquire an application operation log of an application, where the application operation log includes one or more first data records, and the first data record has a data index and a first version number.
[0081] Step 202: Obtain a database log of a source database of the application, where the database log includes one or more second data records, and the second data record has a data index and a second version number.
[0082] The method of the embodiment of the present invention can be applied to the synchronization center. The data record of the synchronization center can include two parts: application operation log and database log. The application operation log is generated by the application using the source database. The application operation log records the data changes of the application and may include information such as the addition, deletion and modification of data. The application server can regularly push the application operation log to the synchronization center, and the synchronization center can also actively pull the application operation log from the application server.
[0083] The database log is automatically generated by the source database. The format and generation method of the database log are determined by the type of the source database. The source database types may include: Redis, Mysql, db2, etc. The source database can regularly push the database log to the synchronization center, and the synchronization center can also actively pull the database log from the source database.
[0084] It should be noted that each record in the application operation log and database log must contain a data index and version number. The data index is used to uniquely identify the data record in the source database or the target database. The version number is used to record the version information of the data record. The version number increases each time the data is changed. The data record corresponding to the largest version number is the latest content of the current data.
[0085] As shown in Table 3, the application operation log may include all fields in a data record, or may only include changed field information. For example, the latter record in Table 3 represents that field 3 of the data record with data index 65535 is changed from 30 to 32.
[0086] Table 3 Application operation log format
[0087]
[0088] Table 3 shows that the application identified as 005 has performed two operations on the data record with data index 65535, and the current version number is 13. Table 4 shows the database log content corresponding to Table 3. As shown in Table 4, the database log contains all the fields of a data, and each field has a field value under the corresponding version. From Table 4, it can be determined that the current latest version number is 13 and the content of all fields in the data record with index identification 65536 corresponding to the version number.
[0089] Table 4 Format of database log
[0090]
[0091] Application operation logs and database logs can be stored in the database or files deployed by the synchronization center. For each data index, the corresponding application operation log and database log are stored in the synchronization.
[0092] An ordered set can automatically sort multiple records in a set according to the sorting field. An ordered set may include: zset, TreeMap, etc. The database of the synchronization center can use an ordered set to store application operation logs and database logs. When there are multiple modification records for the same data, multiple modification logs will be recorded, and the version number in the log will be used as the sorting field. Using an ordered set can automatically sort the stored application operation logs and database logs by version number, reducing the time required for the subsequent log merging process.
[0093] Optionally, the application operation log and the database log are stored separately, and each is sorted using a different ordered set. The purpose of storing the application operation log and the database log separately is to reduce the merging operation in the query phase.
[0094] Step 203: Perform synchronization operation on the target database according to the first data record and the second data record, wherein the first data record and the second data record have the same data index.
[0095] The application operation log saves the data changes of the application. The same data record in the application operation log and the database log has the same data index. The application operation log and the database log record the data changes completely and accurately. The target database can be synchronized with the source database more accurately based on the merging of the application operation log and the database log, thereby solving the problem in the prior art that the target database cannot be accurately synchronized with the source database.
[0096] In addition, the method of the embodiment of the present invention ensures that the write operation is not overwritten based on the incremental log method, and ensures that data is written according to the version based on the data version, thereby preventing data inconsistency caused by concurrency problems.
[0097] In one embodiment of the present invention, step 203 may include:
[0098] Merging a first data record and a second data record having the same data index to obtain a merged data record, wherein the data index of the merged data record is the same as the data index of the first data record or the second data record, and determining a base version number of the merged data record according to a first version number of the first data record and a second version number of the second data record;
[0099] Perform synchronization operations on the target database based on the merged data records.
[0100] There will be more and more real-time operation logs and database change logs, so they need to be merged. There are many ways to merge logs.
[0101] Method 1: First, merge the first data record in the application operation log according to the order of the version number to generate the first merged data record, and use the maximum version number in the first data record as the version number of the first merged data record. Find the second data record with a version number greater than the first merged log from the second data record, merge the first merged log again to generate a merged data record, and use the maximum version number in the second data record as the base version number of the merged data record.
[0102] Method 2: First merge the second data record in the database log to generate the second merged data record, and use the maximum version number in the second data record as the version number of the second merged data record. Find the first data record with a version number greater than the second merged log from the first data record, merge the second merged log again to generate a merged data record, and use the maximum version number in the first data record as the base version number of the merged data record.
[0103] Method three: randomly select the records to be merged from the first data record and the second data record with the same version number, merge the records to be merged corresponding to all version numbers to generate a merged data record, and use the largest version number in the records to be merged as the base version number of the merged data record.
[0104] The base version number records the data after the previous merge. Subsequent modification operations occur after this version. Based on the data version, data is written according to the version to prevent data inconsistency caused by concurrency problems.
[0105] The synchronization center needs to record the baseline version number of each data record in the source database and the synchronization information of each target database. Table 5 and Table 6 show the recording method of the baseline version number of each data record and the synchronization information of each target database in the data center.
[0106] As shown in Table 5, the base version number of the data record with index ID 65535 is 13, and the target database 1 and the target database 4 have not yet synchronized the merged data record of this version. Therefore, it is necessary to synchronize the target database 1 and the target database 4 according to the merged data record with the base version number 13. Once the synchronization operation is completed for the target database 1 and the target database 4, the status of the target database 1 and the target database 4 is changed to synchronized.
[0107] Table 5 Data index version record
[0108]
[0109] If the synchronization center performs another merge process based on the index identifier 65535 and the baseline version number 13, and the baseline version number of the generated merged data record becomes 30, the baseline version number of the data record with the index identifier 65535 is changed to 30, and the status information of all target databases becomes unsynchronized.
[0110] As shown in Table 6, the synchronization center not only records the base index number of the merged data record, but also records the current version number of each target database. For example, if the base version number of the data record with index identifier 65536 is 25, and the current version numbers of target database 1 and target database 2 are less than 25, then synchronization operations need to be performed on target database 1 and target database 2. Once the synchronization operation is completed on target database 1 and target database 2, the version numbers of target database 1 and target database 4 are changed to 25.
[0111] Table 6 Data index version record
[0112]
[0113] If the synchronization center performs another merge process based on the index identifier 65535 and the baseline version number 13, and the baseline version number of the generated merged data record becomes 30, then the baseline version number of the data record with the index identifier 65535 is changed to 30, and the version information of all target databases does not need to be changed.
[0114] Compared with the method shown in Table 5, the versions of each target database recorded in Table 6 can help the system and users better control the data version and better solve the dirty data problem caused by multi-thread concurrency. For example, in the query stage, the log records that have not been merged can be queried in the synchronization center according to the version of the target database, and the query results can be returned after merging to avoid directly returning expired data.
[0115] It should be noted that in the initial state or default state, the baseline version number set by the synchronization center should be small enough or a negative value, so that the baseline version number is smaller than the version numbers of all records in the first data record and the second data record. Since it is necessary to select data records with version numbers greater than the baseline version number from the application operation log and the database log to complete the merge process, the baseline version number should be set small enough to avoid discarding the data records to be merged, thereby ensuring that the merge process can be completed smoothly.
[0116] In one embodiment of the present invention, step 203 may include:
[0117] The first data record and the second data record having the same data index are merged to obtain a merged data record, including:
[0118] Obtaining a task for merging data records using one or more queue records, wherein the task is represented in the queue by a data index;
[0119] A task to be executed is determined from the queue to merge a first data record and a second data record having the same data index to obtain a merged data record.
[0120] The data indexes corresponding to the task of merging data records are divided into several different queues. According to the predetermined rules, the target queue can be selected to execute the task of merging data records corresponding to the data index in the target queue. A corresponding thread can also be set for each queue, and the task of merging data records corresponding to the data index in each queue can be executed in parallel through multiple threads to increase the merging speed.
[0121] Figure 3 It is a schematic diagram of another scenario of database synchronization provided by an embodiment of the present invention. The data area records the data after the previous merge, and subsequent modification operations all occur after this version. The log area saves the application operation log (oplog) and the database log (binlog). The merge task queue of the merge task area records the current data index (bizId) to be merged, and the task can be split into multiple lists (merge_task_1, merge_task_2) according to certain rules (such as modulo), and processed in parallel by multiple merge threads. Among them, arrow 1 represents the merging of the application operation log into the data area, and arrow 2 represents the merging of the database log into the data area.
[0122] Since the writing of the baseline version of a single business data is completed by the merge thread, there is no concurrency problem and no locking is required, which ensures the real-time and efficiency of data synchronization.
[0123] It should be noted that the same data index can only appear in one task queue and only needs to appear once, and the ordered set deduplication process can be used. The purpose of log merging is to merge the application operation log and database log into the baseline version. After the merge is completed, the merged application operation log and database log can be cleared.
[0124] Figure 4 FIG. 1 is a flowchart of a database synchronization method provided by another embodiment of the present invention. Figure 4 As shown, the method includes:
[0125] Step 401: Obtain an application operation log of an application, where the application operation log includes one or more first data records, and the first data record has a data index and a first version number.
[0126] Step 402: Obtain a database log of a source database of the application, where the database log includes one or more second data records, and the second data record has a data index and a second version number.
[0127] Step 403: merge the second data records with the same data index to obtain a merged data record, and determine a base version number of the merged data record according to the second version number of the second data record.
[0128] First, the second data records in the database log are merged. The version number of the second data record must be greater than or equal to the base version number. The maximum version number corresponding to the merged second data record is determined, and the maximum version number is determined as the base version number of the merged record.
[0129] It should be noted that after the second data records are merged, the database log containing the merged second data records should be deleted. On the one hand, the system storage resources can be saved, and on the other hand, deleting the merged second data records can reduce the impact on subsequent merging or query processing.
[0130] Step 404: merge the first data record and the merged data record having the same data index to update the merged data record, and determine a base version number of the merged data record according to the first version number of the first data record.
[0131] Based on the merged data record generated in step 403, further merge is performed by applying the first data record in the operation log. The version number of the first data record must be greater than the base version number. The maximum version number corresponding to the merged first data record is determined, and the maximum version number is determined as the base version number of the merged record.
[0132] It should be noted that after the first data record is merged, the database log containing the merged first data record should be deleted. On the one hand, the system storage resources can be saved, and on the other hand, deleting the merged first data record can reduce the impact on subsequent merging or query processing.
[0133] Step 405: Perform synchronization operation on the target database according to the merged data record.
[0134] The log merging operation can be performed regularly by scheduling threads. The scheduling period can be configured as needed, such as 1 second, 2 seconds, 5 seconds, etc.
[0135] The scheduling thread first obtains the data index to be merged from the task queue, and queries the baseline version number, application operation log, and database log based on the data index. Generally, messages of the same version of the database log will arrive later than the application operation log, and the database log messages carry complete and accurate data. Therefore, when merging, consider merging the database log messages into the baseline version first, and merge when the database log message version is greater than or equal to the baseline version. If the database log version is less than the baseline version, it means that the database log has expired and needs to be discarded.
[0136] After the database log message processing is completed, the application operation log is merged. The application log version number is greater than the baseline version. If the application operation log version number is equal to the baseline version number, it means that the database log has been merged and the baseline version contains the latest data, so there is no need to merge the application operation log.
[0137] Compared with application operation logs, the data records in database logs carry more accurate data information. In the log merging process of the embodiment of the present invention, database logs of the same version are merged before application operation logs, which can ensure that the data in the target database is synchronized with the data in the source database with higher accuracy.
[0138] Step 403 can be implemented in the following two ways:
[0139] Method 1: for each second data record to be merged, determine a target second data record whose second version number is greater than or equal to a base version number of the merged data record;
[0140] The merged data record is updated according to the target second data record, and the base version number of the data merge queue is updated to the second version number of the target second data record.
[0141] If the second version number of the second data record is greater than or equal to the base version number, the second data record is merged into the merged data record, and the second version number is determined as the base version number of the merged data record. If the second version number of the second data record is less than the base version number, the second data record has expired and should be discarded.
[0142] In method 1, the second data records are merged one by one according to the version number to generate a merged data record. Method 1 is more suitable for scenarios with high timeliness. Once the data in the source database changes, the synchronization center can quickly obtain the application operation log and database log. The synchronization center can also quickly merge the obtained logs into the merged data record and perform synchronization operations on the target database based on the merged data record.
[0143] Method 2: determining the maximum second version number of each second data record to be merged;
[0144] When the maximum second version number is greater than or equal to the base version number of the merged data record, the merged data record is updated according to the second data record with the maximum second version number, and the base version number of the data merge queue is updated to the maximum second version number.
[0145] Since the second data record in the database log stores complete data information, that is, data information of each field, it is possible to directly select the second data record with the largest version number from the second data records whose version numbers are greater than or equal to the base version number, update the merged data record through the second data record, and determine the largest version number as the base version number.
[0146] In an embodiment of the present invention, a second data record with the largest version is selected from multiple second data records for updating the merged data record. Compared with the first method, the second method does not need to merge one by one, which can improve the efficiency of merging and reduce system resource consumption.
[0147] Step 404 can be implemented in the following two ways:
[0148] Method 1: for each first data record to be merged, determine a target first data record whose first version number is greater than a base version number of the merged data record;
[0149] According to the sequence of the first version numbers of the target first data records, the merged data records are updated with the target first data records, and the base version number of the data merge queue is updated to the first version number of the target first data records.
[0150] If the first version number of the first data record is greater than the base version number, the first data record is merged into the merged data record, and the first version number is determined as the base version number of the merged data record. If the first version number of the first data record is less than or equal to the base version number, the merged data record has been merged with the second data record of the same version and should be discarded.
[0151] In method 1, the first data records are merged one by one according to the version number to generate a merged data record. Method 1 is more suitable for scenarios with high timeliness. Once the data in the source database changes, the synchronization center can quickly obtain the application operation log and database log. The synchronization center can also quickly merge the obtained logs into the merged data record and perform synchronization operations on the target database based on the merged data record.
[0152] Method 2: determining the maximum first version number of each first data record to be merged;
[0153] When the maximum first version number is greater than the base version number of the merged data record, the merged data record is updated according to the first data record with the maximum first version number, and the base version number of the data merge queue is updated to the maximum first version number.
[0154] The first data record in the application operation log may only store the changed field information or the complete data information. When the first data stores the complete data information, the first data record with the largest version number may be directly selected from the first data records whose version numbers are greater than the base version number, and the merged data record may be updated through the first data record, and the largest version number may be determined as the base version number.
[0155] In an embodiment of the present invention, a first data record with the largest version is selected from multiple first data records for updating the merged data record. Compared with the first method, the second method does not need to merge one by one, which can improve the efficiency of merging and reduce system resource consumption.
[0156] Figure 5 FIG. 1 is a schematic diagram of a process of a data query method provided by an embodiment of the present invention. Figure 5 As shown, the method includes:
[0157] Step 501: Receive a query request for merged data records.
[0158] Step 502: Determine whether there is a first data record to be merged, wherein the first version number of the first data record to be merged is greater than the base version number of the merged data record.
[0159] Step 503: If it exists, update the merged data record according to the first data record, and update the base version number of the data merge queue to the largest first version number of the first data record to be merged.
[0160] Since logs are merged periodically, when querying data, given that application operation logs are more timely and may not have been merged into the merged data records, it is necessary to query both the merged data records and the application operation logs at the same time, and merge the application operation logs into the merged data records before returning them.
[0161] Since only the application operation logs need to be merged, the application operation logs and database logs are stored separately when storing the logs, which helps to quickly merge and respond in the query stage.
[0162] To ensure the accuracy of the query, it may be necessary to merge the queries. Whether the merge will affect the response efficiency of the system can be analyzed from the following two aspects:
[0163] The probability of merging can be calculated based on the merging cycle. For example, if the merging cycle is 1 second, merging is required only if the query is performed within 1 second after the data changes. Considering that most business data has a long life cycle and the number of changes made during the life cycle is limited, the probability of merging during query is very low. For example, if the data changes 100 times a day and the query probability is evenly distributed:
[0164]
[0165] The probability of performing a merge is: 0.1157%.
[0166] Calculated by read-write ratio: If the system read-write ratio is 500:1, the average distribution of query probability is also considered:
[0167]
[0168] The probability of performing a merge is: 0.2%.
[0169] Impact of the merge operation: The merge operation is very simple, which is to re-assign values in order and overwrite with new values. When merging, you can overwrite according to the queried fields. The merge process in the query phase is different from the merge process in the synchronization phase mentioned above. The merge in the synchronization phase needs to process each modified field. Therefore, the merge process in the query phase can greatly reduce the merge operations compared to the merge process in the synchronization phase.
[0170] From the above analysis results, we can see that the probability of merging during the query phase is very low, about 0.1% to 0.2%, and the merging process is very simple with little impact, which can be suitable for applications in many different business scenarios.
[0171] Figure 6 FIG. 1 is a schematic diagram of a process of another database synchronization method provided by an embodiment of the present invention. Figure 6 As shown, the method includes:
[0172] Step 601: after the operation on the source database is successful, an application operation log is generated, the application operation log includes one or more first data records, and the first data record has a data index and a first version number.
[0173] The method of the embodiment of the present invention can be applied to the source database end. The application generates an application operation log after the source database operation is successful. The application operation log can reflect the content information of the changed fields of the data record in the source database, and can also reflect the content information of all fields of the data record in the source database. The first data record in the application operation log has a data index and a first version number. The data index can be used to identify the data record in the source data. The first version number is used to record the changed version of the data record, and the first version number gradually increases as the application operates on the data record.
[0174] The source database can regularly push application operation logs to the synchronization center, and the synchronization center can also actively pull application operation logs from the source database.
[0175] Figure 7 FIG. 1 is a schematic diagram of a process of another database synchronization method provided by an embodiment of the present invention. Figure 7 As shown, the method includes:
[0176] Step 701: Synchronize the target database according to the merged data record, wherein the merged data record is generated by merging the application operation log of the application and the database log of the source database, and the data records in the application operation log and the database log have the same data index.
[0177] The method of the embodiment of the present invention can be applied to the target database end. The merged data record is generated by merging the application operation log of the application and the database log of the source database. The application operation log saves the operation record of the application on the source database. The application operation log and the database log have the same data index, which is used to identify the data record in the source database. Through the application operation log and the database log, the data changes in the source database can be completely and accurately recorded, so that the source database and the target database can be synchronized more accurately.
[0178] The synchronization center can regularly push application merged data records to the target database, and the target database can also regularly and actively pull merged data records from the synchronization center.
[0179] like Figure 8 As shown, an embodiment of the present invention provides a database synchronization device, including:
[0180] A first acquisition unit 801 is used to acquire an application operation log of an application, where the application operation log includes one or more first data records, where the first data record has a data index and a first version number;
[0181] A second acquisition unit 802 is configured to acquire a database log of a source database of the application, wherein the database log includes one or more second data records, and the second data record has a data index and a second version number;
[0182] The synchronization unit 803 is configured to perform a synchronization operation on a target database according to the first data record and the second data record, wherein the first data record and the second data record have the same data index.
[0183] In one embodiment of the present invention, the synchronization unit 803 is specifically configured to: merge the first data record and the second data record having the same data index to obtain a merged data record, wherein the data index of the merged data record is the same as the data index of the first data record or the second data record, and determine a base version number of the merged data record according to a first version number of the first data record and a second version number of the second data record;
[0184] A synchronization operation is performed on the target database according to the merged data record.
[0185] In one embodiment of the present invention, the synchronization unit 803 is specifically configured to: for each of the second data records to be merged, determine a target second data record in the second data record whose second version number is greater than or equal to the base version number of the merged data record;
[0186] The merged data record is updated according to the target second data record, and the base version number of the data merge queue is updated to the second version number of the target second data record.
[0187] In one embodiment of the present invention, the synchronization unit 803 is specifically configured to: determine the maximum second version number of each of the second data records to be merged;
[0188] When the maximum second version number is greater than or equal to the base version number of the merged data record, the merged data record is updated according to the second data record with the maximum second version number, and the base version number of the data merge queue is updated to the maximum second version number.
[0189] In one embodiment of the present invention, the synchronization unit 803 is specifically configured to: for each of the first data records to be merged, determine a target first data record in the first data record whose first version number is greater than a base version number of the merged data record;
[0190] According to the order of the first version numbers of the target first data records, the merged data records are updated with the target first data records, and the base version number of the data merge queue is updated to the first version number of the target first data records.
[0191] In one embodiment of the present invention, the synchronization unit 803 is specifically configured to: determine the maximum first version number of each of the first data records to be merged;
[0192] When the maximum first version number is greater than the base version number of the merged data record, the merged data record is updated according to the first data record with the maximum first version number, and the base version number of the data merge queue is updated to the maximum first version number.
[0193] In one embodiment of the present invention, the device further comprises:
[0194] The query unit 804 is used to: receive a query request for the merged data record;
[0195] Determine whether there is a first data record to be merged, wherein the first version number of the first data record to be merged is greater than the base version number of the merged data record;
[0196] If it exists, the merged data record is updated according to the first data record, and the base version number of the data merge queue is updated to the largest first version number of the first data record to be merged and processed.
[0197] The synchronization unit 803 is specifically used to: obtain a task of merging data records using one or more queue records, wherein the task is represented in the queue by the data index;
[0198] A task to be executed is determined from the queue to merge the first data record and the second data record having the same data index to obtain a merged data record.
[0199] like Fig. 9 As shown, an embodiment of the present invention provides a database synchronization device, including:
[0200] The log generating unit 901 is used to generate an application operation log after the operation on the source database is successful. The application operation log includes one or more first data records. The first data record has a data index and a first version number.
[0201] An embodiment of the present invention provides an electronic device, including:
[0202] one or more processors;
[0203] a storage device for storing one or more programs,
[0204] When one or more programs are executed by one or more processors, the one or more processors implement the method of any of the above embodiments.
[0205] Reference below Fig.10 , which shows a schematic diagram of the structure of a computer system 100 of a terminal device suitable for implementing an embodiment of the present invention. Fig.10 The terminal device shown is only an example and should not bring any limitation to the functions and scope of use of the embodiments of the present invention.
[0206] like Fig.10 As shown, the computer system 100 includes a central processing unit (CPU) 101, which can perform various appropriate actions and processes according to a program stored in a read-only memory (ROM) 102 or a program loaded from a storage part 108 into a random access memory (RAM) 103. In the RAM 103, various programs and data required for the operation of the system 100 are also stored. The CPU 101, the ROM 102, and the RAM 103 are connected to each other through a bus 104. An input / output (I / O) interface 105 is also connected to the bus 104.
[0207] The following components are connected to the I / O interface 105: an input section 106 including a keyboard, a mouse, etc.; an output section 107 including a cathode ray tube (CRT), a liquid crystal display (LCD), etc., and a speaker, etc.; a storage section 108 including a hard disk, etc.; and a communication section 109 including a network interface card such as a LAN card, a modem, etc. The communication section 109 performs communication processing via a network such as the Internet. A drive 110 is also connected to the I / O interface 105 as needed. A removable medium 111, such as a magnetic disk, an optical disk, a magneto-optical disk, a semiconductor memory, etc., is installed on the drive 110 as needed, so that a computer program read therefrom is installed into the storage section 108 as needed.
[0208] In particular, according to the embodiments disclosed in the present invention, the process described above with reference to the flowchart can be implemented as a computer software program. For example, the embodiments disclosed in the present invention include a computer program product, which includes a computer program carried on a computer-readable medium, and the computer program contains program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from the network through the communication part 109, and / or installed from the removable medium 111. When the computer program is executed by the central processing unit (CPU) 101, the above-mentioned functions defined in the system of the present invention are executed.
[0209] It should be noted that the computer-readable medium shown in the present invention may be a computer-readable signal medium or a computer-readable storage medium or any combination of the above two. The computer-readable storage medium may be, for example, but not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or device, or any combination of the above. More specific examples of computer-readable storage media may include, but are not limited to: an electrical connection with one or more wires, a portable computer disk, a hard disk, a random access memory (RAM), a read-only memory (ROM), an erasable programmable read-only memory (EPROM or flash memory), an optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination of the above. In the present invention, a computer-readable storage medium may be any tangible medium containing or storing a program that can be used by or in combination with an instruction execution system, device or device. In the present invention, a computer-readable signal medium may include a data signal propagated in a baseband or as part of a carrier wave, which carries a computer-readable program code. This propagated data signal may take a variety of forms, including but not limited to electromagnetic signals, optical signals, or any suitable combination of the above. Computer-readable signal media may also be any computer-readable medium other than computer-readable storage media, which may send, propagate or transmit a program for use by or in conjunction with an instruction execution system, apparatus or device. The program code contained on the computer-readable medium may be transmitted using any appropriate medium, including but not limited to: wireless, wire, optical cable, RF, etc., or any suitable combination of the above.
[0210] The flow chart and block diagram in the accompanying drawings illustrate the possible architecture, function and operation of the system, method and computer program product according to various embodiments of the present invention. In this regard, each box in the flow chart or block diagram can represent a module, a program segment, or a part of a code, and the above-mentioned module, program segment, or a part of a code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order from the order marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram or flow chart, and the combination of the boxes in the block diagram or flow chart can be implemented with a dedicated hardware-based system that performs a specified function or operation, or can be implemented with a combination of dedicated hardware and computer instructions.
[0211] The modules involved in the embodiments of the present invention may be implemented in software or hardware. The modules described may also be provided in a processor, for example, may be described as: a processor comprising a first acquisition unit, a second acquisition unit and a synchronization unit. The names of these modules do not, in certain cases, constitute limitations on the modules themselves, for example, the first acquisition unit may also be described as "a module for obtaining an application operation log of an application, wherein the application operation log comprises one or more first data records, wherein the first data record has a data index and a first version number".
[0212] As another aspect, the present invention further provides a computer-readable medium, which may be included in the device described in the above embodiment; or may exist independently without being assembled into the device. The above computer-readable medium carries one or more programs, and when the above one or more programs are executed by a device, the device includes:
[0213] Obtaining an application operation log of the application, wherein the application operation log includes one or more first data records, wherein the first data record has a data index and a first version number;
[0214] Obtaining a database log of a source database of the application, wherein the database log includes one or more second data records, and the second data record has a data index and a second version number;
[0215] A synchronization operation is performed on a target database according to the first data record and the second data record, wherein the first data record and the second data record have the same data index.
[0216] According to the technical solution of the embodiment of the present invention, data synchronization between the source database and the target database is achieved by applying the operation log and the database log of the source database. The application operation log saves the operation record of the application on the source database. The application operation log and the database log have the same data index, which is used to identify the data record in the source database. By applying the operation log and the database log, the data changes in the source database can be completely and accurately recorded, so that the source database and the target database can be synchronized more accurately.
[0217] The above specific implementations do not constitute a limitation on the protection scope of the present invention. It should be understood by those skilled in the art that various modifications, combinations, sub-combinations and substitutions may occur depending on design requirements and other factors. Any modification, equivalent substitution and improvement made within the spirit and principle of the present invention shall be included in the protection scope of the present invention.
Claims
1. A database synchronization method, characterized in that: include: Obtaining an application operation log of the application, wherein the application operation log includes one or more first data records, wherein the first data record has a data index and a first version number; Obtaining a database log of a source database of the application, wherein the database log includes one or more second data records, and the second data record has a data index and a second version number; Performing a synchronization operation on a target database according to the first data record and the second data record, wherein the first data record and the second data record have the same data index; The step of performing synchronization operation on the target database according to the first data record and the second data record includes: Merging the first data record and the second data record having the same data index to obtain a merged data record, wherein the data index of the merged data record is the same as the data index of the first data record or the second data record, and determining a base version number of the merged data record according to a first version number of the first data record and a second version number of the second data record; A synchronization operation is performed on the target database according to the merged data record.
2. The method according to claim 1, characterized in that The merging the first data record and the second data record having the same data index to obtain a merged data record includes: For each of the second data records to be merged, determine a target second data record in the second data record whose second version number is greater than or equal to the base version number of the merged data record; The merged data record is updated according to the target second data record, and the base version number of the data merge queue is updated to the second version number of the target second data record.
3. The method according to claim 1, characterized in that The merging the first data record and the second data record having the same data index to obtain a merged data record includes: Determine the maximum second version number of each of the second data records to be merged; When the maximum second version number is greater than or equal to the base version number of the merged data record, the merged data record is updated according to the second data record with the maximum second version number, and the base version number of the data merge queue is updated to the maximum second version number.
4. The method according to claim 2 or 3, characterized in that: After updating the base version number of the data merging queue according to the second version number, the method further includes: For each of the first data records to be merged, determine a target first data record in the first data record whose first version number is greater than a base version number of the merged data record; According to the order of the first version numbers of the target first data records, the merged data records are updated with the target first data records, and the base version number of the data merge queue is updated to the first version number of the target first data records.
5. The method according to claim 2 or 3, characterized in that: After updating the base version number of the data merging queue according to the second version number, the method further includes: Determine the maximum first version number of each of the first data records to be merged; When the maximum first version number is greater than the base version number of the merged data record, the merged data record is updated according to the first data record with the maximum first version number, and the base version number of the data merge queue is updated to the maximum first version number.
6. The method according to claim 1, characterized in that include: receiving a query request for the merged data record; Determine whether there is a first data record to be merged, wherein the first version number of the first data record to be merged is greater than the base version number of the merged data record; If it exists, the merged data record is updated according to the first data record, and the base version number of the data merge queue is updated to the largest first version number of the first data record to be merged and processed.
7. The method according to claim 1, characterized in that The merging the first data record and the second data record having the same data index to obtain a merged data record includes: Obtaining a task of merging data records using one or more queue records, wherein the task is represented in the queue by the data index; A task to be executed is determined from the queue to merge the first data record and the second data record having the same data index to obtain a merged data record.
8. A database synchronization method, characterized in that: include: After the operation on the source database is successful, an application operation log is generated, wherein the application operation log includes one or more first data records, and the first data record has a data index and a first version number; The application operation log is pushed to a synchronization center; wherein the synchronization center obtains a database log of the source database, wherein the database log includes one or more second data records, and the second data record has a data index and a second version number; the synchronization center merges the first data record and the second data record having the same data index to obtain a merged data record, and performs synchronization operations on the target database according to the merged data record; the data index of the merged data record is the same as the data index of the first data record or the second data record, and the base version number of the merged data record is determined according to the first version number of the first data record and the second version number of the second data record.
9. A database synchronization device, characterized in that: include: A first acquisition unit, configured to acquire an application operation log of an application, wherein the application operation log includes one or more first data records, and the first data record has a data index and a first version number; A second acquisition unit, configured to acquire a database log of a source database of the application, wherein the database log includes one or more second data records, and the second data record has a data index and a second version number; a synchronization unit, configured to perform a synchronization operation on a target database according to the first data record and the second data record, wherein the first data record and the second data record have the same data index; The synchronization unit is specifically configured to: merge the first data record and the second data record having the same data index to obtain a merged data record, wherein the data index of the merged data record is the same as the data index of the first data record or the second data record, and determine a base version number of the merged data record according to a first version number of the first data record and a second version number of the second data record; A synchronization operation is performed on the target database according to the merged data record.
10. A database synchronization device, characterized in that: include: A log generating unit, configured to generate an application operation log after the operation on the source database is successful, wherein the application operation log includes one or more first data records, wherein the first data record has a data index and a first version number; A log push module, used for pushing the application operation log to a synchronization center; wherein the synchronization center obtains a database log of the source database, the database log includes one or more second data records, the second data record has a data index and a second version number; the synchronization center merges the first data record and the second data record having the same data index to obtain a merged data record, and performs synchronization operations on the target database according to the merged data record; the data index of the merged data record is the same as the data index of the first data record or the second data record, and the base version number of the merged data record is determined according to the first version number of the first data record and the second version number of the second data record.
11. An electronic device, characterized in that: include: one or more processors; a storage device for storing one or more programs, When the one or more programs are executed by the one or more processors, the one or more processors implement the method according to any one of claims 1 to 8.
12. A computer readable medium having a computer program stored thereon, characterized in that: When the program is executed by a processor, the method according to any one of claims 1 to 8 is implemented.
Citation Information
Patent Citations
Data processing method and data processing device applied to data warehouse
CN104298760A
