Search in sources :

Example 1 with Service

use of com.alibaba.nacos.naming.core.Service in project nacos by alibaba.

the class ClientBeatProcessor method run.

@Override
public void run() {
    Service service = this.service;
    if (Loggers.EVT_LOG.isDebugEnabled()) {
        Loggers.EVT_LOG.debug("[CLIENT-BEAT] processing beat: {}", rsInfo.toString());
    }
    String ip = rsInfo.getIp();
    String clusterName = rsInfo.getCluster();
    int port = rsInfo.getPort();
    Cluster cluster = service.getClusterMap().get(clusterName);
    List<Instance> instances = cluster.allIPs(true);
    for (Instance instance : instances) {
        if (instance.getIp().equals(ip) && instance.getPort() == port) {
            if (Loggers.EVT_LOG.isDebugEnabled()) {
                Loggers.EVT_LOG.debug("[CLIENT-BEAT] refresh beat: {}", rsInfo.toString());
            }
            instance.setLastBeat(System.currentTimeMillis());
            if (!instance.isMarked() && !instance.isHealthy()) {
                instance.setHealthy(true);
                Loggers.EVT_LOG.info("service: {} {POS} {IP-ENABLED} valid: {}:{}@{}, region: {}, msg: client beat ok", cluster.getService().getName(), ip, port, cluster.getName(), UtilsAndCommons.LOCALHOST_SITE);
                getPushService().serviceChanged(service);
            }
        }
    }
}
Also used : Instance(com.alibaba.nacos.naming.core.Instance) UdpPushService(com.alibaba.nacos.naming.push.UdpPushService) Service(com.alibaba.nacos.naming.core.Service) Cluster(com.alibaba.nacos.naming.core.Cluster)

Example 2 with Service

use of com.alibaba.nacos.naming.core.Service in project nacos by alibaba.

the class HealthCheckCommon method checkFail.

/**
 * Health check fail, when instance check failed count more than max failed time, set unhealthy.
 *
 * @param ip   instance
 * @param task health check task
 * @param msg  message
 */
public void checkFail(Instance ip, HealthCheckTask task, String msg) {
    Cluster cluster = task.getCluster();
    try {
        if (ip.isHealthy() || ip.isMockValid()) {
            if (ip.getFailCount().incrementAndGet() >= switchDomain.getCheckTimes()) {
                if (distroMapper.responsible(cluster, ip)) {
                    ip.setHealthy(false);
                    ip.setMockValid(false);
                    Service service = cluster.getService();
                    service.setLastModifiedMillis(System.currentTimeMillis());
                    pushService.serviceChanged(service);
                    Loggers.EVT_LOG.info("serviceName: {} {POS} {IP-DISABLED} invalid: {}:{}@{}, region: {}, msg: {}", cluster.getService().getName(), ip.getIp(), ip.getPort(), cluster.getName(), UtilsAndCommons.LOCALHOST_SITE, msg);
                } else {
                    Loggers.EVT_LOG.info("serviceName: {} {PROBE} {IP-DISABLED} invalid: {}:{}@{}, region: {}, msg: {}", cluster.getService().getName(), ip.getIp(), ip.getPort(), cluster.getName(), UtilsAndCommons.LOCALHOST_SITE, msg);
                }
            } else {
                Loggers.EVT_LOG.info("serviceName: {} {OTHER} {IP-DISABLED} pre-invalid: {}:{}@{} in {}, msg: {}", cluster.getService().getName(), ip.getIp(), ip.getPort(), cluster.getName(), ip.getFailCount(), msg);
            }
        }
    } catch (Throwable t) {
        Loggers.SRV_LOG.error("[CHECK-FAIL] error when close check task.", t);
    }
    ip.getOkCount().set(0);
    ip.setBeingChecked(false);
}
Also used : Cluster(com.alibaba.nacos.naming.core.Cluster) UdpPushService(com.alibaba.nacos.naming.push.UdpPushService) Service(com.alibaba.nacos.naming.core.Service)

Example 3 with Service

use of com.alibaba.nacos.naming.core.Service in project nacos by alibaba.

the class HealthCheckCommon method checkFailNow.

/**
 * Health check fail, set instance unhealthy directly.
 *
 * @param ip   instance
 * @param task health check task
 * @param msg  message
 */
public void checkFailNow(Instance ip, HealthCheckTask task, String msg) {
    Cluster cluster = task.getCluster();
    try {
        if (ip.isHealthy() || ip.isMockValid()) {
            if (distroMapper.responsible(cluster, ip)) {
                ip.setHealthy(false);
                ip.setMockValid(false);
                Service service = cluster.getService();
                service.setLastModifiedMillis(System.currentTimeMillis());
                pushService.serviceChanged(service);
                Loggers.EVT_LOG.info("serviceName: {} {POS} {IP-DISABLED} invalid-now: {}:{}@{}, region: {}, msg: {}", cluster.getService().getName(), ip.getIp(), ip.getPort(), cluster.getName(), UtilsAndCommons.LOCALHOST_SITE, msg);
            } else {
                if (ip.isMockValid()) {
                    ip.setMockValid(false);
                    Loggers.EVT_LOG.info("serviceName: {} {PROBE} {IP-DISABLED} invalid-now: {}:{}@{}, region: {}, msg: {}", cluster.getService().getName(), ip.getIp(), ip.getPort(), cluster.getName(), UtilsAndCommons.LOCALHOST_SITE, msg);
                }
            }
        }
    } catch (Throwable t) {
        Loggers.SRV_LOG.error("[CHECK-FAIL-NOW] error when close check task.", t);
    }
    ip.getOkCount().set(0);
    ip.setBeingChecked(false);
}
Also used : Cluster(com.alibaba.nacos.naming.core.Cluster) UdpPushService(com.alibaba.nacos.naming.push.UdpPushService) Service(com.alibaba.nacos.naming.core.Service)

Example 4 with Service

use of com.alibaba.nacos.naming.core.Service in project nacos by alibaba.

the class AsyncServicesCheckTask method run.

@Override
public void run() {
    if (upgradeJudgement.isUseGrpcFeatures()) {
        return;
    }
    try {
        ServiceManager serviceManager = ApplicationUtils.getBean(ServiceManager.class);
        ServiceStorage serviceStorage = ApplicationUtils.getBean(ServiceStorage.class);
        Map<String, Service> v1Services = new HashMap<>(INITIALCAPACITY);
        for (String each : serviceManager.getAllNamespaces()) {
            for (Map.Entry<String, Service> entry : serviceManager.chooseServiceMap(each).entrySet()) {
                v1Services.put(buildServiceKey(each, entry.getKey()), entry.getValue());
                checkService(each, entry.getKey(), entry.getValue(), serviceStorage);
            }
        }
        Map<String, com.alibaba.nacos.naming.core.v2.pojo.Service> v2Services = new HashMap<>(INITIALCAPACITY);
        for (String each : com.alibaba.nacos.naming.core.v2.ServiceManager.getInstance().getAllNamespaces()) {
            for (com.alibaba.nacos.naming.core.v2.pojo.Service serviceV2 : com.alibaba.nacos.naming.core.v2.ServiceManager.getInstance().getSingletons(each)) {
                v2Services.put(buildServiceKey(each, serviceV2.getGroupedServiceName()), serviceV2);
            }
        }
        // only check v2 services when upgrading.
        v2Services.keySet().removeIf(v1Services::containsKey);
        if (v2Services.isEmpty()) {
            return;
        }
        if (Loggers.SRV_LOG.isDebugEnabled()) {
            Loggers.SRV_LOG.debug("{} service in v2 to removed.", v2Services.size());
        }
        for (com.alibaba.nacos.naming.core.v2.pojo.Service service : v2Services.values()) {
            deleteV2Service(service);
        }
    } catch (Exception e) {
        Loggers.SRV_LOG.warn("async check for service error", e);
    }
}
Also used : HashMap(java.util.HashMap) Service(com.alibaba.nacos.naming.core.Service) ServiceStorage(com.alibaba.nacos.naming.core.v2.index.ServiceStorage) ServiceManager(com.alibaba.nacos.naming.core.ServiceManager) HashMap(java.util.HashMap) Map(java.util.Map)

Example 5 with Service

use of com.alibaba.nacos.naming.core.Service in project nacos by alibaba.

the class ServiceChangeEventTest method testGetService.

@Test
public void testGetService() {
    ServiceChangeEvent serviceChangeEvent = new ServiceChangeEvent(object, service);
    Service service = serviceChangeEvent.getService();
    Assert.assertNotNull(service);
    Assert.assertEquals(service, service);
}
Also used : Service(com.alibaba.nacos.naming.core.Service) Test(org.junit.Test)

Aggregations

Service (com.alibaba.nacos.naming.core.Service)28 Cluster (com.alibaba.nacos.naming.core.Cluster)10 Test (org.junit.Test)10 HashMap (java.util.HashMap)7 Instance (com.alibaba.nacos.naming.core.Instance)5 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 Map (java.util.Map)5 NacosException (com.alibaba.nacos.api.exception.NacosException)4 BaseTest (com.alibaba.nacos.naming.BaseTest)4 UdpPushService (com.alibaba.nacos.naming.push.UdpPushService)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)4 Datum (com.alibaba.nacos.naming.consistency.Datum)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 Instances (com.alibaba.nacos.naming.core.Instances)2 ServiceManager (com.alibaba.nacos.naming.core.ServiceManager)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2