2016-02-09 23:12:00 +00:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
2015-09-23 19:42:43 +00:00
|
|
|
// This source code is licensed under the BSD-style license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
|
|
|
|
#include "rocksdb/sst_file_writer.h"
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
#include "db/dbformat.h"
|
|
|
|
#include "rocksdb/table.h"
|
|
|
|
#include "table/block_based_table_builder.h"
|
2016-10-18 23:59:37 +00:00
|
|
|
#include "table/sst_file_writer_collectors.h"
|
2015-09-23 19:42:43 +00:00
|
|
|
#include "util/file_reader_writer.h"
|
2016-12-29 22:51:52 +00:00
|
|
|
#include "util/sync_point.h"
|
2015-09-23 19:42:43 +00:00
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
const std::string ExternalSstFilePropertyNames::kVersion =
|
|
|
|
"rocksdb.external_sst_file.version";
|
2016-10-18 23:59:37 +00:00
|
|
|
const std::string ExternalSstFilePropertyNames::kGlobalSeqno =
|
|
|
|
"rocksdb.external_sst_file.global_seqno";
|
2017-02-28 19:05:08 +00:00
|
|
|
|
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2016-12-29 22:51:52 +00:00
|
|
|
const size_t kFadviseTrigger = 1024 * 1024; // 1MB
|
2015-09-23 19:42:43 +00:00
|
|
|
|
|
|
|
struct SstFileWriter::Rep {
|
2016-05-17 20:11:56 +00:00
|
|
|
Rep(const EnvOptions& _env_options, const Options& options,
|
2016-12-29 22:51:52 +00:00
|
|
|
const Comparator* _user_comparator, ColumnFamilyHandle* _cfh,
|
|
|
|
bool _invalidate_page_cache)
|
2015-09-23 19:42:43 +00:00
|
|
|
: env_options(_env_options),
|
2016-05-17 20:11:56 +00:00
|
|
|
ioptions(options),
|
2016-09-14 04:11:59 +00:00
|
|
|
mutable_cf_options(options),
|
2016-12-05 22:16:23 +00:00
|
|
|
internal_comparator(_user_comparator),
|
2016-12-29 22:51:52 +00:00
|
|
|
cfh(_cfh),
|
|
|
|
invalidate_page_cache(_invalidate_page_cache),
|
|
|
|
last_fadvise_size(0) {}
|
2015-09-23 19:42:43 +00:00
|
|
|
|
|
|
|
std::unique_ptr<WritableFileWriter> file_writer;
|
|
|
|
std::unique_ptr<TableBuilder> builder;
|
|
|
|
EnvOptions env_options;
|
|
|
|
ImmutableCFOptions ioptions;
|
2016-05-17 20:11:56 +00:00
|
|
|
MutableCFOptions mutable_cf_options;
|
2015-09-23 19:42:43 +00:00
|
|
|
InternalKeyComparator internal_comparator;
|
|
|
|
ExternalSstFileInfo file_info;
|
2016-06-13 16:57:43 +00:00
|
|
|
InternalKey ikey;
|
2016-12-05 22:16:23 +00:00
|
|
|
std::string column_family_name;
|
|
|
|
ColumnFamilyHandle* cfh;
|
2016-12-29 22:51:52 +00:00
|
|
|
// If true, We will give the OS a hint that this file pages is not needed
|
|
|
|
// everytime we write 1MB to the file
|
|
|
|
bool invalidate_page_cache;
|
|
|
|
// the size of the file during the last time we called Fadvise to remove
|
|
|
|
// cached pages from page cache.
|
|
|
|
uint64_t last_fadvise_size;
|
2015-09-23 19:42:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SstFileWriter::SstFileWriter(const EnvOptions& env_options,
|
2016-05-17 20:11:56 +00:00
|
|
|
const Options& options,
|
2016-12-05 22:16:23 +00:00
|
|
|
const Comparator* user_comparator,
|
2016-12-29 22:51:52 +00:00
|
|
|
ColumnFamilyHandle* column_family,
|
|
|
|
bool invalidate_page_cache)
|
|
|
|
: rep_(new Rep(env_options, options, user_comparator, column_family,
|
|
|
|
invalidate_page_cache)) {
|
|
|
|
rep_->file_info.file_size = 0;
|
|
|
|
}
|
2015-09-23 19:42:43 +00:00
|
|
|
|
2016-11-13 04:03:39 +00:00
|
|
|
SstFileWriter::~SstFileWriter() {
|
|
|
|
if (rep_->builder) {
|
|
|
|
// User did not call Finish() or Finish() failed, we need to
|
|
|
|
// abandon the builder.
|
|
|
|
rep_->builder->Abandon();
|
|
|
|
}
|
|
|
|
|
|
|
|
delete rep_;
|
|
|
|
}
|
2015-09-23 19:42:43 +00:00
|
|
|
|
|
|
|
Status SstFileWriter::Open(const std::string& file_path) {
|
|
|
|
Rep* r = rep_;
|
|
|
|
Status s;
|
|
|
|
std::unique_ptr<WritableFile> sst_file;
|
|
|
|
s = r->ioptions.env->NewWritableFile(file_path, &sst_file, r->env_options);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2016-06-20 18:26:25 +00:00
|
|
|
CompressionType compression_type;
|
|
|
|
if (r->ioptions.bottommost_compression != kDisableCompressionOption) {
|
|
|
|
compression_type = r->ioptions.bottommost_compression;
|
|
|
|
} else if (!r->ioptions.compression_per_level.empty()) {
|
2015-09-23 19:42:43 +00:00
|
|
|
// Use the compression of the last level if we have per level compression
|
|
|
|
compression_type = *(r->ioptions.compression_per_level.rbegin());
|
2016-06-20 18:26:25 +00:00
|
|
|
} else {
|
|
|
|
compression_type = r->mutable_cf_options.compression;
|
2015-09-23 19:42:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<std::unique_ptr<IntTblPropCollectorFactory>>
|
|
|
|
int_tbl_prop_collector_factories;
|
2016-08-19 23:17:56 +00:00
|
|
|
|
|
|
|
// SstFileWriter properties collector to add SstFileWriter version.
|
2015-09-23 19:42:43 +00:00
|
|
|
int_tbl_prop_collector_factories.emplace_back(
|
2016-10-18 23:59:37 +00:00
|
|
|
new SstFileWriterPropertiesCollectorFactory(2 /* version */,
|
|
|
|
0 /* global_seqno*/));
|
2015-09-23 19:42:43 +00:00
|
|
|
|
2016-08-19 23:17:56 +00: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 05:30:43 +00:00
|
|
|
int unknown_level = -1;
|
2016-12-05 22:16:23 +00: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;
|
|
|
|
}
|
|
|
|
|
2015-09-23 19:42:43 +00:00
|
|
|
TableBuilderOptions table_builder_options(
|
|
|
|
r->ioptions, r->internal_comparator, &int_tbl_prop_collector_factories,
|
Shared dictionary compression using reference block
Summary:
This adds a new metablock containing a shared dictionary that is used
to compress all data blocks in the SST file. The size of the shared dictionary
is configurable in CompressionOptions and defaults to 0. It's currently only
used for zlib/lz4/lz4hc, but the block will be stored in the SST regardless of
the compression type if the user chooses a nonzero dictionary size.
During compaction, computes the dictionary by randomly sampling the first
output file in each subcompaction. It pre-computes the intervals to sample
by assuming the output file will have the maximum allowable length. In case
the file is smaller, some of the pre-computed sampling intervals can be beyond
end-of-file, in which case we skip over those samples and the dictionary will
be a bit smaller. After the dictionary is generated using the first file in a
subcompaction, it is loaded into the compression library before writing each
block in each subsequent file of that subcompaction.
On the read path, gets the dictionary from the metablock, if it exists. Then,
loads that dictionary into the compression library before reading each block.
Test Plan: new unit test
Reviewers: yhchiang, IslamAbdelRahman, cyan, sdong
Reviewed By: sdong
Subscribers: andrewkr, yoshinorim, kradhakrishnan, dhruba, leveldb
Differential Revision: https://reviews.facebook.net/D52287
2016-04-28 00:36:03 +00:00
|
|
|
compression_type, r->ioptions.compression_opts,
|
|
|
|
nullptr /* compression_dict */, false /* skip_filters */,
|
2016-09-18 05:30:43 +00:00
|
|
|
r->column_family_name, unknown_level);
|
2015-09-23 19:42:43 +00:00
|
|
|
r->file_writer.reset(
|
|
|
|
new WritableFileWriter(std::move(sst_file), r->env_options));
|
2016-10-18 23:59:37 +00: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 19:42:43 +00:00
|
|
|
r->builder.reset(r->ioptions.table_factory->NewTableBuilder(
|
2016-12-05 22:16:23 +00:00
|
|
|
table_builder_options, cf_id, r->file_writer.get()));
|
2015-09-23 19:42:43 +00:00
|
|
|
|
|
|
|
r->file_info.file_path = file_path;
|
|
|
|
r->file_info.file_size = 0;
|
|
|
|
r->file_info.num_entries = 0;
|
|
|
|
r->file_info.sequence_number = 0;
|
2016-10-18 23:59:37 +00:00
|
|
|
r->file_info.version = 2;
|
2015-09-23 19:42:43 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SstFileWriter::Add(const Slice& user_key, const Slice& value) {
|
|
|
|
Rep* r = rep_;
|
|
|
|
if (!r->builder) {
|
|
|
|
return Status::InvalidArgument("File is not opened");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r->file_info.num_entries == 0) {
|
2016-06-13 16:57:43 +00:00
|
|
|
r->file_info.smallest_key.assign(user_key.data(), user_key.size());
|
2015-09-23 19:42:43 +00:00
|
|
|
} else {
|
|
|
|
if (r->internal_comparator.user_comparator()->Compare(
|
|
|
|
user_key, r->file_info.largest_key) <= 0) {
|
|
|
|
// Make sure that keys are added in order
|
|
|
|
return Status::InvalidArgument("Keys must be added in order");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-29 22:51:52 +00:00
|
|
|
// TODO(tec) : For external SST files we could omit the seqno and type.
|
|
|
|
r->ikey.Set(user_key, 0 /* Sequence Number */,
|
|
|
|
ValueType::kTypeValue /* Put */);
|
|
|
|
r->builder->Add(r->ikey.Encode(), value);
|
|
|
|
|
2015-09-23 19:42:43 +00:00
|
|
|
// update file info
|
|
|
|
r->file_info.num_entries++;
|
2016-06-13 16:57:43 +00:00
|
|
|
r->file_info.largest_key.assign(user_key.data(), user_key.size());
|
2015-09-23 19:42:43 +00:00
|
|
|
r->file_info.file_size = r->builder->FileSize();
|
|
|
|
|
2016-12-29 22:51:52 +00:00
|
|
|
InvalidatePageCache(false /* closing */);
|
2015-09-23 19:42:43 +00:00
|
|
|
|
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
Status SstFileWriter::Finish(ExternalSstFileInfo* file_info) {
|
|
|
|
Rep* r = rep_;
|
|
|
|
if (!r->builder) {
|
|
|
|
return Status::InvalidArgument("File is not opened");
|
|
|
|
}
|
2016-01-25 21:47:07 +00:00
|
|
|
if (r->file_info.num_entries == 0) {
|
|
|
|
return Status::InvalidArgument("Cannot create sst file with no entries");
|
|
|
|
}
|
2015-09-23 19:42:43 +00:00
|
|
|
|
|
|
|
Status s = r->builder->Finish();
|
2016-12-29 22:51:52 +00:00
|
|
|
r->file_info.file_size = r->builder->FileSize();
|
|
|
|
|
2015-09-23 19:42:43 +00:00
|
|
|
if (s.ok()) {
|
2017-02-13 18:54:38 +00:00
|
|
|
s = r->file_writer->Sync(r->ioptions.use_fsync);
|
2016-12-29 22:51:52 +00:00
|
|
|
InvalidatePageCache(true /* closing */);
|
2015-09-23 19:42:43 +00:00
|
|
|
if (s.ok()) {
|
|
|
|
s = r->file_writer->Close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!s.ok()) {
|
|
|
|
r->ioptions.env->DeleteFile(r->file_info.file_path);
|
|
|
|
}
|
|
|
|
|
2016-12-29 22:51:52 +00:00
|
|
|
if (file_info != nullptr) {
|
2015-09-23 19:42:43 +00:00
|
|
|
*file_info = r->file_info;
|
|
|
|
}
|
|
|
|
|
|
|
|
r->builder.reset();
|
|
|
|
return s;
|
|
|
|
}
|
2016-12-17 02:26:56 +00:00
|
|
|
|
2016-12-29 22:51:52 +00:00
|
|
|
void SstFileWriter::InvalidatePageCache(bool closing) {
|
|
|
|
Rep* r = rep_;
|
|
|
|
if (r->invalidate_page_cache == false) {
|
|
|
|
// Fadvise disabled
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t bytes_since_last_fadvise =
|
|
|
|
r->builder->FileSize() - r->last_fadvise_size;
|
|
|
|
if (bytes_since_last_fadvise > kFadviseTrigger || closing) {
|
|
|
|
TEST_SYNC_POINT_CALLBACK("SstFileWriter::InvalidatePageCache",
|
|
|
|
&(bytes_since_last_fadvise));
|
|
|
|
// Tell the OS that we dont need this file in page cache
|
|
|
|
r->file_writer->InvalidateCache(0, 0);
|
|
|
|
r->last_fadvise_size = r->builder->FileSize();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-17 02:26:56 +00:00
|
|
|
uint64_t SstFileWriter::FileSize() {
|
|
|
|
return rep_->file_info.file_size;
|
|
|
|
}
|
2017-02-28 19:05:08 +00:00
|
|
|
#endif // !ROCKSDB_LITE
|
|
|
|
|
2015-09-23 19:42:43 +00:00
|
|
|
} // namespace rocksdb
|