subcompaction_state.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (c) Meta Platforms, Inc. and affiliates.
  2. //
  3. // This source code is licensed under both the GPLv2 (found in the
  4. // COPYING file in the root directory) and Apache 2.0 License
  5. // (found in the LICENSE.Apache file in the root directory).
  6. //
  7. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  8. // Use of this source code is governed by a BSD-style license that can be
  9. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  10. #include "db/compaction/subcompaction_state.h"
  11. #include "rocksdb/sst_partitioner.h"
  12. namespace ROCKSDB_NAMESPACE {
  13. void SubcompactionState::AggregateCompactionOutputStats(
  14. InternalStats::CompactionStatsFull& internal_stats) const {
  15. // Outputs should be closed. By extension, any files created just for
  16. // range deletes have already been written also.
  17. assert(compaction_outputs_.HasBuilder() == false);
  18. assert(proximal_level_outputs_.HasBuilder() == false);
  19. // FIXME: These stats currently include abandonned output files
  20. // assert(compaction_outputs_.stats_.num_output_files ==
  21. // compaction_outputs_.outputs_.size());
  22. // assert(proximal_level_outputs_.stats_.num_output_files ==
  23. // proximal_level_outputs_.outputs_.size());
  24. internal_stats.output_level_stats.Add(compaction_outputs_.stats_);
  25. if (proximal_level_outputs_.HasOutput()) {
  26. internal_stats.has_proximal_level_output = true;
  27. internal_stats.proximal_level_stats.Add(proximal_level_outputs_.stats_);
  28. }
  29. }
  30. OutputIterator SubcompactionState::GetOutputs() const {
  31. return OutputIterator(proximal_level_outputs_.outputs_,
  32. compaction_outputs_.outputs_);
  33. }
  34. void SubcompactionState::Cleanup(Cache* cache) {
  35. proximal_level_outputs_.Cleanup();
  36. compaction_outputs_.Cleanup();
  37. if (!status.ok()) {
  38. for (const auto& out : GetOutputs()) {
  39. // If this file was inserted into the table cache then remove it here
  40. // because this compaction was not committed. This is not strictly
  41. // required because of a backstop TableCache::Evict() in
  42. // PurgeObsoleteFiles() but is our opportunity to apply
  43. // uncache_aggressiveness. TODO: instead, put these files into the
  44. // VersionSet::obsolete_files_ pipeline so that they don't have to
  45. // be picked up by scanning the DB directory.
  46. TableCache::ReleaseObsolete(
  47. cache, out.meta.fd.GetNumber(), nullptr /*handle*/,
  48. compaction->mutable_cf_options().uncache_aggressiveness);
  49. }
  50. }
  51. // TODO: sub_compact.io_status is not checked like status. Not sure if thats
  52. // intentional. So ignoring the io_status as of now.
  53. io_status.PermitUncheckedError();
  54. }
  55. Slice SubcompactionState::SmallestUserKey() const {
  56. if (proximal_level_outputs_.HasOutput()) {
  57. Slice a = compaction_outputs_.SmallestUserKey();
  58. Slice b = proximal_level_outputs_.SmallestUserKey();
  59. if (a.empty()) {
  60. return b;
  61. }
  62. if (b.empty()) {
  63. return a;
  64. }
  65. const Comparator* user_cmp =
  66. compaction->column_family_data()->user_comparator();
  67. if (user_cmp->Compare(a, b) > 0) {
  68. return b;
  69. } else {
  70. return a;
  71. }
  72. } else {
  73. return compaction_outputs_.SmallestUserKey();
  74. }
  75. }
  76. Slice SubcompactionState::LargestUserKey() const {
  77. if (proximal_level_outputs_.HasOutput()) {
  78. Slice a = compaction_outputs_.LargestUserKey();
  79. Slice b = proximal_level_outputs_.LargestUserKey();
  80. if (a.empty()) {
  81. return b;
  82. }
  83. if (b.empty()) {
  84. return a;
  85. }
  86. const Comparator* user_cmp =
  87. compaction->column_family_data()->user_comparator();
  88. if (user_cmp->Compare(a, b) < 0) {
  89. return b;
  90. } else {
  91. return a;
  92. }
  93. } else {
  94. return compaction_outputs_.LargestUserKey();
  95. }
  96. }
  97. Status SubcompactionState::AddToOutput(
  98. const CompactionIterator& iter, bool use_proximal_output,
  99. const CompactionFileOpenFunc& open_file_func,
  100. const CompactionFileCloseFunc& close_file_func,
  101. const ParsedInternalKey& prev_table_last_internal_key) {
  102. // update target output
  103. current_outputs_ =
  104. use_proximal_output ? &proximal_level_outputs_ : &compaction_outputs_;
  105. return current_outputs_->AddToOutput(iter, open_file_func, close_file_func,
  106. prev_table_last_internal_key);
  107. }
  108. } // namespace ROCKSDB_NAMESPACE