Skip to content

Commit 161ab42

Browse files
committed
Make table properties shareable
Summary: We are going to expose properties of all tables to end users through "some" db interface. However, current design doesn't naturally fit for this need, which is because: 1. If a table presents in table cache, we cannot simply return the reference to its table properties, because the table may be destroy after compaction (and we don't want to hold the ref of the version). 2. Copy table properties is OK, but it's slow. Thus in this diff, I change the table reader's interface to return a shared pointer (for const table properties), instead a const refernce. Test Plan: `make check` passed Reviewers: haobo, sdong, dhruba Reviewed By: haobo CC: leveldb Differential Revision: https://reviews.facebook.net/D15999
1 parent 0982c38 commit 161ab42

12 files changed

+102
-84
lines changed

db/simple_table_db_test.cc

+4-3
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ class SimpleTableReader: public TableReader {
9696

9797
void SetupForCompaction() override;
9898

99-
TableProperties& GetTableProperties() override;
99+
std::shared_ptr<const TableProperties> GetTableProperties() const override;
100100

101101
~SimpleTableReader();
102102

@@ -172,7 +172,7 @@ struct SimpleTableReader::Rep {
172172
unique_ptr<RandomAccessFile> file;
173173
uint64_t index_start_offset;
174174
int num_entries;
175-
TableProperties table_properties;
175+
std::shared_ptr<TableProperties> table_properties;
176176

177177
const static int user_key_size = 16;
178178
const static int offset_length = 8;
@@ -215,7 +215,8 @@ Status SimpleTableReader::Open(const Options& options,
215215
void SimpleTableReader::SetupForCompaction() {
216216
}
217217

218-
TableProperties& SimpleTableReader::GetTableProperties() {
218+
std::shared_ptr<const TableProperties> SimpleTableReader::GetTableProperties()
219+
const {
219220
return rep_->table_properties;
220221
}
221222

db/table_properties_collector_test.cc

+6-4
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void TestCustomizedTablePropertiesCollector(
157157

158158
// -- Step 2: Read properties
159159
FakeRandomeAccessFile readable(writable->contents());
160-
TableProperties props;
160+
TableProperties* props;
161161
Status s = ReadTableProperties(
162162
&readable,
163163
writable->contents().size(),
@@ -166,9 +166,10 @@ void TestCustomizedTablePropertiesCollector(
166166
nullptr,
167167
&props
168168
);
169+
std::unique_ptr<TableProperties> props_guard(props);
169170
ASSERT_OK(s);
170171

171-
auto user_collected = props.user_collected_properties;
172+
auto user_collected = props->user_collected_properties;
172173

173174
ASSERT_EQ("Rocksdb", user_collected.at("TablePropertiesTest"));
174175

@@ -256,7 +257,7 @@ void TestInternalKeyPropertiesCollector(
256257
ASSERT_OK(builder->Finish());
257258

258259
FakeRandomeAccessFile readable(writable->contents());
259-
TableProperties props;
260+
TableProperties* props;
260261
Status s = ReadTableProperties(
261262
&readable,
262263
writable->contents().size(),
@@ -267,7 +268,8 @@ void TestInternalKeyPropertiesCollector(
267268
);
268269
ASSERT_OK(s);
269270

270-
auto user_collected = props.user_collected_properties;
271+
std::unique_ptr<TableProperties> props_guard(props);
272+
auto user_collected = props->user_collected_properties;
271273
uint64_t deleted = GetDeletedKeys(user_collected);
272274
ASSERT_EQ(4u, deleted);
273275

table/block_based_table_reader.cc

+7-3
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ struct BlockBasedTable::Rep {
6262
unique_ptr<Block> index_block;
6363
unique_ptr<FilterBlockReader> filter;
6464

65-
TableProperties table_properties;
65+
std::shared_ptr<const TableProperties> table_properties;
6666
};
6767

6868
BlockBasedTable::~BlockBasedTable() {
@@ -255,16 +255,19 @@ Status BlockBasedTable::Open(const Options& options, const EnvOptions& soptions,
255255
meta_iter->Seek(kPropertiesBlock);
256256
if (meta_iter->Valid() && meta_iter->key() == kPropertiesBlock) {
257257
s = meta_iter->status();
258+
TableProperties* table_properties = nullptr;
258259
if (s.ok()) {
259260
s = ReadProperties(meta_iter->value(), rep->file.get(), rep->options.env,
260-
rep->options.info_log.get(), &rep->table_properties);
261+
rep->options.info_log.get(), &table_properties);
261262
}
262263

263264
if (!s.ok()) {
264265
auto err_msg =
265266
"[Warning] Encountered error while reading data from properties "
266267
"block " + s.ToString();
267268
Log(rep->options.info_log, "%s", err_msg.c_str());
269+
} else {
270+
rep->table_properties.reset(table_properties);
268271
}
269272
}
270273

@@ -339,7 +342,8 @@ void BlockBasedTable::SetupForCompaction() {
339342
compaction_optimized_ = true;
340343
}
341344

342-
const TableProperties& BlockBasedTable::GetTableProperties() {
345+
std::shared_ptr<const TableProperties> BlockBasedTable::GetTableProperties()
346+
const {
343347
return rep_->table_properties;
344348
}
345349

table/block_based_table_reader.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ class BlockBasedTable : public TableReader {
8686
// posix_fadvise
8787
void SetupForCompaction() override;
8888

89-
const TableProperties& GetTableProperties() override;
89+
std::shared_ptr<const TableProperties> GetTableProperties() const override;
9090

9191
~BlockBasedTable();
9292

table/format.cc

+3-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "table/format.h"
1111

1212
#include <string>
13+
#include <inttypes.h>
1314

1415
#include "port/port.h"
1516
#include "rocksdb/env.h"
@@ -64,7 +65,8 @@ Status Footer::DecodeFrom(Slice* input) {
6465
if (magic != table_magic_number()) {
6566
char buffer[80];
6667
snprintf(buffer, sizeof(buffer) - 1,
67-
"not an sstable (bad magic number --- %lx)", magic);
68+
"not an sstable (bad magic number --- %#" PRIx64 ")",
69+
magic);
6870
return Status::InvalidArgument(buffer);
6971
}
7072
} else {

table/meta_blocks.cc

+26-24
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,9 @@ bool NotifyCollectTableCollectorsOnFinish(
133133
return all_succeeded;
134134
}
135135

136-
Status ReadProperties(
137-
const Slice& handle_value,
138-
RandomAccessFile* file,
139-
Env* env,
140-
Logger* logger,
141-
TableProperties* table_properties) {
136+
Status ReadProperties(const Slice& handle_value, RandomAccessFile* file,
137+
Env* env, Logger* logger,
138+
TableProperties** table_properties) {
142139
assert(table_properties);
143140

144141
Slice v = handle_value;
@@ -161,18 +158,22 @@ Status ReadProperties(
161158
std::unique_ptr<Iterator> iter(
162159
properties_block.NewIterator(BytewiseComparator()));
163160

161+
auto new_table_properties = new TableProperties();
164162
// All pre-defined properties of type uint64_t
165163
std::unordered_map<std::string, uint64_t*> predefined_uint64_properties = {
166-
{TablePropertiesNames::kDataSize, &table_properties->data_size},
167-
{TablePropertiesNames::kIndexSize, &table_properties->index_size},
168-
{TablePropertiesNames::kFilterSize, &table_properties->filter_size},
169-
{TablePropertiesNames::kRawKeySize, &table_properties->raw_key_size},
170-
{TablePropertiesNames::kRawValueSize, &table_properties->raw_value_size},
164+
{TablePropertiesNames::kDataSize, &new_table_properties->data_size},
165+
{TablePropertiesNames::kIndexSize, &new_table_properties->index_size},
166+
{TablePropertiesNames::kFilterSize, &new_table_properties->filter_size},
167+
{TablePropertiesNames::kRawKeySize, &new_table_properties->raw_key_size},
168+
{TablePropertiesNames::kRawValueSize,
169+
&new_table_properties->raw_value_size},
171170
{TablePropertiesNames::kNumDataBlocks,
172-
&table_properties->num_data_blocks},
173-
{TablePropertiesNames::kNumEntries, &table_properties->num_entries},
174-
{TablePropertiesNames::kFormatVersion, &table_properties->format_version},
175-
{TablePropertiesNames::kFixedKeyLen, &table_properties->fixed_key_len}};
171+
&new_table_properties->num_data_blocks},
172+
{TablePropertiesNames::kNumEntries, &new_table_properties->num_entries},
173+
{TablePropertiesNames::kFormatVersion,
174+
&new_table_properties->format_version},
175+
{TablePropertiesNames::kFixedKeyLen,
176+
&new_table_properties->fixed_key_len}, };
176177

177178
std::string last_key;
178179
for (iter->SeekToFirst(); iter->Valid(); iter->Next()) {
@@ -203,24 +204,25 @@ Status ReadProperties(
203204
}
204205
*(pos->second) = val;
205206
} else if (key == TablePropertiesNames::kFilterPolicy) {
206-
table_properties->filter_policy_name = raw_val.ToString();
207+
new_table_properties->filter_policy_name = raw_val.ToString();
207208
} else {
208209
// handle user-collected properties
209-
table_properties->user_collected_properties.insert(
210+
new_table_properties->user_collected_properties.insert(
210211
{key, raw_val.ToString()});
211212
}
212213
}
214+
if (s.ok()) {
215+
*table_properties = new_table_properties;
216+
} else {
217+
delete new_table_properties;
218+
}
213219

214220
return s;
215221
}
216222

217-
Status ReadTableProperties(
218-
RandomAccessFile* file,
219-
uint64_t file_size,
220-
uint64_t table_magic_number,
221-
Env* env,
222-
Logger* info_log,
223-
TableProperties* properties) {
223+
Status ReadTableProperties(RandomAccessFile* file, uint64_t file_size,
224+
uint64_t table_magic_number, Env* env,
225+
Logger* info_log, TableProperties** properties) {
224226
// -- Read metaindex block
225227
Footer footer(table_magic_number);
226228
auto s = ReadFooterFromFile(file, file_size, &footer);

table/meta_blocks.h

+12-13
Original file line numberDiff line numberDiff line change
@@ -103,21 +103,20 @@ bool NotifyCollectTableCollectorsOnFinish(
103103
PropertyBlockBuilder* builder);
104104

105105
// Read the properties from the table.
106-
Status ReadProperties(
107-
const Slice& handle_value,
108-
RandomAccessFile* file,
109-
Env* env,
110-
Logger* logger,
111-
TableProperties* table_properties);
106+
// @returns a status to indicate if the operation succeeded. On success,
107+
// *table_properties will point to a heap-allocated TableProperties
108+
// object, otherwise value of `table_properties` will not be modified.
109+
Status ReadProperties(const Slice& handle_value, RandomAccessFile* file,
110+
Env* env, Logger* logger,
111+
TableProperties** table_properties);
112112

113113
// Directly read the properties from the properties block of a plain table.
114-
Status ReadTableProperties(
115-
RandomAccessFile* file,
116-
uint64_t file_size,
117-
uint64_t table_magic_number,
118-
Env* env,
119-
Logger* info_log,
120-
TableProperties* properties);
114+
// @returns a status to indicate if the operation succeeded. On success,
115+
// *table_properties will point to a heap-allocated TableProperties
116+
// object, otherwise value of `table_properties` will not be modified.
117+
Status ReadTableProperties(RandomAccessFile* file, uint64_t file_size,
118+
uint64_t table_magic_number, Env* env,
119+
Logger* info_log, TableProperties** properties);
121120

122121
// Read the magic number of the specified file directly. The magic number
123122
// of a valid sst table the last 8-byte of the file.

table/plain_table_reader.cc

+8-9
Original file line numberDiff line numberDiff line change
@@ -87,15 +87,15 @@ PlainTableReader::PlainTableReader(const EnvOptions& storage_options,
8787
const InternalKeyComparator& icomparator,
8888
uint64_t file_size, int bloom_bits_per_key,
8989
double hash_table_ratio,
90-
const TableProperties& table_properties)
90+
const TableProperties* table_properties)
9191
: soptions_(storage_options),
9292
internal_comparator_(icomparator),
9393
file_size_(file_size),
9494
kHashTableRatio(hash_table_ratio),
9595
kBloomBitsPerKey(bloom_bits_per_key),
9696
table_properties_(table_properties),
97-
data_end_offset_(table_properties_.data_size),
98-
user_key_len_(table_properties.fixed_key_len) {}
97+
data_end_offset_(table_properties_->data_size),
98+
user_key_len_(table_properties->fixed_key_len) {}
9999

100100
PlainTableReader::~PlainTableReader() {
101101
delete[] hash_table_;
@@ -117,17 +117,16 @@ Status PlainTableReader::Open(const Options& options,
117117
return Status::NotSupported("File is too large for PlainTableReader!");
118118
}
119119

120-
TableProperties table_properties;
120+
TableProperties* props = nullptr;
121121
auto s = ReadTableProperties(file.get(), file_size, kPlainTableMagicNumber,
122-
options.env, options.info_log.get(),
123-
&table_properties);
122+
options.env, options.info_log.get(), &props);
124123
if (!s.ok()) {
125124
return s;
126125
}
127126

128-
std::unique_ptr<PlainTableReader> new_reader(new PlainTableReader(
129-
soptions, internal_comparator, file_size, bloom_bits_per_key,
130-
hash_table_ratio, table_properties));
127+
std::unique_ptr<PlainTableReader> new_reader(
128+
new PlainTableReader(soptions, internal_comparator, file_size,
129+
bloom_bits_per_key, hash_table_ratio, props));
131130
new_reader->file_ = std::move(file);
132131
new_reader->options_ = options;
133132

table/plain_table_reader.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,15 @@ class PlainTableReader: public TableReader {
6464

6565
void SetupForCompaction();
6666

67-
const TableProperties& GetTableProperties() { return table_properties_; }
67+
std::shared_ptr<const TableProperties> GetTableProperties() const {
68+
return table_properties_;
69+
}
6870

6971
PlainTableReader(const EnvOptions& storage_options,
7072
const InternalKeyComparator& internal_comparator,
7173
uint64_t file_size, int bloom_num_bits,
7274
double hash_table_ratio,
73-
const TableProperties& table_properties);
75+
const TableProperties* table_properties);
7476
~PlainTableReader();
7577

7678
private:
@@ -95,7 +97,7 @@ class PlainTableReader: public TableReader {
9597
const int kBloomBitsPerKey;
9698
DynamicBloom* bloom_ = nullptr;
9799

98-
TableProperties table_properties_;
100+
std::shared_ptr<const TableProperties> table_properties_;
99101
const uint32_t data_start_offset_ = 0;
100102
const uint32_t data_end_offset_;
101103
const size_t user_key_len_;

table/table_reader.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// found in the LICENSE file. See the AUTHORS file for names of contributors.
99

1010
#pragma once
11+
#include <memory>
1112

1213
namespace rocksdb {
1314

@@ -47,7 +48,7 @@ class TableReader {
4748
// posix_fadvise
4849
virtual void SetupForCompaction() = 0;
4950

50-
virtual const TableProperties& GetTableProperties() = 0;
51+
virtual std::shared_ptr<const TableProperties> GetTableProperties() const = 0;
5152

5253
// Calls (*result_handler)(handle_context, ...) repeatedly, starting with
5354
// the entry found after a call to Seek(key), until result_handler returns

0 commit comments

Comments
 (0)