63
63
#include " mongo/s/client/shard.h"
64
64
#include " mongo/s/client/shard_registry.h"
65
65
#include " mongo/s/grid.h"
66
+ #include " mongo/s/request_types/flush_routing_table_cache_updates_gen.h"
66
67
#include " mongo/s/request_types/set_shard_version_request.h"
67
68
#include " mongo/s/shard_key_pattern.h"
68
69
#include " mongo/s/shard_util.h"
@@ -149,6 +150,41 @@ void writeFirstChunksForCollection(OperationContext* opCtx,
149
150
}
150
151
}
151
152
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
+
152
188
} // namespace
153
189
154
190
void checkForExistingChunks (OperationContext* opCtx, const NamespaceString& nss) {
@@ -760,7 +796,8 @@ void ShardingCatalogManager::refineCollectionShardKey(OperationContext* opCtx,
760
796
761
797
// Update all config.tags entries for the given namespace by setting their bounds for each new
762
798
// 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.
764
801
uassertStatusOK (
765
802
catalogClient->updateConfigDocuments (opCtx,
766
803
TagsType::ConfigNS,
@@ -769,12 +806,13 @@ void ShardingCatalogManager::refineCollectionShardKey(OperationContext* opCtx,
769
806
false , // upsert
770
807
ShardingCatalogClient::kLocalWriteConcern ));
771
808
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 ));
778
816
779
817
log () << " refineCollectionShardKey: updated zone entries for '" << nss.ns () << " ': took "
780
818
<< executionTimer.millis () << " ms. Total time taken: " << totalTimer.millis () << " ms." ;
@@ -784,6 +822,17 @@ void ShardingCatalogManager::refineCollectionShardKey(OperationContext* opCtx,
784
822
nss.ns (),
785
823
BSONObj (),
786
824
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
+ }
787
836
}
788
837
789
838
} // namespace mongo
0 commit comments