Overview lock
Distributed Lock
Distributed Lock can actually be understood as: control of a distributed system to share resources and orderly operation, to maintain consistency through mutually exclusive . For a very appropriate example: Suppose a shared resource is a house, there are all kinds of books, distributed systems is to come in person to read, distributed lock is to ensure that the house has only one door and only one person can enter , and the door only a key. Many people then go read a book, you can line up, the first person took the key and opened the door into the house and locked the door read, and then the second person is not the key, then waited out a ranking, then you take the key in, then so is
Why lock
- Multitasking environment needed only
- Tasks need to write to the same shared resources;
- Access to resources are mutually exclusive
In order to ensure a method can only be performed in the same thread at the same time high concurrency, in case of the conventional single stand-alone application deployment, Java can be used concurrently process related API (such as the synchronized or ReentrantLcok) for exclusive control. However, as the business development needs, the original single-machine deployment system is evolved into a distributed system, due to the distributed system multi-threaded, multi-process and distributed on different machines, which will enable concurrency control at the original stand-alone deployment locking strategy fails to solve this problem we need a mutual exclusion mechanism across JVM to control access to shared resources, which is distributed lock problem to be solved.
Distributed Lock plan comparison
Template Method Pattern
Arrangement main flow in the parent class, the step of effecting a delay to the sub-class to implement.
- Defined lock interface Lock
- GetLock implemented method AbstractLock template inside the lock, to achieve a common logic.
- You can not really step as a virtual method, thrown pot to subclass implementation.
- Subclasses need only focus his small step logic, implemented tryLock, waitLock, unLock method
Distributed Lock based Zookpper
Zookeeper data structure
- ZooKeeper data structure similar to Linux
- Each node has a value
zookeeper at the bottom really only provides two functions
- Management (storage, read) the data submitted by the user program;
- And provides data node monitoring service for user programs
zookeeper node type
- ** PERSISTENT- after persistent directory node ** zookeeper client is disconnected, the node is still present
- PERSISTENT_SEQUENTIAL- persistent directory node sequence number after zookeeper client disconnects, the node is still present, but Zookeeper numbered sequentially to the node name
- EPHEMERAL- temporary directory node after the client disconnects from the zookeeper, the node is deleted
- ** After EPHEMERAL_SEQUENTIAL- temporary sequentially numbered directory node ** zookeeper client is disconnected, the node is removed, but Zookeeper numbered sequentially to the node name
watcher mechanisms icon
Watcher (observer pattern)
Observer pattern elements:
- Target - shop owner (event source)
- Observer - Customer (event consumer)
- Event - the phone rang