2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-15 23:03:42 +00:00
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
2013-10-16 21:59:46 +00:00
|
|
|
//
|
2015-07-01 23:13:49 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-01-25 22:30:32 +00:00
|
|
|
#include <cstdint>
|
2014-04-10 04:17:14 +00:00
|
|
|
#include <sstream>
|
|
|
|
#include <string>
|
2017-04-06 21:49:13 +00:00
|
|
|
#include <unordered_map>
|
2014-04-10 04:17:14 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
#include "rocksdb/rocksdb_namespace.h"
|
|
|
|
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2013-05-21 18:37:06 +00:00
|
|
|
|
2017-04-06 21:49:13 +00:00
|
|
|
class Slice;
|
|
|
|
|
2014-11-21 16:05:28 +00:00
|
|
|
extern std::vector<std::string> StringSplit(const std::string& arg, char delim);
|
2013-05-21 18:37:06 +00:00
|
|
|
|
2017-04-06 21:49:13 +00:00
|
|
|
// Append a human-readable printout of "num" to *str
|
|
|
|
extern void AppendNumberTo(std::string* str, uint64_t num);
|
|
|
|
|
|
|
|
// Append a human-readable printout of "value" to *str.
|
|
|
|
// Escapes any non-printable characters found in "value".
|
|
|
|
extern void AppendEscapedStringTo(std::string* str, const Slice& value);
|
|
|
|
|
Built-in support for generating unique IDs, bug fix (#8708)
Summary:
Env::GenerateUniqueId() works fine on Windows and on POSIX
where /proc/sys/kernel/random/uuid exists. Our other implementation is
flawed and easily produces collision in a new multi-threaded test.
As we rely more heavily on DB session ID uniqueness, this becomes a
serious issue.
This change combines several individually suitable entropy sources
for reliable generation of random unique IDs, with goal of uniqueness
and portability, not cryptographic strength nor maximum speed.
Specifically:
* Moves code for getting UUIDs from the OS to port::GenerateRfcUuid
rather than in Env implementation details. Callers are now told whether
the operation fails or succeeds.
* Adds an internal API GenerateRawUniqueId for generating high-quality
128-bit unique identifiers, by combining entropy from three "tracks":
* Lots of info from default Env like time, process id, and hostname.
* std::random_device
* port::GenerateRfcUuid (when working)
* Built-in implementations of Env::GenerateUniqueId() will now always
produce an RFC 4122 UUID string, either from platform-specific API or
by converting the output of GenerateRawUniqueId.
DB session IDs now use GenerateRawUniqueId while DB IDs (not as
critical) try to use port::GenerateRfcUuid but fall back on
GenerateRawUniqueId with conversion to an RFC 4122 UUID.
GenerateRawUniqueId is declared and defined under env/ rather than util/
or even port/ because of the Env dependency.
Likely follow-up: enhance GenerateRawUniqueId to be faster after the
first call and to guarantee uniqueness within the lifetime of a single
process (imparting the same property onto DB session IDs).
Pull Request resolved: https://github.com/facebook/rocksdb/pull/8708
Test Plan:
A new mini-stress test in env_test checks the various public
and internal APIs for uniqueness, including each track of
GenerateRawUniqueId individually. We can't hope to verify anywhere close
to 128 bits of entropy, but it can at least detect flaws as bad as the
old code. Serial execution of the new tests takes about 350 ms on
my machine.
Reviewed By: zhichao-cao, mrambacher
Differential Revision: D30563780
Pulled By: pdillinger
fbshipit-source-id: de4c9ff4b2f581cf784fcedb5f39f16e5185c364
2021-08-30 22:19:39 +00:00
|
|
|
// Put n digits from v in base kBase to (*buf)[0] to (*buf)[n-1] and
|
|
|
|
// advance *buf to the position after what was written.
|
|
|
|
template <size_t kBase>
|
|
|
|
inline void PutBaseChars(char** buf, size_t n, uint64_t v, bool uppercase) {
|
|
|
|
const char* digitChars = uppercase ? "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
: "0123456789abcdefghijklmnopqrstuvwxyz";
|
|
|
|
for (size_t i = n; i > 0; --i) {
|
Experimental support for SST unique IDs (#8990)
Summary:
* New public header unique_id.h and function GetUniqueIdFromTableProperties
which computes a universally unique identifier based on table properties
of table files from recent RocksDB versions.
* Generation of DB session IDs is refactored so that they are
guaranteed unique in the lifetime of a process running RocksDB.
(SemiStructuredUniqueIdGen, new test included.) Along with file numbers,
this enables SST unique IDs to be guaranteed unique among SSTs generated
in a single process, and "better than random" between processes.
See https://github.com/pdillinger/unique_id
* In addition to public API producing 'external' unique IDs, there is a function
for producing 'internal' unique IDs, with functions for converting between the
two. In short, the external ID is "safe" for things people might do with it, and
the internal ID enables more "power user" features for the future. Specifically,
the external ID goes through a hashing layer so that any subset of bits in the
external ID can be used as a hash of the full ID, while also preserving
uniqueness guarantees in the first 128 bits (bijective both on first 128 bits
and on full 192 bits).
Intended follow-up:
* Use the internal unique IDs in cache keys. (Avoid conflicts with https://github.com/facebook/rocksdb/issues/8912) (The file offset can be XORed into
the third 64-bit value of the unique ID.)
* Publish the external unique IDs in FileStorageInfo (https://github.com/facebook/rocksdb/issues/8968)
Pull Request resolved: https://github.com/facebook/rocksdb/pull/8990
Test Plan:
Unit tests added, and checking of unique ids in stress test.
NOTE in stress test we do not generate nearly enough files to thoroughly
stress uniqueness, but the test trims off pieces of the ID to check for
uniqueness so that we can infer (with some assumptions) stronger
properties in the aggregate.
Reviewed By: zhichao-cao, mrambacher
Differential Revision: D31582865
Pulled By: pdillinger
fbshipit-source-id: 1f620c4c86af9abe2a8d177b9ccf2ad2b9f48243
2021-10-19 06:28:28 +00:00
|
|
|
(*buf)[i - 1] = digitChars[static_cast<size_t>(v % kBase)];
|
Built-in support for generating unique IDs, bug fix (#8708)
Summary:
Env::GenerateUniqueId() works fine on Windows and on POSIX
where /proc/sys/kernel/random/uuid exists. Our other implementation is
flawed and easily produces collision in a new multi-threaded test.
As we rely more heavily on DB session ID uniqueness, this becomes a
serious issue.
This change combines several individually suitable entropy sources
for reliable generation of random unique IDs, with goal of uniqueness
and portability, not cryptographic strength nor maximum speed.
Specifically:
* Moves code for getting UUIDs from the OS to port::GenerateRfcUuid
rather than in Env implementation details. Callers are now told whether
the operation fails or succeeds.
* Adds an internal API GenerateRawUniqueId for generating high-quality
128-bit unique identifiers, by combining entropy from three "tracks":
* Lots of info from default Env like time, process id, and hostname.
* std::random_device
* port::GenerateRfcUuid (when working)
* Built-in implementations of Env::GenerateUniqueId() will now always
produce an RFC 4122 UUID string, either from platform-specific API or
by converting the output of GenerateRawUniqueId.
DB session IDs now use GenerateRawUniqueId while DB IDs (not as
critical) try to use port::GenerateRfcUuid but fall back on
GenerateRawUniqueId with conversion to an RFC 4122 UUID.
GenerateRawUniqueId is declared and defined under env/ rather than util/
or even port/ because of the Env dependency.
Likely follow-up: enhance GenerateRawUniqueId to be faster after the
first call and to guarantee uniqueness within the lifetime of a single
process (imparting the same property onto DB session IDs).
Pull Request resolved: https://github.com/facebook/rocksdb/pull/8708
Test Plan:
A new mini-stress test in env_test checks the various public
and internal APIs for uniqueness, including each track of
GenerateRawUniqueId individually. We can't hope to verify anywhere close
to 128 bits of entropy, but it can at least detect flaws as bad as the
old code. Serial execution of the new tests takes about 350 ms on
my machine.
Reviewed By: zhichao-cao, mrambacher
Differential Revision: D30563780
Pulled By: pdillinger
fbshipit-source-id: de4c9ff4b2f581cf784fcedb5f39f16e5185c364
2021-08-30 22:19:39 +00:00
|
|
|
v /= kBase;
|
|
|
|
}
|
|
|
|
*buf += n;
|
|
|
|
}
|
|
|
|
|
Experimental support for SST unique IDs (#8990)
Summary:
* New public header unique_id.h and function GetUniqueIdFromTableProperties
which computes a universally unique identifier based on table properties
of table files from recent RocksDB versions.
* Generation of DB session IDs is refactored so that they are
guaranteed unique in the lifetime of a process running RocksDB.
(SemiStructuredUniqueIdGen, new test included.) Along with file numbers,
this enables SST unique IDs to be guaranteed unique among SSTs generated
in a single process, and "better than random" between processes.
See https://github.com/pdillinger/unique_id
* In addition to public API producing 'external' unique IDs, there is a function
for producing 'internal' unique IDs, with functions for converting between the
two. In short, the external ID is "safe" for things people might do with it, and
the internal ID enables more "power user" features for the future. Specifically,
the external ID goes through a hashing layer so that any subset of bits in the
external ID can be used as a hash of the full ID, while also preserving
uniqueness guarantees in the first 128 bits (bijective both on first 128 bits
and on full 192 bits).
Intended follow-up:
* Use the internal unique IDs in cache keys. (Avoid conflicts with https://github.com/facebook/rocksdb/issues/8912) (The file offset can be XORed into
the third 64-bit value of the unique ID.)
* Publish the external unique IDs in FileStorageInfo (https://github.com/facebook/rocksdb/issues/8968)
Pull Request resolved: https://github.com/facebook/rocksdb/pull/8990
Test Plan:
Unit tests added, and checking of unique ids in stress test.
NOTE in stress test we do not generate nearly enough files to thoroughly
stress uniqueness, but the test trims off pieces of the ID to check for
uniqueness so that we can infer (with some assumptions) stronger
properties in the aggregate.
Reviewed By: zhichao-cao, mrambacher
Differential Revision: D31582865
Pulled By: pdillinger
fbshipit-source-id: 1f620c4c86af9abe2a8d177b9ccf2ad2b9f48243
2021-10-19 06:28:28 +00:00
|
|
|
// Parse n digits from *buf in base kBase to *v and advance *buf to the
|
|
|
|
// position after what was read. On success, true is returned. On failure,
|
|
|
|
// false is returned, *buf is placed at the first bad character, and *v
|
|
|
|
// contains the partial parsed data. Overflow is not checked but the
|
|
|
|
// result is accurate mod 2^64. Requires the starting value of *v to be
|
|
|
|
// zero or previously accumulated parsed digits, i.e.
|
|
|
|
// ParseBaseChars(&b, n, &v);
|
|
|
|
// is equivalent to n calls to
|
|
|
|
// ParseBaseChars(&b, 1, &v);
|
|
|
|
template <int kBase>
|
|
|
|
inline bool ParseBaseChars(const char** buf, size_t n, uint64_t* v) {
|
|
|
|
while (n) {
|
|
|
|
char c = **buf;
|
|
|
|
*v *= static_cast<uint64_t>(kBase);
|
|
|
|
if (c >= '0' && (kBase >= 10 ? c <= '9' : c < '0' + kBase)) {
|
|
|
|
*v += static_cast<uint64_t>(c - '0');
|
|
|
|
} else if (kBase > 10 && c >= 'A' && c < 'A' + kBase - 10) {
|
|
|
|
*v += static_cast<uint64_t>(c - 'A' + 10);
|
|
|
|
} else if (kBase > 10 && c >= 'a' && c < 'a' + kBase - 10) {
|
|
|
|
*v += static_cast<uint64_t>(c - 'a' + 10);
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
--n;
|
|
|
|
++*buf;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-04-06 21:49:13 +00:00
|
|
|
// Return a human-readable version of num.
|
|
|
|
// for num >= 10.000, prints "xxK"
|
|
|
|
// for num >= 10.000.000, prints "xxM"
|
|
|
|
// for num >= 10.000.000.000, prints "xxG"
|
|
|
|
extern std::string NumberToHumanString(int64_t num);
|
|
|
|
|
|
|
|
// Return a human-readable version of bytes
|
|
|
|
// ex: 1048576 -> 1.00 GB
|
|
|
|
extern std::string BytesToHumanString(uint64_t bytes);
|
|
|
|
|
2019-07-04 02:03:29 +00:00
|
|
|
// Return a human-readable version of unix time
|
|
|
|
// ex: 1562116015 -> "Tue Jul 2 18:06:55 2019"
|
|
|
|
extern std::string TimeToHumanString(int unixtime);
|
|
|
|
|
2017-04-06 21:49:13 +00:00
|
|
|
// Append a human-readable time in micros.
|
|
|
|
int AppendHumanMicros(uint64_t micros, char* output, int len,
|
|
|
|
bool fixed_format);
|
|
|
|
|
|
|
|
// Append a human-readable size in bytes
|
|
|
|
int AppendHumanBytes(uint64_t bytes, char* output, int len);
|
|
|
|
|
|
|
|
// Return a human-readable version of "value".
|
|
|
|
// Escapes any non-printable characters found in "value".
|
|
|
|
extern std::string EscapeString(const Slice& value);
|
|
|
|
|
|
|
|
// Parse a human-readable number from "*in" into *value. On success,
|
|
|
|
// advances "*in" past the consumed number and sets "*val" to the
|
|
|
|
// numeric value. Otherwise, returns false and leaves *in in an
|
|
|
|
// unspecified state.
|
|
|
|
extern bool ConsumeDecimalNumber(Slice* in, uint64_t* val);
|
|
|
|
|
|
|
|
// Returns true if the input char "c" is considered as a special character
|
|
|
|
// that will be escaped when EscapeOptionString() is called.
|
|
|
|
//
|
|
|
|
// @param c the input char
|
|
|
|
// @return true if the input char "c" is considered as a special character.
|
|
|
|
// @see EscapeOptionString
|
|
|
|
bool isSpecialChar(const char c);
|
|
|
|
|
|
|
|
// If the input char is an escaped char, it will return the its
|
|
|
|
// associated raw-char. Otherwise, the function will simply return
|
|
|
|
// the original input char.
|
|
|
|
char UnescapeChar(const char c);
|
|
|
|
|
|
|
|
// If the input char is a control char, it will return the its
|
|
|
|
// associated escaped char. Otherwise, the function will simply return
|
|
|
|
// the original input char.
|
|
|
|
char EscapeChar(const char c);
|
|
|
|
|
|
|
|
// Converts a raw string to an escaped string. Escaped-characters are
|
|
|
|
// defined via the isSpecialChar() function. When a char in the input
|
|
|
|
// string "raw_string" is classified as a special characters, then it
|
|
|
|
// will be prefixed by '\' in the output.
|
|
|
|
//
|
|
|
|
// It's inverse function is UnescapeOptionString().
|
|
|
|
// @param raw_string the input string
|
|
|
|
// @return the '\' escaped string of the input "raw_string"
|
|
|
|
// @see isSpecialChar, UnescapeOptionString
|
|
|
|
std::string EscapeOptionString(const std::string& raw_string);
|
|
|
|
|
|
|
|
// The inverse function of EscapeOptionString. It converts
|
|
|
|
// an '\' escaped string back to a raw string.
|
|
|
|
//
|
|
|
|
// @param escaped_string the input '\' escaped string
|
|
|
|
// @return the raw string of the input "escaped_string"
|
|
|
|
std::string UnescapeOptionString(const std::string& escaped_string);
|
|
|
|
|
|
|
|
std::string trim(const std::string& str);
|
|
|
|
|
2020-05-21 17:56:40 +00:00
|
|
|
// Returns true if "string" ends with "pattern"
|
|
|
|
bool EndsWith(const std::string& string, const std::string& pattern);
|
|
|
|
|
|
|
|
// Returns true if "string" starts with "pattern"
|
|
|
|
bool StartsWith(const std::string& string, const std::string& pattern);
|
|
|
|
|
2017-04-06 21:49:13 +00:00
|
|
|
bool ParseBoolean(const std::string& type, const std::string& value);
|
|
|
|
|
2021-05-20 04:40:43 +00:00
|
|
|
uint8_t ParseUint8(const std::string& value);
|
|
|
|
|
2017-04-06 21:49:13 +00:00
|
|
|
uint32_t ParseUint32(const std::string& value);
|
2019-03-12 20:46:12 +00:00
|
|
|
|
|
|
|
int32_t ParseInt32(const std::string& value);
|
2017-04-06 21:49:13 +00:00
|
|
|
|
|
|
|
uint64_t ParseUint64(const std::string& value);
|
|
|
|
|
|
|
|
int ParseInt(const std::string& value);
|
|
|
|
|
2019-03-12 20:46:12 +00:00
|
|
|
int64_t ParseInt64(const std::string& value);
|
|
|
|
|
2017-04-06 21:49:13 +00:00
|
|
|
double ParseDouble(const std::string& value);
|
|
|
|
|
|
|
|
size_t ParseSizeT(const std::string& value);
|
|
|
|
|
|
|
|
std::vector<int> ParseVectorInt(const std::string& value);
|
|
|
|
|
|
|
|
bool SerializeIntVector(const std::vector<int>& vec, std::string* value);
|
|
|
|
|
|
|
|
extern const std::string kNullptrString;
|
|
|
|
|
2021-03-25 06:06:31 +00:00
|
|
|
// errnoStr() function returns a string that describes the error code passed in
|
|
|
|
// the argument err
|
|
|
|
extern std::string errnoStr(int err);
|
|
|
|
|
2020-02-20 20:07:53 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|