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