use of com.alibaba.nacos.naming.consistency.Datum in project nacos by alibaba.
the class DistroDataStorageImpl method getDatumSnapshot.
@Override
public DistroData getDatumSnapshot() {
Map<String, Datum> result = dataStore.getDataMap();
byte[] dataContent = ApplicationUtils.getBean(Serializer.class).serialize(result);
DistroKey distroKey = new DistroKey(KeyBuilder.RESOURCE_KEY_SNAPSHOT, KeyBuilder.INSTANCE_LIST_KEY_PREFIX);
return new DistroData(distroKey, dataContent);
}
use of com.alibaba.nacos.naming.consistency.Datum in project nacos by alibaba.
the class DistroDataStorageImpl method getVerifyData.
@Override
public List<DistroData> getVerifyData() {
// If upgrade to 2.0.X, do not verify for v1.
if (ApplicationUtils.getBean(UpgradeJudgement.class).isUseGrpcFeatures()) {
return Collections.emptyList();
}
Map<String, String> keyChecksums = new HashMap<>(64);
for (String key : dataStore.keys()) {
if (!distroMapper.responsible(KeyBuilder.getServiceName(key))) {
continue;
}
Datum datum = dataStore.get(key);
if (datum == null) {
continue;
}
keyChecksums.put(key, datum.value.getChecksum());
}
if (keyChecksums.isEmpty()) {
return Collections.emptyList();
}
DistroKey distroKey = new DistroKey(KeyBuilder.RESOURCE_KEY_CHECKSUM, KeyBuilder.INSTANCE_LIST_KEY_PREFIX);
DistroData data = new DistroData(distroKey, ApplicationUtils.getBean(Serializer.class).serialize(keyChecksums));
data.setType(DataOperation.VERIFY);
return Collections.singletonList(data);
}
use of com.alibaba.nacos.naming.consistency.Datum 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.Datum in project nacos by alibaba.
the class DistroConsistencyServiceImpl method processData.
private boolean processData(byte[] data) throws Exception {
if (data.length > 0) {
Map<String, Datum<Instances>> datumMap = serializer.deserializeMap(data, Instances.class);
for (Map.Entry<String, Datum<Instances>> entry : datumMap.entrySet()) {
dataStore.put(entry.getKey(), entry.getValue());
if (!listeners.containsKey(entry.getKey())) {
// pretty sure the service not exist:
if (switchDomain.isDefaultInstanceEphemeral()) {
// create empty service
Loggers.DISTRO.info("creating service {}", entry.getKey());
Service service = new Service();
String serviceName = KeyBuilder.getServiceName(entry.getKey());
String namespaceId = KeyBuilder.getNamespace(entry.getKey());
service.setName(serviceName);
service.setNamespaceId(namespaceId);
service.setGroupName(Constants.DEFAULT_GROUP);
// now validate the service. if failed, exception will be thrown
service.setLastModifiedMillis(System.currentTimeMillis());
service.recalculateChecksum();
// The Listener corresponding to the key value must not be empty
RecordListener listener = listeners.get(KeyBuilder.SERVICE_META_KEY_PREFIX).peek();
if (Objects.isNull(listener)) {
return false;
}
listener.onChange(KeyBuilder.buildServiceMetaKey(namespaceId, serviceName), service);
}
}
}
for (Map.Entry<String, Datum<Instances>> entry : datumMap.entrySet()) {
if (!listeners.containsKey(entry.getKey())) {
// Should not happen:
Loggers.DISTRO.warn("listener of {} not found.", entry.getKey());
continue;
}
try {
for (RecordListener listener : listeners.get(entry.getKey())) {
listener.onChange(entry.getKey(), entry.getValue().value);
}
} catch (Exception e) {
Loggers.DISTRO.error("[NACOS-DISTRO] error while execute listener of key: {}", entry.getKey(), e);
continue;
}
// Update data store if listener executed successfully:
dataStore.put(entry.getKey(), entry.getValue());
}
}
return true;
}
use of com.alibaba.nacos.naming.consistency.Datum in project nacos by alibaba.
the class ServiceManagerTest method testUpdateMetadata.
@Test
public void testUpdateMetadata() throws NacosException {
serviceManager.createEmptyService(TEST_NAMESPACE, TEST_SERVICE_NAME, true);
List<Instance> instanceList = new LinkedList<>();
Datum datam = new Datum();
datam.key = KeyBuilder.buildInstanceListKey(TEST_NAMESPACE, TEST_SERVICE_NAME, true);
Instances instances = new Instances();
instanceList.add(instance);
instanceList.add(instance2);
instances.setInstanceList(instanceList);
datam.value = instances;
when(consistencyService.get(KeyBuilder.buildInstanceListKey(TEST_NAMESPACE, TEST_SERVICE_NAME, true))).thenReturn(datam);
Instance updateMetadataInstance = new Instance();
updateMetadataInstance.setIp(instance.getIp());
updateMetadataInstance.setPort(instance.getPort());
updateMetadataInstance.setClusterName(cluster.getName());
updateMetadataInstance.setEphemeral(instance.isEphemeral());
Map<String, String> updateMetadata = new HashMap<>(16);
updateMetadata.put("key1", "new-value1");
updateMetadata.put("key2", "value2");
updateMetadataInstance.setMetadata(updateMetadata);
// all=false, update input instances
serviceManager.updateMetadata(TEST_NAMESPACE, TEST_SERVICE_NAME, true, UPDATE_INSTANCE_METADATA_ACTION_UPDATE, false, CollectionUtils.list(updateMetadataInstance), updateMetadata);
assertEquals(instance.getMetadata().get("key1"), "new-value1");
assertEquals(instance.getMetadata().get("key2"), "value2");
// all=true, update all instances
serviceManager.updateMetadata(TEST_NAMESPACE, TEST_SERVICE_NAME, true, UPDATE_INSTANCE_METADATA_ACTION_UPDATE, true, null, updateMetadata);
assertEquals(instance2.getMetadata().get("key1"), "new-value1");
assertEquals(instance2.getMetadata().get("key2"), "value2");
Instance deleteMetadataInstance = new Instance();
deleteMetadataInstance.setIp(instance.getIp());
deleteMetadataInstance.setPort(instance.getPort());
deleteMetadataInstance.setClusterName(cluster.getName());
deleteMetadataInstance.setEphemeral(instance.isEphemeral());
Map<String, String> deleteMetadata = new HashMap<>(16);
deleteMetadata.put("key2", null);
deleteMetadata.put("key3", null);
updateMetadataInstance.setMetadata(deleteMetadata);
serviceManager.updateMetadata(TEST_NAMESPACE, TEST_SERVICE_NAME, true, UPDATE_INSTANCE_METADATA_ACTION_REMOVE, false, CollectionUtils.list(deleteMetadataInstance), deleteMetadata);
assertEquals(instance.getMetadata().get("key1"), "new-value1");
assertNull(instance.getMetadata().get("key2"));
assertNull(instance.getMetadata().get("key3"));
serviceManager.updateMetadata(TEST_NAMESPACE, TEST_SERVICE_NAME, true, UPDATE_INSTANCE_METADATA_ACTION_REMOVE, true, null, deleteMetadata);
assertEquals(instance2.getMetadata().get("key1"), "new-value1");
assertNull(instance2.getMetadata().get("key2"));
assertNull(instance2.getMetadata().get("key3"));
}
Aggregations