Search in sources :

Example 1 with ConsistencyService

use of com.alibaba.nacos.naming.consistency.ConsistencyService 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 ConsistencyService

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

use of com.alibaba.nacos.naming.consistency.ConsistencyService in project nacos by alibaba.

the class ServiceManagerTest method testUpdateInstance.

@Test
public void testUpdateInstance() throws NacosException {
    serviceManager.createEmptyService(TEST_NAMESPACE, TEST_SERVICE_NAME, true);
    Service service = serviceManager.getService(TEST_NAMESPACE, TEST_SERVICE_NAME);
    service.addCluster(cluster);
    ((Set<Instance>) ReflectionTestUtils.getField(cluster, "ephemeralInstances")).add(instance);
    serviceManager.updateInstance(TEST_NAMESPACE, TEST_SERVICE_NAME, instance);
    String instanceListKey = KeyBuilder.buildInstanceListKey(TEST_NAMESPACE, TEST_SERVICE_NAME, true);
    verify(consistencyService).put(eq(instanceListKey), any(Instances.class));
}
Also used : Set(java.util.Set) ConsistencyService(com.alibaba.nacos.naming.consistency.ConsistencyService) Test(org.junit.Test) BaseTest(com.alibaba.nacos.naming.BaseTest)

Example 4 with ConsistencyService

use of com.alibaba.nacos.naming.consistency.ConsistencyService in project nacos by alibaba.

the class ServerStatusManagerTest method testGetErrorMsg.

@Test
public void testGetErrorMsg() throws NoSuchFieldException, IllegalAccessException {
    ServerStatusManager serverStatusManager = new ServerStatusManager(mock(SwitchDomain.class));
    Field field = ServerStatusManager.class.getDeclaredField("consistencyService");
    field.setAccessible(true);
    ConsistencyService consistencyService = mock(ConsistencyService.class);
    when(consistencyService.getErrorMsg()).thenReturn(Optional.empty());
    field.set(serverStatusManager, consistencyService);
    Optional<String> errorMsg = serverStatusManager.getErrorMsg();
    Assert.assertFalse(errorMsg.isPresent());
}
Also used : Field(java.lang.reflect.Field) SwitchDomain(com.alibaba.nacos.naming.misc.SwitchDomain) ConsistencyService(com.alibaba.nacos.naming.consistency.ConsistencyService) Test(org.junit.Test)

Example 5 with ConsistencyService

use of com.alibaba.nacos.naming.consistency.ConsistencyService in project nacos by alibaba.

the class ServerStatusManagerTest method testUpdaterFromConsistency1.

@Test
public void testUpdaterFromConsistency1() throws NoSuchFieldException, IllegalAccessException {
    SwitchDomain switchDomain = mock(SwitchDomain.class);
    ServerStatusManager serverStatusManager = new ServerStatusManager(switchDomain);
    Field field = ServerStatusManager.class.getDeclaredField("consistencyService");
    field.setAccessible(true);
    ConsistencyService consistencyService = mock(ConsistencyService.class);
    when(consistencyService.isAvailable()).thenReturn(true);
    field.set(serverStatusManager, consistencyService);
    ServerStatusManager.ServerStatusUpdater updater = serverStatusManager.new ServerStatusUpdater();
    // then
    updater.run();
    // then
    Assert.assertEquals(ServerStatus.UP, serverStatusManager.getServerStatus());
}
Also used : Field(java.lang.reflect.Field) SwitchDomain(com.alibaba.nacos.naming.misc.SwitchDomain) ConsistencyService(com.alibaba.nacos.naming.consistency.ConsistencyService) Test(org.junit.Test)

Aggregations

ConsistencyService (com.alibaba.nacos.naming.consistency.ConsistencyService)6 Test (org.junit.Test)4 SwitchDomain (com.alibaba.nacos.naming.misc.SwitchDomain)3 Field (java.lang.reflect.Field)3 ServiceManager (com.alibaba.nacos.naming.core.ServiceManager)2 ServiceChangeV2Task (com.alibaba.nacos.naming.core.v2.upgrade.doublewrite.delay.ServiceChangeV2Task)2 BaseTest (com.alibaba.nacos.naming.BaseTest)1 Instances (com.alibaba.nacos.naming.core.Instances)1 Set (java.util.Set)1