use of com.alibaba.nacos.naming.core.Instances in project nacos by alibaba.
the class DoubleWriteInstanceChangeToV1Task method run.
@Override
public void run() {
try {
ServiceManager serviceManager = ApplicationUtils.getBean(ServiceManager.class);
com.alibaba.nacos.naming.core.Service serviceV1 = serviceManager.getService(service.getNamespace(), service.getGroupedServiceName());
if (null == serviceV1) {
serviceManager.createEmptyService(service.getNamespace(), service.getGroupedServiceName(), service.isEphemeral());
}
Instances newInstances = getNewInstances();
String key = KeyBuilder.buildInstanceListKey(service.getNamespace(), service.getGroupedServiceName(), service.isEphemeral());
ConsistencyService consistencyService = ApplicationUtils.getBean(NAME, ConsistencyService.class);
consistencyService.put(key, newInstances);
} catch (Exception e) {
if (Loggers.SRV_LOG.isDebugEnabled()) {
Loggers.SRV_LOG.debug("Double write task for {} instance from 2 to 1 failed", service, e);
}
ServiceChangeV2Task retryTask = new ServiceChangeV2Task(service, DoubleWriteContent.INSTANCE);
retryTask.setTaskInterval(INTERVAL);
String taskKey = ServiceChangeV2Task.getKey(service);
ApplicationUtils.getBean(DoubleWriteDelayTaskEngine.class).addTask(taskKey, retryTask);
}
}
use of com.alibaba.nacos.naming.core.Instances in project nacos by alibaba.
the class DoubleWriteInstanceChangeToV1Task method getNewInstances.
private Instances getNewInstances() {
Instances result = new Instances();
ServiceStorage serviceStorage = ApplicationUtils.getBean(ServiceStorage.class);
InstanceUpgradeHelper instanceUpgradeHelper = ApplicationUtils.getBean(InstanceUpgradeHelper.class);
long currentTimeStamp = System.currentTimeMillis();
for (Instance each : serviceStorage.getData(service).getHosts()) {
com.alibaba.nacos.naming.core.Instance instance = instanceUpgradeHelper.toV1(each);
instance.setLastBeat(currentTimeStamp);
result.getInstanceList().add(instance);
}
return result;
}
use of com.alibaba.nacos.naming.core.Instances in project nacos by alibaba.
the class RaftController method onPublish.
/**
* Commit publish datum.
*
* @param request http request
* @param response http response
* @return 'ok' if success
* @throws Exception exception
*/
@PostMapping("/datum/commit")
public String onPublish(HttpServletRequest request, HttpServletResponse response) throws Exception {
if (versionJudgement.allMemberIsNewVersion()) {
throw new IllegalStateException("old raft protocol already stop");
}
response.setHeader("Content-Type", "application/json; charset=" + getAcceptEncoding(request));
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Content-Encode", "gzip");
String entity = IoUtils.toString(request.getInputStream(), "UTF-8");
String value = URLDecoder.decode(entity, "UTF-8");
JsonNode jsonObject = JacksonUtils.toObj(value);
String key = "key";
RaftPeer source = JacksonUtils.toObj(jsonObject.get("source").toString(), RaftPeer.class);
JsonNode datumJson = jsonObject.get("datum");
Datum datum = null;
if (KeyBuilder.matchInstanceListKey(datumJson.get(key).asText())) {
datum = JacksonUtils.toObj(jsonObject.get("datum").toString(), new TypeReference<Datum<Instances>>() {
});
} else if (KeyBuilder.matchSwitchKey(datumJson.get(key).asText())) {
datum = JacksonUtils.toObj(jsonObject.get("datum").toString(), new TypeReference<Datum<SwitchDomain>>() {
});
} else if (KeyBuilder.matchServiceMetaKey(datumJson.get(key).asText())) {
datum = JacksonUtils.toObj(jsonObject.get("datum").toString(), new TypeReference<Datum<Service>>() {
});
}
raftConsistencyService.onPut(datum, source);
return "ok";
}
use of com.alibaba.nacos.naming.core.Instances 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.core.Instances 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