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