2021-10-22 07:24:39 +08:00
|
|
|
/*
|
|
|
|
* BlobRangeCommand.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-10-22 07:24:39 +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/ManagementAPI.actor.h"
|
2022-08-04 18:21:06 +08:00
|
|
|
#include "fdbclient/NativeAPI.actor.h"
|
2021-10-22 07:24:39 +08:00
|
|
|
|
|
|
|
#include "flow/Arena.h"
|
|
|
|
#include "flow/FastRef.h"
|
|
|
|
#include "flow/ThreadHelper.actor.h"
|
|
|
|
#include "flow/actorcompiler.h" // This must be the last #include.
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2022-07-29 23:20:45 +08:00
|
|
|
ACTOR Future<Version> getLatestReadVersion(Database db) {
|
|
|
|
state Transaction tr(db);
|
|
|
|
loop {
|
|
|
|
try {
|
|
|
|
Version rv = wait(tr.getReadVersion());
|
|
|
|
fmt::print("Resolved latest read version as {0}\n", rv);
|
|
|
|
return rv;
|
|
|
|
} catch (Error& e) {
|
|
|
|
wait(tr.onError(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// print after delay if not cancelled
|
|
|
|
ACTOR Future<Void> printAfterDelay(double delaySeconds, std::string message) {
|
|
|
|
wait(delay(delaySeconds));
|
|
|
|
fmt::print("{}\n", message);
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2022-08-12 06:22:32 +08:00
|
|
|
ACTOR Future<Void> doBlobPurge(Database db, Key startKey, Key endKey, Optional<Version> version, bool force) {
|
2022-07-29 23:20:45 +08:00
|
|
|
state Version purgeVersion;
|
|
|
|
if (version.present()) {
|
|
|
|
purgeVersion = version.get();
|
|
|
|
} else {
|
|
|
|
wait(store(purgeVersion, getLatestReadVersion(db)));
|
|
|
|
}
|
|
|
|
|
2022-08-12 06:22:32 +08:00
|
|
|
state Key purgeKey = wait(db->purgeBlobGranules(KeyRange(KeyRangeRef(startKey, endKey)), purgeVersion, {}, force));
|
2022-07-29 23:20:45 +08:00
|
|
|
|
|
|
|
fmt::print("Blob purge registered for [{0} - {1}) @ {2}\n", startKey.printable(), endKey.printable(), purgeVersion);
|
|
|
|
|
|
|
|
state Future<Void> printWarningActor = printAfterDelay(
|
|
|
|
5.0, "Waiting for purge to complete. (interrupting this wait with CTRL+C will not cancel the purge)");
|
|
|
|
wait(db->waitPurgeGranulesComplete(purgeKey));
|
|
|
|
|
|
|
|
fmt::print("Blob purge complete for [{0} - {1}) @ {2}\n", startKey.printable(), endKey.printable(), purgeVersion);
|
|
|
|
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2022-08-04 07:11:25 +08:00
|
|
|
ACTOR Future<Version> checkBlobSubrange(Database db, KeyRange keyRange, Optional<Version> version) {
|
|
|
|
state Transaction tr(db);
|
|
|
|
state Version readVersionOut = invalidVersion;
|
|
|
|
loop {
|
|
|
|
try {
|
|
|
|
wait(success(tr.readBlobGranules(keyRange, 0, version, &readVersionOut)));
|
|
|
|
return readVersionOut;
|
|
|
|
} catch (Error& e) {
|
|
|
|
wait(tr.onError(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-29 23:20:45 +08:00
|
|
|
ACTOR Future<Void> doBlobCheck(Database db, Key startKey, Key endKey, Optional<Version> version) {
|
|
|
|
state Transaction tr(db);
|
|
|
|
state Version readVersionOut = invalidVersion;
|
|
|
|
state double elapsed = -timer_monotonic();
|
2022-08-04 07:11:25 +08:00
|
|
|
state KeyRange range = KeyRange(KeyRangeRef(startKey, endKey));
|
|
|
|
state Standalone<VectorRef<KeyRangeRef>> allRanges;
|
2022-07-29 23:20:45 +08:00
|
|
|
loop {
|
|
|
|
try {
|
2022-08-04 07:11:25 +08:00
|
|
|
wait(store(allRanges, tr.getBlobGranuleRanges(range)));
|
2022-07-29 23:20:45 +08:00
|
|
|
break;
|
|
|
|
} catch (Error& e) {
|
|
|
|
wait(tr.onError(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-04 07:11:25 +08:00
|
|
|
if (allRanges.empty()) {
|
|
|
|
fmt::print("ERROR: No blob ranges for [{0} - {1})\n", startKey.printable(), endKey.printable());
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
fmt::print("Loaded {0} blob ranges to check\n", allRanges.size());
|
|
|
|
state std::vector<Future<Version>> checkParts;
|
2022-08-06 00:19:17 +08:00
|
|
|
// Chunk up to smaller ranges than this limit. Must be smaller than BG_TOO_MANY_GRANULES to not hit the limit
|
|
|
|
int maxChunkSize = CLIENT_KNOBS->BG_TOO_MANY_GRANULES / 2;
|
2022-08-04 07:11:25 +08:00
|
|
|
KeyRange currentChunk;
|
|
|
|
int currentChunkSize = 0;
|
|
|
|
for (auto& it : allRanges) {
|
|
|
|
if (currentChunkSize == maxChunkSize) {
|
|
|
|
checkParts.push_back(checkBlobSubrange(db, currentChunk, version));
|
|
|
|
currentChunkSize = 0;
|
|
|
|
}
|
|
|
|
if (currentChunkSize == 0) {
|
|
|
|
currentChunk = it;
|
|
|
|
} else if (it.begin != currentChunk.end) {
|
|
|
|
fmt::print("ERROR: Blobrange check failed, gap in blob ranges from [{0} - {1})\n",
|
|
|
|
currentChunk.end.printable(),
|
|
|
|
it.begin.printable());
|
|
|
|
return Void();
|
|
|
|
} else {
|
|
|
|
currentChunk = KeyRangeRef(currentChunk.begin, it.end);
|
|
|
|
}
|
|
|
|
currentChunkSize++;
|
|
|
|
}
|
|
|
|
checkParts.push_back(checkBlobSubrange(db, currentChunk, version));
|
|
|
|
|
|
|
|
wait(waitForAll(checkParts));
|
|
|
|
readVersionOut = checkParts.back().get();
|
|
|
|
|
|
|
|
elapsed += timer_monotonic();
|
|
|
|
|
2022-07-29 23:20:45 +08:00
|
|
|
fmt::print("Blob check complete for [{0} - {1}) @ {2} in {3:.6f} seconds\n",
|
|
|
|
startKey.printable(),
|
|
|
|
endKey.printable(),
|
|
|
|
readVersionOut,
|
|
|
|
elapsed);
|
|
|
|
return Void();
|
|
|
|
}
|
|
|
|
|
2021-10-22 07:24:39 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace fdb_cli {
|
|
|
|
|
2022-02-20 07:25:51 +08:00
|
|
|
ACTOR Future<bool> blobRangeCommandActor(Database localDb,
|
|
|
|
Optional<TenantMapEntry> tenantEntry,
|
|
|
|
std::vector<StringRef> tokens) {
|
2021-10-22 07:24:39 +08:00
|
|
|
// enables blob writing for the given range
|
2022-07-29 23:20:45 +08:00
|
|
|
if (tokens.size() != 4 && tokens.size() != 5) {
|
2021-10-22 07:24:39 +08:00
|
|
|
printUsage(tokens[0]);
|
|
|
|
return false;
|
2022-02-20 07:25:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Key begin;
|
|
|
|
Key end;
|
|
|
|
|
|
|
|
if (tenantEntry.present()) {
|
|
|
|
begin = tokens[2].withPrefix(tenantEntry.get().prefix);
|
|
|
|
end = tokens[3].withPrefix(tenantEntry.get().prefix);
|
|
|
|
} else {
|
|
|
|
begin = tokens[2];
|
|
|
|
end = tokens[3];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (end > LiteralStringRef("\xff")) {
|
2021-10-22 07:24:39 +08:00
|
|
|
// TODO is this something we want?
|
2022-07-29 23:20:45 +08:00
|
|
|
fmt::print("Cannot blobbify system keyspace! Problematic End Key: {0}\n", tokens[3].printable());
|
2021-10-22 07:24:39 +08:00
|
|
|
return false;
|
|
|
|
} else if (tokens[2] >= tokens[3]) {
|
2022-07-29 23:20:45 +08:00
|
|
|
fmt::print("Invalid blob range [{0} - {1})\n", tokens[2].printable(), tokens[3].printable());
|
2021-10-22 07:24:39 +08:00
|
|
|
} else {
|
2022-07-29 23:20:45 +08:00
|
|
|
if (tokencmp(tokens[1], "start") || tokencmp(tokens[1], "stop")) {
|
2022-08-04 19:00:23 +08:00
|
|
|
state bool starting = tokencmp(tokens[1], "start");
|
2022-07-29 23:20:45 +08:00
|
|
|
if (tokens.size() > 4) {
|
|
|
|
printUsage(tokens[0]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fmt::print("{0} blobbify range for [{1} - {2})\n",
|
|
|
|
starting ? "Starting" : "Stopping",
|
|
|
|
tokens[2].printable().c_str(),
|
|
|
|
tokens[3].printable().c_str());
|
2022-08-04 19:00:23 +08:00
|
|
|
state bool success = false;
|
2022-08-04 18:21:06 +08:00
|
|
|
if (starting) {
|
2022-08-04 19:00:23 +08:00
|
|
|
wait(store(success, localDb->blobbifyRange(KeyRangeRef(begin, end))));
|
2022-08-04 18:21:06 +08:00
|
|
|
} else {
|
2022-08-04 19:00:23 +08:00
|
|
|
wait(store(success, localDb->unblobbifyRange(KeyRangeRef(begin, end))));
|
2022-08-04 18:21:06 +08:00
|
|
|
}
|
2022-08-04 19:00:23 +08:00
|
|
|
if (!success) {
|
|
|
|
fmt::print("{0} blobbify range for [{1} - {2}) failed\n",
|
|
|
|
starting ? "Starting" : "Stopping",
|
|
|
|
tokens[2].printable().c_str(),
|
|
|
|
tokens[3].printable().c_str());
|
|
|
|
}
|
|
|
|
return success;
|
2022-08-12 06:22:32 +08:00
|
|
|
} else if (tokencmp(tokens[1], "purge") || tokencmp(tokens[1], "forcepurge") || tokencmp(tokens[1], "check")) {
|
|
|
|
bool purge = tokencmp(tokens[1], "purge") || tokencmp(tokens[1], "forcepurge");
|
|
|
|
bool forcePurge = tokencmp(tokens[1], "forcepurge");
|
2022-07-29 23:20:45 +08:00
|
|
|
|
|
|
|
Optional<Version> version;
|
|
|
|
if (tokens.size() > 4) {
|
|
|
|
Version v;
|
|
|
|
int n = 0;
|
|
|
|
if (sscanf(tokens[4].toString().c_str(), "%" PRId64 "%n", &v, &n) != 1 || n != tokens[4].size()) {
|
|
|
|
printUsage(tokens[0]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
version = v;
|
|
|
|
}
|
|
|
|
|
2022-08-12 06:22:32 +08:00
|
|
|
fmt::print("{0} blob range [{1} - {2}){3}",
|
2022-07-29 23:20:45 +08:00
|
|
|
purge ? "Purging" : "Checking",
|
|
|
|
tokens[2].printable(),
|
2022-08-12 06:22:32 +08:00
|
|
|
tokens[3].printable(),
|
|
|
|
forcePurge ? " (force)" : "");
|
2022-07-29 23:20:45 +08:00
|
|
|
if (version.present()) {
|
|
|
|
fmt::print(" @ {0}", version.get());
|
|
|
|
}
|
|
|
|
fmt::print("\n");
|
|
|
|
|
|
|
|
if (purge) {
|
2022-08-12 06:22:32 +08:00
|
|
|
wait(doBlobPurge(localDb, begin, end, version, forcePurge));
|
2022-07-29 23:20:45 +08:00
|
|
|
} else {
|
|
|
|
wait(doBlobCheck(localDb, begin, end, version));
|
|
|
|
}
|
2021-10-22 07:24:39 +08:00
|
|
|
} else {
|
|
|
|
printUsage(tokens[0]);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-08-12 06:22:32 +08:00
|
|
|
CommandFactory blobRangeFactory(
|
|
|
|
"blobrange",
|
|
|
|
CommandHelp("blobrange <start|stop|check|purge|forcepurge> <startkey> <endkey> [version]", "", ""));
|
2021-10-22 07:24:39 +08:00
|
|
|
} // namespace fdb_cli
|