rustfs-ecstore 1.0.1

Erasure coding storage backend for RustFS, providing efficient data storage and retrieval with redundancy.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
// Copyright 2024 RustFS Team
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use crate::bucket::bandwidth::reader::BucketOptions;
use ratelimit::{Clock, Error as RatelimitError, Ratelimiter};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, RwLock};
use std::time::{Duration, Instant};
use tracing::warn;

/// BETA_BUCKET is the weight used to calculate exponential moving average
const BETA_BUCKET: f64 = 0.1;

// ratelimit 2.0 stores tokens at six decimal places. Above this limit its
// scaled capacity and token-cost calculations saturate instead of preserving
// the configured bandwidth.
const MAX_RATELIMIT_TOKENS: i64 = 18_446_744_073_709;

fn consume_tokens<C: Clock>(limiter: &Ratelimiter<C>, n: u64) -> (u64, f64, u64) {
    if n == 0 {
        return (0, limiter.rate() as f64, 0);
    }

    let mut consumed = 0u64;
    // Consuming one token also refills the bucket based on elapsed time, so
    // the subsequent `available()` read reflects freshly accrued tokens.
    if limiter.try_wait().is_ok() {
        consumed = 1;
    }
    let available = limiter.available();
    let to_consume = n - consumed;
    let batch = to_consume.min(available);
    if batch > 0 && limiter.try_wait_n(batch).is_ok() {
        consumed += batch;
    }
    let deficit = n.saturating_sub(consumed);
    let rate = limiter.rate() as f64;
    (deficit, rate, consumed)
}

#[derive(Clone)]
pub struct BucketThrottle {
    limiter: Arc<Mutex<Ratelimiter>>,
    pub node_bandwidth_per_sec: i64,
}

impl BucketThrottle {
    fn new(node_bandwidth_per_sec: i64) -> Result<Self, RatelimitError> {
        let node_bandwidth_per_sec = node_bandwidth_per_sec.max(1);
        let amount = node_bandwidth_per_sec as u64;
        // ratelimit 2.0's builder takes a per-second rate; the refill period
        // defaults to one second, so `amount` tokens accrue per second.
        let limiter_inner = Ratelimiter::builder(amount).max_tokens(amount).build()?;
        Ok(Self {
            limiter: Arc::new(Mutex::new(limiter_inner)),
            node_bandwidth_per_sec,
        })
    }

    pub fn burst(&self) -> u64 {
        self.limiter.lock().unwrap_or_else(|e| e.into_inner()).max_tokens()
    }

    /// Best-effort bulk token consumption: consume up to `n` tokens and report
    /// how many were taken plus any shortfall.
    ///
    /// `try_wait_n` on the ratelimit crate is all-or-nothing, so we cannot ask
    /// for `n` directly and still consume a partial amount. Instead we take one
    /// token first (which also triggers the internal time-based refill), read
    /// the now-current available count, and consume `min(remaining, available)`
    /// in a single `try_wait_n` call — that batch never exceeds `available`, so
    /// it always succeeds.
    pub(crate) fn consume(&self, n: u64) -> (u64, f64, u64) {
        let guard = self.limiter.lock().unwrap_or_else(|e| {
            warn!("bucket throttle mutex poisoned, recovering");
            e.into_inner()
        });
        consume_tokens(&guard, n)
    }
}

#[derive(Debug)]
pub struct BucketMeasurement {
    bytes_since_last_window: AtomicU64,
    start_time: Mutex<Option<Instant>>,
    exp_moving_avg: Mutex<f64>,
}

impl BucketMeasurement {
    pub fn new(init_time: Instant) -> Self {
        Self {
            bytes_since_last_window: AtomicU64::new(0),
            start_time: Mutex::new(Some(init_time)),
            exp_moving_avg: Mutex::new(0.0),
        }
    }

    pub fn increment_bytes(&self, bytes: u64) {
        self.bytes_since_last_window.fetch_add(bytes, Ordering::Relaxed);
    }

    pub fn update_exponential_moving_average(&self, end_time: Instant) {
        let mut start_time = self.start_time.lock().unwrap_or_else(|e| {
            warn!("bucket measurement start_time mutex poisoned, recovering");
            e.into_inner()
        });
        let previous_start = *start_time;
        *start_time = Some(end_time);

        let Some(prev_start) = previous_start else {
            return;
        };

        if prev_start > end_time {
            return;
        }

        let duration = end_time.duration_since(prev_start);
        if duration.is_zero() {
            return;
        }

        let bytes_since_last_window = self.bytes_since_last_window.swap(0, Ordering::Relaxed);
        let increment = bytes_since_last_window as f64 / duration.as_secs_f64();

        let mut exp_moving_avg = self.exp_moving_avg.lock().unwrap_or_else(|e| {
            warn!("bucket measurement exp_moving_avg mutex poisoned, recovering");
            e.into_inner()
        });

        if *exp_moving_avg == 0.0 {
            *exp_moving_avg = increment;
            return;
        }

        *exp_moving_avg = exponential_moving_average(BETA_BUCKET, *exp_moving_avg, increment);
    }

    pub fn get_exp_moving_avg_bytes_per_second(&self) -> f64 {
        *self.exp_moving_avg.lock().unwrap_or_else(|e| {
            warn!("bucket measurement exp_moving_avg mutex poisoned, recovering");
            e.into_inner()
        })
    }
}

fn exponential_moving_average(beta: f64, previous_avg: f64, increment_avg: f64) -> f64 {
    (1f64 - beta) * increment_avg + beta * previous_avg
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BandwidthDetails {
    pub limit_bytes_per_sec: i64,
    pub current_bandwidth_bytes_per_sec: f64,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BucketBandwidthReport {
    pub bucket_stats: HashMap<BucketOptions, BandwidthDetails>,
}

pub struct Monitor {
    t_lock: RwLock<HashMap<BucketOptions, BucketThrottle>>,
    m_lock: RwLock<HashMap<BucketOptions, BucketMeasurement>>,
    pub node_count: u64,
}

impl Monitor {
    pub fn new(num_nodes: u64) -> Arc<Self> {
        let node_cnt = num_nodes.max(1);
        let m = Arc::new(Monitor {
            t_lock: RwLock::new(HashMap::new()),
            m_lock: RwLock::new(HashMap::new()),
            node_count: node_cnt,
        });
        if let Ok(handle) = tokio::runtime::Handle::try_current() {
            let weak = Arc::downgrade(&m);
            handle.spawn(async move {
                let mut interval = tokio::time::interval(Duration::from_secs(2));
                loop {
                    interval.tick().await;
                    let Some(monitor) = weak.upgrade() else { break };
                    monitor.update_moving_avg();
                }
            });
        }
        m
    }

    pub fn init_measurement(&self, opts: &BucketOptions) {
        let mut guard = self.m_lock.write().unwrap_or_else(|e| {
            warn!("bucket monitor measurement rwlock write poisoned, recovering");
            e.into_inner()
        });
        guard
            .entry(opts.clone())
            .or_insert_with(|| BucketMeasurement::new(Instant::now()));
    }

    pub fn update_measurement(&self, opts: &BucketOptions, bytes: u64) {
        {
            let guard = self.m_lock.read().unwrap_or_else(|e| {
                warn!("bucket monitor measurement rwlock read poisoned, recovering");
                e.into_inner()
            });
            if let Some(measurement) = guard.get(opts) {
                measurement.increment_bytes(bytes);
                return;
            }
        }

        // Miss path: write lock + insert once, then increment.
        let mut guard = self.m_lock.write().unwrap_or_else(|e| {
            warn!("bucket monitor measurement rwlock write poisoned, recovering");
            e.into_inner()
        });

        // Double-check after lock upgrade in case another thread inserted it.
        let measurement = guard
            .entry(opts.clone())
            .or_insert_with(|| BucketMeasurement::new(Instant::now()));

        measurement.increment_bytes(bytes);
    }

    pub fn update_moving_avg(&self) {
        let now = Instant::now();
        let guard = self.m_lock.read().unwrap_or_else(|e| {
            warn!("bucket monitor measurement rwlock read poisoned, recovering");
            e.into_inner()
        });
        for measurement in guard.values() {
            measurement.update_exponential_moving_average(now);
        }
    }

    pub fn get_report(&self, select_bucket: impl Fn(&str) -> bool) -> BucketBandwidthReport {
        let t_guard = self.t_lock.read().unwrap_or_else(|e| {
            warn!("bucket monitor throttle rwlock read poisoned, recovering");
            e.into_inner()
        });
        let m_guard = self.m_lock.read().unwrap_or_else(|e| {
            warn!("bucket monitor measurement rwlock read poisoned, recovering");
            e.into_inner()
        });
        let mut bucket_stats = HashMap::new();
        for (opts, throttle) in t_guard.iter() {
            if !select_bucket(&opts.name) {
                continue;
            }
            let mut current_bandwidth_bytes_per_sec = 0.0;
            if let Some(measurement) = m_guard.get(opts) {
                current_bandwidth_bytes_per_sec = measurement.get_exp_moving_avg_bytes_per_second();
            }
            bucket_stats.insert(
                opts.clone(),
                BandwidthDetails {
                    limit_bytes_per_sec: throttle.node_bandwidth_per_sec * self.node_count as i64,
                    current_bandwidth_bytes_per_sec,
                },
            );
        }
        BucketBandwidthReport { bucket_stats }
    }

    pub fn delete_bucket(&self, bucket: &str) {
        self.t_lock
            .write()
            .unwrap_or_else(|e| {
                warn!("bucket monitor throttle rwlock write poisoned, recovering");
                e.into_inner()
            })
            .retain(|opts, _| opts.name != bucket);
        self.m_lock
            .write()
            .unwrap_or_else(|e| {
                warn!("bucket monitor measurement rwlock write poisoned, recovering");
                e.into_inner()
            })
            .retain(|opts, _| opts.name != bucket);
    }

    pub fn delete_bucket_throttle(&self, bucket: &str, arn: &str) {
        let opts = BucketOptions {
            name: bucket.to_string(),
            replication_arn: arn.to_string(),
        };
        self.t_lock
            .write()
            .unwrap_or_else(|e| {
                warn!("bucket monitor throttle rwlock write poisoned, recovering");
                e.into_inner()
            })
            .remove(&opts);
        self.m_lock
            .write()
            .unwrap_or_else(|e| {
                warn!("bucket monitor measurement rwlock write poisoned, recovering");
                e.into_inner()
            })
            .remove(&opts);
    }

    pub fn throttle(&self, opts: &BucketOptions) -> Option<BucketThrottle> {
        self.t_lock
            .read()
            .unwrap_or_else(|e| {
                warn!("bucket monitor throttle rwlock read poisoned, recovering");
                e.into_inner()
            })
            .get(opts)
            .cloned()
    }

    pub fn set_bandwidth_limit(&self, bucket: &str, arn: &str, limit: i64) {
        if limit <= 0 {
            warn!(
                bucket = bucket,
                arn = arn,
                limit = limit,
                "invalid bandwidth limit, must be positive; ignoring"
            );
            return;
        }
        let limit_bytes = limit / self.node_count as i64;
        if limit_bytes == 0 && limit > 0 {
            warn!(
                bucket = bucket,
                arn = arn,
                limit = limit,
                node_count = self.node_count,
                "bandwidth limit too small for cluster size, per-node limit will clamp to 1 byte/s"
            );
        }
        if limit_bytes > MAX_RATELIMIT_TOKENS {
            warn!(
                bucket = bucket,
                arn = arn,
                limit_bytes = limit_bytes,
                max_limit_bytes = MAX_RATELIMIT_TOKENS,
                "bandwidth limit exceeds ratelimiter capacity, throttling disabled for this target"
            );
            return;
        }
        let opts = BucketOptions {
            name: bucket.to_string(),
            replication_arn: arn.to_string(),
        };
        let throttle = match BucketThrottle::new(limit_bytes) {
            Ok(t) => t,
            Err(e) => {
                warn!(
                    bucket = bucket,
                    arn = arn,
                    limit_bytes = limit_bytes,
                    err = %e,
                    "failed to build bandwidth throttle, throttling disabled for this target"
                );
                return;
            }
        };
        self.t_lock
            .write()
            .unwrap_or_else(|e| {
                warn!("bucket monitor throttle rwlock write poisoned, recovering");
                e.into_inner()
            })
            .insert(opts, throttle);
    }

    pub fn is_throttled(&self, bucket: &str, arn: &str) -> bool {
        let opt = BucketOptions {
            name: bucket.to_string(),
            replication_arn: arn.to_string(),
        };
        self.t_lock
            .read()
            .unwrap_or_else(|e| {
                warn!("bucket monitor throttle rwlock read poisoned, recovering");
                e.into_inner()
            })
            .contains_key(&opt)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::panic::{AssertUnwindSafe, catch_unwind};

    #[derive(Clone)]
    struct TestClock {
        elapsed_ns: Arc<AtomicU64>,
    }

    impl TestClock {
        fn new() -> Self {
            Self {
                elapsed_ns: Arc::new(AtomicU64::new(0)),
            }
        }

        fn advance(&self, duration: Duration) {
            let elapsed_ns = u64::try_from(duration.as_nanos()).unwrap_or(u64::MAX);
            self.elapsed_ns.fetch_add(elapsed_ns, Ordering::Relaxed);
        }
    }

    impl Clock for TestClock {
        fn elapsed(&self) -> Duration {
            Duration::from_nanos(self.elapsed_ns.load(Ordering::Relaxed))
        }
    }

    #[test]
    fn test_set_and_get_throttle_with_node_split() {
        let monitor = Monitor::new(4);
        monitor.set_bandwidth_limit("b1", "arn1", 400);

        let throttle = monitor
            .throttle(&BucketOptions {
                name: "b1".to_string(),
                replication_arn: "arn1".to_string(),
            })
            .expect("throttle should exist");

        assert_eq!(throttle.node_bandwidth_per_sec, 100);
        assert!(monitor.is_throttled("b1", "arn1"));
    }

    #[test]
    fn test_delete_bucket_throttle() {
        let monitor = Monitor::new(2);
        monitor.set_bandwidth_limit("b1", "arn1", 200);
        assert!(monitor.is_throttled("b1", "arn1"));

        monitor.delete_bucket_throttle("b1", "arn1");
        assert!(!monitor.is_throttled("b1", "arn1"));
    }

    #[test]
    fn test_delete_bucket_removes_all_arns() {
        let monitor = Monitor::new(1);
        monitor.set_bandwidth_limit("b1", "arn1", 100);
        monitor.set_bandwidth_limit("b1", "arn2", 100);
        monitor.set_bandwidth_limit("b2", "arn3", 100);

        monitor.delete_bucket("b1");

        assert!(!monitor.is_throttled("b1", "arn1"));
        assert!(!monitor.is_throttled("b1", "arn2"));
        assert!(monitor.is_throttled("b2", "arn3"));
    }

    #[test]
    fn test_set_bandwidth_limit_ignores_non_positive() {
        let monitor = Monitor::new(2);

        monitor.set_bandwidth_limit("b1", "arn1", 0);
        assert!(!monitor.is_throttled("b1", "arn1"));

        monitor.set_bandwidth_limit("b1", "arn1", -10);
        assert!(!monitor.is_throttled("b1", "arn1"));
    }

    #[test]
    fn test_set_bandwidth_limit_rejects_unrepresentable_rate() {
        let monitor = Monitor::new(1);

        monitor.set_bandwidth_limit("b1", "arn1", MAX_RATELIMIT_TOKENS + 1);

        assert!(!monitor.is_throttled("b1", "arn1"));
    }

    #[test]
    fn test_consume_returns_deficit_when_tokens_exhausted() {
        let throttle = BucketThrottle::new(100).expect("test");

        let (deficit, rate, _consumed) = throttle.consume(200);

        assert!(deficit > 0);
        assert!(rate > 0.0);
    }

    // Tokio's paused clock does not advance ratelimit's StdClock. Keep natural
    // refill below one whole token for years and supply each exact budget here.
    fn supply_reader_test_tokens(throttle: &BucketThrottle, available: u64) {
        let rate = u64::try_from(throttle.node_bandwidth_per_sec).expect("test throttle rate should be positive");
        let burst = throttle.burst();
        *throttle.limiter.lock().expect("test throttle should not poison") = Ratelimiter::builder(rate)
            .max_tokens(burst)
            .initial_available(available)
            .period(Duration::from_nanos(u64::MAX))
            .build()
            .expect("controlled token budget should be valid");
    }

    #[tokio::test(start_paused = true)]
    async fn test_monitored_reader_retains_partial_tokens_across_waits() {
        use crate::bucket::bandwidth::reader::{MonitorReaderOptions, MonitoredReader};
        use futures_util::task::noop_waker_ref;
        use std::pin::Pin;
        use std::task::{Context, Poll};
        use tokio::io::{AsyncRead, ReadBuf};

        for header_size in [0, 25] {
            let monitor = Monitor::new(1);
            let opts = BucketOptions {
                name: "reader-budget".to_string(),
                replication_arn: "reader-budget-arn".to_string(),
            };
            monitor.set_bandwidth_limit(&opts.name, &opts.replication_arn, 100);
            let throttle = monitor.throttle(&opts).expect("test throttle should exist");
            supply_reader_test_tokens(&throttle, 25);
            let data = [0xAB; 100];
            let mut reader = MonitoredReader::new(
                monitor,
                data.as_slice(),
                MonitorReaderOptions {
                    bucket_options: opts,
                    header_size,
                },
            );
            let mut output = [0; 100];
            let mut buf = ReadBuf::new(&mut output);
            let mut cx = Context::from_waker(noop_waker_ref());

            assert!(Pin::new(&mut reader).poll_read(&mut cx, &mut buf).is_pending());
            assert!(buf.filled().is_empty(), "partial payment must not authorize payload reads");
            assert_eq!(throttle.limiter.lock().expect("test throttle").available(), 0);

            supply_reader_test_tokens(&throttle, 75);
            tokio::time::advance(Duration::from_secs(1)).await;
            assert!(
                matches!(Pin::new(&mut reader).poll_read(&mut cx, &mut buf), Poll::Ready(Ok(()))),
                "25 paid tokens plus 75 later tokens must fund the original read with header_size={header_size}"
            );
            assert_eq!(buf.filled(), &data[..100 - header_size]);
            assert_eq!(throttle.limiter.lock().expect("test throttle").available(), 0);
        }
    }

    #[tokio::test(start_paused = true)]
    async fn test_monitored_reader_preserves_paid_read_across_cancellation() {
        use crate::bucket::bandwidth::reader::{MonitorReaderOptions, MonitoredReader};
        use futures_util::task::noop_waker_ref;
        use std::future::Future;
        use std::pin::Pin;
        use std::task::{Context, Poll};
        use tokio::io::{AsyncRead, AsyncReadExt, AsyncWriteExt, ReadBuf};

        let monitor = Monitor::new(1);
        let opts = BucketOptions {
            name: "reader-pending".to_string(),
            replication_arn: "reader-pending-arn".to_string(),
        };
        monitor.set_bandwidth_limit(&opts.name, &opts.replication_arn, 100);
        let throttle = monitor.throttle(&opts).expect("test throttle should exist");
        supply_reader_test_tokens(&throttle, 100);
        let (mut writer, inner) = tokio::io::duplex(100);
        let mut reader = MonitoredReader::new(
            monitor,
            inner,
            MonitorReaderOptions {
                bucket_options: opts,
                header_size: 0,
            },
        );
        let mut cx = Context::from_waker(noop_waker_ref());
        let mut first_output = [0; 100];
        let mut first_read = Box::pin(reader.read(&mut first_output));
        assert!(first_read.as_mut().poll(&mut cx).is_pending());
        assert_eq!(throttle.limiter.lock().expect("test throttle").available(), 0);
        drop(first_read);
        writer
            .write_all(&[0xAB; 100])
            .await
            .expect("duplex writer should supply the pending read");

        // A canceled read future keeps its reader alive. Resume in a smaller
        // buffer without charging the already-paid allowance a second time.
        let mut short_output = [0; 40];
        let mut short_buf = ReadBuf::new(&mut short_output);
        assert!(matches!(Pin::new(&mut reader).poll_read(&mut cx, &mut short_buf), Poll::Ready(Ok(()))));
        assert_eq!(short_buf.filled(), &[0xAB; 40]);
        assert_eq!(throttle.limiter.lock().expect("test throttle").available(), 0);

        // Completing the short read discards unused allowance, matching the
        // existing precharge policy. The next read must buy a fresh budget.
        let mut remaining_output = [0; 100];
        let mut remaining_buf = ReadBuf::new(&mut remaining_output);
        assert!(Pin::new(&mut reader).poll_read(&mut cx, &mut remaining_buf).is_pending());
        supply_reader_test_tokens(&throttle, 100);
        tokio::time::advance(Duration::from_secs(1)).await;
        assert!(matches!(
            Pin::new(&mut reader).poll_read(&mut cx, &mut remaining_buf),
            Poll::Ready(Ok(()))
        ));
        assert_eq!(remaining_buf.filled(), &[0xAB; 60]);
        assert_eq!(throttle.limiter.lock().expect("test throttle").available(), 0);

        drop(writer);
        let mut eof_output = [0; 100];
        let mut eof_buf = ReadBuf::new(&mut eof_output);
        assert!(Pin::new(&mut reader).poll_read(&mut cx, &mut eof_buf).is_pending());
        supply_reader_test_tokens(&throttle, 100);
        tokio::time::advance(Duration::from_secs(1)).await;
        assert!(matches!(Pin::new(&mut reader).poll_read(&mut cx, &mut eof_buf), Poll::Ready(Ok(()))));
        assert!(eof_buf.filled().is_empty());
        assert_eq!(throttle.limiter.lock().expect("test throttle").available(), 0);
        assert!(Pin::new(&mut reader).poll_read(&mut cx, &mut eof_buf).is_pending());
    }

    #[tokio::test(start_paused = true)]
    async fn test_monitored_reader_shrinks_unpaid_payload_after_bandwidth_reduction() {
        use crate::bucket::bandwidth::reader::{MonitorReaderOptions, MonitoredReader};
        use futures_util::task::noop_waker_ref;
        use std::pin::Pin;
        use std::task::{Context, Poll};
        use tokio::io::{AsyncRead, ReadBuf};

        for header_size in [0, 25] {
            let monitor = Monitor::new(1);
            let opts = BucketOptions {
                name: "reader-reduced-budget".to_string(),
                replication_arn: "reader-reduced-budget-arn".to_string(),
            };
            monitor.set_bandwidth_limit(&opts.name, &opts.replication_arn, 100);
            let initial_throttle = monitor.throttle(&opts).expect("initial throttle should exist");
            supply_reader_test_tokens(&initial_throttle, 25);
            let data = [0xAB; 100];
            let mut reader = MonitoredReader::new(
                monitor.clone(),
                data.as_slice(),
                MonitorReaderOptions {
                    bucket_options: opts.clone(),
                    header_size,
                },
            );
            let mut output = [0; 100];
            let mut buf = ReadBuf::new(&mut output);
            let mut cx = Context::from_waker(noop_waker_ref());
            assert!(Pin::new(&mut reader).poll_read(&mut cx, &mut buf).is_pending());
            assert!(buf.filled().is_empty());
            assert_eq!(initial_throttle.limiter.lock().expect("initial throttle").available(), 0);

            monitor.set_bandwidth_limit(&opts.name, &opts.replication_arn, 10);
            let reduced_throttle = monitor.throttle(&opts).expect("reduced throttle should exist");
            assert_eq!(reduced_throttle.burst(), 10);
            supply_reader_test_tokens(&reduced_throttle, 0);
            tokio::time::advance(Duration::from_secs(1)).await;
            let result = Pin::new(&mut reader).poll_read(&mut cx, &mut buf);
            let expected_payload = if header_size == 0 {
                assert!(
                    matches!(result, Poll::Ready(Ok(()))),
                    "already-paid payload must not wait for the old burst's unpaid remainder"
                );
                25
            } else {
                assert!(result.is_pending(), "the initial 25 tokens paid only the header");
                assert!(buf.filled().is_empty(), "payload still requires its own reduced budget");
                supply_reader_test_tokens(&reduced_throttle, 10);
                tokio::time::advance(Duration::from_secs(1)).await;
                assert!(matches!(Pin::new(&mut reader).poll_read(&mut cx, &mut buf), Poll::Ready(Ok(()))));
                10
            };
            assert_eq!(buf.filled(), &data[..expected_payload]);
            assert_eq!(reduced_throttle.limiter.lock().expect("reduced throttle").available(), 0);
        }
    }

    #[test]
    fn test_consume_refills_continuously() {
        let clock = TestClock::new();
        let limiter = Ratelimiter::with_clock(100, clock.clone());

        assert_eq!(consume_tokens(&limiter, 100), (100, 100.0, 0));

        clock.advance(Duration::from_millis(250));
        assert_eq!(consume_tokens(&limiter, 100), (75, 100.0, 25));

        clock.advance(Duration::from_millis(250));
        assert_eq!(consume_tokens(&limiter, 100), (75, 100.0, 25));

        clock.advance(Duration::from_millis(500));
        assert_eq!(consume_tokens(&limiter, 100), (50, 100.0, 50));
    }

    #[test]
    fn test_consume_no_deficit_when_tokens_sufficient() {
        let throttle = BucketThrottle::new(10000).expect("test");

        std::thread::sleep(std::time::Duration::from_millis(1100));

        let (deficit, _rate, _consumed) = throttle.consume(5000);
        assert_eq!(deficit, 0);
    }

    #[test]
    fn test_burst_equals_bandwidth() {
        let throttle = BucketThrottle::new(500).expect("test");
        assert_eq!(throttle.burst(), 500);
    }

    #[test]
    fn test_concurrent_consume() {
        let throttle = BucketThrottle::new(10000).expect("test");
        std::thread::sleep(std::time::Duration::from_millis(1100));

        let mut handles = vec![];
        for _ in 0..10 {
            let t = throttle.clone();
            handles.push(std::thread::spawn(move || t.consume(100)));
        }

        let mut total_deficit = 0u64;
        let mut total_consumed = 0u64;
        for h in handles {
            let (deficit, _rate, consumed) = h.join().unwrap();
            total_consumed += consumed;
            total_deficit += deficit;
        }

        assert_eq!(total_consumed + total_deficit, 1000);
        assert!(total_consumed <= 10000);
    }

    #[test]
    fn test_zero_bandwidth_clamped_to_one() {
        let throttle = BucketThrottle::new(0).expect("test");
        assert_eq!(throttle.burst(), 1);
        assert_eq!(throttle.node_bandwidth_per_sec, 1);
    }

    #[test]
    fn test_negative_bandwidth_clamped_to_one() {
        let throttle = BucketThrottle::new(-100).expect("test");
        assert_eq!(throttle.burst(), 1);
        assert_eq!(throttle.node_bandwidth_per_sec, 1);
    }

    #[test]
    fn test_update_bandwidth_limit_overrides_previous() {
        let monitor = Monitor::new(1);
        monitor.set_bandwidth_limit("b1", "arn1", 1000);

        let opts = BucketOptions {
            name: "b1".to_string(),
            replication_arn: "arn1".to_string(),
        };
        let t1 = monitor.throttle(&opts).expect("throttle should exist");
        assert_eq!(t1.burst(), 1000);
        assert_eq!(t1.node_bandwidth_per_sec, 1000);

        monitor.set_bandwidth_limit("b1", "arn1", 500);

        let t2 = monitor.throttle(&opts).expect("throttle should exist after update");
        assert_eq!(t2.burst(), 500);
        assert_eq!(t2.node_bandwidth_per_sec, 500);
    }

    #[test]
    fn test_bucket_measurement_recovers_from_poisoned_mutexes() {
        let measurement = BucketMeasurement::new(Instant::now());

        let _ = catch_unwind(AssertUnwindSafe(|| {
            let _guard = measurement.start_time.lock().unwrap();
            panic!("poison start_time mutex");
        }));
        measurement.increment_bytes(64);
        measurement.update_exponential_moving_average(Instant::now() + Duration::from_secs(1));

        let _ = catch_unwind(AssertUnwindSafe(|| {
            let _guard = measurement.exp_moving_avg.lock().unwrap();
            panic!("poison exp_moving_avg mutex");
        }));
        measurement.increment_bytes(32);
        measurement.update_exponential_moving_average(Instant::now() + Duration::from_secs(2));

        let value = measurement.get_exp_moving_avg_bytes_per_second();
        assert!(value.is_finite());
        assert!(value >= 0.0);
    }

    #[test]
    fn test_get_report_limit_and_current_bandwidth_after_measurement() {
        let monitor = Monitor::new(4);
        monitor.set_bandwidth_limit("b1", "arn1", 400);
        let opts = BucketOptions {
            name: "b1".to_string(),
            replication_arn: "arn1".to_string(),
        };
        monitor.init_measurement(&opts);
        monitor.update_measurement(&opts, 500);
        monitor.update_measurement(&opts, 500);
        std::thread::sleep(Duration::from_millis(110));
        monitor.update_moving_avg();

        let report = monitor.get_report(|name| name == "b1");
        let details = report.bucket_stats.get(&opts).expect("report should contain b1/arn1");
        assert_eq!(details.limit_bytes_per_sec, 400);
        assert!(
            details.current_bandwidth_bytes_per_sec > 0.0,
            "current_bandwidth should be positive after update_measurement and update_moving_avg"
        );
        assert!(
            details.current_bandwidth_bytes_per_sec < 20000.0,
            "current_bandwidth should be in reasonable range"
        );
    }

    #[test]
    fn test_get_report_select_bucket_filters() {
        let monitor = Monitor::new(2);
        monitor.set_bandwidth_limit("b1", "arn1", 100);
        monitor.set_bandwidth_limit("b2", "arn2", 200);
        let opts_b1 = BucketOptions {
            name: "b1".to_string(),
            replication_arn: "arn1".to_string(),
        };
        let opts_b2 = BucketOptions {
            name: "b2".to_string(),
            replication_arn: "arn2".to_string(),
        };
        monitor.init_measurement(&opts_b1);
        monitor.init_measurement(&opts_b2);

        let report_all = monitor.get_report(|_| true);
        assert_eq!(report_all.bucket_stats.len(), 2);

        let report_b1 = monitor.get_report(|name| name == "b1");
        assert_eq!(report_b1.bucket_stats.len(), 1);
        assert_eq!(report_b1.bucket_stats.get(&opts_b1).unwrap().limit_bytes_per_sec, 100);

        let report_b2 = monitor.get_report(|name| name == "b2");
        assert_eq!(report_b2.bucket_stats.len(), 1);
        assert_eq!(report_b2.bucket_stats.get(&opts_b2).unwrap().limit_bytes_per_sec, 200);
    }
}