[SERVER-66829] Refactor extractHedgeOptions Created: 27/May/22  Updated: 29/Oct/23  Resolved: 20/Sep/22

Status: Closed
Project: Core Server
Component/s: None
Affects Version/s: None
Fix Version/s: 6.2.0-rc0

Type: Bug Priority: Minor - P4
Reporter: Daniel Morilha (Inactive) Assignee: Celina Tala
Resolution: Fixed Votes: 0
Labels: save-for-celina
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Depends
depends on SERVER-66764 Polyfill C++20 constexpr isSorted Closed
Backwards Compatibility: Fully Compatible
Operating System: ALL
Sprint: Service Arch 2022-10-03
Participants:

 Description   

During the extensive code review for SERVER-66260, an opportunity on how to improve Sharding's extractHedgeOptions was identified. The suggestion looks theoretically sound, what is left to this ticket is to assess its correctness through the various test suites and its performance impact.

Here's billy.donahue@mongodb.com 's suggestion

namespace {
// Only do hedging for commands that cannot trigger writes.
// Keep this list sorted.
static 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 const bool verifySortOrderOnStartup = [] {
    invariant(std::is_sorted(hedgeCommands.begin(), hedgeCommands.end()));
    return true;
}();
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) {
    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;
} 

 



 Comments   
Comment by Githook User [ 20/Sep/22 ]

Author:

{'name': 'Celina Tala', 'email': 'celinahtala@gmail.com', 'username': 'celinatala-1'}

Message: SERVER-66829 Refactored extractHedgeOptions
Branch: master
https://github.com/mongodb/mongo/commit/c3438171af82aa30fa2b4034f135698eda7d39ec

Comment by Billy Donahue [ 16/Sep/22 ]

The verifySortOrderOnStartup in the description can be a static_assert of constexprIsSorted.
which came out of SERVER-66764, which was spawned by the same discussion (SERVER-66260) as this ticket.

static_assert(constexprIsSorted(hedgeCommands.begin(), hedgeCommands.end()));

Generated at Thu Feb 08 06:06:32 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.