Skip to content

Commit 27a8856

Browse files
committed
Compacting column families
Summary: This diff enables non-default column families to get compacted both automatically and also by calling CompactRange() Test Plan: make check Reviewers: dhruba, haobo, kailiu, sdong CC: leveldb Differential Revision: https://reviews.facebook.net/D15813
1 parent 5661ed8 commit 27a8856

6 files changed

+177
-64
lines changed

db/column_family_test.cc

+87-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,46 @@ class ColumnFamilyTest {
8383
return result;
8484
}
8585

86+
void Compact(int cf, const Slice& start, const Slice& limit) {
87+
ASSERT_OK(db_->CompactRange(handles_[cf], &start, &limit));
88+
}
89+
90+
int NumTableFilesAtLevel(int cf, int level) {
91+
string property;
92+
ASSERT_TRUE(db_->GetProperty(
93+
handles_[cf], "rocksdb.num-files-at-level" + NumberToString(level),
94+
&property));
95+
return atoi(property.c_str());
96+
}
97+
98+
// Return spread of files per level
99+
string FilesPerLevel(int cf) {
100+
string result;
101+
int last_non_zero_offset = 0;
102+
for (int level = 0; level < column_family_options_.num_levels; level++) {
103+
int f = NumTableFilesAtLevel(cf, level);
104+
char buf[100];
105+
snprintf(buf, sizeof(buf), "%s%d", (level ? "," : ""), f);
106+
result += buf;
107+
if (f > 0) {
108+
last_non_zero_offset = result.size();
109+
}
110+
}
111+
result.resize(last_non_zero_offset);
112+
return result;
113+
}
114+
115+
// Do n memtable flushes, each of which produces an sstable
116+
// covering the range [small,large].
117+
void MakeTables(int cf, int n, const string& small,
118+
const string& large) {
119+
for (int i = 0; i < n; i++) {
120+
ASSERT_OK(Put(cf, small, "begin"));
121+
ASSERT_OK(Put(cf, large, "end"));
122+
ASSERT_OK(db_->Flush(FlushOptions(), handles_[cf]));
123+
}
124+
}
125+
86126
void CopyFile(const string& source, const string& destination,
87127
uint64_t size = 0) {
88128
const EnvOptions soptions;
@@ -111,7 +151,7 @@ class ColumnFamilyTest {
111151
ColumnFamilyOptions column_family_options_;
112152
DBOptions db_options_;
113153
string dbname_;
114-
DB* db_;
154+
DB* db_ = nullptr;
115155
Env* env_;
116156
};
117157

@@ -274,6 +314,52 @@ TEST(ColumnFamilyTest, FlushTest) {
274314
Close();
275315
}
276316

317+
// This is the same as DBTest::ManualCompaction, but it does all
318+
// operations on non-default column family
319+
TEST(ColumnFamilyTest, ManualCompaction) {
320+
// iter - 0 with 7 levels
321+
// iter - 1 with 3 levels
322+
int cf = 1;
323+
for (int iter = 0; iter < 2; ++iter) {
324+
column_family_options_.num_levels = (iter == 0) ? 3 : 7;
325+
Destroy();
326+
ASSERT_OK(Open({"default"}));
327+
CreateColumnFamilies({"one"});
328+
Close();
329+
ASSERT_OK(Open({"default", "one"}));
330+
331+
MakeTables(cf, 3, "p", "q");
332+
ASSERT_EQ("1,1,1", FilesPerLevel(cf));
333+
334+
// Compaction range falls before files
335+
Compact(cf, "", "c");
336+
ASSERT_EQ("1,1,1", FilesPerLevel(cf));
337+
338+
// Compaction range falls after files
339+
Compact(cf, "r", "z");
340+
ASSERT_EQ("1,1,1", FilesPerLevel(cf));
341+
342+
// Compaction range overlaps files
343+
Compact(cf, "p1", "p9");
344+
ASSERT_EQ("0,0,1", FilesPerLevel(cf));
345+
346+
// Populate a different range
347+
MakeTables(cf, 3, "c", "e");
348+
ASSERT_EQ("1,1,2", FilesPerLevel(cf));
349+
350+
// Compact just the new range
351+
Compact(cf, "b", "f");
352+
ASSERT_EQ("0,0,2", FilesPerLevel(cf));
353+
354+
// Compact all
355+
MakeTables(cf, 1, "a", "z");
356+
ASSERT_EQ("0,1,2", FilesPerLevel(cf));
357+
Compact(cf, "", "zzz");
358+
ASSERT_EQ("0,0,1", FilesPerLevel(cf));
359+
}
360+
Close();
361+
}
362+
277363

278364
} // namespace rocksdb
279365

db/compaction.cc

+7
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
#include "db/compaction.h"
11+
#include "db/column_family.h"
1112

1213
namespace rocksdb {
1314

@@ -29,6 +30,7 @@ Compaction::Compaction(Version* input_version, int level, int out_level,
2930
max_grandparent_overlap_bytes_(max_grandparent_overlap_bytes),
3031
input_version_(input_version),
3132
number_levels_(input_version_->NumberLevels()),
33+
cfd_(input_version_->cfd_),
3234
seek_compaction_(seek_compaction),
3335
enable_compression_(enable_compression),
3436
grandparent_index_(0),
@@ -43,6 +45,7 @@ Compaction::Compaction(Version* input_version, int level, int out_level,
4345

4446
input_version_->Ref();
4547
edit_ = new VersionEdit();
48+
edit_->SetColumnFamily(cfd_->GetID());
4649
for (int i = 0; i < number_levels_; i++) {
4750
level_ptrs_[i] = 0;
4851
}
@@ -170,6 +173,10 @@ void Compaction::ReleaseInputs() {
170173
}
171174
}
172175

176+
void Compaction::ReleaseCompactionFiles(Status status) {
177+
cfd_->compaction_picker()->ReleaseCompactionFiles(this, status);
178+
}
179+
173180
void Compaction::ResetNextCompactionIndex() {
174181
input_version_->ResetNextCompactionIndex(level_);
175182
}

db/compaction.h

+8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
namespace rocksdb {
1414

1515
class Version;
16+
class ColumnFamilyData;
1617

1718
// A Compaction encapsulates information about a compaction.
1819
class Compaction {
@@ -36,6 +37,8 @@ class Compaction {
3637
// Returns input version of the compaction
3738
Version* input_version() const { return input_version_; }
3839

40+
ColumnFamilyData* column_family_data() const { return cfd_; }
41+
3942
// Return the ith input file at "level()+which" ("which" must be 0 or 1).
4043
FileMetaData* input(int which, int i) const { return inputs_[which][i]; }
4144

@@ -67,6 +70,10 @@ class Compaction {
6770
// is successful.
6871
void ReleaseInputs();
6972

73+
// Clear all files to indicate that they are not being compacted
74+
// Delete this compaction from the list of running compactions.
75+
void ReleaseCompactionFiles(Status status);
76+
7077
void Summary(char* output, int len);
7178

7279
// Return the score that was used to pick this compaction run.
@@ -94,6 +101,7 @@ class Compaction {
94101
Version* input_version_;
95102
VersionEdit* edit_;
96103
int number_levels_;
104+
ColumnFamilyData* cfd_;
97105

98106
bool seek_compaction_;
99107
bool enable_compression_;

0 commit comments

Comments
 (0)