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;
}
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;
}
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);
}
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());
}
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);
}
Aggregations