sofie-flags 0.4.0

Feature flags: declared as constants, stored in the database, cached in process and evaluated per request.
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
use serde_json::{Value, json};
use sofie_core::{Error, Fnv1a64, Result, Uuid};

/// A request without a principal, and a job, are in only at 100 percent.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Rule {
    pub on: bool,
    pub environments: Vec<String>,
    pub areas: Vec<String>,
    pub principals: Vec<Uuid>,
    pub percentage: u8,
    pub variants: Vec<(String, u32)>,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct Subject {
    pub area: Option<&'static str>,
    pub principal: Option<Uuid>,
}

impl Rule {
    #[must_use]
    pub fn off() -> Self {
        Self {
            on: false,
            environments: Vec::new(),
            areas: Vec::new(),
            principals: Vec::new(),
            percentage: 100,
            variants: Vec::new(),
        }
    }

    #[must_use]
    pub fn everyone() -> Self {
        Self {
            on: true,
            ..Self::off()
        }
    }

    #[must_use]
    pub fn is_settled(&self) -> bool {
        let one_variant = self
            .variants
            .iter()
            .filter(|(_, weight)| *weight > 0)
            .count()
            <= 1;
        !self.on
            || (self.environments.is_empty()
                && self.areas.is_empty()
                && self.percentage == 100
                && one_variant)
    }

    pub(crate) fn is_on(&self, name: &str, environment: &str, subject: Subject) -> bool {
        if !self.applies(environment, subject.area) {
            return false;
        }
        if self.percentage >= 100 {
            return true;
        }
        subject.principal.is_some_and(|principal| {
            self.principals.contains(&principal)
                || bucket(&[name], principal, 100) < u64::from(self.percentage)
        })
    }

    /// On for at least one principal of the area, whoever asks.
    pub(crate) fn is_on_for_someone(&self, environment: &str, area: Option<&str>) -> bool {
        self.applies(environment, area) && (self.percentage > 0 || !self.principals.is_empty())
    }

    fn applies(&self, environment: &str, area: Option<&str>) -> bool {
        let listed =
            |list: &[String], value: &str| list.is_empty() || list.iter().any(|item| item == value);
        self.on
            && listed(&self.environments, environment)
            && (self.areas.is_empty() || area.is_some_and(|area| listed(&self.areas, area)))
    }

    pub(crate) fn variant(&self, name: &str, subject: Subject) -> Option<&str> {
        let weighted = self.variants.iter().filter(|(_, weight)| *weight > 0);
        let total: u64 = weighted.clone().map(|(_, weight)| u64::from(*weight)).sum();
        let Some(principal) = subject.principal else {
            return weighted.map(|(variant, _)| variant.as_str()).next();
        };
        let mut point = bucket(&[name, "/variant"], principal, total.max(1));
        for (variant, weight) in weighted {
            match point.checked_sub(u64::from(*weight)) {
                Some(rest) => point = rest,
                None => return Some(variant),
            }
        }
        None
    }

    pub(crate) fn to_json(&self) -> String {
        let principals: Vec<String> = self.principals.iter().map(ToString::to_string).collect();
        json!({
            "on": self.on,
            "environments": self.environments,
            "areas": self.areas,
            "principals": principals,
            "percentage": self.percentage,
            "variants": self.variants,
        })
        .to_string()
    }

    pub(crate) fn from_json(name: &str, text: &str) -> Result<Self> {
        let malformed = |what: &str| {
            Error::internal_message(format!("the stored rule of flag `{name}` has {what}"))
        };
        let value: Value = serde_json::from_str(text).map_err(|error| {
            Error::internal(format!("reading the stored rule of flag `{name}`"), error)
        })?;
        let texts = |key: &str| -> Result<Vec<String>> {
            value[key]
                .as_array()
                .ok_or_else(|| malformed(&format!("no list `{key}`")))?
                .iter()
                .map(|item| {
                    item.as_str()
                        .map(str::to_owned)
                        .ok_or_else(|| malformed(&format!("a non-text entry in `{key}`")))
                })
                .collect()
        };
        let principals = texts("principals")?
            .iter()
            .map(|text| {
                text.parse()
                    .map_err(|_| malformed("a principal that is not an id"))
            })
            .collect::<Result<_>>()?;
        let rule = Self {
            on: value["on"]
                .as_bool()
                .ok_or_else(|| malformed("no switch `on`"))?,
            environments: texts("environments")?,
            areas: texts("areas")?,
            principals,
            percentage: value["percentage"]
                .as_u64()
                .and_then(|percentage| u8::try_from(percentage).ok())
                .ok_or_else(|| malformed("no `percentage`"))?,
            variants: variants_of(&value["variants"])
                .ok_or_else(|| malformed("malformed `variants`"))?,
        };
        rule.validate(name)?;
        Ok(rule)
    }

    pub fn validate(&self, name: &str) -> Result<()> {
        if self.percentage > 100 {
            return Err(Error::invalid(
                "percentage",
                format!("of flag `{name}` must be between 0 and 100"),
            ));
        }
        refuse_malformed_entries("environments", &self.environments)?;
        refuse_malformed_entries("areas", &self.areas)?;
        let variants: Vec<String> = self
            .variants
            .iter()
            .map(|(variant, _)| variant.clone())
            .collect();
        refuse_malformed_entries("variants", &variants)
    }
}

/// An entry is matched against a name the code declares, so one that could never match is refused.
fn refuse_malformed_entries(field: &str, entries: &[String]) -> Result<()> {
    let named = |entry: &String| {
        !entry.is_empty()
            && entry
                .chars()
                .all(|character| character.is_ascii_alphanumeric() || "_-".contains(character))
    };
    if let Some(malformed) = entries.iter().find(|entry| !named(entry)) {
        return Err(Error::invalid(
            field,
            format!("`{malformed}` is not a name of letters, digits, `_` and `-`"),
        ));
    }
    let repeated = entries
        .iter()
        .enumerate()
        .find(|(index, entry)| entries[..*index].contains(entry));
    if let Some((_, entry)) = repeated {
        return Err(Error::invalid(field, format!("lists `{entry}` twice")));
    }
    Ok(())
}

fn variants_of(value: &Value) -> Option<Vec<(String, u32)>> {
    value
        .as_array()?
        .iter()
        .map(|pair| {
            let name = pair.get(0)?.as_str()?.to_owned();
            let weight = u32::try_from(pair.get(1)?.as_u64()?).ok()?;
            Some((name, weight))
        })
        .collect()
}

/// The parts hash as their concatenation, so splitting a name never moves anyone's bucket.
fn bucket(name: &[&str], principal: Uuid, range: u64) -> u64 {
    let hash = name
        .iter()
        .fold(Fnv1a64::new(), |hash, part| hash.write(part.as_bytes()))
        .write(&[0])
        .write(principal.as_bytes())
        .finish();
    mixed(hash) % range
}

/// FNV-1a leaves its low bits a function of the input bytes' parity, so two flags' buckets
/// would decide each other; splitmix64's finalizer spreads every input bit over the output.
fn mixed(mut hash: u64) -> u64 {
    hash ^= hash >> 30;
    hash = hash.wrapping_mul(0xbf58_476d_1ce4_e5b9);
    hash ^= hash >> 27;
    hash = hash.wrapping_mul(0x94d0_49bb_1331_11eb);
    hash ^ (hash >> 31)
}

#[cfg(test)]
mod tests {
    use super::*;

    fn principal(n: u128) -> Uuid {
        Uuid::from_u128(n)
    }

    fn someone(n: u128) -> Subject {
        Subject {
            area: Some("member"),
            principal: Some(principal(n)),
        }
    }

    #[test]
    fn off_is_off_and_everyone_is_on_for_anyone() {
        assert!(!Rule::off().is_on("a.b", "production", someone(1)));
        assert!(Rule::everyone().is_on("a.b", "production", Subject::default()));
    }

    #[test]
    fn environments_and_areas_narrow_it() {
        let rule = Rule {
            environments: vec!["staging".to_owned()],
            areas: vec!["console".to_owned()],
            ..Rule::everyone()
        };
        let console = Subject {
            area: Some("console"),
            principal: None,
        };
        assert!(rule.is_on("a.b", "staging", console));
        assert!(!rule.is_on("a.b", "production", console));
        assert!(!rule.is_on("a.b", "staging", someone(1)));
        assert!(!rule.is_on("a.b", "staging", Subject::default()));
    }

    #[test]
    fn a_partial_rollout_is_on_for_someone_though_off_for_a_caller_without_a_principal() {
        let listed = Rule {
            principals: vec![principal(1)],
            percentage: 0,
            ..Rule::everyone()
        };
        let partial = Rule {
            percentage: 10,
            ..Rule::everyone()
        };
        let nobody = Rule {
            percentage: 0,
            ..Rule::everyone()
        };
        for rule in [&listed, &partial] {
            assert!(!rule.is_on("a.b", "production", Subject::default()));
            assert!(rule.is_on_for_someone("production", Some("member")));
        }
        assert!(!nobody.is_on_for_someone("production", Some("member")));
        assert!(!Rule::off().is_on_for_someone("production", Some("member")));
    }

    fn split() -> Rule {
        Rule {
            variants: vec![("a".to_owned(), 1), ("b".to_owned(), 1)],
            ..Rule::everyone()
        }
    }

    #[test]
    fn two_flags_split_principals_independently() {
        let (first, second) = (split(), split());
        let principals =
            (1..=4_000_u128).map(|n| n.wrapping_mul(0x9e37_79b9_7f4a_7c15_f39c_c060_5ced_c835));
        let same = principals
            .filter(|n| {
                let subject = someone(*n);
                first.variant("a.first", subject) == second.variant("a.second", subject)
            })
            .count();
        assert!((1_700..2_300).contains(&same), "{same}");
    }

    #[test]
    fn a_rollout_does_not_choose_its_principals_variant() {
        let rule = Rule {
            percentage: 10,
            ..split()
        };
        let rolled_in: Vec<Subject> = (1..=20_000_u128)
            .map(someone)
            .filter(|subject| rule.is_on("a.b", "x", *subject))
            .collect();
        let first_arm = rolled_in
            .iter()
            .filter(|subject| rule.variant("a.b", **subject) == Some("a"))
            .count();
        let share = first_arm * 100 / rolled_in.len();
        assert!(
            (40..=60).contains(&share),
            "{share}% of {}",
            rolled_in.len()
        );
    }

    #[test]
    fn a_percentage_is_stable_per_principal_and_near_its_share() {
        let rule = Rule {
            percentage: 30,
            ..Rule::everyone()
        };
        let on = (0..10_000)
            .filter(|n| rule.is_on("a.b", "x", someone(*n)))
            .count();
        assert!((2_700..3_300).contains(&on), "{on}");
        let first: Vec<bool> = (0..50)
            .map(|n| rule.is_on("a.b", "x", someone(n)))
            .collect();
        let again: Vec<bool> = (0..50)
            .map(|n| rule.is_on("a.b", "x", someone(n)))
            .collect();
        assert_eq!(first, again);
        assert!(!rule.is_on("a.b", "x", Subject::default()));
    }

    #[test]
    fn listed_principals_are_in_whatever_the_percentage() {
        let rule = Rule {
            percentage: 0,
            principals: vec![principal(7)],
            ..Rule::everyone()
        };
        assert!(rule.is_on("a.b", "x", someone(7)));
        assert!(!rule.is_on("a.b", "x", someone(8)));
    }

    #[test]
    fn variants_split_by_weight_and_default_to_the_first() {
        let rule = Rule {
            variants: vec![("classic".to_owned(), 1), ("compact".to_owned(), 3)],
            ..Rule::everyone()
        };
        let compact = (0..4_000)
            .filter(|n| rule.variant("a.b", someone(*n)) == Some("compact"))
            .count();
        assert!((2_800..3_200).contains(&compact), "{compact}");
        assert_eq!(rule.variant("a.b", Subject::default()), Some("classic"));
    }

    #[test]
    fn a_rule_survives_its_stored_form() {
        let rule = Rule {
            environments: vec!["production".to_owned()],
            areas: vec!["member".to_owned()],
            principals: vec![principal(3)],
            percentage: 25,
            variants: vec![("a".to_owned(), 1)],
            ..Rule::everyone()
        };
        assert_eq!(Rule::from_json("a.b", &rule.to_json()).unwrap(), rule);
        assert!(Rule::from_json("a.b", "{\"on\":true}").is_err());
    }

    #[test]
    fn a_target_that_could_never_match_is_refused() {
        for refused in [
            Rule {
                areas: vec![String::new()],
                ..Rule::everyone()
            },
            Rule {
                environments: vec!["pro duction".to_owned()],
                ..Rule::everyone()
            },
            Rule {
                variants: vec![("a".to_owned(), 1), ("a".to_owned(), 2)],
                ..Rule::everyone()
            },
        ] {
            assert!(refused.validate("a.b").is_err(), "{refused:?}");
        }
    }

    #[test]
    fn settled_means_the_same_for_everyone() {
        assert!(Rule::off().is_settled());
        assert!(Rule::everyone().is_settled());
        let partial = Rule {
            percentage: 50,
            ..Rule::everyone()
        };
        assert!(!partial.is_settled());
    }
}