CN111708793A - Distributed application lock implementation method and device - Google Patents

Distributed application lock implementation method and device Download PDF

Info

Publication number
CN111708793A
CN111708793A CN202010560782.XA CN202010560782A CN111708793A CN 111708793 A CN111708793 A CN 111708793A CN 202010560782 A CN202010560782 A CN 202010560782A CN 111708793 A CN111708793 A CN 111708793A
Authority
CN
China
Prior art keywords
lock
distributed
processing
heartbeat
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
CN202010560782.XA
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.)
Liaoning Zhenxing Bank Co ltd
Original Assignee
Liaoning Zhenxing Bank 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 Liaoning Zhenxing Bank Co ltd filed Critical Liaoning Zhenxing Bank Co ltd
Priority to CN202010560782.XA priority Critical patent/CN111708793A/en
Publication of CN111708793A publication Critical patent/CN111708793A/en
Pending legal-status Critical Current

Links

Images

Classifications

    • GPHYSICS
    • G06COMPUTING; CALCULATING OR COUNTING
    • G06FELECTRIC DIGITAL DATA PROCESSING
    • G06F16/00Information retrieval; Database structures therefor; File system structures therefor
    • G06F16/20Information retrieval; Database structures therefor; File system structures therefor of structured data, e.g. relational data
    • G06F16/23Updating
    • G06F16/2308Concurrency control
    • G06F16/2336Pessimistic concurrency control approaches, e.g. locking or multiple versions without time stamps
    • G06F16/2343Locking methods, e.g. distributed locking or locking implementation details

Abstract

The invention discloses a method and a device for realizing a distributed application lock, which comprise the following steps: and S1 judging and processing the distributed lock task: s101, before executing a distributed task, inquiring whether a specified distributed lock exists, if not, preparing to insert the specified distributed lock into a database, and if so, preparing to judge whether the distributed lock heartbeat is normal; s102, judging whether the heartbeat of the existing distributed lock is normal or not, if the heartbeat is normal, indicating that the lock is occupied by other owners, directly quitting the processing, if the lock is refreshed overtime and the heartbeat is abnormal, updating the ID of the lock owner, the lock state and the lock maintenance time, refreshing the distributed lock information in the memory, and then starting the specific task processing. The distributed lock realized by the invention does not depend on middleware excessively, has the capability of rapid transplantation, and meanwhile, the realization mode does not occupy the relational database lock for a long time, does not trigger monitoring alarm, and does not have the risk of causing overhigh use of database resources.

Description

Distributed application lock implementation method and device
Technical Field
The invention relates to the technical field of distributed locks, in particular to a method and a device for realizing a distributed application lock.
Background
Distributed system: a system of computer nodes communicating over a network and coordinated to perform a common task. Distributed systems have emerged to perform computing and storage tasks that cannot be performed by a single computer using inexpensive, common machines. The purpose is to process more data with more machines.
Distributed lock: distributed locks are one way to control the synchronous access to shared resources between distributed systems. If one or a group of resources are shared between different systems or different hosts of the same system, it is often necessary to prevent mutual interference by some mutually exclusive means when accessing the resources to ensure consistency, in which case, a distributed lock is used.
Description of the prior art:
1. realizing distributed locks by using relational database row locks;
in the prior art, a distributed lock is realized by using a relational database row lock connected with a distributed system, specifically, a distributed lock table is defined in a relational database, a corresponding lock record is inserted, the distributed system preempts the database row lock through a query statement with a lock before processing a task, a thread which is successfully preempted performs task processing, and a thread which is unsuccessfully preempted performs exit processing or waits for release of the lock. This distributed lock implementation has the advantage of low implementation difficulty.
2. Relying on middleware to implement distributed locks;
in the prior art, a distributed lock is realized through a middleware used by a distributed system, for example, a current popular zookeeper realizes a distributed lock technology, and a zookeeper creates a temporary sequence node to realize the distributed lock, which is suitable for a program executed in sequence.
1. The disadvantages of using relational database row locks to implement distributed locks:
although the distributed lock implementation through the relational database row lock has the advantage of low implementation difficulty, the harm caused by the distributed lock implementation is very much. First, long term occupation of relational database row locks without release may result in over-utilization of database resources. Secondly, the databases used in the production environment are all deployed and monitored, the database row locks which are not released for a long time trigger monitoring alarm, and therefore the distributed lock mode is easy to disturb the monitoring order and cause unnecessary panic of related responsible persons.
2. The disadvantages of relying on middleware to implement distributed locks:
the distributed system has too high dependency on the middleware, and the implementation mode has no universality, for example, a distributed lock implemented by zookeeper cannot be quickly transplanted to other heterogeneous system groups. In addition, implementing distributed locks through middleware may result in the middleware being overloaded, affecting its use of normal functionality.
Disclosure of Invention
Based on the technical problems in the background art, the invention provides a method and a device for realizing a distributed application lock.
The invention provides a distributed application lock implementation method, which comprises the following steps:
and S1 judging and processing the distributed lock task:
s101, before executing a distributed task, inquiring whether a specified distributed lock exists, if not, preparing to insert the specified distributed lock into a database, and if so, preparing to judge whether the distributed lock heartbeat is normal;
s102, judging whether the heartbeat of the existing distributed lock is normal or not, if the heartbeat is normal, indicating that the lock is occupied by other owners, directly quitting the processing, and if the lock is refreshed overtime and the heartbeat is abnormal, updating the ID of the lock owner, the lock state and the lock maintenance time, refreshing the distributed lock information in the memory, and then starting the specific task processing;
s103, if the distributed lock does not exist, the distributed lock is tried to be inserted into the database, and if the insertion fails, the processing is directly quitted. If the insertion is successful, registering distributed lock information in the memory, and then starting specific task processing;
s104, before submitting the transaction, checking whether the current distributed lock is continuously held, if the lock is preempted or does not exist, rolling back the transaction, updating the task to processing failure, if the lock is continuously held, submitting the transaction, updating the task to processing success, and simultaneously updating the lock state in the memory to be unlocked;
s2 distributed lock status monitoring and refreshing:
s201, a distributed lock state background monitoring thread performs polling monitoring check on a distributed lock placement memory area, and locks with normal states and heartbeats needing to be refreshed and locks with abnormal states and ready to be released are placed in a distributed lock queue to be refreshed for subsequent refreshing processing;
s202, refreshing a background thread by the distributed lock state, polling to obtain a lock object in a distributed lock queue to be refreshed, checking the lock state, deleting a corresponding lock record in the database if the lock state is to be released, updating the lock maintenance time in the database if the lock state is normal, and refreshing the heartbeat time of the lock in the memory to maintain the continuous use of the lock.
An apparatus implemented by a distributed application lock, comprising: a memory for storing a computer program; and the processor is used for realizing the steps of the distributed application lock realization method when executing the computer program.
According to the method and the device for realizing the distributed application lock, the realized distributed lock does not depend on middleware excessively, the rapid transplanting capability is realized, meanwhile, the realization mode does not occupy the row lock of the relational database for a long time, the monitoring alarm is not triggered, and the risk of causing overhigh use of database resources does not exist.
Drawings
FIG. 1 is a schematic flow chart of a distributed application lock implementation method according to the present invention;
fig. 2 is a schematic diagram of monitoring and refreshing states of a distributed lock in the distributed application lock implementation method according to the present invention.
Detailed Description
The technical solutions in the embodiments of the present invention will be clearly and completely described below with reference to the drawings in the embodiments of the present invention, and it is obvious that the described embodiments are only a part of the embodiments of the present invention, and not all of the embodiments.
Referring to fig. 1-2, a distributed application lock implementation method includes the following steps:
and S1 judging and processing the distributed lock task:
s101, before executing a distributed task, inquiring whether a specified distributed lock exists, if not, preparing to insert the specified distributed lock into a database, and if so, preparing to judge whether the distributed lock heartbeat is normal;
s102, judging whether the heartbeat of the existing distributed lock is normal or not, if the heartbeat is normal, indicating that the lock is occupied by other owners, directly quitting the processing, and if the lock is refreshed overtime and the heartbeat is abnormal, updating the ID of the lock owner, the lock state and the lock maintenance time, refreshing the distributed lock information in the memory, and then starting the specific task processing;
s103, if the distributed lock does not exist, the distributed lock is tried to be inserted into the database, and if the insertion fails, the processing is directly quitted. If the insertion is successful, registering distributed lock information in the memory, and then starting specific task processing;
s104, before submitting the transaction, checking whether the current distributed lock is continuously held, if the lock is preempted or does not exist, rolling back the transaction, updating the task to processing failure, if the lock is continuously held, submitting the transaction, updating the task to processing success, and simultaneously updating the lock state in the memory to be unlocked;
s2 distributed lock status monitoring and refreshing:
s201, a distributed lock state background monitoring thread performs polling monitoring check on a distributed lock placement memory area, and locks with normal states and heartbeats needing to be refreshed and locks with abnormal states and ready to be released are placed in a distributed lock queue to be refreshed for subsequent refreshing processing;
s202, refreshing a background thread by the distributed lock state, polling to obtain a lock object in a distributed lock queue to be refreshed, checking the lock state, deleting a corresponding lock record in the database if the lock state is to be released, updating the lock maintenance time in the database if the lock state is normal, and refreshing the heartbeat time of the lock in the memory to maintain the continuous use of the lock.
An apparatus implemented by a distributed application lock, comprising: a memory for storing a computer program; and the processor is used for realizing the steps of the distributed application lock realization method when executing the computer program.
The invention comprises the following steps: and (3) judging and processing the distributed lock task: before the distributed task is executed, inquiring whether a specified distributed lock exists, if not, preparing to insert the specified distributed lock into a database, and if so, preparing to judge whether the heartbeat of the distributed lock is normal; judging whether the heartbeat of the existing distributed lock is normal or not, if the heartbeat is normal, indicating that the lock is occupied by other owners, directly quitting the processing, if the lock is refreshed overtime and the heartbeat is abnormal, updating the ID of the lock owner, the lock state and the lock maintenance time, refreshing the distributed lock information in the memory, and then starting the specific task processing; if the distributed lock does not exist, an attempt is made to insert the distributed lock into the database, and if the insertion fails, processing is directly exited. If the insertion is successful, registering distributed lock information in the memory, and then starting specific task processing; before submitting the transaction, checking whether the current distributed lock is continuously held, if the lock is preempted or does not exist, rolling back the transaction, updating the task to be processing failure, if the lock is continuously held, submitting the transaction, updating the task to be processing success, and simultaneously updating the lock state in the memory to be unlocked; distributed lock state monitoring and refreshing: polling, monitoring and checking a distributed lock placement memory area by a distributed lock state background monitoring thread, and placing locks with normal states and heartbeats needing to be refreshed and locks with abnormal states ready to be released into a distributed lock queue to be refreshed for subsequent refreshing; and refreshing the background thread by the distributed lock state, polling to obtain a lock object in a distributed lock queue to be refreshed, checking the lock state, deleting a corresponding lock record in the database if the lock state is to be released, updating the lock maintenance time in the database if the lock state is normal, and refreshing the heartbeat time of the lock in the memory to maintain the continuous use of the lock.
The above description is only for the preferred embodiment of the present invention, but the scope of the present invention is not limited thereto, and any person skilled in the art should be considered to be within the technical scope of the present invention, and the technical solutions and the inventive concepts thereof according to the present invention should be equivalent or changed within the scope of the present invention.

Claims (2)

1. A distributed application lock implementation method is characterized by comprising the following steps:
and S1 judging and processing the distributed lock task:
s101, before executing a distributed task, inquiring whether a specified distributed lock exists, if not, preparing to insert the specified distributed lock into a database, and if so, preparing to judge whether the distributed lock heartbeat is normal;
s102, judging whether the heartbeat of the existing distributed lock is normal or not, if the heartbeat is normal, indicating that the lock is occupied by other owners, directly quitting the processing, and if the lock is refreshed overtime and the heartbeat is abnormal, updating the ID of the lock owner, the lock state and the lock maintenance time, refreshing the distributed lock information in the memory, and then starting the specific task processing;
s103, if the distributed lock does not exist, attempting to insert the distributed lock into the database, if the insertion fails, directly quitting the processing, if the insertion succeeds, registering distributed lock information in a memory, and then starting specific task processing;
s104, before submitting the transaction, checking whether the current distributed lock is continuously held, if the lock is preempted or does not exist, rolling back the transaction, updating the task to processing failure, if the lock is continuously held, submitting the transaction, updating the task to processing success, and simultaneously updating the lock state in the memory to be unlocked;
s2 distributed lock status monitoring and refreshing:
s201, a distributed lock state background monitoring thread performs polling monitoring check on a distributed lock placement memory area, and locks with normal states and heartbeats needing to be refreshed and locks with abnormal states and ready to be released are placed in a distributed lock queue to be refreshed for subsequent refreshing processing;
s202, refreshing a background thread by the distributed lock state, polling to obtain a lock object in a distributed lock queue to be refreshed, checking the lock state, deleting a corresponding lock record in the database if the lock state is to be released, updating the lock maintenance time in the database if the lock state is normal, and refreshing the heartbeat time of the lock in the memory to maintain the continuous use of the lock.
2. An apparatus implemented by a distributed application lock, comprising: a memory for storing a computer program; and the processor is used for realizing the steps of the distributed application lock realization method when executing the computer program.
CN202010560782.XA 2020-06-18 2020-06-18 Distributed application lock implementation method and device Pending CN111708793A (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
CN202010560782.XA CN111708793A (en) 2020-06-18 2020-06-18 Distributed application lock implementation method and device

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
CN202010560782.XA CN111708793A (en) 2020-06-18 2020-06-18 Distributed application lock implementation method and device

Publications (1)

Publication Number Publication Date
CN111708793A true CN111708793A (en) 2020-09-25

Family

ID=72541679

Family Applications (1)

Application Number Title Priority Date Filing Date
CN202010560782.XA Pending CN111708793A (en) 2020-06-18 2020-06-18 Distributed application lock implementation method and device

Country Status (1)

Country Link
CN (1) CN111708793A (en)

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112650776A (en) * 2020-11-03 2021-04-13 重庆盈熙横纵信息技术有限公司 Redis-based distributed lock implementation method, storage device and server
CN113590641A (en) * 2021-08-06 2021-11-02 上海金仕达软件科技有限公司 Distributed processing method and device of automatic bank bond quotation system
WO2022156647A1 (en) * 2021-01-25 2022-07-28 华为技术有限公司 Database management method and apparatus

Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US9171019B1 (en) * 2013-02-19 2015-10-27 Amazon Technologies, Inc. Distributed lock service with external lock information database
CN107145396A (en) * 2016-03-01 2017-09-08 阿里巴巴集团控股有限公司 Distributed lock implementation method and equipment
CN107544850A (en) * 2016-06-29 2018-01-05 百度在线网络技术(北京)有限公司 Method and apparatus for controlling distributed lock
CN107783842A (en) * 2016-08-31 2018-03-09 阿里巴巴集团控股有限公司 A kind of distributed lock implementation method, equipment and storage device
CN110659329A (en) * 2019-09-23 2020-01-07 上海艾融软件股份有限公司 Data processing method and device and electronic equipment

Patent Citations (5)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
US9171019B1 (en) * 2013-02-19 2015-10-27 Amazon Technologies, Inc. Distributed lock service with external lock information database
CN107145396A (en) * 2016-03-01 2017-09-08 阿里巴巴集团控股有限公司 Distributed lock implementation method and equipment
CN107544850A (en) * 2016-06-29 2018-01-05 百度在线网络技术(北京)有限公司 Method and apparatus for controlling distributed lock
CN107783842A (en) * 2016-08-31 2018-03-09 阿里巴巴集团控股有限公司 A kind of distributed lock implementation method, equipment and storage device
CN110659329A (en) * 2019-09-23 2020-01-07 上海艾融软件股份有限公司 Data processing method and device and electronic equipment

Cited By (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
CN112650776A (en) * 2020-11-03 2021-04-13 重庆盈熙横纵信息技术有限公司 Redis-based distributed lock implementation method, storage device and server
WO2022156647A1 (en) * 2021-01-25 2022-07-28 华为技术有限公司 Database management method and apparatus
CN113590641A (en) * 2021-08-06 2021-11-02 上海金仕达软件科技有限公司 Distributed processing method and device of automatic bank bond quotation system

Similar Documents

Publication Publication Date Title
CN111708793A (en) Distributed application lock implementation method and device
EP3278239B1 (en) Processing database transactions in a distributed computing system
EP1696611B1 (en) Server data queuing system and method
US7337290B2 (en) Deadlock resolution through lock requeing
EP0567999B1 (en) Method and apparatus for executing a distributed transaction in a distributed database
US8327375B2 (en) System and method for supporting resource enlistment synchronization
EP0727742A2 (en) Method and apparatus for crash safe enforcement of mutually exclusive access to shared resources in a multitasking computer system
CN112241400A (en) Method for realizing distributed lock based on database
CN111459963A (en) Core accounting transaction concurrent processing method and device
US20070078852A1 (en) Synchronizing shared resources in a collection
CN116149829B (en) Task management method, device, equipment and storage medium
US7171410B1 (en) Fault tolerant network element
CN101751292A (en) Method for realizing consistency function of multimachine core data in ATC (automatic timing corrector) system
CN100483415C (en) Method and apparatus for data library locking and manipulation
US6976107B2 (en) Adaptive spin latches
EP4287021A1 (en) Request processing method and apparatus, computing device and storage medium
CN113238862B (en) Distributed task scheduling method and device
CN113934547B (en) Method, device, equipment and product for recording alarm log in cluster environment
CN112818059B (en) Information real-time synchronization method and device based on container release platform
CN117076146B (en) Data processing method, device, computer equipment and storage medium
CN115686822A (en) Global unique number generation method, device, equipment and storage medium
CN116881359A (en) Method, system, terminal equipment and storage medium for limiting impact of micro service node refreshing on database
CN116225726A (en) Deadlock information collection method, storage medium and device for database cluster
CN113934547A (en) Method, device, equipment and product for recording alarm log in cluster environment
CN114675980A (en) Method, system, device and storage medium for concurrency control in cluster deployment scene

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: 20200925

RJ01 Rejection of invention patent application after publication