2013-12-05 21:09:13 +00:00
|
|
|
// Copyright (c) 2013, Facebook, Inc. All rights reserved.
|
|
|
|
// 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 "table/meta_blocks.h"
|
|
|
|
|
|
|
|
#include <map>
|
2014-02-05 00:21:47 +00:00
|
|
|
#include <string>
|
2013-12-05 21:09:13 +00:00
|
|
|
|
2014-01-28 05:58:46 +00:00
|
|
|
#include "rocksdb/table.h"
|
2014-02-05 00:21:47 +00:00
|
|
|
#include "rocksdb/table_properties.h"
|
2013-12-06 00:51:26 +00:00
|
|
|
#include "table/block.h"
|
2013-12-05 21:09:13 +00:00
|
|
|
#include "table/format.h"
|
|
|
|
#include "util/coding.h"
|
|
|
|
|
|
|
|
namespace rocksdb {
|
|
|
|
|
|
|
|
MetaIndexBuilder::MetaIndexBuilder()
|
|
|
|
: meta_index_block_(
|
|
|
|
new BlockBuilder(1 /* restart interval */, BytewiseComparator())) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void MetaIndexBuilder::Add(const std::string& key,
|
|
|
|
const BlockHandle& handle) {
|
|
|
|
std::string handle_encoding;
|
|
|
|
handle.EncodeTo(&handle_encoding);
|
|
|
|
meta_block_handles_.insert({key, handle_encoding});
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice MetaIndexBuilder::Finish() {
|
|
|
|
for (const auto& metablock : meta_block_handles_) {
|
|
|
|
meta_index_block_->Add(metablock.first, metablock.second);
|
|
|
|
}
|
|
|
|
return meta_index_block_->Finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
PropertyBlockBuilder::PropertyBlockBuilder()
|
|
|
|
: properties_block_(
|
|
|
|
new BlockBuilder(1 /* restart interval */, BytewiseComparator())) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropertyBlockBuilder::Add(const std::string& name,
|
|
|
|
const std::string& val) {
|
|
|
|
props_.insert({name, val});
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropertyBlockBuilder::Add(const std::string& name, uint64_t val) {
|
|
|
|
assert(props_.find(name) == props_.end());
|
|
|
|
|
|
|
|
std::string dst;
|
|
|
|
PutVarint64(&dst, val);
|
|
|
|
|
|
|
|
Add(name, dst);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropertyBlockBuilder::Add(
|
|
|
|
const UserCollectedProperties& user_collected_properties) {
|
|
|
|
for (const auto& prop : user_collected_properties) {
|
|
|
|
Add(prop.first, prop.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void PropertyBlockBuilder::AddTableProperty(const TableProperties& props) {
|
|
|
|
Add(TablePropertiesNames::kRawKeySize, props.raw_key_size);
|
|
|
|
Add(TablePropertiesNames::kRawValueSize, props.raw_value_size);
|
|
|
|
Add(TablePropertiesNames::kDataSize, props.data_size);
|
|
|
|
Add(TablePropertiesNames::kIndexSize, props.index_size);
|
|
|
|
Add(TablePropertiesNames::kNumEntries, props.num_entries);
|
|
|
|
Add(TablePropertiesNames::kNumDataBlocks, props.num_data_blocks);
|
|
|
|
Add(TablePropertiesNames::kFilterSize, props.filter_size);
|
2013-12-20 17:35:24 +00:00
|
|
|
Add(TablePropertiesNames::kFormatVersion, props.format_version);
|
|
|
|
Add(TablePropertiesNames::kFixedKeyLen, props.fixed_key_len);
|
2013-12-05 21:09:13 +00:00
|
|
|
|
|
|
|
if (!props.filter_policy_name.empty()) {
|
|
|
|
Add(TablePropertiesNames::kFilterPolicy,
|
|
|
|
props.filter_policy_name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Slice PropertyBlockBuilder::Finish() {
|
|
|
|
for (const auto& prop : props_) {
|
|
|
|
properties_block_->Add(prop.first, prop.second);
|
|
|
|
}
|
|
|
|
|
|
|
|
return properties_block_->Finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
void LogPropertiesCollectionError(
|
|
|
|
Logger* info_log, const std::string& method, const std::string& name) {
|
|
|
|
assert(method == "Add" || method == "Finish");
|
|
|
|
|
|
|
|
std::string msg =
|
|
|
|
"[Warning] encountered error when calling TablePropertiesCollector::" +
|
|
|
|
method + "() with collector name: " + name;
|
|
|
|
Log(info_log, "%s", msg.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NotifyCollectTableCollectorsOnAdd(
|
|
|
|
const Slice& key,
|
|
|
|
const Slice& value,
|
|
|
|
const Options::TablePropertiesCollectors& collectors,
|
|
|
|
Logger* info_log) {
|
|
|
|
bool all_succeeded = true;
|
|
|
|
for (auto collector : collectors) {
|
|
|
|
Status s = collector->Add(key, value);
|
|
|
|
all_succeeded = all_succeeded && s.ok();
|
|
|
|
if (!s.ok()) {
|
2014-02-05 00:21:47 +00:00
|
|
|
LogPropertiesCollectionError(info_log, "Add" /* method */,
|
|
|
|
collector->Name());
|
2013-12-05 21:09:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return all_succeeded;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool NotifyCollectTableCollectorsOnFinish(
|
|
|
|
const Options::TablePropertiesCollectors& collectors,
|
|
|
|
Logger* info_log,
|
|
|
|
PropertyBlockBuilder* builder) {
|
|
|
|
bool all_succeeded = true;
|
|
|
|
for (auto collector : collectors) {
|
|
|
|
UserCollectedProperties user_collected_properties;
|
|
|
|
Status s = collector->Finish(&user_collected_properties);
|
|
|
|
|
|
|
|
all_succeeded = all_succeeded && s.ok();
|
|
|
|
if (!s.ok()) {
|
2014-02-05 00:21:47 +00:00
|
|
|
LogPropertiesCollectionError(info_log, "Finish" /* method */,
|
|
|
|
collector->Name());
|
2013-12-05 21:09:13 +00:00
|
|
|
} else {
|
|
|
|
builder->Add(user_collected_properties);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return all_succeeded;
|
|
|
|
}
|
|
|
|
|
2014-02-08 03:26:49 +00:00
|
|
|
Status ReadProperties(const Slice& handle_value, RandomAccessFile* file,
|
|
|
|
Env* env, Logger* logger,
|
|
|
|
TableProperties** table_properties) {
|
2013-12-06 00:51:26 +00:00
|
|
|
assert(table_properties);
|
|
|
|
|
|
|
|
Slice v = handle_value;
|
|
|
|
BlockHandle handle;
|
|
|
|
if (!handle.DecodeFrom(&v).ok()) {
|
|
|
|
return Status::InvalidArgument("Failed to decode properties block handle");
|
|
|
|
}
|
|
|
|
|
|
|
|
BlockContents block_contents;
|
2014-02-06 04:56:14 +00:00
|
|
|
ReadOptions read_options;
|
|
|
|
read_options.verify_checksums = false;
|
2014-02-05 00:21:47 +00:00
|
|
|
Status s = ReadBlockContents(file, read_options, handle, &block_contents, env,
|
|
|
|
false);
|
2013-12-06 00:51:26 +00:00
|
|
|
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
Block properties_block(block_contents);
|
|
|
|
std::unique_ptr<Iterator> iter(
|
2014-02-05 00:21:47 +00:00
|
|
|
properties_block.NewIterator(BytewiseComparator()));
|
2013-12-06 00:51:26 +00:00
|
|
|
|
2014-02-08 03:26:49 +00:00
|
|
|
auto new_table_properties = new TableProperties();
|
2013-12-06 00:51:26 +00:00
|
|
|
// All pre-defined properties of type uint64_t
|
|
|
|
std::unordered_map<std::string, uint64_t*> predefined_uint64_properties = {
|
2014-02-08 03:26:49 +00:00
|
|
|
{TablePropertiesNames::kDataSize, &new_table_properties->data_size},
|
|
|
|
{TablePropertiesNames::kIndexSize, &new_table_properties->index_size},
|
|
|
|
{TablePropertiesNames::kFilterSize, &new_table_properties->filter_size},
|
|
|
|
{TablePropertiesNames::kRawKeySize, &new_table_properties->raw_key_size},
|
|
|
|
{TablePropertiesNames::kRawValueSize,
|
|
|
|
&new_table_properties->raw_value_size},
|
2014-02-05 00:21:47 +00:00
|
|
|
{TablePropertiesNames::kNumDataBlocks,
|
2014-02-08 03:26:49 +00:00
|
|
|
&new_table_properties->num_data_blocks},
|
|
|
|
{TablePropertiesNames::kNumEntries, &new_table_properties->num_entries},
|
|
|
|
{TablePropertiesNames::kFormatVersion,
|
|
|
|
&new_table_properties->format_version},
|
|
|
|
{TablePropertiesNames::kFixedKeyLen,
|
|
|
|
&new_table_properties->fixed_key_len}, };
|
2013-12-06 00:51:26 +00:00
|
|
|
|
|
|
|
std::string last_key;
|
|
|
|
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
|
|
|
|
s = iter->status();
|
|
|
|
if (!s.ok()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto key = iter->key().ToString();
|
|
|
|
// properties block is strictly sorted with no duplicate key.
|
2014-02-05 00:21:47 +00:00
|
|
|
assert(last_key.empty() ||
|
|
|
|
BytewiseComparator()->Compare(key, last_key) > 0);
|
2013-12-06 00:51:26 +00:00
|
|
|
last_key = key;
|
|
|
|
|
|
|
|
auto raw_val = iter->value();
|
|
|
|
auto pos = predefined_uint64_properties.find(key);
|
|
|
|
|
|
|
|
if (pos != predefined_uint64_properties.end()) {
|
|
|
|
// handle predefined rocksdb properties
|
|
|
|
uint64_t val;
|
|
|
|
if (!GetVarint64(&raw_val, &val)) {
|
|
|
|
// skip malformed value
|
|
|
|
auto error_msg =
|
|
|
|
"[Warning] detect malformed value in properties meta-block:"
|
|
|
|
"\tkey: " + key + "\tval: " + raw_val.ToString();
|
|
|
|
Log(logger, "%s", error_msg.c_str());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*(pos->second) = val;
|
|
|
|
} else if (key == TablePropertiesNames::kFilterPolicy) {
|
2014-02-08 03:26:49 +00:00
|
|
|
new_table_properties->filter_policy_name = raw_val.ToString();
|
2013-12-06 00:51:26 +00:00
|
|
|
} else {
|
|
|
|
// handle user-collected properties
|
2014-02-08 03:26:49 +00:00
|
|
|
new_table_properties->user_collected_properties.insert(
|
2014-02-05 00:21:47 +00:00
|
|
|
{key, raw_val.ToString()});
|
2013-12-06 00:51:26 +00:00
|
|
|
}
|
|
|
|
}
|
2014-02-08 03:26:49 +00:00
|
|
|
if (s.ok()) {
|
|
|
|
*table_properties = new_table_properties;
|
|
|
|
} else {
|
|
|
|
delete new_table_properties;
|
|
|
|
}
|
2013-12-06 00:51:26 +00:00
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-02-08 03:26:49 +00:00
|
|
|
Status ReadTableProperties(RandomAccessFile* file, uint64_t file_size,
|
|
|
|
uint64_t table_magic_number, Env* env,
|
|
|
|
Logger* info_log, TableProperties** properties) {
|
2013-12-06 00:51:26 +00:00
|
|
|
// -- Read metaindex block
|
|
|
|
Footer footer(table_magic_number);
|
|
|
|
auto s = ReadFooterFromFile(file, file_size, &footer);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto metaindex_handle = footer.metaindex_handle();
|
|
|
|
BlockContents metaindex_contents;
|
2014-02-06 04:56:14 +00:00
|
|
|
ReadOptions read_options;
|
|
|
|
read_options.verify_checksums = false;
|
2014-02-05 00:21:47 +00:00
|
|
|
s = ReadBlockContents(file, read_options, metaindex_handle,
|
|
|
|
&metaindex_contents, env, false);
|
2013-12-06 00:51:26 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
Block metaindex_block(metaindex_contents);
|
|
|
|
std::unique_ptr<Iterator> meta_iter(
|
2014-02-05 00:21:47 +00:00
|
|
|
metaindex_block.NewIterator(BytewiseComparator()));
|
2013-12-06 00:51:26 +00:00
|
|
|
|
|
|
|
// -- Read property block
|
RocksDB 2.8 to be able to read files generated by 2.6
Summary:
From 2.6 to 2.7, property block name is renamed from rocksdb.stats to rocksdb.properties. Older properties were not able to be loaded. In 2.8, we seem to have added some logic that uses property block without checking null pointers, which create segment faults.
In this patch, we fix it by:
(1) try rocksdb.stats if rocksdb.properties is not found
(2) add some null checking before consuming rep->table_properties
Test Plan: make sure a file generated in 2.7 couldn't be opened now can be opened.
Reviewers: haobo, igor, yhchiang
Reviewed By: igor
CC: ljin, xjin, dhruba, kailiu, leveldb
Differential Revision: https://reviews.facebook.net/D17961
2014-04-17 02:30:33 +00:00
|
|
|
// This function is not used by BlockBasedTable, so we don't have to
|
|
|
|
// worry about old properties block name.
|
2013-12-06 00:51:26 +00:00
|
|
|
meta_iter->Seek(kPropertiesBlock);
|
|
|
|
TableProperties table_properties;
|
|
|
|
if (meta_iter->Valid() &&
|
|
|
|
meta_iter->key() == kPropertiesBlock &&
|
|
|
|
meta_iter->status().ok()) {
|
2014-02-05 00:21:47 +00:00
|
|
|
s = ReadProperties(meta_iter->value(), file, env, info_log, properties);
|
2013-12-06 00:51:26 +00:00
|
|
|
} else {
|
|
|
|
s = Status::Corruption(
|
2014-02-05 00:21:47 +00:00
|
|
|
"Unable to read the property block from the plain table");
|
2013-12-06 00:51:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2014-02-05 00:21:47 +00:00
|
|
|
Status ReadTableMagicNumber(const std::string& file_path,
|
|
|
|
const Options& options,
|
|
|
|
const EnvOptions& env_options,
|
|
|
|
uint64_t* table_magic_number) {
|
|
|
|
unique_ptr<RandomAccessFile> file;
|
|
|
|
Status s = options.env->NewRandomAccessFile(file_path, &file, env_options);
|
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t file_size;
|
|
|
|
options.env->GetFileSize(file_path, &file_size);
|
2014-02-14 00:28:21 +00:00
|
|
|
return ReadTableMagicNumber(file.get(), file_size, options, env_options,
|
|
|
|
table_magic_number);
|
|
|
|
}
|
|
|
|
|
|
|
|
Status ReadTableMagicNumber(RandomAccessFile* file, uint64_t file_size,
|
|
|
|
const Options& options,
|
|
|
|
const EnvOptions& env_options,
|
|
|
|
uint64_t* table_magic_number) {
|
2014-02-05 00:21:47 +00:00
|
|
|
if (file_size < Footer::kEncodedLength) {
|
|
|
|
return Status::InvalidArgument("file is too short to be an sstable");
|
|
|
|
}
|
|
|
|
|
|
|
|
Footer footer;
|
2014-02-14 00:28:21 +00:00
|
|
|
auto s = ReadFooterFromFile(file, file_size, &footer);
|
2014-02-05 00:21:47 +00:00
|
|
|
if (!s.ok()) {
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
*table_magic_number = footer.table_magic_number();
|
|
|
|
return Status::OK();
|
|
|
|
}
|
2013-12-06 00:51:26 +00:00
|
|
|
|
2013-12-05 21:09:13 +00:00
|
|
|
} // namespace rocksdb
|