Skip to content

Commit 2dabd5f

Browse files
committed
Optionally use the orchestrator for the "compile" commands in the UI
1 parent 613837c commit 2dabd5f

File tree

6 files changed

+581
-27
lines changed

6 files changed

+581
-27
lines changed

ui/Cargo.lock

Lines changed: 100 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ui/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ futures = "0.3.21"
1717
lazy_static = "1.0.0"
1818
octocrab = "0.25"
1919
openssl-probe = "0.1.2"
20+
orchestrator = { path = "../compiler/base/orchestrator" }
2021
prometheus = "0.13.0"
2122
regex = "1.0.0"
2223
serde = { version = "1.0", features = ["rc"] }

ui/src/main.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ struct Config {
3737
cors_enabled: bool,
3838
gh_token: Option<String>,
3939
metrics_token: Option<String>,
40+
orchestrator_enabled: bool,
4041
port: u16,
4142
root: PathBuf,
4243
}
@@ -90,11 +91,14 @@ impl Config {
9091

9192
let cors_enabled = env::var_os("PLAYGROUND_CORS_ENABLED").is_some();
9293

94+
let orchestrator_enabled = env::var_os("PLAYGROUND_ORCHESTRATOR_ENABLED").is_some();
95+
9396
Self {
9497
address,
9598
cors_enabled,
9699
gh_token,
97100
metrics_token,
101+
orchestrator_enabled,
98102
port,
99103
root,
100104
}
@@ -112,6 +116,10 @@ impl Config {
112116
self.cors_enabled
113117
}
114118

119+
fn use_orchestrator(&self) -> bool {
120+
self.orchestrator_enabled
121+
}
122+
115123
fn metrics_token(&self) -> Option<MetricsToken> {
116124
self.metrics_token.as_deref().map(MetricsToken::new)
117125
}
@@ -203,6 +211,24 @@ pub enum Error {
203211
CachePoisoned,
204212
#[snafu(display("The WebSocket worker panicked: {}", text))]
205213
WebSocketTaskPanic { text: String },
214+
215+
#[snafu(display("Unable to create the coordinator"))]
216+
CreateCoordinator {
217+
source: orchestrator::coordinator::Error,
218+
},
219+
220+
#[snafu(display("Unable to shutdown the coordinator"))]
221+
ShutdownCoordinator {
222+
source: orchestrator::coordinator::Error,
223+
},
224+
225+
#[snafu(display("Unable to convert the compile request"))]
226+
Compile {
227+
source: orchestrator::coordinator::CompileError,
228+
},
229+
230+
#[snafu(display("The operation timed out"))]
231+
Timeout { source: tokio::time::error::Elapsed },
206232
}
207233

208234
type Result<T, E = Error> = ::std::result::Result<T, E>;

ui/src/metrics.rs

Lines changed: 88 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
use futures::future::BoxFuture;
22
use lazy_static::lazy_static;
3+
use orchestrator::coordinator;
34
use prometheus::{
45
self, register_histogram, register_histogram_vec, register_int_counter, register_int_gauge,
56
Histogram, HistogramVec, IntCounter, IntGauge,
67
};
78
use regex::Regex;
8-
use std::{future::Future, time::Instant};
9+
use std::{
10+
future::Future,
11+
time::{Duration, Instant},
12+
};
913

1014
use crate::sandbox::{self, Channel, CompileTarget, CrateType, Edition, Mode};
1115

@@ -62,6 +66,16 @@ pub(crate) enum Outcome {
6266
ErrorUserCode,
6367
}
6468

69+
pub(crate) struct LabelsCore {
70+
target: Option<CompileTarget>,
71+
channel: Option<Channel>,
72+
mode: Option<Mode>,
73+
edition: Option<Option<Edition>>,
74+
crate_type: Option<CrateType>,
75+
tests: Option<bool>,
76+
backtrace: Option<bool>,
77+
}
78+
6579
#[derive(Debug, Copy, Clone)]
6680
pub(crate) struct Labels {
6781
endpoint: Endpoint,
@@ -132,6 +146,29 @@ impl Labels {
132146
backtrace,
133147
]
134148
}
149+
150+
pub(crate) fn complete(endpoint: Endpoint, labels_core: LabelsCore, outcome: Outcome) -> Self {
151+
let LabelsCore {
152+
target,
153+
channel,
154+
mode,
155+
edition,
156+
crate_type,
157+
tests,
158+
backtrace,
159+
} = labels_core;
160+
Self {
161+
endpoint,
162+
outcome,
163+
target,
164+
channel,
165+
mode,
166+
edition,
167+
crate_type,
168+
tests,
169+
backtrace,
170+
}
171+
}
135172
}
136173

137174
pub(crate) trait GenerateLabels {
@@ -406,11 +443,8 @@ where
406443
let outcome = SuccessDetails::for_sandbox_result(&response);
407444
let mut labels = request.generate_labels(outcome);
408445
f(&mut labels);
409-
let values = &labels.as_values();
410-
411-
let histogram = REQUESTS.with_label_values(values);
412446

413-
histogram.observe(elapsed.as_secs_f64());
447+
record_metric_complete(labels, elapsed);
414448

415449
response
416450
}
@@ -443,10 +477,56 @@ where
443477
tests: None,
444478
backtrace: None,
445479
};
446-
let values = &labels.as_values();
447-
let histogram = REQUESTS.with_label_values(values);
448480

449-
histogram.observe(elapsed.as_secs_f64());
481+
record_metric_complete(labels, elapsed);
450482

451483
response
452484
}
485+
486+
pub(crate) trait HasLabelsCore {
487+
fn labels_core(&self) -> LabelsCore;
488+
}
489+
490+
impl HasLabelsCore for coordinator::CompileRequest {
491+
fn labels_core(&self) -> LabelsCore {
492+
let Self {
493+
target,
494+
channel,
495+
crate_type,
496+
mode,
497+
edition,
498+
tests,
499+
backtrace,
500+
code: _,
501+
} = *self;
502+
503+
LabelsCore {
504+
target: Some(target.into()),
505+
channel: Some(channel.into()),
506+
mode: Some(mode.into()),
507+
edition: Some(Some(edition.into())),
508+
crate_type: Some(crate_type.into()),
509+
tests: Some(tests),
510+
backtrace: Some(backtrace),
511+
}
512+
}
513+
}
514+
515+
pub(crate) fn record_metric(
516+
endpoint: Endpoint,
517+
labels_core: LabelsCore,
518+
outcome: Outcome,
519+
elapsed: Duration,
520+
) {
521+
let labels = Labels::complete(endpoint, labels_core, outcome);
522+
record_metric_complete(labels, elapsed)
523+
}
524+
525+
fn record_metric_complete(
526+
labels: Labels,
527+
elapsed: Duration,
528+
) {
529+
let values = &labels.as_values();
530+
let histogram = REQUESTS.with_label_values(values);
531+
histogram.observe(elapsed.as_secs_f64());
532+
}

0 commit comments

Comments
 (0)