1 
2 /**
3  *    Copyright (C) 2018-present MongoDB, Inc.
4  *
5  *    This program is free software: you can redistribute it and/or modify
6  *    it under the terms of the Server Side Public License, version 1,
7  *    as published by MongoDB, Inc.
8  *
9  *    This program is distributed in the hope that it will be useful,
10  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *    Server Side Public License for more details.
13  *
14  *    You should have received a copy of the Server Side Public License
15  *    along with this program. If not, see
16  *    <http://www.mongodb.com/licensing/server-side-public-license>.
17  *
18  *    As a special exception, the copyright holders give permission to link the
19  *    code of portions of this program with the OpenSSL library under certain
20  *    conditions as described in each individual source file and distribute
21  *    linked combinations including the program with the OpenSSL library. You
22  *    must comply with the Server Side Public License in all respects for
23  *    all of the code used other than as permitted herein. If you modify file(s)
24  *    with this exception, you may extend this exception to your version of the
25  *    file(s), but you are not obligated to do so. If you do not wish to do so,
26  *    delete this exception statement from your version. If you delete this
27  *    exception statement from all source files in the program, then also delete
28  *    it in the license file.
29  */
30 
31 #define MONGO_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kSharding
32 
33 #include <boost/none_t.hpp>
34 
35 #include "mongo/platform/basic.h"
36 
37 #include "mongo/s/client/shard_local.h"
38 
39 #include "mongo/base/status.h"
40 #include "mongo/base/status_with.h"
41 #include "mongo/client/remote_command_targeter.h"
42 #include "mongo/db/curop.h"
43 #include "mongo/db/dbdirectclient.h"
44 #include "mongo/db/repl/repl_client_info.h"
45 #include "mongo/db/repl/repl_set_config.h"
46 #include "mongo/db/repl/replication_coordinator_global.h"
47 #include "mongo/rpc/get_status_from_command_result.h"
48 #include "mongo/rpc/unique_message.h"
49 #include "mongo/util/log.h"
50 #include "mongo/util/scopeguard.h"
51 
52 namespace mongo {
53 
ShardLocal(const ShardId & id)54 ShardLocal::ShardLocal(const ShardId& id) : Shard(id), _rsLocalClient() {
55     // Currently ShardLocal only works for config servers. If we ever start using ShardLocal on
56     // shards we'll need to consider how to handle shards.
57     invariant(serverGlobalParams.clusterRole == ClusterRole::ConfigServer);
58 }
59 
getConnString() const60 const ConnectionString ShardLocal::getConnString() const {
61     return repl::getGlobalReplicationCoordinator()->getConfig().getConnectionString();
62 }
63 
getTargeter() const64 std::shared_ptr<RemoteCommandTargeter> ShardLocal::getTargeter() const {
65     MONGO_UNREACHABLE;
66 };
67 
originalConnString() const68 const ConnectionString ShardLocal::originalConnString() const {
69     // Return the local connection string here as this method is only used for updating the
70     // ShardRegistry and we don't need a mapping from hosts in the replica set config to the shard
71     // for local shards.
72     return ConnectionString::forLocal();
73 }
74 
updateReplSetMonitor(const HostAndPort & remoteHost,const Status & remoteCommandStatus)75 void ShardLocal::updateReplSetMonitor(const HostAndPort& remoteHost,
76                                       const Status& remoteCommandStatus) {
77     MONGO_UNREACHABLE;
78 }
79 
toString() const80 std::string ShardLocal::toString() const {
81     return getId().toString() + ":<local>";
82 }
83 
isRetriableError(ErrorCodes::Error code,RetryPolicy options)84 bool ShardLocal::isRetriableError(ErrorCodes::Error code, RetryPolicy options) {
85     if (options == RetryPolicy::kNoRetry) {
86         return false;
87     }
88 
89     if (options == RetryPolicy::kIdempotent) {
90         return code == ErrorCodes::WriteConcernFailed;
91     } else {
92         invariant(options == RetryPolicy::kNotIdempotent);
93         return false;
94     }
95 }
96 
_runCommand(OperationContext * opCtx,const ReadPreferenceSetting & unused,const std::string & dbName,Milliseconds maxTimeMSOverrideUnused,const BSONObj & cmdObj)97 StatusWith<Shard::CommandResponse> ShardLocal::_runCommand(OperationContext* opCtx,
98                                                            const ReadPreferenceSetting& unused,
99                                                            const std::string& dbName,
100                                                            Milliseconds maxTimeMSOverrideUnused,
101                                                            const BSONObj& cmdObj) {
102     return _rsLocalClient.runCommandOnce(opCtx, dbName, cmdObj);
103 }
104 
_exhaustiveFindOnConfig(OperationContext * opCtx,const ReadPreferenceSetting & readPref,const repl::ReadConcernLevel & readConcernLevel,const NamespaceString & nss,const BSONObj & query,const BSONObj & sort,boost::optional<long long> limit)105 StatusWith<Shard::QueryResponse> ShardLocal::_exhaustiveFindOnConfig(
106     OperationContext* opCtx,
107     const ReadPreferenceSetting& readPref,
108     const repl::ReadConcernLevel& readConcernLevel,
109     const NamespaceString& nss,
110     const BSONObj& query,
111     const BSONObj& sort,
112     boost::optional<long long> limit) {
113     return _rsLocalClient.queryOnce(opCtx, readPref, readConcernLevel, nss, query, sort, limit);
114 }
115 
createIndexOnConfig(OperationContext * opCtx,const NamespaceString & ns,const BSONObj & keys,bool unique)116 Status ShardLocal::createIndexOnConfig(OperationContext* opCtx,
117                                        const NamespaceString& ns,
118                                        const BSONObj& keys,
119                                        bool unique) {
120     invariant(ns.db() == "config" || ns.db() == "admin");
121 
122     try {
123         DBDirectClient client(opCtx);
124         IndexSpec index;
125         index.addKeys(keys);
126         index.unique(unique);
127         client.createIndex(ns.toString(), index);
128     } catch (const DBException& e) {
129         return e.toStatus();
130     }
131 
132     return Status::OK();
133 }
134 
135 }  // namespace mongo
136