Implement class-based version of zkhander

This commit is contained in:
Joshua Boniface 2021-05-27 22:48:48 -04:00
parent 3128c8fa70
commit fef230ad98
1 changed files with 206 additions and 138 deletions

View File

@ -21,164 +21,232 @@
import time import time
import uuid import uuid
from kazoo.client import KazooClient
# Exists function class ZKHandler(object):
def exists(zk_conn, key): def __init__(self, hosts):
stat = zk_conn.exists(key) """
if stat: Initialize an instance of the ZKHandler class with config
return True
else:
return False
A zk_conn object will be created but not started
"""
self.encoding = 'utf8'
self.zk_conn = KazooClient(hosts=hosts)
# Child list function #
def listchildren(zk_conn, key): # State/connection management
children = zk_conn.get_children(key) #
return children def connect(self):
"""
Start the zk_conn object and connect to the cluster
"""
self.zk_conn.start()
def disconnect(self):
"""
Stop and close the zk_conn object and disconnect from the cluster
# Delete key function The class instance may be reused later (avoids persistent connections)
def deletekey(zk_conn, key, recursive=True): """
zk_conn.delete(key, recursive=recursive) self.zk_conn.stop()
self.zk_conn.close()
#
# Rename key recursive function # Key Actions
def rename_key_element(zk_conn, zk_transaction, source_key, destination_key): #
data_raw = zk_conn.get(source_key) def exists(self, key):
data = data_raw[0] """
zk_transaction.create(destination_key, data) Check if a key exists
"""
if zk_conn.get_children(source_key): stat = self.zk_conn.exists(key)
for child_key in zk_conn.get_children(source_key): if stat:
child_source_key = "{}/{}".format(source_key, child_key) return True
child_destination_key = "{}/{}".format(destination_key, child_key)
rename_key_element(zk_conn, zk_transaction, child_source_key, child_destination_key)
zk_transaction.delete(source_key)
# Rename key function
def renamekey(zk_conn, kv):
# Start up a transaction
zk_transaction = zk_conn.transaction()
# Proceed one KV pair at a time
for source_key in sorted(kv):
destination_key = kv[source_key]
# Check if the source key exists or fail out
if not zk_conn.exists(source_key):
raise
# Check if the destination key exists and fail out
if zk_conn.exists(destination_key):
raise
rename_key_element(zk_conn, zk_transaction, source_key, destination_key)
# Commit the transaction
try:
zk_transaction.commit()
return True
except Exception:
return False
# Data read function
def readdata(zk_conn, key):
data_raw = zk_conn.get(key)
data = data_raw[0].decode('utf8')
return data
# Data write function
def writedata(zk_conn, kv):
# Start up a transaction
zk_transaction = zk_conn.transaction()
# Proceed one KV pair at a time
for key in sorted(kv):
data = kv[key]
# Check if this key already exists or not
if not zk_conn.exists(key):
# We're creating a new key
zk_transaction.create(key, str(data).encode('utf8'))
else: else:
# We're updating a key with version validation return False
orig_data = zk_conn.get(key)
version = orig_data[1].version
# Set what we expect the new version to be def read(self, key):
new_version = version + 1 """
Read data from a key
"""
return self.zk_conn.get(key)[0].decode(self.encoding)
# Update the data def write(self, kvpairs):
zk_transaction.set_data(key, str(data).encode('utf8')) """
Create or update one or more keys' data
"""
if type(kvpairs) is not list:
print("ZKHandler error: Key-value sequence is not a list")
return False
# Set up the check transaction = self.zk_conn.transaction()
try:
zk_transaction.check(key, new_version) for kvpair in (kvpairs):
except TypeError: if type(kvpair) is not tuple:
print('Zookeeper key "{}" does not match expected version'.format(key)) print("ZKHandler error: Key-value pair '{}' is not a tuple".format(kvpair))
return False return False
# Commit the transaction key = kvpair[0]
try: value = kvpair[1]
zk_transaction.commit()
return True
except Exception:
return False
if not self.exists(key):
# Creating a new key
transaction.create(key, str(value).encode(self.encoding))
# Write lock function
def writelock(zk_conn, key):
count = 1
while True:
try:
lock_id = str(uuid.uuid1())
lock = zk_conn.WriteLock('{}'.format(key), lock_id)
break
except Exception:
count += 1
if count > 5:
break
else: else:
time.sleep(0.5) # Updating an existing key
continue data = self.zk_conn.get(key)
return lock version = data[1].version
# Validate the expected version after the execution
new_version = version + 1
# Update the data
transaction.set_data(key, str(value).encode(self.encoding))
# Check the data
try:
transaction.check(key, new_version)
except TypeError:
print("ZKHandler error: Key '{}' does not match expected version".format(key))
return False
# Read lock function
def readlock(zk_conn, key):
count = 1
while True:
try: try:
lock_id = str(uuid.uuid1()) transaction.commit()
lock = zk_conn.ReadLock('{}'.format(key), lock_id) return True
break except Exception as e:
except Exception: print("ZKHandler error: Failed to commit transaction: {}".format(e))
count += 1 return False
if count > 5:
break
else:
time.sleep(0.5)
continue
return lock
def delete(self, key, recursive=True):
"""
Delete a key (defaults to recursive)
"""
if self.zk_conn.delete(key, recursive=recursive):
return True
else:
return False
def children(self, key):
"""
Lists all children of a key
"""
return self.zk_conn.get_children(key)
def rename(self, kkpairs):
"""
Rename one or more keys to a new value
"""
if type(kkpairs) is not list:
print("ZKHandler error: Key-key sequence is not a list")
return False
transaction = self.zk_conn.transaction()
def rename_element(transaction, source_key, destnation_key):
data = self.zk_conn.get(source_key)[0]
transaction.create(destination_key, data)
if self.children(source_key):
for child_key in self.children(source_key):
child_source_key = "{}/{}".format(source_key, child_key)
child_destination_key = "{}/{}".format(destination_key, child_key)
rename_element(transaction, child_source_key, child_destination_key)
transaction.delete(source_key, recursive=True)
for kkpair in (kkpairs):
if type(kkpair) is not tuple:
print("ZKHandler error: Key-key pair '{}' is not a tuple".format(kkpair))
return False
source_key = kkpair[0]
destination_key = kkpair[1]
if not self.exists(source_key):
print("ZKHander error: Source key '{}' does not exist".format(source_key))
return False
if self.exists(destination_key):
print("ZKHander error: Destination key '{}' already exists".format(destination_key))
return False
rename_element(transaction, source_key, destination_key)
# Exclusive lock function
def exclusivelock(zk_conn, key):
count = 1
while True:
try: try:
lock_id = str(uuid.uuid1()) transaction.commit()
lock = zk_conn.Lock('{}'.format(key), lock_id) return True
break except Exception as e:
except Exception: print("ZKHandler error: Failed to commit transaction: {}".format(e))
count += 1 return False
if count > 5:
#
# Lock actions
#
def readlock(self, key):
"""
Acquires a read lock on a key
"""
count = 1
lock = None
while True:
try:
lock_id = str(uuid.uuid1())
lock = self.zk_conn.ReadLock(key, lock_id)
break break
else: except Exception as e:
time.sleep(0.5) if count > 5:
continue print("ZKHandler warning: Failed to acquire read lock after 5 tries: {}".format(e))
return lock break
else:
time.sleep(0.5)
count += 1
continue
return lock
def writelock(self, key):
"""
Acquires a write lock on a key
"""
count = 1
lock = None
while True:
try:
lock_id = str(uuid.uuid1())
lock = self.zk_conn.WriteLock(key, lock_id)
break
except Exception as e:
if count > 5:
print("ZKHandler warning: Failed to acquire write lock after 5 tries: {}".format(e))
break
else:
time.sleep(0.5)
count += 1
continue
return lock
def exclusivelock(self, key):
"""
Acquires an exclusive lock on a key
"""
count = 1
lock = None
while True:
try:
lock_id = str(uuid.uuid1())
lock = self.zk_conn.Lock(key, lock_id)
break
except Exception as e:
if count > 5:
print("ZKHandler warning: Failed to acquire exclusive lock after 5 tries: {}".format(e))
break
else:
time.sleep(0.5)
count += 1
continue
return lock