YARN-9998. Code cleanup in LeveldbConfigurationStore. Contributed by Benjamin Teke

This commit is contained in:
Szilard Nemeth 2020-04-24 11:15:53 +02:00
parent 5ebfb7203a
commit 3b67dc24aa
1 changed files with 55 additions and 50 deletions

View File

@ -54,6 +54,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Timer; import java.util.Timer;
import java.util.TimerTask; import java.util.TimerTask;
import java.util.function.Consumer;
import static org.fusesource.leveldbjni.JniDBFactory.bytes; import static org.fusesource.leveldbjni.JniDBFactory.bytes;
@ -72,22 +73,23 @@ public class LeveldbConfigurationStore extends YarnConfigurationStore {
private static final String CONF_VERSION_KEY = "conf-version"; private static final String CONF_VERSION_KEY = "conf-version";
private DB db; private DB db;
private DB versiondb; private DB versionDb;
private long maxLogs; private long maxLogs;
private Configuration conf; private Configuration conf;
private LogMutation pendingMutation; private LogMutation pendingMutation;
private Configuration initSchedConf;
@VisibleForTesting @VisibleForTesting
protected static final Version CURRENT_VERSION_INFO = Version protected static final Version CURRENT_VERSION_INFO = Version
.newInstance(0, 1); .newInstance(0, 1);
private Timer compactionTimer;
private long compactionIntervalMsec; private long compactionIntervalMsec;
@Override @Override
public void initialize(Configuration config, Configuration schedConf, public void initialize(Configuration config, Configuration schedConf,
RMContext rmContext) throws IOException { RMContext rmContext) throws IOException {
this.conf = config; this.conf = config;
this.initSchedConf = schedConf;
try { try {
initDatabase(schedConf); initDatabase();
this.maxLogs = config.getLong( this.maxLogs = config.getLong(
YarnConfiguration.RM_SCHEDCONF_MAX_LOGS, YarnConfiguration.RM_SCHEDCONF_MAX_LOGS,
YarnConfiguration.DEFAULT_RM_SCHEDCONF_LEVELDB_MAX_LOGS); YarnConfiguration.DEFAULT_RM_SCHEDCONF_LEVELDB_MAX_LOGS);
@ -108,7 +110,15 @@ public class LeveldbConfigurationStore extends YarnConfigurationStore {
fs.delete(getStorageDir(DB_NAME), true); fs.delete(getStorageDir(DB_NAME), true);
} }
private void initDatabase(Configuration config) throws Exception { private void initDatabase() throws Exception {
Path confVersion = createStorageDir(CONF_VERSION_NAME);
Options confOptions = new Options();
confOptions.createIfMissing(false);
File confVersionFile = new File(confVersion.toString());
versionDb = initDatabaseHelper(confVersionFile, confOptions,
this::initVersionDb);
Path storeRoot = createStorageDir(DB_NAME); Path storeRoot = createStorageDir(DB_NAME);
Options options = new Options(); Options options = new Options();
options.createIfMissing(false); options.createIfMissing(false);
@ -144,49 +154,37 @@ public class LeveldbConfigurationStore extends YarnConfigurationStore {
return key; return key;
} }
}); });
LOG.info("Using conf database at {}", storeRoot);
File dbFile = new File(storeRoot.toString());
db = initDatabaseHelper(dbFile, options, this::initDb);
}
Path confVersion = createStorageDir(CONF_VERSION_NAME); private void initVersionDb(DB database) {
Options confOptions = new Options(); database.put(bytes(CONF_VERSION_KEY), bytes(String.valueOf(0)));
confOptions.createIfMissing(false); }
LOG.info("Using conf version at " + confVersion);
File confVersionFile = new File(confVersion.toString()); private void initDb(DB database) {
try { WriteBatch initBatch = database.createWriteBatch();
versiondb = JniDBFactory.factory.open(confVersionFile, confOptions); for (Map.Entry<String, String> kv : initSchedConf) {
} catch (NativeDB.DBException e) { initBatch.put(bytes(kv.getKey()), bytes(kv.getValue()));
if (e.isNotFound() || e.getMessage().contains(" does not exist ")) {
LOG.info("Creating conf version at " + confVersionFile);
confOptions.createIfMissing(true);
try {
versiondb = JniDBFactory.factory.open(confVersionFile, confOptions);
versiondb.put(bytes(CONF_VERSION_KEY), bytes(String.valueOf(0)));
} catch (DBException dbErr) {
throw new IOException(dbErr.getMessage(), dbErr);
}
} else {
throw e;
}
} }
database.write(initBatch);
increaseConfigVersion();
}
private DB initDatabaseHelper(File configurationFile, Options options,
LOG.info("Using conf database at " + storeRoot); Consumer<DB> initMethod) throws Exception {
File dbfile = new File(storeRoot.toString()); DB database;
try { try {
db = JniDBFactory.factory.open(dbfile, options); database = JniDBFactory.factory.open(configurationFile, options);
} catch (NativeDB.DBException e) { } catch (NativeDB.DBException e) {
if (e.isNotFound() || e.getMessage().contains(" does not exist ")) { if (e.isNotFound() || e.getMessage().contains(" does not exist ")) {
LOG.info("Creating conf database at " + dbfile); LOG.info("Creating configuration version/database at {}",
configurationFile);
options.createIfMissing(true); options.createIfMissing(true);
try { try {
db = JniDBFactory.factory.open(dbfile, options); database = JniDBFactory.factory.open(configurationFile, options);
// Write the initial scheduler configuration initMethod.accept(database);
WriteBatch initBatch = db.createWriteBatch();
for (Map.Entry<String, String> kv : config) {
initBatch.put(bytes(kv.getKey()), bytes(kv.getValue()));
}
db.write(initBatch);
long configVersion = getConfigVersion() + 1L;
versiondb.put(bytes(CONF_VERSION_KEY),
bytes(String.valueOf(configVersion)));
} catch (DBException dbErr) { } catch (DBException dbErr) {
throw new IOException(dbErr.getMessage(), dbErr); throw new IOException(dbErr.getMessage(), dbErr);
} }
@ -194,6 +192,8 @@ public class LeveldbConfigurationStore extends YarnConfigurationStore {
throw e; throw e;
} }
} }
return database;
} }
private Path createStorageDir(String storageName) throws IOException { private Path createStorageDir(String storageName) throws IOException {
@ -217,8 +217,8 @@ public class LeveldbConfigurationStore extends YarnConfigurationStore {
if (db != null) { if (db != null) {
db.close(); db.close();
} }
if (versiondb != null) { if (versionDb != null) {
versiondb.close(); versionDb.close();
} }
} }
@ -245,9 +245,7 @@ public class LeveldbConfigurationStore extends YarnConfigurationStore {
updateBatch.put(bytes(changes.getKey()), bytes(changes.getValue())); updateBatch.put(bytes(changes.getKey()), bytes(changes.getValue()));
} }
} }
long configVersion = getConfigVersion() + 1L; increaseConfigVersion();
versiondb.put(bytes(CONF_VERSION_KEY),
bytes(String.valueOf(configVersion)));
} }
db.write(updateBatch); db.write(updateBatch);
pendingMutation = null; pendingMutation = null;
@ -263,6 +261,10 @@ public class LeveldbConfigurationStore extends YarnConfigurationStore {
} }
} }
// Because of type erasure casting to LinkedList<LogMutation> will be
// unchecked. A way around that would be to iterate over the logMutations
// which is overkill in this case.
@SuppressWarnings("unchecked")
private LinkedList<LogMutation> deserLogMutations(byte[] mutations) throws private LinkedList<LogMutation> deserLogMutations(byte[] mutations) throws
IOException { IOException {
if (mutations == null) { if (mutations == null) {
@ -293,9 +295,15 @@ public class LeveldbConfigurationStore extends YarnConfigurationStore {
return config; return config;
} }
private void increaseConfigVersion() {
long configVersion = getConfigVersion() + 1L;
versionDb.put(bytes(CONF_VERSION_KEY),
bytes(String.valueOf(configVersion)));
}
@Override @Override
public long getConfigVersion() { public long getConfigVersion() {
String version = new String(versiondb.get(bytes(CONF_VERSION_KEY)), String version = new String(versionDb.get(bytes(CONF_VERSION_KEY)),
StandardCharsets.UTF_8); StandardCharsets.UTF_8);
return Long.parseLong(version); return Long.parseLong(version);
} }
@ -305,18 +313,15 @@ public class LeveldbConfigurationStore extends YarnConfigurationStore {
return null; // unimplemented return null; // unimplemented
} }
// TODO below was taken from LeveldbRMStateStore, it can probably be
// refactored
private void startCompactionTimer() { private void startCompactionTimer() {
if (compactionIntervalMsec > 0) { if (compactionIntervalMsec > 0) {
compactionTimer = new Timer( Timer compactionTimer = new Timer(
this.getClass().getSimpleName() + " compaction timer", true); this.getClass().getSimpleName() + " compaction timer", true);
compactionTimer.schedule(new CompactionTimerTask(), compactionTimer.schedule(new CompactionTimerTask(),
compactionIntervalMsec, compactionIntervalMsec); compactionIntervalMsec, compactionIntervalMsec);
} }
} }
// TODO: following is taken from LeveldbRMStateStore
@Override @Override
public Version getConfStoreVersion() throws Exception { public Version getConfStoreVersion() throws Exception {
Version version = null; Version version = null;
@ -370,7 +375,7 @@ public class LeveldbConfigurationStore extends YarnConfigurationStore {
LOG.error("Error compacting database", e); LOG.error("Error compacting database", e);
} }
long duration = Time.monotonicNow() - start; long duration = Time.monotonicNow() - start;
LOG.info("Full compaction cycle completed in " + duration + " msec"); LOG.info("Full compaction cycle completed in {} msec", duration);
} }
} }
} }