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
|
/**
* Copyright (C) 2015 MongoDB Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* 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 GNU Affero General 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 <boost/optional.hpp>
#include <cstddef>
#include <iosfwd>
#include <string>
#include "mongo/base/disallow_copying.h"
#include "mongo/base/string_data.h"
#include "mongo/db/catalog/collection.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/query/index_bounds.h"
#include "mongo/db/repl/collection_bulk_loader.h"
#include "mongo/db/repl/optime.h"
#include "mongo/db/service_context.h"
#include "mongo/db/storage/snapshot_name.h"
namespace mongo {
class Collection;
struct CollectionOptions;
class OperationContext;
namespace repl {
struct TimestampedBSONObj {
BSONObj obj;
SnapshotName timestamp;
};
/**
* Storage interface used by the replication system to interact with storage.
* This interface provides seperation of concerns and a place for mocking out test
* interactions.
*
* The grouping of functionality includes general collection helpers, and more specific replication
* concepts:
* * Create Collection and Oplog
* * Drop database and all user databases
* * Drop a collection
* * Insert documents into a collection
*/
class StorageInterface {
MONGO_DISALLOW_COPYING(StorageInterface);
public:
// Operation Context binding.
static StorageInterface* get(ServiceContext* service);
static StorageInterface* get(ServiceContext& service);
static StorageInterface* get(OperationContext* opCtx);
static void set(ServiceContext* service, std::unique_ptr<StorageInterface> storageInterface);
// Constructor and Destructor.
StorageInterface() = default;
virtual ~StorageInterface() = default;
/**
* Rollback ID is an increasing counter of how many rollbacks have occurred on this server.
*/
virtual StatusWith<int> getRollbackID(OperationContext* opCtx) = 0;
virtual Status initializeRollbackID(OperationContext* opCtx) = 0;
virtual Status incrementRollbackID(OperationContext* opCtx) = 0;
// Collection creation and population for initial sync.
/**
* Creates a collection with the provided indexes.
*
* Assumes that no database locks have been acquired prior to calling this function.
*/
virtual StatusWith<std::unique_ptr<CollectionBulkLoader>> createCollectionForBulkLoading(
const NamespaceString& nss,
const CollectionOptions& options,
const BSONObj idIndexSpec,
const std::vector<BSONObj>& secondaryIndexSpecs) = 0;
/**
* Inserts a document with a timestamp into a collection.
*
* NOTE: If the collection doesn't exist, it will not be created, and instead
* an error is returned.
*/
virtual Status insertDocument(OperationContext* opCtx,
const NamespaceString& nss,
const TimestampedBSONObj& doc,
long long term) = 0;
/**
* Inserts the given documents, with associated timestamps and statement id's, into the
* collection.
* It is an error to call this function with an empty set of documents.
*/
virtual Status insertDocuments(OperationContext* opCtx,
const NamespaceString& nss,
const std::vector<InsertStatement>& docs) = 0;
/**
* Creates the initial oplog, errors if it exists.
*/
virtual Status createOplog(OperationContext* opCtx, const NamespaceString& nss) = 0;
/**
* Returns the configured maximum size of the oplog.
*
* Implementations are allowed to be "fuzzy" and delete documents when the actual size is
* slightly above or below this, so callers should not rely on its exact value.
*/
virtual StatusWith<size_t> getOplogMaxSize(OperationContext* opCtx,
const NamespaceString& nss) = 0;
/**
* Creates a collection.
*/
virtual Status createCollection(OperationContext* opCtx,
const NamespaceString& nss,
const CollectionOptions& options) = 0;
/**
* Drops a collection.
*/
virtual Status dropCollection(OperationContext* opCtx, const NamespaceString& nss) = 0;
/**
* Truncates a collection.
*/
virtual Status truncateCollection(OperationContext* opCtx, const NamespaceString& nss) = 0;
/**
* Renames a collection from the "fromNS" to the "toNS". Fails if the new collection already
* exists.
*/
virtual Status renameCollection(OperationContext* opCtx,
const NamespaceString& fromNS,
const NamespaceString& toNS,
bool stayTemp) = 0;
/**
* Drops all databases except "local".
*/
virtual Status dropReplicatedDatabases(OperationContext* opCtx) = 0;
/**
* Validates that the admin database is valid during initial sync.
*/
virtual Status isAdminDbValid(OperationContext* opCtx) = 0;
/**
* Finds at most "limit" documents returned by a collection or index scan on the collection in
* the requested direction.
* The documents returned will be copied and buffered. No cursors on the underlying collection
* will be kept open once this function returns.
* If "indexName" is boost::none, a collection scan is used to locate the document.
* Index scan options:
* If "startKey" is not empty, the index scan will start from the given key (instead of
* MinKey/MaxKey).
* Set "boundInclusion" to BoundInclusion::kIncludeStartKeyOnly to include "startKey" in
* the index scan results. Set to BoundInclusion::kIncludeEndKeyOnly to return the key
* immediately following "startKey" from the index.
*/
enum class ScanDirection {
kForward = 1,
kBackward = -1,
};
virtual 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) = 0;
/**
* Deletes at most "limit" documents returned by a collection or index scan on the collection in
* the requested direction. Returns deleted documents on success.
* The documents returned will be copied and buffered. No cursors on the underlying collection
* will be kept open once this function returns.
* If "indexName" is null, a collection scan is used to locate the document.
*/
virtual 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) = 0;
/**
* Finds a singleton document in a collection. Returns 'CollectionIsEmpty' if the collection
* is empty or 'TooManyMatchingDocuments' if it is not a singleton collection.
*/
virtual StatusWith<BSONObj> findSingleton(OperationContext* opCtx,
const NamespaceString& nss) = 0;
/**
* Updates a singleton document in a collection. Upserts the document if it does not exist. If
* the document is upserted and no '_id' is provided, one will be generated.
* If the collection has more than 1 document, the update will only be performed on the first
* one found.
* Returns 'NamespaceNotFound' if the collection does not exist. This does not implicitly
* create the collection so that the caller can create the collection with any collection
* options they want (ex: capped, temp, collation, etc.).
*/
virtual Status putSingleton(OperationContext* opCtx,
const NamespaceString& nss,
const BSONObj& update) = 0;
/**
* Updates a singleton document in a collection. Never upsert.
*
* If the collection has more than 1 document, the update will only be performed on the first
* one found.
* Returns 'NamespaceNotFound' if the collection does not exist. This does not implicitly
* create the collection so that the caller can create the collection with any collection
* options they want (ex: capped, temp, collation, etc.).
*/
virtual Status updateSingleton(OperationContext* opCtx,
const NamespaceString& nss,
const BSONObj& query,
const BSONObj& update) = 0;
/**
* Finds a single document in the collection referenced by the specified _id.
*
* Not supported on collections with a default collation.
*/
virtual StatusWith<BSONObj> findById(OperationContext* opCtx,
const NamespaceString& nss,
const BSONElement& idKey) = 0;
/**
* Deletes a single document in the collection referenced by the specified _id.
* Returns deleted document on success.
*
* Not supported on collections with a default collation.
*/
virtual StatusWith<BSONObj> deleteById(OperationContext* opCtx,
const NamespaceString& nss,
const BSONElement& idKey) = 0;
/**
* Updates a single document in the collection referenced by the specified _id.
* The document is located by looking up "idKey" in the id index.
* "update" represents the replacement document or list of requested modifications to be applied
* to the document.
* If the document is not found, a new document will be created with the requested modifications
* applied.
*/
virtual Status upsertById(OperationContext* opCtx,
const NamespaceString& nss,
const BSONElement& idKey,
const BSONObj& update) = 0;
/**
* Removes all documents that match the "filter" from a collection.
* "filter" specifies the deletion criteria using query operators. Pass in an empty document to
* delete all documents in a collection.
*/
virtual Status deleteByFilter(OperationContext* opCtx,
const NamespaceString& nss,
const BSONObj& filter) = 0;
using CollectionSize = uint64_t;
using CollectionCount = uint64_t;
/**
* Returns the sum of the sizes of documents in the collection in bytes.
*/
virtual StatusWith<CollectionSize> getCollectionSize(OperationContext* opCtx,
const NamespaceString& nss) = 0;
/**
* Returns the number of documents in the collection.
*/
virtual StatusWith<CollectionCount> getCollectionCount(OperationContext* opCtx,
const NamespaceString& nss) = 0;
/**
* Returns the UUID of the collection specified by nss, if such a UUID exists.
*/
virtual StatusWith<OptionalCollectionUUID> getCollectionUUID(OperationContext* opCtx,
const NamespaceString& nss) = 0;
/**
* Adds UUIDs for non-replicated collections. To be called only at the end of initial
* sync and only if the admin.system.version collection has a UUID.
*/
virtual Status upgradeUUIDSchemaVersionNonReplicated(OperationContext* opCtx) = 0;
/**
* Sets the highest timestamp at which the storage engine is allowed to take a checkpoint.
* This timestamp can never decrease, and thus should be a timestamp that can never roll back.
*/
virtual void setStableTimestamp(ServiceContext* serviceCtx, SnapshotName snapshotName) = 0;
/**
* Tells the storage engine the timestamp of the data at startup. This is necessary because
* timestamps are not persisted in the storage layer.
*/
virtual void setInitialDataTimestamp(ServiceContext* serviceCtx, SnapshotName snapshotName) = 0;
/**
* Reverts the state of all database data to the last stable timestamp.
*
* The "local" database is exempt and none of its state should be reverted except for
* "local.replset.minvalid" and "local.replset.checkpointTimestamp" which should be reverted to
* the last stable timestamp.
*
* The 'stable' timestamp is set by calling StorageInterface::setStableTimestamp.
*/
virtual Status recoverToStableTimestamp(ServiceContext* serviceCtx) = 0;
/**
* Waits for oplog writes to be visible in the oplog.
* This function is used to ensure tests do not fail due to initial sync receiving an empty
* batch.
*/
virtual void waitForAllEarlierOplogWritesToBeVisible(OperationContext* opCtx) = 0;
};
} // namespace repl
} // namespace mongo
|