diff options
author | Geert Bosch <geert@mongodb.com> | 2019-03-26 15:53:42 -0400 |
---|---|---|
committer | Geert Bosch <geert@mongodb.com> | 2019-05-31 11:18:53 -0400 |
commit | 36389e9fbb1bf1cc0aaba16e6b46adc171a34f46 (patch) | |
tree | cf528842ef760bb286a9f702a417b72cb0eef855 | |
parent | 933c6ad19c3f19a964c74a5174cbcf11cde0a66e (diff) | |
download | mongo-36389e9fbb1bf1cc0aaba16e6b46adc171a34f46.tar.gz |
SERVER-41344 Format using fmt/format instead of snprintf
5 files changed, 201 insertions, 67 deletions
diff --git a/src/mongo/db/storage/wiredtiger/SConscript b/src/mongo/db/storage/wiredtiger/SConscript index 162adf07611..e0aa028a938 100644 --- a/src/mongo/db/storage/wiredtiger/SConscript +++ b/src/mongo/db/storage/wiredtiger/SConscript @@ -263,19 +263,31 @@ if wiredtiger: wtEnv.CppUnitTest( target='storage_wiredtiger_util_test', source=['wiredtiger_util_test.cpp', - ], + ], LIBDEPS=[ '$BUILD_DIR/mongo/db/service_context', '$BUILD_DIR/mongo/db/storage/kv/kv_engine_core', 'storage_wiredtiger_mock', - ], - ) + ], + ) wtEnv.CppUnitTest( target='storage_wiredtiger_session_cache_test', source=['wiredtiger_session_cache_test.cpp', - ], + ], LIBDEPS=[ 'storage_wiredtiger_mock', - ], - ) + ], + ) + + wtEnv.Benchmark( + target='storage_wiredtiger_begin_transaction_block_bm', + source='wiredtiger_begin_transaction_block_bm.cpp', + LIBDEPS=[ + '$BUILD_DIR/mongo/db/service_context', + '$BUILD_DIR/mongo/db/storage/kv/kv_engine_core', + '$BUILD_DIR/mongo/unittest/unittest', + '$BUILD_DIR/mongo/util/clock_source_mock', + 'storage_wiredtiger_mock', + ], + ) diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_begin_transaction_block.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_begin_transaction_block.cpp index e499aeda71f..2e51a57db4c 100644 --- a/src/mongo/db/storage/wiredtiger/wiredtiger_begin_transaction_block.cpp +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_begin_transaction_block.cpp @@ -32,6 +32,7 @@ #include "mongo/platform/basic.h" #include <cstdio> +#include <fmt/format.h> #include "mongo/db/storage/wiredtiger/wiredtiger_begin_transaction_block.h" #include "mongo/db/storage/wiredtiger/wiredtiger_util.h" @@ -39,6 +40,7 @@ #include "mongo/util/log.h" namespace mongo { +using namespace fmt::literals; WiredTigerBeginTxnBlock::WiredTigerBeginTxnBlock( WT_SESSION* session, @@ -84,21 +86,9 @@ WiredTigerBeginTxnBlock::~WiredTigerBeginTxnBlock() { Status WiredTigerBeginTxnBlock::setReadSnapshot(Timestamp readTimestamp) { invariant(_rollback); - char readTSConfigString[15 /* read_timestamp= */ + 16 /* 16 hexadecimal digits */ + - 1 /* trailing null */]; - auto size = std::snprintf(readTSConfigString, - sizeof(readTSConfigString), - "read_timestamp=%llx", - readTimestamp.asULL()); - if (size < 0) { - int e = errno; - error() << "error snprintf " << errnoWithDescription(e); - fassertFailedNoTrace(40664); - } - invariant(static_cast<std::size_t>(size) < sizeof(readTSConfigString)); + std::string readTSConfigString = "read_timestamp={:x}"_format(readTimestamp.asULL()); - auto status = wtRCToStatus(_session->timestamp_transaction(_session, readTSConfigString)); - return status; + return wtRCToStatus(_session->timestamp_transaction(_session, readTSConfigString.c_str())); } void WiredTigerBeginTxnBlock::done() { diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_begin_transaction_block_bm.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_begin_transaction_block_bm.cpp new file mode 100644 index 00000000000..23504054675 --- /dev/null +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_begin_transaction_block_bm.cpp @@ -0,0 +1,161 @@ + +/** + * 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/platform/basic.h" + +#include <benchmark/benchmark.h> + +#include "mongo/base/checked_cast.h" +#include "mongo/db/repl/repl_settings.h" +#include "mongo/db/repl/replication_coordinator_mock.h" +#include "mongo/db/service_context.h" +#include "mongo/db/storage/recovery_unit_test_harness.h" +#include "mongo/db/storage/wiredtiger/wiredtiger_begin_transaction_block.h" +#include "mongo/db/storage/wiredtiger/wiredtiger_kv_engine.h" +#include "mongo/db/storage/wiredtiger/wiredtiger_record_store.h" +#include "mongo/db/storage/wiredtiger/wiredtiger_recovery_unit.h" +#include "mongo/db/storage/wiredtiger/wiredtiger_session_cache.h" +#include "mongo/db/storage/wiredtiger/wiredtiger_util.h" +#include "mongo/unittest/temp_dir.h" +#include "mongo/unittest/unittest.h" +#include "mongo/util/clock_source_mock.h" + +namespace mongo { +namespace { + +class WiredTigerConnection { +public: + WiredTigerConnection(StringData dbpath, StringData extraStrings) : _conn(nullptr) { + std::stringstream ss; + ss << "create,"; + ss << extraStrings; + std::string config = ss.str(); + int ret = wiredtiger_open(dbpath.toString().c_str(), nullptr, config.c_str(), &_conn); + invariant(wtRCToStatus(ret).isOK()); + } + ~WiredTigerConnection() { + _conn->close(_conn, nullptr); + } + WT_CONNECTION* getConnection() const { + return _conn; + } + +private: + WT_CONNECTION* _conn; +}; + +class WiredTigerTestHelper { +public: + WiredTigerTestHelper() + : _dbpath("wt_test"), + _connection(_dbpath.path(), ""), + _sessionCache(_connection.getConnection(), &_clockSource) { + _opCtx.reset(newOperationContext()); + auto ru = WiredTigerRecoveryUnit::get(_opCtx.get()); + _wtSession = ru->getSession()->getSession(); + invariant(wtRCToStatus(_wtSession->create(_wtSession, "table:mytable", nullptr)).isOK()); + ru->abandonSnapshot(); + } + + WiredTigerSessionCache* getSessionCache() { + return &_sessionCache; + } + + WiredTigerOplogManager* getOplogManager() { + return &_oplogManager; + } + + WT_SESSION* wtSession() const { + return _wtSession; + } + + OperationContext* newOperationContext() { + return new OperationContextNoop( + new WiredTigerRecoveryUnit(getSessionCache(), &_oplogManager)); + } + + OperationContext* getOperationContext() const { + return _opCtx.get(); + } + +private: + unittest::TempDir _dbpath; + WiredTigerConnection _connection; + ClockSourceMock _clockSource; + WiredTigerSessionCache _sessionCache; + WiredTigerOplogManager _oplogManager; + std::unique_ptr<OperationContext> _opCtx; + WT_SESSION* _wtSession; +}; + +void BM_WiredTigerBeginTxnBlock(benchmark::State& state) { + WiredTigerTestHelper helper; + for (auto _ : state) { + WiredTigerBeginTxnBlock beginTxn(helper.wtSession(), nullptr); + } +} + +using mongo::WiredTigerBeginTxnBlock; + +template <IgnorePrepared ignore, RoundUpPreparedTimestamps round> +void BM_WiredTigerBeginTxnBlockWithArgs(benchmark::State& state) { + WiredTigerTestHelper helper; + for (auto _ : state) { + WiredTigerBeginTxnBlock beginTxn(helper.wtSession(), ignore, round); + } +} + + +void BM_setTimestamp(benchmark::State& state) { + WiredTigerTestHelper helper; + for (auto _ : state) { + WiredTigerBeginTxnBlock beginTxn(helper.wtSession(), nullptr); + ASSERT_OK(beginTxn.setReadSnapshot(Timestamp(1))); + } +} + +BENCHMARK(BM_WiredTigerBeginTxnBlock); +BENCHMARK_TEMPLATE(BM_WiredTigerBeginTxnBlockWithArgs, + IgnorePrepared::kNoIgnore, + RoundUpPreparedTimestamps::kNoRound); +BENCHMARK_TEMPLATE(BM_WiredTigerBeginTxnBlockWithArgs, + IgnorePrepared::kNoIgnore, + RoundUpPreparedTimestamps::kRound); +BENCHMARK_TEMPLATE(BM_WiredTigerBeginTxnBlockWithArgs, + IgnorePrepared::kIgnore, + RoundUpPreparedTimestamps::kNoRound); +BENCHMARK_TEMPLATE(BM_WiredTigerBeginTxnBlockWithArgs, + IgnorePrepared::kIgnore, + RoundUpPreparedTimestamps::kRound); + +BENCHMARK(BM_setTimestamp); + +} // namespace +} // namespace mongo diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp index 2d9e69c653a..e7db913f93b 100644 --- a/src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_kv_engine.cpp @@ -95,6 +95,8 @@ #define __has_feature(x) 0 #endif +using namespace fmt::literals; + namespace mongo { bool WiredTigerFileVersion::shouldDowngrade(bool readOnly, @@ -1599,8 +1601,8 @@ void WiredTigerKVEngine::setStableTimestamp(Timestamp stableTimestamp, bool forc auto ts = stableTimestamp.asULL(); if (force) { stableTSConfigString = - "force=true,oldest_timestamp={:x},commit_timestamp={:x},stable_timestamp={:x}"_format( - ts, ts, ts); + "force=true,oldest_timestamp={0:x},commit_timestamp={0:x},stable_timestamp={0:x}"_format( + ts); } else { stableTSConfigString = "stable_timestamp={:x}"_format(ts); } @@ -1651,40 +1653,19 @@ void WiredTigerKVEngine::setOldestTimestamp(Timestamp newOldestTimestamp, bool f return; } - char oldestTSConfigString["force=true,oldest_timestamp=,commit_timestamp="_sd.size() + - (2 * 8 * 2) /* 2 timestamps of 16 hexadecimal digits each */ + - 1 /* trailing null */]; - int size = 0; - if (force) { - size = std::snprintf(oldestTSConfigString, - sizeof(oldestTSConfigString), - "force=true,oldest_timestamp=%llx,commit_timestamp=%llx", - newOldestTimestamp.asULL(), - newOldestTimestamp.asULL()); - } else { - size = std::snprintf(oldestTSConfigString, - sizeof(oldestTSConfigString), - "oldest_timestamp=%llx", - newOldestTimestamp.asULL()); - } - if (size < 0) { - int e = errno; - error() << "error snprintf " << errnoWithDescription(e); - fassertFailedNoTrace(40661); - } - invariant(static_cast<std::size_t>(size) < sizeof(oldestTSConfigString)); - invariantWTOK(_conn->set_timestamp(_conn, oldestTSConfigString)); - - // set_timestamp above ignores backwards in time unless 'force' is set. if (force) { + auto oldestTSConfigString = + "force=true,oldest_timestamp={0:x},commit_timestamp={0:x}"_format( + newOldestTimestamp.asULL()); + invariantWTOK(_conn->set_timestamp(_conn, oldestTSConfigString.c_str())); _oldestTimestamp.store(newOldestTimestamp.asULL()); - } else if (_oldestTimestamp.load() < newOldestTimestamp.asULL()) { - _oldestTimestamp.store(newOldestTimestamp.asULL()); - } - - if (force) { LOG(2) << "oldest_timestamp and commit_timestamp force set to " << newOldestTimestamp; } else { + auto oldestTSConfigString = "oldest_timestamp={:x}"_format(newOldestTimestamp.asULL()); + invariantWTOK(_conn->set_timestamp(_conn, oldestTSConfigString.c_str())); + // set_timestamp above ignores backwards in time if 'force' is not set. + if (_oldestTimestamp.load() < newOldestTimestamp.asULL()) + _oldestTimestamp.store(newOldestTimestamp.asULL()); LOG(2) << "oldest_timestamp set to " << newOldestTimestamp; } } diff --git a/src/mongo/db/storage/wiredtiger/wiredtiger_record_store.cpp b/src/mongo/db/storage/wiredtiger/wiredtiger_record_store.cpp index 5bab4a26b67..081a88ba1e1 100644 --- a/src/mongo/db/storage/wiredtiger/wiredtiger_record_store.cpp +++ b/src/mongo/db/storage/wiredtiger/wiredtiger_record_store.cpp @@ -33,6 +33,8 @@ #include "mongo/platform/basic.h" +#include <fmt/format.h> + #include "mongo/db/storage/wiredtiger/wiredtiger_record_store.h" #include "mongo/base/checked_cast.h" @@ -68,6 +70,7 @@ namespace mongo { +using namespace fmt::literals; using std::unique_ptr; using std::string; @@ -1807,22 +1810,9 @@ void WiredTigerRecordStore::cappedTruncateAfter(OperationContext* opCtx, const bool force = true; _kvEngine->setOldestTimestamp(truncTs, force); } else { - char commitTSConfigString["commit_timestamp="_sd.size() + - (8 * 2) /* 8 hexadecimal characters */ + - 1 /* trailing null */]; - auto size = std::snprintf(commitTSConfigString, - sizeof(commitTSConfigString), - "commit_timestamp=%llx", - truncTs.asULL()); - if (size < 0) { - int e = errno; - error() << "error snprintf " << errnoWithDescription(e); - fassertFailedNoTrace(40662); - } - - invariant(static_cast<std::size_t>(size) < sizeof(commitTSConfigString)); auto conn = WiredTigerRecoveryUnit::get(opCtx)->getSessionCache()->conn(); - invariantWTOK(conn->set_timestamp(conn, commitTSConfigString)); + auto commitTSConfigString = "commit_timestamp={:x}"_format(truncTs.asULL()); + invariantWTOK(conn->set_timestamp(conn, commitTSConfigString.c_str())); } _kvEngine->getOplogManager()->setOplogReadTimestamp(truncTs); |