func()

in istio/pkg/controllers/servicecenter/connector.go [136:178]


func (c *Connector) GetServiceInstances(entries []*event.MicroserviceEntry) map[string][]*discovery.MicroServiceInstance {
	if len(entries) == 0 {
		return nil
	}
	appServiceKeys := make(map[string][]*discovery.FindService, len(entries))
	for _, e := range entries {
		s := e.MicroService
		appId := s.AppId
		key := &discovery.MicroServiceKey{
			ServiceName: s.ServiceName,
			AppId:       appId,
			Environment: s.Environment,
			Version:     s.Version,
			Alias:       s.Alias,
		}
		if _, ok := c.AppInstanceWatcherCache.Load(appId); !ok {
			log.Errorf("failed to watch new microservices for appId %s, watcher service failed to start\n", appId)
			continue
		}
		appServiceKeys[appId] = append(appServiceKeys[appId], &discovery.FindService{Service: key})
	}
	serviceInstanceMap := map[string][]*discovery.MicroServiceInstance{}
	for appId, keys := range appServiceKeys {
		if ks, ok := c.AppInstanceWatcherCache.Load(appId); ok {
			// Initial instance sync of services with same appId, will use app instance watcher for future instance updates
			res, err := c.client.BatchFindInstances(ks.(string), keys, sc.WithGlobal(), sc.WithoutRevision())
			if err != nil {
				log.Errorf("failed to watch new microservices, unable to get instances, err[%v]\n", err)
			}
			for _, r := range res.Services.Updated {
				e := entries[r.Index]
				serviceInstanceMap[e.MicroService.ServiceId] = r.Instances
			}
			log.Infof("Started watching instances of services with appId %s\n", appId)
		}
	}
	for _, e := range entries {
		if _, ok := serviceInstanceMap[e.MicroService.ServiceId]; !ok {
			log.Errorf("failed to watch instances of service %s with id %s\n", e.MicroService.ServiceName, e.MicroService.ServiceId)
		}
	}
	return serviceInstanceMap
}