2020-08-27 18:54:43 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. 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/blob/blob_file_builder.h"
|
|
|
|
|
|
|
|
#include <cassert>
|
|
|
|
|
2022-08-25 23:45:48 +00:00
|
|
|
#include "db/blob/blob_contents.h"
|
2020-08-27 18:54:43 +00:00
|
|
|
#include "db/blob/blob_file_addition.h"
|
2021-03-18 03:43:22 +00:00
|
|
|
#include "db/blob/blob_file_completion_callback.h"
|
2020-08-27 18:54:43 +00:00
|
|
|
#include "db/blob/blob_index.h"
|
|
|
|
#include "db/blob/blob_log_format.h"
|
|
|
|
#include "db/blob/blob_log_writer.h"
|
2021-09-17 00:17:40 +00:00
|
|
|
#include "db/event_helpers.h"
|
2020-08-27 18:54:43 +00:00
|
|
|
#include "db/version_set.h"
|
|
|
|
#include "file/filename.h"
|
|
|
|
#include "file/read_write_util.h"
|
|
|
|
#include "file/writable_file_writer.h"
|
2020-08-31 19:30:02 +00:00
|
|
|
#include "logging/logging.h"
|
2020-08-27 18:54:43 +00:00
|
|
|
#include "options/cf_options.h"
|
2021-02-11 06:18:33 +00:00
|
|
|
#include "options/options_helper.h"
|
2020-08-27 18:54:43 +00:00
|
|
|
#include "rocksdb/slice.h"
|
|
|
|
#include "rocksdb/status.h"
|
|
|
|
#include "test_util/sync_point.h"
|
2021-02-16 17:47:12 +00:00
|
|
|
#include "trace_replay/io_tracer.h"
|
2020-08-27 18:54:43 +00:00
|
|
|
#include "util/compression.h"
|
|
|
|
|
|
|
|
namespace ROCKSDB_NAMESPACE {
|
|
|
|
|
|
|
|
BlobFileBuilder::BlobFileBuilder(
|
2021-03-15 11:32:24 +00:00
|
|
|
VersionSet* versions, FileSystem* fs,
|
2021-06-16 23:50:43 +00:00
|
|
|
const ImmutableOptions* immutable_options,
|
2020-08-27 18:54:43 +00:00
|
|
|
const MutableCFOptions* mutable_cf_options, const FileOptions* file_options,
|
2022-07-17 14:13:59 +00:00
|
|
|
std::string db_id, std::string db_session_id, int job_id,
|
|
|
|
uint32_t column_family_id, const std::string& column_family_name,
|
|
|
|
Env::IOPriority io_priority, Env::WriteLifeTimeHint write_hint,
|
2021-02-16 17:47:12 +00:00
|
|
|
const std::shared_ptr<IOTracer>& io_tracer,
|
2021-03-18 03:43:22 +00:00
|
|
|
BlobFileCompletionCallback* blob_callback,
|
2021-09-17 00:17:40 +00:00
|
|
|
BlobFileCreationReason creation_reason,
|
2020-09-15 04:10:09 +00:00
|
|
|
std::vector<std::string>* blob_file_paths,
|
2020-08-27 18:54:43 +00:00
|
|
|
std::vector<BlobFileAddition>* blob_file_additions)
|
2021-03-15 11:32:24 +00:00
|
|
|
: BlobFileBuilder([versions]() { return versions->NewFileNumber(); }, fs,
|
2021-06-16 23:50:43 +00:00
|
|
|
immutable_options, mutable_cf_options, file_options,
|
2022-07-17 14:13:59 +00:00
|
|
|
db_id, db_session_id, job_id, column_family_id,
|
|
|
|
column_family_name, io_priority, write_hint, io_tracer,
|
|
|
|
blob_callback, creation_reason, blob_file_paths,
|
|
|
|
blob_file_additions) {}
|
2020-08-27 18:54:43 +00:00
|
|
|
|
|
|
|
BlobFileBuilder::BlobFileBuilder(
|
2021-03-15 11:32:24 +00:00
|
|
|
std::function<uint64_t()> file_number_generator, FileSystem* fs,
|
2021-06-16 23:50:43 +00:00
|
|
|
const ImmutableOptions* immutable_options,
|
2020-08-27 18:54:43 +00:00
|
|
|
const MutableCFOptions* mutable_cf_options, const FileOptions* file_options,
|
2022-07-17 14:13:59 +00:00
|
|
|
std::string db_id, std::string db_session_id, int job_id,
|
|
|
|
uint32_t column_family_id, const std::string& column_family_name,
|
|
|
|
Env::IOPriority io_priority, Env::WriteLifeTimeHint write_hint,
|
2021-02-16 17:47:12 +00:00
|
|
|
const std::shared_ptr<IOTracer>& io_tracer,
|
2021-03-18 03:43:22 +00:00
|
|
|
BlobFileCompletionCallback* blob_callback,
|
2021-09-17 00:17:40 +00:00
|
|
|
BlobFileCreationReason creation_reason,
|
2020-09-15 04:10:09 +00:00
|
|
|
std::vector<std::string>* blob_file_paths,
|
2020-08-27 18:54:43 +00:00
|
|
|
std::vector<BlobFileAddition>* blob_file_additions)
|
|
|
|
: file_number_generator_(std::move(file_number_generator)),
|
|
|
|
fs_(fs),
|
2021-06-16 23:50:43 +00:00
|
|
|
immutable_options_(immutable_options),
|
2020-08-27 18:54:43 +00:00
|
|
|
min_blob_size_(mutable_cf_options->min_blob_size),
|
|
|
|
blob_file_size_(mutable_cf_options->blob_file_size),
|
|
|
|
blob_compression_type_(mutable_cf_options->blob_compression_type),
|
2022-07-17 14:13:59 +00:00
|
|
|
prepopulate_blob_cache_(mutable_cf_options->prepopulate_blob_cache),
|
2020-08-27 18:54:43 +00:00
|
|
|
file_options_(file_options),
|
2022-07-17 14:13:59 +00:00
|
|
|
db_id_(std::move(db_id)),
|
|
|
|
db_session_id_(std::move(db_session_id)),
|
2020-08-31 19:30:02 +00:00
|
|
|
job_id_(job_id),
|
2020-08-27 18:54:43 +00:00
|
|
|
column_family_id_(column_family_id),
|
2020-08-31 19:30:02 +00:00
|
|
|
column_family_name_(column_family_name),
|
2020-08-27 18:54:43 +00:00
|
|
|
io_priority_(io_priority),
|
|
|
|
write_hint_(write_hint),
|
2021-02-16 17:47:12 +00:00
|
|
|
io_tracer_(io_tracer),
|
2021-03-18 03:43:22 +00:00
|
|
|
blob_callback_(blob_callback),
|
2021-09-17 00:17:40 +00:00
|
|
|
creation_reason_(creation_reason),
|
2020-09-15 04:10:09 +00:00
|
|
|
blob_file_paths_(blob_file_paths),
|
2020-08-27 18:54:43 +00:00
|
|
|
blob_file_additions_(blob_file_additions),
|
|
|
|
blob_count_(0),
|
|
|
|
blob_bytes_(0) {
|
|
|
|
assert(file_number_generator_);
|
|
|
|
assert(fs_);
|
2021-06-16 23:50:43 +00:00
|
|
|
assert(immutable_options_);
|
2020-08-27 18:54:43 +00:00
|
|
|
assert(file_options_);
|
2020-09-15 04:10:09 +00:00
|
|
|
assert(blob_file_paths_);
|
|
|
|
assert(blob_file_paths_->empty());
|
2020-08-27 18:54:43 +00:00
|
|
|
assert(blob_file_additions_);
|
2020-09-15 04:10:09 +00:00
|
|
|
assert(blob_file_additions_->empty());
|
2020-08-27 18:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
BlobFileBuilder::~BlobFileBuilder() = default;
|
|
|
|
|
|
|
|
Status BlobFileBuilder::Add(const Slice& key, const Slice& value,
|
|
|
|
std::string* blob_index) {
|
|
|
|
assert(blob_index);
|
|
|
|
assert(blob_index->empty());
|
|
|
|
|
|
|
|
if (value.size() < min_blob_size_) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const Status s = OpenBlobFileIfNeeded();
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice blob = value;
|
|
|
|
std::string compressed_blob;
|
|
|
|
|
|
|
|
{
|
|
|
|
const Status s = CompressBlobIfNeeded(&blob, &compressed_blob);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t blob_file_number = 0;
|
|
|
|
uint64_t blob_offset = 0;
|
|
|
|
|
|
|
|
{
|
|
|
|
const Status s =
|
|
|
|
WriteBlobToFile(key, blob, &blob_file_number, &blob_offset);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
const Status s = CloseBlobFileIfNeeded();
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-17 14:13:59 +00:00
|
|
|
{
|
|
|
|
const Status s =
|
|
|
|
PutBlobIntoCacheIfNeeded(value, blob_file_number, blob_offset);
|
|
|
|
if (!s.ok()) {
|
|
|
|
ROCKS_LOG_WARN(immutable_options_->info_log,
|
|
|
|
"Failed to pre-populate the blob into blob cache: %s",
|
|
|
|
s.ToString().c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-27 18:54:43 +00:00
|
|
|
BlobIndex::EncodeBlob(blob_index, blob_file_number, blob_offset, blob.size(),
|
|
|
|
blob_compression_type_);
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status BlobFileBuilder::Finish() {
|
|
|
|
if (!IsBlobFileOpen()) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
return CloseBlobFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool BlobFileBuilder::IsBlobFileOpen() const { return !!writer_; }
|
|
|
|
|
|
|
|
Status BlobFileBuilder::OpenBlobFileIfNeeded() {
|
|
|
|
if (IsBlobFileOpen()) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!blob_count_);
|
|
|
|
assert(!blob_bytes_);
|
|
|
|
|
|
|
|
assert(file_number_generator_);
|
|
|
|
const uint64_t blob_file_number = file_number_generator_();
|
|
|
|
|
2021-06-16 23:50:43 +00:00
|
|
|
assert(immutable_options_);
|
|
|
|
assert(!immutable_options_->cf_paths.empty());
|
|
|
|
std::string blob_file_path =
|
|
|
|
BlobFileName(immutable_options_->cf_paths.front().path, blob_file_number);
|
2020-08-27 18:54:43 +00:00
|
|
|
|
2021-09-17 00:17:40 +00:00
|
|
|
if (blob_callback_) {
|
|
|
|
blob_callback_->OnBlobFileCreationStarted(
|
|
|
|
blob_file_path, column_family_name_, job_id_, creation_reason_);
|
|
|
|
}
|
|
|
|
|
2020-08-27 18:54:43 +00:00
|
|
|
std::unique_ptr<FSWritableFile> file;
|
|
|
|
|
|
|
|
{
|
|
|
|
assert(file_options_);
|
Do not explicitly flush blob files when using the integrated BlobDB (#7892)
Summary:
In the original stacked BlobDB implementation, which writes blobs to blob files
immediately and treats blob files as logs, it makes sense to flush the file after
writing each blob to protect against process crashes; however, in the integrated
implementation, which builds blob files in the background jobs, this unnecessarily
reduces performance. This patch fixes this by simply adding a `do_flush` flag to
`BlobLogWriter`, which is set to `true` by the stacked implementation and to `false`
by the new code. Note: the change itself is trivial but the tests needed some work;
since in the new implementation, blobs are now buffered, adding a blob to
`BlobFileBuilder` is no longer guaranteed to result in an actual I/O. Therefore, we can
no longer rely on `FaultInjectionTestEnv` when testing failure cases; instead, we
manipulate the return values of I/O methods directly using `SyncPoint`s.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/7892
Test Plan: `make check`
Reviewed By: jay-zhuang
Differential Revision: D26022814
Pulled By: ltamasi
fbshipit-source-id: b3dce419f312137fa70d84cdd9b908fd5d60d8cd
2021-01-25 21:30:17 +00:00
|
|
|
Status s = NewWritableFile(fs_, blob_file_path, &file, *file_options_);
|
|
|
|
|
|
|
|
TEST_SYNC_POINT_CALLBACK(
|
|
|
|
"BlobFileBuilder::OpenBlobFileIfNeeded:NewWritableFile", &s);
|
|
|
|
|
2020-08-27 18:54:43 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-15 04:10:09 +00:00
|
|
|
// Note: files get added to blob_file_paths_ right after the open, so they
|
|
|
|
// can be cleaned up upon failure. Contrast this with blob_file_additions_,
|
|
|
|
// which only contains successfully written files.
|
|
|
|
assert(blob_file_paths_);
|
|
|
|
blob_file_paths_->emplace_back(std::move(blob_file_path));
|
|
|
|
|
2020-08-27 18:54:43 +00:00
|
|
|
assert(file);
|
|
|
|
file->SetIOPriority(io_priority_);
|
|
|
|
file->SetWriteLifeTimeHint(write_hint_);
|
2021-06-16 23:50:43 +00:00
|
|
|
FileTypeSet tmp_set = immutable_options_->checksum_handoff_file_types;
|
|
|
|
Statistics* const statistics = immutable_options_->stats;
|
2020-09-08 17:49:01 +00:00
|
|
|
std::unique_ptr<WritableFileWriter> file_writer(new WritableFileWriter(
|
2021-03-15 11:32:24 +00:00
|
|
|
std::move(file), blob_file_paths_->back(), *file_options_,
|
2021-06-16 23:50:43 +00:00
|
|
|
immutable_options_->clock, io_tracer_, statistics,
|
|
|
|
immutable_options_->listeners,
|
|
|
|
immutable_options_->file_checksum_gen_factory.get(),
|
Using existing crc32c checksum in checksum handoff for Manifest and WAL (#8412)
Summary:
In PR https://github.com/facebook/rocksdb/issues/7523 , checksum handoff is introduced in RocksDB for WAL, Manifest, and SST files. When user enable checksum handoff for a certain type of file, before the data is written to the lower layer storage system, we calculate the checksum (crc32c) of each piece of data and pass the checksum down with the data, such that data verification can be down by the lower layer storage system if it has the capability. However, it cannot cover the whole lifetime of the data in the memory and also it potentially introduces extra checksum calculation overhead.
In this PR, we introduce a new interface in WritableFileWriter::Append, which allows the caller be able to pass the data and the checksum (crc32c) together. In this way, WritableFileWriter can directly use the pass-in checksum (crc32c) to generate the checksum of data being passed down to the storage system. It saves the calculation overhead and achieves higher protection coverage. When a new checksum is added with the data, we use Crc32cCombine https://github.com/facebook/rocksdb/issues/8305 to combine the existing checksum and the new checksum. To avoid the segmenting of data by rate-limiter before it is stored, rate-limiter is called enough times to accumulate enough credits for a certain write. This design only support Manifest and WAL which use log_writer in the current stage.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/8412
Test Plan: make check, add new testing cases.
Reviewed By: anand1976
Differential Revision: D29151545
Pulled By: zhichao-cao
fbshipit-source-id: 75e2278c5126cfd58393c67b1efd18dcc7a30772
2021-06-25 07:46:33 +00:00
|
|
|
tmp_set.Contains(FileType::kBlobFile), false));
|
2020-08-27 18:54:43 +00:00
|
|
|
|
Do not explicitly flush blob files when using the integrated BlobDB (#7892)
Summary:
In the original stacked BlobDB implementation, which writes blobs to blob files
immediately and treats blob files as logs, it makes sense to flush the file after
writing each blob to protect against process crashes; however, in the integrated
implementation, which builds blob files in the background jobs, this unnecessarily
reduces performance. This patch fixes this by simply adding a `do_flush` flag to
`BlobLogWriter`, which is set to `true` by the stacked implementation and to `false`
by the new code. Note: the change itself is trivial but the tests needed some work;
since in the new implementation, blobs are now buffered, adding a blob to
`BlobFileBuilder` is no longer guaranteed to result in an actual I/O. Therefore, we can
no longer rely on `FaultInjectionTestEnv` when testing failure cases; instead, we
manipulate the return values of I/O methods directly using `SyncPoint`s.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/7892
Test Plan: `make check`
Reviewed By: jay-zhuang
Differential Revision: D26022814
Pulled By: ltamasi
fbshipit-source-id: b3dce419f312137fa70d84cdd9b908fd5d60d8cd
2021-01-25 21:30:17 +00:00
|
|
|
constexpr bool do_flush = false;
|
|
|
|
|
|
|
|
std::unique_ptr<BlobLogWriter> blob_log_writer(new BlobLogWriter(
|
2021-06-16 23:50:43 +00:00
|
|
|
std::move(file_writer), immutable_options_->clock, statistics,
|
|
|
|
blob_file_number, immutable_options_->use_fsync, do_flush));
|
2020-08-27 18:54:43 +00:00
|
|
|
|
|
|
|
constexpr bool has_ttl = false;
|
|
|
|
constexpr ExpirationRange expiration_range;
|
|
|
|
|
|
|
|
BlobLogHeader header(column_family_id_, blob_compression_type_, has_ttl,
|
|
|
|
expiration_range);
|
|
|
|
|
|
|
|
{
|
Do not explicitly flush blob files when using the integrated BlobDB (#7892)
Summary:
In the original stacked BlobDB implementation, which writes blobs to blob files
immediately and treats blob files as logs, it makes sense to flush the file after
writing each blob to protect against process crashes; however, in the integrated
implementation, which builds blob files in the background jobs, this unnecessarily
reduces performance. This patch fixes this by simply adding a `do_flush` flag to
`BlobLogWriter`, which is set to `true` by the stacked implementation and to `false`
by the new code. Note: the change itself is trivial but the tests needed some work;
since in the new implementation, blobs are now buffered, adding a blob to
`BlobFileBuilder` is no longer guaranteed to result in an actual I/O. Therefore, we can
no longer rely on `FaultInjectionTestEnv` when testing failure cases; instead, we
manipulate the return values of I/O methods directly using `SyncPoint`s.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/7892
Test Plan: `make check`
Reviewed By: jay-zhuang
Differential Revision: D26022814
Pulled By: ltamasi
fbshipit-source-id: b3dce419f312137fa70d84cdd9b908fd5d60d8cd
2021-01-25 21:30:17 +00:00
|
|
|
Status s = blob_log_writer->WriteHeader(header);
|
|
|
|
|
|
|
|
TEST_SYNC_POINT_CALLBACK(
|
|
|
|
"BlobFileBuilder::OpenBlobFileIfNeeded:WriteHeader", &s);
|
2020-08-27 18:54:43 +00:00
|
|
|
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
writer_ = std::move(blob_log_writer);
|
|
|
|
|
|
|
|
assert(IsBlobFileOpen());
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status BlobFileBuilder::CompressBlobIfNeeded(
|
|
|
|
Slice* blob, std::string* compressed_blob) const {
|
|
|
|
assert(blob);
|
|
|
|
assert(compressed_blob);
|
|
|
|
assert(compressed_blob->empty());
|
2021-08-18 00:21:16 +00:00
|
|
|
assert(immutable_options_);
|
2020-08-27 18:54:43 +00:00
|
|
|
|
|
|
|
if (blob_compression_type_ == kNoCompression) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
CompressionOptions opts;
|
|
|
|
CompressionContext context(blob_compression_type_);
|
|
|
|
constexpr uint64_t sample_for_compression = 0;
|
|
|
|
|
|
|
|
CompressionInfo info(opts, context, CompressionDict::GetEmptyDict(),
|
|
|
|
blob_compression_type_, sample_for_compression);
|
|
|
|
|
|
|
|
constexpr uint32_t compression_format_version = 2;
|
|
|
|
|
2021-08-18 00:21:16 +00:00
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
{
|
|
|
|
StopWatch stop_watch(immutable_options_->clock, immutable_options_->stats,
|
|
|
|
BLOB_DB_COMPRESSION_MICROS);
|
|
|
|
success =
|
|
|
|
CompressData(*blob, info, compression_format_version, compressed_blob);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!success) {
|
2020-08-27 18:54:43 +00:00
|
|
|
return Status::Corruption("Error compressing blob");
|
|
|
|
}
|
|
|
|
|
|
|
|
*blob = Slice(*compressed_blob);
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status BlobFileBuilder::WriteBlobToFile(const Slice& key, const Slice& blob,
|
|
|
|
uint64_t* blob_file_number,
|
|
|
|
uint64_t* blob_offset) {
|
|
|
|
assert(IsBlobFileOpen());
|
|
|
|
assert(blob_file_number);
|
|
|
|
assert(blob_offset);
|
|
|
|
|
|
|
|
uint64_t key_offset = 0;
|
|
|
|
|
Do not explicitly flush blob files when using the integrated BlobDB (#7892)
Summary:
In the original stacked BlobDB implementation, which writes blobs to blob files
immediately and treats blob files as logs, it makes sense to flush the file after
writing each blob to protect against process crashes; however, in the integrated
implementation, which builds blob files in the background jobs, this unnecessarily
reduces performance. This patch fixes this by simply adding a `do_flush` flag to
`BlobLogWriter`, which is set to `true` by the stacked implementation and to `false`
by the new code. Note: the change itself is trivial but the tests needed some work;
since in the new implementation, blobs are now buffered, adding a blob to
`BlobFileBuilder` is no longer guaranteed to result in an actual I/O. Therefore, we can
no longer rely on `FaultInjectionTestEnv` when testing failure cases; instead, we
manipulate the return values of I/O methods directly using `SyncPoint`s.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/7892
Test Plan: `make check`
Reviewed By: jay-zhuang
Differential Revision: D26022814
Pulled By: ltamasi
fbshipit-source-id: b3dce419f312137fa70d84cdd9b908fd5d60d8cd
2021-01-25 21:30:17 +00:00
|
|
|
Status s = writer_->AddRecord(key, blob, &key_offset, blob_offset);
|
|
|
|
|
|
|
|
TEST_SYNC_POINT_CALLBACK("BlobFileBuilder::WriteBlobToFile:AddRecord", &s);
|
2020-08-27 18:54:43 +00:00
|
|
|
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
*blob_file_number = writer_->get_log_number();
|
|
|
|
|
|
|
|
++blob_count_;
|
|
|
|
blob_bytes_ += BlobLogRecord::kHeaderSize + key.size() + blob.size();
|
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status BlobFileBuilder::CloseBlobFile() {
|
|
|
|
assert(IsBlobFileOpen());
|
|
|
|
|
|
|
|
BlobLogFooter footer;
|
|
|
|
footer.blob_count = blob_count_;
|
|
|
|
|
|
|
|
std::string checksum_method;
|
|
|
|
std::string checksum_value;
|
|
|
|
|
Do not explicitly flush blob files when using the integrated BlobDB (#7892)
Summary:
In the original stacked BlobDB implementation, which writes blobs to blob files
immediately and treats blob files as logs, it makes sense to flush the file after
writing each blob to protect against process crashes; however, in the integrated
implementation, which builds blob files in the background jobs, this unnecessarily
reduces performance. This patch fixes this by simply adding a `do_flush` flag to
`BlobLogWriter`, which is set to `true` by the stacked implementation and to `false`
by the new code. Note: the change itself is trivial but the tests needed some work;
since in the new implementation, blobs are now buffered, adding a blob to
`BlobFileBuilder` is no longer guaranteed to result in an actual I/O. Therefore, we can
no longer rely on `FaultInjectionTestEnv` when testing failure cases; instead, we
manipulate the return values of I/O methods directly using `SyncPoint`s.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/7892
Test Plan: `make check`
Reviewed By: jay-zhuang
Differential Revision: D26022814
Pulled By: ltamasi
fbshipit-source-id: b3dce419f312137fa70d84cdd9b908fd5d60d8cd
2021-01-25 21:30:17 +00:00
|
|
|
Status s = writer_->AppendFooter(footer, &checksum_method, &checksum_value);
|
|
|
|
|
|
|
|
TEST_SYNC_POINT_CALLBACK("BlobFileBuilder::WriteBlobToFile:AppendFooter", &s);
|
2020-08-27 18:54:43 +00:00
|
|
|
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint64_t blob_file_number = writer_->get_log_number();
|
|
|
|
|
2021-09-17 00:17:40 +00:00
|
|
|
if (blob_callback_) {
|
|
|
|
s = blob_callback_->OnBlobFileCompleted(
|
|
|
|
blob_file_paths_->back(), column_family_name_, job_id_,
|
|
|
|
blob_file_number, creation_reason_, s, checksum_value, checksum_method,
|
|
|
|
blob_count_, blob_bytes_);
|
|
|
|
}
|
|
|
|
|
2020-08-27 18:54:43 +00:00
|
|
|
assert(blob_file_additions_);
|
|
|
|
blob_file_additions_->emplace_back(blob_file_number, blob_count_, blob_bytes_,
|
|
|
|
std::move(checksum_method),
|
|
|
|
std::move(checksum_value));
|
|
|
|
|
2021-06-16 23:50:43 +00:00
|
|
|
assert(immutable_options_);
|
|
|
|
ROCKS_LOG_INFO(immutable_options_->logger,
|
2020-08-31 19:30:02 +00:00
|
|
|
"[%s] [JOB %d] Generated blob file #%" PRIu64 ": %" PRIu64
|
|
|
|
" total blobs, %" PRIu64 " total bytes",
|
|
|
|
column_family_name_.c_str(), job_id_, blob_file_number,
|
|
|
|
blob_count_, blob_bytes_);
|
2021-03-18 03:43:22 +00:00
|
|
|
|
2020-08-27 18:54:43 +00:00
|
|
|
writer_.reset();
|
|
|
|
blob_count_ = 0;
|
|
|
|
blob_bytes_ = 0;
|
|
|
|
|
2021-03-18 03:43:22 +00:00
|
|
|
return s;
|
2020-08-27 18:54:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Status BlobFileBuilder::CloseBlobFileIfNeeded() {
|
|
|
|
assert(IsBlobFileOpen());
|
|
|
|
|
|
|
|
const WritableFileWriter* const file_writer = writer_->file();
|
|
|
|
assert(file_writer);
|
|
|
|
|
|
|
|
if (file_writer->GetFileSize() < blob_file_size_) {
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
return CloseBlobFile();
|
|
|
|
}
|
|
|
|
|
2021-09-17 00:17:40 +00:00
|
|
|
void BlobFileBuilder::Abandon(const Status& s) {
|
2021-03-18 03:43:22 +00:00
|
|
|
if (!IsBlobFileOpen()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (blob_callback_) {
|
|
|
|
// BlobFileBuilder::Abandon() is called because of error while writing to
|
|
|
|
// Blob files. So we can ignore the below error.
|
2021-09-17 00:17:40 +00:00
|
|
|
blob_callback_
|
|
|
|
->OnBlobFileCompleted(blob_file_paths_->back(), column_family_name_,
|
|
|
|
job_id_, writer_->get_log_number(),
|
|
|
|
creation_reason_, s, "", "", blob_count_,
|
|
|
|
blob_bytes_)
|
2021-03-18 03:43:22 +00:00
|
|
|
.PermitUncheckedError();
|
|
|
|
}
|
|
|
|
|
|
|
|
writer_.reset();
|
|
|
|
blob_count_ = 0;
|
|
|
|
blob_bytes_ = 0;
|
|
|
|
}
|
2022-07-17 14:13:59 +00:00
|
|
|
|
|
|
|
Status BlobFileBuilder::PutBlobIntoCacheIfNeeded(const Slice& blob,
|
|
|
|
uint64_t blob_file_number,
|
|
|
|
uint64_t blob_offset) const {
|
|
|
|
Status s = Status::OK();
|
|
|
|
|
|
|
|
auto blob_cache = immutable_options_->blob_cache;
|
|
|
|
auto statistics = immutable_options_->statistics.get();
|
|
|
|
bool warm_cache =
|
|
|
|
prepopulate_blob_cache_ == PrepopulateBlobCache::kFlushOnly &&
|
|
|
|
creation_reason_ == BlobFileCreationReason::kFlush;
|
|
|
|
|
|
|
|
if (blob_cache && warm_cache) {
|
Derive cache keys from SST unique IDs (#10394)
Summary:
... so that cache keys can be derived from DB manifest data
before reading the file from storage--so that every part of the file
can potentially go in a persistent cache.
See updated comments in cache_key.cc for technical details. Importantly,
the new cache key encoding uses some fancy but efficient math to pack
data into the cache key without depending on the sizes of the various
pieces. This simplifies some existing code creating cache keys, like
cache warming before the file size is known.
This should provide us an essentially permanent mapping between SST
unique IDs and base cache keys, with the ability to "upgrade" SST
unique IDs (and thus cache keys) with new SST format_versions.
These cache keys are of similar, perhaps indistinguishable quality to
the previous generation. Before this change (see "corrected" days
between collision):
```
./cache_bench -stress_cache_key -sck_keep_bits=43
18 collisions after 2 x 90 days, est 10 days between (1.15292e+19 corrected)
```
After this change (keep 43 bits, up through 50, to validate "trajectory"
is ok on "corrected" days between collision):
```
19 collisions after 3 x 90 days, est 14.2105 days between (1.63836e+19 corrected)
16 collisions after 5 x 90 days, est 28.125 days between (1.6213e+19 corrected)
15 collisions after 7 x 90 days, est 42 days between (1.21057e+19 corrected)
15 collisions after 17 x 90 days, est 102 days between (1.46997e+19 corrected)
15 collisions after 49 x 90 days, est 294 days between (2.11849e+19 corrected)
15 collisions after 62 x 90 days, est 372 days between (1.34027e+19 corrected)
15 collisions after 53 x 90 days, est 318 days between (5.72858e+18 corrected)
15 collisions after 309 x 90 days, est 1854 days between (1.66994e+19 corrected)
```
However, the change does modify (probably weaken) the "guaranteed unique" promise from this
> SST files generated in a single process are guaranteed to have unique cache keys, unless/until number session ids * max file number = 2**86
to this (see https://github.com/facebook/rocksdb/issues/10388)
> With the DB id limitation, we only have nice guaranteed unique cache keys for files generated in a single process until biggest session_id_counter and offset_in_file reach combined 64 bits
I don't think this is a practical concern, though.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/10394
Test Plan: unit tests updated, see simulation results above
Reviewed By: jay-zhuang
Differential Revision: D38667529
Pulled By: pdillinger
fbshipit-source-id: 49af3fe7f47e5b61162809a78b76c769fd519fba
2022-08-12 20:49:49 +00:00
|
|
|
const OffsetableCacheKey base_cache_key(db_id_, db_session_id_,
|
|
|
|
blob_file_number);
|
2022-07-17 14:13:59 +00:00
|
|
|
const CacheKey cache_key = base_cache_key.WithOffset(blob_offset);
|
|
|
|
const Slice key = cache_key.AsSlice();
|
|
|
|
|
2022-08-13 00:59:06 +00:00
|
|
|
const Cache::Priority priority = Cache::Priority::BOTTOM;
|
2022-07-17 14:13:59 +00:00
|
|
|
|
|
|
|
// Objects to be put into the cache have to be heap-allocated and
|
|
|
|
// self-contained, i.e. own their contents. The Cache has to be able to
|
2022-08-25 23:45:48 +00:00
|
|
|
// take unique ownership of them.
|
|
|
|
CacheAllocationPtr allocation(new char[blob.size()]);
|
|
|
|
memcpy(allocation.get(), blob.data(), blob.size());
|
|
|
|
std::unique_ptr<BlobContents> buf =
|
|
|
|
BlobContents::Create(std::move(allocation), blob.size());
|
2022-07-17 14:13:59 +00:00
|
|
|
|
|
|
|
// TODO: support custom allocators and provide a better estimated memory
|
|
|
|
// usage using malloc_usable_size.
|
|
|
|
s = blob_cache->Insert(key, buf.get(), buf->size(),
|
2022-08-25 23:45:48 +00:00
|
|
|
&BlobContents::DeleteCallback,
|
2022-07-17 14:13:59 +00:00
|
|
|
nullptr /* cache_handle */, priority);
|
|
|
|
if (s.ok()) {
|
|
|
|
RecordTick(statistics, BLOB_DB_CACHE_ADD);
|
|
|
|
RecordTick(statistics, BLOB_DB_CACHE_BYTES_WRITE, buf->size());
|
|
|
|
buf.release();
|
|
|
|
} else {
|
|
|
|
RecordTick(statistics, BLOB_DB_CACHE_ADD_FAILURES);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2020-08-27 18:54:43 +00:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|