forked from facebook/rocksdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Experimental support for SST unique IDs (facebook#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 facebook#8912) (The file offset can be XORed into the third 64-bit value of the unique ID.) * Publish the external unique IDs in FileStorageInfo (facebook#8968) Pull Request resolved: facebook#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
- Loading branch information
1 parent
aa21896
commit ad5325a
Showing
30 changed files
with
1,085 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.