Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
forked frombaidu/dlock

An effective and reliable Distributed Lock

License

NotificationsYou must be signed in to change notification settings

baozhi/dlock

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

In Chinese 中文版

DLock is a Java implemented, effective and reliable Distributed Lock. It uses Redis to store lock object,and executes atomic operation on lock via [Lua](https://en.wikipedia.org/wiki/Lua_(programming_language)) script.Based on Redis expire mechanism, DLock implements lock [lease](https://en.wikipedia.org/wiki/Lease_(computer_science))to ensure release. In order to provide high performance, DLock adopts process level lock model and usesvariant CLH queue to manage lock competitors.

Requirements:Java8+、Redis2.6.12+(Require Redis Set -> NX PX command)

Architecture

DLock

Features

  • Atomic lock operation

    One Lock Operation will correspond to one Lua script. Since Redis can execute Lua script atomically,Lock Operation such as lock, release and expand lease will be atomic.

  • Reentrant ability

    The variableholdCountis maintained by local DLock object.holdCount will be increased by one whenlocker re-enter, and decreased by one when leave.

  • Lock lease

    On the basis of Redis expire mechanism, the lock lease is integrated to release lock after the lockercrashed. That is to say, infinite lock hold will never happen.

  • High performance lock model

    A lock-free variant CLH queue is used to maintain the competitor threads. And the retry thread will periodicallyawake the CLH queue's head thread to retry, so that only one thread per process can participate in lock competition,which will avoid unnecessary lock competition. Meanwhile, the unfair lock is also provided for throughput.

Quick Start

Here we have a demo with 3 steps to introduce how to integrate DLock into Spring based projects.

Step 1: Install Java8, Maven, Redis

If you have already installed Maven, JDK and Redis, just skip to next.
DownloadJava8,Maven andRedis2.6.12, then install. For maven,extracting and setting MAVEN_HOME is enough.Download, extract and compile Redis with:

wget http://download.redis.io/releases/redis-3.2.6.tar.gztar xzf redis-3.2.6.tar.gzcd redis-3.2.6make

The binaries that are now compiled are available in the src directory. Run Redis with:

src/redis-server

Now, Redis server is ready to accept connections on port 6379 by default.

Set JAVA_HOME & MAVEN_HOME

Here is a sample script to set JAVA_HOME and MAVEN_HOME

export MAVEN_HOME=/xxx/xxx/software/maven/apache-maven-3.3.9export PATH=$MAVEN_HOME/bin:$PATHJAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home";export JAVA_HOME;

Step 2: Reset Redis configuration

Reset property of 'redis.host' and 'redis.port' inredis.properties

Step 3: Run UnitTest

DLock implements the interface ofjava.util.concurrent.Lock, and so, you can use DLock like that.
DLockSimpleTest shows basic use of DLock;
DistributedReentrantLockTest show more complex use such like multi-competitors, reentrant lock.

DLock TPS

DLock will group lock competitors into several groups, only one competitor of a group is able to compete, andcompetitors within a group obtain the competition chance sequentially by default. This will avoid unnecessarycompetition. To verify that, we compare DLock with tradition Lock Model(tradition) using the concept 'Lock Trip',and a 'Lock Trip' consists oflock(),calculate(), andunlock().In experiment, we setcalculate() time and DLock's lease time to 10ms and 60ms separately, and vary concurrencyfrom 8 to 128 with step 8. Finally, the TPS ratio of DLock totradition is calculated, i.e:
R = TPSDLock / TPStradition

threads81624324048566472808896104112120128
R1.011.071.211.451.561.601.661.671.691.701.711.721.741.741.671.71

####AttentionThe Lock Model's TPS is related tocalculate(), so we just consider the performance trend.
DLock VS Tradition

However, R value has nothing to withcalculate() time, and so it is significant.
R

From above data, we findtradition has comparable performance to DLock in low concurrency environment.With the increase of concurrency,tradition's performance degrade rapidly. But DLock is still good, this isbecause DLock will group competitors into a few groups with only one active competitor per group, as a result, DLock'sperformance will not worsen with concurrency increase. Furthermore, DLock implements non-fair Lock, which can respondto high priority request.

About

An effective and reliable Distributed Lock

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java100.0%

[8]ページ先頭

©2009-2025 Movatter.jp