2016-02-10 00:12:00 +01:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2017-07-16 01:03:42 +02: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).
|
2015-09-23 21:42:43 +02:00
|
|
|
|
|
|
|
#include "rocksdb/sst_file_writer.h"
|
|
|
|
|
|
|
|
#include <vector>
|
2019-05-30 23:47:29 +02:00
|
|
|
|
2015-09-23 21:42:43 +02:00
|
|
|
#include "db/dbformat.h"
|
2019-09-16 19:31:27 +02:00
|
|
|
#include "file/writable_file_writer.h"
|
2021-01-29 07:08:46 +01:00
|
|
|
#include "rocksdb/file_system.h"
|
2015-09-23 21:42:43 +02:00
|
|
|
#include "rocksdb/table.h"
|
2019-05-30 23:47:29 +02:00
|
|
|
#include "table/block_based/block_based_table_builder.h"
|
2016-10-19 01:59:37 +02:00
|
|
|
#include "table/sst_file_writer_collectors.h"
|
2019-05-30 20:21:38 +02:00
|
|
|
#include "test_util/sync_point.h"
|
2015-09-23 21:42:43 +02:00
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2015-09-23 21:42:43 +02:00
|
|
|
|
|
|
|
const std::string ExternalSstFilePropertyNames::kVersion =
|
|
|
|
"rocksdb.external_sst_file.version";
|
2016-10-19 01:59:37 +02:00
|
|
|
const std::string ExternalSstFilePropertyNames::kGlobalSeqno =
|
|
|
|
"rocksdb.external_sst_file.global_seqno";
|
2017-02-28 20:05:08 +01:00
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2016-12-29 23:51:52 +01:00
|
|
|
const size_t kFadviseTrigger = 1024 * 1024; // 1MB
|
2015-09-23 21:42:43 +02:00
|
|
|
|
|
|
|
struct SstFileWriter::Rep {
|
2016-05-17 22:11:56 +02:00
|
|
|
Rep(const EnvOptions& _env_options, const Options& options,
|
2017-05-23 20:36:06 +02:00
|
|
|
Env::IOPriority _io_priority, const Comparator* _user_comparator,
|
2018-01-22 23:37:37 +01:00
|
|
|
ColumnFamilyHandle* _cfh, bool _invalidate_page_cache, bool _skip_filters)
|
2015-09-23 21:42:43 +02:00
|
|
|
: env_options(_env_options),
|
2016-05-17 22:11:56 +02:00
|
|
|
ioptions(options),
|
2016-09-14 06:11:59 +02:00
|
|
|
mutable_cf_options(options),
|
2017-05-23 20:36:06 +02:00
|
|
|
io_priority(_io_priority),
|
2016-12-05 23:16:23 +01:00
|
|
|
internal_comparator(_user_comparator),
|
2016-12-29 23:51:52 +01:00
|
|
|
cfh(_cfh),
|
|
|
|
invalidate_page_cache(_invalidate_page_cache),
|
2018-01-22 23:37:37 +01:00
|
|
|
last_fadvise_size(0),
|
|
|
|
skip_filters(_skip_filters) {}
|
2015-09-23 21:42:43 +02:00
|
|
|
|
|
|
|
std::unique_ptr<WritableFileWriter> file_writer;
|
|
|
|
std::unique_ptr<TableBuilder> builder;
|
|
|
|
EnvOptions env_options;
|
|
|
|
ImmutableCFOptions ioptions;
|
2016-05-17 22:11:56 +02:00
|
|
|
MutableCFOptions mutable_cf_options;
|
2017-05-23 20:36:06 +02:00
|
|
|
Env::IOPriority io_priority;
|
2015-09-23 21:42:43 +02:00
|
|
|
InternalKeyComparator internal_comparator;
|
|
|
|
ExternalSstFileInfo file_info;
|
2016-06-13 18:57:43 +02:00
|
|
|
InternalKey ikey;
|
2016-12-05 23:16:23 +01:00
|
|
|
std::string column_family_name;
|
|
|
|
ColumnFamilyHandle* cfh;
|
2016-12-29 23:51:52 +01:00
|
|
|
// If true, We will give the OS a hint that this file pages is not needed
|
2018-03-08 19:18:34 +01:00
|
|
|
// every time we write 1MB to the file.
|
2016-12-29 23:51:52 +01:00
|
|
|
bool invalidate_page_cache;
|
2017-05-23 20:36:06 +02:00
|
|
|
// The size of the file during the last time we called Fadvise to remove
|
2016-12-29 23:51:52 +01:00
|
|
|
// cached pages from page cache.
|
|
|
|
uint64_t last_fadvise_size;
|
2018-01-22 23:37:37 +01:00
|
|
|
bool skip_filters;
|
2017-05-26 21:05:19 +02:00
|
|
|
Status Add(const Slice& user_key, const Slice& value,
|
|
|
|
const ValueType value_type) {
|
|
|
|
if (!builder) {
|
|
|
|
return Status::InvalidArgument("File is not opened");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (file_info.num_entries == 0) {
|
|
|
|
file_info.smallest_key.assign(user_key.data(), user_key.size());
|
|
|
|
} else {
|
|
|
|
if (internal_comparator.user_comparator()->Compare(
|
|
|
|
user_key, file_info.largest_key) <= 0) {
|
|
|
|
// Make sure that keys are added in order
|
2020-01-06 19:55:53 +01:00
|
|
|
return Status::InvalidArgument(
|
|
|
|
"Keys must be added in strict ascending order.");
|
2017-05-26 21:05:19 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO(tec) : For external SST files we could omit the seqno and type.
|
|
|
|
switch (value_type) {
|
|
|
|
case ValueType::kTypeValue:
|
|
|
|
ikey.Set(user_key, 0 /* Sequence Number */,
|
|
|
|
ValueType::kTypeValue /* Put */);
|
|
|
|
break;
|
|
|
|
case ValueType::kTypeMerge:
|
|
|
|
ikey.Set(user_key, 0 /* Sequence Number */,
|
|
|
|
ValueType::kTypeMerge /* Merge */);
|
|
|
|
break;
|
|
|
|
case ValueType::kTypeDeletion:
|
|
|
|
ikey.Set(user_key, 0 /* Sequence Number */,
|
|
|
|
ValueType::kTypeDeletion /* Delete */);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return Status::InvalidArgument("Value type is not supported");
|
|
|
|
}
|
|
|
|
builder->Add(ikey.Encode(), value);
|
|
|
|
|
|
|
|
// update file info
|
|
|
|
file_info.num_entries++;
|
|
|
|
file_info.largest_key.assign(user_key.data(), user_key.size());
|
|
|
|
file_info.file_size = builder->FileSize();
|
|
|
|
|
2021-04-06 20:53:54 +02:00
|
|
|
InvalidatePageCache(false /* closing */).PermitUncheckedError();
|
|
|
|
return Status::OK();
|
2017-05-26 21:05:19 +02:00
|
|
|
}
|
|
|
|
|
2018-07-14 07:40:23 +02:00
|
|
|
Status DeleteRange(const Slice& begin_key, const Slice& end_key) {
|
|
|
|
if (!builder) {
|
|
|
|
return Status::InvalidArgument("File is not opened");
|
|
|
|
}
|
|
|
|
|
|
|
|
RangeTombstone tombstone(begin_key, end_key, 0 /* Sequence Number */);
|
|
|
|
if (file_info.num_range_del_entries == 0) {
|
|
|
|
file_info.smallest_range_del_key.assign(tombstone.start_key_.data(),
|
|
|
|
tombstone.start_key_.size());
|
|
|
|
file_info.largest_range_del_key.assign(tombstone.end_key_.data(),
|
|
|
|
tombstone.end_key_.size());
|
|
|
|
} else {
|
|
|
|
if (internal_comparator.user_comparator()->Compare(
|
|
|
|
tombstone.start_key_, file_info.smallest_range_del_key) < 0) {
|
|
|
|
file_info.smallest_range_del_key.assign(tombstone.start_key_.data(),
|
|
|
|
tombstone.start_key_.size());
|
|
|
|
}
|
|
|
|
if (internal_comparator.user_comparator()->Compare(
|
|
|
|
tombstone.end_key_, file_info.largest_range_del_key) > 0) {
|
|
|
|
file_info.largest_range_del_key.assign(tombstone.end_key_.data(),
|
|
|
|
tombstone.end_key_.size());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ikey_and_end_key = tombstone.Serialize();
|
|
|
|
builder->Add(ikey_and_end_key.first.Encode(), ikey_and_end_key.second);
|
|
|
|
|
|
|
|
// update file info
|
|
|
|
file_info.num_range_del_entries++;
|
|
|
|
file_info.file_size = builder->FileSize();
|
|
|
|
|
2021-04-06 20:53:54 +02:00
|
|
|
InvalidatePageCache(false /* closing */).PermitUncheckedError();
|
|
|
|
return Status::OK();
|
2018-07-14 07:40:23 +02:00
|
|
|
}
|
|
|
|
|
2020-09-29 00:11:54 +02:00
|
|
|
Status InvalidatePageCache(bool closing) {
|
|
|
|
Status s = Status::OK();
|
2017-05-26 21:05:19 +02:00
|
|
|
if (invalidate_page_cache == false) {
|
|
|
|
// Fadvise disabled
|
2020-09-29 00:11:54 +02:00
|
|
|
return s;
|
2017-05-26 21:05:19 +02:00
|
|
|
}
|
|
|
|
uint64_t bytes_since_last_fadvise =
|
|
|
|
builder->FileSize() - last_fadvise_size;
|
|
|
|
if (bytes_since_last_fadvise > kFadviseTrigger || closing) {
|
|
|
|
TEST_SYNC_POINT_CALLBACK("SstFileWriter::Rep::InvalidatePageCache",
|
|
|
|
&(bytes_since_last_fadvise));
|
2020-02-28 23:10:51 +01:00
|
|
|
// Tell the OS that we don't need this file in page cache
|
2020-09-29 00:11:54 +02:00
|
|
|
s = file_writer->InvalidateCache(0, 0);
|
|
|
|
if (s.IsNotSupported()) {
|
|
|
|
// NotSupported is fine as it could be a file type that doesn't use page
|
|
|
|
// cache.
|
|
|
|
s = Status::OK();
|
|
|
|
}
|
2017-05-26 21:05:19 +02:00
|
|
|
last_fadvise_size = builder->FileSize();
|
|
|
|
}
|
2020-09-29 00:11:54 +02:00
|
|
|
return s;
|
2017-05-26 21:05:19 +02:00
|
|
|
}
|
2015-09-23 21:42:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
SstFileWriter::SstFileWriter(const EnvOptions& env_options,
|
2016-05-17 22:11:56 +02:00
|
|
|
const Options& options,
|
2016-12-05 23:16:23 +01:00
|
|
|
const Comparator* user_comparator,
|
2016-12-29 23:51:52 +01:00
|
|
|
ColumnFamilyHandle* column_family,
|
2017-05-23 20:36:06 +02:00
|
|
|
bool invalidate_page_cache,
|
2018-01-22 23:37:37 +01:00
|
|
|
Env::IOPriority io_priority, bool skip_filters)
|
2017-05-23 20:36:06 +02:00
|
|
|
: rep_(new Rep(env_options, options, io_priority, user_comparator,
|
2018-01-22 23:37:37 +01:00
|
|
|
column_family, invalidate_page_cache, skip_filters)) {
|
2016-12-29 23:51:52 +01:00
|
|
|
rep_->file_info.file_size = 0;
|
|
|
|
}
|
2015-09-23 21:42:43 +02:00
|
|
|
|
2016-11-13 05:03:39 +01:00
|
|
|
SstFileWriter::~SstFileWriter() {
|
|
|
|
if (rep_->builder) {
|
|
|
|
// User did not call Finish() or Finish() failed, we need to
|
|
|
|
// abandon the builder.
|
|
|
|
rep_->builder->Abandon();
|
|
|
|
}
|
|
|
|
}
|
2015-09-23 21:42:43 +02:00
|
|
|
|
|
|
|
Status SstFileWriter::Open(const std::string& file_path) {
|
2017-05-23 20:36:06 +02:00
|
|
|
Rep* r = rep_.get();
|
2015-09-23 21:42:43 +02:00
|
|
|
Status s;
|
2021-01-29 07:08:46 +01:00
|
|
|
std::unique_ptr<FSWritableFile> sst_file;
|
2021-02-11 07:18:33 +01:00
|
|
|
FileOptions cur_file_opts(r->env_options);
|
2021-01-29 07:08:46 +01:00
|
|
|
s = r->ioptions.env->GetFileSystem()->NewWritableFile(
|
2021-02-11 07:18:33 +01:00
|
|
|
file_path, cur_file_opts, &sst_file, nullptr);
|
2015-09-23 21:42:43 +02:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2017-05-23 20:36:06 +02:00
|
|
|
sst_file->SetIOPriority(r->io_priority);
|
|
|
|
|
2016-06-20 20:26:25 +02:00
|
|
|
CompressionType compression_type;
|
2018-06-28 02:34:07 +02:00
|
|
|
CompressionOptions compression_opts;
|
2020-03-31 21:08:41 +02:00
|
|
|
if (r->mutable_cf_options.bottommost_compression !=
|
|
|
|
kDisableCompressionOption) {
|
|
|
|
compression_type = r->mutable_cf_options.bottommost_compression;
|
|
|
|
if (r->mutable_cf_options.bottommost_compression_opts.enabled) {
|
|
|
|
compression_opts = r->mutable_cf_options.bottommost_compression_opts;
|
2018-06-28 02:34:07 +02:00
|
|
|
} else {
|
2020-03-31 21:08:41 +02:00
|
|
|
compression_opts = r->mutable_cf_options.compression_opts;
|
2018-06-28 02:34:07 +02:00
|
|
|
}
|
2016-06-20 20:26:25 +02:00
|
|
|
} else if (!r->ioptions.compression_per_level.empty()) {
|
2015-09-23 21:42:43 +02:00
|
|
|
// Use the compression of the last level if we have per level compression
|
|
|
|
compression_type = *(r->ioptions.compression_per_level.rbegin());
|
2020-03-31 21:08:41 +02:00
|
|
|
compression_opts = r->mutable_cf_options.compression_opts;
|
2016-06-20 20:26:25 +02:00
|
|
|
} else {
|
|
|
|
compression_type = r->mutable_cf_options.compression;
|
2020-03-31 21:08:41 +02:00
|
|
|
compression_opts = r->mutable_cf_options.compression_opts;
|
2015-09-23 21:42:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<IntTblPropCollectorFactory>>
|
|
|
|
int_tbl_prop_collector_factories;
|
2016-08-20 01:17:56 +02:00
|
|
|
|
|
|
|
// SstFileWriter properties collector to add SstFileWriter version.
|
2015-09-23 21:42:43 +02:00
|
|
|
int_tbl_prop_collector_factories.emplace_back(
|
2016-10-19 01:59:37 +02:00
|
|
|
new SstFileWriterPropertiesCollectorFactory(2 /* version */,
|
|
|
|
0 /* global_seqno*/));
|
2015-09-23 21:42:43 +02:00
|
|
|
|
2016-08-20 01:17:56 +02:00
|
|
|
// User collector factories
|
|
|
|
auto user_collector_factories =
|
|
|
|
r->ioptions.table_properties_collector_factories;
|
|
|
|
for (size_t i = 0; i < user_collector_factories.size(); i++) {
|
|
|
|
int_tbl_prop_collector_factories.emplace_back(
|
|
|
|
new UserKeyTablePropertiesCollectorFactory(
|
|
|
|
user_collector_factories[i]));
|
|
|
|
}
|
2016-09-18 07:30:43 +02:00
|
|
|
int unknown_level = -1;
|
2016-12-05 23:16:23 +01:00
|
|
|
uint32_t cf_id;
|
|
|
|
|
|
|
|
if (r->cfh != nullptr) {
|
|
|
|
// user explicitly specified that this file will be ingested into cfh,
|
|
|
|
// we can persist this information in the file.
|
|
|
|
cf_id = r->cfh->GetID();
|
|
|
|
r->column_family_name = r->cfh->GetName();
|
|
|
|
} else {
|
|
|
|
r->column_family_name = "";
|
|
|
|
cf_id = TablePropertiesCollectorFactory::Context::kUnknownColumnFamily;
|
|
|
|
}
|
2020-06-17 19:55:42 +02:00
|
|
|
// SstFileWriter is used to create sst files that can be added to database
|
|
|
|
// later. Therefore, no real db_id and db_session_id are associated with it.
|
|
|
|
// Here we mimic the way db_session_id behaves by resetting the db_session_id
|
|
|
|
// every time SstFileWriter is used, and in this case db_id is set to be "SST
|
|
|
|
// Writer".
|
|
|
|
std::string db_session_id = r->ioptions.env->GenerateUniqueId();
|
|
|
|
if (!db_session_id.empty() && db_session_id.back() == '\n') {
|
|
|
|
db_session_id.pop_back();
|
|
|
|
}
|
2015-09-23 21:42:43 +02:00
|
|
|
TableBuilderOptions table_builder_options(
|
2018-05-21 23:33:55 +02:00
|
|
|
r->ioptions, r->mutable_cf_options, r->internal_comparator,
|
2021-03-25 22:58:23 +01:00
|
|
|
&int_tbl_prop_collector_factories, compression_type, compression_opts,
|
|
|
|
r->skip_filters, r->column_family_name, unknown_level,
|
|
|
|
0 /* creation_time */, 0 /* oldest_key_time */, 0 /* target_file_size */,
|
2020-06-17 19:55:42 +02:00
|
|
|
0 /* file_creation_time */, "SST Writer" /* db_id */, db_session_id);
|
2021-02-11 07:18:33 +01:00
|
|
|
FileTypeSet tmp_set = r->ioptions.checksum_handoff_file_types;
|
2020-05-20 20:53:49 +02:00
|
|
|
r->file_writer.reset(new WritableFileWriter(
|
2021-03-15 12:32:24 +01:00
|
|
|
std::move(sst_file), file_path, r->env_options, r->ioptions.clock,
|
|
|
|
nullptr /* io_tracer */, nullptr /* stats */, r->ioptions.listeners,
|
2021-02-11 07:18:33 +01:00
|
|
|
r->ioptions.file_checksum_gen_factory,
|
|
|
|
tmp_set.Contains(FileType::kTableFile)));
|
2016-10-19 01:59:37 +02:00
|
|
|
|
|
|
|
// TODO(tec) : If table_factory is using compressed block cache, we will
|
|
|
|
// be adding the external sst file blocks into it, which is wasteful.
|
2015-09-23 21:42:43 +02:00
|
|
|
r->builder.reset(r->ioptions.table_factory->NewTableBuilder(
|
2016-12-05 23:16:23 +01:00
|
|
|
table_builder_options, cf_id, r->file_writer.get()));
|
2015-09-23 21:42:43 +02:00
|
|
|
|
2018-07-14 07:40:23 +02:00
|
|
|
r->file_info = ExternalSstFileInfo();
|
2015-09-23 21:42:43 +02:00
|
|
|
r->file_info.file_path = file_path;
|
2016-10-19 01:59:37 +02:00
|
|
|
r->file_info.version = 2;
|
2015-09-23 21:42:43 +02:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SstFileWriter::Add(const Slice& user_key, const Slice& value) {
|
2017-05-26 21:05:19 +02:00
|
|
|
return rep_->Add(user_key, value, ValueType::kTypeValue);
|
|
|
|
}
|
2016-12-29 23:51:52 +01:00
|
|
|
|
2017-05-26 21:05:19 +02:00
|
|
|
Status SstFileWriter::Put(const Slice& user_key, const Slice& value) {
|
|
|
|
return rep_->Add(user_key, value, ValueType::kTypeValue);
|
|
|
|
}
|
2015-09-23 21:42:43 +02:00
|
|
|
|
2017-05-26 21:05:19 +02:00
|
|
|
Status SstFileWriter::Merge(const Slice& user_key, const Slice& value) {
|
|
|
|
return rep_->Add(user_key, value, ValueType::kTypeMerge);
|
|
|
|
}
|
2015-09-23 21:42:43 +02:00
|
|
|
|
2017-05-26 21:05:19 +02:00
|
|
|
Status SstFileWriter::Delete(const Slice& user_key) {
|
|
|
|
return rep_->Add(user_key, Slice(), ValueType::kTypeDeletion);
|
2015-09-23 21:42:43 +02:00
|
|
|
}
|
|
|
|
|
2018-07-14 07:40:23 +02:00
|
|
|
Status SstFileWriter::DeleteRange(const Slice& begin_key,
|
|
|
|
const Slice& end_key) {
|
|
|
|
return rep_->DeleteRange(begin_key, end_key);
|
|
|
|
}
|
|
|
|
|
2015-09-23 21:42:43 +02:00
|
|
|
Status SstFileWriter::Finish(ExternalSstFileInfo* file_info) {
|
2017-05-23 20:36:06 +02:00
|
|
|
Rep* r = rep_.get();
|
2015-09-23 21:42:43 +02:00
|
|
|
if (!r->builder) {
|
|
|
|
return Status::InvalidArgument("File is not opened");
|
|
|
|
}
|
2018-07-14 07:40:23 +02:00
|
|
|
if (r->file_info.num_entries == 0 &&
|
|
|
|
r->file_info.num_range_del_entries == 0) {
|
2016-01-25 22:47:07 +01:00
|
|
|
return Status::InvalidArgument("Cannot create sst file with no entries");
|
|
|
|
}
|
2015-09-23 21:42:43 +02:00
|
|
|
|
|
|
|
Status s = r->builder->Finish();
|
2016-12-29 23:51:52 +01:00
|
|
|
r->file_info.file_size = r->builder->FileSize();
|
|
|
|
|
2015-09-23 21:42:43 +02:00
|
|
|
if (s.ok()) {
|
2017-02-13 19:54:38 +01:00
|
|
|
s = r->file_writer->Sync(r->ioptions.use_fsync);
|
2021-04-06 20:53:54 +02:00
|
|
|
r->InvalidatePageCache(true /* closing */).PermitUncheckedError();
|
2015-09-23 21:42:43 +02:00
|
|
|
if (s.ok()) {
|
|
|
|
s = r->file_writer->Close();
|
|
|
|
}
|
|
|
|
}
|
2020-05-20 20:53:49 +02:00
|
|
|
if (s.ok()) {
|
|
|
|
r->file_info.file_checksum = r->file_writer->GetFileChecksum();
|
|
|
|
r->file_info.file_checksum_func_name =
|
|
|
|
r->file_writer->GetFileChecksumFuncName();
|
|
|
|
}
|
2015-09-23 21:42:43 +02:00
|
|
|
if (!s.ok()) {
|
|
|
|
r->ioptions.env->DeleteFile(r->file_info.file_path);
|
|
|
|
}
|
|
|
|
|
2016-12-29 23:51:52 +01:00
|
|
|
if (file_info != nullptr) {
|
2015-09-23 21:42:43 +02:00
|
|
|
*file_info = r->file_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
r->builder.reset();
|
|
|
|
return s;
|
|
|
|
}
|
2016-12-17 03:26:56 +01:00
|
|
|
|
|
|
|
uint64_t SstFileWriter::FileSize() {
|
|
|
|
return rep_->file_info.file_size;
|
|
|
|
}
|
2017-02-28 20:05:08 +01:00
|
|
|
#endif // !ROCKSDB_LITE
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|