Search in sources :

Example 1 with Cluster

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

the class DefaultServiceMetadataUpgradeHelper method toV2ServiceMetadata.

@Override
public ServiceMetadata toV2ServiceMetadata(Service service, boolean ephemeral) {
    ServiceMetadata result = new ServiceMetadata();
    result.setEphemeral(ephemeral);
    result.setProtectThreshold(service.getProtectThreshold());
    result.setSelector(service.getSelector());
    result.setExtendData(service.getMetadata());
    for (Map.Entry<String, Cluster> entry : service.getClusterMap().entrySet()) {
        result.getClusters().put(entry.getKey(), toV2ClusterMetadata(entry.getValue()));
    }
    return result;
}
Also used : Cluster(com.alibaba.nacos.naming.core.Cluster) Map(java.util.Map) ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)

Example 2 with Cluster

use of com.alibaba.nacos.naming.core.Cluster 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 3 with Cluster

use of com.alibaba.nacos.naming.core.Cluster 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 4 with Cluster

use of com.alibaba.nacos.naming.core.Cluster 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 5 with Cluster

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

the class ClusterControllerTest method testUpdate.

@Test
public void testUpdate() throws Exception {
    Service service = new Service(TEST_SERVICE_NAME);
    service.setNamespaceId("test-namespace");
    when(serviceManager.getService(Constants.DEFAULT_NAMESPACE_ID, TEST_SERVICE_NAME)).thenReturn(service);
    MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.put(UtilsAndCommons.NACOS_NAMING_CONTEXT + "/cluster").param("clusterName", TEST_CLUSTER_NAME).param("serviceName", TEST_SERVICE_NAME).param("healthChecker", "{\"type\":\"HTTP\"}").param("checkPort", "1").param("useInstancePort4Check", "true");
    Assert.assertEquals("ok", mockmvc.perform(builder).andReturn().getResponse().getContentAsString());
    Cluster expectedCluster = new Cluster(TEST_CLUSTER_NAME, service);
    Cluster actualCluster = service.getClusterMap().get(TEST_CLUSTER_NAME);
    Assert.assertEquals(expectedCluster, actualCluster);
    Assert.assertEquals(1, actualCluster.getDefCkport());
    Assert.assertTrue(actualCluster.isUseIPPort4Check());
}
Also used : MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) Service(com.alibaba.nacos.naming.core.Service) Cluster(com.alibaba.nacos.naming.core.Cluster) Test(org.junit.Test) BaseTest(com.alibaba.nacos.naming.BaseTest)

Aggregations

Cluster (com.alibaba.nacos.naming.core.Cluster)14 Service (com.alibaba.nacos.naming.core.Service)10 Instance (com.alibaba.nacos.naming.core.Instance)5 UdpPushService (com.alibaba.nacos.naming.push.UdpPushService)4 BaseTest (com.alibaba.nacos.naming.BaseTest)3 Test (org.junit.Test)3 MockHttpServletRequestBuilder (org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2 AbstractHealthChecker (com.alibaba.nacos.api.naming.pojo.healthcheck.AbstractHealthChecker)1 Http (com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http)1 Header (com.alibaba.nacos.common.http.param.Header)1 CatalogServiceV1Impl (com.alibaba.nacos.naming.core.CatalogServiceV1Impl)1 ClusterMetadata (com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata)1 ServiceMetadata (com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)1 UpgradeJudgement (com.alibaba.nacos.naming.core.v2.upgrade.UpgradeJudgement)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 URL (java.net.URL)1 Before (org.junit.Before)1