Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref(projects): Jitter project state requests #277

Merged
merged 4 commits into from
Oct 25, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions common/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ struct Cache {
batch_interval: u32,
/// Interval for watching local cache override files in seconds.
file_interval: u32,
/// Jitter to be applied to cache timeouts, such as projects. `0` for no jitter, `1` for 100%
/// jitter.
cache_jitter: f64,
}

impl Default for Cache {
Expand All @@ -319,6 +322,7 @@ impl Default for Cache {
miss_expiry: 60, // 1 minute
batch_interval: 100, // 100ms
file_interval: 10, // 10 seconds
cache_jitter: 0.2, // 20%
}
}
}
Expand Down Expand Up @@ -777,6 +781,15 @@ impl Config {
Duration::from_secs(self.values.cache.file_interval.into())
}

/// Returns a jitter percentage that should be applied to cache timeouts.
pub fn cache_timeout_jitter(&self) -> f64 {
match self.values.cache.cache_jitter {
jitter if jitter > 1.0 => 1.0,
jitter if jitter < 0.0 => 0.0,
jitter => jitter,
}
}

/// Returns the maximum size of an event payload in bytes.
pub fn max_event_payload_size(&self) -> usize {
self.values.limits.max_event_payload_size.as_bytes() as usize
Expand Down
1 change: 1 addition & 0 deletions server/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ native-tls = { version = "0.2.3", optional = true }
num_cpus = "1.10.1"
parking_lot = "0.9.0"
r2d2 = { version = "0.8.5", optional = true }
rand = "0.7.2"
rdkafka = { version = "0.21.0", git = "https://github.com/fede1024/rust-rdkafka", optional = true, rev = "b1a391b3264ca8e0b0e053c6aec7a6db09af3131" }
redis = { version = "0.12.1-alpha.0", git = "https://github.com/mitsuhiko/redis-rs", optional = true, branch = "feature/cluster", features = ["cluster", "r2d2"] }
regex = "1.2.0"
Expand Down
16 changes: 14 additions & 2 deletions server/src/actors/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,10 @@ impl ProjectConfig {
}
}

fn project_state_seed() -> f64 {
rand::random()
}

/// The project state is a cached server state of a project.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down Expand Up @@ -277,6 +281,9 @@ pub struct ProjectState {
/// The organization id.
#[serde(default)]
pub organization_id: Option<u64>,
/// Random seed for jitter and related operations.
#[serde(skip, default = "project_state_seed")]
pub seed: f64,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the intent that the dice are rolled once per project over the uptime of a relay, not once per fetch?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intent is once per fetch (as stated in the description):

The seed changes with every fetch so that certain projects are not systematically discriminated.

... when Relay has long uptimes.

}

impl ProjectState {
Expand All @@ -291,6 +298,7 @@ impl ProjectState {
config: Default::default(),
rev: None,
organization_id: None,
seed: project_state_seed(),
}
}

Expand Down Expand Up @@ -334,11 +342,15 @@ impl ProjectState {

/// Returns whether this state is outdated and needs to be refetched.
pub fn outdated(&self, config: &Config) -> bool {
// Jitter is used to compute a random interval between [0; 2 * expiry_interval]
let jitter = config.cache_timeout_jitter();
let factor = jitter * (self.seed * 2.0);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Double check this when less tired. Math is hard.

seed is standard deviated, meaning it centers around 0.5. This makes seed * 2 center around 1.0, but jitter will make it center around something smaller. This actually needs to be something more like:

1 + jitter * (self.seed * 2.0 - 1)


SystemTime::from(self.last_fetch)
.elapsed()
.map(|e| match self.slug {
Some(_) => e > config.project_cache_expiry(),
None => e > config.cache_miss_expiry(),
Some(_) => e > config.project_cache_expiry().mul_f64(factor),
None => e > config.cache_miss_expiry().mul_f64(factor),
})
.unwrap_or(false)
}
Expand Down