Skip to main content

remote/protocol/
directory_limits.rs

1//! Directory lifetime limits shared by the source and destination.
2
3use anyhow::Context as _;
4use serde::{Deserialize, Serialize};
5use std::num::NonZeroUsize;
6
7/// Normal parallel traversal or the single reserved subtree with pipelined completion.
8#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
9pub enum DirectoryClass {
10    Normal,
11    Reserve,
12}
13
14/// Maximum directory lifetimes in each traversal class.
15///
16/// The destination sends its limits before normal discovery. The source takes the smaller
17/// capacity for each class across both endpoints. Ready does not return lifetime admission.
18#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
19pub struct DirectoryLimits {
20    pub normal: NonZeroUsize,
21    pub reserve: NonZeroUsize,
22}
23
24impl DirectoryLimits {
25    /// Use the remote plan installed during startup, or bounded logical limits for direct callers.
26    pub fn for_endpoint(
27        admission: common::EndpointAdmission,
28        streams: usize,
29        pending: usize,
30    ) -> anyhow::Result<Self> {
31        let resources = match admission {
32            common::EndpointAdmission::Remote(resources) => resources,
33            common::EndpointAdmission::Disabled => {
34                let streams =
35                    NonZeroUsize::new(streams).context("remote stream capacity is zero")?;
36                let pending =
37                    NonZeroUsize::new(pending).context("remote pending capacity is zero")?;
38                common::RemoteResources::for_limits(None, streams, streams, pending)?
39            }
40            common::EndpointAdmission::Configured { .. } => {
41                anyhow::bail!("remote directory traversal requires remote endpoint admission")
42            }
43        };
44        Ok(Self {
45            normal: resources.normal_directories,
46            reserve: resources.reserved_directories,
47        })
48    }
49
50    /// Take the per-class capacity supported by both endpoints.
51    pub fn intersect(self, other: Self) -> Self {
52        Self {
53            normal: self.normal.min(other.normal),
54            reserve: self.reserve.min(other.reserve),
55        }
56    }
57}
58
59#[cfg(test)]
60mod tests {
61    use super::*;
62
63    #[test]
64    fn directory_limits_leave_room_for_both_parallel_parents_and_deep_reserve() {
65        let limits = DirectoryLimits::for_endpoint(
66            common::EndpointAdmission::Remote(
67                common::RemoteResources::for_limits(
68                    std::num::NonZeroU64::new(1024),
69                    NonZeroUsize::new(20).unwrap(),
70                    NonZeroUsize::new(20).unwrap(),
71                    NonZeroUsize::new(80).unwrap(),
72                )
73                .unwrap(),
74            ),
75            20,
76            80,
77        )
78        .unwrap();
79        assert!(
80            limits.normal.get() >= 100,
81            "80 pinned parents and 20 active scans"
82        );
83        assert!(
84            limits.reserve.get() >= 80,
85            "an 80-level reserved traversal must fit"
86        );
87        assert!(2 * (limits.normal.get() + limits.reserve.get()) + 80 + 20 + 32 <= 1024);
88    }
89
90    #[test]
91    fn local_leaf_configuration_cannot_authorize_remote_directory_ownership() {
92        let result = DirectoryLimits::for_endpoint(
93            common::EndpointAdmission::Configured {
94                soft_limit: std::num::NonZeroU64::new(64),
95                leaf_capacity: NonZeroUsize::new(8).unwrap(),
96            },
97            8,
98            32,
99        );
100        assert!(result.is_err());
101    }
102
103    #[test]
104    fn disabled_admission_has_bounded_logical_capacity_without_a_depth_two_limit() {
105        let limits =
106            DirectoryLimits::for_endpoint(common::EndpointAdmission::Disabled, 20, 80).unwrap();
107        assert_eq!(limits.normal.get(), 200);
108        assert_eq!(limits.reserve.get(), 200);
109    }
110
111    #[test]
112    fn installed_remote_directory_counts_are_authoritative() {
113        let resources = common::RemoteResources::for_limits(
114            std::num::NonZeroU64::new(128),
115            NonZeroUsize::new(200).unwrap(),
116            NonZeroUsize::new(20).unwrap(),
117            NonZeroUsize::new(80).unwrap(),
118        )
119        .unwrap();
120        let limits =
121            DirectoryLimits::for_endpoint(common::EndpointAdmission::Remote(resources), 20, 80)
122                .unwrap();
123        assert_eq!(limits.normal.get(), 13);
124        assert_eq!(limits.reserve.get(), 13);
125    }
126
127    #[test]
128    fn endpoint_limits_preserve_normal_ancestor_capacity_at_low_file_concurrency() {
129        for (streams, normal, reserve) in [(4, 243, 243), (8, 238, 238), (20, 223, 223)] {
130            let resources = common::RemoteResources::for_limits(
131                std::num::NonZeroU64::new(1024),
132                NonZeroUsize::new(streams).unwrap(),
133                NonZeroUsize::new(streams).unwrap(),
134                NonZeroUsize::new(streams * 4).unwrap(),
135            )
136            .unwrap();
137            let limits = DirectoryLimits::for_endpoint(
138                common::EndpointAdmission::Remote(resources),
139                streams,
140                streams * 4,
141            )
142            .unwrap();
143            assert_eq!(limits.normal.get(), normal);
144            assert_eq!(limits.reserve.get(), reserve);
145        }
146    }
147
148    #[test]
149    fn each_endpoint_can_constrain_a_different_directory_class() {
150        let source = DirectoryLimits {
151            normal: NonZeroUsize::new(200).unwrap(),
152            reserve: NonZeroUsize::new(80).unwrap(),
153        };
154        let destination = DirectoryLimits {
155            normal: NonZeroUsize::new(100).unwrap(),
156            reserve: NonZeroUsize::new(160).unwrap(),
157        };
158        let negotiated = source.intersect(destination);
159        assert_eq!(negotiated.normal.get(), 100);
160        assert_eq!(negotiated.reserve.get(), 80);
161    }
162}