Search in sources :

Example 1 with InstanceOperationInfo

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;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Instance(com.alibaba.nacos.api.naming.pojo.Instance) InstanceOperationInfo(com.alibaba.nacos.naming.pojo.InstanceOperationInfo) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) DeleteMapping(org.springframework.web.bind.annotation.DeleteMapping) Secured(com.alibaba.nacos.auth.annotation.Secured) CanDistro(com.alibaba.nacos.naming.web.CanDistro)

Example 2 with InstanceOperationInfo

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

Example 3 with InstanceOperationInfo

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());
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Instance(com.alibaba.nacos.api.naming.pojo.Instance) InstanceOperationInfo(com.alibaba.nacos.naming.pojo.InstanceOperationInfo) Test(org.junit.Test)

Example 4 with InstanceOperationInfo

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());
}
Also used : ServiceInfo(com.alibaba.nacos.api.naming.pojo.ServiceInfo) Instance(com.alibaba.nacos.api.naming.pojo.Instance) InstanceOperationInfo(com.alibaba.nacos.naming.pojo.InstanceOperationInfo) Test(org.junit.Test)

Example 5 with InstanceOperationInfo

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;
}
Also used : ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) Instance(com.alibaba.nacos.api.naming.pojo.Instance) InstanceOperationInfo(com.alibaba.nacos.naming.pojo.InstanceOperationInfo) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) Secured(com.alibaba.nacos.auth.annotation.Secured) PutMapping(org.springframework.web.bind.annotation.PutMapping) CanDistro(com.alibaba.nacos.naming.web.CanDistro)

Aggregations

InstanceOperationInfo (com.alibaba.nacos.naming.pojo.InstanceOperationInfo)7 Instance (com.alibaba.nacos.api.naming.pojo.Instance)6 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)5 Secured (com.alibaba.nacos.auth.annotation.Secured)4 CanDistro (com.alibaba.nacos.naming.web.CanDistro)4 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)4 ServiceInfo (com.alibaba.nacos.api.naming.pojo.ServiceInfo)2 PutMapping (org.springframework.web.bind.annotation.PutMapping)2 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