Skip to content

Commit f748912

Browse files
committed
Move compaction picker and internal key comparator to ColumnFamilyData
Summary: Compaction picker and internal key comparator are different for each column family (not global), so they should live in ColumnFamilyData Test Plan: make check Reviewers: dhruba, haobo CC: leveldb Differential Revision: https://reviews.facebook.net/D15801
1 parent 5693db2 commit f748912

11 files changed

+146
-155
lines changed

db/column_family.cc

+21-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <algorithm>
1515

1616
#include "db/version_set.h"
17+
#include "db/compaction_picker.h"
1718

1819
namespace rocksdb {
1920

@@ -65,14 +66,21 @@ ColumnFamilyData::ColumnFamilyData(uint32_t id, const std::string& name,
6566
dummy_versions_(dummy_versions),
6667
current_(nullptr),
6768
options_(options),
69+
icmp_(options_.comparator),
6870
mem_(nullptr),
6971
imm_(options.min_write_buffer_number_to_merge),
7072
super_version_(nullptr),
7173
super_version_number_(0),
7274
next_(nullptr),
7375
prev_(nullptr),
7476
log_number_(0),
75-
need_slowdown_for_num_level0_files_(false) {}
77+
need_slowdown_for_num_level0_files_(false) {
78+
if (options_.compaction_style == kCompactionStyleUniversal) {
79+
compaction_picker_.reset(new UniversalCompactionPicker(&options_, &icmp_));
80+
} else {
81+
compaction_picker_.reset(new LevelCompactionPicker(&options_, &icmp_));
82+
}
83+
}
7684

7785
ColumnFamilyData::~ColumnFamilyData() {
7886
if (super_version_ != nullptr) {
@@ -114,6 +122,18 @@ void ColumnFamilyData::CreateNewMemtable() {
114122
mem_->Ref();
115123
}
116124

125+
Compaction* ColumnFamilyData::PickCompaction() {
126+
return compaction_picker_->PickCompaction(current_);
127+
}
128+
129+
Compaction* ColumnFamilyData::CompactRange(int input_level, int output_level,
130+
const InternalKey* begin,
131+
const InternalKey* end,
132+
InternalKey** compaction_end) {
133+
return compaction_picker_->CompactRange(current_, input_level, output_level,
134+
begin, end, compaction_end);
135+
}
136+
117137
SuperVersion* ColumnFamilyData::InstallSuperVersion(
118138
SuperVersion* new_superversion) {
119139
new_superversion->Init(mem_, imm_.current(), current_);

db/column_family.h

+22
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ class Version;
2323
class VersionSet;
2424
class MemTable;
2525
class MemTableListVersion;
26+
class CompactionPicker;
27+
class Compaction;
28+
class InternalKey;
2629

2730
// holds references to memtable, all immutable memtables and version
2831
struct SuperVersion {
@@ -62,6 +65,8 @@ class ColumnFamilyData {
6265
uint32_t GetID() const { return id_; }
6366
const std::string& GetName() { return name_; }
6467

68+
int NumberLevels() const { return options_.num_levels; }
69+
6570
void SetLogNumber(uint64_t log_number) { log_number_ = log_number; }
6671
uint64_t GetLogNumber() const { return log_number_; }
6772

@@ -75,6 +80,17 @@ class ColumnFamilyData {
7580
void SetCurrent(Version* current);
7681
void CreateNewMemtable();
7782

83+
// See documentation in compaction_picker.h
84+
Compaction* PickCompaction();
85+
Compaction* CompactRange(int input_level, int output_level,
86+
const InternalKey* begin, const InternalKey* end,
87+
InternalKey** compaction_end);
88+
89+
CompactionPicker* compaction_picker() const {
90+
return compaction_picker_.get();
91+
}
92+
const InternalKeyComparator& internal_comparator() const { return icmp_; }
93+
7894
SuperVersion* GetSuperVersion() const { return super_version_; }
7995
uint64_t GetSuperVersionNumber() const {
8096
return super_version_number_.load();
@@ -102,6 +118,8 @@ class ColumnFamilyData {
102118
Version* current_; // == dummy_versions->prev_
103119
ColumnFamilyOptions options_;
104120

121+
const InternalKeyComparator icmp_;
122+
105123
MemTable* mem_;
106124
MemTableList imm_;
107125
SuperVersion* super_version_;
@@ -124,6 +142,10 @@ class ColumnFamilyData {
124142
// A flag indicating whether we should delay writes because
125143
// we have too many level 0 files
126144
bool need_slowdown_for_num_level0_files_;
145+
146+
// An object that keeps all the compaction stats
147+
// and picks the next compaction
148+
std::unique_ptr<CompactionPicker> compaction_picker_;
127149
};
128150

129151
// Thread safe only for reading without a writer. All access should be

db/compaction_picker.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ uint64_t MultiplyCheckOverflow(uint64_t op1, int op2) {
4141

4242
} // anonymous namespace
4343

44-
CompactionPicker::CompactionPicker(const Options* options,
44+
CompactionPicker::CompactionPicker(const ColumnFamilyOptions* options,
4545
const InternalKeyComparator* icmp)
4646
: compactions_in_progress_(options->num_levels),
4747
options_(options),

db/compaction_picker.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ class Version;
2424

2525
class CompactionPicker {
2626
public:
27-
CompactionPicker(const Options* options, const InternalKeyComparator* icmp);
27+
CompactionPicker(const ColumnFamilyOptions* options,
28+
const InternalKeyComparator* icmp);
2829
virtual ~CompactionPicker();
2930

3031
// Pick level and inputs for a new compaction.
@@ -115,7 +116,7 @@ class CompactionPicker {
115116
// Per-level max bytes
116117
std::unique_ptr<uint64_t[]> level_max_bytes_;
117118

118-
const Options* const options_;
119+
const ColumnFamilyOptions* const options_;
119120
private:
120121
int num_levels_;
121122

@@ -124,7 +125,7 @@ class CompactionPicker {
124125

125126
class UniversalCompactionPicker : public CompactionPicker {
126127
public:
127-
UniversalCompactionPicker(const Options* options,
128+
UniversalCompactionPicker(const ColumnFamilyOptions* options,
128129
const InternalKeyComparator* icmp)
129130
: CompactionPicker(options, icmp) {}
130131
virtual Compaction* PickCompaction(Version* version) override;
@@ -141,7 +142,7 @@ class UniversalCompactionPicker : public CompactionPicker {
141142

142143
class LevelCompactionPicker : public CompactionPicker {
143144
public:
144-
LevelCompactionPicker(const Options* options,
145+
LevelCompactionPicker(const ColumnFamilyOptions* options,
145146
const InternalKeyComparator* icmp)
146147
: CompactionPicker(options, icmp) {}
147148
virtual Compaction* PickCompaction(Version* version) override;

db/db_impl.cc

+12-10
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,10 @@ int DBImpl::FindMinimumEmptyLevelFitting(int level) {
13121312
// stop if level i is not empty
13131313
if (current->NumLevelFiles(i) > 0) break;
13141314
// stop if level i is too small (cannot fit the level files)
1315-
if (versions_->MaxBytesForLevel(i) < current->NumLevelBytes(level)) break;
1315+
if (default_cfd_->compaction_picker()->MaxBytesForLevel(i) <
1316+
current->NumLevelBytes(level)) {
1317+
break;
1318+
}
13161319

13171320
minimum_level = i;
13181321
}
@@ -1943,8 +1946,8 @@ Status DBImpl::BackgroundCompaction(bool* madeProgress,
19431946
if (is_manual) {
19441947
ManualCompaction* m = manual_compaction_;
19451948
assert(m->in_progress);
1946-
c.reset(versions_->CompactRange(
1947-
m->input_level, m->output_level, m->begin, m->end, &manual_end));
1949+
c.reset(default_cfd_->CompactRange(m->input_level, m->output_level,
1950+
m->begin, m->end, &manual_end));
19481951
if (!c) {
19491952
m->done = true;
19501953
}
@@ -1959,7 +1962,7 @@ Status DBImpl::BackgroundCompaction(bool* madeProgress,
19591962
? "(end)"
19601963
: manual_end->DebugString().c_str()));
19611964
} else if (!options_.disable_auto_compactions) {
1962-
c.reset(versions_->PickCompaction());
1965+
c.reset(default_cfd_->PickCompaction());
19631966
}
19641967

19651968
Status status;
@@ -1983,14 +1986,14 @@ Status DBImpl::BackgroundCompaction(bool* madeProgress,
19831986
static_cast<unsigned long long>(f->number), c->level() + 1,
19841987
static_cast<unsigned long long>(f->file_size),
19851988
status.ToString().c_str(), default_cfd_->current()->LevelSummary(&tmp));
1986-
versions_->ReleaseCompactionFiles(c.get(), status);
1989+
default_cfd_->compaction_picker()->ReleaseCompactionFiles(c.get(), status);
19871990
*madeProgress = true;
19881991
} else {
19891992
MaybeScheduleFlushOrCompaction(); // do more compaction work in parallel.
19901993
CompactionState* compact = new CompactionState(c.get());
19911994
status = DoCompactionWork(compact, deletion_state);
19921995
CleanupCompaction(compact, status);
1993-
versions_->ReleaseCompactionFiles(c.get(), status);
1996+
default_cfd_->compaction_picker()->ReleaseCompactionFiles(c.get(), status);
19941997
c->ReleaseInputs();
19951998
*madeProgress = true;
19961999
}
@@ -2121,7 +2124,8 @@ Status DBImpl::OpenCompactionOutputFile(CompactionState* compact) {
21212124
// Over-estimate slightly so we don't end up just barely crossing
21222125
// the threshold.
21232126
compact->outfile->SetPreallocationBlockSize(
2124-
1.1 * versions_->MaxFileSizeForLevel(compact->compaction->output_level()));
2127+
1.1 * default_cfd_->compaction_picker()->MaxFileSizeForLevel(
2128+
compact->compaction->output_level()));
21252129

21262130
CompressionType compression_type = GetCompressionType(
21272131
options_, compact->compaction->output_level(),
@@ -3534,9 +3538,7 @@ bool DBImpl::GetProperty(const ColumnFamilyHandle& column_family,
35343538
const Slice& property, std::string* value) {
35353539
value->clear();
35363540
MutexLock l(&mutex_);
3537-
return internal_stats_.GetProperty(property, value, versions_.get(),
3538-
default_cfd_->current(),
3539-
default_cfd_->imm()->size());
3541+
return internal_stats_.GetProperty(property, value, default_cfd_);
35403542
}
35413543

35423544
void DBImpl::GetApproximateSizes(const ColumnFamilyHandle& column_family,

db/internal_stats.cc

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

1010
#include "db/internal_stats.h"
11+
#include "db/column_family.h"
1112

1213
#include <vector>
1314

1415
namespace rocksdb {
1516

1617
bool InternalStats::GetProperty(const Slice& property, std::string* value,
17-
VersionSet* version_set, Version* current,
18-
int immsize) {
18+
ColumnFamilyData* cfd) {
1919
Slice in = property;
2020
Slice prefix("rocksdb.");
2121
if (!in.starts_with(prefix)) return false;
@@ -30,7 +30,7 @@ bool InternalStats::GetProperty(const Slice& property, std::string* value,
3030
} else {
3131
char buf[100];
3232
snprintf(buf, sizeof(buf), "%d",
33-
current->NumLevelFiles(static_cast<int>(level)));
33+
cfd->current()->NumLevelFiles(static_cast<int>(level)));
3434
*value = buf;
3535
return true;
3636
}
@@ -43,8 +43,8 @@ bool InternalStats::GetProperty(const Slice& property, std::string* value,
4343

4444
for (int level = 0; level < number_levels_; level++) {
4545
snprintf(buf, sizeof(buf), "%3d %8d %8.0f\n", level,
46-
current->NumLevelFiles(level),
47-
current->NumLevelBytes(level) / 1048576.0);
46+
cfd->current()->NumLevelFiles(level),
47+
cfd->current()->NumLevelBytes(level) / 1048576.0);
4848
value->append(buf);
4949
}
5050
return true;
@@ -87,7 +87,7 @@ bool InternalStats::GetProperty(const Slice& property, std::string* value,
8787
);
8888
value->append(buf);
8989
for (int level = 0; level < number_levels_; level++) {
90-
int files = current->NumLevelFiles(level);
90+
int files = cfd->current()->NumLevelFiles(level);
9191
if (compaction_stats_[level].micros > 0 || files > 0) {
9292
int64_t bytes_read = compaction_stats_[level].bytes_readn +
9393
compaction_stats_[level].bytes_readnp1;
@@ -117,9 +117,9 @@ bool InternalStats::GetProperty(const Slice& property, std::string* value,
117117
"%3d %8d %8.0f %5.1f %9.0f %9.0f %9.0f %9.0f %9.0f %9.0f "
118118
"%10.1f %9.1f %11.1f %8d %8d %8d %8d %8d %8d %9.1f %9.1f "
119119
"%9lu\n",
120-
level, files, current->NumLevelBytes(level) / 1048576.0,
121-
current->NumLevelBytes(level) /
122-
version_set->MaxBytesForLevel(level),
120+
level, files, cfd->current()->NumLevelBytes(level) / 1048576.0,
121+
cfd->current()->NumLevelBytes(level) /
122+
cfd->compaction_picker()->MaxBytesForLevel(level),
123123
compaction_stats_[level].micros / 1e6, bytes_read / 1048576.0,
124124
compaction_stats_[level].bytes_written / 1048576.0,
125125
compaction_stats_[level].bytes_readn / 1048576.0,
@@ -285,10 +285,10 @@ bool InternalStats::GetProperty(const Slice& property, std::string* value,
285285

286286
return true;
287287
} else if (in == "sstables") {
288-
*value = current->DebugString();
288+
*value = cfd->current()->DebugString();
289289
return true;
290290
} else if (in == "num-immutable-mem-table") {
291-
*value = std::to_string(immsize);
291+
*value = std::to_string(cfd->imm()->size());
292292
return true;
293293
}
294294

db/internal_stats.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
#include <vector>
1717
#include <string>
1818

19+
class ColumnFamilyData;
20+
1921
namespace rocksdb {
2022
class InternalStats {
2123
public:
@@ -100,7 +102,7 @@ class InternalStats {
100102
}
101103

102104
bool GetProperty(const Slice& property, std::string* value,
103-
VersionSet* version_set, Version* current, int immsize);
105+
ColumnFamilyData* cfd);
104106

105107
private:
106108
std::vector<CompactionStats> compaction_stats_;

0 commit comments

Comments
 (0)