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
use std::{collections::HashMap, sync::Arc, time::Duration};

use derivative::Derivative;
use serde::{de::Error as SerdeError, Deserialize, Deserializer, Serialize};
use typed_builder::TypedBuilder;

use crate::{
    bson::doc,
    error::{ErrorKind, Result},
    options::ServerAddress,
    sdam::public::ServerInfo,
    serde_util,
};

/// Describes which servers are suitable for a given operation.
#[derive(Clone, Derivative, derive_more::Display)]
#[derivative(Debug)]
#[non_exhaustive]
pub enum SelectionCriteria {
    /// A read preference that describes the suitable servers based on the server type, max
    /// staleness, and server tags.
    ///
    /// See the documentation [here](https://www.mongodb.com/docs/manual/core/read-preference/) for more details.
    #[display(fmt = "ReadPreference {}", _0)]
    ReadPreference(ReadPreference),

    /// A predicate used to filter servers that are considered suitable. A `server` will be
    /// considered suitable by a `predicate` if `predicate(server)` returns true.
    #[display(fmt = "Custom predicate")]
    Predicate(#[derivative(Debug = "ignore")] Predicate),
}

impl PartialEq for SelectionCriteria {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::ReadPreference(r1), Self::ReadPreference(r2)) => r1 == r2,
            _ => false,
        }
    }
}

impl From<ReadPreference> for SelectionCriteria {
    fn from(read_pref: ReadPreference) -> Self {
        Self::ReadPreference(read_pref)
    }
}

impl SelectionCriteria {
    pub(crate) fn as_read_pref(&self) -> Option<&ReadPreference> {
        match self {
            Self::ReadPreference(ref read_pref) => Some(read_pref),
            Self::Predicate(..) => None,
        }
    }

    #[cfg(test)]
    pub(crate) fn as_predicate(&self) -> Option<&Predicate> {
        match self {
            Self::Predicate(ref p) => Some(p),
            _ => None,
        }
    }

    #[cfg(test)]
    pub(crate) fn is_read_pref_primary(&self) -> bool {
        matches!(self, Self::ReadPreference(ReadPreference::Primary))
    }

    pub(crate) fn from_address(address: ServerAddress) -> Self {
        SelectionCriteria::Predicate(Arc::new(move |server| server.address() == &address))
    }

    #[cfg(test)]
    pub(crate) fn serialize_for_client_options<S>(
        selection_criteria: &Option<SelectionCriteria>,
        serializer: S,
    ) -> std::result::Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        match selection_criteria {
            Some(SelectionCriteria::ReadPreference(pref)) => {
                ReadPreference::serialize_for_client_options(pref, serializer)
            }
            _ => serializer.serialize_none(),
        }
    }
}

impl<'de> Deserialize<'de> for SelectionCriteria {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        Ok(SelectionCriteria::ReadPreference(
            ReadPreference::deserialize(deserializer)?,
        ))
    }
}

/// A predicate used to filter servers that are considered suitable.
pub type Predicate = Arc<dyn Send + Sync + Fn(&ServerInfo) -> bool>;

/// Specifies how the driver should route a read operation to members of a replica set.
///
/// If applicable, `tag_sets` can be used to target specific nodes in a replica set, and
/// `max_staleness` specifies the maximum lag behind the primary that a secondary can be to remain
/// eligible for the operation. The max staleness value maps to the `maxStalenessSeconds` MongoDB
/// option and will be sent to the server as an integer number of seconds.
///
/// See the [MongoDB docs](https://www.mongodb.com/docs/manual/core/read-preference) for more details.
#[allow(missing_docs)]
#[derive(Clone, Debug, PartialEq)]
pub enum ReadPreference {
    /// Only route this operation to the primary.
    Primary,

    /// Only route this operation to a secondary.
    Secondary { options: ReadPreferenceOptions },

    /// Route this operation to the primary if it's available, but fall back to the secondaries if
    /// not.
    PrimaryPreferred { options: ReadPreferenceOptions },

    /// Route this operation to a secondary if one is available, but fall back to the primary if
    /// not.
    SecondaryPreferred { options: ReadPreferenceOptions },

    /// Route this operation to the node with the least network latency regardless of whether it's
    /// the primary or a secondary.
    Nearest { options: ReadPreferenceOptions },
}

impl std::fmt::Display for ReadPreference {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{{ Mode: ")?;
        let opts_ref = match self {
            ReadPreference::Primary => {
                write!(f, "Primary")?;
                None
            }
            ReadPreference::Secondary { options } => {
                write!(f, "Secondary")?;
                Some(options)
            }
            ReadPreference::PrimaryPreferred { options } => {
                write!(f, "PrimaryPreferred")?;
                Some(options)
            }
            ReadPreference::SecondaryPreferred { options } => {
                write!(f, "SecondaryPreferred")?;
                Some(options)
            }
            ReadPreference::Nearest { options } => {
                write!(f, "Nearest")?;
                Some(options)
            }
        };
        if let Some(opts) = opts_ref {
            if !opts.is_default() {
                if let Some(ref tag_sets) = opts.tag_sets {
                    write!(f, ", Tag Sets: {:?}", tag_sets)?;
                }
                if let Some(ref max_staleness) = opts.max_staleness {
                    write!(f, ", Max Staleness: {:?}", max_staleness)?;
                }
                if let Some(ref hedge) = opts.hedge {
                    write!(f, ", Hedge: {}", hedge.enabled)?;
                }
            }
        }
        write!(f, " }}")
    }
}

impl<'de> Deserialize<'de> for ReadPreference {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        #[derive(Serialize, Deserialize)]
        #[serde(rename_all = "camelCase", deny_unknown_fields)]
        struct ReadPreferenceHelper {
            mode: String,
            #[serde(flatten)]
            options: ReadPreferenceOptions,
        }
        let preference = ReadPreferenceHelper::deserialize(deserializer)?;
        match preference.mode.to_ascii_lowercase().as_str() {
            "primary" => {
                if !preference.options.is_default() {
                    return Err(D::Error::custom(&format!(
                        "no options can be specified with read preference mode = primary, but got \
                         {:?}",
                        preference.options
                    )));
                }
                Ok(ReadPreference::Primary)
            }
            "secondary" => Ok(ReadPreference::Secondary {
                options: preference.options,
            }),
            "primarypreferred" => Ok(ReadPreference::PrimaryPreferred {
                options: preference.options,
            }),
            "secondarypreferred" => Ok(ReadPreference::SecondaryPreferred {
                options: preference.options,
            }),
            "nearest" => Ok(ReadPreference::Nearest {
                options: preference.options,
            }),
            other => Err(D::Error::custom(format!(
                "Unknown read preference mode: {}",
                other
            ))),
        }
    }
}

impl Serialize for ReadPreference {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        #[serde_with::skip_serializing_none]
        #[derive(Serialize)]
        #[serde(rename_all = "camelCase", deny_unknown_fields)]
        struct ReadPreferenceHelper<'a> {
            mode: &'static str,
            #[serde(flatten)]
            options: Option<&'a ReadPreferenceOptions>,
        }
        let helper = match self {
            ReadPreference::Primary => ReadPreferenceHelper {
                mode: "primary",
                options: None,
            },
            ReadPreference::PrimaryPreferred { options } => ReadPreferenceHelper {
                mode: "primaryPreferred",
                options: Some(options),
            },
            ReadPreference::Secondary { options } => ReadPreferenceHelper {
                mode: "secondary",
                options: Some(options),
            },
            ReadPreference::SecondaryPreferred { options } => ReadPreferenceHelper {
                mode: "secondaryPreferred",
                options: Some(options),
            },
            ReadPreference::Nearest { options } => ReadPreferenceHelper {
                mode: "nearest",
                options: Some(options),
            },
        };

        helper.serialize(serializer)
    }
}

/// Specifies read preference options for non-primary read preferences.
#[serde_with::skip_serializing_none]
#[derive(Clone, Debug, Default, Serialize, Deserialize, PartialEq, TypedBuilder)]
#[builder(field_defaults(default, setter(into)))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ReadPreferenceOptions {
    /// Specifies which replica set members should be considered for operations. Each tag set will
    /// be checked in order until one or more servers is found with each tag in the set.
    pub tag_sets: Option<Vec<TagSet>>,

    /// Specifies the maximum amount of lag behind the primary that a secondary can be to be
    /// considered for the given operation. Any secondaries lagging behind more than
    /// `max_staleness` will not be considered for the operation.
    ///
    /// `max_staleness` must be at least 90 seconds. If a `max_staleness` less than 90 seconds is
    /// specified for an operation, the operation will return an error.
    #[serde(
        rename = "maxStalenessSeconds",
        default,
        with = "serde_util::duration_option_as_int_seconds"
    )]
    pub max_staleness: Option<Duration>,

    /// Specifies hedging behavior for reads. These options only apply to sharded clusters on
    /// servers that are at least version 4.4. Note that hedged reads are automatically enabled for
    /// read preference mode "nearest".
    ///
    /// See the [MongoDB docs](https://www.mongodb.com/docs/manual/core/read-preference-hedge-option/) for more details.
    pub hedge: Option<HedgedReadOptions>,
}

impl ReadPreferenceOptions {
    pub(crate) fn is_default(&self) -> bool {
        self.hedge.is_none()
            && self.max_staleness.is_none()
            && self
                .tag_sets
                .as_ref()
                .map(|ts| ts.is_empty() || ts[..] == [HashMap::default()])
                .unwrap_or(true)
    }
}

/// Specifies hedging behavior for reads.
///
/// See the [MongoDB docs](https://www.mongodb.com/docs/manual/core/read-preference-hedge-option/) for more details.
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, TypedBuilder)]
#[non_exhaustive]
pub struct HedgedReadOptions {
    /// Whether or not to allow reads from a sharded cluster to be "hedged" across two replica
    /// set members per shard, with the results from the first response received back from either
    /// being returned.
    pub enabled: bool,
}

impl HedgedReadOptions {
    /// Creates a new `HedgedReadOptions` with the given value for `enabled`.
    pub fn with_enabled(enabled: bool) -> Self {
        Self { enabled }
    }
}

impl ReadPreference {
    pub(crate) fn max_staleness(&self) -> Option<Duration> {
        match self {
            ReadPreference::Primary => None,
            ReadPreference::Secondary { ref options }
            | ReadPreference::PrimaryPreferred { ref options }
            | ReadPreference::SecondaryPreferred { ref options }
            | ReadPreference::Nearest { ref options } => options.max_staleness,
        }
    }

    pub(crate) fn with_tags(mut self, tag_sets: Vec<TagSet>) -> Result<Self> {
        let options = match self {
            ReadPreference::Primary => {
                return Err(ErrorKind::InvalidArgument {
                    message: "read preference tags can only be specified when a non-primary mode \
                              is specified"
                        .to_string(),
                }
                .into());
            }
            ReadPreference::Secondary { ref mut options } => options,
            ReadPreference::PrimaryPreferred { ref mut options } => options,
            ReadPreference::SecondaryPreferred { ref mut options } => options,
            ReadPreference::Nearest { ref mut options } => options,
        };

        options.tag_sets = Some(tag_sets);

        Ok(self)
    }

    pub(crate) fn with_max_staleness(mut self, max_staleness: Duration) -> Result<Self> {
        let options = match self {
            ReadPreference::Primary => {
                return Err(ErrorKind::InvalidArgument {
                    message: "max staleness can only be specified when a non-primary mode is \
                              specified"
                        .to_string(),
                }
                .into());
            }
            ReadPreference::Secondary { ref mut options } => options,
            ReadPreference::PrimaryPreferred { ref mut options } => options,
            ReadPreference::SecondaryPreferred { ref mut options } => options,
            ReadPreference::Nearest { ref mut options } => options,
        };

        options.max_staleness = Some(max_staleness);

        Ok(self)
    }

    #[cfg(test)]
    pub(crate) fn serialize_for_client_options<S>(
        read_preference: &ReadPreference,
        serializer: S,
    ) -> std::result::Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        #[derive(serde::Serialize)]
        struct ReadPreferenceHelper<'a> {
            readpreference: &'a str,

            readpreferencetags: Option<&'a Vec<HashMap<String, String>>>,

            #[serde(serialize_with = "serde_util::duration_option_as_int_seconds::serialize")]
            maxstalenessseconds: Option<Duration>,
        }

        let state = match read_preference {
            ReadPreference::Primary => ReadPreferenceHelper {
                readpreference: "primary",
                readpreferencetags: None,
                maxstalenessseconds: None,
            },
            ReadPreference::PrimaryPreferred { options } => ReadPreferenceHelper {
                readpreference: "primaryPreferred",
                readpreferencetags: options.tag_sets.as_ref(),
                maxstalenessseconds: options.max_staleness,
            },
            ReadPreference::Secondary { options } => ReadPreferenceHelper {
                readpreference: "secondary",
                readpreferencetags: options.tag_sets.as_ref(),
                maxstalenessseconds: options.max_staleness,
            },
            ReadPreference::SecondaryPreferred { options } => ReadPreferenceHelper {
                readpreference: "secondaryPreferred",
                readpreferencetags: options.tag_sets.as_ref(),
                maxstalenessseconds: options.max_staleness,
            },
            ReadPreference::Nearest { options } => ReadPreferenceHelper {
                readpreference: "nearest",
                readpreferencetags: options.tag_sets.as_ref(),
                maxstalenessseconds: options.max_staleness,
            },
        };

        state.serialize(serializer)
    }
}

/// A read preference tag set. See the documentation [here](https://www.mongodb.com/docs/manual/tutorial/configure-replica-set-tag-sets/) for more details.
pub type TagSet = HashMap<String, String>;

#[cfg(test)]
mod test {
    use super::{HedgedReadOptions, ReadPreference, ReadPreferenceOptions};
    use crate::bson::doc;

    #[test]
    fn hedged_read_included_in_document() {
        let options = ReadPreferenceOptions::builder()
            .hedge(HedgedReadOptions { enabled: true })
            .build();

        let read_pref = ReadPreference::Secondary { options };
        let doc = bson::to_document(&read_pref).unwrap();

        assert_eq!(
            doc,
            doc! { "mode": "secondary", "hedge": { "enabled": true } }
        );
    }
}