preface

General work commonly used distributed lock, is based on Redis and ZooKeeper, the previous has introduced the Redisson lock related source code, let’s take a look at the ZooKeeper-based lock. This is the frame of the Exhibit.

Exhibit locks are also divided into many types, this article analyzes the shared reentrant locks.

Considering that if the article is long, it is not suitable for reading, so the article is properly broken down.

Environment configuration

Three nodes of the machine

Version: 3.7.0 System: macOS Installation mode: brew install ZooKeeper exhibit Maven dependent version: 5.1.0

<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>5.1.0</version>
</dependency>
Copy the code

Lock sample

Refer to the official documentation for details.

Before the lock

Before locking, ZooKeeper had only one node/ZooKeeper.

In the lock

Lock the /locks/lock_01 path.

After locking:

  1. I created a/locks/lock_01With a child node under it_c_cc4fc045-5a1e-4378-b3c7-8a8d3fb9a37c-lock-0000000000
  2. node/locks/lock_01/_c_cc4fc045-5a1e-4378-b3c7-8a8d3fb9a37c-lock-0000000000Yes temporary node
  3. node/locks/lock_01/_c_cc4fc045-5a1e-4378-b3c7-8a8d3fb9a37c-lock-0000000000Is the IP address of the machine

Lock the source code

PS: The code style in the following code screenshot is the code style of the exhibit source code.

The entrance

InterProcessMutex#internalLock

AttemptLock () ¶ Start by fetching the current thread from threadData, which must not be available here, so enter the attemptLock method.

This method also includes lock reentrant logic, which will be described later.

lock

LockInternals#attemptLock

The core is these two lines:

  1. CreatesTheLock Creates a temporary sequential node
  2. InternalLockLoop Checks whether the system is created successfully

Create temporary sequential nodes

StandardLockInternalsDriver#createsTheLock

You can see that the mode of the node is createmode.ephemeral_sequential, indicating that this is a temporary sequential node!

Enter the CreateBuilderImpl# forPath (Java. Lang. String, byte [])

Client.getdefaultdata () is the local IP address.

This adjustPath method is just looking in the name of adjusting the path or something like that. /locks/lock_01/ _c_uuID-lock -.

Because you are creating temporary sequential nodes, the sequence is automatically added later, ending in /locks/lock_01/ _c_uuID-lock-0000000000.

The node is created in the CreateBuilderImpl#pathInForeground.

  1. Creating a temporary node succeeds if the path exists. If the path does not exist, the creation fails.
  2. If the creation fails, create a path and then a node.

conclusion

This article mainly introduces the ZooKeeper based on the use of distributed lock frame exhibit, as well as locking process, source code analysis.

Here is a summary of the content:

The key points to focus on are:

  1. The distributed lock based on ZooKeeper is a temporary sequential node and the parent is a persistent node.
  2. When a temporary node is created, the parent node (path) is created first if the parent node does not exist.
  3. The lock structure is as follows: Yes/locks/lock_01Lock it. What’s actually locked isThe locks/lock_01 / _c_UUID - lock - serial numberFor examples,/locks/lock_01/_c_cc4fc045-5a1e-4378-b3c7-8a8d3fb9a37c-lock-0000000000

Related to recommend

  • Redisson distributed lock source 11: Semaphore and CountDownLatch
  • Redisson distributed lock source 10: read and write lock
  • Redisson distributed lock source 09: RedLock RedLock story