@@ -3,9 +3,6 @@ use std::collections::BTreeSet;
3
3
use std:: convert:: Infallible ;
4
4
use std:: error:: Error ;
5
5
use std:: mem;
6
- use std:: sync:: atomic:: AtomicI64 ;
7
- use std:: sync:: atomic:: Ordering as AtomicOrdering ;
8
- use std:: sync:: Arc ;
9
6
use std:: time:: Duration ;
10
7
11
8
use chrono:: { DateTime , Utc } ;
@@ -165,6 +162,24 @@ impl PolymorphicEnvelopeBuffer {
165
162
}
166
163
}
167
164
165
+ /// Returns the total number of envelopes that have been spooled since the startup. It does
166
+ /// not include the count that existed in a persistent spooler before.
167
+ pub fn item_count ( & self ) -> u64 {
168
+ match self {
169
+ Self :: Sqlite ( buffer) => buffer. tracked_count ,
170
+ Self :: InMemory ( buffer) => buffer. tracked_count ,
171
+ }
172
+ }
173
+
174
+ /// Returns the total number of bytes that the spooler storage uses or `None` if the number
175
+ /// cannot be reliably determined.
176
+ pub fn total_size ( & self ) -> Option < u64 > {
177
+ match self {
178
+ Self :: Sqlite ( buffer) => buffer. stack_provider . total_size ( ) ,
179
+ Self :: InMemory ( buffer) => buffer. stack_provider . total_size ( ) ,
180
+ }
181
+ }
182
+
168
183
/// Shuts down the [`PolymorphicEnvelopeBuffer`].
169
184
pub async fn shutdown ( & mut self ) -> bool {
170
185
// Currently, we want to flush the buffer only for disk, since the in memory implementation
@@ -228,7 +243,13 @@ struct EnvelopeBuffer<P: StackProvider> {
228
243
/// count might not succeed if it takes more than a set timeout. For example, if we load the
229
244
/// count of all envelopes from disk, and it takes more than the time we set, we will mark the
230
245
/// initial count as 0 and just count incoming and outgoing envelopes from the buffer.
231
- total_count : Arc < AtomicI64 > ,
246
+ total_count : i64 ,
247
+ /// The total count of envelopes that the buffer is working with ignoring envelopes that
248
+ /// were previously stored on disk.
249
+ ///
250
+ /// On startup this will always be 0 and will only count incoming envelopes. If a reliable
251
+ /// count of currently buffered envelopes is required, prefer this over `total_count`
252
+ tracked_count : u64 ,
232
253
/// Whether the count initialization succeeded or not.
233
254
///
234
255
/// This boolean is just used for tagging the metric that tracks the total count of envelopes
@@ -245,7 +266,8 @@ impl EnvelopeBuffer<MemoryStackProvider> {
245
266
stacks_by_project : Default :: default ( ) ,
246
267
priority_queue : Default :: default ( ) ,
247
268
stack_provider : MemoryStackProvider :: new ( memory_checker) ,
248
- total_count : Arc :: new ( AtomicI64 :: new ( 0 ) ) ,
269
+ total_count : 0 ,
270
+ tracked_count : 0 ,
249
271
total_count_initialized : false ,
250
272
partition_tag : partition_id. to_string ( ) ,
251
273
}
@@ -260,7 +282,8 @@ impl EnvelopeBuffer<SqliteStackProvider> {
260
282
stacks_by_project : Default :: default ( ) ,
261
283
priority_queue : Default :: default ( ) ,
262
284
stack_provider : SqliteStackProvider :: new ( partition_id, config) . await ?,
263
- total_count : Arc :: new ( AtomicI64 :: new ( 0 ) ) ,
285
+ total_count : 0 ,
286
+ tracked_count : 0 ,
264
287
total_count_initialized : false ,
265
288
partition_tag : partition_id. to_string ( ) ,
266
289
} )
@@ -318,7 +341,8 @@ where
318
341
prio. received_at = received_at;
319
342
} ) ;
320
343
321
- self . total_count . fetch_add ( 1 , AtomicOrdering :: SeqCst ) ;
344
+ self . total_count += 1 ;
345
+ self . tracked_count += 1 ;
322
346
self . track_total_count ( ) ;
323
347
324
348
Ok ( ( ) )
@@ -385,7 +409,8 @@ where
385
409
// We are fine with the count going negative, since it represents that more data was popped,
386
410
// than it was initially counted, meaning that we had a wrong total count from
387
411
// initialization.
388
- self . total_count . fetch_sub ( 1 , AtomicOrdering :: SeqCst ) ;
412
+ self . total_count -= 1 ;
413
+ self . tracked_count = self . tracked_count . saturating_sub ( 1 ) ;
389
414
self . track_total_count ( ) ;
390
415
391
416
Ok ( Some ( envelope) )
@@ -529,8 +554,7 @@ where
529
554
. await ;
530
555
match total_count {
531
556
Ok ( total_count) => {
532
- self . total_count
533
- . store ( total_count as i64 , AtomicOrdering :: SeqCst ) ;
557
+ self . total_count = total_count as i64 ;
534
558
self . total_count_initialized = true ;
535
559
}
536
560
Err ( error) => {
@@ -546,7 +570,7 @@ where
546
570
547
571
/// Emits a metric to track the total count of envelopes that are in the envelope buffer.
548
572
fn track_total_count ( & self ) {
549
- let total_count = self . total_count . load ( AtomicOrdering :: SeqCst ) as f64 ;
573
+ let total_count = self . total_count as f64 ;
550
574
let initialized = match self . total_count_initialized {
551
575
true => "true" ,
552
576
false => "false" ,
0 commit comments