Perform Master discovery for specified service.
429554
async def discover_master(self, service, timeout):
        """"""
        # TODO: get lock
        idle_timeout = timeout
        # FIXME: single timeout used 4 times;
        #   meaning discovery can take up to:
        #   3 * timeout * (sentinels count)
        #
        #   having one global timeout also can leed to
        #   a problem when not all sentinels are checked.

        # use a copy, cause pools can change
        pools = self._pools[:]
        for sentinel in pools:
            try:
                with async_timeout(timeout, loop=self._loop):
                    address = await self._get_masters_address(
                        sentinel, service)

                pool = self._masters[service]
                with async_timeout(timeout, loop=self._loop), \
                        contextlib.ExitStack() as stack:
                    conn = await pool._create_new_connection(address)
                    stack.callback(conn.close)
                    await self._verify_service_role(conn, 'master')
                    stack.pop_all()

                return conn
            except asyncio.CancelledError:
                # we must correctly handle CancelledError(s):
                #   application may be stopped or function can be cancelled
                #   by outer timeout, so we must stop the look up.
                raise
            except asyncio.TimeoutError:
                continue
            except DiscoverError as err:
                sentinel_logger.debug("DiscoverError(%r, %s): %r",
                                      sentinel, service, err)
                await asyncio.sleep(idle_timeout, loop=self._loop)
                continue
            except RedisError as err:
                raise MasterReplyError("Service {} error".format(service), err)
            except Exception:
                # TODO: clear (drop) connections to schedule reconnect
                await asyncio.sleep(idle_timeout, loop=self._loop)
                continue
        else:
            raise MasterNotFoundError("No master found for {}".format(service))
367175
def __find(self, name):
        
        service = None
        if not self.__services:
            raise Exception, "No services defined"
        if isinstance(name, int):
            try:
                service = self.__services[name]
                name = service.name
            except IndexError:
                raise ServiceNotFound, "at [%d]" % (name,)
        else:
            for s in self.__services:
                if name == s.name:
                    service = s
                    break
        if service is None:
            raise ServiceNotFound, name
        return PortSelector(self.__client, service.ports, name)
838472
def master_for(self, service):
        """"""
        # TODO: make it coroutine and connect minsize connections
        if service not in self._masters:
            self._masters[service] = ManagedPool(
                self, service, is_master=True,
                db=self._redis_db,
                password=self._redis_password,
                encoding=self._redis_encoding,
                minsize=self._redis_minsize,
                maxsize=self._redis_maxsize,
                ssl=self._redis_ssl,
                parser=self._parser_class,
                loop=self._loop)
        return self._masters[service]
17579
def find_service_by_type(self, service_type):
        
        for service in self._services:
            if service_type == service.type:
                return service
        return None
1264729
def check_service(self):
        """"""
        try:
            service = yield Service.get(self.service_id)
        except couch.NotFound:
            raise exceptions.ValidationError('Service {} not found'
                                             .format(self.service_id))

        if service.service_type != 'repository':
            raise exceptions.ValidationError('{} is not a repository service'
                                             .format(self.service_id))

        if service.state != State.approved:
            raise exceptions.ValidationError('{} is not an approved service'
                                             .format(self.service_id))
305033
def get_services(root, hosts_map, view=None):
  ''' 
  '''
  services_list = []
  mgmt_service = root.get_cloudera_manager().get_service()
  services_list.append({"hostname": CM_DUMMY_HOST,
                        "name": mgmt_service.name,
                        "display_name": "CM Managed Service: %s" % (mgmt_service.name,),
                        "status": get_status(mgmt_service),
                        "url": mgmt_service.serviceUrl,
                        "health_summary": mgmt_service.healthSummary})
  for cm_role in root.get_cloudera_manager().get_service().get_all_roles(view):
    services_list.append({"hostname": hosts_map[cm_role.hostRef.hostId]["hostname"],
                          "name": cm_role.name,
                          "display_name": "CM Management Service: %s" % (cm_role.name,),
                          "status": get_status(cm_role),
                          "url": cm_role.roleUrl,
                          "health_summary": cm_role.healthSummary})
  for cm_host in root.get_all_hosts(view):
    services_list.append({"hostname": hosts_map[cm_host.hostId]["hostname"],
                          "name": "cm-host-%s" % (cm_host.hostname,),
                          "display_name": "CM Managed Host: %s" % (cm_host.hostname,),
                          "status": get_status(cm_host),
                          "url": cm_host.hostUrl,
                          "health_summary": cm_host.healthSummary})
  for cluster in root.get_all_clusters(view):
    for service in cluster.get_all_services(view):
      services_list.append({"hostname": cluster.name,
                            "name": service.name,
                            "display_name": "CM Managed Service: %s" % (service.name,),
                            "status": get_status(service),
                            "url": service.serviceUrl,
                            "health_summary": service.healthSummary})
      for role in service.get_all_roles(view):
        services_list.append({"hostname": hosts_map[role.hostRef.hostId]["hostname"],
                              "name": role.name,
                              "display_name": "%s:%s" % (cluster.name, role.name,),
                              "status": get_status(role),
                              "url": role.roleUrl,
                              "health_summary": role.healthSummary})
  return services_list
1217775
def list_services(self, limit=None, marker=None):
        """"""
        return self._services_manager.list(limit=limit, marker=marker)
1368267
def _read_services(self, services):
        """"""
        for service in services:
            parser = FritzSCDPParser(self.address, self.port, service)
            actions = parser.get_actions()
            service.actions = {action.name: action for action in actions}
            self.services[service.name] = service
193120
def connect_master(self, failed=False):
        '''
        
        '''
        master, self.pub_channel = yield self.eval_master(self.opts, self.timeout, self.safe, failed)
        yield self._post_master_init(master)
1077558
def get_master(self, host, port, sentinel_port, sentinel_name):
        
        if sentinel_port and sentinel_name:
            master = Sentinel([(host, sentinel_port)], socket_timeout=1)\
                .discover_master(sentinel_name)
            return master
        return host, port
262844
async def discover_slave(self, service, timeout, **kwargs):
        """"""
        # TODO: use kwargs to change how slaves are picked up
        #   (eg: round-robin, priority, random, etc)
        idle_timeout = timeout
        pools = self._pools[:]
        for sentinel in pools:
            try:
                with async_timeout(timeout, loop=self._loop):
                    address = await self._get_slave_address(
                        sentinel, service)  # add **kwargs
                pool = self._slaves[service]
                with async_timeout(timeout, loop=self._loop), \
                        contextlib.ExitStack() as stack:
                    conn = await pool._create_new_connection(address)
                    stack.callback(conn.close)
                    await self._verify_service_role(conn, 'slave')
                    stack.pop_all()
                return conn
            except asyncio.CancelledError:
                raise
            except asyncio.TimeoutError:
                continue
            except DiscoverError:
                await asyncio.sleep(idle_timeout, loop=self._loop)
                continue
            except RedisError as err:
                raise SlaveReplyError("Service {} error".format(service), err)
            except Exception:
                await asyncio.sleep(idle_timeout, loop=self._loop)
                continue
        raise SlaveNotFoundError("No slave found for {}".format(service))
195865
def __StartMaster(self):
        
    num_masters = len(self.cluster.get_instances_in_role("master", "running"))
    assert(num_masters < 1)
    logging.info( "waiting for masters to start")
    if self.config.master_on_spot_instances:          
      self.__LaunchSpotMasterInstances()
    else:
      self.__LaunchOnDemandMasterInstances()
    time.sleep(1)
    self.__ConfigureMaster()
    return True
451337
def set_web_master(self):
        """"""
        try:
            self.web_master = self.soup.find('webmaster').string
        except AttributeError:
            self.web_master = None
501541
def _collect_masters_map(self, response):
        '''
        
        '''
        while True:
            try:
                data, addr = self._socket.recvfrom(0x400)
                if data:
                    if addr not in response:
                        response[addr] = []
                    response[addr].append(data)
                else:
                    break
            except Exception as err:
                if not response:
                    self.log.error('Discovery master collection failure: %s', err)
                break
546759
def discover(self):
        '''
        
        '''
        response = {}
        masters = {}
        self.log.info("Looking for a server discovery")
        self._query()
        self._collect_masters_map(response)
        if not response:
            msg = 'No master has been discovered.'
            self.log.info(msg)
        else:
            for addr, descriptions in response.items():
                for data in descriptions:  # Several masters can run at the same machine.
                    msg = salt.utils.stringutils.to_unicode(data)
                    if msg.startswith(self.signature):
                        msg = msg.split(self.signature)[-1]
                        self.log.debug(
                            "Service announcement at '%s:%s'. Response: '%s'",
                            addr[0], addr[1], msg
                        )
                        if ':E:' in msg:
                            err = msg.split(':E:')[-1]
                            self.log.error(
                                'Error response from the service publisher at %s: %s',
                                addr, err
                            )
                            if "timestamp" in err:
                                self.log.error('Publisher sent shifted timestamp from %s', addr)
                        else:
                            if addr not in masters:
                                masters[addr] = []
                            masters[addr].append(
                                salt.utils.json.loads(msg.split(':@:')[-1], _json_module=_json)
                            )
        return masters
909151
def AddServiceFromResource(self, service_name, methods):
        """"""
        service_name = self.__names.CleanName(service_name)
        method_descriptions = methods.get('methods', {})
        method_info_map = collections.OrderedDict()
        items = sorted(method_descriptions.items())
        for method_name, method_description in items:
            method_name = self.__names.MethodName(method_name)

            # NOTE: According to the discovery document, if the request or
            # response is present, it will simply contain a `$ref`.
            body_type = method_description.get('request')
            if body_type is None:
                request_type = None
            else:
                request_type = self.__GetRequestType(body_type)
            if self.__NeedRequestType(method_description, request_type):
                request = self.__CreateRequestType(
                    method_description, body_type=body_type)
                request_field = self.__GetRequestField(
                    method_description, body_type)
            else:
                request = request_type
                request_field = base_api.REQUEST_IS_BODY

            if 'response' in method_description:
                response = method_description['response']['$ref']
            else:
                response = self.__CreateVoidResponseType(method_description)

            method_info_map[method_name] = self.__ComputeMethodInfo(
                method_description, request, response, request_field)

        nested_services = methods.get('resources', {})
        services = sorted(nested_services.items())
        for subservice_name, submethods in services:
            new_service_name = '%s_%s' % (service_name, subservice_name)
            self.AddServiceFromResource(new_service_name, submethods)

        self.__RegisterService(service_name, method_info_map)
1000359
private void initDiscovery() {
    if (discoveryServiceClient == null) {
      LOG.info("No DiscoveryServiceClient provided. Skipping service discovery.");
      return;
    }

    endpointStrategy = new TimeLimitEndpointStrategy(
      new RandomEndpointStrategy(
        discoveryServiceClient.discover(
          configuration.get(TxConstants.Service.CFG_DATA_TX_DISCOVERY_SERVICE_NAME,
                            TxConstants.Service.DEFAULT_DATA_TX_DISCOVERY_SERVICE_NAME))),
      2, TimeUnit.SECONDS);
  }