rocketmq-transport 1.0.0

Bounded TCP/TLS transport for RocketMQ Rust
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
// Copyright 2026 The RocketMQ Rust Authors
//
// 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.

//! Retained-byte admission for deferred waits.

use std::alloc::Layout;
use std::fmt;
use std::mem::size_of;
use std::sync::atomic::AtomicUsize;
use std::sync::Arc;

use rocketmq_runtime::BudgetClass;
use rocketmq_runtime::BudgetDimension;
use rocketmq_runtime::BudgetLimit;
use rocketmq_runtime::BudgetRejection;
use rocketmq_runtime::FullPolicy;
use rocketmq_runtime::ResourceBudget;
use rocketmq_runtime::ResourcePermit;
use rocketmq_runtime::RuntimeContractViolation;

use super::DeferredResponder;
use super::ResponseState;
use crate::admission::AdmissionController;
use crate::contract::TransportContractViolation;

const DEFERRED_BUDGET_NAME: &str = "transport-deferred-wait";

/// Fixed count and retained-byte limits for deferred waits.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DeferredWaitLimits {
    max_waiters: usize,
    max_retained_bytes: usize,
}

impl DeferredWaitLimits {
    /// Creates explicit deferred-wait limits.
    #[must_use]
    pub const fn new(max_waiters: usize, max_retained_bytes: usize) -> Self {
        Self {
            max_waiters,
            max_retained_bytes,
        }
    }

    /// Returns the maximum simultaneously retained waits.
    #[must_use]
    pub const fn max_waiters(self) -> usize {
        self.max_waiters
    }

    /// Returns the maximum bytes retained by all waits.
    #[must_use]
    pub const fn max_retained_bytes(self) -> usize {
        self.max_retained_bytes
    }
}

/// Caller-declared variable ownership retained by one deferred wait.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DeferredRetainedSizeParts {
    resume_bytes: usize,
    filter_bytes: usize,
    secondary_index_bytes: usize,
    metadata_bytes: usize,
}

impl DeferredRetainedSizeParts {
    /// Starts a retained-size declaration with the owned resume payload.
    #[must_use]
    pub const fn new(resume_bytes: usize) -> Self {
        Self {
            resume_bytes,
            filter_bytes: 0,
            secondary_index_bytes: 0,
            metadata_bytes: 0,
        }
    }

    /// Declares retained filter or subscription-builder storage.
    #[must_use]
    pub const fn with_filter_bytes(mut self, filter_bytes: usize) -> Self {
        self.filter_bytes = filter_bytes;
        self
    }

    /// Declares the retained secondary-index lease estimate.
    #[must_use]
    pub const fn with_secondary_index_bytes(mut self, secondary_index_bytes: usize) -> Self {
        self.secondary_index_bytes = secondary_index_bytes;
        self
    }

    /// Declares other retained wait metadata.
    #[must_use]
    pub const fn with_metadata_bytes(mut self, metadata_bytes: usize) -> Self {
        self.metadata_bytes = metadata_bytes;
        self
    }

    /// Returns the owned resume payload estimate.
    #[must_use]
    pub const fn resume_bytes(self) -> usize {
        self.resume_bytes
    }

    /// Returns the filter or subscription-builder estimate.
    #[must_use]
    pub const fn filter_bytes(self) -> usize {
        self.filter_bytes
    }

    /// Returns the secondary-index lease estimate.
    #[must_use]
    pub const fn secondary_index_bytes(self) -> usize {
        self.secondary_index_bytes
    }

    /// Returns the other retained metadata estimate.
    #[must_use]
    pub const fn metadata_bytes(self) -> usize {
        self.metadata_bytes
    }
}

/// Checked total retained by one admitted deferred wait.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DeferredRetainedSize(usize);

impl DeferredRetainedSize {
    /// Computes the exact fixed capability/state/permit charge and adds every
    /// caller-declared retained ownership class with checked arithmetic.
    ///
    /// The logical `Arc<ResponseState>` allocation includes two strong/weak
    /// reference counters, the aligned state payload, and final allocation
    /// padding. Allocator-private metadata is deliberately excluded.
    ///
    /// # Errors
    ///
    /// Returns a contract violation if any layout or size addition exceeds
    /// `usize`.
    pub fn try_from_parts(parts: DeferredRetainedSizeParts) -> Result<Self, TransportContractViolation> {
        let bytes = fixed_retained_bytes()
            .and_then(|bytes| bytes.checked_add(parts.resume_bytes))
            .and_then(|bytes| bytes.checked_add(parts.filter_bytes))
            .and_then(|bytes| bytes.checked_add(parts.secondary_index_bytes))
            .and_then(|bytes| bytes.checked_add(parts.metadata_bytes))
            .ok_or(TransportContractViolation::DeferredRetainedSizeOverflow)?;
        Ok(Self(bytes))
    }

    /// Returns the checked total retained bytes.
    #[must_use]
    pub const fn bytes(self) -> usize {
        self.0
    }

    pub(crate) fn checked_add(self, additional: usize) -> Result<Self, TransportContractViolation> {
        self.0
            .checked_add(additional)
            .map(Self)
            .ok_or(TransportContractViolation::DeferredRetainedSizeOverflow)
    }
}

/// Result of reserving bounded capacity for one deferred wait.
#[must_use]
pub enum DeferredAdmissionAcquireOutcome {
    /// Admission closed when a dynamic parent generation was retired.
    Closed,
    /// Capacity was acquired and the affine permit is returned.
    Acquired(DeferredWaitPermit),
    /// The deferred owner exhausted its waiter count.
    WaiterCapacityExhausted(BudgetRejection),
    /// The deferred owner exhausted its retained-byte capacity.
    RetainedByteCapacityExhausted(BudgetRejection),
    /// Another child exhausted the shared process or service root.
    ParentCapacityExhausted(BudgetRejection),
}

/// Independent low-cardinality view of retained deferred waits.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct DeferredAdmissionSnapshot {
    waiting_count: usize,
    retained_bytes: usize,
    rejected_count: u64,
}

impl DeferredAdmissionSnapshot {
    /// Returns the number of currently retained waits.
    #[must_use]
    pub const fn waiting_count(self) -> usize {
        self.waiting_count
    }

    /// Returns the bytes currently retained by waits.
    #[must_use]
    pub const fn retained_bytes(self) -> usize {
        self.retained_bytes
    }

    /// Returns failed deferred reservations observed at this owner.
    #[must_use]
    pub const fn rejected_count(self) -> u64 {
        self.rejected_count
    }
}

struct DeferredAdmissionInner {
    budget: ResourceBudget,
    limits: DeferredWaitLimits,
}

/// Cloneable composition owner for independent deferred-wait admission.
#[derive(Clone)]
pub struct DeferredAdmission {
    inner: Arc<DeferredAdmissionInner>,
}

impl DeferredAdmission {
    /// Atomically configures or retrieves the deferred owner attached to one
    /// transport admission controller.
    ///
    /// Concurrent equal configurations return clones of one owner. A different
    /// configuration is rejected without replacing the established owner.
    ///
    /// # Errors
    ///
    /// Returns a typed configuration error for zero/excess capacity, a
    /// different prior configuration, or an established budget invariant.
    pub fn try_configure(
        controller: &AdmissionController,
        limits: DeferredWaitLimits,
    ) -> Result<Self, TransportContractViolation> {
        controller.configure_deferred_admission(limits)
    }

    pub(crate) fn try_new(
        process_budget: &ResourceBudget,
        limits: DeferredWaitLimits,
    ) -> Result<Self, TransportContractViolation> {
        let path = format!("{}/{DEFERRED_BUDGET_NAME}", process_budget.path());
        validate_limits(limits, process_budget.limit(), &path)?;
        let budget = process_budget
            .child(
                DEFERRED_BUDGET_NAME,
                BudgetLimit::new(limits.max_waiters, limits.max_retained_bytes, FullPolicy::Reject),
            )
            .map_err(deferred_budget_contract)?;
        Ok(Self {
            inner: Arc::new(DeferredAdmissionInner { budget, limits }),
        })
    }

    /// Returns the immutable limits shared by this owner.
    #[must_use]
    pub fn limits(&self) -> DeferredWaitLimits {
        self.inner.limits
    }

    /// Returns the current deferred wait count/byte observation.
    #[must_use]
    pub fn snapshot(&self) -> DeferredAdmissionSnapshot {
        let snapshot = self.inner.budget.snapshot();
        DeferredAdmissionSnapshot {
            waiting_count: snapshot.current_count,
            retained_bytes: snapshot.current_bytes,
            rejected_count: snapshot.rejected_count,
        }
    }

    /// Attempts to reserve one already-checked retained size without waiting.
    ///
    /// The returned outcome distinguishes local count, local retained-byte,
    /// and shared-parent exhaustion. No registry or asynchronous wait is
    /// created.
    pub fn try_reserve(&self, retained: DeferredRetainedSize) -> DeferredAdmissionAcquireOutcome {
        match self.inner.budget.try_acquire(retained.bytes(), BudgetClass::Data) {
            Ok(permit) => DeferredAdmissionAcquireOutcome::Acquired(DeferredWaitPermit {
                permit: Some(permit),
                retained_bytes: retained.bytes(),
            }),
            Err(rejection) if rejection.is_closed() => DeferredAdmissionAcquireOutcome::Closed,
            Err(rejection) if rejection.exhausted_path() != self.inner.budget.path() => {
                DeferredAdmissionAcquireOutcome::ParentCapacityExhausted(rejection)
            }
            Err(rejection) => match rejection.dimension() {
                Some(BudgetDimension::Count) => DeferredAdmissionAcquireOutcome::WaiterCapacityExhausted(rejection),
                Some(BudgetDimension::Bytes) => {
                    DeferredAdmissionAcquireOutcome::RetainedByteCapacityExhausted(rejection)
                }
                Some(BudgetDimension::Rate) => DeferredAdmissionAcquireOutcome::ParentCapacityExhausted(rejection),
                None => DeferredAdmissionAcquireOutcome::Closed,
            },
        }
    }

    #[cfg(test)]
    pub(crate) fn same_owner(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.inner, &other.inner)
    }
}

impl fmt::Debug for DeferredAdmission {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("DeferredAdmission")
            .field("limits", &self.inner.limits)
            .finish_non_exhaustive()
    }
}

/// Affine ownership of one deferred waiter count/byte reservation.
///
/// The permit owns only the established resource-budget permit. It contains no
/// processor execution permit and creates no task or wait queue.
///
/// ```compile_fail
/// use rocketmq_transport::api::DeferredWaitPermit;
///
/// fn permits_are_affine(permit: &DeferredWaitPermit) {
///     let _: DeferredWaitPermit = permit.clone();
/// }
/// ```
#[must_use]
pub struct DeferredWaitPermit {
    permit: Option<ResourcePermit>,
    retained_bytes: usize,
}

impl DeferredWaitPermit {
    /// Returns the exact bytes charged by this reservation.
    #[must_use]
    pub const fn retained_bytes(&self) -> usize {
        self.retained_bytes
    }

    /// Consumes this permit and releases its count/byte ownership immediately.
    pub fn release(mut self) {
        drop(self.permit.take());
    }
}

impl fmt::Debug for DeferredWaitPermit {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("DeferredWaitPermit")
            .field("retained_bytes", &self.retained_bytes)
            .finish_non_exhaustive()
    }
}

fn validate_limits(
    limits: DeferredWaitLimits,
    parent: BudgetLimit,
    _path: &str,
) -> Result<(), TransportContractViolation> {
    let failure = if limits.max_waiters == 0 {
        Some(RuntimeContractViolation::ZeroBudgetCapacity {
            dimension: BudgetDimension::Count,
        })
    } else if limits.max_retained_bytes == 0 {
        Some(RuntimeContractViolation::ZeroBudgetCapacity {
            dimension: BudgetDimension::Bytes,
        })
    } else if limits.max_waiters > parent.capacity.count {
        Some(RuntimeContractViolation::ChildBudgetExceedsParent {
            dimension: BudgetDimension::Count,
        })
    } else if limits.max_retained_bytes > parent.capacity.bytes {
        Some(RuntimeContractViolation::ChildBudgetExceedsParent {
            dimension: BudgetDimension::Bytes,
        })
    } else {
        None
    };
    match failure {
        Some(source) => Err(deferred_budget_contract(source)),
        None => Ok(()),
    }
}

fn deferred_budget_contract(source: RuntimeContractViolation) -> TransportContractViolation {
    match source {
        RuntimeContractViolation::ZeroBudgetCapacity {
            dimension: BudgetDimension::Count,
        } => TransportContractViolation::DeferredAdmissionZeroWaiterCapacity(source),
        RuntimeContractViolation::ZeroBudgetCapacity {
            dimension: BudgetDimension::Bytes,
        } => TransportContractViolation::DeferredAdmissionZeroRetainedByteCapacity(source),
        RuntimeContractViolation::ChildBudgetExceedsParent { .. } => {
            TransportContractViolation::DeferredAdmissionExceedsProcessCapacity(source)
        }
        RuntimeContractViolation::ZeroBudgetCapacity {
            dimension: BudgetDimension::Rate,
        }
        | RuntimeContractViolation::EmptyBudgetName
        | RuntimeContractViolation::InvalidBudgetName
        | RuntimeContractViolation::ZeroBudgetRate
        | RuntimeContractViolation::ZeroBudgetMaxAge
        | RuntimeContractViolation::ReserveExceedsBudgetCapacity { .. }
        | RuntimeContractViolation::ReserveWithoutBudgetCapacity { .. }
        | RuntimeContractViolation::ChildBudgetMaxAgeExceedsParent
        | RuntimeContractViolation::InvalidConfiguration { .. }
        | RuntimeContractViolation::PermitTargetInDifferentTree
        | RuntimeContractViolation::InvalidMemoryLimit { .. }
        | RuntimeContractViolation::InvalidSchedule { .. }
        | RuntimeContractViolation::InvalidMetadataConfiguration { .. } => {
            TransportContractViolation::DeferredAdmissionBudget(source)
        }
    }
}

fn fixed_retained_bytes() -> Option<usize> {
    let header = Layout::array::<AtomicUsize>(2).ok()?;
    let (state_allocation, _) = header.extend(Layout::new::<ResponseState>()).ok()?;
    size_of::<DeferredResponder>()
        .checked_add(size_of::<crate::dispatch::RequestControlView>())?
        .checked_add(state_allocation.pad_to_align().size())?
        .checked_add(size_of::<DeferredWaitPermit>())
}

#[cfg(test)]
fn response_state_allocation_bytes() -> usize {
    let header = Layout::array::<AtomicUsize>(2).expect("two Arc counters have a valid layout");
    let (state_allocation, _) = header
        .extend(Layout::new::<ResponseState>())
        .expect("ResponseState follows the Arc header");
    state_allocation.pad_to_align().size()
}

#[cfg(test)]
#[path = "../../tests/unit/dispatch/deferred_admission.rs"]
mod tests;