1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
|
/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#pragma once
#include <string>
#include <vector>
#include "mongo/client/connection_string.h"
#include "mongo/client/query.h"
#include "mongo/db/jsobj.h"
#include "mongo/db/query/find_command_gen.h"
#include "mongo/rpc/unique_message.h"
#include "mongo/stdx/unordered_map.h"
#include "mongo/util/concurrency/spin_lock.h"
namespace mongo {
namespace projection_executor {
class ProjectionExecutor;
} // namespace projection_executor
const std::string IdentityNS("local.me");
const BSONField<std::string> HostField("host");
/**
* A very simple mock that acts like a database server. Every object keeps track of its own
* InstanceID, which initially starts at zero and increments every time it is restarted.
* This is primarily used for simulating the state of which old connections won't be able
* to talk to the sockets that has already been closed on this server.
*
* Note: All operations on this server are protected by a lock.
*/
class MockRemoteDBServer {
public:
typedef size_t InstanceID;
/**
* Creates a new mock server. This can also be setup to work with the
* ConnectionString class by using mongo::MockConnRegistry as follows:
*
* ConnectionString::setConnectionHook(MockConnRegistry::get()->getConnStrHook());
* MockRemoteDBServer server("$a:27017");
* MockConnRegistry::get()->addServer(&server);
*
* This allows clients using the ConnectionString::connect interface to create
* connections to this server. The requirements to make this hook fully functional are:
*
* 1. hostAndPort of this server should start with $.
* 2. No other instance has the same hostAndPort as this.
*
* This server will also contain the hostAndPort inside the IdentityNS
* collection. This is convenient for testing query routing.
*
* @param hostAndPort the host name with port for this server.
*
* @see MockConnRegistry
*/
MockRemoteDBServer(const std::string& hostAndPort);
virtual ~MockRemoteDBServer();
//
// Connectivity methods
//
/**
* Set a delay for calls to query and runCommand
*/
void setDelay(long long milliSec);
/**
* Shuts down this server. Any operations on this server with an InstanceID
* less than or equal to the current one will throw a mongo::SocketException.
* To bring the server up again, use the reboot method.
*/
void shutdown();
/**
* Increments the instanceID of this server.
*/
void reboot();
/**
* @return true if this server is running
*/
bool isRunning() const;
//
// Mocking methods
//
/**
* Sets the reply for a command.
*
* @param cmdName the name of the command
* @param replyObj the exact reply for the command
*/
void setCommandReply(const std::string& cmdName, const StatusWith<mongo::BSONObj>& replyObj);
/**
* Sets the reply for a command.
*
* @param cmdName the name of the command.
* @param replySequence the sequence of replies to cycle through every time
* the given command is requested. This is useful for setting up a
* sequence of response when the command can be called more than once
* that requires different results when calling a method.
*/
void setCommandReply(const std::string& cmdName,
const std::vector<StatusWith<mongo::BSONObj>>& replySequence);
/**
* Inserts a single document to this server.
*
* @param ns the namespace to insert the document to.
* @param obj the document to insert.
*/
void insert(const std::string& ns, BSONObj obj);
/**
* Removes documents from this server.
*
* @param ns the namespace to remove documents from.
* @param filter ignored.
*/
void remove(const std::string& ns, const BSONObj& filter);
/**
* Assign a UUID to a collection
*
* @param ns the namespace to be associated with the uuid.
* @param uuid the uuid to associate with the namespace.
*/
void assignCollectionUuid(const std::string& ns, const mongo::UUID& uuid);
//
// DBClientBase methods
//
rpc::UniqueReply runCommand(InstanceID id, const OpMsgRequest& request);
/**
* Finds documents from this mock server according to 'findRequest'.
*/
mongo::BSONArray find(InstanceID id, const FindCommandRequest& findRequest);
/**
* Legacy query API: New callers should use 'find()' rather than this method.
*/
mongo::BSONArray query(InstanceID id,
const NamespaceStringOrUUID& nsOrUuid,
const BSONObj& filter,
const Query& querySettings,
int limit = 0,
int nToSkip = 0,
const mongo::BSONObj* fieldsToReturn = nullptr,
int queryOptions = 0,
int batchSize = 0,
boost::optional<BSONObj> readConcernObj = boost::none);
//
// Getters
//
InstanceID getInstanceID() const;
mongo::ConnectionString::ConnectionType type() const;
double getSoTimeout() const;
/**
* @returns the value passed to hostAndPort parameter of the
* constructor. In other words, doesn't automatically append a
* 'default' port if none is specified.
*/
std::string getServerAddress() const;
std::string toString();
const HostAndPort& getServerHostAndPort() const;
//
// Call counters
//
size_t getCmdCount() const;
size_t getQueryCount() const;
void clearCounters();
private:
/**
* A very simple class for cycling through a set of BSONObj
*/
class CircularBSONIterator {
public:
/**
* Creates a new iterator with a deep copy of the vector.
*/
CircularBSONIterator(const std::vector<StatusWith<mongo::BSONObj>>& replyVector);
StatusWith<mongo::BSONObj> next();
private:
std::vector<StatusWith<mongo::BSONObj>>::iterator _iter;
std::vector<StatusWith<mongo::BSONObj>> _replyObjs;
};
/**
* Checks whether the instance of the server is still up.
*
* @throws mongo::SocketException if this server is down
*/
void checkIfUp(InstanceID id) const;
/**
* Creates a ProjectionExecutor to handle fieldsToReturn.
*/
std::unique_ptr<projection_executor::ProjectionExecutor> createProjectionExecutor(
const BSONObj& projectionSpec);
/**
* Projects the object, unless the projectionExecutor is null, in which case returns a
* copy of the object.
*/
BSONObj project(projection_executor::ProjectionExecutor* projectionExecutor, const BSONObj& o);
/**
* Logic shared between 'find()' and 'query()'. This can go away when the legacy 'query()' API
* is removed.
*/
mongo::BSONArray findImpl(InstanceID id,
const NamespaceStringOrUUID& nsOrUuid,
BSONObj projection);
typedef stdx::unordered_map<std::string, std::shared_ptr<CircularBSONIterator>> CmdToReplyObj;
typedef stdx::unordered_map<std::string, std::vector<BSONObj>> MockDataMgr;
typedef stdx::unordered_map<mongo::UUID, std::string, UUID::Hash> UUIDMap;
bool _isRunning;
const HostAndPort _hostAndPort;
long long _delayMilliSec;
//
// Mock replies
//
CmdToReplyObj _cmdMap;
MockDataMgr _dataMgr;
UUIDMap _uuidToNs;
//
// Op Counters
//
size_t _cmdCount;
size_t _queryCount;
// Unique id for every restart of this server used for rejecting requests from
// connections that are still "connected" to the old instance
InstanceID _instanceID;
// protects this entire instance
mutable mongo::SpinLock _lock;
};
} // namespace mongo
|