summaryrefslogtreecommitdiff
path: root/src/mongo/s/chunk_version.h
blob: fef1a581909e585a69140483c91ae01d476e3bfa (plain)
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/**
*    Copyright (C) 2012 10gen 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 "mongo/db/jsobj.h"

namespace mongo {

/**
 * ChunkVersions consist of a major/minor version scoped to a version epoch
 *
 * Version configurations (format: major version, epoch):
 *
 * 1. (0, 0) - collection is dropped.
 * 2. (0, n), n > 0 - applicable only to shardVersion; shard has no chunk.
 * 3. (n, 0), n > 0 - invalid configuration.
 * 4. (n, m), n > 0, m > 0 - normal sharded collection version.
 *
 * TODO: This is a "manual type" but, even so, still needs to comform to what's
 * expected from types.
 */
struct ChunkVersion {
    union {
        struct {
            int _minor;
            int _major;
        };
        unsigned long long _combined;
    };
    OID _epoch;

    ChunkVersion() : _minor(0), _major(0), _epoch(OID()) {}

    //
    // Constructors shouldn't have default parameters here, since it's vital we track from
    // here on the epochs of versions, even if not used.
    //

    ChunkVersion(int major, int minor, const OID& epoch)
        : _minor(minor), _major(major), _epoch(epoch) {}

    static ChunkVersion DROPPED() {
        return ChunkVersion(0, 0, OID());  // dropped OID is zero time, zero machineId/inc
    }

    static ChunkVersion UNSHARDED() {
        // TODO: Distinguish between these cases
        return DROPPED();
    }

    static ChunkVersion IGNORED() {
        ChunkVersion version = ChunkVersion();
        version._epoch.init(Date_t(), true);  // ignored OID is zero time, max machineId/inc
        return version;
    }

    static ChunkVersion fromDeprecatedLong(unsigned long long num, const OID& epoch) {
        ChunkVersion version(0, 0, epoch);
        version._combined = num;
        return version;
    }

    static bool isIgnoredVersion(const ChunkVersion& version) {
        return version.majorVersion() == 0 && version.minorVersion() == 0 &&
            version.epoch() == IGNORED().epoch();
    }

    void incMajor() {
        _major++;
        _minor = 0;
    }

    void incMinor() {
        _minor++;
    }

    // Incrementing an epoch creates a new, randomly generated identifier
    void incEpoch() {
        _epoch = OID::gen();
        _major = 0;
        _minor = 0;
    }

    // Note: this shouldn't be used as a substitute for version except in specific cases -
    // epochs make versions more complex
    unsigned long long toLong() const {
        return _combined;
    }

    bool isSet() const {
        return _combined > 0;
    }

    bool isEpochSet() const {
        return _epoch.isSet();
    }

    std::string toString() const {
        std::stringstream ss;
        // Similar to month/day/year.  For the most part when debugging, we care about major
        // so it's first
        ss << _major << "|" << _minor << "||" << _epoch;
        return ss.str();
    }

    int majorVersion() const {
        return _major;
    }
    int minorVersion() const {
        return _minor;
    }
    OID epoch() const {
        return _epoch;
    }

    //
    // Explicit comparison operators - versions with epochs have non-trivial comparisons.
    // > < operators do not check epoch cases.  Generally if using == we need to handle
    // more complex cases.
    //

    bool operator>(const ChunkVersion& otherVersion) const {
        return this->_combined > otherVersion._combined;
    }

    bool operator>=(const ChunkVersion& otherVersion) const {
        return this->_combined >= otherVersion._combined;
    }

    bool operator<(const ChunkVersion& otherVersion) const {
        return this->_combined < otherVersion._combined;
    }

    bool operator<=(const ChunkVersion& otherVersion) const {
        return this->_combined <= otherVersion._combined;
    }

    //
    // Equivalence comparison types.
    //

    // Can we write to this data and not have a problem?
    bool isWriteCompatibleWith(const ChunkVersion& otherVersion) const {
        if (!hasEqualEpoch(otherVersion))
            return false;
        return otherVersion._major == _major;
    }

    // Is this the same version?
    bool equals(const ChunkVersion& otherVersion) const {
        if (!hasEqualEpoch(otherVersion))
            return false;
        return otherVersion._combined == _combined;
    }

    /**
     * Returns true if the otherVersion is the same as this version and enforces strict epoch
     * checking (empty epochs are not wildcards).
     */
    bool isStrictlyEqualTo(const ChunkVersion& otherVersion) const {
        if (otherVersion._epoch != _epoch)
            return false;
        return otherVersion._combined == _combined;
    }

    /**
     * Returns true if this version is (strictly) in the same epoch as the other version and
     * this version is older.  Returns false if we're not sure because the epochs are different
     * or if this version is newer.
     */
    bool isOlderThan(const ChunkVersion& otherVersion) const {
        if (otherVersion._epoch != _epoch)
            return false;

        if (_major != otherVersion._major)
            return _major < otherVersion._major;

        return _minor < otherVersion._minor;
    }

    // Is this in the same epoch?
    bool hasEqualEpoch(const ChunkVersion& otherVersion) const {
        return hasEqualEpoch(otherVersion._epoch);
    }

    bool hasEqualEpoch(const OID& otherEpoch) const {
        return _epoch == otherEpoch;
    }

    //
    // BSON input/output
    //
    // The idea here is to make the BSON input style very flexible right now, so we
    // can then tighten it up in the next version.  We can accept either a BSONObject field
    // with version and epoch, or version and epoch in different fields (either is optional).
    // In this case, epoch always is stored in a field name of the version field name + "Epoch"
    //

    //
    // { version : <TS> } and { version : [<TS>,<OID>] } format
    //

    static bool canParseBSON(const BSONElement& el, const std::string& prefix = "") {
        bool canParse;
        fromBSON(el, prefix, &canParse);
        return canParse;
    }

    static ChunkVersion fromBSON(const BSONElement& el, const std::string& prefix = "") {
        bool canParse;
        return fromBSON(el, prefix, &canParse);
    }

    static ChunkVersion fromBSON(const BSONElement& el, const std::string& prefix, bool* canParse) {
        *canParse = true;

        int type = el.type();

        if (type == Array) {
            return fromBSON(BSONArray(el.Obj()), canParse);
        }

        if (type == jstOID) {
            return ChunkVersion(0, 0, el.OID());
        }

        if (type == bsonTimestamp || type == Date) {
            return fromDeprecatedLong(el._numberLong(), OID());
        }

        *canParse = false;

        return ChunkVersion(0, 0, OID());
    }

    //
    // { version : <TS>, versionEpoch : <OID> } object format
    //

    static bool canParseBSON(const BSONObj& obj, const std::string& prefix = "") {
        bool canParse;
        fromBSON(obj, prefix, &canParse);
        return canParse;
    }

    static ChunkVersion fromBSON(const BSONObj& obj, const std::string& prefix = "") {
        bool canParse;
        return fromBSON(obj, prefix, &canParse);
    }

    static ChunkVersion fromBSON(const BSONObj& obj, const std::string& prefixIn, bool* canParse) {
        *canParse = true;

        std::string prefix = prefixIn;
        // "version" doesn't have a "cluster constanst" because that field is never
        // written to the config.
        if (prefixIn == "" && !obj["version"].eoo()) {
            prefix = (std::string) "version";
        }
        // TODO: use ChunkType::DEPRECATED_lastmod()
        // NOTE: type_chunk.h includes this file
        else if (prefixIn == "" && !obj["lastmod"].eoo()) {
            prefix = (std::string) "lastmod";
        }

        ChunkVersion version = fromBSON(obj[prefix], prefixIn, canParse);

        if (obj[prefix + "Epoch"].type() == jstOID) {
            version._epoch = obj[prefix + "Epoch"].OID();
            *canParse = true;
        }

        return version;
    }

    //
    // { version : [<TS>, <OID>] } format
    //

    static bool canParseBSON(const BSONArray& arr) {
        bool canParse;
        fromBSON(arr, &canParse);
        return canParse;
    }

    static ChunkVersion fromBSON(const BSONArray& arr) {
        bool canParse;
        return fromBSON(arr, &canParse);
    }

    static ChunkVersion fromBSON(const BSONArray& arr, bool* canParse) {
        *canParse = false;

        ChunkVersion version;

        BSONObjIterator it(arr);
        if (!it.more())
            return version;

        version = fromBSON(it.next(), "", canParse);
        if (!(*canParse))
            return version;

        *canParse = true;

        if (!it.more())
            return version;
        BSONElement next = it.next();
        if (next.type() != jstOID)
            return version;

        version._epoch = next.OID();

        return version;
    }

    enum VersionChoice { VersionChoice_Local, VersionChoice_Remote, VersionChoice_Unknown };

    /**
     * Compares a remotely-loaded version 'remoteVersion' to the latest local version of a
     * collection, 'localVersion', and returns the newest.
     *
     * Because it isn't clear during epoch changes which epoch is newer, the local version
     * before the reload occurred, 'prevLocalVersion', is used to determine whether the remote
     * epoch is definitely newer, or we're not sure.
     */
    static VersionChoice chooseNewestVersion(ChunkVersion prevLocalVersion,
                                             ChunkVersion localVersion,
                                             ChunkVersion remoteVersion) {
        OID prevEpoch = prevLocalVersion.epoch();
        OID localEpoch = localVersion.epoch();
        OID remoteEpoch = remoteVersion.epoch();

        // Everything changed in-flight, so we need to try again
        if (prevEpoch != localEpoch && localEpoch != remoteEpoch) {
            return VersionChoice_Unknown;
        }

        // We're in the same (zero) epoch as the latest metadata, nothing to do
        if (localEpoch == remoteEpoch && !remoteEpoch.isSet()) {
            return VersionChoice_Local;
        }

        // We're in the same (non-zero) epoch as the latest metadata, so increment the version
        if (localEpoch == remoteEpoch && remoteEpoch.isSet()) {
            // Use the newer version if possible
            if (localVersion < remoteVersion) {
                return VersionChoice_Remote;
            } else {
                return VersionChoice_Local;
            }
        }

        // We're now sure we're installing a new epoch and the epoch didn't change during reload
        dassert(prevEpoch == localEpoch && localEpoch != remoteEpoch);
        return VersionChoice_Remote;
    }

    //
    // Currently our BSON output is to two different fields, to cleanly work with older
    // versions that know nothing about epochs.
    //

    BSONObj toBSONWithPrefix(const std::string& prefixIn) const {
        BSONObjBuilder b;

        std::string prefix = prefixIn;
        if (prefix == "")
            prefix = "version";

        b.appendTimestamp(prefix, _combined);
        b.append(prefix + "Epoch", _epoch);
        return b.obj();
    }

    void addToBSON(BSONObjBuilder& b, const std::string& prefix = "") const {
        b.appendElements(toBSONWithPrefix(prefix));
    }

    void addEpochToBSON(BSONObjBuilder& b, const std::string& prefix = "") const {
        b.append(prefix + "Epoch", _epoch);
    }

    BSONObj toBSON() const {
        // ChunkVersion wants to be an array.
        BSONArrayBuilder b;
        b.appendTimestamp(_combined);
        b.append(_epoch);
        return b.arr();
    }

    bool parseBSON(const BSONObj& source, std::string* errMsg) {
        // ChunkVersion wants to be an array.
        BSONArray arrSource = static_cast<BSONArray>(source);

        bool canParse;
        ChunkVersion version = fromBSON(arrSource, &canParse);
        if (!canParse) {
            *errMsg = "Could not parse version structure";
            return false;
        }

        _minor = version._minor;
        _major = version._major;
        _epoch = version._epoch;
        return true;
    }

    void clear() {
        _minor = 0;
        _major = 0;
        _epoch = OID();
    }

    void cloneTo(ChunkVersion* other) const {
        other->clear();
        other->_minor = _minor;
        other->_major = _major;
        other->_epoch = _epoch;
    }
};

inline std::ostream& operator<<(std::ostream& s, const ChunkVersion& v) {
    s << v.toString();
    return s;
}

}  // namespace mongo