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).
|
2014-12-23 22:24:07 +01:00
|
|
|
//
|
|
|
|
// Copyright (c) 2012 The LevelDB Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
|
|
|
2015-07-20 20:24:54 +02:00
|
|
|
#ifndef ROCKSDB_LITE
|
|
|
|
|
2014-12-23 22:24:07 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
#include "rocksdb/sst_dump_tool.h"
|
|
|
|
|
2019-09-16 19:31:27 +02:00
|
|
|
#include "file/random_access_file_reader.h"
|
2019-10-09 04:17:39 +02:00
|
|
|
#include "port/stack_trace.h"
|
2014-12-23 22:24:07 +01:00
|
|
|
#include "rocksdb/filter_policy.h"
|
2019-05-30 23:47:29 +02:00
|
|
|
#include "table/block_based/block_based_table_factory.h"
|
2014-12-23 22:24:07 +01:00
|
|
|
#include "table/table_builder.h"
|
2019-05-30 20:21:38 +02:00
|
|
|
#include "test_util/testharness.h"
|
|
|
|
#include "test_util/testutil.h"
|
2014-12-23 22:24:07 +01:00
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
const uint32_t optLength = 100;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
static std::string MakeKey(int i) {
|
|
|
|
char buf[100];
|
|
|
|
snprintf(buf, sizeof(buf), "k_%04d", i);
|
|
|
|
InternalKey key(std::string(buf), 0, ValueType::kTypeValue);
|
|
|
|
return key.Encode().ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string MakeValue(int i) {
|
|
|
|
char buf[100];
|
|
|
|
snprintf(buf, sizeof(buf), "v_%04d", i);
|
|
|
|
InternalKey key(std::string(buf), 0, ValueType::kTypeValue);
|
|
|
|
return key.Encode().ToString();
|
|
|
|
}
|
|
|
|
|
2019-01-03 20:11:09 +01:00
|
|
|
void createSST(const Options& opts, const std::string& file_name) {
|
|
|
|
Env* env = opts.env;
|
|
|
|
EnvOptions env_options(opts);
|
2014-12-23 22:24:07 +01:00
|
|
|
ReadOptions read_options;
|
|
|
|
const ImmutableCFOptions imoptions(opts);
|
2018-05-21 23:33:55 +02:00
|
|
|
const MutableCFOptions moptions(opts);
|
2014-12-23 22:24:07 +01:00
|
|
|
rocksdb::InternalKeyComparator ikc(opts.comparator);
|
2018-11-09 20:17:34 +01:00
|
|
|
std::unique_ptr<TableBuilder> tb;
|
2014-12-23 22:24:07 +01:00
|
|
|
|
2019-01-03 20:11:09 +01:00
|
|
|
std::unique_ptr<WritableFile> file;
|
2018-08-09 23:21:35 +02:00
|
|
|
ASSERT_OK(env->NewWritableFile(file_name, &file, env_options));
|
|
|
|
|
A new call back to TablePropertiesCollector to allow users know the entry is add, delete or merge
Summary:
Currently users have no idea a key is add, delete or merge from TablePropertiesCollector call back. Add a new function to add it.
Also refactor the codes so that
(1) make table property collector and internal table property collector two separate data structures with the later one now exposed
(2) table builders only receive internal table properties
Test Plan: Add cases in table_properties_collector_test to cover both of old and new ways of using TablePropertiesCollector.
Reviewers: yhchiang, igor.sugak, rven, igor
Reviewed By: rven, igor
Subscribers: meyering, yoshinorim, maykov, leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D35373
2015-04-06 19:04:30 +02:00
|
|
|
std::vector<std::unique_ptr<IntTblPropCollectorFactory> >
|
|
|
|
int_tbl_prop_collector_factories;
|
Introduce a new storage specific Env API (#5761)
Summary:
The current Env API encompasses both storage/file operations, as well as OS related operations. Most of the APIs return a Status, which does not have enough metadata about an error, such as whether its retry-able or not, scope (i.e fault domain) of the error etc., that may be required in order to properly handle a storage error. The file APIs also do not provide enough control over the IO SLA, such as timeout, prioritization, hinting about placement and redundancy etc.
This PR separates out the file/storage APIs from Env into a new FileSystem class. The APIs are updated to return an IOStatus with metadata about the error, as well as to take an IOOptions structure as input in order to allow more control over the IO.
The user can set both ```options.env``` and ```options.file_system``` to specify that RocksDB should use the former for OS related operations and the latter for storage operations. Internally, a ```CompositeEnvWrapper``` has been introduced that inherits from ```Env``` and redirects individual methods to either an ```Env``` implementation or the ```FileSystem``` as appropriate. When options are sanitized during ```DB::Open```, ```options.env``` is replaced with a newly allocated ```CompositeEnvWrapper``` instance if both env and file_system have been specified. This way, the rest of the RocksDB code can continue to function as before.
This PR also ports PosixEnv to the new API by splitting it into two - PosixEnv and PosixFileSystem. PosixEnv is defined as a sub-class of CompositeEnvWrapper, and threading/time functions are overridden with Posix specific implementations in order to avoid an extra level of indirection.
The ```CompositeEnvWrapper``` translates ```IOStatus``` return code to ```Status```, and sets the severity to ```kSoftError``` if the io_status is retryable. The error handling code in RocksDB can then recover the DB automatically.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/5761
Differential Revision: D18868376
Pulled By: anand1976
fbshipit-source-id: 39efe18a162ea746fabac6360ff529baba48486f
2019-12-13 23:47:08 +01:00
|
|
|
std::unique_ptr<WritableFileWriter> file_writer(new WritableFileWriter(
|
|
|
|
NewLegacyWritableFileWrapper(std::move(file)), file_name, EnvOptions()));
|
2016-04-07 08:10:32 +02:00
|
|
|
std::string column_family_name;
|
2016-09-18 07:30:43 +02:00
|
|
|
int unknown_level = -1;
|
A new call back to TablePropertiesCollector to allow users know the entry is add, delete or merge
Summary:
Currently users have no idea a key is add, delete or merge from TablePropertiesCollector call back. Add a new function to add it.
Also refactor the codes so that
(1) make table property collector and internal table property collector two separate data structures with the later one now exposed
(2) table builders only receive internal table properties
Test Plan: Add cases in table_properties_collector_test to cover both of old and new ways of using TablePropertiesCollector.
Reviewers: yhchiang, igor.sugak, rven, igor
Reviewed By: rven, igor
Subscribers: meyering, yoshinorim, maykov, leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D35373
2015-04-06 19:04:30 +02:00
|
|
|
tb.reset(opts.table_factory->NewTableBuilder(
|
2018-05-21 23:33:55 +02:00
|
|
|
TableBuilderOptions(
|
|
|
|
imoptions, moptions, ikc, &int_tbl_prop_collector_factories,
|
2019-03-18 20:07:35 +01:00
|
|
|
CompressionType::kNoCompression, 0 /* sample_for_compression */,
|
|
|
|
CompressionOptions(), false /* skip_filters */, column_family_name,
|
|
|
|
unknown_level),
|
2015-10-09 01:57:35 +02:00
|
|
|
TablePropertiesCollectorFactory::Context::kUnknownColumnFamily,
|
Move rate_limiter, write buffering, most perf context instrumentation and most random kill out of Env
Summary: We want to keep Env a think layer for better portability. Less platform dependent codes should be moved out of Env. In this patch, I create a wrapper of file readers and writers, and put rate limiting, write buffering, as well as most perf context instrumentation and random kill out of Env. It will make it easier to maintain multiple Env in the future.
Test Plan: Run all existing unit tests.
Reviewers: anthony, kradhakrishnan, IslamAbdelRahman, yhchiang, igor
Reviewed By: igor
Subscribers: leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D42321
2015-07-18 01:16:11 +02:00
|
|
|
file_writer.get()));
|
2014-12-23 22:24:07 +01:00
|
|
|
|
|
|
|
// Populate slightly more than 1K keys
|
|
|
|
uint32_t num_keys = 1024;
|
|
|
|
for (uint32_t i = 0; i < num_keys; i++) {
|
|
|
|
tb->Add(MakeKey(i), MakeValue(i));
|
|
|
|
}
|
|
|
|
tb->Finish();
|
Move rate_limiter, write buffering, most perf context instrumentation and most random kill out of Env
Summary: We want to keep Env a think layer for better portability. Less platform dependent codes should be moved out of Env. In this patch, I create a wrapper of file readers and writers, and put rate limiting, write buffering, as well as most perf context instrumentation and random kill out of Env. It will make it easier to maintain multiple Env in the future.
Test Plan: Run all existing unit tests.
Reviewers: anthony, kradhakrishnan, IslamAbdelRahman, yhchiang, igor
Reviewed By: igor
Subscribers: leveldb, dhruba
Differential Revision: https://reviews.facebook.net/D42321
2015-07-18 01:16:11 +02:00
|
|
|
file_writer->Close();
|
2014-12-23 22:24:07 +01:00
|
|
|
}
|
|
|
|
|
2019-01-03 20:11:09 +01:00
|
|
|
void cleanup(const Options& opts, const std::string& file_name) {
|
|
|
|
Env* env = opts.env;
|
2014-12-23 22:24:07 +01:00
|
|
|
env->DeleteFile(file_name);
|
|
|
|
std::string outfile_name = file_name.substr(0, file_name.length() - 4);
|
|
|
|
outfile_name.append("_dump.txt");
|
|
|
|
env->DeleteFile(outfile_name);
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Test for sst dump tool "raw" mode
|
2015-03-17 22:08:00 +01:00
|
|
|
class SSTDumpToolTest : public testing::Test {
|
2019-10-09 04:17:39 +02:00
|
|
|
std::string test_dir_;
|
|
|
|
Env* env_;
|
|
|
|
std::shared_ptr<Env> env_guard_;
|
2018-08-23 19:04:10 +02:00
|
|
|
|
|
|
|
public:
|
2019-10-09 04:17:39 +02:00
|
|
|
SSTDumpToolTest() : env_(Env::Default()) {
|
|
|
|
const char* test_env_uri = getenv("TEST_ENV_URI");
|
|
|
|
if (test_env_uri) {
|
|
|
|
Env::LoadEnv(test_env_uri, &env_, &env_guard_);
|
|
|
|
}
|
|
|
|
test_dir_ = test::PerThreadDBPath(env_, "sst_dump_test_db");
|
|
|
|
Status s = env_->CreateDirIfMissing(test_dir_);
|
|
|
|
EXPECT_OK(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
~SSTDumpToolTest() override {
|
|
|
|
if (getenv("KEEP_DB")) {
|
|
|
|
fprintf(stdout, "Data is still at %s\n", test_dir_.c_str());
|
|
|
|
} else {
|
|
|
|
EXPECT_OK(env_->DeleteDir(test_dir_));
|
|
|
|
}
|
|
|
|
}
|
2014-12-23 22:24:07 +01:00
|
|
|
|
2019-10-09 04:17:39 +02:00
|
|
|
Env* env() { return env_; }
|
2018-08-09 23:21:35 +02:00
|
|
|
|
|
|
|
std::string MakeFilePath(const std::string& file_name) const {
|
2019-10-09 04:17:39 +02:00
|
|
|
std::string path(test_dir_);
|
2018-08-09 23:21:35 +02:00
|
|
|
path.append("/").append(file_name);
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2018-08-23 19:04:10 +02:00
|
|
|
template <std::size_t N>
|
2018-08-09 23:21:35 +02:00
|
|
|
void PopulateCommandArgs(const std::string& file_path, const char* command,
|
2018-08-23 19:04:10 +02:00
|
|
|
char* (&usage)[N]) const {
|
2018-08-10 04:37:47 +02:00
|
|
|
for (int i = 0; i < static_cast<int>(N); ++i) {
|
2018-08-09 23:21:35 +02:00
|
|
|
usage[i] = new char[optLength];
|
|
|
|
}
|
|
|
|
snprintf(usage[0], optLength, "./sst_dump");
|
|
|
|
snprintf(usage[1], optLength, "%s", command);
|
|
|
|
snprintf(usage[2], optLength, "--file=%s", file_path.c_str());
|
|
|
|
}
|
2014-12-23 22:24:07 +01:00
|
|
|
};
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(SSTDumpToolTest, EmptyFilter) {
|
2019-01-03 20:11:09 +01:00
|
|
|
Options opts;
|
2019-10-09 04:17:39 +02:00
|
|
|
opts.env = env();
|
2018-08-09 23:21:35 +02:00
|
|
|
std::string file_path = MakeFilePath("rocksdb_sst_test.sst");
|
2019-01-03 20:11:09 +01:00
|
|
|
createSST(opts, file_path);
|
2014-12-23 22:24:07 +01:00
|
|
|
|
|
|
|
char* usage[3];
|
2018-08-09 23:21:35 +02:00
|
|
|
PopulateCommandArgs(file_path, "--command=raw", usage);
|
2014-12-23 22:24:07 +01:00
|
|
|
|
|
|
|
rocksdb::SSTDumpTool tool;
|
2019-01-03 20:11:09 +01:00
|
|
|
ASSERT_TRUE(!tool.Run(3, usage, opts));
|
2014-12-23 22:24:07 +01:00
|
|
|
|
2019-01-03 20:11:09 +01:00
|
|
|
cleanup(opts, file_path);
|
2014-12-23 22:24:07 +01:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
delete[] usage[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(SSTDumpToolTest, FilterBlock) {
|
2019-01-03 20:11:09 +01:00
|
|
|
Options opts;
|
2019-10-09 04:17:39 +02:00
|
|
|
opts.env = env();
|
2019-01-03 20:11:09 +01:00
|
|
|
BlockBasedTableOptions table_opts;
|
|
|
|
table_opts.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, true));
|
|
|
|
opts.table_factory.reset(new BlockBasedTableFactory(table_opts));
|
2018-08-09 23:21:35 +02:00
|
|
|
std::string file_path = MakeFilePath("rocksdb_sst_test.sst");
|
2019-01-03 20:11:09 +01:00
|
|
|
createSST(opts, file_path);
|
2014-12-23 22:24:07 +01:00
|
|
|
|
|
|
|
char* usage[3];
|
2018-08-09 23:21:35 +02:00
|
|
|
PopulateCommandArgs(file_path, "--command=raw", usage);
|
2014-12-23 22:24:07 +01:00
|
|
|
|
|
|
|
rocksdb::SSTDumpTool tool;
|
2019-01-03 20:11:09 +01:00
|
|
|
ASSERT_TRUE(!tool.Run(3, usage, opts));
|
2014-12-23 22:24:07 +01:00
|
|
|
|
2019-01-03 20:11:09 +01:00
|
|
|
cleanup(opts, file_path);
|
2014-12-23 22:24:07 +01:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
delete[] usage[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(SSTDumpToolTest, FullFilterBlock) {
|
2019-01-03 20:11:09 +01:00
|
|
|
Options opts;
|
2019-10-09 04:17:39 +02:00
|
|
|
opts.env = env();
|
2019-01-03 20:11:09 +01:00
|
|
|
BlockBasedTableOptions table_opts;
|
|
|
|
table_opts.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false));
|
|
|
|
opts.table_factory.reset(new BlockBasedTableFactory(table_opts));
|
2018-08-09 23:21:35 +02:00
|
|
|
std::string file_path = MakeFilePath("rocksdb_sst_test.sst");
|
2019-01-03 20:11:09 +01:00
|
|
|
createSST(opts, file_path);
|
2014-12-23 22:24:07 +01:00
|
|
|
|
|
|
|
char* usage[3];
|
2018-08-09 23:21:35 +02:00
|
|
|
PopulateCommandArgs(file_path, "--command=raw", usage);
|
2014-12-23 22:24:07 +01:00
|
|
|
|
|
|
|
rocksdb::SSTDumpTool tool;
|
2019-01-03 20:11:09 +01:00
|
|
|
ASSERT_TRUE(!tool.Run(3, usage, opts));
|
2014-12-23 22:24:07 +01:00
|
|
|
|
2019-01-03 20:11:09 +01:00
|
|
|
cleanup(opts, file_path);
|
2014-12-23 22:24:07 +01:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
delete[] usage[i];
|
|
|
|
}
|
|
|
|
}
|
2015-02-26 01:34:26 +01:00
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
TEST_F(SSTDumpToolTest, GetProperties) {
|
2019-01-03 20:11:09 +01:00
|
|
|
Options opts;
|
2019-10-09 04:17:39 +02:00
|
|
|
opts.env = env();
|
2019-01-03 20:11:09 +01:00
|
|
|
BlockBasedTableOptions table_opts;
|
|
|
|
table_opts.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false));
|
|
|
|
opts.table_factory.reset(new BlockBasedTableFactory(table_opts));
|
2018-08-09 23:21:35 +02:00
|
|
|
std::string file_path = MakeFilePath("rocksdb_sst_test.sst");
|
2019-01-03 20:11:09 +01:00
|
|
|
createSST(opts, file_path);
|
2015-02-26 01:34:26 +01:00
|
|
|
|
|
|
|
char* usage[3];
|
2018-08-09 23:21:35 +02:00
|
|
|
PopulateCommandArgs(file_path, "--show_properties", usage);
|
2015-02-26 01:34:26 +01:00
|
|
|
|
|
|
|
rocksdb::SSTDumpTool tool;
|
2019-01-03 20:11:09 +01:00
|
|
|
ASSERT_TRUE(!tool.Run(3, usage, opts));
|
2015-02-26 01:34:26 +01:00
|
|
|
|
2019-01-03 20:11:09 +01:00
|
|
|
cleanup(opts, file_path);
|
2015-02-26 01:34:26 +01:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
delete[] usage[i];
|
|
|
|
}
|
|
|
|
}
|
2015-07-24 02:05:33 +02:00
|
|
|
|
|
|
|
TEST_F(SSTDumpToolTest, CompressedSizes) {
|
2019-01-03 20:11:09 +01:00
|
|
|
Options opts;
|
2019-10-09 04:17:39 +02:00
|
|
|
opts.env = env();
|
2019-01-03 20:11:09 +01:00
|
|
|
BlockBasedTableOptions table_opts;
|
|
|
|
table_opts.filter_policy.reset(rocksdb::NewBloomFilterPolicy(10, false));
|
|
|
|
opts.table_factory.reset(new BlockBasedTableFactory(table_opts));
|
2018-08-09 23:21:35 +02:00
|
|
|
std::string file_path = MakeFilePath("rocksdb_sst_test.sst");
|
2019-01-03 20:11:09 +01:00
|
|
|
createSST(opts, file_path);
|
2015-07-24 02:05:33 +02:00
|
|
|
|
|
|
|
char* usage[3];
|
2018-08-09 23:21:35 +02:00
|
|
|
PopulateCommandArgs(file_path, "--command=recompress", usage);
|
2015-07-24 02:05:33 +02:00
|
|
|
|
|
|
|
rocksdb::SSTDumpTool tool;
|
2019-01-03 20:11:09 +01:00
|
|
|
ASSERT_TRUE(!tool.Run(3, usage, opts));
|
2015-07-24 02:05:33 +02:00
|
|
|
|
2019-01-03 20:11:09 +01:00
|
|
|
cleanup(opts, file_path);
|
2015-07-24 02:05:33 +02:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
delete[] usage[i];
|
|
|
|
}
|
|
|
|
}
|
2019-01-03 20:11:09 +01:00
|
|
|
|
|
|
|
TEST_F(SSTDumpToolTest, MemEnv) {
|
2019-10-09 04:17:39 +02:00
|
|
|
std::unique_ptr<Env> mem_env(NewMemEnv(env()));
|
2019-01-03 20:11:09 +01:00
|
|
|
Options opts;
|
2019-10-09 04:17:39 +02:00
|
|
|
opts.env = mem_env.get();
|
2019-01-03 20:11:09 +01:00
|
|
|
std::string file_path = MakeFilePath("rocksdb_sst_test.sst");
|
|
|
|
createSST(opts, file_path);
|
|
|
|
|
|
|
|
char* usage[3];
|
|
|
|
PopulateCommandArgs(file_path, "--command=verify_checksum", usage);
|
|
|
|
|
|
|
|
rocksdb::SSTDumpTool tool;
|
|
|
|
ASSERT_TRUE(!tool.Run(3, usage, opts));
|
|
|
|
|
|
|
|
cleanup(opts, file_path);
|
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
delete[] usage[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-23 22:24:07 +01:00
|
|
|
} // namespace rocksdb
|
|
|
|
|
2019-10-09 04:17:39 +02:00
|
|
|
#ifdef ROCKSDB_UNITTESTS_WITH_CUSTOM_OBJECTS_FROM_STATIC_LIBS
|
|
|
|
extern "C" {
|
|
|
|
void RegisterCustomObjects(int argc, char** argv);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void RegisterCustomObjects(int /*argc*/, char** /*argv*/) {}
|
|
|
|
#endif // !ROCKSDB_UNITTESTS_WITH_CUSTOM_OBJECTS_FROM_STATIC_LIBS
|
|
|
|
|
2015-03-17 22:08:00 +01:00
|
|
|
int main(int argc, char** argv) {
|
2019-10-09 04:17:39 +02:00
|
|
|
rocksdb::port::InstallStackTraceHandler();
|
2015-03-17 22:08:00 +01:00
|
|
|
::testing::InitGoogleTest(&argc, argv);
|
2019-10-09 04:17:39 +02:00
|
|
|
RegisterCustomObjects(argc, argv);
|
2015-03-17 22:08:00 +01:00
|
|
|
return RUN_ALL_TESTS();
|
|
|
|
}
|
2015-07-20 20:24:54 +02:00
|
|
|
|
|
|
|
#else
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2018-04-16 02:19:57 +02:00
|
|
|
int main(int /*argc*/, char** /*argv*/) {
|
2015-07-20 20:24:54 +02:00
|
|
|
fprintf(stderr, "SKIPPED as SSTDumpTool is not supported in ROCKSDB_LITE\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // !ROCKSDB_LITE return RUN_ALL_TESTS();
|