summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBlake Oler <blake.oler@mongodb.com>2020-02-05 16:07:41 +0000
committerevergreen <evergreen@mongodb.com>2020-02-05 16:07:41 +0000
commit3114642e2e211fadcbed1b41d13c9f9423a4e27f (patch)
tree6326bc9abdf3750eb16ed34bfd2fb6d832a71c2b
parent67e70cffac0d02e6c2ff58d31f87a72529b017ae (diff)
downloadmongo-3114642e2e211fadcbed1b41d13c9f9423a4e27f.tar.gz
SERVER-45850 Remove mutex guards on read access for _shardId and _clusterId in ShardingState
-rw-r--r--src/mongo/db/s/sharding_state.cpp2
-rw-r--r--src/mongo/db/s/sharding_state.h19
2 files changed, 13 insertions, 8 deletions
diff --git a/src/mongo/db/s/sharding_state.cpp b/src/mongo/db/s/sharding_state.cpp
index 37e5f8930fa..441d303a038 100644
--- a/src/mongo/db/s/sharding_state.cpp
+++ b/src/mongo/db/s/sharding_state.cpp
@@ -105,13 +105,11 @@ Status ShardingState::canAcceptShardedCommands() const {
ShardId ShardingState::shardId() {
invariant(enabled());
- stdx::lock_guard<Latch> lk(_mutex);
return _shardId;
}
OID ShardingState::clusterId() {
invariant(enabled());
- stdx::lock_guard<Latch> lk(_mutex);
return _clusterId;
}
diff --git a/src/mongo/db/s/sharding_state.h b/src/mongo/db/s/sharding_state.h
index ab3430fb5ec..e64ba436f17 100644
--- a/src/mongo/db/s/sharding_state.h
+++ b/src/mongo/db/s/sharding_state.h
@@ -93,14 +93,12 @@ public:
Status canAcceptShardedCommands() const;
/**
- * Returns the shard id to which this node belongs. May only be called if 'enabled()' above
- * returns true.
+ * Returns the shard id to which this node belongs.
*/
ShardId shardId();
/**
- * Returns the cluster id of the cluster to which this node belongs. May only be called if
- * 'enabled()' above returns true.
+ * Returns the cluster id of the cluster to which this node belongs.
*/
OID clusterId();
@@ -135,13 +133,22 @@ private:
return static_cast<InitializationState>(_initializationState.load());
}
- // Protects state below
+ // Protects state for initializing '_shardId', '_clusterId', and '_initializationStatus'.
+ // Protects read access for '_initializationStatus'.
Mutex _mutex = MONGO_MAKE_LATCH("ShardingState::_mutex");
// State of the initialization of the sharding state along with any potential errors
AtomicWord<unsigned> _initializationState{static_cast<uint32_t>(InitializationState::kNew)};
- // Sets the shard name for this host (comes through setShardVersion)
+ // For '_shardId' and '_clusterId':
+ //
+ // These variables will invariant if attempts are made to access them before 'enabled()' has
+ // been set to true. 'enabled()' being set to true guarantees that these variables have been
+ // set, and that they will remain unchanged for the duration of the ShardingState object's
+ // lifetime. Because these variables will not change, it's unnecessary to acquire the class's
+ // mutex in order to call these getters and access their underlying data.
+
+ // Sets the shard name for this host.
ShardId _shardId;
// The id for the cluster this shard belongs to.