diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftStore.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftStore.java index f1593aa5e..cc391e09b 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftStore.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/persistent/raft/RaftStore.java @@ -22,6 +22,7 @@ import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.naming.consistency.ApplyAction; import com.alibaba.nacos.naming.consistency.Datum; import com.alibaba.nacos.naming.consistency.KeyBuilder; +import com.alibaba.nacos.naming.core.Instance; import com.alibaba.nacos.naming.core.Instances; import com.alibaba.nacos.naming.core.Service; import com.alibaba.nacos.naming.misc.Loggers; @@ -37,6 +38,7 @@ import java.io.FileOutputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; +import java.util.List; import java.util.Properties; import java.util.concurrent.ConcurrentHashMap; @@ -157,8 +159,34 @@ public class RaftStore { } if (KeyBuilder.matchInstanceListKey(file.getName())) { - return JSON.parseObject(json, new TypeReference>() { - }); + + Datum instancesDatum; + + try { + instancesDatum = JSON.parseObject(json, new TypeReference>() { + }); + } catch (Exception e) { + JSONObject jsonObject = JSON.parseObject(json); + instancesDatum = new Datum<>(); + instancesDatum.timestamp.set(jsonObject.getLongValue("timestamp")); + + String key = jsonObject.getString("key"); + 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(JSON.parseObject(jsonObject.getString("value"), + new TypeReference>(){})); + if (!instancesDatum.value.getInstanceList().isEmpty()) { + for (Instance instance : instancesDatum.value.getInstanceList()) { + instance.setEphemeral(false); + } + } + } + + return instancesDatum; } return JSON.parseObject(json, Datum.class); @@ -208,6 +236,21 @@ public class RaftStore { fc.close(); } } + + // remove old format file: + if (StringUtils.isNoneBlank(namespaceId)) { + if (datum.key.contains(Constants.DEFAULT_GROUP + Constants.SERVICE_INFO_SPLITER)) { + String oldFormatKey = + datum.key.replace(Constants.DEFAULT_GROUP + Constants.SERVICE_INFO_SPLITER, StringUtils.EMPTY); + + cacheFile = new File(cacheDir + File.separator + namespaceId + File.separator + encodeFileName(oldFormatKey)); + if (cacheFile.exists() && !cacheFile.delete()) { + Loggers.RAFT.error("[RAFT-DELETE] failed to delete old format datum: {}, value: {}", + datum.key, datum.value); + throw new IllegalStateException("failed to delete old format datum: " + datum.key); + } + } + } } private File[] listCaches() throws Exception { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java index 6452ad95d..3d9a11506 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/controllers/InstanceController.java @@ -135,10 +135,10 @@ public class InstanceController { ClientInfo clientInfo = new ClientInfo(agent); if (clientInfo.type == ClientInfo.ClientType.JAVA && - clientInfo.version.compareTo(VersionUtil.parseVersion("1.0.0")) < 0) { - serviceManager.registerInstance(namespaceId, serviceName, parseInstance(request)); - } else { + clientInfo.version.compareTo(VersionUtil.parseVersion("1.0.0")) >= 0) { serviceManager.updateInstance(namespaceId, serviceName, parseInstance(request)); + } else { + serviceManager.registerInstance(namespaceId, serviceName, parseInstance(request)); } return "ok"; } @@ -364,6 +364,7 @@ public class InstanceController { public JSONObject doSrvIPXT(String namespaceId, String serviceName, String agent, String clusters, String clientIP, int udpPort, String env, boolean isCheck, String app, String tid, boolean healthyOnly) throws Exception { + ClientInfo clientInfo = new ClientInfo(agent); JSONObject result = new JSONObject(); Service service = serviceManager.getService(namespaceId, serviceName); @@ -407,8 +408,14 @@ public class InstanceController { Loggers.DEBUG_LOG.debug("no instance to serve for service: " + serviceName); } + if (clientInfo.type == ClientInfo.ClientType.JAVA && + clientInfo.version.compareTo(VersionUtil.parseVersion("1.0.0")) >= 0) { + result.put("dom", serviceName); + } else { + result.put("dom", NamingUtils.getServiceName(serviceName)); + } + result.put("hosts", new JSONArray()); - result.put("dom", serviceName); result.put("name", serviceName); result.put("cacheMillis", cacheMillis); result.put("lastRefTime", System.currentTimeMillis()); @@ -454,8 +461,6 @@ public class InstanceController { JSONArray hosts = new JSONArray(); - ClientInfo clientInfo = new ClientInfo(agent); - for (Map.Entry> entry : ipMap.entrySet()) { List ips = entry.getValue(); diff --git a/pom.xml b/pom.xml index df49487d4..000879587 100644 --- a/pom.xml +++ b/pom.xml @@ -94,8 +94,8 @@ false true - 1.6 - 1.6 + 1.8 + 1.8 jacoco ${project.basedir}/../test/target/jacoco-it.exec