2021-07-14 01:21:55 +08:00
|
|
|
/*
|
|
|
|
* LockCommand.actor.cpp
|
|
|
|
*
|
|
|
|
* This source file is part of the FoundationDB open source project
|
|
|
|
*
|
2022-03-22 04:36:23 +08:00
|
|
|
* Copyright 2013-2022 Apple Inc. and the FoundationDB project authors
|
2021-07-14 01:21:55 +08:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "fdbcli/fdbcli.actor.h"
|
|
|
|
|
|
|
|
#include "fdbclient/FDBOptions.g.h"
|
|
|
|
#include "fdbclient/IClientApi.h"
|
|
|
|
#include "fdbclient/Knobs.h"
|
|
|
|
#include "fdbclient/Schemas.h"
|
|
|
|
|
|
|
|
#include "flow/Arena.h"
|
|
|
|
#include "flow/FastRef.h"
|
|
|
|
#include "flow/ThreadHelper.actor.h"
|
|
|
|
#include "flow/actorcompiler.h" // This must be the last #include.
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2021-09-10 06:59:43 +08:00
|
|
|
ACTOR Future<bool> lockDatabase(Reference<IDatabase> db, UID id) {
|
2021-07-14 01:21:55 +08:00
|
|
|
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()));
|
2021-09-10 06:59:43 +08:00
|
|
|
printf("Database locked.\n");
|
|
|
|
return true;
|
2021-07-14 01:21:55 +08:00
|
|
|
} catch (Error& e) {
|
2021-09-10 06:59:43 +08:00
|
|
|
state Error err(e);
|
2021-07-14 01:21:55 +08:00
|
|
|
if (e.code() == error_code_database_locked)
|
|
|
|
throw e;
|
2021-09-10 06:59:43 +08:00
|
|
|
else if (e.code() == error_code_special_keys_api_failure) {
|
2021-09-10 05:48:20 +08:00
|
|
|
std::string errorMsgStr = wait(fdb_cli::getSpecialKeysFailureErrorMessage(tr));
|
2021-09-10 06:59:43 +08:00
|
|
|
fprintf(stderr, "%s\n", errorMsgStr.c_str());
|
|
|
|
return false;
|
2021-07-14 01:21:55 +08:00
|
|
|
}
|
|
|
|
wait(safeThreadFutureToFuture(tr->onError(err)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace fdb_cli {
|
|
|
|
|
|
|
|
const KeyRef lockSpecialKey = LiteralStringRef("\xff\xff/management/db_locked");
|
|
|
|
|
|
|
|
ACTOR Future<bool> lockCommandActor(Reference<IDatabase> db, std::vector<StringRef> tokens) {
|
|
|
|
if (tokens.size() != 1) {
|
|
|
|
printUsage(tokens[0]);
|
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
state UID lockUID = deterministicRandom()->randomUniqueID();
|
|
|
|
printf("Locking database with lockUID: %s\n", lockUID.toString().c_str());
|
2021-09-10 06:59:43 +08:00
|
|
|
bool result = wait((lockDatabase(db, lockUID)));
|
|
|
|
return result;
|
2021-07-14 01:21:55 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 06:59:43 +08:00
|
|
|
ACTOR Future<bool> unlockDatabaseActor(Reference<IDatabase> db, UID uid) {
|
2021-07-14 01:21:55 +08:00
|
|
|
state Reference<ITransaction> tr = db->createTransaction();
|
|
|
|
loop {
|
|
|
|
tr->setOption(FDBTransactionOptions::SPECIAL_KEY_SPACE_ENABLE_WRITES);
|
|
|
|
try {
|
|
|
|
state ThreadFuture<Optional<Value>> valF = tr->get(fdb_cli::lockSpecialKey);
|
|
|
|
Optional<Value> val = wait(safeThreadFutureToFuture(valF));
|
|
|
|
|
|
|
|
if (!val.present())
|
2021-09-10 06:59:43 +08:00
|
|
|
return true;
|
2021-07-14 01:21:55 +08:00
|
|
|
|
|
|
|
if (val.present() && UID::fromString(val.get().toString()) != uid) {
|
2021-09-10 06:59:43 +08:00
|
|
|
printf("Unable to unlock database. Make sure to unlock with the correct lock UID.\n");
|
|
|
|
return false;
|
2021-07-14 01:21:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
tr->clear(fdb_cli::lockSpecialKey);
|
|
|
|
wait(safeThreadFutureToFuture(tr->commit()));
|
2021-09-10 06:59:43 +08:00
|
|
|
printf("Database unlocked.\n");
|
|
|
|
return true;
|
2021-07-14 01:21:55 +08:00
|
|
|
} catch (Error& e) {
|
|
|
|
state Error err(e);
|
|
|
|
if (e.code() == error_code_special_keys_api_failure) {
|
2021-09-10 05:48:20 +08:00
|
|
|
std::string errorMsgStr = wait(fdb_cli::getSpecialKeysFailureErrorMessage(tr));
|
2021-09-10 06:59:43 +08:00
|
|
|
fprintf(stderr, "%s\n", errorMsgStr.c_str());
|
|
|
|
return false;
|
2021-07-14 01:21:55 +08:00
|
|
|
}
|
|
|
|
wait(safeThreadFutureToFuture(tr->onError(err)));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CommandFactory lockFactory(
|
|
|
|
"lock",
|
|
|
|
CommandHelp("lock",
|
|
|
|
"lock the database with a randomly generated lockUID",
|
|
|
|
"Randomly generates a lockUID, prints this lockUID, and then uses the lockUID to lock the database."));
|
|
|
|
|
|
|
|
CommandFactory unlockFactory(
|
|
|
|
"unlock",
|
|
|
|
CommandHelp("unlock <UID>",
|
|
|
|
"unlock the database with the provided lockUID",
|
|
|
|
"Unlocks the database with the provided lockUID. This is a potentially dangerous operation, so the "
|
|
|
|
"user will be asked to enter a passphrase to confirm their intent."));
|
|
|
|
} // namespace fdb_cli
|