poise-discovery 0.1.1

Versioned membership snapshots for Poise load balancing
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
use std::{borrow::Borrow, collections::HashMap, error::Error, fmt, hash::Hash, sync::Arc};

use crate::{Discovered, Membership, PublishError, Revision, Snapshot, SnapshotPublisher};

/// A membership change produced by a discovery source.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum Change<Key, Backend> {
    /// Inserts a new member or refreshes and reactivates an existing member.
    Upsert(Key, Backend),
    /// Begins graceful draining for a member.
    Remove(Key),
}

/// The effect of applying a directory operation.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub enum Effect {
    /// A new member was inserted.
    Inserted,
    /// An active member's backend value was replaced.
    Updated,
    /// A draining member was refreshed and made active again.
    Revived,
    /// Graceful draining began.
    DrainStarted,
    /// The member was already draining; no state changed.
    AlreadyDraining,
    /// Draining finished and the member was physically removed.
    DrainFinished,
    /// The requested member did not exist; no state changed.
    NotFound,
    /// Drain completion was requested for an active member; no state changed.
    NotDraining,
}

impl Effect {
    /// Returns whether the operation changed the directory and its revision.
    #[must_use]
    pub const fn changed(self) -> bool {
        matches!(
            self,
            Self::Inserted
                | Self::Updated
                | Self::Revived
                | Self::DrainStarted
                | Self::DrainFinished
        )
    }
}

/// The effect and resulting revision of a directory operation.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct Applied {
    effect: Effect,
    revision: Revision,
}

impl Applied {
    const fn new(effect: Effect, revision: Revision) -> Self {
        Self { effect, revision }
    }

    /// Returns the operation effect.
    #[must_use]
    pub const fn effect(self) -> Effect {
        self.effect
    }

    /// Returns the directory revision after the operation.
    #[must_use]
    pub const fn revision(self) -> Revision {
        self.revision
    }

    /// Returns whether the operation changed the directory.
    #[must_use]
    pub const fn changed(self) -> bool {
        self.effect.changed()
    }
}

struct Entry<Key, Backend> {
    key: Arc<Key>,
    backend: Arc<Backend>,
    membership: Membership,
}

impl<Key, Backend> Clone for Entry<Key, Backend> {
    fn clone(&self) -> Self {
        Self {
            key: Arc::clone(&self.key),
            backend: Arc::clone(&self.backend),
            membership: self.membership,
        }
    }
}

/// A single-writer, insertion-ordered backend membership directory.
///
/// Updates are optimized for correctness and stable snapshot order. Reads
/// should use published [`Snapshot`]s rather than sharing the directory.
pub struct Directory<Key, Backend> {
    revision: Revision,
    entries: Vec<Entry<Key, Backend>>,
    positions: HashMap<Key, usize>,
}

impl<Key, Backend> Directory<Key, Backend>
where
    Key: Clone + Eq + Hash,
{
    /// Creates an empty directory at [`Revision::INITIAL`].
    #[must_use]
    pub fn new() -> Self {
        Self {
            revision: Revision::INITIAL,
            entries: Vec::new(),
            positions: HashMap::new(),
        }
    }

    /// Creates an empty directory at a caller-provided revision.
    ///
    /// This supports restoring a persisted revision clock.
    #[must_use]
    pub fn with_revision(revision: Revision) -> Self {
        Self {
            revision,
            entries: Vec::new(),
            positions: HashMap::new(),
        }
    }

    /// Returns the current revision.
    #[must_use]
    pub const fn revision(&self) -> Revision {
        self.revision
    }

    /// Returns the number of active and draining members.
    #[must_use]
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Returns whether the directory contains no active or draining members.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Applies a discovery change.
    ///
    /// # Errors
    ///
    /// Returns [`RevisionExhausted`] if an effective change would overflow the
    /// revision clock. In that case the directory remains unchanged.
    pub fn apply(&mut self, change: Change<Key, Backend>) -> Result<Applied, RevisionExhausted> {
        match change {
            Change::Upsert(key, backend) => self.upsert(key, backend),
            Change::Remove(key) => self.begin_drain(&key),
        }
    }

    /// Applies a group of discovery changes transactionally.
    ///
    /// The returned outcomes retain their per-change revisions. The directory
    /// commits only when every change succeeds, allowing a caller to publish
    /// one coherent snapshot for a discovery response.
    ///
    /// This operation clones directory metadata and shared backend handles; it
    /// does not clone backend values.
    ///
    /// # Errors
    ///
    /// Returns [`RevisionExhausted`] and leaves the directory unchanged if any
    /// effective change would exhaust the revision clock.
    pub fn apply_batch<I>(&mut self, changes: I) -> Result<Vec<Applied>, RevisionExhausted>
    where
        I: IntoIterator<Item = Change<Key, Backend>>,
    {
        let mut staged = self.clone();
        let outcomes = changes
            .into_iter()
            .map(|change| staged.apply(change))
            .collect::<Result<Vec<_>, _>>()?;
        *self = staged;
        Ok(outcomes)
    }

    /// Inserts, refreshes, or revives a member.
    ///
    /// # Errors
    ///
    /// Returns [`RevisionExhausted`] without mutation if the revision clock is
    /// exhausted.
    pub fn upsert(&mut self, key: Key, backend: Backend) -> Result<Applied, RevisionExhausted> {
        let next = self.next_revision()?;

        let effect = if let Some(&index) = self.positions.get(&key) {
            let entry = &mut self.entries[index];
            let effect = if entry.membership == Membership::Draining {
                Effect::Revived
            } else {
                Effect::Updated
            };
            entry.backend = Arc::new(backend);
            entry.membership = Membership::Active;
            effect
        } else {
            let index = self.entries.len();
            self.positions.insert(key.clone(), index);
            self.entries.push(Entry {
                key: Arc::new(key),
                backend: Arc::new(backend),
                membership: Membership::Active,
            });
            Effect::Inserted
        };

        self.revision = next;
        Ok(Applied::new(effect, self.revision))
    }

    /// Starts graceful draining for a member.
    ///
    /// # Errors
    ///
    /// Returns [`RevisionExhausted`] without mutation if a state change is
    /// needed but the revision clock is exhausted.
    pub fn begin_drain<Q>(&mut self, key: &Q) -> Result<Applied, RevisionExhausted>
    where
        Key: Borrow<Q>,
        Q: Eq + Hash + ?Sized,
    {
        let Some(&index) = self.positions.get(key) else {
            return Ok(Applied::new(Effect::NotFound, self.revision));
        };
        if self.entries[index].membership == Membership::Draining {
            return Ok(Applied::new(Effect::AlreadyDraining, self.revision));
        }

        let next = self.next_revision()?;
        self.entries[index].membership = Membership::Draining;
        self.revision = next;
        Ok(Applied::new(Effect::DrainStarted, self.revision))
    }

    /// Physically removes a member after its outstanding work has drained.
    ///
    /// # Errors
    ///
    /// Returns [`RevisionExhausted`] without mutation if removal is needed but
    /// the revision clock is exhausted.
    pub fn finish_drain<Q>(&mut self, key: &Q) -> Result<Applied, RevisionExhausted>
    where
        Key: Borrow<Q>,
        Q: Eq + Hash + ?Sized,
    {
        let Some(&index) = self.positions.get(key) else {
            return Ok(Applied::new(Effect::NotFound, self.revision));
        };
        if self.entries[index].membership != Membership::Draining {
            return Ok(Applied::new(Effect::NotDraining, self.revision));
        }

        let next = self.next_revision()?;
        self.positions.remove(key);
        self.entries.remove(index);
        for (position, entry) in self.entries[index..].iter().enumerate() {
            self.positions
                .insert(entry.key.as_ref().clone(), index + position);
        }
        self.revision = next;
        Ok(Applied::new(Effect::DrainFinished, self.revision))
    }

    /// Creates an immutable snapshot in stable insertion order.
    #[must_use]
    pub fn snapshot(&self) -> Snapshot<Discovered<Key, Backend>> {
        let members = self
            .entries
            .iter()
            .map(|entry| {
                Discovered::new(
                    Arc::clone(&entry.key),
                    Arc::clone(&entry.backend),
                    entry.membership,
                )
            })
            .collect();
        Snapshot::new(self.revision, members)
    }

    /// Creates and atomically publishes the current snapshot.
    ///
    /// # Errors
    ///
    /// Returns [`PublishError`] if this directory's revision is not newer than
    /// the publisher's current revision.
    pub fn publish(
        &self,
        publisher: &mut SnapshotPublisher<Discovered<Key, Backend>>,
    ) -> Result<Arc<Snapshot<Discovered<Key, Backend>>>, PublishError> {
        publisher.publish(self.snapshot())
    }

    fn next_revision(&self) -> Result<Revision, RevisionExhausted> {
        self.revision.checked_next().ok_or(RevisionExhausted)
    }
}

impl<Key, Backend> Default for Directory<Key, Backend>
where
    Key: Clone + Eq + Hash,
{
    fn default() -> Self {
        Self::new()
    }
}

impl<Key, Backend> Clone for Directory<Key, Backend>
where
    Key: Clone,
{
    fn clone(&self) -> Self {
        Self {
            revision: self.revision,
            entries: self.entries.clone(),
            positions: self.positions.clone(),
        }
    }
}

/// The directory's monotonic revision clock cannot advance further.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct RevisionExhausted;

impl fmt::Display for RevisionExhausted {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("the membership revision clock is exhausted")
    }
}

impl Error for RevisionExhausted {}

#[cfg(test)]
mod tests {
    use poise_core::{Backend, Candidate, Policy, Status, policy::RoundRobin};

    use super::*;

    #[test]
    fn removal_drains_before_physical_retirement() {
        let mut directory = Directory::new();
        directory.upsert("a", Backend::new("http://a")).unwrap();
        directory.upsert("b", Backend::new("http://b")).unwrap();

        let applied = directory.begin_drain("a").unwrap();
        assert_eq!(applied.effect(), Effect::DrainStarted);
        assert_eq!(applied.revision(), Revision::new(3));

        let draining = directory.snapshot();
        assert_eq!(draining.len(), 2);
        assert_eq!(draining[0].membership(), Membership::Draining);
        assert_eq!(draining[0].status(), Status::Draining);

        let selected = RoundRobin::new().pick(&draining, &()).unwrap();
        assert_eq!(draining[selected.index()].key(), &"b");

        let applied = directory.finish_drain("a").unwrap();
        assert_eq!(applied.effect(), Effect::DrainFinished);
        let retired = directory.snapshot();
        assert_eq!(retired.len(), 1);
        assert_eq!(retired[0].key(), &"b");

        // Existing readers keep their coherent pre-retirement snapshot.
        assert_eq!(draining.len(), 2);
        assert_eq!(draining[0].key(), &"a");
    }

    #[test]
    fn rediscovery_revives_a_draining_member_in_place() {
        let mut directory = Directory::new();
        directory.upsert("a", Backend::new(1)).unwrap();
        directory.begin_drain("a").unwrap();

        let applied = directory.upsert("a", Backend::new(2)).unwrap();
        assert_eq!(applied.effect(), Effect::Revived);

        let snapshot = directory.snapshot();
        assert_eq!(snapshot[0].membership(), Membership::Active);
        assert_eq!(snapshot[0].backend().id(), &2);
    }

    #[test]
    fn no_op_lifecycle_requests_do_not_advance_revision() {
        let mut directory: Directory<&str, Backend<usize>> = Directory::new();

        let missing = directory.begin_drain("missing").unwrap();
        assert_eq!(missing.effect(), Effect::NotFound);
        assert!(!missing.changed());
        assert_eq!(missing.revision(), Revision::INITIAL);

        directory.upsert("a", Backend::new(1)).unwrap();
        let active = directory.finish_drain("a").unwrap();
        assert_eq!(active.effect(), Effect::NotDraining);
        assert_eq!(active.revision(), Revision::new(1));

        directory.begin_drain("a").unwrap();
        let repeated = directory.begin_drain("a").unwrap();
        assert_eq!(repeated.effect(), Effect::AlreadyDraining);
        assert_eq!(repeated.revision(), Revision::new(2));
    }

    #[test]
    fn updates_and_retirement_preserve_relative_order() {
        let mut directory = Directory::new();
        for key in ["a", "b", "c", "d"] {
            directory.upsert(key, Backend::new(key)).unwrap();
        }
        directory.upsert("c", Backend::new("c2")).unwrap();
        directory.begin_drain("b").unwrap();
        directory.finish_drain("b").unwrap();

        let keys: Vec<_> = directory
            .snapshot()
            .iter()
            .map(Discovered::key)
            .copied()
            .collect();
        assert_eq!(keys, ["a", "c", "d"]);
    }

    #[test]
    fn revision_overflow_is_transactional() {
        let mut directory: Directory<&str, Backend<usize>> =
            Directory::with_revision(Revision::new(u64::MAX));

        assert_eq!(
            directory.upsert("a", Backend::new(1)),
            Err(RevisionExhausted)
        );
        assert!(directory.is_empty());
        assert_eq!(directory.revision(), Revision::new(u64::MAX));
    }

    #[test]
    fn batch_is_all_or_nothing() {
        let mut directory: Directory<&str, Backend<usize>> =
            Directory::with_revision(Revision::new(u64::MAX - 1));

        let result = directory.apply_batch([
            Change::Upsert("a", Backend::new(1)),
            Change::Upsert("b", Backend::new(2)),
        ]);

        assert_eq!(result, Err(RevisionExhausted));
        assert!(directory.is_empty());
        assert_eq!(directory.revision(), Revision::new(u64::MAX - 1));
    }

    #[test]
    fn batch_reports_each_effect_and_commits_once_complete() {
        let mut directory = Directory::new();
        let outcomes = directory
            .apply_batch([
                Change::Upsert("a", Backend::new(1)),
                Change::Upsert("b", Backend::new(2)),
                Change::Remove("a"),
            ])
            .unwrap();

        let effects: Vec<_> = outcomes.iter().map(|outcome| outcome.effect()).collect();
        assert_eq!(
            effects,
            [Effect::Inserted, Effect::Inserted, Effect::DrainStarted]
        );
        assert_eq!(directory.revision(), Revision::new(3));
        assert_eq!(directory.snapshot()[0].membership(), Membership::Draining);
    }
}