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
    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()

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

    # create znodes, one znode per session (client), one session per server
Esempio n. 4
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. 5
0
    #每个节点启动时去/work节点注册自身ip
    zk = ZKClient(config.server_list)
    print "dir zk", dir(zk)
    #try:
    #    ret = zk.delete("/worker")
    #    print "delete ret:", ret
    #except:
    #    pass
    #try:
    #    ret = zk.create("/worker", "")
    #    print "create ret:", ret
    #except Exception, reason:
    #    print "create failed", reason
    #    pass
    try:
        ret = zk.create("/worker/work", "100", zookeeper.EPHEMERAL | zookeeper.SEQUENCE)
        print "create ret:", ret
    except Exception, reason:
        print "create failed", reason
        pass
    l = zk.get_children("/")
    print "children:", l
    #work_v = zk.get("/work")
    #print "work_v:", work_v
    lock_name = zk_lock.get_lock(zk, "/lock")
    exit()
    logger = init_logger()

    logger.info("logger init done.")

    def safe_reg_self(parent_node, node_id):
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
    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'

Esempio n. 8
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. 9
0
    client_set = config.client_node_ids
    r_dbhd = redis.Redis(host = config.redis_host, port = config.redis_port, db = config.redis_db)
    zk = ZKClient(config.server_list)

    #此处创建server端监视节点
    safe_reg_self(config.server_root_path, config.node_id)
    #try:
    #    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))
Esempio n. 10
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. 11
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. 12
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. 13
0
                zk.set(parent_node, s, ver)
            except zookeeper.BadVersionException, reason:
                try_limit -= 1
            else:
                break
        else:
            return -1

    #print zk.get("/xyz")

    def test_fun(event):
        print "type_name:", event.type_name
        print "state_name:", event.state_name
        zk.exists("/xyz", watchmethod(test_fun))

    l = zk.create("/work", "123", config.zknode_type_ephemeral)

    raw_input(">>")

    l = zk.get("/work")
    exit()
    try:
        l = zk.get("/work/nonode")
    except zookeeper.NoNodeException, reason:
        print reason
    else:
        print l

    exit()

    print l[1]['version']
Esempio n. 14
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. 15
0
sys.path.append("../dep/ice/")
import Ice
Ice.loadSlice("../slice/mq.ice")
import com.renren.x2.mq
mq=com.renren.x2.mq



class MqServiceI(mq.MqService):
	def forwardMq(self, mid, msg, current=None):
		print msg
		return 1

if __name__ == "__main__":
        zookeeper.set_debug_level(zookeeper.LOG_LEVEL_ERROR)
        zk = ZKClient("localhost:2181")
        try:
                path = zk.create("/3g/ice/x2/MqService/msgmap/test/.service_nodes/127.0.0.1:10015", flags=zookeeper.EPHEMERAL)
        except:
                pass
        try:
            ic = Ice.initialize(sys.argv)
            addr ="default -h %s -p %d -t 200" % ('127.0.0.1',10015)
            adapter   = ic.createObjectAdapterWithEndpoints( "MqService", addr)
            adapter.add(MqServiceI(), ic.stringToIdentity("MqService"))
            adapter.activate()
            ic.waitForShutdown()
        except Exception,x:
            conf.logger.error(str(x))
            
Esempio n. 16
0
    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()

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

    # create znodes, one znode per session (client), one session per server
Esempio n. 17
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'