Skip to content

Commit a13433a

Browse files
authored
enable parking_lot locks in dependencies (#589)
Tracing and Tokio both expose a feature flagged optional dependency on the parking_lot crate's Mutex and RwLock implementations rather than std's. Enabling this dependency causes these crates to use parking_lot internally with no externally visible API change. In microbenchmarks of Tokio's synchronization primitives, we've observed better performance with parking_lot vs std, and tend to recommend it in high-performance applications where the cost of another dependency is acceptable. This branch enables tokio and tracing's parking_lot features. I also removed a bonus dependency on a vintage Tokio version that we must have missed during the 0.2 update, as it was pulling an incompatible parking_lot version.
1 parent f897208 commit a13433a

File tree

8 files changed

+79
-308
lines changed

8 files changed

+79
-308
lines changed

Cargo.lock

Lines changed: 57 additions & 280 deletions
Large diffs are not rendered by default.

linkerd/app/core/Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ linkerd2-stack-tracing = { path = "../../stack/tracing" }
6363
linkerd2-trace-context = { path = "../../trace-context" }
6464
rand = { version = "0.7", features = ["small_rng"] }
6565
regex = "1.0.0"
66-
tokio = { version = "0.2", features = ["macros", "sync"]}
66+
tokio = { version = "0.2", features = ["macros", "sync", "parking_lot"]}
6767
tokio-timer = "0.2"
6868
tower-request-modifier = { git = "https://github.com/tower-rs/tower-http" }
6969
tonic = { version = "0.2", default-features = false, features = ["prost"] }
@@ -73,10 +73,10 @@ tracing-log = "0.1"
7373
pin-project = "0.4"
7474

7575
[dependencies.tracing-subscriber]
76-
version = "0.2.1"
76+
version = "0.2.7"
7777
# we don't need `chrono` time formatting
7878
default-features = false
79-
features = ["env-filter", "fmt", "smallvec", "tracing-log", "ansi", "json"]
79+
features = ["env-filter", "fmt", "smallvec", "tracing-log", "ansi", "json", "parking_lot"]
8080

8181
[dependencies.tower]
8282
version = "0.3"

linkerd/app/integration/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ tower = { version = "0.3", default-features = false}
3939
tonic = { version = "0.2", default-features = false }
4040
tracing = "0.1.9"
4141
tracing-futures = { version = "0.2", features = ["std-future"] }
42-
tracing-subscriber = "0.2.5"
42+
tracing-subscriber = "0.2.7"
4343
webpki = "0.21.0"
4444

4545
[dev-dependencies]

linkerd/http-metrics/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ indexmap = "1.0"
1616
linkerd2-error = { path = "../error" }
1717
linkerd2-http-classify = { path = "../http-classify" }
1818
linkerd2-metrics = { path = "../metrics" }
19-
linkerd2-stack = { path = "../stack" }
20-
tokio = "0.1"
21-
tokio-timer = "0.2" # for tokio_timer::clock
19+
linkerd2-stack = { path = "../stack" }
2220
tracing = "0.1.9"
2321
pin-project = "0.4"
2422

linkerd/http-metrics/src/requests/layer.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ use std::pin::Pin;
1313
use std::sync::{Arc, Mutex};
1414
use std::task::{Context, Poll};
1515
use std::time::Instant;
16-
use tokio_timer::clock;
1716

1817
/// A stack module that wraps services to record metrics.
1918
#[derive(Debug)]
@@ -285,7 +284,7 @@ where
285284

286285
if req.body().is_end_stream() {
287286
if let Some(lock) = req_metrics.take() {
288-
let now = clock::now();
287+
let now = Instant::now();
289288
if let Ok(mut metrics) = lock.lock() {
290289
(*metrics).last_update = now;
291290
(*metrics).total.incr();
@@ -307,7 +306,7 @@ where
307306
ResponseFuture {
308307
classify: Some(classify),
309308
metrics: self.metrics.clone(),
310-
stream_open_at: clock::now(),
309+
stream_open_at: Instant::now(),
311310
inner: self.inner.proxy(svc, req),
312311
}
313312
}
@@ -335,7 +334,7 @@ where
335334

336335
if req.body().is_end_stream() {
337336
if let Some(lock) = req_metrics.take() {
338-
let now = clock::now();
337+
let now = Instant::now();
339338
if let Ok(mut metrics) = lock.lock() {
340339
(*metrics).last_update = now;
341340
(*metrics).total.incr();
@@ -357,7 +356,7 @@ where
357356
ResponseFuture {
358357
classify: Some(classify),
359358
metrics: self.metrics.clone(),
360-
stream_open_at: clock::now(),
359+
stream_open_at: Instant::now(),
361360
inner: self.inner.call(req),
362361
}
363362
}
@@ -427,7 +426,7 @@ where
427426
let frame = ready!(this.inner.poll_data(cx));
428427

429428
if let Some(lock) = this.metrics.take() {
430-
let now = clock::now();
429+
let now = Instant::now();
431430
if let Ok(mut metrics) = lock.lock() {
432431
(*metrics).last_update = now;
433432
(*metrics).total.incr();
@@ -472,7 +471,7 @@ where
472471
Self {
473472
status: http::StatusCode::OK,
474473
inner: B::default(),
475-
stream_open_at: clock::now(),
474+
stream_open_at: Instant::now(),
476475
classify: None,
477476
metrics: None,
478477
latency_recorded: false,
@@ -488,7 +487,7 @@ where
488487
{
489488
fn record_latency(self: Pin<&mut Self>) {
490489
let this = self.project();
491-
let now = clock::now();
490+
let now = Instant::now();
492491

493492
let lock = match this.metrics.as_mut() {
494493
Some(lock) => lock,
@@ -537,7 +536,7 @@ fn measure_class<C: Hash + Eq>(
537536
class: C,
538537
status: Option<http::StatusCode>,
539538
) {
540-
let now = clock::now();
539+
let now = Instant::now();
541540
let mut metrics = match lock.lock() {
542541
Ok(m) => m,
543542
Err(_) => return,

linkerd/http-metrics/src/requests/mod.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::fmt::Debug;
77
use std::hash::Hash;
88
use std::sync::{Arc, Mutex};
99
use std::time::{Duration, Instant};
10-
use tokio_timer::clock;
1110

1211
mod layer;
1312
mod report;
@@ -79,7 +78,7 @@ impl<T: Hash + Eq, C: Hash + Eq> Clone for Requests<T, C> {
7978
impl<C: Hash + Eq> Default for Metrics<C> {
8079
fn default() -> Self {
8180
Self {
82-
last_update: clock::now(),
81+
last_update: Instant::now(),
8382
total: Counter::default(),
8483
by_status: IndexMap::default(),
8584
}
@@ -110,8 +109,7 @@ mod tests {
110109
fn expiry() {
111110
use linkerd2_metrics::FmtLabels;
112111
use std::fmt;
113-
use std::time::Duration;
114-
use tokio_timer::clock;
112+
use std::time::{Duration, Instant};
115113

116114
#[derive(Clone, Debug, Hash, Eq, PartialEq)]
117115
struct Target(usize);
@@ -142,14 +140,14 @@ mod tests {
142140
let report = r.clone().into_report(retain_idle_for);
143141
let mut registry = r.0.lock().unwrap();
144142

145-
let before_update = clock::now();
143+
let before_update = Instant::now();
146144
let metrics = registry
147145
.by_target
148146
.entry(Target(123))
149147
.or_insert_with(|| Default::default())
150148
.clone();
151149
assert_eq!(registry.by_target.len(), 1, "target should be registered");
152-
let after_update = clock::now();
150+
let after_update = Instant::now();
153151

154152
registry.retain_since(after_update);
155153
assert_eq!(

linkerd/http-metrics/src/requests/report.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{Prefixed, Registry, Report};
33
use linkerd2_metrics::{latency, Counter, FmtLabels, FmtMetric, FmtMetrics, Histogram, Metric};
44
use std::fmt;
55
use std::hash::Hash;
6-
use tokio_timer::clock;
6+
use std::time::Instant;
77
use tracing::trace;
88

99
struct Status(http::StatusCode);
@@ -73,7 +73,7 @@ where
7373
metric.fmt_help(f)?;
7474
registry.fmt_by_class(f, metric, |s| &s.total)?;
7575

76-
registry.retain_since(clock::now() - self.retain_idle);
76+
registry.retain_since(Instant::now() - self.retain_idle);
7777

7878
Ok(())
7979
}

linkerd/http-metrics/src/retries.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use std::fmt;
44
use std::hash::Hash;
55
use std::sync::{Arc, Mutex};
66
use std::time::{Duration, Instant};
7-
use tokio_timer::clock;
87
use tracing::trace;
98

109
#[derive(Debug)]
@@ -59,7 +58,7 @@ impl<T: Hash + Eq> Clone for Retries<T> {
5958
impl Handle {
6059
pub fn incr_retryable(&self, has_budget: bool) {
6160
if let Ok(mut m) = self.0.lock() {
62-
m.last_update = clock::now();
61+
m.last_update = Instant::now();
6362
m.retryable.incr();
6463
if !has_budget {
6564
m.no_budget.incr();
@@ -73,7 +72,7 @@ impl Handle {
7372
impl Default for Metrics {
7473
fn default() -> Self {
7574
Self {
76-
last_update: clock::now(),
75+
last_update: Instant::now(),
7776
retryable: Counter::default(),
7877
no_budget: Counter::default(),
7978
}
@@ -129,7 +128,7 @@ where
129128
}
130129
}
131130

132-
registry.retain_since(clock::now() - self.retain_idle);
131+
registry.retain_since(Instant::now() - self.retain_idle);
133132

134133
Ok(())
135134
}

0 commit comments

Comments
 (0)