edge_schema/schema/
locality.rs

1use serde::{Deserialize, Serialize};
2
3use super::Merge;
4
5#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
6pub struct CapabilityLocalityCountryV1 {
7    /// Country that the placement will be applied to
8    pub country: String,
9    /// Sets the density of this country
10    pub density: u64,
11}
12
13#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
14pub struct CapabilityLocalityContinentV1 {
15    /// Continent to set the density for
16    pub continent: String,
17    /// Sets the density of this continent
18    pub density: u64,
19}
20
21#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, schemars::JsonSchema)]
22pub struct CapabilityLocalityCityV1 {
23    /// City to set the density for
24    pub city: String,
25    /// Sets the density of this city
26    pub density: u64,
27}
28
29/// The locality configuration allows a workload author to place
30/// the workloads that respond to requests at different densities
31/// in different locations.
32///
33/// For example, if you wanted to place a single server in the US
34/// you would use this locality
35///
36/// ```yaml
37/// locality:
38///   region_density: 0
39///   country_density:
40///   - country: 'US'
41///     density: 1
42/// ```
43///
44/// Or perhaps you want to have the service available in most countries
45/// with a decent amount of capacity except for a particular city that is blacklisted
46///
47/// ```yaml
48/// locality:
49///   region_density: 8
50///   city_density:
51///   - city: 'London'
52///     density: 0
53/// ```
54///
55/// The seed value allows the author to randomly move the projection of
56/// where the workload runs onto a new set of servers. Simply changing
57/// the seed to a different value will randomly place it somewhere different
58/// than it was. This can be useful if there are noisy neighbors and you
59/// want to place yourself somewhere with different neighbors
60///
61/// ```yaml
62/// locality:
63///   seed: 12345
64/// ```
65#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug, Default, schemars::JsonSchema)]
66pub struct CapabilityLocalityV1 {
67    /// Sets the seed for a particular placement strategy
68    pub seed: Option<u64>,
69    /// Represents the placement density within all the regions unless it is overridden
70    /// by a country, continent or city locality
71    pub region_density: Option<u64>,
72    /// Sets the placement density of a particular country
73    pub country_densities: Vec<CapabilityLocalityCountryV1>,
74    /// Sets the placement density of a particular continent
75    pub continent_densities: Vec<CapabilityLocalityContinentV1>,
76    /// Sets the placement density of a particular city
77    pub city_densities: Vec<CapabilityLocalityCityV1>,
78}
79
80impl Merge for CapabilityLocalityV1 {
81    fn merge_extend(self, other: &Self) -> Self {
82        let mut ret = Self {
83            seed: self.seed,
84            region_density: self.region_density.merge_extend(&other.region_density),
85            country_densities: self.country_densities,
86            continent_densities: self.continent_densities,
87            city_densities: self.city_densities,
88        };
89        if let Some(seed) = other.seed {
90            ret.seed = Some(seed);
91        }
92        ret.country_densities
93            .extend(other.country_densities.clone());
94        ret.continent_densities
95            .extend(other.continent_densities.clone());
96        ret.city_densities.extend(other.city_densities.clone());
97        ret
98    }
99}