@@ -850,12 +850,8 @@ void VersionStorageInfo::GenerateLevelFilesBrief() {
850
850
}
851
851
}
852
852
853
- void Version::PrepareApply (const MutableCFOptions& mutable_cf_options,
854
- std::vector<uint64_t >& size_being_compacted) {
853
+ void Version::PrepareApply () {
855
854
UpdateAccumulatedStats ();
856
- storage_info_.ComputeCompactionScore (
857
- mutable_cf_options, cfd_->ioptions ()->compaction_options_fifo ,
858
- size_being_compacted);
859
855
storage_info_.UpdateFilesBySize ();
860
856
storage_info_.UpdateNumNonEmptyLevels ();
861
857
storage_info_.GenerateFileIndexer ();
@@ -947,7 +943,9 @@ void VersionStorageInfo::ComputeCompensatedSizes() {
947
943
for (int level = 0 ; level < num_levels_; level++) {
948
944
for (auto * file_meta : files_[level]) {
949
945
// Here we only compute compensated_file_size for those file_meta
950
- // which compensated_file_size is uninitialized (== 0).
946
+ // which compensated_file_size is uninitialized (== 0). This is true only
947
+ // for files that have been created right now and no other thread has
948
+ // access to them. That's why we can safely mutate compensated_file_size.
951
949
if (file_meta->compensated_file_size == 0 ) {
952
950
file_meta->compensated_file_size = file_meta->fd .GetFileSize () +
953
951
file_meta->num_deletions * average_value_size *
@@ -966,8 +964,7 @@ int VersionStorageInfo::MaxInputLevel() const {
966
964
967
965
void VersionStorageInfo::ComputeCompactionScore (
968
966
const MutableCFOptions& mutable_cf_options,
969
- const CompactionOptionsFIFO& compaction_options_fifo,
970
- std::vector<uint64_t >& size_being_compacted) {
967
+ const CompactionOptionsFIFO& compaction_options_fifo) {
971
968
double max_score = 0 ;
972
969
int max_score_level = 0 ;
973
970
@@ -1008,9 +1005,13 @@ void VersionStorageInfo::ComputeCompactionScore(
1008
1005
}
1009
1006
} else {
1010
1007
// Compute the ratio of current size to size limit.
1011
- const uint64_t level_bytes =
1012
- TotalCompensatedFileSize (files_[level]) - size_being_compacted[level];
1013
- score = static_cast <double >(level_bytes) /
1008
+ uint64_t level_bytes_no_compacting = 0 ;
1009
+ for (auto f : files_[level]) {
1010
+ if (f && f->being_compacted == false ) {
1011
+ level_bytes_no_compacting += f->compensated_file_size ;
1012
+ }
1013
+ }
1014
+ score = static_cast <double >(level_bytes_no_compacting) /
1014
1015
mutable_cf_options.MaxBytesForLevel (level);
1015
1016
if (max_score < score) {
1016
1017
max_score = score;
@@ -1527,6 +1528,11 @@ VersionSet::~VersionSet() {
1527
1528
1528
1529
void VersionSet::AppendVersion (ColumnFamilyData* column_family_data,
1529
1530
Version* v) {
1531
+ // compute new compaction score
1532
+ v->storage_info ()->ComputeCompactionScore (
1533
+ *column_family_data->GetLatestMutableCFOptions (),
1534
+ column_family_data->ioptions ()->compaction_options_fifo );
1535
+
1530
1536
// Mark v finalized
1531
1537
v->storage_info_ .SetFinalized ();
1532
1538
@@ -1637,13 +1643,6 @@ Status VersionSet::LogAndApply(ColumnFamilyData* column_family_data,
1637
1643
// Unlock during expensive operations. New writes cannot get here
1638
1644
// because &w is ensuring that all new writes get queued.
1639
1645
{
1640
- std::vector<uint64_t > size_being_compacted;
1641
- if (!edit->IsColumnFamilyManipulation ()) {
1642
- size_being_compacted.resize (v->storage_info ()->num_levels () - 1 );
1643
- // calculate the amount of data being compacted at every level
1644
- column_family_data->compaction_picker ()->SizeBeingCompacted (
1645
- size_being_compacted);
1646
- }
1647
1646
1648
1647
mu->Unlock ();
1649
1648
@@ -1674,7 +1673,7 @@ Status VersionSet::LogAndApply(ColumnFamilyData* column_family_data,
1674
1673
1675
1674
if (!edit->IsColumnFamilyManipulation ()) {
1676
1675
// This is cpu-heavy operations, which should be called outside mutex.
1677
- v->PrepareApply (mutable_cf_options, size_being_compacted );
1676
+ v->PrepareApply ();
1678
1677
}
1679
1678
1680
1679
// Write new record to MANIFEST log
@@ -2097,10 +2096,7 @@ Status VersionSet::Recover(
2097
2096
builder->SaveTo (v->storage_info ());
2098
2097
2099
2098
// Install recovered version
2100
- std::vector<uint64_t > size_being_compacted (
2101
- v->storage_info ()->num_levels () - 1 );
2102
- cfd->compaction_picker ()->SizeBeingCompacted (size_being_compacted);
2103
- v->PrepareApply (*cfd->GetLatestMutableCFOptions (), size_being_compacted);
2099
+ v->PrepareApply ();
2104
2100
AppendVersion (cfd, v);
2105
2101
}
2106
2102
@@ -2434,10 +2430,7 @@ Status VersionSet::DumpManifest(Options& options, std::string& dscname,
2434
2430
2435
2431
Version* v = new Version (cfd, this , current_version_number_++);
2436
2432
builder->SaveTo (v->storage_info ());
2437
- std::vector<uint64_t > size_being_compacted (
2438
- v->storage_info ()->num_levels () - 1 );
2439
- cfd->compaction_picker ()->SizeBeingCompacted (size_being_compacted);
2440
- v->PrepareApply (*cfd->GetLatestMutableCFOptions (), size_being_compacted);
2433
+ v->PrepareApply ();
2441
2434
2442
2435
printf (" --------------- Column family \" %s\" (ID %u) --------------\n " ,
2443
2436
cfd->GetName ().c_str (), (unsigned int )cfd->GetID ());
0 commit comments