CN111241042A - Distributed lock implementation method, system and equipment based on Etcd - Google Patents
Distributed lock implementation method, system and equipment based on Etcd Download PDFInfo
- Publication number
- CN111241042A CN111241042A CN201911416714.XA CN201911416714A CN111241042A CN 111241042 A CN111241042 A CN 111241042A CN 201911416714 A CN201911416714 A CN 201911416714A CN 111241042 A CN111241042 A CN 111241042A
- Authority
- CN
- China
- Prior art keywords
- lock
- etcd
- resource
- management
- resources
- 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
Links
- 238000000034 method Methods 0.000 title claims abstract description 38
- 230000008569 process Effects 0.000 claims description 15
- 238000004590 computer program Methods 0.000 claims description 6
- 230000002860 competitive effect Effects 0.000 claims description 3
- 238000000926 separation method Methods 0.000 claims 1
- 238000007726 management method Methods 0.000 abstract description 42
- 238000012545 processing Methods 0.000 abstract description 10
- 238000013500 data storage Methods 0.000 abstract description 2
- 238000013461 design Methods 0.000 abstract description 2
- 230000008878 coupling Effects 0.000 description 3
- 238000010168 coupling process Methods 0.000 description 3
- 238000005859 coupling reaction Methods 0.000 description 3
- 230000007246 mechanism Effects 0.000 description 3
- 230000009286 beneficial effect Effects 0.000 description 2
- 238000004891 communication Methods 0.000 description 2
- 230000002950 deficient Effects 0.000 description 1
- 238000010586 diagram Methods 0.000 description 1
- 238000005516 engineering process Methods 0.000 description 1
- 230000006870 function Effects 0.000 description 1
- 238000012986 modification Methods 0.000 description 1
- 230000004048 modification Effects 0.000 description 1
- 230000003287 optical effect Effects 0.000 description 1
- 230000001360 synchronised effect Effects 0.000 description 1
- 238000012795 verification Methods 0.000 description 1
Images
Classifications
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F16/00—Information retrieval; Database structures therefor; File system structures therefor
- G06F16/10—File systems; File servers
- G06F16/17—Details of further file system functions
- G06F16/176—Support for shared access to files; File sharing support
- G06F16/1767—Concurrency control, e.g. optimistic or pessimistic approaches
- G06F16/1774—Locking methods, e.g. locking methods for file systems allowing shared and concurrent access to files
-
- G—PHYSICS
- G06—COMPUTING; CALCULATING OR COUNTING
- G06F—ELECTRIC DIGITAL DATA PROCESSING
- G06F16/00—Information retrieval; Database structures therefor; File system structures therefor
- G06F16/10—File systems; File servers
- G06F16/18—File system types
- G06F16/182—Distributed file systems
Landscapes
- Engineering & Computer Science (AREA)
- Theoretical Computer Science (AREA)
- Data Mining & Analysis (AREA)
- Databases & Information Systems (AREA)
- Physics & Mathematics (AREA)
- General Engineering & Computer Science (AREA)
- General Physics & Mathematics (AREA)
- Information Retrieval, Db Structures And Fs Structures Therefor (AREA)
- Stored Programmes (AREA)
Abstract
According to the distributed lock implementation method, system and equipment based on the Etcd, provided by the invention, the corresponding lock resources are replaced by using the character strings according to DLM lock semantics and lock compatibility design. By designing a complete lock scheme of lock management initialization, lock resource competition processing, lock contention-free locking processing and lock processing of an existing lock in the Etcd, rich lock semantics are provided for the Etcd lock scheme, and the Etcd management data storage is more convenient; meanwhile, a complete DLM semantic locking interface can be opened in a user mode, the DLM does not need to be called in a kernel mode for realization, the scheme is simpler and more effective, and the complexity is low. The invention can utilize various scenes such as cluster management, storage resource management, distributed lock management and the like, and has great benefits in the cloud computing and storage direction.
Description
Technical Field
The invention relates to the technical field of computers, in particular to a distributed lock implementation method, a system and equipment based on Etcd.
Background
Etcd (a distributed high-availability key value database) is an open-source project initiated by the CoreOS team in 2013 in 6 months, and mainly aims to realize a high-availability distributed key value (key-value) database. The Etcd core is based on a raft protocol as a consistency algorithm and has wide application in the scenes of container scheduling, cluster management, service discovery and the like.
DLM (distributed lock manager) is a VMS operating system offering, first appearing in VMS version 3 in 1982, and formally integrating DLM almost identical to VMS DLM interface in linux2.6.12 kernel in 2006. DLM locks typically need to be collocated based on a cluster management service such as Corosync. DLM may provide a uniform, shared lock resource management mechanism for the entire cluster. And running a lock management process at each DLM lock running server node, communicating the processes mutually, and maintaining lock resource management in a cluster range. DLM locks have rich lock working modes, and 6 lock semantics are defined as NL, CR, CW, PR, PW and EX respectively, so as to manage shared resources.
Etcd is currently deficient as an emerging key value database management open source component in a lock management mechanism, only a lock lease can be provided, resource lock management is achieved by updating the lock lease regularly, and the lock mode only supports Exclusive (EX) locks. DLM realized in Linux version depends on cluster system synchronous relative lock resource information, and is realized in kernel with very complex logic control. How to enrich the lock management mechanism based on the mature cluster management and key value database function of the Etcd and based on DLM lock semantic design, the related technology is utilized to meet different resource management requirements, and the problem to be solved is urgent.
Disclosure of Invention
In view of the above problems, an object of the present invention is to provide a distributed lock implementation method and system based on a distributed high-availability key value database, which can utilize various scenarios such as cluster management, storage resource management, distributed lock management, and the like, and have great benefits in cloud computing and storage directions.
In order to achieve the purpose, the invention is realized by the following technical scheme: a distributed lock implementation method based on Etcd comprises the following steps:
managing and initializing lock resources;
when a plurality of hosts or a plurality of processes of a single host need to lock keys stored by the Etcd, performing competitive locking management on the resources;
when no lock resource exists under the preset lock resource catalog, performing lock-free resource competition management;
the existing lock resources are managed.
Further, the management initialization of the lock resource includes the following steps:
s11: after the Etcd is established, a key with lock as a name and a corresponding similar directory are established, a sub-key is established at the next level of the lock, and a new directory and corresponding lock resources are established under the similar directory;
s12: when Etcd creates a certain key, it defaults to create a key at the next level of lock.
Further, when a plurality of hosts or a plurality of processes of a single host need to lock keys already stored by the Etcd, the contention locking management of the resources comprises the following steps:
s21: using the host name and the process ID number as the resource competitor identification;
s22: when a resource competitor locks the same key, firstly, a request is sent to a master node of the Etcd, the master node uses a Hash algorithm for all the competitors, and the minimum value after Hash is taken to obtain a lock resource;
s23: writing host _ name, pid and lock information into the value of the corresponding key under the lock directory through the Etcd master node;
s24: and the resource competitor checks the value content of the corresponding key under the lock directory by using the match and write interface provided by the Etcd, if the value content is consistent with the resource needing to be locked, the locking is successful, and if the value content is inconsistent with the resource needing to be locked, the locking is failed.
Further, when there is no lock resource in the preset lock resource directory, performing lock-free resource contention management includes:
when no lock resource exists under the same-name key in the lock directory and no lock resource competition exists, the master directly updates the value corresponding to the corresponding key; the resource competitor checks whether the locking is successful by using the match and write interface provided by the Etcd.
Further, the managing the existing lock resources comprises the following steps:
s41: when finding that corresponding lock resources exist under keys with the same name in the lock directory, reading a lock field in a lock character string of host _ name: pid;
s42: checking whether conflict exists according to the lock compatibility of the DLM, and if conflict exists, locking fails; if the conflict does not exist, adding the resource information of which the record needs to be locked in the value.
Further, when the value is
If more than 1 lock semantics exist in the value, if the lock semantics are added, the lock semantics are separated by commas.
Correspondingly, the invention also discloses a distributed lock implementation system based on the Etcd, which comprises the following steps:
the initialization unit is used for managing and initializing the lock resources;
the first management unit is used for carrying out competition locking management on the resources when a plurality of hosts or a plurality of processes of a single host need to lock keys already stored by the Etcd;
the second management unit is used for carrying out non-lock resource competition management when no lock resource exists under the preset lock resource catalog;
and the third management unit is used for managing the existing lock resources.
Correspondingly, the invention also discloses a distributed lock implementation device based on the Etcd, which comprises the following steps:
a memory for storing a computer program;
a processor configured to implement the steps of the Etcd-based distributed lock implementation method of any of the above when the computer program is executed.
Compared with the prior art, the invention has the beneficial effects that: the invention provides a distributed lock implementation method, a system and equipment based on Etcd, which are designed according to DLM lock semantics and lock compatibility and use character strings to replace corresponding lock resources. By designing a complete lock scheme of lock management initialization, lock resource competition processing, lock contention-free locking processing and lock processing of an existing lock in the Etcd, rich lock semantics are provided for the Etcd lock scheme, and the Etcd management data storage is more convenient; meanwhile, a complete DLM semantic locking interface can be opened in a user mode, the DLM does not need to be called in a kernel mode for realization, the scheme is simpler and more effective, and the complexity is low. The invention can utilize various scenes such as cluster management, storage resource management, distributed lock management and the like, and has great benefits in the cloud computing and storage direction.
Therefore, compared with the prior art, the invention has prominent substantive features and remarkable progress, and the beneficial effects of the implementation are also obvious.
Drawings
In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings used in the description of the embodiments or the prior art will be briefly described below, it is obvious that the drawings in the following description are only embodiments of the present invention, and for those skilled in the art, other drawings can be obtained according to the provided drawings without creative efforts.
FIG. 1 is a flow chart of the method of the present invention.
Fig. 2 is a system block diagram of the present invention.
Detailed Description
The following description of the embodiments of the present invention will be made with reference to the accompanying drawings.
The method for implementing the distributed lock based on the Etcd shown in fig. 1 includes:
1. and (3) management initialization is carried out on the lock resources:
1.1, after the Etcd is established, establishing 'lock' as a name key, similar to a directory, and establishing a sub-key at the next level of the lock and a new directory under the similar directory; the key name of the resource needing to be locked, and the value stores the relevant lock resource;
1.2, when Etcd creates a certain key, default to create a key at the next level of lock.
2. When a plurality of hosts or a plurality of processes of a single host need to lock keys already stored by the Etcd, performing competitive locking management on the resources:
2.1, using the host name and the process ID number as resource competitors, namely using character strings of 'host _ name: pid: lock' identification;
2.2, when a resource competitor locks the same key, firstly, a request is sent to a master node of the etcd, the master node uses Hash (host _ name: pid: lock) for all the competitors, and the minimum value after the Hash is taken to obtain a lock resource;
2.3, writing host _ name, pid and lock information into the value of the corresponding key under the lock directory by the Etcd master node;
and 2.4, the resource competitor checks the value content of the corresponding key under the lock directory by using the match and write interface provided by the etcd, if the value content is consistent with the required locking, the locking is successful, and if the value content is inconsistent with the required locking, the locking is failed.
3. When no lock resource exists under the preset lock resource catalog, performing lock-free resource competition management:
when the fact that no lock resource exists under the key with the same name in the lock directory is found, and no lock resource competition exists, the master directly updates the value corresponding to the corresponding key, the value is also 'host _ name: pid: lock', and a resource competitor verifies whether the locking is successful by using a match and write interface provided by etcd.
4. Managing already existing lock resources:
4.1, reading a lock field in a character string of 'host _ name: pid: lock' in the value when finding that corresponding lock resources exist under a key with the same name in the lock directory;
4.2, checking whether conflict exists according to the lock compatibility of the DLM, and if conflict exists, locking fails; if not, adding resource information 'host _ name: pid: lock' for recording the locking requirement in the value;
4.3, note that in the value, there may be multiple lock semantics, each increment, separated by a "comma", and the added lock resource needs to be considered when reading the added lock resource; lock conflict verification also needs to ensure that the lock conflict with all added lock fields is avoided;
in the above-described embodiment of the present invention,
the host _ name is a host name, namely a parameter item when the Etcd service is started, and is used for identifying which host is;
pid is a process number, namely which process in the host needs to be locked to read and write the data of the Etcd;
lock, for DLM lock semantics, stores the "000100" character string, namely the above-mentioned lock message, to judge lock conflicts;
compare and write provide the standard interface for etcd.
Correspondingly, as shown in fig. 2, the present invention also discloses a distributed lock implementation system based on Etcd, which includes:
the initialization unit is used for managing and initializing the lock resources;
the first management unit is used for carrying out competition locking management on the resources when a plurality of hosts or a plurality of processes of a single host need to lock keys already stored by the Etcd;
the second management unit is used for carrying out non-lock resource competition management when no lock resource exists under the preset lock resource catalog;
and the third management unit is used for managing the existing lock resources.
Correspondingly, the invention also discloses a distributed lock implementation device based on the Etcd, which comprises the following steps:
a memory for storing a computer program;
a processor configured to implement the steps of the Etcd-based distributed lock implementation method of any of the above when the computer program is executed.
Those skilled in the art will readily appreciate that the techniques of the embodiments of the present invention may be implemented as software plus a required general purpose hardware platform. Based on such understanding, the technical solutions in the embodiments of the present invention may be embodied in the form of a software product, where the computer software product is stored in a storage medium, such as a usb disk, a removable hard disk, a Read-Only Memory (ROM), a Random Access Memory (RAM), a magnetic disk or an optical disk, and the like, and the storage medium can store program codes, and includes instructions for enabling a computer terminal (which may be a personal computer, a server, or a second terminal, a network terminal, and the like) to perform all or part of the steps of the method in the embodiments of the present invention. The same and similar parts in the various embodiments in this specification may be referred to each other. Especially, for the terminal embodiment, since it is basically similar to the method embodiment, the description is relatively simple, and the relevant points can be referred to the description in the method embodiment.
In the embodiments provided by the present invention, it should be understood that the disclosed system, system and method can be implemented in other ways. For example, the above-described system embodiments are merely illustrative, and for example, the division of the units is only one logical functional division, and other divisions may be realized in practice, for example, a plurality of units or components may be combined or integrated into another system, or some features may be omitted, or not executed. In addition, the shown or discussed mutual coupling or direct coupling or communication connection may be an indirect coupling or communication connection through some interfaces, systems or units, and may be in an electrical, mechanical or other form.
The units described as separate parts may or may not be physically separate, and parts displayed as units may or may not be physical units, may be located in one place, or may be distributed on a plurality of network units. Some or all of the units can be selected according to actual needs to achieve the purpose of the solution of the embodiment.
In addition, functional modules in the embodiments of the present invention may be integrated into one processing unit, or each module may exist alone physically, or two or more modules are integrated into one unit.
Similarly, each processing unit in the embodiments of the present invention may be integrated into one functional module, or each processing unit may exist physically, or two or more processing units are integrated into one functional module.
The invention is further described with reference to the accompanying drawings and specific embodiments. It should be understood that these examples are for illustrative purposes only and are not intended to limit the scope of the present invention. Further, it should be understood that various changes or modifications of the present invention may be made by those skilled in the art after reading the teaching of the present invention, and these equivalents also fall within the scope of the present application.
Claims (8)
1. A distributed lock implementation method based on Etcd is characterized by comprising the following steps:
managing and initializing lock resources;
when a plurality of hosts or a plurality of processes of a single host need to lock keys stored by the Etcd, performing competitive locking management on the resources;
when no lock resource exists under the preset lock resource catalog, performing lock-free resource competition management;
the existing lock resources are managed.
2. The method of claim 1, wherein the initializing management of lock resources comprises:
s11: after the Etcd is established, a key with lock as a name and a corresponding similar directory are established, a sub-key is established at the next level of the lock, and a new directory and corresponding lock resources are established under the similar directory;
s12: when Etcd creates a certain key, it defaults to create a key at the next level of lock.
3. The method for implementing the distributed lock based on the Etcd as described in claim 1, wherein when a plurality of hosts or a single host and a plurality of processes need to lock keys already stored by the Etcd, the contention locking management for the resources comprises the following steps:
s21: using the host name and the process ID number as the resource competitor identification;
s22: when a resource competitor locks the same key, firstly, a request is sent to a master node of the Etcd, the master node uses a Hash algorithm for all the competitors, and the minimum value after Hash is taken to obtain a lock resource;
s23: writing host _ name, pid and lock information into the value of the corresponding key under the lock directory through the Etcd master node;
s24: and the resource competitor checks the value content of the corresponding key under the lock directory by using the match and write interface provided by the Etcd, if the value content is consistent with the resource needing to be locked, the locking is successful, and if the value content is inconsistent with the resource needing to be locked, the locking is failed.
4. The method for implementing the distributed lock based on the Etcd as claimed in claim 2, wherein the performing the lock-free resource contention management when the lock resource does not exist in the preset lock resource directory comprises:
when no lock resource exists under the same-name key in the lock directory and no lock resource competition exists, the master directly updates the value corresponding to the corresponding key; the resource competitor checks whether the locking is successful by using the match and write interface provided by the Etcd.
5. The method of claim 1, wherein the managing existing lock resources comprises:
s41: when finding that corresponding lock resources exist under keys with the same name in the lock directory, reading a lock field in a lock character string of host _ name: pid;
s42: checking whether conflict exists according to the lock compatibility of the DLM, and if conflict exists, locking fails; if the conflict does not exist, adding the resource information of which the record needs to be locked in the value.
6. The method of claim 1, wherein when there are more than 1 lock semantics in the value, if lock semantics addition is performed, then comma separation is performed.
7. An Etcd-based distributed lock implementation system, comprising:
the initialization unit is used for managing and initializing the lock resources;
the first management unit is used for carrying out competition locking management on the resources when a plurality of hosts or a plurality of processes of a single host need to lock keys already stored by the Etcd;
the second management unit is used for carrying out non-lock resource competition management when no lock resource exists under the preset lock resource catalog;
and the third management unit is used for managing the existing lock resources.
8. An Etcd-based distributed lock implementation device, comprising:
a memory for storing a computer program;
a processor for implementing the steps of the Etcd based distributed lock implementation method of any of claims 1 to 6 when executing the computer program.
Priority Applications (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201911416714.XA CN111241042B (en) | 2019-12-31 | 2019-12-31 | Distributed lock implementation method, system and equipment based on Etcd |
Applications Claiming Priority (1)
Application Number | Priority Date | Filing Date | Title |
---|---|---|---|
CN201911416714.XA CN111241042B (en) | 2019-12-31 | 2019-12-31 | Distributed lock implementation method, system and equipment based on Etcd |
Publications (2)
Publication Number | Publication Date |
---|---|
CN111241042A true CN111241042A (en) | 2020-06-05 |
CN111241042B CN111241042B (en) | 2022-11-11 |
Family
ID=70864719
Family Applications (1)
Application Number | Title | Priority Date | Filing Date |
---|---|---|---|
CN201911416714.XA Active CN111241042B (en) | 2019-12-31 | 2019-12-31 | Distributed lock implementation method, system and equipment based on Etcd |
Country Status (1)
Country | Link |
---|---|
CN (1) | CN111241042B (en) |
Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20100023521A1 (en) * | 2008-07-28 | 2010-01-28 | International Business Machines Corporation | System and method for managing locks across distributed computing nodes |
CN109862097A (en) * | 2019-01-31 | 2019-06-07 | 上海易点时空网络有限公司 | ID generation method and device |
CN110392876A (en) * | 2017-03-10 | 2019-10-29 | 净睿存储股份有限公司 | Data set and other managed objects are synchronously copied into storage system based on cloud |
-
2019
- 2019-12-31 CN CN201911416714.XA patent/CN111241042B/en active Active
Patent Citations (3)
Publication number | Priority date | Publication date | Assignee | Title |
---|---|---|---|---|
US20100023521A1 (en) * | 2008-07-28 | 2010-01-28 | International Business Machines Corporation | System and method for managing locks across distributed computing nodes |
CN110392876A (en) * | 2017-03-10 | 2019-10-29 | 净睿存储股份有限公司 | Data set and other managed objects are synchronously copied into storage system based on cloud |
CN109862097A (en) * | 2019-01-31 | 2019-06-07 | 上海易点时空网络有限公司 | ID generation method and device |
Also Published As
Publication number | Publication date |
---|---|
CN111241042B (en) | 2022-11-11 |
Similar Documents
Publication | Publication Date | Title |
---|---|---|
US11847438B2 (en) | Offline capabilities for live applications in a cloud collaboration platform | |
US10713654B2 (en) | Enterprise blockchains and transactional systems | |
US11232098B2 (en) | Data structure reading methods and apparatuses, data structure update methods and apparatuses, and electronic devices | |
CN107480237B (en) | Data fusion method and system for heterogeneous desktop cloud platform | |
CN101650660B (en) | Booting a computer system from central storage | |
US8615488B2 (en) | Physical replication of database subset to improve availability and reduce resource cost in a cloud environment | |
TWI724570B (en) | Method, device, electronic equipment and storage medium for reading and updating data structure | |
EP2572290A2 (en) | Sharing and synchronization of objects | |
CN102243648A (en) | Concurrently accessing data | |
CN111399753B (en) | Method and device for writing pictures | |
CN115578096A (en) | Block chain parallel transaction method, device, equipment and storage medium | |
US11157456B2 (en) | Replication of data in a distributed file system using an arbiter | |
US20100023577A1 (en) | Method, system and article for mobile metadata software agent in a data-centric computing environment | |
US10127270B1 (en) | Transaction processing using a key-value store | |
US20140181809A1 (en) | Creating multiple rules for a device to allow concurrent access to the device by different virtual machines | |
CN110908644B (en) | Configuration method and device of state node, computer equipment and storage medium | |
CN112785248A (en) | Human resource data cross-organization interaction method, device, equipment and storage medium | |
CN111241042B (en) | Distributed lock implementation method, system and equipment based on Etcd | |
Vohra | Pro Docker | |
WO2024021417A1 (en) | Data account creation method and apparatus | |
US8560572B2 (en) | System for lightweight objects | |
US10776344B2 (en) | Index management in a multi-process environment | |
US20050198005A1 (en) | Systems and methods for controlling access to an object | |
CN110019113B (en) | Database service processing method and database server | |
US20190377708A1 (en) | System and method for providing a container environment in a single file |
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 |