Search in sources :

Example 11 with InstanceMetadata

use of com.alibaba.nacos.naming.core.v2.metadata.InstanceMetadata in project nacos by alibaba.

the class InstanceOperatorClientImpl method batchDeleteMetadata.

@Override
public List<String> batchDeleteMetadata(String namespaceId, InstanceOperationInfo instanceOperationInfo, Map<String, String> metadata) throws NacosException {
    boolean isEphemeral = !UtilsAndCommons.PERSIST.equals(instanceOperationInfo.getConsistencyType());
    String serviceName = instanceOperationInfo.getServiceName();
    Service service = getService(namespaceId, serviceName, isEphemeral);
    List<String> result = new LinkedList<>();
    List<Instance> needUpdateInstance = findBatchUpdateInstance(instanceOperationInfo, service);
    for (Instance each : needUpdateInstance) {
        String metadataId = InstancePublishInfo.genMetadataId(each.getIp(), each.getPort(), each.getClusterName());
        Optional<InstanceMetadata> instanceMetadata = metadataManager.getInstanceMetadata(service, metadataId);
        InstanceMetadata newMetadata = instanceMetadata.map(this::cloneMetadata).orElseGet(InstanceMetadata::new);
        metadata.keySet().forEach(key -> newMetadata.getExtendData().remove(key));
        metadataOperateService.updateInstanceMetadata(service, metadataId, newMetadata);
        result.add(each.toInetAddr() + ":" + UtilsAndCommons.LOCALHOST_SITE + ":" + each.getClusterName() + ":" + (each.isEphemeral() ? UtilsAndCommons.EPHEMERAL : UtilsAndCommons.PERSIST));
    }
    return result;
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) InstanceMetadata(com.alibaba.nacos.naming.core.v2.metadata.InstanceMetadata) NamingMetadataOperateService(com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) ClientOperationService(com.alibaba.nacos.naming.core.v2.service.ClientOperationService) UdpPushService(com.alibaba.nacos.naming.push.UdpPushService) LinkedList(java.util.LinkedList)

Example 12 with InstanceMetadata

use of com.alibaba.nacos.naming.core.v2.metadata.InstanceMetadata in project nacos by alibaba.

the class InstanceOperatorClientImpl method batchUpdateMetadata.

@Override
public List<String> batchUpdateMetadata(String namespaceId, InstanceOperationInfo instanceOperationInfo, Map<String, String> metadata) throws NacosException {
    boolean isEphemeral = !UtilsAndCommons.PERSIST.equals(instanceOperationInfo.getConsistencyType());
    String serviceName = instanceOperationInfo.getServiceName();
    Service service = getService(namespaceId, serviceName, isEphemeral);
    List<String> result = new LinkedList<>();
    List<Instance> needUpdateInstance = findBatchUpdateInstance(instanceOperationInfo, service);
    for (Instance each : needUpdateInstance) {
        String metadataId = InstancePublishInfo.genMetadataId(each.getIp(), each.getPort(), each.getClusterName());
        Optional<InstanceMetadata> instanceMetadata = metadataManager.getInstanceMetadata(service, metadataId);
        InstanceMetadata newMetadata = instanceMetadata.map(this::cloneMetadata).orElseGet(InstanceMetadata::new);
        newMetadata.getExtendData().putAll(metadata);
        metadataOperateService.updateInstanceMetadata(service, metadataId, newMetadata);
        result.add(each.toInetAddr() + ":" + UtilsAndCommons.LOCALHOST_SITE + ":" + each.getClusterName() + ":" + (each.isEphemeral() ? UtilsAndCommons.EPHEMERAL : UtilsAndCommons.PERSIST));
    }
    return result;
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) InstanceMetadata(com.alibaba.nacos.naming.core.v2.metadata.InstanceMetadata) NamingMetadataOperateService(com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) ClientOperationService(com.alibaba.nacos.naming.core.v2.service.ClientOperationService) UdpPushService(com.alibaba.nacos.naming.push.UdpPushService) LinkedList(java.util.LinkedList)

Example 13 with InstanceMetadata

use of com.alibaba.nacos.naming.core.v2.metadata.InstanceMetadata in project nacos by alibaba.

the class NamingMetadataManagerTest method setUp.

@Before
public void setUp() throws Exception {
    namingMetadataManager = new NamingMetadataManager();
    Class<NamingMetadataManager> namingMetadataManagerClass = NamingMetadataManager.class;
    Field serviceMetadataMapField = namingMetadataManagerClass.getDeclaredField("serviceMetadataMap");
    serviceMetadataMapField.setAccessible(true);
    ConcurrentMap<Service, ServiceMetadata> serviceMetadataMap = (ConcurrentMap<Service, ServiceMetadata>) serviceMetadataMapField.get(namingMetadataManager);
    serviceMetadataMap.put(service, serviceMetadata);
    Field instanceMetadataMapField = namingMetadataManagerClass.getDeclaredField("instanceMetadataMap");
    instanceMetadataMapField.setAccessible(true);
    ConcurrentMap<Service, ConcurrentMap<String, InstanceMetadata>> instanceMetadataMap = (ConcurrentMap<Service, ConcurrentMap<String, InstanceMetadata>>) instanceMetadataMapField.get(namingMetadataManager);
    ConcurrentMap<String, InstanceMetadata> concurrentMap = new ConcurrentHashMap<>();
    concurrentMap.put(METADATA_ID, instanceMetadata);
    instanceMetadataMap.put(service, concurrentMap);
}
Also used : Field(java.lang.reflect.Field) ConcurrentMap(java.util.concurrent.ConcurrentMap) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Before(org.junit.Before)

Example 14 with InstanceMetadata

use of com.alibaba.nacos.naming.core.v2.metadata.InstanceMetadata in project nacos by alibaba.

the class HealthCheckTaskInterceptWrapperTest method testRunHealthyInstanceWithTimeoutFromMetadata.

@Test
public void testRunHealthyInstanceWithTimeoutFromMetadata() throws InterruptedException {
    InstancePublishInfo instance = injectInstance(true, System.currentTimeMillis());
    Service service = Service.newService(NAMESPACE, GROUP_NAME, SERVICE_NAME);
    InstanceMetadata metadata = new InstanceMetadata();
    metadata.getExtendData().put(PreservedMetadataKeys.HEART_BEAT_TIMEOUT, 1000L);
    when(namingMetadataManager.getInstanceMetadata(service, instance.getMetadataId())).thenReturn(Optional.of(metadata));
    when(globalConfig.isExpireInstance()).thenReturn(true);
    TimeUnit.SECONDS.sleep(1);
    taskWrapper.run();
    assertFalse(client.getAllInstancePublishInfo().isEmpty());
    assertFalse(client.getInstancePublishInfo(Service.newService(NAMESPACE, GROUP_NAME, SERVICE_NAME)).isHealthy());
}
Also used : HealthCheckInstancePublishInfo(com.alibaba.nacos.naming.core.v2.pojo.HealthCheckInstancePublishInfo) InstancePublishInfo(com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo) InstanceMetadata(com.alibaba.nacos.naming.core.v2.metadata.InstanceMetadata) Service(com.alibaba.nacos.naming.core.v2.pojo.Service) Test(org.junit.Test)

Example 15 with InstanceMetadata

use of com.alibaba.nacos.naming.core.v2.metadata.InstanceMetadata in project nacos by alibaba.

the class InstanceUtilTest method testUpdateInstanceMetadata.

@Test
public void testUpdateInstanceMetadata() {
    InstanceMetadata metaData = new InstanceMetadata();
    Map<String, Object> extendData = new ConcurrentHashMap<>(1);
    extendData.put("k1", "v1");
    extendData.put("k2", "v2");
    metaData.setExtendData(extendData);
    metaData.setEnabled(true);
    metaData.setWeight(1);
    Instance instance = InstanceUtil.parseToApiInstance(service, instancePublishInfo);
    InstanceUtil.updateInstanceMetadata(instance, metaData);
    assertNotNull(instance.getMetadata());
    assertEquals(metaData.getExtendData().size(), 2);
}
Also used : Instance(com.alibaba.nacos.api.naming.pojo.Instance) InstanceMetadata(com.alibaba.nacos.naming.core.v2.metadata.InstanceMetadata) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Test(org.junit.Test)

Aggregations

InstanceMetadata (com.alibaba.nacos.naming.core.v2.metadata.InstanceMetadata)12 Service (com.alibaba.nacos.naming.core.v2.pojo.Service)9 Instance (com.alibaba.nacos.api.naming.pojo.Instance)5 NamingMetadataOperateService (com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataOperateService)4 ClientOperationService (com.alibaba.nacos.naming.core.v2.service.ClientOperationService)4 UdpPushService (com.alibaba.nacos.naming.push.UdpPushService)4 Test (org.junit.Test)4 HealthCheckInstancePublishInfo (com.alibaba.nacos.naming.core.v2.pojo.HealthCheckInstancePublishInfo)2 InstancePublishInfo (com.alibaba.nacos.naming.core.v2.pojo.InstancePublishInfo)2 LinkedList (java.util.LinkedList)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 Client (com.alibaba.nacos.naming.core.v2.client.Client)1 IpPortBasedClient (com.alibaba.nacos.naming.core.v2.client.impl.IpPortBasedClient)1 ServiceEvent (com.alibaba.nacos.naming.core.v2.event.service.ServiceEvent)1 NamingMetadataManager (com.alibaba.nacos.naming.core.v2.metadata.NamingMetadataManager)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Before (org.junit.Before)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1