Skip to content

Commit 4605e20

Browse files
committed
If User setting of compaction multipliers overflow, use default value 1 instead
Summary: Currently, compaction multipliers can overflow and cause unexpected behaviors. In this patch, we detect those overflows and use multiplier 1 for them. Test Plan: make all check Reviewers: dhruba, haobo, igor, kailiu Reviewed By: kailiu CC: leveldb Differential Revision: https://reviews.facebook.net/D15321
1 parent aba2acb commit 4605e20

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

db/compaction_picker.cc

+20-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,21 @@ uint64_t TotalFileSize(const std::vector<FileMetaData*>& files) {
2222
return sum;
2323
}
2424

25+
// Multiple two operands. If they overflow, return op1.
26+
uint64_t MultiplyCheckOverflow(uint64_t op1, int op2) {
27+
if (op1 == 0) {
28+
return 0;
29+
}
30+
if (op2 <= 0) {
31+
return op1;
32+
}
33+
uint64_t casted_op2 = (uint64_t) op2;
34+
if (std::numeric_limits<uint64_t>::max() / op1 < casted_op2) {
35+
return op1;
36+
}
37+
return op1 * casted_op2;
38+
}
39+
2540
} // anonymous namespace
2641

2742
CompactionPicker::CompactionPicker(const Options* options,
@@ -48,10 +63,11 @@ void CompactionPicker::Init() {
4863
max_file_size_[i] = ULLONG_MAX;
4964
level_max_bytes_[i] = options_->max_bytes_for_level_base;
5065
} else if (i > 1) {
51-
max_file_size_[i] = max_file_size_[i - 1] * target_file_size_multiplier;
52-
level_max_bytes_[i] =
53-
level_max_bytes_[i - 1] * max_bytes_multiplier *
54-
options_->max_bytes_for_level_multiplier_additional[i - 1];
66+
max_file_size_[i] = MultiplyCheckOverflow(max_file_size_[i - 1],
67+
target_file_size_multiplier);
68+
level_max_bytes_[i] = MultiplyCheckOverflow(
69+
MultiplyCheckOverflow(level_max_bytes_[i - 1], max_bytes_multiplier),
70+
options_->max_bytes_for_level_multiplier_additional[i - 1]);
5571
} else {
5672
max_file_size_[i] = options_->target_file_size_base;
5773
level_max_bytes_[i] = options_->max_bytes_for_level_base;

0 commit comments

Comments
 (0)