1 //  Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2 //  This source code is licensed under both the GPLv2 (found in the
3 //  COPYING file in the root directory) and Apache 2.0 License
4 //  (found in the LICENSE.Apache file in the root directory).
5 //
6 // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style license that can be
8 // found in the LICENSE file. See the AUTHORS file for names of contributors.
9 //
10 // The test uses an array to compare against values written to the database.
11 // Keys written to the array are in 1:1 correspondence to the actual values in
12 // the database according to the formula in the function GenerateValue.
13 
14 // Space is reserved in the array from 0 to FLAGS_max_key and values are
15 // randomly written/deleted/read from those positions. During verification we
16 // compare all the positions in the array. To shorten/elongate the running
17 // time, you could change the settings: FLAGS_max_key, FLAGS_ops_per_thread,
18 // (sometimes also FLAGS_threads).
19 //
20 // NOTE that if FLAGS_test_batches_snapshots is set, the test will have
21 // different behavior. See comment of the flag for details.
22 
23 #ifdef GFLAGS
24 #pragma once
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 
30 #include <algorithm>
31 #include <array>
32 #include <chrono>
33 #include <cinttypes>
34 #include <exception>
35 #include <queue>
36 #include <thread>
37 
38 #include "db/db_impl/db_impl.h"
39 #include "db/version_set.h"
40 #include "db_stress_tool/db_stress_env_wrapper.h"
41 #include "db_stress_tool/db_stress_listener.h"
42 #include "db_stress_tool/db_stress_shared_state.h"
43 #include "db_stress_tool/db_stress_test_base.h"
44 #include "hdfs/env_hdfs.h"
45 #include "logging/logging.h"
46 #include "monitoring/histogram.h"
47 #include "options/options_helper.h"
48 #include "port/port.h"
49 #include "rocksdb/cache.h"
50 #include "rocksdb/env.h"
51 #include "rocksdb/slice.h"
52 #include "rocksdb/slice_transform.h"
53 #include "rocksdb/statistics.h"
54 #include "rocksdb/utilities/backupable_db.h"
55 #include "rocksdb/utilities/checkpoint.h"
56 #include "rocksdb/utilities/db_ttl.h"
57 #include "rocksdb/utilities/debug.h"
58 #include "rocksdb/utilities/options_util.h"
59 #include "rocksdb/utilities/transaction.h"
60 #include "rocksdb/utilities/transaction_db.h"
61 #include "rocksdb/write_batch.h"
62 #include "test_util/testutil.h"
63 #include "util/coding.h"
64 #include "util/compression.h"
65 #include "util/crc32c.h"
66 #include "util/gflags_compat.h"
67 #include "util/mutexlock.h"
68 #include "util/random.h"
69 #include "util/string_util.h"
70 #include "utilities/blob_db/blob_db.h"
71 #include "utilities/merge_operators.h"
72 
73 using GFLAGS_NAMESPACE::ParseCommandLineFlags;
74 using GFLAGS_NAMESPACE::RegisterFlagValidator;
75 using GFLAGS_NAMESPACE::SetUsageMessage;
76 
77 DECLARE_uint64(seed);
78 DECLARE_bool(read_only);
79 DECLARE_int64(max_key);
80 DECLARE_double(hot_key_alpha);
81 DECLARE_int32(max_key_len);
82 DECLARE_string(key_len_percent_dist);
83 DECLARE_int32(key_window_scale_factor);
84 DECLARE_int32(column_families);
85 DECLARE_string(options_file);
86 DECLARE_int64(active_width);
87 DECLARE_bool(test_batches_snapshots);
88 DECLARE_bool(atomic_flush);
89 DECLARE_bool(test_cf_consistency);
90 DECLARE_int32(threads);
91 DECLARE_int32(ttl);
92 DECLARE_int32(value_size_mult);
93 DECLARE_int32(compaction_readahead_size);
94 DECLARE_bool(enable_pipelined_write);
95 DECLARE_bool(verify_before_write);
96 DECLARE_bool(histogram);
97 DECLARE_bool(destroy_db_initially);
98 DECLARE_bool(verbose);
99 DECLARE_bool(progress_reports);
100 DECLARE_uint64(db_write_buffer_size);
101 DECLARE_int32(write_buffer_size);
102 DECLARE_int32(max_write_buffer_number);
103 DECLARE_int32(min_write_buffer_number_to_merge);
104 DECLARE_int32(max_write_buffer_number_to_maintain);
105 DECLARE_int64(max_write_buffer_size_to_maintain);
106 DECLARE_double(memtable_prefix_bloom_size_ratio);
107 DECLARE_bool(memtable_whole_key_filtering);
108 DECLARE_int32(open_files);
109 DECLARE_int64(compressed_cache_size);
110 DECLARE_int32(compaction_style);
111 DECLARE_int32(num_levels);
112 DECLARE_int32(level0_file_num_compaction_trigger);
113 DECLARE_int32(level0_slowdown_writes_trigger);
114 DECLARE_int32(level0_stop_writes_trigger);
115 DECLARE_int32(block_size);
116 DECLARE_int32(format_version);
117 DECLARE_int32(index_block_restart_interval);
118 DECLARE_int32(max_background_compactions);
119 DECLARE_int32(num_bottom_pri_threads);
120 DECLARE_int32(compaction_thread_pool_adjust_interval);
121 DECLARE_int32(compaction_thread_pool_variations);
122 DECLARE_int32(max_background_flushes);
123 DECLARE_int32(universal_size_ratio);
124 DECLARE_int32(universal_min_merge_width);
125 DECLARE_int32(universal_max_merge_width);
126 DECLARE_int32(universal_max_size_amplification_percent);
127 DECLARE_int32(clear_column_family_one_in);
128 DECLARE_int32(get_live_files_one_in);
129 DECLARE_int32(get_sorted_wal_files_one_in);
130 DECLARE_int32(get_current_wal_file_one_in);
131 DECLARE_int32(set_options_one_in);
132 DECLARE_int32(set_in_place_one_in);
133 DECLARE_int64(cache_size);
134 DECLARE_int32(cache_numshardbits);
135 DECLARE_bool(cache_index_and_filter_blocks);
136 DECLARE_int32(top_level_index_pinning);
137 DECLARE_int32(partition_pinning);
138 DECLARE_int32(unpartitioned_pinning);
139 DECLARE_bool(use_clock_cache);
140 DECLARE_uint64(subcompactions);
141 DECLARE_uint64(periodic_compaction_seconds);
142 DECLARE_uint64(compaction_ttl);
143 DECLARE_bool(allow_concurrent_memtable_write);
144 DECLARE_double(experimental_mempurge_threshold);
145 DECLARE_bool(enable_write_thread_adaptive_yield);
146 DECLARE_int32(reopen);
147 DECLARE_double(bloom_bits);
148 DECLARE_bool(use_block_based_filter);
149 DECLARE_int32(ribbon_starting_level);
150 DECLARE_bool(partition_filters);
151 DECLARE_bool(optimize_filters_for_memory);
152 DECLARE_int32(index_type);
153 DECLARE_string(db);
154 DECLARE_string(secondaries_base);
155 DECLARE_bool(test_secondary);
156 DECLARE_string(expected_values_dir);
157 DECLARE_bool(verify_checksum);
158 DECLARE_bool(mmap_read);
159 DECLARE_bool(mmap_write);
160 DECLARE_bool(use_direct_reads);
161 DECLARE_bool(use_direct_io_for_flush_and_compaction);
162 DECLARE_bool(mock_direct_io);
163 DECLARE_bool(statistics);
164 DECLARE_bool(sync);
165 DECLARE_bool(use_fsync);
166 DECLARE_int32(kill_random_test);
167 DECLARE_string(kill_exclude_prefixes);
168 DECLARE_bool(disable_wal);
169 DECLARE_uint64(recycle_log_file_num);
170 DECLARE_int64(target_file_size_base);
171 DECLARE_int32(target_file_size_multiplier);
172 DECLARE_uint64(max_bytes_for_level_base);
173 DECLARE_double(max_bytes_for_level_multiplier);
174 DECLARE_int32(range_deletion_width);
175 DECLARE_uint64(rate_limiter_bytes_per_sec);
176 DECLARE_bool(rate_limit_bg_reads);
177 DECLARE_uint64(sst_file_manager_bytes_per_sec);
178 DECLARE_uint64(sst_file_manager_bytes_per_truncate);
179 DECLARE_bool(use_txn);
180 DECLARE_uint64(txn_write_policy);
181 DECLARE_bool(unordered_write);
182 DECLARE_int32(backup_one_in);
183 DECLARE_uint64(backup_max_size);
184 DECLARE_int32(checkpoint_one_in);
185 DECLARE_int32(ingest_external_file_one_in);
186 DECLARE_int32(ingest_external_file_width);
187 DECLARE_int32(compact_files_one_in);
188 DECLARE_int32(compact_range_one_in);
189 DECLARE_int32(mark_for_compaction_one_file_in);
190 DECLARE_int32(flush_one_in);
191 DECLARE_int32(pause_background_one_in);
192 DECLARE_int32(compact_range_width);
193 DECLARE_int32(acquire_snapshot_one_in);
194 DECLARE_bool(compare_full_db_state_snapshot);
195 DECLARE_uint64(snapshot_hold_ops);
196 DECLARE_bool(long_running_snapshots);
197 DECLARE_bool(use_multiget);
198 DECLARE_int32(readpercent);
199 DECLARE_int32(prefixpercent);
200 DECLARE_int32(writepercent);
201 DECLARE_int32(delpercent);
202 DECLARE_int32(delrangepercent);
203 DECLARE_int32(nooverwritepercent);
204 DECLARE_int32(iterpercent);
205 DECLARE_uint64(num_iterations);
206 DECLARE_string(compression_type);
207 DECLARE_string(bottommost_compression_type);
208 DECLARE_int32(compression_max_dict_bytes);
209 DECLARE_int32(compression_zstd_max_train_bytes);
210 DECLARE_int32(compression_parallel_threads);
211 DECLARE_uint64(compression_max_dict_buffer_bytes);
212 DECLARE_string(checksum_type);
213 DECLARE_string(hdfs);
214 DECLARE_string(env_uri);
215 DECLARE_string(fs_uri);
216 DECLARE_uint64(ops_per_thread);
217 DECLARE_uint64(log2_keys_per_lock);
218 DECLARE_uint64(max_manifest_file_size);
219 DECLARE_bool(in_place_update);
220 DECLARE_int32(secondary_catch_up_one_in);
221 DECLARE_string(memtablerep);
222 DECLARE_int32(prefix_size);
223 DECLARE_bool(use_merge);
224 DECLARE_bool(use_full_merge_v1);
225 DECLARE_int32(sync_wal_one_in);
226 DECLARE_bool(avoid_unnecessary_blocking_io);
227 DECLARE_bool(write_dbid_to_manifest);
228 DECLARE_bool(avoid_flush_during_recovery);
229 DECLARE_uint64(max_write_batch_group_size_bytes);
230 DECLARE_bool(level_compaction_dynamic_level_bytes);
231 DECLARE_int32(verify_checksum_one_in);
232 DECLARE_int32(verify_db_one_in);
233 DECLARE_int32(continuous_verification_interval);
234 DECLARE_int32(get_property_one_in);
235 DECLARE_string(file_checksum_impl);
236 
237 #ifndef ROCKSDB_LITE
238 // Options for StackableDB-based BlobDB
239 DECLARE_bool(use_blob_db);
240 DECLARE_uint64(blob_db_min_blob_size);
241 DECLARE_uint64(blob_db_bytes_per_sync);
242 DECLARE_uint64(blob_db_file_size);
243 DECLARE_bool(blob_db_enable_gc);
244 DECLARE_double(blob_db_gc_cutoff);
245 #endif  // !ROCKSDB_LITE
246 
247 // Options for integrated BlobDB
248 DECLARE_bool(allow_setting_blob_options_dynamically);
249 DECLARE_bool(enable_blob_files);
250 DECLARE_uint64(min_blob_size);
251 DECLARE_uint64(blob_file_size);
252 DECLARE_string(blob_compression_type);
253 DECLARE_bool(enable_blob_garbage_collection);
254 DECLARE_double(blob_garbage_collection_age_cutoff);
255 DECLARE_double(blob_garbage_collection_force_threshold);
256 
257 DECLARE_int32(approximate_size_one_in);
258 DECLARE_bool(sync_fault_injection);
259 
260 DECLARE_bool(best_efforts_recovery);
261 DECLARE_bool(skip_verifydb);
262 DECLARE_bool(enable_compaction_filter);
263 DECLARE_bool(paranoid_file_checks);
264 DECLARE_bool(fail_if_options_file_error);
265 DECLARE_uint64(batch_protection_bytes_per_key);
266 
267 DECLARE_uint64(user_timestamp_size);
268 DECLARE_string(secondary_cache_uri);
269 
270 constexpr long KB = 1024;
271 constexpr int kRandomValueMaxFactor = 3;
272 constexpr int kValueMaxLen = 100;
273 
274 // wrapped posix or hdfs environment
275 extern ROCKSDB_NAMESPACE::Env* db_stress_env;
276 #ifndef NDEBUG
277 namespace ROCKSDB_NAMESPACE {
278 class FaultInjectionTestFS;
279 }  // namespace ROCKSDB_NAMESPACE
280 extern std::shared_ptr<ROCKSDB_NAMESPACE::FaultInjectionTestFS> fault_fs_guard;
281 #endif
282 
283 extern enum ROCKSDB_NAMESPACE::CompressionType compression_type_e;
284 extern enum ROCKSDB_NAMESPACE::CompressionType bottommost_compression_type_e;
285 extern enum ROCKSDB_NAMESPACE::ChecksumType checksum_type_e;
286 
287 enum RepFactory { kSkipList, kHashSkipList, kVectorRep };
288 
StringToRepFactory(const char * ctype)289 inline enum RepFactory StringToRepFactory(const char* ctype) {
290   assert(ctype);
291 
292   if (!strcasecmp(ctype, "skip_list"))
293     return kSkipList;
294   else if (!strcasecmp(ctype, "prefix_hash"))
295     return kHashSkipList;
296   else if (!strcasecmp(ctype, "vector"))
297     return kVectorRep;
298 
299   fprintf(stdout, "Cannot parse memreptable %s\n", ctype);
300   return kSkipList;
301 }
302 
303 extern enum RepFactory FLAGS_rep_factory;
304 
305 namespace ROCKSDB_NAMESPACE {
StringToCompressionType(const char * ctype)306 inline enum ROCKSDB_NAMESPACE::CompressionType StringToCompressionType(
307     const char* ctype) {
308   assert(ctype);
309 
310   ROCKSDB_NAMESPACE::CompressionType ret_compression_type;
311 
312   if (!strcasecmp(ctype, "disable")) {
313     ret_compression_type = ROCKSDB_NAMESPACE::kDisableCompressionOption;
314   } else if (!strcasecmp(ctype, "none")) {
315     ret_compression_type = ROCKSDB_NAMESPACE::kNoCompression;
316   } else if (!strcasecmp(ctype, "snappy")) {
317     ret_compression_type = ROCKSDB_NAMESPACE::kSnappyCompression;
318   } else if (!strcasecmp(ctype, "zlib")) {
319     ret_compression_type = ROCKSDB_NAMESPACE::kZlibCompression;
320   } else if (!strcasecmp(ctype, "bzip2")) {
321     ret_compression_type = ROCKSDB_NAMESPACE::kBZip2Compression;
322   } else if (!strcasecmp(ctype, "lz4")) {
323     ret_compression_type = ROCKSDB_NAMESPACE::kLZ4Compression;
324   } else if (!strcasecmp(ctype, "lz4hc")) {
325     ret_compression_type = ROCKSDB_NAMESPACE::kLZ4HCCompression;
326   } else if (!strcasecmp(ctype, "xpress")) {
327     ret_compression_type = ROCKSDB_NAMESPACE::kXpressCompression;
328   } else if (!strcasecmp(ctype, "zstd")) {
329     ret_compression_type = ROCKSDB_NAMESPACE::kZSTD;
330   } else {
331     fprintf(stderr, "Cannot parse compression type '%s'\n", ctype);
332     ret_compression_type =
333         ROCKSDB_NAMESPACE::kSnappyCompression;  // default value
334   }
335   if (ret_compression_type != ROCKSDB_NAMESPACE::kDisableCompressionOption &&
336       !CompressionTypeSupported(ret_compression_type)) {
337     // Use no compression will be more portable but considering this is
338     // only a stress test and snappy is widely available. Use snappy here.
339     ret_compression_type = ROCKSDB_NAMESPACE::kSnappyCompression;
340   }
341   return ret_compression_type;
342 }
343 
StringToChecksumType(const char * ctype)344 inline enum ROCKSDB_NAMESPACE::ChecksumType StringToChecksumType(
345     const char* ctype) {
346   assert(ctype);
347   auto iter = ROCKSDB_NAMESPACE::checksum_type_string_map.find(ctype);
348   if (iter != ROCKSDB_NAMESPACE::checksum_type_string_map.end()) {
349     return iter->second;
350   }
351   fprintf(stderr, "Cannot parse checksum type '%s'\n", ctype);
352   return ROCKSDB_NAMESPACE::kCRC32c;
353 }
354 
ChecksumTypeToString(ROCKSDB_NAMESPACE::ChecksumType ctype)355 inline std::string ChecksumTypeToString(ROCKSDB_NAMESPACE::ChecksumType ctype) {
356   auto iter = std::find_if(
357       ROCKSDB_NAMESPACE::checksum_type_string_map.begin(),
358       ROCKSDB_NAMESPACE::checksum_type_string_map.end(),
359       [&](const std::pair<std::string, ROCKSDB_NAMESPACE::ChecksumType>&
360               name_and_enum_val) { return name_and_enum_val.second == ctype; });
361   assert(iter != ROCKSDB_NAMESPACE::checksum_type_string_map.end());
362   return iter->first;
363 }
364 
SplitString(std::string src)365 inline std::vector<std::string> SplitString(std::string src) {
366   std::vector<std::string> ret;
367   if (src.empty()) {
368     return ret;
369   }
370   size_t pos = 0;
371   size_t pos_comma;
372   while ((pos_comma = src.find(',', pos)) != std::string::npos) {
373     ret.push_back(src.substr(pos, pos_comma - pos));
374     pos = pos_comma + 1;
375   }
376   ret.push_back(src.substr(pos, src.length()));
377   return ret;
378 }
379 
380 #ifdef _MSC_VER
381 #pragma warning(push)
382 // truncation of constant value on static_cast
383 #pragma warning(disable : 4309)
384 #endif
GetNextPrefix(const ROCKSDB_NAMESPACE::Slice & src,std::string * v)385 inline bool GetNextPrefix(const ROCKSDB_NAMESPACE::Slice& src, std::string* v) {
386   std::string ret = src.ToString();
387   for (int i = static_cast<int>(ret.size()) - 1; i >= 0; i--) {
388     if (ret[i] != static_cast<char>(255)) {
389       ret[i] = ret[i] + 1;
390       break;
391     } else if (i != 0) {
392       ret[i] = 0;
393     } else {
394       // all FF. No next prefix
395       return false;
396     }
397   }
398   *v = ret;
399   return true;
400 }
401 #ifdef _MSC_VER
402 #pragma warning(pop)
403 #endif
404 
405 // convert long to a big-endian slice key
GetStringFromInt(int64_t val)406 extern inline std::string GetStringFromInt(int64_t val) {
407   std::string little_endian_key;
408   std::string big_endian_key;
409   PutFixed64(&little_endian_key, val);
410   assert(little_endian_key.size() == sizeof(val));
411   big_endian_key.resize(sizeof(val));
412   for (size_t i = 0; i < sizeof(val); ++i) {
413     big_endian_key[i] = little_endian_key[sizeof(val) - 1 - i];
414   }
415   return big_endian_key;
416 }
417 
418 // A struct for maintaining the parameters for generating variable length keys
419 struct KeyGenContext {
420   // Number of adjacent keys in one cycle of key lengths
421   uint64_t window;
422   // Number of keys of each possible length in a given window
423   std::vector<uint64_t> weights;
424 };
425 extern KeyGenContext key_gen_ctx;
426 
427 // Generate a variable length key string from the given int64 val. The
428 // order of the keys is preserved. The key could be anywhere from 8 to
429 // max_key_len * 8 bytes.
430 // The algorithm picks the length based on the
431 // offset of the val within a configured window and the distribution of the
432 // number of keys of various lengths in that window. For example, if x, y, x are
433 // the weights assigned to each possible key length, the keys generated would be
434 // - {0}...{x-1}
435 // {(x-1),0}..{(x-1),(y-1)},{(x-1),(y-1),0}..{(x-1),(y-1),(z-1)} and so on.
436 // Additionally, a trailer of 0-7 bytes could be appended.
Key(int64_t val)437 extern inline std::string Key(int64_t val) {
438   uint64_t window = key_gen_ctx.window;
439   size_t levels = key_gen_ctx.weights.size();
440   std::string key;
441 
442   for (size_t level = 0; level < levels; ++level) {
443     uint64_t weight = key_gen_ctx.weights[level];
444     uint64_t offset = static_cast<uint64_t>(val) % window;
445     uint64_t mult = static_cast<uint64_t>(val) / window;
446     uint64_t pfx = mult * weight + (offset >= weight ? weight - 1 : offset);
447     key.append(GetStringFromInt(pfx));
448     if (offset < weight) {
449       // Use the bottom 3 bits of offset as the number of trailing 'x's in the
450       // key. If the next key is going to be of the next level, then skip the
451       // trailer as it would break ordering. If the key length is already at max,
452       // skip the trailer.
453       if (offset < weight - 1 && level < levels - 1) {
454         size_t trailer_len = offset & 0x7;
455         key.append(trailer_len, 'x');
456       }
457       break;
458     }
459     val = offset - weight;
460     window -= weight;
461   }
462 
463   return key;
464 }
465 
466 // Given a string key, map it to an index into the expected values buffer
GetIntVal(std::string big_endian_key,uint64_t * key_p)467 extern inline bool GetIntVal(std::string big_endian_key, uint64_t* key_p) {
468   size_t size_key = big_endian_key.size();
469   std::vector<uint64_t> prefixes;
470 
471   assert(size_key <= key_gen_ctx.weights.size() * sizeof(uint64_t));
472 
473   std::string little_endian_key;
474   little_endian_key.resize(size_key);
475   for (size_t start = 0; start + sizeof(uint64_t) <= size_key;
476        start += sizeof(uint64_t)) {
477     size_t end = start + sizeof(uint64_t);
478     for (size_t i = 0; i < sizeof(uint64_t); ++i) {
479       little_endian_key[start + i] = big_endian_key[end - 1 - i];
480     }
481     Slice little_endian_slice =
482         Slice(&little_endian_key[start], sizeof(uint64_t));
483     uint64_t pfx;
484     if (!GetFixed64(&little_endian_slice, &pfx)) {
485       return false;
486     }
487     prefixes.emplace_back(pfx);
488   }
489 
490   uint64_t key = 0;
491   for (size_t i = 0; i < prefixes.size(); ++i) {
492     uint64_t pfx = prefixes[i];
493     key += (pfx / key_gen_ctx.weights[i]) * key_gen_ctx.window +
494            pfx % key_gen_ctx.weights[i];
495     if (i < prefixes.size() - 1) {
496       // The encoding writes a `key_gen_ctx.weights[i] - 1` that counts for
497       // `key_gen_ctx.weights[i]` when there are more prefixes to come. So we
498       // need to add back the one here as we're at a non-last prefix.
499       ++key;
500     }
501   }
502   *key_p = key;
503   return true;
504 }
505 
506 // Given a string prefix, map it to the first corresponding index in the
507 // expected values buffer.
GetFirstIntValInPrefix(std::string big_endian_prefix,uint64_t * key_p)508 inline bool GetFirstIntValInPrefix(std::string big_endian_prefix,
509                                    uint64_t* key_p) {
510   size_t size_key = big_endian_prefix.size();
511   // Pad with zeros to make it a multiple of 8. This function may be called
512   // with a prefix, in which case we return the first index that falls
513   // inside or outside that prefix, dependeing on whether the prefix is
514   // the start of upper bound of a scan
515   unsigned int pad = sizeof(uint64_t) - (size_key % sizeof(uint64_t));
516   if (pad < sizeof(uint64_t)) {
517     big_endian_prefix.append(pad, '\0');
518   }
519   return GetIntVal(std::move(big_endian_prefix), key_p);
520 }
521 
GetPrefixKeyCount(const std::string & prefix,const std::string & ub)522 extern inline uint64_t GetPrefixKeyCount(const std::string& prefix,
523                                          const std::string& ub) {
524   uint64_t start = 0;
525   uint64_t end = 0;
526 
527   if (!GetFirstIntValInPrefix(prefix, &start) ||
528       !GetFirstIntValInPrefix(ub, &end)) {
529     return 0;
530   }
531 
532   return end - start;
533 }
534 
StringToHex(const std::string & str)535 extern inline std::string StringToHex(const std::string& str) {
536   std::string result = "0x";
537   result.append(Slice(str).ToString(true));
538   return result;
539 }
540 
541 // Unified output format for double parameters
FormatDoubleParam(double param)542 extern inline std::string FormatDoubleParam(double param) {
543   return std::to_string(param);
544 }
545 
546 // Make sure that double parameter is a value we can reproduce by
547 // re-inputting the value printed.
SanitizeDoubleParam(double * param)548 extern inline void SanitizeDoubleParam(double* param) {
549   *param = std::atof(FormatDoubleParam(*param).c_str());
550 }
551 
552 extern void PoolSizeChangeThread(void* v);
553 
554 extern void DbVerificationThread(void* v);
555 
556 extern void PrintKeyValue(int cf, uint64_t key, const char* value, size_t sz);
557 
558 extern int64_t GenerateOneKey(ThreadState* thread, uint64_t iteration);
559 
560 extern std::vector<int64_t> GenerateNKeys(ThreadState* thread, int num_keys,
561                                           uint64_t iteration);
562 
563 extern size_t GenerateValue(uint32_t rand, char* v, size_t max_sz);
564 
565 extern StressTest* CreateCfConsistencyStressTest();
566 extern StressTest* CreateBatchedOpsStressTest();
567 extern StressTest* CreateNonBatchedOpsStressTest();
568 extern void InitializeHotKeyGenerator(double alpha);
569 extern int64_t GetOneHotKeyID(double rand_seed, int64_t max_key);
570 
571 extern std::string GenerateTimestampForRead();
572 extern std::string NowNanosStr();
573 
574 std::shared_ptr<FileChecksumGenFactory> GetFileChecksumImpl(
575     const std::string& name);
576 }  // namespace ROCKSDB_NAMESPACE
577 #endif  // GFLAGS
578