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: 1f620c4c86af9abe2a8d177b9ccf2ad2b9f48243main
parent
aa21896880
commit
ad5325a736
@ -0,0 +1,136 @@ |
|||||||
|
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||||
|
// 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).
|
||||||
|
|
||||||
|
#include "db_stress_tool/db_stress_listener.h" |
||||||
|
|
||||||
|
#include <cstdint> |
||||||
|
|
||||||
|
#include "rocksdb/file_system.h" |
||||||
|
#include "util/coding_lean.h" |
||||||
|
|
||||||
|
namespace ROCKSDB_NAMESPACE { |
||||||
|
|
||||||
|
#ifdef GFLAGS |
||||||
|
#ifndef ROCKSDB_LITE |
||||||
|
|
||||||
|
// TODO: consider using expected_values_dir instead, but this is more
|
||||||
|
// convenient for now.
|
||||||
|
UniqueIdVerifier::UniqueIdVerifier(const std::string& db_name) |
||||||
|
: path_(db_name + "/.unique_ids") { |
||||||
|
// We expect such a small number of files generated during this test
|
||||||
|
// (thousands?), checking full 192-bit IDs for uniqueness is a very
|
||||||
|
// weak check. For a stronger check, we pick a specific 64-bit
|
||||||
|
// subsequence from the ID to check for uniqueness. All bits of the
|
||||||
|
// ID should be high quality, and 64 bits should be unique with
|
||||||
|
// very good probability for the quantities in this test.
|
||||||
|
offset_ = Random::GetTLSInstance()->Uniform(17); // 0 to 16
|
||||||
|
|
||||||
|
// Use default FileSystem to avoid fault injection, etc.
|
||||||
|
FileSystem& fs = *FileSystem::Default(); |
||||||
|
IOOptions opts; |
||||||
|
|
||||||
|
{ |
||||||
|
std::unique_ptr<FSSequentialFile> reader; |
||||||
|
Status s = |
||||||
|
fs.NewSequentialFile(path_, FileOptions(), &reader, /*dbg*/ nullptr); |
||||||
|
if (s.ok()) { |
||||||
|
// Load from file
|
||||||
|
std::string id(24U, '\0'); |
||||||
|
Slice result; |
||||||
|
for (;;) { |
||||||
|
s = reader->Read(id.size(), opts, &result, &id[0], /*dbg*/ nullptr); |
||||||
|
if (!s.ok()) { |
||||||
|
fprintf(stderr, "Error reading unique id file: %s\n", |
||||||
|
s.ToString().c_str()); |
||||||
|
assert(false); |
||||||
|
} |
||||||
|
if (result.size() < id.size()) { |
||||||
|
// EOF
|
||||||
|
if (result.size() != 0) { |
||||||
|
// Corrupt file. Not a DB bug but could happen if OS doesn't provide
|
||||||
|
// good guarantees on process crash.
|
||||||
|
fprintf(stdout, "Warning: clearing corrupt unique id file\n"); |
||||||
|
id_set_.clear(); |
||||||
|
reader.reset(); |
||||||
|
s = fs.DeleteFile(path_, opts, /*dbg*/ nullptr); |
||||||
|
assert(s.ok()); |
||||||
|
} |
||||||
|
break; |
||||||
|
} |
||||||
|
VerifyNoWrite(id); |
||||||
|
} |
||||||
|
} else { |
||||||
|
// Newly created is ok.
|
||||||
|
// But FileSystem doesn't tell us whether non-existence was the cause of
|
||||||
|
// the failure. (Issue #9021)
|
||||||
|
Status s2 = fs.FileExists(path_, opts, /*dbg*/ nullptr); |
||||||
|
if (!s2.IsNotFound()) { |
||||||
|
fprintf(stderr, "Error opening unique id file: %s\n", |
||||||
|
s.ToString().c_str()); |
||||||
|
assert(false); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
fprintf(stdout, "(Re-)verified %zu unique IDs\n", id_set_.size()); |
||||||
|
Status s = fs.ReopenWritableFile(path_, FileOptions(), &data_file_writer_, |
||||||
|
/*dbg*/ nullptr); |
||||||
|
if (!s.ok()) { |
||||||
|
fprintf(stderr, "Error opening unique id file for append: %s\n", |
||||||
|
s.ToString().c_str()); |
||||||
|
assert(false); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
UniqueIdVerifier::~UniqueIdVerifier() { |
||||||
|
data_file_writer_->Close(IOOptions(), /*dbg*/ nullptr); |
||||||
|
} |
||||||
|
|
||||||
|
void UniqueIdVerifier::VerifyNoWrite(const std::string& id) { |
||||||
|
assert(id.size() == 24); |
||||||
|
bool is_new = id_set_.insert(DecodeFixed64(&id[offset_])).second; |
||||||
|
if (!is_new) { |
||||||
|
fprintf(stderr, |
||||||
|
"Duplicate partial unique ID found (offset=%zu, count=%zu)\n", |
||||||
|
offset_, id_set_.size()); |
||||||
|
assert(false); |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
void UniqueIdVerifier::Verify(const std::string& id) { |
||||||
|
assert(id.size() == 24); |
||||||
|
std::lock_guard<std::mutex> lock(mutex_); |
||||||
|
// If we accumulate more than ~4 million IDs, there would be > 1 in 1M
|
||||||
|
// natural chance of collision. Thus, simply stop checking at that point.
|
||||||
|
if (id_set_.size() >= 4294967) { |
||||||
|
return; |
||||||
|
} |
||||||
|
IOStatus s = |
||||||
|
data_file_writer_->Append(Slice(id), IOOptions(), /*dbg*/ nullptr); |
||||||
|
if (!s.ok()) { |
||||||
|
fprintf(stderr, "Error writing to unique id file: %s\n", |
||||||
|
s.ToString().c_str()); |
||||||
|
assert(false); |
||||||
|
} |
||||||
|
s = data_file_writer_->Flush(IOOptions(), /*dbg*/ nullptr); |
||||||
|
if (!s.ok()) { |
||||||
|
fprintf(stderr, "Error flushing unique id file: %s\n", |
||||||
|
s.ToString().c_str()); |
||||||
|
assert(false); |
||||||
|
} |
||||||
|
VerifyNoWrite(id); |
||||||
|
} |
||||||
|
|
||||||
|
void DbStressListener::VerifyTableFileUniqueId( |
||||||
|
const TableProperties& new_file_properties) { |
||||||
|
// Verify unique ID
|
||||||
|
std::string id; |
||||||
|
GetUniqueIdFromTableProperties(new_file_properties, &id); |
||||||
|
unique_ids_.Verify(id); |
||||||
|
} |
||||||
|
|
||||||
|
#endif // !ROCKSDB_LITE
|
||||||
|
#endif // GFLAGS
|
||||||
|
|
||||||
|
} // namespace ROCKSDB_NAMESPACE
|
@ -1,40 +0,0 @@ |
|||||||
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
|
||||||
// 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).
|
|
||||||
|
|
||||||
// This file is for functions that extract novel entropy or sources of
|
|
||||||
// uniqueness from the execution environment. (By contrast, random.h is
|
|
||||||
// for algorithmic pseudorandomness.)
|
|
||||||
//
|
|
||||||
// These functions could eventually migrate to public APIs, such as in Env.
|
|
||||||
|
|
||||||
#pragma once |
|
||||||
|
|
||||||
#include <cstdint> |
|
||||||
|
|
||||||
#include "rocksdb/rocksdb_namespace.h" |
|
||||||
|
|
||||||
namespace ROCKSDB_NAMESPACE { |
|
||||||
|
|
||||||
// Generates a new 128-bit identifier that is universally unique
|
|
||||||
// (with high probability) for each call. The result is split into
|
|
||||||
// two 64-bit pieces. This function has NOT been validated for use in
|
|
||||||
// cryptography.
|
|
||||||
//
|
|
||||||
// This is used in generating DB session IDs and by Env::GenerateUniqueId
|
|
||||||
// (used for DB IDENTITY) if the platform does not provide a generator of
|
|
||||||
// RFC 4122 UUIDs or fails somehow. (Set exclude_port_uuid=true if this
|
|
||||||
// function is used as a fallback for GenerateRfcUuid, because no need
|
|
||||||
// trying it again.)
|
|
||||||
void GenerateRawUniqueId(uint64_t* a, uint64_t* b, |
|
||||||
bool exclude_port_uuid = false); |
|
||||||
|
|
||||||
#ifndef NDEBUG |
|
||||||
// A version of above with options for challenge testing
|
|
||||||
void TEST_GenerateRawUniqueId(uint64_t* a, uint64_t* b, bool exclude_port_uuid, |
|
||||||
bool exclude_env_details, |
|
||||||
bool exclude_random_device); |
|
||||||
#endif |
|
||||||
|
|
||||||
} // namespace ROCKSDB_NAMESPACE
|
|
@ -0,0 +1,69 @@ |
|||||||
|
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||||
|
// 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).
|
||||||
|
|
||||||
|
// This file is for functions that generate unique identifiers by
|
||||||
|
// (at least in part) by extracting novel entropy or sources of uniqueness
|
||||||
|
// from the execution environment. (By contrast, random.h is for algorithmic
|
||||||
|
// pseudorandomness.)
|
||||||
|
//
|
||||||
|
// These functions could eventually migrate to public APIs, such as in Env.
|
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <atomic> |
||||||
|
#include <cstdint> |
||||||
|
|
||||||
|
#include "rocksdb/rocksdb_namespace.h" |
||||||
|
|
||||||
|
namespace ROCKSDB_NAMESPACE { |
||||||
|
|
||||||
|
// Generates a new 128-bit identifier that is universally unique
|
||||||
|
// (with high probability) for each call. The result is split into
|
||||||
|
// two 64-bit pieces. This function has NOT been validated for use in
|
||||||
|
// cryptography.
|
||||||
|
//
|
||||||
|
// This is used in generating DB session IDs and by Env::GenerateUniqueId
|
||||||
|
// (used for DB IDENTITY) if the platform does not provide a generator of
|
||||||
|
// RFC 4122 UUIDs or fails somehow. (Set exclude_port_uuid=true if this
|
||||||
|
// function is used as a fallback for GenerateRfcUuid, because no need
|
||||||
|
// trying it again.)
|
||||||
|
void GenerateRawUniqueId(uint64_t* a, uint64_t* b, |
||||||
|
bool exclude_port_uuid = false); |
||||||
|
|
||||||
|
#ifndef NDEBUG |
||||||
|
// A version of above with options for challenge testing
|
||||||
|
void TEST_GenerateRawUniqueId(uint64_t* a, uint64_t* b, bool exclude_port_uuid, |
||||||
|
bool exclude_env_details, |
||||||
|
bool exclude_random_device); |
||||||
|
#endif |
||||||
|
|
||||||
|
// Generates globally unique ids with lower probability of any collisions
|
||||||
|
// vs. each unique id being independently random (GenerateRawUniqueId).
|
||||||
|
// We call this "semi-structured" because between different
|
||||||
|
// SemiStructuredUniqueIdGen objects, the IDs are separated by random
|
||||||
|
// intervals (unstructured), but within a single SemiStructuredUniqueIdGen
|
||||||
|
// object, the generated IDs are trivially related (structured). See
|
||||||
|
// https://github.com/pdillinger/unique_id for how this improves probability
|
||||||
|
// of no collision. In short, if we have n SemiStructuredUniqueIdGen
|
||||||
|
// objects each generating m IDs, the first collision is expected at
|
||||||
|
// around n = sqrt(2^128 / m), equivalently n * sqrt(m) = 2^64,
|
||||||
|
// rather than n * m = 2^64 for fully random IDs.
|
||||||
|
class SemiStructuredUniqueIdGen { |
||||||
|
public: |
||||||
|
// Initializes with random starting state (from GenerateRawUniqueId)
|
||||||
|
SemiStructuredUniqueIdGen(); |
||||||
|
|
||||||
|
// Assuming no fork(), `lower` is guaranteed unique from one call
|
||||||
|
// to the next (thread safe).
|
||||||
|
void GenerateNext(uint64_t* upper, uint64_t* lower); |
||||||
|
|
||||||
|
private: |
||||||
|
uint64_t base_upper_; |
||||||
|
uint64_t base_lower_; |
||||||
|
std::atomic<uint64_t> counter_; |
||||||
|
int64_t saved_process_id_; |
||||||
|
}; |
||||||
|
|
||||||
|
} // namespace ROCKSDB_NAMESPACE
|
@ -0,0 +1,46 @@ |
|||||||
|
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||||
|
// 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).
|
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
#include "rocksdb/table_properties.h" |
||||||
|
|
||||||
|
namespace ROCKSDB_NAMESPACE { |
||||||
|
|
||||||
|
// EXPERIMENTAL: This API is subject to change
|
||||||
|
//
|
||||||
|
// Computes a stable, universally unique 192-bit (24 binary char) identifier
|
||||||
|
// for an SST file from TableProperties. This is supported for table (SST)
|
||||||
|
// files created with RocksDB 6.24 and later. NotSupported will be returned
|
||||||
|
// for other cases. The first 16 bytes (128 bits) is of sufficient quality
|
||||||
|
// for almost all applications, and shorter prefixes are usable as a
|
||||||
|
// hash of the full unique id.
|
||||||
|
//
|
||||||
|
// Note: .c_str() is not compatible with binary char strings, so using
|
||||||
|
// .c_str() on the result will often result in information loss and very
|
||||||
|
// poor uniqueness probability.
|
||||||
|
//
|
||||||
|
// More detail: the first 128 bits are *guaranteed* unique for SST files
|
||||||
|
// generated in the same process (even different DBs, RocksDB >= 6.26),
|
||||||
|
// and first 128 bits are guaranteed not "all zeros" (RocksDB >= 6.26)
|
||||||
|
// so that the "all zeros" value can be used reliably for a null ID.
|
||||||
|
// Assuming one generates many SST files in the lifetime of each process,
|
||||||
|
// the probability of collision between processes is "better than
|
||||||
|
// random": if processes generate n SST files on average, we expect to
|
||||||
|
// generate roughly 2^64 * sqrt(n) files before first collision in the
|
||||||
|
// first 128 bits. See https://github.com/pdillinger/unique_id
|
||||||
|
// Using the full 192 bits, we expect to generate roughly 2^96 * sqrt(n)
|
||||||
|
// files before first collision.
|
||||||
|
Status GetUniqueIdFromTableProperties(const TableProperties &props, |
||||||
|
std::string *out_id); |
||||||
|
|
||||||
|
// EXPERIMENTAL: This API is subject to change
|
||||||
|
//
|
||||||
|
// Converts a binary string (unique id) to hexadecimal, with each 64 bits
|
||||||
|
// separated by '-', e.g. 6474DF650323BDF0-B48E64F3039308CA-17284B32E7F7444B
|
||||||
|
// Also works on unique id prefix.
|
||||||
|
std::string UniqueIdToHumanString(const std::string &id); |
||||||
|
|
||||||
|
} // namespace ROCKSDB_NAMESPACE
|
@ -0,0 +1,166 @@ |
|||||||
|
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||||
|
// 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).
|
||||||
|
|
||||||
|
#include <cstdint> |
||||||
|
|
||||||
|
#include "table/unique_id_impl.h" |
||||||
|
#include "util/coding_lean.h" |
||||||
|
#include "util/hash.h" |
||||||
|
#include "util/string_util.h" |
||||||
|
|
||||||
|
namespace ROCKSDB_NAMESPACE { |
||||||
|
|
||||||
|
std::string EncodeSessionId(uint64_t upper, uint64_t lower) { |
||||||
|
std::string db_session_id(20U, '\0'); |
||||||
|
char *buf = &db_session_id[0]; |
||||||
|
// Preserving `lower` is slightly tricky. 36^12 is slightly more than
|
||||||
|
// 62 bits, so we use 12 chars plus the bottom two bits of one more.
|
||||||
|
// (A tiny fraction of 20 digit strings go unused.)
|
||||||
|
uint64_t a = (upper << 2) | (lower >> 62); |
||||||
|
uint64_t b = lower & (UINT64_MAX >> 2); |
||||||
|
PutBaseChars<36>(&buf, 8, a, /*uppercase*/ true); |
||||||
|
PutBaseChars<36>(&buf, 12, b, /*uppercase*/ true); |
||||||
|
assert(buf == &db_session_id.back() + 1); |
||||||
|
return db_session_id; |
||||||
|
} |
||||||
|
|
||||||
|
Status DecodeSessionId(const std::string &db_session_id, uint64_t *upper, |
||||||
|
uint64_t *lower) { |
||||||
|
const size_t len = db_session_id.size(); |
||||||
|
if (len == 0) { |
||||||
|
return Status::NotSupported("Missing db_session_id"); |
||||||
|
} |
||||||
|
// Anything from 13 to 24 chars is reasonable. We don't have to limit to
|
||||||
|
// exactly 20.
|
||||||
|
if (len < 13) { |
||||||
|
return Status::NotSupported("Too short db_session_id"); |
||||||
|
} |
||||||
|
if (len > 24) { |
||||||
|
return Status::NotSupported("Too long db_session_id"); |
||||||
|
} |
||||||
|
uint64_t a = 0, b = 0; |
||||||
|
const char *buf = &db_session_id.front(); |
||||||
|
bool success = ParseBaseChars<36>(&buf, len - 12U, &a); |
||||||
|
if (!success) { |
||||||
|
return Status::NotSupported("Bad digit in db_session_id"); |
||||||
|
} |
||||||
|
success = ParseBaseChars<36>(&buf, 12U, &b); |
||||||
|
if (!success) { |
||||||
|
return Status::NotSupported("Bad digit in db_session_id"); |
||||||
|
} |
||||||
|
assert(buf == &db_session_id.back() + 1); |
||||||
|
*upper = a >> 2; |
||||||
|
*lower = (b & (UINT64_MAX >> 2)) | (a << 62); |
||||||
|
return Status::OK(); |
||||||
|
} |
||||||
|
|
||||||
|
Status GetSstInternalUniqueId(const std::string &db_id, |
||||||
|
const std::string &db_session_id, |
||||||
|
uint64_t file_number, UniqueId64x3 *out) { |
||||||
|
if (db_id.empty()) { |
||||||
|
return Status::NotSupported("Missing db_id"); |
||||||
|
} |
||||||
|
if (file_number == 0) { |
||||||
|
return Status::NotSupported("Missing or bad file number"); |
||||||
|
} |
||||||
|
if (db_session_id.empty()) { |
||||||
|
return Status::NotSupported("Missing db_session_id"); |
||||||
|
} |
||||||
|
uint64_t session_upper = 0; // Assignment to appease clang-analyze
|
||||||
|
uint64_t session_lower = 0; // Assignment to appease clang-analyze
|
||||||
|
{ |
||||||
|
Status s = DecodeSessionId(db_session_id, &session_upper, &session_lower); |
||||||
|
if (!s.ok()) { |
||||||
|
return s; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
// Exactly preserve session lower to ensure that session ids generated
|
||||||
|
// during the same process lifetime are guaranteed unique.
|
||||||
|
// DBImpl also guarantees (in recent versions) that this is not zero,
|
||||||
|
// so that we can guarantee unique ID is never all zeros. (Can't assert
|
||||||
|
// that here because of testing and old versions.)
|
||||||
|
// We put this first in anticipation of matching a small-ish set of cache
|
||||||
|
// key prefixes to cover entries relevant to any DB.
|
||||||
|
(*out)[0] = session_lower; |
||||||
|
|
||||||
|
// Hash the session upper (~39 bits entropy) and DB id (120+ bits entropy)
|
||||||
|
// for very high global uniqueness entropy.
|
||||||
|
// (It is possible that many DBs descended from one common DB id are copied
|
||||||
|
// around and proliferate, in which case session id is critical, but it is
|
||||||
|
// more common for different DBs to have different DB ids.)
|
||||||
|
uint64_t db_a, db_b; |
||||||
|
Hash2x64(db_id.data(), db_id.size(), session_upper, &db_a, &db_b); |
||||||
|
|
||||||
|
// Xor in file number for guaranteed uniqueness by file number for a given
|
||||||
|
// session and DB id. (Xor slightly better than + here. See
|
||||||
|
// https://github.com/pdillinger/unique_id )
|
||||||
|
(*out)[1] = db_a ^ file_number; |
||||||
|
|
||||||
|
// Extra (optional) global uniqueness
|
||||||
|
(*out)[2] = db_b; |
||||||
|
|
||||||
|
return Status::OK(); |
||||||
|
} |
||||||
|
|
||||||
|
namespace { |
||||||
|
// For InternalUniqueIdToExternal / ExternalUniqueIdToInternal we want all
|
||||||
|
// zeros in first 128 bits to map to itself, so that excluding zero in
|
||||||
|
// internal IDs (session_lower != 0 above) does the same for external IDs.
|
||||||
|
// These values are meaningless except for making that work.
|
||||||
|
constexpr uint64_t kHiOffsetForZero = 17391078804906429400U; |
||||||
|
constexpr uint64_t kLoOffsetForZero = 6417269962128484497U; |
||||||
|
} // namespace
|
||||||
|
|
||||||
|
void InternalUniqueIdToExternal(UniqueId64x3 *in_out) { |
||||||
|
uint64_t hi, lo; |
||||||
|
BijectiveHash2x64((*in_out)[1] + kHiOffsetForZero, |
||||||
|
(*in_out)[0] + kLoOffsetForZero, &hi, &lo); |
||||||
|
(*in_out)[0] = lo; |
||||||
|
(*in_out)[1] = hi; |
||||||
|
(*in_out)[2] += lo + hi; |
||||||
|
} |
||||||
|
|
||||||
|
void ExternalUniqueIdToInternal(UniqueId64x3 *in_out) { |
||||||
|
uint64_t lo = (*in_out)[0]; |
||||||
|
uint64_t hi = (*in_out)[1]; |
||||||
|
(*in_out)[2] -= lo + hi; |
||||||
|
BijectiveUnhash2x64(hi, lo, &hi, &lo); |
||||||
|
(*in_out)[0] = lo - kLoOffsetForZero; |
||||||
|
(*in_out)[1] = hi - kHiOffsetForZero; |
||||||
|
} |
||||||
|
|
||||||
|
std::string EncodeUniqueIdBytes(const UniqueId64x3 &in) { |
||||||
|
std::string ret(24U, '\0'); |
||||||
|
EncodeFixed64(&ret[0], in[0]); |
||||||
|
EncodeFixed64(&ret[8], in[1]); |
||||||
|
EncodeFixed64(&ret[16], in[2]); |
||||||
|
return ret; |
||||||
|
} |
||||||
|
|
||||||
|
Status GetUniqueIdFromTableProperties(const TableProperties &props, |
||||||
|
std::string *out_id) { |
||||||
|
UniqueId64x3 tmp{}; |
||||||
|
Status s = GetSstInternalUniqueId(props.db_id, props.db_session_id, |
||||||
|
props.orig_file_number, &tmp); |
||||||
|
if (s.ok()) { |
||||||
|
InternalUniqueIdToExternal(&tmp); |
||||||
|
*out_id = EncodeUniqueIdBytes(tmp); |
||||||
|
} else { |
||||||
|
out_id->clear(); |
||||||
|
} |
||||||
|
return s; |
||||||
|
} |
||||||
|
|
||||||
|
std::string UniqueIdToHumanString(const std::string &id) { |
||||||
|
// Not so efficient, but that's OK
|
||||||
|
std::string str = Slice(id).ToString(/*hex*/ true); |
||||||
|
for (size_t i = 16; i < str.size(); i += 17) { |
||||||
|
str.insert(i, "-"); |
||||||
|
} |
||||||
|
return str; |
||||||
|
} |
||||||
|
|
||||||
|
} // namespace ROCKSDB_NAMESPACE
|
@ -0,0 +1,59 @@ |
|||||||
|
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||||
|
// 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).
|
||||||
|
|
||||||
|
#pragma once |
||||||
|
|
||||||
|
#include <array> |
||||||
|
|
||||||
|
#include "rocksdb/unique_id.h" |
||||||
|
|
||||||
|
namespace ROCKSDB_NAMESPACE { |
||||||
|
|
||||||
|
using UniqueId64x3 = std::array<uint64_t, 3>; |
||||||
|
|
||||||
|
// Helper for GetUniqueIdFromTableProperties. This function can also be used
|
||||||
|
// for temporary ids for files without sufficient information in table
|
||||||
|
// properties. The internal unique id is more structured than the public
|
||||||
|
// unique id, so can be manipulated in more ways but very carefully.
|
||||||
|
// These must be long term stable to ensure GetUniqueIdFromTableProperties
|
||||||
|
// is long term stable.
|
||||||
|
Status GetSstInternalUniqueId(const std::string &db_id, |
||||||
|
const std::string &db_session_id, |
||||||
|
uint64_t file_number, UniqueId64x3 *out); |
||||||
|
|
||||||
|
// Helper for GetUniqueIdFromTableProperties. External unique ids go through
|
||||||
|
// this extra hashing layer so that prefixes of the unique id have predictable
|
||||||
|
// "full" entropy. This hashing layer is 1-to-1 on the first 128 bits and on
|
||||||
|
// the full 192 bits.
|
||||||
|
// This transformation must be long term stable to ensure
|
||||||
|
// GetUniqueIdFromTableProperties is long term stable.
|
||||||
|
void InternalUniqueIdToExternal(UniqueId64x3 *in_out); |
||||||
|
|
||||||
|
// Reverse of InternalUniqueIdToExternal mostly for testing purposes
|
||||||
|
// (demonstrably 1-to-1 on the first 128 bits and on the full 192 bits).
|
||||||
|
void ExternalUniqueIdToInternal(UniqueId64x3 *in_out); |
||||||
|
|
||||||
|
// Convert numerical format to byte format for public API
|
||||||
|
std::string EncodeUniqueIdBytes(const UniqueId64x3 &in); |
||||||
|
|
||||||
|
// Reformat a random value down to our "DB session id" format,
|
||||||
|
// which is intended to be compact and friendly for use in file names.
|
||||||
|
// `lower` is fully preserved and data is lost from `upper`.
|
||||||
|
//
|
||||||
|
// Detail: Encoded into 20 chars in base-36 ([0-9A-Z]), which is ~103 bits of
|
||||||
|
// entropy, which is enough to expect no collisions across a billion servers
|
||||||
|
// each opening DBs a million times (~2^50). Benefits vs. RFC-4122 unique id:
|
||||||
|
// * Save ~ dozen bytes per SST file
|
||||||
|
// * Shorter shared backup file names (some platforms have low limits)
|
||||||
|
// * Visually distinct from DB id format (usually RFC-4122)
|
||||||
|
std::string EncodeSessionId(uint64_t upper, uint64_t lower); |
||||||
|
|
||||||
|
// Reverse of EncodeSessionId. Returns NotSupported on error rather than
|
||||||
|
// Corruption because non-standard session IDs should be allowed with degraded
|
||||||
|
// functionality.
|
||||||
|
Status DecodeSessionId(const std::string &db_session_id, uint64_t *upper, |
||||||
|
uint64_t *lower); |
||||||
|
|
||||||
|
} // namespace ROCKSDB_NAMESPACE
|
Loading…
Reference in new issue