Distributed transaction ID generation method and device, electronic equipment and readable medium
By generating distributed transaction IDs through a time synchronization cluster, and combining transaction metadata with a multi-node storage mechanism, the problem of global uniqueness and ordering of transaction IDs in distributed systems is solved, thereby improving the reliability and performance of the system.
Patent Information
- Application Number
- CN202511015479.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-07-22
- Publication Date
- 2025-11-07
AI Technical Summary
Existing technologies for generating distributed transaction IDs in distributed systems struggle to meet the requirements of global uniqueness and ordering, and also pose performance bottlenecks and single-point-of-failure risks.
A time-synchronized cluster is used to generate distributed transaction IDs by providing timestamps and transaction metadata. The master node synchronizes the time to ensure the global uniqueness and order of the timestamps. When the persistence conditions are met, the timestamps are stored on multiple nodes. The leading value is used to ensure global uniformity and monotonically increasing in the event of a single point of failure.
It achieves global uniqueness and ordering of distributed transaction IDs, reduces information query overhead during transaction processes, and improves system reliability and performance.
Smart Images

Figure CN120909709A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present disclosure relates to the technical field of computer, in particular to a distributed transaction ID generation method, a distributed transaction ID generation device, an electronic device and a computer readable storage medium. BACKGROUND
[0002] A distributed transaction refers to a transaction involving multiple nodes in a distributed system. In order to maintain data consistency, a transaction ID (XID) can be used to uniquely identify each distributed transaction, and transaction tracking across services and databases can be based on the transaction ID. Therefore, the transaction ID should meet the requirements of global uniqueness and orderliness, and avoid conflicts in the execution of distributed transactions.
[0003] Currently, a centralized service timestamp or a Snowflake algorithm can be used to generate an XID. The former relies on centralized services, has performance bottlenecks, and is difficult to deal with single point failures. In addition, node information query based on XID is also required in the transaction process, resulting in additional computational overhead. The latter needs to be generated based on local timestamps, relies on global clock synchronization, and is difficult to achieve global monotonic increase, which cannot meet the requirements of global uniqueness and orderliness.
[0004] It should be noted that the information disclosed in the above background section is only used to strengthen the understanding of the background of the present disclosure, and therefore can include information that does not constitute prior art known to those of ordinary skill in the art. SUMMARY
[0005] The purpose of the present disclosure is to provide a distributed transaction ID generation method, a distributed transaction ID generation device, an electronic device and a computer readable storage medium. The distributed transaction ID is generated by the timestamp provided by the time service cluster and the transaction metadata, which can guarantee the global uniqueness and orderliness of the timestamp allocation, and save the information query overhead in the transaction process.
[0006] According to a first aspect of the present disclosure, a distributed transaction ID generation method is provided, which can be applied to a transaction manager, comprising: sending a time service request to a time service cluster when a distributed transaction is started; obtaining a first timestamp provided by the time service cluster in response to the time service request; the time service cluster is time-served by a master node; the first timestamp is greater than a second timestamp and less than an advance value; the second timestamp is the latest timestamp allocated by the master node of the time service cluster in response to the time service request; obtaining transaction metadata of the distributed transaction; generating a distributed transaction ID corresponding to the distributed transaction based on the transaction metadata and the first timestamp; wherein the time service cluster determines the advance value based on a third timestamp and an allocatable duration when the persistence condition is met, and stores the advance value in multiple nodes; the third timestamp is the latest timestamp allocated by the master node of the time service cluster when the persistence condition is met.
[0007] In an example embodiment, the transaction metadata of the distributed transaction is acquired, including: extracting routing information of the distributed transaction; and generating the transaction metadata corresponding to the distributed transaction according to the routing information.
[0008] In an example embodiment, the routing information includes a logical library name and at least one shard name.
[0009] In an example embodiment, the transaction metadata corresponding to the distributed transaction is generated according to the routing information, including: generating the transaction metadata corresponding to the distributed transaction according to the logical library name and any one of the shard names.
[0010] In an example embodiment, the persistence condition includes that a time difference between the advance value and the first timestamp is less than or equal to a persistence threshold; and the persistence threshold is less than the allocatable duration.
[0011] In an example embodiment, the persistence condition includes that a master node switching occurs in the time service cluster.
[0012] In an example embodiment, when the advance value is stored in multiple nodes, the master node in the time service cluster provides the first timestamp in a case where a number of nodes successfully persistently storing the advance value is greater than or equal to a node number threshold.
[0013] In an example embodiment, when the master node switching occurs in the time service cluster, the master node of the time service cluster takes the advance value persistently stored before the master node switching as a second timestamp, determines the advance value based on the second timestamp and the allocatable duration, and stores the advance value in multiple nodes.
[0014] In an example embodiment, when the master node of the time service cluster provides the first timestamp for the first time after the master node switching, the first timestamp is greater than or equal to the second timestamp and less than the advance value; and when the master node of the time service cluster provides the first timestamp for the first time after the master node switching, the first timestamp is greater than the second timestamp and less than the advance value.
[0015] According to a second aspect of the present disclosure, a distributed transaction ID generation apparatus is provided, comprising: a time service request module configured to send a time service request to a time service cluster when a distributed transaction is started; a time service acquisition module configured to acquire a first timestamp provided by the time service cluster in response to the time service request; the time service cluster is time-served by a master node; the first timestamp is greater than a second timestamp and less than an advance value; the second timestamp is a latest timestamp allocated by the master node of the time service cluster in response to the time service request; a data acquisition module configured to acquire transaction metadata of the distributed transaction; an ID generation module configured to generate a distributed transaction ID corresponding to the distributed transaction based on the transaction metadata and the first timestamp; wherein the time service cluster determines the advance value based on a third timestamp and an allocatable time length when a persistence condition is met, and stores the advance value in multiple nodes; the third timestamp is a latest timestamp allocated by the master node of the time service cluster when the persistence condition is met.
[0016] According to a third aspect of the present disclosure, an electronic device is provided, comprising: a processor; and a memory configured to store executable instructions of the processor; wherein the processor is configured to implement the above method by executing the executable instructions.
[0017] According to a fourth aspect of the present disclosure, a computer readable storage medium is provided, which stores a computer program, and the computer program is executed by a processor to implement the above method.
[0018] The present disclosure provides a distributed transaction ID generation method, a distributed transaction ID generation apparatus, an electronic device and a computer readable storage medium. In the method, when a distributed transaction is started, a transaction manager can send a request to a time service cluster, and then acquire a first timestamp provided by the time service cluster in response to the time service request. Further, transaction metadata of the distributed transaction is acquired, and a distributed transaction ID corresponding to the distributed transaction is generated based on the transaction metadata and the first timestamp. The time service cluster is time-served by a master node; the first timestamp is greater than a second timestamp and less than an advance value; the second timestamp is a latest timestamp allocated by the master node of the time service cluster in response to the time service request; the advance value is determined based on a third timestamp and an allocatable time length when a persistence condition is met, and the advance value is stored in multiple nodes; the third timestamp is a latest timestamp allocated by the master node of the time service cluster when the persistence condition is met. In the method, the distributed transaction ID is generated by combining the first timestamp provided by the time service cluster and the transaction metadata of the distributed transaction. Based on the transaction metadata, the distributed transaction is identified, and the overhead of additional query of transaction context in the transaction process is saved. The first timestamp is greater than the second timestamp and less than the advance value, so that the first timestamp is monotonically increasing within the allocatable time length after the second timestamp, and in the case of single point failure, the advance value stored in multiple nodes can be used as a reference to ensure global uniform and monotonically increasing, which meets the requirements of global uniqueness and orderliness.
[0019] It is to be understood that both the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the disclosure. BRIEF DESCRIPTION OF DRAWINGS
[0020] The accompanying drawings, which are incorporated in and form a part of the specification, illustrate one or more embodiments of the present disclosure and, together with the description, further serve to explain the principles of the disclosure. It is to be understood that other drawings can be utilized, and that elements from one drawing can be employed in another without paying extra attention to the specifics.
[0021] Figure 1 One of the step flow charts of the distributed transaction ID generation method in the exemplary embodiments of the present disclosure is schematically shown.
[0022] Figure 2 Another of the step flow charts of the distributed transaction ID generation method in the exemplary embodiments of the present disclosure is schematically shown.
[0023] Figure 3 One of the interaction flow diagrams of the distributed transaction ID generation method according to the exemplary embodiments of the present disclosure is shown.
[0024] Figure 4 Another of the interaction flow diagrams of the distributed transaction ID generation method according to the exemplary embodiments of the present disclosure is shown.
[0025] Figure 5 Another of the interaction flow diagrams of the distributed transaction ID generation method according to the exemplary embodiments of the present disclosure is shown.
[0026] Figure 6 One of the component diagrams of the electronic device to which the distributed transaction ID generation method in the exemplary embodiments of the present disclosure can be applied is shown.
[0027] Figure 7 One of the component diagrams of the electronic device to which the distributed transaction ID generation method in the exemplary embodiments of the present disclosure can be applied is shown. DETAILED DESCRIPTION
[0028] Example implementations will now be described more fully with reference to the accompanying drawings. Example implementations may, however, be implemented in many different forms and should not be construed as limited to the implementations set forth herein; rather, these implementations are provided so that this disclosure will be thorough and complete, and will fully convey the scope of example implementations to those skilled in the art. Features described in the description, structures, or characteristics can be combined in any suitable manner in one or more implementations.
[0029] Furthermore, the accompanying drawings are merely illustrative of this disclosure and are not necessarily drawn to scale. The same reference numerals in the drawings denote the same or similar parts, and therefore repeated descriptions of them will be omitted. Some block diagrams shown in the drawings are functional entities and do not necessarily correspond to physically or logically independent entities. These functional entities may be implemented in software, in one or more hardware modules or integrated circuits, or in different network and / or processor devices and / or microcontroller devices.
[0030] Figure 1 One of the steps of a distributed transaction ID generation method according to an exemplary embodiment of the present disclosure is shown, which can be applied to a transaction manager.
[0031] A Transaction Manager (TM) coordinates transactions among multiple transaction participants (ResourceManagers) in a distributed system. Each transaction participant typically only perceives its local transaction state; in this case, the Transaction Manager controls the logic of the multiple transaction participants to ensure atomicity, consistency, isolation, and durability. After a distributed transaction begins, the Transaction Manager can use a distributed transaction ID for unique identification and global cross-service / database tracing. This embodiment of the disclosure is applied to a scenario where the Transaction Manager generates a corresponding distributed transaction ID when a distributed transaction begins.
[0032] For example, in an e-commerce scenario, transaction participants could include an order database, an inventory database, a payment database, and a logistics database. When a client initiates a distributed transaction to place an order for goods, the transaction manager can generate a corresponding distributed transaction ID to coordinate branch transactions such as order creation, payment balance deduction, order status update, remaining inventory update, and logistics information update among multiple transaction participants.
[0033] like Figure 1 The distributed transaction ID generation method of this disclosure embodiment may include the following steps 101 to 104.
[0034] In step 101, when the distributed transaction starts, a time synchronization request is sent to the time synchronization cluster.
[0035] In this embodiment of the disclosure, when the client initiates a distributed transaction, the transaction manager prepares to generate a corresponding distributed transaction ID. Based on the generation rules of the distributed transaction ID, the transaction manager can request a timestamp from the time synchronization cluster.
[0036] In step 102, a first timestamp provided by a time service cluster in response to a time service request is obtained; the time service cluster is time-served by a master node; the first timestamp is greater than a second timestamp and less than a lookahead value; the second timestamp is a latest timestamp assigned by the master node of the time service cluster in response to the time service request.
[0037] In the embodiments of the present disclosure, the transaction manager can obtain a first timestamp provided by a master node of a time service cluster in response to a time service request. The first timestamp provided by the time service cluster is referenced to a second timestamp and a persisted lookahead value. The second timestamp is a latest timestamp assigned by the master node of the time service cluster in response to the time service request. On this basis, the first timestamp provided by the time service cluster each time is greater than the latest assigned second timestamp and less than the lookahead value, so that the first timestamp is monotonically increasing within the time window range represented by the lookahead value.
[0038] In step 103, transaction metadata of the distributed transaction is obtained.
[0039] In the embodiments of the present disclosure, the transaction metadata refers to data describing the meta-attributes of the distributed transaction, which can be the initiator, name, type, and involved transaction participants of the distributed transaction, and the like. On this basis, the information required by the transaction management or the description of the required information can be taken as the transaction metadata based on the context query requirements in the transaction process.
[0040] In step 104, a distributed transaction ID corresponding to the distributed transaction is generated based on the transaction metadata and the first timestamp.
[0041] In the embodiments of the present disclosure, the transaction manager can combine the first timestamp and the transaction metadata based on the obtained first timestamp and transaction metadata, thereby generating a distributed transaction ID. The distributed transaction ID is unchanged within the entire life cycle of the distributed transaction.
[0042] Further, the time service cluster determines the lookahead value based on a third timestamp and an assignable duration when the persistence condition is met, and stores the lookahead value in multiple nodes; the third timestamp is a latest timestamp assigned by the master node of the time service cluster when the persistence condition is met.
[0043] In the embodiments of the present disclosure, the time service cluster provides time service for the transaction manager, and the time service is performed by the master node (leader) in the cluster. On this basis, the time service cluster can judge the persistence condition to evaluate whether the lookahead value needs to be persisted, and initiate the multi-node storage of the lookahead value in the time service cluster by the master node when the persistence condition is met. The lookahead value is determined based on a third timestamp and an assignable duration, so that the lookahead value represents a time window range of the assignable duration after the third timestamp.
[0044] Exemplarily, in response to the Nth timing request in the timing cluster, the first timestamp newTS1 provided by the master node should be greater than the second timestamp latestTS1 and less than the advance value persistentTS1, which is determined by the third timestamp latestTS2 and the assignable duration advanceTS.
[0045] The latestTS1 is the most recently assigned timestamp by the master node of the timing cluster in response to the timing request. Assuming that the master node of the timing cluster is responding to the Nth timing request, the latestTS1 can be the newTS2 generated by the master node of the timing cluster in response to the (N-1)th timing request.
[0046] The latestTS2 is the most recently assigned timestamp by the master node of the timing cluster when the timing cluster meets the persistence condition. Assuming that the master node of the timing cluster is responding to the Nth timing request, and the master node of the timing cluster meets the persistence condition in response to the (N-3)th timing request, the latestTS2 can be the newTS3 generated by the master node of the timing cluster in response to the (N-3)th timing request. Alternatively, the master node of the timing cluster meets the persistence condition in response to the Nth timing request, the newTS1 generated in response to the Nth timing request is taken as the latestTS2, the advance value persistentTS2 is determined based on the latestTS2 and the advanceTS, and the persistentTS2 is stored in multiple nodes.
[0047] On this basis, in response to the (N+1)th timing request in the timing cluster, the latestTS1 can be the newTS1 generated by the master node of the timing cluster in response to the Nth timing request. The first timestamp newTS4 provided by the master node in the (N+1)th time should be greater than the latestTS1 and less than the persistentTS2.
[0048] In an optional embodiment of the present disclosure, the first timestamp can be a logical time generated by the master node of the timing cluster. The logical time is generated by shifting the physical timestamp left by 18 bits, so that 262144 (2 18 ) concurrent distributed transactions can be supported on the physical timestamp per millisecond, achieving the requirements of high concurrency and low latency transaction management. Those skilled in the art can select and adjust the generation method of the timestamp according to actual needs, and the embodiments of the present disclosure do not make specific limitations thereon.
[0049] Figure 2 A second step flowchart of a distributed transaction ID generation method according to an exemplary embodiment of the present disclosure is shown, which can be applied to a transaction manager. Among them, the transaction manager can correspond to the foregoing Figure 1The related description of the foregoing step 101 is not repeated here.
[0050] As Figure 2 The distributed transaction ID generation method of the embodiments of the present disclosure can include steps 201 to 205.
[0051] In step 201, when the distributed transaction is started, a time service request is sent to a time service cluster.
[0052] In the embodiments of the present disclosure, step 201 can correspond to the related description of the foregoing step 101, and the related description of the foregoing step 101 is not repeated here.
[0053] In step 202, a first timestamp provided by the time service cluster in response to the time service request is obtained; the time service cluster is time-served by a master node; the first timestamp is greater than a second timestamp and less than an advance value; and the second timestamp is the latest timestamp allocated by the master node of the time service cluster in response to the time service request.
[0054] In the embodiments of the present disclosure, step 202 can correspond to the related description of the foregoing step 102, and the related description of the foregoing step 102 is not repeated here.
[0055] In step 203, routing information of the distributed transaction is extracted.
[0056] In the embodiments of the present disclosure, the routing information can be obtained based on the routing information of the distributed transaction. The routing information can be indication information for processing the data node corresponding to the transaction node after the transaction node of the distributed transaction is split. The transaction manager can execute the first statement when the distributed transaction is started, and extract the routing information of the distributed transaction.
[0057] In step 204, transaction metadata corresponding to the distributed transaction is generated according to the routing information.
[0058] In the embodiments of the present disclosure, the transaction manager can generate the transaction metadata of the distributed transaction based on the routing information, so that the nodes of the transaction log can be persisted in the generation of the subsequent distributed transaction ID, and the query amount in the transaction process is reduced, thereby effectively saving the computing resources.
[0059] In an exemplary method embodiment of the present disclosure, the routing information includes a logical library name and at least one shard name.
[0060] In an embodiment of the present disclosure, the routing information can include a logical library name and a shard name. Before this, data in a distributed system is stored in the form of a library shard, and based on the routing information of the first statement, the logical library name and one or more shard names can be included to indicate the participant nodes involved in one or more distributed transactions. The foregoing routing information usually needs to be repeatedly called multiple times in the transaction process, and persisting it as transaction metadata can effectively reduce the additional query calculation overhead in the transaction process.
[0061] In an example method embodiment of the present disclosure, the foregoing step 204 can include the following step A.
[0062] Step A, generating transaction metadata corresponding to the distributed transaction according to the logical library name and any one shard name.
[0063] In an embodiment of the present disclosure, the transaction metadata can be composed of a logical library name and any one shard name, which avoids the problem of too long transaction metadata and complex calculation and processing when there are multiple shard names.
[0064] In step 205, a distributed transaction ID corresponding to the distributed transaction is generated based on the transaction metadata and the first timestamp.
[0065] In an embodiment of the present disclosure, step 205 can correspond to the related description of the foregoing step 104, and details are not repeated here to avoid repetition.
[0066] For example, the distributed transaction ID can be represented as "newTS + logical library name + shard name", where newTS can be represented as "timestamp << 18", that is, the physical timestamp is left shifted by 18 bits.
[0067] Further, when the time service cluster meets the persistence condition, the time service cluster determines the advance value based on the third timestamp and the allocatable duration, and stores the advance value in multiple nodes; the third timestamp is the latest timestamp allocated by the master node of the time service cluster when the time service cluster meets the persistence condition.
[0068] In an embodiment of the present disclosure, the time service cluster can correspond to the related description of the foregoing Figure 1 , and details are not repeated here to avoid repetition.
[0069] Figure 3 Fig. 1 shows one of the interaction flow diagrams of the distributed transaction ID generation method according to an example embodiment of the present disclosure, as shown in Figure 3 , which can be applied to the interaction between the transaction manager and the client, and the time service cluster. It can include the following steps 301 to 306.
[0070] Step 301, the client starts a distributed transaction.
[0071] Step 302, the transaction manager sends a time request to the time cluster when the distributed transaction is started.
[0072] Step 303, the master node in the time cluster generates a first timestamp in response to the time request.
[0073] Step 304, the time cluster provides the first timestamp to the transaction manager.
[0074] Step 305, the transaction manager extracts routing information and generates transaction metadata corresponding to the distributed transaction according to the logical database name and any shard name.
[0075] Step 306, the transaction manager generates a distributed transaction ID according to the first timestamp, the logical database name, and the shard name.
[0076] In an exemplary method embodiment of the present disclosure, the persistence condition includes that the time difference between the advance value and the first timestamp is less than or equal to the persistence threshold, and the persistence threshold is less than the allocatable duration.
[0077] In an embodiment of the present disclosure, the persistence condition can be judged based on the time difference between the advance value and the first timestamp. The time cluster can set a persistence threshold, and when the aforementioned time difference is less than or equal to the persistence threshold, it can be considered that the time window range represented by the advance value has been consumed to a certain extent, so that a new advance value is determined by the third timestamp and the allocatable duration, and the new advance value is stored in multiple nodes. The persistence threshold can be set according to actual needs and calculation conditions. The smaller the persistence threshold is within the time window range, the lower the frequency of updating the advance value is; on the contrary, the larger the persistence threshold is, the higher the frequency of updating the advance value is.
[0078] Wherein, on the basis that the persistence threshold is less than the allocatable duration, when the persistence threshold is 0, it means that the first timestamp is equal to the advance value, which meets the persistence condition, at this time, the advance value is updated when the allocatable duration is completely consumed, which reduces the read-write pressure of persistent storage; when the persistence threshold is greater than 0 but less than the allocatable duration, it means that the first timestamp is less than the advance value, which may meet the persistence condition, at this time, the advance value is updated before the allocatable duration is consumed, which can be persisted in advance, avoids the accumulation of requests caused by the consumption of the allocatable timestamp, and reduces the problem of performance jitter.
[0079] For example, when the persistence threshold is half of the allocatable duration , the aforementioned newTS1 meets the persistence condition, which can be less than or equal to At this time, the newTS1 is taken as the latestTS2, the advance value persistentTS2 is determined based on the latestTS2 and the advanceTS, and the persistentTS2 is stored in multiple nodes.
[0080] In an example method embodiment of the present disclosure, when the advance value is stored in multiple nodes, the master node in the time service cluster provides the first timestamp in a case where the number of nodes that successfully persist the advance value is greater than or equal to the node number threshold.
[0081] In an embodiment of the present disclosure, after the master node of the time service cluster generates the first timestamp, the master node initiates the multi-node storage of the advance value when the time difference between the advance value and the first timestamp meets the persistence condition. At this time, after the multi-node storage of the advance value is successful, the master node of the time service cluster provides the first timestamp to the transaction manager to avoid the failure of the advance value persistence affecting the global uniqueness and the monotonically increasing time service logic; if the multi-node storage of the advance value fails, it can be considered that the time service fails this time, at which time the persistence of the advance value can be reinitiated, or a prompt of the time service failure can be returned to the transaction manager.
[0082] In an embodiment of the present disclosure, when the master node of the time service cluster initiates the multi-node storage of the advance value, the master node can determine whether the multi-node storage of the advance value is successful according to the number of nodes that successfully persist the advance value in all nodes. The node number threshold can be flexibly set considering factors such as storage conditions and disaster recovery needs. For example, the node number threshold can be set to 50% of all nodes, and the master node of the time service cluster determines that the multi-node storage of the advance value is successful when more than half of the nodes successfully persist the advance value; the node number threshold can also be set to 40% or 60% of all nodes, and the present embodiment does not make a specific limitation on this.
[0083] Figure 4 Fig. 2 shows an interaction flow diagram of a distributed transaction ID generation method according to an example embodiment of the present disclosure, which can be applied to the interaction between the transaction manager and the client and the time service cluster. The time service cluster can include time service node 1, time service node 2, time service node 3, and the like, and the time service node 1 is the master node. The method can include the following steps 401 to 409. Figure 4
[0084] Step 401: The client starts a distributed transaction.
[0085] Step 402: The transaction manager sends a time service request to the time service cluster when the distributed transaction is started.
[0086] Step 403: The time service node 1 generates a first timestamp in response to the time service request.
[0087] Step 404, the time service node 1 performs a persistent condition determination based on the first timestamp and the persisted advance value, and performs step 405 if the persistent condition is met (Y); or performs step 407 if the persistent condition is not met (N).
[0088] Step 405, the time service node 1 determines the advance value with the first timestamp as the third timestamp and the allocatable time length, and initiates the time service node 2, the time service node 3, and the like to persist the advance value.
[0089] Step 406, the time service node 1 determines, based on the feedback of the time service node 2, the time service node 3, and the like, that the number of nodes successfully persisting the advance value is greater than or equal to the node number threshold.
[0090] Step 407, the time service node 1 provides the first timestamp to the transaction manager.
[0091] Step 408, the transaction manager extracts routing information, and generates transaction metadata corresponding to the distributed transaction according to the logical database name and any one shard name.
[0092] Step 409, the transaction manager generates a distributed transaction ID with the first timestamp, the logical database name, and the shard name.
[0093] In an exemplary method embodiment of the present disclosure, the persistent condition includes occurrence of master node switching in the time service cluster.
[0094] In an embodiment of the present disclosure, when the time service cluster is provided with time service by the master node, the master node may be switched due to single point failure or other reasons, at which time the switched master node can be persisted to normally provide subsequent time service. In order to maintain the monotonic increase of time service, the time service cluster can refer to the advance value that has been persisted before the occurrence of the master node switching.
[0095] In an exemplary method embodiment of the present disclosure, in the case of occurrence of master node switching in the time service cluster, the master node of the time service cluster takes the advance value that has been persisted before the occurrence of the master node switching as the second timestamp, and determines the advance value based on the second timestamp and the allocatable time length, and stores the advance value in multiple nodes.
[0096] In the embodiments of the present disclosure, when the master node of the time service cluster after the switch has not started to respond to the time service request, the advanced value stored persistently before the master node switch can be taken as the second timestamp, and the advanced value is determined based on the second timestamp and the assignable time length, and the advanced value is stored in multiple nodes. Based on the foregoing time service logic, when the master node before the switch generates the first timestamp, it is made smaller than the advanced value stored persistently, so that when the advanced value is determined and stored in multiple nodes after the master node switch, the second timestamp is taken as the advanced value stored persistively, which ensures that the new master node performs the first timestamp allocation within the time window represented by the advanced value, which is globally unique and monotonically increasing. The specific determination of the advanced value and the storage in multiple nodes can refer to the foregoing related description. To avoid repetition, it will not be described here.
[0097] In an exemplary method embodiment, when the master node of the time service cluster provides the first timestamp for the first time after the master node switch, the first timestamp is greater than or equal to the second timestamp and smaller than the advanced value; when the master node of the time service cluster provides the first timestamp for the first time after the master node switch, the first timestamp is greater than the second timestamp and smaller than the advanced value.
[0098] In the embodiments of the present disclosure, when the master node of the time service cluster provides the first timestamp for the first time after the master node switch, since the second timestamp is the advanced value stored persistently before the master node switch, and the master node before the switch should also make the last generated timestamp smaller than the advanced value stored persistently, the first timestamp can be greater than or equal to the second timestamp while being smaller than the advanced value, maintaining its monotonically increasing feature relative to the last allocated timestamp of the master node before the switch; when the master node of the time service cluster provides the first timestamp for the first time after the master node switch, since the second timestamp is the last provided timestamp of the master node, the first timestamp should be greater than the second timestamp while being smaller than the advanced value, to maintain the monotonically increasing feature.
[0099] Figure 5 Fig. 3 shows a third interaction flow diagram of a distributed transaction ID generation method according to an exemplary embodiment of the present disclosure, which can be applied to the interaction between the transaction manager and the client, and the time service cluster, which can include time service node 1, time service node 2, time service node 3, and the like. The master node is switched from time service node 1 to time service node 2. The following steps 501 to 509 can be included. Figure 5
[0100] Step 501, after time service node 2 is switched to the master node, the advanced value stored persistently before the master node switch is read.
[0101] Step 502, the time service node 2 determines the advance value based on the second timestamp and the assignable time length, and initiates the persistent storage of the advance value to the time service node 1, the time service node 3, and the like.
[0102] Step 503, the time service node 2 determines that the number of nodes successfully persisting the advance value is greater than or equal to the node number threshold based on the feedback of the time service node 1, the time service node 3, and the like.
[0103] Step 504, the client starts the distributed transaction.
[0104] Step 505, the transaction manager sends a time service request to the time service cluster when the distributed transaction is started.
[0105] Step 506, the time service node 2 generates a first timestamp in response to the time service request, and makes the first timestamp greater than or equal to the second timestamp and less than the advance value.
[0106] Step 507, the time service node 2 provides the first timestamp to the transaction manager.
[0107] Step 508, the transaction manager extracts routing information and generates transaction metadata corresponding to the distributed transaction according to the logical database name and any one of the shard names.
[0108] Step 509, the transaction manager generates a distributed transaction ID with the first timestamp, the logical database name, and the shard name.
[0109] The embodiment of the present disclosure provides a distributed transaction ID generation method, in which a transaction manager can send a request to a time service cluster when a distributed transaction is started, and then obtain a first timestamp provided by the time service cluster in response to the time service request; further, transaction metadata of the distributed transaction is obtained, and a distributed transaction ID corresponding to the distributed transaction is generated based on the transaction metadata and the first timestamp. Wherein, the time service cluster is time-served by a master node; the first timestamp is greater than a second timestamp and less than an advance value; the second timestamp is the latest timestamp allocated by the master node of the time service cluster in response to the time service request; the advance value is determined based on a third timestamp and an allocatable duration when a persistence condition is met, and the advance value is stored by multiple nodes; the third timestamp is the latest timestamp allocated by the master node of the time service cluster when the persistence condition is met. In the method, the distributed transaction ID is generated by combining the first timestamp provided by the time service cluster and the transaction metadata of the distributed transaction, which not only identifies the distributed transaction, but also saves the overhead of additional query transaction context in the transaction process; the first timestamp is greater than the second timestamp and less than the advance value, so that the first timestamp is monotonically increasing within the allocatable duration after the second timestamp, and can be used as a reference to ensure global uniform and monotonically increasing when a single point failure occurs, meeting the requirements of global uniqueness and orderliness.
[0110] It should be noted that the above-described figures are only schematic representations of the processes included in the method according to the exemplary embodiments of the present disclosure, and are not intended for limiting purposes. It is easy to understand that the processes shown in the above-described figures do not indicate or limit the time sequence of these processes. In addition, it is also easy to understand that these processes can be executed synchronously or asynchronously, for example, in multiple modules.
[0111] Further, with reference to Figure 6 In the exemplary implementation of the present disclosure, a distributed transaction ID generation apparatus 600 is provided, which can be applied to a transaction manager. The apparatus can include: a time service request module 601, configured to send a time service request to a time service cluster when a distributed transaction is started; a time service acquisition module 602, configured to obtain a first timestamp provided by the time service cluster in response to the time service request; the time service cluster is time-served by a master node; the first timestamp is greater than a second timestamp and less than an advance value; the second timestamp is the latest timestamp allocated by the master node of the time service cluster in response to the time service request; a data acquisition module 603, configured to obtain transaction metadata of the distributed transaction; an ID generation module 604, configured to generate a distributed transaction ID corresponding to the distributed transaction based on the transaction metadata and the first timestamp; wherein the time service cluster determines the advance value based on a third timestamp and an allocatable duration when a persistence condition is met, and stores the advance value by multiple nodes; the third timestamp is the latest timestamp allocated by the master node of the time service cluster when the persistence condition is met.
[0112] In an exemplary apparatus embodiment, the data obtaining module 603 is specifically configured to extract routing information of the distributed transaction; and generate transaction metadata corresponding to the distributed transaction according to the routing information.
[0113] In an exemplary apparatus embodiment, the routing information comprises a logical library name and at least one shard name.
[0114] In an exemplary apparatus embodiment, the data obtaining module 603 is specifically configured to generate the transaction metadata corresponding to the distributed transaction according to the logical library name and any one of the shard names.
[0115] In an exemplary apparatus embodiment, the persistence condition comprises that a time difference between the advance value and the first timestamp is less than or equal to a persistence threshold value; and the persistence threshold value is less than the allocatable time length.
[0116] In an exemplary apparatus embodiment, the persistence condition comprises that a master node switching occurs in the time service cluster.
[0117] In an exemplary apparatus embodiment, when the advance value is stored in multiple nodes, the master node in the time service cluster provides the first timestamp in a case where a number of nodes successfully storing the advance value is greater than or equal to a node number threshold value.
[0118] In an exemplary apparatus embodiment, when the master node switching occurs in the time service cluster, the master node of the time service cluster takes an advance value stored persistently before the master node switching as a second timestamp, determines the advance value based on the second timestamp and the allocatable time length, and stores the advance value in multiple nodes.
[0119] In an exemplary apparatus embodiment, when the master node of the time service cluster provides the first timestamp for the first time after the master node switching, the first timestamp is greater than or equal to the second timestamp and less than the advance value; and when the master node of the time service cluster provides the first timestamp for the first time after the master node switching, the first timestamp is greater than the second timestamp and less than the advance value.
[0120] The distributed transaction ID generation apparatus provided by the embodiments of the present disclosure can send a request to a time service cluster when a distributed transaction is started, and then obtain a first timestamp provided by the time service cluster in response to the time service request. Further, transaction metadata of the distributed transaction is obtained, and a distributed transaction ID corresponding to the distributed transaction is generated based on the transaction metadata and the first timestamp. The time service cluster is time-served by a master node. The first timestamp is greater than a second timestamp and less than an advanced value. The second timestamp is the latest timestamp allocated by the master node of the time service cluster in response to the time service request. The advanced value is determined based on a third timestamp and an allocatable duration when a persistence condition is met, and the advanced value is stored by multiple nodes. The third timestamp is the latest timestamp allocated by the master node of the time service cluster when the persistence condition is met. In the method, the distributed transaction ID is generated by combining the first timestamp provided by the time service cluster and the transaction metadata of the distributed transaction. Based on the transaction metadata, the distributed transaction is identified, and the overhead of additional query of transaction context in the transaction process is saved. The first timestamp is greater than the second timestamp and less than the advanced value, so that the first timestamp is monotonically increasing within the allocatable duration after the second timestamp, and the advanced value stored by multiple nodes can be used as a reference to ensure global uniform and monotonically increasing in the case of single point failure, thereby meeting the requirements of global uniqueness and orderliness.
[0121] The specific details of the modules in the distributed transaction ID generation apparatus have been described in detail in the method part embodiments, and the undisclosed details can be referred to the embodiment content of the method part. That is, the explanation and advantages of the distributed transaction ID generation method of the above embodiments are also applicable to the distributed transaction ID generation apparatus 600 of the embodiments of the present disclosure, and will not be described in detail here.
[0122] Those skilled in the art can understand that each aspect of the present disclosure can be implemented as a system, a method or a program product. Therefore, each aspect of the present disclosure can be embodied as a whole hardware embodiment, a whole software embodiment (including firmware, microcode, etc.), or an embodiment combining hardware and software aspects, which can be collectively referred to as "circuitry", "module" or "system" here.
[0123] The electronic device 700 according to the embodiments of the present disclosure will be described below with reference to Figure 7 . Figure 7 The electronic device 700 shown is merely an example, and should not impose any limitation on the functions and use range of the embodiments of the present disclosure.
[0124] As Figure 7As shown, the electronic device 700 is in the form of a general-purpose computing device. The components of electronic device 700 can include, but are not limited to, the at least one processing unit 710, the at least one memory unit 720, a bus 730 that connects the various system components, including the memory unit 720 and the processing unit 710, and a display unit 740.
[0125] The memory unit stores program code that can be executed by the processing unit 710 such that the processing unit 710 performs the steps described in the above "Exemplary Methods" section in accordance with various exemplary embodiments of the present disclosure.
[0126] The memory unit 720 can include a read-only memory (ROM) 723 in the form of a non-volatile memory unit, as well as a random access memory (RAM) 721 and / or a cache memory 722 in the form of a volatile memory unit that can be used by the processing unit 710.
[0127] The memory unit 720 can also include a program / utility 724 having a set of program modules 725, including but not limited to an operating system, one or more application programs, other program modules, and program data, each of which can govern interactions with a network environment.
[0128] The bus 730 can represent one or more of several types of bus structures, including a memory bus or memory controller, a peripheral bus, a graphics acceleration bus, a processor or local bus using any of a variety of bus architectures, and the like.
[0129] The electronic device 700 can also communicate with one or more external devices 800 such as a keyboard or pointing device, a Bluetooth device, etc.; other devices such as printers, scanners, etc.; and / or various types of networks including a local area network (LAN), a wide area network (WAN), and / or the Internet. The communication can be facilitated by way of an Input / Output (I / O) interface 750. In addition, the electronic device 700 can communicate with one or more devices that enable user interaction with the electronic device 700, and / or one or more devices that enable communication of the electronic device 700 with one or more other computing devices. Such communication can occur via an I / O interface 750. Still yet, the electronic device 700 can communicate with one or more networks such as a local area network (LAN), a wide area network (WAN), and / or the Internet through a network adapter 760. As depicted, the network adapter 760 communicates with the other components of the electronic device 700 via the bus 730. It should be appreciated that the electronic device 700 can be a part of another device or be a stand-alone device, and can not necessarily have all of the components described it. For example, a cellular telephone can not include a local storage unit 720 and / or a display unit 740.
[0130] In addition, the exemplary embodiments of the present disclosure also provide a computer readable storage medium, which stores a program product capable of implementing the method described above. In some possible embodiments, various aspects of the present disclosure can also be implemented in the form of a program product, which includes program codes for causing a terminal device to perform the steps described in the "Exemplary Method" section above according to various exemplary embodiments of the present disclosure when the program product is run on the terminal device.
[0131] It should be noted that the computer readable medium shown in the present disclosure can be a computer readable signal medium or a computer readable storage medium or any combination of the two. The computer readable storage medium may, for example, but is not limited to, an electrical, magnetic, optical, electromagnetic, infrared, or semiconductor system, device or apparatus, or any combination of the above. More specific examples of the computer readable storage medium can include, but are not limited to, an electrical connection having one or more wires, a portable computer diskette, 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.
[0132] In the present disclosure, the computer readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, device or apparatus. In the present disclosure, the computer readable signal medium can include a data signal carried in a baseband or as a part of a carrier wave, which carries computer readable program codes. Such a propagated data signal can take various forms, including but not limited to an electromagnetic signal, an optical signal or any suitable combination of the above. The computer readable signal medium can also be any computer readable medium other than the computer readable storage medium, which can send, propagate or transmit the program for use by or in conjunction with an instruction execution system, device or apparatus. The program codes contained on the computer readable medium can be transmitted by any suitable medium, including but not limited to wireless, wire, optical cable, RF, etc., or any suitable combination of the above.
[0133] Furthermore, the program code can be written in any combination of one or more programming languages, including an object oriented programming language such as Java, C++ or the like, and conventional procedural programming languages, such as the "C" programming language or similar programming languages. The program code can execute entirely on the user's computing device, partly on the user's computing device, as a stand-alone software package, partly on the user's computing device and partly on a remote computing device or entirely on the remote computing device or server. In the latter scenario, the remote computing device can be connected to the user's computing device through any type of network, including a local area network (LAN) or a wide area network (WAN), or the connection can be made to an external computing device, such as through the Internet using an Internet Service Provider.
[0134] Other embodiments of the disclosure will be apparent to those skilled in the art from consideration of the specification and practice of the features disclosed herein. It is intended that the specification and examples be considered as exemplary only, with the true scope and spirit of the disclosure being indicated by the following claims.
Claims
1. A distributed transaction ID generation method, characterized by, The method is applied to a transaction manager, and comprises: sending a time service request to a time service cluster when a distributed transaction is started; obtaining a first timestamp provided by the time service cluster in response to the time service request; the time service cluster is time-served by a master node; the first timestamp is greater than a second timestamp and less than an advance value; the second timestamp is a latest timestamp allocated by the master node of the time service cluster in response to the time service request; obtaining transaction metadata of the distributed transaction; generating a distributed transaction ID corresponding to the distributed transaction based on the transaction metadata and the first timestamp; wherein the time service cluster determines the advance value based on a third timestamp and an allocatable time length when a persistence condition is met, and stores the advance value in multiple nodes; the third timestamp is a latest timestamp allocated by the master node of the time service cluster when the persistence condition is met.
2. The method of claim 1, wherein, The obtaining of the transaction metadata of the distributed transaction comprises: extracting routing information of the distributed transaction; generating the transaction metadata corresponding to the distributed transaction according to the routing information.
3. The method of claim 2, wherein, The routing information comprises a logical library name and at least one shard name.
4. The method of claim 3, wherein, The generation of the transaction metadata corresponding to the distributed transaction according to the routing information comprises: generating the transaction metadata corresponding to the distributed transaction according to the logical library name and any one of the shard names.
5. The method according to any one of claims 1 to 4, characterized in that, The persistence condition comprises at least one of the following: a time difference between the advance value and the first timestamp is less than or equal to a persistence threshold, and the persistence threshold is less than the allocatable time length; a master node switching occurs in the time service cluster.
6. The method of claim 5, wherein, When the advance value is stored in multiple nodes, the master node of the time service cluster provides the first timestamp in a case where a number of nodes in which the advance value is successfully and persistently stored is greater than or equal to a node number threshold.
7. The method of claim 5, wherein, In a case where the master node switching occurs in the time service cluster, the master node of the time service cluster takes an advance value persistently stored before the master node switching as the second timestamp, determines an advance value based on the second timestamp and the allocatable time length, and stores the advance value in multiple nodes.
8. The method of claim 7, wherein, When the master node of the time service cluster provides the first timestamp for the first time after the master node switching, the first timestamp is greater than or equal to the second timestamp and less than the advance value; When the master node of the time service cluster provides the first timestamp for the first time after the master node switching, the first timestamp is greater than the second timestamp and less than the advance value.
9. A distributed transaction ID generation device, characterized in that, The device is applied to a transaction manager, and comprises: a time service request module configured to send a time service request to a time service cluster when a distributed transaction is started; a time service obtaining module configured to obtain a first timestamp provided by the time service cluster in response to the time service request; the time service cluster is time-served by a master node; the first timestamp is greater than a second timestamp and less than an advance value; the second timestamp is a latest timestamp allocated by the master node of the time service cluster in response to the time service request; a data obtaining module configured to obtain transaction metadata of the distributed transaction; a generating module configured to generate a distributed transaction ID corresponding to the distributed transaction based on the transaction metadata and the first timestamp. An ID generation module configured to generate a distributed transaction ID corresponding to the distributed transaction based on the transaction metadata and the first timestamp; The time service cluster determines an advance value based on a third timestamp and a distributable time length when the persistence condition is met, and stores the advance value in multiple nodes; the third timestamp is the latest timestamp allocated by a master node of the time service cluster when the persistence condition is met.
10. An electronic device, comprising: Comprising: a processor; and a memory for storing executable instructions of the processor; wherein the processor is configured to execute the method of any one of claims 1 to 8 via executing the executable instructions.
11. A computer readable storage medium having stored thereon a computer program, characterized in that, The computer program, when executed by a processor, implements the method of any one of claims 1 to 8.