Scenario 1:
Algorithm idea: The use of name uniqueness, lock operation, only requires all clients to create the/test/lock node together, only one creation succeeds, the successful person obtains the lock. When unlocking, simply remove the/test/lock node, and the remaining clients enter the competition to create the node again until all the clients get the lock.
Features: The correctness and reliability of this scheme is guaranteed by the zookeeper mechanism and is simple to realize. The disadvantage is that there is a "surprise swarm" effect, and if many clients are waiting for a lock, when the lock is released, all clients are awakened, and only one client gets the lock.
Scenario 2:
Algorithm idea: The temporary sequential node realizes the shared lock
The client calls the Create () method creating a node named "locknode/guid-lock-", and it is important to note that the creation type of the node here needs to be set to ephemeral_sequential.
The client invokes the GetChildren ("Locknode") method to obtain all the child nodes that have been created, while registering the Watcher of the child node change notification on this node.
After the client obtains the path to all child nodes, if it finds that the node you created in step 1 is the smallest ordinal in all nodes, the client is considered to have acquired a lock.
If you found in step 3 that you are not the smallest of all child nodes, stating that you have not acquired the lock, start waiting until the next second son node change notification, then the child node acquisition, determine whether to acquire the lock.
Features: Suitable for small cluster distribution. Cluster assembly time is serious.
Scenario 3:
Algorithm idea: An improved implementation of the shared lock implemented by the temporary sequential nodes
For the lock operation, you can have all clients go to the/lock directory to create a temporary order node, if the created client discovers that its own creation node sequence number is the smallest node in the/lock/directory, the lock is obtained. Otherwise, monitor the node that is smaller than the serial number of the node you created (the largest node that is smaller than the node you created) and enter the wait.
For an unlock operation, you only need to delete the node you created.
Features: Using temporary sequential nodes to implement distributed lock mechanism is actually a kind of implementation that queues in order of creation. This scheme is efficient and avoids the "surprise swarm" effect, where multiple clients wait for a lock and only one client is awakened when the lock is released.
Open Source Package Menagerie: A package for scenario 3, directly to use, it seems that after 11 has not been updated------"is not recommended. Latest Github:https://github.com/sfines/menagerie.
The play is coming, our curator package has been packaged and can be used directly. -------------"is recommended for use. Concrete Curator Realization principle + Test, follow-up re-analysis.
Advanced Java must be series one: Zookeeper distributed lock