Skip to content

Commit 3102c73

Browse files
jsmulrowMongoDB Bot
authored andcommitted
SERVER-84283 Return const reference from Shard::getConnString
GitOrigin-RevId: aafa09c
1 parent f711eed commit 3102c73

File tree

7 files changed

+18
-12
lines changed

7 files changed

+18
-12
lines changed

src/mongo/db/s/shard_local.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,16 @@
4343

4444
namespace mongo {
4545

46+
const ConnectionString ShardLocal::kLocalConnectionString = ConnectionString::forLocal();
47+
4648
ShardLocal::ShardLocal(const ShardId& id) : Shard(id) {
4749
// Currently ShardLocal only works for config servers. If we ever start using ShardLocal on
4850
// shards we'll need to consider how to handle shards.
4951
invariant(serverGlobalParams.clusterRole.has(ClusterRole::ConfigServer));
5052
}
5153

52-
ConnectionString ShardLocal::getConnString() const {
53-
return repl::ReplicationCoordinator::get(getGlobalServiceContext())
54-
->getConfigConnectionString();
54+
const ConnectionString& ShardLocal::getConnString() const {
55+
return kLocalConnectionString;
5556
}
5657

5758
std::shared_ptr<RemoteCommandTargeter> ShardLocal::getTargeter() const {

src/mongo/db/s/shard_local.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ class ShardLocal : public Shard {
6161
ShardLocal& operator=(const ShardLocal&) = delete;
6262

6363
public:
64+
// ShardLocal doesn't have a "real" connection string since it's always a connection to the
65+
// local process, so it uses the hardcoded local connection string. Only used in tests.
66+
static const ConnectionString kLocalConnectionString;
67+
6468
explicit ShardLocal(const ShardId& id);
6569

6670
~ShardLocal() = default;
@@ -69,7 +73,7 @@ class ShardLocal : public Shard {
6973
* These functions are implemented for the Shard interface's sake. They should not be called on
7074
* ShardLocal because doing so triggers invariants.
7175
*/
72-
ConnectionString getConnString() const override;
76+
const ConnectionString& getConnString() const override;
7377
std::shared_ptr<RemoteCommandTargeter> getTargeter() const override;
7478
void updateReplSetMonitor(const HostAndPort& remoteHost,
7579
const Status& remoteCommandStatus) override;

src/mongo/s/client/config_shard_wrapper.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ ConfigShardWrapper::ConfigShardWrapper(std::shared_ptr<Shard> configShard)
4444
invariant(_configShard->isConfig());
4545
}
4646

47-
ConnectionString ConfigShardWrapper::getConnString() const {
47+
const ConnectionString& ConfigShardWrapper::getConnString() const {
4848
return _configShard->getConnString();
4949
}
5050

src/mongo/s/client/config_shard_wrapper.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class ConfigShardWrapper : public Shard {
7272

7373
~ConfigShardWrapper() = default;
7474

75-
ConnectionString getConnString() const override;
75+
const ConnectionString& getConnString() const override;
7676

7777
std::shared_ptr<RemoteCommandTargeter> getTargeter() const override;
7878

src/mongo/s/client/config_shard_wrapper_test.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
namespace mongo {
5454
namespace {
5555

56+
const HostAndPort kConfigHost{"configHost1"};
57+
ConnectionString kConfigCS{ConnectionString::forReplicaSet("configReplSet", {kConfigHost})};
58+
5659
class MockShard : public Shard {
5760
MockShard(const MockShard&) = delete;
5861
MockShard& operator=(const MockShard&) = delete;
@@ -61,10 +64,8 @@ class MockShard : public Shard {
6164
explicit MockShard(const ShardId& id) : Shard(id) {}
6265
~MockShard() = default;
6366

64-
ConnectionString getConnString() const override {
65-
const HostAndPort configHost{"configHost1"};
66-
ConnectionString configCS{ConnectionString::forReplicaSet("configReplSet", {configHost})};
67-
return configCS;
67+
const ConnectionString& getConnString() const override {
68+
return kConfigCS;
6869
}
6970

7071
std::shared_ptr<RemoteCommandTargeter> getTargeter() const override {

src/mongo/s/client/shard.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class Shard {
128128
/**
129129
* Returns the current connection string for the shard.
130130
*/
131-
virtual ConnectionString getConnString() const = 0;
131+
virtual const ConnectionString& getConnString() const = 0;
132132

133133
/**
134134
* Returns the RemoteCommandTargeter for the hosts in this shard.

src/mongo/s/client/shard_remote.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class ShardRemote : public Shard {
7474

7575
~ShardRemote();
7676

77-
ConnectionString getConnString() const override {
77+
const ConnectionString& getConnString() const override {
7878
return _connString;
7979
}
8080

0 commit comments

Comments
 (0)