summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--jstests/sharding/configsvr_retries_createindex_on_stale_config.js40
-rw-r--r--src/mongo/db/SConscript1
-rw-r--r--src/mongo/db/sessions_collection_config_server.cpp55
-rw-r--r--src/mongo/db/sessions_collection_config_server.h4
4 files changed, 62 insertions, 38 deletions
diff --git a/jstests/sharding/configsvr_retries_createindex_on_stale_config.js b/jstests/sharding/configsvr_retries_createindex_on_stale_config.js
new file mode 100644
index 00000000000..b8d98b4a142
--- /dev/null
+++ b/jstests/sharding/configsvr_retries_createindex_on_stale_config.js
@@ -0,0 +1,40 @@
+/*
+ * Verifies creating the logical sessions collection TTL index retries on stale version errors.
+ */
+
+(function() {
+"use strict";
+
+load('jstests/libs/sessions_collection.js');
+load("jstests/sharding/libs/shard_versioning_util.js");
+
+let st = new ShardingTest({shards: 2});
+
+// Validate the initial state.
+validateSessionsCollection(st.shard0, true, true);
+validateSessionsCollection(st.shard1, false, false);
+validateSessionsCollection(st.configRS.getPrimary(), false, false);
+
+// Drop the TTL index on shard0.
+assert.commandWorked(st.shard0.getDB("config").system.sessions.dropIndex({lastUse: 1}));
+
+// Validate that index has been dropped.
+validateSessionsCollection(st.shard0, true, false);
+validateSessionsCollection(st.shard1, false, false);
+
+// Move the only chunk in the logical sessions collection from shard0 to shard1 with refresh
+// suppressed.
+ShardVersioningUtil.moveChunkNotRefreshRecipient(
+ st.s, "config.system.sessions", st.shard0, st.shard1, {_id: MinKey});
+
+// Refresh session cache.
+assert.commandWorked(
+ st.configRS.getPrimary().getDB("config").runCommand({refreshLogicalSessionCacheNow: 1}));
+
+// Verify that the refresh recreated the index only on the shard that owns the logical sessions
+// collection chunk despite that shard being stale.
+validateSessionsCollection(st.shard0, true, false);
+validateSessionsCollection(st.shard1, true, true);
+
+st.stop();
+})(); \ No newline at end of file
diff --git a/src/mongo/db/SConscript b/src/mongo/db/SConscript
index f93d37a0f6f..4c62a46d924 100644
--- a/src/mongo/db/SConscript
+++ b/src/mongo/db/SConscript
@@ -1335,6 +1335,7 @@ env.Library(
],
LIBDEPS_PRIVATE=[
'$BUILD_DIR/mongo/s/sharding_router_api',
+ '$BUILD_DIR/mongo/db/pipeline/sharded_agg_helpers',
]
)
diff --git a/src/mongo/db/sessions_collection_config_server.cpp b/src/mongo/db/sessions_collection_config_server.cpp
index 5d5f0153c7b..0acc4cec7c2 100644
--- a/src/mongo/db/sessions_collection_config_server.cpp
+++ b/src/mongo/db/sessions_collection_config_server.cpp
@@ -38,6 +38,7 @@
#include "mongo/db/dbdirectclient.h"
#include "mongo/db/logical_session_id.h"
#include "mongo/db/operation_context.h"
+#include "mongo/db/pipeline/sharded_agg_helpers.h"
#include "mongo/logv2/log.h"
#include "mongo/rpc/get_status_from_command_result.h"
#include "mongo/s/client/shard_registry.h"
@@ -79,41 +80,27 @@ void SessionsCollectionConfigServer::_shardCollectionIfNeeded(OperationContext*
void SessionsCollectionConfigServer::_generateIndexesIfNeeded(OperationContext* opCtx) {
const auto nss = NamespaceString::kLogicalSessionsNamespace;
- auto routingInfo =
- uassertStatusOK(Grid::get(opCtx)->catalogCache()->getCollectionRoutingInfo(opCtx, nss));
- try {
- for (int tries = 0;; ++tries) {
- const bool canRetry = tries < kMaxNumStaleVersionRetries - 1;
- try {
- scatterGatherVersionedTargetByRoutingTable(
- opCtx,
- nss.db(),
- nss,
- routingInfo,
- SessionsCollection::generateCreateIndexesCmd(),
- ReadPreferenceSetting(ReadPreference::PrimaryOnly),
- Shard::RetryPolicy::kNoRetry,
- BSONObj() /* query */,
- BSONObj() /* collation */);
- return;
- } catch (const ExceptionForCat<ErrorCategory::StaleShardVersionError>& ex) {
- LOGV2(21980,
- "Attempt {tries} to generate TTL index for {nss} received StaleShardVersion "
- "error{causedBy_ex}",
- "tries"_attr = tries,
- "nss"_attr = nss,
- "causedBy_ex"_attr = causedBy(ex));
- if (canRetry) {
- continue;
- }
- throw;
- }
- }
- } catch (DBException& ex) {
- ex.addContext(str::stream() << "Failed to generate TTL index for " << nss);
- throw;
- }
+ sharded_agg_helpers::shardVersionRetry(
+ opCtx,
+ Grid::get(opCtx)->catalogCache(),
+ nss,
+ "SessionsCollectionConfigServer::_generateIndexesIfNeeded",
+ [&] {
+ auto routingInfo = uassertStatusOK(
+ Grid::get(opCtx)->catalogCache()->getCollectionRoutingInfo(opCtx, nss));
+
+ scatterGatherVersionedTargetByRoutingTable(
+ opCtx,
+ nss.db(),
+ nss,
+ routingInfo,
+ SessionsCollection::generateCreateIndexesCmd(),
+ ReadPreferenceSetting(ReadPreference::PrimaryOnly),
+ Shard::RetryPolicy::kNoRetry,
+ BSONObj() /* query */,
+ BSONObj() /* collation */);
+ });
}
void SessionsCollectionConfigServer::setupSessionsCollection(OperationContext* opCtx) {
diff --git a/src/mongo/db/sessions_collection_config_server.h b/src/mongo/db/sessions_collection_config_server.h
index 0c5bdbb596e..2deeac82549 100644
--- a/src/mongo/db/sessions_collection_config_server.h
+++ b/src/mongo/db/sessions_collection_config_server.h
@@ -29,12 +29,8 @@
#pragma once
-#include <memory>
-
-#include "mongo/db/logical_session_id.h"
#include "mongo/db/sessions_collection_sharded.h"
#include "mongo/platform/mutex.h"
-#include "mongo/util/time_support.h"
namespace mongo {