Search in sources :

Example 1 with ServiceStorage

use of com.alibaba.nacos.naming.core.v2.index.ServiceStorage in project nacos by alibaba.

the class DoubleWriteInstanceChangeToV1Task method getNewInstances.

private Instances getNewInstances() {
    Instances result = new Instances();
    ServiceStorage serviceStorage = ApplicationUtils.getBean(ServiceStorage.class);
    InstanceUpgradeHelper instanceUpgradeHelper = ApplicationUtils.getBean(InstanceUpgradeHelper.class);
    long currentTimeStamp = System.currentTimeMillis();
    for (Instance each : serviceStorage.getData(service).getHosts()) {
        com.alibaba.nacos.naming.core.Instance instance = instanceUpgradeHelper.toV1(each);
        instance.setLastBeat(currentTimeStamp);
        result.getInstanceList().add(instance);
    }
    return result;
}
Also used : Instances(com.alibaba.nacos.naming.core.Instances) ServiceStorage(com.alibaba.nacos.naming.core.v2.index.ServiceStorage) Instance(com.alibaba.nacos.api.naming.pojo.Instance)

Example 2 with ServiceStorage

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

Example 3 with ServiceStorage

use of com.alibaba.nacos.naming.core.v2.index.ServiceStorage in project nacos by alibaba.

the class CatalogServiceV2ImplTest method setUp.

@Before
public void setUp() {
    catalogServiceV2Impl = new CatalogServiceV2Impl(serviceStorage, metadataManager);
    ServiceManager serviceManager = ServiceManager.getInstance();
    Service service = Service.newService("A", "B", "C");
    serviceManager.getSingleton(service);
}
Also used : ServiceManager(com.alibaba.nacos.naming.core.v2.ServiceManager) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) Before(org.junit.Before)

Example 4 with ServiceStorage

use of com.alibaba.nacos.naming.core.v2.index.ServiceStorage in project nacos by alibaba.

the class ServiceStorageTest method testGetInstanceInfo.

@Test
public void testGetInstanceInfo() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
    Class<ServiceStorage> serviceStorageClass = ServiceStorage.class;
    Method getInstanceInfo = serviceStorageClass.getDeclaredMethod("getInstanceInfo", String.class, Service.class);
    getInstanceInfo.setAccessible(true);
    Optional<InstancePublishInfo> optionalInstancePublishInfo = (Optional<InstancePublishInfo>) getInstanceInfo.invoke(serviceStorage, NACOS, SERVICE);
    Assert.assertFalse(optionalInstancePublishInfo.isPresent());
}
Also used : Optional(java.util.Optional) InstancePublishInfo(com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo) Method(java.lang.reflect.Method) Test(org.junit.Test)

Example 5 with ServiceStorage

use of com.alibaba.nacos.naming.core.v2.index.ServiceStorage in project nacos by alibaba.

the class ServiceStorageTest method setUp.

@Before
public void setUp() throws NoSuchFieldException, IllegalAccessException {
    serviceStorage = new ServiceStorage(clientServiceIndexesManager, clientManagerDelegate, switchDomain, namingMetadataManager);
    Field serviceClusterIndex = ServiceStorage.class.getDeclaredField("serviceClusterIndex");
    serviceClusterIndex.setAccessible(true);
    ConcurrentMap<Service, Set<String>> serviceSetConcurrentMap = (ConcurrentMap<Service, Set<String>>) serviceClusterIndex.get(serviceStorage);
    serviceSetConcurrentMap.put(SERVICE, new HashSet<>(Collections.singletonList(NACOS)));
    Field serviceDataIndexes = ServiceStorage.class.getDeclaredField("serviceDataIndexes");
    serviceDataIndexes.setAccessible(true);
    ConcurrentMap<Service, ServiceInfo> infoConcurrentMap = (ConcurrentMap<Service, ServiceInfo>) serviceDataIndexes.get(serviceStorage);
    infoConcurrentMap.put(SERVICE, serviceInfo);
}
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) Before(org.junit.Before)

Aggregations

ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)4 Service (com.alibaba.nacos.naming.core.v2.pojo.Service)4 ServiceStorage (com.alibaba.nacos.naming.core.v2.index.ServiceStorage)3 Field (java.lang.reflect.Field)3 Before (org.junit.Before)3 Test (org.junit.Test)3 Instance (com.alibaba.nacos.api.naming.pojo.Instance)2 ServiceManager (com.alibaba.nacos.naming.core.v2.ServiceManager)2 ServiceChangeV1Task (com.alibaba.nacos.naming.core.v2.upgrade.doublewrite.delay.ServiceChangeV1Task)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 Serializer (com.alibaba.nacos.consistency.Serializer)1 Response (com.alibaba.nacos.consistency.entity.Response)1 WriteRequest (com.alibaba.nacos.consistency.entity.WriteRequest)1 InstanceOperatorClientImpl (com.alibaba.nacos.naming.core.InstanceOperatorClientImpl)1 Instances (com.alibaba.nacos.naming.core.Instances)1 Service (com.alibaba.nacos.naming.core.Service)1 ServiceManager (com.alibaba.nacos.naming.core.ServiceManager)1 MetadataEvent (com.alibaba.nacos.naming.core.v2.event.metadata.MetadataEvent)1