diff --git a/src/mongo/db/commands/write_commands.cpp b/src/mongo/db/commands/write_commands.cpp index 548e267b2f..ab98859272 100644 --- a/src/mongo/db/commands/write_commands.cpp +++ b/src/mongo/db/commands/write_commands.cpp @@ -59,6 +59,8 @@ #include "mongo/db/repl/tenant_migration_conflict_info.h" #include "mongo/db/repl/tenant_migration_decoration.h" #include "mongo/db/retryable_writes_stats.h" +#include "mongo/db/s/operation_sharding_state.h" +#include "mongo/db/s/sharding_state.h" #include "mongo/db/stats/counters.h" #include "mongo/db/storage/duplicate_key_error_info.h" #include "mongo/db/storage/storage_parameters_gen.h" @@ -600,6 +602,9 @@ void transactionChecks(OperationContext* opCtx, const NamespaceString& ns) { !replCoord->isOplogDisabledFor(opCtx, ns)); } +static bool failWithStaleError = false; +static int skipped = 0; + class CmdInsert final : public write_ops::InsertCmdVersion1Gen { public: AllowedOnSecondary secondaryAllowed(ServiceContext*) const final { @@ -644,6 +649,22 @@ public: } write_ops::InsertCommandReply typedRun(OperationContext* opCtx) final try { + if (failWithStaleError && ShardingState::get(opCtx)->canAcceptShardedCommands().isOK()) { + auto sv = OperationShardingState::get(opCtx).getShardVersion(ns()); + if(sv) { + failWithStaleError = false; + StaleConfigInfo sci(ns(), + *sv, + boost::none, + ShardingState::get(opCtx)->shardId()); + uasserted(std::move(sci), "Interminent StaleConfig error"); + } + } else { + skipped++; + if(skipped == 5) { + failWithStaleError = true; + } + } transactionChecks(opCtx, ns()); write_ops::InsertCommandReply insertReply;