Last active
November 22, 2020 09:27
-
-
Save ilyaevseev/feab9a42e91b55cd063ccadd08a1e0e8 to your computer and use it in GitHub Desktop.
Delete old locks from Zookeeper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import os | |
MAX_DEL = int(os.getenv('MAX_DEL','10')) | |
MAX_AGE = int(os.getenv('MAX_AGE', '7')) | |
DRY_RUN = int(os.getenv('DRY_RUN', '0')) | |
QUIET = int(os.getenv('QUIET', '0')) | |
import time | |
now = time.time() | |
import logging | |
logging.basicConfig() | |
from kazoo.client import KazooClient | |
zk = KazooClient(hosts='127.0.0.1:2181') | |
zk.start() | |
locks = zk.get_children("/locks") | |
numDeleted = 0 | |
for i, lkname in enumerate(locks): | |
if numDeleted >= MAX_DEL: break | |
lk = zk.get('/locks/' + lkname) | |
lkstat = lk[1] | |
nc = lkstat.numChildren | |
if (len(lk) != 2) or (nc != 0 and nc != 2) or (lkstat.ctime != lkstat.mtime): | |
print(i, "Invalid lock", lkname, lkstat) | |
continue | |
if now - (lkstat.ctime / 1000) <= MAX_AGE * 86400: | |
continue | |
if QUIET == 0: print(i, "Delete", numDeleted, lkname, lkstat) | |
if DRY_RUN == 0: zk.delete('/locks/' + lkname, recursive=True) | |
numDeleted += 1 | |
print("Total %d locks, deleted %s locks." % (len(locks), numDeleted)) | |
zk.stop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment