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
use std::hash::Hash;

use hydro_lang::*;
use location::NoTick;

#[expect(clippy::type_complexity, reason = "stream types with ordering")]
pub fn collect_quorum_with_response<
    'a,
    L: Location<'a> + NoTick,
    Order,
    K: Clone + Eq + Hash,
    V: Clone,
    E: Clone,
>(
    responses: Stream<(K, Result<V, E>), Timestamped<L>, Unbounded, Order>,
    min: usize,
    max: usize,
) -> (
    Stream<(K, V), Timestamped<L>, Unbounded, Order>,
    Stream<(K, E), Timestamped<L>, Unbounded, Order>,
) {
    let tick = responses.timestamp_source();
    let (not_all_complete_cycle, not_all) = tick.cycle::<Stream<_, _, _, Order>>();

    let current_responses = not_all.union(unsafe {
        // SAFETY: we always persist values that have not reached quorum, so even
        // with arbitrary batching we always produce deterministic quorum results
        responses.clone().tick_batch()
    });

    let count_per_key = current_responses.clone().fold_keyed_commutative(
        q!(move || (0, 0)),
        q!(move |accum, value| {
            if value.is_ok() {
                accum.0 += 1;
            } else {
                accum.1 += 1;
            }
        }),
    );

    let not_reached_min_count =
        count_per_key
            .clone()
            .filter_map(q!(move |(key, (success, _error))| if success < min {
                Some(key)
            } else {
                None
            }));

    let reached_min_count =
        count_per_key
            .clone()
            .filter_map(q!(move |(key, (success, _error))| if success >= min {
                Some(key)
            } else {
                None
            }));

    let just_reached_quorum = if max == min {
        not_all_complete_cycle
            .complete_next_tick(current_responses.clone().anti_join(reached_min_count));

        current_responses.anti_join(not_reached_min_count)
    } else {
        let (min_but_not_max_complete_cycle, min_but_not_max) = tick.cycle();

        let received_from_all =
            count_per_key.filter_map(q!(
                move |(key, (success, error))| if (success + error) >= max {
                    Some(key)
                } else {
                    None
                }
            ));

        min_but_not_max_complete_cycle
            .complete_next_tick(reached_min_count.filter_not_in(received_from_all.clone()));

        not_all_complete_cycle
            .complete_next_tick(current_responses.clone().anti_join(received_from_all));

        current_responses
            .anti_join(not_reached_min_count)
            .anti_join(min_but_not_max)
    };

    (
        just_reached_quorum
            .filter_map(q!(move |(key, res)| match res {
                Ok(v) => Some((key, v)),
                Err(_) => None,
            }))
            .all_ticks(),
        responses.filter_map(q!(move |(key, res)| match res {
            Ok(_) => None,
            Err(e) => Some((key, e)),
        })),
    )
}

#[expect(clippy::type_complexity, reason = "stream types with ordering")]
pub fn collect_quorum<'a, L: Location<'a> + NoTick, Order, K: Clone + Eq + Hash, E: Clone>(
    responses: Stream<(K, Result<(), E>), Timestamped<L>, Unbounded, Order>,
    min: usize,
    max: usize,
) -> (
    Stream<K, Timestamped<L>, Unbounded, Order>,
    Stream<(K, E), Timestamped<L>, Unbounded, Order>,
) {
    let tick = responses.timestamp_source();
    let (not_all_complete_cycle, not_all) = tick.cycle::<Stream<_, _, _, Order>>();

    let current_responses = not_all.union(unsafe {
        // SAFETY: we always persist values that have not reached quorum, so even
        // with arbitrary batching we always produce deterministic quorum results
        responses.clone().tick_batch()
    });

    let count_per_key = current_responses.clone().fold_keyed_commutative(
        q!(move || (0, 0)),
        q!(move |accum, value| {
            if value.is_ok() {
                accum.0 += 1;
            } else {
                accum.1 += 1;
            }
        }),
    );

    let reached_min_count =
        count_per_key
            .clone()
            .filter_map(q!(move |(key, (success, _error))| if success >= min {
                Some(key)
            } else {
                None
            }));

    let just_reached_quorum = if max == min {
        not_all_complete_cycle.complete_next_tick(
            current_responses
                .clone()
                .anti_join(reached_min_count.clone()),
        );

        reached_min_count
    } else {
        let (min_but_not_max_complete_cycle, min_but_not_max) = tick.cycle();

        let received_from_all =
            count_per_key.filter_map(q!(
                move |(key, (success, error))| if (success + error) >= max {
                    Some(key)
                } else {
                    None
                }
            ));

        min_but_not_max_complete_cycle.complete_next_tick(
            reached_min_count
                .clone()
                .filter_not_in(received_from_all.clone()),
        );

        not_all_complete_cycle.complete_next_tick(current_responses.anti_join(received_from_all));

        reached_min_count.filter_not_in(min_but_not_max)
    };

    (
        just_reached_quorum.all_ticks(),
        responses.filter_map(q!(move |(key, res)| match res {
            Ok(_) => None,
            Err(e) => Some((key, e)),
        })),
    )
}