From 6158fc821773ab8706d9e79ff7ef9a69683d3e4c Mon Sep 17 00:00:00 2001 From: Joao Eduardo Luis Date: Sat, 3 Jun 2023 10:25:19 +0000 Subject: [PATCH] rgw/sfs: update bucket stats Signed-off-by: Joao Eduardo Luis --- src/rgw/driver/sfs/bucket.cc | 27 +++++++++++++++ src/rgw/driver/sfs/sqlite/sqlite_buckets.cc | 38 +++++++++++++++++++++ src/rgw/driver/sfs/sqlite/sqlite_buckets.h | 8 +++++ 3 files changed, 73 insertions(+) diff --git a/src/rgw/driver/sfs/bucket.cc b/src/rgw/driver/sfs/bucket.cc index 28e1e43ef2a63..8ded87d6ae5cc 100644 --- a/src/rgw/driver/sfs/bucket.cc +++ b/src/rgw/driver/sfs/bucket.cc @@ -419,9 +419,36 @@ int SFSBucket::sync_user_stats( ) { return 0; } + int SFSBucket::update_container_stats(const DoutPrefixProvider* dpp) { + lsfs_dout(dpp, 10) << fmt::format( + "update bucket {} (id {}) stats", get_name(), + get_bucket_id() + ) + << dendl; + sfs::sqlite::SQLiteBuckets bucketdb(store->db_conn); + auto stats = bucketdb.get_stats(get_bucket_id()); + + if (!stats.has_value()) { + lsfs_dout(dpp, 10) << fmt::format( + "unable to obtain stats for bucket {} (id {}) -- " + "no such bucket!", + get_name(), get_bucket_id() + ) + << dendl; + return -ERR_NO_SUCH_BUCKET; + } + + lsfs_dout(dpp, 10) << fmt::format( + "bucket {} stats: size: {}, obj_cnt: {}", + get_name(), stats->size, stats->obj_count + ) + << dendl; + ent.size = ent.size_rounded = stats->size; + ent.count = stats->obj_count; return 0; } + int SFSBucket::check_bucket_shards(const DoutPrefixProvider* dpp) { ldpp_dout(dpp, 10) << __func__ << ": TODO" << dendl; return -ENOTSUP; diff --git a/src/rgw/driver/sfs/sqlite/sqlite_buckets.cc b/src/rgw/driver/sfs/sqlite/sqlite_buckets.cc index 5d93cd1e7c020..19d1eaf9a50db 100644 --- a/src/rgw/driver/sfs/sqlite/sqlite_buckets.cc +++ b/src/rgw/driver/sfs/sqlite/sqlite_buckets.cc @@ -97,4 +97,42 @@ std::vector SQLiteBuckets::get_deleted_buckets_ids() const { ); } +const std::optional SQLiteBuckets::get_stats( + const std::string& bucket_id +) const { + auto storage = conn->get_storage(); + std::optional stats; + storage.transaction([&]() mutable { + // ensure bucket exists + auto tmp = storage.get_all( + where(is_equal(&DBBucket::bucket_id, bucket_id)) + ); + if (tmp.size() == 0) { + return false; + } + ceph_assert(tmp.size() == 1); + + stats = SQLiteBucketStats(); + stats->obj_count = storage.count( + where(is_equal(&DBOPObjectInfo::bucket_id, bucket_id)) + ); + + auto res = storage.sum( + &DBVersionedObject::size, + left_join( + on(is_equal(&DBOPObjectInfo::uuid, &DBVersionedObject::object_id)) + ), + where(is_equal(&DBOPObjectInfo::bucket_id, bucket_id)) + ); + + if (res) { + stats->size = *res; + } + + return true; + }); + + return stats; +} + } // namespace rgw::sal::sfs::sqlite diff --git a/src/rgw/driver/sfs/sqlite/sqlite_buckets.h b/src/rgw/driver/sfs/sqlite/sqlite_buckets.h index 0f84638cd8d94..9799cde2e68e0 100644 --- a/src/rgw/driver/sfs/sqlite/sqlite_buckets.h +++ b/src/rgw/driver/sfs/sqlite/sqlite_buckets.h @@ -18,6 +18,11 @@ namespace rgw::sal::sfs::sqlite { +struct SQLiteBucketStats { + size_t size; + uint64_t obj_count; +}; + class SQLiteBuckets { DBConnRef conn; @@ -42,6 +47,9 @@ class SQLiteBuckets { std::vector get_buckets(const std::string& user_id) const; std::vector get_deleted_buckets_ids() const; + + const std::optional get_stats(const std::string& bucket_id + ) const; }; } // namespace rgw::sal::sfs::sqlite