Isomeric database consistency checking method and system, electronic device and storage medium
By identifying a common verification watermark in heterogeneous databases and constructing a hash tree, hash values are calculated only for active data shards. This solves the problem of time-consuming hash comparison under large data volumes, achieves efficient consistency verification and repair, and reduces the false alarm rate.
Patent Information
- Application Number
- CN202611397643.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2026-09-09
- Publication Date
- 2026-11-24
AI Technical Summary
Existing hash comparison schemes are time-consuming to verify large amounts of data and cannot efficiently perform consistency verification of heterogeneous databases.
By identifying a common verification watermark, a hash tree is constructed for the source and target ends. Hash values are calculated only for active data shards, while historical hash values are reused for inactive shards. The hash values are compared layer by layer to locate the difference rows, thereby achieving dynamic incremental construction and repair.
It reduces verification time, decreases false alarms caused by synchronization delay, and improves verification efficiency and accuracy.
Smart Images

Figure CN123157733A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of database synchronization technology, and in particular to a heterogeneous database consistency verification method, system, electronic device and storage medium. Background Technology
[0002] In database synchronization scenarios, data consistency verification is typically required between the source and target databases to ensure that no data is lost or tampered with during the synchronization process. Existing consistency verification schemes usually employ full hash comparison. Full hash comparison involves performing a full table scan on both the source and target databases, calculating and comparing the MD5 (Message Digest Algorithm 5) or SHA-256 (Secure Hash Algorithm 256-bit) hash values of the entire table. MD5 and SHA-256 are both commonly used hash algorithms that can convert input data of arbitrary length into fixed-length hash values (MD5 outputs 128 bits, SHA-256 outputs 256 bits), used for quickly comparing whether two sets of data are consistent. Because hash algorithms are input-sensitive, even a slight change in the original data will result in a drastically different hash value; therefore, they are often used for data integrity verification.
[0003] Existing hash comparison schemes are time-consuming to verify large amounts of data. Summary of the Invention
[0004] This application provides a heterogeneous database consistency verification method, system, electronic device, and storage medium to solve the shortcomings of existing hash comparison schemes that consume a lot of time in the case of large data volume, and to reduce the verification time.
[0005] This application provides a method for heterogeneous database consistency verification, including the following steps: A common verification watermark is determined, which is used to characterize the logical time boundary of synchronized data between the source database and the target database. After confirming that the target database has been applied to the data position indicated by the common verification watermark, a source hash tree is constructed based on the data set in the source database whose timestamp is no later than the common verification watermark, and a target hash tree is constructed based on the data set in the target database whose applied position is no later than the common verification watermark. Both the source hash tree and the target hash tree are constructed based on the same sharding rule and only calculate the hash value of the corresponding leaf node for active data shards that have undergone data changes in their respective datasets. The leaf nodes of inactive data shards reuse historical hash values. Obtain the root value of the source hash tree of the source database and the root value of the target hash tree of the target database respectively; If the root value of the source hash tree is inconsistent with the root value of the target hash tree, the hash values of the child nodes are compared layer by layer from the root node to locate the target leaf node with inconsistent hash values. The target leaf node corresponds to a data shard under the sharding rule. Based on the data shards corresponding to the target leaf node, locate the difference row, obtain the latest data of the difference row from the source database, and repair the difference row in the target database based on the latest data.
[0006] In some embodiments, obtaining the source hash tree root value of the source database and the target hash tree root value of the target database respectively includes: Obtain a unified set of shards to be verified. The unified set of shards to be verified is generated by the verifier based on the data change events recorded in the changed data capture stream, and includes at least one data shard that has undergone data change. Based on the unified set of shards to be verified, the following operations are performed in both the source database and the target database: For each data shard indicated by the unified set of shards to be verified, if the data shard is an active data shard, the hash value of the leaf node corresponding to the active data shard is calculated; if it is an inactive data shard, the historical hash value of its leaf node is reused. The hash value of the parent node is calculated layer by layer upwards based on the hash value of each leaf node until the hash value of the root node is obtained, thus obtaining the root value of the source hash tree or the root value of the target hash tree.
[0007] In some embodiments, obtaining a unified set of shards to be verified includes: The corresponding dirty fragment bitmap is maintained in the verifier memory. Each bit of the dirty fragment bitmap corresponds to a data fragment. The first value of each bit indicates that the corresponding data fragment has undergone data changes, and the second value of each bit indicates that the corresponding data fragment has not undergone data changes. The verifier listens to the change data capture stream, and when it captures a data change event for any data fragment, it sets the value of the bit corresponding to the data fragment in the dirty fragment bitmap to the first value. When performing consistency checks, the verifier reads the data fragment corresponding to the bit with the first value in the dirty fragment bitmap and uses it as the unified set of fragments to be checked.
[0008] In some embodiments, For each data shard indicated by the unified set of shards to be verified, if the data shard is an active data shard, then the hash value of the leaf node corresponding to the active data shard is calculated, including: For each active data shard, obtain the row hash value of each data row in the active data shard and auxiliary verification information. The auxiliary verification information includes at least one of the following: the total number of data rows in the active data shard, the modulo sum of the row hash values of each data row in the active data shard, and the digest value of the row hash values of each data row in the active data shard. The row hash values of each data row are aggregated by performing an XOR operation in any order to obtain the first aggregated component; A second aggregate component is generated based on the auxiliary verification information; Based on the first aggregated component and the second aggregated component, the leaf node hash value of the active data shard is generated.
[0009] In some embodiments, the incremental maintenance of the leaf node hash value includes: Retrieve the idempotent key and event sequence number of the data change event; Detect whether the data change event has been processed based on the idempotent key and the event sequence number; If the process has already been processed, skip incremental maintenance; otherwise, perform incremental maintenance.
[0010] In some embodiments, performing incremental maintenance includes: When adding a new data row, the hash value of the current leaf node is XORed with the hash value of the new data row to obtain the updated hash value of the leaf node. When deleting a data row, the current leaf node hash value is XORed with the row hash value of the deleted data row to obtain the updated leaf node hash value. When modifying a data row, the XOR result of the row hash value before modification and the row hash value after modification is calculated. The current leaf node hash value is then XORed with the XOR result to obtain the updated leaf node hash value.
[0011] In some embodiments, it also includes: When performing consistency checks, the leaf node hash values of the active data shards are recalculated, and the recalculated leaf node hash values are overwritten with the incrementally maintained leaf node hash values.
[0012] In some embodiments, the step of comparing the hash values of child nodes layer by layer from the root node to locate the target leaf node with inconsistent hash values includes: Starting from the root node of the source hash tree and the root node of the target hash tree, obtain the hash values of each child node of the next level layer by layer downwards; The hash values of the child nodes in the source hash tree are compared with the hash values of the child nodes at the corresponding positions in the target hash tree. When a child node with an inconsistent hash value is found, determine whether the child node is a leaf node. If not, proceed to the next level of that child node for further comparison; If so, then the child node is determined as a differential leaf node; Traverse all subtrees with inconsistent hash values until all differing leaf nodes are located, and these differing leaf nodes are the target leaf nodes.
[0013] In some embodiments, the step of locating the differing row based on the data shard corresponding to the target leaf node, obtaining the latest data of the differing row from the source database, and repairing the differing row in the target database based on the latest data includes: Within the sharding range corresponding to the target leaf node, obtain the primary key and row hash value of each data row in the source database, and the primary key and row hash value of each data row in the target database. Align the data rows in the source database and the target database according to the primary key, and compare the row hash values; Based on the alignment results and the row hash value comparison results, the difference rows are determined. The difference rows include at least one of the following: the first type of difference rows that exist at the source end but not at the target end, the second type of difference rows that exist at both ends but have different row hash values, and the third type of difference rows that do not exist at the source end but exist at the target end. The latest data of the differing rows is obtained from the source database, and the differing rows in the target database are repaired based on the latest data.
[0014] In some embodiments, obtaining the latest data of the differing rows from the source database and repairing the differing rows in the target database based on the latest data includes: Based on the type of the differing row, generate the corresponding repair instruction using any of the following methods and send it to the target database for execution: For lines that exist in the source but not in the target, generate an insert instruction; For lines that exist on both ends but have different content, generate an update instruction; For lines that are not present in the source but exist in the target, generate a deletion command.
[0015] In some embodiments, determining the common verification watermark includes: A heartbeat table is maintained in the source database. The heartbeat table is updated at preset time intervals. Each update is committed as a transaction, and the logical clock TSO corresponding to the transaction is obtained. The update event carrying the TSO is synchronized to the target database via a change data capture stream; The TSO is used as the common verification watermark.
[0016] A consistency verification system according to this application includes the following modules: The watermark determination module is used to determine a common verification watermark, which is used to characterize the logical time boundary of synchronized data between the source database and the target database. The hash tree construction module is used to construct a source hash tree based on the data set in the source database whose timestamp is no later than the common verification watermark after confirming that the target database has been applied to the data position indicated by the common verification watermark, and to construct a target hash tree based on the data set in the target database whose applied position is no later than the common verification watermark. Both the source hash tree and the target hash tree are constructed based on the same sharding rule and only calculate the hash value of the corresponding leaf node for active data shards that have undergone data changes in their respective datasets. The leaf nodes of inactive data shards reuse historical hash values. The data acquisition module is used to acquire the root value of the source hash tree of the source database and the root value of the target hash tree of the target database, respectively. The data location module is used to compare the hash values of child nodes layer by layer from the root node if the root value of the source hash tree is inconsistent with the root value of the target hash tree, and locate the target leaf node with inconsistent hash values. The target leaf node corresponds to a data shard under the sharding rule. The data repair module is used to locate the difference row according to the data shard corresponding to the target leaf node, obtain the latest data of the difference row from the source database, and repair the difference row in the target database according to the latest data.
[0017] This application also provides an electronic device, including a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the processor executes the computer program to implement any of the heterogeneous database consistency verification methods described above.
[0018] This application also provides a non-transitory computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the heterogeneous database consistency verification method as described above.
[0019] This application also provides a computer program product, including a computer program that, when executed by a processor, implements the heterogeneous database consistency verification method as described above.
[0020] This application provides a heterogeneous database consistency verification method, system, electronic device, and storage medium. By determining a common verification watermark, after confirming that the target database has been applied to the data position indicated by the common verification watermark, the root value of the hash tree of the source database and the target database is obtained for consistency determination. The hash tree only calculates the hash value of the corresponding leaf node for active data shards that have undergone data changes, and reuses the historical hash value for the leaf nodes of inactive data shards, thereby reducing the verification time. Attached Figure Description
[0021] To more clearly illustrate the technical solutions in this application or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of this application. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0022] Figure 1 This is a flowchart illustrating the heterogeneous database consistency verification method provided in this application.
[0023] Figure 2 This is a schematic diagram of the application system of the heterogeneous database consistency verification method provided in this application.
[0024] Figure 3 This is the overall flowchart of the heterogeneous database consistency verification method provided in this application.
[0025] Figure 4 This is a schematic diagram of the hash tree's drill-down and self-healing principle provided in this application.
[0026] Figure 5 This is a schematic diagram of the consistency verification system provided in this application.
[0027] Figure 6 This is a schematic diagram of the physical structure of the electronic device provided in this application. Detailed Implementation
[0028] To make the objectives, technical solutions, and advantages of this application clearer, the technical solutions of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0029] Figure 1 This is a flowchart illustrating the heterogeneous database consistency verification method provided in this application. Figure 1 As shown, the method includes the following: Step 101: Determine the common verification watermark, which is used to characterize the logical time boundary of the synchronized data between the source database and the target database.
[0030] The shared verification watermark is a logical time boundary value used to define the scope of data that has been synchronized between the source and target databases. The source database is the data producer, and the target database is the data consumer; data synchronization between the two is achieved through Change Data Capture (CDC).
[0031] It should be noted that in asynchronous data synchronization scenarios, data written from the source end is transmitted to the target end via CDC, resulting in a time delay. If consistency verification is initiated directly without watermark constraints, false alarms may occur because the target end has not yet received the latest data. The method for determining the common verification watermark is as follows: a heartbeat table is maintained in the source database, and this heartbeat table is updated at preset time intervals. Each heartbeat update is committed as a transaction and generates a corresponding logical clock TSO (TimestampsOracle). The update event carrying this TSO is synchronized to the target database through the CDC stream, and the verifier uses this TSO as the common verification watermark.
[0032] Step 102: After confirming that the target database has been applied to the data position indicated by the common verification watermark, construct a source hash tree based on the data set in the source database whose timestamp is no later than the common verification watermark, and construct a target hash tree based on the data set in the target database whose applied position is no later than the common verification watermark. Both the source hash tree and the target hash tree are constructed based on the same sharding rule and only calculate the hash value of the corresponding leaf node for active data shards that have undergone data changes within their respective datasets. The leaf nodes of inactive data shards reuse historical hash values.
[0033] A Merkle Tree is a tree-like data structure where leaf nodes store the hash values of data fragments, non-leaf nodes store the combined hash values of their child nodes, and the root node's hash value represents the overall state of the entire tree.
[0034] It should be noted that the verifier monitors the consumption checkpoints of the target database in real time. Once it confirms that the target has applied the checkpoint indicated by the common verification watermark W, the verifier defines the verification range on both the source and target sides: the source side takes data with timestamps no later than W, and the target side takes data with applied checkpoints no later than W. The hash tree uses data shards as the smallest unit, and each leaf node stores the aggregate hash value of the corresponding data shard.
[0035] Step 103: Obtain the root value of the source hash tree of the source database and the root value of the target hash tree of the target database.
[0036] In this system, the source database acts as the data producer (e.g., TiDB), and the target database acts as the data consumer (e.g., ClickHouse). Data synchronization between the two is achieved through Change Data Capture (CDC). A Merkle Tree is a tree-like data structure. Leaf nodes store the hash values of data shards, non-leaf nodes store the combined hash values of their child nodes, and the root node's hash value represents the overall state of the entire tree. In a hash tree, leaf nodes are at the bottom level, each corresponding to a data shard created according to sharding rules. The hash value of a leaf node is the aggregated hash value of all data rows within that shard. Non-leaf nodes are located above the leaf nodes, and their hash values are calculated from the hash values of all their child nodes using a predefined hash combination algorithm, representing the overall state of their corresponding subtree. The root node is at the top level, and its hash value aggregates information from all leaf nodes, thus representing the overall data state of the entire table. Active data shards refer to data shards that have undergone data changes (insertion, update, deletion) during CDC synchronization. Historical hash value refers to the leaf node hash value calculated and persisted during the last consistency check.
[0037] The consistency verification method provided in this application is applicable to source databases that support distributed transactions, such as TiDB v4.0 and above, and target databases with columnar storage, such as ClickHouse v20.0 and above. In the implementation where ClickHouse is used as the target database, the target table uses the ReplacingMergeTree engine. In the implementation where TiDB is used as the source database, data shards correspond to TiDB Regions. When a Region splits or merges, the verifier obtains the Region ID and Key Range before and after the change, recalculates the hash value of the leaf nodes of the affected new Region, clears the original Region's mark in the dirty sharding bitmap and marks the new Region as active, and recalculates the parent node hash value in a bottom-up order until the root node.
[0038] It should be noted that the system retrieves the root node hash values of the hash trees from both the source and target databases. The leaf nodes of the hash tree are based on data shards as the smallest unit, with each leaf node storing the aggregate hash value of the corresponding data shard. To improve verification efficiency, the system employs a dynamic incremental construction strategy. That is, during each consistency check, only the hash values of the corresponding leaf nodes of data shards marked as active in the CDC stream are recalculated. Inactive shards directly reuse the historical hash values saved during the previous check, thus avoiding repeated scanning of cold data shards. For the initial check, since no historical hash values exist, the system calculates the hash values of the corresponding leaf nodes for all data shards and persists the calculation results as historical hash values for subsequent checks.
[0039] Step 104: If the root value of the source hash tree is inconsistent with the root value of the target hash tree, then start from the root node and compare the hash values of the child nodes layer by layer downwards to locate the target leaf node with inconsistent hash values. The target leaf node corresponds to a data shard under the sharding rule.
[0040] The target leaf node refers to the first leaf node with inconsistent hash values found during the layer-by-layer comparison process, and its corresponding data shard is the shard with data differences.
[0041] It's important to note that if the root value of the source hash tree differs from the root value of the target hash tree, it indicates a discrepancy in the overall data state on both sides, requiring discrepancy localization. The system compares the hash values of child nodes layer by layer downwards from the root node. If the hash values of child nodes at a certain level are consistent, the subtree as a whole is identical, and further drilling is unnecessary. If they are inconsistent, the system continues comparing at the next level of that child node until the leaf node with the inconsistent hash value is located. The data shard corresponding to this target leaf node is the shard with the data discrepancy.
[0042] Step 105: Locate the difference row according to the data shards corresponding to the target leaf node, obtain the latest data of the difference row from the source database, and repair the difference row in the target database according to the latest data.
[0043] Among them, the difference row refers to the specific data row in the data shard range corresponding to the target leaf node where the hash values of the source and target ends are inconsistent.
[0044] It should be noted that the system performs fine-grained location within the data shard range corresponding to the target leaf node to identify rows with inconsistent hash values. Subsequently, the system retrieves the latest data for this inconsistent row from the source database and uses this latest data to repair the inconsistent row in the target database. This completes the automated closed-loop process of consistency verification and repair.
[0045] The above embodiments define common snapshots through logical watermarking, construct hash trees only for active shard increments, and automatically repair inconsistencies by drilling down layer by layer to locate specific rows. This reduces the amount of verification scanning and reduces false alarms caused by synchronization delays.
[0046] In some embodiments, obtaining the source hash tree root value of the source database and the target hash tree root value of the target database respectively includes: Obtain a unified set of shards to be verified. The unified set of shards to be verified is generated by the verifier based on the data change events recorded in the changed data capture stream, and includes at least one data shard that has undergone data change. Based on the unified set of shards to be verified, the following operations are performed in both the source database and the target database: For each data shard indicated by the unified set of shards to be verified, if the data shard is an active data shard, the hash value of the leaf node corresponding to the active data shard is calculated; if it is an inactive data shard, the historical hash value of its leaf node is reused. The hash value of the parent node is calculated layer by layer upwards based on the hash value of each leaf node until the hash value of the root node is obtained, thus obtaining the root value of the source hash tree or the root value of the target hash tree.
[0047] It should be noted that the methods for obtaining the root value of the source hash tree in the source database and the root value of the target hash tree in the target database are as follows: The validator generates a unified set of shards to be verified based on the data change events recorded in the changed data capture stream. Based on this unified set of shards to be verified, the hash tree root value is calculated in both the source and target databases. On the validator side, each data shard in the unified set of shards to be verified is the data shard that needs to be hashed or reused. When the source and target databases are executed respectively, for each data shard indicated by the unified set of shards to be verified, if the data shard is an active data shard, the hash value of the leaf node corresponding to the active data shard is calculated; if it is an inactive data shard, the historical hash value persisted during the last verification is directly reused. After obtaining the hash values of all leaf nodes, the hash values of the parent nodes are calculated layer by layer upwards from the leaf nodes according to the hierarchical structure of the hash tree until the hash value of the root node is calculated. The hash value of the root node is the root value of the hash tree at that end. Both the source database and the target database execute the above process based on the same unified set of shards to be verified, and obtain their respective hash tree root values.
[0048] This embodiment generates a unified set of shards to be verified by a verifier and uses it simultaneously at both ends, avoiding inconsistencies in the shard set caused by the source and target ends independently determining the active shards, and reducing the risk of misjudgment due to shard misalignment.
[0049] In some embodiments, obtaining a unified set of shards to be verified includes: The corresponding dirty fragment bitmap is maintained in the verifier memory. Each bit of the dirty fragment bitmap corresponds to a data fragment. The first value of each bit indicates that the corresponding data fragment has undergone data changes, and the second value of each bit indicates that the corresponding data fragment has not undergone data changes. The verifier listens to the change data capture stream, and when it captures a data change event for any data fragment, it sets the value of the bit corresponding to the data fragment in the dirty fragment bitmap to the first value. When performing consistency checks, the verifier reads the data fragment corresponding to the bit with the first value in the dirty fragment bitmap and uses it as the unified set of fragments to be checked.
[0050] The dirty fragment bitmap is a memory data structure that uses a bitmap to record the change status of each data fragment. Each bit corresponds one-to-one with a data fragment, and the bit value indicates whether the fragment has undergone data changes. The first and second values are two different values, such as 1 and 0, used to distinguish between the two states of "changed" and "not changed".
[0051] It should be noted that the verifier maintains a dirty shard bitmap in memory, where each bit corresponds one-to-one with a data shard, and the value of each bit indicates whether the corresponding data shard has undergone data modification. When the verifier captures a data modification operation (insertion, update, or deletion) on any data shard from the changed data capture stream, the verifier sets the corresponding bit in the dirty shard bitmap for that data shard to the first value. When consistency verification is required, the verifier traverses the dirty shard bitmap, reads all data shards corresponding to bits with the first value, and identifies them as active data shards, i.e., the set of shards whose hash values need to be recalculated. The first value can be 1, and the second value can be 0.
[0052] For example, this application adopts a dynamic incremental construction logic, that is, a technical solution that builds hash trees only for Region / Key Ranges marked as active in the CDC stream, rather than building the entire table.
[0053] In this application, the hash tree is referred to as a "Dynamic Incremental Merkle Tree". Unlike the Sparse Merkle Tree with fixed key space, the Dynamic Incremental Merkle Tree of this application does not rely on the default hash and path encoding of the pre-set empty nodes. Instead, it achieves efficient hash tree maintenance by caching the historical leaf hash values and updating them incrementally with CDC events.
[0054] This embodiment uses a dirty sharding bitmap to achieve fast and accurate identification of active data shards, avoiding traversing all shards or relying on additional metadata queries.
[0055] For example, the validator maintains a Dirty Bitmap in memory. When the validator captures data changes (Insert / Update / Delete) from the CDC stream, it sets the corresponding Region ID to 1 in the bitmap in real time. When verification starts, it only reconstructs the Hash tree nodes for active shards marked as 1 in the bitmap. For unmarked cold data blocks, it directly reads the previously calculated Hash value from the persistent cache, thus achieving "dynamic incremental construction".
[0056] In some embodiments, for each data shard indicated by the unified set of shards to be verified, if the data shard is an active data shard, calculating the hash value of the leaf node corresponding to the active data shard includes: For each active data shard, obtain the row hash value of each data row in the active data shard and auxiliary verification information. The auxiliary verification information includes at least one of the following: the total number of data rows in the active data shard, the modulo sum of the row hash values of each data row in the active data shard, and the digest value of the row hash values of each data row in the active data shard. The row hash values of each data row are aggregated by performing an XOR operation in any order to obtain the first aggregated component; A second aggregate component is generated based on the auxiliary verification information; Based on the first aggregated component and the second aggregated component, the leaf node hash value of the active data shard is generated.
[0057] The row hash value is generated by deterministically encoding a data row. The input for deterministic encoding includes at least the primary key of the row, the current table structure version number, and the normalized values of each field. Auxiliary verification information is an additional verification dimension besides the XOR aggregate hash used to assist in verifying the consistency of data shards. The modulo sum is the aggregated value obtained by adding the row hash values of each data row modulo (e.g., 2^32 or 2^64). The digest is the digest value obtained by further aggregating the row hash values of each data row using a different hash algorithm than XOR (e.g., CRC64, MD5, or SHA-256).
[0058] It should be noted that the XOR operation satisfies the commutative and associative laws, i.e., A⊕B = B⊕A. Therefore, aggregating the row hash values of each data row by performing an XOR operation in any order is independent of the physical storage order of the data rows. For each active data shard, after obtaining the row hash values of each data row, the system performs an XOR operation on all row hash values to obtain the first aggregated component. Simultaneously, it obtains auxiliary verification information (such as at least one of the following: total number of data rows, modulo sum, and digest) to generate a second aggregated component. Then, the first aggregated component and the second aggregated component are combined to generate the leaf node hash value of that shard. By introducing at least one auxiliary verification piece of information, the probability of misjudgment due to hash collisions can be effectively reduced.
[0059] The above embodiments achieve row-order-independent hash calculation through XOR aggregation and introduce auxiliary verification information to reduce the risk of misjudgment caused by hash collisions.
[0060] In some embodiments, the incremental maintenance of the leaf node hash value includes: Retrieve the idempotent key and event sequence number of the data change event; Detect whether the data change event has been processed based on the idempotent key and the event sequence number; If the process has already been processed, skip incremental maintenance; otherwise, perform incremental maintenance.
[0061] The idempotent key is an identifier used to uniquely identify a data change event, generated based on the transaction identifier, table identifier, and primary key value to which the data change event belongs. The event sequence number is used to identify the sequential position of the same data change event within the change data capture stream, ensuring the global order of event processing.
[0062] It should be noted that in CDC streaming synchronization scenarios, the same data change event may be delivered multiple times due to factors such as network retransmission or component restarts. If no deduplication is performed on the events and incremental maintenance is directly applied to the leaf node hash values, duplicate flips may occur, resulting in hash value errors. This embodiment obtains the idempotent key and event sequence number of the data change event during incremental maintenance. It then checks whether the event has already been processed based on the idempotent key and event sequence number: if it has been processed, the incremental maintenance is skipped; otherwise, incremental maintenance is performed. This combined verification of the idempotent key and event sequence number ensures that each data change event is only effective once.
[0063] The above embodiments use a combination of idempotent keys and event sequence numbers for verification to prevent the same data change event from being processed repeatedly, which could lead to hash state errors.
[0064] In some embodiments, performing incremental maintenance includes: When adding a new data row, the hash value of the current leaf node is XORed with the hash value of the new data row to obtain the updated hash value of the leaf node. When deleting a data row, the current leaf node hash value is XORed with the row hash value of the deleted data row to obtain the updated leaf node hash value. When modifying a data row, the XOR result of the row hash value before modification and the row hash value after modification is calculated. The current leaf node hash value is then XORed with the XOR result to obtain the updated leaf node hash value.
[0065] The current leaf node hash value refers to the aggregate hash value of the leaf node before this data change operation was performed. The XOR operation is reflexive (A⊕A=0), meaning that the same value cancels out after being XORed twice consecutively. This characteristic allows incremental maintenance to be unaware of the existence of data rows.
[0066] It should be noted that when a data row within a data shard changes, the verifier does not need to recalculate the hash values of all data rows within that shard. It only needs to perform an XOR operation based on the hash value of the current leaf node and the row hash value of the changed data row to complete the update.
[0067] Specifically, when adding a new data row, the hash value of the current leaf node is XORed with the hash value of the newly added data row; when deleting a data row, the hash value of the current leaf node is XORed with the hash value of the deleted data row; when modifying a data row, the hash value of the current leaf node is XORed with the XOR result of the hash values of the row before and after modification. Utilizing the reflexivity of the XOR operation, all three modification operations can update the leaf node hash value in O(1) operations, without needing to know if the data row already exists.
[0068] This embodiment can complete the O(1) incremental maintenance of the leaf node hash value based only on the data row that has changed, without recalculating all the data in the fragment, thus avoiding repeated scanning of cold data.
[0069] For example, to ensure consistency of heterogeneous data, the system performs deterministic encoding on data rows, including: arranging fields in a uniform order, including the primary key in the row hash calculation, using different identifiers to encode NULL and empty strings, converting Decimal to strings of uniform precision, standardizing floating-point special values (NaN, Infinity, etc.), uniformly converting date and time to UTC timezone timestamps, sorting JSON field keys lexicographically, and carrying the table structure version number. Subsequently, the system uses an XOR algorithm that satisfies both commutative and associative laws to calculate the aggregate hash of the data blocks.
[0070] Let the data block contain a set of rows R = The formula for calculating its aggregate hash value is: ; Where Hash(r) i The hash value is the deterministically encoded value of the data in the i-th row. The input to this deterministic encoding includes at least the primary key of the row, the current table structure version number, and the normalized values of each field. The row hash value is generated using a 64-bit digest algorithm to control the probability of hash collisions within an engineering-acceptable range.
[0071] Line order irrelevant: because This algorithm ensures that the verification results are independent of the physical read order, eliminating the need for costly sorting of ClickHouse data.
[0072] It should be noted that XOR aggregation hashing is a probabilistic verification method that can detect most data inconsistencies, but theoretically there is still a possibility of hash collisions. Therefore, it should not be used as a "unique identifier" or a "100% accurate" verification basis.
[0073] The above scheme employs O(1) incremental maintenance, that is, it utilizes the reflexivity of XOR. When processing DELETE operations, the system does not need to check if the row exists. It can automatically cancel the row's hash value by XORing it with the total value, thus achieving dynamic maintenance with O(1) complexity.
[0074] In some embodiments, it also includes: When performing consistency checks, the leaf node hash values of the active data shards are recalculated, and the recalculated leaf node hash values are overwritten with the incrementally maintained leaf node hash values.
[0075] It should be noted that during incremental maintenance, if CDC events are delivered repeatedly, out of order, or omitted, the hash values of the leaf nodes obtained during incremental maintenance may be inaccurate. Therefore, during consistency verification, the system recalculates the hash values of the leaf nodes for active data shards and overwrites the hash values obtained during incremental maintenance with the recalculated hash values. This recalculation mechanism during verification uses the full recalculation result as a benchmark to calibrate and correct the incremental maintenance results, ensuring the accuracy of the verification benchmark.
[0076] This embodiment recalculates the incremental maintenance results during verification to calibrate the cumulative deviations that may be generated by incremental maintenance, ensuring the accuracy of the leaf node hash value and the reliability of the verification benchmark.
[0077] In some embodiments, the step of comparing the hash values of child nodes layer by layer from the root node to locate the target leaf node with inconsistent hash values includes: Starting from the root node of the source hash tree and the root node of the target hash tree, obtain the hash values of each child node of the next level layer by layer downwards; The hash values of the child nodes in the source hash tree are compared with the hash values of the child nodes at the corresponding positions in the target hash tree. When a child node with an inconsistent hash value is found, determine whether the child node is a leaf node. If not, proceed to the next level of that child node for further comparison; If so, then the child node is determined as a differential leaf node; Traverse all subtrees with inconsistent hash values until all differing leaf nodes are located, and these differing leaf nodes are the target leaf nodes.
[0078] It should be noted that the system starts from the root node of the source hash tree and the root node of the target hash tree, and retrieves the hash values of each child node at the next level layer by layer. The hash values of the child nodes in the source hash tree are compared with the corresponding child node hash values in the target hash tree. When a child node with inconsistent hash values is found, it is determined whether the child node is a leaf node. If it is not a leaf node, the comparison continues at the next level. If it is a leaf node, it is identified as a differing leaf node. This process traverses all subtrees with inconsistent hash values until all differing leaf nodes are located; these differing leaf nodes are the target leaf node. This process utilizes the hierarchical structure of the hash tree to progressively narrow the range of difference location until it is accurate to the specific data fragment granularity.
[0079] Understandably, this application does not construct a full table hash tree, but instead adopts a dynamic incremental hash tree, also known as a dynamic incremental Merkle tree construction strategy. The system uses data shard identifiers (such as TiDB's Region ID or Key Range) as the partition boundaries of hash tree leaf nodes, and only constructs tree nodes for data shards marked as active in the CDC stream, thereby achieving dynamic incremental construction.
[0080] This embodiment achieves efficient difference segment location by drilling down layer by layer to compare hash values, gradually narrowing the difference location range from the entire table to specific data shards.
[0081] In some embodiments, the step of locating the differing row based on the data shard corresponding to the target leaf node, obtaining the latest data of the differing row from the source database, and repairing the differing row in the target database based on the latest data includes: Within the sharding range corresponding to the target leaf node, obtain the primary key and row hash value of each data row in the source database, and the primary key and row hash value of each data row in the target database. Align the data rows in the source database and the target database according to the primary key, and compare the row hash values; Based on the alignment results and the row hash value comparison results, the difference rows are determined. The difference rows include at least one of the following: the first type of difference rows that exist at the source end but not at the target end, the second type of difference rows that exist at both ends but have different row hash values, and the third type of difference rows that do not exist at the source end but exist at the target end. The latest data of the differing rows is obtained from the source database, and the differing rows in the target database are repaired based on the latest data.
[0082] Among them, the difference row refers to the specific data row that is inconsistent between the source and the target after primary key alignment and row hash value comparison within the data shard range corresponding to the target leaf node. It includes three types of situations: the source has it but the target does not, both have it but the content is different, and the source does not have it but the target has it.
[0083] It should be noted that after locating the target leaf node with inconsistent hash values through drill-down, the system uses the data shard corresponding to that leaf node as the target range. Within this shard range, it compares the primary keys and row hash values of each data row in the source and target databases row by row. Using the primary key as the matching criterion, it matches the data rows at both ends one-to-one, compares the row hash values of the corresponding rows, and identifies primary keys that cannot be matched. Based on the alignment results and row hash value comparison results, it determines the discrepancy rows. Discrepancy rows include at least one of the following categories: the first category, rows that exist in the source but not in the target; the second category, rows that exist in both ends but have different row hash values; and the third category, rows that are absent in the source but exist in the target. After determining the discrepancy rows, the system retrieves the latest data for that discrepancy row from the source database and repairs the discrepancy rows in the target database according to the type of discrepancy row.
[0084] This embodiment accurately locates discrepancies by comparing each row within a segment and automatically retrieves data from the source for repair, achieving automated closed-loop self-healing for data consistency.
[0085] In some embodiments, obtaining the latest data of the differing rows from the source database and repairing the differing rows in the target database based on the latest data includes: Based on the type of the differing row, generate the corresponding repair instruction using any of the following methods and send it to the target database for execution: For lines that exist in the source but not in the target, generate an insert instruction; For lines that exist on both ends but have different content, generate an update instruction; For lines that are not present in the source but exist in the target, generate a deletion command.
[0086] It should be noted that after determining the type of the discrepancy, the system generates corresponding repair instructions based on that type. Specifically, for discrepancy rows that exist in the source database but not in the target database (i.e., missing data rows in the target database), an insert instruction is generated to write the source data to the target database; for discrepancy rows that exist in both databases but have different content, an update instruction is generated to overwrite the corresponding row in the target database with the source data; for discrepancy rows that do not exist in the source database but exist in the target database (i.e., redundant data rows in the target database), a delete instruction is generated to remove the row from the target database. By performing differentiated repair operations for different discrepancy types, the system ensures that the target database and the source database remain consistent.
[0087] This embodiment generates differentiated repair instructions for different types of discrepancies, achieving accurate alignment between the target and source data.
[0088] For example, when the root node hash of a Dynamic Incremental Merkle Tree (DSMT) is inconsistent, the verifier initiates a drill-down localization procedure: 1. Parallel retrieval: Parallel requests for the hash value of the next level (such as a sub-partition or data block) to quickly locate abnormal subtrees through comparison.
[0089] 2. Fine-grained positioning: Recursively apply this process until the specific primary key range or single row ID is located.
[0090] 3. Self-healing instruction: The verifier pulls the latest snapshot data of the RowID from the source, automatically generates and executes the INSERT INTO REPLACE statement to ClickHouse, corrects the discrepancies, and completes the consistency self-healing closed loop.
[0091] For example, the automated drill-down self-healing process includes hierarchical location logic from Root to RowID and a closed-loop control system that automatically generates and executes repair SQL (such as INSERT INTO REPLACE).
[0092] The above solution achieves automatic closed-loop repair, that is, it quickly locates the specific RowID through Merkle Proof and automatically triggers single-line supplementation, thus realizing automatic self-healing of data consistency.
[0093] In some embodiments, determining the common verification watermark includes: Maintain a heartbeat table in the source-side database, update the heartbeat table according to a preset time interval, submit each update as a transaction, and obtain the logical clock timestamp TSO corresponding to the transaction; Synchronize the update event carrying the TSO to the target-side database through a change data capture stream; Use the TSO as the common check watermark.
[0094] The heartbeat table is a special data table in the source-side database for recording logical clock watermarks. Each heartbeat update is submitted as a transaction and generates a corresponding logical clock TSO, which is synchronized to the target-side database through a change data capture stream along with the update event.
[0095] It should be noted that the system maintains a heartbeat table in the source-side database, updates the heartbeat table according to a preset time interval, submits each update as a transaction, obtains the logical clock TSO corresponding to the transaction, synchronizes the update event carrying the TSO to the target-side database through a change data capture stream, and uses the TSO as the common check watermark. The check watermark is used to characterize the logical clock progress of the currently committed transactions in the source-side database. The verifier can know the time point to which the target side has synchronized with the source side through the application status of the heartbeat table update event in the target-side database, thereby providing a reliable time reference for check triggering.
[0096] Illustratively, this application adopts a check trigger mechanism based on a bidirectional logical clock watermark, and the synchronization system periodically inserts a Watermark_Event carrying a source-side transaction version number (TSO) into the CDC data stream. The verifier monitors the consumption checkpoint of the target side (ClickHouse) in real time.
[0097] In specific implementation, the system maintains a heartbeat table _sys_heartbeat on the source side, and the CDC component updates the timestamp of the table every 50 ms. Each heartbeat update is submitted as a transaction and a corresponding TSO is obtained. This update operation is synchronized to the target side as a DML statement. The verifier polls the target-side heartbeat table. When TSO = N is read, according to the transaction ordering principle, it is proved that all service data with TSO < N has been persisted to disk.
[0098] At this time, the system defines a safe check time window: common check watermark W = min(Source_TSO, Target_Apply_Time - Safe_Margin). When the target-side checkpoint has been applied to W, the check action is triggered, thereby avoiding false positives caused by synchronization delay.
[0099] The above embodiments generate a logical clock watermark periodically through a heartbeat table and synchronize it to the target end, providing a reliable time reference for verification triggering.
[0100] The purpose of this application is to provide a consistency verification scheme for massive heterogeneous databases. It reduces the pressure of full scans by introducing a Dynamic Incremental Merkle Tree (i.e., a hash tree), reduces false positives caused by synchronization delays by using a Bidirectional Logical Watermark, and achieves rapid location of erroneous data through a drill-down mechanism. Finally, it completes data repair through automatically generated merge and insert statements.
[0101] Figure 2 This is a schematic diagram of an application system for the heterogeneous database consistency verification method provided in this application. For example... Figure 2 As shown, the overall architecture of this system includes a source database (Source DB), a change data capture service (CDC Service), a consistency validator (Validator), and a target database (Target DB). The consistency validator, as the core component, interacts with both the source and target databases, controlling the watermark checking and hash comparison processes.
[0102] Figure 3 This is the overall flowchart of the heterogeneous database consistency verification method provided in this application. Figure 3 As shown, this method mainly includes five consecutive steps: logical watermark checking, dynamic tree construction, root hash comparison, drill-down positioning, and self-healing repair. Specifically, it includes: S1: Obtain the Source TSO watermark and Target Checkpoint.
[0103] S2: Determine whether to enter the security window, i.e. whether the target checkpoint has been applied to the common verification watermark W, where W = min(Source_TSO, Target_Apply_Time - Safe_Margin). If yes, proceed with verification; otherwise, continue waiting.
[0104] S3: Construct a dynamic incremental hash tree (based on XOR aggregation) for active data shard (Region) reconstruction. Check if the Root Hash is consistent; if yes, the verification passes; otherwise, proceed to S4.
[0105] S4: Drill down layer by layer for positioning.
[0106] S5: Automatic repair (INSERT INTO REPLACE), end.
[0107] Figure 4 This is a schematic diagram illustrating the principle of layer-by-layer drill-down and self-healing in the hash tree provided in this application. For example... Figure 4 As shown, when an inconsistency in the RootHash is detected, the system only downloads the hashes of child nodes along the differing paths, skipping the consistent subtrees, thus locating the specific erroneous leaf node with minimal cost. Specifically, this includes: 1. Global hash verification and drill-down.
[0108] First, the status of the Root node (global hash) is verified. If the status is detected as "Mismatch", then the shards are filtered according to the hash comparison results: shards with consistent hashes are skipped directly, while shards with different hashes are drilled down to intermediate nodes for further investigation.
[0109] 2. Segment node location.
[0110] Perform hash comparisons on intermediate nodes after drilling down. Taking intermediate node B of shard 2 as an example, if its state is "Mismatch", then continue drilling down to the leaf node level; while shards with consistent hashes (such as intermediate node A) are skipped directly.
[0111] 3. Differentiated target identification.
[0112] The final hash comparison is performed at the leaf node level. For a specific range (such as leaf node 1 in Range: 0-5000), if the comparison finds "different hashes", then the node is precisely located as the difference (Diff) target; nodes with the same hash (such as leaf node 2 in Range: 5001-1W) are skipped.
[0113] 4. Trigger self-healing commands and data correction.
[0114] Once the leaf node with the discrepancy is identified, the self-healing instruction flow is immediately triggered, and the following repair operations are performed: Lock in a specific range of RowIDs.
[0115] Generate a merge insert (INSERT INTO REPLACE) statement.
[0116] Perform the ClickHouse data correction operation to complete the data self-healing.
[0117] The above embodiments ensure that verification is triggered only after data synchronization is completed by watermarking. Combined with dynamic incremental hash tree to build hashes only for active shards, drill down layer by layer to quickly locate the leaf nodes of differences, and merge insertion statements to automatically repair the difference rows, the amount of verification scanning is reduced, false alarms caused by synchronization delay are reduced, and automatic repair of difference data is supported.
[0118] The consistency verification system provided in this application is described below. The consistency verification system described below can be referred to in correspondence with the heterogeneous database consistency verification method described above.
[0119] Figure 5 This is a schematic diagram of the consistency verification system provided in this application. Figure 5 As shown, the consistency verification system includes: The watermark determination module 210 is used to determine a common verification watermark, which is used to characterize the logical time boundary of synchronized data between the source database and the target database. The hash tree construction module 220 is used to construct a source hash tree based on the data set in the source database whose timestamp is no later than the common verification watermark after confirming that the target database has been applied to the data position indicated by the common verification watermark, and to construct a target hash tree based on the data set in the target database whose applied position is no later than the common verification watermark. Both the source hash tree and the target hash tree are constructed based on the same sharding rule and both calculate the hash value of the corresponding leaf node only for active data shards that have undergone data changes in their respective datasets. The leaf nodes of inactive data shards reuse historical hash values. Data acquisition module 230 is used to acquire the root value of the source hash tree of the source database and the root value of the target hash tree of the target database respectively; The data positioning module 240 is used to compare the hash values of child nodes layer by layer from the root node if the root value of the source hash tree is inconsistent with the root value of the target hash tree, and locate the target leaf node with inconsistent hash values. The target leaf node corresponds to a data shard under the sharding rule. The data repair module 250 is used to locate the difference row according to the data shard corresponding to the target leaf node, obtain the latest data of the difference row from the source database, and repair the difference row in the target database according to the latest data.
[0120] In some embodiments, the data acquisition module 230 includes: The set acquisition unit is used to acquire a unified set of shards to be verified. The unified set of shards to be verified is generated by the verifier based on the data change events recorded in the changed data capture stream, and includes at least one data shard that has undergone data change. The database operation unit is configured to perform the following operations on the source database and the target database, respectively, based on the unified set of shards to be verified: For each data shard indicated by the unified set of shards to be verified, if the data shard is an active data shard, the hash value of the leaf node corresponding to the active data shard is calculated; if it is an inactive data shard, the historical hash value of its leaf node is reused. The hash value of the parent node is calculated layer by layer upwards based on the hash value of each leaf node until the hash value of the root node is obtained, thus obtaining the root value of the source hash tree or the root value of the target hash tree.
[0121] In some embodiments, the set acquisition unit is specifically used for: The corresponding dirty fragment bitmap is maintained in the verifier memory. Each bit of the dirty fragment bitmap corresponds to a data fragment. The first value of each bit indicates that the corresponding data fragment has undergone data changes, and the second value of each bit indicates that the corresponding data fragment has not undergone data changes. The verifier listens to the change data capture stream, and when it captures a data change event for any data fragment, it sets the value of the bit corresponding to the data fragment in the dirty fragment bitmap to the first value. When performing consistency checks, the verifier reads the data fragment corresponding to the bit with the first value in the dirty fragment bitmap and uses it as the unified set of fragments to be checked.
[0122] In some embodiments, the database operation unit is specifically used for: For each active data shard, obtain the row hash value of each data row in the active data shard and auxiliary verification information. The auxiliary verification information includes at least one of the following: the total number of data rows in the active data shard, the modulo sum of the row hash values of each data row in the active data shard, and the digest value of the row hash values of each data row in the active data shard. The row hash values of each data row are aggregated by performing an XOR operation in any order to obtain the first aggregated component; A second aggregate component is generated based on the auxiliary verification information; Based on the first aggregated component and the second aggregated component, the leaf node hash value of the active data shard is generated.
[0123] In some embodiments, the incremental maintenance of the leaf node hash value includes: Retrieve the idempotent key and event sequence number of the data change event; Detect whether the data change event has been processed based on the idempotent key and the event sequence number; If the process has already been processed, skip incremental maintenance; otherwise, perform incremental maintenance.
[0124] In some embodiments, performing incremental maintenance includes: When adding a new data row, the hash value of the current leaf node is XORed with the hash value of the new data row to obtain the updated hash value of the leaf node. When deleting a data row, the current leaf node hash value is XORed with the row hash value of the deleted data row to obtain the updated leaf node hash value. When modifying a data row, the XOR result of the row hash value before modification and the row hash value after modification is calculated. The current leaf node hash value is then XORed with the XOR result to obtain the updated leaf node hash value.
[0125] In some embodiments, it also includes: The recalculation unit is used to recalculate the leaf node hash value of the active data shard when performing consistency checks, and to overwrite the incrementally maintained leaf node hash value with the recalculated leaf node hash value.
[0126] In some embodiments, the data positioning module 240 includes: The first acquisition unit is used to acquire the hash values of each child node of the next level by starting from the root node of the source hash tree and the root node of the target hash tree. The first comparison unit is used to compare the hash value of the child node in the source hash tree with the hash value of the child node at the corresponding position in the target hash tree. The first judgment unit is used to determine whether a child node is a leaf node when a child node with an inconsistent hash value is found. The first processing unit is used to proceed to the next level of the child node for comparison if the result is not found. The second processing unit is used to determine the child node as a differential leaf node if the condition is met. The traversal processing unit is used to traverse all subtrees with inconsistent hash values until all differing leaf nodes are located, and the all differing leaf nodes are the target leaf node.
[0127] In some embodiments, the data repair module 250 includes: The data acquisition unit is used to acquire the primary key and row hash value of each data row in the source database and the primary key and row hash value of each data row in the target database within the shard range corresponding to the target leaf node. A hash value comparison unit is used to align data rows in the source database and the target database according to the primary key, and compare the hash values of the rows; The difference row determination unit is used to determine the difference row based on the alignment result and the row hash value comparison result. The difference row includes at least one of the following categories: the first type of difference row that exists at the source end but not at the target end, the second type of difference row that exists at both ends but has different row hash values, and the third type of difference row that does not exist at the source end but exists at the target end. The data repair unit is used to obtain the latest data of the difference rows from the source database and repair the difference rows in the target database based on the latest data.
[0128] In some embodiments, the watermark determination module 210: The event acquisition unit is used to maintain a heartbeat table in the source database, update the heartbeat table at preset time intervals, commit each update as a transaction, and acquire the logical clock TSO corresponding to the transaction. An event sending unit is used to synchronize the update event carrying the TSO to the target database through a change data capture stream; A watermark determination unit is used to use the TSO as the common verification watermark.
[0129] Figure 6 This is a schematic diagram of the physical structure of the electronic device provided in this application. For example... Figure 6 As shown, the electronic device may include a processor 310, a communications interface 320, a memory 330, and a communication bus 340, wherein the processor 310, communications interface 320, and memory 330 communicate with each other via the communication bus 340. The processor 310 can invoke logical instructions from the memory 330 to execute a heterogeneous database consistency verification method. This method includes: determining a common verification watermark, wherein the common verification watermark is used to characterize the logical time boundary of synchronized data between the source database and the target database. After confirming that the target database has been applied to the data position indicated by the common verification watermark, a source hash tree is constructed based on the data set in the source database whose timestamp is no later than the common verification watermark, and a target hash tree is constructed based on the data set in the target database whose applied position is no later than the common verification watermark. Both the source hash tree and the target hash tree are constructed based on the same sharding rule and only calculate the hash value of the corresponding leaf node for active data shards that have undergone data changes in their respective datasets. The leaf nodes of inactive data shards reuse historical hash values. Obtain the root value of the source hash tree of the source database and the root value of the target hash tree of the target database respectively; If the root value of the source hash tree is inconsistent with the root value of the target hash tree, the hash values of the child nodes are compared layer by layer from the root node to locate the target leaf node with inconsistent hash values. The target leaf node corresponds to a data shard under the sharding rule. Based on the data shards corresponding to the target leaf node, locate the difference row, obtain the latest data of the difference row from the source database, and repair the difference row in the target database based on the latest data.
[0130] Furthermore, the logical instructions in the aforementioned memory 330 can be implemented as software functional units and, when sold or used as independent products, can be stored in a computer-readable storage medium. Based on this understanding, the technical solution of this application, in essence, or the part that contributes to the prior art, or a part of the technical solution, can be embodied in the form of a software product. This computer software product is stored in a storage medium and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute all or part of the steps of the methods described in the various embodiments of this application. The aforementioned storage medium includes various media capable of storing program code, such as USB flash drives, portable hard drives, read-only memory (ROM), random access memory (RAM), magnetic disks, or optical disks.
[0131] On the other hand, this application also provides a computer program product, which includes a computer program that can be stored on a non-transitory computer-readable storage medium. When the computer program is executed by a processor, the computer can execute the heterogeneous database consistency verification method provided by the above methods. The method includes: determining a common verification watermark, which is used to characterize the logical time boundary of synchronized data between the source database and the target database. After confirming that the target database has been applied to the data position indicated by the common verification watermark, a source hash tree is constructed based on the data set in the source database whose timestamp is no later than the common verification watermark, and a target hash tree is constructed based on the data set in the target database whose applied position is no later than the common verification watermark. Both the source hash tree and the target hash tree are constructed based on the same sharding rule and only calculate the hash value of the corresponding leaf node for active data shards that have undergone data changes in their respective datasets. The leaf nodes of inactive data shards reuse historical hash values. Obtain the root value of the source hash tree of the source database and the root value of the target hash tree of the target database respectively; If the root value of the source hash tree is inconsistent with the root value of the target hash tree, the hash values of the child nodes are compared layer by layer from the root node to locate the target leaf node with inconsistent hash values. The target leaf node corresponds to a data shard under the sharding rule. Based on the data shards corresponding to the target leaf node, locate the difference row, obtain the latest data of the difference row from the source database, and repair the difference row in the target database based on the latest data.
[0132] In another aspect, this application also provides a non-transitory computer-readable storage medium storing a computer program thereon, which, when executed by a processor, is implemented to perform the heterogeneous database consistency verification method provided by the above methods. The method includes: determining a common verification watermark, wherein the common verification watermark is used to characterize the logical time boundary of synchronized data between the source database and the target database. After confirming that the target database has been applied to the data position indicated by the common verification watermark, a source hash tree is constructed based on the data set in the source database whose timestamp is no later than the common verification watermark, and a target hash tree is constructed based on the data set in the target database whose applied position is no later than the common verification watermark. Both the source hash tree and the target hash tree are constructed based on the same sharding rule and only calculate the hash value of the corresponding leaf node for active data shards that have undergone data changes in their respective datasets. The leaf nodes of inactive data shards reuse historical hash values. Obtain the root value of the source hash tree of the source database and the root value of the target hash tree of the target database respectively; If the root value of the source hash tree is inconsistent with the root value of the target hash tree, the hash values of the child nodes are compared layer by layer from the root node to locate the target leaf node with inconsistent hash values. The target leaf node corresponds to a data shard under the sharding rule. Based on the data shards corresponding to the target leaf node, locate the difference row, obtain the latest data of the difference row from the source database, and repair the difference row in the target database based on the latest data.
[0133] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any creative effort.
[0134] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented by means of software plus necessary general-purpose hardware platforms, and of course, it can also be implemented by hardware. Based on this understanding, the above technical solutions, in essence or the part that contributes to the prior art, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.
[0135] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of this application, and are not intended to limit them. Although this application has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of this application.
Claims
1. A method for verifying consistency in heterogeneous databases, characterized in that, include: A common verification watermark is determined, which is used to characterize the logical time boundary of synchronized data between the source database and the target database. After confirming that the target database has been applied to the data position indicated by the common verification watermark, a source hash tree is constructed based on the data set in the source database whose timestamp is no later than the common verification watermark, and a target hash tree is constructed based on the data set in the target database whose applied position is no later than the common verification watermark. Both the source hash tree and the target hash tree are constructed based on the same sharding rule, and both calculate the hash value of the corresponding leaf node only for active data shards that have undergone data changes in their respective datasets. The leaf nodes of inactive data shards reuse historical hash values. Obtain the root value of the source hash tree in the source database and the root value of the target hash tree in the target database, respectively. If the root value of the source hash tree is inconsistent with the root value of the target hash tree, the hash values of the child nodes are compared layer by layer from the root node to locate the target leaf node with inconsistent hash values. The target leaf node corresponds to a data shard under the sharding rule. Based on the data shards corresponding to the target leaf node, locate the difference row, obtain the latest data of the difference row from the source database, and repair the difference row in the target database based on the latest data.
2. The method according to claim 1, characterized in that, The steps of obtaining the root value of the source hash tree in the source database and the root value of the target hash tree in the target database respectively include: Obtain a unified set of shards to be verified. The unified set of shards to be verified is generated by the verifier based on the data change events recorded in the changed data capture stream, and includes at least one data shard that has undergone data change. Based on the unified set of shards to be verified, the following operations are performed in both the source database and the target database: For each data shard indicated by the unified set of shards to be verified, if the data shard is an active data shard, the hash value of the leaf node corresponding to the active data shard is calculated; if it is an inactive data shard, the historical hash value of its leaf node is reused. The hash value of the parent node is calculated layer by layer upwards based on the hash value of each leaf node until the hash value of the root node is obtained, thus obtaining the root value of the source hash tree or the root value of the target hash tree.
3. The method according to claim 2, characterized in that, The process of obtaining a unified set of shards to be verified includes: The corresponding dirty fragment bitmap is maintained in the verifier memory. Each bit of the dirty fragment bitmap corresponds to a data fragment. The first value of each bit indicates that the corresponding data fragment has undergone data changes, and the second value of each bit indicates that the corresponding data fragment has not undergone data changes. The verifier listens to the change data capture stream, and when it captures a data change event for any data fragment, it sets the value of the bit corresponding to the data fragment in the dirty fragment bitmap to the first value. When performing consistency checks, the verifier reads the data fragment corresponding to the bit with the first value in the dirty fragment bitmap and uses it as the unified set of fragments to be checked.
4. The method according to claim 2, characterized in that, For each data shard indicated by the unified set of shards to be verified, if the data shard is an active data shard, then the hash value of the leaf node corresponding to the active data shard is calculated, including: For each active data shard, obtain the row hash value of each data row in the active data shard and auxiliary verification information. The auxiliary verification information includes at least one of the following: the total number of data rows in the active data shard, the modulo sum of the row hash values of each data row in the active data shard, and the digest value of the row hash values of each data row in the active data shard. The row hash values of each data row are aggregated by performing an XOR operation in any order to obtain the first aggregated component; A second aggregate component is generated based on the auxiliary verification information; Based on the first aggregated component and the second aggregated component, the leaf node hash value of the active data shard is generated.
5. The method according to claim 1, characterized in that, The process of comparing the hash values of child nodes layer by layer from the root node to locate the target leaf node with a different hash value includes: Starting from the root node of the source hash tree and the root node of the target hash tree, obtain the hash values of each child node of the next level layer by layer downwards; The hash values of the child nodes in the source hash tree are compared with the hash values of the child nodes at the corresponding positions in the target hash tree. When a child node with an inconsistent hash value is found, determine whether the child node is a leaf node. If not, proceed to the next level of that child node for further comparison; If so, then the child node is determined as a differential leaf node; Traverse all subtrees with inconsistent hash values until all differing leaf nodes are located, and these differing leaf nodes are the target leaf nodes.
6. The method according to claim 1, characterized in that, The step of locating the difference row based on the data shards corresponding to the target leaf node, obtaining the latest data of the difference row from the source database, and repairing the difference row in the target database based on the latest data includes: Within the sharding range corresponding to the target leaf node, obtain the primary key and row hash value of each data row in the source database, and the primary key and row hash value of each data row in the target database. Align the data rows in the source database and the target database according to the primary key, and compare the row hash values; Based on the alignment results and the row hash value comparison results, the difference rows are determined. The difference rows include at least one of the following: the first type of difference rows that exist at the source end but not at the target end, the second type of difference rows that exist at both ends but have different row hash values, and the third type of difference rows that do not exist at the source end but exist at the target end. The latest data of the differing rows is obtained from the source database, and the differing rows in the target database are repaired based on the latest data.
7. The method according to claim 6, characterized in that, The step of obtaining the latest data of the differing rows from the source database and repairing the differing rows in the target database based on the latest data includes: Based on the type of the differing row, generate the corresponding repair instruction using any of the following methods and send it to the target database for execution: For lines that exist in the source but not in the target, generate an insert instruction; For lines that exist on both ends but have different content, generate an update instruction; For rows that are not present in the source but exist in the target, generate a deletion command.
8. The method according to claim 1, characterized in that, The determination of the common verification watermark includes: A heartbeat table is maintained in the source database. The heartbeat table is updated at preset time intervals. Each update is committed as a transaction, and the logical clock TSO corresponding to the transaction is obtained. The update event carrying the TSO is synchronized to the target database via a change data capture stream; The TSO is used as the common verification watermark.
9. A consistency verification system for heterogeneous databases, characterized in that, include The watermark determination module is used to determine a common verification watermark, which is used to characterize the logical time boundary of synchronized data between the source database and the target database. The hash tree construction module is used to construct a source hash tree based on the data set in the source database whose timestamp is no later than the common verification watermark after confirming that the target database has been applied to the data position indicated by the common verification watermark, and to construct a target hash tree based on the data set in the target database whose applied position is no later than the common verification watermark. Both the source hash tree and the target hash tree are constructed based on the same sharding rule, and both calculate the hash value of the corresponding leaf node only for active data shards that have undergone data changes in their respective datasets. The leaf nodes of inactive data shards reuse historical hash values. The data acquisition module is used to acquire the root value of the source hash tree of the source database and the root value of the target hash tree of the target database, respectively. The data location module is used to compare the hash values of child nodes layer by layer from the root node if the root value of the source hash tree is inconsistent with the root value of the target hash tree, and locate the target leaf node with inconsistent hash values. The target leaf node corresponds to a data shard under the sharding rule. The data repair module is used to locate the difference row according to the data shard corresponding to the target leaf node, obtain the latest data of the difference row from the source database, and repair the difference row in the target database according to the latest data.
10. An electronic device comprising a memory, a processor, and a computer program stored in the memory and running on the processor, characterized in that, When the processor executes the computer program, it implements the heterogeneous database consistency verification method as described in any one of claims 1 to 8.
