Implementation Method, Device, Electronic Device and Storage Medium of Distributed Lock
Through the combination of distributed data storage cluster and cluster management mechanism, the reliability problem of distributed locks is solved, automatic renewal and timely release of locks is realized, and data consistency and processor efficiency are ensured.
Patent Information
- Application Number
- CN202210111035.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-01-12
- Publication Date
- 2025-07-25
- Estimated Expiration
- 2042-01-12
AI Technical Summary
In the prior art, distributed locks have problems in the distributed system that locks cannot expire or cannot be released actively, resulting in lock loss and affecting data consistency.
Through the watchdog mechanism and cluster management mechanism based on the distributed data storage cluster architecture, lock reliability is achieved, and the lock call information is used to obtain the asynchronous listening task released by the lock, ensuring that the lock thread automatically releases the lock after the business logic is completed, and the thread waiting to acquire the lock is notified.
The reliability of distributed locks is realized, avoiding the early release and loss of locks, ensuring data consistency, and reducing processor consumption.
Smart Images

Figure CN114461410B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of software testing, and in particular to a method, device, electronic device and storage medium for implementing a distributed lock. Background Art
[0002] A distributed lock is an implementation of a lock that controls access to shared resources between distributed systems, and the main problem it solves is to ensure data consistency. In a single-process system, when there are multiple threads that can change a certain variable simultaneously, it is necessary to synchronize the variable or code block so that when modifying such a variable, it can be executed linearly to eliminate concurrent variable modification. In order to ensure that only one thread can execute the same code block at a certain moment among multiple threads, a mark needs to be made somewhere. This mark must be visible to each thread. When the mark does not exist, the mark can be set. The remaining subsequent threads will wait until the thread that owns the mark finishes the synchronized code block and cancels the mark, and then try to set the mark. In a distributed environment, processes may not even be on the same physical machine, so the mark needs to be stored in a place where all processes can see it. In the implementation of distributed locks in the prior art, in some cases, after a thread finishes execution, the lock cannot expire, or the lock cannot be released actively, or the phenomenon of lock loss occurs.
[0003] Therefore, a method for implementing a distributed lock is needed. Based on the distributed data storage cluster architecture, the reliability of locking is achieved through a watchdog mechanism, and threads that have not obtained the lock can timely obtain the message of lock release through a cluster management mechanism, so as to achieve a reliable implementation of the distributed lock. Summary of the Invention
[0004] For this reason, the present invention provides a method, device, electronic device and storage medium for implementing a distributed lock, in an attempt to solve or at least alleviate at least one of the above problems.
[0005] According to one aspect of the present invention, a method for implementing a distributed lock is provided. The method is based on the distributed data storage cluster architecture, and the reliability of locking is achieved through a watchdog mechanism. Threads that have not obtained the lock can timely obtain the message of lock release through a cluster management mechanism, so as to achieve a reliable implementation of the distributed lock. The method includes the steps of:
[0006] Obtain the connection address and connection pool of the configured distributed data storage cluster and the cluster management mechanism, create a keyword for the thread to be locked, and obtain lock call information. The keyword for the locked thread includes a lock key, a lock time, and a lock thread;
[0007] Obtain an asynchronous listening task for lock release according to the lock call information;
[0008] Obtain the information that the business logic execution of the locked thread is completed and the lock is released according to the asynchronous listening task for lock release;
[0009] Delete the asynchronous listening task for lock release according to the information that the business logic execution of the locked thread is completed and the lock is released, and obtain other threads waiting to acquire the lock to acquire the lock.
[0010] In another embodiment, the step of obtaining lock call information includes:
[0011] Judge whether the lock key exists in the keyword of the thread to be locked;
[0012] If the lock key does not exist in the keyword of the thread to be locked, obtain the information of the increased field value in the hash table of the thread to be locked, and the information of the increased field value includes the lock key, lock thread, lock increment step, and lock expiration time of the keyword of the locked thread;
[0013] If the lock key exists in the keyword of the thread to be locked and the lock thread is the thread to be locked, obtain the lock increment step and the lock reset expiration time information of the keyword of the thread to be locked;
[0014] Otherwise, obtain the lock expiration time.
[0015] In another embodiment, the step of obtaining the asynchronous listening task for lock release according to the lock call information further includes:
[0016] Obtain the ephemeral node created under the keyword directory of the thread to be locked through the cluster management mechanism according to the lock call information;
[0017] Obtain the path name and expiration time of the ephemeral node according to the ephemeral node created under the keyword directory of the thread to be locked through the cluster management mechanism;
[0018] Obtain the node event registered under the path name of the ephemeral node according to the path name and expiration time of the ephemeral node;
[0019] Obtain the asynchronous listening task for lock release of the thread to be locked according to the node event registered under the path name of the ephemeral node.
[0020] In another embodiment, the step of obtaining the asynchronous listening task for lock release according to the lock call information further includes:
[0021] Obtain the node event registered under the path name of the ephemeral node;
[0022] Obtain the renewal information of the node event according to the node event registered under the path name of the ephemeral node;
[0023] Re-register the node event under the path name of the temporary node according to the renewal information of the node event.
[0024] In another embodiment, the step of obtaining the asynchronous listening task for lock release according to the lock call information further includes:
[0025] Obtain the information that the lock call is not successful;
[0026] According to the information that the lock call is not successful, obtain the asynchronous listening task for the change of the keyword directory of the locked thread through the cluster management mechanism;
[0027] According to the asynchronous listening task for the change of the keyword directory of the locked thread obtained through the cluster management mechanism, obtain the lock key of the keyword of the locked thread for circular query and the path logic value of the node event corresponding to the keyword of the locked thread;
[0028] If the logical value is true, obtain the lock and stop the loop;
[0029] If the logical value is false, continue the loop.
[0030] In another embodiment, the step of obtaining the information that the business logic execution of the locked thread is completed and the lock is released according to the asynchronous listening task for lock release includes:
[0031] According to the asynchronous listening task for lock release, detect whether the lock key and the lock thread exist in the keyword of the thread to be locked;
[0032] If not, obtain the information that the unlocking is successful;
[0033] If they exist, obtain the information that the field value in the hash table of the locked thread decreases. The information that the field value decreases includes the lock key, the lock thread, the lock step size reduction, and the lock expiration time of the keyword of the locked thread;
[0034] If the logical value of the information that the field value in the hash table of the locked thread decreases is greater than 0, obtain the information for resetting the lock expiration time in the keyword of the locked thread;
[0035] If the logical value of the information that the field value in the hash table of the locked thread decreases is not greater than 0, delete the information in the keyword directory of the locked thread through the cluster management mechanism to perform successful unlocking;
[0036] According to the information that the unlocking is successful, obtain the information that the business logic execution of the locked thread is completed and the lock is released.
[0037] In another embodiment, the steps of deleting the asynchronous listening task for lock release, and obtaining other threads waiting to acquire the lock to acquire the lock based on the information that the business logic of the locked thread is executed and the lock is released include:
[0038] Based on the information that the business logic of the locked thread is executed and the lock is released, obtain the information for deleting the asynchronous listening task of the keyword directory of the locked thread through the cluster management mechanism;
[0039] Based on the information for deleting the asynchronous listening task of the keyword directory of the locked thread through the cluster management mechanism, obtain other threads waiting to acquire the lock to acquire the lock;
[0040] Based on the threads waiting to acquire the lock to acquire the lock, update the keyword of the thread and the path logic value of the node event corresponding to the keyword of the thread in the hash table of the threads waiting to acquire the lock.
[0041] According to another aspect of the present invention, there is provided an apparatus for implementing a distributed lock. The apparatus is based on a distributed data storage cluster architecture, realizes the reliability of locking through a watchdog mechanism, and enables threads that have not acquired the lock to timely obtain the message of lock release through a cluster management mechanism, thereby achieving a reliable implementation of the distributed lock. The apparatus includes: a lock creation module, configured to obtain the connection address and connection pool of the configured distributed data storage cluster and the cluster management mechanism, create the keyword of the thread to be locked, and obtain lock call information, where the keyword of the locked thread includes a lock key, a lock time, and a lock thread; a lock execution module, configured to obtain the asynchronous listening task for lock release based on the lock call information; and obtain the information that the business logic of the locked thread is executed and the lock is released based on the asynchronous listening task for lock release; a lock release module, configured to delete the asynchronous listening task for lock release based on the information that the business logic of the locked thread is executed and the lock is released, and obtain other threads waiting to acquire the lock to acquire the lock.
[0042] According to another aspect of the present invention, there is provided a computing device, including: one or more processors; and a memory; one or more programs, where the one or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs include instructions for executing any of the methods in the method for implementing a distributed lock as described above.
[0043] According to another aspect of the present invention, there is provided a computer-readable storage medium storing one or more programs, where the one or more programs include instructions that, when executed by a computing device, cause the computing device to execute any of the methods in the method for implementing a distributed lock as described above.
[0044] According to the implementation solution of the distributed lock of the present invention, by obtaining the connection addresses and connection pools of the configured distributed data storage cluster and the cluster management mechanism, creating keywords for the threads to be locked, and obtaining lock call information; according to the lock call information, obtaining an asynchronous listening task for lock release; according to the asynchronous listening task for lock release, obtaining information that the business logic execution of the locked thread is completed and the lock is released; according to the information that the business logic execution of the locked thread is completed and the lock is released, deleting the asynchronous listening task for lock release, and obtaining other threads waiting to acquire the lock to acquire the lock. This application can use the cluster management mechanism to renew the lock automatically through the watchdog after successfully locking, ensuring that the lock will not be released prematurely. The lock release notification implemented through the cluster management mechanism ensures that after the locked thread finishes execution, other threads waiting to acquire the lock can obtain the message in a timely manner, avoiding the threads waiting to acquire the lock from continuously looping and waiting for the lock acquisition request, and reducing the consumption of the processor. BRIEF DESCRIPTION OF THE DRAWINGS
[0045] To achieve the above and related purposes, certain illustrative aspects are described herein in conjunction with the following description and the accompanying drawings, which indicate various ways in which the principles disclosed herein can be practiced, and all aspects and their equivalent aspects are intended to fall within the scope of the claimed subject matter. The above and other objects, features, and advantages of the present disclosure will become more apparent by reading the following detailed description in conjunction with the accompanying drawings. Throughout the present disclosure, the same reference numerals generally refer to the same components or elements.
[0046] Figure 1 FIG. 9 shows a schematic structural diagram of a computing device 100 according to an embodiment of the present invention; and
[0047] Figure 2 FIG. 13 shows a flowchart of a method 200 for implementing a distributed lock according to an embodiment of the present invention; and
[0048] Figure 3 FIG. 17 shows a schematic structural diagram of an apparatus 300 for implementing a distributed lock according to an embodiment of the present invention. DETAILED DESCRIPTION
[0049] The exemplary embodiments of the present disclosure will be described in more detail below with reference to the accompanying drawings. Although the exemplary embodiments of the present disclosure are shown in the drawings, it should be understood that the present disclosure can be implemented in various forms and should not be limited by the embodiments set forth herein. On the contrary, these embodiments are provided so that the present disclosure can be more thoroughly understood and the scope of the present disclosure can be fully conveyed to those skilled in the art.
[0050] Figure 1is a block diagram of an example computing device 100. In a basic configuration 102, the computing device 100 typically includes a system memory 106 and one or more processors 104. A memory bus 108 can be used for communication between the processor 104 and the system memory 106.
[0051] Depending on the desired configuration, the processor 104 can be any type of processor, including but not limited to: a microprocessor (μP), a microcontroller (μC), a digital signal processor (DSP), or any combination thereof. The processor 104 can include one or more levels of cache such as a level 1 cache 110 and a level 2 cache 112, a processor core 114, and registers 116. An example processor core 114 can include an arithmetic logic unit (ALU), a floating point unit (FPU), a digital signal processing core (DSP core), or any combination thereof. An example memory controller 118 can be used with the processor 104, or in some implementations, the memory controller 118 can be an internal part of the processor 104.
[0052] Depending on the desired configuration, the system memory 106 can be any type of memory, including but not limited to: volatile memory (such as RAM), non-volatile memory (such as ROM, flash memory, etc.), or any combination thereof. The system memory 106 can include an operating system 120, one or more applications 122, and program data 124. In some embodiments, the application 122 can be arranged to operate on the operating system using the program data 124. In some embodiments, the computing device 100 is configured to execute a method 200 for implementing a distributed lock. The method 200 can achieve lock acquisition reliability based on a distributed data storage cluster architecture through a watchdog mechanism, and enable a thread that has not acquired the lock to timely obtain a message indicating the release of the lock through a cluster management mechanism, thereby achieving a reliable implementation of the distributed lock. The program data 124 contains instructions for executing the method 200.
[0053] The computing device 100 may also include an interface bus 140 that facilitates communication from various interface devices (e.g., output device 142, peripheral interface 144, and communication device 146) to the basic configuration 102 via the bus / interface controller 130. Example output devices 142 include a graphics processing unit 148 and an audio processing unit 150. They may be configured to facilitate communication with various external devices such as a display or speakers via one or more A / V ports 152. Example peripheral interfaces 144 may include a serial interface controller 154 and a parallel interface controller 156, which may be configured to facilitate communication with external devices such as input devices (e.g., keyboard, mouse, pen, voice input device, touch input device) or other peripherals (e.g., printer, scanner, etc.) via one or more I / O ports 158. Example communication device 146 may include a network controller 160, which may be arranged to facilitate communication with one or more other computing devices 162 via one or more communication ports 164 through a network communication link.
[0054] The network communication link may be an example of a communication medium. A communication medium generally may embody computer-readable instructions, data structures, program modules in a modulated data signal such as a carrier wave or other transport mechanism, and may include any information-delivery medium. A "modulated data signal" may be a signal that has one or more of its data set or its changes set in a manner that encodes information in the signal. By way of non-limiting example, the communication medium may include wired media such as a wired network or a dedicated line network, as well as various wireless media such as sound, radio frequency (RF), microwave, infrared (IR), or other wireless media. The term computer-readable medium as used herein may include both storage media and communication media. In some embodiments, one or more programs are stored in the computer-readable medium, and the one or more programs include instructions for performing certain methods, such as, according to an embodiment of the present invention, the computing device 100 executes the implementation method 200 of the distributed lock through the instructions.
[0055] The computing device 100 may be implemented as part of a small-form factor portable (or mobile) electronic device, which may be, for example, a cellular phone, a personal digital assistant (PDA), a personal media player device, a wireless network browsing device, a personal head-mounted device, an application-specific device, or a hybrid device that may include any of the above functions. The computing device 100 may also be implemented as a personal computer including a desktop computer and a laptop computer configuration.
[0056] Figure 2 A flowchart of an implementation method 200 of a distributed lock according to an embodiment of the present invention is shown. As Figure 2As shown, the method 200 is based on a distributed data storage cluster architecture. It realizes the locking reliability through a watchdog mechanism, and enables the threads that have not obtained the lock to timely obtain the message of lock release through a cluster management mechanism, thereby achieving a reliable implementation of the distributed lock. The method 200 starts from step S210, obtains the connection addresses and connection pools of the configured distributed data storage cluster and the cluster management mechanism, creates the keyword of the thread to be locked, and obtains the lock call information. The keyword of the locked thread includes the lock key, lock time, and lock thread.
[0057] Specifically, the distributed data storage cluster (Codis) is a distributed Redis solution, which is a cluster composed of multiple Redis nodes to meet the business requirements with large data volume and high concurrency. Under a single Redis node instance, in the case of large stored data volume and high concurrency, the memory is very likely to soar. At the same time, the memory of a single Redis node is also limited. One reason is that when the memory is too large, during data synchronization, full synchronization will cause too long time and increase the risk of synchronization failure; another reason is that generally Redis is deployed on cloud servers, which will also be affected by the CPU usage rate. Therefore, when facing a large amount of data, the Redis cluster solution is used for management, and at the same time, the CPU computing capabilities of so many Redis instances are pooled together to complete the read and write operations on large data and high concurrency. The cluster management mechanism (Zookeeper) is a distributed and open-source program coordination service, which is a sub-project under the Hadoop project. The main functions it provides include: configuration management, naming service, distributed lock, and cluster management. Through the distributed data storage cluster and the cluster management mechanism, the high availability of the distributed lock can be realized, ensuring the strong consistency of the data.
[0058] Specifically, in an embodiment of the present application, the step of obtaining the lock call information includes:
[0059] Judge whether the lock key in the keyword of the thread to be locked exists;
[0060] If the lock key in the keyword of the thread to be locked does not exist, obtain the information of the increase in the field value in the hash table of the thread to be locked. The information of the increase in the field value includes the lock key, lock thread, lock increment step, and lock expiration time of the keyword of the locked thread;
[0061] If the lock key in the keyword of the thread to be locked exists and the lock thread is the thread to be locked, obtain the lock increment step and the information of the reset expiration time of the lock of the keyword of the thread to be locked;
[0062] Otherwise, obtain the lock expiration time.
[0063] Specifically, for a thread with distributed execution, each thread is processed through locking. For example, in scenarios such as train tickets on "12306.cn" not being oversold and not allowing one ticket to be bought by multiple people, or not allowing an order to be paid twice, a distributed lock is required to protect shared resources. To ensure that only one thread can execute the same code block at a time among multiple threads, a marker needs to be set somewhere. This marker must be visible to each thread. When the marker does not exist, it can be set. If subsequent threads find that the marker already exists, they will wait until the thread that owns the marker finishes the synchronized code block and cancels the marker before attempting to set the marker again. The role of the lock is equivalent to this marker. Once a thread acquires the lock, that thread is in an executable state. After the task is completed, the lock needs to be released so that other processes can be executed because they have obtained the lock.
[0064] By configuring the connection addresses and connection pools of the distributed data storage cluster and the cluster management mechanism, the client can quickly obtain connections, avoiding the need to create new connections every time when communicating with the server, and reducing the communication cost.
[0065] For lock calls, first, it is necessary to check whether a thread has a lock key. The lock key is a flag for a thread to acquire the lock. If it does not exist, the hincrby command is used to achieve atomic increment. The hincrby command is used to add a specified increment value to the field value in the hash table. The increment value can also be negative, which is equivalent to performing a subtraction operation on the specified field. If the lock key of the hash table does not exist, a new hash table is created and the hincrby command is executed. If the specified field does not exist, the field value is initialized to 0 before the command is executed. At this time, the key of the set parameter is the lock key, the lock thread (field) is, the step parameter is 1, representing an increment of 1 each time, and at the same time, pexpire is used to give the lock key an expiration time. The pexpire is a command for setting the time. The survival time of the lock will continuously decrease as time passes. When the survival time of a key is exhausted, Redis will remove this lock.
[0066] Specifically, if the hexists command is used to determine that the lock key exists and the lock thread is this thread, the hincrby command will be used to increment the lock step by 1, and at the same time, pexpire will be used to reset the expiration time of the lock key.
[0067] Through step S220, an asynchronous listening task for lock release is obtained according to the lock call information.
[0068] Specifically, in order to ensure the reliability of the lock, an asynchronous listening task for lock release is set so that threads that have not acquired the lock can obtain the lock release message as soon as possible, avoiding the problem of unavailability of pub / sub commands in the codis environment.
[0069] Specifically, in one embodiment of the present application, the step of acquiring the asynchronous listening task for lock release according to the lock call information includes:
[0070] According to the lock call information, a temporary node created by a cluster management mechanism under the keyword directory of the thread that needs to be locked is obtained; specifically, the keyword directory of the thread that needs to be locked is: / codislock / watchdog / {key}.
[0071] According to the temporary node created under the keyword directory of the thread to be locked through the cluster management mechanism, the path name and expiration time of the temporary node are obtained; specifically, the path name of the temporary node is: uuid+threadId, and the expiration time of the temporary node is 3 / 4 of the expiration time of the lock.
[0072] According to the path name and expiration time of the temporary node, the node event registered under the path name of the temporary node is obtained; specifically, the path of the temporary node is: / codislock / watchdog / {key} / uuid+threadId.
[0073] According to the node event registered under the path name of the temporary node, the asynchronous listening task for releasing the lock of the thread that needs to be locked is obtained.
[0074] Specifically, in one embodiment of the present application, the step of acquiring the asynchronous monitoring task for lock release according to the lock call information further includes:
[0075] Obtain the node event registered under the path name of the temporary node;
[0076] According to the node event registered under the path name of the temporary node, obtaining the renewal information of the node event;
[0077] According to the renewal information of the node event, the node event under the path name of the temporary node is registered again.
[0078] Specifically, if the asynchronous monitoring task of the lock release monitors the node event in the / codislock / watchdog / {key} directory, it means that the locking thread has set the asynchronous monitoring task of the lock release. At this time, the contract is renewed by using the lua script, and then the monitoring event is registered again.
[0079] Specifically, in an embodiment of the present application, the step of obtaining the asynchronous listening task for lock release according to the lock call information further includes:
[0080] Obtain the information that the lock call is unsuccessful; specifically, if the lock call is unsuccessful, it means that the lock has not been obtained.
[0081] According to the information that the lock call is unsuccessful, obtain the asynchronous listening task for the change of the keyword directory of the locked thread through the cluster management mechanism; specifically, the program execution process is to subscribe to the event change Listener of the directory / codislock / {key} of zookeeper.
[0082] According to the asynchronous listening task for the change of the keyword directory of the locked thread obtained through the cluster management mechanism, obtain the lock key of the keyword of the locked thread for loop query and the path logical value of the node event corresponding to the keyword of the locked thread; specifically, the program execution process is: use the while command to loop query the value of the lock key uuid+threadid in ConcurrentHashMap.
[0083] If the logical value is true, obtain the lock and stop the loop;
[0084] If the logical value is false, continue the loop.
[0085] Through step S230, obtain the information that the business logic execution of the locked thread is completed and the lock is released according to the asynchronous listening task for lock release.
[0086] Specifically, after the thread obtains the lock, it starts to execute the business logic of the thread. When the business logic is executed, the logic of releasing the lock starts to be executed.
[0087] Specifically, in an embodiment of the present application, the step of obtaining the information that the business logic execution of the locked thread is completed and the lock is released according to the asynchronous listening task for lock release includes:
[0088] According to the asynchronous listening task for lock release, detect whether the lock key and the lock thread exist in the keyword of the thread to be locked; specifically, use the hexists command to detect whether the lock key and the lock thread exist.
[0089] If they do not exist, obtain the information that the unlocking is successful; specifically, when the lock key and the lock thread are not detected during the thread detection, it means that the thread does not have a lock. Then, the lock locked by the current thread has been successfully released.
[0090] If it exists, obtain the information on the reduction of the field value in the hash table of the locked thread. The information on the reduction of the field value includes the lock key, the locking thread, the lock reduction step, and the lock expiration time of the keyword of the locked thread. Specifically, if the thread has a lock key and a locking thread, it means that the thread has not released the lock yet. Then, the atomic subtraction operation needs to be performed using the hincrby command.
[0091] If the logical value of the information on the reduction of the field value in the hash table of the locked thread is greater than 0, obtain the information on resetting the lock expiration time in the keyword of the locked thread. Specifically, if the logical value of the information on the reduction of the field value in the hash table of the locked thread is greater than 0, it means that the thread lock is a reentrant lock, and the pexpire command needs to be used to reset the lock expiration time.
[0092] If the logical value of the information on the reduction of the field value in the hash table of the locked thread is not greater than 0, delete the information in the keyword directory of the locked thread through the cluster management mechanism to perform successful unlocking. Specifically, if the logical value of the information on the reduction of the field value in the hash table of the locked thread is not greater than 0, it means that the thread lock is a non-reentrant lock. At this time, the del command needs to be executed to delete the lock key.
[0093] According to the successful unlocking information, obtain the information that the business logic of the locked thread is executed and the lock is released. Specifically, after successful unlocking, the lock release event of the thread needs to be published to allow other threads waiting to obtain the lock to continue to execute to obtain the lock.
[0094] Through step S240, according to the information that the business logic of the locked thread is executed and the lock is released, delete the asynchronous listening task for the lock release, and obtain other threads waiting to obtain the lock to obtain the lock.
[0095] Specifically, when the lock locked by the thread is released, it means that the business logic of the thread has been executed. At this time, the asynchronous listening task timely obtains the lock release information of the locked thread and sends this information to all threads waiting to obtain the lock through broadcasting. The threads waiting to obtain the lock will obtain the lock according to the set rules.
[0096] Specifically, in an embodiment of the present application, the step of deleting the asynchronous listening task for the lock release and obtaining other threads waiting to obtain the lock according to the information that the business logic of the locked thread is executed and the lock is released includes:
[0097] According to the information that the business logic of the locked thread is executed and the lock is released, obtain the information on deleting the asynchronous listening task of the keyword directory of the locked thread through the cluster management mechanism. Specifically, the program execution process is: delete the directory / codislock / {key} of zookeeper.
[0098] According to the asynchronous listening task deletion information of the keyword directory of the locked thread through the cluster management mechanism, other threads waiting to acquire the lock are obtained for lock acquisition; specifically, other threads waiting to acquire the lock will compete for the distributed lock according to the set rules.
[0099] According to the threads waiting to acquire the lock for lock acquisition, the keyword of the thread and the path logical value of the node event corresponding to the keyword of the thread in the hash table of the threads waiting to acquire the lock are updated. Specifically, the program execution process is: update the lock key of ConcurrentHashMap with the value of uuid + threadid to true.
[0100] According to the implementation solution of the distributed lock of the present invention, by obtaining the connection address and connection pool of the configured distributed data storage cluster and the cluster management mechanism, the keyword for locking the thread is created, and the lock call information is obtained; according to the lock call information, the asynchronous listening task for lock release is obtained; according to the asynchronous listening task for lock release, the information that the business logic execution of the locked thread is completed and the lock is released is obtained; according to the information that the business logic execution of the locked thread is completed and the lock is released, the asynchronous listening task for lock release is deleted, and other threads waiting to acquire the lock are obtained for lock acquisition. This application can use the cluster management mechanism to implement automatic lock renewal for the locked thread through the watchdog after the lock acquisition is successful, ensuring that the lock will not be released in advance. The lock release notification implemented through the cluster management mechanism ensures that after the locked thread finishes execution, other threads waiting to acquire the lock can obtain the message in a timely manner, avoiding the threads waiting to acquire the lock from continuously looping and waiting for the lock acquisition request, and reducing the consumption of the processor.
[0101] It should be understood that although Figure 2 the steps in the flowchart are shown in sequence according to the arrows, these steps are not necessarily executed in the order indicated by the arrows. Unless specifically stated in this article, there is no strict order restriction for the execution of these steps, and these steps can be executed in other orders. Moreover, Figure 2 at least a part of the steps in
[0102] In one embodiment, as Figure 3As shown, an implementation device 300 for a distributed lock is provided. The device 300 is based on a distributed data storage cluster architecture, realizes the reliability of locking through a watchdog mechanism, and enables threads that have not obtained the lock to timely obtain the message of lock release through a cluster management mechanism, thereby achieving the reliable implementation of the distributed lock. The device 300 includes: a lock creation module, a lock execution module, and a lock release module.
[0103] The lock creation module is used to obtain the connection address and connection pool of the configured distributed data storage cluster and the cluster management mechanism, create the keyword for the thread to be locked, and obtain the lock call information. The keyword for the locked thread includes a lock key, a lock time, and a lock thread. The lock execution module is used to obtain the asynchronous listening task for lock release according to the lock call information; obtain the information that the business logic execution of the locked thread is completed and the lock is released according to the asynchronous listening task for lock release. The lock release module is used to delete the asynchronous listening task for lock release according to the information that the business logic execution of the locked thread is completed and the lock is released, and obtain other threads waiting to obtain the lock to obtain the lock.
[0104] Specifically, in another embodiment of the present application, the lock creation module is used to determine whether the lock key exists in the keyword of the thread to be locked; if the lock key does not exist in the keyword of the thread to be locked, obtain the information of the increased field value in the hash table of the thread to be locked. The information of the increased field value includes the lock key, lock thread, lock increment step, and lock expiration time of the keyword of the locked thread; if the lock key exists in the keyword of the thread to be locked and the lock thread is the thread to be locked, obtain the lock increment step and the information of the reset expiration time of the lock of the keyword of the thread to be locked; otherwise, obtain the lock expiration time.
[0105] Specifically, in another embodiment of the present application, the lock execution module is used to obtain the temporary node created under the keyword directory of the thread to be locked through the cluster management mechanism according to the lock call information; obtain the path name and expiration time of the temporary node according to the temporary node created under the keyword directory of the thread to be locked through the cluster management mechanism; obtain the node event registered under the path name of the temporary node according to the path name and expiration time of the temporary node; obtain the asynchronous listening task for lock release of the thread to be locked according to the node event registered under the path name of the temporary node.
[0106] Specifically, in another embodiment of the present application, the lock execution module is used to obtain the node event registered under the path name of the temporary node; obtain the renewal information of the node event according to the node event registered under the path name of the temporary node; re-register the node event under the path name of the temporary node according to the renewal information of the node event.
[0107] Specifically, in another embodiment of the present application, the lock execution module is used to obtain the information that the lock call fails; according to the information that the lock call fails, an asynchronous monitoring task for changes in the keyword directory of the locked thread is obtained through the cluster management mechanism; according to the asynchronous monitoring task for changes in the keyword directory of the locked thread obtained through the cluster management mechanism, the lock key of the keyword of the locked thread for loop query and the path logical value of the node event corresponding to the keyword of the locked thread are obtained; if the logical value is true, a lock is obtained and the loop is stopped; if the logical value is false, the loop continues.
[0108] Specifically, in another embodiment of the present application, the lock execution module is used to detect whether the lock key and the lock thread exist in the keyword of the thread to be locked according to the asynchronous monitoring task of lock release; if not, the information that the unlocking is successful is obtained; if they exist, the information that the field value in the hash table of the locked thread decreases is obtained, and the information that the field value decreases includes the lock key, the lock thread, the lock reduction step, and the lock expiration time of the keyword of the locked thread; if the logical value of the information that the field value in the hash table of the locked thread decreases is greater than 0, the information for resetting the lock expiration time in the keyword of the locked thread is obtained; if the logical value of the information that the field value in the hash table of the locked thread decreases is not greater than 0, the information in the keyword directory of the locked thread is deleted through the cluster management mechanism to perform successful unlocking; according to the information that the unlocking is successful, the information that the business logic execution of the locked thread is completed and the lock is released is obtained.
[0109] Specifically, in another embodiment of the present application, the lock release module is used to obtain the information for deleting the asynchronous monitoring task of the keyword directory of the locked thread through the cluster management mechanism according to the information that the business logic execution of the locked thread is completed and the lock is released; according to the information for deleting the asynchronous monitoring task of the keyword directory of the locked thread obtained through the cluster management mechanism, other threads waiting to obtain the lock are enabled to obtain the lock; according to the threads waiting to obtain the lock to obtain the lock, the keyword of the thread in the hash table of the threads waiting to obtain the lock and the path logical value of the node event corresponding to the keyword of the thread are updated.
[0110] According to the implementation solution of the distributed lock of the present invention, the connection address and connection pool of the configured distributed data storage cluster and the cluster management mechanism are obtained through the lock creation module, the keyword for the thread to be locked is created, and the lock call information is obtained; the lock execution module obtains the asynchronous monitoring task for lock release according to the lock call information; according to the asynchronous monitoring task for lock release, the information that the business logic execution of the locked thread is completed and the lock is released is obtained; the lock release module deletes the asynchronous monitoring task for lock release according to the information that the business logic execution of the locked thread is completed and the lock is released, and obtains other threads waiting to acquire the lock to acquire the lock. This application can utilize the cluster management mechanism to implement automatic lock renewal for the locked thread through the watchdog after the lock acquisition is successful, ensuring that the lock will not be released prematurely. The lock release notification implemented through the cluster management mechanism ensures that after the locked thread finishes execution, other threads waiting to acquire the lock can receive the message in a timely manner, avoiding the threads waiting to acquire the lock from continuously looping and waiting for the lock acquisition request, and reducing the consumption of the processor.
[0111] It should be understood that, in order to streamline the present disclosure and assist in understanding one or more of the various inventive aspects, in the above description of the exemplary embodiments of the present invention, the various features of the present invention are sometimes grouped together into a single embodiment, figure, or description thereof. However, the disclosed method should not be construed as reflecting an intention that the claimed invention requires more features than are expressly recited in each claim. Rather, as reflected in the following claims, the inventive aspects lie in less than all the features of the single foregoing disclosed embodiment. Thus, the claims following the detailed description are hereby expressly incorporated into the detailed description, with each claim standing on its own as a separate embodiment of the present invention.
[0112] Those skilled in the art should understand that the modules or units or components of the devices in the examples disclosed herein may be arranged in the devices as described in the embodiments, or alternatively may be located in one or more devices different from the devices in the examples. The modules in the foregoing examples may be combined into one module or further divided into multiple sub-modules.
[0113] Those skilled in the art can understand that the modules in the devices in the embodiments can be adaptively changed and set in one or more devices different from the embodiments. The modules or units or components in the embodiments can be combined into one module or unit or component, and in addition, they can be divided into multiple sub-modules or sub-units or sub-components. Except that at least some of such features and / or processes or units are mutually exclusive, any combination can be adopted to combine all the features disclosed in this specification (including the accompanying claims, abstract and drawings) and all the processes or units of any method or device so disclosed. Unless otherwise explicitly stated, each feature disclosed in this specification (including the accompanying claims, abstract and drawings) can be replaced by an alternative feature that provides the same, equivalent or similar purpose.
[0114] In addition, those skilled in the art can understand that although some of the embodiments described herein include certain features included in other embodiments rather than other features, the combination of the features of different embodiments means that it is within the scope of the present invention and forms different embodiments. For example, in the following claims, any one of the claimed embodiments can be used in any combination.
[0115] In addition, some of the embodiments are described herein as a combination of methods or method elements that can be implemented by a processor of a computer system or by other devices performing the functions. Therefore, a processor having the necessary instructions for implementing the method or method element forms a device for implementing the method or method element. In addition, the elements described herein in the device embodiments are examples of the following devices: the device is used to implement the functions performed by the elements for the purpose of implementing the invention.
[0116] As used herein, unless otherwise specified, the use of ordinal numbers "first", "second", "third", etc. to describe ordinary objects only indicates different instances of similar objects, and does not intend to imply that the objects so described must have a given order in terms of time, space, sorting, or in any other way.
[0117] Although the present invention has been described in terms of a limited number of embodiments, those skilled in the art, having the benefit of the foregoing description, will appreciate that other embodiments can be contemplated within the scope of the invention as thus described. Further, it should be noted that the language used in this specification has been principally selected for readability and instructional purposes and not to limit or circumscribe the inventive subject matter. Accordingly, many modifications and variations will be apparent to those of ordinary skill in the art without departing from the scope and spirit of the appended claims. For the scope of the present invention, the disclosure herein is illustrative and not restrictive, and the scope of the invention is defined by the appended claims.
Claims
1. A method for implementing a distributed lock. The method is based on a distributed data storage cluster architecture, realizes the reliability of locking through a watchdog mechanism, and enables threads that have not acquired the lock to obtain the message of lock release in a timely manner through a cluster management mechanism, so as to achieve a reliable implementation of the distributed lock. The method includes the steps: Obtain the connection addresses and connection pools of the configured distributed data storage cluster and the cluster management mechanism, create a keyword for the thread to be locked, and obtain lock call information. The keyword for the locked thread includes a lock key, a lock time, and a lock thread; According to the lock call information, obtain an asynchronous listening task for lock release, including: according to the lock call information, obtain a temporary node created by the cluster management mechanism under the keyword directory of the thread to be locked; according to the temporary node created by the cluster management mechanism under the keyword directory of the thread to be locked, obtain the path name and expiration time of the temporary node; according to the path name and expiration time of the temporary node, obtain the node events registered under the path of the temporary node; according to the node events registered under the path of the temporary node, obtain the asynchronous listening task for lock release of the thread to be locked; According to the asynchronous listening task for lock release, obtain the information that the business logic of the locked thread is executed and the lock is released; According to the information that the business logic of the locked thread is executed and the lock is released, delete the asynchronous listening task for lock release, and obtain other threads waiting to acquire the lock to acquire the lock.
2. The method according to claim 1, wherein The step of obtaining the lock call information includes: Judge whether the lock key exists in the keyword of the thread to be locked; If the lock key does not exist in the keyword of the thread to be locked, obtain the information of the increased field value in the hash table of the thread to be locked. The information of the increased field value includes the lock key, lock thread, lock increment step, and lock expiration time of the keyword of the locked thread; If the lock key exists in the keyword of the thread to be locked and the lock thread is the thread to be locked, obtain the lock increment step and the information of the reset expiration time of the lock of the keyword of the thread to be locked; Otherwise, obtain the lock expiration time.
3. The method according to claim 1, wherein The step of obtaining the asynchronous listening task for lock release according to the lock call information further includes: Obtain the node events registered under the path of the temporary node; According to the node events registered under the path of the temporary node, obtain the renewal information of the node events; According to the renewal information of the node events, re-register the node events under the path of the temporary node.
4. The method according to claim 1, wherein, The step of obtaining the asynchronous listening task for lock release according to the lock call information further includes: Obtain the information that the lock call is unsuccessful; According to the information that the lock call is unsuccessful, obtain the change asynchronous listening task of the keyword directory of the locked thread through the cluster management mechanism; According to the change asynchronous listening task of the keyword directory of the locked thread obtained through the cluster management mechanism, obtain the lock key of the keyword of the locked thread for loop query and the path logic value of the node event corresponding to the keyword of the locked thread; If the logical value is true, obtain the lock and stop the loop; If the logical value is false, continue the loop.
5. The method according to claim 1, wherein, The step of obtaining the information that the business logic execution of the locked thread is completed and the lock is released according to the asynchronous listening task released by the lock includes: According to the asynchronous listening task released by the lock, detect whether the lock key and the lock thread exist in the keyword of the thread to be locked; If not, obtain the information of successful unlocking; If so, obtain the information of the decrease of the field value in the hash table of the locked thread, and the information of the decrease of the field value includes the lock key, the lock thread, the lock reduction step, and the lock expiration time of the keyword of the locked thread; If the logical value of the information of the decrease of the field value in the hash table of the locked thread is greater than 0, obtain the information of resetting the lock expiration time in the keyword of the locked thread; If the logical value of the information of the decrease of the field value in the hash table of the locked thread is not greater than 0, delete the information in the keyword directory of the locked thread through the cluster management mechanism to successfully unlock the lock; According to the information of successful unlocking, obtain the information that the business logic execution of the locked thread is completed and the lock is released.
6. The method according to claim 1, wherein, The step of deleting the asynchronous listening task released by the lock and obtaining other threads waiting to obtain the lock to obtain the lock according to the information that the business logic execution of the locked thread is completed and the lock is released includes: According to the information that the business logic execution of the locked thread is completed and the lock is released, obtain the information of deleting the asynchronous listening task of the keyword directory of the locked thread through the cluster management mechanism; According to the information of deleting the asynchronous listening task of the keyword directory of the locked thread through the cluster management mechanism, obtain other threads waiting to obtain the lock to obtain the lock; According to the thread waiting to obtain the lock to obtain the lock, update the keyword of the thread in the hash table of the thread waiting to obtain the lock and the path logical value of the node event corresponding to the keyword of the thread.
7. An implementation device of a distributed lock. The device is based on a distributed data storage cluster architecture, realizes the reliability of locking through a watchdog mechanism, and enables threads that have not obtained the lock to obtain the lock release message in time through a cluster management mechanism, so as to achieve the reliable implementation of the distributed lock. The device includes: A lock creation module, configured to obtain the connection address and connection pool of the configured distributed data storage cluster and the cluster management mechanism, create a keyword for the thread to be locked, and obtain lock call information. The keyword of the locked thread includes a lock key, a lock time, and a lock thread; A lock execution module, configured to obtain an asynchronous listening task for lock release according to the lock call information, including obtaining a temporary node created under the keyword directory of the thread to be locked through a cluster management mechanism according to the lock call information, obtaining the path name and expiration time of the temporary node according to the temporary node created under the keyword directory of the thread to be locked through the cluster management mechanism, obtaining the node event registered under the path name of the temporary node according to the path name and expiration time of the temporary node, and obtaining the asynchronous listening task for lock release of the thread to be locked according to the node event registered under the path name of the temporary node; obtaining information that the business logic execution of the locked thread is completed and the lock is released according to the asynchronous listening task for lock release; A lock release module, configured to delete the asynchronous listening task for lock release according to the information that the business logic execution of the locked thread is completed and the lock is released, and obtain other threads waiting to obtain the lock to obtain the lock.
8. An electronic device, comprising: One or more processors; And A memory; One or more programs, wherein the one or more programs are stored in the memory and configured to be executed by the one or more processors, and the one or more programs include instructions for executing any of the methods according to claims 1-6.
9. A computer-readable storage medium storing one or more programs, the one or more programs including instructions that, when executed by a computing device, cause the computing device to execute any of the methods according to claims 1-6.
Citation Information
Patent Citations
Asynchronous distributed lock implementation method and device, storage medium and electronic equipment
CN111897646A
Efficient distributed lock manager
US20120310881A1