def get_all(self, group_id): """Return all members in a list, or a ServiceGroupUnavailable exception. """ monitor = self._monitors.get(group_id, None) if monitor is None: path = "%s/%s" % (CONF.zookeeper.sg_prefix, group_id) null = open(os.devnull, "w") local_session = evzookeeper.ZKSession( CONF.zookeeper.address, recv_timeout=CONF.zookeeper.recv_timeout, zklog_fd=null) monitor = membership.MembershipMonitor(local_session, path) self._monitors[group_id] = monitor # Note(maoy): When initialized for the first time, it takes a # while to retrieve all members from zookeeper. To prevent # None to be returned, we sleep 5 sec max to wait for data to # be ready. for _retry in range(50): eventlet.sleep(0.1) all_members = monitor.get_all() if all_members is not None: return all_members all_members = monitor.get_all() if all_members is None: raise exception.ServiceGroupUnavailable(driver="ZooKeeperDriver") return all_members
def get_all(self, group_id): """Return all members in a list, or a ServiceGroupUnavailable exception. """ monitor = self._monitors.get(group_id, None) if monitor is None: path = "%s/%s" % (CONF.zookeeper.sg_prefix, group_id) null = open(os.devnull, "w") local_session = evzookeeper.ZKSession(CONF.zookeeper.address, recv_timeout= CONF.zookeeper.recv_timeout, zklog_fd=null) monitor = membership.MembershipMonitor(local_session, path) self._monitors[group_id] = monitor # Note(maoy): When initialized for the first time, it takes a # while to retrieve all members from zookeeper. To prevent # None to be returned, we sleep 5 sec max to wait for data to # be ready. timeout = 5 # seconds interval = 0.1 tries = int(timeout / interval) for _retry in range(tries): eventlet.sleep(interval) all_members = monitor.get_all() if all_members is not None: # Stop the tries once the cache is populated LOG.debug('got info about members in %r: %r', path, ', '.join(all_members)) break else: # if all_members, weren't populated LOG.warning(_LW('Problem with acquiring the list of ' 'children of %(path)r within a given ' 'timeout=%(timeout)rs'), path, timeout) else: all_members = monitor.get_all() if all_members is None: raise exception.ServiceGroupUnavailable(driver="ZooKeeperDriver") def have_processes(member): """Predicate that given member has processes (subnode exists).""" value, stat = monitor.get_member_details(member) # only check nodes that are created by Membership class if value == 'ZKMembers': num_children = stat['numChildren'] return num_children > 0 else: # unknown type of node found - ignoring return False # filter only this members that have processes running all_members = filter(have_processes, all_members) return all_members