本文整理汇总了Python中zookeeper.get_children函数的典型用法代码示例。如果您正苦于以下问题:Python get_children函数的具体用法?Python get_children怎么用?Python get_children使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了get_children函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ListService
def ListService(self, service_id = None, endpoint = None):
if service_id:
if endpoint:
ep = Endpoint(self.zk, Service(self.zk, service_id), endpoint)
print INFO(datetime.datetime.now().strftime("[%Y-%m-%d %H:%M:%S] ") + " endpoint is:" + endpoint + " status is:" + ep.Status() + " healthy is:" + ep.Healthy())
return ep
else:
service = Service(self.zk, service_id)
service.Load()
service.ShowStatus()
return service
else:
zkinfolog = OpenZkLog("./log.txt")
departments = zookeeper.get_children(self.zk, '/com/renren/xoa', None)
CloseZkLog(zkinfolog)
service_ids = []
for department in departments:
dp_path = '/com/renren/xoa/' + department
zkinfolog = OpenZkLog("./log.txt")
services = zookeeper.get_children(self.zk, dp_path, None)
CloseZkLog(zkinfolog)
for service in services:
service_id = GetServiceIdByPath(dp_path + '/' + service)
print INFO(datetime.datetime.now().strftime("[%Y-%m-%d %H:%M:%S] ") + "service id is:" + service_id)
service_ids.append(service_id)
return service_ids
示例2: __init__
def __init__(self, service_name, connect_string, timeout=DEFAULT_TIMEOUT, default_port=6664):
self.SERVICE_NODE = "/" + service_name
self.AVAILABILITY_NODE = self.SERVICE_NODE + "/available"
self.MEMBERSHIP_NODE = self.SERVICE_NODE + "/members"
self.connected = False
self.timeout = timeout
self.default_port = default_port
self.conn_cv = threading.Condition()
self.conn_cv.acquire()
self.handle = zookeeper.init(connect_string, self.connection_watcher, timeout)
self.conn_cv.wait(timeout / 1000)
self.conn_cv.release()
self.watcher_lock = threading.Lock()
self.logger = logging.getLogger("sincc")
if not self.connected:
raise SinClusterClientError("Unable to connect to %s" % connect_string)
for path in [self.SERVICE_NODE, self.AVAILABILITY_NODE, self.MEMBERSHIP_NODE]:
if not zookeeper.exists(self.handle, path):
zookeeper.create(self.handle, path, "", [ZOO_OPEN_ACL_UNSAFE], 0)
self.listeners = []
# Start to watch both /members and /available
zookeeper.get_children(self.handle, self.MEMBERSHIP_NODE, self.watcher)
available = zookeeper.get_children(self.handle, self.AVAILABILITY_NODE, self.watcher)
self.available_nodes = {}
for node_id in available:
self.available_nodes[int(node_id)] = Node(int(node_id),
zookeeper.get(self.handle, self.AVAILABILITY_NODE + "/" + node_id)[0])
示例3: __init__
def __init__(self, zkhosts, root=NODE_HQ_ROOT, alivenode='alive'):
"""zkhosts: a string or a list. list will be ','.join-ed into a string.
root: root node path (any parents must exist, if any)
"""
if not isinstance(zkhosts, basestring):
zkhosts = ','.join(zkhosts)
self.zkhosts = zkhosts
self.ROOT = root
self.alivenode = alivenode
self.nodename = os.uname()[1]
self.NODE_SERVERS = self.ROOT + '/servers'
self.NODE_ME = self.NODE_SERVERS+'/'+self.nodename
self.zh = zk.init(self.zkhosts, self.__watcher)
self.jobs = {}
self.initialize_node_structure()
if not zk.exists(self.zh, self.NODE_ME):
zk.create(self.zh, self.NODE_ME, '', PERM_WORLD)
# setup notification
zk.get_children(self.zh, self.NODE_SERVERS, self.__servers_watcher)
#zk.get_children(self.zh, self.NODE_ME, self.__watcher)
self.NODE_JOBS = self.NODE_ME+'/jobs'
zk.acreate(self.zh, self.NODE_JOBS, '', PERM_WORLD)
示例4: notifyFailedTransaction
def notifyFailedTransaction(self, app_id, txid):
""" Notify failed transaction id.
This method will add the transaction id into black list.
After this call, the transaction becomes invalid.
"""
self.__waitForConnect()
self.checkTransaction(app_id, txid)
print "notify failed transaction app:%s, txid:%d" % (app_id, txid)
txpath = self.__getTransactionPath(app_id, txid)
lockpath = None
try:
lockpath = zookeeper.get(self.handle, PATH_SEPARATOR.join([txpath, TX_LOCK_PATH]), None)[0]
except zookeeper.NoNodeException:
# there is no lock. it means there is no need to rollback.
pass
if lockpath:
# add transacion id to black list.
now = str(time.time())
broot = self.__getBlacklistRootPath(app_id)
if not zookeeper.exists(self.handle, broot):
self.__forceCreatePath(broot)
zookeeper.acreate(self.handle, PATH_SEPARATOR.join([broot, str(txid)]), now, ZOO_ACL_OPEN)
# update local cache before notification
if app_id in self.blacklistcache:
with self.blacklistcv:
self.blacklistcache[app_id].add(str(txid))
# copy valid transaction id for each updated key into valid list.
for child in zookeeper.get_children(self.handle, txpath):
if re.match("^" + TX_UPDATEDKEY_PREFIX, child):
value = zookeeper.get(self.handle, PATH_SEPARATOR.join([txpath, child]), None)[0]
valuelist = value.split(PATH_SEPARATOR)
key = urllib.unquote_plus(valuelist[0])
vid = valuelist[1]
vtxroot = self.__getValidTransactionRootPath(app_id)
if not zookeeper.exists(self.handle, vtxroot):
self.__forceCreatePath(vtxroot)
vtxpath = self.__getValidTransactionPath(app_id, key)
zookeeper.acreate(self.handle, vtxpath, vid, ZOO_ACL_OPEN)
# release the lock
try:
zookeeper.adelete(self.handle, lockpath)
except zookeeper.NoNodeException:
# this should be retry.
pass
# just remove transaction node
try:
for item in zookeeper.get_children(self.handle, txpath):
zookeeper.adelete(self.handle, PATH_SEPARATOR.join([txpath, item]))
zookeeper.adelete(self.handle, txpath)
except zookeeper.NoNodeException:
# something wrong. next GC will take care of it.
return False
return True
示例5: handle_membership_changed
def handle_membership_changed(self):
members = zookeeper.get_children(self.handle, self.MEMBERSHIP_NODE, self.watcher)
# No need to watch /available here.
available = zookeeper.get_children(self.handle, self.AVAILABILITY_NODE)
self.available_nodes.clear()
for node_id in members:
if node_id in available:
self.available_nodes[int(node_id)] = Node(int(node_id),
zookeeper.get(self.handle, self.AVAILABILITY_NODE + "/" + node_id)[0])
self.notify_all()
示例6: get_job_list
def get_job_list(zk):
root_list = zookeeper.get_children(zk, '/')
job_list = {}
job_list['data'] = []
for root in root_list:
if root == "zookeeper":
continue
job_name = zookeeper.get_children(zk, '/' + root)
for job in job_name:
temp = {'{#JOBPATH}':"/%s/%s" % (root,job)}
job_list['data'].append(temp)
job_list_json = json.dumps(job_list, indent=4, sort_keys=True)
print job_list_json
示例7: get_politicians
def get_politicians(self, watcher=None, cell=None):
if cell is None:
cell = self._cell
if watcher is None:
children = zookeeper.get_children(self._z, '%s/politicians' % (cell))
else:
children = zookeeper.get_children(self._z, '%s/politicians' % (cell),
watcher)
children = sorted(children)
politicians = list()
for child in children:
politicians.append('%s/politicians/%s' % (cell, child))
return sorted(politicians)
示例8: getReadLock
def getReadLock(self, metadata):
#Acquire read lock on file through ZooKeeper
fn = metadata.filename
print "Trying to acquire read lock for " + self.root + fn
self.zpath = self.createLockNode(fn, 'r')
myLock = self.zpath.rsplit('/',1)[1]
while True:
children = sorted(zookeeper.get_children(self.zh, self.root + fn))
lastLock = ''
for child in children:
try:
if child == myLock:
break
elif zookeeper.get(self.zh, self.root+fn+'/'+child)[0] == 'w':
lastLock = child
except:
pass
if lastLock != '':
def watcher (handle, type, state, path):
self.cv2.acquire()
self.cv2.notify()
self.cv2.release()
self.cv2.acquire()
if zookeeper.exists(self.zh, self.root+fn+'/'+lastLock, watcher):
self.cv2.wait()
self.cv2.release()
else:
break
print "Acquired read lock for " + self.root + fn
示例9: list
def list(self, path=''):
path = '%s%s' % (self.prefix, path)
try:
return zookeeper.get_children(self.handle, path, None)
except:
self.log.exception('Failed to fetch subkeys for %s', path)
return []
示例10: LoadEndpoints
def LoadEndpoints(self):
group_path = self.GetActiveGroupPath()
try:
group_children = zookeeper.get_children(self.zk, group_path, None);
except zookeeper.NoNodeException, msg:
print ERROR(group_path + " node exist.")
return
示例11: find_leaves_first
def find_leaves_first(node_name):
global zh
children = zookeeper.get_children(zh,node_name)
for c in children:
for n in find_leaves_first(os.path.join(node_name,c)):
yield n
yield node_name
示例12: zookeeper_watch
def zookeeper_watch(self, zh, event, state, path):
self.cond.acquire()
try:
result = None
if event == zookeeper.CHILD_EVENT:
fns = self.child_watches.get(path, None)
if fns:
try:
result = zookeeper.get_children(self.handle, path, self.zookeeper_watch)
except zookeeper.NoNodeException:
result = None
elif event == zookeeper.CHANGED_EVENT:
fns = self.content_watches.get(path, None)
if fns:
try:
result, _ = zookeeper.get(self.handle, path, self.zookeeper_watch)
except zookeeper.NoNodeException:
result = None
else:
return
if result != None and fns != None:
for fn in fns:
# Don't allow an individual watch firing an exception to
# prevent all other watches from being fired. Just log an
# error message and moved on to the next callback.
try:
fn(result)
except Exception:
logging.exception("Error executing watch for %s.", path)
finally:
self.cond.release()
示例13: gc_runner
def gc_runner(self):
""" Transaction ID garbage collection (GC) runner.
Note: This must be running as separate thread.
"""
logging.info("Starting GC thread.")
while self.gc_running:
if self.connected:
# Scan each application's last GC time.
try:
app_list = zookeeper.get_children(self.handle, APPS_PATH)
for app in app_list:
app_id = urllib.unquote_plus(app)
# App is already encoded, so we should not use
# self.get_app_root_path.
app_path = PATH_SEPARATOR.join([APPS_PATH, app])
self.try_garbage_collection(app_id, app_path)
except zookeeper.NoNodeException:
# There were no nodes for this application.
pass
except zookeeper.OperationTimeoutException, ote:
logging.warning("GC operation timed out while trying to get {0}"\
" with {1}".format(APPS_PATH, str(ote)))
except zookeeper.ZooKeeperException, zk_exception:
logging.error("ZK Exception: {0}".format(zk_exception))
self.reestablish_connection()
return
示例14: __receiveNotify
def __receiveNotify(self, handle, type, state, path):
# print "notify type:%s, state:%s, path:%s" % (type, state, path)
if type == zookeeper.SESSION_EVENT:
if state == zookeeper.CONNECTED_STATE:
# connected
with self.connectcv:
self.connected = True
self.connectcv.notifyAll()
else:
# disconnected
with self.connectcv:
self.connected = False
self.connectcv.notifyAll()
elif type == zookeeper.CHILD_EVENT:
pathlist = path.split(PATH_SEPARATOR)
if pathlist[-1] == TX_BLACKLIST_PATH:
# update blacklist cache
appid = urllib.unquote_plus(pathlist[-3])
try:
blist = zookeeper.get_children(self.handle, path, self.__receiveNotify)
# print "update blacklist: ",blist
with self.blacklistcv:
self.blacklistcache[appid] = set(blist)
except zookeeper.NoNodeException:
if appid in self.blacklistcache:
with self.blacklistcv:
del self.blacklistcache[appid]
示例15: handle_availability_changed
def handle_availability_changed(self):
available = zookeeper.get_children(self.handle, self.AVAILABILITY_NODE, self.watcher)
self.available_nodes.clear()
for node_id in available:
self.available_nodes[int(node_id)] = Node(int(node_id),
zookeeper.get(self.handle, self.AVAILABILITY_NODE + "/" + node_id)[0])
self.notify_all()