]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
librbd: avoid broken mingw rng
authorLucian Petrut <lpetrut@cloudbasesolutions.com>
Thu, 2 Apr 2020 10:19:20 +0000 (10:19 +0000)
committerLucian Petrut <lpetrut@cloudbasesolutions.com>
Thu, 22 Oct 2020 12:04:38 +0000 (12:04 +0000)
std::random_device is broken when using Mingw < 9.2 [1].
It always produces the same sequence of numbers, so one critical
implication is that multiple sessions coming from different
processes will use the same identifier.

For now, we'll use boost::random::random_device when building with
Mingw.

[1] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85494

Signed-off-by: Lucian Petrut <lpetrut@cloudbasesolutions.com>
src/include/random.h
src/include/uuid.h
src/librbd/Utils.cc
src/mon/MonClient.cc
src/test/common/CMakeLists.txt
src/tools/osdmaptool.cc

index 0501a878049220bb594c0b6f11c6ce7274128bbc..f2e3e37bcd7520c20a65e2f016b1dfbc929bdcec 100644 (file)
 #include <type_traits>
 #include <boost/optional.hpp>
 
+// Workaround for https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85494
+#ifdef __MINGW32__
+#include <boost/random/random_device.hpp>
+
+using random_device_t = boost::random::random_device;
+#else
+using random_device_t = std::random_device;
+#endif
+
 // Basic random number facility (see N3551 for inspiration):
 namespace ceph::util {
 
@@ -91,7 +100,7 @@ void randomize_rng(const SeedT seed, MutexT& m, EngineT& e)
 template <typename MutexT, typename EngineT>
 void randomize_rng(MutexT& m, EngineT& e)
 {
-  std::random_device rd;
+  random_device_t rd;
  
   std::lock_guard<MutexT> lg(m);
   e.seed(rd());
@@ -107,7 +116,7 @@ void randomize_rng(const SeedT n)
 template <typename EngineT = std::default_random_engine>
 void randomize_rng()
 {
-  std::random_device rd;
+  random_device_t rd;
   detail::engine<EngineT>().seed(rd());
 }
 
@@ -232,7 +241,7 @@ template <typename NumberT>
 class random_number_generator final
 {
   std::mutex l;
-  std::random_device rd;
+  random_device_t rd;
   std::default_random_engine e;
 
   using seed_type = typename decltype(e)::result_type;
index bee66c36fa38f155d9edd50cef0f0c033ee8bac6..091a0c049cb5e0ac3b008e119c500a6a2a6e2b09 100644 (file)
@@ -7,6 +7,7 @@
  */
 
 #include "encoding.h"
+#include "random.h"
 
 #include <ostream>
 #include <random>
@@ -32,7 +33,7 @@ struct uuid_d {
   }
 
   void generate_random() {
-    std::random_device rng;
+    random_device_t rng;
     boost::uuids::basic_random_generator gen(rng);
     uuid = gen();
   }
index 420f73a69bd38027c06366ff74cd1977bc1cd82b..36e8fa353a60585d91c32c03e6e6318a23b0dcee 100644 (file)
@@ -5,6 +5,7 @@
 #include <boost/lexical_cast.hpp>
 
 #include "librbd/Utils.h"
+#include "include/random.h"
 #include "include/rbd_types.h"
 #include "include/stringify.h"
 #include "include/neorados/RADOS.hpp"
@@ -55,7 +56,7 @@ std::string generate_image_id(librados::IoCtx &ioctx) {
   librados::Rados rados(ioctx);
 
   uint64_t bid = rados.get_instance_id();
-  std::mt19937 generator{std::random_device{}()};
+  std::mt19937 generator{random_device_t{}()};
   std::uniform_int_distribution<uint32_t> distribution{0, 0xFFFFFFFF};
   uint32_t extra = distribution(generator);
 
index edff5f509586a58e44e6541430c6048fb80f7dc2..0840c4641a49bcce5b0a79bbf1239d7429f181ef 100644 (file)
@@ -21,6 +21,7 @@
 #include <boost/range/algorithm_ext/copy_n.hpp>
 #include "common/weighted_shuffle.h"
 
+#include "include/random.h"
 #include "include/scope_guard.h"
 #include "include/stringify.h"
 
@@ -792,7 +793,7 @@ void MonClient::_add_conns(uint64_t global_id)
       auto rank_name = monmap.get_name(i);
       weights.push_back(monmap.get_weight(rank_name));
     }
-    std::random_device rd;
+    random_device_t rd;
     if (std::accumulate(begin(weights), end(weights), 0u) == 0) {
       std::shuffle(begin(ranks), end(ranks), std::mt19937{rd()});
     } else {
index 4bf46a00873d09cf67171cd9bd5e53871ce6eeaf..6dd405e8d798c05a053e9247ce9370818d4ec335 100644 (file)
@@ -134,6 +134,7 @@ add_executable(unittest_random
   test_random.cc
   )
 add_ceph_unittest(unittest_random)
+target_link_libraries(unittest_random Boost::random)
 
 # unittest_throttle
 add_executable(unittest_throttle
index 73ad1a535b6f1863ee29e48cc2a27c7a4defab38..afd5fe88d76f232f584fb20580569045ca13be50 100644 (file)
@@ -18,6 +18,7 @@
 #include "common/ceph_argparse.h"
 #include "common/errno.h"
 #include "common/safe_io.h"
+#include "include/random.h"
 #include "mon/health_check.h"
 #include <time.h>
 #include <algorithm>
@@ -423,7 +424,7 @@ int main(int argc, const char **argv)
     int r = clock_gettime(CLOCK_MONOTONIC, &round_start);
     assert(r == 0);
     do {
-      std::random_device rd;
+      random_device_t rd;
       std::shuffle(pools.begin(), pools.end(), std::mt19937{rd()});
       cout << "pools ";
       for (auto& i: pools)