@@ -26,6 +26,11 @@ static const int kTargetFileSize = 2 * 1048576;
26
26
// stop building a single file in a level->level+1 compaction.
27
27
static const int64_t kMaxGrandParentOverlapBytes = 10 * kTargetFileSize ;
28
28
29
+ // Maximum number of bytes in all compacted files. We avoid expanding
30
+ // the lower level file set of a compaction if it would make the
31
+ // total compaction cover more than this many bytes.
32
+ static const int64_t kExpandedCompactionByteSizeLimit = 25 * kTargetFileSize ;
33
+
29
34
static double MaxBytesForLevel (int level) {
30
35
// Note: the result for level zero is not really used since we set
31
36
// the level-0 compaction threshold based on number of files.
@@ -1223,20 +1228,26 @@ void VersionSet::SetupOtherInputs(Compaction* c) {
1223
1228
if (!c->inputs_ [1 ].empty ()) {
1224
1229
std::vector<FileMetaData*> expanded0;
1225
1230
current_->GetOverlappingInputs (level, &all_start, &all_limit, &expanded0);
1226
- if (expanded0.size () > c->inputs_ [0 ].size ()) {
1231
+ const int64_t inputs0_size = TotalFileSize (c->inputs_ [0 ]);
1232
+ const int64_t inputs1_size = TotalFileSize (c->inputs_ [1 ]);
1233
+ const int64_t expanded0_size = TotalFileSize (expanded0);
1234
+ if (expanded0.size () > c->inputs_ [0 ].size () &&
1235
+ inputs1_size + expanded0_size < kExpandedCompactionByteSizeLimit ) {
1227
1236
InternalKey new_start, new_limit;
1228
1237
GetRange (expanded0, &new_start, &new_limit);
1229
1238
std::vector<FileMetaData*> expanded1;
1230
1239
current_->GetOverlappingInputs (level+1 , &new_start, &new_limit,
1231
1240
&expanded1);
1232
1241
if (expanded1.size () == c->inputs_ [1 ].size ()) {
1233
1242
Log (options_->info_log ,
1234
- " Expanding@%d %d+%d to %d+%d\n " ,
1243
+ " Expanding@%d %d+%d (%ld+%ld bytes) to %d+%d (%ld+%ld bytes) \n " ,
1235
1244
level,
1236
1245
int (c->inputs_ [0 ].size ()),
1237
1246
int (c->inputs_ [1 ].size ()),
1247
+ long (inputs0_size), long (inputs1_size),
1238
1248
int (expanded0.size ()),
1239
- int (expanded1.size ()));
1249
+ int (expanded1.size ()),
1250
+ long (expanded0_size), long (inputs1_size));
1240
1251
smallest = new_start;
1241
1252
largest = new_limit;
1242
1253
c->inputs_ [0 ] = expanded0;
0 commit comments