option_change_migration.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
  2. // This source code is licensed under both the GPLv2 (found in the
  3. // COPYING file in the root directory) and Apache 2.0 License
  4. // (found in the LICENSE.Apache file in the root directory).
  5. #include "rocksdb/utilities/option_change_migration.h"
  6. #ifndef ROCKSDB_LITE
  7. #include "rocksdb/db.h"
  8. namespace ROCKSDB_NAMESPACE {
  9. namespace {
  10. // Return a version of Options `opts` that allow us to open/write into a DB
  11. // without triggering an automatic compaction or stalling. This is guaranteed
  12. // by disabling automatic compactions and using huge values for stalling
  13. // triggers.
  14. Options GetNoCompactionOptions(const Options& opts) {
  15. Options ret_opts = opts;
  16. ret_opts.disable_auto_compactions = true;
  17. ret_opts.level0_slowdown_writes_trigger = 999999;
  18. ret_opts.level0_stop_writes_trigger = 999999;
  19. ret_opts.soft_pending_compaction_bytes_limit = 0;
  20. ret_opts.hard_pending_compaction_bytes_limit = 0;
  21. return ret_opts;
  22. }
  23. Status OpenDb(const Options& options, const std::string& dbname,
  24. std::unique_ptr<DB>* db) {
  25. db->reset();
  26. DB* tmpdb;
  27. Status s = DB::Open(options, dbname, &tmpdb);
  28. if (s.ok()) {
  29. db->reset(tmpdb);
  30. }
  31. return s;
  32. }
  33. Status CompactToLevel(const Options& options, const std::string& dbname,
  34. int dest_level, bool need_reopen) {
  35. std::unique_ptr<DB> db;
  36. Options no_compact_opts = GetNoCompactionOptions(options);
  37. if (dest_level == 0) {
  38. // L0 has strict sequenceID requirements to files to it. It's safer
  39. // to only put one compacted file to there.
  40. // This is only used for converting to universal compaction with
  41. // only one level. In this case, compacting to one file is also
  42. // optimal.
  43. no_compact_opts.target_file_size_base = 999999999999999;
  44. no_compact_opts.max_compaction_bytes = 999999999999999;
  45. }
  46. Status s = OpenDb(no_compact_opts, dbname, &db);
  47. if (!s.ok()) {
  48. return s;
  49. }
  50. CompactRangeOptions cro;
  51. cro.change_level = true;
  52. cro.target_level = dest_level;
  53. if (dest_level == 0) {
  54. // cannot use kForceOptimized because the compaction is expected to
  55. // generate one output file
  56. cro.bottommost_level_compaction = BottommostLevelCompaction::kForce;
  57. }
  58. db->CompactRange(cro, nullptr, nullptr);
  59. if (need_reopen) {
  60. // Need to restart DB to rewrite the manifest file.
  61. // In order to open a DB with specific num_levels, the manifest file should
  62. // contain no record that mentiones any level beyond num_levels. Issuing a
  63. // full compaction will move all the data to a level not exceeding
  64. // num_levels, but the manifest may still contain previous record mentioning
  65. // a higher level. Reopening the DB will force the manifest to be rewritten
  66. // so that those records will be cleared.
  67. db.reset();
  68. s = OpenDb(no_compact_opts, dbname, &db);
  69. }
  70. return s;
  71. }
  72. Status MigrateToUniversal(std::string dbname, const Options& old_opts,
  73. const Options& new_opts) {
  74. if (old_opts.num_levels <= new_opts.num_levels ||
  75. old_opts.compaction_style == CompactionStyle::kCompactionStyleFIFO) {
  76. return Status::OK();
  77. } else {
  78. bool need_compact = false;
  79. {
  80. std::unique_ptr<DB> db;
  81. Options opts = GetNoCompactionOptions(old_opts);
  82. Status s = OpenDb(opts, dbname, &db);
  83. if (!s.ok()) {
  84. return s;
  85. }
  86. ColumnFamilyMetaData metadata;
  87. db->GetColumnFamilyMetaData(&metadata);
  88. if (!metadata.levels.empty() &&
  89. metadata.levels.back().level >= new_opts.num_levels) {
  90. need_compact = true;
  91. }
  92. }
  93. if (need_compact) {
  94. return CompactToLevel(old_opts, dbname, new_opts.num_levels - 1, true);
  95. }
  96. return Status::OK();
  97. }
  98. }
  99. Status MigrateToLevelBase(std::string dbname, const Options& old_opts,
  100. const Options& new_opts) {
  101. if (!new_opts.level_compaction_dynamic_level_bytes) {
  102. if (old_opts.num_levels == 1) {
  103. return Status::OK();
  104. }
  105. // Compact everything to level 1 to guarantee it can be safely opened.
  106. Options opts = old_opts;
  107. opts.target_file_size_base = new_opts.target_file_size_base;
  108. // Although sometimes we can open the DB with the new option without error,
  109. // We still want to compact the files to avoid the LSM tree to stuck
  110. // in bad shape. For example, if the user changed the level size
  111. // multiplier from 4 to 8, with the same data, we will have fewer
  112. // levels. Unless we issue a full comaction, the LSM tree may stuck
  113. // with more levels than needed and it won't recover automatically.
  114. return CompactToLevel(opts, dbname, 1, true);
  115. } else {
  116. // Compact everything to the last level to guarantee it can be safely
  117. // opened.
  118. if (old_opts.num_levels == 1) {
  119. return Status::OK();
  120. } else if (new_opts.num_levels > old_opts.num_levels) {
  121. // Dynamic level mode requires data to be put in the last level first.
  122. return CompactToLevel(new_opts, dbname, new_opts.num_levels - 1, false);
  123. } else {
  124. Options opts = old_opts;
  125. opts.target_file_size_base = new_opts.target_file_size_base;
  126. return CompactToLevel(opts, dbname, new_opts.num_levels - 1, true);
  127. }
  128. }
  129. }
  130. } // namespace
  131. Status OptionChangeMigration(std::string dbname, const Options& old_opts,
  132. const Options& new_opts) {
  133. if (old_opts.compaction_style == CompactionStyle::kCompactionStyleFIFO) {
  134. // LSM generated by FIFO compation can be opened by any compaction.
  135. return Status::OK();
  136. } else if (new_opts.compaction_style ==
  137. CompactionStyle::kCompactionStyleUniversal) {
  138. return MigrateToUniversal(dbname, old_opts, new_opts);
  139. } else if (new_opts.compaction_style ==
  140. CompactionStyle::kCompactionStyleLevel) {
  141. return MigrateToLevelBase(dbname, old_opts, new_opts);
  142. } else if (new_opts.compaction_style ==
  143. CompactionStyle::kCompactionStyleFIFO) {
  144. return CompactToLevel(old_opts, dbname, 0, true);
  145. } else {
  146. return Status::NotSupported(
  147. "Do not how to migrate to this compaction style");
  148. }
  149. }
  150. } // namespace ROCKSDB_NAMESPACE
  151. #else
  152. namespace ROCKSDB_NAMESPACE {
  153. Status OptionChangeMigration(std::string /*dbname*/,
  154. const Options& /*old_opts*/,
  155. const Options& /*new_opts*/) {
  156. return Status::NotSupported();
  157. }
  158. } // namespace ROCKSDB_NAMESPACE
  159. #endif // ROCKSDB_LITE