Search in sources :

Example 1 with DistroHttpCombinedKey

use of com.alibaba.nacos.naming.consistency.ephemeral.distro.combined.DistroHttpCombinedKey in project nacos by alibaba.

the class DistroController method get.

/**
 * Get datum.
 *
 * @param body keys of data
 * @return datum
 * @throws Exception if failed
 */
@GetMapping("/datum")
public ResponseEntity get(@RequestBody String body) throws Exception {
    JsonNode bodyNode = JacksonUtils.toObj(body);
    String keys = bodyNode.get("keys").asText();
    String keySplitter = ",";
    DistroHttpCombinedKey distroKey = new DistroHttpCombinedKey(KeyBuilder.INSTANCE_LIST_KEY_PREFIX, "");
    for (String key : keys.split(keySplitter)) {
        distroKey.getActualResourceTypes().add(key);
    }
    DistroData distroData = distroProtocol.onQuery(distroKey);
    return ResponseEntity.ok(distroData.getContent());
}
Also used : JsonNode(com.fasterxml.jackson.databind.JsonNode) DistroHttpCombinedKey(com.alibaba.nacos.naming.consistency.ephemeral.distro.combined.DistroHttpCombinedKey) DistroData(com.alibaba.nacos.core.distributed.distro.entity.DistroData) GetMapping(org.springframework.web.bind.annotation.GetMapping)

Example 2 with DistroHttpCombinedKey

use of com.alibaba.nacos.naming.consistency.ephemeral.distro.combined.DistroHttpCombinedKey in project nacos by alibaba.

the class DistroDataStorageImpl method getDistroData.

@Override
public DistroData getDistroData(DistroKey distroKey) {
    Map<String, Datum> result = new HashMap<>(2);
    if (distroKey instanceof DistroHttpCombinedKey) {
        result = dataStore.batchGet(((DistroHttpCombinedKey) distroKey).getActualResourceTypes());
    } else {
        Datum datum = dataStore.get(distroKey.getResourceKey());
        result.put(distroKey.getResourceKey(), datum);
    }
    byte[] dataContent = ApplicationUtils.getBean(Serializer.class).serialize(result);
    return new DistroData(distroKey, dataContent);
}
Also used : Datum(com.alibaba.nacos.naming.consistency.Datum) HashMap(java.util.HashMap) DistroHttpCombinedKey(com.alibaba.nacos.naming.consistency.ephemeral.distro.combined.DistroHttpCombinedKey) Serializer(com.alibaba.nacos.naming.cluster.transport.Serializer) DistroData(com.alibaba.nacos.core.distributed.distro.entity.DistroData)

Example 3 with DistroHttpCombinedKey

use of com.alibaba.nacos.naming.consistency.ephemeral.distro.combined.DistroHttpCombinedKey in project nacos by alibaba.

the class DistroConsistencyServiceImpl method onReceiveChecksums.

/**
 * Check sum when receive checksums request.
 *
 * @param checksumMap map of checksum
 * @param server      source server request checksum
 */
public void onReceiveChecksums(Map<String, String> checksumMap, String server) {
    if (syncChecksumTasks.putIfAbsent(server, ON_RECEIVE_CHECKSUMS_PROCESSING_TAG) != null) {
        // Already in process of this server:
        Loggers.DISTRO.warn("sync checksum task already in process with {}", server);
        return;
    }
    try {
        List<String> toUpdateKeys = new ArrayList<>();
        List<String> toRemoveKeys = new ArrayList<>();
        for (Map.Entry<String, String> entry : checksumMap.entrySet()) {
            if (distroMapper.responsible(KeyBuilder.getServiceName(entry.getKey()))) {
                // this key should not be sent from remote server:
                Loggers.DISTRO.error("receive responsible key timestamp of " + entry.getKey() + " from " + server);
                // abort the procedure:
                return;
            }
            if (!dataStore.contains(entry.getKey()) || dataStore.get(entry.getKey()).value == null || !dataStore.get(entry.getKey()).value.getChecksum().equals(entry.getValue())) {
                toUpdateKeys.add(entry.getKey());
            }
        }
        for (String key : dataStore.keys()) {
            if (!server.equals(distroMapper.mapSrv(KeyBuilder.getServiceName(key)))) {
                continue;
            }
            if (!checksumMap.containsKey(key)) {
                toRemoveKeys.add(key);
            }
        }
        Loggers.DISTRO.info("to remove keys: {}, to update keys: {}, source: {}", toRemoveKeys, toUpdateKeys, server);
        for (String key : toRemoveKeys) {
            onRemove(key);
        }
        if (toUpdateKeys.isEmpty()) {
            return;
        }
        try {
            DistroHttpCombinedKey distroKey = new DistroHttpCombinedKey(KeyBuilder.INSTANCE_LIST_KEY_PREFIX, server);
            distroKey.getActualResourceTypes().addAll(toUpdateKeys);
            DistroData remoteData = distroProtocol.queryFromRemote(distroKey);
            if (null != remoteData) {
                processData(remoteData.getContent());
            }
        } catch (Exception e) {
            Loggers.DISTRO.error("get data from " + server + " failed!", e);
        }
    } finally {
        // Remove this 'in process' flag:
        syncChecksumTasks.remove(server);
    }
}
Also used : ArrayList(java.util.ArrayList) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DistroHttpCombinedKey(com.alibaba.nacos.naming.consistency.ephemeral.distro.combined.DistroHttpCombinedKey) NacosException(com.alibaba.nacos.api.exception.NacosException) DistroData(com.alibaba.nacos.core.distributed.distro.entity.DistroData)

Example 4 with DistroHttpCombinedKey

use of com.alibaba.nacos.naming.consistency.ephemeral.distro.combined.DistroHttpCombinedKey in project nacos by alibaba.

the class DistroHttpAgent method getData.

@Override
public DistroData getData(DistroKey key, String targetServer) {
    try {
        List<String> toUpdateKeys = null;
        if (key instanceof DistroHttpCombinedKey) {
            toUpdateKeys = ((DistroHttpCombinedKey) key).getActualResourceTypes();
        } else {
            toUpdateKeys = new ArrayList<>(1);
            toUpdateKeys.add(key.getResourceKey());
        }
        byte[] queriedData = NamingProxy.getData(toUpdateKeys, key.getTargetServer());
        return new DistroData(key, queriedData);
    } catch (Exception e) {
        throw new DistroException(String.format("Get data from %s failed.", key.getTargetServer()), e);
    }
}
Also used : DistroException(com.alibaba.nacos.core.distributed.distro.exception.DistroException) DistroHttpCombinedKey(com.alibaba.nacos.naming.consistency.ephemeral.distro.combined.DistroHttpCombinedKey) DistroException(com.alibaba.nacos.core.distributed.distro.exception.DistroException) DistroData(com.alibaba.nacos.core.distributed.distro.entity.DistroData)

Aggregations

DistroData (com.alibaba.nacos.core.distributed.distro.entity.DistroData)4 DistroHttpCombinedKey (com.alibaba.nacos.naming.consistency.ephemeral.distro.combined.DistroHttpCombinedKey)4 NacosException (com.alibaba.nacos.api.exception.NacosException)1 DistroException (com.alibaba.nacos.core.distributed.distro.exception.DistroException)1 Serializer (com.alibaba.nacos.naming.cluster.transport.Serializer)1 Datum (com.alibaba.nacos.naming.consistency.Datum)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 GetMapping (org.springframework.web.bind.annotation.GetMapping)1