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

Remove mutex from `ShardingState`

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 8.0.0-rc0
    • Affects Version/s: None
    • Component/s: Internal Code
    • None
    • Service Arch
    • Fully Compatible
    • Service Arch 2024-04-01

      As highlighted in the following code snippet, ShardingState is using a mutex to synchronize accesses to its Future, which is unnecessary as Future is a thread-safe primitive:

      boost::optional<ClusterRole> ShardingState::pollClusterRole() const {
          stdx::unique_lock<Latch> ul(_mutex);
          if (!_future.isReady())
              return boost::none;
      
          const auto& role = uassertStatusOK(_future.getNoThrow());
          return role.role;
      }
      

      So, the above can be simplified and rewritten as follows:

      boost::optional<ClusterRole> ShardingState::pollClusterRole() const {
          if (!_future.isReady())
              return boost::none;
          return _future.get().role;
      }
      

      As part of this ticket, we should ShardingState::_mutex and better utilize Future APIs.

            Assignee:
            james.bronsted@mongodb.com James Bronsted
            Reporter:
            amirsaman.memaripour@mongodb.com Amirsaman Memaripour
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated:
              Resolved: