示例#1
0
def counter():
    zk = KazooClient(hosts=os.getenv("ZOOKEEPER_URL"))
    print("connected with zookeeper")
    zk.start()
    zk.add_listener(lambda zk_state: print(zk_state))
    counter_range = random.sample(counters_range.keys(), 1)[0]
    counter = zk.Counter(counter_range, default=counters_range[counter_range])
    counter += 1
    return counter.pre_value + 1
示例#2
0
class ZkUtil(object):
    def __init__(self):
        self.zk = KazooClient(hosts=CONFIG["ZK_HOST"])
        try:
            self.zk.start()
        except KazooTimeoutError as e:
            e.__traceback__

    @property
    def client(self):
        return self.zk

    def exists(self, path: str):
        if self.zk.client_state == KeeperState.CONNECTED:
            return self.zk.exists(path)
        else:
            return None

    def get_value(self, path: str):
        if self.zk.client_state == KeeperState.CONNECTED:
            return self.zk.get(path)[0].decode()
        else:
            return None

    def create(self, path: str, value):
        if isinstance(value, int):
            value = str(value)
        if self.zk.client_state == KeeperState.CONNECTED:
            if self.zk.exists(path):
                self.zk.delete(path, recursive=True)
            self.zk.create(path=path, value=value.encode(), makepath=True)

    def create_ephemeral(self, path: str, value):
        if isinstance(value, int):
            value = str(value)
        if self.zk.client_state == KeeperState.CONNECTED:
            if self.zk.exists(path):
                self.zk.delete(path, recursive=True)
            self.zk.create(path=path, value=value.encode(), ephemeral=True, makepath=True)

    def delete(self, path):
        self.zk.delete(path, recursive=True)

    def counter(self, path):
        if self.zk.client_state == KeeperState.CONNECTED:
            if self.zk.exists(path):
                self.zk.delete(path, recursive=True)
            return self.zk.Counter(path)
        else:
            return 0

    def stop(self):
        try:
            self.zk.close()
        except Exception as e:
            e.__traceback__
示例#3
0
 def __init__(self, zk: KazooClient, reservation_size: int):
     self.zk = zk
     self.reservation_size = reservation_size
     self.current_id = None
     self.last_id = None
     self.counter = zk.Counter("/tiny_id", default=1)
示例#4
0
# encoding: utf-8

from kazoo.client import KazooClient
import logging
import argparse

parser = argparse.ArgumentParser()
parser.add_argument('counter', help="Nombre del contador")
parser.add_argument('--value',
                    help="Valor (Valor positivo o negativo)",
                    default="1")
args = parser.parse_args()

if args.counter is None:
    parser.error("Es necesario especificar el nombre del contador!")
    sys.exit(1)

logging.basicConfig()
zk = KazooClient(hosts='127.0.0.1:2181')
zk.start()

counter = zk.Counter("/%s" % args.counter, default=0)
counter += int(args.value)

print("Contador %s\nValor %d" % (args.counter, counter.value))

zk.stop()