Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
nkorange committed Apr 10, 2019
1 parent d649106 commit 8d9b91e
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
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;
Expand All @@ -37,6 +38,7 @@
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;

Expand Down Expand Up @@ -157,8 +159,34 @@ public synchronized Datum readDatum(File file, String namespaceId) throws IOExce
}

if (KeyBuilder.matchInstanceListKey(file.getName())) {
return JSON.parseObject(json, new TypeReference<Datum<Instances>>() {
});

Datum<Instances> instancesDatum;

try {
instancesDatum = JSON.parseObject(json, new TypeReference<Datum<Instances>>() {
});
} 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<List<Instance>>(){}));
if (!instancesDatum.value.getInstanceList().isEmpty()) {
for (Instance instance : instancesDatum.value.getInstanceList()) {
instance.setEphemeral(false);
}
}
}

return instancesDatum;
}

return JSON.parseObject(json, Datum.class);
Expand Down Expand Up @@ -208,6 +236,21 @@ public synchronized void write(final Datum datum) throws Exception {
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 {
Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@
<maven.test.skip>false</maven.test.skip>
<maven.javadoc.skip>true</maven.javadoc.skip>
<!-- Compiler settings properties -->
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<sonar.java.coveragePlugin>jacoco</sonar.java.coveragePlugin>
<!-- Exclude all generated code -->
<sonar.jacoco.itReportPath>${project.basedir}/../test/target/jacoco-it.exec</sonar.jacoco.itReportPath>
Expand Down

0 comments on commit 8d9b91e

Please sign in to comment.