summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorKaitlin Mahar <kaitlin.mahar@mongodb.com>2018-01-24 17:07:06 -0500
committerKaitlin Mahar <kaitlin.mahar@mongodb.com>2018-01-28 19:11:24 -0500
commit9a235bd7703b6c5c0b194c8456e192bf4734a8d0 (patch)
tree907fa88e099727e84210fcb391d0a7ada470555c /src
parentd887ee0da56d4f099e3bec9f1079467ec465850c (diff)
downloadmongo-9a235bd7703b6c5c0b194c8456e192bf4734a8d0.tar.gz
SERVER-32607 Implement a DatabaseVersion type class and add it to the DatabaseType
Diffstat (limited to 'src')
-rw-r--r--src/mongo/s/SConscript2
-rw-r--r--src/mongo/s/catalog/sharding_catalog_test.cpp4
-rw-r--r--src/mongo/s/catalog/type_database.cpp16
-rw-r--r--src/mongo/s/catalog/type_database.h13
-rw-r--r--src/mongo/s/catalog/type_database_test.cpp12
-rw-r--r--src/mongo/s/database_version.idl33
-rw-r--r--src/mongo/s/versioning.cpp47
-rw-r--r--src/mongo/s/versioning.h39
-rw-r--r--src/mongo/util/uuid.h1
9 files changed, 163 insertions, 4 deletions
diff --git a/src/mongo/s/SConscript b/src/mongo/s/SConscript
index ec341c87500..2c4b0bee9f8 100644
--- a/src/mongo/s/SConscript
+++ b/src/mongo/s/SConscript
@@ -91,6 +91,8 @@ env.Library(
'move_chunk_request.cpp',
'set_shard_version_request.cpp',
'shard_key_pattern.cpp',
+ env.Idlc('database_version.idl')[0],
+ 'versioning.cpp',
],
LIBDEPS=[
'shard_id',
diff --git a/src/mongo/s/catalog/sharding_catalog_test.cpp b/src/mongo/s/catalog/sharding_catalog_test.cpp
index 636838dd11e..c460b66c93a 100644
--- a/src/mongo/s/catalog/sharding_catalog_test.cpp
+++ b/src/mongo/s/catalog/sharding_catalog_test.cpp
@@ -53,6 +53,7 @@
#include "mongo/s/catalog/type_tags.h"
#include "mongo/s/chunk_version.h"
#include "mongo/s/client/shard_registry.h"
+#include "mongo/s/versioning.h"
#include "mongo/s/write_ops/batched_command_response.h"
#include "mongo/stdx/future.h"
#include "mongo/util/log.h"
@@ -161,6 +162,7 @@ TEST_F(ShardingCatalogClientTest, GetDatabaseExisting) {
expectedDb.setName("bigdata");
expectedDb.setPrimary(ShardId("shard0000"));
expectedDb.setSharded(true);
+ expectedDb.setVersion(Versioning::newDatabaseVersion());
const OpTime newOpTime(Timestamp(7, 6), 5);
@@ -208,6 +210,7 @@ TEST_F(ShardingCatalogClientTest, GetDatabaseStaleSecondaryRetrySuccess) {
expectedDb.setName("bigdata");
expectedDb.setPrimary(ShardId("shard0000"));
expectedDb.setSharded(true);
+ expectedDb.setVersion(Versioning::newDatabaseVersion());
auto future = launchAsync([this, &expectedDb] {
return assertGet(
@@ -1096,6 +1099,7 @@ TEST_F(ShardingCatalogClientTest, UpdateDatabase) {
dbt.setName("test");
dbt.setPrimary(ShardId("shard0000"));
dbt.setSharded(true);
+ dbt.setVersion(Versioning::newDatabaseVersion());
auto future = launchAsync([this, dbt] {
auto status = catalogClient()->updateDatabase(operationContext(), dbt.getName(), dbt);
diff --git a/src/mongo/s/catalog/type_database.cpp b/src/mongo/s/catalog/type_database.cpp
index 9ccf117ea87..492077a9337 100644
--- a/src/mongo/s/catalog/type_database.cpp
+++ b/src/mongo/s/catalog/type_database.cpp
@@ -34,6 +34,7 @@
#include "mongo/bson/bsonobj.h"
#include "mongo/bson/bsonobjbuilder.h"
#include "mongo/bson/util/bson_extract.h"
+#include "mongo/s/versioning.h"
#include "mongo/util/assert_util.h"
namespace mongo {
@@ -45,6 +46,7 @@ const std::string DatabaseType::ConfigNS = "config.databases";
const BSONField<std::string> DatabaseType::name("_id");
const BSONField<std::string> DatabaseType::primary("primary");
const BSONField<bool> DatabaseType::sharded("partitioned");
+const BSONField<BSONObj> DatabaseType::version("version");
StatusWith<DatabaseType> DatabaseType::fromBSON(const BSONObj& source) {
@@ -78,6 +80,15 @@ StatusWith<DatabaseType> DatabaseType::fromBSON(const BSONObj& source) {
dbt._sharded = dbtSharded;
}
+ {
+ BSONObj versionField = source.getObjectField("version");
+ // TODO: parse this unconditionally after v4.0.
+ if (!versionField.isEmpty()) {
+ dbt._version =
+ DatabaseVersion::parse(IDLParserErrorContext("DatabaseType"), versionField);
+ }
+ }
+
return StatusWith<DatabaseType>(dbt);
}
@@ -103,6 +114,11 @@ BSONObj DatabaseType::toBSON() const {
builder.append(primary.name(), _primary.get_value_or(ShardId()).toString());
builder.append(sharded.name(), _sharded.get_value_or(false));
+ BSONObjBuilder versionBuilder;
+ DatabaseVersion defaultDbv;
+ _version.get_value_or(Versioning::newDatabaseVersion()).serialize(&versionBuilder);
+ builder.append(version.name(), versionBuilder.obj());
+
return builder.obj();
}
diff --git a/src/mongo/s/catalog/type_database.h b/src/mongo/s/catalog/type_database.h
index c1948f1132c..9525263a151 100644
--- a/src/mongo/s/catalog/type_database.h
+++ b/src/mongo/s/catalog/type_database.h
@@ -32,6 +32,7 @@
#include <string>
#include "mongo/db/jsobj.h"
+#include "mongo/s/database_version_gen.h"
#include "mongo/s/shard_id.h"
namespace mongo {
@@ -55,7 +56,7 @@ public:
static const BSONField<std::string> name;
static const BSONField<std::string> primary;
static const BSONField<bool> sharded;
-
+ static const BSONField<BSONObj> version;
/**
* Constructs a new DatabaseType object from BSON. Also does validation of the contents.
@@ -95,6 +96,14 @@ public:
_sharded = sharded;
}
+ DatabaseVersion getVersion() const {
+ return _version.get();
+ }
+
+ void setVersion(DatabaseVersion version) {
+ _version = std::move(version);
+ }
+
private:
// Requred database name
boost::optional<std::string> _name;
@@ -106,6 +115,8 @@ private:
// Required whether sharding is enabled for this database. Even though this field is of
// type optional, it is only used as an indicator that the value was explicitly set.
boost::optional<bool> _sharded;
+
+ boost::optional<DatabaseVersion> _version;
};
} // namespace mongo
diff --git a/src/mongo/s/catalog/type_database_test.cpp b/src/mongo/s/catalog/type_database_test.cpp
index f038ee65bb4..08f13f2e576 100644
--- a/src/mongo/s/catalog/type_database_test.cpp
+++ b/src/mongo/s/catalog/type_database_test.cpp
@@ -32,6 +32,7 @@
#include "mongo/db/jsobj.h"
#include "mongo/s/catalog/type_database.h"
#include "mongo/unittest/unittest.h"
+#include "mongo/util/uuid.h"
namespace {
@@ -44,15 +45,20 @@ TEST(DatabaseType, Empty) {
}
TEST(DatabaseType, Basic) {
- StatusWith<DatabaseType> status =
- DatabaseType::fromBSON(BSON(DatabaseType::name("mydb") << DatabaseType::primary("shard")
- << DatabaseType::sharded(true)));
+ UUID uuid = UUID::gen();
+ StatusWith<DatabaseType> status = DatabaseType::fromBSON(
+ BSON(DatabaseType::name("mydb")
+ << DatabaseType::primary("shard")
+ << DatabaseType::sharded(true)
+ << DatabaseType::version(BSON("uuid" << uuid << "version" << 0))));
ASSERT_TRUE(status.isOK());
DatabaseType db = status.getValue();
ASSERT_EQUALS(db.getName(), "mydb");
ASSERT_EQUALS(db.getPrimary(), "shard");
ASSERT_TRUE(db.getSharded());
+ ASSERT_EQUALS(db.getVersion().getUuid(), uuid);
+ ASSERT_EQUALS(db.getVersion().getVersion(), 0);
}
TEST(DatabaseType, BadType) {
diff --git a/src/mongo/s/database_version.idl b/src/mongo/s/database_version.idl
new file mode 100644
index 00000000000..3875144b6bb
--- /dev/null
+++ b/src/mongo/s/database_version.idl
@@ -0,0 +1,33 @@
+# Copyright (C) 2017 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/>.
+#
+
+# DatabaseVersion type
+
+global:
+ cpp_namespace: "mongo"
+
+imports:
+ - "mongo/idl/basic_types.idl"
+
+structs:
+ DatabaseVersion:
+ description: "A type to represent DatabaseVersions"
+ fields:
+ uuid:
+ type: uuid
+ description: "UUID, indicating the incarnation of the database"
+ version:
+ type: int
+ description: "Int32, which is bumped whenever movePrimary is called"
diff --git a/src/mongo/s/versioning.cpp b/src/mongo/s/versioning.cpp
new file mode 100644
index 00000000000..52dbc2cfa80
--- /dev/null
+++ b/src/mongo/s/versioning.cpp
@@ -0,0 +1,47 @@
+/**
+ * Copyright (C) 2018 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.
+ */
+
+#include "mongo/s/versioning.h"
+#include "mongo/s/database_version_gen.h"
+
+namespace mongo {
+
+DatabaseVersion Versioning::increment(const DatabaseVersion& v) {
+ DatabaseVersion dbv;
+ dbv.setVersion(v.getVersion() + 1);
+ dbv.setUuid(v.getUuid());
+ return dbv;
+}
+
+DatabaseVersion Versioning::newDatabaseVersion() {
+ DatabaseVersion dbv;
+ dbv.setVersion(0);
+ dbv.setUuid(UUID::gen());
+ return dbv;
+}
+}
diff --git a/src/mongo/s/versioning.h b/src/mongo/s/versioning.h
new file mode 100644
index 00000000000..ebd7b8a3760
--- /dev/null
+++ b/src/mongo/s/versioning.h
@@ -0,0 +1,39 @@
+/**
+ * Copyright (C) 2018 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.
+ */
+
+#include "mongo/s/database_version_gen.h"
+
+namespace mongo {
+
+class Versioning {
+public:
+ static DatabaseVersion increment(const DatabaseVersion& v);
+
+ static DatabaseVersion newDatabaseVersion();
+};
+}
diff --git a/src/mongo/util/uuid.h b/src/mongo/util/uuid.h
index 3eedf7db04f..bc0db1257d0 100644
--- a/src/mongo/util/uuid.h
+++ b/src/mongo/util/uuid.h
@@ -60,6 +60,7 @@ class UUID {
// Make the IDL generated parser a friend
friend class ConfigsvrShardCollectionResponse;
+ friend class DatabaseVersion;
friend class DbCheckOplogCollection;
friend class idl::import::One_UUID;
friend class LogicalSessionId;