Ejemplo n.º 1
0
    if zk.exists(rootpath):
        if not options.quiet:
            print("Node %s already exists, attempting reuse" % (rootpath))
    else:
        zk.create(
            rootpath, "smoketest root, delete after test done, created %s" %
            (datetime.datetime.now().ctime()))

    # make sure previous cleaned up properly
    children = zk.get_children(rootpath)
    if len(children) > 0:
        raise SmokeError("there should not be children in %s"
                         "root data is: %s, count is %d" %
                         (rootpath, zk.get(rootpath), len(children)))

    zk.close()

    def child_path(i):
        return "%s/session_%d" % (rootpath, i)

    # create znodes, one znode per session (client), one session per server
    for i, server in enumerate(servers):
        # ensure this server is up to date with leader
        sessions[i]. async ()

        ## create child znode
        sessions[i].create(child_path(i), "", zookeeper.EPHEMERAL)

    watchers = []
    for i, server in enumerate(servers):
        # ensure this server is up to date with leader
Ejemplo n.º 2
0
    if zk.exists(rootpath):
        if not options.quiet:
            print("Node %s already exists, attempting reuse" % (rootpath))
    else:
        zk.create(rootpath,
                  "smoketest root, delete after test done, created %s" %
                  (datetime.datetime.now().ctime()))

    # make sure previous cleaned up properly
    children = zk.get_children(rootpath)
    if len(children) > 0:
        raise SmokeError("there should not be children in %s"
                         "root data is: %s, count is %d"
                         % (rootpath, zk.get(rootpath), len(children)))

    zk.close()

    def child_path(i):
        return "%s/session_%d" % (rootpath, i)

    # create znodes, one znode per session (client), one session per server
    for i, server in enumerate(servers):
        # ensure this server is up to date with leader
        sessions[i].async()

        ## create child znode
        sessions[i].create(child_path(i), "", zookeeper.EPHEMERAL)

    watchers = []
    for i, server in enumerate(servers):
        # ensure this server is up to date with leader
Ejemplo n.º 3
0
class ZookeeperStore(object):

    TIMEOUT = 1000

    def __init__(self,
                 verbose=True,
                 database='test',
                 table='targets',
                 zkhost='127.0.0.1:32181'):
        self.VERBOSE = verbose
        self.path = None
        self.ZK_HOST = zkhost
        self.database = "/" + database
        self.table = os.path.join(self.database, table)

        self.zk = ZKClient(self.ZK_HOST, timeout=self.TIMEOUT)
        self.say("login ok")
        self._init_zk()

    def __del__(self):
        self.zk.close()

    def _init_zk(self):
        nodes = (self.database, self.table)
        for node in nodes:
            if not self.zk.exists(node):
                try:
                    self.zk.create(node, "")
                except:
                    pass

    def create(self, path, msg, flags=0):
        msg = self.encode(msg)
        self.path = os.path.join(self.table, path)
        nodes = self.path.split("/")
        # FIXME
        for i in range(4, len(nodes)):
            node = "/".join(nodes[:i])
            if not self.zk.exists(node):
                try:
                    self.zk.create(node, "")
                except:
                    pass
        if not self.zk.exists(self.path):
            self.path = self.zk.create(self.path, msg, flags=flags)
        else:
            self.zk.set(self.path, msg)
        #self.path = os.path.basename(self.path)
        self.say("register ok! I'm %s" % self.path)

    def get_children(self, path=""):
        if not path:
            path = self.table
        children = self.zk.get_children(path)
        return children

    def get(self, node):
        msg = self.zk.get(node)
        return msg

    def decode(self, obj):
        return obj

    def encode(self, msg):
        return msg

    def say(self, msg):
        if self.VERBOSE:
            if self.path:
                log.info(msg)
            else:
                log.info(msg)
Ejemplo n.º 4
0
    timer2(func, "deleted %7d permanent znodes " % (options.znode_count))

def get_zk_servers():
    return options.cluster

if __name__ == '__main__':
    data = options.znode_size * "x"
    servers = get_zk_servers()

    session_gets = []
    for x in xrange(options.watch_session):
        session_gets.append(ZKClient(servers, options.timeout))
    session_set = ZKClient(servers, options.timeout)

    session_set.create(options.root_znode,
        "smoketest root, delete after test done, created %s" % (datetime.datetime.now().ctime()))

    create_nodes(session_set)
    for s in session_gets:
        add_wathches(s)
    set_nodes(session_set, data)
    delete_nodes(session_set) 
    session_set.delete(options.root_znode)

    time.sleep(10)
    for s in session_gets:
        s.close()
    session_set.close()
    print 'watch test complete'

Ejemplo n.º 5
0
class ZookeeperStore(object):
  
  ZK_HOST = "127.0.0.1:32181"
  TIMEOUT = 1000

  def __init__(self, verbose = True, database='/test', table='targets'):
    self.VERBOSE = verbose
    self.path = None
    self.database = database
    self.table = os.path.join(self.database, table)

    self.zk = ZKClient(self.ZK_HOST, timeout = self.TIMEOUT)
    self.say("login ok")
    self._init_zk()

  def __del__(self):
    self.zk.close()

  def _init_zk(self):
    nodes = (self.database, self.table)
    for node in nodes:
      if not self.zk.exists(node):
        try:
          self.zk.create(node, "")
        except:
          pass

  def create(self, path, msg, flags=0):
    msg = self.encode(msg)
    self.path = os.path.join(self.table, path)
    nodes = self.path.split("/")
    # FIXME
    for i in range(4,len(nodes)):
      node = "/".join(nodes[:i])
      if not self.zk.exists(node):
        try:
          self.zk.create(node, "")
        except:
          pass
    if not self.zk.exists(self.path):
      self.path = self.zk.create(self.path, msg, flags=flags)
    else:
      self.zk.set(self.path, msg)
    #self.path = os.path.basename(self.path)
    self.say("register ok! I'm %s" % self.path)
  
  def get_children(self, path=""):
    if not path:
      path = self.table
    children = self.zk.get_children(path)
    return children

  def get(self, node):
    msg = self.zk.get(node)
    return msg

  def decode(self, obj):
    return obj

  def encode(self, msg):
    return msg

  def say(self, msg):
    if self.VERBOSE:
      if self.path:
        log.info(msg)
      else:
        log.info(msg)
Ejemplo n.º 6
0
def get_zk_servers():
    return options.cluster


if __name__ == '__main__':
    data = options.znode_size * "x"
    servers = get_zk_servers()

    session_gets = []
    for x in xrange(options.watch_session):
        session_gets.append(ZKClient(servers, options.timeout))
    session_set = ZKClient(servers, options.timeout)

    session_set.create(
        options.root_znode,
        "smoketest root, delete after test done, created %s" %
        (datetime.datetime.now().ctime()))

    create_nodes(session_set)
    for s in session_gets:
        add_wathches(s)
    set_nodes(session_set, data)
    delete_nodes(session_set)
    session_set.delete(options.root_znode)

    time.sleep(10)
    for s in session_gets:
        s.close()
    session_set.close()
    print 'watch test complete'