Skip to content

Commit 50e97d0

Browse files
committed
SERVER-22499 Mark mongos as inShutdown() as soon as shutdown starts
1 parent c3a232e commit 50e97d0

File tree

1 file changed

+43
-45
lines changed

1 file changed

+43
-45
lines changed

src/mongo/s/server.cpp

Lines changed: 43 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,51 @@ ntservice::NtServiceDefaultStrings defaultServiceStrings = {
108108
static ExitCode initService();
109109
#endif
110110

111-
bool dbexitCalled = false;
111+
static AtomicUInt32 shutdownInProgress(0);
112112

113113
bool inShutdown() {
114-
return dbexitCalled;
114+
return shutdownInProgress.loadRelaxed() != 0;
115+
}
116+
void signalShutdown() {
117+
// Notify all threads shutdown has started
118+
shutdownInProgress.fetchAndAdd(1);
119+
}
120+
121+
void exitCleanly(ExitCode code) {
122+
signalShutdown();
123+
{
124+
Client& client = cc();
125+
ServiceContext::UniqueOperationContext uniqueTxn;
126+
OperationContext* txn = client.getOperationContext();
127+
if (!txn) {
128+
uniqueTxn = client.makeOperationContext();
129+
txn = uniqueTxn.get();
130+
}
131+
132+
auto cursorManager = grid.getCursorManager();
133+
cursorManager->shutdown();
134+
grid.shardRegistry()->shutdown();
135+
grid.catalogManager(txn)->shutDown(txn);
136+
}
137+
138+
dbexit(code);
139+
}
140+
141+
void dbexit(ExitCode rc, const char* why) {
142+
audit::logShutdown(ClientBasic::getCurrent());
143+
144+
#if defined(_WIN32)
145+
// Windows Service Controller wants to be told when we are done shutting down
146+
// and call quickExit itself.
147+
//
148+
if (rc == EXIT_WINDOWS_SERVICE_STOP) {
149+
log() << "dbexit: exiting because Windows service was stopped";
150+
return;
151+
}
152+
#endif
153+
154+
log() << "dbexit: " << why << " rc:" << rc;
155+
quickExit(rc);
115156
}
116157

117158
static BSONObj buildErrReply(const DBException& ex) {
@@ -442,46 +483,3 @@ int main(int argc, char* argv[], char** envp) {
442483
quickExit(exitCode);
443484
}
444485
#endif
445-
446-
void mongo::signalShutdown() {
447-
// Notify all threads shutdown has started
448-
dbexitCalled = true;
449-
}
450-
451-
void mongo::exitCleanly(ExitCode code) {
452-
// TODO: do we need to add anything?
453-
{
454-
Client& client = cc();
455-
ServiceContext::UniqueOperationContext uniqueTxn;
456-
OperationContext* txn = client.getOperationContext();
457-
if (!txn) {
458-
uniqueTxn = client.makeOperationContext();
459-
txn = uniqueTxn.get();
460-
}
461-
462-
auto cursorManager = grid.getCursorManager();
463-
cursorManager->shutdown();
464-
grid.shardRegistry()->shutdown();
465-
grid.catalogManager(txn)->shutDown(txn);
466-
}
467-
468-
mongo::dbexit(code);
469-
}
470-
471-
void mongo::dbexit(ExitCode rc, const char* why) {
472-
dbexitCalled = true;
473-
audit::logShutdown(ClientBasic::getCurrent());
474-
475-
#if defined(_WIN32)
476-
// Windows Service Controller wants to be told when we are done shutting down
477-
// and call quickExit itself.
478-
//
479-
if (rc == EXIT_WINDOWS_SERVICE_STOP) {
480-
log() << "dbexit: exiting because Windows service was stopped";
481-
return;
482-
}
483-
#endif
484-
485-
log() << "dbexit: " << why << " rc:" << rc;
486-
quickExit(rc);
487-
}

0 commit comments

Comments
 (0)