sqry-core 6.0.23

Core library for sqry - semantic code search engine
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
//! `AdmissionController`: Reservation-based admission control for delta buffers.
//!
//! This module implements `AdmissionController`, which provides back-pressure
//! management for the delta buffer using atomic CAS operations.
//!
//! # Design
//!
//! - **Reservation-based**: Writers must acquire a reservation before writing
//! - **Dual CAS loops**: Atomic reservation of both bytes and ops
//! - **Compensating rollback**: CAS-based rollback prevents counter corruption
//!
//! # Thread Safety
//!
//! All operations use atomic CAS loops for lock-free concurrent access.
//! The compensating rollback ensures counter integrity even under contention.
//!
//! # Usage
//!
//! ```rust,ignore
//! use sqry_core::graph::unified::admission::{AdmissionController, SharedBufferState};
//!
//! let state = Arc::new(SharedBufferState::new());
//! let controller = AdmissionController::new(
//!     Arc::clone(&state),
//!     1024 * 1024, // 1MB max bytes
//!     10_000,      // 10k max ops
//! );
//!
//! let guard = controller.try_reserve(100, 1)?;
//! // ... write edges ...
//! guard.commit();
//! ```

use std::fmt;
use std::sync::Arc;
use std::sync::atomic::Ordering;

fn usize_to_f64(value: usize) -> f64 {
    #[allow(clippy::cast_precision_loss)]
    {
        value as f64
    }
}

use super::reservation::{AdmissionError, Reservation, ReservationGuard};
use super::state::SharedBufferState;

/// Admission controller for back-pressure management.
///
/// Controls access to the delta buffer by requiring reservations before writes.
/// Uses atomic CAS operations for lock-free concurrent access.
///
/// # Limits
///
/// The controller enforces two limits:
/// - `max_bytes`: Maximum total bytes (committed + reserved)
/// - `max_ops`: Maximum total operations (committed + reserved)
///
/// # Reservation Flow
///
/// 1. Writer calls `try_reserve(bytes, ops)`
/// 2. Controller atomically reserves both counters via dual CAS loops
/// 3. On success, returns `ReservationGuard` for RAII cleanup
/// 4. Writer performs delta buffer operations
/// 5. Guard is committed (transfers to committed) or aborted (releases)
#[derive(Debug)]
pub struct AdmissionController {
    /// Shared state with atomic counters.
    buffer_state: Arc<SharedBufferState>,
    /// Maximum bytes (committed + reserved).
    max_bytes: usize,
    /// Maximum operations (committed + reserved).
    max_ops: usize,
}

impl AdmissionController {
    /// Creates a new admission controller with the given limits.
    ///
    /// # Arguments
    ///
    /// * `buffer_state` - Shared state with atomic counters
    /// * `max_bytes` - Maximum total bytes (committed + reserved)
    /// * `max_ops` - Maximum total operations (committed + reserved)
    #[must_use]
    pub fn new(buffer_state: Arc<SharedBufferState>, max_bytes: usize, max_ops: usize) -> Self {
        Self {
            buffer_state,
            max_bytes,
            max_ops,
        }
    }

    /// Returns the shared buffer state.
    #[must_use]
    pub fn buffer_state(&self) -> &Arc<SharedBufferState> {
        &self.buffer_state
    }

    /// Returns the maximum bytes limit.
    #[must_use]
    pub fn max_bytes(&self) -> usize {
        self.max_bytes
    }

    /// Returns the maximum operations limit.
    #[must_use]
    pub fn max_ops(&self) -> usize {
        self.max_ops
    }

    /// Attempts to reserve capacity for a write operation.
    ///
    /// This method atomically reserves both bytes and ops using dual CAS loops.
    /// If the ops reservation fails after bytes succeeds, compensating CAS
    /// rollback ensures no counter corruption.
    ///
    /// # Arguments
    ///
    /// * `bytes` - Number of bytes to reserve
    /// * `ops` - Number of operations to reserve
    ///
    /// # Returns
    ///
    /// * `Ok(ReservationGuard)` - Reservation acquired successfully
    /// * `Err(AdmissionError)` - Limit exceeded or zero reservation
    ///
    /// # Errors
    ///
    /// Returns `AdmissionError::ByteLimitExceeded` if `committed + reserved + bytes > max_bytes`.
    /// Returns `AdmissionError::OpsLimitExceeded` if `committed + reserved + ops > max_ops`.
    /// Returns `AdmissionError::ZeroReservation` if both bytes and ops are zero.
    pub fn try_reserve(
        &self,
        bytes: usize,
        ops: usize,
    ) -> Result<ReservationGuard, AdmissionError> {
        // Reject zero reservations
        if bytes == 0 && ops == 0 {
            return Err(AdmissionError::ZeroReservation);
        }

        // Phase 1: CAS loop for bytes
        self.try_reserve_bytes(bytes)?;

        // Phase 2: CAS loop for ops (, with compensating rollback )
        self.try_reserve_ops(ops, bytes)?;

        // Note: ReservationGuard::new() increments active_guards internally
        Ok(ReservationGuard::new(
            Arc::clone(&self.buffer_state),
            Reservation { bytes, ops },
        ))
    }

    fn try_reserve_bytes(&self, bytes: usize) -> Result<(), AdmissionError> {
        if bytes == 0 {
            return Ok(());
        }

        loop {
            let (current_reserved, committed) = self.load_bytes_snapshot();
            self.check_byte_limit(committed, current_reserved, bytes)?;

            if self.try_update_reserved_bytes(current_reserved, bytes) {
                return Ok(());
            }
            // CAS failed due to contention, retry
        }
    }

    fn try_reserve_ops(&self, ops: usize, bytes: usize) -> Result<(), AdmissionError> {
        if ops == 0 {
            return Ok(());
        }

        loop {
            let (current_reserved, committed) = self.load_ops_snapshot();
            if let Err(err) = self.check_ops_limit(committed, current_reserved, ops) {
                self.rollback_bytes_if_needed(bytes);
                return Err(err);
            }

            if self.try_update_reserved_ops(current_reserved, ops) {
                return Ok(());
            }
            // CAS failed due to contention, retry
        }
    }

    fn load_bytes_snapshot(&self) -> (usize, usize) {
        let reserved = self.buffer_state.reserved_bytes.load(Ordering::Acquire);
        let committed = self.buffer_state.committed_bytes.load(Ordering::Acquire);
        (reserved, committed)
    }

    fn load_ops_snapshot(&self) -> (usize, usize) {
        let reserved = self.buffer_state.reserved_ops.load(Ordering::Acquire);
        let committed = self.buffer_state.committed_ops.load(Ordering::Acquire);
        (reserved, committed)
    }

    fn check_byte_limit(
        &self,
        committed: usize,
        reserved: usize,
        requested: usize,
    ) -> Result<(), AdmissionError> {
        let new_total = committed.saturating_add(reserved).saturating_add(requested);
        if new_total > self.max_bytes {
            return Err(AdmissionError::ByteLimitExceeded {
                requested,
                available: self
                    .max_bytes
                    .saturating_sub(committed.saturating_add(reserved)),
                max: self.max_bytes,
            });
        }
        Ok(())
    }

    fn check_ops_limit(
        &self,
        committed: usize,
        reserved: usize,
        requested: usize,
    ) -> Result<(), AdmissionError> {
        let new_total = committed.saturating_add(reserved).saturating_add(requested);
        if new_total > self.max_ops {
            return Err(AdmissionError::OpsLimitExceeded {
                requested,
                available: self
                    .max_ops
                    .saturating_sub(committed.saturating_add(reserved)),
                max: self.max_ops,
            });
        }
        Ok(())
    }

    fn try_update_reserved_bytes(&self, current_reserved: usize, bytes: usize) -> bool {
        self.buffer_state
            .reserved_bytes
            .compare_exchange_weak(
                current_reserved,
                current_reserved + bytes,
                Ordering::AcqRel,
                Ordering::Relaxed,
            )
            .is_ok()
    }

    fn try_update_reserved_ops(&self, current_reserved: usize, ops: usize) -> bool {
        self.buffer_state
            .reserved_ops
            .compare_exchange_weak(
                current_reserved,
                current_reserved + ops,
                Ordering::AcqRel,
                Ordering::Relaxed,
            )
            .is_ok()
    }

    fn rollback_bytes_if_needed(&self, bytes: usize) {
        if bytes > 0 {
            self.compensating_rollback_bytes(bytes);
        }
    }

    /// Compensating CAS rollback for bytes counter.
    ///
    /// Called when ops reservation fails after bytes was successfully reserved.
    /// Uses CAS to ensure we only decrement what we added, preventing corruption
    /// under concurrent modifications.
    ///
    /// # Why CAS Instead of `fetch_sub`
    ///
    /// With `fetch_sub`, if the bytes counter was modified between our increment
    /// and the ops failure (e.g., by compaction), we might:
    /// 1. Underflow (if decremented by compaction)
    /// 2. Corrupt another reservation's state
    ///
    /// Compensating CAS verifies our increment is still present before decrementing.
    fn compensating_rollback_bytes(&self, bytes: usize) {
        loop {
            let current = self.buffer_state.reserved_bytes.load(Ordering::Acquire);
            if current >= bytes {
                if self
                    .buffer_state
                    .reserved_bytes
                    .compare_exchange_weak(
                        current,
                        current - bytes,
                        Ordering::AcqRel,
                        Ordering::Relaxed,
                    )
                    .is_ok()
                {
                    return;
                }
                // CAS failed, retry
            } else {
                // Our increment was already consumed (by another rollback or compaction)
                // This is a rare race condition, but safe to exit
                return;
            }
        }
    }

    /// Returns the current utilization as a percentage (0.0 to 1.0).
    ///
    /// Returns the maximum of bytes and ops utilization.
    #[must_use]
    pub fn utilization(&self) -> f64 {
        let bytes_util = if self.max_bytes > 0 {
            usize_to_f64(self.buffer_state.total_bytes()) / usize_to_f64(self.max_bytes)
        } else {
            0.0
        };

        let ops_util = if self.max_ops > 0 {
            usize_to_f64(self.buffer_state.total_ops()) / usize_to_f64(self.max_ops)
        } else {
            0.0
        };

        bytes_util.max(ops_util)
    }

    /// Returns true if utilization exceeds the soft limit (80%).
    #[must_use]
    pub fn is_near_limit(&self) -> bool {
        self.utilization() >= 0.8
    }

    /// Returns statistics about the controller.
    #[must_use]
    pub fn stats(&self) -> AdmissionControllerStats {
        AdmissionControllerStats {
            max_bytes: self.max_bytes,
            max_ops: self.max_ops,
            committed_bytes: self.buffer_state.committed_bytes(),
            committed_ops: self.buffer_state.committed_ops(),
            reserved_bytes: self.buffer_state.reserved_bytes(),
            reserved_ops: self.buffer_state.reserved_ops(),
            active_guards: self.buffer_state.active_guards(),
        }
    }
}

/// Statistics for an admission controller.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AdmissionControllerStats {
    /// Maximum bytes limit.
    pub max_bytes: usize,
    /// Maximum operations limit.
    pub max_ops: usize,
    /// Current committed bytes.
    pub committed_bytes: usize,
    /// Current committed operations.
    pub committed_ops: usize,
    /// Current reserved bytes.
    pub reserved_bytes: usize,
    /// Current reserved operations.
    pub reserved_ops: usize,
    /// Number of active reservation guards.
    pub active_guards: usize,
}

impl AdmissionControllerStats {
    /// Total bytes (committed + reserved).
    #[inline]
    #[must_use]
    pub const fn total_bytes(&self) -> usize {
        self.committed_bytes + self.reserved_bytes
    }

    /// Total operations (committed + reserved).
    #[inline]
    #[must_use]
    pub const fn total_ops(&self) -> usize {
        self.committed_ops + self.reserved_ops
    }

    /// Available bytes before limit.
    #[inline]
    #[must_use]
    pub const fn available_bytes(&self) -> usize {
        self.max_bytes.saturating_sub(self.total_bytes())
    }

    /// Available operations before limit.
    #[inline]
    #[must_use]
    pub const fn available_ops(&self) -> usize {
        self.max_ops.saturating_sub(self.total_ops())
    }
}

impl fmt::Display for AdmissionControllerStats {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "bytes: {}/{} ({}% used), ops: {}/{} ({}% used), guards: {}",
            self.total_bytes(),
            self.max_bytes,
            if self.max_bytes > 0 {
                (self.total_bytes() * 100) / self.max_bytes
            } else {
                0
            },
            self.total_ops(),
            self.max_ops,
            if self.max_ops > 0 {
                (self.total_ops() * 100) / self.max_ops
            } else {
                0
            },
            self.active_guards
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn make_controller(max_bytes: usize, max_ops: usize) -> AdmissionController {
        let state = Arc::new(SharedBufferState::new());
        AdmissionController::new(state, max_bytes, max_ops)
    }

    #[test]
    fn test_new() {
        let state = Arc::new(SharedBufferState::new());
        let controller = AdmissionController::new(Arc::clone(&state), 1000, 100);
        assert_eq!(controller.max_bytes(), 1000);
        assert_eq!(controller.max_ops(), 100);
    }

    #[test]
    fn test_try_reserve_success() {
        let controller = make_controller(1000, 100);
        let guard = controller.try_reserve(100, 1).expect("should succeed");

        assert_eq!(controller.buffer_state().reserved_bytes(), 100);
        assert_eq!(controller.buffer_state().reserved_ops(), 1);
        assert_eq!(controller.buffer_state().active_guards(), 1);

        let _ = guard.abort();
        assert_eq!(controller.buffer_state().reserved_bytes(), 0);
        assert_eq!(controller.buffer_state().reserved_ops(), 0);
        assert_eq!(controller.buffer_state().active_guards(), 0);
    }

    #[test]
    fn test_try_reserve_zero_rejected() {
        let controller = make_controller(1000, 100);
        let result = controller.try_reserve(0, 0);
        assert!(matches!(result, Err(AdmissionError::ZeroReservation)));
    }

    #[test]
    fn test_try_reserve_bytes_only() {
        let controller = make_controller(1000, 100);
        let guard = controller.try_reserve(100, 0).expect("should succeed");

        assert_eq!(controller.buffer_state().reserved_bytes(), 100);
        assert_eq!(controller.buffer_state().reserved_ops(), 0);

        let _ = guard.abort();
    }

    #[test]
    fn test_try_reserve_ops_only() {
        let controller = make_controller(1000, 100);
        let guard = controller.try_reserve(0, 10).expect("should succeed");

        assert_eq!(controller.buffer_state().reserved_bytes(), 0);
        assert_eq!(controller.buffer_state().reserved_ops(), 10);

        let _ = guard.abort();
    }

    #[test]
    fn test_byte_limit_exceeded() {
        let controller = make_controller(100, 100);
        let result = controller.try_reserve(150, 1);

        match result {
            Err(AdmissionError::ByteLimitExceeded {
                requested,
                available,
                max,
            }) => {
                assert_eq!(requested, 150);
                assert_eq!(available, 100);
                assert_eq!(max, 100);
            }
            _ => panic!("Expected ByteLimitExceeded"),
        }
    }

    #[test]
    fn test_ops_limit_exceeded() {
        let controller = make_controller(1000, 10);
        let result = controller.try_reserve(100, 20);

        match result {
            Err(AdmissionError::OpsLimitExceeded {
                requested,
                available,
                max,
            }) => {
                assert_eq!(requested, 20);
                assert_eq!(available, 10);
                assert_eq!(max, 10);
            }
            _ => panic!("Expected OpsLimitExceeded"),
        }

        // Verify compensating rollback worked - bytes should be 0
        assert_eq!(controller.buffer_state().reserved_bytes(), 0);
    }

    #[test]
    fn test_multiple_reservations() {
        let controller = make_controller(1000, 100);

        let guard1 = controller.try_reserve(100, 5).unwrap();
        let guard2 = controller.try_reserve(200, 10).unwrap();
        let guard3 = controller.try_reserve(300, 15).unwrap();

        assert_eq!(controller.buffer_state().reserved_bytes(), 600);
        assert_eq!(controller.buffer_state().reserved_ops(), 30);
        assert_eq!(controller.buffer_state().active_guards(), 3);

        let _ = guard1.abort();
        assert_eq!(controller.buffer_state().reserved_bytes(), 500);
        assert_eq!(controller.buffer_state().active_guards(), 2);

        let _ = guard2.commit();
        assert_eq!(controller.buffer_state().reserved_bytes(), 300);
        assert_eq!(controller.buffer_state().committed_bytes(), 200);
        assert_eq!(controller.buffer_state().active_guards(), 1);

        let _ = guard3.abort();
        assert_eq!(controller.buffer_state().reserved_bytes(), 0);
        assert_eq!(controller.buffer_state().committed_bytes(), 200);
        assert_eq!(controller.buffer_state().active_guards(), 0);
    }

    #[test]
    fn test_commit_with_actual() {
        let controller = make_controller(1000, 100);
        let guard = controller.try_reserve(100, 10).unwrap();

        // Commit with less than reserved
        guard.commit_with_actual(50, 5).unwrap();

        assert_eq!(controller.buffer_state().reserved_bytes(), 0);
        assert_eq!(controller.buffer_state().committed_bytes(), 50);
        assert_eq!(controller.buffer_state().committed_ops(), 5);
    }

    #[test]
    fn test_limit_boundary() {
        let controller = make_controller(100, 10);

        // Exactly at limit should succeed
        let guard = controller.try_reserve(100, 10).unwrap();
        assert_eq!(controller.buffer_state().reserved_bytes(), 100);
        assert_eq!(controller.buffer_state().reserved_ops(), 10);

        // Any more should fail
        let result = controller.try_reserve(1, 1);
        assert!(result.is_err());

        let _ = guard.abort();
    }

    #[test]
    fn test_utilization() {
        let controller = make_controller(100, 100);
        assert!(controller.utilization().abs() < f64::EPSILON);

        let guard = controller.try_reserve(50, 25).unwrap();
        // Max of (50/100, 25/100) = 0.5
        assert!((controller.utilization() - 0.5).abs() < 0.01);
        assert!(!controller.is_near_limit());

        let _ = guard.abort();

        let guard2 = controller.try_reserve(80, 10).unwrap();
        // Max of (80/100, 10/100) = 0.8
        assert!((controller.utilization() - 0.8).abs() < 0.01);
        assert!(controller.is_near_limit());

        let _ = guard2.abort();
    }

    #[test]
    fn test_stats() {
        let controller = make_controller(1000, 100);
        let guard = controller.try_reserve(200, 20).unwrap();
        guard.commit_with_actual(150, 15).unwrap();

        let stats = controller.stats();
        assert_eq!(stats.max_bytes, 1000);
        assert_eq!(stats.max_ops, 100);
        assert_eq!(stats.committed_bytes, 150);
        assert_eq!(stats.committed_ops, 15);
        assert_eq!(stats.reserved_bytes, 0);
        assert_eq!(stats.reserved_ops, 0);
        assert_eq!(stats.total_bytes(), 150);
        assert_eq!(stats.available_bytes(), 850);
    }

    #[test]
    fn test_stats_display() {
        let stats = AdmissionControllerStats {
            max_bytes: 1000,
            max_ops: 100,
            committed_bytes: 100,
            committed_ops: 10,
            reserved_bytes: 50,
            reserved_ops: 5,
            active_guards: 1,
        };

        let display = format!("{stats}");
        assert!(display.contains("bytes: 150/1000"));
        assert!(display.contains("ops: 15/100"));
        assert!(display.contains("guards: 1"));
    }

    #[test]
    fn test_concurrent_reservations() {
        use std::thread;

        let state = Arc::new(SharedBufferState::new());
        let controller = Arc::new(AdmissionController::new(Arc::clone(&state), 10_000, 1_000));

        let mut handles = vec![];

        // Spawn 10 threads, each making 10 reservations
        for _ in 0..10 {
            let controller = Arc::clone(&controller);
            handles.push(thread::spawn(move || {
                for _ in 0..10 {
                    if let Ok(guard) = controller.try_reserve(100, 1) {
                        // Simulate some work
                        std::thread::yield_now();
                        let _ = guard.commit();
                    } else {
                        // Limit exceeded, acceptable under contention
                    }
                }
            }));
        }

        for h in handles {
            h.join().unwrap();
        }

        // All guards should be cleaned up
        assert_eq!(state.active_guards(), 0);
        assert_eq!(state.reserved_bytes(), 0);
        assert_eq!(state.reserved_ops(), 0);
    }

    #[test]
    fn test_compensating_rollback_under_contention() {
        // This test verifies that compensating rollback works correctly
        // when ops limit is exceeded after bytes reservation succeeds

        let state = Arc::new(SharedBufferState::new());
        // Set up: bytes limit high, ops limit low
        let controller = AdmissionController::new(Arc::clone(&state), 10_000, 1);

        // First reservation takes the only op
        let guard1 = controller.try_reserve(100, 1).unwrap();

        // Second reservation should fail on ops, but bytes was already incremented
        // Compensating rollback should clean up the bytes increment
        let result = controller.try_reserve(100, 1);
        assert!(matches!(
            result,
            Err(AdmissionError::OpsLimitExceeded { .. })
        ));

        // Verify bytes were rolled back
        assert_eq!(state.reserved_bytes(), 100); // Only guard1's reservation
        assert_eq!(state.reserved_ops(), 1);

        let _ = guard1.abort();
        assert_eq!(state.reserved_bytes(), 0);
        assert_eq!(state.reserved_ops(), 0);
    }
}