summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMarcos José Grillo Ramírez <marcos.grillo@10gen.com>2019-10-24 15:29:44 +0000
committerevergreen <evergreen@mongodb.com>2019-10-24 15:29:44 +0000
commit4003ef1b40e4404fc42d8a13db7e3a12946e0832 (patch)
treec0997b9a30a40ac32d5a56724fb975c34c608a39
parentd7469a75b1643157994a44134c9fdb061cca354a (diff)
downloadmongo-4003ef1b40e4404fc42d8a13db7e3a12946e0832.tar.gz
SERVER-42508 Convert SessionsCollection to throw instead of return status Part One: setupSessionsCollection
-rw-r--r--src/mongo/db/logical_session_cache_impl.cpp12
-rw-r--r--src/mongo/db/sessions_collection.h2
-rw-r--r--src/mongo/db/sessions_collection_config_server.cpp55
-rw-r--r--src/mongo/db/sessions_collection_config_server.h6
-rw-r--r--src/mongo/db/sessions_collection_mock.h4
-rw-r--r--src/mongo/db/sessions_collection_rs.cpp59
-rw-r--r--src/mongo/db/sessions_collection_rs.h2
-rw-r--r--src/mongo/db/sessions_collection_sharded.cpp5
-rw-r--r--src/mongo/db/sessions_collection_sharded.h2
-rw-r--r--src/mongo/db/sessions_collection_standalone.cpp10
-rw-r--r--src/mongo/db/sessions_collection_standalone.h2
11 files changed, 76 insertions, 83 deletions
diff --git a/src/mongo/db/logical_session_cache_impl.cpp b/src/mongo/db/logical_session_cache_impl.cpp
index 61debaf9644..83671cdd039 100644
--- a/src/mongo/db/logical_session_cache_impl.cpp
+++ b/src/mongo/db/logical_session_cache_impl.cpp
@@ -124,7 +124,8 @@ void LogicalSessionCacheImpl::_periodicRefresh(Client* client) {
try {
_refresh(client);
} catch (...) {
- log() << "Failed to refresh session cache: " << exceptionToStatus();
+ log() << "Failed to refresh session cache: " << exceptionToStatus()
+ << ", will try again at the next refresh interval";
}
}
@@ -241,11 +242,12 @@ void LogicalSessionCacheImpl::_refresh(Client* client) {
ON_BLOCK_EXIT([&opCtx] { clearShardingOperationFailedStatus(opCtx); });
- auto setupStatus = _sessionsColl->setupSessionsCollection(opCtx);
+ try {
+ _sessionsColl->setupSessionsCollection(opCtx);
- if (!setupStatus.isOK()) {
- log() << "Sessions collection is not set up; "
- << "waiting until next sessions refresh interval: " << setupStatus.reason();
+ } catch (DBException& ex) {
+ log() << "Failed to refresh session cache: " << ex.reason()
+ << ", will try again at the next refresh interval";
return;
}
diff --git a/src/mongo/db/sessions_collection.h b/src/mongo/db/sessions_collection.h
index 7eeaaf0dada..a632e5094d5 100644
--- a/src/mongo/db/sessions_collection.h
+++ b/src/mongo/db/sessions_collection.h
@@ -53,7 +53,7 @@ public:
/**
* Ensures that the sessions collection exists and has the proper indexes.
*/
- virtual Status setupSessionsCollection(OperationContext* opCtx) = 0;
+ virtual void setupSessionsCollection(OperationContext* opCtx) = 0;
/**
* Checks if the sessions collection exists and has the proper indexes.
diff --git a/src/mongo/db/sessions_collection_config_server.cpp b/src/mongo/db/sessions_collection_config_server.cpp
index 6e4e8fbc29b..30431007ba7 100644
--- a/src/mongo/db/sessions_collection_config_server.cpp
+++ b/src/mongo/db/sessions_collection_config_server.cpp
@@ -49,18 +49,18 @@ namespace mongo {
// Returns an error if the collection didn't exist and we couldn't
// shard it into existence, either.
-Status SessionsCollectionConfigServer::_shardCollectionIfNeeded(OperationContext* opCtx) {
+void SessionsCollectionConfigServer::_shardCollectionIfNeeded(OperationContext* opCtx) {
// First, check if the collection is already sharded.
auto res = _checkCacheForSessionsCollection(opCtx);
if (res.isOK()) {
- return res;
+ return;
}
// If we don't have any shards, we can't set up this collection yet.
- if (Grid::get(opCtx)->shardRegistry()->getNumShards() == 0) {
- return {ErrorCodes::ShardNotFound,
- "Cannot create config.system.sessions until there are shards"};
- }
+ uassert(ErrorCodes::ShardNotFound,
+ str::stream() << "Failed to create " << NamespaceString::kLogicalSessionsNamespace
+ << ": cannot create the collection until there are shards",
+ Grid::get(opCtx)->shardRegistry()->getNumShards() != 0);
// First, shard the sessions collection to create it.
ConfigsvrShardCollectionRequest shardCollection;
@@ -71,48 +71,37 @@ Status SessionsCollectionConfigServer::_shardCollectionIfNeeded(OperationContext
BSONObj info;
if (!client.runCommand(
"admin", CommandHelpers::appendMajorityWriteConcern(shardCollection.toBSON()), info)) {
- return getStatusFromCommandResult(info);
+ uassertStatusOKWithContext(getStatusFromCommandResult(info),
+ str::stream() << "Failed to create "
+ << NamespaceString::kLogicalSessionsNamespace);
}
-
- return Status::OK();
}
-Status SessionsCollectionConfigServer::_generateIndexesIfNeeded(OperationContext* opCtx) {
+void SessionsCollectionConfigServer::_generateIndexesIfNeeded(OperationContext* opCtx) {
try {
dispatchCommandAssertCollectionExistsOnAtLeastOneShard(
opCtx,
NamespaceString::kLogicalSessionsNamespace,
SessionsCollection::generateCreateIndexesCmd());
- return Status::OK();
- } catch (const DBException& ex) {
- return ex.toStatus();
+ } catch (DBException& ex) {
+ ex.addContext(str::stream()
+ << "Failed to generate TTL index for "
+ << NamespaceString::kLogicalSessionsNamespace << " on all shards");
+ throw;
}
}
-Status SessionsCollectionConfigServer::setupSessionsCollection(OperationContext* opCtx) {
+void SessionsCollectionConfigServer::setupSessionsCollection(OperationContext* opCtx) {
// If the sharding state is not yet initialized, fail.
- if (!Grid::get(opCtx)->isShardingInitialized()) {
- return {ErrorCodes::ShardingStateNotInitialized, "sharding state is not yet initialized"};
- }
+ uassert(ErrorCodes::ShardingStateNotInitialized,
+ "sharding state is not yet initialized",
+ Grid::get(opCtx)->isShardingInitialized());
stdx::lock_guard<Latch> lk(_mutex);
- {
- auto res = _shardCollectionIfNeeded(opCtx);
- if (!res.isOK()) {
- log() << "Failed to create config.system.sessions: " << res.reason()
- << ", will try again at the next refresh interval";
- return res;
- }
-
- res = _generateIndexesIfNeeded(opCtx);
- if (!res.isOK()) {
- log() << "Failed to generate TTL index for config.system.sessions on all shards: "
- << res.reason() << ", will try again on the next refresh interval";
- }
-
- return res;
- }
+
+ _shardCollectionIfNeeded(opCtx);
+ _generateIndexesIfNeeded(opCtx);
}
Status SessionsCollectionConfigServer::checkSessionsCollectionExists(OperationContext* opCtx) {
diff --git a/src/mongo/db/sessions_collection_config_server.h b/src/mongo/db/sessions_collection_config_server.h
index 701d055772a..d7ee8d3fb63 100644
--- a/src/mongo/db/sessions_collection_config_server.h
+++ b/src/mongo/db/sessions_collection_config_server.h
@@ -53,7 +53,7 @@ public:
*
* If there are no shards in this cluster, this method will do nothing.
*/
- Status setupSessionsCollection(OperationContext* opCtx) override;
+ void setupSessionsCollection(OperationContext* opCtx) override;
/**
* Checks if the sessions collection exists.
@@ -61,8 +61,8 @@ public:
Status checkSessionsCollectionExists(OperationContext* opCtx) override;
private:
- Status _shardCollectionIfNeeded(OperationContext* opCtx);
- Status _generateIndexesIfNeeded(OperationContext* opCtx);
+ void _shardCollectionIfNeeded(OperationContext* opCtx);
+ void _generateIndexesIfNeeded(OperationContext* opCtx);
Mutex _mutex = MONGO_MAKE_LATCH("SessionsCollectionConfigServer::_mutex");
};
diff --git a/src/mongo/db/sessions_collection_mock.h b/src/mongo/db/sessions_collection_mock.h
index da6477692fa..e4352249d48 100644
--- a/src/mongo/db/sessions_collection_mock.h
+++ b/src/mongo/db/sessions_collection_mock.h
@@ -106,9 +106,7 @@ public:
explicit MockSessionsCollection(std::shared_ptr<MockSessionsCollectionImpl> impl)
: _impl(std::move(impl)) {}
- Status setupSessionsCollection(OperationContext* opCtx) override {
- return Status::OK();
- }
+ void setupSessionsCollection(OperationContext* opCtx) override {}
Status checkSessionsCollectionExists(OperationContext* opCtx) override {
return Status::OK();
diff --git a/src/mongo/db/sessions_collection_rs.cpp b/src/mongo/db/sessions_collection_rs.cpp
index 316b95b53de..281f80c4c1e 100644
--- a/src/mongo/db/sessions_collection_rs.cpp
+++ b/src/mongo/db/sessions_collection_rs.cpp
@@ -47,6 +47,7 @@
#include "mongo/db/repl/repl_set_config.h"
#include "mongo/db/repl/replication_coordinator.h"
#include "mongo/rpc/get_status_from_command_result.h"
+#include "mongo/util/assert_util.h"
namespace mongo {
@@ -120,35 +121,37 @@ auto SessionsCollectionRS::_dispatch(const NamespaceString& ns,
}
}
-Status SessionsCollectionRS::setupSessionsCollection(OperationContext* opCtx) {
- return _dispatch(
- NamespaceString::kLogicalSessionsNamespace,
- opCtx,
- [&] {
- auto existsStatus = checkSessionsCollectionExists(opCtx);
- if (existsStatus.isOK()) {
- return Status::OK();
- }
+void SessionsCollectionRS::setupSessionsCollection(OperationContext* opCtx) {
+ uassertStatusOKWithContext(
+ _dispatch(
+ NamespaceString::kLogicalSessionsNamespace,
+ opCtx,
+ [&] {
+ auto existsStatus = checkSessionsCollectionExists(opCtx);
+ if (existsStatus.isOK()) {
+ return Status::OK();
+ }
+
+ DBDirectClient client(opCtx);
+ BSONObj cmd;
+
+ if (existsStatus.code() == ErrorCodes::IndexOptionsConflict) {
+ cmd = generateCollModCmd();
+ } else {
+ // Creating the TTL index will auto-generate the collection.
+ cmd = generateCreateIndexesCmd();
+ }
+
+ BSONObj info;
+ if (!client.runCommand(
+ NamespaceString::kLogicalSessionsNamespace.db().toString(), cmd, info)) {
+ return getStatusFromCommandResult(info);
+ }
- DBDirectClient client(opCtx);
- BSONObj cmd;
-
- if (existsStatus.code() == ErrorCodes::IndexOptionsConflict) {
- cmd = generateCollModCmd();
- } else {
- // Creating the TTL index will auto-generate the collection.
- cmd = generateCreateIndexesCmd();
- }
-
- BSONObj info;
- if (!client.runCommand(
- NamespaceString::kLogicalSessionsNamespace.db().toString(), cmd, info)) {
- return getStatusFromCommandResult(info);
- }
-
- return Status::OK();
- },
- [&](DBClientBase*) { return checkSessionsCollectionExists(opCtx); });
+ return Status::OK();
+ },
+ [&](DBClientBase*) { return checkSessionsCollectionExists(opCtx); }),
+ str::stream() << "Failed to create " << NamespaceString::kLogicalSessionsNamespace);
}
Status SessionsCollectionRS::checkSessionsCollectionExists(OperationContext* opCtx) {
diff --git a/src/mongo/db/sessions_collection_rs.h b/src/mongo/db/sessions_collection_rs.h
index 0d52d3f52e4..93971948e2e 100644
--- a/src/mongo/db/sessions_collection_rs.h
+++ b/src/mongo/db/sessions_collection_rs.h
@@ -52,7 +52,7 @@ public:
/**
* Ensures that the sessions collection exists and has the proper indexes.
*/
- Status setupSessionsCollection(OperationContext* opCtx) override;
+ void setupSessionsCollection(OperationContext* opCtx) override;
/**
* Checks if the sessions collection exists and has the proper indexes.
diff --git a/src/mongo/db/sessions_collection_sharded.cpp b/src/mongo/db/sessions_collection_sharded.cpp
index e65ddd0dab5..fad42e13abb 100644
--- a/src/mongo/db/sessions_collection_sharded.cpp
+++ b/src/mongo/db/sessions_collection_sharded.cpp
@@ -48,6 +48,7 @@
#include "mongo/s/write_ops/batched_command_request.h"
#include "mongo/s/write_ops/batched_command_response.h"
#include "mongo/s/write_ops/cluster_write.h"
+#include "mongo/util/assert_util.h"
namespace mongo {
@@ -134,8 +135,8 @@ std::vector<LogicalSessionRecord> SessionsCollectionSharded::_groupSessionRecord
return sessionRecordsGroupedByShard;
}
-Status SessionsCollectionSharded::setupSessionsCollection(OperationContext* opCtx) {
- return checkSessionsCollectionExists(opCtx);
+void SessionsCollectionSharded::setupSessionsCollection(OperationContext* opCtx) {
+ uassertStatusOK(checkSessionsCollectionExists(opCtx));
}
Status SessionsCollectionSharded::checkSessionsCollectionExists(OperationContext* opCtx) {
diff --git a/src/mongo/db/sessions_collection_sharded.h b/src/mongo/db/sessions_collection_sharded.h
index 95aba591cb7..bef89cc368e 100644
--- a/src/mongo/db/sessions_collection_sharded.h
+++ b/src/mongo/db/sessions_collection_sharded.h
@@ -48,7 +48,7 @@ public:
* Ensures that the sessions collection exists, is sharded,
* and has the proper indexes.
*/
- Status setupSessionsCollection(OperationContext* opCtx) override;
+ void setupSessionsCollection(OperationContext* opCtx) override;
/**
* Checks if the sessions collection exists. Does not check if the index exists in the sharded
diff --git a/src/mongo/db/sessions_collection_standalone.cpp b/src/mongo/db/sessions_collection_standalone.cpp
index 2b1f815decc..c994d7694ad 100644
--- a/src/mongo/db/sessions_collection_standalone.cpp
+++ b/src/mongo/db/sessions_collection_standalone.cpp
@@ -45,10 +45,10 @@ BSONObj lsidQuery(const LogicalSessionId& lsid) {
}
} // namespace
-Status SessionsCollectionStandalone::setupSessionsCollection(OperationContext* opCtx) {
+void SessionsCollectionStandalone::setupSessionsCollection(OperationContext* opCtx) {
auto existsStatus = checkSessionsCollectionExists(opCtx);
if (existsStatus.isOK()) {
- return Status::OK();
+ return;
}
DBDirectClient client(opCtx);
@@ -62,10 +62,10 @@ Status SessionsCollectionStandalone::setupSessionsCollection(OperationContext* o
BSONObj info;
if (!client.runCommand(NamespaceString::kLogicalSessionsNamespace.db().toString(), cmd, info)) {
- return getStatusFromCommandResult(info);
+ uassertStatusOKWithContext(getStatusFromCommandResult(info),
+ str::stream() << "Failed to create "
+ << NamespaceString::kLogicalSessionsNamespace);
}
-
- return Status::OK();
}
Status SessionsCollectionStandalone::checkSessionsCollectionExists(OperationContext* opCtx) {
diff --git a/src/mongo/db/sessions_collection_standalone.h b/src/mongo/db/sessions_collection_standalone.h
index e73f5aad10e..04e7eea9ed6 100644
--- a/src/mongo/db/sessions_collection_standalone.h
+++ b/src/mongo/db/sessions_collection_standalone.h
@@ -46,7 +46,7 @@ public:
/**
* Ensures that the sessions collection exists and has the proper indexes.
*/
- Status setupSessionsCollection(OperationContext* opCtx) override;
+ void setupSessionsCollection(OperationContext* opCtx) override;
/**
* Checks if the sessions collection exists and has the proper indexes.