use of com.alibaba.nacos.naming.pojo.InstanceOperationInfo in project nacos by alibaba.
the class InstanceControllerV2 method batchDeleteInstanceMetadata.
/**
* Batch delete instance's metadata. old key exist = delete, old key not exist = not operate
*
* @param namespaceId namespace id
* @param serviceName service name
* @param metadata service metadata
* @param consistencyType consistencyType
* @param instances instances info
* @return success updated instances. such as '{"updated":["2.2.2.2:8080:unknown:xxxx-cluster:ephemeral"}'.
* @throws Exception any error during update
* @since 1.4.0
*/
@CanDistro
@DeleteMapping("/metadata/batch")
@Secured(action = ActionTypes.WRITE)
public ObjectNode batchDeleteInstanceMetadata(@RequestParam(defaultValue = Constants.DEFAULT_NAMESPACE_ID) String namespaceId, @RequestParam String serviceName, @RequestParam(defaultValue = "") String consistencyType, @RequestParam(defaultValue = "") String instances, @RequestParam String metadata) throws Exception {
List<Instance> targetInstances = parseBatchInstances(instances);
Map<String, String> targetMetadata = UtilsAndCommons.parseMetadata(metadata);
InstanceOperationInfo instanceOperationInfo = buildOperationInfo(serviceName, consistencyType, targetInstances);
List<String> operatedInstances = instanceServiceV2.batchDeleteMetadata(namespaceId, instanceOperationInfo, targetMetadata);
ObjectNode result = JacksonUtils.createEmptyJsonNode();
ArrayNode ipArray = JacksonUtils.createEmptyArrayNode();
for (String ip : operatedInstances) {
ipArray.add(ip);
}
result.replace("updated", ipArray);
return result;
}
use of com.alibaba.nacos.naming.pojo.InstanceOperationInfo in project nacos by alibaba.
the class ServiceManager method batchOperate.
/**
* batch operate kinds of resources.
*
* @param namespace namespace.
* @param operationInfo operation resources description.
* @param operateFunction some operation defined by kinds of situation.
*/
public List<Instance> batchOperate(String namespace, InstanceOperationInfo operationInfo, Function<InstanceOperationContext, List<Instance>> operateFunction) {
List<Instance> operatedInstances = new ArrayList<>();
try {
String serviceName = operationInfo.getServiceName();
NamingUtils.checkServiceNameFormat(serviceName);
// type: ephemeral/persist
InstanceOperationContext operationContext;
String type = operationInfo.getConsistencyType();
if (!StringUtils.isEmpty(type)) {
switch(type) {
case UtilsAndCommons.EPHEMERAL:
operationContext = new InstanceOperationContext(namespace, serviceName, true, true);
operatedInstances.addAll(operateFunction.apply(operationContext));
break;
case UtilsAndCommons.PERSIST:
operationContext = new InstanceOperationContext(namespace, serviceName, false, true);
operatedInstances.addAll(operateFunction.apply(operationContext));
break;
default:
Loggers.SRV_LOG.warn("UPDATE-METADATA: services.all value is illegal, it should be ephemeral/persist. ignore the service '" + serviceName + "'");
break;
}
} else {
List<Instance> instances = (List<Instance>) operationInfo.getInstances();
if (!CollectionUtils.isEmpty(instances)) {
// ephemeral:instances or persist:instances
Map<Boolean, List<Instance>> instanceMap = instances.stream().collect(Collectors.groupingBy(ele -> ele.isEphemeral()));
for (Map.Entry<Boolean, List<Instance>> entry : instanceMap.entrySet()) {
operationContext = new InstanceOperationContext(namespace, serviceName, entry.getKey(), false, entry.getValue());
operatedInstances.addAll(operateFunction.apply(operationContext));
}
}
}
} catch (Exception e) {
Loggers.SRV_LOG.warn("UPDATE-METADATA: update metadata failed, ignore the service '" + operationInfo.getServiceName() + "'", e);
}
return operatedInstances;
}
use of com.alibaba.nacos.naming.pojo.InstanceOperationInfo in project nacos by alibaba.
the class InstanceOperatorClientImplTest method testBatchUpdateMetadata.
@Test
public void testBatchUpdateMetadata() throws NacosException {
Instance instance = new Instance();
instance.setServiceName("C");
instance.setIp("1.1.1.1");
instance.setPort(8848);
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setHosts(Collections.singletonList(instance));
Mockito.when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo);
InstanceOperationInfo instanceOperationInfo = new InstanceOperationInfo();
List<String> res = instanceOperatorClient.batchUpdateMetadata("A", instanceOperationInfo, new HashMap<>());
Assert.assertEquals(1, res.size());
}
use of com.alibaba.nacos.naming.pojo.InstanceOperationInfo in project nacos by alibaba.
the class InstanceOperatorClientImplTest method testBatchDeleteMetadata.
@Test
public void testBatchDeleteMetadata() throws NacosException {
Instance instance = new Instance();
instance.setServiceName("C");
instance.setIp("1.1.1.1");
instance.setPort(8848);
ServiceInfo serviceInfo = new ServiceInfo();
serviceInfo.setHosts(Collections.singletonList(instance));
Mockito.when(serviceStorage.getData(Mockito.any())).thenReturn(serviceInfo);
List<String> res = instanceOperatorClient.batchDeleteMetadata("A", new InstanceOperationInfo(), new HashMap<>());
Assert.assertEquals(1, res.size());
}
use of com.alibaba.nacos.naming.pojo.InstanceOperationInfo in project nacos by alibaba.
the class InstanceController method batchUpdateInstanceMetadata.
/**
* Batch update instance's metadata. old key exist = update, old key not exist = add.
*
* @param request http request
* @return success updated instances. such as '{"updated":["2.2.2.2:8080:unknown:xxxx-cluster:ephemeral"}'.
* @throws Exception any error during update
* @since 1.4.0
*/
@CanDistro
@PutMapping(value = "/metadata/batch")
@Secured(action = ActionTypes.WRITE)
public ObjectNode batchUpdateInstanceMetadata(HttpServletRequest request) throws Exception {
final String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
String consistencyType = WebUtils.optional(request, "consistencyType", StringUtils.EMPTY);
String instances = WebUtils.optional(request, "instances", StringUtils.EMPTY);
List<Instance> targetInstances = parseBatchInstances(instances);
String metadata = WebUtils.required(request, METADATA);
Map<String, String> targetMetadata = UtilsAndCommons.parseMetadata(metadata);
InstanceOperationInfo instanceOperationInfo = buildOperationInfo(serviceName, consistencyType, targetInstances);
List<String> operatedInstances = getInstanceOperator().batchUpdateMetadata(namespaceId, instanceOperationInfo, targetMetadata);
ObjectNode result = JacksonUtils.createEmptyJsonNode();
ArrayNode ipArray = JacksonUtils.createEmptyArrayNode();
for (String ip : operatedInstances) {
ipArray.add(ip);
}
result.replace("updated", ipArray);
return result;
}
Aggregations