Skip to main content

Distributed locking using Zookeeper

Project description

Lockpick

Lockpick is an utility for distributed locking using zookeeper. Its intended to be used in distributed environments where scripts need synchronization between runs.

Why Lockpick

Lockpick uses the kazoo library but provides following benefits over kazoo recipes :

  • Use non ephemeral zknode to allow explicit lock release
  • Exits with correct return codes
  • Correct signal Handling and cleanup
  • Retires and timeout

Usage

$ lockpick --help
usage: lockpick [-h] [-s SERVERS] [-c CHROOT] [-i IDENTIFIER] [-v]
                [-r RETRY_COUNT] [-p RETRY_SLEEP]
                {lock,rlock,wlock,unlock,list} lock_path

Distributed locking using Zookeeper primarily for scripting. After acquiring
the lock, the zk node is printed to STDOUT. This zk node should be passed to
unlock. Note: all logs are printed to STDERR

positional arguments:
  {lock,rlock,wlock,unlock,list}
                        lock: acquire a mutex lock. rlock: acquire a read lock
                        wlock: acquire a write lock. unlock: release lock
                        identified by zk node. list: list all lock contenders
  lock_path             ZK path to lock OR ZK node to unlock. For the
                        unlockaction this must be the chrooted zk path to the
                        node e.g.lockpick lock -c /devops /mylocklockpick
                        unlock -c /devops /mylock/9d2badeec7684f35b10f4860db42
                        e45c__rlock__0000000022

optional arguments:
  -h, --help            show this help message and exit
  -s SERVERS, --servers SERVERS
  -c CHROOT, --chroot CHROOT
                        ZK chroot for the lock path
  -i IDENTIFIER, --identifier IDENTIFIER
                        Optional string identifier to add to the lock node OR
                        verify when unlocking
  -v, --verbose
  -r RETRY_COUNT, --retry-count RETRY_COUNT
  -p RETRY_SLEEP, --retry-sleep RETRY_SLEEP

Install

We recommend using pip to install in a virtualenv :

$ virtualenv lockpick
$ pip install lockpick

Example

$ lockpick -s localhost:2181 -c /test -i "test lock1" rlock /mylock
/mylock/503e61fadc8f492497db823b87e64afe__rlock__0000000038

$ lockpick -s localhost:2181 -c /test -i "test lock1" rlock /mylock
/mylock/e21b0ecd480346aaaba23e77946e16e9__rlock__0000000039

$ lockpick -s localhost:2181 -c /test -i "test lock1" list /mylock
'test lock1' /mylock/e21b0ecd480346aaaba23e77946e16e9__rlock__0000000039
'test lock1' /mylock/503e61fadc8f492497db823b87e64afe__rlock__0000000038

$ lockpick -s localhost:2181 -c /test -i "test lock1" wlock /mylock
[2018-07-25 19:09:53,095] WARNING lockpick: Waiting to acquire lock, contenders are :
[2018-07-25 19:09:53,103] WARNING lockpick: 'test lock1' /mylock/e21b0ecd480346aaaba23e77946e16e9__rlock__0000000039
[2018-07-25 19:09:53,103] WARNING lockpick: 'test lock1' /mylock/503e61fadc8f492497db823b87e64afe__rlock__0000000038
[2018-07-25 19:09:53,104] WARNING lockpick: 'test lock1' /mylock/492b4a6c1727417daf511b343d0d11b7__lock__0000000042
[2018-07-25 19:09:56,104] WARNING lockpick: Waiting to acquire lock, contenders are :
[2018-07-25 19:09:56,114] WARNING lockpick: 'test lock1' /mylock/e21b0ecd480346aaaba23e77946e16e9__rlock__0000000039
[2018-07-25 19:09:56,114] WARNING lockpick: 'test lock1' /mylock/503e61fadc8f492497db823b87e64afe__rlock__0000000038
[2018-07-25 19:09:56,114] WARNING lockpick: 'test lock1' /mylock/492b4a6c1727417daf511b343d0d11b7__lock__0000000042
[2018-07-25 19:09:59,116] WARNING lockpick: LockTimeout exception rasied by locker thread
[2018-07-25 19:09:59,117] ERROR lockpick: Failed to acquire lock within timeout!

$ lockpick -s localhost:2181 -c /test -i "test lock1" unlock /mylock/503e61fadc8f492497db823b87e64afe__rlock__0000000038

$ lockpick -s localhost:2181 -c /test -i "test lock1" unlock /mylock/e21b0ecd480346aaaba23e77946e16e9__rlock__0000000039

$ lockpick -s localhost:2181 -c /test -i "test lock1" wlock /mylock
/mylock/d6602d4a54124826b27c50a43ab67a5e__lock__0000000043

syncd_spawn.sh

Spawns the given command only if it can acquire the given lock.

./syncd_spawn -n /test -C 'test' -l rlock sleep 5
Got lock on /test
Running your command sleep 5
Unlocked '/test/141bcfad04c6459591164ea12d1604af__rlock__0000000011'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lockpick-1.0.2.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

lockpick-1.0.2-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file lockpick-1.0.2.tar.gz.

File metadata

  • Download URL: lockpick-1.0.2.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.8

File hashes

Hashes for lockpick-1.0.2.tar.gz
Algorithm Hash digest
SHA256 e0a3476162b0d651505dc9592b74b303a208e9085a72101c6433cf7607ae2f20
MD5 7754ed8d62af7aab4259b4d5e4e1691f
BLAKE2b-256 aa0809f06eb00e2f4214a016e0e3b495ffba4018c5f2872337e58371c4499409

See more details on using hashes here.

File details

Details for the file lockpick-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: lockpick-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.0.1 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.60.0 CPython/3.8.8

File hashes

Hashes for lockpick-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 16faffb1cc735ae3bbac95aff4066d81c427aa53ea4cc57222c29058a62d25ae
MD5 25180c6c55dbd23aa04e01a457c4ba0a
BLAKE2b-256 0f08a79e8ce241edbf7d7b2faffb142d5c6431df6511d03ea47ebbbd65c8143f

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page