Search in sources :

Example 26 with ServiceInfo

use of com.alibaba.nacos.api.naming.pojo.ServiceInfo 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)

Example 27 with ServiceInfo

use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.

the class ServiceStorageTest method testGetPushData.

@Test()
public void testGetPushData() {
    ServiceInfo pushData = serviceStorage.getPushData(SERVICE);
    Mockito.verify(switchDomain).getDefaultPushCacheMillis();
    Assert.assertNotNull(pushData);
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Test(org.junit.Test)

Example 28 with ServiceInfo

use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.

the class ServiceOperatorV2ImplTest method testDelete.

@Test
public void testDelete() throws NacosException {
    ServiceInfo serviceInfo = new ServiceInfo();
    serviceInfo.setHosts(Collections.emptyList());
    Mockito.when(serviceStorage.getPushData(Mockito.any())).thenReturn(serviceInfo);
    serviceOperatorV2.delete("A", "C");
    Mockito.verify(metadataOperateService).deleteServiceMetadata(Mockito.any());
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Test(org.junit.Test)

Example 29 with ServiceInfo

use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.

the class DiskCache method read.

/**
 * Read service info from disk.
 *
 * @param cacheDir cache file dir
 * @return service infos
 */
public static Map<String, ServiceInfo> read(String cacheDir) {
    Map<String, ServiceInfo> domMap = new HashMap<String, ServiceInfo>(16);
    BufferedReader reader = null;
    try {
        File[] files = makeSureCacheDirExists(cacheDir).listFiles();
        if (files == null || files.length == 0) {
            return domMap;
        }
        for (File file : files) {
            if (!file.isFile()) {
                continue;
            }
            String fileName = URLDecoder.decode(file.getName(), "UTF-8");
            if (!(fileName.endsWith(Constants.SERVICE_INFO_SPLITER + "meta") || fileName.endsWith(Constants.SERVICE_INFO_SPLITER + "special-url"))) {
                ServiceInfo dom = new ServiceInfo(fileName);
                List<Instance> ips = new ArrayList<Instance>();
                dom.setHosts(ips);
                ServiceInfo newFormat = null;
                try {
                    String dataString = ConcurrentDiskUtil.getFileContent(file, Charset.defaultCharset().toString());
                    reader = new BufferedReader(new StringReader(dataString));
                    String json;
                    while ((json = reader.readLine()) != null) {
                        try {
                            if (!json.startsWith("{")) {
                                continue;
                            }
                            newFormat = JacksonUtils.toObj(json, ServiceInfo.class);
                            if (StringUtils.isEmpty(newFormat.getName())) {
                                ips.add(JacksonUtils.toObj(json, Instance.class));
                            }
                        } catch (Throwable e) {
                            NAMING_LOGGER.error("[NA] error while parsing cache file: " + json, e);
                        }
                    }
                } catch (Exception e) {
                    NAMING_LOGGER.error("[NA] failed to read cache for dom: " + file.getName(), e);
                } finally {
                    try {
                        if (reader != null) {
                            reader.close();
                        }
                    } catch (Exception e) {
                    // ignore
                    }
                }
                if (newFormat != null && !StringUtils.isEmpty(newFormat.getName()) && !CollectionUtils.isEmpty(newFormat.getHosts())) {
                    domMap.put(dom.getKey(), newFormat);
                } else if (!CollectionUtils.isEmpty(dom.getHosts())) {
                    domMap.put(dom.getKey(), dom);
                }
            }
        }
    } catch (Throwable e) {
        NAMING_LOGGER.error("[NA] failed to read cache file", e);
    }
    return domMap;
}
Also used : HashMap(java.util.HashMap) Instance(com.alibaba.nacos.api.naming.pojo.Instance) ArrayList(java.util.ArrayList) ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) File(java.io.File)

Example 30 with ServiceInfo

use of com.alibaba.nacos.api.naming.pojo.ServiceInfo in project nacos by alibaba.

the class ServiceInfoHolder method processServiceInfo.

/**
 * Process service info.
 *
 * @param serviceInfo new service info
 * @return service info
 */
public ServiceInfo processServiceInfo(ServiceInfo serviceInfo) {
    String serviceKey = serviceInfo.getKey();
    if (serviceKey == null) {
        return null;
    }
    ServiceInfo oldService = serviceInfoMap.get(serviceInfo.getKey());
    if (isEmptyOrErrorPush(serviceInfo)) {
        // empty or error push, just ignore
        return oldService;
    }
    serviceInfoMap.put(serviceInfo.getKey(), serviceInfo);
    boolean changed = isChangedServiceInfo(oldService, serviceInfo);
    if (StringUtils.isBlank(serviceInfo.getJsonFromServer())) {
        serviceInfo.setJsonFromServer(JacksonUtils.toJson(serviceInfo));
    }
    MetricsMonitor.getServiceInfoMapSizeMonitor().set(serviceInfoMap.size());
    if (changed) {
        NAMING_LOGGER.info("current ips:({}) service: {} -> {}", serviceInfo.ipCount(), serviceInfo.getKey(), JacksonUtils.toJson(serviceInfo.getHosts()));
        NotifyCenter.publishEvent(new InstancesChangeEvent(serviceInfo.getName(), serviceInfo.getGroupName(), serviceInfo.getClusters(), serviceInfo.getHosts()));
        DiskCache.write(serviceInfo, cacheDir);
    }
    return serviceInfo;
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) InstancesChangeEvent(com.alibaba.nacos.client.naming.event.InstancesChangeEvent)

Aggregations

ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)74 Test (org.junit.Test)44 Instance (com.alibaba.nacos.api.naming.pojo.Instance)29 ArrayList (java.util.ArrayList)17 ServiceMetadata (com.alibaba.nacos.naming.core.v2.metadata.ServiceMetadata)12 Properties (java.util.Properties)10 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)9 NacosException (com.alibaba.nacos.api.exception.NacosException)8 ServiceInfoHolder (com.alibaba.nacos.client.naming.cache.ServiceInfoHolder)8 Before (org.junit.Before)8 Service (com.alibaba.nacos.naming.core.v2.pojo.Service)7 Field (java.lang.reflect.Field)6 HashSet (java.util.HashSet)5 EventListener (com.alibaba.nacos.api.naming.listener.EventListener)4 SubscribeServiceResponse (com.alibaba.nacos.api.naming.remote.response.SubscribeServiceResponse)4 PushDataWrapper (com.alibaba.nacos.naming.push.v2.PushDataWrapper)4 HashMap (java.util.HashMap)4 QueryServiceResponse (com.alibaba.nacos.api.naming.remote.response.QueryServiceResponse)3 Secured (com.alibaba.nacos.auth.annotation.Secured)3 InstancesChangeNotifier (com.alibaba.nacos.client.naming.event.InstancesChangeNotifier)3