Skip to content

Commit 942b30c

Browse files
Rajneesh Lakkundifacebook-github-bot
Rajneesh Lakkundi
authored andcommitted
Send excess cache event once
Summary: Send the excess cache event only once. This event is reported at ActionExecutionEnd event which are numerous. Reviewed By: JakobDegen Differential Revision: D73570875 fbshipit-source-id: d1f3fb66053d8132a9fc9133f2c10a62080f32c6
1 parent c1b79e7 commit 942b30c

File tree

1 file changed

+62
-10
lines changed

1 file changed

+62
-10
lines changed

app/buck2_client_ctx/src/subscribers/health_check_subscriber.rs

Lines changed: 62 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,15 @@ use crate::subscribers::subscriber::EventSubscriber;
2828

2929
const EVENT_CHANNEL_SIZE: usize = 100;
3030

31+
struct HealthCheckEventStats {
32+
excess_cache_miss_reported: bool,
33+
}
34+
3135
/// This subscriber is responsible for forwarding events to the health check client
3236
pub struct HealthCheckSubscriber {
3337
health_check_client: Option<Box<dyn HealthCheckClient>>,
3438
event_sender: Option<Sender<HealthCheckEvent>>,
39+
event_stats: HealthCheckEventStats,
3540
}
3641

3742
#[async_trait]
@@ -62,6 +67,9 @@ impl HealthCheckSubscriber {
6267
Box::new(Self {
6368
health_check_client,
6469
event_sender: Some(events_tx),
70+
event_stats: HealthCheckEventStats {
71+
excess_cache_miss_reported: false,
72+
},
6573
})
6674
}
6775

@@ -96,17 +104,20 @@ impl HealthCheckSubscriber {
96104
)
97105
}),
98106
ActionExecution(action_execution_end) => {
99-
let has_excess_cache_miss = action_execution_end
100-
.invalidation_info
101-
.as_ref()
102-
.is_some_and(|v| v.changed_file.is_none());
103-
104-
if has_excess_cache_miss {
105-
Some(HealthCheckEvent::HealthCheckContextEvent(
106-
HealthCheckContextEvent::HasExcessCacheMisses(),
107-
))
108-
} else {
107+
if self.event_stats.excess_cache_miss_reported {
109108
None
109+
} else {
110+
let has_excess_cache_miss = action_execution_end
111+
.invalidation_info
112+
.as_ref()
113+
.is_some_and(|v| v.changed_file.is_none());
114+
115+
has_excess_cache_miss.then(|| {
116+
self.event_stats.excess_cache_miss_reported = true;
117+
HealthCheckEvent::HealthCheckContextEvent(
118+
HealthCheckContextEvent::HasExcessCacheMisses(),
119+
)
120+
})
110121
}
111122
}
112123
_ => None,
@@ -168,6 +179,8 @@ impl HealthCheckSubscriber {
168179
mod tests {
169180
use std::time::SystemTime;
170181

182+
use buck2_data::ActionExecutionEnd;
183+
use buck2_data::ActionKind;
171184
use buck2_health_check::interface::HealthCheckType;
172185
use buck2_health_check::report::DisplayReport;
173186
use buck2_health_check::report::HealthIssue;
@@ -211,6 +224,8 @@ mod tests {
211224
}
212225

213226
impl HealthCheckClient for TestHealthCheckClient {}
227+
struct NoOpHealthCheckClient {}
228+
impl HealthCheckClient for NoOpHealthCheckClient {}
214229

215230
fn test_reports() -> Vec<DisplayReport> {
216231
vec![
@@ -243,6 +258,23 @@ mod tests {
243258
))
244259
}
245260

261+
fn event_for_excess_cache_miss() -> buck2_data::buck_event::Data {
262+
let action_end = ActionExecutionEnd {
263+
kind: ActionKind::Run as i32,
264+
invalidation_info: Some(buck2_data::CommandInvalidationInfo {
265+
changed_file: None,
266+
changed_any: None,
267+
}),
268+
..Default::default()
269+
};
270+
buck2_data::buck_event::Data::SpanEnd(buck2_data::SpanEndEvent {
271+
data: Some(buck2_data::span_end_event::Data::ActionExecution(
272+
Box::new(action_end).into(),
273+
)),
274+
..buck2_data::SpanEndEvent::default()
275+
})
276+
}
277+
246278
#[tokio::test]
247279
async fn test_health_check_subscriber() -> buck2_error::Result<()> {
248280
let (tags_tx, mut tags_rx) = mpsc::channel::<Vec<String>>(10);
@@ -281,4 +313,24 @@ mod tests {
281313

282314
Ok(())
283315
}
316+
317+
#[tokio::test]
318+
async fn test_excess_cache_miss_reporting() {
319+
let (events_tx, mut events_rx) = mpsc::channel::<HealthCheckEvent>(EVENT_CHANNEL_SIZE);
320+
321+
let mut subscriber = HealthCheckSubscriber::new_with_client(
322+
Some(Box::new(NoOpHealthCheckClient {})),
323+
events_tx,
324+
);
325+
326+
let event1 = test_event(event_for_excess_cache_miss().into());
327+
let event2 = test_event(event_for_excess_cache_miss().into());
328+
329+
subscriber.handle_event(&event1).await.unwrap();
330+
subscriber.handle_event(&event2).await.unwrap();
331+
332+
// Verify that only one HealthCheckContextEvent::HasExcessCacheMisses is sent
333+
let mut buffer: Vec<HealthCheckEvent> = Vec::with_capacity(2);
334+
assert_eq!(1, events_rx.recv_many(&mut buffer, 2).await);
335+
}
284336
}

0 commit comments

Comments
 (0)