Search in sources :

Example 1 with ClusterMetadata

use of com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata in project nacos by alibaba.

the class HealthCheckTaskV2 method doHealthCheck.

@Override
public void doHealthCheck() {
    try {
        for (Service each : client.getAllPublishedService()) {
            if (switchDomain.isHealthCheckEnabled(each.getGroupedServiceName())) {
                InstancePublishInfo instancePublishInfo = client.getInstancePublishInfo(each);
                ClusterMetadata metadata = getClusterMetadata(each, instancePublishInfo);
                ApplicationUtils.getBean(HealthCheckProcessorV2Delegate.class).process(this, each, metadata);
                if (Loggers.EVT_LOG.isDebugEnabled()) {
                    Loggers.EVT_LOG.debug("[HEALTH-CHECK-V2] schedule health check task: {}", client.getClientId());
                }
            }
        }
    } catch (Throwable e) {
        Loggers.SRV_LOG.error("[HEALTH-CHECK-V2] error while process health check for {}", client.getClientId(), e);
    } finally {
        if (!cancelled) {
            HealthCheckReactor.scheduleCheck(this);
            // worst == 0 means never checked
            if (this.getCheckRtWorst() > 0) {
                // TLog doesn't support float so we must convert it into long
                long checkRtLastLast = getCheckRtLastLast();
                this.setCheckRtLastLast(this.getCheckRtLast());
                if (checkRtLastLast > 0) {
                    long diff = ((this.getCheckRtLast() - this.getCheckRtLastLast()) * 10000) / checkRtLastLast;
                    if (Loggers.CHECK_RT.isDebugEnabled()) {
                        Loggers.CHECK_RT.debug("{}->normalized: {}, worst: {}, best: {}, last: {}, diff: {}", client.getClientId(), this.getCheckRtNormalized(), this.getCheckRtWorst(), this.getCheckRtBest(), this.getCheckRtLast(), diff);
                    }
                }
            }
        }
    }
}
Also used : ClusterMetadata(com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata) InstancePublishInfo(com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) HealthCheckProcessorV2Delegate(com.alibaba.nacos.naming.healthcheck.v2.processor.HealthCheckProcessorV2Delegate)

Example 2 with ClusterMetadata

use of com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata in project nacos by alibaba.

the class HealthCheckTaskV2 method getClusterMetadata.

private ClusterMetadata getClusterMetadata(Service service, InstancePublishInfo instancePublishInfo) {
    Optional<ServiceMetadata> serviceMetadata = metadataManager.getServiceMetadata(service);
    if (!serviceMetadata.isPresent()) {
        return new ClusterMetadata();
    }
    String cluster = instancePublishInfo.getCluster();
    ClusterMetadata result = serviceMetadata.get().getClusters().get(cluster);
    return null == result ? new ClusterMetadata() : result;
}
Also used : ClusterMetadata(com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata) ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)

Example 3 with ClusterMetadata

use of com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata in project nacos by alibaba.

the class HttpHealthCheckProcessor method process.

@Override
public void process(HealthCheckTaskV2 task, Service service, ClusterMetadata metadata) {
    HealthCheckInstancePublishInfo instance = (HealthCheckInstancePublishInfo) task.getClient().getInstancePublishInfo(service);
    if (null == instance) {
        return;
    }
    try {
        // TODO handle marked(white list) logic like v1.x.
        if (!instance.tryStartCheck()) {
            SRV_LOG.warn("http check started before last one finished, service: {} : {} : {}:{}", service.getGroupedServiceName(), instance.getCluster(), instance.getIp(), instance.getPort());
            healthCheckCommon.reEvaluateCheckRT(task.getCheckRtNormalized() * 2, task, switchDomain.getHttpHealthParams());
            return;
        }
        Http healthChecker = (Http) metadata.getHealthChecker();
        int ckPort = metadata.isUseInstancePortForCheck() ? instance.getPort() : metadata.getHealthyCheckPort();
        URL host = new URL(HTTP_PREFIX + instance.getIp() + ":" + ckPort);
        URL target = new URL(host, healthChecker.getPath());
        Map<String, String> customHeaders = healthChecker.getCustomHeaders();
        Header header = Header.newInstance();
        header.addAll(customHeaders);
        ASYNC_REST_TEMPLATE.get(target.toString(), header, Query.EMPTY, String.class, new HttpHealthCheckCallback(instance, task, service));
        MetricsMonitor.getHttpHealthCheckMonitor().incrementAndGet();
    } catch (Throwable e) {
        instance.setCheckRt(switchDomain.getHttpHealthParams().getMax());
        healthCheckCommon.checkFail(task, service, "http:error:" + e.getMessage());
        healthCheckCommon.reEvaluateCheckRT(switchDomain.getHttpHealthParams().getMax(), task, switchDomain.getHttpHealthParams());
    }
}
Also used : Header(com.alibaba.nacos.common.http.param.Header) Http(com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http) HealthCheckInstancePublishInfo(com.alibaba.nacos.naming.core.v2.pojo.HealthCheckInstancePublishInfo) URL(java.net.URL)

Example 4 with ClusterMetadata

use of com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata in project nacos by alibaba.

the class ClusterController method update.

/**
 * Update cluster.
 *
 * @param request http request
 * @return 'ok' if success
 * @throws Exception if failed
 */
@PutMapping
@Secured(action = ActionTypes.WRITE)
public String update(HttpServletRequest request) throws Exception {
    final String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
    final String clusterName = WebUtils.required(request, CommonParams.CLUSTER_NAME);
    final String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
    ClusterMetadata clusterMetadata = new ClusterMetadata();
    clusterMetadata.setHealthyCheckPort(NumberUtils.toInt(WebUtils.required(request, "checkPort")));
    clusterMetadata.setUseInstancePortForCheck(ConvertUtils.toBoolean(WebUtils.required(request, "useInstancePort4Check")));
    AbstractHealthChecker healthChecker = HealthCheckerFactory.deserialize(WebUtils.required(request, "healthChecker"));
    clusterMetadata.setHealthChecker(healthChecker);
    clusterMetadata.setHealthyCheckType(healthChecker.getType());
    clusterMetadata.setExtendData(UtilsAndCommons.parseMetadata(WebUtils.optional(request, "metadata", StringUtils.EMPTY)));
    judgeClusterOperator().updateClusterMetadata(namespaceId, serviceName, clusterName, clusterMetadata);
    return "ok";
}
Also used : ClusterMetadata(com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata) AbstractHealthChecker(com.alibaba.nacos.api.naming.pojo.healthcheck.AbstractHealthChecker) Secured(com.alibaba.nacos.auth.annotation.Secured) PutMapping(org.springframework.web.bind.annotation.PutMapping)

Example 5 with ClusterMetadata

use of com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata in project nacos by alibaba.

the class TcpHealthCheckProcessor method process.

@Override
public void process(HealthCheckTaskV2 task, Service service, ClusterMetadata metadata) {
    HealthCheckInstancePublishInfo instance = (HealthCheckInstancePublishInfo) task.getClient().getInstancePublishInfo(service);
    if (null == instance) {
        return;
    }
    // TODO handle marked(white list) logic like v1.x.
    if (!instance.tryStartCheck()) {
        SRV_LOG.warn("[HEALTH-CHECK-V2] tcp check started before last one finished, service: {} : {} : {}:{}", service.getGroupedServiceName(), instance.getCluster(), instance.getIp(), instance.getPort());
        healthCheckCommon.reEvaluateCheckRT(task.getCheckRtNormalized() * 2, task, switchDomain.getTcpHealthParams());
        return;
    }
    taskQueue.add(new Beat(task, service, metadata, instance));
    MetricsMonitor.getTcpHealthCheckMonitor().incrementAndGet();
}
Also used : HealthCheckInstancePublishInfo(com.alibaba.nacos.naming.core.v2.pojo.HealthCheckInstancePublishInfo)

Aggregations

ClusterMetadata (com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata)12 ServiceMetadata (com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)7 Service (com.alibaba.nacos.naming.core.v2.pojo.Service)6 NacosException (com.alibaba.nacos.api.exception.NacosException)4 NamingMetadataOperateService (com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService)3 HealthCheckInstancePublishInfo (com.alibaba.nacos.naming.core.v2.pojo.HealthCheckInstancePublishInfo)3 InstancePublishInfo (com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo)3 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)3 HashMap (java.util.HashMap)3 Test (org.junit.Test)3 IpPortBasedClient (com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient)2 Cluster (com.alibaba.nacos.api.naming.pojo.Cluster)1 Instance (com.alibaba.nacos.api.naming.pojo.Instance)1 AbstractHealthChecker (com.alibaba.nacos.api.naming.pojo.healthcheck.AbstractHealthChecker)1 Http (com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http)1 Secured (com.alibaba.nacos.auth.annotation.Secured)1 Header (com.alibaba.nacos.common.http.param.Header)1 Cluster (com.alibaba.nacos.naming.core.Cluster)1 Service (com.alibaba.nacos.naming.core.Service)1 Client (com.alibaba.nacos.naming.core.v2.client.Client)1