Skip to content

Commit c1db1e0

Browse files
committed
SERVER-41160 Add shutdown method for CatalogCacheLoader
1 parent d4843fc commit c1db1e0

12 files changed

+82
-13
lines changed

src/mongo/db/db.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,10 @@ void shutdownTask(const ShutdownTaskArgs& shutdownArgs) {
978978
validator->shutDown();
979979
}
980980

981+
if (ShardingState::get(serviceContext)->enabled()) {
982+
CatalogCacheLoader::get(serviceContext).shutDown();
983+
}
984+
981985
#if __has_feature(address_sanitizer)
982986
// When running under address sanitizer, we get false positive leaks due to disorder around
983987
// the lifecycle of a connection and request. When we are running under ASAN, we try a lot

src/mongo/db/s/catalog_cache_loader_mock.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ void CatalogCacheLoaderMock::onStepUp() {
8282
MONGO_UNREACHABLE;
8383
}
8484

85+
void CatalogCacheLoaderMock::shutDown() {}
86+
8587
void CatalogCacheLoaderMock::notifyOfCollectionVersionUpdate(const NamespaceString& nss) {
8688
MONGO_UNREACHABLE;
8789
}

src/mongo/db/s/catalog_cache_loader_mock.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class CatalogCacheLoaderMock final : public CatalogCacheLoader {
5252
void initializeReplicaSetRole(bool isPrimary) override;
5353
void onStepDown() override;
5454
void onStepUp() override;
55+
void shutDown() override;
5556
void notifyOfCollectionVersionUpdate(const NamespaceString& nss) override;
5657
void waitForCollectionFlush(OperationContext* opCtx, const NamespaceString& nss) override;
5758
void waitForDatabaseFlush(OperationContext* opCtx, StringData dbName) override;

src/mongo/db/s/read_only_catalog_cache_loader.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ namespace mongo {
3535

3636
using CollectionAndChangedChunks = CatalogCacheLoader::CollectionAndChangedChunks;
3737

38+
ReadOnlyCatalogCacheLoader::~ReadOnlyCatalogCacheLoader() {
39+
shutDown();
40+
}
41+
3842
void ReadOnlyCatalogCacheLoader::waitForCollectionFlush(OperationContext* opCtx,
3943
const NamespaceString& nss) {
4044
MONGO_UNREACHABLE;
@@ -44,6 +48,10 @@ void ReadOnlyCatalogCacheLoader::waitForDatabaseFlush(OperationContext* opCtx, S
4448
MONGO_UNREACHABLE;
4549
}
4650

51+
void ReadOnlyCatalogCacheLoader::shutDown() {
52+
_configServerLoader.shutDown();
53+
}
54+
4755
std::shared_ptr<Notification<void>> ReadOnlyCatalogCacheLoader::getChunksSince(
4856
const NamespaceString& nss, ChunkVersion version, GetChunksSinceCallbackFn callbackFn) {
4957
return _configServerLoader.getChunksSince(nss, version, callbackFn);

src/mongo/db/s/read_only_catalog_cache_loader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,12 @@ namespace mongo {
4040
*/
4141
class ReadOnlyCatalogCacheLoader final : public CatalogCacheLoader {
4242
public:
43+
~ReadOnlyCatalogCacheLoader();
44+
4345
void initializeReplicaSetRole(bool isPrimary) override {}
4446
void onStepDown() override {}
4547
void onStepUp() override {}
48+
void shutDown() override;
4649
void notifyOfCollectionVersionUpdate(const NamespaceString& nss) override {}
4750
void waitForCollectionFlush(OperationContext* opCtx, const NamespaceString& nss) override;
4851
void waitForDatabaseFlush(OperationContext* opCtx, StringData dbName) override;

src/mongo/db/s/shard_server_catalog_cache_loader.cpp

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -342,16 +342,7 @@ ShardServerCatalogCacheLoader::ShardServerCatalogCacheLoader(
342342
}
343343

344344
ShardServerCatalogCacheLoader::~ShardServerCatalogCacheLoader() {
345-
// Prevent further scheduling, then interrupt ongoing tasks.
346-
_threadPool.shutdown();
347-
{
348-
stdx::lock_guard<stdx::mutex> lock(_mutex);
349-
_contexts.interrupt(ErrorCodes::InterruptedAtShutdown);
350-
++_term;
351-
}
352-
353-
_threadPool.join();
354-
invariant(_contexts.isEmpty());
345+
shutDown();
355346
}
356347

357348
void ShardServerCatalogCacheLoader::notifyOfCollectionVersionUpdate(const NamespaceString& nss) {
@@ -384,6 +375,30 @@ void ShardServerCatalogCacheLoader::onStepUp() {
384375
_role = ReplicaSetRole::Primary;
385376
}
386377

378+
void ShardServerCatalogCacheLoader::shutDown() {
379+
{
380+
stdx::lock_guard<stdx::mutex> lg(_mutex);
381+
if (_inShutdown) {
382+
return;
383+
}
384+
385+
_inShutdown = true;
386+
}
387+
388+
// Prevent further scheduling, then interrupt ongoing tasks.
389+
_threadPool.shutdown();
390+
{
391+
stdx::lock_guard<stdx::mutex> lock(_mutex);
392+
_contexts.interrupt(ErrorCodes::InterruptedAtShutdown);
393+
++_term;
394+
}
395+
396+
_threadPool.join();
397+
invariant(_contexts.isEmpty());
398+
399+
_configServerLoader->shutDown();
400+
}
401+
387402
std::shared_ptr<Notification<void>> ShardServerCatalogCacheLoader::getChunksSince(
388403
const NamespaceString& nss, ChunkVersion version, GetChunksSinceCallbackFn callbackFn) {
389404
auto notify = std::make_shared<Notification<void>>();

src/mongo/db/s/shard_server_catalog_cache_loader.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ class ShardServerCatalogCacheLoader : public CatalogCacheLoader {
6969
*/
7070
void onStepUp() override;
7171

72+
void shutDown() override;
73+
7274
/**
7375
* Sets any notifications waiting for this version to arrive and invalidates the catalog cache's
7476
* chunk metadata for collection 'nss' so that the next caller provokes a refresh.
@@ -473,7 +475,7 @@ class ShardServerCatalogCacheLoader : public CatalogCacheLoader {
473475

474476
// Loader used by the shard primary to retrieve the authoritative routing metadata from the
475477
// config server
476-
const std::unique_ptr<CatalogCacheLoader> _configServerLoader;
478+
std::unique_ptr<CatalogCacheLoader> _configServerLoader;
477479

478480
// Thread pool used to run blocking tasks which perform disk reads and writes
479481
ThreadPool _threadPool;
@@ -484,6 +486,9 @@ class ShardServerCatalogCacheLoader : public CatalogCacheLoader {
484486
// Protects the class state below
485487
stdx::mutex _mutex;
486488

489+
// True if shutDown was called.
490+
bool _inShutdown{false};
491+
487492
// This value is bumped every time the set of currently scheduled tasks should no longer be
488493
// running. This includes, replica set state transitions and shutdown.
489494
long long _term{0};

src/mongo/db/s/shard_server_catalog_cache_loader_test.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ void ShardServerCatalogCacheLoaderTest::setUp() {
107107
}
108108

109109
void ShardServerCatalogCacheLoaderTest::tearDown() {
110+
_shardLoader->shutDown();
110111
_shardLoader.reset();
111112
ShardServerTestFixture::tearDown();
112113
}

src/mongo/s/catalog_cache_loader.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,12 @@ class CatalogCacheLoader {
108108
*/
109109
virtual void onStepUp() = 0;
110110

111+
/**
112+
* Transitions into shut down and cleans up state. Once this transitions to shut down, should
113+
* not be able to transition back to normal. Should be safe to be called more than once.
114+
*/
115+
virtual void shutDown() = 0;
116+
111117
/**
112118
* Notifies the loader that the persisted collection version for 'nss' has been updated.
113119
*/

src/mongo/s/config_server_catalog_cache_loader.cpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,7 @@ ConfigServerCatalogCacheLoader::ConfigServerCatalogCacheLoader()
143143
}
144144

145145
ConfigServerCatalogCacheLoader::~ConfigServerCatalogCacheLoader() {
146-
_threadPool.shutdown();
147-
_threadPool.join();
146+
shutDown();
148147
}
149148

150149
void ConfigServerCatalogCacheLoader::initializeReplicaSetRole(bool isPrimary) {
@@ -159,6 +158,20 @@ void ConfigServerCatalogCacheLoader::onStepUp() {
159158
MONGO_UNREACHABLE;
160159
}
161160

161+
void ConfigServerCatalogCacheLoader::shutDown() {
162+
{
163+
stdx::lock_guard<stdx::mutex> lg(_mutex);
164+
if (_inShutdown) {
165+
return;
166+
}
167+
168+
_inShutdown = true;
169+
}
170+
171+
_threadPool.shutdown();
172+
_threadPool.join();
173+
}
174+
162175
void ConfigServerCatalogCacheLoader::notifyOfCollectionVersionUpdate(const NamespaceString& nss) {
163176
MONGO_UNREACHABLE;
164177
}

0 commit comments

Comments
 (0)