Search in sources :

Example 6 with Datum

use of com.alibaba.nacos.naming.consistency.Datum in project nacos by alibaba.

the class RaftCore method deleteDatum.

private void deleteDatum(String key) {
    Datum deleted;
    try {
        deleted = datums.remove(URLDecoder.decode(key, "UTF-8"));
        if (deleted != null) {
            raftStore.delete(deleted);
            Loggers.RAFT.info("datum deleted, key: {}", key);
        }
        NotifyCenter.publishEvent(ValueChangeEvent.builder().key(URLDecoder.decode(key, "UTF-8")).action(DataOperation.DELETE).build());
    } catch (UnsupportedEncodingException e) {
        Loggers.RAFT.warn("datum key decode failed: {}", key);
    }
}
Also used : Datum(com.alibaba.nacos.naming.consistency.Datum) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 7 with Datum

use of com.alibaba.nacos.naming.consistency.Datum in project nacos by alibaba.

the class RaftCore method loadDatum.

/**
 * Load datum.
 *
 * @param key datum key
 */
public void loadDatum(String key) {
    try {
        Datum datum = raftStore.load(key);
        if (datum == null) {
            return;
        }
        datums.put(key, datum);
    } catch (Exception e) {
        Loggers.RAFT.error("load datum failed: " + key, e);
    }
}
Also used : Datum(com.alibaba.nacos.naming.consistency.Datum) UnsupportedEncodingException(java.io.UnsupportedEncodingException) NacosException(com.alibaba.nacos.api.exception.NacosException) NacosRuntimeException(com.alibaba.nacos.api.exception.runtime.NacosRuntimeException) IOException(java.io.IOException)

Example 8 with Datum

use of com.alibaba.nacos.naming.consistency.Datum in project nacos by alibaba.

the class RaftStore method readDatum.

private synchronized Datum readDatum(File file, String namespaceId) throws IOException {
    if (!KeyBuilder.isDatumCacheFile(file.getName())) {
        return null;
    }
    ByteBuffer buffer;
    try (FileChannel fc = new FileInputStream(file).getChannel()) {
        buffer = ByteBuffer.allocate((int) file.length());
        fc.read(buffer);
        String json = new String(buffer.array(), StandardCharsets.UTF_8);
        if (StringUtils.isBlank(json)) {
            return null;
        }
        final String fileName = file.getName();
        if (KeyBuilder.matchSwitchKey(fileName)) {
            return JacksonUtils.toObj(json, new TypeReference<Datum<SwitchDomain>>() {
            });
        }
        if (KeyBuilder.matchServiceMetaKey(fileName)) {
            Datum<Service> serviceDatum;
            try {
                serviceDatum = JacksonUtils.toObj(json.replace("\\", ""), new TypeReference<Datum<Service>>() {
                });
            } catch (Exception e) {
                JsonNode jsonObject = JacksonUtils.toObj(json);
                serviceDatum = new Datum<>();
                serviceDatum.timestamp.set(jsonObject.get("timestamp").asLong());
                serviceDatum.key = jsonObject.get("key").asText();
                serviceDatum.value = JacksonUtils.toObj(jsonObject.get("value").toString(), Service.class);
            }
            if (StringUtils.isBlank(serviceDatum.value.getGroupName())) {
                serviceDatum.value.setGroupName(Constants.DEFAULT_GROUP);
            }
            if (!serviceDatum.value.getName().contains(Constants.SERVICE_INFO_SPLITER)) {
                serviceDatum.value.setName(Constants.DEFAULT_GROUP + Constants.SERVICE_INFO_SPLITER + serviceDatum.value.getName());
            }
            return serviceDatum;
        }
        if (KeyBuilder.matchInstanceListKey(fileName)) {
            Datum<Instances> instancesDatum;
            try {
                instancesDatum = JacksonUtils.toObj(json, new TypeReference<Datum<Instances>>() {
                });
            } catch (Exception e) {
                JsonNode jsonObject = JacksonUtils.toObj(json);
                instancesDatum = new Datum<>();
                instancesDatum.timestamp.set(jsonObject.get("timestamp").asLong());
                String key = jsonObject.get("key").asText();
                String serviceName = KeyBuilder.getServiceName(key);
                key = key.substring(0, key.indexOf(serviceName)) + Constants.DEFAULT_GROUP + Constants.SERVICE_INFO_SPLITER + serviceName;
                instancesDatum.key = key;
                instancesDatum.value = new Instances();
                instancesDatum.value.setInstanceList(JacksonUtils.toObj(jsonObject.get("value").toString(), new TypeReference<List<Instance>>() {
                }));
                if (!instancesDatum.value.getInstanceList().isEmpty()) {
                    for (Instance instance : instancesDatum.value.getInstanceList()) {
                        instance.setEphemeral(false);
                    }
                }
            }
            return instancesDatum;
        }
        return JacksonUtils.toObj(json, Datum.class);
    } catch (Exception e) {
        Loggers.RAFT.warn("waning: failed to deserialize key: {}", file.getName());
        throw e;
    }
}
Also used : Datum(com.alibaba.nacos.naming.consistency.Datum) Instance(com.alibaba.nacos.naming.core.Instance) FileChannel(java.nio.channels.FileChannel) Service(com.alibaba.nacos.naming.core.Service) JsonNode(com.fasterxml.jackson.databind.JsonNode) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream) NacosException(com.alibaba.nacos.api.exception.NacosException) IOException(java.io.IOException) Instances(com.alibaba.nacos.naming.core.Instances) List(java.util.List) TypeReference(com.fasterxml.jackson.core.type.TypeReference)

Example 9 with Datum

use of com.alibaba.nacos.naming.consistency.Datum in project nacos by alibaba.

the class SwitchManager method update.

/**
 * Update switch information.
 *
 * @param entry item entry of switch, {@link SwitchEntry}
 * @param value switch value
 * @param debug whether debug
 * @throws Exception exception
 */
public void update(String entry, String value, boolean debug) throws Exception {
    lock.lock();
    try {
        Datum datum = consistencyService.get(KeyBuilder.getSwitchDomainKey());
        SwitchDomain switchDomain;
        if (datum != null && datum.value != null) {
            switchDomain = (SwitchDomain) datum.value;
        } else {
            switchDomain = this.switchDomain.clone();
        }
        if (SwitchEntry.BATCH.equals(entry)) {
            // batch update
            SwitchDomain dom = JacksonUtils.toObj(value, SwitchDomain.class);
            dom.setEnableStandalone(switchDomain.isEnableStandalone());
            if (dom.getHttpHealthParams().getMin() < SwitchDomain.HttpHealthParams.MIN_MIN || dom.getTcpHealthParams().getMin() < SwitchDomain.HttpHealthParams.MIN_MIN) {
                throw new IllegalArgumentException("min check time for http or tcp is too small(<500)");
            }
            if (dom.getHttpHealthParams().getMax() < SwitchDomain.HttpHealthParams.MIN_MAX || dom.getTcpHealthParams().getMax() < SwitchDomain.HttpHealthParams.MIN_MAX) {
                throw new IllegalArgumentException("max check time for http or tcp is too small(<3000)");
            }
            if (dom.getHttpHealthParams().getFactor() < 0 || dom.getHttpHealthParams().getFactor() > 1 || dom.getTcpHealthParams().getFactor() < 0 || dom.getTcpHealthParams().getFactor() > 1) {
                throw new IllegalArgumentException("malformed factor");
            }
            switchDomain = dom;
        }
        if (entry.equals(SwitchEntry.DISTRO_THRESHOLD)) {
            float threshold = Float.parseFloat(value);
            if (threshold <= 0) {
                throw new IllegalArgumentException("distroThreshold can not be zero or negative: " + threshold);
            }
            switchDomain.setDistroThreshold(threshold);
        }
        if (entry.equals(SwitchEntry.CLIENT_BEAT_INTERVAL)) {
            long clientBeatInterval = Long.parseLong(value);
            switchDomain.setClientBeatInterval(clientBeatInterval);
        }
        if (entry.equals(SwitchEntry.PUSH_VERSION)) {
            String type = value.split(":")[0];
            String version = value.split(":")[1];
            if (!version.matches(UtilsAndCommons.VERSION_STRING_SYNTAX)) {
                throw new IllegalArgumentException("illegal version, must match: " + UtilsAndCommons.VERSION_STRING_SYNTAX);
            }
            if (StringUtils.equals(SwitchEntry.CLIENT_JAVA, type)) {
                switchDomain.setPushJavaVersion(version);
            } else if (StringUtils.equals(SwitchEntry.CLIENT_PYTHON, type)) {
                switchDomain.setPushPythonVersion(version);
            } else if (StringUtils.equals(SwitchEntry.CLIENT_C, type)) {
                switchDomain.setPushCVersion(version);
            } else if (StringUtils.equals(SwitchEntry.CLIENT_GO, type)) {
                switchDomain.setPushGoVersion(version);
            } else {
                throw new IllegalArgumentException("unsupported client type: " + type);
            }
        }
        if (entry.equals(SwitchEntry.PUSH_CACHE_MILLIS)) {
            long cacheMillis = Long.parseLong(value);
            if (cacheMillis < SwitchEntry.MIN_PUSH_CACHE_TIME_MIILIS) {
                throw new IllegalArgumentException("min cache time for http or tcp is too small(<10000)");
            }
            switchDomain.setDefaultPushCacheMillis(cacheMillis);
        }
        // extremely careful while modifying this, cause it will affect all clients without pushing enabled
        if (entry.equals(SwitchEntry.DEFAULT_CACHE_MILLIS)) {
            long cacheMillis = Long.parseLong(value);
            if (cacheMillis < SwitchEntry.MIN_CACHE_TIME_MIILIS) {
                throw new IllegalArgumentException("min default cache time  is too small(<1000)");
            }
            switchDomain.setDefaultCacheMillis(cacheMillis);
        }
        if (entry.equals(SwitchEntry.MASTERS)) {
            List<String> masters = Arrays.asList(value.split(","));
            switchDomain.setMasters(masters);
        }
        if (entry.equals(SwitchEntry.DISTRO)) {
            boolean enabled = Boolean.parseBoolean(value);
            switchDomain.setDistroEnabled(enabled);
        }
        if (entry.equals(SwitchEntry.CHECK)) {
            boolean enabled = Boolean.parseBoolean(value);
            switchDomain.setHealthCheckEnabled(enabled);
        }
        if (entry.equals(SwitchEntry.PUSH_ENABLED)) {
            boolean enabled = Boolean.parseBoolean(value);
            switchDomain.setPushEnabled(enabled);
        }
        if (entry.equals(SwitchEntry.SERVICE_STATUS_SYNC_PERIOD)) {
            long millis = Long.parseLong(value);
            if (millis < SwitchEntry.MIN_SERVICE_SYNC_TIME_MIILIS) {
                throw new IllegalArgumentException("serviceStatusSynchronizationPeriodMillis is too small(<5000)");
            }
            switchDomain.setServiceStatusSynchronizationPeriodMillis(millis);
        }
        if (entry.equals(SwitchEntry.SERVER_STATUS_SYNC_PERIOD)) {
            long millis = Long.parseLong(value);
            if (millis < SwitchEntry.MIN_SERVER_SYNC_TIME_MIILIS) {
                throw new IllegalArgumentException("serverStatusSynchronizationPeriodMillis is too small(<15000)");
            }
            switchDomain.setServerStatusSynchronizationPeriodMillis(millis);
        }
        if (entry.equals(SwitchEntry.HEALTH_CHECK_TIMES)) {
            int times = Integer.parseInt(value);
            switchDomain.setCheckTimes(times);
        }
        if (entry.equals(SwitchEntry.DISABLE_ADD_IP)) {
            boolean disableAddIp = Boolean.parseBoolean(value);
            switchDomain.setDisableAddIP(disableAddIp);
        }
        if (entry.equals(SwitchEntry.SEND_BEAT_ONLY)) {
            boolean sendBeatOnly = Boolean.parseBoolean(value);
            switchDomain.setSendBeatOnly(sendBeatOnly);
        }
        if (entry.equals(SwitchEntry.LIMITED_URL_MAP)) {
            Map<String, Integer> limitedUrlMap = new HashMap<>(16);
            if (!StringUtils.isEmpty(value)) {
                String[] entries = value.split(",");
                for (String each : entries) {
                    String[] parts = each.split(":");
                    if (parts.length < 2) {
                        throw new IllegalArgumentException("invalid input for limited urls");
                    }
                    String limitedUrl = parts[0];
                    if (StringUtils.isEmpty(limitedUrl)) {
                        throw new IllegalArgumentException("url can not be empty, url: " + limitedUrl);
                    }
                    int statusCode = Integer.parseInt(parts[1]);
                    if (statusCode <= 0) {
                        throw new IllegalArgumentException("illegal normal status code: " + statusCode);
                    }
                    limitedUrlMap.put(limitedUrl, statusCode);
                }
                switchDomain.setLimitedUrlMap(limitedUrlMap);
            }
        }
        if (entry.equals(SwitchEntry.ENABLE_STANDALONE)) {
            if (!StringUtils.isNotEmpty(value)) {
                switchDomain.setEnableStandalone(Boolean.parseBoolean(value));
            }
        }
        if (entry.equals(SwitchEntry.OVERRIDDEN_SERVER_STATUS)) {
            String status = value;
            if (Constants.NULL_STRING.equals(status)) {
                status = StringUtils.EMPTY;
            }
            switchDomain.setOverriddenServerStatus(status);
        }
        if (entry.equals(SwitchEntry.DEFAULT_INSTANCE_EPHEMERAL)) {
            switchDomain.setDefaultInstanceEphemeral(Boolean.parseBoolean(value));
        }
        if (entry.equals(SwitchEntry.DISTRO_SERVER_EXPIRED_MILLIS)) {
            switchDomain.setDistroServerExpiredMillis(Long.parseLong(value));
        }
        if (entry.equals(SwitchEntry.LIGHT_BEAT_ENABLED)) {
            switchDomain.setLightBeatEnabled(ConvertUtils.toBoolean(value));
        }
        if (entry.equals(SwitchEntry.AUTO_CHANGE_HEALTH_CHECK_ENABLED)) {
            switchDomain.setAutoChangeHealthCheckEnabled(ConvertUtils.toBoolean(value));
        }
        if (entry.equals(SwitchEntry.DOUBLE_WRITE_ENABLED)) {
            if (!EnvUtil.isSupportUpgradeFrom1X()) {
                throw new IllegalAccessException("Upgrade from 1X feature has closed, " + "please set `nacos.core.support.upgrade.from.1x=true` in application.properties");
            }
            switchDomain.setDoubleWriteEnabled(ConvertUtils.toBoolean(value));
        }
        if (debug) {
            update(switchDomain);
        } else {
            consistencyService.put(KeyBuilder.getSwitchDomainKey(), switchDomain);
        }
    } finally {
        lock.unlock();
    }
}
Also used : Datum(com.alibaba.nacos.naming.consistency.Datum) HashMap(java.util.HashMap)

Example 10 with Datum

use of com.alibaba.nacos.naming.consistency.Datum in project nacos by alibaba.

the class NamingHttpClientProxy_ITCase method testSyncData.

@Test
public void testSyncData() throws NacosException, InterruptedException {
    // write data to DataStore
    String groupedName = NamingUtils.getGroupedName(serviceName, groupName);
    Instances instances = new Instances();
    Instance instance = new Instance();
    instance.setIp("1.2.3.4");
    instance.setPort(8888);
    instance.setEphemeral(true);
    instance.setServiceName(groupedName);
    List<Instance> instanceList = new ArrayList<Instance>(1);
    instanceList.add(instance);
    instances.setInstanceList(instanceList);
    String key = KeyBuilder.buildInstanceListKey(namespaceId, instance.getServiceName(), true);
    Datum<Instances> datum = new Datum<>();
    datum.value = instances;
    datum.key = key;
    datum.timestamp.incrementAndGet();
    this.dataStore.put(key, datum);
    // sync data to server
    Map<String, Datum> dataMap = dataStore.getDataMap();
    byte[] content = serializer.serialize(dataMap);
    boolean result = NamingProxy.syncData(content, "localhost:" + port);
    if (!result) {
        Assert.fail("NamingProxy.syncData error");
    }
    // query instance by api
    List<com.alibaba.nacos.api.naming.pojo.Instance> allInstances = namingService.getAllInstances(serviceName, false);
    for (int i = 0; i < 3 && allInstances.isEmpty(); i++) {
        // wait for async op
        TimeUnit.SECONDS.sleep(100);
        allInstances = namingService.getAllInstances(serviceName, false);
    }
    if (allInstances.isEmpty()) {
        Assert.fail("instance is empty");
    }
    com.alibaba.nacos.api.naming.pojo.Instance dst = allInstances.get(0);
    Assert.assertEquals(instance.getIp(), dst.getIp());
    Assert.assertEquals(instance.getPort(), dst.getPort());
    Assert.assertEquals(instance.getServiceName(), dst.getServiceName());
}
Also used : Datum(com.alibaba.nacos.naming.consistency.Datum) Instance(com.alibaba.nacos.naming.core.Instance) ArrayList(java.util.ArrayList) Instances(com.alibaba.nacos.naming.core.Instances) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

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