Esempio n. 1
0
class GJZookeeper(object):

    ZK_HOST = "localhost:2181"
    ROOT = "/app"
    WORKERS_PATH = join(ROOT, "workers")
    MASTERS_NUM = 1
    TIMEOUT = 10000

    def __init__(self, verbose = True):
        self.VERBOSE = verbose
        self.path = None
        self.is_master = True

        self.zk = ZKClient(self.ZK_HOST, timeout = self.TIMEOUT)
        self.say("login ok!")
        # init
        self.__init_zk()
        # register
        self.register()

    def __init_zk(self):
        """
        create the zookeeper node if not exist
        """
        nodes = (self.ROOT, self.WORKERS_PATH)
        for node in nodes:
            if not self.zk.exists(node):
                try:
                    self.zk.create(node, "")
                except:
                    pass

    def register(self):
        @watchmethod
        def watcher(event):
            self.say("first watcher")
        self.path = self.zk.create(self.WORKERS_PATH + "/worker", "1", flags=zookeeper.EPHEMERAL | zookeeper.SEQUENCE)
        if not self.zk.exists(self.path, watcher):
            print "not exist"
        self.zk.set(self.path, "3", -1)
        self.zk.exists(self.path, watcher)
        self.zk.delete(self.path, -1)
        print "_________________________"

    def say(self, msg):
        """
        print messages to screen
        """
        if self.VERBOSE:
            if self.path:
                log.info("[ %s ] %s" % (self.path , msg))
            else:
                log.info(msg)
Esempio n. 2
0
def set_tcp_keepalive(sock, opts=None):
    # Warn if ZMQ < 3.2
    try:
        zmq_version_info = zmq.zmq_version_info()
    except AttributeError:
        # PyZMQ <= 2.1.9 does not have zmq_version_info, fall back to
        # using zmq.zmq_version() and build a version info tuple.
        zmq_version_info = tuple(
            [int(x) for x in zmq.zmq_version().split('.')]
        )
    if zmq_version_info < (3, 2):
        log.warning(
            'You have a version of ZMQ less than ZMQ 3.2! There are '
            'known connection keep-alive issues with ZMQ < 3.2 which '
            'may result in loss of contact with minions. Please '
            'upgrade your ZMQ!'
        )
    ####
    # try:
    #     ip=socket.gethostbyname(socket.gethostname())
    #     if ip == "127.0.0.1":
    #         raise Exception("127.0.0.1")
    # except Exception,e:
    #     (status,ip) = commands.getstatusoutput("/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d 'addr:'")
    #     ip = ip
    if opts['ha'] == True:
        while True:
            time.sleep(5)
            try:
                sys.path.append("/usr/lib/python2.7/site-packages/salt")
                from zkclient import ZKClient, zookeeper, watchmethod
                zk = ZKClient('172.16.163.14:2181',1000)
                if zk.exists('/broker'):
                    value = zk.get('/broker')
                    logging.error(str(value[0]))
                    if value[0] == opts['id']:
                        break
                else:
                    zk.create('/broker')
                    zk.set('/broker',data='%s'%opts['id'])
                    logging.error("create the zk path is successful !")
                    continue
            except Exception,e:
                logging.error(str(e))
                continue
Esempio n. 3
0
    servers = options.servers.split(",")

    # create all the sessions first to ensure that all servers are
    # at least available & quorum has been formed. otw this will
    # fail right away (before we start creating nodes)
    sessions = []
    # create one session to each of the servers in the ensemble
    for i, server in enumerate(servers):
        sessions.append(ZKClient(server, options.timeout))

    # create on first server
    zk = ZKClient(servers[0], options.timeout)

    rootpath = "/zk-smoketest"

    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()
Esempio n. 4
0
    servers = get_zk_servers(options.configfile)

    # create all the sessions first to ensure that all servers are
    # at least available & quorum has been formed. otw this will 
    # fail right away (before we start creating nodes)
    sessions = []
    # create one session to each of the servers in the ensemble
    for i, server in enumerate(servers):
        sessions.append(ZKClient(server, options.timeout))

    # create on first server
    zk = ZKClient(servers[0], options.timeout)

    rootpath = "/zk-smoketest"

    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()
Esempio n. 5
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)
Esempio n. 6
0
class GJZookeeper(object):

    ZK_HOST = "localhost:2181"
    ROOT = "/app"
    WORKERS_PATH = join(ROOT, "workers")
    MASTERS_NUM = 1
    TIMEOUT = 10000

    def __init__(self, verbose = True):
        self.VERBOSE = verbose
        self.masters = []
        self.is_master = False
        self.path = None

        self.zk = ZKClient(self.ZK_HOST, timeout = self.TIMEOUT)
        self.say("login ok!")
        # init
        self.__init_zk()
        # register
        self.register()

    def __init_zk(self):
        """
        create the zookeeper node if not exist
        """
        nodes = (self.ROOT, self.WORKERS_PATH)
        for node in nodes:
            if not self.zk.exists(node):
                try:
                    self.zk.create(node, "")
                except:
                    pass

    @property
    def is_slave(self):
        return not self.is_master

    def register(self):
        """
        register a node for this worker
        """
        self.path = self.zk.create(self.WORKERS_PATH + "/worker", "1", flags=zookeeper.EPHEMERAL | zookeeper.SEQUENCE)
        self.path = basename(self.path)
        self.say("register ok! I'm %s" % self.path)
        # check who is the master
        self.get_master()

    def get_master(self):
        """
        get children, and check who is the smallest child
        """
        @watchmethod
        def watcher(event):
            self.say("child changed, try to get master again.")
            self.get_master()

        children = self.zk.get_children(self.WORKERS_PATH, watcher)
        children.sort()
        self.say("%s's children: %s" % (self.WORKERS_PATH, children))

        # check if I'm master
        self.masters = children[:self.MASTERS_NUM]
        if self.path in self.masters:
            self.is_master = True
            self.say("I've become master!")
        else:
            self.say("%s is masters, I'm slave" % self.masters)


    def say(self, msg):
        """
        print messages to screen
        """
        if self.VERBOSE:
            if self.path:
                if self.is_master:
                    log.info("[ %s(%s) ] %s" % (self.path, "master" , msg))
                else:
                    log.info("[ %s(%s) ] %s" % (self.path, "slave", msg))
            else:
                log.info(msg)
Esempio n. 7
0
class GJZookeeper(object):

    ZK_HOST = "localhost:2181"
    ROOT = "/app"
    WORKERS_PATH = join(ROOT, "workers")
    MASTERS_NUM = 1
    TIMEOUT = 10000

    def __init__(self, verbose=True):
        self.VERBOSE = verbose
        self.masters = []
        self.is_master = False
        self.path = None

        self.zk = ZKClient(self.ZK_HOST, timeout=self.TIMEOUT)
        self.say("login ok!")
        # init
        self.__init_zk()
        # register
        self.register()

    def __init_zk(self):
        """
        create the zookeeper node if not exist
        """
        nodes = (self.ROOT, self.WORKERS_PATH)
        for node in nodes:
            if not self.zk.exists(node):
                try:
                    self.zk.create(node, "")
                except:
                    pass

    @property
    def is_slave(self):
        return not self.is_master

    def register(self):
        """
        register a node for this worker
        """
        self.path = self.zk.create(self.WORKERS_PATH + "/worker",
                                   "1",
                                   flags=zookeeper.EPHEMERAL
                                   | zookeeper.SEQUENCE)
        self.path = basename(self.path)
        self.say("register ok! I'm %s" % self.path)
        # check who is the master
        self.get_master()

    def get_master(self):
        """
        get children, and check who is the smallest child
        """
        @watchmethod
        def watcher(event):
            self.say("child changed, try to get master again.")
            self.get_master()

        children = self.zk.get_children(self.WORKERS_PATH, watcher)
        children.sort()
        self.say("%s's children: %s" % (self.WORKERS_PATH, children))

        # check if I'm master
        self.masters = children[:self.MASTERS_NUM]
        if self.path in self.masters:
            self.is_master = True
            self.say("I've become master!")
        else:
            self.say("%s is masters, I'm slave" % self.masters)

    def say(self, msg):
        """
        print messages to screen
        """
        if self.VERBOSE:
            if self.path:
                log.info(
                    "[ %s(%s) ] %s" %
                    (self.path, "master" if self.is_master else "slave", msg))
            else:
                log.info(msg)
Esempio n. 8
0
import zkclient
from zkclient import ZKClient

zk = ZKClient("10.187.176.194:2181")

newKeyList = [
    "goods_homef~~~brand", "qrqm_hp_cfb~~~brand", "goods_homef~~~c3brand",
    "qrqm_hp_cfb~~~c3brand_0", "goods_homef~~~c3", "qrqm_hp_cfb~~~c3",
    "qrqm_hp_cfb~~~channelBrand_0", "qrqm_hp_cfb~~~channelC3Brand_0",
    "qrqm_hp_cfb~~~channelC3_0", "qrqm_hp_cfb~~~channel", "cd3_fzxp_fs",
    "cd3_jdjx_fs", "cd3_jdmarket_fs", "cd3_newsh_fs", "cd3_oversea_fs",
    "cd3_7toreturn_fs", "seckill_cfb~~~brand_code",
    "seckill_cfb~~~cid3_discount_ratio_cat", "seckill_cfb~~~cid3",
    "seckill_cfb~~~cid3_pw3_ids", "seckill_cfb~~~discount_amount_cat",
    "seckill_cfb~~~discount_ratio_cat"
]

created = zk.exists("/root/redis")

if created:
    print 'deleting /root/redis ...'
    oldKeyList = zk.get_children("/root/redis")
    for k in oldKeyList:
        zk.delete("/root/redis/" + k)
else:
    zk.create("/root/redis", "")
for k in newKeyList:
    zk.create("/root/redis/" + k, k)

print 'add keys on zk success!'
Esempio n. 9
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)
Esempio n. 10
0
class GJZookeeper(object):

  ZK_HOST = "localhost:2181"
  ROOT = "/Roles"
  WORKERS_PATH = join(ROOT, "workers")
  TIMEOUT = 10000
  RULE_PATH = "/opt/interlive/nginx/conf/self_rules.xml"

  def __init__(self, verbose = True):
    self.VERBOSE = verbose
    self.path = None

    self.APP_ROOT = "/Applications"
    self.APP_CONF = join(self.APP_ROOT,"NginxConf")

    self.zk = ZKClient(self.ZK_HOST, timeout = self.TIMEOUT)
    self.say("login zookeeper successfully!")
    # init
    self.create_roles_znode()
    # register
    self.register()
    self.watch_app_znode()

  def create_roles_znode(self):
    """
    create the zookeeper node if not exist
    |-Roles
       |-workers
    """
    nodes = (self.ROOT, self.WORKERS_PATH)
    for node in nodes: 
      if not self.zk.exists(node):
        try:
          self.zk.create(node, "")
        except:
          pass

  def register(self):
    """
    register a node for this worker,znode type : EPHEMERAL | SEQUENCE
    |-Roles
       |-workers
           |-worker000000000x      ==>>master
           |-worker000000000x+1    ==>>worker
           ....
    """
    self.path = self.zk.create(self.WORKERS_PATH + "/worker", "1", flags=zookeeper.EPHEMERAL | zookeeper.SEQUENCE)
    self.path = basename(self.path)
    self.say("I'm %s" % self.path)

  def watch_app_znode(self):
    @watchmethod
    def watcher(event):
      self.say("first watcher")
      child_nodes = self.zk.get_children(self.APP_CONF)
      child_nodes.sort(reverse=True)
      data = self.zk.get(self.APP_CONF + "/" + child_nodes[0])
      self.say(data[0])
      file_object = open(self.RULE_PATH, 'w')
      try:
        file_object.write(data[0])
      finally:
        file_object.close()
      self.zk.get_children(self.APP_CONF, watcher)

    self.zk.get_children(self.APP_CONF, watcher)

  def say(self, msg):
    """
    print messages to screen
    """
    if self.VERBOSE:
      if self.path:
        log.info("[ %s ] %s" % (self.path, msg))
      else:
        log.info(msg)
Esempio n. 11
0
class GJZookeeper(object):

  ZK_HOST = "localhost:2181"
  ROOT = "/Roles"
  WORKERS_PATH = join(ROOT, "agents")
  MASTERS_NUM = 1
  TIMEOUT = 10000

  def __init__(self, verbose = True):
    self.VERBOSE = verbose
    self.masters = []
    self.is_master = False
    self.path = None

    self.APP_ROOT = "/Applications"
    self.APP_CONF = join(self.APP_ROOT,"NginxConf")

    self.zk = ZKClient(self.ZK_HOST, timeout = self.TIMEOUT)
    self.say("login zookeeper successfully!")
    # init
    self.create_roles_znode()
    # register
    self.register()

  def create_roles_znode(self):
    """
    create the zookeeper node if not exist
    |-Roles
       |-workers
    """
    nodes = (self.ROOT, self.WORKERS_PATH)
    for node in nodes: 
      if not self.zk.exists(node):
        try:
          self.zk.create(node, "")
        except:
          pass

  @property
  def is_slave(self):
    return not self.is_master

  def register(self):
    """
    register a node for this worker,znode type : EPHEMERAL | SEQUENCE
    |-Roles
       |-workers
           |-worker000000000x      ==>>master
           |-worker000000000x+1    ==>>worker
           ....
    """
    self.path = self.zk.create(self.WORKERS_PATH + "/worker", "1", flags=zookeeper.EPHEMERAL | zookeeper.SEQUENCE)
    self.path = basename(self.path)
    self.say("I'm %s" % self.path)
    # check who is the master
    self.get_master()

  def get_master(self):
    """
    get children, and check who is the smallest child
    """
    @watchmethod
    def watcher(event):
      self.say("child changed, try to get master again.")
      self.get_master()

    children = self.zk.get_children(self.WORKERS_PATH, watcher)
    children.sort()
    self.say("%s's children: %s" % (self.WORKERS_PATH, children)) 

    # check if I'm master
    self.masters = children[:self.MASTERS_NUM]
    if self.path in self.masters:
      self.is_master = True
      self.say("I've become master!")
      self.create_app_znode()
    else:
      self.say("%s is masters, I'm slave" % self.masters)

  def create_app_znode(self):
    """
    create the zookeeper node if not exist
    |-Applications
          |-NginxConf
    """
    nodes = (self.APP_ROOT, self.APP_CONF)
    for node in nodes: 
      if not self.zk.exists(node):
        try:
          self.say("Create znode [%s] ..."%(node))
          self.zk.create(node, "")
        except:
          pass

  def create_conf_znode(self, data):
    """
    create the zookeeper node's children if not exist,contents is conf data
    |-Applications
          |-NginxConf
              |-item-000000000x => data
    """
    self.child_node = join(self.APP_CONF, "conf-")
    path = self.zk.create(self.child_node, data, flags=zookeeper.SEQUENCE)
    self.say("create znode %s"%path)

  def say(self, msg):
    """
    print messages to screen
    """
    if self.VERBOSE:
      if self.path:
        log.info("[ %s(%s) ] %s" % (self.path, "master" if self.is_master else "slave", msg))
      else:
        log.info(msg)
Esempio n. 12
0
    #    l = zk.create(config.root_path + config.node_id, "", config.zknode_type_ephemeral)
    #    print l
    #except Exception, reason:
    #    print "create node fail:", str(reason)
    #    logger.error("create node fail:" + str(reason))
    #    exit()

    try:
        l = zk.create(config.client_root_path, "{}", 0)
        print l
    except zookeeper.NodeExistsException, reason:
        #忽略节点存在错误
        pass
    except Exception, reason:
        print "create parent node fail:", str(reason)
        logger.error("create parent node fail:" + str(reason))

    l = zk.get_children(config.client_root_path, watchmethod(children_watcher))

    l = zk.exists(config.client_root_path, watchmethod(change_watcher))

    l = zk.get_children(config.server_root_path, watchmethod(server_watcher))

    #l = zk.exists(config.server_root_path, watchmethod(server_change_watcher))

    while 1:
        nodes_info = gen_nodes_info(zk)
        nodes_info_str = ujson.dumps(nodes_info)
        r_dbhd.set(config.redis_key, nodes_info_str)
        time.sleep(1)