Skip to content

Commit bf86af5

Browse files
committed
Remove the terrible hack in for flush_block_policy_factory
Summary: Previous code is too convoluted and I must be drunk for letting such code to be written without a second thought. Thanks to the discussion with @sdong, I added the `Options` when generating the flusher, thus avoiding the tricks. Just FYI: I resisted to add Options in flush_block_policy.h since I wanted to avoid cyclic dependencies: FlushBlockPolicy dpends on Options and Options also depends FlushBlockPolicy... While I appreciate my effort to prevent it, the old design turns out creating more troubles than it tried to avoid. Test Plan: ran ./table_test Reviewers: sdong Reviewed By: sdong CC: sdong, leveldb Differential Revision: https://reviews.facebook.net/D16503
1 parent 58ca641 commit bf86af5

7 files changed

+24
-46
lines changed

db/version_set.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -1553,8 +1553,7 @@ Status VersionSet::LogAndApply(VersionEdit* edit, port::Mutex* mu,
15531553
// only one thread can be here at the same time
15541554
if (!new_manifest_filename.empty()) {
15551555
unique_ptr<WritableFile> descriptor_file;
1556-
s = env_->NewWritableFile(new_manifest_filename,
1557-
&descriptor_file,
1556+
s = env_->NewWritableFile(new_manifest_filename, &descriptor_file,
15581557
storage_options_.AdaptForLogWrite());
15591558
if (s.ok()) {
15601559
descriptor_log_.reset(new log::Writer(std::move(descriptor_file)));

include/rocksdb/flush_block_policy.h

+4-10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ namespace rocksdb {
1111

1212
class Slice;
1313
class BlockBuilder;
14+
struct Options;
1415

1516
// FlushBlockPolicy provides a configurable way to determine when to flush a
1617
// block in the block based tables,
@@ -36,29 +37,22 @@ class FlushBlockPolicyFactory {
3637
// Callers must delete the result after any database that is using the
3738
// result has been closed.
3839
virtual FlushBlockPolicy* NewFlushBlockPolicy(
39-
const BlockBuilder& data_block_builder) const = 0;
40+
const Options& options, const BlockBuilder& data_block_builder) const = 0;
4041

4142
virtual ~FlushBlockPolicyFactory() { }
4243
};
4344

4445
class FlushBlockBySizePolicyFactory : public FlushBlockPolicyFactory {
4546
public:
46-
FlushBlockBySizePolicyFactory(const uint64_t block_size,
47-
const uint64_t block_size_deviation) :
48-
block_size_(block_size),
49-
block_size_deviation_(block_size_deviation) {
50-
}
47+
FlushBlockBySizePolicyFactory() {}
5148

5249
virtual const char* Name() const override {
5350
return "FlushBlockBySizePolicyFactory";
5451
}
5552

5653
virtual FlushBlockPolicy* NewFlushBlockPolicy(
54+
const Options& options,
5755
const BlockBuilder& data_block_builder) const override;
58-
59-
private:
60-
const uint64_t block_size_;
61-
const uint64_t block_size_deviation_;
6256
};
6357

6458
} // rocksdb

table/block_based_table_builder.cc

+2-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ struct BlockBasedTableBuilder::Rep {
8787
filter_block(opt.filter_policy == nullptr
8888
? nullptr
8989
: new FilterBlockBuilder(opt, &internal_comparator)),
90-
flush_block_policy(
91-
flush_block_policy_factory->NewFlushBlockPolicy(data_block)) {}
90+
flush_block_policy(flush_block_policy_factory->NewFlushBlockPolicy(
91+
options, data_block)) {}
9292
};
9393

9494
BlockBasedTableBuilder::BlockBasedTableBuilder(

table/block_based_table_factory.cc

+11-24
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818

1919
namespace rocksdb {
2020

21+
BlockBasedTableFactory::BlockBasedTableFactory(
22+
const BlockBasedTableOptions& table_options)
23+
: table_options_(table_options) {
24+
if (table_options_.flush_block_policy_factory == nullptr) {
25+
table_options_.flush_block_policy_factory.reset(
26+
new FlushBlockBySizePolicyFactory());
27+
}
28+
}
29+
2130
Status BlockBasedTableFactory::NewTableReader(
2231
const Options& options, const EnvOptions& soptions,
2332
const InternalKeyComparator& internal_comparator,
@@ -31,35 +40,13 @@ Status BlockBasedTableFactory::NewTableReader(
3140
TableBuilder* BlockBasedTableFactory::NewTableBuilder(
3241
const Options& options, const InternalKeyComparator& internal_comparator,
3342
WritableFile* file, CompressionType compression_type) const {
34-
auto flush_block_policy_factory =
35-
table_options_.flush_block_policy_factory.get();
36-
37-
// if flush block policy factory is not set, we'll create the default one
38-
// from the options.
39-
//
40-
// NOTE: we cannot pre-cache the "default block policy factory" because
41-
// `FlushBlockBySizePolicyFactory` takes `options.block_size` and
42-
// `options.block_size_deviation` as parameters, which may be different
43-
// every time.
44-
if (flush_block_policy_factory == nullptr) {
45-
flush_block_policy_factory =
46-
new FlushBlockBySizePolicyFactory(options.block_size,
47-
options.block_size_deviation);
48-
}
43+
auto flush_block_policy_factory =
44+
table_options_.flush_block_policy_factory.get();
4945

5046
auto table_builder =
5147
new BlockBasedTableBuilder(options, internal_comparator, file,
5248
flush_block_policy_factory, compression_type);
5349

54-
// Delete flush_block_policy_factory only when it's just created from the
55-
// options.
56-
// We can safely delete flush_block_policy_factory since it will only be used
57-
// during the construction of `BlockBasedTableBuilder`.
58-
if (flush_block_policy_factory !=
59-
table_options_.flush_block_policy_factory.get()) {
60-
delete flush_block_policy_factory;
61-
}
62-
6350
return table_builder;
6451
}
6552

table/block_based_table_factory.h

+1-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ class BlockBasedTableBuilder;
2626
class BlockBasedTableFactory : public TableFactory {
2727
public:
2828
explicit BlockBasedTableFactory(
29-
const BlockBasedTableOptions& table_options = BlockBasedTableOptions())
30-
: table_options_(table_options) {}
29+
const BlockBasedTableOptions& table_options = BlockBasedTableOptions());
3130

3231
~BlockBasedTableFactory() {}
3332

table/flush_block_policy.cc

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// LICENSE file in the root directory of this source tree. An additional grant
44
// of patent rights can be found in the PATENTS file in the same directory.
55

6+
#include "rocksdb/options.h"
67
#include "rocksdb/flush_block_policy.h"
78
#include "rocksdb/slice.h"
89
#include "table/block_builder.h"
@@ -61,10 +62,9 @@ class FlushBlockBySizePolicy : public FlushBlockPolicy {
6162
};
6263

6364
FlushBlockPolicy* FlushBlockBySizePolicyFactory::NewFlushBlockPolicy(
64-
const BlockBuilder& data_block_builder) const {
65-
return new FlushBlockBySizePolicy(block_size_,
66-
block_size_deviation_,
67-
data_block_builder);
65+
const Options& options, const BlockBuilder& data_block_builder) const {
66+
return new FlushBlockBySizePolicy(
67+
options.block_size, options.block_size_deviation, data_block_builder);
6868
}
6969

7070
} // namespace rocksdb

table/table_test.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -689,8 +689,7 @@ class Harness {
689689
switch (args.type) {
690690
case BLOCK_BASED_TABLE_TEST:
691691
table_options.flush_block_policy_factory.reset(
692-
new FlushBlockBySizePolicyFactory(options_.block_size,
693-
options_.block_size_deviation));
692+
new FlushBlockBySizePolicyFactory());
694693
options_.table_factory.reset(new BlockBasedTableFactory(table_options));
695694
constructor_ = new TableConstructor(options_.comparator);
696695
break;

0 commit comments

Comments
 (0)