Search in sources :

Example 11 with DistroData

use of com.alibaba.nacos.core.distributed.distro.entity.DistroData in project nacos by alibaba.

the class DistroClientDataProcessor method getVerifyData.

@Override
public List<DistroData> getVerifyData() {
    List<DistroData> result = new LinkedList<>();
    for (String each : clientManager.allClientId()) {
        Client client = clientManager.getClient(each);
        if (null == client || !client.isEphemeral()) {
            continue;
        }
        if (clientManager.isResponsibleClient(client)) {
            // TODO add revision for client.
            DistroClientVerifyInfo verifyData = new DistroClientVerifyInfo(client.getClientId(), 0);
            DistroKey distroKey = new DistroKey(client.getClientId(), TYPE);
            DistroData data = new DistroData(distroKey, ApplicationUtils.getBean(Serializer.class).serialize(verifyData));
            data.setType(DataOperation.VERIFY);
            result.add(data);
        }
    }
    return result;
}
Also used : Client(com.alibaba.nacos.naming.core.v2.client.Client) DistroKey(com.alibaba.nacos.core.distributed.distro.entity.DistroKey) LinkedList(java.util.LinkedList) DistroData(com.alibaba.nacos.core.distributed.distro.entity.DistroData)

Example 12 with DistroData

use of com.alibaba.nacos.core.distributed.distro.entity.DistroData 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 13 with DistroData

use of com.alibaba.nacos.core.distributed.distro.entity.DistroData in project nacos by alibaba.

the class DistroControllerTest method testGet.

@Test
public void testGet() {
    try {
        Mockito.when(distroProtocol.onQuery(Mockito.any())).thenReturn(new DistroData(null, "content".getBytes()));
        ResponseEntity<byte[]> responseEntity = distroController.get("{\"keys\":[\"12\", \"33\"]}");
        Assert.assertArrayEquals("content".getBytes(), responseEntity.getBody());
    } catch (Exception e) {
        e.printStackTrace();
        Assert.fail(e.getMessage());
    }
}
Also used : NacosException(com.alibaba.nacos.api.exception.NacosException) DistroData(com.alibaba.nacos.core.distributed.distro.entity.DistroData) Test(org.junit.Test)

Example 14 with DistroData

use of com.alibaba.nacos.core.distributed.distro.entity.DistroData in project nacos by alibaba.

the class DistroDataResponseTest method test.

@Test
public void test() {
    DistroDataResponse distroDataResponse = new DistroDataResponse();
    DistroData distroData = mock(DistroData.class);
    distroDataResponse.setDistroData(distroData);
    assertEquals(distroData, distroDataResponse.getDistroData());
}
Also used : DistroData(com.alibaba.nacos.core.distributed.distro.entity.DistroData) Test(org.junit.Test)

Example 15 with DistroData

use of com.alibaba.nacos.core.distributed.distro.entity.DistroData 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)24 DistroKey (com.alibaba.nacos.core.distributed.distro.entity.DistroKey)5 Test (org.junit.Test)5 DistroDataResponse (com.alibaba.nacos.naming.cluster.remote.response.DistroDataResponse)4 Serializer (com.alibaba.nacos.naming.cluster.transport.Serializer)4 DistroHttpCombinedKey (com.alibaba.nacos.naming.consistency.ephemeral.distro.combined.DistroHttpCombinedKey)4 NacosException (com.alibaba.nacos.api.exception.NacosException)3 Member (com.alibaba.nacos.core.cluster.Member)3 Datum (com.alibaba.nacos.naming.consistency.Datum)3 Client (com.alibaba.nacos.naming.core.v2.client.Client)3 DistroDataStorage (com.alibaba.nacos.core.distributed.distro.component.DistroDataStorage)2 DistroTransportAgent (com.alibaba.nacos.core.distributed.distro.component.DistroTransportAgent)2 DistroException (com.alibaba.nacos.core.distributed.distro.exception.DistroException)2 DistroDataRequest (com.alibaba.nacos.naming.cluster.remote.request.DistroDataRequest)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 RequestMeta (com.alibaba.nacos.api.remote.request.RequestMeta)1 Response (com.alibaba.nacos.api.remote.response.Response)1 DataOperation (com.alibaba.nacos.consistency.DataOperation)1 DistroDataProcessor (com.alibaba.nacos.core.distributed.distro.component.DistroDataProcessor)1