Github user vanzin commented on a diff in the pull request: https://github.com/apache/spark/pull/7943#discussion_r36774168 --- Diff: network/shuffle/src/main/java/org/apache/spark/network/shuffle/ExternalShuffleBlockResolver.java --- @@ -252,4 +338,118 @@ public String toString() { .toString(); } } + + static ObjectMapper mapper = new ObjectMapper(); + + private static byte[] dbAppExecKey(AppExecId appExecId) throws IOException { + // we stick a common prefix on all the keys so we can find them in the DB + String appExecJson = mapper.writeValueAsString(appExecId); + String key = (APP_KEY_PREFIX + ";" + appExecJson); + return key.getBytes(Charsets.UTF_8); + } + + private static AppExecId parseDbAppExecKey(String s) throws IOException { + int p = s.indexOf(';'); + String json = s.substring(p + 1); + AppExecId parsed = mapper.readValue(json, AppExecId.class); + return parsed; + } + + private static final String APP_KEY_PREFIX = "AppExecShuffleInfo"; + + @VisibleForTesting + static ConcurrentMap<AppExecId, ExecutorShuffleInfo> reloadRegisteredExecutors(DB db) + throws IOException { + ConcurrentMap<AppExecId, ExecutorShuffleInfo> registeredExecutors = Maps.newConcurrentMap(); + if (db != null) { + DBIterator itr = db.iterator(); + itr.seek(APP_KEY_PREFIX.getBytes(Charsets.UTF_8)); + while (itr.hasNext()) { + Map.Entry<byte[], byte[]> e = itr.next(); + String key = new String(e.getKey(), Charsets.UTF_8); + if (!key.startsWith(APP_KEY_PREFIX)) + break; + AppExecId id = parseDbAppExecKey(key); + ExecutorShuffleInfo shuffleInfo = + mapper.readValue(new String(e.getValue(), Charsets.UTF_8), ExecutorShuffleInfo.class); + registeredExecutors.put(id, shuffleInfo); + } + } + return registeredExecutors; + } + + private static class LevelDBLogger implements org.iq80.leveldb.Logger { + private static final Logger LOG = LoggerFactory.getLogger(LevelDBLogger.class); + + @Override + public void log(String message) { + LOG.info(message); + } + } + + private static final StoreVersion CURRENT_VERSION = new StoreVersion(1,0); + private static void checkVersion(DB db) throws IOException { + byte[] bytes = db.get(StoreVersion.KEY); + if (bytes == null) { + storeVersion(db); + } else if (bytes.length != 8) { + throw new IOException("unexpected version format"); + } else { + DataInputStream in = new DataInputStream(new ByteArrayInputStream(bytes)); + int major = in.readInt(); + int minor = in.readInt(); + StoreVersion version = new StoreVersion(major, minor); + if (version.major != CURRENT_VERSION.major) { + throw new IOException("cannot read state DB with version " + version + ", incompatible " + + "with current version " + CURRENT_VERSION); + } + storeVersion(db); + } + } + + private static void storeVersion(DB db) throws IOException { + ByteArrayOutputStream bytesOut = new ByteArrayOutputStream(); + DataOutputStream out = new DataOutputStream(bytesOut); + out.writeInt(CURRENT_VERSION.major); + out.writeInt(CURRENT_VERSION.minor); + out.close(); + db.put(StoreVersion.KEY, bytesOut.toByteArray()); + } + + + private static class StoreVersion { + + final static byte[] KEY = "StoreVersion".getBytes(Charsets.UTF_8); + + final int major; + final int minor; + + StoreVersion(int major, int minor) { + this.major = major; + this.minor = minor; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + StoreVersion that = (StoreVersion) o; + + if (major != that.major) return false; --- End diff -- `return major == that.major && minor == that.minor;`
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. --- --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org