Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Principle Analysis of zookeeper distributed Lock Service

2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)05/31 Report--

Editor to share with you the principle of zookeeper distributed lock service analysis, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!

Distributed locking service may not be used much in everyone's project, because everyone puts exclusivity on the database level to block it. When a large number of row locks, table locks, and transactions flood the database. In general, many bottlenecks of web applications are on the database. Here we introduce a scheme to reduce the burden of database locks, using zookeeper distributed locking services.

Zookeeper is a sub-project under hadoop to coordinate some distributed frameworks related to hadoop, such as hadoop, hive, pig, etc., in fact, they are all animals, so they are called zookeeper-"zookeeper". Of course, there are many animals in the zoo. Visitors can go to different venues to see all kinds of animals according to the guide map provided by the zoo, instead of walking in the primeval jungle and being watched by the animals. In order to keep different animals where they are supposed to be, instead of visiting or killing each other, zookeepers need to classify and manage the animals according to their habits. in this way, we can watch the animals more safely. Back to our enterprise application system, with the continuous improvement of the level of information technology, our enterprise system has become more and more bloated, the performance has declined sharply, and customers complain frequently. At present, split system is the only effective way to solve the scalability and performance problems of the system. But the split system also brings the complexity of the system-the subsystems do not exist in isolation, they need to cooperate and interact with each other, which is what we often call distributed systems. Each subsystem is like an animal in a zoo. In order for each subsystem to provide unified services to users normally, it must be coordinated by a mechanism-that is, the ZooKeeper-- zookeeper.

ZooKeeper is essentially a distributed small file storage system. Originally a component of Apache Hadoop, it is now split into a separate subproject of Hadoop, and ZooKeeper clusters are also used in HBase (another split subproject of Hadoop for DBMS with large amounts of data in a distributed environment). ZooKeeper has the following features:

1) simple

The core of ZooKeeper is a compact file system that provides simple file manipulation as well as additional functions such as sorting and notification.

2) easy to express

The prototype of ZooKeeper's data structure is a znode tree (a file system similar to Linux), and they are blocks that have been built and can be used to build large collaborative data structures and protocols.

3) High availability

ZooKeeper can run on a set of servers, and they are designed to be highly available to avoid a single point of failure for your application.

4) loosely coupled interaction

The Watcher mechanism provided by ZooKeeper makes the interaction between each client and the server loosely coupled, and each client can exchange data with other clients without knowing the existence of other clients.

5) Rich API

ZooKeeper provides developers with a rich set of API, reducing the burden of writing common protocols.

Zookeeper actually means that each node in the cluster maintains the same tree. The structure of the tree is similar to the concept of the directory structure of linux. With / as the node, any node and leaf node can be expanded below, and each node can write data. The implementation of distributed lock based on zookeeper actually benefits from the strength of zookeeper synchronized files. We believe that when we visit the tree of zookeeper, the data returned by the same node is consistent. This depends on some algorithms within zookeeper to achieve. Especially the election algorithm of leader.

Official document: http://zookeeper.apache.org/doc/r3.3.2/zookeeperOver.html#ch_DesignOverview

Download: http://zookeeper.apache.org/releases.html

There are still some zookeeper clients of C # that are compatible with the latest version of zookeeper 3.4.3 https://github.com/ewhauser/zookeeper/branches

The data of each node in zookeeper cluster is consistent, so we can use these nodes as lock marks.

First set API to the lock, including at least three methods: lock (lock), unlock (unlock), isLocked (lock or not), and then we can create a factory (LockFactory) to specialize in producing locks. The process of creating a lock is described as follows:

Premise: each lock needs a path to specify (e.g. / geffzhang/lock)

1. According to the specified path, find out whether this node under the zookeeper cluster exists. (it means there is already a lock)

two。 If so, whether the current lock belongs to the querier based on some characteristic data of the querier (such as ip address / hostname)

3. If it is not the querier's lock, null is returned, indicating that the lock creation failed

4. If it is the querier's lock, return the lock to the querier

5. If this node does not exist, which means that there is no lock, then create a temporary node, write the querier's characteristic information into the node's data, and then return the lock.

According to the above 5 parts, a distributed lock can be created.

Locks are created in three states:

1. Failed to create (null), indicating that the lock was used by another querier.'

two。 Created successfully but not currently locked (unlocked), you can use the

3. Created successfully, but is currently locked (locked), can not continue to lock.

These are all the contents of the article "Analysis of the principles of zookeeper distributed Lock Service". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report