CN117667421A - Method, device, computer equipment and storage medium for realizing distributed lock - Google Patents

Method, device, computer equipment and storage medium for realizing distributed lock Download PDF

Info

Publication number
CN117667421A
CN117667421A CN202311733530.2A CN202311733530A CN117667421A CN 117667421 A CN117667421 A CN 117667421A CN 202311733530 A CN202311733530 A CN 202311733530A CN 117667421 A CN117667421 A CN 117667421A
Authority
CN
China
Prior art keywords
locking
current thread
lock
state
access resource
Prior art date
Legal status (The legal status is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the status listed.)
Pending
Application number
CN202311733530.2A
Other languages
Chinese (zh)
Inventor
孙长平
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
China Life Insurance Co ltd
Original Assignee
China Life Insurance Co ltd
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by China Life Insurance Co ltd filed Critical China Life Insurance Co ltd
Priority to CN202311733530.2A priority Critical patent/CN117667421A/en
Publication of CN117667421A publication Critical patent/CN117667421A/en
Pending legal-status Critical Current

Links

Classifications

    • YGENERAL TAGGING OF NEW TECHNOLOGICAL DEVELOPMENTS; GENERAL TAGGING OF CROSS-SECTIONAL TECHNOLOGIES SPANNING OVER SEVERAL SECTIONS OF THE IPC; TECHNICAL SUBJECTS COVERED BY FORMER USPC CROSS-REFERENCE ART COLLECTIONS [XRACs] AND DIGESTS
    • Y02TECHNOLOGIES OR APPLICATIONS FOR MITIGATION OR ADAPTATION AGAINST CLIMATE CHANGE
    • Y02DCLIMATE CHANGE MITIGATION TECHNOLOGIES IN INFORMATION AND COMMUNICATION TECHNOLOGIES [ICT], I.E. INFORMATION AND COMMUNICATION TECHNOLOGIES AIMING AT THE REDUCTION OF THEIR OWN ENERGY USE
    • Y02D10/00Energy efficient computing, e.g. low power processors, power management or thermal management

Landscapes

  • Information Retrieval, Db Structures And Fs Structures Therefor (AREA)

Abstract

The present application relates to a method, apparatus, computer device, storage medium and computer program product for implementing a distributed lock. By responding to the starting instruction, identifying distributed lock information, when the distributed lock information is detected to be triggered, determining a locking keyword corresponding to the current thread, locking the current thread by adopting the locking keyword, acquiring a locking state, adding the current thread into a waiting queue if the locking state is a locking failure and the locking type is a timeout lock or a waiting lock, detecting whether target access resources are released according to a preset time interval, and locking the current thread if the target access resources are detected to be released and the waiting time of the current thread is smaller than the target waiting time corresponding to the locking type. According to the embodiment, automatic locking is realized through annotating codes or a pre-configured locking strategy, so that the complexity of codes is reduced, the locking realization process is simplified, and the locking efficiency is improved.

Description

Method, device, computer equipment and storage medium for realizing distributed lock
Technical Field
The present invention relates to the field of computer technology, and in particular, to a method, an apparatus, a computer device, a storage medium, and a computer program product for implementing a distributed lock.
Background
With the development of computer technology, the concept of "lock" has emerged, such as mutual exclusion lock of an operating system, and process or thread level lock supported by itself in various high-level languages, to secure access. With the widespread popularity of distributed applications, distributed locks are becoming more and more widely used, which are a way to control multiple users to access shared resources synchronously between distributed systems.
In the conventional technology, the distributed lock is generally implemented based on a database self-lock, a zookeeper (distributed application coordination service) or a Redis (Remote Dictionary Server, an open-source high-performance memory data storage system) redlock (an implementation manner of the distributed lock in the Redis) algorithm. However, when the traditional distributed lock is realized based on the database self-lock, the zookeeper and the redlock of the Redis, more codes need to be realized, so that the realization process is complicated and the efficiency is low.
Disclosure of Invention
In view of the foregoing, it is desirable to provide a method, apparatus, computer device, computer readable storage medium, and computer program product for implementing a distributed lock that can improve locking efficiency and simplify locking procedures.
In a first aspect, the present application provides a method for implementing a distributed lock, where the method includes:
in response to the initiation instruction, identifying distributed lock information, the distributed lock information including a lock type;
when the distributed lock information is detected to be triggered, determining a locking keyword corresponding to the current thread;
locking the current thread by adopting the locking key word, and acquiring a locking state;
if the locking state is a locking failure and the lock type is overtime lock or waiting lock, adding the current thread into a waiting queue, wherein the locking failure is used for representing that a target access resource corresponding to the current thread in a distributed system is locked by other threads;
detecting whether the target access resource is released or not according to a preset time interval, and locking the current thread if the target access resource is released and the waiting time of the current thread is smaller than the target waiting time corresponding to the lock type.
In one embodiment, the method further comprises:
and if the locking state is failed locking and the lock type is immediate locking, returning a prompt message of failed locking.
In one embodiment, the method further comprises:
and if the locking state is successful, determining that the lock node state corresponding to the current thread is an operating state, and executing task operation corresponding to the current thread.
In one embodiment, the locking the current thread includes:
locking the current thread by adopting the locking key word, and acquiring a locking state;
if the locking state is successful, executing task operation corresponding to the current thread;
and if the locking state is the locking failure, returning to execute the step of detecting whether the target access resource is released or not according to the preset time interval.
In one embodiment, the method further comprises:
after the current thread is added into a waiting queue, determining that the lock node state corresponding to the current thread is a waiting state;
if the locking state is successful, deleting the current thread from the waiting queue, and determining that the lock node state corresponding to the current thread is an operation state;
and if the task operation is completed, determining the lock node state corresponding to the current thread as a completion state.
In one embodiment, after the performing the task operation corresponding to the current thread, the method further includes:
and if the task operation is completed and the target access resource is determined to be locked by the current thread, releasing the lock of the current thread to release the target access resource.
In one embodiment, the method further comprises:
if the target access resource is detected to be released and the waiting time of the current thread is longer than or equal to the target waiting time corresponding to the lock type, returning a prompt message of lock failure;
and if the target access resource is detected not to be released and the waiting time of the current thread is smaller than the target waiting time corresponding to the lock type, returning to the step of detecting whether the target access resource is released according to the preset time interval.
In a second aspect, the present application further provides an implementation apparatus of a distributed lock, where the apparatus includes:
the lock information identification module is used for responding to the starting instruction and identifying distributed lock information, wherein the distributed lock information comprises a lock type;
the first detection module is used for determining a locking keyword corresponding to the current thread when the distributed lock information is detected to be triggered;
The locking state acquisition module is used for locking the current thread by adopting the locking key words and acquiring a locking state;
the thread processing module is used for adding the current thread into a waiting queue if the locking state is a locking failure and the locking type is a timeout lock or a waiting lock, wherein the locking failure is used for representing that a target access resource corresponding to the current thread in the distributed system is locked by other threads;
and the second detection module is used for detecting whether the target access resource is released or not according to a preset time interval, and locking the current thread if the target access resource is detected to be released and the waiting time of the current thread is smaller than the target waiting time corresponding to the lock type.
In a third aspect, the present application also provides a computer device comprising a memory storing a computer program and a processor implementing the steps of the above method when the processor executes the computer program.
In a fourth aspect, the present application also provides a computer readable storage medium having stored thereon a computer program which when executed by a processor performs the steps of the above-described method.
In a fifth aspect, the present application also provides a computer program product comprising a computer program which, when executed by a processor, implements the steps of the method described above.
The method, the device, the computer equipment, the storage medium and the computer program product for realizing the distributed lock are characterized in that the distributed lock information is identified by responding to a starting instruction, when the distributed lock information is detected to be triggered, the locking key corresponding to the current thread is determined, the current thread is locked by adopting the locking key, the locking state is obtained, if the locking state is locking failure and the locking type is overtime lock or waiting lock, the current thread is added into a waiting queue, whether the target access resource is released is detected according to a preset time interval, and if the target access resource is detected to be released and the waiting time length of the current thread is smaller than the target waiting time length corresponding to the locking type, the current thread is locked. According to the embodiment, automatic locking is realized through annotating codes or a pre-configured locking strategy, so that the complexity of codes is reduced, the locking realization process is simplified, and the locking efficiency is improved.
Drawings
In order to more clearly illustrate the embodiments of the present application or the technical solutions in the related art, the drawings that are required to be used in the embodiments or the related technical descriptions will be briefly described below, and it is obvious that the drawings in the following description are only some embodiments of the present application, and other drawings may be obtained according to the drawings without inventive effort for a person having ordinary skill in the art.
FIG. 1 is an application environment diagram of a method of implementing a distributed lock in one embodiment;
FIG. 2 is a flow diagram of a method of implementing a distributed lock in one embodiment;
FIG. 3 is a flow chart of a method of implementing a distributed lock in another embodiment;
FIG. 4 is a block diagram of an implementation of a distributed lock in one embodiment;
fig. 5 is an internal structural diagram of a computer device in one embodiment.
Detailed Description
In order to make the objects, technical solutions and advantages of the present application more apparent, the present application will be further described in detail with reference to the accompanying drawings and examples. It should be understood that the specific embodiments described herein are for purposes of illustration only and are not intended to limit the present application.
Traditional locks based on databases are basically realized by relying on the databases, and more codes need to be realized, or the availability of the locks is judged based on a global variable, so that pressure is added to the databases, and the overall performance and efficiency of the databases are affected. The lock based on zookeer not only needs more codes to be realized, but also has poorer concurrency performance. The Redis-based lock has the greatest problems that multiple sets of Redis are required to be deployed, the performance is poor under high concurrency, the implementation process is complex, the Redlock algorithm is not used, deadlock is likely to exist due to the fact that the Redlock algorithm is too simple, or other threads release locks which do not belong to the threads, the whole lock is invalid, in addition, codes which need to be implemented are too many, and a developer with certain experience is required to realize the corresponding codes. And neither the above conventional manner can implement a timeout lock nor a wait lock, resulting in a single lock type.
Based on this, the embodiment of the application provides a method for implementing a distributed lock, which can be applied to a distributed system including a plurality of terminals as shown in fig. 1. Wherein a plurality of terminals 102 communicate with a server 104 via a network. The data storage system may store data that the server 104 needs to process. The data storage system may be integrated on the server 104 or may be located on a cloud or other network server. In this embodiment, a corresponding application program is installed in the terminal, and the application program is a computer program for performing one or more specific tasks, and is operated in a user mode, and can interact with a user, so that a visual user interface is provided.
Specifically, when an application program of any terminal is started, the distributed lock information can be automatically identified, and when the distributed lock information is detected to be triggered, a locking keyword corresponding to a current thread is determined; locking the current thread by adopting a locking keyword, and acquiring a locking state; if the locking state is failed locking and the lock type is overtime lock or waiting lock, adding the current thread into a waiting queue, detecting whether the target access resource is released according to a preset time interval, and if the target access resource is detected to be released and the waiting time of the current thread is smaller than the target waiting time corresponding to the lock type, locking the current thread, thereby efficiently realizing automatic locking.
The terminal 102 may be, but not limited to, various personal computers, notebook computers, smart phones, tablet computers, internet of things devices, and portable wearable devices, where the internet of things devices may be smart speakers, smart televisions, smart air conditioners, smart vehicle devices, and the like. The portable wearable device may be a smart watch, smart bracelet, headset, or the like. The server 104 may be implemented as a stand-alone server or as a server cluster of multiple servers.
In an exemplary embodiment, as shown in fig. 2, a method for implementing a distributed lock is provided, and the method is applied to any terminal 102 in fig. 1 for illustration, and includes the following steps:
step 202, in response to a start instruction, identifies distributed lock information.
Wherein the start instruction is an instruction or command for starting a target application in the terminal. In particular, the target application may be a computer program for performing some specific task or tasks, for example, a kernel insurance program. The distributed lock information may be a method that requires locking, including at least a lock type.
In this embodiment, when the target application program in the terminal receives the start instruction, it can automatically identify the distributed lock information, that is, automatically identify the method that needs to be locked in the program when starting. Wherein the automatic identification may be achieved by identifying the target annotation code or may be achieved based on a pre-configured locking policy. For example, the target annotation code may be an enhancement or augmentation annotation made on the part of the code that needs to be locked in the program, in advance with bytecode enhancement or tangent plane enhancement. The preconfigured locking policy may be a prefix and a suffix of a preconfigured packet path to be locked, or a preassigned service keyword to be locked, etc. It will be appreciated that the target annotation code or the pre-configured locking policy includes a lock type that needs to be locked, where the lock type includes, but is not limited to, an immediate lock, a timeout lock, and a wait lock.
Step 204, when the distributed lock information is detected to be triggered, determining a locking key corresponding to the current thread.
Where the function of a thread is to perform a particular task in an application program, such as a program segment, a function, etc. The current thread refers to a thread generated when the distributed lock information is triggered, that is, a thread generated when a program or a function corresponding to the distributed lock information is called.
The locking key refers to a key for locking the current thread, and specifically, the locking key includes a key, i.e., a key value. In this embodiment, the key of the locking key may be calculated by using a method, specifically, the key value may be calculated by using SpEL (Spring Expression Language ) and a method; the function can be customized, and key values are calculated based on the customized function and the method; key values may also be calculated based on a combination of SpEL and custom functions. The present embodiment is not limited thereto.
Step 206, locking the current thread by using the locking key, and obtaining the locking state.
The locking state comprises a successful locking state or a failed locking state. In this embodiment, after the target application program of the terminal determines the locking key corresponding to the current thread based on the above steps, the locking key may be adopted to attempt to lock the current thread, that is, attempt to lock the target access resource corresponding to the current thread, and obtain the corresponding locking state. Specifically, the target application program may send a locking instruction to the corresponding server, so that the server performs locking processing according to the locking instruction, and returns a locking result including a locking state. If the server locks the current thread successfully according to the locking instruction, a result of successful locking is returned, and if the server fails to lock the current thread according to the locking instruction, a result of failure locking is returned.
If the locking status is a failed locking status and the lock type is a timeout lock or a wait lock, step 208, the current thread is added to the wait queue.
The timeout lock is an exception that allows the locking method to be executed according to a preset timeout period, that is, locking can be tried multiple times within the timeout period, and locking failure is returned when the timeout period is exceeded. Waiting for the lock is not limited by the timeout period, and locking can be attempted multiple times until locking is successful.
The lock failure is used to characterize that the target access resource corresponding to the current thread in the distributed system has been locked by other threads. That is, when the target access resource corresponding to the current thread has been locked by other threads, the current thread cannot access the target access resource, and thus cannot be locked.
The wait queue is then an ordered queue for holding threads that need to access the locked target access resource. In this embodiment, when the locking state is a failed locking state and the lock type is a timeout lock or a wait lock, the current thread is added to the wait queue.
Step 210, detecting whether the target access resource is released according to the preset time interval, and locking the current thread if the target access resource is detected to be released and the waiting time of the current thread is smaller than the target waiting time corresponding to the lock type.
The preset time interval may be a preset interval duration between two detections. The target waiting time period may be a maximum waiting time period for waiting to lock based on the lock type, i.e. a time-out time period for locking. The wait time of the current thread may be a time period from when the current thread joins the wait queue to when the current processing time begins.
In this embodiment, when locking fails, whether the target access resource is released or not may be periodically detected according to a preset time interval, that is, whether the target access resource is released or not is detected by other threads, and if it is detected that the target access resource is released, whether the waiting duration of the current thread is smaller than the corresponding target waiting duration is further determined, that is, whether the current thread is expired is determined. If the waiting time of the current thread is smaller than the target waiting time corresponding to the lock type, determining that the current thread is not expired, and therefore locking the current thread can be carried out.
In the implementation method of the distributed lock, the distributed lock information is identified by responding to the starting instruction, when the distributed lock information is detected to be triggered, the locking key corresponding to the current thread is determined, the current thread is locked by adopting the locking key, the locking state is obtained, if the locking state is failed and the lock type is overtime lock or waiting lock, the current thread is added into the waiting queue, whether the target access resource is released is detected according to the preset time interval, and if the target access resource is detected to be released and the waiting duration of the current thread is smaller than the target waiting duration corresponding to the lock type, the current thread is locked. According to the embodiment, automatic locking is realized through annotating codes or a pre-configured locking strategy, so that the complexity of codes is reduced, the locking realization process is simplified, and the locking efficiency is improved.
In an exemplary embodiment, the method may further include: if the target access resource is detected to be released and the waiting time length of the current thread is greater than or equal to the target waiting time length corresponding to the lock type, returning a prompt message of lock failure.
Specifically, when it is detected that the target access resource has been released, it is further determined whether the waiting duration of the current thread is smaller than the corresponding target waiting duration, that is, whether the current thread is expired. If the waiting time of the current thread is greater than or equal to the target waiting time corresponding to the lock type, determining that the current thread has expired, so that prompt information of lock failure can be returned, and ending the current thread to release resources and improve the utilization rate of the resources.
In an exemplary embodiment, the method may further include: if the target access resource is not released and the waiting time of the current thread is smaller than the target waiting time corresponding to the lock type, returning to execute the step of detecting whether the target access resource is released or not according to the preset time interval.
Specifically, when it is detected that the target access resource is not released and the waiting time length of the current thread is smaller than the corresponding target waiting time length, that is, it is determined that the current thread is not expired, the step of detecting whether the target access resource is released according to the preset time interval is continuously performed, that is, whether the target access resource is released is continuously performed is performed, if it is detected that the target access resource is released, whether the waiting time length of the current thread is smaller than the corresponding target waiting time length is further determined, and if the waiting time length of the current thread is smaller than the target waiting time length corresponding to the lock type, it is determined that the current thread is not expired, so that locking can be performed on the current thread. I.e., the current thread is continually attempting to lock it while it is not expired. And when the current thread is expired, returning prompt information of lock failure, ending the current thread to release the resource and improving the utilization rate of the resource.
In an exemplary embodiment, in step 210, locking the current thread may specifically include: locking the current thread by adopting a locking keyword, and acquiring a locking state; if the locking state is successful, executing task operation corresponding to the current thread; if the locking state is the locking failure, returning to execute the step of detecting whether the target access resource is released or not according to the preset time interval.
Specifically, when locking a current thread, a locking key may be used to lock the current thread. That is, the target application program may send a locking instruction including a locking key to the corresponding server, so that the server performs locking processing according to the locking instruction, and returns a locking result including a locking state. If the server locks the current thread successfully according to the locking instruction, a result of successful locking is returned, and then task operation corresponding to the current thread can be executed.
And if the server fails to lock the current thread according to the locking instruction, returning a locking failure result. If the target application program determines that the locking state is failed, returning to execute the step of detecting whether the target access resource is released according to the preset time interval, namely returning to continuously detect whether the target access resource is released, if the target access resource is detected to be released, further judging whether the waiting time of the current thread is smaller than the corresponding target waiting time, and if the waiting time of the current thread is smaller than the target waiting time corresponding to the lock type, determining that the current thread is not expired, so that the current thread can be locked. I.e., the current thread is continually attempting to lock it while it is not expired.
In an exemplary embodiment, after the locking state is obtained in step 206, the method may further include: if the locking state is failed locking and the lock type is immediate locking, returning prompt information of failed locking. Namely, when the lock type is configured as an immediate lock and the locking of the current thread fails, the prompt information of the lock failure is returned, and the current thread is ended, so that an immediate lock scene is realized.
In an exemplary embodiment, after the locking state is obtained in step 206, the method may further include: if the locking state is successful, determining that the lock node state corresponding to the current thread is an operating state, and executing task operation corresponding to the current thread. That is, after the current thread is successfully locked, task operation corresponding to the current thread can be executed, and the state of the lock node corresponding to the current thread is determined to be an operating state, that is, the state that the current thread is locked is determined, so that automatic locking of the current thread is realized.
In an exemplary embodiment, after adding the current thread to the wait queue in step 208, the method may further include: and determining the lock node state corresponding to the current thread as a waiting state. I.e., after the current thread is added to the wait queue, the current thread may be determined to be in a state waiting to be locked.
In an exemplary embodiment, the method further comprises: if the locking state is successful, deleting the current thread from the waiting queue, and determining the lock node state corresponding to the current thread as the running state. That is, after the current thread is successfully locked, the current thread may be deleted from the waiting queue, and the lock node state corresponding to the current thread is determined to be the running state, that is, the current thread is determined to be in the locked state.
In an exemplary embodiment, the method further comprises: and if the task operation is completed, determining the lock node state corresponding to the current thread as a completion state. That is, after the locked current thread performs the corresponding task operation, it may be determined that the current thread is in a completed state.
In an exemplary embodiment, after performing the task operation corresponding to the current thread, the method may further include: and if the task operation is completed and the target access resource is determined to be locked by the current thread, releasing the lock of the current thread to release the target access resource.
In this embodiment, in determining whether the target access resource is locked by the current thread, the determination may be based on the locking timestamp of the current thread. Specifically, when the current thread is locked by using the locking key, a lock value, that is, a value, of the locking key can be generated based on the current timestamp, and when the current thread is successfully locked, the corresponding locking key comprises a locking key, the lock value and a thread identifier of the current thread locked by using the locking key are recorded in a lua script of redis. Therefore, after the current thread performs the corresponding task operation, the value can be obtained from the lua script of the redis, and whether the value is consistent with the value held by the current thread is detected, if so, the target access resource is locked by the current thread, so that the lock release can be performed on the current thread to release the target access resource. If not, the process ends.
In an exemplary embodiment, as shown in fig. 3, the implementation method of the distributed lock is further described below, which specifically includes the following steps:
step 302, when the target application program is started, the distributed lock information is automatically identified.
Specifically, the distributed lock information may be automatically identified based on the target annotation code or a pre-configured locking policy when the target application is started.
Step 304, when the distributed lock information is triggered, the key of the locking key is calculated.
When the distributed lock information is triggered, then the lock key of the locking key corresponding to the current thread may be calculated.
At step 306, a lock key is used to attempt to lock the current thread.
Specifically, the target application program may send a locking instruction to the corresponding server, so that the server performs locking processing according to the locking instruction, and returns a locking result including a locking state.
Step 308, it is determined whether the locking is successful.
Specifically, if the locking is successful, step 310 is performed, and if the locking is failed, step 318 is performed.
Step 310, executing the task operation corresponding to the current thread.
Specifically, when the locking is successful, the task operation corresponding to the current thread is executed, and the state of the lock node corresponding to the current thread is determined to be the running state, namely, the state that the current thread is locked is determined.
Step 312, when the task operation is completed, it is determined whether the lock is applied to the current thread.
When the task operation corresponding to the current thread is executed, further judging whether the target access resource is locked by the current thread, if so, executing step 314, and determining that the lock node state corresponding to the current thread is a completion state; if not, step 316 is performed.
Step 314, lock release.
Specifically, if the task operation is completed and it is determined that the target access resource is locked by the current thread, the lock is released for the current thread to release the target access resource.
Step 316, the flow ends.
Specifically, if the task operation is completed and it is determined that the target access resource is not locked by the current thread, the process is ended. It can be understood that, when the thread is successfully locked, in order to avoid that the thread monopolizes the resource, an effective life time is usually set for the lock, if the task operation corresponding to the current thread is executed completely, and it is determined that the target access resource is locked by the current thread, the lock added by the current thread is indicated to be still within the effective life time, and at this time, the task operation corresponding to the current thread is executed completely, so that the lock release can be performed on the current thread, and the utilization rate of the resource is improved. If the task operation corresponding to the current thread is completed and it is determined that the target access resource is not locked by the current thread, the lock added by the current thread is indicated to have exceeded the effective life time, and is automatically released when the effective life time is reached.
At step 318, it is determined whether the lock type is an immediate lock.
Specifically, when the locking fails, it is further determined whether the lock type is an immediate lock, if so, step 320 is executed, and if not, step 322 is executed.
Step 320, return the prompt message of lock failure.
Specifically, when the locking fails and the lock type is immediate lock, a prompt message of the lock failure is returned. That is, when the lock type is configured as an immediate lock and the locking of the current thread fails, a prompt message of the lock failure is returned.
Step 322, block the current thread.
Specifically, when the locking fails and the lock type is not an immediate lock, if the lock type is a timeout lock or a wait lock, the current thread is blocked, i.e. the current thread is added into the wait queue, and the lock node state corresponding to the current thread is determined to be a wait state.
At step 324, it is detected whether the target access resource is released.
When the locking fails, it may be periodically detected whether the target access resource is released according to a preset time interval, that is, whether the target access resource is released by another thread, if it is detected that the target access resource is released, that is, the target access resource does not have a lock, step 326 is executed, and if it is detected that the target access resource is not released, that is, the target access resource has a lock, step 324 is continuously executed when the preset time interval is reached.
Step 326, it is determined whether the current thread is expired.
Specifically, whether the current thread is out of date is determined by determining whether the waiting duration of the current thread is less than the corresponding target waiting duration. If the wait time of the current thread is less than the target wait time corresponding to the lock type, then it is determined that the current thread is not expired, and thus step 328 may be performed. If the waiting duration of the current thread is greater than the target waiting duration corresponding to the lock type, determining that the current thread has expired, returning to execute step 320, that is, returning to the prompt message of lock failure.
Step 328, an attempt is made to lock the current thread.
Specifically, a locking instruction is sent to a corresponding server, so that the server performs locking processing according to the locking instruction, and a locking result containing a locking state is returned.
Step 330, it is determined whether the locking is successful.
Specifically, if the locking is successful, step 332 is executed, and if the locking is failed, step 324 is executed again.
Step 332 wakes up the blocked current thread.
Specifically, if the locking is successful, the blocked current thread is awakened, and step 310 is performed, that is, the task operation corresponding to the current thread is performed, while the current thread is deleted from the waiting queue.
According to the method for realizing the distributed lock, automatic locking is realized through annotation codes or a pre-configured locking strategy, so that the code complexity is reduced, the locking realization process is simplified, the locking efficiency is improved, the lock types are thinned, immediate locking, overtime locking and waiting locking are realized, the problems of deadlock and invalid locking are avoided, the resource competition problem is solved for distributed application, and the concurrency performance of the system is improved.
It should be understood that, although the steps in the flowcharts related to the embodiments described above are sequentially shown as indicated by arrows, these steps are not necessarily sequentially performed in the order indicated by the arrows. The steps are not strictly limited to the order of execution unless explicitly recited herein, and the steps may be executed in other orders. Moreover, at least some of the steps in the flowcharts described in the above embodiments may include a plurality of steps or a plurality of stages, which are not necessarily performed at the same time, but may be performed at different times, and the order of the steps or stages is not necessarily performed sequentially, but may be performed alternately or alternately with at least some of the other steps or stages.
Based on the same inventive concept, the embodiment of the application also provides a distributed lock implementation device for implementing the above related distributed lock implementation method. The implementation of the solution provided by the device is similar to the implementation described in the above method, so the specific limitation in the embodiments of the implementation device of one or more distributed locks provided below may refer to the limitation of the implementation method of the distributed lock hereinabove, and will not be repeated herein.
In an exemplary embodiment, as shown in fig. 4, there is provided an implementation apparatus of a distributed lock, including: a lock information identification module 402, a first detection module 404, a lock state acquisition module 406, a thread processing module 408, and a second detection module 410, wherein:
a lock information identification module 402, configured to identify distributed lock information in response to a start instruction, where the distributed lock information includes a lock type;
a first detection module 404, configured to determine a locking keyword corresponding to a current thread when the distributed lock information is detected to be triggered;
a locking state obtaining module 406, configured to lock the current thread using the locking keyword, and obtain a locking state;
A thread processing module 408, configured to add the current thread to a waiting queue if the locking status is a locking failure and the lock type is a timeout lock or a waiting lock, where the locking failure is used to characterize that a target access resource corresponding to the current thread in the distributed system is already locked by other threads;
and a second detection module 410, configured to detect whether the target access resource is released according to a preset time interval, and lock the current thread if it is detected that the target access resource is released and the waiting duration of the current thread is less than the target waiting duration corresponding to the lock type.
In an exemplary embodiment, the apparatus further includes a prompt module, configured to return a prompt for a lock failure if the locking state is a lock failure and the lock type is an immediate lock.
In an exemplary embodiment, the apparatus further includes a task execution module, configured to determine, if the locking state is successful, that a lock node state corresponding to the current thread is an operation state, and execute a task operation corresponding to the current thread.
In an exemplary embodiment, the second detection module is further configured to lock the current thread using the locking key and obtain a locking state; if the locking state is successful, executing task operation corresponding to the current thread; and if the locking state is the locking failure, returning to execute the step of detecting whether the target access resource is released or not according to the preset time interval.
In an exemplary embodiment, the apparatus further includes a state determining module configured to determine, after adding the current thread to a wait queue, that a lock node state corresponding to the current thread is a wait state; if the locking state is successful, deleting the current thread from the waiting queue, and determining that the lock node state corresponding to the current thread is an operation state; and if the task operation is completed, determining the lock node state corresponding to the current thread as a completion state.
In an exemplary embodiment, the apparatus further includes a lock release module configured to, if the task operation is performed and it is determined that the target access resource is locked by the current thread, release the lock of the current thread to release the target access resource.
In an exemplary embodiment, the second detection module is further configured to: if the target access resource is detected to be released and the waiting time of the current thread is longer than or equal to the target waiting time corresponding to the lock type, returning a prompt message of lock failure; and if the target access resource is detected not to be released and the waiting time of the current thread is smaller than the target waiting time corresponding to the lock type, returning to the step of detecting whether the target access resource is released according to the preset time interval.
The modules in the implementation device of the distributed lock may be implemented in whole or in part by software, hardware, and a combination thereof. The above modules may be embedded in hardware or may be independent of a processor in the computer device, or may be stored in software in a memory in the computer device, so that the processor may call and execute operations corresponding to the above modules.
In one exemplary embodiment, a computer device is provided, which may be a terminal, and an internal structure diagram thereof may be as shown in fig. 5. The computer device includes a processor, a memory, an input/output interface, a communication interface, a display unit, and an input means. The processor, the memory and the input/output interface are connected through a system bus, and the communication interface, the display unit and the input device are connected to the system bus through the input/output interface. Wherein the processor of the computer device is configured to provide computing and control capabilities. The memory of the computer device includes a non-volatile storage medium and an internal memory. The non-volatile storage medium stores an operating system and a computer program. The internal memory provides an environment for the operation of the operating system and computer programs in the non-volatile storage media. The input/output interface of the computer device is used to exchange information between the processor and the external device. The communication interface of the computer device is used for carrying out wired or wireless communication with an external terminal, and the wireless mode can be realized through WIFI, a mobile cellular network, NFC (near field communication) or other technologies. The computer program, when executed by a processor, implements a method of implementing a distributed lock. The display unit of the computer device is used for forming a visual picture, and can be a display screen, a projection device or a virtual reality imaging device. The display screen can be a liquid crystal display screen or an electronic ink display screen, and the input device of the computer equipment can be a touch layer covered on the display screen, can also be a key, a track ball or a touch pad arranged on the shell of the computer equipment, and can also be an external keyboard, a touch pad or a mouse and the like.
It will be appreciated by those skilled in the art that the structure shown in fig. 5 is merely a block diagram of some of the structures associated with the present application and is not limiting of the computer device to which the present application may be applied, and that a particular computer device may include more or fewer components than shown, or may combine certain components, or have a different arrangement of components.
In an exemplary embodiment, a computer device is provided comprising a memory and a processor, the memory having stored therein a computer program, the processor implementing the steps of the method as above when executing the computer program.
In one embodiment, a computer readable storage medium is provided, having stored thereon a computer program which, when executed by a processor, implements the steps of the method as above.
In one embodiment, a computer program product is provided comprising a computer program which, when executed by a processor, implements the steps of the method as above.
It should be noted that, the user information (including, but not limited to, user equipment information, user personal information, etc.) and the data (including, but not limited to, data for analysis, stored data, presented data, etc.) referred to in the present application are information and data authorized by the user or sufficiently authorized by each party, and the collection, use, and processing of the related data are required to meet the related regulations.
Those skilled in the art will appreciate that implementing all or part of the above described methods may be accomplished by way of a computer program stored on a non-transitory computer readable storage medium, which when executed, may comprise the steps of the embodiments of the methods described above. Any reference to memory, database, or other medium used in the various embodiments provided herein may include at least one of non-volatile and volatile memory. The nonvolatile Memory may include Read-Only Memory (ROM), magnetic tape, floppy disk, flash Memory, optical Memory, high density embedded nonvolatile Memory, resistive random access Memory (ReRAM), magnetic random access Memory (Magnetoresistive Random Access Memory, MRAM), ferroelectric Memory (Ferroelectric Random Access Memory, FRAM), phase change Memory (Phase Change Memory, PCM), graphene Memory, and the like. Volatile memory can include random access memory (Random Access Memory, RAM) or external cache memory, and the like. By way of illustration, and not limitation, RAM can be in the form of a variety of forms, such as static random access memory (Static Random Access Memory, SRAM) or dynamic random access memory (Dynamic Random Access Memory, DRAM), and the like. The databases referred to in the various embodiments provided herein may include at least one of relational databases and non-relational databases. The non-relational database may include, but is not limited to, a blockchain-based distributed database, and the like. The processors referred to in the embodiments provided herein may be general purpose processors, central processing units, graphics processors, digital signal processors, programmable logic units, quantum computing-based data processing logic units, etc., without being limited thereto.
The technical features of the above embodiments may be arbitrarily combined, and all possible combinations of the technical features in the above embodiments are not described for brevity of description, however, as long as there is no contradiction between the combinations of the technical features, they should be considered as the scope of the description.
The above examples only represent a few embodiments of the present application, which are described in more detail and are not to be construed as limiting the scope of the present application. It should be noted that it would be apparent to those skilled in the art that various modifications and improvements could be made without departing from the spirit of the present application, which would be within the scope of the present application. Accordingly, the scope of protection of the present application shall be subject to the appended claims.

Claims (10)

1. A method for implementing a distributed lock, the method comprising:
in response to the initiation instruction, identifying distributed lock information, the distributed lock information including a lock type;
when the distributed lock information is detected to be triggered, determining a locking keyword corresponding to the current thread;
locking the current thread by adopting the locking key word, and acquiring a locking state;
If the locking state is a locking failure and the lock type is overtime lock or waiting lock, adding the current thread into a waiting queue, wherein the locking failure is used for representing that a target access resource corresponding to the current thread in a distributed system is locked by other threads;
detecting whether the target access resource is released or not according to a preset time interval, and locking the current thread if the target access resource is released and the waiting time of the current thread is smaller than the target waiting time corresponding to the lock type.
2. The method according to claim 1, wherein the method further comprises:
and if the locking state is failed locking and the lock type is immediate locking, returning a prompt message of failed locking.
3. The method according to claim 1, wherein the method further comprises:
and if the locking state is successful, determining that the lock node state corresponding to the current thread is an operating state, and executing task operation corresponding to the current thread.
4. The method of claim 1, wherein said locking the current thread comprises:
Locking the current thread by adopting the locking key word, and acquiring a locking state;
if the locking state is successful, executing task operation corresponding to the current thread;
and if the locking state is the locking failure, returning to execute the step of detecting whether the target access resource is released or not according to the preset time interval.
5. The method according to claim 4, wherein the method further comprises:
after the current thread is added into a waiting queue, determining that the lock node state corresponding to the current thread is a waiting state;
if the locking state is successful, deleting the current thread from the waiting queue, and determining that the lock node state corresponding to the current thread is an operation state;
and if the task operation is completed, determining the lock node state corresponding to the current thread as a completion state.
6. The method of claim 3 or 5, wherein after the performing the task operation corresponding to the current thread, the method further comprises:
and if the task operation is completed and the target access resource is determined to be locked by the current thread, releasing the lock of the current thread to release the target access resource.
7. The method according to any one of claims 1 to 5, further comprising:
if the target access resource is detected to be released and the waiting time of the current thread is longer than or equal to the target waiting time corresponding to the lock type, returning a prompt message of lock failure;
and if the target access resource is detected not to be released and the waiting time of the current thread is smaller than the target waiting time corresponding to the lock type, returning to the step of detecting whether the target access resource is released according to the preset time interval.
8. An apparatus for implementing a distributed lock, the apparatus comprising:
the lock information identification module is used for responding to the starting instruction and identifying distributed lock information, wherein the distributed lock information comprises a lock type;
the first detection module is used for determining a locking keyword corresponding to the current thread when the distributed lock information is detected to be triggered;
the locking state acquisition module is used for locking the current thread by adopting the locking key words and acquiring a locking state;
the thread processing module is used for adding the current thread into a waiting queue if the locking state is a locking failure and the locking type is a timeout lock or a waiting lock, wherein the locking failure is used for representing that a target access resource corresponding to the current thread in the distributed system is locked by other threads;
And the second detection module is used for detecting whether the target access resource is released or not according to a preset time interval, and locking the current thread if the target access resource is detected to be released and the waiting time of the current thread is smaller than the target waiting time corresponding to the lock type.
9. A computer device comprising a memory and a processor, the memory storing a computer program, characterized in that the processor implements the steps of the method of any of claims 1 to 7 when the computer program is executed.
10. A computer readable storage medium, on which a computer program is stored, characterized in that the computer program, when being executed by a processor, implements the steps of the method of any of claims 1 to 7.
CN202311733530.2A 2023-12-15 2023-12-15 Method, device, computer equipment and storage medium for realizing distributed lock Pending CN117667421A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202311733530.2A CN117667421A (en) 2023-12-15 2023-12-15 Method, device, computer equipment and storage medium for realizing distributed lock

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202311733530.2A CN117667421A (en) 2023-12-15 2023-12-15 Method, device, computer equipment and storage medium for realizing distributed lock

Publications (1)

Publication Number Publication Date
CN117667421A true CN117667421A (en) 2024-03-08

Family

ID=90082518

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202311733530.2A Pending CN117667421A (en) 2023-12-15 2023-12-15 Method, device, computer equipment and storage medium for realizing distributed lock

Country Status (1)

Country Link
CN (1) CN117667421A (en)

Similar Documents

Publication Publication Date Title
US10521393B2 (en) Remote direct memory access (RDMA) high performance producer-consumer message processing
CN102541661B (en) Realize the method and apparatus of wait on address synchronization interface
CN112988284B (en) Application program starting method and device, computer equipment and storage medium
US8973117B2 (en) Propagating security identity information to components of a composite application
CN105847446B (en) Method, device and system for acquiring network data
CN110413432A (en) A kind of information processing method, electronic equipment and storage medium
CN116541142A (en) Task scheduling method, device, equipment, storage medium and computer program product
CN116561137A (en) Transaction processing method, device, computer equipment and storage medium
CN110908644B (en) Configuration method and device of state node, computer equipment and storage medium
CN116820790A (en) Delay processing method, device, equipment and medium for distributed lock
CN117667421A (en) Method, device, computer equipment and storage medium for realizing distributed lock
CN116048490A (en) Business event processing method, device, computer equipment and storage medium
CN111159782B (en) Safety task processing method and electronic equipment
CN102426535A (en) Dynamic process virtualization
CN108121580A (en) The implementation method and device of application notification service
CN112799791A (en) Method and device for calling distributed lock, electronic equipment and storage medium
CN117311930A (en) Task scheduling method, device, equipment, storage medium and program product
US20230161664A1 (en) Method of responding to operation, electronic device, and storage medium
WO2020133455A1 (en) Application program management method, device, storage medium and electronic apparatus
CN118069700A (en) Data verification method, device, computer equipment and storage medium
CN116204291A (en) Task processing method, device, computer equipment and storage medium
CN118158094A (en) Configuration information processing method, device, network equipment and storage medium
CN116541137A (en) Transaction processing method, apparatus, computer device, storage medium, and program product
CN116755934A (en) Snapshot starting method, device, equipment, storage medium and program product
CN116909684A (en) Deployment method and device of low-code application, computer equipment and storage medium

Legal Events

Date Code Title Description
PB01 Publication
PB01 Publication
SE01 Entry into force of request for substantive examination
SE01 Entry into force of request for substantive examination