persistent_stats_history.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. #include "monitoring/persistent_stats_history.h"
  9. #include <cstring>
  10. #include <string>
  11. #include <utility>
  12. #include "db/db_impl/db_impl.h"
  13. #include "util/string_util.h"
  14. namespace ROCKSDB_NAMESPACE {
  15. // 10 digit seconds timestamp => [Sep 9, 2001 ~ Nov 20, 2286]
  16. const int kNowSecondsStringLength = 10;
  17. const std::string kFormatVersionKeyString =
  18. "__persistent_stats_format_version__";
  19. const std::string kCompatibleVersionKeyString =
  20. "__persistent_stats_compatible_version__";
  21. // Every release maintains two versions numbers for persistents stats: Current
  22. // format version and compatible format version. Current format version
  23. // designates what type of encoding will be used when writing to stats CF;
  24. // compatible format version designates the minimum format version that
  25. // can decode the stats CF encoded using the current format version.
  26. const uint64_t kStatsCFCurrentFormatVersion = 1;
  27. const uint64_t kStatsCFCompatibleFormatVersion = 1;
  28. Status DecodePersistentStatsVersionNumber(DBImpl* db, StatsVersionKeyType type,
  29. uint64_t* version_number) {
  30. if (type >= StatsVersionKeyType::kKeyTypeMax) {
  31. return Status::InvalidArgument("Invalid stats version key type provided");
  32. }
  33. std::string key;
  34. if (type == StatsVersionKeyType::kFormatVersion) {
  35. key = kFormatVersionKeyString;
  36. } else if (type == StatsVersionKeyType::kCompatibleVersion) {
  37. key = kCompatibleVersionKeyString;
  38. }
  39. // TODO: plumb Env::IOActivity, Env::IOPriority
  40. ReadOptions options;
  41. options.verify_checksums = true;
  42. std::string result;
  43. Status s = db->Get(options, db->PersistentStatsColumnFamily(), key, &result);
  44. if (!s.ok() || result.empty()) {
  45. return Status::NotFound("Persistent stats version key " + key +
  46. " not found.");
  47. }
  48. // read version_number but do nothing in current version
  49. *version_number = ParseUint64(result);
  50. return Status::OK();
  51. }
  52. int EncodePersistentStatsKey(uint64_t now_seconds, const std::string& key,
  53. int size, char* buf) {
  54. char timestamp[kNowSecondsStringLength + 1];
  55. // make time stamp string equal in length to allow sorting by time
  56. snprintf(timestamp, sizeof(timestamp), "%010d",
  57. static_cast<int>(now_seconds));
  58. timestamp[kNowSecondsStringLength] = '\0';
  59. return snprintf(buf, size, "%s#%s", timestamp, key.c_str());
  60. }
  61. void OptimizeForPersistentStats(ColumnFamilyOptions* cfo) {
  62. cfo->write_buffer_size = 2 << 20;
  63. cfo->target_file_size_base = 2 * 1048576;
  64. cfo->max_bytes_for_level_base = 10 * 1048576;
  65. cfo->soft_pending_compaction_bytes_limit = 256 * 1048576;
  66. cfo->hard_pending_compaction_bytes_limit = 1073741824ul;
  67. cfo->compression = kNoCompression;
  68. }
  69. PersistentStatsHistoryIterator::~PersistentStatsHistoryIterator() = default;
  70. bool PersistentStatsHistoryIterator::Valid() const { return valid_; }
  71. Status PersistentStatsHistoryIterator::status() const { return status_; }
  72. void PersistentStatsHistoryIterator::Next() {
  73. // increment start_time by 1 to avoid infinite loop
  74. AdvanceIteratorByTime(GetStatsTime() + 1, end_time_);
  75. }
  76. uint64_t PersistentStatsHistoryIterator::GetStatsTime() const { return time_; }
  77. const std::map<std::string, uint64_t>&
  78. PersistentStatsHistoryIterator::GetStatsMap() const {
  79. return stats_map_;
  80. }
  81. std::pair<uint64_t, std::string> parseKey(const Slice& key,
  82. uint64_t start_time) {
  83. std::pair<uint64_t, std::string> result;
  84. std::string key_str = key.ToString();
  85. std::string::size_type pos = key_str.find('#');
  86. // TODO(Zhongyi): add counters to track parse failures?
  87. if (pos == std::string::npos) {
  88. result.first = std::numeric_limits<uint64_t>::max();
  89. result.second.clear();
  90. } else {
  91. uint64_t parsed_time = ParseUint64(key_str.substr(0, pos));
  92. // skip entries with timestamp smaller than start_time
  93. if (parsed_time < start_time) {
  94. result.first = std::numeric_limits<uint64_t>::max();
  95. result.second = "";
  96. } else {
  97. result.first = parsed_time;
  98. std::string key_resize = key_str.substr(pos + 1);
  99. result.second = key_resize;
  100. }
  101. }
  102. return result;
  103. }
  104. // advance the iterator to the next time between [start_time, end_time)
  105. // if success, update time_ and stats_map_ with new_time and stats_map
  106. void PersistentStatsHistoryIterator::AdvanceIteratorByTime(uint64_t start_time,
  107. uint64_t end_time) {
  108. // try to find next entry in stats_history_ map
  109. if (db_impl_ != nullptr) {
  110. // TODO: plumb Env::IOActivity, Env::IOPriority
  111. ReadOptions ro;
  112. Iterator* iter =
  113. db_impl_->NewIterator(ro, db_impl_->PersistentStatsColumnFamily());
  114. char timestamp[kNowSecondsStringLength + 1];
  115. snprintf(timestamp, sizeof(timestamp), "%010d",
  116. static_cast<int>(std::max(time_, start_time)));
  117. timestamp[kNowSecondsStringLength] = '\0';
  118. iter->Seek(timestamp);
  119. // no more entries with timestamp >= start_time is found or version key
  120. // is found to be incompatible
  121. if (!iter->Valid()) {
  122. valid_ = false;
  123. delete iter;
  124. return;
  125. }
  126. time_ = parseKey(iter->key(), start_time).first;
  127. valid_ = true;
  128. // check parsed time and invalid if it exceeds end_time
  129. if (time_ > end_time) {
  130. valid_ = false;
  131. delete iter;
  132. return;
  133. }
  134. // find all entries with timestamp equal to time_
  135. std::map<std::string, uint64_t> new_stats_map;
  136. std::pair<uint64_t, std::string> kv;
  137. for (; iter->Valid(); iter->Next()) {
  138. kv = parseKey(iter->key(), start_time);
  139. if (kv.first != time_) {
  140. break;
  141. }
  142. if (kv.second.compare(kFormatVersionKeyString) == 0) {
  143. continue;
  144. }
  145. new_stats_map[kv.second] = ParseUint64(iter->value().ToString());
  146. }
  147. stats_map_.swap(new_stats_map);
  148. delete iter;
  149. } else {
  150. valid_ = false;
  151. }
  152. }
  153. } // namespace ROCKSDB_NAMESPACE