Skip to content

Commit 0738ae6

Browse files
committed
Merge branch 'master' into columnfamilies
Conflicts: db/db_impl.cc
2 parents 9625acb + 8ca30bd commit 0738ae6

12 files changed

+566
-274
lines changed

HISTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
* By default, checksums are verified on every read from database
1414
* Added is_manual_compaction to CompactionFilter::Context
1515
* Added "virtual void WaitForJoin() = 0" in class Env
16+
* Removed BackupEngine::DeleteBackupsNewerThan() function
1617

1718
### New Features
1819
* If we find one truncated record at the end of the MANIFEST or WAL files,

db/compaction.cc

+45-14
Original file line numberDiff line numberDiff line change
@@ -191,41 +191,72 @@ void Compaction::ResetNextCompactionIndex() {
191191
input_version_->ResetNextCompactionIndex(level_);
192192
}
193193

194-
static void InputSummary(std::vector<FileMetaData*>& files, char* output,
194+
/*
195+
for sizes >=10TB, print "XXTB"
196+
for sizes >=10GB, print "XXGB"
197+
etc.
198+
*/
199+
static void FileSizeSummary(unsigned long long sz, char* output, int len) {
200+
const unsigned long long ull10 = 10;
201+
if (sz >= ull10<<40) {
202+
snprintf(output, len, "%lluTB", sz>>40);
203+
} else if (sz >= ull10<<30) {
204+
snprintf(output, len, "%lluGB", sz>>30);
205+
} else if (sz >= ull10<<20) {
206+
snprintf(output, len, "%lluMB", sz>>20);
207+
} else if (sz >= ull10<<10) {
208+
snprintf(output, len, "%lluKB", sz>>10);
209+
} else {
210+
snprintf(output, len, "%lluB", sz);
211+
}
212+
}
213+
214+
static int InputSummary(std::vector<FileMetaData*>& files, char* output,
195215
int len) {
196216
int write = 0;
197217
for (unsigned int i = 0; i < files.size(); i++) {
198218
int sz = len - write;
199-
int ret = snprintf(output + write, sz, "%lu(%lu) ",
200-
(unsigned long)files.at(i)->number,
201-
(unsigned long)files.at(i)->file_size);
219+
int ret;
220+
char sztxt[16];
221+
FileSizeSummary((unsigned long long)files.at(i)->file_size, sztxt, 16);
222+
ret = snprintf(output + write, sz, "%lu(%s) ",
223+
(unsigned long)files.at(i)->number,
224+
sztxt);
202225
if (ret < 0 || ret >= sz)
203226
break;
204227
write += ret;
205228
}
229+
return write;
206230
}
207231

208232
void Compaction::Summary(char* output, int len) {
209233
int write = snprintf(output, len,
210-
"Base version %lu Base level %d, seek compaction:%d, inputs:",
234+
"Base version %lu Base level %d, seek compaction:%d, inputs: [",
211235
(unsigned long)input_version_->GetVersionNumber(),
212236
level_,
213237
seek_compaction_);
214-
if (write < 0 || write > len) {
238+
if (write < 0 || write >= len) {
239+
return;
240+
}
241+
242+
write += InputSummary(inputs_[0], output+write, len-write);
243+
if (write < 0 || write >= len) {
244+
return;
245+
}
246+
247+
write += snprintf(output+write, len-write, "],[");
248+
if (write < 0 || write >= len) {
215249
return;
216250
}
217251

218-
char level_low_summary[100];
219-
InputSummary(inputs_[0], level_low_summary, sizeof(level_low_summary));
220-
char level_up_summary[100];
221252
if (inputs_[1].size()) {
222-
InputSummary(inputs_[1], level_up_summary, sizeof(level_up_summary));
223-
} else {
224-
level_up_summary[0] = '\0';
253+
write += InputSummary(inputs_[1], output+write, len-write);
254+
}
255+
if (write < 0 || write >= len) {
256+
return;
225257
}
226258

227-
snprintf(output + write, len - write, "[%s],[%s]",
228-
level_low_summary, level_up_summary);
259+
snprintf(output+write, len-write, "]");
229260
}
230261

231262
} // namespace rocksdb

db/compaction_picker.cc

+34-11
Original file line numberDiff line numberDiff line change
@@ -555,22 +555,27 @@ Compaction* UniversalCompactionPicker::PickCompaction(Version* version) {
555555
version->files_[level].size(), version->LevelFileSummary(&tmp, 0));
556556

557557
// Check for size amplification first.
558-
Compaction* c = PickCompactionUniversalSizeAmp(version, score);
559-
if (c == nullptr) {
558+
Compaction* c;
559+
if ((c = PickCompactionUniversalSizeAmp(version, score)) != nullptr) {
560+
Log(options_->info_log, "Universal: compacting for size amp\n");
561+
} else {
560562

561563
// Size amplification is within limits. Try reducing read
562564
// amplification while maintaining file size ratios.
563565
unsigned int ratio = options_->compaction_options_universal.size_ratio;
564-
c = PickCompactionUniversalReadAmp(version, score, ratio, UINT_MAX);
565566

566-
// Size amplification and file size ratios are within configured limits.
567-
// If max read amplification is exceeding configured limits, then force
568-
// compaction without looking at filesize ratios and try to reduce
569-
// the number of files to fewer than level0_file_num_compaction_trigger.
570-
if (c == nullptr) {
567+
if ((c = PickCompactionUniversalReadAmp(version, score, ratio, UINT_MAX)) != nullptr) {
568+
Log(options_->info_log, "Universal: compacting for size ratio\n");
569+
} else {
570+
// Size amplification and file size ratios are within configured limits.
571+
// If max read amplification is exceeding configured limits, then force
572+
// compaction without looking at filesize ratios and try to reduce
573+
// the number of files to fewer than level0_file_num_compaction_trigger.
571574
unsigned int num_files = version->files_[level].size() -
572575
options_->level0_file_num_compaction_trigger;
573-
c = PickCompactionUniversalReadAmp(version, score, UINT_MAX, num_files);
576+
if ((c = PickCompactionUniversalReadAmp(version, score, UINT_MAX, num_files)) != nullptr) {
577+
Log(options_->info_log, "Universal: compacting for file num\n");
578+
}
574579
}
575580
}
576581
if (c == nullptr) {
@@ -675,14 +680,32 @@ Compaction* UniversalCompactionPicker::PickCompactionUniversalReadAmp(
675680
if (f->being_compacted) {
676681
break;
677682
}
678-
// pick files if the total candidate file size (increased by the
683+
// Pick files if the total/last candidate file size (increased by the
679684
// specified ratio) is still larger than the next candidate file.
685+
// candidate_size is the total size of files picked so far with the
686+
// default kCompactionStopStyleTotalSize; with
687+
// kCompactionStopStyleSimilarSize, it's simply the size of the last
688+
// picked file.
680689
uint64_t sz = (candidate_size * (100L + ratio)) /100;
681690
if (sz < f->file_size) {
682691
break;
692+
}
693+
if (options_->compaction_options_universal.stop_style == kCompactionStopStyleSimilarSize) {
694+
// Similar-size stopping rule: also check the last picked file isn't
695+
// far larger than the next candidate file.
696+
sz = (f->file_size * (100L + ratio)) / 100;
697+
if (sz < candidate_size) {
698+
// If the small file we've encountered begins a run of similar-size
699+
// files, we'll pick them up on a future iteration of the outer
700+
// loop. If it's some lonely straggler, it'll eventually get picked
701+
// by the last-resort read amp strategy which disregards size ratios.
702+
break;
703+
}
704+
candidate_size = f->file_size;
705+
} else { // default kCompactionStopStyleTotalSize
706+
candidate_size += f->file_size;
683707
}
684708
candidate_count++;
685-
candidate_size += f->file_size;
686709
}
687710

688711
// Found a series of consecutive files that need compaction.

0 commit comments

Comments
 (0)