CN108897628B - Method and device for realizing distributed lock and electronic equipment - Google Patents

Method and device for realizing distributed lock and electronic equipment Download PDF

Info

Publication number
CN108897628B
CN108897628B CN201810560749.XA CN201810560749A CN108897628B CN 108897628 B CN108897628 B CN 108897628B CN 201810560749 A CN201810560749 A CN 201810560749A CN 108897628 B CN108897628 B CN 108897628B
Authority
CN
China
Prior art keywords
thread
target
node
sequence
distributed lock
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.)
Active
Application number
CN201810560749.XA
Other languages
Chinese (zh)
Other versions
CN108897628A (en
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.)
Beijing QIYI Century Science and Technology Co Ltd
Original Assignee
Beijing QIYI Century Science and Technology 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 Beijing QIYI Century Science and Technology Co Ltd filed Critical Beijing QIYI Century Science and Technology Co Ltd
Priority to CN201810560749.XA priority Critical patent/CN108897628B/en
Publication of CN108897628A publication Critical patent/CN108897628A/en
Application granted granted Critical
Publication of CN108897628B publication Critical patent/CN108897628B/en
Active legal-status Critical Current
Anticipated expiration legal-status Critical

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/52Program synchronisation; Mutual exclusion, e.g. by means of semaphores
    • G06F9/524Deadlock detection or avoidance
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F9/00Arrangements for program control, e.g. control units
    • G06F9/06Arrangements for program control, e.g. control units using stored programs, i.e. using an internal store of processing equipment to receive or retain programs
    • G06F9/46Multiprogramming arrangements
    • G06F9/50Allocation of resources, e.g. of the central processing unit [CPU]
    • G06F9/5005Allocation of resources, e.g. of the central processing unit [CPU] to service a request
    • G06F9/5011Allocation of resources, e.g. of the central processing unit [CPU] to service a request the resources being hardware resources other than CPUs, Servers and Terminals
    • G06F9/5022Mechanisms to release resources
    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F2209/00Indexing scheme relating to G06F9/00
    • G06F2209/50Indexing scheme relating to G06F9/50
    • G06F2209/5018Thread allocation

Abstract

The embodiment of the invention provides a method and a device for realizing a distributed lock and electronic equipment. The method comprises the following steps: when a thread requests a distributed lock with resource information of a target resource identifier, creating a sequence temporary node corresponding to the thread under a target persistent node in a Zookeeper, generating a target token corresponding to the thread, and taking the target token as a reference token corresponding to the target resource identifier; judging whether the thread acquires the distributed lock or not based on the sequence number of the sequence temporary node corresponding to the thread; when the thread is judged to acquire the distributed lock, accessing resource information; and after the resource information is accessed, deleting the sequence temporary node corresponding to the thread, judging whether the target token corresponding to the thread is consistent with the reference token corresponding to the target resource identifier, and if so, deleting the target persistent node. The scheme can solve the problem of occupying a large storage space caused by creating too many persistent nodes.

Description

Method and device for realizing distributed lock and electronic equipment
Technical Field
The present invention relates to the field of computer technologies, and in particular, to a method and an apparatus for implementing a distributed lock, and an electronic device.
Background
The distributed lock is mainly used for protecting shared resources which are cross-process, cross-host and cross-network in a distributed environment to realize mutual exclusion access so as to ensure the consistency of data. At present, three popular schemes mainly exist for realizing the distributed lock, and the three popular schemes are respectively based on a database, a cache and a Zookeeper. Zookeeper (zk for short) is a centralized service system for providing configuration management, distributed collaboration and naming, the functions provided by Zookeeper are very basic and indispensable functions in a distributed system, and developers can build various distributed systems on the Zookeeper.
However, in the process of implementing the present invention, the inventor finds that in the prior art, in the process of implementing a distributed lock based on a Zookeeper, if a thread requests the distributed lock, a persistent node generally needs to be created in the Zookeeper. However, since the creation of the persistent node is based on the resource identifier, a persistent node is created for each resource identifier, and when more threads request a distributed lock on the resource information of the corresponding resource identifier, and the resource identifiers corresponding to the threads are different, the more persistent nodes need to be created. But after the distributed lock is released, the Zookeeper performance may be affected because the previously created persistent node still exists in the Zookeeper, which occupies a large storage space.
Disclosure of Invention
The embodiment of the invention aims to provide a method, a device and electronic equipment for realizing a distributed lock, so as to solve the problem of large storage space occupation caused by creating too many persistent nodes. The specific technical scheme is as follows:
in a first aspect, an embodiment of the present invention provides a method for implementing a distributed lock, where the method includes:
when a thread requests a distributed lock with resource information of a target resource identifier, creating a sequence temporary node corresponding to the thread under a target persistent node in a Zookeeper, generating a target token corresponding to the thread, and taking the target token as a reference token corresponding to the target resource identifier; the target persistent node is a persistent node named by the target resource identifier;
judging whether the thread acquires the distributed lock or not based on the sequence number of the sequence temporary node corresponding to the thread;
when the thread is judged to acquire the distributed lock, accessing the resource information;
and after the resource information is accessed, deleting the sequence temporary node corresponding to the thread, judging whether the target token corresponding to the thread is consistent with the reference token corresponding to the target resource identifier, and if so, deleting the target persistent node.
Optionally, the step of creating a sequential temporary node corresponding to the thread under the target persistent node in the Zookeeper includes:
judging whether a target persistent node named by a target resource identifier exists in the Zookeeper;
if so, creating a sequence temporary node corresponding to the thread under the target persistent node;
and if the order temporary node does not exist, creating a target persistent node named by a target resource identifier in the Zookeeper, and creating the order temporary node corresponding to the thread under the target persistent node.
Optionally, the step of determining whether the thread acquires the distributed lock based on the sequence number of the sequential temporary node corresponding to the thread includes:
regularly judging whether the sequence number of the sequence temporary node corresponding to the thread is the minimum value in the sequence number group or not; the sequence number group consists of sequence numbers of all sequence temporary nodes under the target persistent node;
and if so, determining that the thread acquires the distributed lock.
Optionally, the step of generating the target token corresponding to the thread includes:
and generating a target token corresponding to the thread by using the sequence number of the sequence temporary node corresponding to the thread.
Optionally, the method further includes:
and after the target persistent node is deleted, deleting the reference token corresponding to the target resource identifier.
In a second aspect, an embodiment of the present invention further provides an apparatus for implementing a distributed lock, where the apparatus includes:
the creating module is used for creating a sequence temporary node corresponding to a thread under a target persistent node in a Zookeeper when the thread requests a distributed lock with resource information of a target resource identifier, generating a target token corresponding to the thread, and taking the target token as a reference token corresponding to the target resource identifier; the target persistent node is a persistent node named by the target resource identifier;
the judging module is used for judging whether the thread acquires the distributed lock or not based on the sequence number of the sequence temporary node corresponding to the thread;
the access module is used for accessing the resource information when the thread is judged to acquire the distributed lock;
and the deleting module is used for deleting the sequence temporary node corresponding to the thread after the resource information is accessed, judging whether the target token corresponding to the thread is consistent with the reference token corresponding to the target resource identifier or not, and deleting the target persistent node if the target token corresponding to the thread is consistent with the reference token corresponding to the target resource identifier.
Optionally, the creating module is specifically configured to:
when a thread requests a distributed lock with resource information of a target resource identifier, judging whether a target persistent node named by the target resource identifier exists in a Zookeeper;
if so, creating a sequence temporary node corresponding to the thread under the target persistent node;
and if the order temporary node does not exist, creating a target persistent node named by a target resource identifier in the Zookeeper, and creating the order temporary node corresponding to the thread under the target persistent node.
Optionally, the determining module is specifically configured to:
regularly judging whether the sequence number of the sequence temporary node corresponding to the thread is the minimum value in the sequence number group or not; the sequence number group consists of sequence numbers of all sequence temporary nodes under the target persistent node;
and if so, determining that the thread acquires the distributed lock.
Optionally, the creating module is specifically configured to:
when a thread requests a distributed lock with resource information of a target resource identifier, creating a sequence temporary node corresponding to the thread under a target persistent node in a Zookeeper, and generating a target token corresponding to the thread by using a sequence number of the sequence temporary node corresponding to the thread.
Optionally, the apparatus further comprises: a token deletion module;
and the token deleting module is used for deleting the reference token corresponding to the target resource identifier after the deleting module deletes the target persistent node.
In a third aspect, an embodiment of the present invention further provides an electronic device, which is characterized by including a processor, a communication interface, a memory, and a communication bus, where the processor and the communication interface complete communication between the memory and the processor through the communication bus;
a memory for storing a computer program;
and the processor is configured to implement the method for implementing the distributed lock according to the first aspect when executing the program stored in the memory.
In a fourth aspect, an embodiment of the present invention further provides a computer-readable storage medium, where instructions are stored in the computer-readable storage medium, and when the instructions are executed on a computer, the computer is caused to execute the method for implementing a distributed lock according to the first aspect.
In a fifth aspect, an embodiment of the present invention further provides a computer program product containing instructions, which when run on a computer, causes the computer to execute the method for implementing a distributed lock according to the first aspect.
In the scheme provided by the embodiment of the invention, when the thread requests the distributed lock with the resource information of the target resource identifier, the target token can be generated, and the target token is taken as the reference token corresponding to the target resource identifier, so that the reference token corresponding to the target resource identifier is always consistent with the target token of the thread which requests the distributed lock at the latest. And after the resource information is accessed, if the thread judges that the target token corresponding to the thread is consistent with the reference token corresponding to the target resource identifier, the thread deletes the target persistent node, namely, when the thread which requests the distributed lock at the latest under the target persistent node is judged, the persistent node is deleted. After the distributed lock competition is finished, the persistent node can be deleted, so that the problem that a large storage space is occupied due to the fact that too many persistent nodes are created can be solved through the scheme.
Of course, it is not necessary for any product or method of practicing the invention to achieve all of the above-described advantages at the same time.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below.
Fig. 1 is a flowchart of a method for implementing a distributed lock according to an embodiment of the present invention;
FIG. 2 is a schematic diagram of a system architecture for implementing a distributed lock using a Zookeeper in an embodiment of the present invention;
fig. 3 is a schematic structural diagram of an implementation apparatus of a distributed lock according to an embodiment of the present invention;
fig. 4 is a schematic structural diagram of an electronic device according to an embodiment of the present invention.
Detailed Description
The technical solutions in the embodiments of the present invention will be described below with reference to the drawings in the embodiments of the present invention.
In order to solve the problem of occupying a large storage space caused by creating too many persistent nodes, embodiments of the present invention provide a method and an apparatus for implementing a distributed lock, and an electronic device.
First, a method for implementing a distributed lock provided in an embodiment of the present invention is described below.
It should be noted that an execution subject of the method for implementing a distributed lock provided by the embodiment of the present invention may be a thread running in an electronic device. Moreover, any thread that needs to request the distributed lock can execute the method provided by the embodiment of the invention. In addition, in a specific application, the electronic device may be a terminal or a server, but is not limited thereto.
In addition, at least two threads requesting the same distributed lock may belong to different electronic devices, respectively, that is, at least two threads that need to access the same resource information may belong to different electronic devices, respectively. Of course, at least two threads of the same electronic device may request the same distributed lock, i.e., at least two threads of the same electronic device may request access to the same resource information.
Fig. 1 is a flowchart of a method for implementing a distributed lock according to an embodiment of the present invention. As shown in fig. 1, a method for implementing a distributed lock according to an embodiment of the present invention may include the following steps:
s101, when a thread requests a distributed lock with resource information of a target resource identifier, creating a sequence temporary node corresponding to the thread under a target persistent node in a Zookeeper, generating a target token corresponding to the thread, and taking the target token as a reference token corresponding to the target resource identifier; and the target persistent node is a persistent node named by the target resource identifier.
In order to solve the problem of the prior art, in the embodiment of the present invention, an initial reference token corresponding to the resource identifier may be set in the cache region in advance, that is, the initial reference token is set for the resource information. When a thread requests a distributed lock with resource information of a target resource identifier, a sequence temporary node corresponding to the thread requesting the distributed lock is created under a target persistent node named by the target resource identifier in a Zookeeper, a target token corresponding to the thread is generated at the same time, and the generated target token is used as a reference token corresponding to the target resource identifier. In this way, the reference token corresponding to the target resource identifier is always consistent with the target token of the thread which requests the distributed lock at the latest, so that a realization basis is provided for judging whether to delete the persistent node. It is emphasized that, since the temporary nodes under the persistent nodes have sequence numbers, and the sequence number of the temporary node created first is smaller than that of the temporary node created later, the temporary nodes under the persistent nodes are generally referred to as sequential temporary nodes.
Specifically, the step of creating a sequential temporary node corresponding to the thread under a target persistent node in the Zookeeper may include:
judging whether a target persistent node named by a target resource identifier exists in the Zookeeper;
if so, creating a sequence temporary node corresponding to the thread under the target persistent node;
and if the order temporary node does not exist, creating a target persistent node named by a target resource identifier in the Zookeeper, and creating the order temporary node corresponding to the thread under the target persistent node.
It can be understood that, if the thread determines that there is no target persistent node named with the target resource identifier in the Zookeeper, it may indicate that the thread requests the distributed lock with the resource information of the target resource identifier at the earliest, at this time, the target persistent node named with the target resource identifier may be created in the Zookeeper, and the sequential temporary node corresponding to the thread may be created under the target persistent node. And if the thread judges that the target persistent node named by the target resource identifier exists in the Zookeeper, the time that the thread requests the distributed lock with the resource information of the target resource identifier can be shown to be later than that of at least one other thread, and at this time, the sequence temporary node corresponding to the thread can be directly created under the target persistent node.
In addition, there are various specific implementation manners for generating the target token corresponding to the thread. Optionally, in a specific implementation manner, the step of generating the target token corresponding to the thread may include:
and generating a target token corresponding to the thread by using the sequence number of the sequence temporary node corresponding to the thread.
In practical application, when the sequence temporary nodes corresponding to the threads are created under the target persistent node, each sequence temporary node corresponds to one sequence number, and the sequence number of the sequence temporary node created first is smaller than that of the sequence temporary node created later.
In the embodiment of the present invention, the sequence number of the sequential temporary node corresponding to each thread may be used to generate the target token corresponding to the thread. Specifically, the sequence number of the sequential temporary node corresponding to each thread may be directly used as the target token corresponding to the thread; or the sequence number of the sequential temporary node corresponding to each thread may be encrypted, and then the encrypted information is used as the target token corresponding to the thread, and a specific encryption algorithm is not limited in the embodiment of the present invention.
It should be noted that, the specific implementation manner of creating the target persistent node named by the target resource identifier in the Zookeeper and the specific implementation manner of creating the sequential temporary node corresponding to the thread under the target persistent node may be implemented by using the prior art. The embodiment of the present invention is described by taking the above generation method as an example, and the method of generating the target token corresponding to the thread in practical application is not limited to this.
And S102, judging whether the thread acquires the distributed lock or not based on the sequence number of the sequence temporary node corresponding to the thread.
When a thread requests a distributed lock and a sequence temporary node corresponding to the thread is created under a target persistent node in the Zookeeper, whether the thread acquires the distributed lock can be judged based on the sequence number of the sequence temporary node corresponding to the thread.
Specifically, the step of determining whether the thread acquires the distributed lock based on the sequence number of the sequential temporary node corresponding to the thread may include:
regularly judging whether the sequence number of the sequence temporary node corresponding to the thread is the minimum value in the sequence number group or not; the sequence number group consists of sequence numbers of all sequence temporary nodes under the target persistent node;
and if so, determining that the thread acquires the distributed lock.
When the sequence temporary nodes corresponding to the threads are created under the target persistent node, the created sequence temporary nodes correspond to a sequence number, and then all the sequence temporary nodes created under the target persistent node correspond to the sequence numbers to form a sequence number group.
One way for the thread to determine whether the thread acquires the distributed lock may be: and regularly judging whether the sequence number of the sequence temporary node corresponding to the thread is the minimum value in the sequence number group. And if the sequence number of the sequence temporary node corresponding to the thread is the minimum value in the sequence number group, the thread acquires the distributed lock. And if the sequence number of the sequence temporary node corresponding to the thread is not the minimum value in the sequence number group, the thread does not acquire the distributed lock, and continues to wait for acquiring the distributed lock. Of course, the present application is only described above as an example, and the manner for determining whether a thread acquires a distributed lock in practical applications is not limited thereto.
For example: the thread can determine a first sequence temporary node, wherein the first sequence temporary node is a node with a sequence number smaller than that of the sequence temporary node of the thread; registering an event listener for the first sequential temporary node; when the notification that the first sequence temporary node is deleted is obtained, whether the sequence number of the sequence temporary node corresponding to the thread is the minimum value in the sequence number group is judged, if yes, the thread is judged to obtain the distributed lock, if not, the thread can re-determine a new first sequence temporary node, and whether the new first sequence temporary node is deleted is monitored.
S103, when the thread is judged to acquire the distributed lock, accessing the resource information.
When the thread is judged to acquire the distributed lock, the thread can access the resource information. Specifically, the resource information may be read, modified, deleted, or a certain operation may be completed by using the resource information, and the specific form of accessing the resource information by the thread in the embodiment of the present invention is not limited.
S104, after the resource information is accessed, deleting the sequence temporary node corresponding to the thread, judging whether the target token corresponding to the thread is consistent with the reference token corresponding to the target resource identifier, and if so, deleting the target persistent node.
When the thread finishes creating the sequence temporary nodes, the thread establishes connection with the sequence temporary nodes corresponding to the thread in the Zookeeper, when the thread acquires the distributed lock and accesses the resource information, the thread is disconnected with the sequence temporary nodes corresponding to the thread in the Zookeeper, the sequence temporary nodes corresponding to the thread are deleted, and the thread releases the lock. Meanwhile, whether the target token corresponding to the thread is consistent with the reference token corresponding to the target resource identifier is judged, and when the target token corresponding to the thread is consistent with the reference token corresponding to the target resource identifier, the target persistent node named by the target resource identifier is deleted; and when the target token corresponding to the thread is not consistent with the reference token corresponding to the target resource identifier, not processing.
In the embodiment of the invention, the target token corresponding to the thread is generated when the sequence temporary node corresponding to the thread is created. The reference token corresponding to the target resource identifier is updated when the sequential temporary node corresponding to the thread is created, that is, the reference token corresponding to the target resource identifier is the target token of the sequential temporary node corresponding to the last creation thread next to the target persistent node named by the target resource identifier.
Then, when the target token corresponding to the thread is consistent with the reference token corresponding to the target resource identifier, it indicates that all threads under the target persistent node named by the target resource identifier have sequentially acquired the distributed lock, and the thread currently releasing the distributed lock is the last thread holding the lock under the target persistent node, and then the deletion operation of the target persistent node is executed. And when the target token corresponding to the thread is inconsistent with the reference token corresponding to the target resource identifier, indicating that the thread currently releasing the distributed lock is not the last thread holding the lock under the target persistent node, and not processing the thread.
In the embodiment of the present invention, the implementation method may further include:
and after the target persistent node is deleted, deleting the reference token corresponding to the target resource identifier.
And when the thread finishes accessing the resource information, releasing the lock, and the thread currently releasing the distributed lock is the last thread holding the lock under the target persistent node, deleting the target persistent node. And after the target persistent node is deleted, deleting the reference token corresponding to the target resource identification together.
In the method for implementing a distributed lock provided in the embodiments of the present invention, when a thread requests a distributed lock having resource information of a target resource identifier, a target token may be generated, and the target token is used as a reference token corresponding to the target resource identifier, so that the reference token corresponding to the target resource identifier is always consistent with the target token of the thread that requested the distributed lock at the latest. And after the resource information is accessed, if the thread judges that the target token corresponding to the thread is consistent with the reference token corresponding to the target resource identifier, the thread deletes the target persistent node, namely, when the thread which requests the distributed lock at the latest under the target persistent node is judged, the persistent node is deleted. After the distributed lock competition is finished, the persistent node can be deleted, so that the problem that a large storage space is occupied due to the fact that too many persistent nodes are created can be solved through the scheme.
The following describes a method for implementing a distributed lock according to an embodiment of the present invention with reference to a specific application example.
It is assumed that the application environment of the method for implementing the distributed lock is that, when the method is applied to the credit level service of the open platform, a schematic diagram of a system architecture for implementing the distributed lock by using Zookeeper is shown in fig. 2.
The method comprises the steps that a thread in a credit level system requests a distributed lock with resource information of a target resource identifier from a Zookeeper cluster, a sequence temporary node corresponding to the thread is created under a target persistent node in the Zookeeper, a target token corresponding to the thread is generated, and the target token is used as a reference token corresponding to the target resource identifier.
And judging whether the thread acquires the distributed lock or not based on the sequence number of the sequence temporary node corresponding to the thread.
And when the thread is judged to acquire the distributed lock, the thread reads the MySql database to acquire data information, then settlement logic is executed, a calculation result is stored in the MySql database, and the calculation is finished.
And after the calculation is finished, disconnecting the thread from the sequence temporary node corresponding to the thread in the Zookeeper, releasing the lock, deleting the sequence temporary node corresponding to the thread, judging whether the target token corresponding to the thread is consistent with the reference token corresponding to the target resource identifier or not, and deleting the target persistent node if the target token corresponding to the thread is consistent with the reference token corresponding to the target resource identifier.
In the embodiment of the invention, when the thread requests the Zookeeper cluster for the distributed lock with the resource information of the target resource identifier, the target token can be generated, and the target token is taken as the reference token corresponding to the target resource identifier, so that the reference token corresponding to the target resource identifier is always consistent with the target token of the thread which requests the distributed lock at the latest. And after the data information is acquired and the calculation is completed, if the thread judges that the target token corresponding to the thread is consistent with the reference token corresponding to the target resource identifier, the thread deletes the target persistent node, namely when the thread which requests the distributed lock at the latest under the target persistent node is judged, the persistent node is deleted. After the distributed lock competition is finished, the persistent node can be deleted, so that the problem that a large storage space is occupied due to the fact that too many persistent nodes are created can be solved through the scheme.
Corresponding to the foregoing method embodiment, an embodiment of the present invention provides an apparatus for implementing a distributed lock, where as shown in fig. 3, the apparatus may include:
a creating module 301, configured to, when a thread requests a distributed lock with resource information of a target resource identifier, create a sequential temporary node corresponding to the thread under a target persistent node in a Zookeeper, and generate a target token corresponding to the thread, where the target token is used as a reference token corresponding to the target resource identifier; the target persistent node is a persistent node named by the target resource identifier;
a determining module 302, configured to determine whether the thread acquires the distributed lock based on a sequence number of a sequential temporary node corresponding to the thread;
an access module 303, configured to access the resource information when it is determined that the thread acquires the distributed lock;
a deleting module 304, configured to delete the sequential temporary node corresponding to the thread after the resource information is accessed, determine whether the target token corresponding to the thread is consistent with the reference token corresponding to the target resource identifier, and delete the target persistent node if the target token corresponding to the thread is consistent with the reference token corresponding to the target resource identifier.
In the apparatus for implementing a distributed lock according to the embodiments of the present invention, when a thread requests a distributed lock having resource information of a target resource identifier, a target token may be generated, and the target token is used as a reference token corresponding to the target resource identifier, so that the reference token corresponding to the target resource identifier is always consistent with the target token of the thread that requests the distributed lock at the latest. And after the resource information is accessed, if the thread judges that the target token corresponding to the thread is consistent with the reference token corresponding to the target resource identifier, the thread deletes the target persistent node, namely, when the thread which requests the distributed lock at the latest under the target persistent node is judged, the persistent node is deleted. After the distributed lock competition is finished, the persistent node can be deleted, so that the problem that a large storage space is occupied due to the fact that too many persistent nodes are created can be solved through the scheme.
It should be noted that the apparatus in the embodiment of the present invention is an apparatus corresponding to the implementation method of the distributed lock shown in fig. 1, and all the embodiments of the implementation method of the distributed lock shown in fig. 1 are applicable to the apparatus and all can achieve the same beneficial effects.
Wherein the creating module is specifically configured to:
when a thread requests a distributed lock with resource information of a target resource identifier, judging whether a target persistent node named by the target resource identifier exists in a Zookeeper;
if so, creating a sequence temporary node corresponding to the thread under the target persistent node;
and if the order temporary node does not exist, creating a target persistent node named by a target resource identifier in the Zookeeper, and creating the order temporary node corresponding to the thread under the target persistent node.
The judgment module is specifically configured to:
regularly judging whether the sequence number of the sequence temporary node corresponding to the thread is the minimum value in the sequence number group or not; the sequence number group consists of sequence numbers of all sequence temporary nodes under the target persistent node;
and if so, determining that the thread acquires the distributed lock.
The creating module is specifically configured to:
when a thread requests a distributed lock with resource information of a target resource identifier, creating a sequence temporary node corresponding to the thread under a target persistent node in a Zookeeper, and generating a target token corresponding to the thread by using a sequence number of the sequence temporary node corresponding to the thread.
The device further comprises: a token deletion module;
and the token deleting module is used for deleting the reference token corresponding to the target resource identifier after the deleting module deletes the target persistent node.
An embodiment of the present invention further provides an electronic device, as shown in fig. 4, including a processor 401, a communication interface 402, a memory 403, and a communication bus 404, where the processor 401, the communication interface 402, and the memory 403 complete mutual communication through the communication bus 404,
a memory 403 for storing a computer program;
the processor 401 is configured to implement the method provided by the embodiment of the present invention when executing the program stored in the memory 403.
According to the electronic device provided by the embodiment of the invention, when a thread requests a distributed lock with resource information of a target resource identifier, a target token can be generated, and the target token is taken as a reference token corresponding to the target resource identifier, so that the reference token corresponding to the target resource identifier is always consistent with the target token of the thread which requests the distributed lock at the latest. And after the resource information is accessed, if the thread judges that the target token corresponding to the thread is consistent with the reference token corresponding to the target resource identifier, the thread deletes the target persistent node, namely, when the thread which requests the distributed lock at the latest under the target persistent node is judged, the persistent node is deleted. After the distributed lock competition is finished, the persistent node can be deleted, so that the problem that a large storage space is occupied due to the fact that too many persistent nodes are created can be solved through the scheme.
The communication bus mentioned in the electronic device may be a Peripheral Component Interconnect (PCI) bus, an Extended Industry Standard Architecture (EISA) bus, or the like. The communication bus may be divided into an address bus, a data bus, a control bus, etc. For ease of illustration, only one thick line is shown, but this does not mean that there is only one bus or one type of bus.
The communication interface is used for communication between the electronic equipment and other equipment.
The Memory may include a Random Access Memory (RAM) or a non-volatile Memory (non-volatile Memory), such as at least one disk Memory. Optionally, the memory may also be at least one memory device located remotely from the processor.
The Processor may be a general-purpose Processor, and includes a Central Processing Unit (CPU), a Network Processor (NP), and the like; the device can also be a Digital Signal Processor (DSP), an Application Specific Integrated Circuit (ASIC), a Field Programmable Gate Array (FPGA) or other Programmable logic device, a discrete Gate or transistor logic device, or a discrete hardware component.
In another embodiment of the present invention, a computer-readable storage medium is further provided, where instructions are stored, and when the instructions are executed on a computer, the computer is caused to execute the method for implementing a distributed lock in any of the above embodiments, so as to achieve the same technical effect.
In another embodiment of the present invention, there is also provided a computer program product containing instructions, which when run on a computer, causes the computer to execute the method for implementing a distributed lock according to any one of the above embodiments, so as to achieve the same technical effect.
In the above embodiments, the implementation may be wholly or partially realized by software, hardware, firmware, or any combination thereof. When implemented in software, may be implemented in whole or in part in the form of a computer program product. The computer program product includes one or more computer instructions. When loaded and executed on a computer, cause the processes or functions described in accordance with the embodiments of the invention to occur, in whole or in part. The computer may be a general purpose computer, a special purpose computer, a network of computers, or other programmable device. The computer instructions may be stored in a computer readable storage medium or transmitted from one computer readable storage medium to another, for example, from one website site, computer, server, or data center to another website site, computer, server, or data center via wired (e.g., coaxial cable, fiber optic, Digital Subscriber Line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.). The computer-readable storage medium can be any available medium that can be accessed by a computer or a data storage device, such as a server, a data center, etc., that incorporates one or more of the available media. The usable medium may be a magnetic medium (e.g., floppy Disk, hard Disk, magnetic tape), an optical medium (e.g., DVD), or a semiconductor medium (e.g., Solid State Disk (SSD)), among others.
It is noted that, herein, relational terms such as first and second, and the like may be used solely to distinguish one entity or action from another entity or action without necessarily requiring or implying any actual such relationship or order between such entities or actions. Also, the terms "comprises," "comprising," or any other variation thereof, are intended to cover a non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements does not include only those elements but may include other elements not expressly listed or inherent to such process, method, article, or apparatus. The term "comprising", without further limitation, means that the element so defined is not excluded from the group consisting of additional identical elements in the process, method, article, or apparatus that comprises the element.
All the embodiments in the present specification are described in a related manner, and the same and similar parts among the embodiments may be referred to each other, and each embodiment focuses on the differences from the other embodiments. In particular, as for the apparatus embodiment, since it is substantially similar to the method embodiment, the description is relatively simple, and for the relevant points, reference may be made to the partial description of the method embodiment.
The above description is only for the preferred embodiment of the present invention, and is not intended to limit the scope of the present invention. Any modification, equivalent replacement, or improvement made within the spirit and principle of the present invention shall fall within the protection scope of the present invention.

Claims (9)

1. A method for implementing a distributed lock, comprising:
when a thread requests a distributed lock with resource information of a target resource identifier, creating a sequence temporary node corresponding to the thread under a target persistent node in a Zookeeper, generating a target token corresponding to the thread, and taking the target token as a reference token corresponding to the target resource identifier; the target persistent node is a persistent node named by the target resource identifier;
judging whether the thread acquires the distributed lock or not based on the sequence number of the sequence temporary node corresponding to the thread;
when the thread is judged to acquire the distributed lock, accessing the resource information;
after the resource information is accessed, deleting the sequence temporary node corresponding to the thread, judging whether a target token corresponding to the thread is consistent with a reference token corresponding to the target resource identifier or not, and if so, deleting the target persistent node;
wherein the step of generating the target token corresponding to the thread includes:
and generating a target token corresponding to the thread by using the sequence number of the sequence temporary node corresponding to the thread.
2. The method according to claim 1, wherein the step of creating a sequential temporary node corresponding to the thread under a target persistent node in a Zookeeper includes:
judging whether a target persistent node named by a target resource identifier exists in the Zookeeper;
if so, creating a sequence temporary node corresponding to the thread under the target persistent node;
and if the order temporary node does not exist, creating a target persistent node named by a target resource identifier in the Zookeeper, and creating the order temporary node corresponding to the thread under the target persistent node.
3. The method according to claim 1, wherein the step of determining whether the thread acquires the distributed lock based on the sequence number of the sequential temporary node corresponding to the thread comprises:
regularly judging whether the sequence number of the sequence temporary node corresponding to the thread is the minimum value in the sequence number group or not; the sequence number group consists of sequence numbers of all sequence temporary nodes under the target persistent node;
and if so, determining that the thread acquires the distributed lock.
4. The method according to any one of claims 1-3, further comprising:
and after the target persistent node is deleted, deleting the reference token corresponding to the target resource identifier.
5. An apparatus for implementing a distributed lock, comprising:
the creating module is used for creating a sequence temporary node corresponding to a thread under a target persistent node in a Zookeeper when the thread requests a distributed lock with resource information of a target resource identifier, generating a target token corresponding to the thread, and taking the target token as a reference token corresponding to the target resource identifier; the target persistent node is a persistent node named by the target resource identifier;
the judging module is used for judging whether the thread acquires the distributed lock or not based on the sequence number of the sequence temporary node corresponding to the thread;
the access module is used for accessing the resource information when the thread is judged to acquire the distributed lock;
the deleting module is used for deleting the sequence temporary node corresponding to the thread after the resource information is accessed, judging whether a target token corresponding to the thread is consistent with a reference token corresponding to the target resource identifier or not, and deleting the target persistent node if the target token is consistent with the reference token;
wherein the creating module is specifically configured to:
when a thread requests a distributed lock with resource information of a target resource identifier, creating a sequence temporary node corresponding to the thread under a target persistent node in a Zookeeper, and generating a target token corresponding to the thread by using a sequence number of the sequence temporary node corresponding to the thread.
6. The apparatus of claim 5, wherein the creation module is specifically configured to:
when a thread requests a distributed lock with resource information of a target resource identifier, judging whether a target persistent node named by the target resource identifier exists in a Zookeeper;
if so, creating a sequence temporary node corresponding to the thread under the target persistent node;
and if the order temporary node does not exist, creating a target persistent node named by a target resource identifier in the Zookeeper, and creating the order temporary node corresponding to the thread under the target persistent node.
7. The apparatus of claim 5, wherein the determining module is specifically configured to:
regularly judging whether the sequence number of the sequence temporary node corresponding to the thread is the minimum value in the sequence number group or not; the sequence number group consists of sequence numbers of all sequence temporary nodes under the target persistent node;
and if so, determining that the thread acquires the distributed lock.
8. The apparatus of any of claims 5-7, further comprising: a token deletion module;
and the token deleting module is used for deleting the reference token corresponding to the target resource identifier after the deleting module deletes the target persistent node.
9. An electronic device is characterized by comprising a processor, a communication interface, a memory and a communication bus, wherein the processor and the communication interface are used for realizing mutual communication by the memory through the communication bus;
a memory for storing a computer program;
a processor for implementing the method steps of any of claims 1 to 4 when executing a program stored in the memory.
CN201810560749.XA 2018-05-25 2018-05-25 Method and device for realizing distributed lock and electronic equipment Active CN108897628B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201810560749.XA CN108897628B (en) 2018-05-25 2018-05-25 Method and device for realizing distributed lock and electronic equipment

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201810560749.XA CN108897628B (en) 2018-05-25 2018-05-25 Method and device for realizing distributed lock and electronic equipment

Publications (2)

Publication Number Publication Date
CN108897628A CN108897628A (en) 2018-11-27
CN108897628B true CN108897628B (en) 2020-06-26

Family

ID=64344043

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201810560749.XA Active CN108897628B (en) 2018-05-25 2018-05-25 Method and device for realizing distributed lock and electronic equipment

Country Status (1)

Country Link
CN (1) CN108897628B (en)

Families Citing this family (13)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109525436B (en) * 2018-12-19 2022-09-16 福建新大陆软件工程有限公司 Method and system for switching main application program and standby application program
CN110474963A (en) * 2019-07-17 2019-11-19 中国平安人寿保险股份有限公司 A kind of resource access method based on zookeeper, system, medium and electronic equipment
CN111163140A (en) * 2019-12-20 2020-05-15 深圳市中农易讯信息技术有限公司 Method, apparatus and computer readable storage medium for resource acquisition and allocation
CN113127210B (en) * 2019-12-31 2024-03-29 阿里巴巴集团控股有限公司 Storage management method, device and storage medium of distributed system
CN111461643A (en) * 2020-03-17 2020-07-28 上海维信荟智金融科技有限公司 Sponsor routing method and system
CN112099962B (en) * 2020-11-06 2021-02-19 成都新希望金融信息有限公司 Distributed lock implementation method and device and electronic equipment
CN112131012B (en) * 2020-11-26 2021-07-20 九州华兴集成电路设计(北京)有限公司 Token processing method, token processing device and computer readable storage medium
CN112351110B (en) * 2020-11-27 2022-10-21 杭州安恒信息技术股份有限公司 ID distribution method and device based on zookeeper
CN112559558B (en) * 2020-12-07 2024-04-09 北京理房通支付科技有限公司 Method and device for generating serial numbers, computing equipment and storage medium
CN112486695A (en) * 2020-12-07 2021-03-12 浪潮云信息技术股份公司 Distributed lock implementation method under high concurrency service
CN113285975A (en) * 2021-03-30 2021-08-20 紫光云技术有限公司 High-concurrency resource detection method
CN113641477A (en) * 2021-08-17 2021-11-12 南方电网深圳数字电网研究院有限公司 Thread scheduling method and device based on Zookeeper
CN114884961B (en) * 2022-04-21 2024-04-16 京东科技信息技术有限公司 Distributed lock handover method, apparatus, electronic device, and computer readable medium

Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101273333A (en) * 2005-04-13 2008-09-24 普罗格雷斯软件公司 Fault tolerant distributed lock management
CN103997498A (en) * 2014-05-27 2014-08-20 北京京东尚科信息技术有限公司 Distributed lock service implementation method and component
CN106776156A (en) * 2016-12-13 2017-05-31 东软集团股份有限公司 A kind of service control method and device
CN107450991A (en) * 2017-07-24 2017-12-08 无锡江南计算技术研究所 A kind of efficiently distributed global lock coordination approach

Family Cites Families (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
GB0227825D0 (en) * 2002-11-29 2003-01-08 Ibm High-performance lock management for flash copy in n-way shared storage systems

Patent Citations (4)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN101273333A (en) * 2005-04-13 2008-09-24 普罗格雷斯软件公司 Fault tolerant distributed lock management
CN103997498A (en) * 2014-05-27 2014-08-20 北京京东尚科信息技术有限公司 Distributed lock service implementation method and component
CN106776156A (en) * 2016-12-13 2017-05-31 东软集团股份有限公司 A kind of service control method and device
CN107450991A (en) * 2017-07-24 2017-12-08 无锡江南计算技术研究所 A kind of efficiently distributed global lock coordination approach

Non-Patent Citations (1)

* Cited by examiner, † Cited by third party
Title
基于zookeeper实现分布式锁;panamera918;《https://blog.csdn.net/panamera918/article/details/80196762》;20180504;第1页-第10页 *

Also Published As

Publication number Publication date
CN108897628A (en) 2018-11-27

Similar Documents

Publication Publication Date Title
CN108897628B (en) Method and device for realizing distributed lock and electronic equipment
CN106712981B (en) Node change notification method and device
CN111274214B (en) File lock processing method and device, electronic equipment and storage medium
CN108829738B (en) Data storage method and device in ceph
CN111556005A (en) Authority management method, device, electronic equipment and storage medium
CN109213604B (en) Data source management method and device
TWI712972B (en) Trustworthiness verification method, system, device and equipment of alliance chain
CN111046310A (en) Page processing method, device, server and computer readable storage medium
CN111385255B (en) Asynchronous call implementation method and device, server and server cluster
CN114925015A (en) Data processing method, device, equipment and medium based on multi-core processor
CN108108126B (en) Data processing method, device and equipment
CN112181599A (en) Model training method, device and storage medium
CN108804568B (en) Method and device for storing copy data in Openstack in ceph
CN111309693A (en) Data synchronization method, device and system, electronic equipment and storage medium
CN113742050B (en) Method, device, computing equipment and storage medium for operating data object
CN111835513B (en) Method, device and equipment for updating certificate data
CN108388424B (en) Method and device for calling interface data and electronic equipment
CN111858497A (en) Storage type conversion method, device and equipment
CN111400324B (en) Method, device and server for locking object in cloud storage
CN113076552B (en) HDFS (Hadoop distributed File System) resource access permission verification method and device and electronic equipment
CN111163088B (en) Message processing method, system and device and electronic equipment
CN110784479B (en) Data verification method and device, electronic equipment and storage medium
CN115525684A (en) Data writing method, device, equipment, computer readable medium and program product
CN117592097A (en) Data processing method and device, electronic equipment and storage medium
CN115730011A (en) Data storage method, device and equipment of fragment type cluster

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
GR01 Patent grant
GR01 Patent grant