use of com.alibaba.nacos.naming.pojo.InstanceOperationContext 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;
}
Aggregations