summaryrefslogtreecommitdiff
path: root/src/mongo/s/chunk_version.h
diff options
context:
space:
mode:
authorKaloian Manassiev <kaloian.manassiev@mongodb.com>2015-08-13 17:04:55 -0400
committerKaloian Manassiev <kaloian.manassiev@mongodb.com>2015-08-14 15:27:44 -0400
commitcec89b290906adb7d36507d20d619aaa5a6f6c16 (patch)
tree7f9bc55b68751f87c78046aa23294e29e7034a1b /src/mongo/s/chunk_version.h
parent914c12f4c7e850cacc6292bc330101579a0627ef (diff)
downloadmongo-cec89b290906adb7d36507d20d619aaa5a6f6c16.tar.gz
SERVER-19855 Include min OpTime with shard version
This change adds the OpTime of the chunk manager as of the time its cached metadata was loaded along with each versioned request sent from MongoS. This includes write commands and the setShardVersion command. The OpTime is only sent as part of this change. There will be a follow-up change to add code to interpret this information on the MongoD side.
Diffstat (limited to 'src/mongo/s/chunk_version.h')
-rw-r--r--src/mongo/s/chunk_version.h178
1 files changed, 112 insertions, 66 deletions
diff --git a/src/mongo/s/chunk_version.h b/src/mongo/s/chunk_version.h
index 02a05f3dd57..7c4b2655412 100644
--- a/src/mongo/s/chunk_version.h
+++ b/src/mongo/s/chunk_version.h
@@ -1,37 +1,42 @@
/**
-* 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.
-*/
+ * Copyright (C) 2012-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 "mongo/db/jsobj.h"
+#include "mongo/db/repl/optime_pair.h"
namespace mongo {
+class BSONObj;
+template <typename T>
+class StatusWith;
+
/**
* ChunkVersions consist of a major/minor version scoped to a version epoch
*
@@ -46,32 +51,38 @@ namespace mongo {
* expected from types.
*/
struct ChunkVersion {
- union {
- struct {
- int _minor;
- int _major;
- };
- unsigned long long _combined;
- };
- OID _epoch;
-
+public:
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) {}
+ /**
+ * Interprets the specified BSON content as the format for commands, which is in the form:
+ * { ..., shardVersion: [ <combined major/minor>, <OID epoch> ], ... }
+ */
+ static StatusWith<ChunkVersion> parseFromBSONForCommands(const BSONObj& obj);
+
+ /**
+ * Interprets the specified BSON content as the format for the setShardVersion command, which
+ * is in the form:
+ * { ..., version: [ <combined major/minor> ], versionEpoch: [ <OID epoch> ], ... }
+ */
+ static StatusWith<ChunkVersion> parseFromBSONForSetShardVersion(const BSONObj& obj);
+
+ /**
+ * Indicates a dropped collection. All components are zeroes (OID is zero time, zero
+ * machineId/inc).
+ */
static ChunkVersion DROPPED() {
- return ChunkVersion(0, 0, OID()); // dropped OID is zero time, zero machineId/inc
+ return ChunkVersion(0, 0, OID());
}
+ /**
+ * Indicates that the collection is not sharded. Same as DROPPED.
+ */
static ChunkVersion UNSHARDED() {
- // TODO: Distinguish between these cases
- return DROPPED();
+ return ChunkVersion(0, 0, OID());
}
static ChunkVersion IGNORED() {
@@ -369,35 +380,23 @@ struct ChunkVersion {
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;
- }
+private:
+ union {
+ struct {
+ int _minor;
+ int _major;
+ };
+
+ uint64_t _combined;
+ };
+
+ OID _epoch;
};
inline std::ostream& operator<<(std::ostream& s, const ChunkVersion& v) {
@@ -405,4 +404,51 @@ inline std::ostream& operator<<(std::ostream& s, const ChunkVersion& v) {
return s;
}
+
+/**
+ * Represents a chunk version along with the optime from when it was retrieved. Provides logic to
+ * serialize and deserialize the combo to BSON.
+ */
+class ChunkVersionAndOpTime {
+public:
+ ChunkVersionAndOpTime(ChunkVersion chunkVersion);
+ ChunkVersionAndOpTime(ChunkVersion chunkVersion, repl::OpTime ts);
+
+ const ChunkVersion& getVersion() const {
+ return _verAndOpT.value;
+ }
+
+ const repl::OpTime& getOpTime() const {
+ return _verAndOpT.opTime;
+ }
+
+ /**
+ * Interprets the contents of the BSON documents as having been constructed in the format for
+ * write commands. The optime component is optional for backwards compatibility and if not
+ * present, the optime will be default initialized.
+ */
+ static StatusWith<ChunkVersionAndOpTime> parseFromBSONForCommands(const BSONObj& obj);
+
+ /**
+ * Interprets the contents of the BSON document as having been constructed in the format for the
+ * setShardVersion command. The optime component is optional for backwards compatibility and if
+ * not present, the optime will be default initialized.
+ */
+ static StatusWith<ChunkVersionAndOpTime> parseFromBSONForSetShardVersion(const BSONObj& obj);
+
+ /**
+ * Appends the contents to the specified builder in the format expected by the setShardVersion
+ * command.
+ */
+ void appendForSetShardVersion(BSONObjBuilder* builder) const;
+
+ /**
+ * Appends the contents to the specified builder in the format expected by the write commands.
+ */
+ void appendForCommands(BSONObjBuilder* builder) const;
+
+private:
+ repl::OpTimePair<ChunkVersion> _verAndOpT;
+};
+
} // namespace mongo