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
// Copyright 2019 Stichting Organism
// Copyright (c) 2018-2019 Prime Type Ltd
//
// 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.


//! Ring module of the node, it manages the ring links.
//! It aims at discovering a node’s successor and predecessor
//! for each topic in its subscription, and at quickly adapting
//! to new successors/predecessors in dynamic networks.
//!

use std::collections::{BTreeMap, BTreeSet};

use crate::topology::Module;
use crate::{Id, InterestLevel, NodeData, Topic};

/// the number of neighbor for a given subscribed topic of the given node.
///
/// although the protocol only requires a view of length 2 (i.e. one
/// predecessor and one successor), we keep an additional predecessor
/// and successor in case of failures or node churn.
pub const RINGS_MAX_VIEW_SIZE: usize = 4;

/// see [`RINGS_MAX_VIEW_SIZE`]
pub const RINGS_NEIGHBOR_PREDECESSOR_SIZE: usize = RINGS_MAX_VIEW_SIZE / 2;
/// see [`RINGS_MAX_VIEW_SIZE`]
pub const RINGS_NEIGHBOR_SUCCESSOR_SIZE: usize = RINGS_MAX_VIEW_SIZE / 2;

/// this object is responsible for maintaining the ring links
/// of the node.
#[derive(Clone, Debug)]
pub struct Rings {
    /// each node maintains `MAX_VIEW_SIZE` neighbors for each topic
    /// in its subscription: `NEIGHBOR_PREDECESSOR` with lower
    /// and `NEIGHBOR_SUCCESSOR` with higher id.
    pub(crate) neighbors: BTreeMap<Topic, TopicView>,
}

#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
pub(crate) enum Slot<A> {
    Taken(A),
    Available,
}

/// the Rings' Topic view, [`Id`] of the nodes that are subscribed
/// to a given Topic.
///
/// This structure is mainly necessary for the [`Rings`] module.
///
/// [`Rings`]: ./node/ring/struct.Rings.html
#[derive(Clone, Debug)]
pub struct TopicView([Slot<Id>; RINGS_MAX_VIEW_SIZE]);

impl Module for Rings {
    fn name(&self) -> &'static str {
        "rings"
    }

    fn select_gossips(
        &self,
        our_node: &NodeData,
        gossip_recipient: &NodeData,
        known_nodes: &BTreeMap<Id, NodeData>,
    ) -> BTreeMap<Id, NodeData> {
        self.select_nodes_to_send(our_node, gossip_recipient, known_nodes)
    }

    fn update(&mut self, self_node: &NodeData, known_nodes: &BTreeMap<Id, NodeData>) {
        self.update_view(self_node, known_nodes)
    }

    fn view(&self, known_nodes: &BTreeMap<Id, NodeData>, view: &mut BTreeMap<Id, NodeData>) {
        for neighborhood in self.neighbors.values() {
            for slot in neighborhood.iter() {
                if let Slot::Taken(id) = slot {
                    if let Some(node) = known_nodes.get(id) {
                        view.insert(*id, node.clone());
                    } else {
                        unreachable!()
                    }
                }
            }
        }
    }
}

impl<A> Slot<A> {
    pub(crate) fn is_taken(&self) -> bool {
        match self {
            Slot::Taken(_) => true,
            Slot::Available => false,
        }
    }

    pub(crate) fn option(&self) -> Option<&A> {
        match self {
            Slot::Taken(a) => Some(a),
            Slot::Available => None,
        }
    }
}

impl Default for TopicView {
    fn default() -> Self {
        TopicView([Slot::Available; RINGS_MAX_VIEW_SIZE])
    }
}

impl Default for Rings {
    fn default() -> Self {
        Rings {
            neighbors: BTreeMap::default(),
        }
    }
}

impl TopicView {
    /// the degree of the [`TopicView`] is the number of available
    /// neighbors in this view.
    pub(crate) fn degree(&self) -> usize {
        self.0.iter().filter(|v| v.is_taken()).count()
    }

    /// this function will remove the node of the given [`Id`] from the
    /// [`TopicView`].
    ///
    /// This function is linear in time. But we expect the [`RINGS_MAX_VIEW_SIZE`]
    /// to be rather small so performance should not be an issue
    pub(crate) fn remove_node(&mut self, id: Id) -> bool {
        let id = Slot::Taken(id);
        for slot in self.0.iter_mut() {
            if slot == &id {
                *slot = Slot::Available;
                return true;
            }
        }
        false
    }

    /// check if the given node of identifier [`Id`] is present in this [`TopicView`].
    ///
    /// This function is linear in time. But we expect the [`RINGS_MAX_VIEW_SIZE`]
    /// to be rather small so performance should not be an issue
    pub(crate) fn contains(&self, id: Id) -> bool {
        let id = Slot::Taken(id);
        self.0.iter().any(|v| v == &id)
    }

    /// return an iterator over the successors in this [`TopicView`].
    ///
    /// In the case [`RINGS_NEIGHBOR_SUCCESSOR_SIZE`] allows more than
    /// one successor, the successors in this iterator are sorted from
    /// the closest to the node to the farthest (i.e. they are in the
    /// order of interest already.)
    ///
    /// This function returns the [`Slot`] as well, this is so we can
    /// modify the slot in the rings module when we need to add new items
    /// in the slot
    #[cfg(test)]
    fn successors(&self) -> impl Iterator<Item = &Slot<Id>> {
        self.0
            .iter()
            .skip(RINGS_NEIGHBOR_PREDECESSOR_SIZE)
            .take(RINGS_NEIGHBOR_SUCCESSOR_SIZE)
    }

    /// return a mutable iterator over the successors in this [`TopicView`].
    ///
    /// In the case [`RINGS_NEIGHBOR_SUCCESSOR_SIZE`] allows more than
    /// one successor, the successors in this iterator are sorted from
    /// the closest to the node to the farthest (i.e. they are in the
    /// order of interest already.)
    pub(crate) fn successors_mut(&mut self) -> impl Iterator<Item = &mut Slot<Id>> {
        self.0
            .iter_mut()
            .skip(RINGS_NEIGHBOR_PREDECESSOR_SIZE)
            .take(RINGS_NEIGHBOR_SUCCESSOR_SIZE)
    }

    /// return an iterator over the predecessors in this [`TopicView`].
    ///
    /// In the case [`RINGS_NEIGHBOR_PREDECESSOR_SIZE`] allows more than
    /// one predecessor, the predecessors in this iterator are sorted from
    /// the closest to the node to the farthest (i.e. they are in the
    /// order of interest already.)
    #[cfg(test)]
    fn predecessors(&self) -> impl Iterator<Item = &Slot<Id>> {
        self.0
            .iter()
            .rev()
            .skip(RINGS_NEIGHBOR_SUCCESSOR_SIZE)
            .take(RINGS_NEIGHBOR_PREDECESSOR_SIZE)
    }

    /// return a mutable iterator over the predecessors in this [`TopicView`].
    ///
    /// In the case [`RINGS_NEIGHBOR_PREDECESSOR_SIZE`] allows more than
    /// one predecessor, the predecessors in this iterator are sorted from
    /// the closest to the node to the farthest (i.e. they are in the
    /// order of interest already.)
    pub(crate) fn predecessors_mut(&mut self) -> impl Iterator<Item = &mut Slot<Id>> {
        self.0
            .iter_mut()
            .rev()
            .skip(RINGS_NEIGHBOR_SUCCESSOR_SIZE)
            .take(RINGS_NEIGHBOR_PREDECESSOR_SIZE)
    }

    /// iterator over every neighbors, not ordered by preferences (see
    /// [`predecessors`] and [`successors`] for preference ordered iterators)
    pub(crate) fn iter(&self) -> impl Iterator<Item = &Slot<Id>> {
        self.0.iter()
    }

    /// mutable iterator over every neighbors, not ordered by preferences (see
    /// [`predecessors_mut`] and [`successors_mut`] for preference ordered iterators)
    #[cfg(test)]
    fn iter_mut<'a>(&'a mut self) -> impl Iterator<Item = &'a mut Slot<Id>> {
        self.0.iter_mut()
    }
}

impl Rings {
    /// update the associated node's subscription priorities
    pub fn update_priorities(&mut self, self_node: &mut NodeData) {
        for (k, v) in self_node.subscriptions.iter_mut() {
            let degree = self
                .neighbors
                .entry(*k)
                .or_insert_with(TopicView::default)
                .degree();
            *v = match RINGS_MAX_VIEW_SIZE - degree {
                0 => InterestLevel::Low,
                1 => InterestLevel::Normal,
                _ => InterestLevel::High,
            };
        }
    }

    pub fn remove_node(&mut self, id: Id) -> bool {
        self.neighbors
            .iter_mut()
            .fold(false, |acc, (_, v)| acc || v.remove_node(id))
    }

    pub fn contains(&self, id: Id) -> bool {
        self.neighbors.iter().any(|(_, v)| v.contains(id))
    }

    /// return the size of the neighbor list
    pub fn degree(&self) -> usize {
        self.neighbors.iter().map(|(_, v)| v.degree()).sum()
    }

    // update the Rings view (neighbors for every topics) with the given new nodes
    fn update_view(&mut self, self_node: &NodeData, known_nodes: &BTreeMap<Id, NodeData>) {
        self.neighbors = BTreeMap::new();

        for topic in self_node.subscriptions.topics() {
            let view = select_best_nodes_for_topic(*self_node.id(), *topic, known_nodes);

            self.neighbors.insert(*topic, view);
        }
    }

    fn select_nodes_to_send(
        &self,
        self_node: &NodeData,
        gossip_node: &NodeData,
        known_nodes: &BTreeMap<Id, NodeData>,
    ) -> BTreeMap<Id, NodeData> {
        // these are the subscriptions in common between the gossip node and our nodes
        let common_topics: BTreeSet<Topic> = self_node
            .common_subscriptions(gossip_node)
            .cloned()
            .collect();

        // these are the subscribers in common between the 2 nodes
        let common_subscribers: BTreeSet<Id> = self_node
            .common_subscribers(&gossip_node)
            .cloned()
            .collect();

        // candidates are the one that are common subscribers sharing the same common
        // topics.
        let candidates: BTreeMap<Id, NodeData> = known_nodes
            .iter()
            .filter(|(k, v)| {
                common_subscribers.contains(k)
                    && v.subscriptions.topics().any(|k| common_topics.contains(k))
            })
            .map(|(k, v)| (*k, v.clone()))
            .collect();

        let mut nodes = BTreeMap::new();
        for topic in common_topics {
            let view = select_best_nodes_for_topic(*gossip_node.id(), topic, &candidates);

            for candidate in view.iter().filter_map(|v| v.option()) {
                if let Some(node) = candidates.get(candidate) {
                    nodes.insert(*candidate, node.clone());
                }
            }
        }

        nodes
    }
}

fn select_best_nodes_for_topic(
    other_id: Id,
    topic: Topic,
    candidates: &BTreeMap<Id, NodeData>,
) -> TopicView {
    use std::ops::Bound::{self, Excluded, Included};
    let mut view = TopicView::default();

    {
        // these are the predecessor
        let mut predecessor = view.predecessors_mut();
        for (id, candidate) in candidates
            .range((Included(Id::zero()), Excluded(other_id)))
            .rev()
        {
            if candidate.subscriptions.contains(topic) {
                if let Some(p) = predecessor.next() {
                    *p = Slot::Taken(*id);
                } else {
                    // we can stop as soon as we have all the necessary element
                    break;
                }
            }
        }
    }

    {
        // these are the successor of the topic
        let mut successor = view.successors_mut();
        for (id, candidate) in candidates
            .range((Excluded(other_id), Bound::Unbounded))
            .rev()
        {
            if candidate.subscriptions.contains(topic) {
                if let Some(p) = successor.next() {
                    *p = Slot::Taken(*id);
                } else {
                    // we can stop as soon as we have all the necessary element
                    break;
                }
            }
        }
    }

    view
}

#[cfg(test)]
mod test {
    use super::*;
    use quickcheck::{Arbitrary, Gen};

    impl<A: Arbitrary> Arbitrary for Slot<A> {
        fn arbitrary<G: Gen>(g: &mut G) -> Self {
            match Arbitrary::arbitrary(g) {
                Some(v) => Slot::Taken(v),
                None => Slot::Available,
            }
        }
    }

    impl Arbitrary for TopicView {
        fn arbitrary<G: Gen>(g: &mut G) -> Self {
            let mut view = TopicView::default();
            for v in view.iter_mut() {
                *v = Arbitrary::arbitrary(g);
            }
            view
        }
    }

    quickcheck! {
        // here we test that reading all the predecessor and then the successor
        // is similar to reading all the elements
        fn predecessors_and_successors_is_all(view: TopicView) -> bool {
            let all = view.iter().cloned();

            // as a reminder, the predecessors are iterated from the end, so we need
            // to reverse them to see the same view as the original
            let mut predecessors : Vec<_> = view.predecessors().cloned().collect();
            predecessors.reverse();
            let chained = predecessors.into_iter().chain(view.successors().cloned());

            all.zip(chained).all(|(k, v)| k == v)
        }
    }
}