Skip to content

Commit

Permalink
MB-53294: Introduce storage_bytes metering metric
Browse files Browse the repository at this point in the history
Exposes disk usage per-bucket, and is included in the _metering
endpoint.

Change-Id: Ifb3fec767a2e7d23ad8474b5dd6b9900239ae331
Reviewed-on: https://review.couchbase.org/c/kv_engine/+/178604
Tested-by: Build Bot <build@couchbase.com>
Reviewed-by: Paolo Cocchi <paolo.cocchi@couchbase.com>
  • Loading branch information
jameseh96 committed Aug 9, 2022
1 parent be15770 commit f61b2e1
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 7 deletions.
4 changes: 4 additions & 0 deletions daemon/buckets.cc
Expand Up @@ -112,6 +112,10 @@ nlohmann::json Bucket::to_json() const {

void Bucket::addMeteringMetrics(const BucketStatCollector& collector) const {
using namespace cb::stats;
// engine-related metering (e.g., disk usage)
getEngine().get_prometheus_stats(
collector, cb::prometheus::MetricGroup::Metering);

// metering
collector.addStat(Key::meter_ru_total, read_units_used);
collector.addStat(Key::meter_wu_total, write_units_used);
Expand Down
18 changes: 12 additions & 6 deletions engines/ep/src/ep_bucket.cc
Expand Up @@ -1516,17 +1516,23 @@ bool EPBucket::updateCompactionTasks(Vbid vbid) {
return false;
}

cb::engine_errc EPBucket::getFileStats(const BucketStatCollector& collector) {
DBFileInfo EPBucket::getAggregatedFileInfo() {
const auto numShards = vbMap.getNumShards();
DBFileInfo totalInfo;

for (uint16_t shardId = 0; shardId < numShards; shardId++) {
const auto dbInfo =
getRWUnderlyingByShard(shardId)->getAggrDbFileInfo();
totalInfo.spaceUsed += dbInfo.spaceUsed;
totalInfo.fileSize += dbInfo.fileSize;
totalInfo.prepareBytes += dbInfo.prepareBytes;
totalInfo += getRWUnderlyingByShard(shardId)->getAggrDbFileInfo();
}
return totalInfo;
}

uint64_t EPBucket::getTotalDiskSize() {
using namespace cb::stats;
return getAggregatedFileInfo().fileSize;
}

cb::engine_errc EPBucket::getFileStats(const BucketStatCollector& collector) {
auto totalInfo = getAggregatedFileInfo();

using namespace cb::stats;
collector.addStat(Key::ep_db_data_size, totalInfo.getEstimatedLiveData());
Expand Down
8 changes: 8 additions & 0 deletions engines/ep/src/ep_bucket.h
Expand Up @@ -165,6 +165,8 @@ class EPBucket : public KVBucket {
*/
bool updateCompactionTasks(Vbid vbid);

uint64_t getTotalDiskSize() override;

cb::engine_errc getFileStats(const BucketStatCollector& collector) override;

cb::engine_errc getPerVBucketDiskStats(const CookieIface* cookie,
Expand Down Expand Up @@ -449,6 +451,12 @@ class EPBucket : public KVBucket {
*/
void updateCompactionConcurrency();

/**
* Return disk usage information, summed across all shards.
* @return total file info
*/
DBFileInfo getAggregatedFileInfo();

/**
* Max number of backill items in a single flusher batch before we split
* into multiple batches. Actual batch size may be larger as we will not
Expand Down
11 changes: 11 additions & 0 deletions engines/ep/src/ep_engine.cc
Expand Up @@ -378,6 +378,8 @@ cb::engine_errc EventuallyPersistentEngine::get_prometheus_stats(
return doMetricGroupHigh(collector);
case MetricGroup::Low:
return doMetricGroupLow(collector);
case MetricGroup::Metering:
return doMetricGroupMetering(collector);
case MetricGroup::All:
// nothing currently requests All metrics at this level, so rather
// than leaving an unused impl to rot, defer until it is actually
Expand Down Expand Up @@ -432,6 +434,15 @@ cb::engine_errc EventuallyPersistentEngine::doMetricGroupLow(
return doConnAggStats(collector, ":");
}

cb::engine_errc EventuallyPersistentEngine::doMetricGroupMetering(
const BucketStatCollector& collector) {
using namespace cb::stats;
// equivalent to Key::ep_db_file_size but named to match metering
// requirements.
collector.addStat(Key::storage, kvBucket->getTotalDiskSize());
return cb::engine_errc::success;
}

cb::engine_errc EventuallyPersistentEngine::store(
const CookieIface& cookie,
ItemIface& itm,
Expand Down
10 changes: 10 additions & 0 deletions engines/ep/src/ep_engine.h
Expand Up @@ -1137,6 +1137,16 @@ class EventuallyPersistentEngine : public EngineIface, public DcpIface {
*/
cb::engine_errc doMetricGroupLow(const BucketStatCollector& collector);

/**
* Get metrics from this engine for the "Metering" group.
*
* These metrics will only be gathered if DeploymentModel::Serverless.
* These are likely to be scraped frequently, so should not be too
* numerous or expensive.
*/
cb::engine_errc doMetricGroupMetering(
const BucketStatCollector& collector);

void addLookupResult(const CookieIface* cookie,
std::unique_ptr<Item> result);

Expand Down
5 changes: 5 additions & 0 deletions engines/ep/src/ephemeral_bucket.h
Expand Up @@ -46,6 +46,11 @@ class EphemeralBucket : public KVBucket {
return cb::mcbp::Status::NotSupported;
}

uint64_t getTotalDiskSize() override {
// Ephemeral buckets do not use a notable amount of disk space.
return 0;
}

/// File stats not supported for Ephemeral buckets.
cb::engine_errc getFileStats(
const BucketStatCollector& collector) override {
Expand Down
7 changes: 7 additions & 0 deletions engines/ep/src/kv_bucket_iface.h
Expand Up @@ -379,6 +379,13 @@ class KVBucketIface {
const BucketStatCollector& collector,
cb::prometheus::MetricGroup metricGroup) = 0;

/**
* Get the total disk space used by this bucket.
*
* @return number of bytes of disk space used
*/
virtual uint64_t getTotalDiskSize() = 0;

/**
* Get file statistics
*
Expand Down
7 changes: 7 additions & 0 deletions engines/ep/src/kvstore/kvstore.cc
Expand Up @@ -778,3 +778,10 @@ std::ostream& operator<<(std::ostream& os,
return os << "INVALID GetCollectionStatsStatus value:"
<< static_cast<int>(status);
}

DBFileInfo& DBFileInfo::operator+=(const DBFileInfo& other) {
spaceUsed += other.spaceUsed;
fileSize += other.fileSize;
prepareBytes += other.prepareBytes;
return *this;
}
2 changes: 2 additions & 0 deletions engines/ep/src/kvstore/kvstore.h
Expand Up @@ -369,6 +369,8 @@ struct DBFileInfo {
}
return spaceUsed;
}

DBFileInfo& operator+=(const DBFileInfo& other);
};

struct vbucket_state;
Expand Down
2 changes: 1 addition & 1 deletion include/statistics/cardinality.h
Expand Up @@ -19,5 +19,5 @@ namespace cb::prometheus {
* * all: include metrics from both low and high cardinality groups (e.g.,
* for code shared with cbstats)
*/
enum class MetricGroup { Low, High, All };
enum class MetricGroup { Low, High, Metering, All };
} // namespace cb::prometheus
5 changes: 5 additions & 0 deletions statistics/stat_definitions.json
Expand Up @@ -2580,5 +2580,10 @@
"key": "throttle_count_total",
"unit": "none",
"description": "Total number of times requests have been throttled for a bucket since reset"
},
{
"key": "storage",
"unit": "bytes",
"description": "Total bytes on disk used by a bucket"
}
]

0 comments on commit f61b2e1

Please sign in to comment.