Search in sources :

Example 1 with InstanceOperationContext

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;
}
Also used : Member(com.alibaba.nacos.core.cluster.Member) Datum(com.alibaba.nacos.naming.consistency.Datum) RecordListener(com.alibaba.nacos.naming.consistency.RecordListener) ServerMemberManager(com.alibaba.nacos.core.cluster.ServerMemberManager) ServiceStatusSynchronizer(com.alibaba.nacos.naming.misc.ServiceStatusSynchronizer) KeyBuilder(com.alibaba.nacos.naming.consistency.KeyBuilder) Map(java.util.Map) JsonNode(com.fasterxml.jackson.databind.JsonNode) NetUtils(com.alibaba.nacos.naming.misc.NetUtils) JacksonUtils(com.alibaba.nacos.common.utils.JacksonUtils) Collection(java.util.Collection) InternetAddressUtil(com.alibaba.nacos.common.utils.InternetAddressUtil) Loggers(com.alibaba.nacos.naming.misc.Loggers) Resource(javax.annotation.Resource) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Collectors(java.util.stream.Collectors) RaftPeerSet(com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeerSet) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Objects(java.util.Objects) InstanceOperationContext(com.alibaba.nacos.naming.pojo.InstanceOperationContext) UtilsAndCommons(com.alibaba.nacos.naming.misc.UtilsAndCommons) List(java.util.List) Constants(com.alibaba.nacos.api.common.Constants) StringUtils(com.alibaba.nacos.common.utils.StringUtils) PostConstruct(javax.annotation.PostConstruct) HashMap(java.util.HashMap) Function(java.util.function.Function) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) HashSet(java.util.HashSet) RaftPeer(com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer) ConsistencyService(com.alibaba.nacos.naming.consistency.ConsistencyService) SwitchDomain(com.alibaba.nacos.naming.misc.SwitchDomain) NacosException(com.alibaba.nacos.api.exception.NacosException) Message(com.alibaba.nacos.naming.misc.Message) InstanceOperationInfo(com.alibaba.nacos.naming.pojo.InstanceOperationInfo) CollectionUtils(com.alibaba.nacos.common.utils.CollectionUtils) UPDATE_INSTANCE_METADATA_ACTION_REMOVE(com.alibaba.nacos.naming.misc.UtilsAndCommons.UPDATE_INSTANCE_METADATA_ACTION_REMOVE) Iterator(java.util.Iterator) Synchronizer(com.alibaba.nacos.naming.misc.Synchronizer) NamingUtils(com.alibaba.nacos.api.naming.utils.NamingUtils) EmptyServiceAutoCleaner(com.alibaba.nacos.naming.core.v2.cleaner.EmptyServiceAutoCleaner) TimeUnit(java.util.concurrent.TimeUnit) UPDATE_INSTANCE_METADATA_ACTION_UPDATE(com.alibaba.nacos.naming.misc.UtilsAndCommons.UPDATE_INSTANCE_METADATA_ACTION_UPDATE) Component(org.springframework.stereotype.Component) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) StringJoiner(java.util.StringJoiner) LinkedBlockingDeque(java.util.concurrent.LinkedBlockingDeque) UdpPushService(com.alibaba.nacos.naming.push.UdpPushService) GlobalExecutor(com.alibaba.nacos.naming.misc.GlobalExecutor) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentSkipListMap(java.util.concurrent.ConcurrentSkipListMap) InstanceOperationContext(com.alibaba.nacos.naming.pojo.InstanceOperationContext) NacosException(com.alibaba.nacos.api.exception.NacosException)

Aggregations

Constants (com.alibaba.nacos.api.common.Constants)1 NacosException (com.alibaba.nacos.api.exception.NacosException)1 NamingUtils (com.alibaba.nacos.api.naming.utils.NamingUtils)1 CollectionUtils (com.alibaba.nacos.common.utils.CollectionUtils)1 InternetAddressUtil (com.alibaba.nacos.common.utils.InternetAddressUtil)1 JacksonUtils (com.alibaba.nacos.common.utils.JacksonUtils)1 StringUtils (com.alibaba.nacos.common.utils.StringUtils)1 Member (com.alibaba.nacos.core.cluster.Member)1 ServerMemberManager (com.alibaba.nacos.core.cluster.ServerMemberManager)1 ConsistencyService (com.alibaba.nacos.naming.consistency.ConsistencyService)1 Datum (com.alibaba.nacos.naming.consistency.Datum)1 KeyBuilder (com.alibaba.nacos.naming.consistency.KeyBuilder)1 RecordListener (com.alibaba.nacos.naming.consistency.RecordListener)1 RaftPeer (com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer)1 RaftPeerSet (com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeerSet)1 EmptyServiceAutoCleaner (com.alibaba.nacos.naming.core.v2.cleaner.EmptyServiceAutoCleaner)1 GlobalExecutor (com.alibaba.nacos.naming.misc.GlobalExecutor)1 Loggers (com.alibaba.nacos.naming.misc.Loggers)1 Message (com.alibaba.nacos.naming.misc.Message)1 NetUtils (com.alibaba.nacos.naming.misc.NetUtils)1