def conn(self):
     from zc.zk import ZooKeeper
     if connection:
         conn = connection[0]
     else:
         conn = ZooKeeper()
         connection.append(conn)
     return conn
コード例 #2
0
def main():
    logger.info("Enter")
    SLEEP_TIME = 300
    parser = OptionParser()
    parser.add_option("-z",
                      "--zkhosts",
                      dest="zk_hosts",
                      type='string',
                      action='callback',
                      callback=opt_callback,
                      help="zookeeper list of host:port comma separated ")
    parser.add_option("-p",
                      "--path",
                      dest="zk_path",
                      type='string',
                      help="zookeeper root path")
    parser.add_option("-r",
                      "--redishosts",
                      dest="rs_hosts",
                      type='string',
                      action='callback',
                      callback=opt_callback,
                      help="redis list of host:port comma separated ")
    parser.add_option("-s",
                      "--sleeptime",
                      dest="sleep_time",
                      type='int',
                      help="waiting time in seconds between thread execution")
    options = parser.parse_args()[0]
    if options.sleep_time:
        SLEEP_TIME = options.sleep_time
        logger.info("SLEEP_TIME: %d", SLEEP_TIME)
    logger.info("Options: %r", options)
    logger.info("Acquiring a exclusive lock on ZooKeeper...")
    try:
        zk = ZooKeeper(','.join(options.zk_hosts))
        with ZkLock(zk, ZOOKEEPER_LOCK):
            logger.info("Lock acquired! Connecting to Redis...")
            nm = RedisMonitor(zk, options.rs_hosts, SLEEP_TIME,
                              options.zk_path)
            nm.execute()
    except FailedConnect, err:
        logger.critical(err)
コード例 #3
0
 def __init__(self, zk_conn_str):
     try:
         self._zk = ZooKeeper(zk_conn_str)
     except FailedConnect as e:
         raise ZKConnectError(e)
def lock_cli():
    """Zktools Lock CLI"""
    from clint.textui import colored
    from clint.textui import columns
    from clint.textui import puts

    usage = "usage: %prog COMMAND"
    parser = OptionParser(usage=usage)
    parser.add_option("--host",
                      dest="host",
                      type="str",
                      default='localhost:2181',
                      help="Zookeeper host string")
    parser.add_option("--lock_root",
                      dest="lock_root",
                      type="str",
                      default="/ZktoolsLocks",
                      help="Lock root node")
    (options, args) = parser.parse_args()

    if len(args) < 1:
        puts(colored.red("Specify a command: list, remove, or show"))
        return
    command = args[0]
    if command not in ['list', 'remove', 'show']:
        puts(
            colored.red("Unrecognized command. Valid commands: list, remove, "
                        "show"))
        return

    conn = ZooKeeper(options.host)
    if command == 'list':
        children = conn.get_children(options.lock_root)

        col1, col2 = 30, 70
        puts(
            columns([colored.cyan("LOCK"), col1],
                    [colored.cyan("STATUS"), col2]))
        for child in children:
            try:
                locks = conn.get_children(options.lock_root + '/' + child)
            except zookeeper.NoNodeException:
                continue
            if locks:
                status = colored.red("Locked")
            else:
                status = colored.green("Free")
            puts(columns([child, col1], [status, col2]))
    elif command == 'remove':
        if len(args) < 2:
            puts(colored.red("You must specify a node to remove."))
            return
        conn.delete(options.lock_root + '/' + args[1])
    elif command == 'show':
        if len(args) < 2:
            puts(colored.red("You must specify a node to show."))
            return
        children = conn.get_children(options.lock_root + '/' + args[1])

        col1, col2, col3 = 20, 15, None
        puts(
            columns([colored.cyan("LOCK HOLDER"), col1],
                    [colored.cyan("DATA"), col2],
                    [colored.cyan("INFO"), col3]))
        for child in sorted(children):
            try:
                node_name = '%s/%s/%s' % (options.lock_root, args[1], child)
                value, info = conn.get(node_name)
            except zookeeper.NoNodeException:
                continue
            info['created_ago'] = int(time.time() - (info['ctime'] / 1000))
            info['modifed_ago'] = int(time.time() - (info['mtime'] / 1000))
            puts(columns([child, col1], [value, col2], [str(info), col3]))
コード例 #5
0
ファイル: test_zk.py プロジェクト: isabella232/brod
def print_zk_snapshot():
    # Dump all the ZooKeeper state at this point
    zk = ZooKeeper(ZK_CONNECT_STR)
    print zk.export_tree(ephemeral=True)