From 31da5e34c1b97a7a1c842044fae46c1f7d10e342 Mon Sep 17 00:00:00 2001 From: Peter Dillinger Date: Mon, 20 Apr 2020 13:21:34 -0700 Subject: [PATCH] C++20 compatibility (#6697) Summary: Based on https://github.com/facebook/rocksdb/issues/6648 (CLA Signed), but heavily modified / extended: * Implicit capture of this via [=] deprecated in C++20, and [=,this] not standard before C++20 -> now using explicit capture lists * Implicit copy operator deprecated in gcc 9 -> add explicit '= default' definition * std::random_shuffle deprecated in C++17 and removed in C++20 -> migrated to a replacement in RocksDB random.h API * Add the ability to build with different std version though -DCMAKE_CXX_STANDARD=11/14/17/20 on the cmake command line * Minimal rebuild flag of MSVC is deprecated and is forbidden with /std:c++latest (C++20) * Added MSVC 2019 C++11 & MSVC 2019 C++20 in AppVeyor * Added GCC 9 C++11 & GCC9 C++20 in Travis Pull Request resolved: https://github.com/facebook/rocksdb/pull/6697 Test Plan: make check and CI Reviewed By: cheng-chang Differential Revision: D21020318 Pulled By: pdillinger fbshipit-source-id: 12311be5dbd8675a0e2c817f7ec50fa11c18ab91 --- .travis.yml | 47 ++++++++++++++++++- CMakeLists.txt | 21 +++++++-- appveyor.yml | 17 +++++-- build_tools/build_detect_platform | 1 + cmake/modules/CxxFlags.cmake | 7 +++ db/db_bloom_filter_test.cc | 3 +- db/db_compaction_test.cc | 2 +- db/db_dynamic_level_test.cc | 2 +- db/db_test.cc | 6 +-- db/log_reader.cc | 2 +- db/memtable.cc | 2 +- db/perf_context_test.cc | 4 +- db/write_batch.cc | 2 +- include/rocksdb/file_system.h | 2 + memory/concurrent_arena.h | 11 +++-- memtable/memtablerep_bench.cc | 5 +- util/util.h => port/lang.h | 0 .../block_based/block_based_table_builder.cc | 7 +-- table/block_based/block_based_table_reader.cc | 2 +- test_util/fault_injection_test_fs.cc | 2 +- test_util/transaction_test_util.cc | 4 +- .../folly/folly/synchronization/Baton.h | 5 +- .../block_cache_trace_analyzer.cc | 7 ++- tools/db_bench_tool.cc | 5 +- util/crc32c.cc | 2 +- util/hash.cc | 4 +- util/murmurhash.cc | 2 +- util/random.h | 14 ++++++ util/xxhash.cc | 2 +- utilities/blob_db/blob_db_impl.cc | 4 +- .../write_unprepared_transaction_test.cc | 5 +- 31 files changed, 144 insertions(+), 55 deletions(-) create mode 100644 cmake/modules/CxxFlags.cmake rename util/util.h => port/lang.h (100%) diff --git a/.travis.yml b/.travis.yml index 257474dcc..561fbff8a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -50,12 +50,18 @@ env: - JOB_NAME=examples # 5-7 minutes - JOB_NAME=cmake # 3-5 minutes - JOB_NAME=cmake-gcc8 # 3-5 minutes + - JOB_NAME=cmake-gcc9 # 3-5 minutes + - JOB_NAME=cmake-gcc9-c++20 # 3-5 minutes - JOB_NAME=cmake-mingw # 3 minutes matrix: exclude: - os: osx env: JOB_NAME=cmake-gcc8 + - os: osx + env: JOB_NAME=cmake-gcc9 + - os: osx + env: JOB_NAME=cmake-gcc9-c++20 - os: osx env: JOB_NAME=cmake-mingw - os: osx @@ -70,6 +76,19 @@ matrix: env: JOB_NAME=cmake-mingw - os: linux compiler: clang + # Exclude all but most unique cmake variants for pull requests, but build all in branches + - if: type = pull_request + os : linux + arch: amd64 + env: JOB_NAME=cmake + - if: type = pull_request + os : linux + arch: amd64 + env: JOB_NAME=cmake-gcc8 + - if: type = pull_request + os : linux + arch: amd64 + env: JOB_NAME=cmake-gcc9 # Exclude most osx, arm64 and ppc64le tests for pull requests, but build in branches # Temporarily disable ppc64le unit tests in PRs until Travis gets its act together (#6653) - if: type = pull_request @@ -162,6 +181,22 @@ matrix: os: linux arch: ppc64le env: JOB_NAME=cmake-gcc8 + - if: type = pull_request + os : linux + arch: arm64 + env: JOB_NAME=cmake-gcc9 + - if: type = pull_request + os: linux + arch: ppc64le + env: JOB_NAME=cmake-gcc9 + - if: type = pull_request + os : linux + arch: arm64 + env: JOB_NAME=cmake-gcc9-c++20 + - if: type = pull_request + os: linux + arch: ppc64le + env: JOB_NAME=cmake-gcc9-c++20 install: - if [ "${TRAVIS_OS_NAME}" == osx ]; then @@ -171,6 +206,10 @@ install: sudo apt-get install -y g++-8; CC=gcc-8 && CXX=g++-8; fi + - if [ "${JOB_NAME}" == cmake-gcc9 ] || [ "${JOB_NAME}" == cmake-gcc9-c++20 ]; then + sudo apt-get install -y g++-9; + CC=gcc-9 && CXX=g++-9; + fi - if [ "${JOB_NAME}" == cmake-mingw ]; then sudo apt-get install -y mingw-w64 ; fi @@ -240,7 +279,13 @@ script: mkdir build && cd build && cmake -DJNI=1 -DWITH_GFLAGS=OFF .. -DCMAKE_C_COMPILER=x86_64-w64-mingw32-gcc -DCMAKE_CXX_COMPILER=x86_64-w64-mingw32-g++ -DCMAKE_SYSTEM_NAME=Windows && make -j4 rocksdb rocksdbjni ;; cmake*) - mkdir build && cd build && cmake -DJNI=1 .. -DCMAKE_BUILD_TYPE=Release && make -j4 rocksdb rocksdbjni + case $JOB_NAME in + *-c++20) + OPT=-DCMAKE_CXX_STANDARD=20 + ;; + esac + + mkdir build && cd build && cmake -DJNI=1 .. -DCMAKE_BUILD_TYPE=Release $OPT && make -j4 rocksdb rocksdbjni ;; esac notifications: diff --git a/CMakeLists.txt b/CMakeLists.txt index 7a9bc71f8..bcb2c0c8f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -83,6 +83,10 @@ else() option(WITH_FOLLY_DISTRIBUTED_MUTEX "build with folly::DistributedMutex" OFF) endif() +if( NOT DEFINED CMAKE_CXX_STANDARD ) + set(CMAKE_CXX_STANDARD 11) +endif() + include(CMakeDependentOption) CMAKE_DEPENDENT_OPTION(WITH_GFLAGS "build with GFlags" ON "NOT MSVC;NOT MINGW" OFF) @@ -191,7 +195,6 @@ else() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-format -fno-asynchronous-unwind-tables") add_definitions(-D_POSIX_C_SOURCE=1) endif() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") if(NOT CMAKE_BUILD_TYPE STREQUAL "Debug") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-omit-frame-pointer") include(CheckCXXCompilerFlag) @@ -254,6 +257,7 @@ include(CheckCXXSourceCompiles) if(NOT MSVC) set(CMAKE_REQUIRED_FLAGS "-msse4.2 -mpclmul") endif() + CHECK_CXX_SOURCE_COMPILES(" #include #include @@ -386,7 +390,15 @@ if(MSVC) message(STATUS "Debug optimization is enabled") set(CMAKE_CXX_FLAGS_DEBUG "/Oxt") else() - set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Od /RTC1 /Gm") + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Od /RTC1") + + # Minimal Build is deprecated after MSVC 2015 + if( MSVC_VERSION GREATER 1900 ) + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Gm-") + else() + set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /Gm") + endif() + endif() if(WITH_RUNTIME_DEBUG) set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} /${RUNTIME_LIBRARY}d") @@ -849,7 +861,6 @@ if(ROCKSDB_BUILD_SHARED) LINKER_LANGUAGE CXX VERSION ${rocksdb_VERSION} SOVERSION ${rocksdb_VERSION_MAJOR} - CXX_STANDARD 11 OUTPUT_NAME "rocksdb") endif() endif() @@ -1130,7 +1141,7 @@ if(WITH_TESTS) PROPERTIES EXCLUDE_FROM_DEFAULT_BUILD_RELEASE 1 EXCLUDE_FROM_DEFAULT_BUILD_MINRELEASE 1 EXCLUDE_FROM_DEFAULT_BUILD_RELWITHDEBINFO 1 - ) + ) foreach(sourcefile ${TESTS}) get_filename_component(exename ${sourcefile} NAME_WE) @@ -1140,7 +1151,7 @@ if(WITH_TESTS) EXCLUDE_FROM_DEFAULT_BUILD_MINRELEASE 1 EXCLUDE_FROM_DEFAULT_BUILD_RELWITHDEBINFO 1 OUTPUT_NAME ${exename}${ARTIFACT_SUFFIX} - ) + ) target_link_libraries(${CMAKE_PROJECT_NAME}_${exename}${ARTIFACT_SUFFIX} testutillib${ARTIFACT_SUFFIX} testharness gtest ${ROCKSDB_LIB}) if(NOT "${exename}" MATCHES "db_sanity_test") add_test(NAME ${exename} COMMAND ${exename}${ARTIFACT_SUFFIX}) diff --git a/appveyor.yml b/appveyor.yml index 37ae19ef3..439cf860b 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -1,6 +1,6 @@ version: 1.0.{build} -image: Visual Studio 2017 +image: Visual Studio 2019 environment: JAVA_HOME: C:\Program Files\Java\jdk1.8.0 @@ -24,6 +24,15 @@ environment: - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017 CMAKE_GENERATOR: Visual Studio 15 Win64 DEV_ENV: C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\Common7\IDE\devenv.com + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019 + CMAKE_GENERATOR: Visual Studio 16 + CMAKE_PLATEFORM_NAME: x64 + DEV_ENV: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\devenv.com + - APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2019 + CMAKE_GENERATOR: Visual Studio 16 + CMAKE_PLATEFORM_NAME: x64 + CMAKE_OPT: -DCMAKE_CXX_STANDARD=20 + DEV_ENV: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\Common7\IDE\devenv.com install: - md %THIRDPARTY_HOME% @@ -34,7 +43,8 @@ install: - cd snappy-1.1.7 - mkdir build - cd build - - cmake -G "%CMAKE_GENERATOR%" .. + - if DEFINED CMAKE_PLATEFORM_NAME (set "PLATEFORM_OPT=-A %CMAKE_PLATEFORM_NAME%") + - cmake .. -G "%CMAKE_GENERATOR%" %PLATEFORM_OPT% - msbuild Snappy.sln /p:Configuration=Debug /p:Platform=x64 - msbuild Snappy.sln /p:Configuration=Release /p:Platform=x64 - echo "Building LZ4 dependency..." @@ -57,7 +67,8 @@ install: before_build: - md %APPVEYOR_BUILD_FOLDER%\build - cd %APPVEYOR_BUILD_FOLDER%\build - - cmake -G "%CMAKE_GENERATOR%" -DCMAKE_BUILD_TYPE=Debug -DOPTDBG=1 -DPORTABLE=1 -DSNAPPY=1 -DLZ4=1 -DZSTD=1 -DXPRESS=1 -DJNI=1 .. + - if DEFINED CMAKE_PLATEFORM_NAME (set "PLATEFORM_OPT=-A %CMAKE_PLATEFORM_NAME%") + - cmake .. -G "%CMAKE_GENERATOR%" %PLATEFORM_OPT% %CMAKE_OPT% -DCMAKE_BUILD_TYPE=Debug -DOPTDBG=1 -DPORTABLE=1 -DSNAPPY=1 -DLZ4=1 -DZSTD=1 -DXPRESS=1 -DJNI=1 - cd .. build: diff --git a/build_tools/build_detect_platform b/build_tools/build_detect_platform index f7250e9f5..d574e43cd 100755 --- a/build_tools/build_detect_platform +++ b/build_tools/build_detect_platform @@ -705,6 +705,7 @@ EOF fi fi + if [ "$FBCODE_BUILD" != "true" -a "$PLATFORM" = OS_LINUX ]; then $CXX $COMMON_FLAGS $PLATFORM_SHARED_CFLAGS -x c++ -c - -o test_dl.o 2>/dev/null < #include "file/sequence_file_reader.h" +#include "port/lang.h" #include "rocksdb/env.h" #include "test_util/sync_point.h" #include "util/coding.h" #include "util/crc32c.h" -#include "util/util.h" namespace ROCKSDB_NAMESPACE { namespace log { diff --git a/db/memtable.cc b/db/memtable.cc index efc73b7ca..6e44d5658 100644 --- a/db/memtable.cc +++ b/db/memtable.cc @@ -23,6 +23,7 @@ #include "memory/memory_usage.h" #include "monitoring/perf_context_imp.h" #include "monitoring/statistics.h" +#include "port/lang.h" #include "port/port.h" #include "rocksdb/comparator.h" #include "rocksdb/env.h" @@ -36,7 +37,6 @@ #include "util/autovector.h" #include "util/coding.h" #include "util/mutexlock.h" -#include "util/util.h" namespace ROCKSDB_NAMESPACE { diff --git a/db/perf_context_test.cc b/db/perf_context_test.cc index 86f2db7b6..0410ada7a 100644 --- a/db/perf_context_test.cc +++ b/db/perf_context_test.cc @@ -251,7 +251,7 @@ void ProfileQueries(bool enabled_time = false) { } if (FLAGS_random_key) { - std::random_shuffle(keys.begin(), keys.end()); + RandomShuffle(std::begin(keys), std::end(keys)); } #ifndef NDEBUG ThreadStatusUtil::TEST_SetStateDelay(ThreadStatus::STATE_MUTEX_WAIT, 1U); @@ -524,7 +524,7 @@ TEST_F(PerfContextTest, SeekKeyComparison) { } if (FLAGS_random_key) { - std::random_shuffle(keys.begin(), keys.end()); + RandomShuffle(std::begin(keys), std::end(keys)); } HistogramImpl hist_put_time; diff --git a/db/write_batch.cc b/db/write_batch.cc index d578db59b..563c33de2 100644 --- a/db/write_batch.cc +++ b/db/write_batch.cc @@ -53,13 +53,13 @@ #include "db/write_batch_internal.h" #include "monitoring/perf_context_imp.h" #include "monitoring/statistics.h" +#include "port/lang.h" #include "rocksdb/merge_operator.h" #include "util/autovector.h" #include "util/cast_util.h" #include "util/coding.h" #include "util/duplicate_detector.h" #include "util/string_util.h" -#include "util/util.h" namespace ROCKSDB_NAMESPACE { diff --git a/include/rocksdb/file_system.h b/include/rocksdb/file_system.h index 587cdfc2b..4fdd51c67 100644 --- a/include/rocksdb/file_system.h +++ b/include/rocksdb/file_system.h @@ -105,6 +105,8 @@ struct FileOptions : EnvOptions { FileOptions(const FileOptions& opts) : EnvOptions(opts), io_options(opts.io_options) {} + + FileOptions& operator=(const FileOptions& opts) = default; }; // A structure to pass back some debugging information from the FileSystem diff --git a/memory/concurrent_arena.h b/memory/concurrent_arena.h index dad27a307..9c55587e6 100644 --- a/memory/concurrent_arena.h +++ b/memory/concurrent_arena.h @@ -13,6 +13,7 @@ #include #include "memory/allocator.h" #include "memory/arena.h" +#include "port/lang.h" #include "port/likely.h" #include "util/core_local.h" #include "util/mutexlock.h" @@ -49,7 +50,7 @@ class ConcurrentArena : public Allocator { char* Allocate(size_t bytes) override { return AllocateImpl(bytes, false /*force_arena*/, - [=]() { return arena_.Allocate(bytes); }); + [this, bytes]() { return arena_.Allocate(bytes); }); } char* AllocateAligned(size_t bytes, size_t huge_page_size = 0, @@ -58,9 +59,11 @@ class ConcurrentArena : public Allocator { assert(rounded_up >= bytes && rounded_up < bytes + sizeof(void*) && (rounded_up % sizeof(void*)) == 0); - return AllocateImpl(rounded_up, huge_page_size != 0 /*force_arena*/, [=]() { - return arena_.AllocateAligned(rounded_up, huge_page_size, logger); - }); + return AllocateImpl(rounded_up, huge_page_size != 0 /*force_arena*/, + [this, rounded_up, huge_page_size, logger]() { + return arena_.AllocateAligned(rounded_up, + huge_page_size, logger); + }); } size_t ApproximateMemoryUsage() const { diff --git a/memtable/memtablerep_bench.cc b/memtable/memtablerep_bench.cc index 06074a3a4..b44bc06fd 100644 --- a/memtable/memtablerep_bench.cc +++ b/memtable/memtablerep_bench.cc @@ -170,9 +170,8 @@ class KeyGenerator { for (uint64_t i = 0; i < num_; ++i) { values_[i] = i; } - std::shuffle( - values_.begin(), values_.end(), - std::default_random_engine(static_cast(FLAGS_seed))); + RandomShuffle(values_.begin(), values_.end(), + static_cast(FLAGS_seed)); } } diff --git a/util/util.h b/port/lang.h similarity index 100% rename from util/util.h rename to port/lang.h diff --git a/table/block_based/block_based_table_builder.cc b/table/block_based/block_based_table_builder.cc index 5a702d6ef..76e087bb5 100644 --- a/table/block_based/block_based_table_builder.cc +++ b/table/block_based/block_based_table_builder.cc @@ -21,6 +21,7 @@ #include "db/dbformat.h" #include "index_builder.h" +#include "port/lang.h" #include "rocksdb/cache.h" #include "rocksdb/comparator.h" @@ -661,13 +662,13 @@ BlockBasedTableBuilder::BlockBasedTableBuilder( rep_->pc_rep->compress_thread_pool.reserve( rep_->compression_opts.parallel_threads); for (uint32_t i = 0; i < rep_->compression_opts.parallel_threads; i++) { - rep_->pc_rep->compress_thread_pool.emplace_back([=] { + rep_->pc_rep->compress_thread_pool.emplace_back([this, i] { BGWorkCompression(*(rep_->compression_ctxs[i]), rep_->verify_ctxs[i].get()); }); } rep_->pc_rep->write_thread.reset( - new port::Thread([=] { BGWorkWriteRawBlock(); })); + new port::Thread([this] { BGWorkWriteRawBlock(); })); } } @@ -832,7 +833,7 @@ void BlockBasedTableBuilder::Flush() { if (first_block) { std::unique_lock lock(r->pc_rep->first_block_mutex); r->pc_rep->first_block_cond.wait(lock, - [=] { return !r->pc_rep->first_block; }); + [r] { return !r->pc_rep->first_block; }); } } else { WriteBlock(&r->data_block, &r->pending_handle, true /* is_data_block */); diff --git a/table/block_based/block_based_table_reader.cc b/table/block_based/block_based_table_reader.cc index 1dc2ae080..9a925de83 100644 --- a/table/block_based/block_based_table_reader.cc +++ b/table/block_based/block_based_table_reader.cc @@ -53,12 +53,12 @@ #include "table/two_level_iterator.h" #include "monitoring/perf_context_imp.h" +#include "port/lang.h" #include "test_util/sync_point.h" #include "util/coding.h" #include "util/crc32c.h" #include "util/stop_watch.h" #include "util/string_util.h" -#include "util/util.h" #include "util/xxhash.h" namespace ROCKSDB_NAMESPACE { diff --git a/test_util/fault_injection_test_fs.cc b/test_util/fault_injection_test_fs.cc index 6e4bd84ae..619731319 100644 --- a/test_util/fault_injection_test_fs.cc +++ b/test_util/fault_injection_test_fs.cc @@ -17,8 +17,8 @@ #include "test_util/fault_injection_test_fs.h" #include #include +#include "port/lang.h" #include "port/stack_trace.h" -#include "util/util.h" namespace ROCKSDB_NAMESPACE { diff --git a/test_util/transaction_test_util.cc b/test_util/transaction_test_util.cc index 736707595..a9410f5fc 100644 --- a/test_util/transaction_test_util.cc +++ b/test_util/transaction_test_util.cc @@ -139,7 +139,7 @@ bool RandomTransactionInserter::DoInsert(DB* db, Transaction* txn, std::vector set_vec(num_sets_); std::iota(set_vec.begin(), set_vec.end(), static_cast(0)); - std::shuffle(set_vec.begin(), set_vec.end(), std::random_device{}); + RandomShuffle(set_vec.begin(), set_vec.end()); // For each set, pick a key at random and increment it for (uint16_t set_i : set_vec) { @@ -296,7 +296,7 @@ Status RandomTransactionInserter::Verify(DB* db, uint16_t num_sets, std::vector set_vec(num_sets); std::iota(set_vec.begin(), set_vec.end(), static_cast(0)); - std::shuffle(set_vec.begin(), set_vec.end(), std::random_device{}); + RandomShuffle(set_vec.begin(), set_vec.end()); // For each set of keys with the same prefix, sum all the values for (uint16_t set_i : set_vec) { diff --git a/third-party/folly/folly/synchronization/Baton.h b/third-party/folly/folly/synchronization/Baton.h index 6a6403def..bf156ec8a 100644 --- a/third-party/folly/folly/synchronization/Baton.h +++ b/third-party/folly/folly/synchronization/Baton.h @@ -249,7 +249,8 @@ class Baton { bool tryWaitSlow( const std::chrono::time_point& deadline, const WaitOptions& opt) noexcept { - switch (detail::spin_pause_until(deadline, opt, [=] { return ready(); })) { + switch ( + detail::spin_pause_until(deadline, opt, [this] { return ready(); })) { case detail::spin_result::success: return true; case detail::spin_result::timeout: @@ -259,7 +260,7 @@ class Baton { } if (!MayBlock) { - switch (detail::spin_yield_until(deadline, [=] { return ready(); })) { + switch (detail::spin_yield_until(deadline, [this] { return ready(); })) { case detail::spin_result::success: return true; case detail::spin_result::timeout: diff --git a/tools/block_cache_analyzer/block_cache_trace_analyzer.cc b/tools/block_cache_analyzer/block_cache_trace_analyzer.cc index a5137a0e2..bffd480b4 100644 --- a/tools/block_cache_analyzer/block_cache_trace_analyzer.cc +++ b/tools/block_cache_analyzer/block_cache_trace_analyzer.cc @@ -578,8 +578,8 @@ void BlockCacheTraceAnalyzer::WriteSkewness( } // Sort in descending order. sort(pairs.begin(), pairs.end(), - [=](const std::pair& a, - const std::pair& b) { + [](const std::pair& a, + const std::pair& b) { return b.second < a.second; }); @@ -652,7 +652,6 @@ void BlockCacheTraceAnalyzer::WriteCorrelationFeaturesToFile( const std::map& label_features, const std::map& label_predictions, uint32_t max_number_of_values) const { - std::default_random_engine rand_engine(static_cast(env_->NowMicros())); for (auto const& label_feature_vectors : label_features) { const Features& past = label_feature_vectors.second; auto it = label_predictions.find(label_feature_vectors.first); @@ -674,7 +673,7 @@ void BlockCacheTraceAnalyzer::WriteCorrelationFeaturesToFile( for (uint32_t i = 0; i < past.num_accesses_since_last_access.size(); i++) { indexes.push_back(i); } - std::shuffle(indexes.begin(), indexes.end(), rand_engine); + RandomShuffle(indexes.begin(), indexes.end()); for (uint32_t i = 0; i < max_number_of_values && i < indexes.size(); i++) { uint32_t rand_index = indexes[i]; out << std::to_string(past.num_accesses_since_last_access[rand_index]) diff --git a/tools/db_bench_tool.cc b/tools/db_bench_tool.cc index e7e71d118..8a1d09b35 100644 --- a/tools/db_bench_tool.cc +++ b/tools/db_bench_tool.cc @@ -4313,9 +4313,8 @@ class Benchmark { for (uint64_t i = 0; i < num_; ++i) { values_[i] = i; } - std::shuffle( - values_.begin(), values_.end(), - std::default_random_engine(static_cast(FLAGS_seed))); + RandomShuffle(values_.begin(), values_.end(), + static_cast(FLAGS_seed)); } } diff --git a/util/crc32c.cc b/util/crc32c.cc index 6f729d58c..a709e9b1c 100644 --- a/util/crc32c.cc +++ b/util/crc32c.cc @@ -15,8 +15,8 @@ #include #include #endif +#include "port/lang.h" #include "util/coding.h" -#include "util/util.h" #include "util/crc32c_arm64.h" diff --git a/util/hash.cc b/util/hash.cc index d72be8a45..452216fa3 100644 --- a/util/hash.cc +++ b/util/hash.cc @@ -7,10 +7,10 @@ // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. See the AUTHORS file for names of contributors. +#include "util/hash.h" #include +#include "port/lang.h" #include "util/coding.h" -#include "util/hash.h" -#include "util/util.h" #include "util/xxhash.h" namespace ROCKSDB_NAMESPACE { diff --git a/util/murmurhash.cc b/util/murmurhash.cc index 3b759c5e6..9ec4aa633 100644 --- a/util/murmurhash.cc +++ b/util/murmurhash.cc @@ -10,7 +10,7 @@ is under the MIT license. */ #include "murmurhash.h" -#include "util/util.h" +#include "port/lang.h" #if defined(__x86_64__) diff --git a/util/random.h b/util/random.h index f97b2126f..246d6c2ff 100644 --- a/util/random.h +++ b/util/random.h @@ -9,6 +9,7 @@ #pragma once #include +#include #include #include "rocksdb/rocksdb_namespace.h" @@ -163,4 +164,17 @@ class Random64 { } }; +// A seeded replacement for removed std::random_shuffle +template +void RandomShuffle(RandomIt first, RandomIt last, uint32_t seed) { + std::mt19937 rng(seed); + std::shuffle(first, last, rng); +} + +// A replacement for removed std::random_shuffle +template +void RandomShuffle(RandomIt first, RandomIt last) { + RandomShuffle(first, last, std::random_device{}()); +} + } // namespace ROCKSDB_NAMESPACE diff --git a/util/xxhash.cc b/util/xxhash.cc index 6620ae8b6..d1b1395ab 100644 --- a/util/xxhash.cc +++ b/util/xxhash.cc @@ -126,7 +126,7 @@ static void* XXH_memcpy(void* dest, const void* src, size_t size) { return memcp #include "xxhash.h" /* BEGIN RocksDB customizations */ -#include "util/util.h" /* for FALLTHROUGH_INTENDED, inserted as appropriate */ +#include "port/lang.h" /* for FALLTHROUGH_INTENDED, inserted as appropriate */ /* END RocksDB customizations */ /* ************************************* diff --git a/utilities/blob_db/blob_db_impl.cc b/utilities/blob_db/blob_db_impl.cc index ea8710727..0647d831d 100644 --- a/utilities/blob_db/blob_db_impl.cc +++ b/utilities/blob_db/blob_db_impl.cc @@ -622,7 +622,7 @@ void BlobDBImpl::MarkUnreferencedBlobFilesObsolete() { const SequenceNumber obsolete_seq = GetLatestSequenceNumber(); MarkUnreferencedBlobFilesObsoleteImpl( - [=](const std::shared_ptr& blob_file) { + [this, obsolete_seq](const std::shared_ptr& blob_file) { WriteLock file_lock(&blob_file->mutex_); return MarkBlobFileObsoleteIfNeeded(blob_file, obsolete_seq); }); @@ -630,7 +630,7 @@ void BlobDBImpl::MarkUnreferencedBlobFilesObsolete() { void BlobDBImpl::MarkUnreferencedBlobFilesObsoleteDuringOpen() { MarkUnreferencedBlobFilesObsoleteImpl( - [=](const std::shared_ptr& blob_file) { + [this](const std::shared_ptr& blob_file) { return MarkBlobFileObsoleteIfNeeded(blob_file, /* obsolete_seq */ 0); }); } diff --git a/utilities/transactions/write_unprepared_transaction_test.cc b/utilities/transactions/write_unprepared_transaction_test.cc index 4ecbfd14f..1d21ae36e 100644 --- a/utilities/transactions/write_unprepared_transaction_test.cc +++ b/utilities/transactions/write_unprepared_transaction_test.cc @@ -147,9 +147,6 @@ TEST_P(WriteUnpreparedStressTest, ReadYourOwnWriteStress) { const uint32_t kNumThreads = 10; const uint32_t kNumKeys = 5; - std::default_random_engine rand(static_cast( - std::hash()(std::this_thread::get_id()))); - // Test with // 1. no snapshots set // 2. snapshot set on ReadOptions @@ -164,7 +161,7 @@ TEST_P(WriteUnpreparedStressTest, ReadYourOwnWriteStress) { for (uint32_t k = 0; k < kNumKeys * kNumThreads; k++) { keys.push_back("k" + ToString(k)); } - std::shuffle(keys.begin(), keys.end(), rand); + RandomShuffle(keys.begin(), keys.end()); // This counter will act as a "sequence number" to help us validate // visibility logic with snapshots. If we had direct access to the seqno of