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 #pragma once
32 
33 #include <string>
34 
35 #include "mongo/s/client/shard.h"
36 
37 #include "mongo/base/disallow_copying.h"
38 
39 namespace mongo {
40 
41 /*
42  * Maintains the targeting and command execution logic for a single shard. Performs polling of
43  * the shard (if replica set).
44  */
45 class ShardRemote : public Shard {
46     MONGO_DISALLOW_COPYING(ShardRemote);
47 
48 public:
49     /**
50      * Instantiates a new shard connection management object for the specified shard.
51      */
52     ShardRemote(const ShardId& id,
53                 const ConnectionString& originalConnString,
54                 std::unique_ptr<RemoteCommandTargeter> targeter);
55 
56     ~ShardRemote();
57 
58     const ConnectionString getConnString() const override;
59 
originalConnString()60     const ConnectionString originalConnString() const override {
61         return _originalConnString;
62     }
63 
getTargeter()64     std::shared_ptr<RemoteCommandTargeter> getTargeter() const override {
65         return _targeter;
66     }
67 
68     void updateReplSetMonitor(const HostAndPort& remoteHost,
69                               const Status& remoteCommandStatus) override;
70 
71     std::string toString() const override;
72 
73     bool isRetriableError(ErrorCodes::Error code, RetryPolicy options) final;
74 
75     Status createIndexOnConfig(OperationContext* opCtx,
76                                const NamespaceString& ns,
77                                const BSONObj& keys,
78                                bool unique) override;
79 
80 private:
81     /**
82      * Returns the metadata that should be used when running commands against this shard with
83      * the given read preference.
84      */
85     BSONObj _appendMetadataForCommand(OperationContext* opCtx,
86                                       const ReadPreferenceSetting& readPref);
87 
88     StatusWith<Shard::CommandResponse> _runCommand(OperationContext* opCtx,
89                                                    const ReadPreferenceSetting& readPref,
90                                                    const std::string& dbname,
91                                                    Milliseconds maxTimeMSOverride,
92                                                    const BSONObj& cmdObj) final;
93 
94     StatusWith<QueryResponse> _exhaustiveFindOnConfig(
95         OperationContext* opCtx,
96         const ReadPreferenceSetting& readPref,
97         const repl::ReadConcernLevel& readConcernLevel,
98         const NamespaceString& nss,
99         const BSONObj& query,
100         const BSONObj& sort,
101         boost::optional<long long> limit) final;
102 
103     /**
104      * Connection string for the shard at the creation time.
105      */
106     const ConnectionString _originalConnString;
107 
108     /**
109      * Targeter for obtaining hosts from which to read or to which to write.
110      */
111     const std::shared_ptr<RemoteCommandTargeter> _targeter;
112 };
113 
114 }  // namespace mongo
115