Skip to content

Commit 347e5a9

Browse files
committed
SERVER-32198 Change the namespaces stored as StringData to NamespaceString
This avoids having to cast them to NamespaceString for passing to functions or to std::string for comparisons.
1 parent 87fb823 commit 347e5a9

13 files changed

+50
-77
lines changed

src/mongo/db/catalog/database_impl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ Status DatabaseImpl::dropCollection(OperationContext* opCtx,
454454
"turn off profiling before dropping system.profile collection");
455455
} else if (!(nss.isSystemDotViews() || nss.isHealthlog() ||
456456
nss == SessionsCollection::kSessionsNamespaceString ||
457-
nss.ns() == NamespaceString::kSystemKeysCollectionName)) {
457+
nss == NamespaceString::kSystemKeysNamespace)) {
458458
return Status(ErrorCodes::IllegalOperation,
459459
str::stream() << "can't drop system collection " << fullns);
460460
}

src/mongo/db/namespace_string.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,8 @@
3636
#include "mongo/util/mongoutils/str.h"
3737

3838
namespace mongo {
39-
40-
using std::string;
41-
4239
namespace {
4340

44-
const char kServerConfiguration[] = "admin.system.version";
45-
4641
constexpr auto listCollectionsCursorCol = "$cmd.listCollections"_sd;
4742
constexpr auto listIndexesCursorNSPrefix = "$cmd.listIndexes."_sd;
4843
constexpr auto collectionlessAggregateCursorCol = "$cmd.aggregate"_sd;
@@ -54,13 +49,17 @@ constexpr StringData NamespaceString::kAdminDb;
5449
constexpr StringData NamespaceString::kLocalDb;
5550
constexpr StringData NamespaceString::kConfigDb;
5651
constexpr StringData NamespaceString::kSystemDotViewsCollectionName;
57-
constexpr StringData NamespaceString::kShardConfigCollectionsCollectionName;
58-
constexpr StringData NamespaceString::kShardConfigDatabasesCollectionName;
59-
constexpr StringData NamespaceString::kSystemKeysCollectionName;
6052

61-
const NamespaceString NamespaceString::kServerConfigurationNamespace(kServerConfiguration);
53+
const NamespaceString NamespaceString::kServerConfigurationNamespace(NamespaceString::kAdminDb,
54+
"system.version");
6255
const NamespaceString NamespaceString::kSessionTransactionsTableNamespace(
6356
NamespaceString::kConfigDb, "transactions");
57+
const NamespaceString NamespaceString::kShardConfigCollectionsNamespace(NamespaceString::kConfigDb,
58+
"cache.collections");
59+
const NamespaceString NamespaceString::kShardConfigDatabasesNamespace(NamespaceString::kConfigDb,
60+
"cache.databases");
61+
const NamespaceString NamespaceString::kSystemKeysNamespace(NamespaceString::kAdminDb,
62+
"system.keys");
6463
const NamespaceString NamespaceString::kRsOplogNamespace(NamespaceString::kLocalDb, "oplog.rs");
6564

6665
bool NamespaceString::isListCollectionsCursorNS() const {
@@ -80,9 +79,9 @@ bool NamespaceString::isLegalClientSystemNS() const {
8079
if (db() == "admin") {
8180
if (ns() == "admin.system.roles")
8281
return true;
83-
if (ns() == kServerConfiguration)
82+
if (ns() == kServerConfigurationNamespace.ns())
8483
return true;
85-
if (ns() == kSystemKeysCollectionName)
84+
if (ns() == kSystemKeysNamespace.ns())
8685
return true;
8786
if (ns() == "admin.system.new_users")
8887
return true;
@@ -92,6 +91,7 @@ bool NamespaceString::isLegalClientSystemNS() const {
9291
if (ns() == "config.system.sessions")
9392
return true;
9493
}
94+
9595
if (ns() == "local.system.replset")
9696
return true;
9797

src/mongo/db/namespace_string.h

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -60,18 +60,6 @@ class NamespaceString {
6060
// Name for the system views collection
6161
static constexpr StringData kSystemDotViewsCollectionName = "system.views"_sd;
6262

63-
// Name for a shard's collections metadata collection, each document of which indicates the
64-
// state of a specific collection.
65-
static constexpr StringData kShardConfigCollectionsCollectionName =
66-
"config.cache.collections"_sd;
67-
68-
// Name for a shard's databases metadata collection, each document of which indicates the
69-
// state of a specific database.
70-
static constexpr StringData kShardConfigDatabasesCollectionName = "config.cache.databases"_sd;
71-
72-
// Name for causal consistency's key collection.
73-
static constexpr StringData kSystemKeysCollectionName = "admin.system.keys"_sd;
74-
7563
// Namespace for storing configuration data, which needs to be replicated if the server is
7664
// running as a replica set. Documents in this collection should represent some configuration
7765
// state of the server, which needs to be recovered/consulted at startup. Each document in this
@@ -82,6 +70,17 @@ class NamespaceString {
8270
// Namespace for storing the transaction information for each session
8371
static const NamespaceString kSessionTransactionsTableNamespace;
8472

73+
// Name for a shard's collections metadata collection, each document of which indicates the
74+
// state of a specific collection
75+
static const NamespaceString kShardConfigCollectionsNamespace;
76+
77+
// Name for a shard's databases metadata collection, each document of which indicates the state
78+
// of a specific database
79+
static const NamespaceString kShardConfigDatabasesNamespace;
80+
81+
// Name for causal consistency's key collection.
82+
static const NamespaceString kSystemKeysNamespace;
83+
8584
// Namespace of the the oplog collection.
8685
static const NamespaceString kRsOplogNamespace;
8786

src/mongo/db/s/collection_sharding_state.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
#include "mongo/db/operation_context.h"
4040
#include "mongo/db/s/operation_sharding_state.h"
4141
#include "mongo/db/s/sharded_connection_info.h"
42-
#include "mongo/db/s/sharding_state.h"
4342
#include "mongo/db/server_parameters.h"
4443
#include "mongo/db/service_context.h"
4544
#include "mongo/executor/network_interface_factory.h"
@@ -108,15 +107,16 @@ class CollectionShardingStateMap {
108107
public:
109108
CollectionShardingStateMap() = default;
110109

111-
CollectionShardingState& getOrCreate(OperationContext* opCtx, const std::string& ns) {
110+
static const ServiceContext::Decoration<CollectionShardingStateMap> get;
111+
112+
CollectionShardingState& getOrCreate(const std::string& ns) {
112113
stdx::lock_guard<stdx::mutex> lg(_mutex);
113114

114115
auto it = _collections.find(ns);
115116
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)));
120120
invariant(inserted.second);
121121
it = std::move(inserted.first);
122122
}
@@ -151,7 +151,7 @@ class CollectionShardingStateMap {
151151
CollectionsMap _collections;
152152
};
153153

154-
const auto getCollectionShardingStateMap =
154+
const ServiceContext::Decoration<CollectionShardingStateMap> CollectionShardingStateMap::get =
155155
ServiceContext::declareDecoration<CollectionShardingStateMap>();
156156

157157
} // namespace
@@ -171,12 +171,12 @@ CollectionShardingState* CollectionShardingState::get(OperationContext* opCtx,
171171
// Collection lock must be held to have a reference to the collection's sharding state
172172
dassert(opCtx->lockState()->isCollectionLockedForMode(ns, MODE_IS));
173173

174-
auto& collectionsMap = getCollectionShardingStateMap(opCtx->getServiceContext());
175-
return &collectionsMap.getOrCreate(opCtx, ns);
174+
auto& collectionsMap = CollectionShardingStateMap::get(opCtx->getServiceContext());
175+
return &collectionsMap.getOrCreate(ns);
176176
}
177177

178178
void CollectionShardingState::report(OperationContext* opCtx, BSONObjBuilder* builder) {
179-
auto& collectionsMap = getCollectionShardingStateMap(opCtx->getServiceContext());
179+
auto& collectionsMap = CollectionShardingStateMap::get(opCtx->getServiceContext());
180180
collectionsMap.report(opCtx, builder);
181181
}
182182

@@ -313,15 +313,14 @@ bool CollectionShardingState::_checkShardVersionOk(OperationContext* opCtx,
313313
std::string* errmsg,
314314
ChunkVersion* expectedShardVersion,
315315
ChunkVersion* actualShardVersion) {
316-
auto* const client = opCtx->getClient();
317316
auto& oss = OperationShardingState::get(opCtx);
318317

319318
// If there is a version attached to the OperationContext, use it as the received version.
320319
// Otherwise, get the received version from the ShardedConnectionInfo.
321320
if (oss.hasShardVersion()) {
322321
*expectedShardVersion = oss.getShardVersion(_nss);
323322
} else {
324-
ShardedConnectionInfo* info = ShardedConnectionInfo::get(client, false);
323+
auto const info = ShardedConnectionInfo::get(opCtx->getClient(), false);
325324
if (!info) {
326325
// There is no shard version information on either 'opCtx' or 'client'. This means that
327326
// the operation represented by 'opCtx' is unversioned, and the shard version is always

src/mongo/db/s/shard_metadata_util.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,11 @@ StatusWith<ShardCollectionType> readShardCollectionsEntry(OperationContext* opCt
142142
try {
143143
DBDirectClient client(opCtx);
144144
std::unique_ptr<DBClientCursor> cursor =
145-
client.query(ShardCollectionType::ConfigNS.ns(), fullQuery, 1);
145+
client.query(NamespaceString::kShardConfigCollectionsNamespace.ns(), fullQuery, 1);
146146
if (!cursor) {
147147
return Status(ErrorCodes::OperationFailed,
148148
str::stream() << "Failed to establish a cursor for reading "
149-
<< ShardCollectionType::ConfigNS.ns()
149+
<< NamespaceString::kShardConfigCollectionsNamespace.ns()
150150
<< " from local storage");
151151
}
152152

@@ -175,11 +175,11 @@ StatusWith<ShardDatabaseType> readShardDatabasesEntry(OperationContext* opCtx, S
175175
try {
176176
DBDirectClient client(opCtx);
177177
std::unique_ptr<DBClientCursor> cursor =
178-
client.query(ShardDatabaseType::ConfigNS.ns(), fullQuery, 1);
178+
client.query(NamespaceString::kShardConfigDatabasesNamespace.ns(), fullQuery, 1);
179179
if (!cursor) {
180180
return Status(ErrorCodes::OperationFailed,
181181
str::stream() << "Failed to establish a cursor for reading "
182-
<< ShardDatabaseType::ConfigNS.ns()
182+
<< NamespaceString::kShardConfigDatabasesNamespace.ns()
183183
<< " from local storage");
184184
}
185185

@@ -228,7 +228,7 @@ Status updateShardCollectionsEntry(OperationContext* opCtx,
228228
}
229229

230230
auto commandResponse = client.runCommand([&] {
231-
write_ops::Update updateOp(NamespaceString{ShardCollectionType::ConfigNS});
231+
write_ops::Update updateOp(NamespaceString::kShardConfigCollectionsNamespace);
232232
updateOp.setUpdates({[&] {
233233
write_ops::UpdateOpEntry entry;
234234
entry.setQ(query);
@@ -271,7 +271,7 @@ Status updateShardDatabasesEntry(OperationContext* opCtx,
271271
}
272272

273273
auto commandResponse = client.runCommand([&] {
274-
write_ops::Update updateOp(NamespaceString{ShardDatabaseType::ConfigNS});
274+
write_ops::Update updateOp(NamespaceString::kShardConfigDatabasesNamespace);
275275
updateOp.setUpdates({[&] {
276276
write_ops::UpdateOpEntry entry;
277277
entry.setQ(query);
@@ -407,8 +407,7 @@ Status dropChunksAndDeleteCollectionsEntry(OperationContext* opCtx, const Namesp
407407
DBDirectClient client(opCtx);
408408

409409
auto deleteCommandResponse = client.runCommand([&] {
410-
write_ops::Delete deleteOp(
411-
NamespaceString{NamespaceString::kShardConfigCollectionsCollectionName});
410+
write_ops::Delete deleteOp(NamespaceString::kShardConfigCollectionsNamespace);
412411
deleteOp.setDeletes({[&] {
413412
write_ops::DeleteOpEntry entry;
414413
entry.setQ(BSON(ShardCollectionType::ns << nss.ns()));
@@ -442,8 +441,7 @@ Status deleteDatabasesEntry(OperationContext* opCtx, StringData dbName) {
442441
DBDirectClient client(opCtx);
443442

444443
auto deleteCommandResponse = client.runCommand([&] {
445-
write_ops::Delete deleteOp(
446-
NamespaceString{NamespaceString::kShardConfigDatabasesCollectionName});
444+
write_ops::Delete deleteOp(NamespaceString::kShardConfigDatabasesNamespace);
447445
deleteOp.setDeletes({[&] {
448446
write_ops::DeleteOpEntry entry;
449447
entry.setQ(BSON(ShardDatabaseType::name << dbName.toString()));

src/mongo/db/s/shard_metadata_util_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ TEST_F(ShardMetadataUtilTest, DropChunksAndDeleteCollectionsEntry) {
325325
ASSERT_OK(dropChunksAndDeleteCollectionsEntry(operationContext(), kNss));
326326
checkCollectionIsEmpty(kChunkMetadataNss);
327327
// Collections collection should be empty because it only had one entry.
328-
checkCollectionIsEmpty(ShardCollectionType::ConfigNS);
328+
checkCollectionIsEmpty(NamespaceString::kShardConfigCollectionsNamespace);
329329
}
330330

331331
} // namespace

src/mongo/db/s/shard_server_op_observer.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ void ShardServerOpObserver::onUpdate(OperationContext* opCtx, const OplogUpdateE
230230
auto const css = CollectionShardingState::get(opCtx, args.nss);
231231
const auto metadata = css->getMetadata(opCtx);
232232

233-
if (args.nss.ns() == NamespaceString::kShardConfigCollectionsCollectionName) {
233+
if (args.nss == NamespaceString::kShardConfigCollectionsNamespace) {
234234
// Notification of routing table changes are only needed on secondaries
235235
if (isStandaloneOrPrimary(opCtx)) {
236236
return;
@@ -283,7 +283,7 @@ void ShardServerOpObserver::onUpdate(OperationContext* opCtx, const OplogUpdateE
283283
}
284284
}
285285

286-
if (args.nss.ns() == NamespaceString::kShardConfigDatabasesCollectionName) {
286+
if (args.nss == NamespaceString::kShardConfigDatabasesNamespace) {
287287
// Notification of routing table changes are only needed on secondaries
288288
if (isStandaloneOrPrimary(opCtx)) {
289289
return;
@@ -337,10 +337,10 @@ void ShardServerOpObserver::onDelete(OperationContext* opCtx,
337337
const boost::optional<BSONObj>& deletedDoc) {
338338
auto& deleteState = getDeleteState(opCtx);
339339

340-
if (nss.ns() == NamespaceString::kShardConfigCollectionsCollectionName) {
340+
if (nss == NamespaceString::kShardConfigCollectionsNamespace) {
341341
onConfigDeleteInvalidateCachedCollectionMetadataAndNotify(opCtx, deleteState.documentKey);
342342
}
343-
if (nss.ns() == NamespaceString::kShardConfigDatabasesCollectionName) {
343+
if (nss == NamespaceString::kShardConfigDatabasesNamespace) {
344344
if (isStandaloneOrPrimary(opCtx)) {
345345
return;
346346
}

src/mongo/db/s/split_chunk_command.cpp

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "mongo/db/s/operation_sharding_state.h"
4242
#include "mongo/db/s/sharding_state.h"
4343
#include "mongo/db/s/split_chunk.h"
44+
#include "mongo/s/catalog/type_chunk.h"
4445
#include "mongo/s/stale_exception.h"
4546
#include "mongo/util/log.h"
4647
#include "mongo/util/mongoutils/str.h"
@@ -64,7 +65,7 @@ class SplitChunkCommand : public ErrmsgCommandDeprecated {
6465
"splitKeys : [ {a:150} , ... ]}";
6566
}
6667

67-
virtual bool supportsWriteConcern(const BSONObj& cmd) const override {
68+
bool supportsWriteConcern(const BSONObj& cmd) const override {
6869
return false;
6970
}
7071

@@ -95,19 +96,11 @@ class SplitChunkCommand : public ErrmsgCommandDeprecated {
9596
const BSONObj& cmdObj,
9697
std::string& errmsg,
9798
BSONObjBuilder& result) override {
98-
9999
uassertStatusOK(ShardingState::get(opCtx)->canAcceptShardedCommands());
100100

101-
//
102-
// Check whether parameters passed to splitChunk are sound
103-
//
104101
const NamespaceString nss = NamespaceString(parseNs(dbname, cmdObj));
105-
if (!nss.isValid()) {
106-
errmsg = str::stream() << "invalid namespace '" << nss.toString()
107-
<< "' specified for command";
108-
return false;
109-
}
110102

103+
// Check whether parameters passed to splitChunk are sound
111104
BSONObj keyPatternObj;
112105
{
113106
BSONElement keyPatternElem;

src/mongo/s/catalog/type_shard_collection.cpp

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@
3939

4040
namespace mongo {
4141

42-
const NamespaceString ShardCollectionType::ConfigNS(
43-
NamespaceString::kShardConfigCollectionsCollectionName);
44-
4542
const BSONField<std::string> ShardCollectionType::ns("_id");
4643
const BSONField<UUID> ShardCollectionType::uuid("uuid");
4744
const BSONField<OID> ShardCollectionType::epoch("epoch");

src/mongo/s/catalog/type_shard_collection.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,6 @@ class StatusWith;
7070
*/
7171
class ShardCollectionType {
7272
public:
73-
// Name of the collections collection on the shard server.
74-
static const NamespaceString ConfigNS;
75-
7673
static const BSONField<std::string> ns; // "_id"
7774
static const BSONField<UUID> uuid;
7875
static const BSONField<OID> epoch;

0 commit comments

Comments
 (0)