|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This software may be used and distributed according to the terms of the |
| 5 | + * GNU General Public License version 2. |
| 6 | + */ |
| 7 | + |
| 8 | +//! A blobstore wrapper that counts operations via shared atomic counters. |
| 9 | +//! |
| 10 | +//! Unlike `CountedBlobstore` (ODS timeseries) or `LogBlob` (CoreContext perf |
| 11 | +//! counters), this wrapper exposes operation counts programmatically through |
| 12 | +//! `BlobstoreCounters`, making it suitable for tests and benchmarks that need |
| 13 | +//! to assert on the number of blobstore operations performed. |
| 14 | +
|
| 15 | +use std::sync::Arc; |
| 16 | +use std::sync::atomic::AtomicU64; |
| 17 | +use std::sync::atomic::Ordering; |
| 18 | + |
| 19 | +use anyhow::Result; |
| 20 | +use async_trait::async_trait; |
| 21 | +use blobstore::Blobstore; |
| 22 | +use blobstore::BlobstoreBytes; |
| 23 | +use blobstore::BlobstoreGetData; |
| 24 | +use blobstore::BlobstoreIsPresent; |
| 25 | +use blobstore::OverwriteStatus; |
| 26 | +use blobstore::PutBehaviour; |
| 27 | +use context::CoreContext; |
| 28 | + |
| 29 | +/// Shared atomic counters for blobstore operations. |
| 30 | +#[derive(Debug, Default)] |
| 31 | +pub struct BlobstoreCounters { |
| 32 | + pub gets: AtomicU64, |
| 33 | + pub puts: AtomicU64, |
| 34 | + pub is_presents: AtomicU64, |
| 35 | +} |
| 36 | + |
| 37 | +/// Snapshot of blobstore operation counts at a point in time. |
| 38 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 39 | +pub struct BlobstoreCountersSnapshot { |
| 40 | + pub gets: u64, |
| 41 | + pub puts: u64, |
| 42 | + pub is_presents: u64, |
| 43 | +} |
| 44 | + |
| 45 | +impl std::ops::Sub for BlobstoreCountersSnapshot { |
| 46 | + type Output = BlobstoreCountersSnapshot; |
| 47 | + |
| 48 | + fn sub(self, rhs: BlobstoreCountersSnapshot) -> BlobstoreCountersSnapshot { |
| 49 | + BlobstoreCountersSnapshot { |
| 50 | + gets: self.gets - rhs.gets, |
| 51 | + puts: self.puts - rhs.puts, |
| 52 | + is_presents: self.is_presents - rhs.is_presents, |
| 53 | + } |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +impl BlobstoreCounters { |
| 58 | + pub fn new() -> Self { |
| 59 | + Self::default() |
| 60 | + } |
| 61 | + |
| 62 | + pub fn reset(&self) { |
| 63 | + self.gets.store(0, Ordering::SeqCst); |
| 64 | + self.puts.store(0, Ordering::SeqCst); |
| 65 | + self.is_presents.store(0, Ordering::SeqCst); |
| 66 | + } |
| 67 | + |
| 68 | + pub fn snapshot(&self) -> BlobstoreCountersSnapshot { |
| 69 | + BlobstoreCountersSnapshot { |
| 70 | + gets: self.gets.load(Ordering::SeqCst), |
| 71 | + puts: self.puts.load(Ordering::SeqCst), |
| 72 | + is_presents: self.is_presents.load(Ordering::SeqCst), |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +/// A blobstore wrapper that counts operations via shared atomic counters. |
| 78 | +#[derive(Debug)] |
| 79 | +pub struct CountingBlobstore<B> { |
| 80 | + inner: B, |
| 81 | + counters: Arc<BlobstoreCounters>, |
| 82 | +} |
| 83 | + |
| 84 | +impl<B: std::fmt::Display> std::fmt::Display for CountingBlobstore<B> { |
| 85 | + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { |
| 86 | + write!(f, "CountingBlobstore<{}>", &self.inner) |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +impl<B> CountingBlobstore<B> { |
| 91 | + pub fn new(inner: B, counters: Arc<BlobstoreCounters>) -> Self { |
| 92 | + Self { inner, counters } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +#[async_trait] |
| 97 | +impl<B: Blobstore> Blobstore for CountingBlobstore<B> { |
| 98 | + async fn get<'a>( |
| 99 | + &'a self, |
| 100 | + ctx: &'a CoreContext, |
| 101 | + key: &'a str, |
| 102 | + ) -> Result<Option<BlobstoreGetData>> { |
| 103 | + self.counters.gets.fetch_add(1, Ordering::SeqCst); |
| 104 | + self.inner.get(ctx, key).await |
| 105 | + } |
| 106 | + |
| 107 | + async fn is_present<'a>( |
| 108 | + &'a self, |
| 109 | + ctx: &'a CoreContext, |
| 110 | + key: &'a str, |
| 111 | + ) -> Result<BlobstoreIsPresent> { |
| 112 | + self.counters.is_presents.fetch_add(1, Ordering::SeqCst); |
| 113 | + self.inner.is_present(ctx, key).await |
| 114 | + } |
| 115 | + |
| 116 | + async fn unlink<'a>(&'a self, ctx: &'a CoreContext, key: &'a str) -> Result<()> { |
| 117 | + self.inner.unlink(ctx, key).await |
| 118 | + } |
| 119 | + |
| 120 | + async fn put_explicit<'a>( |
| 121 | + &'a self, |
| 122 | + ctx: &'a CoreContext, |
| 123 | + key: String, |
| 124 | + value: BlobstoreBytes, |
| 125 | + put_behaviour: PutBehaviour, |
| 126 | + ) -> Result<OverwriteStatus> { |
| 127 | + self.counters.puts.fetch_add(1, Ordering::SeqCst); |
| 128 | + self.inner |
| 129 | + .put_explicit(ctx, key, value, put_behaviour) |
| 130 | + .await |
| 131 | + } |
| 132 | + |
| 133 | + async fn put_with_status<'a>( |
| 134 | + &'a self, |
| 135 | + ctx: &'a CoreContext, |
| 136 | + key: String, |
| 137 | + value: BlobstoreBytes, |
| 138 | + ) -> Result<OverwriteStatus> { |
| 139 | + self.counters.puts.fetch_add(1, Ordering::SeqCst); |
| 140 | + self.inner.put_with_status(ctx, key, value).await |
| 141 | + } |
| 142 | +} |
0 commit comments