From 8de3417f24ce27a7aa5a5ea2e31b8df73274b31c Mon Sep 17 00:00:00 2001 From: Chaoguang Lin Date: Tue, 24 Aug 2021 14:18:43 -0700 Subject: [PATCH] Fix comment, add a new function for reading special keys' error message --- fdbcli/CMakeLists.txt | 2 +- fdbcli/SetClassCommand.actor.cpp | 10 ++-------- fdbcli/{Util.cpp => Util.actor.cpp} | 21 ++++++++++++++++++++- fdbcli/fdbcli.actor.h | 3 +++ 4 files changed, 26 insertions(+), 10 deletions(-) rename fdbcli/{Util.cpp => Util.actor.cpp} (61%) diff --git a/fdbcli/CMakeLists.txt b/fdbcli/CMakeLists.txt index c4fe3d8371..2d9ceefb84 100644 --- a/fdbcli/CMakeLists.txt +++ b/fdbcli/CMakeLists.txt @@ -10,7 +10,7 @@ set(FDBCLI_SRCS SetClassCommand.actor.cpp SnapshotCommand.actor.cpp ThrottleCommand.actor.cpp - Util.cpp + Util.actor.cpp linenoise/linenoise.h) if(NOT WIN32) diff --git a/fdbcli/SetClassCommand.actor.cpp b/fdbcli/SetClassCommand.actor.cpp index 11b8c1da96..ee3ebfe454 100644 --- a/fdbcli/SetClassCommand.actor.cpp +++ b/fdbcli/SetClassCommand.actor.cpp @@ -23,8 +23,6 @@ #include "fdbclient/FDBOptions.g.h" #include "fdbclient/IClientApi.h" #include "fdbclient/Knobs.h" -#include "fdbclient/Schemas.h" -#include "fdbclient/Status.h" #include "flow/Arena.h" #include "flow/FastRef.h" @@ -83,13 +81,9 @@ ACTOR Future setProcessClass(Reference db, KeyRef network_addre } catch (Error& e) { state Error err(e); if (e.code() == error_code_special_keys_api_failure) { - Optional errorMsg = wait(safeThreadFutureToFuture(tr->get(fdb_cli::errorMsgSpecialKey))); - ASSERT(errorMsg.present()); - std::string errorMsgStr; - auto valueObj = readJSONStrictly(errorMsg.get().toString()).get_obj(); - auto schema = readJSONStrictly(JSONSchemas::managementApiErrorSchema.toString()).get_obj(); + std::string errorMsgStr = wait(fdb_cli::getSpecialKeysFailureErrorMessage(tr)); // error message already has \n at the end - fprintf(stderr, "%s", valueObj["message"].get_str().c_str()); + fprintf(stderr, "%s", errorMsgStr.c_str()); return false; } wait(safeThreadFutureToFuture(tr->onError(err))); diff --git a/fdbcli/Util.cpp b/fdbcli/Util.actor.cpp similarity index 61% rename from fdbcli/Util.cpp rename to fdbcli/Util.actor.cpp index f67f27c774..c68602e7fe 100644 --- a/fdbcli/Util.cpp +++ b/fdbcli/Util.actor.cpp @@ -1,5 +1,5 @@ /* - * Util.cpp + * Util.actor.cpp * * This source file is part of the FoundationDB open source project * @@ -19,9 +19,14 @@ */ #include "fdbcli/fdbcli.actor.h" +#include "fdbclient/ManagementAPI.actor.h" +#include "fdbclient/Schemas.h" +#include "fdbclient/Status.h" #include "flow/Arena.h" +#include "flow/actorcompiler.h" // This must be the last #include. + namespace fdb_cli { bool tokencmp(StringRef token, const char* command) { @@ -40,4 +45,18 @@ void printUsage(StringRef command) { fprintf(stderr, "ERROR: Unknown command `%s'\n", command.toString().c_str()); } +ACTOR Future getSpecialKeysFailureErrorMessage(Reference tr) { + Optional errorMsg = wait(safeThreadFutureToFuture(tr->get(fdb_cli::errorMsgSpecialKey))); + // Error message should be present + ASSERT(errorMsg.present()); + // Read the json string + auto valueObj = readJSONStrictly(errorMsg.get().toString()).get_obj(); + // verify schema + auto schema = readJSONStrictly(JSONSchemas::managementApiErrorSchema.toString()).get_obj(); + std::string errorStr; + ASSERT(schemaMatch(schema, valueObj, errorStr, SevError, true)); + // return the error message + return valueObj["message"].get_str(); +} + } // namespace fdb_cli diff --git a/fdbcli/fdbcli.actor.h b/fdbcli/fdbcli.actor.h index 735eec6478..bf73e35ca1 100644 --- a/fdbcli/fdbcli.actor.h +++ b/fdbcli/fdbcli.actor.h @@ -75,6 +75,9 @@ inline const KeyRef errorMsgSpecialKey = LiteralStringRef("\xff\xff/error_messag bool tokencmp(StringRef token, const char* command); // print the usage of the specified command void printUsage(StringRef command); +// Pre: tr failed with special_keys_api_failure error +// Read the error message special key and return the message +ACTOR Future getSpecialKeysFailureErrorMessage(Reference tr); // All fdbcli commands (alphabetically) // advanceversion command