39
39
#include " mongo/db/operation_context.h"
40
40
#include " mongo/db/s/operation_sharding_state.h"
41
41
#include " mongo/db/s/sharded_connection_info.h"
42
- #include " mongo/db/s/sharding_state.h"
43
42
#include " mongo/db/server_parameters.h"
44
43
#include " mongo/db/service_context.h"
45
44
#include " mongo/executor/network_interface_factory.h"
@@ -108,15 +107,16 @@ class CollectionShardingStateMap {
108
107
public:
109
108
CollectionShardingStateMap () = default ;
110
109
111
- CollectionShardingState& getOrCreate (OperationContext* opCtx, const std::string& ns) {
110
+ static const ServiceContext::Decoration<CollectionShardingStateMap> get;
111
+
112
+ CollectionShardingState& getOrCreate (const std::string& ns) {
112
113
stdx::lock_guard<stdx::mutex> lg (_mutex);
113
114
114
115
auto it = _collections.find (ns);
115
116
if (it == _collections.end ()) {
116
- auto inserted =
117
- _collections.emplace (ns,
118
- std::make_unique<CollectionShardingState>(
119
- opCtx->getServiceContext (), NamespaceString (ns)));
117
+ auto inserted = _collections.emplace (
118
+ ns,
119
+ std::make_unique<CollectionShardingState>(get.owner (this ), NamespaceString (ns)));
120
120
invariant (inserted.second );
121
121
it = std::move (inserted.first );
122
122
}
@@ -151,7 +151,7 @@ class CollectionShardingStateMap {
151
151
CollectionsMap _collections;
152
152
};
153
153
154
- const auto getCollectionShardingStateMap =
154
+ const ServiceContext::Decoration<CollectionShardingStateMap> CollectionShardingStateMap::get =
155
155
ServiceContext::declareDecoration<CollectionShardingStateMap>();
156
156
157
157
} // namespace
@@ -171,12 +171,12 @@ CollectionShardingState* CollectionShardingState::get(OperationContext* opCtx,
171
171
// Collection lock must be held to have a reference to the collection's sharding state
172
172
dassert (opCtx->lockState ()->isCollectionLockedForMode (ns, MODE_IS));
173
173
174
- auto & collectionsMap = getCollectionShardingStateMap (opCtx->getServiceContext ());
175
- return &collectionsMap.getOrCreate (opCtx, ns);
174
+ auto & collectionsMap = CollectionShardingStateMap::get (opCtx->getServiceContext ());
175
+ return &collectionsMap.getOrCreate (ns);
176
176
}
177
177
178
178
void CollectionShardingState::report (OperationContext* opCtx, BSONObjBuilder* builder) {
179
- auto & collectionsMap = getCollectionShardingStateMap (opCtx->getServiceContext ());
179
+ auto & collectionsMap = CollectionShardingStateMap::get (opCtx->getServiceContext ());
180
180
collectionsMap.report (opCtx, builder);
181
181
}
182
182
@@ -313,15 +313,14 @@ bool CollectionShardingState::_checkShardVersionOk(OperationContext* opCtx,
313
313
std::string* errmsg,
314
314
ChunkVersion* expectedShardVersion,
315
315
ChunkVersion* actualShardVersion) {
316
- auto * const client = opCtx->getClient ();
317
316
auto & oss = OperationShardingState::get (opCtx);
318
317
319
318
// If there is a version attached to the OperationContext, use it as the received version.
320
319
// Otherwise, get the received version from the ShardedConnectionInfo.
321
320
if (oss.hasShardVersion ()) {
322
321
*expectedShardVersion = oss.getShardVersion (_nss);
323
322
} else {
324
- ShardedConnectionInfo* info = ShardedConnectionInfo::get (client , false );
323
+ auto const info = ShardedConnectionInfo::get (opCtx-> getClient () , false );
325
324
if (!info) {
326
325
// There is no shard version information on either 'opCtx' or 'client'. This means that
327
326
// the operation represented by 'opCtx' is unversioned, and the shard version is always
0 commit comments