Solve comments, refatctor the error handling part

This commit is contained in:
Chaoguang Lin 2021-09-09 15:59:43 -07:00
parent e81748e732
commit cb9dde358c
4 changed files with 22 additions and 29 deletions

View File

@ -32,22 +32,23 @@
namespace {
ACTOR Future<Void> lockDatabase(Reference<IDatabase> db, UID id) {
ACTOR Future<bool> lockDatabase(Reference<IDatabase> db, UID id) {
state Reference<ITransaction> tr = db->createTransaction();
loop {
tr->setOption(FDBTransactionOptions::SPECIAL_KEY_SPACE_ENABLE_WRITES);
try {
tr->set(fdb_cli::lockSpecialKey, id.toString());
wait(safeThreadFutureToFuture(tr->commit()));
return Void();
printf("Database locked.\n");
return true;
} catch (Error& e) {
state Error err(e);
if (e.code() == error_code_database_locked)
throw e;
state Error err(e);
if (e.code() == error_code_special_keys_api_failure) {
else if (e.code() == error_code_special_keys_api_failure) {
std::string errorMsgStr = wait(fdb_cli::getSpecialKeysFailureErrorMessage(tr));
if (errorMsgStr == "Database has already been locked")
throw database_locked();
fprintf(stderr, "%s\n", errorMsgStr.c_str());
return false;
}
wait(safeThreadFutureToFuture(tr->onError(err)));
}
@ -67,13 +68,12 @@ ACTOR Future<bool> lockCommandActor(Reference<IDatabase> db, std::vector<StringR
} else {
state UID lockUID = deterministicRandom()->randomUniqueID();
printf("Locking database with lockUID: %s\n", lockUID.toString().c_str());
wait((lockDatabase(db, lockUID)));
printf("Database locked.\n");
return true;
bool result = wait((lockDatabase(db, lockUID)));
return result;
}
}
ACTOR Future<Void> unlockDatabaseActor(Reference<IDatabase> db, UID uid) {
ACTOR Future<bool> unlockDatabaseActor(Reference<IDatabase> db, UID uid) {
state Reference<ITransaction> tr = db->createTransaction();
loop {
tr->setOption(FDBTransactionOptions::SPECIAL_KEY_SPACE_ENABLE_WRITES);
@ -82,23 +82,23 @@ ACTOR Future<Void> unlockDatabaseActor(Reference<IDatabase> db, UID uid) {
Optional<Value> val = wait(safeThreadFutureToFuture(valF));
if (!val.present())
return Void();
return true;
if (val.present() && UID::fromString(val.get().toString()) != uid) {
throw database_locked();
printf("Unable to unlock database. Make sure to unlock with the correct lock UID.\n");
return false;
}
tr->clear(fdb_cli::lockSpecialKey);
wait(safeThreadFutureToFuture(tr->commit()));
return Void();
printf("Database unlocked.\n");
return true;
} catch (Error& e) {
if (e.code() == error_code_database_locked)
throw e;
state Error err(e);
if (e.code() == error_code_special_keys_api_failure) {
std::string errorMsgStr = wait(fdb_cli::getSpecialKeysFailureErrorMessage(tr));
if (errorMsgStr == "Database has already been locked")
throw database_locked();
fprintf(stderr, "%s\n", errorMsgStr.c_str());
return false;
}
wait(safeThreadFutureToFuture(tr->onError(err)));
}

View File

@ -2276,16 +2276,9 @@ ACTOR Future<int> cli(CLIOptions opt, LineNoise* plinenoise) {
checkStatus(timeWarning(5.0, "\nWARNING: Long delay (Ctrl-C to interrupt)\n"), db, localDb);
if (input.present() && input.get() == passPhrase) {
UID unlockUID = UID::fromString(tokens[1].toString());
try {
wait(makeInterruptable(unlockDatabaseActor(db, unlockUID)));
printf("Database unlocked.\n");
} catch (Error& e) {
if (e.code() == error_code_database_locked) {
printf(
"Unable to unlock database. Make sure to unlock with the correct lock UID.\n");
}
throw e;
}
bool _result = wait(makeInterruptable(unlockDatabaseActor(db, unlockUID)));
if (!_result)
is_error = true;
} else {
fprintf(stderr, "ERROR: Incorrect passphrase entered.\n");
is_error = true;

View File

@ -128,7 +128,7 @@ ACTOR Future<bool> killCommandActor(Reference<IDatabase> db,
std::map<Key, std::pair<Value, ClientLeaderRegInterface>>* address_interface);
// lock/unlock command
ACTOR Future<bool> lockCommandActor(Reference<IDatabase> db, std::vector<StringRef> tokens);
ACTOR Future<Void> unlockDatabaseActor(Reference<IDatabase> db, UID uid);
ACTOR Future<bool> unlockDatabaseActor(Reference<IDatabase> db, UID uid);
// maintenance command
ACTOR Future<bool> setHealthyZone(Reference<IDatabase> db, StringRef zoneId, double seconds, bool printWarning = false);
ACTOR Future<bool> clearHealthyZone(Reference<IDatabase> db,

View File

@ -1322,7 +1322,7 @@ ACTOR Future<Optional<std::string>> lockDatabaseCommitActor(ReadYourWritesTransa
if (val.present() && BinaryReader::fromStringRef<UID>(val.get().substr(10), Unversioned()) != uid) {
// check database not locked
// if locked already, throw error
msg = ManagementAPIError::toJsonString(false, "lock", "Database has already been locked");
throw database_locked();
} else if (!val.present()) {
// lock database
ryw->getTransaction().atomicOp(databaseLockedKey,