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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
|
/**
* 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 "mongo/base/disallow_copying.h"
#include "mongo/base/status.h"
#include "mongo/base/status_with.h"
#include "mongo/base/string_data.h"
#include "mongo/bson/bsonobj.h"
#include "mongo/bson/timestamp.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/repl/storage_interface.h"
#include "mongo/stdx/mutex.h"
namespace mongo {
namespace repl {
struct CollectionMockStats {
bool initCalled = false;
int insertCount = 0;
bool commitCalled = false;
};
class CollectionBulkLoaderMock : public CollectionBulkLoader {
MONGO_DISALLOW_COPYING(CollectionBulkLoaderMock);
public:
CollectionBulkLoaderMock(CollectionMockStats* collStats) : stats(collStats){};
virtual ~CollectionBulkLoaderMock() = default;
virtual Status init(const std::vector<BSONObj>& secondaryIndexSpecs) override;
virtual Status insertDocuments(const std::vector<BSONObj>::const_iterator begin,
const std::vector<BSONObj>::const_iterator end) override;
virtual Status commit() override;
std::string toString() const override {
return toBSON().toString();
};
BSONObj toBSON() const override {
return BSONObj();
};
CollectionMockStats* stats;
// Override functions.
stdx::function<Status(const std::vector<BSONObj>::const_iterator begin,
const std::vector<BSONObj>::const_iterator end)>
insertDocsFn = [](const std::vector<BSONObj>::const_iterator,
const std::vector<BSONObj>::const_iterator) { return Status::OK(); };
stdx::function<Status()> abortFn = []() { return Status::OK(); };
stdx::function<Status()> commitFn = []() { return Status::OK(); };
};
class StorageInterfaceMock : public StorageInterface {
MONGO_DISALLOW_COPYING(StorageInterfaceMock);
public:
// Used for testing.
using CreateCollectionForBulkFn =
stdx::function<StatusWith<std::unique_ptr<CollectionBulkLoader>>(
const NamespaceString& nss,
const CollectionOptions& options,
const BSONObj idIndexSpec,
const std::vector<BSONObj>& secondaryIndexSpecs)>;
using InsertDocumentFn = stdx::function<Status(OperationContext* opCtx,
const NamespaceStringOrUUID& nsOrUUID,
const TimestampedBSONObj& doc,
long long term)>;
using InsertDocumentsFn = stdx::function<Status(OperationContext* opCtx,
const NamespaceStringOrUUID& nsOrUUID,
const std::vector<InsertStatement>& docs)>;
using DropUserDatabasesFn = stdx::function<Status(OperationContext* opCtx)>;
using CreateOplogFn =
stdx::function<Status(OperationContext* opCtx, const NamespaceString& nss)>;
using CreateCollectionFn = stdx::function<Status(
OperationContext* opCtx, const NamespaceString& nss, const CollectionOptions& options)>;
using TruncateCollectionFn =
stdx::function<Status(OperationContext* opCtx, const NamespaceString& nss)>;
using DropCollectionFn =
stdx::function<Status(OperationContext* opCtx, const NamespaceString& nss)>;
using FindDocumentsFn =
stdx::function<StatusWith<std::vector<BSONObj>>(OperationContext* opCtx,
const NamespaceString& nss,
boost::optional<StringData> indexName,
ScanDirection scanDirection,
const BSONObj& startKey,
BoundInclusion boundInclusion,
std::size_t limit)>;
using DeleteDocumentsFn =
stdx::function<StatusWith<std::vector<BSONObj>>(OperationContext* opCtx,
const NamespaceString& nss,
boost::optional<StringData> indexName,
ScanDirection scanDirection,
const BSONObj& startKey,
BoundInclusion boundInclusion,
std::size_t limit)>;
using IsAdminDbValidFn = stdx::function<Status(OperationContext* opCtx)>;
using GetCollectionUUIDFn = stdx::function<StatusWith<OptionalCollectionUUID>(
OperationContext* opCtx, const NamespaceString& nss)>;
using UpgradeNonReplicatedUniqueIndexesFn = stdx::function<Status(OperationContext* opCtx)>;
StorageInterfaceMock() = default;
StatusWith<int> getRollbackID(OperationContext* opCtx) override;
StatusWith<int> initializeRollbackID(OperationContext* opCtx) override;
StatusWith<int> incrementRollbackID(OperationContext* opCtx) override;
StatusWith<std::unique_ptr<CollectionBulkLoader>> createCollectionForBulkLoading(
const NamespaceString& nss,
const CollectionOptions& options,
const BSONObj idIndexSpec,
const std::vector<BSONObj>& secondaryIndexSpecs) override {
return createCollectionForBulkFn(nss, options, idIndexSpec, secondaryIndexSpecs);
};
Status insertDocument(OperationContext* opCtx,
const NamespaceStringOrUUID& nsOrUUID,
const TimestampedBSONObj& doc,
long long term) override {
return insertDocumentFn(opCtx, nsOrUUID, doc, term);
};
Status insertDocuments(OperationContext* opCtx,
const NamespaceStringOrUUID& nsOrUUID,
const std::vector<InsertStatement>& docs) override {
return insertDocumentsFn(opCtx, nsOrUUID, docs);
}
Status dropReplicatedDatabases(OperationContext* opCtx) override {
return dropUserDBsFn(opCtx);
};
Status createOplog(OperationContext* opCtx, const NamespaceString& nss) override {
return createOplogFn(opCtx, nss);
};
StatusWith<size_t> getOplogMaxSize(OperationContext* opCtx,
const NamespaceString& nss) override {
return 1024 * 1024 * 1024;
}
Status createCollection(OperationContext* opCtx,
const NamespaceString& nss,
const CollectionOptions& options) override {
return createCollFn(opCtx, nss, options);
}
Status dropCollection(OperationContext* opCtx, const NamespaceString& nss) override {
return dropCollFn(opCtx, nss);
};
Status truncateCollection(OperationContext* opCtx, const NamespaceString& nss) override {
return truncateCollFn(opCtx, nss);
}
Status renameCollection(OperationContext* opCtx,
const NamespaceString& fromNS,
const NamespaceString& toNS,
bool stayTemp) override {
return Status{ErrorCodes::IllegalOperation, "renameCollection not implemented."};
}
Status setIndexIsMultikey(OperationContext* opCtx,
const NamespaceString& nss,
const std::string& indexName,
const MultikeyPaths& paths,
Timestamp ts) override {
return Status{ErrorCodes::IllegalOperation, "setIndexIsMultikey not implemented."};
}
StatusWith<std::vector<BSONObj>> findDocuments(OperationContext* opCtx,
const NamespaceString& nss,
boost::optional<StringData> indexName,
ScanDirection scanDirection,
const BSONObj& startKey,
BoundInclusion boundInclusion,
std::size_t limit) override {
return findDocumentsFn(
opCtx, nss, indexName, scanDirection, startKey, boundInclusion, limit);
}
StatusWith<std::vector<BSONObj>> deleteDocuments(OperationContext* opCtx,
const NamespaceString& nss,
boost::optional<StringData> indexName,
ScanDirection scanDirection,
const BSONObj& startKey,
BoundInclusion boundInclusion,
std::size_t limit) override {
return deleteDocumentsFn(
opCtx, nss, indexName, scanDirection, startKey, boundInclusion, limit);
}
StatusWith<BSONObj> findSingleton(OperationContext* opCtx,
const NamespaceString& nss) override {
return Status{ErrorCodes::IllegalOperation, "findSingleton not implemented."};
}
Status putSingleton(OperationContext* opCtx,
const NamespaceString& nss,
const TimestampedBSONObj& update) override {
return Status{ErrorCodes::IllegalOperation, "putSingleton not implemented."};
}
Status updateSingleton(OperationContext* opCtx,
const NamespaceString& nss,
const BSONObj& query,
const TimestampedBSONObj& update) override {
return Status{ErrorCodes::IllegalOperation, "updateSingleton not implemented."};
}
StatusWith<BSONObj> findById(OperationContext* opCtx,
const NamespaceStringOrUUID&,
const BSONElement& idKey) override {
return Status{ErrorCodes::IllegalOperation, "findById not implemented."};
}
StatusWith<BSONObj> deleteById(OperationContext* opCtx,
const NamespaceStringOrUUID&,
const BSONElement& idKey) override {
return Status{ErrorCodes::IllegalOperation, "deleteById not implemented."};
}
Status upsertById(OperationContext* opCtx,
const NamespaceStringOrUUID& nsOrUUID,
const BSONElement& idKey,
const BSONObj& update) override {
return Status{ErrorCodes::IllegalOperation, "upsertById not implemented."};
}
Status deleteByFilter(OperationContext* opCtx,
const NamespaceString& nss,
const BSONObj& filter) override {
return Status{ErrorCodes::IllegalOperation, "deleteByFilter not implemented."};
}
StatusWith<StorageInterface::CollectionSize> getCollectionSize(
OperationContext* opCtx, const NamespaceString& nss) override {
return 0;
}
StatusWith<StorageInterface::CollectionCount> getCollectionCount(
OperationContext* opCtx, const NamespaceStringOrUUID& nsOrUUID) override {
return 0;
}
Status setCollectionCount(OperationContext* opCtx,
const NamespaceStringOrUUID& nsOrUUID,
long long newCount) override {
return Status{ErrorCodes::IllegalOperation, "setCollectionCount not implemented."};
}
StatusWith<OptionalCollectionUUID> getCollectionUUID(OperationContext* opCtx,
const NamespaceString& nss) override {
return getCollectionUUIDFn(opCtx, nss);
}
Status upgradeNonReplicatedUniqueIndexes(OperationContext* opCtx) override {
return upgradeNonReplicatedUniqueIndexesFn(opCtx);
}
void setStableTimestamp(ServiceContext* serviceCtx, Timestamp snapshotName) override;
void setInitialDataTimestamp(ServiceContext* serviceCtx, Timestamp snapshotName) override;
Timestamp getStableTimestamp() const;
Timestamp getInitialDataTimestamp() const;
StatusWith<Timestamp> recoverToStableTimestamp(OperationContext* opCtx) override {
return Status{ErrorCodes::IllegalOperation, "recoverToStableTimestamp not implemented."};
}
bool supportsRecoverToStableTimestamp(ServiceContext* serviceCtx) const override {
return false;
}
bool supportsRecoveryTimestamp(ServiceContext* serviceCtx) const override {
return false;
}
boost::optional<Timestamp> getRecoveryTimestamp(ServiceContext* serviceCtx) const override {
return boost::none;
}
Timestamp getAllCommittedTimestamp(ServiceContext* serviceCtx) const override;
bool supportsDocLocking(ServiceContext* serviceCtx) const override;
Status isAdminDbValid(OperationContext* opCtx) override {
return isAdminDbValidFn(opCtx);
};
void waitForAllEarlierOplogWritesToBeVisible(OperationContext* opCtx) override {
return;
}
void oplogDiskLocRegister(OperationContext* opCtx,
const Timestamp& ts,
bool orderedCommit) override {
return;
}
boost::optional<Timestamp> getLastStableRecoveryTimestamp(
ServiceContext* serviceCtx) const override {
return boost::none;
}
boost::optional<Timestamp> getLastStableCheckpointTimestampDeprecated(
ServiceContext* serviceCtx) const override {
return boost::none;
}
// Testing functions.
CreateCollectionForBulkFn createCollectionForBulkFn =
[](const NamespaceString& nss,
const CollectionOptions& options,
const BSONObj idIndexSpec,
const std::vector<BSONObj>&
secondaryIndexSpecs) -> StatusWith<std::unique_ptr<CollectionBulkLoader>> {
return Status{ErrorCodes::IllegalOperation, "CreateCollectionForBulkFn not implemented."};
};
InsertDocumentFn insertDocumentFn = [](OperationContext* opCtx,
const NamespaceStringOrUUID& nsOrUUID,
const TimestampedBSONObj& doc,
long long term) {
return Status{ErrorCodes::IllegalOperation, "InsertDocumentFn not implemented."};
};
InsertDocumentsFn insertDocumentsFn = [](OperationContext* opCtx,
const NamespaceStringOrUUID& nsOrUUID,
const std::vector<InsertStatement>& docs) {
return Status{ErrorCodes::IllegalOperation, "InsertDocumentsFn not implemented."};
};
DropUserDatabasesFn dropUserDBsFn = [](OperationContext* opCtx) {
return Status{ErrorCodes::IllegalOperation, "DropUserDatabasesFn not implemented."};
};
CreateOplogFn createOplogFn = [](OperationContext* opCtx, const NamespaceString& nss) {
return Status{ErrorCodes::IllegalOperation, "CreateOplogFn not implemented."};
};
CreateCollectionFn createCollFn =
[](OperationContext* opCtx, const NamespaceString& nss, const CollectionOptions& options) {
return Status{ErrorCodes::IllegalOperation, "CreateCollectionFn not implemented."};
};
TruncateCollectionFn truncateCollFn = [](OperationContext* opCtx, const NamespaceString& nss) {
return Status{ErrorCodes::IllegalOperation, "TruncateCollectionFn not implemented."};
};
DropCollectionFn dropCollFn = [](OperationContext* opCtx, const NamespaceString& nss) {
return Status{ErrorCodes::IllegalOperation, "DropCollectionFn not implemented."};
};
FindDocumentsFn findDocumentsFn = [](OperationContext* opCtx,
const NamespaceString& nss,
boost::optional<StringData> indexName,
ScanDirection scanDirection,
const BSONObj& startKey,
BoundInclusion boundInclusion,
std::size_t limit) {
return Status{ErrorCodes::IllegalOperation, "FindOneFn not implemented."};
};
DeleteDocumentsFn deleteDocumentsFn = [](OperationContext* opCtx,
const NamespaceString& nss,
boost::optional<StringData> indexName,
ScanDirection scanDirection,
const BSONObj& startKey,
BoundInclusion boundInclusion,
std::size_t limit) {
return Status{ErrorCodes::IllegalOperation, "DeleteOneFn not implemented."};
};
IsAdminDbValidFn isAdminDbValidFn = [](OperationContext*) {
return Status{ErrorCodes::IllegalOperation, "IsAdminDbValidFn not implemented."};
};
GetCollectionUUIDFn getCollectionUUIDFn = [](
OperationContext* opCtx, const NamespaceString& nss) -> StatusWith<OptionalCollectionUUID> {
return Status{ErrorCodes::IllegalOperation, "GetCollectionUUIDFn not implemented."};
};
UpgradeNonReplicatedUniqueIndexesFn upgradeNonReplicatedUniqueIndexesFn =
[](OperationContext* opCtx) -> Status {
return Status{ErrorCodes::IllegalOperation,
"upgradeNonReplicatedUniqueIndexesFn not implemented."};
};
bool supportsDocLockingBool = false;
Timestamp allCommittedTimestamp = Timestamp::min();
private:
mutable stdx::mutex _mutex;
int _rbid;
bool _rbidInitialized = false;
Timestamp _stableTimestamp = Timestamp::min();
Timestamp _initialDataTimestamp = Timestamp::min();
OptionalCollectionUUID _uuid;
bool _schemaUpgraded;
};
} // namespace repl
} // namespace mongo
|