SERVER-112639 Fold fsync_locked back into fsync (#42833)

GitOrigin-RevId: 7aa3095a8baa16e327f74bc66458305e146f3f20
This commit is contained in:
Thomas Goyne 2025-10-21 09:12:39 -07:00 committed by MongoDB Bot
parent 4ff409eee6
commit 0422ef8481
16 changed files with 77 additions and 188 deletions

View File

@ -87,7 +87,7 @@ Note: This feature is still in development; see https://jira.mongodb.org/browse/
To run clang-tidy via Bazel, do the following:
1. To analyze all code, run `bazel build --config=clang-tidy src/...`
2. To analyze a single target (e.g.: `fsync_locked`), run the following command (note that `_with_debug` suffix on the target): `bazel build --config=clang-tidy src/mongo/db/commands:fsync_locked_with_debug`
2. To analyze a single target (e.g.: `environment_buffer`), run the following command (note that `_with_debug` suffix on the target): `bazel build --config=clang-tidy src/mongo/db/commands:environment_buffer_with_debug`
Testing notes:

View File

@ -119,12 +119,12 @@ tasks:
- func: "bazel compile"
vars:
targets: >-
//src/mongo/db/commands:fsync_locked_with_debug
//src/mongo/base:environment_buffer_with_debug
bazel_args: >-
--config=evg
- func: "verify build output present"
vars:
output: bazel-bin/src/mongo/db/commands/libfsync_locked_with_debug.lo
output: bazel-bin/src/mongo/base/libenvironment_buffer_with_debug.lo
- name: run_bazel_program
tags: ["assigned_to_jira_team_devprod_build", "auxiliary", "bazel_check"]

View File

@ -3064,7 +3064,7 @@ mongo_cc_library(
"//src/mongo/db/auth:authprivilege",
"//src/mongo/db/auth:security_token_auth",
"//src/mongo/db/auth:user_acquisition_stats",
"//src/mongo/db/commands:fsync_locked",
"//src/mongo/db/commands:mongod_fsync",
"//src/mongo/db/commands:txn_cmd_request",
"//src/mongo/db/commands/server_status:server_status_core",
"//src/mongo/db/repl:repl_server_parameters",

View File

@ -9,12 +9,6 @@ exports_files(
]),
)
mongo_cc_library(
name = "fsync_locked",
srcs = ["fsync_locked.cpp"],
hdrs = ["fsync_locked.h"],
)
idl_generator(
name = "test_commands_enabled_gen",
src = "test_commands_enabled.idl",
@ -600,7 +594,6 @@ mongo_cc_library(
"fsync.h",
],
deps = [
":fsync_locked",
"//src/mongo/db:commands",
"//src/mongo/db:dbdirectclient",
"//src/mongo/db/auth",
@ -957,7 +950,6 @@ mongo_cc_library(
"buildinfo_common",
"core",
"create_command",
"fsync_locked",
"kill_common",
"list_collections_filter",
"list_databases_command",

View File

@ -27,13 +27,9 @@
* it in the license file.
*/
// IWYU pragma: no_include "cxxabi.h"
#include "mongo/db/commands/fsync.h"
#include "mongo/base/error_codes.h"
#include "mongo/base/init.h" // IWYU pragma: keep
#include "mongo/base/initializer.h"
#include "mongo/base/status.h"
#include "mongo/base/string_data.h"
#include "mongo/bson/bsonelement.h"
@ -45,7 +41,6 @@
#include "mongo/db/client.h"
#include "mongo/db/commands.h"
#include "mongo/db/commands/fsync_gen.h"
#include "mongo/db/commands/fsync_locked.h"
#include "mongo/db/commands/test_commands_enabled.h"
#include "mongo/db/database_name.h"
#include "mongo/db/dbdirectclient.h"
@ -60,6 +55,7 @@
#include "mongo/logv2/log.h"
#include "mongo/stdx/condition_variable.h"
#include "mongo/util/assert_util.h"
#include "mongo/util/background.h"
#include "mongo/util/duration.h"
#include "mongo/util/str.h"
@ -70,6 +66,46 @@
#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kCommand
namespace mongo {
namespace {
/**
* Maintains a global read lock while mongod is fsyncLocked.
*/
class FSyncLockThread final : public BackgroundJob {
public:
FSyncLockThread(ServiceContext* serviceContext,
bool allowFsyncFailure,
const Milliseconds deadline)
: BackgroundJob(false),
_serviceContext(serviceContext),
_allowFsyncFailure(allowFsyncFailure),
_deadline(deadline) {}
std::string name() const override {
return "FSyncLockThread";
}
void run() override;
/**
* Releases the fsync lock for shutdown.
*/
void shutdown(stdx::unique_lock<stdx::mutex>& lk);
private:
/**
* Wait lastApplied to catch lastWritten so we won't write/apply any oplog when fsync locked.
*/
void _waitUntilLastAppliedCatchupLastWritten();
private:
ServiceContext* const _serviceContext;
bool _allowFsyncFailure;
const Milliseconds _deadline;
};
// Ensures that only one command is operating on fsyncLock state at a time. As a 'ResourceMutex',
// lock time will be reported for a given user operation.
Lock::ResourceMutex fsyncSingleCommandExclusionMutex("fsyncSingleCommandExclusionMutex");
// Protects access to globalFsyncLockThread and other global fsync state.
stdx::mutex fsyncStateMutex;
@ -78,16 +114,6 @@ stdx::mutex fsyncStateMutex;
// Must acquire the 'fsyncStateMutex' before accessing.
std::unique_ptr<FSyncLockThread> globalFsyncLockThread = nullptr;
// Exposed publically via extern in fsync.h.
stdx::mutex oplogWriterLockedFsync;
stdx::mutex oplogApplierLockedFsync;
namespace {
// Ensures that only one command is operating on fsyncLock state at a time. As a 'ResourceMutex',
// lock time will be reported for a given user operation.
Lock::ResourceMutex fsyncSingleCommandExclusionMutex("fsyncSingleCommandExclusionMutex");
class FSyncCore {
public:
static const char* url() {
@ -283,8 +309,7 @@ private:
stdx::mutex _fsyncLockedMutex;
bool _fsyncLocked = false;
};
FSyncCore fsyncCore;
} fsyncCore;
class FSyncCommand : public TypedCommand<FSyncCommand> {
public:
@ -419,8 +444,6 @@ bool FSyncCore::runFsyncUnlockCommand(OperationContext* opCtx,
}
}
} // namespace
void FSyncLockThread::shutdown(stdx::unique_lock<stdx::mutex>& stateLock) {
if (fsyncCore.getLockCount_inLock() > 0) {
LOGV2_WARNING(20469, "Interrupting fsync because the server is shutting down");
@ -543,9 +566,21 @@ void FSyncLockThread::run() {
LOGV2_FATAL(40350, "FSyncLockThread exception", "error"_attr = e.what());
}
}
} // namespace
MONGO_INITIALIZER(fsyncLockedForWriting)(InitializerContext* context) {
setLockedForWritingImpl([]() { return fsyncCore.fsyncLocked(); });
// Exposed publicly via extern in fsync.h.
stdx::mutex oplogWriterLockedFsync;
stdx::mutex oplogApplierLockedFsync;
bool lockedForWriting() {
return fsyncCore.fsyncLocked();
}
void shutdownFsyncLockThread() {
stdx::unique_lock stateLock(fsyncStateMutex);
if (globalFsyncLockThread) {
globalFsyncLockThread->shutdown(stateLock);
}
}
} // namespace mongo

View File

@ -29,50 +29,21 @@
#pragma once
#include "mongo/db/service_context.h"
#include "mongo/stdx/mutex.h"
#include "mongo/util/background.h"
#include "mongo/util/modules.h"
#include <memory>
#include <string>
MONGO_MOD_PUBLIC;
namespace mongo {
/**
* Returns true if mongod is currently fsyncLocked.
*/
bool lockedForWriting();
/**
* Maintains a global read lock while mongod is fsyncLocked.
* If the fsynclock thread has been created, shut it down.
*/
class FSyncLockThread : public BackgroundJob {
public:
FSyncLockThread(ServiceContext* serviceContext,
bool allowFsyncFailure,
const Milliseconds deadline)
: BackgroundJob(false),
_serviceContext(serviceContext),
_allowFsyncFailure(allowFsyncFailure),
_deadline(deadline) {}
std::string name() const override {
return "FSyncLockThread";
}
void run() override;
/**
* Releases the fsync lock for shutdown.
*/
void shutdown(stdx::unique_lock<stdx::mutex>& lk);
private:
/**
* Wait lastApplied to catch lastWritten so we won't write/apply any oplog when fsync locked.
*/
void _waitUntilLastAppliedCatchupLastWritten();
private:
ServiceContext* const _serviceContext;
bool _allowFsyncFailure;
const Milliseconds _deadline;
};
void shutdownFsyncLockThread();
/**
* This is used to block oplogWriter and should never be acquired by others.
@ -83,16 +54,4 @@ extern stdx::mutex oplogWriterLockedFsync;
* This is used to block oplogApplier and should never be acquired by others.
*/
extern stdx::mutex oplogApplierLockedFsync;
/**
* Must be taken before accessing globalFsyncLockThread below.
*/
extern stdx::mutex fsyncStateMutex;
/**
* The FSyncLockThread must be external available for interruption during shutdown.
* Must lock the 'fsyncStateMutex' before accessing.
*/
extern std::unique_ptr<FSyncLockThread> globalFsyncLockThread;
} // namespace mongo

View File

@ -1,47 +0,0 @@
/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#include "mongo/db/commands/fsync_locked.h"
#include <functional>
#include <utility>
namespace mongo {
namespace {
std::function<bool()> lockedForWritingImpl;
} // namespace
bool lockedForWriting() {
return lockedForWritingImpl();
}
void setLockedForWritingImpl(std::function<bool()> impl) {
lockedForWritingImpl = std::move(impl);
}
} // namespace mongo

View File

@ -1,46 +0,0 @@
/**
* Copyright (C) 2018-present MongoDB, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the Server Side Public License, version 1,
* as published by MongoDB, Inc.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* Server Side Public License for more details.
*
* You should have received a copy of the Server Side Public License
* along with this program. If not, see
* <http://www.mongodb.com/licensing/server-side-public-license>.
*
* As a special exception, the copyright holders give permission to link the
* code of portions of this program with the OpenSSL library under certain
* conditions as described in each individual source file and distribute
* linked combinations including the program with the OpenSSL library. You
* must comply with the Server Side Public License in all respects for
* all of the code used other than as permitted herein. If you modify file(s)
* with this exception, you may extend this exception to your version of the
* file(s), but you are not obligated to do so. If you do not wish to do so,
* delete this exception statement from your version. If you delete this
* exception statement from all source files in the program, then also delete
* it in the license file.
*/
#pragma once
#include <functional>
namespace mongo {
/**
* Returns true if mongod is currently fsyncLocked.
*/
bool lockedForWriting();
/**
* Sets the implementation for lockedForWriting(). Should be done once during startup in a
* MONGO_INITIALIZER.
*/
void setLockedForWritingImpl(std::function<bool()> impl);
} // namespace mongo

View File

@ -38,7 +38,7 @@
#include "mongo/db/auth/privilege.h"
#include "mongo/db/auth/resource_pattern.h"
#include "mongo/db/commands.h"
#include "mongo/db/commands/fsync_locked.h"
#include "mongo/db/commands/fsync.h"
#include "mongo/db/commands/query_cmd/current_op_common.h"
#include "mongo/db/commands/query_cmd/run_aggregate.h"
#include "mongo/db/database_name.h"

View File

@ -29,7 +29,7 @@
#include "mongo/db/exec/agg/current_op_stage.h"
#include "mongo/db/commands/fsync_locked.h"
#include "mongo/db/commands/fsync.h"
#include "mongo/db/exec/agg/document_source_to_stage_registry.h"
#include "mongo/db/pipeline/document_source_current_op.h"

View File

@ -1478,12 +1478,7 @@ void shutdownTask(const ShutdownTaskArgs& shutdownArgs) {
BSONObjBuilder shutdownInfoBuilder;
// Before doing anything else, ensure fsync is inactive or make it release its GlobalRead lock.
{
stdx::unique_lock<stdx::mutex> stateLock(fsyncStateMutex);
if (globalFsyncLockThread) {
globalFsyncLockThread->shutdown(stateLock);
}
}
shutdownFsyncLockThread();
auto const serviceContext = getGlobalServiceContext();

View File

@ -825,7 +825,7 @@ mongo_cc_library(
"//src/mongo/db:shard_filterer",
"//src/mongo/db:shard_role_api",
"//src/mongo/db/auth",
"//src/mongo/db/commands:fsync_locked",
"//src/mongo/db/commands:mongod_fsync",
"//src/mongo/db/commands:test_commands_enabled",
"//src/mongo/db/exec:bucket_unpacker",
"//src/mongo/db/exec:projection_executor",

View File

@ -36,7 +36,7 @@
#include "mongo/bson/bsonobjbuilder.h"
#include "mongo/db/client.h"
#include "mongo/db/commands.h"
#include "mongo/db/commands/fsync_locked.h"
#include "mongo/db/commands/fsync.h"
#include "mongo/db/curop.h"
#include "mongo/db/global_catalog/catalog_cache/catalog_cache.h"
#include "mongo/db/global_catalog/catalog_cache/shard_cannot_refresh_due_to_locks_held_exception.h"

View File

@ -28,6 +28,7 @@
*/
#pragma once
#include "mongo/base/error_codes.h"
#include "mongo/base/status.h"
#include "mongo/base/status_with.h"
@ -35,7 +36,7 @@
#include "mongo/bson/bsonobjbuilder.h"
#include "mongo/db/client.h"
#include "mongo/db/commands.h"
#include "mongo/db/commands/fsync_locked.h"
#include "mongo/db/commands/fsync.h"
#include "mongo/db/curop.h"
#include "mongo/db/global_catalog/catalog_cache/catalog_cache.h"
#include "mongo/db/global_catalog/catalog_cache/shard_cannot_refresh_due_to_locks_held_exception.h"

View File

@ -47,7 +47,7 @@ mongo_cc_library(
"//src/mongo/db:server_base",
"//src/mongo/db:service_context",
"//src/mongo/db:shard_role",
"//src/mongo/db/commands:fsync_locked",
"//src/mongo/db/commands:mongod_fsync",
"//src/mongo/db/commands/server_status:server_status_core",
"//src/mongo/db/local_catalog:catalog_helpers",
"//src/mongo/db/local_catalog:index_key_validate",

View File

@ -37,7 +37,7 @@
#include "mongo/db/admission/execution_admission_context.h"
#include "mongo/db/auth/authorization_session.h"
#include "mongo/db/client.h"
#include "mongo/db/commands/fsync_locked.h"
#include "mongo/db/commands/fsync.h"
#include "mongo/db/commands/server_status/server_status_metric.h"
#include "mongo/db/exec/classic/batched_delete_stage.h"
#include "mongo/db/exec/classic/delete_stage.h"