Distributed lock processing method and device

By locking shared resources in the first stage of distributed transactions and releasing locks in the second stage, the data inconsistency caused by uncertainty in reserved resource state in the TCC mode is solved, and data consistency and simplified locks are achieved manage.

CN119066078AActive Publication Date: 2024-12-03CLP JINXIN SOFTWARE (SHANGHAI CO LTD

Patent Information

Application Number
CN202411105400.9
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2024-08-12
Publication Date
2024-12-03
Estimated Expiration
2044-08-12

AI Technical Summary

Technical Problem

In the distributed transaction scenario of TCC model, the state of reserved resources is not determined between the Try stage and the Confirm or Cancel stage, resulting in concurrent transactions that may make decisions based on inaccurate balance information, resulting in data inconsistency and funding problems .

Method used

By locking shared resources in the first-stage business processing and releasing the lock uniformly after the second-stage business processing is completed, we ensure that the lock release is consistent with the second-stage business processing state, avoid deadlocks and simplify lock management.

Benefits of technology

Ensure that the balance of the same account will not be modified by multiple transactions at the same time, ensure the consistency of data, avoid funding problems caused by inaccurate balances, and support the needs of multi-debit and multi-loan scenarios, simplifying the management and maintenance of locks.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN119066078A_ABST
    Figure CN119066078A_ABST
Patent Text Reader

Abstract

The invention provides a distributed lock processing method and device, and the method comprises the steps: responding to a transaction request sent by a transaction center, opening a branch transaction, and opening a local transaction for the database operation of a distributed service node in the branch transaction; first-stage business processing is executed in the local transaction, and in the execution process of the first-stage business processing, shared resources involved in the first-stage business processing are locked; in response to triggering of the second-stage business processing, after the second-stage business processing is completed, lock release of the shared resources is performed, and the transaction of lock release of the shared resources and the transaction of the second-stage business processing are submitted together to ensure that the lock release state is consistent with the second-stage business processing state. According to the invention, the balance of the same account can be ensured not to be modified by a plurality of transactions at the same time in concurrent transactions, the data consistency is ensured, and the fund problem caused by inaccurate balance is avoided.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present disclosure relates to the field of computer technology, and in particular to a distributed lock processing method and device. Background Art

[0002] In the distributed transaction scenario of TCC (Try-Confirm-Cancel) mode, a two-phase asynchronous execution scheme is currently adopted. In the first phase of TCC (Try phase), the system will try to execute business operations and reserve necessary resources (such as frozen amounts). However, the status of these reserved resources is uncertain until the second phase (Confirm or Cancel phase) is completed. This is because the Try phase is only part of the distributed transaction, which is mainly responsible for checking business conditions, performing preliminary operations and reserving resources. However, the entire transaction is not completed until the Confirm or Cancel phase is executed. Between these two phases, the status of the transaction is incomplete, so the reserved resources are also in an undetermined state. If other concurrent transactions occur during this period and rely on the queried balance to trigger subsequent business processes, then due to the uncertainty of the reserved resource status, these concurrent transactions may make decisions based on inaccurate balance information. This may lead to data inconsistency, such as over-allocation, under-collection, and errors in entrusted loan details in the fund pool, affecting the stability and reliability of the entire system. Summary of the invention

[0003] The present disclosure provides a distributed lock processing method and device to solve one of the technical problems in the related art at least to a certain extent. The technical solution of the present disclosure is as follows:

[0004] According to a first aspect of an embodiment of the present disclosure, a distributed lock processing method is provided, comprising: in response to receiving a transaction request sent by a transaction center, opening a branch transaction and opening a local transaction for a database operation of the distributed service node in the branch transaction; executing a first-stage business processing in the local transaction, and during the execution of the first-stage business processing, locking the shared resources involved in the first-stage business processing; in response to triggering a second-stage business processing, after the second-stage business processing is completed, releasing the lock of the shared resource, and submitting the transaction for releasing the lock of the shared resource together with the transaction for the second-stage business processing to ensure that the release of the lock is consistent with the state of the second-stage business processing.

[0005] According to a second aspect of an embodiment of the present disclosure, a distributed lock processing device is provided, comprising: a first processing module, for opening a branch transaction in response to receiving a transaction request sent by a transaction center and opening a local transaction for a database operation of the distributed service node in the branch transaction; a second processing module, for executing a first-stage business processing in the local transaction, and during the execution of the first-stage business processing, locking the shared resources involved in the first-stage business processing; a first lock releasing module, for releasing the lock of the shared resource in response to triggering a second-stage business processing, after the second-stage business processing is completed, and submitting the transaction for releasing the lock of the shared resource together with the transaction for the second-stage business processing to ensure that the release of the lock is consistent with the state of the second-stage business processing.

[0006] According to a third aspect of an embodiment of the present disclosure, an electronic device is provided, comprising: a processor; and a memory for storing instructions executable by the processor; wherein the processor is configured to execute the instructions to implement a distributed lock processing method as described in the embodiment of the first aspect of the present disclosure.

[0007] According to a fourth aspect of an embodiment of the present disclosure, a computer-readable storage medium is provided. When instructions in the computer-readable storage medium are executed by a processor of an electronic device, the electronic device can execute the distributed lock processing method as described in the embodiment of the first aspect of the present disclosure.

[0008] According to a fifth aspect of an embodiment of the present disclosure, a computer program product is provided, including: a computer program, which, when executed by a processor, implements the distributed lock processing method as described in the embodiment of the first aspect of the present disclosure.

[0009] The technical solution provided by the embodiments of the present disclosure brings at least the following beneficial effects:

[0010] In the technical scheme, in response to receiving a transaction request sent by a transaction center, a branch transaction is opened and a local transaction is opened for the database operation of the distributed service node in the branch transaction; a first-stage business processing is executed in the local transaction, and during the execution of the first-stage business processing, the shared resources involved in the first-stage business processing are locked; in response to triggering the second-stage business processing, after the second-stage business processing is completed, the lock of the shared resource is released, and the transaction for releasing the lock of the shared resource is submitted together with the transaction for the second-stage business processing to ensure that the release of the lock is consistent with the state of the second-stage business processing. Therefore, by locking the shared resource in the first stage, it can be ensured that in concurrent transactions, the balance of the same account will not be modified by multiple transactions at the same time, thereby ensuring data consistency and avoiding funding problems caused by inaccurate balances; wherein, during the locking process, In the process, the present disclosure can support the reentrancy of locks by implementing logic, so that multiple borrowing and lending operations under the same transaction can be executed smoothly without blocking each other, which can meet the needs of multiple borrowing and lending scenarios; in addition, in the present disclosure, the distributed service node only focuses on locking the shared resources involved in the first-stage business processing during the execution of the first-stage business processing, without paying attention to the release of the lock. The release of the lock depends on the global transaction manager to trigger the second-stage business processing, and releases the lock uniformly after the second-stage business processing is completed. Among them, the locking of the first stage and the releasing of the lock of the second stage are not in the same thread, which simplifies the management and maintenance of the lock; in addition, in the present disclosure, by releasing the lock after the second-stage business processing is completed and submitting the transaction of releasing the lock together with the transaction of the second-stage business processing, these two points can effectively ensure that the release of the lock is consistent with the status of the second-stage business processing.

[0011] It is to be understood that the foregoing general description and the following detailed description are exemplary and explanatory only and are not restrictive of the present disclosure. BRIEF DESCRIPTION OF THE DRAWINGS

[0012] The drawings herein are incorporated into and constitute a part of the specification, illustrate embodiments consistent with the present disclosure, and together with the description are used to explain the principles of the present disclosure, and do not constitute improper limitations on the present disclosure.

[0013] Figure 1 is a flowchart of a distributed lock processing method shown in the first embodiment of the present disclosure;

[0014] Figure 2 is a flow chart of a distributed lock processing method shown in the second embodiment of the present disclosure;

[0015] Figure 3 is a flowchart of a locking process shown in the third embodiment of the present disclosure;

[0016] Figure 4is a flowchart of a distributed lock processing method shown in the fourth embodiment of the present disclosure;

[0017] Figure 5 is a flowchart of a distributed lock processing method shown in the fifth embodiment of the present disclosure;

[0018] Figure 6 A schematic diagram of the principle of processing a distributed lock provided in the sixth embodiment of the present disclosure;

[0019] Figure 7 A schematic diagram of a distributed lock processing scenario provided in the seventh embodiment of the present disclosure;

[0020] Figure 8 is a structural diagram of a distributed lock processing device shown in the eighth embodiment of the present disclosure;

[0021] Fig. 9 It is a schematic diagram of the structure of an electronic device shown in an exemplary embodiment of the present disclosure. DETAILED DESCRIPTION

[0022] In order to enable ordinary persons in the art to better understand the technical solutions of the present disclosure, the technical solutions in the embodiments of the present disclosure will be clearly and completely described below in conjunction with the accompanying drawings.

[0023] It should be noted that the terms "first", "second", etc. in the specification and claims of the present disclosure and the above-mentioned drawings are used to distinguish similar objects, and are not necessarily used to describe a specific order or sequence. It should be understood that the data used in this way can be interchanged where appropriate, so that the embodiments of the present disclosure described herein can be implemented in an order other than those illustrated or described herein. The embodiments described in the following exemplary embodiments do not represent all embodiments consistent with the present disclosure. Instead, they are merely examples of devices and methods consistent with some aspects of the present disclosure as detailed in the appended claims.

[0024] It should be noted that in the technical solution of the present disclosure, the collection, storage, use, processing, transmission, provision and disclosure of user personal information are all carried out with the user's consent, and are in compliance with the relevant laws and regulations and do not violate public order and good morals.

[0025] In order to ensure that fund pool related transactions can be processed in parallel and reduce the scope of accounts affected by transactions, the present disclosure proposes a distributed lock processing method and device, which locks shared resources in the first stage, supports lock reentrancy during the locking process, and releases the locks uniformly after the second stage of business processing is completed, which ensures data consistency while meeting the needs of multiple borrowing and lending scenarios and simplifies lock management and maintenance.

[0026] The following describes the distributed lock processing method and device according to the embodiments of the present disclosure with reference to the accompanying drawings.

[0027] Figure 1 It is a flowchart of a distributed lock processing method shown in the first embodiment of the present disclosure.

[0028] It should be noted that the distributed lock processing method of the embodiment of the present disclosure is applied to a distributed service node.

[0029] like Figure 1 As shown, the distributed lock processing method includes the following steps:

[0030] Step 101, in response to receiving a transaction request sent by a transaction center, a branch transaction is started and a local transaction is started for a database operation of a distributed service node in the branch transaction.

[0031] The transaction center is used to initiate a distributed transaction that needs to be executed across multiple distributed service nodes. As a possible implementation method, the transaction center can send a transaction request to multiple distributed service nodes to trigger the initiation of a distributed transaction.

[0032] Since distributed transactions need to be executed across multiple distributed service nodes, in order to ensure the atomicity, consistency, isolation and persistence of transactions executed by different distributed service nodes, as a possible implementation method, each distributed service node can respond to the transaction request sent by the transaction center and start one or more branch transactions to manage its own operations in the entire distributed transaction. As a result, the entire distributed transaction is decomposed into multiple branch transactions, and each branch transaction is responsible for executing part of the operations in the corresponding distributed service node.

[0033] For example, assuming that the content of the transaction request is to transfer money from account A to account B, with an amount of X yuan, then the operation of the service node of account A in the entire distributed transaction is: check whether the balance of account A is sufficient, and if it is sufficient, reserve (or freeze) X yuan; the operation of the service node of account B in the entire distributed transaction is: prepare to receive X yuan, but do not increase the balance immediately, and wait for confirmation.

[0034] For example, assuming that the request content of the transaction request is that the user submits an order to purchase product Y, the operation of the inventory service node in the entire distributed transaction is: check the inventory of product Y, and if it is sufficient, reserve a certain amount of inventory; the operation of the order service node in the entire distributed transaction is: create an order record and set the order status to "pending payment"; the operation of the payment service node (if instant payment is involved) in the entire distributed transaction is: wait for the user to complete the payment operation and verify the payment result.

[0035] As an example, a distributed service node can use the API (Application Programming Interface) provided by a distributed transaction framework (such as Seata (Simple Extensible Autonomous Transaction Architecture, simple extensible autonomous distributed transaction management framework), Atomikos, etc.) to start a branch transaction.

[0036] It should be noted that in order to clearly track the status and progress of each participant in a distributed transaction, in the embodiment of the present disclosure, the distributed service node can also register the main flow of branch transactions, that is, record the main flow information of the branch transaction (such as the transaction identifier (transaction ID) of the branch transaction, the service node identifier (service node ID) of the distributed service node, operation type, timestamp, etc.), so that when a failure occurs, the execution status of the transaction can be restored by reading the record of the main flow of the branch transaction, thereby ensuring the reliability and stability of the distributed transaction.

[0037] As an example, the distributed service node may use a log framework (such as Log4j, SLF4J, etc.) to record the main flow information of the branch transaction, or write the main flow information of the branch transaction into a transaction log table.

[0038] As a possible implementation method, after opening a branch transaction, a distributed service node can open one or more local transactions in each branch transaction to execute specific business logic. Among them, the local transaction is at the database or resource manager level and is used to ensure the consistency of data within a single service node.

[0039] As an example, the distributed service node may use the transaction management mechanism provided by the database (such as beginTransaction of JDBC (Java Database Connectivity)) to start a local transaction.

[0040] It is understandable that during the execution of a local transaction, multiple operations or sub-steps may be generated. In order to facilitate more detailed tracking of the execution process of a transaction, in the embodiment of the present disclosure, the distributed service node can also register a branch transaction sub-flow, that is, during the execution of a local transaction, the detailed information of each operation or sub-step is recorded as a branch transaction sub-flow, thereby recording the execution of the transaction at a finer granularity.

[0041] Step 102: execute a phase of business processing in a local transaction, and during the execution of the phase of business processing, lock the shared resources involved in the phase of business processing.

[0042] As a possible implementation method, after starting a local transaction, the distributed service node can execute specific business logic in each local transaction, such as querying the database, updating data, etc.

[0043] It should be noted that, in order to ensure data consistency, as a possible implementation method, the distributed service node can lock the shared resources involved in the first-stage business processing during the execution of the first-stage business processing to prevent other transactions or operations from concurrently changing the shared resources. Therefore, by locking the shared resources in the first stage, it can be ensured that in concurrent transactions, the balance of the same account will not be modified by multiple transactions at the same time, ensuring data consistency and avoiding funding problems caused by inaccurate balances.

[0044] For example, in an e-commerce business scenario, when a user places an order to purchase a product, it is necessary to first check whether the product inventory is sufficient. If sufficient, the inventory is deducted and the order is created. In this process, the inventory is a shared resource and needs to be locked to prevent concurrent changes. Optionally, Redis (Remote Dictionary Server) technology can be used to lock, for example, use the SET key value NX PX milliseconds command to try to lock. Among them, key is the identifier of the lock, value is the unique identifier of the party requesting the lock (such as UUID (Universally Unique Identifier)), which is used to verify the ownership of the lock when unlocking, and milliseconds is the expiration time of the lock. If the command is executed successfully, it means that the lock is successfully obtained, and subsequent business processing (such as inventory deduction) can be safely performed. If the command fails to execute (indicating that the shared resource has been locked with an incompatible lock), you can choose to wait for a period of time and try again, or use other strategies (such as polling, backoff algorithms, etc.) to avoid frequent lock competition.

[0045] Step 103, in response to triggering the second phase business processing, after the second phase business processing is completed, the lock of the shared resource is released, and the transaction of the shared resource lock release is submitted together with the transaction of the second phase business processing to ensure that the lock release is consistent with the state of the second phase business processing.

[0046] Among them, the locking of shared resources and the release of shared resource locks when triggering the second-stage business processing are not in the same thread.

[0047] In TCC mode, if the lock is not properly added in the first phase, a deadlock may occur. For example, suppose there are two distributed service nodes: distributed service node A and distributed service node B, and two shared resources: shared resource R1 and shared resource R2. Distributed service node A needs shared resource R1 and shared resource R2 to complete its transaction, and distributed service node B also needs these two shared resources, but their request order is opposite, that is:

[0048] Operation of distributed service node A:

[0049] Phase 1: Distributed service node A locks shared resource R1 while distributed service node B locks shared resource R2. Later, when distributed service node A tries to lock shared resource R2 for operation, shared resource R2 has been locked by distributed service node B. At this time, distributed service node A will keep the shared resource R1 locked and wait for shared resource R2 to become available.

[0050] Operation of distributed service node B:

[0051] Phase 1: While distributed service node A locks shared resource R1, distributed service node B locks shared resource R2. Later, when distributed service node B tries to lock shared resource R1 for operation, shared resource R1 has been locked by distributed service node A. At this time, distributed service node B will keep the shared resource R2 locked and wait for shared resource R1 to become available.

[0052] Since the distributed service node A and the distributed service node B both hold the lock of the shared resource needed by the other party, and both are waiting for the other party to release the lock of the shared resource held by it, a deadlock is formed.

[0053] In order to avoid deadlock and improve resource utilization, as a possible implementation method, the distributed service node can respond to triggering the second-phase business processing, and after the second-phase business processing is completed, release the lock of the shared resource to facilitate subsequent other transactions to use the shared resource.

[0054] In the present disclosure, the distributed service nodes only focus on locking the shared resources involved in the first-stage business processing during the execution of the first-stage business processing, without paying attention to the release of the locks. The release of the locks depends on the global transaction manager to trigger the second-stage business processing, so that the distributed service nodes respond to the triggering of the second-stage business processing and uniformly release the locks after the second-stage business processing is completed. Among them, the locking of the first stage and the releasing of the locks of the second stage are not in the same thread, which simplifies the management and maintenance of the locks.

[0055] The second-stage business processing includes confirming the execution of the business processing and canceling the execution of the business processing. The second-stage business processing executed by the distributed service node is determined by the global transaction manager according to the first-stage business processing results of multiple distributed service nodes involved in the distributed transaction corresponding to the distributed service node;

[0056] In response to the fact that the first-stage business processing results of each distributed service node involved in the distributed transaction corresponding to the distributed service node are all successfully executed, the global transaction manager triggers confirmation of executing business processing to each distributed service node involved in the distributed transaction corresponding to the distributed service node;

[0057] In response to a first-stage business processing result of any distributed service node involved in the distributed transaction corresponding to the distributed service node being an execution failure, the global transaction manager triggers cancellation of business processing to each distributed service node involved in the distributed transaction corresponding to the distributed service node.

[0058] It should be noted that the normal completion of the first-phase business processing will trigger the second-phase business processing. As a possible implementation method, the distributed service node can release the lock of the shared resource after the second-phase business processing is completed, and submit the transaction of releasing the lock of the shared resource together with the transaction of the second-phase business processing to ensure that the lock release is consistent with the status of the second-phase business processing.

[0059] In summary, on the one hand, the lock release of shared resources will be executed after the second-stage business processing is completed. On the other hand, the lock release of shared resources does not use an independent transaction, but submits the transaction of lock release of shared resources together with the transaction of second-stage business processing. Therefore, when the transaction submission of lock release fails, it will cause business processing exception, which can ensure that the lock release is consistent with the status of second-stage business processing.

[0060] The distributed lock processing method of the embodiment of the present disclosure is as follows: in response to receiving a transaction request sent by a transaction center, a branch transaction is opened and a local transaction is opened for the database operation of the distributed service node in the branch transaction; a first-stage business processing is executed in the local transaction, and during the execution of the first-stage business processing, the shared resources involved in the first-stage business processing are locked; in response to triggering the second-stage business processing, after the second-stage business processing is completed, the lock of the shared resource is released, and the transaction for releasing the lock of the shared resource is submitted together with the transaction for the second-stage business processing to ensure that the release of the lock is consistent with the state of the second-stage business processing. Therefore, by locking the shared resources in the first stage, it can be ensured that in concurrent transactions, the balance of the same account will not be modified by multiple transactions at the same time, thereby ensuring data consistency and avoiding funding problems caused by inaccurate balances.

[0061] In order to clearly explain how the shared resources involved in the first-stage business processing are locked during the execution of the first-stage business processing in the above embodiment, the present disclosure proposes another distributed lock processing method.

[0062] Figure 2 It is a flowchart of a distributed lock processing method shown in the second embodiment of the present disclosure.

[0063] like Figure 2 As shown, the distributed lock processing method includes the following steps:

[0064] Step 201: In response to receiving a transaction request sent by a transaction center, a branch transaction is started and a local transaction is started for a database operation of a distributed service node in the branch transaction.

[0065] Step 202: Execute a phase of business processing in the local transaction.

[0066] Step 203, during the execution of a phase of business processing, the locking interface provided by the distributed lock service is called to send a locking request to the distributed lock service, so that the distributed lock service locks the shared resource according to the resource identifier of the shared resource, the transaction identifier of the branch transaction, the lock type and the locking time carried in the locking request, and based on the locking result, sends the corresponding locking identifier to the distributed service node.

[0067] The lock request is used to request to lock a shared resource, and the lock request may carry information such as the resource identifier of the shared resource, the transaction identifier of the branch transaction, the lock type, and the lock time. For example, the lock type includes a write lock, a read lock, and the like.

[0068] To facilitate the maintenance and development of business systems, as a possible implementation method, distributed service nodes can lock shared resources by interacting with distributed lock services.

[0069] In an embodiment of the present disclosure, a distributed service node can, during the execution of a phase of business processing, call the locking interface provided by the distributed lock service to send a locking request to the distributed lock service, so that the distributed lock service locks the shared resource according to the resource identifier of the shared resource, the transaction identifier of the branch transaction, the lock type and the locking time carried in the locking request, and sends a corresponding locking identifier to the distributed service node based on the locking result.

[0070] In a possible implementation, the lock flag includes a lock success flag and a lock failure flag. When the distributed lock service locks the shared resource according to the resource flag of the shared resource carried in the lock request, the transaction flag of the branch transaction, the lock type, and the lock time, the specific method is as follows: Figure 3 The steps shown are performed:

[0071] Step 2031: The distributed lock service determines whether there is a first lock record in the lock record table whose resource identifier is the same as the resource identifier carried in the lock request.

[0072] In the disclosed embodiment, after receiving a lock request, the distributed lock service can perform a primary key (resource identifier) ​​conflict check, that is, check whether the resource identifier of each lock record in the lock record table is consistent with the resource identifier carried in the lock request. If they are consistent, the corresponding lock record is the first lock record, that is, the lock record that already exists for the shared resource.

[0073] If there is a first lock record, the number of the first lock records may be one or more, which is not limited in the embodiment of the present disclosure.

[0074] Step 2032: If the first lock record does not exist, the distributed lock service adds a second lock record of the branch transaction on the shared resource in the lock record table, and sends a lock success flag to the distributed service node.

[0075] If the primary key does not conflict, that is, the first lock record does not exist in the lock record table, it means that the shared resource is not locked. At this time, the distributed service node can lock the shared resource. Therefore, the distributed lock service can add a new lock record in the lock record table (the second lock record of the branch transaction on the shared resource) to record the locking information of the shared resource by the distributed service node (such as the resource identifier of the shared resource, the transaction identifier of the branch transaction, the lock type, the locking time, etc.).

[0076] In addition, after adding the second lock record, the distributed lock service may send a lock success flag to the distributed service node to inform the distributed service node that the lock is successful.

[0077] Step 2033: If the first lock record exists, the distributed lock service determines whether the lock type in the first lock record is compatible with the lock type carried in the lock request.

[0078] If the primary key conflicts, that is, there is a first lock record in the lock record table, it means that the shared resource has been locked. At this time, the distributed service node can determine whether the lock type in the first lock record is compatible with the lock type carried in the lock request.

[0079] As an example, lock types include write lock, read lock, and exclusive lock. The compatibility of these three locks is as follows: read lock is compatible with read lock (read lock allows the holder to read data, and also allows other read lock holders to read data in parallel), write lock is incompatible with write lock (write lock allows the holder to write data, but does not allow other write lock holders to write data in parallel), write lock is compatible with read lock (write lock allows the holder to write data, and also allows other read lock holders to read data in parallel), and exclusive lock is incompatible with any other lock (exclusive lock only allows the holder to perform operations, and does not allow any other lock holders to perform operations).

[0080] In step 2034, if it is incompatible, the distributed lock service determines whether the first lock record is an expired record according to the locking time in the first lock record.

[0081] If the lock type in the first lock record is incompatible with the lock type carried in the lock request (for example, there is already a write lock, and the current request is also a write lock), at this time, in order to avoid the lock record being in the lock record table due to the inability to release the lock, the distributed service node can then determine whether the first lock record is an expired record. As a possible implementation method, the distributed service node can determine whether the first lock record is an expired record based on the lock time in the first lock record. For example, if the current time is no longer within the range of the lock time in the first lock record, it can be determined that the first lock record is an expired record, otherwise it is the opposite.

[0082] As an example, when the process crashes after the first-stage locking is successful, the lock cannot be released, and the lock record remains in the lock record table.

[0083] It should be noted that the situation of deleting expired records when locking will occur only if and only if the lock record remains in the lock record table because the lock cannot be released.

[0084] Step 2035: If it is an expired record, the distributed lock service first deletes the first lock record in the lock record table, then adds a second lock record in the lock record table, and sends a lock success flag to the distributed service node.

[0085] If the first lock record is an expired record, it means that the shared resource is not locked and used by other transactions, and the shared resource can be locked at this time. Therefore, the distributed lock service can first delete the first lock record in the lock record table, then add the second lock record in the lock record table, and send a lock success flag to the distributed service node to inform the distributed service node that the lock is successful.

[0086] It should be noted that, in order to avoid the situation where the shared resource is still used by the target transaction indicated by the transaction identifier recorded in the locked record even though the lock record is expired, as a possible implementation method, before the distributed lock service deletes the first lock record in the lock record table, it can query the state of the target transaction indicated by the transaction identifier recorded in the first lock record, so as to delete the first lock record in the lock record table when the state of the target transaction is empty. The state of the target transaction is empty, indicating that the target transaction is not being executed.

[0087] In step 2036, if it is not an expired record, the distributed lock service determines that the locking has failed, and sends a locking failure flag to the distributed service node.

[0088] If the first lock record is not an expired record, it means that the shared resource is being locked and used by other transactions, and the shared resource cannot be locked at this time. Therefore, the distributed lock service can determine that the lock has failed and send a lock failure identifier to the distributed service node to inform the distributed service node of the lock failure.

[0089] Step 2037: If compatible, the distributed lock service determines whether the transaction identifier in the first lock record is the same as the transaction identifier carried in the lock request.

[0090] If the lock type in the first lock record is compatible with the lock type carried in the lock request, for example, a read lock already exists and the current request is also a read lock, then the shared resource can be locked because the read lock is compatible with the read lock. Therefore, the distributed lock service can determine whether the lock requested by the lock request is a reentrant lock by determining whether the transaction identifier in the first lock record is the same as the transaction identifier carried in the lock request.

[0091] Step 2038, if they are the same, the distributed lock service determines that the lock requested by the lock request is a re-entry lock, and determines whether it is necessary to update the lock time in the first lock record based on the lock time carried in the lock request, in response to updating the lock time in the first lock record, or, without updating the lock time in the first lock record, sends a lock success flag to the distributed service node.

[0092] If the transaction identifier in the first lock record is the same as the transaction identifier carried in the lock request, it can be determined that the lock requested by the lock request is a reentrant lock. At this time, the lock time of the lock requested by the lock request may be inconsistent with the lock time in the first lock record. Therefore, the distributed lock service needs the lock time carried in the lock request to determine whether it is necessary to update the lock time in the first lock record. For example, when the lock time in the first lock record does not include the lock time carried in the lock request, it is necessary to update the lock time in the first lock record, otherwise it does not need to be updated. Then, the distributed lock service can send a lock success identifier to the distributed service node after updating the lock time in the first lock record, or when there is no need to update the lock time in the first lock record, to inform the distributed service node that the lock is successful.

[0093] Step 2039, if they are not the same, the distributed lock service determines that the lock requested by the lock request is not a reentrant lock, and determines whether the first lock record is an expired record based on the lock time in the first lock record.

[0094] If the transaction identifier of the first lock record is different from the transaction identifier carried in the lock request, it can be determined that the lock requested by the lock request is not a reentrant lock. At this time, in order to avoid the lock record being in the lock record table due to the inability to release the lock, the distributed service node can then determine whether the first lock record is an expired record. As a possible implementation method, the distributed service node can determine whether the first lock record is an expired record based on the lock time in the first lock record. For example, if the current time is no longer within the range of the lock time in the first lock record, it can be determined that the first lock record is an expired record, otherwise it is the opposite.

[0095] Step 2040: If it is an expired record, the distributed lock service first deletes the first lock record in the lock record table, then adds a second lock record in the lock record table, and sends a lock success flag to the distributed service node.

[0096] It should be noted that the execution process of this step can refer to step 2035 and will not be repeated here.

[0097] In step 2041, if it is not an expired record, the distributed lock service adds a second lock record in the lock record table and sends a lock success flag to the distributed service node.

[0098] If the first lock record is not an expired record, it means that the shared resource is being locked and used by other transactions. However, since the lock type in the first lock record is compatible with the lock type carried in the lock request, the shared resource can still be locked at this time. Therefore, the distributed lock service can add a second lock record to the lock record table and send a lock success identifier to the distributed service node to inform the distributed service node that the lock is successful.

[0099] In summary, during the locking process, the reentrancy of the lock can be supported by implementing logic, so that multiple borrowing and lending operations under the same transaction can be executed smoothly without blocking each other, which can meet the needs of multiple borrowing and lending scenarios. In addition, during the locking process, it is also possible to determine whether the existing lock record has expired to avoid the situation where the lock record remains in the lock record table due to the inability to release the lock, which leads to the inability to lock again.

[0100] Step 204, receiving a lock identifier sent by the distributed lock service, and in response to a successful lock, continuing to execute a phase of business processing, or, in response to a failed lock, waiting for a set time to re-lock the shared resource.

[0101] In the disclosed embodiment, the distributed service node can obtain the locking result by receiving the locking identifier sent by the distributed lock service, so that if the locking is successful, the node can continue to execute the first-stage business processing, or if the locking fails, the node can wait for a set time to re-lock the shared resource. The set time can be any time.

[0102] Step 205, in response to triggering the second phase business processing, after the second phase business processing is completed, the lock of the shared resource is released, and the transaction of the shared resource lock release is submitted together with the transaction of the second phase business processing to ensure that the lock release is consistent with the state of the second phase business processing.

[0103] It should be noted that the execution process of steps 201 to 202 and step 205 can be implemented in any way in the embodiments of the present disclosure, and the embodiments of the present disclosure do not limit this and will not be described in detail.

[0104] The distributed lock processing method of the embodiment of the present invention is to call the locking interface provided by the distributed lock service to send a locking request to the distributed lock service during the execution of a phase of business processing, so that the distributed lock service locks the shared resource according to the resource identifier of the shared resource, the transaction identifier of the branch transaction, the lock type and the locking time carried in the locking request, and sends the corresponding locking identifier to the distributed service node based on the locking result; receive the locking identifier sent by the distributed service node and continue to execute the phase of business processing. Therefore, by introducing the distributed lock service, the locking logic is separated from the business logic, and the coupling between the system components is reduced. Moreover, a unified interface is provided by the distributed lock service, which facilitates the centralized management and maintenance of lock resources. In addition, for high-concurrency business scenarios, the number of nodes of the distributed lock service can be increased to meet the high-concurrency requirements.

[0105] In order to clearly explain how to release the lock of the shared resource in the above embodiment, the present disclosure proposes another distributed lock processing method.

[0106] Figure 4 It is a flowchart of a distributed lock processing method shown in the fourth embodiment of the present disclosure.

[0107] like Figure 4 As shown, the distributed lock processing method includes the following steps:

[0108] Step 401, in response to receiving a transaction request sent by a transaction center, a branch transaction is started and a local transaction is started for a database operation of a distributed service node in the branch transaction.

[0109] Step 402: execute a phase of business processing in a local transaction, and during the execution of the phase of business processing, lock the shared resources involved in the phase of business processing.

[0110] Step 403, in response to triggering the second-stage business processing, after the second-stage business processing is completed, a lock release request is sent to the distributed lock service, so that the distributed lock service deletes the lock record of the branch transaction on the shared resource in the lock record table according to the resource identifier of the shared resource and the transaction identifier of the branch transaction carried in the lock release request, and sends a lock release identifier to the distributed service node.

[0111] The lock release flag is used to indicate that the lock of the branch transaction on the shared resource has been released.

[0112] In order to simplify the management and maintenance of locks, as a possible implementation method, the distributed service nodes can release the locks uniformly after the second-stage business processing is completed.

[0113] In an embodiment of the present disclosure, the distributed service node can send a lock release request to the distributed lock service after the second-stage business processing is completed, so that the distributed lock service deletes the lock record of the branch transaction on the shared resource in the lock record table according to the resource identifier of the shared resource and the transaction identifier of the branch transaction carried in the lock release request, and sends a lock release identifier to the distributed service node to inform the distributed service node that the lock has been successfully released.

[0114] Step 404: Receive a lock release identifier sent by the distributed lock service.

[0115] In the embodiment of the present disclosure, if the distributed service node receives a lock release identifier sent by the distributed lock service, it can be determined that the lock has been successfully released.

[0116] Step 405: submit the transaction for releasing the lock of the shared resource together with the transaction for the second-phase business processing to ensure that the lock release is consistent with the state of the second-phase business processing.

[0117] It should be noted that the execution process of steps 401 to 402 and step 405 can be implemented in any way in the embodiments of the present disclosure, and the embodiments of the present disclosure do not limit this and will not be described in detail.

[0118] The distributed lock processing method of the embodiment of the present disclosure responds to triggering the second-stage business processing. After the second-stage business processing is completed, a lock release request is sent to the distributed lock service, so that the distributed lock service deletes the lock record of the branch transaction on the shared resource in the lock record table according to the resource identifier of the shared resource and the transaction identifier of the branch transaction carried in the lock release request, and sends a lock release identifier to the distributed service node; wherein the lock release identifier is used to indicate that the lock of the branch transaction on the shared resource is released; the lock release identifier sent by the distributed lock service is received, thereby, the distributed service node only focuses on locking the shared resources involved in the first-stage business processing during the execution of the first-stage business processing, without paying attention to the release of the lock, the release of the lock depends on the global transaction manager to trigger the second-stage business processing, and the lock is released uniformly after the second-stage business processing is completed, wherein the first-stage locking and the second-stage releasing locks are not in the same thread, which simplifies the management and maintenance of the locks.

[0119] It should be noted that in the present disclosure, the distributed service node can also roll back the local transaction when an exception occurs in the previous stage of business processing after the shared resource is locked. Figure 5 This process is described below.

[0120] Figure 5 It is a flowchart of a distributed lock processing method shown in the fifth embodiment of the present disclosure.

[0121] like Figure 5 As shown, the distributed lock processing method includes the following steps:

[0122] Step 501: In response to receiving a transaction request sent by a transaction center, a branch transaction is started and a local transaction is started for a database operation of a distributed service node in the branch transaction.

[0123] Step 502: Execute a phase of business processing in the local transaction, and during the execution of the phase of business processing, lock the shared resources involved in the phase of business processing.

[0124] Step 503: In response to an exception in a business process at a previous stage after the shared resource is locked, the local transaction is rolled back, and after the local transaction is rolled back, the lock of the shared resource is released.

[0125] The locking of shared resources and the release of shared resource locks when triggering a local transaction rollback are performed in the same thread.

[0126] In an embodiment of the present disclosure, if an exception occurs in business processing after a shared resource is locked in one stage, causing the transaction to be unable to continue to execute normally, the local transaction will be rolled back, and the local transaction manager will call the post-transaction rollback processing to release the lock of the shared resource after the transaction is rolled back.

[0127] It should be noted that after the local transaction is rolled back, the distributed service node can execute the first-stage business processing again, and lock the shared resources involved in the first-stage business processing again during the first-stage business processing. Therefore, the lock release of the shared resources when the local transaction rollback is triggered will not affect the lock release of the shared resources when the second-stage business processing is triggered.

[0128] It should be noted that the lock release of shared resources when triggering a local transaction rollback is an independent transaction and can be submitted independently; the lock release of shared resources when triggering the second-stage business processing is a non-independent transaction and needs to be submitted together with the transaction of the second-stage business processing. When triggering a local transaction rollback, the lock release of shared resources is to call the transaction rollback post-processing to release the lock after the local transaction is rolled back; when triggering the second-stage business processing, the lock release of shared resources is to call the branch transaction post-processing to release the lock after the second-stage business processing is completed and before the transaction is committed.

[0129] Step 504, in response to triggering the second phase business processing, after the second phase business processing is completed, the lock of the shared resource is released, and the transaction of the shared resource lock release is submitted together with the transaction of the second phase business processing to ensure that the lock release is consistent with the state of the second phase business processing.

[0130] It should be noted that the execution process of steps 501 to 502 and step 504 can be implemented in any way in the embodiments of the present disclosure, and the embodiments of the present disclosure do not limit this and will not be described in detail.

[0131] The distributed lock processing method of the embodiment of the present disclosure performs a local transaction rollback in response to an exception in a business process at a previous stage after the shared resource is locked, and releases the lock of the shared resource after the local transaction rollback is completed. Therefore, if an exception occurs in a business process at a previous stage after the shared resource is locked, data consistency can be guaranteed by transaction rollback, and deadlock can be avoided by releasing the lock of the shared resource.

[0132] Based on any embodiment of the present disclosure, Figure 6 As shown, the distributed lock processing method of the embodiment of the present disclosure can also be implemented based on the following steps:

[0133] 1. Receive transaction requests sent by the trading center

[0134] This is the starting point of a distributed transaction, usually initiated by a service (such as an order service), which requests to start a distributed transaction that needs to be executed across multiple distributed service nodes.

[0135] 2. Start branch transactions

[0136] When a distributed service node receives a transaction request sent by a transaction center, it will start one or more branch transactions based on the information carried in the transaction request.

[0137] 3. Register branch affairs mainstream

[0138] The distributed service node will also record the main flow information of the opened branch transaction, including the transaction identifier (transaction ID) of the branch transaction, the service node identifier (service node ID) of the distributed service node, the operation type, timestamp, etc., to facilitate subsequent tracking and recovery.

[0139] 4. Enable local transactions

[0140] In order to ensure the consistency of data within a single service node, the distributed service node will start one or more local transactions in the branch transaction to execute specific business logic.

[0141] 5. Register branch transaction sub-flow

[0142] Similar to registering the branch transaction main flow, the distributed service node will also record the execution status of the transaction at a finer granularity as the branch transaction sub-flow, such as the execution result of SQL (Structured Query Language) statements.

[0143] 6. First-stage business processing

[0144] Distributed service nodes execute specific business logic, such as querying databases, updating data, etc.

[0145] 7. Business call locking interface locking

[0146] During the execution of the first phase of business processing, in order to prevent data inconsistency, the distributed service node will lock the shared resources involved in the first phase of business processing.

[0147] 8. First-stage business processing

[0148] After the lock is successfully acquired, the distributed service node continues to execute specific business logic.

[0149] 9. Modify branch transaction status

[0150] The distributed service node will modify the branch transaction status according to the result of the first-stage business processing. For example, if the result of the first-stage business processing is successful execution, the branch transaction status will be modified to ready to commit; if the result of the first-stage business processing is failure, the branch transaction status will be modified to ready to cancel.

[0151] 10. Local Transaction Manager

[0152] The local transaction manager of the distributed service node will roll back the local transaction when an exception occurs in the first-stage business processing. If the exception occurs after the lock is completed, the local transaction manager will also call the transaction rollback post-processing to release the lock after the local transaction is rolled back. At this time, the lock release is an independent transaction and can be submitted independently.

[0153] 11. Transaction Commit

[0154] The distributed service node will submit the corresponding transaction based on the result of the first-stage business processing. For example, if the result of the first-stage business processing is successful execution, the feedback is successful execution; if the result of the first-stage business processing is failure execution, the feedback is failure execution.

[0155] In summary, the first phase of business processing is completed. Now proceed to the second phase of business processing:

[0156] It should be noted that Figure 6 The connection engine and distributed transaction in the text refer to the overall coordination and management mechanism of distributed transactions, which can be implemented by a global transaction manager (such as Seata's TM (Transaction Manager) and RM components (Resource Manager)) and a distributed transaction framework. Moreover, the two-phase business processing can be performed asynchronously by multiple distributed service nodes involved in the distributed transaction corresponding to the distributed service node.

[0157] like Figure 6 As shown in the figure, in the second phase, the distributed service node can execute the confirmation execution business processing (confim) or cancel the execution business processing (cancel), and after the second phase business processing is completed, the branch transaction post-processing is called to release the lock. At this time, releasing the lock is a non-independent transaction and needs to be submitted together with the transaction of the second phase business processing.

[0158] Among them, whether the distributed service node executes the confirmation execution business processing (confim) or the cancellation execution business processing (cancel) is determined by the global transaction manager according to the one-stage business processing results of multiple distributed service nodes involved in the distributed transaction corresponding to the distributed service node. As a possible implementation method, the global transaction manager can trigger the confirmation execution business processing to each distributed service node involved in the distributed transaction corresponding to the distributed service node in response to the one-stage business processing results of each distributed service node involved in the distributed transaction corresponding to the distributed service node being successful, or can trigger the cancellation execution business processing to each distributed service node involved in the distributed transaction corresponding to the distributed service node in response to the one-stage business processing result of any distributed service node involved in the distributed transaction corresponding to the distributed service node being a failure.

[0159] The release of the lock is uniformly triggered by the distributed transaction in the second stage, and the release of the lock is guaranteed by the exception handling mechanism of the distributed transaction. If the lock is not released, the distributed transaction is not ended, and the branch transaction is not ended at this time. When the first stage of locking (business active call) is completed and then the system crashes, it is impossible to rely on the transaction rollback to release the lock, which will cause the lock to be unable to be released. When another transaction is locked, if the lock has expired, the branch transaction status will be queried according to the previous branch transaction flow. If the branch transaction status is empty, the expired record will be deleted. It can be inferred from this that the deletion of expired records during locking will only occur if and only if the process crashes after the first stage of locking (independent transaction) is successful.

[0160] It should be noted that the normal completion of the first phase will trigger the second phase of the distributed transaction. During the process of releasing the lock in the second phase of the distributed transaction, the following two points can be used to ensure that the lock release is consistent with the business processing status of the second phase:

[0161] 1) The lock is released after the business processing is completed, that is, the lock is released only after confim or cancel is executed;

[0162] 2) The second-phase lock release is a non-independent transaction. The lock transaction and the business transaction are submitted together. When the lock submission fails, it will cause business processing exceptions.

[0163] In the first stage, a lock exception occurs during a business call, which will cause the local transaction to be rolled back. The local transaction manager calls the transaction rollback post-processing to release the lock after the transaction is rolled back.

[0164] After the first phase of locking is completed, the server crashes. At this time, the distributed transaction initiates a rollback of the global transaction, and the current node does not trigger the second phase, resulting in the lock not being released. At this time, the lock interface queries the expired lock record. If there is an expired record, the branch transaction is queried based on the branch transaction ID of the expired record. If it is empty, it is deleted.

[0165] The above process can be applied in scenarios such as Figure 7 As shown, Figure 7 In the transaction, Try is the first stage, confim and cancel are the second stage, the lock operation is performed in Try, and the lock release operation is performed in the transaction rollback post-processing and / or branch transaction post-processing.

[0166] Corresponding to the distributed lock processing method provided in the above-mentioned embodiment, the present disclosure also provides a distributed lock processing device. Since the distributed lock processing device provided in the embodiment of the present disclosure corresponds to the distributed lock processing method provided in the above-mentioned embodiment, the implementation method of the distributed lock processing method is also applicable to the distributed lock processing device provided in the embodiment of the present disclosure, and will not be described in detail in the embodiment of the present disclosure.

[0167] Figure 8 It is a structural diagram of a distributed lock processing device shown in the eighth embodiment of the present disclosure.

[0168] like Figure 8 As shown, the distributed lock processing device 800 includes: a first processing module 810 , a second processing module 820 and a first lock releasing module 830 .

[0169] Among them, the first processing module 810 is used to respond to the transaction request sent by the transaction center, start a branch transaction and start a local transaction for the database operation of the distributed service node in the branch transaction; the second processing module 820 is used to execute the first-stage business processing in the local transaction, and during the execution of the first-stage business processing, lock the shared resources involved in the first-stage business processing; the first lock release module 830 is used to respond to the triggering of the second-stage business processing, release the lock of the shared resource after the second-stage business processing is completed, and submit the transaction of the shared resource lock release together with the transaction of the second-stage business processing to ensure that the lock release is consistent with the state of the second-stage business processing.

[0170] As a possible implementation method of the embodiment of the present disclosure, the second processing module 820 is used to: during the execution of a first-stage business processing, call the locking interface provided by the distributed lock service to send a locking request to the distributed lock service, so that the distributed lock service locks the shared resource according to the resource identifier of the shared resource, the transaction identifier of the branch transaction, the lock type and the locking time carried in the locking request, and based on the locking result, sends a corresponding locking identifier to the distributed service node; receive the locking identifier sent by the distributed lock service, and in response to a successful locking, continue to execute the first-stage business processing, or, in response to a locking failure, wait for a set period of time to re-lock the shared resource.

[0171] As a possible implementation method of the embodiment of the present disclosure, the lock identifier includes a lock success identifier and a lock failure identifier; the second processing module 820 is used for: the distributed lock service to determine whether there is a first lock record in the lock record table that is identical to the resource identifier carried in the lock request; if the first lock record does not exist, the distributed lock service adds a second lock record of the branch transaction for the shared resource in the lock record table, and sends a lock success identifier to the distributed service node; if the first lock record exists, the distributed lock service determines whether the lock type in the first lock record is compatible with the lock type carried in the lock request; if not compatible, the distributed lock service determines whether the first lock record is an expired record; if it is an expired record, the distributed lock service first deletes the first lock record in the lock record table, then adds the second lock record in the lock record table, and sends a lock success identifier to the distributed service node; if it is not an expired record, the distributed lock service determines that the lock fails, and sends a "no lock" to the distributed service node. Send a lock failure flag; if compatible, the distributed lock service determines whether the transaction identifier of the first lock record is the same as the transaction identifier carried in the lock request; if they are the same, the distributed lock service determines that the lock requested by the lock request is a re-entry lock, and determines whether the lock time of the first lock record needs to be updated according to the lock time carried in the lock request, in response to the update of the lock time of the first lock record, or, without updating the lock time of the first lock record, sends a lock success flag to the distributed service node; if they are not the same, the distributed lock service determines that the lock requested by the lock request is not a re-entry lock, and determines whether the first lock record is an expired record; if it is an expired record, the distributed lock service first deletes the first lock record in the lock record table, then adds a second lock record in the lock record table, and sends a lock success flag to the distributed service node; if it is not an expired record, the distributed lock service adds a second lock record in the lock record table, and sends a lock success flag to the distributed service node.

[0172] As a possible implementation method of the embodiment of the present disclosure, the second processing module 820 is used for: the distributed lock service queries the status of the target transaction indicated by the transaction identifier recorded in the first lock record, so as to delete the first lock record in the lock record table when the status of the target transaction is empty.

[0173] As a possible implementation method of the embodiment of the present disclosure, the first lock release module 830 is used to: in response to triggering the second-stage business processing, after the second-stage business processing is completed, send a lock release request to the distributed lock service, so that the distributed lock service deletes the lock record of the branch transaction on the shared resource in the lock record table according to the resource identifier of the shared resource and the transaction identifier of the branch transaction carried in the lock release request, and sends a lock release identifier to the distributed service node; wherein the lock release identifier is used to indicate that the lock of the branch transaction on the shared resource has been released; receive the lock release identifier sent by the distributed lock service.

[0174] As a possible implementation method of the embodiment of the present disclosure, the above-mentioned device also includes: a second lock release module, which is used to trigger a local transaction rollback in response to an exception occurring in a previous stage of business processing after the shared resource is locked, and release the lock of the shared resource after the local transaction rollback is completed.

[0175] As a possible implementation manner of the embodiment of the present disclosure, locking of a shared resource and releasing of the lock of the shared resource when triggering a local transaction rollback are performed in the same thread.

[0176] As a possible implementation manner of the embodiment of the present disclosure, locking of a shared resource and releasing of the lock of the shared resource when triggering the second-stage business processing are not in the same thread.

[0177] As a possible implementation method of the embodiment of the present disclosure, the two-phase business processing is determined by the global transaction manager based on the first-phase business processing results of multiple distributed service nodes involved in the distributed transaction corresponding to the distributed service node; wherein the two-phase business processing includes confirming the execution of the business operation and canceling the execution of the business operation; in response to the first-phase business processing results of each distributed service node involved in the distributed transaction corresponding to the distributed service node being successful, the global transaction manager triggers confirmation of the execution of the business operation to each distributed service node; in response to the first-phase business processing result of any distributed service node involved in the distributed transaction corresponding to the distributed service node being a failure, the global transaction manager triggers cancellation of the execution of the business operation to each distributed service node.

[0178] The distributed lock processing device of the embodiment of the present disclosure, in response to receiving a transaction request sent by a transaction center, opens a branch transaction and opens a local transaction for the database operation of the distributed service node in the branch transaction; executes a first-stage business processing in the local transaction, and during the execution of the first-stage business processing, locks the shared resources involved in the first-stage business processing; in response to triggering a second-stage business processing, after the second-stage business processing is completed, releases the lock of the shared resource, and submits the transaction of the shared resource lock release together with the transaction of the second-stage business processing to ensure that the lock release is consistent with the second-stage business processing state. Therefore, by locking the shared resource in the first stage, it can be ensured that in concurrent transactions, the balance of the same account will not be modified by multiple transactions at the same time, thereby ensuring data consistency and avoiding funding problems caused by inaccurate balances.

[0179] In an exemplary embodiment, an electronic device is also provided.

[0180] Among them, electronic equipment includes:

[0181] processor;

[0182] a memory for storing processor-executable instructions;

[0183] The processor is configured to execute instructions to implement the distributed lock processing method proposed in any of the aforementioned embodiments.

[0184] As an example, Fig. 9 is a schematic diagram of the structure of an electronic device 900 shown in an exemplary embodiment of the present disclosure, such as Fig. 9 As shown, the electronic device 900 may further include:

[0185] The memory 910 and the processor 920, a bus 930 connecting different components (including the memory 910 and the processor 920), the memory 910 stores a computer program, and when the processor 920 executes the program, the distributed lock processing method described in the embodiment of the present disclosure is implemented.

[0186] Bus 930 represents one or more of several types of bus structures, including a memory bus or memory controller, a peripheral bus, an accelerated graphics port, a processor or a local bus using any of a variety of bus architectures. For example, these architectures include but are not limited to Industry Standard Architecture (ISA) bus, Micro Channel Architecture (MAC) bus, Enhanced ISA bus, Video Electronics Standards Association (VESA) local bus and Peripheral Component Interconnect (PCI) bus.

[0187] The electronic device 900 typically includes a variety of electronic device readable media. These media can be any available media that can be accessed by the electronic device 900, including volatile and non-volatile media, removable and non-removable media.

[0188] The memory 910 may also include computer system readable media in the form of volatile memory, such as random access memory (RAM) 940 and / or cache memory 950. The server 900 may further include other removable / non-removable, volatile / non-volatile computer system storage media. By way of example only, the storage system 960 may be used to read and write non-removable, non-volatile magnetic media ( Fig. 9 not shown, often referred to as a "hard drive packager"). Although Fig. 9Not shown, a disk driver package for reading and writing a removable non-volatile disk (e.g., a "floppy disk"), and an optical disk driver package for reading and writing a removable non-volatile optical disk (e.g., a CD-ROM, a DVD-ROM, or other optical media) may be provided. In these cases, each driver package can be connected to the bus 930 via one or more data medium interfaces. The memory 910 may include at least one program product having a set (e.g., at least one) of program modules that are configured to perform the functions of the various embodiments of the present disclosure.

[0189] A program / utility 980 having a set (at least one) of program modules 970 may be stored, for example, in the memory 910, such program modules 970 including but not limited to an operating system, one or more application programs, other program modules, and program data, each of which or some combination may include an implementation of a network environment. The program modules 970 generally perform the functions and / or methods of the embodiments described in the present disclosure.

[0190] The electronic device 900 may also communicate with one or more external devices 990 (e.g., keyboard, pointing device, display 991, etc.), may also communicate with one or more devices that enable a user to interact with the electronic device 900, and / or communicate with any device that enables the electronic device 900 to communicate with one or more other computing devices (e.g., network card, modem, etc.). Such communication may be performed through an input / output (I / O) interface 992. In addition, the electronic device 900 may also communicate with one or more networks (e.g., local area network (LAN), wide area network (WAN) and / or public network, such as the Internet) through a network adapter 993. As shown, the network adapter 993 communicates with other modules of the electronic device 900 through a bus 930. It should be understood that, although not shown in the figure, other hardware and / or software modules may be used in conjunction with the electronic device 900, including but not limited to: microcode, device driver package, redundant processing unit, external disk driver package array, RAID system, tape driver package, and data backup storage system, etc.

[0191] The processor 920 executes various functional applications and data processing by running the programs stored in the memory 910 .

[0192] It should be noted that the implementation process and technical principles of the electronic device of this embodiment refer to the aforementioned explanation of the processing method of the distributed lock of the embodiment of the present disclosure, and will not be repeated here.

[0193] In an exemplary embodiment, a computer-readable storage medium including instructions is also provided, such as a memory including instructions, and the above instructions can be executed by a processor of an electronic device to complete the processing method of the distributed lock proposed in any of the above embodiments. Optionally, the computer-readable storage medium can be a ROM, a random access memory (RAM), a CD-ROM, a tape, a floppy disk, an optical data storage device, etc.

[0194] In an exemplary embodiment, a computer program product is further provided, including a computer program / instruction, wherein when the computer program / instruction is executed by a processor, the distributed lock processing method proposed in any of the above embodiments is implemented.

[0195] Those skilled in the art will readily appreciate other embodiments of the present disclosure after considering the specification and practicing the invention disclosed herein. The present disclosure is intended to cover any variations, uses or adaptations of the present disclosure that follow the general principles of the present disclosure and include common knowledge or customary techniques in the art that are not disclosed in the present disclosure. The description and examples are to be considered exemplary only, and the true scope and spirit of the present disclosure are indicated by the following claims.

[0196] It should be understood that the present disclosure is not limited to the exact structures that have been described above and shown in the drawings, and that various modifications and changes may be made without departing from the scope thereof. The scope of the present disclosure is limited only by the appended claims.

Claims

1. A distributed lock processing method, characterized in that: Applied to distributed service nodes, including: In response to receiving a transaction request sent by a transaction center, opening a branch transaction and opening a local transaction for a database operation of the distributed service node in the branch transaction; Execute a phase of business processing in the local transaction, and during the execution of the phase of business processing, lock the shared resources involved in the phase of business processing; In response to triggering the second-stage business processing, after the second-stage business processing is completed, the lock of the shared resource is released, and the transaction of releasing the lock of the shared resource is submitted together with the transaction of the second-stage business processing to ensure that the lock release is consistent with the state of the second-stage business processing.

2. The method according to claim 1, characterized in that: During the execution of the business processing of the first phase, locking the shared resources involved in the business processing of the first phase includes: During the execution of the first-stage business processing, a lock interface provided by a distributed lock service is called to send a lock request to the distributed lock service, so that the distributed lock service locks the shared resource according to the resource identifier of the shared resource, the transaction identifier of the branch transaction, the lock type and the lock time carried in the lock request, and sends a corresponding lock identifier to the distributed service node based on the lock result; A lock identifier sent by the distributed lock service is received, and in response to a successful lock, the first-stage business processing is continued, or in response to a failed lock, a set time period is waited to re-lock the shared resource.

3. The method according to claim 2, characterized in that The lock identifier includes a lock success identifier and a lock failure identifier; the distributed lock service locks the shared resource according to the resource identifier of the shared resource carried in the lock request, the transaction identifier of the branch transaction, the lock type and the lock time, and sends a corresponding lock identifier to the distributed service node based on the lock result, including: The distributed lock service determines whether there is a first lock record in the lock record table with a resource identifier identical to the resource identifier carried in the lock request; If the first lock record does not exist, the distributed lock service adds a second lock record of the branch transaction on the shared resource in the lock record table, and sends the lock success identifier to the distributed service node; If the first lock record exists, the distributed lock service determines whether the lock type in the first lock record is compatible with the lock type carried in the lock request; If they are not compatible, the distributed lock service determines whether the first lock record is an expired record according to the locking time in the first lock record; If it is an expired record, the distributed lock service first deletes the first lock record in the lock record table, then adds the second lock record in the lock record table, and sends the lock success identifier to the distributed service node; If it is not an expired record, the distributed lock service determines that the locking has failed, and sends a locking failure flag to the distributed service node; If compatible, the distributed lock service determines whether the transaction identifier in the first lock record is the same as the transaction identifier carried in the lock request; If they are the same, the distributed lock service determines that the lock requested by the lock request is a reentrant lock, and determines whether it is necessary to update the lock time in the first lock record according to the lock time carried in the lock request, in response to updating the lock time in the first lock record, or, without updating the lock time in the first lock record, sends the lock success identifier to the distributed service node; If they are not the same, the distributed lock service determines that the lock requested by the lock request is not a reentrant lock, and determines whether the first lock record is an expired record according to the lock time in the first lock record; If it is an expired record, the distributed lock service first deletes the first lock record in the lock record table, then adds the second lock record in the lock record table, and sends the lock success identifier to the distributed service node; If it is not an expired record, the distributed lock service adds the second lock record to the lock record table and sends the lock success identifier to the distributed service node.

4. The method according to claim 3, characterized in that: Before the distributed lock service deletes the first lock record in the lock record table, the method further includes: The distributed lock service queries the state of the target transaction indicated by the transaction identifier recorded in the first lock record, and deletes the first lock record in the lock record table if the state of the target transaction is empty.

5. The method according to claim 1, characterized in that: In response to triggering the second-stage business processing, after the second-stage business processing is completed, releasing the lock of the shared resource includes: In response to triggering the second-stage business processing, after the second-stage business processing is completed, a lock release request is sent to the distributed lock service, so that the distributed lock service deletes the lock record of the branch transaction on the shared resource in the lock record table according to the resource identifier of the shared resource and the transaction identifier of the branch transaction carried in the lock release request, and sends a lock release identifier to the distributed service node; wherein the lock release identifier is used to indicate that the lock of the branch transaction on the shared resource has been released; Receive a lock release identifier sent by the distributed lock service.

6. The method according to claim 1, characterized in that The method further comprises: In response to an exception occurring in the business process of the first phase after the shared resource is locked, a local transaction is rolled back, and after the local transaction is rolled back, the lock of the shared resource is released.

7. The method according to claim 6, characterized in that The locking of the shared resource and the releasing of the lock of the shared resource when triggering the rollback of the local transaction are in the same thread.

8. The method according to any one of claims 1 to 7, characterized in that The locking of the shared resource and the releasing of the lock of the shared resource when triggering the second-stage business processing are not in the same thread.

9. The method according to any one of claims 1 to 7, characterized in that: The two-stage business processing includes confirming the execution of the business processing and canceling the execution of the business processing. The two-stage business processing executed by the distributed service node is determined by the global transaction manager according to the first-stage business processing results of multiple distributed service nodes involved in the distributed transaction corresponding to the distributed service node; In response to the first-stage business processing results of each distributed service node involved in the distributed transaction corresponding to the distributed service node being all successfully executed, the global transaction manager triggers confirmation of business processing execution to each distributed service node involved in the distributed transaction corresponding to the distributed service node; In response to a result of a phase of business processing of any distributed service node involved in the distributed transaction corresponding to the distributed service node being an execution failure, the global transaction manager triggers cancellation of business processing to each distributed service node involved in the distributed transaction corresponding to the distributed service node.

10. A distributed lock processing device, characterized in that: Applied to distributed service nodes, including: A first processing module, configured to, in response to receiving a transaction request sent by a transaction center, start a branch transaction and start a local transaction for a database operation of the distributed service node in the branch transaction; A second processing module is used to execute a phase of business processing in the local transaction, and during the execution of the phase of business processing, lock the shared resources involved in the phase of business processing; The first lock release module is used to respond to triggering the second-stage business processing, release the lock of the shared resource after the second-stage business processing is completed, and submit the transaction of the lock release of the shared resource together with the transaction of the second-stage business processing to ensure that the lock release is consistent with the state of the second-stage business processing.

Citation Information

Patent Citations

  • Transaction processing method and system

    CN114579260A

  • Distributed lock management method and device, electronic equipment and storage medium

    CN115454581A

  • Distributed transaction control method and related equipment

    CN118069299A

  • Distributed transaction processing system and method thereof

    US20070260542A1

Cited By

  • TCC-based transaction processing method and device, electronic equipment and storage medium

    CN119722312A

  • Data real-time monitoring method based on smart factory management platform system

    CN120746504A