|
@@ -42,8 +42,11 @@ import org.apache.hadoop.yarn.proto.YarnProtos.LocalResourceProto;
|
|
|
import org.apache.hadoop.yarn.proto.YarnServerCommonProtos.MasterKeyProto;
|
|
|
import org.apache.hadoop.yarn.proto.YarnServerNodemanagerRecoveryProtos.DeletionServiceDeleteTaskProto;
|
|
|
import org.apache.hadoop.yarn.proto.YarnServerNodemanagerRecoveryProtos.LocalizedResourceProto;
|
|
|
+import org.apache.hadoop.yarn.proto.YarnServerNodemanagerRecoveryProtos.NMDBSchemaVersionProto;
|
|
|
import org.apache.hadoop.yarn.server.api.records.MasterKey;
|
|
|
import org.apache.hadoop.yarn.server.api.records.impl.pb.MasterKeyPBImpl;
|
|
|
+import org.apache.hadoop.yarn.server.nodemanager.recovery.records.NMDBSchemaVersion;
|
|
|
+import org.apache.hadoop.yarn.server.nodemanager.recovery.records.impl.pb.NMDBSchemaVersionPBImpl;
|
|
|
import org.apache.hadoop.yarn.server.utils.LeveldbIterator;
|
|
|
import org.apache.hadoop.yarn.util.ConverterUtils;
|
|
|
import org.fusesource.leveldbjni.JniDBFactory;
|
|
@@ -54,14 +57,18 @@ import org.iq80.leveldb.Logger;
|
|
|
import org.iq80.leveldb.Options;
|
|
|
import org.iq80.leveldb.WriteBatch;
|
|
|
|
|
|
+import com.google.common.annotations.VisibleForTesting;
|
|
|
+
|
|
|
public class NMLeveldbStateStoreService extends NMStateStoreService {
|
|
|
|
|
|
public static final Log LOG =
|
|
|
LogFactory.getLog(NMLeveldbStateStoreService.class);
|
|
|
|
|
|
private static final String DB_NAME = "yarn-nm-state";
|
|
|
- private static final String DB_SCHEMA_VERSION_KEY = "schema-version";
|
|
|
- private static final String DB_SCHEMA_VERSION = "1.0";
|
|
|
+ private static final String DB_SCHEMA_VERSION_KEY = "nm-schema-version";
|
|
|
+
|
|
|
+ private static final NMDBSchemaVersion CURRENT_VERSION_INFO = NMDBSchemaVersion
|
|
|
+ .newInstance(1, 0);
|
|
|
|
|
|
private static final String DELETION_TASK_KEY_PREFIX =
|
|
|
"DeletionService/deltask_";
|
|
@@ -475,22 +482,16 @@ public class NMLeveldbStateStoreService extends NMStateStoreService {
|
|
|
options.logger(new LeveldbLogger());
|
|
|
LOG.info("Using state database at " + storeRoot + " for recovery");
|
|
|
File dbfile = new File(storeRoot.toString());
|
|
|
- byte[] schemaVersionData = null;
|
|
|
try {
|
|
|
db = JniDBFactory.factory.open(dbfile, options);
|
|
|
- try {
|
|
|
- schemaVersionData = db.get(bytes(DB_SCHEMA_VERSION_KEY));
|
|
|
- } catch (DBException e) {
|
|
|
- throw new IOException(e.getMessage(), e);
|
|
|
- }
|
|
|
} catch (NativeDB.DBException e) {
|
|
|
if (e.isNotFound() || e.getMessage().contains(" does not exist ")) {
|
|
|
LOG.info("Creating state database at " + dbfile);
|
|
|
options.createIfMissing(true);
|
|
|
try {
|
|
|
db = JniDBFactory.factory.open(dbfile, options);
|
|
|
- schemaVersionData = bytes(DB_SCHEMA_VERSION);
|
|
|
- db.put(bytes(DB_SCHEMA_VERSION_KEY), schemaVersionData);
|
|
|
+ // store version
|
|
|
+ storeVersion();
|
|
|
} catch (DBException dbErr) {
|
|
|
throw new IOException(dbErr.getMessage(), dbErr);
|
|
|
}
|
|
@@ -498,16 +499,7 @@ public class NMLeveldbStateStoreService extends NMStateStoreService {
|
|
|
throw e;
|
|
|
}
|
|
|
}
|
|
|
- if (schemaVersionData != null) {
|
|
|
- String schemaVersion = asString(schemaVersionData);
|
|
|
- // only support exact schema matches for now
|
|
|
- if (!DB_SCHEMA_VERSION.equals(schemaVersion)) {
|
|
|
- throw new IOException("Incompatible state database schema, found "
|
|
|
- + schemaVersion + " expected " + DB_SCHEMA_VERSION);
|
|
|
- }
|
|
|
- } else {
|
|
|
- throw new IOException("State database schema version not found");
|
|
|
- }
|
|
|
+ checkVersion();
|
|
|
}
|
|
|
|
|
|
private Path createStorageDir(Configuration conf) throws IOException {
|
|
@@ -532,4 +524,68 @@ public class NMLeveldbStateStoreService extends NMStateStoreService {
|
|
|
LOG.info(message);
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ NMDBSchemaVersion loadVersion() throws IOException {
|
|
|
+ byte[] data = db.get(bytes(DB_SCHEMA_VERSION_KEY));
|
|
|
+ // if version is not stored previously, treat it as 1.0.
|
|
|
+ if (data == null || data.length == 0) {
|
|
|
+ return NMDBSchemaVersion.newInstance(1, 0);
|
|
|
+ }
|
|
|
+ NMDBSchemaVersion version =
|
|
|
+ new NMDBSchemaVersionPBImpl(NMDBSchemaVersionProto.parseFrom(data));
|
|
|
+ return version;
|
|
|
+ }
|
|
|
+
|
|
|
+ private void storeVersion() throws IOException {
|
|
|
+ dbStoreVersion(CURRENT_VERSION_INFO);
|
|
|
+ }
|
|
|
+
|
|
|
+ // Only used for test
|
|
|
+ @VisibleForTesting
|
|
|
+ void storeVersion(NMDBSchemaVersion state) throws IOException {
|
|
|
+ dbStoreVersion(state);
|
|
|
+ }
|
|
|
+
|
|
|
+ private void dbStoreVersion(NMDBSchemaVersion state) throws IOException {
|
|
|
+ String key = DB_SCHEMA_VERSION_KEY;
|
|
|
+ byte[] data =
|
|
|
+ ((NMDBSchemaVersionPBImpl) state).getProto().toByteArray();
|
|
|
+ try {
|
|
|
+ db.put(bytes(key), data);
|
|
|
+ } catch (DBException e) {
|
|
|
+ throw new IOException(e.getMessage(), e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ NMDBSchemaVersion getCurrentVersion() {
|
|
|
+ return CURRENT_VERSION_INFO;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 1) Versioning scheme: major.minor. For e.g. 1.0, 1.1, 1.2...1.25, 2.0 etc.
|
|
|
+ * 2) Any incompatible change of state-store is a major upgrade, and any
|
|
|
+ * compatible change of state-store is a minor upgrade.
|
|
|
+ * 3) Within a minor upgrade, say 1.1 to 1.2:
|
|
|
+ * overwrite the version info and proceed as normal.
|
|
|
+ * 4) Within a major upgrade, say 1.2 to 2.0:
|
|
|
+ * throw exception and indicate user to use a separate upgrade tool to
|
|
|
+ * upgrade NM state or remove incompatible old state.
|
|
|
+ */
|
|
|
+ private void checkVersion() throws IOException {
|
|
|
+ NMDBSchemaVersion loadedVersion = loadVersion();
|
|
|
+ LOG.info("Loaded NM state version info " + loadedVersion);
|
|
|
+ if (loadedVersion != null && loadedVersion.equals(getCurrentVersion())) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (loadedVersion.isCompatibleTo(getCurrentVersion())) {
|
|
|
+ LOG.info("Storing NM state version info " + getCurrentVersion());
|
|
|
+ storeVersion();
|
|
|
+ } else {
|
|
|
+ throw new IOException(
|
|
|
+ "Incompatible version for NM state: expecting NM state version "
|
|
|
+ + getCurrentVersion() + ", but loading version " + loadedVersion);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
}
|