mongodb 3.6.0

The official MongoDB driver for 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
#[cfg(test)]
mod test;

use std::{
    collections::{HashMap, HashSet},
    fmt,
    ops::Deref,
    sync::Arc,
    time::Duration,
};

use super::TopologyDescription;
use crate::{
    error::{ErrorKind, Result},
    options::ServerAddress,
    sdam::{
        description::{
            server::{ServerDescription, ServerType},
            topology::TopologyType,
        },
        Server,
        ServerInfo,
    },
    selection_criteria::{ReadPreference, SelectionCriteria, TagSet},
};

const DEFAULT_LOCAL_THRESHOLD: Duration = Duration::from_millis(15);
pub(crate) const IDLE_WRITE_PERIOD: Duration = Duration::from_secs(10);

/// Struct encapsulating a selected server that handles the opcount accounting.
#[derive(Debug)]
pub(crate) struct SelectedServer {
    server: Arc<Server>,
}

impl SelectedServer {
    fn new(server: Arc<Server>) -> Self {
        server.increment_operation_count();
        Self { server }
    }
}

impl Deref for SelectedServer {
    type Target = Server;

    fn deref(&self) -> &Server {
        self.server.deref()
    }
}

impl Drop for SelectedServer {
    fn drop(&mut self) {
        self.server.decrement_operation_count();
    }
}

/// Attempt to select a server, returning None if no server could be selected
/// that matched the provided criteria.
pub(crate) fn attempt_to_select_server<'a>(
    criteria: &'a SelectionCriteria,
    topology_description: &'a TopologyDescription,
    servers: &'a HashMap<ServerAddress, Arc<Server>>,
    deprioritized: Option<&HashSet<ServerAddress>>,
) -> Result<Option<SelectedServer>> {
    if let Some(message) = topology_description.compatibility_error() {
        return Err(ErrorKind::ServerSelection {
            message: message.to_string(),
        }
        .into());
    }
    if topology_description.is_replica_set() {
        if let Some(max_staleness) = criteria.as_read_pref().and_then(|rp| rp.max_staleness()) {
            super::verify_max_staleness(max_staleness, topology_description.heartbeat_frequency())?;
        }
    }

    let mut servers_matching_criteria =
        topology_description.filter_servers_by_selection_criteria(criteria, deprioritized);

    topology_description.retain_servers_within_latency_window(&mut servers_matching_criteria);
    let in_window_servers = servers_matching_criteria
        .into_iter()
        .flat_map(|description| servers.get(&description.address))
        .collect::<Vec<_>>();

    let selected_server = if in_window_servers.len() < 2 {
        in_window_servers.first()
    } else {
        super::choose_n(&in_window_servers, 2).min_by_key(|s| s.operation_count())
    }
    .cloned();
    Ok(selected_server.map(|s| SelectedServer::new(s.clone())))
}

impl TopologyDescription {
    pub(crate) fn server_selection_timeout_error_message(
        &self,
        criteria: &SelectionCriteria,
    ) -> String {
        if self.has_available_servers() {
            format!(
                "Server selection timeout: None of the available servers suitable for criteria \
                 {criteria:?}. Topology: {self}"
            )
        } else {
            format!("Server selection timeout: No available servers. Topology: {self}")
        }
    }

    pub(crate) fn has_available_servers(&self) -> bool {
        self.servers.values().any(|server| server.is_available())
    }

    pub(crate) fn filter_servers_by_selection_criteria(
        &self,
        selection_criteria: &SelectionCriteria,
        deprioritized: Option<&HashSet<ServerAddress>>,
    ) -> Vec<&ServerDescription> {
        let mut servers_matching_criteria =
            self.filter_servers_by_selection_criteria_inner(selection_criteria, deprioritized);
        if servers_matching_criteria.is_empty() && deprioritized.is_some_and(|d| !d.is_empty()) {
            servers_matching_criteria =
                self.filter_servers_by_selection_criteria_inner(selection_criteria, None);
        }
        servers_matching_criteria
    }

    fn filter_servers_by_selection_criteria_inner(
        &self,
        selection_criteria: &SelectionCriteria,
        deprioritized: Option<&HashSet<ServerAddress>>,
    ) -> Vec<&ServerDescription> {
        let prioritized = self.servers.iter().filter_map(|(address, description)| {
            if deprioritized.is_none_or(|d| !d.contains(address)) {
                Some(description)
            } else {
                None
            }
        });

        match selection_criteria {
            SelectionCriteria::ReadPreference(read_preference) => match self.topology_type {
                TopologyType::Unknown => Vec::new(),
                TopologyType::Single | TopologyType::LoadBalanced => prioritized.collect(),
                TopologyType::Sharded => prioritized
                    .filter(|sd| sd.server_type == ServerType::Mongos)
                    .collect(),
                TopologyType::ReplicaSetWithPrimary | TopologyType::ReplicaSetNoPrimary => {
                    self.filter_servers_in_replica_set(prioritized, read_preference)
                }
            },
            SelectionCriteria::Predicate(ref predicate) => prioritized
                .filter(|s| {
                    // If we're direct-connected or connected to a standalone, ignore whether the
                    // single server in the topology is data-bearing.
                    (self.topology_type == TopologyType::Single || s.server_type.is_data_bearing())
                        && predicate(&ServerInfo::new_borrowed(s))
                })
                .collect(),
        }
    }

    fn filter_servers_in_replica_set<'a>(
        &self,
        servers: impl Iterator<Item = &'a ServerDescription> + Clone,
        read_preference: &ReadPreference,
    ) -> Vec<&'a ServerDescription> {
        match read_preference {
            ReadPreference::Primary => servers
                .filter(|sd| sd.server_type == ServerType::RsPrimary)
                .collect(),
            ReadPreference::Secondary { .. } => self.filter_servers_with_read_preference(
                servers,
                &[ServerType::RsSecondary],
                read_preference,
            ),
            ReadPreference::PrimaryPreferred { .. } => {
                let primary = servers
                    .clone()
                    .filter(|sd| sd.server_type == ServerType::RsPrimary)
                    .collect::<Vec<_>>();
                if !primary.is_empty() {
                    primary
                } else {
                    self.filter_servers_with_read_preference(
                        servers,
                        &[ServerType::RsSecondary],
                        read_preference,
                    )
                }
            }
            ReadPreference::SecondaryPreferred { .. } => {
                let primary = servers
                    .clone()
                    .filter(|sd| sd.server_type == ServerType::RsPrimary);
                let secondaries = self.filter_servers_with_read_preference(
                    servers,
                    &[ServerType::RsSecondary],
                    read_preference,
                );
                if !secondaries.is_empty() {
                    secondaries
                } else {
                    primary.collect()
                }
            }
            ReadPreference::Nearest { .. } => self.filter_servers_with_read_preference(
                servers,
                &[ServerType::RsPrimary, ServerType::RsSecondary],
                read_preference,
            ),
        }
    }

    pub(crate) fn retain_servers_within_latency_window(
        &self,
        suitable_servers: &mut Vec<&ServerDescription>,
    ) {
        let shortest_average_rtt = suitable_servers
            .iter()
            .filter_map(|server_desc| server_desc.average_round_trip_time)
            .fold(Option::<Duration>::None, |min, curr| match min {
                Some(prev) => Some(prev.min(curr)),
                None => Some(curr),
            });

        let local_threshold = self.local_threshold.unwrap_or(DEFAULT_LOCAL_THRESHOLD);

        let max_rtt_within_window = shortest_average_rtt
            .map(|rtt| rtt.checked_add(local_threshold).unwrap_or(Duration::MAX));

        suitable_servers.retain(move |server_desc| {
            if let Some(server_rtt) = server_desc.average_round_trip_time {
                // unwrap() is safe here because this server's avg rtt being Some indicates that
                // there exists a max rtt as well.
                server_rtt <= max_rtt_within_window.unwrap()
            } else {
                // SDAM isn't performed with a load balanced topology, so the load balancer won't
                // have an RTT. Instead, we just select it.
                matches!(server_desc.server_type, ServerType::LoadBalancer)
            }
        });
    }

    pub(crate) fn primary(&self) -> Option<&ServerDescription> {
        self.servers
            .values()
            .find(|sd| sd.server_type == ServerType::RsPrimary)
    }

    fn filter_servers_with_read_preference<'a>(
        &self,
        servers: impl Iterator<Item = &'a ServerDescription>,
        types: &[ServerType],
        read_preference: &ReadPreference,
    ) -> Vec<&'a ServerDescription> {
        let tag_sets = read_preference.tag_sets();
        let max_staleness = read_preference.max_staleness();

        let mut servers = servers
            .filter(|sd| types.contains(&sd.server_type))
            .collect();

        // We don't need to check for the Client's default max_staleness because it would be passed
        // in as part of the Client's default ReadPreference if none is specified for the operation.
        if let Some(max_staleness) = max_staleness {
            // According to the spec, max staleness <= 0 is the same as no max staleness.
            if max_staleness > Duration::from_secs(0) {
                self.filter_servers_by_max_staleness(&mut servers, max_staleness);
            }
        }

        if let Some(tag_sets) = tag_sets {
            filter_servers_by_tag_sets(&mut servers, tag_sets);
        }

        servers
    }

    fn filter_servers_by_max_staleness(
        &self,
        servers: &mut Vec<&ServerDescription>,
        max_staleness: Duration,
    ) {
        match self.primary() {
            Some(primary) => {
                self.filter_servers_by_max_staleness_with_primary(servers, primary, max_staleness)
            }
            None => self.filter_servers_by_max_staleness_without_primary(servers, max_staleness),
        };
    }

    fn filter_servers_by_max_staleness_with_primary(
        &self,
        servers: &mut Vec<&ServerDescription>,
        primary: &ServerDescription,
        max_staleness: Duration,
    ) {
        let max_staleness_ms = max_staleness.as_millis().try_into().unwrap_or(i64::MAX);

        servers.retain(|server| {
            let server_staleness = self.calculate_secondary_staleness_with_primary(server, primary);

            server_staleness
                .map(|staleness| staleness <= max_staleness_ms)
                .unwrap_or(false)
        })
    }

    fn filter_servers_by_max_staleness_without_primary(
        &self,
        servers: &mut Vec<&ServerDescription>,
        max_staleness: Duration,
    ) {
        let max_staleness = max_staleness.as_millis().try_into().unwrap_or(i64::MAX);
        let max_write_date = self
            .servers
            .values()
            .filter(|server| server.server_type == ServerType::RsSecondary)
            .filter_map(|server| {
                server
                    .last_write_date()
                    .ok()
                    .and_then(std::convert::identity)
            })
            .map(|last_write_date| last_write_date.timestamp_millis())
            .max();

        let secondary_max_write_date = match max_write_date {
            Some(max_write_date) => max_write_date,
            None => return,
        };

        servers.retain(|server| {
            let server_staleness = self
                .calculate_secondary_staleness_without_primary(server, secondary_max_write_date);

            server_staleness
                .map(|staleness| staleness <= max_staleness)
                .unwrap_or(false)
        })
    }

    fn calculate_secondary_staleness_with_primary(
        &self,
        secondary: &ServerDescription,
        primary: &ServerDescription,
    ) -> Option<i64> {
        let primary_last_update = primary.last_update_time?.timestamp_millis();
        let primary_last_write = primary.last_write_date().ok()??.timestamp_millis();

        let secondary_last_update = secondary.last_update_time?.timestamp_millis();
        let secondary_last_write = secondary.last_write_date().ok()??.timestamp_millis();

        let heartbeat_frequency = self
            .heartbeat_frequency()
            .as_millis()
            .try_into()
            .unwrap_or(i64::MAX);

        let staleness = (secondary_last_update - secondary_last_write)
            - (primary_last_update - primary_last_write)
            + heartbeat_frequency;

        Some(staleness)
    }

    fn calculate_secondary_staleness_without_primary(
        &self,
        secondary: &ServerDescription,
        max_last_write_date: i64,
    ) -> Option<i64> {
        let secondary_last_write = secondary.last_write_date().ok()??.timestamp_millis();
        let heartbeat_frequency = self
            .heartbeat_frequency()
            .as_millis()
            .try_into()
            .unwrap_or(i64::MAX);

        let staleness = max_last_write_date - secondary_last_write + heartbeat_frequency;
        Some(staleness)
    }
}

impl fmt::Display for TopologyDescription {
    fn fmt(&self, f: &mut fmt::Formatter) -> std::result::Result<(), fmt::Error> {
        write!(f, "{{ Type: {}", self.topology_type)?;

        if let Some(ref set_name) = self.set_name {
            write!(f, ", Set Name: {set_name}")?;
        }

        if let Some(max_set_version) = self.max_set_version {
            write!(f, ", Max Set Version: {max_set_version}")?;
        }

        if let Some(max_election_id) = self.max_election_id {
            write!(f, ", Max Election ID: {max_election_id}")?;
        }

        if let Some(ref compatibility_error) = self.compatibility_error {
            write!(f, ", Compatibility Error: {compatibility_error}")?;
        }

        if !self.servers.is_empty() {
            write!(f, ", Servers: [ ")?;
            let mut iter = self.servers.values();
            if let Some(server) = iter.next() {
                write!(f, "{}", ServerInfo::new_borrowed(server))?;
            }
            for server in iter {
                write!(f, ", {}", ServerInfo::new_borrowed(server))?;
            }
            write!(f, " ]")?;
        }

        write!(f, " }}")
    }
}

fn filter_servers_by_tag_sets(servers: &mut Vec<&ServerDescription>, tag_sets: &[TagSet]) {
    if tag_sets.is_empty() {
        return;
    }

    for tag_set in tag_sets {
        let matches_tag_set = |server: &&ServerDescription| server.matches_tag_set(tag_set);

        if servers.iter().any(matches_tag_set) {
            servers.retain(matches_tag_set);

            return;
        }
    }

    servers.clear();
}