Skip to content
Open
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
4 changes: 4 additions & 0 deletions app/buck2_client_ctx/src/streaming.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,10 @@ fn get_event_log_subscriber<T: StreamingCommand>(
T::COMMAND_NAME.to_owned(),
ctx.start_time,
log_size_counter_bytes,
ctx.immediate_config
.daemon_startup_config()
.map(|daemon_startup_config| daemon_startup_config.retained_event_logs)
.unwrap(),
);
Box::new(log)
}
Expand Down
2 changes: 2 additions & 0 deletions app/buck2_client_ctx/src/subscribers/event_log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ impl EventLog {
command_name: String,
start_time: SystemTime,
log_size_counter_bytes: Option<Arc<AtomicU64>>,
retained_event_logs: usize,
) -> EventLog {
Self {
writer: WriteEventLog::new(
Expand All @@ -50,6 +51,7 @@ impl EventLog {
command_name,
start_time,
log_size_counter_bytes,
retained_event_logs,
),
}
}
Expand Down
11 changes: 11 additions & 0 deletions app/buck2_common/src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use serde::Serialize;
use crate::legacy_configs::configs::LegacyBuckConfig;
use crate::legacy_configs::key::BuckconfigKeyRef;

const DEFAULT_RETAINED_EVENT_LOGS: usize = 12;

/// Helper enum to categorize the kind of timeout we get from the startup config.
#[derive(Clone, Debug)]
pub enum Timeout {
Expand Down Expand Up @@ -446,6 +448,7 @@ pub struct DaemonStartupConfig {
pub resource_control: ResourceControlConfig,
pub log_download_method: LogDownloadMethod,
pub health_check_config: HealthCheckConfig,
pub retained_event_logs: usize,
}

impl DaemonStartupConfig {
Expand Down Expand Up @@ -521,6 +524,13 @@ impl DaemonStartupConfig {
resource_control: ResourceControlConfig::from_config(config)?,
log_download_method,
health_check_config: HealthCheckConfig::from_config(config)?,
retained_event_logs: config
.get(BuckconfigKeyRef {
section: "buck2",
property: "retained_event_logs",
})
.and_then(|s| s.parse::<usize>().ok())
.unwrap_or(DEFAULT_RETAINED_EVENT_LOGS),
})
}

Expand Down Expand Up @@ -548,6 +558,7 @@ impl DaemonStartupConfig {
LogDownloadMethod::None
},
health_check_config: HealthCheckConfig::default(),
retained_event_logs: DEFAULT_RETAINED_EVENT_LOGS,
}
}
}
6 changes: 2 additions & 4 deletions app/buck2_event_log/src/file_names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,9 @@ pub(crate) fn get_logfile_name(
))
}

pub(crate) async fn remove_old_logs(logdir: &AbsNormPath) {
const N_LOGS_RETAINED: usize = 12;

pub(crate) async fn remove_old_logs(logdir: &AbsNormPath, retained_event_logs: usize) {
if let Ok(logfiles) = get_files_in_log_dir(logdir) {
futures::stream::iter(logfiles.into_iter().rev().skip(N_LOGS_RETAINED - 1))
futures::stream::iter(logfiles.into_iter().rev().skip(retained_event_logs))
.then(|file| async move {
// The oldest logs might be open from another concurrent build, so suppress error.
tokio::fs::remove_file(file).await.ok()
Expand Down
6 changes: 5 additions & 1 deletion app/buck2_event_log/src/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub struct WriteEventLog {
/// Allocation cache. Must be cleaned before use.
buf: Vec<u8>,
log_size_counter_bytes: Option<Arc<AtomicU64>>,
retained_event_logs: usize,
}

impl WriteEventLog {
Expand All @@ -75,6 +76,7 @@ impl WriteEventLog {
command_name: String,
start_time: SystemTime,
log_size_counter_bytes: Option<Arc<AtomicU64>>,
retained_event_logs: usize,
) -> Self {
Self {
state: LogWriterState::Unopened {
Expand All @@ -88,6 +90,7 @@ impl WriteEventLog {
start_time,
buf: Vec::new(),
log_size_counter_bytes,
retained_event_logs,
}
}

Expand Down Expand Up @@ -165,7 +168,7 @@ impl WriteEventLog {
.with_buck_error_context(|| {
format!("Error creating event log directory: `{logdir}`")
})?;
remove_old_logs(logdir).await;
remove_old_logs(logdir, self.retained_event_logs).await;

let encoding = Encoding::PROTO_ZSTD;
let file_name = &get_logfile_name(event, encoding, &self.command_name)?;
Expand Down Expand Up @@ -479,6 +482,7 @@ mod tests {
buf: Vec::new(),
log_size_counter_bytes: None,
start_time: SystemTime::UNIX_EPOCH,
retained_event_logs: 5,
})
}
}
Expand Down