foundationdb/fdbcli/IdempotencyIdsCommand.cpp

82 lines
2.4 KiB
C++

/*
* IdempotencyIdsCommand.cpp
*
* This source file is part of the FoundationDB open source project
*
* Copyright 2013-2026 Apple Inc. and the FoundationDB project authors
*
* 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.h"
#include "fdbclient/IdempotencyId.h"
#include "fdbclient/JsonBuilder.h"
#include "fdbclient/json_spirit/json_spirit_reader_template.h"
namespace {
Optional<double> parseAgeValue(StringRef token) {
try {
return std::stod(token.toString());
} catch (...) {
return {};
}
}
} // namespace
namespace fdb_cli {
Future<bool> idempotencyIdsCommandActor(Database db, std::vector<StringRef> const& tokens) {
if (tokens.size() < 2 || tokens.size() > 3) {
printUsage(tokens[0]);
co_return false;
} else {
auto const action = tokens[1];
if (action == "status"_sr) {
if (tokens.size() != 2) {
printUsage(tokens[0]);
co_return false;
}
JsonBuilderObject status = co_await getIdmpKeyStatus(db);
fmt::print("{}\n", status.getJson());
co_return true;
} else if (action == "clear"_sr) {
if (tokens.size() != 3) {
printUsage(tokens[0]);
co_return false;
}
auto const age = parseAgeValue(tokens[2]);
if (!age.present()) {
printUsage(tokens[0]);
co_return false;
}
co_await cleanIdempotencyIds(db, age.get());
fmt::print("Successfully cleared idempotency IDs.\n");
co_return true;
} else {
printUsage(tokens[0]);
co_return false;
}
}
}
CommandFactory idempotencyIdsCommandFactory(
"idempotencyids",
CommandHelp(
"idempotencyids [status | clear <min_age_seconds>]",
"View status of idempotency ids, or reclaim space used by idempotency ids older than the given age",
"View status of idempotency ids currently in the cluster, or reclaim space by clearing all the idempotency ids "
"older than min_age_seconds (which will expire all transaction versions older than this age)\n"));
} // namespace fdb_cli