diff options
| author | Celina Tala <celinahtala@gmail.com> | 2022-09-20 18:04:50 +0000 |
|---|---|---|
| committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2022-09-20 19:15:57 +0000 |
| commit | c3438171af82aa30fa2b4034f135698eda7d39ec (patch) | |
| tree | 9c814d16cc983eda89c8c57036cca9417bf9fec1 /src/mongo | |
| parent | 17e74a7fe2bb2e9efd7ae7de6861e99a4559be65 (diff) | |
| download | mongo-c3438171af82aa30fa2b4034f135698eda7d39ec.tar.gz | |
SERVER-66829 Refactored extractHedgeOptions
Diffstat (limited to 'src/mongo')
| -rw-r--r-- | src/mongo/s/hedge_options_util.cpp | 57 | ||||
| -rw-r--r-- | src/mongo/s/hedge_options_util_test.cpp | 5 |
2 files changed, 36 insertions, 26 deletions
diff --git a/src/mongo/s/hedge_options_util.cpp b/src/mongo/s/hedge_options_util.cpp index ebdfc3ddd86..fd40c7f762c 100644 --- a/src/mongo/s/hedge_options_util.cpp +++ b/src/mongo/s/hedge_options_util.cpp @@ -30,36 +30,47 @@ #include "mongo/s/hedge_options_util.h" #include "mongo/s/mongos_server_parameters_gen.h" +#include "mongo/util/sort.h" namespace mongo { namespace { -// Only hedge commands that cannot trigger writes. -const std::set<std::string> supportedCmds{"collStats", - "count", - "dataSize", - "dbStats", - "distinct", - "filemd5", - "find", - "listCollections", - "listIndexes", - "planCacheListFilters"}; +// Only do hedging for commands that cannot trigger writes. +constexpr std::array hedgeCommands{ + "collStats"_sd, + "count"_sd, + "dataSize"_sd, + "dbStats"_sd, + "distinct"_sd, + "filemd5"_sd, + "find"_sd, + "listCollections"_sd, + "listIndexes"_sd, + "planCacheListFilters"_sd, +}; + +static_assert(constexprIsSorted(hedgeCommands.begin(), hedgeCommands.end())); + +bool commandCanHedge(StringData command) { + return std::binary_search(hedgeCommands.begin(), hedgeCommands.end(), command); +} } // namespace void extractHedgeOptions(const BSONObj& cmdObj, const ReadPreferenceSetting& readPref, executor::RemoteCommandRequestOnAny::Options& options) { - options.resetHedgeOptions(); - if (!(gReadHedgingMode.load() == ReadHedgingMode::kOn && readPref.hedgingMode && - readPref.hedgingMode->getEnabled())) { - return; - } - auto cmdName(cmdObj.firstElement().fieldNameStringData().toString()); - if (supportedCmds.count(cmdName)) { - options.hedgeCount = 1; - options.maxTimeMSForHedgedReads = gMaxTimeMSForHedgedReads.load(); - options.isHedgeEnabled = true; - return; - } + const bool canHedge = [&] { + if (gReadHedgingMode.load() != ReadHedgingMode::kOn) + return false; // Hedging is globally disabled. + auto&& mode = readPref.hedgingMode; + if (!mode || !mode->getEnabled()) + return false; // The read preference didn't enable hedging. + auto cmdName = cmdObj.firstElement().fieldNameStringData(); + if (!commandCanHedge(cmdName)) + return false; // This is not a command that can hedge. + return true; + }(); + options.isHedgeEnabled = canHedge; + options.hedgeCount = canHedge ? 1 : 0; + options.maxTimeMSForHedgedReads = canHedge ? gMaxTimeMSForHedgedReads.load() : 0; } } // namespace mongo diff --git a/src/mongo/s/hedge_options_util_test.cpp b/src/mongo/s/hedge_options_util_test.cpp index e1093dfa66f..e0c5bbb6325 100644 --- a/src/mongo/s/hedge_options_util_test.cpp +++ b/src/mongo/s/hedge_options_util_test.cpp @@ -60,9 +60,8 @@ protected: /** * Sets the given server parameters and creates ReadPreferenceSetting from 'rspObj' and extracts - * HedgeOptions from it. If 'hedge' is true, asserts that the resulting HedgeOptions is not - * empty, otherwise asserts that it is empty. Resets the server parameters to the defaults - * before returning. + * HedgeOptions from it. If 'hedge' is true, asserts that the resulting HedgeOptions' + * maxTimeforHedgedReads is equal to what was passed in. */ void checkHedgeOptions(const BSONObj& serverParameters, const BSONObj& cmdObj, |
