6363#include " mongo/s/client/shard.h"
6464#include " mongo/s/client/shard_registry.h"
6565#include " mongo/s/grid.h"
66+ #include " mongo/s/request_types/flush_routing_table_cache_updates_gen.h"
6667#include " mongo/s/request_types/set_shard_version_request.h"
6768#include " mongo/s/shard_key_pattern.h"
6869#include " mongo/s/shard_util.h"
@@ -149,6 +150,41 @@ void writeFirstChunksForCollection(OperationContext* opCtx,
149150 }
150151}
151152
153+ void triggerFireAndForgetShardRefreshes (OperationContext* opCtx, const NamespaceString& nss) {
154+ const auto shardRegistry = Grid::get (opCtx)->shardRegistry ();
155+ const auto allShards = uassertStatusOK (Grid::get (opCtx)->catalogClient ()->getAllShards (
156+ opCtx, repl::ReadConcernLevel::kLocalReadConcern ))
157+ .value ;
158+
159+ for (const auto & shardEntry : allShards) {
160+ const auto chunk = uassertStatusOK (shardRegistry->getConfigShard ()->exhaustiveFindOnConfig (
161+ opCtx,
162+ ReadPreferenceSetting{ReadPreference::PrimaryOnly},
163+ repl::ReadConcernLevel::kLocalReadConcern ,
164+ ChunkType::ConfigNS,
165+ BSON (ChunkType::ns (nss.ns ())
166+ << ChunkType::shard (shardEntry.getName ())),
167+ BSONObj (),
168+ 1LL ))
169+ .docs ;
170+
171+ invariant (chunk.size () == 0 || chunk.size () == 1 );
172+
173+ if (chunk.size () == 1 ) {
174+ const auto shard =
175+ uassertStatusOK (shardRegistry->getShard (opCtx, shardEntry.getName ()));
176+
177+ // This is a best-effort attempt to refresh the shard 'shardEntry'. Fire and forget an
178+ // asynchronous '_flushRoutingTableCacheUpdates' request.
179+ shard->runFireAndForgetCommand (
180+ opCtx,
181+ ReadPreferenceSetting{ReadPreference::PrimaryOnly},
182+ NamespaceString::kAdminDb .toString (),
183+ BSON (_flushRoutingTableCacheUpdates::kCommandName << nss.ns ()));
184+ }
185+ }
186+ }
187+
152188} // namespace
153189
154190void checkForExistingChunks (OperationContext* opCtx, const NamespaceString& nss) {
@@ -760,7 +796,8 @@ void ShardingCatalogManager::refineCollectionShardKey(OperationContext* opCtx,
760796
761797 // Update all config.tags entries for the given namespace by setting their bounds for each new
762798 // field in the refined key to MinKey (except for the global max tag where the max bounds are
763- // set to MaxKey).
799+ // set to MaxKey). NOTE: The last update has majority write concern to ensure that all updates
800+ // are majority committed before refreshing each shard.
764801 uassertStatusOK (
765802 catalogClient->updateConfigDocuments (opCtx,
766803 TagsType::ConfigNS,
@@ -769,12 +806,13 @@ void ShardingCatalogManager::refineCollectionShardKey(OperationContext* opCtx,
769806 false , // upsert
770807 ShardingCatalogClient::kLocalWriteConcern ));
771808
772- uassertStatusOK (catalogClient->updateConfigDocument (opCtx,
773- TagsType::ConfigNS,
774- isGlobalMaxQuery,
775- BSON (" $max" << globalMaxBounds),
776- false , // upsert
777- ShardingCatalogClient::kLocalWriteConcern ));
809+ uassertStatusOK (
810+ catalogClient->updateConfigDocument (opCtx,
811+ TagsType::ConfigNS,
812+ isGlobalMaxQuery,
813+ BSON (" $max" << globalMaxBounds),
814+ false , // upsert
815+ ShardingCatalogClient::kMajorityWriteConcern ));
778816
779817 log () << " refineCollectionShardKey: updated zone entries for '" << nss.ns () << " ': took "
780818 << executionTimer.millis () << " ms. Total time taken: " << totalTimer.millis () << " ms." ;
@@ -784,6 +822,17 @@ void ShardingCatalogManager::refineCollectionShardKey(OperationContext* opCtx,
784822 nss.ns (),
785823 BSONObj (),
786824 ShardingCatalogClient::kLocalWriteConcern );
825+
826+ // Trigger refreshes on each shard containing chunks in the namespace 'nss'. Since this isn't
827+ // necessary for correctness, all refreshes are best-effort.
828+ try {
829+ triggerFireAndForgetShardRefreshes (opCtx, nss);
830+ } catch (const DBException& ex) {
831+ log () << ex.toStatus ().withContext (str::stream ()
832+ << " refineCollectionShardKey: failed to best-effort "
833+ " refresh all shards containing chunks in '"
834+ << nss.ns () << " '" );
835+ }
787836}
788837
789838} // namespace mongo
0 commit comments