Search in sources :

Example 1 with ServiceManager

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

the class DoubleWriteInstanceChangeToV1Task method run.

@Override
public void run() {
    try {
        ServiceManager serviceManager = ApplicationUtils.getBean(ServiceManager.class);
        com.alibaba.nacos.naming.core.Service serviceV1 = serviceManager.getService(service.getNamespace(), service.getGroupedServiceName());
        if (null == serviceV1) {
            serviceManager.createEmptyService(service.getNamespace(), service.getGroupedServiceName(), service.isEphemeral());
        }
        Instances newInstances = getNewInstances();
        String key = KeyBuilder.buildInstanceListKey(service.getNamespace(), service.getGroupedServiceName(), service.isEphemeral());
        ConsistencyService consistencyService = ApplicationUtils.getBean(NAME, ConsistencyService.class);
        consistencyService.put(key, newInstances);
    } catch (Exception e) {
        if (Loggers.SRV_LOG.isDebugEnabled()) {
            Loggers.SRV_LOG.debug("Double write task for {} instance from 2 to 1 failed", service, e);
        }
        ServiceChangeV2Task retryTask = new ServiceChangeV2Task(service, DoubleWriteContent.INSTANCE);
        retryTask.setTaskInterval(INTERVAL);
        String taskKey = ServiceChangeV2Task.getKey(service);
        ApplicationUtils.getBean(DoubleWriteDelayTaskEngine.class).addTask(taskKey, retryTask);
    }
}
Also used : Instances(com.alibaba.nacos.naming.core.Instances) ServiceManager(com.alibaba.nacos.naming.core.ServiceManager) ServiceChangeV2Task(com.alibaba.nacos.naming.core.v2.upgrade.doublewrite.delay.ServiceChangeV2Task) ConsistencyService(com.alibaba.nacos.naming.consistency.ConsistencyService)

Example 2 with ServiceManager

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

the class DoubleWriteMetadataChangeToV1Task method run.

@Override
public void run() {
    try {
        NamingMetadataManager metadataManager = ApplicationUtils.getBean(NamingMetadataManager.class);
        Optional<ServiceMetadata> serviceMetadata = metadataManager.getServiceMetadata(service);
        if (!serviceMetadata.isPresent()) {
            return;
        }
        ServiceManager serviceManager = ApplicationUtils.getBean(ServiceManager.class);
        com.alibaba.nacos.naming.core.Service serviceV1 = newServiceForV1(serviceManager, serviceMetadata.get());
        serviceManager.addOrReplaceService(serviceV1);
    } catch (Exception e) {
        if (Loggers.SRV_LOG.isDebugEnabled()) {
            Loggers.SRV_LOG.debug("Double write task for {} metadata from 2 to 1 failed", service, e);
        }
        ServiceChangeV2Task retryTask = new ServiceChangeV2Task(service, DoubleWriteContent.METADATA);
        retryTask.setTaskInterval(INTERVAL);
        String taskKey = ServiceChangeV2Task.getKey(service);
        ApplicationUtils.getBean(DoubleWriteDelayTaskEngine.class).addTask(taskKey, retryTask);
    }
}
Also used : ServiceManager(com.alibaba.nacos.naming.core.ServiceManager) ServiceChangeV2Task(com.alibaba.nacos.naming.core.v2.upgrade.doublewrite.delay.ServiceChangeV2Task) ServiceMetadata(com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata) NamingMetadataManager(com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataManager)

Example 3 with ServiceManager

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

the class DoubleWriteServiceRemovalToV1Task method run.

@Override
public void run() {
    try {
        ServiceManager serviceManager = ApplicationUtils.getBean(ServiceManager.class);
        com.alibaba.nacos.naming.core.Service serviceV1 = serviceManager.getService(service.getNamespace(), service.getGroupedServiceName());
        if (serviceV1 == null) {
            if (Loggers.SRV_LOG.isDebugEnabled()) {
                Loggers.SRV_LOG.debug("Double write task is removing a non-exist service: {}", service);
            }
            return;
        }
        ConsistencyService consistencyService = ApplicationUtils.getBean("consistencyDelegate", ConsistencyService.class);
        // remove instances
        String instanceListKey = KeyBuilder.buildInstanceListKey(service.getNamespace(), service.getGroupedServiceName(), service.isEphemeral());
        consistencyService.remove(instanceListKey);
        // remove metadata
        serviceManager.easyRemoveService(service.getNamespace(), service.getGroupedServiceName());
    } catch (Exception e) {
        if (Loggers.SRV_LOG.isDebugEnabled()) {
            Loggers.SRV_LOG.debug("Double write task for removal of {} from 2 to 1 failed", service, e);
        }
        ServiceChangeV2Task retryTask = new ServiceChangeV2Task(service, DoubleWriteContent.BOTH, DoubleWriteAction.REMOVE);
        retryTask.setTaskInterval(INTERVAL);
        String taskKey = ServiceChangeV2Task.getKey(service);
        ApplicationUtils.getBean(DoubleWriteDelayTaskEngine.class).addTask(taskKey, retryTask);
    }
}
Also used : ServiceManager(com.alibaba.nacos.naming.core.ServiceManager) ServiceChangeV2Task(com.alibaba.nacos.naming.core.v2.upgrade.doublewrite.delay.ServiceChangeV2Task) ConsistencyService(com.alibaba.nacos.naming.consistency.ConsistencyService)

Example 4 with ServiceManager

use of com.alibaba.nacos.naming.core.ServiceManager 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)

Aggregations

ServiceManager (com.alibaba.nacos.naming.core.ServiceManager)4 ServiceChangeV2Task (com.alibaba.nacos.naming.core.v2.upgrade.doublewrite.delay.ServiceChangeV2Task)3 ConsistencyService (com.alibaba.nacos.naming.consistency.ConsistencyService)2 Instances (com.alibaba.nacos.naming.core.Instances)1 Service (com.alibaba.nacos.naming.core.Service)1 ServiceStorage (com.alibaba.nacos.naming.core.v2.index.ServiceStorage)1 NamingMetadataManager (com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataManager)1 ServiceMetadata (com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1