Search in sources :

Example 1 with ClientInfo

use of com.alibaba.nacos.naming.push.v1.ClientInfo in project nacos by alibaba.

the class InstanceOperatorServiceImpl method listInstance.

@Override
public ServiceInfo listInstance(String namespaceId, String serviceName, Subscriber subscriber, String cluster, boolean healthOnly) throws Exception {
    ClientInfo clientInfo = new ClientInfo(subscriber.getAgent());
    String clientIP = subscriber.getIp();
    ServiceInfo result = new ServiceInfo(serviceName, cluster);
    Service service = serviceManager.getService(namespaceId, serviceName);
    long cacheMillis = switchDomain.getDefaultCacheMillis();
    // now try to enable the push
    try {
        if (subscriber.getPort() > 0 && pushService.canEnablePush(subscriber.getAgent())) {
            subscriberServiceV1.addClient(namespaceId, serviceName, cluster, subscriber.getAgent(), new InetSocketAddress(clientIP, subscriber.getPort()), pushDataSource, StringUtils.EMPTY, StringUtils.EMPTY);
            cacheMillis = switchDomain.getPushCacheMillis(serviceName);
        }
    } catch (Exception e) {
        Loggers.SRV_LOG.error("[NACOS-API] failed to added push client {}, {}:{}", clientInfo, clientIP, subscriber.getPort(), e);
        cacheMillis = switchDomain.getDefaultCacheMillis();
    }
    if (service == null) {
        if (Loggers.SRV_LOG.isDebugEnabled()) {
            Loggers.SRV_LOG.debug("no instance to serve for service: {}", serviceName);
        }
        result.setCacheMillis(cacheMillis);
        return result;
    }
    checkIfDisabled(service);
    List<com.alibaba.nacos.naming.core.Instance> srvedIps = service.srvIPs(Arrays.asList(StringUtils.split(cluster, StringUtils.COMMA)));
    // filter ips using selector:
    if (service.getSelector() != null && StringUtils.isNotBlank(clientIP)) {
        srvedIps = selectorManager.select(service.getSelector(), clientIP, srvedIps);
    }
    if (CollectionUtils.isEmpty(srvedIps)) {
        if (Loggers.SRV_LOG.isDebugEnabled()) {
            Loggers.SRV_LOG.debug("no instance to serve for service: {}", serviceName);
        }
        result.setCacheMillis(cacheMillis);
        result.setLastRefTime(System.currentTimeMillis());
        result.setChecksum(service.getChecksum());
        return result;
    }
    long total = 0;
    Map<Boolean, List<com.alibaba.nacos.naming.core.Instance>> ipMap = new HashMap<>(2);
    ipMap.put(Boolean.TRUE, new ArrayList<>());
    ipMap.put(Boolean.FALSE, new ArrayList<>());
    for (com.alibaba.nacos.naming.core.Instance ip : srvedIps) {
        // remove disabled instance:
        if (!ip.isEnabled()) {
            continue;
        }
        ipMap.get(ip.isHealthy()).add(ip);
        total += 1;
    }
    double threshold = service.getProtectThreshold();
    List<Instance> hosts;
    if ((float) ipMap.get(Boolean.TRUE).size() / total <= threshold) {
        Loggers.SRV_LOG.warn("protect threshold reached, return all ips, service: {}", result.getName());
        result.setReachProtectionThreshold(true);
        hosts = Stream.of(Boolean.TRUE, Boolean.FALSE).map(ipMap::get).flatMap(Collection::stream).map(InstanceUtil::deepCopy).peek(instance -> instance.setHealthy(true)).collect(Collectors.toCollection(LinkedList::new));
    } else {
        result.setReachProtectionThreshold(false);
        hosts = new LinkedList<>(ipMap.get(Boolean.TRUE));
        if (!healthOnly) {
            hosts.addAll(ipMap.get(Boolean.FALSE));
        }
    }
    result.setHosts(hosts);
    result.setCacheMillis(cacheMillis);
    result.setLastRefTime(System.currentTimeMillis());
    result.setChecksum(service.getChecksum());
    return result;
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) HashMap(java.util.HashMap) InetSocketAddress(java.net.InetSocketAddress) ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) UdpPushService(com.alibaba.nacos.naming.push.UdpPushService) NacosException(com.alibaba.nacos.api.exception.NacosException) LinkedList(java.util.LinkedList) Collection(java.util.Collection) ClientInfo(com.alibaba.nacos.naming.push.v1.ClientInfo)

Aggregations

NacosException (com.alibaba.nacos.api.exception.NacosException)1 Instance (com.alibaba.nacos.api.naming.pojo.Instance)1 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)1 UdpPushService (com.alibaba.nacos.naming.push.UdpPushService)1 ClientInfo (com.alibaba.nacos.naming.push.v1.ClientInfo)1 InetSocketAddress (java.net.InetSocketAddress)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1