persistent_stats_history.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "port/likely.h"
  14. #include "util/string_util.h"
  15. namespace ROCKSDB_NAMESPACE {
  16. // 10 digit seconds timestamp => [Sep 9, 2001 ~ Nov 20, 2286]
  17. const int kNowSecondsStringLength = 10;
  18. const std::string kFormatVersionKeyString =
  19. "__persistent_stats_format_version__";
  20. const std::string kCompatibleVersionKeyString =
  21. "__persistent_stats_compatible_version__";
  22. // Every release maintains two versions numbers for persistents stats: Current
  23. // format version and compatible format version. Current format version
  24. // designates what type of encoding will be used when writing to stats CF;
  25. // compatible format version designates the minimum format version that
  26. // can decode the stats CF encoded using the current format version.
  27. const uint64_t kStatsCFCurrentFormatVersion = 1;
  28. const uint64_t kStatsCFCompatibleFormatVersion = 1;
  29. Status DecodePersistentStatsVersionNumber(DBImpl* db, StatsVersionKeyType type,
  30. uint64_t* version_number) {
  31. if (type >= StatsVersionKeyType::kKeyTypeMax) {
  32. return Status::InvalidArgument("Invalid stats version key type provided");
  33. }
  34. std::string key;
  35. if (type == StatsVersionKeyType::kFormatVersion) {
  36. key = kFormatVersionKeyString;
  37. } else if (type == StatsVersionKeyType::kCompatibleVersion) {
  38. key = kCompatibleVersionKeyString;
  39. }
  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() {}
  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 = port::kMaxUint64;
  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 = port::kMaxUint64;
  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. ReadOptions ro;
  111. Iterator* iter =
  112. db_impl_->NewIterator(ro, db_impl_->PersistentStatsColumnFamily());
  113. char timestamp[kNowSecondsStringLength + 1];
  114. snprintf(timestamp, sizeof(timestamp), "%010d",
  115. static_cast<int>(std::max(time_, start_time)));
  116. timestamp[kNowSecondsStringLength] = '\0';
  117. iter->Seek(timestamp);
  118. // no more entries with timestamp >= start_time is found or version key
  119. // is found to be incompatible
  120. if (!iter->Valid()) {
  121. valid_ = false;
  122. delete iter;
  123. return;
  124. }
  125. time_ = parseKey(iter->key(), start_time).first;
  126. valid_ = true;
  127. // check parsed time and invalid if it exceeds end_time
  128. if (time_ > end_time) {
  129. valid_ = false;
  130. delete iter;
  131. return;
  132. }
  133. // find all entries with timestamp equal to time_
  134. std::map<std::string, uint64_t> new_stats_map;
  135. std::pair<uint64_t, std::string> kv;
  136. for (; iter->Valid(); iter->Next()) {
  137. kv = parseKey(iter->key(), start_time);
  138. if (kv.first != time_) {
  139. break;
  140. }
  141. if (kv.second.compare(kFormatVersionKeyString) == 0) {
  142. continue;
  143. }
  144. new_stats_map[kv.second] = ParseUint64(iter->value().ToString());
  145. }
  146. stats_map_.swap(new_stats_map);
  147. delete iter;
  148. } else {
  149. valid_ = false;
  150. }
  151. }
  152. } // namespace ROCKSDB_NAMESPACE