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;
|
}
|