in_memory_stats_history.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. // Copyright (c) 2011 The LevelDB Authors. All rights reserved.
  6. // Use of this source code is governed by a BSD-style license that can be
  7. // found in the LICENSE file. See the AUTHORS file for names of contributors.
  8. #pragma once
  9. #include "rocksdb/stats_history.h"
  10. namespace ROCKSDB_NAMESPACE {
  11. // InMemoryStatsHistoryIterator can be used to access stats history that was
  12. // stored by an in-memory two level std::map(DBImpl::stats_history_). It keeps
  13. // a copy of the stats snapshot (in stats_map_) that is currently being pointed
  14. // to, which allows the iterator to access the stats snapshot even when
  15. // the background garbage collecting thread purges it from the source of truth
  16. // (`DBImpl::stats_history_`). In that case, the iterator will continue to be
  17. // valid until a call to `Next()` returns no result and invalidates it. In
  18. // some extreme cases, the iterator may also return fragmented segments of
  19. // stats snapshots due to long gaps between `Next()` calls and interleaved
  20. // garbage collection.
  21. class InMemoryStatsHistoryIterator final : public StatsHistoryIterator {
  22. public:
  23. // Setup InMemoryStatsHistoryIterator to return stats snapshots between
  24. // seconds timestamps [start_time, end_time)
  25. InMemoryStatsHistoryIterator(uint64_t start_time, uint64_t end_time,
  26. DBImpl* db_impl)
  27. : start_time_(start_time),
  28. end_time_(end_time),
  29. valid_(true),
  30. db_impl_(db_impl) {
  31. AdvanceIteratorByTime(start_time_, end_time_);
  32. }
  33. // no copying allowed
  34. InMemoryStatsHistoryIterator(const InMemoryStatsHistoryIterator&) = delete;
  35. void operator=(const InMemoryStatsHistoryIterator&) = delete;
  36. InMemoryStatsHistoryIterator(InMemoryStatsHistoryIterator&&) = delete;
  37. InMemoryStatsHistoryIterator& operator=(InMemoryStatsHistoryIterator&&) =
  38. delete;
  39. ~InMemoryStatsHistoryIterator() override;
  40. bool Valid() const override;
  41. Status status() const override;
  42. // Move to the next stats snapshot currently available
  43. // This function may invalidate the iterator
  44. // REQUIRES: Valid()
  45. void Next() override;
  46. // REQUIRES: Valid()
  47. uint64_t GetStatsTime() const override;
  48. // This function is idempotent
  49. // REQUIRES: Valid()
  50. const std::map<std::string, uint64_t>& GetStatsMap() const override;
  51. private:
  52. // advance the iterator to the next stats history record with timestamp
  53. // between [start_time, end_time)
  54. void AdvanceIteratorByTime(uint64_t start_time, uint64_t end_time);
  55. uint64_t time_;
  56. uint64_t start_time_;
  57. uint64_t end_time_;
  58. std::map<std::string, uint64_t> stats_map_;
  59. Status status_;
  60. bool valid_;
  61. DBImpl* db_impl_;
  62. };
  63. } // namespace ROCKSDB_NAMESPACE