Search in sources :

Example 11 with Datum

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);
}
Also used : Datum(com.alibaba.nacos.naming.consistency.Datum) DistroKey(com.alibaba.nacos.core.distributed.distro.entity.DistroKey) Serializer(com.alibaba.nacos.naming.cluster.transport.Serializer) DistroData(com.alibaba.nacos.core.distributed.distro.entity.DistroData)

Example 12 with Datum

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);
}
Also used : Datum(com.alibaba.nacos.naming.consistency.Datum) HashMap(java.util.HashMap) DistroKey(com.alibaba.nacos.core.distributed.distro.entity.DistroKey) UpgradeJudgement(com.alibaba.nacos.naming.core.v2.upgrade.UpgradeJudgement) DistroData(com.alibaba.nacos.core.distributed.distro.entity.DistroData)

Example 13 with Datum

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);
}
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 14 with Datum

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;
}
Also used : Datum(com.alibaba.nacos.naming.consistency.Datum) EphemeralConsistencyService(com.alibaba.nacos.naming.consistency.ephemeral.EphemeralConsistencyService) Service(com.alibaba.nacos.naming.core.Service) RecordListener(com.alibaba.nacos.naming.consistency.RecordListener) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) NacosException(com.alibaba.nacos.api.exception.NacosException)

Example 15 with Datum

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"));
}
Also used : Datum(com.alibaba.nacos.naming.consistency.Datum) HashMap(java.util.HashMap) LinkedList(java.util.LinkedList) Test(org.junit.Test) BaseTest(com.alibaba.nacos.naming.BaseTest)

Aggregations

Datum (com.alibaba.nacos.naming.consistency.Datum)29 NacosException (com.alibaba.nacos.api.exception.NacosException)10 HashMap (java.util.HashMap)10 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)6 Test (org.junit.Test)6 Instances (com.alibaba.nacos.naming.core.Instances)5 Instance (com.alibaba.nacos.naming.core.Instance)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)4 IOException (java.io.IOException)4 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 ArrayList (java.util.ArrayList)4 NacosRuntimeException (com.alibaba.nacos.api.exception.runtime.NacosRuntimeException)3 DistroData (com.alibaba.nacos.core.distributed.distro.entity.DistroData)3 BaseTest (com.alibaba.nacos.naming.BaseTest)3 Service (com.alibaba.nacos.naming.core.Service)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)3 Map (java.util.Map)3 WriteRequest (com.alibaba.nacos.consistency.entity.WriteRequest)2 DistroKey (com.alibaba.nacos.core.distributed.distro.entity.DistroKey)2 Serializer (com.alibaba.nacos.naming.cluster.transport.Serializer)2