flush_scheduler.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. #pragma once
  6. #include <stdint.h>
  7. #include <atomic>
  8. #include <mutex>
  9. #include <set>
  10. #include "util/autovector.h"
  11. namespace ROCKSDB_NAMESPACE {
  12. class ColumnFamilyData;
  13. // FlushScheduler keeps track of all column families whose memtable may
  14. // be full and require flushing. Unless otherwise noted, all methods on
  15. // FlushScheduler should be called only with the DB mutex held or from
  16. // a single-threaded recovery context.
  17. class FlushScheduler {
  18. public:
  19. FlushScheduler() : head_(nullptr) {}
  20. // May be called from multiple threads at once, but not concurrent with
  21. // any other method calls on this instance
  22. void ScheduleWork(ColumnFamilyData* cfd);
  23. // Removes and returns Ref()-ed column family. Client needs to Unref().
  24. // Filters column families that have been dropped.
  25. ColumnFamilyData* TakeNextColumnFamily();
  26. // This can be called concurrently with ScheduleWork but it would miss all
  27. // the scheduled flushes after the last synchronization. This would result
  28. // into less precise enforcement of memtable sizes but should not matter much.
  29. bool Empty();
  30. void Clear();
  31. private:
  32. struct Node {
  33. ColumnFamilyData* column_family;
  34. Node* next;
  35. };
  36. std::atomic<Node*> head_;
  37. #ifndef NDEBUG
  38. std::mutex checking_mutex_;
  39. std::set<ColumnFamilyData*> checking_set_;
  40. #endif // NDEBUG
  41. };
  42. } // namespace ROCKSDB_NAMESPACE