Skip to content

Commit fd63d88

Browse files
committed
graph, graphql: Configure window and bin sizes for stats through environment
1 parent b1cc6c7 commit fd63d88

File tree

4 files changed

+52
-4
lines changed

4 files changed

+52
-4
lines changed

docs/environment-variables.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,3 +112,8 @@ those.
112112
Due to implementation details, this value may not be strictly adhered to. Defaults to 10.
113113
- `GRAPH_LOG_POI_EVENTS`: Logs Proof of Indexing events deterministically.
114114
This may be useful for debugging.
115+
- `GRAPH_LOAD_WINDOW_SIZE`, `GRAPH_LOAD_BIN_SIZE`: Load can be
116+
automatically throttled if load measurements over a time period of
117+
`GRAPH_LOAD_WINDOW_SIZE` seconds exceed a threshold. Measurements within
118+
each window are binned into bins of `GRAPH_LOAD_BIN_SIZE` seconds. The
119+
variables default to 300s and 1s

graph/src/data/graphql/effort.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::collections::HashMap;
44
use std::sync::{Arc, RwLock};
55
use std::time::{Duration, Instant};
66

7-
use crate::util::stats::MovingStats;
7+
use crate::util::stats::{MovingStats, BIN_SIZE, WINDOW_SIZE};
88

99
pub struct QueryEffort {
1010
inner: Arc<RwLock<QueryEffortInner>>,
@@ -19,6 +19,14 @@ struct QueryEffortInner {
1919
total: MovingStats,
2020
}
2121

22+
/// Create a `QueryEffort` that uses the window and bin sizes configured in
23+
/// the environment
24+
impl Default for QueryEffort {
25+
fn default() -> Self {
26+
Self::new(*WINDOW_SIZE, *BIN_SIZE)
27+
}
28+
}
29+
2230
impl QueryEffort {
2331
pub fn new(window_size: Duration, bin_size: Duration) -> Self {
2432
Self {

graph/src/util/stats.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,35 @@
11
use std::collections::VecDeque;
2+
use std::env;
3+
use std::str::FromStr;
24
use std::time::{Duration, Instant};
35

6+
use lazy_static::lazy_static;
7+
8+
lazy_static! {
9+
pub static ref WINDOW_SIZE: Duration = {
10+
let window_size = env::var("GRAPH_LOAD_WINDOW_SIZE")
11+
.ok()
12+
.map(|s| {
13+
u64::from_str(&s).unwrap_or_else(|_| {
14+
panic!("GRAPH_LOAD_WINDOW_SIZE must be a number, but is `{}`", s)
15+
})
16+
})
17+
.unwrap_or(300);
18+
Duration::from_secs(window_size)
19+
};
20+
pub static ref BIN_SIZE: Duration = {
21+
let bin_size = env::var("GRAPH_LOAD_BIN_SIZE")
22+
.ok()
23+
.map(|s| {
24+
u64::from_str(&s).unwrap_or_else(|_| {
25+
panic!("GRAPH_LOAD_BIN_SIZE must be a number but is `{}`", s)
26+
})
27+
})
28+
.unwrap_or(1);
29+
Duration::from_secs(bin_size)
30+
};
31+
}
32+
433
/// One bin of durations. The bin starts at time `start`, and we've added `count`
534
/// entries to it whose durations add up to `duration`
635
struct Bin {
@@ -61,6 +90,14 @@ pub struct MovingStats {
6190
total: Bin,
6291
}
6392

93+
/// Create `MovingStats` that use the window and bin sizes configured in
94+
/// the environment
95+
impl Default for MovingStats {
96+
fn default() -> Self {
97+
Self::new(*WINDOW_SIZE, *BIN_SIZE)
98+
}
99+
}
100+
64101
impl MovingStats {
65102
pub fn new(window_size: Duration, bin_size: Duration) -> Self {
66103
let capacity = if bin_size.as_millis() > 0 {

graphql/src/runner.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ lazy_static! {
4949
.map(|s| u32::from_str(&s)
5050
.unwrap_or_else(|_| panic!("failed to parse env var GRAPH_GRAPHQL_MAX_FIRST")))
5151
.unwrap_or(1000);
52-
static ref WINDOW_SIZE: Duration = Duration::from_secs(300);
53-
static ref BIN_SIZE: Duration = Duration::from_secs(1);
5452
}
5553

5654
impl<S> GraphQlRunner<S>
@@ -67,7 +65,7 @@ where
6765
logger: logger.new(o!("component" => "GraphQlRunner")),
6866
store,
6967
expensive,
70-
effort: Arc::new(QueryEffort::new(*WINDOW_SIZE, *BIN_SIZE)),
68+
effort: Arc::new(QueryEffort::default()),
7169
}
7270
}
7371

0 commit comments

Comments
 (0)