Lock execution method and device based on transactional key-value database, equipment and medium
By designing distributed lock key-value pairs in a transactional key-value database and utilizing a transaction conflict detection mechanism, the architectural complexity and operational costs caused by concurrency conflicts in cloud databases are resolved, achieving high-performance and highly reliable distributed lock functionality.
Patent Information
- Application Number
- CN202511478497.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-10-16
- Publication Date
- 2025-12-30
- Estimated Expiration
- 2045-10-16
AI Technical Summary
In cloud databases, introducing external lock services to resolve concurrency conflicts increases architectural complexity and operational costs, while also creating points of failure in the system.
In a transactional key-value database, a distributed lock key-value pair is designed. The lock occupancy status is determined by reading the lock key and lock value. When the lock is not occupied, the lock value is updated to execute transaction operations. A transaction conflict detection mechanism is used to achieve high-performance and highly reliable distributed lock functionality.
It achieves high-performance, highly reliable distributed lock functionality without the need for external components, reducing architectural complexity and operational costs, and avoiding system failure points.
Smart Images

Figure CN120950735B_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of electronic digital data processing, and more specifically to a lock execution method, apparatus, device, and medium based on a transactional key-value database. Background Technology
[0002] Cloud database products run in a containerized environment. All metadata, especially tenant account information, is uniformly stored in the underlying transactional key-value database cluster. The instance management program is responsible for executing read and write transactions such as adding, deleting, modifying, and querying tenant data. When two or more instances attempt to modify the same tenant's information simultaneously, concurrency conflicts can occur. Therefore, to avoid concurrency conflicts, external components are introduced to implement locking services.
[0003] However, external components can significantly increase the architectural complexity, operational costs, and potential points of failure for cloud database products. Summary of the Invention
[0004] In view of the above problems, the present invention provides a lock execution method, apparatus, device and medium based on a transactional key-value database.
[0005] According to a first aspect of the present invention, a lock execution method based on a transactional key-value database is provided, comprising: in response to a first transaction operation request for a target resource in the transactional key-value database, reading a first lock key and a first lock value of a distributed lock corresponding to the target resource, wherein the first lock value includes an instance identifier holding the distributed lock and valid timestamp information of the distributed lock, and the first transaction operation request includes a target instance identifier and a lease duration of the distributed lock; determining a occupancy result of the distributed lock based on the first lock key and the first lock value; and, if the occupancy result indicates that the distributed lock is not occupied, updating the instance identifier and valid timestamp information in the first lock value based on the target instance identifier and the lease duration, so that the target instance executes the transaction operation indicated by the first transaction operation request.
[0006] Optionally, determining the occupancy result of the distributed lock based on the first lock key and the first lock value includes: if the first lock key does not exist, determining that the distributed lock is not occupied; if the first lock key exists and the response time of the first transaction operation request is greater than the valid timestamp information, determining that the distributed lock is not occupied; if the first lock key exists and the response time of the first transaction operation request is less than or equal to the valid timestamp information, determining that the distributed lock is occupied.
[0007] Optionally, the lock execution method further includes: performing a renewal operation on the distributed lock corresponding to the target resource during the process of the target instance executing the transaction operation indicated by the first transaction operation request.
[0008] Optionally, a renewal operation is performed on the distributed lock corresponding to the target resource, including: in response to a second transaction operation request for the target resource in the transactional key-value database, reading the second lock key of the distributed lock corresponding to the target resource, and obtaining the second lock value based on the second lock key, wherein the second transaction operation request is initiated by a transaction operation on the target resource based on a preset renewal frequency, and the preset renewal frequency is determined based on the lease duration of the distributed lock; verifying the instance identifier and the target instance identifier in the second lock value to obtain a verification result; if the verification result is successful, updating the valid timestamp information in the second lock value according to the response time of the second transaction operation request and the lease duration.
[0009] Optionally, the lock execution method also includes: stopping the renewal operation and releasing the distributed lock after the transaction operation is completed.
[0010] Optionally, releasing the distributed lock includes: in response to a third transaction operation request for the target resource in the transactional key-value database, reading the third lock key and third lock value of the distributed lock corresponding to the target resource; and deleting the third lock key and third lock value if the instance identifier in the third lock value matches the target instance identifier.
[0011] Optionally, the lock execution method further includes: if it is determined that the distributed lock is occupied, in response to a fourth transaction operation request for the target resource in the transactional key-value database, re-determining the occupancy result of the distributed lock corresponding to the target resource.
[0012] A second aspect of the present invention provides a lock execution apparatus based on a transactional key-value database, comprising: a response module, configured to, in response to a first transaction operation request for a target resource in the transactional key-value database, read a first lock key and a first lock value of a distributed lock corresponding to the target resource, wherein the first lock value includes an instance identifier holding the distributed lock and valid timestamp information of the distributed lock, and the first transaction operation request includes a target instance identifier and a lease duration of the distributed lock; a determination module, configured to, based on the first lock key and the first lock value, determine the occupancy result of the distributed lock; and an update module, configured to, if the occupancy result indicates that the distributed lock is not occupied, update the instance identifier and valid timestamp information in the first lock value based on the target instance identifier and the lease duration, so that the target instance executes the transaction operation indicated by the first transaction operation request.
[0013] A third aspect of the present invention provides an electronic device comprising: one or more processors; and a memory for storing one or more programs, wherein when the one or more programs are executed by the one or more processors, the one or more processors perform the method described above.
[0014] A fourth aspect of this application also provides a computer-readable storage medium having a computer program or instructions stored thereon, which, when executed by a processor, implement the steps of the above-described method.
[0015] The lock execution method, apparatus, device, and medium based on a transactional key-value database provided by the present invention utilize the transaction characteristics of the transaction conflict detection mechanism of the transactional key-value database. A lock key-value pair is designed in the transactional key-value database to represent the state of the distributed lock. In each response to a first transaction operation request for a target resource, the first lock key and first lock value of the distributed lock corresponding to the target resource are read. Then, the occupancy result of the distributed lock is determined and the distributed lock is updated based on the first lock key and first lock value. This achieves the function of a high-performance, highly reliable distributed lock at "zero cost," solving the technical problems of increased architectural complexity, operational costs, and potential system failure points caused by introducing external lock components. Attached Figure Description
[0016] The above and other objects, features and advantages of the present invention will become clearer from the following description of embodiments of the invention with reference to the accompanying drawings.
[0017] Figure 1 The present invention illustrates an application scenario of a lock execution method and apparatus based on a transactional key-value database according to an embodiment of the present invention.
[0018] Figure 2 A flowchart of a lock execution method based on a transactional key-value database according to an embodiment of the present invention is shown.
[0019] Figure 3 A schematic diagram of a distributed lock being used according to an embodiment of the present invention is shown.
[0020] Figure 4 A schematic diagram of a lock renewal operation according to an embodiment of the present invention is shown.
[0021] Figure 5 A structural block diagram of a lock execution device based on a transactional key-value database according to an embodiment of the present invention is shown.
[0022] Figure 6 A block diagram of an electronic device suitable for implementing a lock execution method based on a transactional key-value database according to an embodiment of the present invention is shown. Detailed Implementation
[0023] Hereinafter, embodiments of the present invention will be described with reference to the accompanying drawings. However, it should be understood that these descriptions are exemplary only and are not intended to limit the scope of the invention. In the following detailed description, numerous specific details are set forth to provide a thorough understanding of the embodiments of the invention for ease of explanation. However, it will be apparent that one or more embodiments may be practiced without these specific details. Furthermore, descriptions of well-known structures and techniques are omitted in the following description to avoid unnecessarily obscuring the concept of the invention.
[0024] The terminology used herein is for the purpose of describing particular embodiments only and is not intended to limit the invention. The terms “comprising,” “including,” etc., as used herein indicate the presence of features, steps, operations, and / or components, but do not exclude the presence or addition of one or more other features, steps, operations, or components.
[0025] All terms used herein (including technical and scientific terms) have the meanings commonly understood by those skilled in the art, unless otherwise defined. It should be noted that the terms used herein are to be interpreted in a manner consistent with the context of this specification, and not in an idealized or overly rigid way.
[0026] When using expressions such as "at least one of A, B and C", they should generally be interpreted in accordance with the meaning that is commonly understood by those skilled in the art (e.g., "a system having at least one of A, B and C" should include, but is not limited to, a system having A alone, a system having B alone, a system having C alone, a system having A and B, a system having A and C, a system having B and C, and / or a system having A, B and C, etc.).
[0027] In the process of implementing this invention, it was discovered that because the instance program runs in a stateless, multi-instance manner, when two or more instances attempt to modify the same tenant's information simultaneously, concurrency conflicts will occur, potentially leading to data inconsistency, operation failures, or even damage to metadata, severely impacting the stability of cloud database products. However, introducing and maintaining an entire set of external components to implement a lock function also incurs high operational costs.
[0028] In view of this, embodiments of the present invention provide a lock execution method, apparatus, device, and medium based on a transactional key-value database. The method includes: responding to a first transaction operation request for a target resource in the transactional key-value database, reading a first lock key and a first lock value of a distributed lock corresponding to the target resource, wherein the first lock value includes an instance identifier holding the distributed lock and valid timestamp information of the distributed lock, and the first transaction operation request includes a target instance identifier and a lease duration of the distributed lock; determining the occupancy result of the distributed lock based on the first lock key and the first lock value; and updating the instance identifier and valid timestamp information in the first lock value based on the target instance identifier and the lease duration when the occupancy result indicates that the distributed lock is not occupied, so that the target instance executes the transaction operation indicated by the first transaction operation request.
[0029] In the technical solution of this invention, the user information (including but not limited to user personal information, user image information, user device information, such as location information) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved are all information and data authorized by the user or fully authorized by all parties. Furthermore, the collection, storage, use, processing, transmission, provision, disclosure, and application of related data all comply with relevant laws, regulations, and standards, take necessary confidentiality measures, do not violate public order and good morals, and provide corresponding operation entry points for users to choose to authorize or refuse.
[0030] Figure 1 The present invention illustrates an application scenario of a lock execution method and apparatus based on a transactional key-value database according to an embodiment of the present invention.
[0031] like Figure 1 As shown, the business system architecture 100 according to this embodiment may include a first terminal device 101, a second terminal device 102, a third terminal device 103, a network 104, and a server 105. The network 104 serves as a medium for providing communication links between the first terminal device 101, the second terminal device 102, the third terminal device 103, and the server 105. The network 104 may include various connection types, such as wired or wireless communication links, or fiber optic cables, etc.
[0032] Users can interact with server 105 via network 104 using at least one of the first terminal device 101, second terminal device 102, and third terminal device 103 to receive or send messages, etc. Various communication client applications can be installed on the first terminal device 101, second terminal device 102, and third terminal device 103, such as shopping applications, web browser applications, search applications, instant messaging tools, email clients, social media platform software, etc. (for example only).
[0033] The first terminal device 101, the second terminal device 102, and the third terminal device 103 can be various electronic devices with displays and support web browsing, including but not limited to smartphones, tablets, laptops, and desktop computers.
[0034] Server 105 can be a server that provides various services, such as a backend management server that supports websites browsed by users using the first terminal device 101, the second terminal device 102, and the third terminal device 103 (this is just an example). The clustered distributed database is deployed on the clustered server 105. Server 105 includes multiple server nodes that can analyze and process data such as received user requests, and feed back the processing results (such as web pages, information, or data obtained or generated according to user requests) to the terminal devices.
[0035] It should be noted that the lock execution method based on a transactional key-value database provided in this embodiment of the invention can generally be executed by server 105. Correspondingly, the lock execution device based on a transactional key-value database provided in this embodiment of the invention can generally be located in server 105. The lock execution method based on a transactional key-value database provided in this embodiment of the invention can also be executed by a server or server cluster that is different from server 105 and capable of communicating with the first terminal device 101, the second terminal device 102, the third terminal device 103, and / or server 105. Correspondingly, the lock execution device based on a transactional key-value database provided in this embodiment of the invention can also be located in a server or server cluster that is different from server 105 and capable of communicating with the first terminal device 101, the second terminal device 102, the third terminal device 103, and / or server 105.
[0036] Alternatively, the lock execution method based on a transactional key-value database provided in this embodiment of the invention can also be executed by the first terminal device 101, the second terminal device 102, or the third terminal device 103, or by other terminal devices different from the first terminal device 101, the second terminal device 102, or the third terminal device 103. Correspondingly, the lock execution device based on a transactional key-value database provided in this embodiment of the invention can also be located in the first terminal device 101, the second terminal device 102, or the third terminal device 103, or in other terminal devices different from the first terminal device 101, the second terminal device 102, or the third terminal device 103.
[0037] It should be understood that Figure 1 The number of terminal devices, networks, and servers shown is merely illustrative. Depending on implementation needs, any number of terminal devices, networks, and servers can be included.
[0038] It should be noted that the sequence numbers of the operations in the following methods are for descriptive purposes only and should not be considered as indicating the execution order of the operations. Unless explicitly stated otherwise, the method does not need to be executed in the exact order shown.
[0039] Figure 2 A flowchart of a lock execution method based on a transactional key-value database according to an embodiment of the present invention is shown.
[0040] like Figure 2 As shown, the method 200 includes operations S210 to S230.
[0041] In operation S210, in response to the first transaction operation request for the target resource in the transactional key-value database, the first lock key and the first lock value of the distributed lock corresponding to the target resource are read.
[0042] Optionally, the target resource can be a business resource related to the target object. The target resource is stored in a transactional key-value database in key-value format. The key is a unique namespace for the object, and the value is a business resource associated with the object name. For example, the key `tenant=app1` represents the object `app1`, and the value `app1:order:123` represents that the business resource of object `app1` is the entire record of order number 123, such as the supply quantity and supply date.
[0043] Optionally, the first transaction operation request is a transaction operation request initiated by the target instance for the target resource in the transactional key-value database. The first transaction operation request is used to request read and write business operations on the target resource.
[0044] Optionally, the first transaction operation request includes the target instance identifier and the lease duration of the distributed lock. The target instance can be a running service, container, or process, and the target instance identifier is a unique number or name of the target instance.
[0045] For example, the target instance is identified by the name of the container (Pod) or a universally unique identifier (UUID).
[0046] Optionally, the lease duration is the duration for which the target instance holds the distributed lock.
[0047] Optionally, in response to the first transaction operation request from the target instance for the target resource in the transactional key-value database, the server reads the first lock key and the first lock value of the distributed lock corresponding to the target resource.
[0048] Optionally, the transaction conflict detection mechanism of a transactional key-value database is as follows: If multiple concurrent transactions attempt to perform a "read first, write later" sequence of operations on the same key, the concurrency control model of the transactional key-value database guarantees that only the first successfully committed transaction will take effect. All other transactions operating on the same key will fail upon commit and receive a clear "transaction conflict" error result. This "deterministic failure" behavior is used to determine the "winner" in the concurrent competition. Whoever's transaction succeeds acquires the lock; whoever's transaction conflicts fails to acquire the lock. This provides us with a natural, atomic mutual exclusion primitive. Based on the above transaction conflict detection mechanism, a lock key-value pair is designed to concretize the distributed lock in the transactional key-value database.
[0049] Optionally, the state of the distributed lock can be represented by constructing lock key-value pairs in a transactional key-value database, and the first lock key and first lock value of the distributed lock corresponding to the target resource can be read when responding to the first transaction operation request.
[0050] Optionally, the first lock key matches the target object corresponding to the target resource. To lock the target resource of the target object, a unique lock key is generated. For example, to lock the target object 'tenant-abc', the first lock key is gcdw.account.locks.info['tenant-abc']. Using the uniform prefix gcdw.accountlocks facilitates management and observation.
[0051] Optionally, the first lock value includes the instance identifier holding the distributed lock and the valid timestamp information of the distributed lock. The instance identifier is used to identify the instance currently holding the distributed lock.
[0052] Optionally, valid timestamp information is used to record the expiration time of an instance holding the distributed lock, preventing deadlocks. For example, the precise time the distributed lock was acquired can be recorded, with a lease period of 30 seconds. The valid timestamp information is determined based on the precise acquisition time and the lease period. After one instance acquires the distributed lock, other instances can determine that the distributed lock has "expired" after 30 seconds, thus allowing them to safely hold the distributed lock.
[0053] In operation S220, the holding result of the distributed lock is determined based on the first lock key and the first lock value.
[0054] Optionally, the holding result of the distributed lock can be determined based on the state of the first lock key and the valid timestamp information.
[0055] For example, if the valid timestamp is 2:00:00 and the response time to the first transaction operation request is 3:00:00, the distributed lock is determined to have "expired," and the result of the distributed lock being occupied is that it is not occupied.
[0056] Optionally, the outcome of a distributed lock can be either not being held or being held.
[0057] In operation S230, if the occupancy result indicates that the distributed lock is not occupied, the instance identifier and valid timestamp information in the first lock value are updated according to the target instance identifier and lease duration, so that the target instance can execute the transaction operation indicated by the first transaction operation request.
[0058] Optionally, the occupancy result indicates that the distributed lock is not occupied, meaning that the target instance can safely hold this distributed lock, and the instance identifier in the first lock value is updated based on the target instance identifier.
[0059] Optionally, the expiration time of the target object's holding of the distributed lock is determined based on the sum of the precise time when the distributed lock is acquired by the target instance and the lease duration, and the effective timestamp information of the first lock value is changed based on the expiration time of the target object's holding of the distributed lock.
[0060] Optionally, the first lock value is updated. After confirming that the target instance has successfully acquired the distributed lock, the target instance executes the transaction operation indicated by the first transaction operation request to operate on the target resource in the transactional key-value database.
[0061] Optionally, by leveraging the transaction characteristics of the transaction conflict detection mechanism in a transactional key-value database, a lock key value is designed in the transactional key-value database to represent the state of the distributed lock. In each response to the first transaction operation request for the target resource, the first lock key and the first lock value of the distributed lock corresponding to the target resource are read. Then, the occupancy result of the distributed lock is determined and the distributed lock is updated based on the first lock key and the first lock value. This achieves the function of a high-performance and highly reliable distributed lock at "zero cost," solving the technical problems of increasing architectural complexity, operation and maintenance costs, and potential system failure points by introducing external lock components.
[0062] Optionally, determining the occupancy result of the distributed lock based on the first lock key and the first lock value includes: if the first lock key does not exist, determining that the distributed lock is not occupied; if the first lock key exists and the response time of the first transaction operation request is greater than the valid timestamp information, determining that the distributed lock is not occupied; if the first lock key exists and the response time of the first transaction operation request is less than or equal to the valid timestamp information, determining that the distributed lock is occupied.
[0063] Optionally, if the first lock key does not exist, it means that the first lock key is null and the distributed lock is currently free. The result of the distributed lock occupancy is that it is not occupied. The distributed lock can be acquired by the target instance. In addition to updating the instance identifier and valid timestamp information in the first lock value based on the target instance identifier and lease duration, it is also necessary to update the first lock key based on the target object corresponding to the target resource.
[0064] Optionally, if the first lock exists and the response time of the first transaction operation request is greater than the valid timestamp information, it means that an instance holding this distributed lock may have crashed and failed to release the lock in time, and the lock has expired. In this case, the result of the distributed lock being occupied is that it is not occupied. At this time, the distributed lock can also be acquired by the target instance.
[0065] Optionally, if the first lock key exists and the response time of the first transaction operation request is less than or equal to the valid timestamp information, it means that there is currently an instance holding this distributed lock normally, and the result of the distributed lock being occupied is that it is occupied. In this case, the distributed lock cannot be acquired by the target instance.
[0066] Figure 3 A schematic diagram of a distributed lock being used according to an embodiment of the present invention is shown.
[0067] like Figure 3 As shown, the first container 301 (Pod1) initiates a first transaction operation request for the target resource in the transactional key-value database 305. Upon receiving the first transaction operation request, the distributed server 304 reads the first lock key and first lock value of the distributed lock corresponding to the target resource from the transactional key-value database. Based on the first lock key and first lock value, it determines that the distributed lock is not occupied and sends the occupancy result to the first container 301, thus the first container 301 successfully occupies the distributed lock. During this process, the second container 302 (Pod2) and the third container 303 (Pod3) respectively initiate first transaction operation requests for the target resources in the transactional key-value database. Upon receiving the first transaction operation request, the distributed server 304 reads the first lock key and first lock value of the distributed lock corresponding to the target resource from the transactional key-value database 305. Based on the first lock key and first lock value, it determines that the distributed lock is occupied and sends the occupancy result to the second container 302 and the third container 303, respectively, thus the second container 302 and the third container 303 fail to acquire the distributed lock.
[0068] Optionally, the above lock execution method further includes: performing a renewal operation on the distributed lock corresponding to the target resource during the process of the target instance executing the transaction operation indicated by the first transaction operation request.
[0069] Optionally, if the occupancy result indicates that the distributed lock is not occupied, the first lock key and the first lock value of the distributed lock are updated in the local transaction cache, and then the lock acquisition transaction updated is committed globally, thereby realizing log writing to disk and ensuring that the lock acquisition transaction has atomicity, visibility and conflict detection capabilities, thus achieving distributed mutual exclusion.
[0070] Optionally, if the lock acquisition transaction is successfully committed, it means that the target instance has successfully acquired this distributed lock, and the target instance executes the transaction operation indicated by the first transaction operation request.
[0071] Optionally, upon successful lock acquisition and transaction commit, the server immediately starts a thread to renew the distributed lock, which will execute periodically.
[0072] Optionally, some critical transactions require long execution times, such as data migration or backup and recovery for a large tenant. A fixed lease duration may not be sufficient. If the distributed lock expires before the transaction is completed, it can be preempted by other instances, causing serious problems.
[0073] Optionally, during the process of the target instance executing the transaction operation indicated by the first transaction operation request, a renewal operation is periodically performed on the distributed lock corresponding to the target resource to ensure that the target instance can complete the transaction operation during the period of holding the distributed lock, thereby avoiding the technical problem of the distributed lock expiring before the critical transaction operation is completed.
[0074] Optionally, a renewal operation is performed on the distributed lock corresponding to the target resource, including: in response to a second transaction operation request for the target resource in the transactional key-value database, reading the second lock key of the distributed lock corresponding to the target resource, and obtaining the second lock value based on the second lock key, wherein the second transaction operation request is initiated by a transaction operation on the target resource based on a preset renewal frequency, and the preset renewal frequency is determined based on the lease duration of the distributed lock; verifying the instance identifier and the target instance identifier in the second lock value to obtain a verification result; if the verification result is successful, updating the valid timestamp information in the second lock value according to the response time of the second transaction operation request and the lease duration.
[0075] Optionally, the preset renewal frequency can be less than the lease duration, and can be set to 1 / 3 or 1 / 2 of the lease duration. For example, if the lease duration is 30 seconds, the preset renewal frequency can be 10 seconds or 15 seconds, in which case a renewal operation will be performed every 10 seconds or 15 seconds, thus leaving enough room for retry and network latency.
[0076] Optionally, based on a preset renewal frequency, when a target instance initiates a second transaction operation request to a target resource in a transactional key-value database, the server responds by receiving the second transaction operation request, reading the second lock key corresponding to the distributed lock corresponding to the target resource, obtaining the corresponding second lock value based on the second lock key, and parsing the second lock value to obtain the instance identifier included in the second lock value.
[0077] Optionally, if the instance identifier in the second lock value matches the target instance identifier, a verification result indicating successful characterization verification is obtained; otherwise, a verification result indicating failed characterization verification is obtained.
[0078] Optionally, if the verification result is successful, it means that the distributed lock is still held by the target instance. At this time, the renewal operation can be performed.
[0079] Optionally, the response time of the second transaction operation request can represent the current renewal time. The valid timestamp information in the second lock value is updated based on the sum of the response time of the second transaction operation request and the lease duration.
[0080] For example, the lease duration is 30 seconds, the response time of the second transaction operation request is 3:00:00, and the valid timestamp information in the second lock value is 3:00:30.
[0081] Optionally, if the verification result is "verification failed," it means that the current distributed lock has been preempted by another instance or has been actively released. In this case, the renewal process must stop immediately and must notify the main process executing the transaction to immediately abort the operation to prevent it from continuing to write dirty data without the protection of the distributed lock.
[0082] Optionally, a global commit of the lock renewal transaction can be performed. A successful commit indicates a successful lock renewal.
[0083] Optionally, without introducing external components, a distributed lock renewal function can be introduced in the transactional key-value database based on the lock key-value pair. This solves the technical problem of distributed locks expiring before critical transaction operations are completed, while reducing architectural complexity and resource consumption.
[0084] Figure 4 A schematic diagram of a lock renewal operation according to an embodiment of the present invention is shown.
[0085] like Figure 4As shown, the server responds to the first transaction operation request for the target resource in the transactional key-value database by performing the following four phases: Phase 1: Calling the read function to read the first lock key and first lock value of the first distributed lock corresponding to the target resource, and determining that the distributed lock is not occupied based on the first lock key and first lock value; Phase 2: Updating the first lock value: Updating the instance identifier and valid timestamp information in the first lock value in the local transaction cache, while the instance identifier and valid timestamp information in the first lock value of the second distributed lock remain unchanged, and the instance identifier and valid timestamp information in the first lock value of the third distributed lock remain unchanged; Phase 3: Returning a success message for the update; Phase 4: Committing the lock and acquiring the transaction: After receiving the result of the successful local update, the server... The server globally commits the lock acquisition transaction for this update, and the commit is successful. The fifth stage is to start the lock renewal thread: After the commit is successful, the server immediately starts a thread to renew the first distributed lock, calls the renewal function to execute the renewal operation, the target instance initiates a second transaction operation request, the server responds to the second transaction operation request, reads the second lock key of the first distributed lock corresponding to the target resource, and obtains the second lock value based on the second lock key. The server verifies the instance identifier and the target instance identifier in the second lock value and obtains the verification result. The sixth stage is to update the valid timestamp information: If the verification result is successful, the server updates the valid timestamp information of the first distributed lock and globally commits the lock renewal transaction.
[0086] Optionally, the lock execution method also includes: stopping the renewal operation and releasing the distributed lock after the transaction operation is completed.
[0087] Optionally, after the transaction operation is completed, the thread executing the renewal operation on the server should be stopped first.
[0088] Optionally, release the distributed lock after stopping the renewal operation.
[0089] Optionally, releasing the distributed lock includes: in response to a third transaction operation request for the target resource in the transactional key-value database, reading the third lock key and third lock value of the distributed lock corresponding to the target resource; and deleting the third lock key and third lock value if the instance identifier in the third lock value matches the target instance identifier.
[0090] Optionally, the server returns the result of the transaction operation to the target instance and stops the renewal operation. The target instance initiates a third transaction operation request to the target resource in the transactional key-value database, reads the lock key-value pair corresponding to the distributed lock of the target resource, extracts the third lock value from the lock key-value pair, and parses the third lock value to obtain the instance identifier.
[0091] Optionally, the third lock key and the third lock value are the lock key and lock value at the moment of responding to a third transaction operation request.
[0092] Optionally, if the instance identifier in the third lock value matches the target instance identifier, a verification result indicating successful characterization verification is obtained; otherwise, a verification result indicating failed characterization verification is obtained.
[0093] Optionally, if the verification result is successful, it means that the current distributed lock is still held by the target instance. At this time, the third lock key and the third lock value can be deleted to release the distributed lock.
[0094] Optionally, the lock can be released by committing the transaction globally. A successful commit means the lock has been released successfully.
[0095] Optionally, the lock execution method further includes: if it is determined that the distributed lock is occupied, in response to a fourth transaction operation request for the target resource in the transactional key-value database, re-determining the occupancy result of the distributed lock corresponding to the target resource.
[0096] Optionally, if the server determines that the distributed lock is occupied, it will return the occupancy result to the target instance.
[0097] Optionally, after a preset time interval or a short random time, the target instance re-submits a fourth transaction operation request for the target resource in the transactional key-value database. In response to the fourth transaction operation request, the server re-reads the lock key-value pair of the distributed lock corresponding to the target resource and re-determines the occupancy result of the distributed lock corresponding to the target resource based on the lock key-value pair.
[0098] Optionally, after the server returns the occupancy result to the target instance, the target instance does not need to wait in the queue, can end the current thread, and then re-initiate the fourth transaction operation request to determine the occupancy result of the distributed lock, thus reducing the consumption of thread resources.
[0099] Based on the above-described lock execution method using a transactional key-value database, this invention also provides a lock execution device based on a transactional key-value database. The following will be combined with... Figure 5 The device is described in detail.
[0100] Figure 5 A structural block diagram of a lock execution device based on a transactional key-value database according to an embodiment of the present invention is shown.
[0101] like Figure 5 As shown, the lock execution device 500 based on a transactional key-value database in this embodiment includes a response module 510, a determination module 520, and an update module 530.
[0102] The response module 510 is used to respond to a first transaction operation request for a target resource in a transactional key-value database by reading the first lock key and the first lock value of the distributed lock corresponding to the target resource. The first lock value includes the instance identifier holding the distributed lock and the valid timestamp information of the distributed lock. The first transaction operation request includes the target instance identifier and the lease duration of the distributed lock. In one embodiment, the response module 510 can be used to execute the operation S210 described above, which will not be repeated here.
[0103] The determining module 520 is used to determine the occupancy result of the distributed lock based on the first lock key and the first lock value. In one embodiment, the determining module 520 can be used to perform the operation S220 described above, which will not be repeated here.
[0104] The update module 530 is used to update the instance identifier and valid timestamp information in the first lock value according to the target instance identifier and lease duration when the occupancy result indicates that the distributed lock is not occupied, so that the target instance can execute the transaction operation indicated by the first transaction operation request. In one embodiment, the update module 530 can be used to execute the operation S230 described above, which will not be repeated here.
[0105] Optionally, the determining module 520 includes a first determining submodule, a second determining submodule, and a third determining submodule.
[0106] The first determination submodule is used to determine that the distributed lock is not occupied if the first lock key does not exist.
[0107] The second determining submodule is used to determine that the distributed lock is not occupied when the first lock key exists and the response time of the first transaction operation request is greater than the valid timestamp information.
[0108] The third determination submodule is used to determine the occupancy result of the distributed lock as occupied when the first lock key exists and the response time of the first transaction operation request is less than or equal to the valid timestamp information.
[0109] Optionally, the lock execution device 500 based on a transactional key-value database also includes a renewal module.
[0110] The renewal module is used to perform a renewal operation on the distributed lock corresponding to the target resource during the transaction operation indicated by the first transaction operation request of the target instance.
[0111] Optionally, the renewal module includes a first renewal submodule, a second renewal submodule, and a third renewal submodule.
[0112] The first renewal submodule block is used to respond to the second transaction operation request for the target resource in the transactional key-value database, read the second lock key of the distributed lock corresponding to the target resource, and obtain the second lock value based on the second lock key. The second transaction operation request is initiated by the transaction operation on the target resource based on a preset renewal frequency, which is determined based on the lease duration of the distributed lock.
[0113] The second renewal submodule is used to verify the instance identifier and the target instance identifier in the second lock value and obtain the verification result.
[0114] The third renewal submodule is used to update the valid timestamp information in the second lock value according to the response time of the second transaction operation request and the lease duration if the verification result is successful.
[0115] Optionally, the lock execution device 500 based on a transactional key-value database may also include a release module.
[0116] The release module is used to stop the renewal operation and release the distributed lock after the transaction operation is completed.
[0117] Optionally, the release module includes a first release submodule and a second release submodule.
[0118] The first release submodule is used to respond to a third transaction operation request for the target resource in the transactional key-value database by reading the third lock key and the third lock value of the distributed lock corresponding to the target resource.
[0119] The second release submodule is used to delete the third lock key and the third lock value if the instance identifier in the third lock value matches the target instance identifier.
[0120] Optionally, the lock execution device 500 based on a transactional key-value database also includes a return module.
[0121] The return module is used to, in response to a fourth transaction operation request for the target resource in the transactional key-value database, re-determine the occupancy result of the distributed lock corresponding to the target resource when it is determined that the distributed lock is occupied.
[0122] Optionally, any plurality of modules among the response module 510, determination module 520, and update module 530 may be combined into one module, or any one of these modules may be split into multiple modules. Alternatively, at least a portion of the functionality of one or more of these modules may be combined with at least a portion of the functionality of other modules and implemented in one module. Optionally, at least one of the response module 510, determination module 520, and update module 530 may be at least partially implemented as hardware circuitry, such as a field-programmable gate array (FPGA), a programmable logic array (PLA), a system-on-a-chip, a system-on-a-substrate, a system-on-package, an application-specific integrated circuit (ASIC), or any other reasonable means of integrating or packaging circuitry, or implemented in software, hardware, or firmware, or in any appropriate combination of any of these three implementation methods. Alternatively, at least one of the response module 510, determination module 520, and update module 530 may be at least partially implemented as a computer program module, which, when run, can perform the corresponding function.
[0123] Figure 6 A block diagram of an electronic device suitable for implementing a lock execution method based on a transactional key-value database according to an embodiment of the present invention is shown.
[0124] Figure 6 The electronic device shown is merely an example and should not be construed as limiting the functionality and scope of the embodiments of the present invention.
[0125] like Figure 6 As shown, a computer electronic device 600 according to an embodiment of the present invention includes a processor 601, which can perform various appropriate actions and processes according to a program stored in a ROM 602 (read-only memory) or a program loaded from a storage portion 608 into a RAM 603 (random access memory). The processor 601 may include, for example, a general-purpose microprocessor (e.g., a CPU), an instruction set processor and / or an associated chipset and / or a special-purpose microprocessor (e.g., an application-specific integrated circuit (ASIC)), etc. The processor 601 may also include onboard memory for caching purposes. The processor 601 may include a single processing unit or multiple processing units for performing different actions of the method flow according to an embodiment of the present invention.
[0126] RAM 603 stores various programs and data required for the operation of electronic device 600. Processor 601, ROM 602, and RAM 603 are interconnected via bus 604. Processor 601 executes various operations of the method flow according to embodiments of the present invention by executing programs in ROM 602 and / or RAM 603. It should be noted that programs may also be stored in one or more memories other than ROM 602 and RAM 603. Processor 601 may also execute various operations of the method flow according to embodiments of the present invention by executing programs stored in one or more memories.
[0127] Optionally, the electronic device 600 may also include an input / output (I / O) interface 605, which is also connected to the bus 604. The electronic device 600 may also include one or more of the following components connected to the input / output (I / O) interface 605: an input section 606 including a keyboard, mouse, etc.; an output section 607 including a cathode ray tube (CRT), liquid crystal display (LCD), etc., and speakers, etc.; a storage section 608 including a hard disk, etc.; and a communication section 609 including a network interface card such as a LAN card, modem, etc. The communication section 609 performs communication processing via a network such as the Internet. A drive 610 is also connected to the input / output (I / O) interface 605 as needed. A removable medium 611, such as a disk, optical disk, magneto-optical disk, semiconductor memory, etc., is installed on the drive 610 as needed so that computer programs read from it can be installed into the storage section 608 as needed.
[0128] Optionally, the method flow according to embodiments of the present invention can be implemented as a computer software program. For example, embodiments of the present invention include a computer program product comprising a computer program carried on a computer-readable storage medium, the computer program containing program code for performing the method shown in the flowchart. In such embodiments, the computer program can be downloaded and installed from a network via communication section 609, and / or installed from removable medium 611. When the computer program is executed by processor 601, it performs the functions defined in the system of embodiments of the present invention. Optionally, the systems, devices, apparatuses, modules, units, etc., described above can be implemented by computer program modules.
[0129] The present invention also provides a computer-readable storage medium, which may be included in the device / apparatus / system described in the above embodiments; or it may exist independently and not assembled into the device / apparatus / system. The computer-readable storage medium carries one or more programs, which, when executed, implement the lock execution method based on a transactional key-value database according to embodiments of the present invention.
[0130] Optionally, the computer-readable storage medium can be a non-volatile computer-readable storage medium. Examples include, but are not limited to: portable computer disks, hard disks, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof. In this invention, the computer-readable storage medium can be any tangible medium containing or storing a program that can be used by or in conjunction with an instruction execution system, apparatus, or device.
[0131] For example, optionally, the computer-readable storage medium may include ROM 602 and / or RAM 603 and / or one or more memories other than ROM 602 and RAM 603 as described above.
[0132] Embodiments of the present invention also include a computer program product comprising a computer program containing program code for executing the methods provided in the embodiments of the present invention. When the computer program product is run on an electronic device, the program code is used to enable the electronic device to implement the lock execution method based on a transactional key-value database provided in the embodiments of the present invention.
[0133] When the computer program is executed by the processor 601, it performs the functions defined in the system / apparatus of this embodiment of the invention. Optionally, the systems, apparatuses, modules, units, etc., described above can be implemented by computer program modules.
[0134] In one embodiment, the computer program may rely on a tangible storage medium such as an optical storage device or a magnetic storage device. In another embodiment, the computer program may also be transmitted and distributed in the form of signals over a network medium, and downloaded and installed via the communication section 609, and / or installed from the removable medium 611. The program code contained in the computer program can be transmitted using any suitable network medium, including but not limited to: wireless, wired, etc., or any suitable combination thereof.
[0135] Optionally, program code for executing the computer programs provided in the embodiments of the present invention can be written in any combination of one or more programming languages. Specifically, these computational programs can be implemented using high-level procedural and / or object-oriented programming languages, and / or assembly / machine languages. Programming languages include, but are not limited to, languages such as Java, C++, Python, "C", or similar programming languages. The program code can be executed entirely on the user's computing device, partially on the user's device, partially on a remote computing device, or entirely on a remote computing device or server. In cases involving remote computing devices, the remote computing device can be connected to the user's computing device via any type of network, including a local area network (LAN) or a wide area network (WAN), or it can be connected to an external computing device (e.g., via the Internet using an Internet service provider).
[0136] The flowcharts and block diagrams in the accompanying drawings illustrate the architecture, functionality, and operation of possible implementations of systems, methods, and computer program products according to various embodiments of the present invention. In this regard, each block in a flowchart or block diagram may represent a module, segment, or portion of code containing one or more executable instructions for implementing a specified logical function. It should also be noted that in some alternative implementations, the functions indicated in the blocks may occur in a different order than those indicated in the drawings. For example, two consecutively indicated blocks may actually be executed substantially in parallel, and they may sometimes be executed in reverse order, depending on the functions involved. It should also be noted that each block in a block diagram or flowchart, and combinations of blocks in a block diagram or flowchart, may be implemented using a dedicated hardware-based system that performs the specified function or operation, or using a combination of dedicated hardware and computer instructions. Those skilled in the art will understand that the features described in the various embodiments of the present invention can be combined and / or combined in various ways, even if such combinations or combinations are not explicitly described in the present invention. In particular, the features described in the various embodiments of the present invention can be combined and / or combined in various ways without departing from the spirit and teachings of the present invention. All such combinations and / or pairings fall within the scope of this invention.
[0137] The embodiments of the present invention have been described above. However, these embodiments are merely illustrative and not intended to limit the scope of the invention. Although various embodiments have been described above, this does not mean that the measures in the various embodiments cannot be used advantageously in combination. Various substitutions and modifications can be made by those skilled in the art without departing from the scope of the invention, and all such substitutions and modifications should fall within the scope of the invention.
Claims
1. A lock execution method based on a transactional key-value database, characterized by, The method comprises: in response to a first transaction operation request for a target resource in a transactional key-value database, reading a first lock key and a first lock value of a distributed lock corresponding to the target resource, wherein the first lock value comprises an instance identifier holding the distributed lock and valid timestamp information of the distributed lock, the first transaction operation request comprises a target instance identifier and a lease duration of the distributed lock; determining the occupation result of the distributed lock according to the first lock key and the first lock value comprises: in the case that the first lock key does not exist, determining that the occupation result of the distributed lock is not occupied; in the case that the first lock key exists and the response time of the first transaction operation request is greater than the valid timestamp information, determining that the occupation result of the distributed lock is not occupied; in the case that the first lock key exists and the response time of the first transaction operation request is less than or equal to the valid timestamp information, determining that the occupation result of the distributed lock is occupied; in the case that the occupation result indicates that the distributed lock is not occupied, updating the instance identifier and the valid timestamp information in the first lock value according to the target instance identifier and the lease duration, so that the target instance executes the transaction operation indicated by the first transaction operation request.
2. The method of claim 1, wherein, The method further comprises: during the execution of the transaction operation indicated by the first transaction operation request by the target instance, performing a lease renewal operation on the distributed lock corresponding to the target resource.
3. The method of claim 2, wherein, The lease renewal operation on the distributed lock corresponding to the target resource comprises: in response to a second transaction operation request for a target resource in a transactional key-value database, reading a second lock key of a distributed lock corresponding to the target resource, and obtaining a second lock value based on the second lock key, wherein the second transaction operation request is initiated based on a preset lease renewal frequency, and the preset lease renewal frequency is determined based on the lease duration of the distributed lock; performing identity verification on the instance identifier in the second lock value and the target instance identifier to obtain a verification result; in the case that the verification result is verification success, updating the valid timestamp information in the second lock value according to the response time of the second transaction operation request and the lease duration.
4. The method of claim 2, wherein, The method further comprises: after the execution of the transaction operation, stopping the lease renewal operation and releasing the distributed lock.
5. The method of claim 4, wherein, The release of the distributed lock comprises: in response to a third transaction operation request for a target resource in a transactional key-value database, reading a third lock key and a third lock value of a distributed lock corresponding to the target resource; in the case that the instance identifier in the third lock value is consistent with the target instance identifier, deleting the third lock key and the third lock value.
6. The method of claim 1, wherein, The method further comprises: in the case that the occupation result of the distributed lock is determined to be occupied, in response to a fourth transaction operation request for a target resource in a transactional key-value database, re-determining the occupation result of the distributed lock corresponding to the target resource.
7. A lock execution apparatus based on a transactional key-value database, characterized by, The device comprises: A response module is configured to read a first lock key and a first lock value of a distributed lock corresponding to a target resource in a transactional key-value database in response to a first transaction operation request for the target resource, wherein the first lock value includes an instance identifier of an instance holding the distributed lock and valid timestamp information of the distributed lock, and the first transaction operation request includes a target instance identifier and a lease duration of the distributed lock. A determination module is configured to determine an occupancy result of the distributed lock according to the first lock key and the first lock value, and the determination module includes a first determination submodule, a second determination submodule and a third determination submodule. The first determination submodule is configured to determine that the occupancy result of the distributed lock is unoccupied when the first lock key does not exist. The second determination submodule is configured to determine that the occupancy result of the distributed lock is unoccupied when the first lock key exists and a response time of the first transaction operation request is greater than the valid timestamp information. The third determination submodule is configured to determine that the occupancy result of the distributed lock is occupied when the first lock key exists and the response time of the first transaction operation request is less than or equal to the valid timestamp information. An update module is configured to update the instance identifier and the valid timestamp information in the first lock value according to the target instance identifier and the lease duration to enable a target instance to perform a transaction operation indicated by the first transaction operation request when the occupancy result indicates that the distributed lock is unoccupied.
8. An electronic device, comprising: One or more processors; a memory configured to store one or more instructions, wherein the one or more instructions, when executed by the one or more processors, cause the one or more processors to implement the method of any one of claims 1 to 6. The computer program, when invoked by a processor, implements the steps of the method according to any one of claims 1 to 6.
9. A computer readable storage medium having stored thereon a computer program, characterized in that,
Citation Information
Patent Citations
Method for executing timed task, storage medium and server
CN113282580A
Distributed queue lock execution method based on key-value database
CN118885284A