def tearDown(test): """The matching tearDown for setUp. The single argument is the test case passed to setUp. """ setupstack.tearDown(test) real_zk = testing_with_real_zookeeper() if real_zk: zk = zc.zk.ZooKeeper(real_zk) root = setupstack.globs(test)['/zc.zk.testing.test-root'] if zk.exists(root): zk.delete_recursive(root) zk.close()
def setup_tree(tree, connection_string, root='/test-root', zookeeper_node=False): zk = zc.zk.ZooKeeper(connection_string) if zk.exists(root): zk.delete_recursive(root) zk.create(root, '', zc.zk.OPEN_ACL_UNSAFE) zk.import_tree(tree or """ /fooservice /providers database = '/databases/foomain' threads = 1 favorite_color = 'red' """, root) if zookeeper_node: zk.import_tree(""" /zookeeper /quota """, root) zk.close()
zookeeper_path_list = [] for server_type in service_hash.keys(): temp = "/%s-%s-%s-%s" % (datacenter,server_type,location,environment) zookeeper_path_list.append(temp) return service_hash, zookeeper_path_list def get_ip_encode(children): ip_encode = ''.join(list(children)) ip_encode = base64.b64encode(ip_encode) return ip_encode while True: service_hash, zookeeper_path_list = get_service_hash() for path in zookeeper_path_list: try: exists = zk.exists(path) except KazooException: exists = None zk = get_zk_conn() if exists: children = zk.get_children(path, watch=my_func) print path,children ip_encode = get_ip_encode(children) server_type = path.split('-')[1] if os.path.isfile('/etc/haproxy/conf.d/%s-%s.cfg' % (server_type,ip_encode))==False: create_cgf(path,list(children)) sys.stdout.flush() sys.stderr.flush()
environment) zookeeper_path_list.append(temp) return service_hash, zookeeper_path_list def get_ip_encode(children): ip_encode = ''.join(list(children)) ip_encode = base64.b64encode(ip_encode) return ip_encode while True: service_hash, zookeeper_path_list = get_service_hash() for path in zookeeper_path_list: try: exists = zk.exists(path) except KazooException: exists = None zk = get_zk_conn() if exists: children = zk.get_children(path, watch=my_func) print path, children ip_encode = get_ip_encode(children) server_type = path.split('-')[1] if os.path.isfile('/etc/haproxy/conf.d/%s-%s.cfg' % (server_type, ip_encode)) == False: create_cgf(path, list(children)) sys.stdout.flush() sys.stderr.flush()
def clients(path): logging.basicConfig() random.seed(0) import zc.zk zk = zc.zk.ZooKeeper() properties = zk.properties(path) settings = zc.mappingobject.mappingobject(properties) siteids = [] @properties def _(*a): n = settings.sites siteids[:] = [0] for i in range(4): if n: siteids.extend(range(n)) n /= 2 wpath = path + '/lb/providers' zope.testing.wait.wait(lambda : zk.get_children(wpath)) [waddr] = zk.get_children(wpath) waddr = zc.parse_addr.parse_addr(waddr) stats = zc.mappingobject.mappingobject(dict( truncated = 0, requests = 0, bypid = {}, nobs = 0, nhits = 0, )) spath = path + '/stats' if not zk.exists(spath): zk.create(spath, '', zc.zk.OPEN_ACL_UNSAFE) import gevent.socket def do_request(): siteid = random.choice(siteids) oids = set( int(random.gauss(0, settings.objects_per_site/4)) for i in range(settings.objects_per_request) ) socket = gevent.socket.create_connection(waddr) try: socket.sendall( request_template % dict( data='_'.join(map(str, oids)), host='h%s' % siteid, ) ) response = '' while '\r\n\r\n' not in response: data = socket.recv(9999) if not data: stats.truncated += 1 return response += data headers, body = response.split('\r\n\r\n') headers = headers.split('\r\n') status = headers.pop(0) headers = dict(l.strip().lower().split(':', 1) for l in headers if ':' in l) content_length = int(headers['content-length']) while len(body) < content_length: data = socket.recv(9999) if not data: stats.truncated += 1 return body += data pid, n, nhit, nmiss, nevict = map(int, body.strip().split()) stats.requests += 1 stats.nobs += n stats.nhits += nhit bypid = stats.bypid.get(pid) if bypid is None: bypid = stats.bypid[pid] = dict(nr=0, n=0, nhit=0) bypid['nr'] += 1 bypid['n'] += n bypid['nhit'] += nhit logger.info(' '.join(map(str, ( 100*stats.nhits/stats.nobs, pid, n, nhit, 100*nhit/n, )))) finally: socket.close() def client(): try: while 1: do_request() except: print 'client error' logging.getLogger(__name__+'-client').exception('client') greenlets = [gevent.spawn(client) for i in range(settings.clients)] # Set up notification of address changes. watcher = gevent.get_hub().loop.async() @watcher.start def _(): print 'got update event' while settings.clients > len(greenlets): greenlets.append(gevent.spawn(client)) while settings.clients < len(greenlets): greenlets.pop().kill() properties(lambda a: watcher.send()) while 1: gevent.sleep(60.0)
#!/usr/bin/env python #conding=utf-8 #by huangjie import commands import zc.zk import sys projectname = "/" + sys.argv[1] stat, proStr = commands.getstatusoutput('cat cafago.zk |egrep -v "^$|^sent|^send|^total"') zk = zc.zk.ZooKeeper("192.168.221.57:2181,192.168.221.57:2182,192.168.221.57:2183") if zk.exists(projectname): zk.set(projectname,proStr) else: zk.create(projectname) zk.set(projectname,proStr)