logs_with_prep_tracker.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. //
  6. #pragma once
  7. #include <stdint.h>
  8. #include <cassert>
  9. #include <cstdlib>
  10. #include <mutex>
  11. #include <unordered_map>
  12. #include <vector>
  13. #include "rocksdb/rocksdb_namespace.h"
  14. namespace ROCKSDB_NAMESPACE {
  15. // This class is used to track the log files with outstanding prepare entries.
  16. class LogsWithPrepTracker {
  17. public:
  18. // Called when a transaction prepared in `log` has been committed or aborted.
  19. void MarkLogAsHavingPrepSectionFlushed(uint64_t log);
  20. // Called when a transaction is prepared in `log`.
  21. void MarkLogAsContainingPrepSection(uint64_t log);
  22. // Return the earliest log file with outstanding prepare entries.
  23. uint64_t FindMinLogContainingOutstandingPrep();
  24. size_t TEST_PreparedSectionCompletedSize() {
  25. return prepared_section_completed_.size();
  26. }
  27. size_t TEST_LogsWithPrepSize() { return logs_with_prep_.size(); }
  28. private:
  29. // REQUIRES: logs_with_prep_mutex_ held
  30. //
  31. // sorted list of log numbers still containing prepared data.
  32. // this is used by FindObsoleteFiles to determine which
  33. // flushed logs we must keep around because they still
  34. // contain prepared data which has not been committed or rolled back
  35. struct LogCnt {
  36. uint64_t log; // the log number
  37. uint64_t cnt; // number of prepared sections in the log
  38. };
  39. std::vector<LogCnt> logs_with_prep_;
  40. std::mutex logs_with_prep_mutex_;
  41. // REQUIRES: prepared_section_completed_mutex_ held
  42. //
  43. // to be used in conjunction with logs_with_prep_.
  44. // once a transaction with data in log L is committed or rolled back
  45. // rather than updating logs_with_prep_ directly we keep track of that
  46. // in prepared_section_completed_ which maps LOG -> instance_count. This helps
  47. // avoiding contention between a commit thread and the prepare threads.
  48. //
  49. // when trying to determine the minimum log still active we first
  50. // consult logs_with_prep_. while that root value maps to
  51. // an equal value in prepared_section_completed_ we erase the log from
  52. // both logs_with_prep_ and prepared_section_completed_.
  53. std::unordered_map<uint64_t, uint64_t> prepared_section_completed_;
  54. std::mutex prepared_section_completed_mutex_;
  55. };
  56. } // namespace ROCKSDB_NAMESPACE