Search in sources :

Example 96 with Service

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

the class DoubleWriteServiceRemovalToV2Task method run.

@Override
public void run() {
    try {
        InstanceOperatorClientImpl instanceOperator = ApplicationUtils.getBean(InstanceOperatorClientImpl.class);
        ClientServiceIndexesManager clientServiceIndexesManager = ApplicationUtils.getBean(ClientServiceIndexesManager.class);
        ServiceStorage serviceStorage = ApplicationUtils.getBean(ServiceStorage.class);
        ServiceInfo serviceInfo = serviceStorage.getPushData(service);
        for (Instance instance : serviceInfo.getHosts()) {
            instanceOperator.removeInstance(service.getNamespace(), service.getName(), instance);
        }
        int count = 0;
        while (!clientServiceIndexesManager.getAllClientsRegisteredService(service).isEmpty() && count < MAX_WAIT_TIMES) {
            TimeUnit.MILLISECONDS.sleep(100);
            count += 1;
        }
        clientServiceIndexesManager.removePublisherIndexesByEmptyService(service);
        ServiceManager.getInstance().removeSingleton(service);
        serviceStorage.removeData(service);
        NotifyCenter.publishEvent(new MetadataEvent.ServiceMetadataEvent(service, true));
    } catch (Exception e) {
        if (Loggers.SRV_LOG.isDebugEnabled()) {
            Loggers.SRV_LOG.debug("Double write removal of {} from 1 to 2 failed", service, e);
        }
        ServiceChangeV1Task retryTask = new ServiceChangeV1Task(service.getNamespace(), service.getGroupedServiceName(), service.isEphemeral(), DoubleWriteContent.BOTH, DoubleWriteAction.REMOVE);
        retryTask.setTaskInterval(INTERVAL);
        String taskKey = ServiceChangeV1Task.getKey(service.getNamespace(), service.getGroupedServiceName(), service.isEphemeral());
        ApplicationUtils.getBean(DoubleWriteDelayTaskEngine.class).addTask(taskKey, retryTask);
    }
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) ServiceStorage(com.alibaba.nacos.naming.core.v2.index.ServiceStorage) Instance(com.alibaba.nacos.api.naming.pojo.Instance) MetadataEvent(com.alibaba.nacos.naming.core.v2.event.metadata.MetadataEvent) ClientServiceIndexesManager(com.alibaba.nacos.naming.core.v2.index.ClientServiceIndexesManager) ServiceChangeV1Task(com.alibaba.nacos.naming.core.v2.upgrade.doublewrite.delay.ServiceChangeV1Task) InstanceOperatorClientImpl(com.alibaba.nacos.naming.core.InstanceOperatorClientImpl)

Example 97 with Service

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

the class HealthCheckCommonV2 method checkFailNow.

/**
 * Health check fail, set instance unhealthy directly.
 *
 * @param task    health check task
 * @param service service
 * @param msg     message
 */
public void checkFailNow(HealthCheckTaskV2 task, Service service, String msg) {
    try {
        HealthCheckInstancePublishInfo instance = (HealthCheckInstancePublishInfo) task.getClient().getInstancePublishInfo(service);
        if (null == instance) {
            return;
        }
        try {
            if (instance.isHealthy()) {
                String serviceName = service.getGroupedServiceName();
                String clusterName = instance.getCluster();
                if (switchDomain.isHealthCheckEnabled(serviceName) && !task.isCancelled() && distroMapper.responsible(task.getClient().getResponsibleId())) {
                    healthStatusSynchronizer.instanceHealthStatusChange(false, task.getClient(), service, instance);
                    Loggers.EVT_LOG.info("serviceName: {} {POS} {IP-DISABLED} invalid: {}:{}@{}, region: {}, msg: {}", serviceName, instance.getIp(), instance.getPort(), clusterName, UtilsAndCommons.LOCALHOST_SITE, msg);
                }
            }
        } finally {
            instance.resetOkCount();
            instance.finishCheck();
        }
    } catch (Throwable t) {
        Loggers.SRV_LOG.error("[CHECK-FAIL] error when close check task.", t);
    }
}
Also used : HealthCheckInstancePublishInfo(com.alibaba.nacos.naming.core.v2.pojo.HealthCheckInstancePublishInfo)

Example 98 with Service

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

the class HealthCheckCommonV2 method checkFail.

/**
 * Health check fail, when instance check failed count more than max failed time, set unhealthy.
 *
 * @param task    health check task
 * @param service service
 * @param msg     message
 */
public void checkFail(HealthCheckTaskV2 task, Service service, String msg) {
    try {
        HealthCheckInstancePublishInfo instance = (HealthCheckInstancePublishInfo) task.getClient().getInstancePublishInfo(service);
        if (instance == null) {
            return;
        }
        try {
            if (instance.isHealthy()) {
                String serviceName = service.getGroupedServiceName();
                String clusterName = instance.getCluster();
                if (instance.getFailCount().incrementAndGet() >= switchDomain.getCheckTimes()) {
                    if (switchDomain.isHealthCheckEnabled(serviceName) && !task.isCancelled() && distroMapper.responsible(task.getClient().getResponsibleId())) {
                        healthStatusSynchronizer.instanceHealthStatusChange(false, task.getClient(), service, instance);
                        Loggers.EVT_LOG.info("serviceName: {} {POS} {IP-DISABLED} invalid: {}:{}@{}, region: {}, msg: {}", serviceName, instance.getIp(), instance.getPort(), clusterName, UtilsAndCommons.LOCALHOST_SITE, msg);
                    }
                } else {
                    Loggers.EVT_LOG.info("serviceName: {} {OTHER} {IP-DISABLED} pre-invalid: {}:{}@{} in {}, msg: {}", serviceName, instance.getIp(), instance.getPort(), clusterName, instance.getFailCount(), msg);
                }
            }
        } finally {
            instance.resetOkCount();
            instance.finishCheck();
        }
    } catch (Throwable t) {
        Loggers.SRV_LOG.error("[CHECK-FAIL] error when close check task.", t);
    }
}
Also used : HealthCheckInstancePublishInfo(com.alibaba.nacos.naming.core.v2.pojo.HealthCheckInstancePublishInfo)

Example 99 with Service

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

the class HealthCheckCommonV2 method checkOk.

/**
 * Health check pass.
 *
 * @param task    health check task
 * @param service service
 * @param msg     message
 */
public void checkOk(HealthCheckTaskV2 task, Service service, String msg) {
    try {
        HealthCheckInstancePublishInfo instance = (HealthCheckInstancePublishInfo) task.getClient().getInstancePublishInfo(service);
        if (instance == null) {
            return;
        }
        try {
            if (!instance.isHealthy()) {
                String serviceName = service.getGroupedServiceName();
                String clusterName = instance.getCluster();
                if (instance.getOkCount().incrementAndGet() >= switchDomain.getCheckTimes()) {
                    if (switchDomain.isHealthCheckEnabled(serviceName) && !task.isCancelled() && distroMapper.responsible(task.getClient().getResponsibleId())) {
                        healthStatusSynchronizer.instanceHealthStatusChange(true, task.getClient(), service, instance);
                        Loggers.EVT_LOG.info("serviceName: {} {POS} {IP-ENABLED} valid: {}:{}@{}, region: {}, msg: {}", serviceName, instance.getIp(), instance.getPort(), clusterName, UtilsAndCommons.LOCALHOST_SITE, msg);
                    }
                } else {
                    Loggers.EVT_LOG.info("serviceName: {} {OTHER} {IP-ENABLED} pre-valid: {}:{}@{} in {}, msg: {}", serviceName, instance.getIp(), instance.getPort(), clusterName, instance.getOkCount(), msg);
                }
            }
        } finally {
            instance.resetFailCount();
            instance.finishCheck();
        }
    } catch (Throwable t) {
        Loggers.SRV_LOG.error("[CHECK-OK] error when close check task.", t);
    }
}
Also used : HealthCheckInstancePublishInfo(com.alibaba.nacos.naming.core.v2.pojo.HealthCheckInstancePublishInfo)

Example 100 with Service

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

the class ServiceStorageTest method testRemoveData.

@Test
public void testRemoveData() throws NoSuchFieldException, IllegalAccessException {
    serviceStorage.removeData(SERVICE);
    Field serviceClusterIndex = ServiceStorage.class.getDeclaredField("serviceClusterIndex");
    serviceClusterIndex.setAccessible(true);
    ConcurrentMap<Service, Set<String>> serviceSetConcurrentMap = (ConcurrentMap<Service, Set<String>>) serviceClusterIndex.get(serviceStorage);
    Field serviceDataIndexes = ServiceStorage.class.getDeclaredField("serviceDataIndexes");
    serviceDataIndexes.setAccessible(true);
    ConcurrentMap<Service, ServiceInfo> infoConcurrentMap = (ConcurrentMap<Service, ServiceInfo>) serviceDataIndexes.get(serviceStorage);
    Assert.assertEquals(serviceSetConcurrentMap.size(), 0);
    Assert.assertEquals(infoConcurrentMap.size(), 0);
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Field(java.lang.reflect.Field) Set(java.util.Set) HashSet(java.util.HashSet) ConcurrentMap(java.util.concurrent.ConcurrentMap) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) Test(org.junit.Test)

Aggregations

Service (com.alibaba.nacos.naming.core.v2.pojo.Service)75 ServiceMetadata (com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)20 NamingMetadataOperateService (com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService)19 ClientOperationService (com.alibaba.nacos.naming.core.v2.service.ClientOperationService)17 Test (org.junit.Test)16 Instance (com.alibaba.nacos.api.naming.pojo.Instance)13 InstancePublishInfo (com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo)13 UdpPushService (com.alibaba.nacos.naming.push.UdpPushService)13 NacosException (com.alibaba.nacos.api.exception.NacosException)11 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)11 HealthCheckInstancePublishInfo (com.alibaba.nacos.naming.core.v2.pojo.HealthCheckInstancePublishInfo)11 HashSet (java.util.HashSet)11 Secured (com.alibaba.nacos.auth.annotation.Secured)10 Before (org.junit.Before)10 Client (com.alibaba.nacos.naming.core.v2.client.Client)9 ClientOperationEvent (com.alibaba.nacos.naming.core.v2.event.client.ClientOperationEvent)8 ClusterMetadata (com.alibaba.nacos.naming.core.v2.metadata.ClusterMetadata)8 InstanceMetadata (com.alibaba.nacos.naming.core.v2.metadata.InstanceMetadata)8 IpPortBasedClient (com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient)7 Set (java.util.Set)7