Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-66829

Refactor extractHedgeOptions

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Minor - P4 Minor - P4
    • 6.2.0-rc0
    • Affects Version/s: None
    • Component/s: None
    • Fully Compatible
    • ALL
    • Service Arch 2022-10-03

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

       

            Assignee:
            celina.tala@mongodb.com Celina Tala
            Reporter:
            daniel.morilha@mongodb.com Daniel Morilha (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: