CN108897628A - A kind of implementation method of distributed lock, device and electronic equipment - Google Patents

A kind of implementation method of distributed lock, device and electronic equipment Download PDF

Info

Publication number
CN108897628A
CN108897628A CN201810560749.XA CN201810560749A CN108897628A CN 108897628 A CN108897628 A CN 108897628A CN 201810560749 A CN201810560749 A CN 201810560749A CN 108897628 A CN108897628 A CN 108897628A
Authority
CN
China
Prior art keywords
target
thread
node
lasting
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.)
Granted
Application number
CN201810560749.XA
Other languages
Chinese (zh)
Other versions
CN108897628B (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

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

Landscapes

  • Engineering & Computer Science (AREA)
  • Software Systems (AREA)
  • Theoretical Computer Science (AREA)
  • Physics & Mathematics (AREA)
  • General Engineering & Computer Science (AREA)
  • General Physics & Mathematics (AREA)
  • Multi Processors (AREA)
  • Computer And Data Communications (AREA)

Abstract

The embodiment of the invention provides a kind of implementation method of distributed lock, device and electronic equipments.The method includes:Thread is when request has the distributed lock of the resource information of target resource identifier, under the lasting node of target in Zookeeper, create the corresponding sequence transient node of the thread, and the corresponding target spoke of the thread is generated, token is referred to using the target spoke as target resource identifier is corresponding;Based on the sequence number of the corresponding sequence transient node of thread, judge whether thread gets distributed lock;When judging that thread gets distributed lock, resource information is accessed;After access finishes resource information, the corresponding sequence transient node of the thread is deleted, and judges whether the corresponding target spoke of the thread is corresponding consistent with reference to token with the target resource identifier, if unanimously, deleting the lasting node of the target.This programme can solve the problem of larger memory space of occupancy caused by the excessive persistently node of creation.

Description

A kind of implementation method of distributed lock, device and electronic equipment
Technical field
The present invention relates to field of computer technology, more particularly to a kind of implementation method of distributed lock, device and electronics Equipment.
Background technique
Distributed lock is mainly used for that striding course, the shared resource across host, across a network is protected to realize in distributed environment Exclusive reference, to guarantee the consistency of data.Realize that there are mainly three types of popular schemes for distributed lock at present, respectively based on number Distributed lock is realized according to library, caching and Zookeeper.Zookeeper (abbreviation zk) is a kind of offer configuration management, distributed association With and name centralization service system, Zookeeper provide these functions be all in distributed system very bottom and Essential basic function, developer can construct the various distributed systems of oneself on Zookeeper.
However, inventor has found in the implementation of the present invention, the prior art is realizing distribution based on Zookeeper During lock, if thread request distributed lock, generally requires and create lasting node in Zookeeper.However, due to holding The creation of long node is that basis is identified as with resource, a lasting node can be all created for each resource identification, when more The distributed lock for the resource information that thread request is identified about its corresponding resource, and the corresponding resource identification of multiple threads is not identical When, the lasting node for needing to create will be more.But after distributed lock is released, due to the lasting node created before Can still exist in Zookeeper, cause to occupy biggish memory space, so as to influence Zookeeper performance.
Summary of the invention
The implementation method for being designed to provide a kind of distributed lock, device and the electronic equipment of the embodiment of the present invention, with solution Certainly the problem of the larger memory space of occupancy caused by the excessive persistently node of creation.Specific technical solution is as follows:
In a first aspect, the embodiment of the invention provides a kind of implementation method of distributed lock, the method includes:
Target of the thread when request has the distributed lock of the resource information of target resource identifier, in Zookeeper Under lasting node, the corresponding sequence transient node of the thread is created, and generate the corresponding target spoke of the thread, with described Target spoke is corresponding with reference to token as the target resource identifier;Wherein, the lasting node of the target is with the target The lasting node of resource identification name;
Based on the sequence number of the corresponding sequence transient node of the thread, judge whether the thread gets described point Cloth lock;
When judging that the thread gets the distributed lock, the resource information is accessed;
After access finishes the resource information, the corresponding sequence transient node of the thread is deleted, and judge the line Whether the corresponding target spoke of journey is corresponding consistent with reference to token with the target resource identifier, if unanimously, deleting the mesh Mark lasting node.
Optionally, it under the lasting node of the target in Zookeeper, creates the corresponding sequence of the thread and temporarily saves The step of point, including:
Judge in Zookeeper with the presence or absence of the lasting node of target named with target resource identifier;
If it does, creating the corresponding sequence transient node of the thread under the lasting node of the target;
If it does not, the lasting node of target named with target resource identifier is created in the Zookeeper, and The corresponding sequence transient node of the thread is created under the lasting node of target.
Optionally, whether the sequence number based on the corresponding sequence transient node of the thread, judge the thread The step of getting the distributed lock, including:
Timing judges whether the sequence number of the corresponding sequence transient node of the thread is minimum in sequence number group Value;Wherein, the sequence number group is made of the sequence number of all sequence transient nodes under the lasting node of the target;
If so, determining that the thread gets the distributed lock.
Optionally, described the step of generating the thread corresponding target spoke, including:
Using the sequence number of the corresponding sequence transient node of the thread, the corresponding target spoke of the thread is generated.
Optionally, the method also includes:
After deleting the lasting node of target, it is corresponding with reference to token to delete the target resource identifier.
Second aspect, the embodiment of the invention also provides a kind of realization device of distributed lock, described device includes:
Creation module, for thread when request has the distributed lock of the resource information of target resource identifier, Under the lasting node of target in Zookeeper, the corresponding sequence transient node of the thread is created, and it is corresponding to generate the thread Target spoke, using the target spoke as the target resource identifier it is corresponding refer to token;Wherein, the target is lasting Node is the lasting node named with the target resource identifier;
Judgment module judges that the thread is for the sequence number based on the corresponding sequence transient node of the thread It is no to get the distributed lock;
Access modules, for accessing the resource information when judging that the thread gets the distributed lock;
Removing module, for deleting the thread corresponding sequence transient node after access finishes the resource information, And judge whether the corresponding target spoke of the thread is corresponding consistent with reference to token with the target resource identifier, if one It causes, deletes the lasting node of the target.
Optionally, the creation module, is specifically used for:
Thread when request has the distributed lock of the resource information of target resource identifier, judge in Zookeeper whether In the presence of the lasting node of target named with target resource identifier;
If it does, creating the corresponding sequence transient node of the thread under the lasting node of the target;
If it does not, the lasting node of target named with target resource identifier is created in the Zookeeper, and The corresponding sequence transient node of the thread is created under the lasting node of target.
Optionally, the judgment module, is specifically used for:
Timing judges whether the sequence number of the corresponding sequence transient node of the thread is minimum in sequence number group Value;Wherein, the sequence number group is made of the sequence number of all sequence transient nodes under the lasting node of the target;
If so, determining that the thread gets the distributed lock.
Optionally, the creation module, is specifically used for:
Target of the thread when request has the distributed lock of the resource information of target resource identifier, in Zookeeper Under lasting node, the corresponding sequence transient node of the thread is created, the sequence of the corresponding sequence transient node of the thread is utilized Column number generates the corresponding target spoke of the thread.
Optionally, described device further includes:Token removing module;
The token removing module, for deleting the mesh after the removing module deletes the lasting node of target It is corresponding with reference to token to mark resource identification.
The third aspect, the embodiment of the invention also provides a kind of electronic equipment, which is characterized in that including processor, communication Interface, memory and communication bus, wherein processor, communication interface, memory complete mutual lead to by communication bus Letter;
Memory, for storing computer program;
Processor when for executing the program stored on memory, realizes a kind of distribution described in above-mentioned first aspect The implementation method of formula lock.
Fourth aspect, it is described computer-readable to deposit the embodiment of the invention also provides a kind of computer readable storage medium Instruction is stored in storage media, when run on a computer, so that computer executes one kind described in above-mentioned first aspect The implementation method of distributed lock.
5th aspect, the embodiment of the invention also provides a kind of computer program products comprising instruction, when it is being calculated When being run on machine, so that computer executes a kind of implementation method of distributed lock described in above-mentioned first aspect.
In scheme provided in an embodiment of the present invention, thread has the distribution of the resource information of target resource identifier in request When lock, target spoke can be generated, and refer to token, such target money using the target spoke as target resource identifier is corresponding Source identifies corresponding consistent with the request target spoke of thread of distributed lock the latest always with reference to token.Also, it is accessing After finishing resource information, if thread, which judges that the target spoke corresponding to itself is corresponding with the target resource identifier, refers to token Unanimously, then the thread deletes the lasting node of the target, i.e., the request distribution the latest in the case where judging itself for the lasting node of the target When the thread of formula lock, lasting node is deleted.Since after distributed lock competes, lasting node can be deleted, therefore, lead to Cross the problem of this programme can solve the larger memory space of occupancy caused by the excessive persistently node of creation.
Certainly, it implements any of the products of the present invention or method must be not necessarily required to reach all the above excellent simultaneously Point.
Detailed description of the invention
In order to more clearly explain the embodiment of the invention or the technical proposal in the existing technology, to embodiment or will show below There is attached drawing needed in technical description to be briefly described.
Fig. 1 is a kind of flow chart of the implementation method of distributed lock provided in an embodiment of the present invention;
Fig. 2 is a kind of system structure diagram that distributed lock is realized using Zookeeper in the embodiment of the present invention;
Fig. 3 is a kind of structural schematic diagram of the realization device of distributed lock provided in an embodiment of the present invention;
Fig. 4 is the structural schematic diagram of a kind of electronic equipment provided in an embodiment of the present invention.
Specific embodiment
Following will be combined with the drawings in the embodiments of the present invention, and technical solution in the embodiment of the present invention is described.
In order to solve the problems, such as the larger memory space of occupancy caused by the excessive persistently node of creation, the embodiment of the present invention is mentioned Implementation method, device and the electronic equipment of a kind of distributed lock are supplied.
The implementation method for being provided for the embodiments of the invention a kind of distributed lock first below is introduced.
It should be noted that a kind of executing subject of the implementation method of distributed lock provided by the embodiment of the present invention can be with To run on the thread in electronic equipment.Furthermore, it is desirable to which implementation of the present invention can be executed by requesting any thread of distributed lock Method provided by example.In addition, in a particular application, electronic equipment can be terminal or server, it is not limited thereto certainly.
In addition, requesting at least two threads of same distributed lock, it can be belonging respectively in different electronic equipments, that is, need At least two threads for accessing same resource information, can be belonging respectively to different electronic equipments.Certainly, same electronic equipment At least two threads can request same distributed lock, i.e., at least two threads of same electronic equipment can request access to together One resource information.
Fig. 1 is a kind of flow chart of the implementation method of distributed lock provided in an embodiment of the present invention.As shown in Figure 1, this hair A kind of implementation method of distributed lock provided by bright embodiment may include steps of:
S101, thread is when request has the distributed lock of the resource information of target resource identifier, in Zookeeper Under the lasting node of target, the corresponding sequence transient node of the thread is created, and generate the corresponding target spoke of the thread, with The target spoke is corresponding with reference to token as the target resource identifier;Wherein, the lasting node of the target is with described The lasting node of target resource identifier name.
In order to solve prior art problem, in the embodiment of the present invention, resource identification pair can be set in buffer area in advance The initial reference token answered, the as initial reference token of resource information setting.When have thread request have target resource When the distributed lock of the resource information of mark, not only the target with target resource identifier name in Zookeeper is persistently saved Under point, the corresponding sequence transient node of the thread of request to create distributed lock, while the corresponding target spoke of the thread is generated, and And token is referred to using the target spoke of generation as target resource identifier is corresponding.In this way, the corresponding reference of target resource identifier Token is consistent with the request target spoke of thread of distributed lock the latest always, to provide to judge whether to delete lasting node Optimized integration.It is emphasized that since the transient node under lasting node has sequence number, and the transient node first created Sequence number be less than after the sequence number of transient node that creates therefore usually the transient node under lasting node is known as Sequence transient node.
Specifically, creating the corresponding sequence transient node of the thread under the lasting node of target in Zookeeper Step may include:
Judge in Zookeeper with the presence or absence of the lasting node of target named with target resource identifier;
If it does, creating the corresponding sequence transient node of the thread under the lasting node of the target;
If it does not, the lasting node of target named with target resource identifier is created in the Zookeeper, and The corresponding sequence transient node of the thread is created under the lasting node of target.
It is understood that if thread judges that there is no the targets named with target resource identifier in Zookeeper Lasting node may indicate that the thread requests the distributed lock of the resource information with target resource identifier earliest, at this point it is possible to The lasting node of target named with target resource identifier is created in the Zookeeper, and under the lasting node of the target Create the corresponding sequence transient node of the thread.And if thread judges exist in Zookeeper with target resource identifier The lasting node of the target of name, may indicate that the thread request have target resource identifier resource information distributed lock when Between, it is later than at least one other thread, at this point it is possible to which it is corresponding suitable directly to create the thread under the lasting node of the target Sequence transient node.
In addition, generating the specific implementation of the corresponding target spoke of the thread, there are a variety of.Optionally, in a kind of tool In body implementation, the step of the above-mentioned generation thread corresponding target spoke, may include:
Using the sequence number of the corresponding sequence transient node of the thread, the corresponding target spoke of the thread is generated.
In practical application, when creating the corresponding sequence transient node of thread under the lasting node of target, each sequence is interim Node all corresponds to a sequence number, and the sequence number of the sequence transient node first created be less than after the sequence that creates temporarily save The sequence number of point.
In the embodiment of the present invention, it can use the sequence number of the corresponding sequence transient node of each thread, it is right to generate its The target spoke answered.Specifically, can be directly by the sequence number of the corresponding sequence transient node of each thread, as the thread Corresponding target spoke;The sequence number of the corresponding sequence transient node of each thread can also be encrypted, then will be added Information after close is as the corresponding target spoke of the thread, and the specific Encryption Algorithm embodiment of the present invention is without limitation.
It should be noted that creating the lasting node of target named with target resource identifier in the Zookeeper Specific implementation, and create under the lasting node of the target specific implementation of the corresponding sequence transient node of the thread Mode can be realized using the prior art.Also, the embodiment of the present invention is only illustrated by taking above-mentioned generating mode as an example, real The mode that the corresponding target spoke of thread is generated in the application of border is not limited to that.
S102 judges whether the thread gets based on the sequence number of the corresponding sequence transient node of the thread The distributed lock.
Under the lasting node of target when thread request distributed lock, and in Zookeeper, it is corresponding to create thread After sequence transient node, it can judge whether thread gets based on the sequence number of the corresponding sequence transient node of thread Distributed lock.
Specifically, the sequence number based on the corresponding sequence transient node of the thread, judges whether the thread obtains The step of to the distributed lock, may include:
Timing judges whether the sequence number of the corresponding sequence transient node of the thread is minimum in sequence number group Value;Wherein, the sequence number group is made of the sequence number of all sequence transient nodes under the lasting node of the target;
If so, determining that the thread gets the distributed lock.
When creating the corresponding sequence transient node of thread under the lasting node of target, the sequence transient node created is corresponding One sequence number, then, all sequence transient node corresponding sequences number created under the lasting node of the target will group At a sequence number group.
Wherein, thread judges that a kind of mode for oneself whether getting distributed lock can be:Timing judges the thread institute Whether the sequence number of corresponding sequence transient node is minimum value in sequence number group.If sequence corresponding to the thread The sequence number of transient node is the minimum value in sequence number group, then the thread gets distributed lock.If the thread institute The sequence number of corresponding sequence transient node is not the minimum value in sequence number group, then the thread does not get distribution Lock continues waiting for obtaining distributed lock.Certainly, the application is only illustrated for above-mentioned, and thread is judged in practical application The mode for whether getting distributed lock is not limited to that.
Such as:Thread can determine that a first sequence transient node, the first sequence transient node are corresponding sequence Number is less than the node of the sequence number of the sequence transient node of the thread itself;To the first sequence transient node registered events Monitor;When obtaining the notice that the first sequence transient node is deleted, the corresponding sequence transient node of the thread is judged Sequence number whether be minimum value in sequence number group, if so, determining that the thread gets the distributed lock, such as Fruit is not that thread can redefine a first new sequence transient node, and monitor the first new sequence transient node Whether it is deleted.
S103 accesses the resource information when judging that the thread gets the distributed lock.
When judging that thread gets distributed lock, the accessible resource information of the thread.Specifically, can be to resource Information is read out, modifies, deleting or completing a certain operation etc. using resource information, and thread is visited in the embodiment of the present invention Ask the concrete form of resource information with no restriction.
S104 deletes the corresponding sequence transient node of the thread, and judge institute after access finishes the resource information It is corresponding whether consistent with reference to token with the target resource identifier to state the corresponding target spoke of thread, if unanimously, deleting institute State the lasting node of target.
For thread when completing creation sequence transient node, thread sequence corresponding with the thread in Zookeeper is interim Node establishes connection, should in thread and Zookeeper after thread gets distributed lock, and access finishes resource information The corresponding sequence transient node of thread disconnects, and deletes the corresponding sequence transient node of the thread, thread release lock.Together When judge the corresponding target spoke of the thread it is corresponding with target resource identifier with reference to token it is whether consistent, when the thread is corresponding When target spoke is corresponding with target resource identifier consistent with reference to token, deletion is persistently saved with the target that target resource identifier is named Point;When the corresponding target spoke of the thread is corresponding with target resource identifier inconsistent with reference to token, do not deal with.
In the embodiment of the present invention, the corresponding target spoke of thread is raw when creating the corresponding sequence transient node of the thread At.Target resource identifier is corresponding to be with reference to token, what when creation thread corresponding sequence transient node updated, i.e., target provides Source mark is corresponding to be with reference to token, corresponding with the last one creation thread under the lasting node of target of target resource identifier name Sequence transient node target spoke.
So, it when the corresponding target spoke of thread is corresponding with target resource identifier consistent with reference to token, indicates with mesh All threads all successively get distributed lock under the lasting node of target of mark resource identification name, and currently release should The thread of distributed lock is that the last one holds the thread of lock under the lasting node of the target, then executes the deletion of the lasting node of the target Operation.When the corresponding target spoke of thread is corresponding with target resource identifier inconsistent with reference to token, indicate that current release should The thread of distributed lock is not that the last one holds the thread of lock under the lasting node of the target, then does not deal with.
In the embodiment of the present invention, above-mentioned implementation method can also include:
After deleting the lasting node of target, it is corresponding with reference to token to delete the target resource identifier.
It is the lasting node of target when thread accesses finish the thread that resource information, release lock, and current release profile formula are locked When the last one holds the thread of lock down, the lasting node of the target is deleted.After the lasting node of delete target, by target resource mark Know the corresponding token that refers to delete together.
A kind of implementation method of distributed lock provided in an embodiment of the present invention, thread have target resource identifier in request When the distributed lock of resource information, target spoke can be generated, and using the target spoke as the corresponding ginseng of target resource identifier Examine token, such target resource identifier is corresponding with reference to the token target spoke one with the thread for requesting distributed lock the latest always It causes.Also, after access finishes resource information, if thread judges target spoke and the target resource mark corresponding to itself Know corresponding consistent with reference to token, then the thread deletes the lasting node of the target, i.e., persistently saves for the target judging itself When requesting the thread of distributed lock the latest under point, lasting node is deleted.Since after distributed lock competes, lasting node can To be deleted, therefore, asking for the larger memory space of occupancy caused by the excessive persistently node of creation can solve by this programme Topic.
Below with reference to specific application example, be provided for the embodiments of the invention the implementation method of distributed lock a kind of into Row is introduced.
Assuming that the application environment of the implementation method of the distributed lock is, it is applied in open platform credit grade business, makes Realize that the system architecture schematic diagram of distributed lock is as shown in Figure 2 with Zookeeper.
Thread in credit grade system has the resource information of target resource identifier to the request of Zookeeper cluster Distributed lock under the lasting node of target in Zookeeper, creates the corresponding sequence transient node of the thread, and generate The corresponding target spoke of the thread refers to token using the target spoke as the target resource identifier is corresponding.
Based on the sequence number of the corresponding sequence transient node of the thread, judge whether the thread gets described point Cloth lock.
When judging that the thread gets the distributed lock, the thread reads MySql database and obtains data Then information executes clearing logic, and calculated result is stored in MySql database, calculating terminates.
After calculating, thread sequence transient node corresponding with the thread in Zookeeper is disconnected, and is released Lock is put, the corresponding sequence transient node of the thread is deleted, and judges the corresponding target spoke of the thread and target money Whether source mark is corresponding consistent with reference to token, if unanimously, deleting the lasting node of the target.
In the embodiment of the present invention, thread is in the resource information to the request of Zookeeper cluster with target resource identifier When distributed lock, target spoke can be generated, and refer to token using the target spoke as target resource identifier is corresponding, in this way Target resource identifier is corresponding consistent with the request target spoke of thread of distributed lock the latest always with reference to token.Also, It gets data information to complete after calculating, if thread judges target spoke and the target resource identifier pair corresponding to itself The reference token answered is consistent, then the thread deletes the lasting node of the target, i.e., in the case where judging itself for the lasting node of the target When requesting the thread of distributed lock the latest, lasting node is deleted.Due to after distributed lock competes, lasting node can be by It deletes, therefore, the problem of larger memory space of occupancy caused by the excessive persistently node of creation can solve by this programme.
Corresponding to above method embodiment, the embodiment of the invention provides a kind of realization devices of distributed lock, such as Fig. 3 institute Show, the apparatus may include:
Creation module 301, for thread when request has the distributed lock of the resource information of target resource identifier, Under the lasting node of target in Zookeeper, the corresponding sequence transient node of the thread is created, and it is corresponding to generate the thread Target spoke, using the target spoke as the target resource identifier it is corresponding refer to token;Wherein, the target is lasting Node is the lasting node named with the target resource identifier;
Judgment module 302 judges the thread for the sequence number based on the corresponding sequence transient node of the thread Whether the distributed lock is got;
Access modules 303, for when judging that the thread gets the distributed lock, accessing the resource letter Breath;
Removing module 304, for deleting the corresponding sequence of the thread and temporarily saving after access finishes the resource information Point, and judge whether the corresponding target spoke of the thread is corresponding consistent with reference to token with the target resource identifier, if Unanimously, the lasting node of the target is deleted.
A kind of realization device of distributed lock provided in an embodiment of the present invention, thread have target resource identifier in request When the distributed lock of resource information, target spoke can be generated, and using the target spoke as the corresponding ginseng of target resource identifier Examine token, such target resource identifier is corresponding with reference to the token target spoke one with the thread for requesting distributed lock the latest always It causes.Also, after access finishes resource information, if thread judges target spoke and the target resource mark corresponding to itself Know corresponding consistent with reference to token, then the thread deletes the lasting node of the target, i.e., persistently saves for the target judging itself When requesting the thread of distributed lock the latest under point, lasting node is deleted.Since after distributed lock competes, lasting node can To be deleted, therefore, asking for the larger memory space of occupancy caused by the excessive persistently node of creation can solve by this programme Topic.
It should be noted that the device of the embodiment of the present invention is and a kind of implementation method pair of distributed lock shown in FIG. 1 The device answered, a kind of all embodiments of the implementation method of distributed lock shown in FIG. 1 are suitable for the device, and can reach Identical beneficial effect.
Wherein, creation module described above, is specifically used for:
Thread when request has the distributed lock of the resource information of target resource identifier, judge in Zookeeper whether In the presence of the lasting node of target named with target resource identifier;
If it does, creating the corresponding sequence transient node of the thread under the lasting node of the target;
If it does not, the lasting node of target named with target resource identifier is created in the Zookeeper, and The corresponding sequence transient node of the thread is created under the lasting node of target.
The judgment module, is specifically used for:
Timing judges whether the sequence number of the corresponding sequence transient node of the thread is minimum in sequence number group Value;Wherein, the sequence number group is made of the sequence number of all sequence transient nodes under the lasting node of the target;
If so, determining that the thread gets the distributed lock.
The creation module, is specifically used for:
Target of the thread when request has the distributed lock of the resource information of target resource identifier, in Zookeeper Under lasting node, the corresponding sequence transient node of the thread is created, the sequence of the corresponding sequence transient node of the thread is utilized Column number generates the corresponding target spoke of the thread.
Described device further includes:Token removing module;
The token removing module, for deleting the mesh after the removing module deletes the lasting node of target It is corresponding with reference to token to mark resource identification.
The embodiment of the invention also provides a kind of electronic equipment, as shown in figure 4, include processor 401, communication interface 402, Memory 403 and communication bus 404, wherein processor 401, communication interface 402, memory 403 are complete by communication bus 404 At mutual communication,
Memory 403, for storing computer program;
Processor 401 when for executing the program stored on memory 403, is realized provided by the embodiment of the present invention Method.
A kind of electronic equipment provided in an embodiment of the present invention, thread have the resource information of target resource identifier in request When distributed lock, target spoke can be generated, and refer to token using the target spoke as target resource identifier is corresponding, in this way Target resource identifier is corresponding consistent with the request target spoke of thread of distributed lock the latest always with reference to token.Also, After access finishes resource information, if thread judges the target spoke ginseng corresponding with the target resource identifier corresponding to itself It is consistent to examine token, then the thread deletes the lasting node of the target, i.e., asks the latest in the case where judging itself for the lasting node of the target When seeking the thread of distributed lock, lasting node is deleted.Since after distributed lock competes, lasting node can be deleted, Therefore, the problem of larger memory space of occupancy caused by the excessive persistently node of creation can solve by this programme.
The communication bus that above-mentioned electronic equipment is mentioned can be Peripheral Component Interconnect standard (Peripheral Component Interconnect, abbreviation PCI) bus or expanding the industrial standard structure (Extended Industry Standard Architecture, abbreviation EISA) bus etc..The communication bus can be divided into address bus, data/address bus, control bus etc.. Only to be indicated with a thick line in figure, it is not intended that an only bus or a type of bus convenient for indicating.
Communication interface is for the communication between above-mentioned electronic equipment and other equipment.
Memory may include random access memory (Random Access Memory, abbreviation RAM), also may include Nonvolatile memory (non-volatile memory), for example, at least a magnetic disk storage.Optionally, memory may be used also To be storage device that at least one is located remotely from aforementioned processor.
Above-mentioned processor can be general processor, including central processing unit (Central Processing Unit, Abbreviation CPU), network processing unit (Network Processor, abbreviation NP) etc.;It can also be digital signal processor (Digital Signal Processing, abbreviation DSP), specific integrated circuit (Application Specific Integrated Circuit, abbreviation ASIC), field programmable gate array (Field-Programmable Gate Array, Abbreviation FPGA) either other programmable logic device, discrete gate or transistor logic, discrete hardware components.
In another embodiment provided by the invention, a kind of computer readable storage medium is additionally provided, which can It reads to be stored with instruction in storage medium, when run on a computer, so that computer executes any institute in above-described embodiment The implementation method for the distributed lock stated, to obtain identical technical effect.
In another embodiment provided by the invention, a kind of computer program product comprising instruction is additionally provided, when it When running on computers, so that computer executes the implementation method of any distributed lock in above-described embodiment, to obtain Take identical technical effect.
In the above-described embodiments, can come wholly or partly by software, hardware, firmware or any combination thereof real It is existing.When implemented in software, it can entirely or partly realize in the form of a computer program product.The computer program Product includes one or more computer instructions.When loading on computers and executing the computer program instructions, all or It partly generates according to process or function described in the embodiment of the present invention.The computer can be general purpose computer, dedicated meter Calculation machine, computer network or other programmable devices.The computer instruction can store in computer readable storage medium In, or from a computer readable storage medium to the transmission of another computer readable storage medium, for example, the computer Instruction can pass through wired (such as coaxial cable, optical fiber, number from a web-site, computer, server or data center User's line (DSL)) or wireless (such as infrared, wireless, microwave etc.) mode to another web-site, computer, server or Data center is transmitted.The computer readable storage medium can be any usable medium that computer can access or It is comprising data storage devices such as one or more usable mediums integrated server, data centers.The usable medium can be with It is magnetic medium, (for example, floppy disk, hard disk, tape), optical medium (for example, DVD) or semiconductor medium (such as solid state hard disk Solid State Disk (SSD)) etc..
It should be noted that, in this document, relational terms such as first and second and the like are used merely to a reality Body or operation are distinguished with another entity or operation, are deposited without necessarily requiring or implying between these entities or operation In any actual relationship or order or sequence.Moreover, the terms "include", "comprise" or its any other variant are intended to Non-exclusive inclusion, so that the process, method, article or equipment including a series of elements is not only wanted including those Element, but also including other elements that are not explicitly listed, or further include for this process, method, article or equipment Intrinsic element.In the absence of more restrictions, the element limited by sentence " including one ... ", it is not excluded that There is also other identical elements in the process, method, article or apparatus that includes the element.
Each embodiment in this specification is all made of relevant mode and describes, same and similar portion between each embodiment Dividing may refer to each other, and each embodiment focuses on the differences from other embodiments.Especially for device reality For applying example, since it is substantially similar to the method embodiment, so being described relatively simple, related place is referring to embodiment of the method Part explanation.
The foregoing is merely illustrative of the preferred embodiments of the present invention, is not intended to limit the scope of the present invention.It is all Any modification, equivalent replacement, improvement and so within the spirit and principles in the present invention, are all contained in protection scope of the present invention It is interior.

Claims (11)

1. a kind of implementation method of distributed lock, which is characterized in that including:
For thread when request has the distributed lock of the resource information of target resource identifier, the target in Zookeeper is lasting Under node, the corresponding sequence transient node of the thread is created, and generate the corresponding target spoke of the thread, with the target Token is corresponding with reference to token as the target resource identifier;Wherein, the lasting node of the target is with the target resource Identify the lasting node of name;
Based on the sequence number of the corresponding sequence transient node of the thread, judge whether the thread gets the distribution Lock;
When judging that the thread gets the distributed lock, the resource information is accessed;
After access finishes the resource information, the corresponding sequence transient node of the thread is deleted, and judge the thread pair Whether the target spoke answered is corresponding consistent with reference to token with the target resource identifier, if unanimously, deleting the target and holding Long node.
2. the method according to claim 1, wherein being created under the lasting node of the target in Zookeeper The step of building the thread corresponding sequence transient node, including:
Judge in Zookeeper with the presence or absence of the lasting node of target named with target resource identifier;
If it does, creating the corresponding sequence transient node of the thread under the lasting node of the target;
If it does not, creating the lasting node of target named with target resource identifier in the Zookeeper, and described The corresponding sequence transient node of the thread is created under the lasting node of target.
3. the method according to claim 1, wherein described based on the corresponding sequence transient node of the thread Sequence number judges the step of whether thread gets the distributed lock, including:
Timing judges whether the sequence number of the corresponding sequence transient node of the thread is minimum value in sequence number group;Its In, the sequence number group is made of the sequence number of all sequence transient nodes under the lasting node of the target;
If so, determining that the thread gets the distributed lock.
4. the method according to claim 1, wherein the step for generating the corresponding target spoke of the thread Suddenly, including:
Using the sequence number of the corresponding sequence transient node of the thread, the corresponding target spoke of the thread is generated.
5. method according to claim 1-4, which is characterized in that the method also includes:
After deleting the lasting node of target, it is corresponding with reference to token to delete the target resource identifier.
6. a kind of realization device of distributed lock, which is characterized in that including:
Creation module, for thread when request has the distributed lock of the resource information of target resource identifier, in Zookeeper In the lasting node of target under, create the corresponding sequence transient node of the thread, and generate the corresponding target of the thread and enable Board refers to token using the target spoke as the target resource identifier is corresponding;Wherein, the lasting node of the target be with The lasting node of the target resource identifier name;
Judgment module judges whether the thread obtains for the sequence number based on the corresponding sequence transient node of the thread Get the distributed lock;
Access modules, for accessing the resource information when judging that the thread gets the distributed lock;
Removing module, for deleting the corresponding sequence transient node of the thread, and sentence after access finishes the resource information Whether the corresponding target spoke of the thread of breaking is corresponding consistent with reference to token with the target resource identifier, if unanimously, deleted Except the lasting node of the target.
7. device according to claim 6, which is characterized in that the creation module is specifically used for:
Thread judges to whether there is in Zookeeper when request has the distributed lock of the resource information of target resource identifier The lasting node of target named with target resource identifier;
If it does, creating the corresponding sequence transient node of the thread under the lasting node of the target;
If it does not, creating the lasting node of target named with target resource identifier in the Zookeeper, and described The corresponding sequence transient node of the thread is created under the lasting node of target.
8. device according to claim 6, which is characterized in that the judgment module is specifically used for:
Timing judges whether the sequence number of the corresponding sequence transient node of the thread is minimum value in sequence number group;Its In, the sequence number group is made of the sequence number of all sequence transient nodes under the lasting node of the target;
If so, determining that the thread gets the distributed lock.
9. device according to claim 6, which is characterized in that the creation module is specifically used for:
For thread when request has the distributed lock of the resource information of target resource identifier, the target in Zookeeper is lasting Under node, the corresponding sequence transient node of the thread is created, is compiled using the sequence of the corresponding sequence transient node of the thread Number, generate the corresponding target spoke of the thread.
10. according to any device of claim 6-9, which is characterized in that described device further includes:Token removing module;
The token removing module, for after the removing module deletes the lasting node of target, deleting the target money Source mark is corresponding to refer to token.
11. a kind of electronic equipment, which is characterized in that including processor, communication interface, memory and communication bus, wherein processing Device, communication interface, memory complete mutual communication by communication bus;
Memory, for storing computer program;
Processor when for executing the program stored on memory, realizes any method and step of claim 1-5.
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 true CN108897628A (en) 2018-11-27
CN108897628B 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)

Cited By (13)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN109525436A (en) * 2018-12-19 2019-03-26 福建新大陆软件工程有限公司 Application program main/standby switching method and system
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
CN111461643A (en) * 2020-03-17 2020-07-28 上海维信荟智金融科技有限公司 Sponsor routing method and system
CN112099962A (en) * 2020-11-06 2020-12-18 成都新希望金融信息有限公司 Distributed lock implementation method and device and electronic equipment
CN112131012A (en) * 2020-11-26 2020-12-25 九州华兴集成电路设计(北京)有限公司 Token processing method, token processing device and computer readable storage medium
CN112351110A (en) * 2020-11-27 2021-02-09 杭州安恒信息技术股份有限公司 ID distribution method and device based on zookeeper
CN112486695A (en) * 2020-12-07 2021-03-12 浪潮云信息技术股份公司 Distributed lock implementation method under high concurrency service
CN112559558A (en) * 2020-12-07 2021-03-26 北京理房通支付科技有限公司 Serial number generation method and device, computing device and storage medium
CN113127210A (en) * 2019-12-31 2021-07-16 阿里巴巴集团控股有限公司 Storage management method, device and storage medium of distributed system
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
CN114884961A (en) * 2022-04-21 2022-08-09 京东科技信息技术有限公司 Distributed lock handover method, apparatus, electronic device, and computer-readable medium

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20060095682A1 (en) * 2002-11-29 2006-05-04 Fuente Carlos F High-performance lock management for flash copy in n-way shared storage systems
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

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US20060095682A1 (en) * 2002-11-29 2006-05-04 Fuente Carlos F High-performance lock management for flash copy in n-way shared storage systems
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
PANAMERA918: "基于zookeeper实现分布式锁", 《HTTPS://BLOG.CSDN.NET/PANAMERA918/ARTICLE/DETAILS/80196762》 *

Cited By (17)

* 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
CN109525436A (en) * 2018-12-19 2019-03-26 福建新大陆软件工程有限公司 Application program main/standby switching method and system
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
CN113127210A (en) * 2019-12-31 2021-07-16 阿里巴巴集团控股有限公司 Storage management method, device and storage medium of distributed system
CN111461643A (en) * 2020-03-17 2020-07-28 上海维信荟智金融科技有限公司 Sponsor routing method and system
CN112099962A (en) * 2020-11-06 2020-12-18 成都新希望金融信息有限公司 Distributed lock implementation method and device and electronic equipment
CN112131012A (en) * 2020-11-26 2020-12-25 九州华兴集成电路设计(北京)有限公司 Token processing method, token processing device and computer readable storage medium
CN112351110A (en) * 2020-11-27 2021-02-09 杭州安恒信息技术股份有限公司 ID distribution method and device based on zookeeper
CN112559558A (en) * 2020-12-07 2021-03-26 北京理房通支付科技有限公司 Serial number generation method and device, computing device and storage medium
CN112486695A (en) * 2020-12-07 2021-03-12 浪潮云信息技术股份公司 Distributed lock implementation method under high concurrency service
CN112559558B (en) * 2020-12-07 2024-04-09 北京理房通支付科技有限公司 Method and device for generating serial numbers, computing equipment and storage medium
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
CN114884961A (en) * 2022-04-21 2022-08-09 京东科技信息技术有限公司 Distributed lock handover method, apparatus, electronic device, and computer-readable medium
CN114884961B (en) * 2022-04-21 2024-04-16 京东科技信息技术有限公司 Distributed lock handover method, apparatus, electronic device, and computer readable medium

Also Published As

Publication number Publication date
CN108897628B (en) 2020-06-26

Similar Documents

Publication Publication Date Title
CN108897628A (en) A kind of implementation method of distributed lock, device and electronic equipment
US10235047B2 (en) Memory management method, apparatus, and system
CN109885786B (en) Data caching processing method and device, electronic equipment and readable storage medium
CN109213604B (en) Data source management method and device
CN109525658A (en) A kind of production method, server, equipment, storage medium and operation system
CN109491928A (en) Buffer control method, device, terminal and storage medium
CN106294387B (en) Data migration method and device
CN106095483A (en) The Automation arranging method of service and device
CN109656886B (en) Key value pair-based file system implementation method, device, equipment and storage medium
CN108536473A (en) The method and apparatus for reading data
US9160705B2 (en) Identifier management
CN108154024A (en) A kind of data retrieval method, device and electronic equipment
US11429311B1 (en) Method and system for managing requests in a distributed system
CN112131478B (en) Transaction detection method and device
CN103414756B (en) A kind of task distribution method, distribution node and system
CN110554914B (en) Resource lock management method, device, server and storage medium
CN108804568B (en) Method and device for storing copy data in Openstack in ceph
CN108846009B (en) Copy data storage method and device in ceph
CN115687359A (en) Data table partitioning method and device, storage medium and computer equipment
CN106796644A (en) Access control system and access control method
CN111209304B (en) Data processing method, device and system
CN109725856B (en) Shared node management method and device, electronic equipment and storage medium
CN110019031B (en) File creation method and file management device
CN109583907A (en) A kind of checking method of electronic invoice, device, medium and electronic equipment
CN106650480B (en) File access isolation method and device

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