CN110134522A - A kind of distributed lock realization method and system with heartbeat - Google Patents

A kind of distributed lock realization method and system with heartbeat Download PDF

Info

Publication number
CN110134522A
CN110134522A CN201910273229.5A CN201910273229A CN110134522A CN 110134522 A CN110134522 A CN 110134522A CN 201910273229 A CN201910273229 A CN 201910273229A CN 110134522 A CN110134522 A CN 110134522A
Authority
CN
China
Prior art keywords
lock
heartbeat
ttl
distributed
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.)
Pending
Application number
CN201910273229.5A
Other languages
Chinese (zh)
Inventor
周江翔
孙海涛
周蓉
高国明
王帅
陶伟松
Current Assignee (The listed assignees may be inaccurate. Google has not performed a legal analysis and makes no representation or warranty as to the accuracy of the list.)
Hangzhou Jitter Technology Co Ltd
Original Assignee
Hangzhou Jitter 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 Hangzhou Jitter Technology Co Ltd filed Critical Hangzhou Jitter Technology Co Ltd
Priority to CN201910273229.5A priority Critical patent/CN110134522A/en
Publication of CN110134522A publication Critical patent/CN110134522A/en
Pending legal-status Critical Current

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/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
    • 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

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)

Abstract

The invention belongs to technical field of data processing, disclose a kind of distributed lock realization method and system with heartbeat;After successfully application acquisition lock, the time-out ttl time is arranged to the lock, then starts timer, regularly go to update ttl value, elder generation's timeing closing actuator when lock discharges, no longer updates the ttl value of lock, then release profile formula lock;Since the timing locking successfully acquisition locked automatic release after ttl seconds;Other applicants, which have, after lock release obtains the distributed lock;When not discharging lock according to normal flow, heartbeat also stops, and does not continue timing and goes to update the validity period locked, and validity period expires, and lock is recycled automatically, other service requests acquisition distributed lock.The present invention has heartbeat mechanism, once its service stopping, then heartbeat can not normally be sent, then locking can discharge after validity period, and new service can apply for the lock.

Description

A kind of distributed lock realization method and system with heartbeat
Technical field
The invention belongs to technical field of data processing more particularly to a kind of distributed lock implementation method with heartbeat and it is System.
Background technique
Currently, the prior art commonly used in the trade is such that after service delay machine, the distributed lock that original obtains is not released It puts, the service of the service or backup that newly start can only borrow outer because obtaining can not effectively be worked less than distributed lock Power deletes lock and can just carry out the work.
Distributed lock is very widely used, some are realized with mysql, uses the row lock principle of mysql.Some use redis It realizes, redis provides an atomicity script and does atomic operation, in this way can be by judging whether some value is that fixed value is real The now operation such as application lock.Etcd also provides atomicity operation prevExist etc..
When servicing improper closing due to various reasons, the distributed lock that application obtains does not discharge, therefore new clothes Business cannot obtain this distributed lock.
Distributed lock use scope is bigger, much realizes distributed lock with redis, sql, file system, etcd etc..All It is normal processing method, then applies when needs using lock, has used and just actively discharged.It can be just in normal environment It being often used, but actual motion environment is complicated, some services improper may be exited, once RAM leakage, program bug occurs, Or even the case where power-off, disk failure, it just generally requires that manual releasing is manually gone to lock, O&M cost is bigger.
In conclusion problem of the existing technology is: the existing improper needs that exit of distributed lock service are manually gone Manual releasing lock, causes O&M cost bigger.
Solve the difficulty of above-mentioned technical problem:
Heartbeat be a kind of common inspections of industry service whether normal technology, Heart-Beat Technology is introduced into distributed lock, The validity period of lock is updated using heartbeat.Note above scheme needs distributed lock service to realize timeout mechanism, that is to say, that lock is in Shen Please after can distribute a validity period, cross this validity period after lock and will discharge automatically.
Solve the meaning of above-mentioned technical problem:
Heart-Beat Technology and distributed lock technology are combined, both can guarantee and operated normally under normal circumstances, moreover it is possible to protect System can be restored not needing manpower intervention card automatically in abnormal cases.Introduce the ttl value that heartbeat mechanism updates distributed lock Afterwards, under normal circumstances, distributed lock normally can be applied and discharge.In abnormal cases, lock also can validity period it is expired with Automatically release afterwards, other locks applicant can normally obtain new lock, not need artificial intervention in whole process, completely entirely certainly It is dynamic, also lifting system performance while reducing artificial O&M cost, once need artificial intervention to generally require a few minutes, but system The automatic time for restoring at most only to need several seconds.
Summary of the invention
In view of the problems of the existing technology, the present invention provides a kind of distributed lock implementation method with heartbeat and is System.
The invention is realized in this way a kind of distributed lock implementation method with heartbeat, the distribution with heartbeat Formula locks implementation method
Application obtains the process of lock, is realized using etcd, redis third party's Open Source Code, is grasped using the atomicity of offer Make the atomic operation application lock of principle encapsulation.Etcd provides prevExist, preValue function, calls according to other side's interface.
After successfully application obtains lock, the time-out ttl time is arranged to the lock, then starts timer, regularly goes Ttl value is updated, elder generation's timeing closing actuator when lock discharges no longer updates the ttl value of lock, then release profile formula lock;
Ttl value is updated, a timer timing, the function for calling third party's distributed lock provider to provide, etcd are started The operation that its ttl value of set is done to some key is provided.After calling every time, the ttl value of key just be will be updated, the validity period of value Corresponding change.
Since the timing locking successfully acquisition locked automatic release after ttl seconds;Other applicants have acquisition after lock release The distributed lock;
When not discharging lock according to normal flow, heartbeat also stops, and does not continue timing and goes to update the validity period locked, locks validity period It expires, lock is just recycled automatically, other service requests obtain distributed lock.
Further, being periodically executed the time is set as (ttl-1)/2, and lock is arranged to former ttl value before time-out fails.
Further, apply for that the method locked includes:
Setting lock validity period ttl: validity period is arranged by etcd system;
Starting timer time regularly updates lock validity period ttl value: realizing that regularly task is held by starting timer mechanism Row;The task is each n seconds and a ttl for lock is gone to be set as original value, and n value is set as (ttl-1)/2;
Stop timer timer not continuing to that heartbeat update validity period occurs: closing timer and be allowed to no longer periodically go to execute Pinned task;After closing timer, then will not make up one's mind hop-information, the update that effective time value ttl of the lock will not be timed again.Cause This as time goes by, ttl is gradually kept to 0, once ttl be 0 when lock automatically retracted.This locks reclaim mechanism The offer of third party system etcd, the key which will be 0 to ttl value are recycled.
Further, the method for discharging lock includes: to call third party's distributed lock system, release lock;Timer is no longer made up one's mind Equal ttl value locks automatic release after jump after being kept to 0.
The release of lock, normally discharges process, can call the interface of third party system, and etcd calls delete Discharge corresponding lock.Improper release, it should which discharging improper closing of process of lock etc. causes effectively release to be called to lock Journey is then recycled by system by ttl value expiration mechanism and is locked, to realize the process of lock release
Point with heartbeat for the distributed lock implementation method with heartbeat that another object of the present invention is to provide a kind of Cloth lock control system.
Another object of the present invention is to provide a kind of, and the distributed lock with heartbeat realizes program, operation and terminal, institute It states the distributed lock with heartbeat and realizes that program realizes the distributed lock for having heartbeat described in Claims 1 to 4 any one Implementation method.
Another object of the present invention is to provide a kind of terminal, the terminal, which is carried, realizes the distribution for having heartbeat Lock the controller of implementation method.
Another object of the present invention is to provide a kind of computer readable storage mediums, including instruction, when it is in computer When upper operation, so that computer executes the distributed lock implementation method with heartbeat.
In conclusion advantages of the present invention and good effect are as follows:
The present invention have heartbeat mechanism, once its service stopping, then heartbeat can not normally be sent, then lock can validity period it After discharge, new service can apply for the lock.
The present invention proposes that distributed lock is added in scheme of the timing heartbeat to maintain lock validity period, is not limited only to etcd, benefit With any system realization heartbeat distributed lock method for having ttl time-out setting function again using distributed lock function is provided.
Detailed description of the invention
Fig. 1 is the distributed lock implementation method flow chart provided in an embodiment of the present invention with heartbeat.
Fig. 2 is application lock flow chart provided in an embodiment of the present invention.
Fig. 3 is release lock flow chart provided in an embodiment of the present invention.
Specific embodiment
To make the objectives, technical solutions, and advantages of the present invention more comprehensible, with reference to embodiments, to the present invention into Row is further described.It should be appreciated that the specific embodiments described herein are merely illustrative of the present invention, it is not used to limit The fixed present invention.
The invention reside in the effective time value interfaces of setting ttl for utilizing third party's storage service to provide with Heart-Beat Technology timing more New ttl is allowed to not fail, the interface delete lock that standby provides when normal use completes release lock, improper to exit, and utilizes The mechanism that ttl then ceases to be in force automatically discharges lock.Guarantee to discharge lock so automatically under any circumstance without artificial Intervention.
Distributed lock use scope is bigger, much realizes distributed lock with redis, sql, file system, etcd etc..This A little implementations are all normal processing methods, are then applied when needs using lock, have used and have just actively discharged.These designs It can normal use in normal environment.But actual motion environment is complicated, some services improper may be exited, once hair Raw RAM leakage, program bug, or even the case where power-off, disk failure, just generally require that manual releasing is manually gone to lock, O&M at This meeting is bigger.
In order to solve the above technical problems, below with reference to concrete scheme, the present invention is described in detail.
As shown in Figure 1, the distributed lock implementation method provided in an embodiment of the present invention with heartbeat includes:
S101 is arranged the time-out ttl time to the lock, then starts a timer after successfully application obtains lock, It regularly goes to update ttl value, being generally periodically executed the time is set as (ttl-1)/2, to guarantee that lock can be set before time-out failure For former ttl value.Elder generation's timeing closing actuator, is allowed to no longer update the ttl value of lock when lock release, then release profile formula lock.Lock Other services are possible to application to the lock after release.
S102, ttl value refer to the lock effective time, calculate in seconds, the timing since locking successfully acquisition, after ttl seconds, lock It will discharge automatically;Other applicants are possible to obtain the lock after lock discharges.
S103, once service abnormal end, will not since heartbeat also and then stops when not discharging lock according to normal flow It will continue to timing to go to update the validity period locked, once lock validity period expires, lock will be recycled automatically, other services can Shen It please obtain the lock.Ensure that lock will be also recovered even if servicing improper closing in this way.
As shown in Fig. 2, application lock includes: to call file system, db, redis or etcd in step S101 of the invention The method that equal third parties support the application distributed lock of distributed lock function system.Generally have atomic operation support application lock, on A series of states such as lock, setting state.The consistency of atomic operation guarantee affairs.Regardless of whether application success or failure, system State will be returned and inform result.
Lock validity period ttl is arranged: validity period can be set in the system service of payment distributed lock function a bit, this is system Included function.Subtract 1 every one second ttl, once ttl value reduces to 0, system will recycle lock automatically.This is not each system It supports, file system does not just have this effect.The present invention is used with etcd system, supports this function.
Starting timer time regularly updates lock validity period ttl value: by starting timer mechanism, such as java voice Timer class realizes regularly task execution.Here task is exactly each n seconds and a ttl for lock is gone to be set as original value, so as far as possible It avoids the ttl of lock from reaching 0 value and recycles lock automatically to prevent locking system.In view of the redundancy of performance and system, n value is set as (ttl-1)/2.Ttl value is 10, that n is exactly 4, that is to say, that ttl value was set as 10 in every 4 seconds.
Stop timer timer not continuing to that heartbeat update validity period occurs: closing timer and be allowed to no longer periodically go to execute Pinned task.Ttl in this way by slowly be kept to 0, lock will be automatically retracted.
As shown in figure 3, release lock includes: to call third party's distributed lock system, release in step S101 of the invention Lock.Although timer is no longer sent out equal ttl value after heartbeat and locked after being kept to 0 can also discharge automatically, here explicit release lock, can not It must wait, otherwise just have to ttl seconds equal.
The invention uses on the block platform chain of independent research, miner dig mine process, validate verification process, Many places of execute executive process require to use distributed lock, the acquisition and update of the id value of chain under dpos knows together, just Need multiple miner to dig the synchronization between mine processes, as soon as can only moment can only a process go to update this value, this is needed Use distributed lock.The message-oriented middleware system etcd for using third party to increase income, code is available on GitHub, Middle etcd provides atomic operation interface, taken in the url of access in its V2 version parameter prevExist or PrevValue can realize atomic operation, and the present invention realizes the logic of distributed lock with interface, and interface guarantees a time only There can be a process successfully to obtain.Etcd provides the interfaces such as set again, calls this set to update its ttl value by heartbeat mechanism, It can postpone the out-of-service time of lock.
The foregoing is merely illustrative of the preferred embodiments of the present invention, is not intended to limit the invention, all in essence of the invention Made any modifications, equivalent replacements, and improvements etc., should all be included in the protection scope of the present invention within mind and principle.

Claims (8)

1. a kind of distributed lock implementation method with heartbeat, which is characterized in that the distributed lock realization side with heartbeat Method includes:
One time-out ttl time was arranged to the lock that success is applied, starts timer, regularly updates ttl value;It is fixed first to close when lock release When actuator, no longer update the ttl value of lock, then release profile formula lock;
Since the timing locking successfully acquisition locked automatic release after ttl seconds;Other applicants, which have, after lock release obtains this point Cloth lock;
When discharging lock within the ttl time, heartbeat stops, and does not continue timing and goes to update the validity period locked;Lock validity period expire after from Dynamic recycling, other service requests obtain distributed lock.
2. having the distributed lock implementation method of heartbeat as described in claim 1, which is characterized in that being periodically executed the time is set as (ttl-1)/2, lock is arranged to former ttl value before time-out fails.
3. having the distributed lock implementation method of heartbeat as described in claim 1, which is characterized in that apply for the method packet of lock It includes:
By etcd system, validity period ttl is set;
Starting timer time regularly updates lock validity period ttl value: realizing regularly task execution by starting timer mechanism; The task is each n seconds and a ttl for lock is gone to be set as original value, and n value is set as (ttl-1)/2;
Stop timer timer not continuing to that heartbeat update validity period occurs: closing timer and be allowed to no longer periodically go to execute fixation Task;Ttl is kept to 0, and lock is automatically retracted.
4. having the distributed lock implementation method of heartbeat as described in claim 1, which is characterized in that discharge the method packet of lock It includes: calling third party's distributed lock system, release lock;Timer no longer sends out equal ttl value after heartbeat and locks automatic release after being kept to 0.
5. a kind of distributed lock with heartbeat of the distributed lock implementation method with heartbeat as described in claim 1 controls system System.
6. a kind of distributed lock with heartbeat realizes program, operation and terminal, which is characterized in that the distribution with heartbeat Formula lock realizes that program realizes the distributed lock implementation method that heartbeat is had described in Claims 1 to 4 any one.
7. a kind of terminal, which is characterized in that the terminal carries and has heartbeat described in realization Claims 1 to 4 any one The controller of distributed lock implementation method.
8. a kind of computer readable storage medium, including instruction, when run on a computer, so that computer is executed as weighed Benefit requires the distributed lock implementation method that heartbeat is had described in 1-4 any one.
CN201910273229.5A 2019-04-04 2019-04-04 A kind of distributed lock realization method and system with heartbeat Pending CN110134522A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN201910273229.5A CN110134522A (en) 2019-04-04 2019-04-04 A kind of distributed lock realization method and system with heartbeat

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN201910273229.5A CN110134522A (en) 2019-04-04 2019-04-04 A kind of distributed lock realization method and system with heartbeat

Publications (1)

Publication Number Publication Date
CN110134522A true CN110134522A (en) 2019-08-16

Family

ID=67569473

Family Applications (1)

Application Number Title Priority Date Filing Date
CN201910273229.5A Pending CN110134522A (en) 2019-04-04 2019-04-04 A kind of distributed lock realization method and system with heartbeat

Country Status (1)

Country Link
CN (1) CN110134522A (en)

Cited By (1)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110535939A (en) * 2019-08-29 2019-12-03 深圳前海环融联易信息科技服务有限公司 A kind of service discovery and method for pre-emptively, device, computer equipment and storage medium

Citations (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103209095A (en) * 2013-03-13 2013-07-17 广东新支点技术服务有限公司 Method and device for preventing split brain on basis of disk service lock
US20150286586A1 (en) * 2014-04-07 2015-10-08 Oracle International Corporation System and Method for Implementing Scalable Adaptive Reader-Writer Locks
CN107145396A (en) * 2016-03-01 2017-09-08 阿里巴巴集团控股有限公司 Distributed lock implementation method and equipment
CN107783842A (en) * 2016-08-31 2018-03-09 阿里巴巴集团控股有限公司 A kind of distributed lock implementation method, equipment and storage device
US20180095666A1 (en) * 2016-10-03 2018-04-05 International Business Machines Corporation Fair High-Throughput Locking For Expedited Grace Periods
CN109101341A (en) * 2017-06-21 2018-12-28 阿里巴巴集团控股有限公司 The distribution method and equipment of distributed lock
CN109257396A (en) * 2017-07-12 2019-01-22 阿里巴巴集团控股有限公司 A kind of distributed lock dispatching method and device
CN109359081A (en) * 2015-12-14 2019-02-19 华为技术有限公司 The method of lock tube reason, lock server and client in a kind of cluster

Patent Citations (8)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN103209095A (en) * 2013-03-13 2013-07-17 广东新支点技术服务有限公司 Method and device for preventing split brain on basis of disk service lock
US20150286586A1 (en) * 2014-04-07 2015-10-08 Oracle International Corporation System and Method for Implementing Scalable Adaptive Reader-Writer Locks
CN109359081A (en) * 2015-12-14 2019-02-19 华为技术有限公司 The method of lock tube reason, lock server and client in a kind of cluster
CN107145396A (en) * 2016-03-01 2017-09-08 阿里巴巴集团控股有限公司 Distributed lock implementation method and equipment
CN107783842A (en) * 2016-08-31 2018-03-09 阿里巴巴集团控股有限公司 A kind of distributed lock implementation method, equipment and storage device
US20180095666A1 (en) * 2016-10-03 2018-04-05 International Business Machines Corporation Fair High-Throughput Locking For Expedited Grace Periods
CN109101341A (en) * 2017-06-21 2018-12-28 阿里巴巴集团控股有限公司 The distribution method and equipment of distributed lock
CN109257396A (en) * 2017-07-12 2019-01-22 阿里巴巴集团控股有限公司 A kind of distributed lock dispatching method and device

Cited By (2)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN110535939A (en) * 2019-08-29 2019-12-03 深圳前海环融联易信息科技服务有限公司 A kind of service discovery and method for pre-emptively, device, computer equipment and storage medium
CN110535939B (en) * 2019-08-29 2022-02-11 深圳前海环融联易信息科技服务有限公司 Service discovery and preemption method, device, computer equipment and storage medium

Similar Documents

Publication Publication Date Title
US10678533B2 (en) Enhanced server farm patching system for enabling developers to override off-peak patching schedules
US7089550B2 (en) Method and system for performing SW upgrade in a real-time system
US20200042308A1 (en) Enabling tenant administrators to initiate request driven peak-hour builds to override off-peak patching schedules
US20100205587A1 (en) Method, device and system for realizing kernel online patching
CN109471711B (en) Task processing method and device
CN110134522A (en) A kind of distributed lock realization method and system with heartbeat
US20060282831A1 (en) Method and hardware node for customized upgrade control
CN106708547B (en) Service plug-in management method and system
CN111666088A (en) Pod replacement method and device, electronic equipment and computer-readable storage medium
Ghafari et al. An architectural approach to ensure globally consistent dynamic reconfiguration of component-based systems
CN101963913B (en) Method for online evolution of component based on transactions
CN104111843A (en) Sandbox based script updating method and system
CN104125101B (en) The method that communication system network element uses the affiliated business single-deck of hot patch batch upgrade
CA3129985A1 (en) Abnormal operation environment restoration method and device, computer equipment and storage medium
CN105988885B (en) Operating system failure self-recovery method based on compensation rollback
US20100069053A1 (en) System and Method For Transactional Application Lifecycle Management For Mobile Devices
CA2679021C (en) System and method for transactional application lifecycle management for mobile devices
CN115208834A (en) Service flow limiting method based on database storage process design
CN107870777A (en) A kind of hot patch implementation method and device, terminal
CN111831422B (en) Task processing method and device
JP2015153128A (en) Call processing control device and software update method of the same, call processing system, and computer program
CN101826988A (en) Dynamic service upgrading method, equipment and system
CN112035115A (en) System API setting and calling method and system based on Android system platform calling
Rinsaka et al. An adaptive cost-based software rejuvenation scheme with nonparametric predictive inference approach
CN116028544B (en) Timing task dynamic adding method based on OPENSTACK

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
RJ01 Rejection of invention patent application after publication

Application publication date: 20190816

RJ01 Rejection of invention patent application after publication