Skip to content

Commit e818ef2

Browse files
authored
Views: blocking thread for views cleanup (#3707)
# Description of Changes Change from `tokio::spawn` task to blocking thread for Views cleanup tasks. # API and ABI breaking changes NA # Expected complexity level and risk 1
1 parent cb35104 commit e818ef2

File tree

1 file changed

+34
-23
lines changed

1 file changed

+34
-23
lines changed

crates/core/src/db/relational_db.rs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,39 +1084,50 @@ const VIEW_CLEANUP_BUDGET: std::time::Duration = std::time::Duration::from_milli
10841084

10851085
/// Spawn a background task that periodically cleans up expired views
10861086
pub fn spawn_view_cleanup_loop(db: Arc<RelationalDB>) -> tokio::task::AbortHandle {
1087-
tokio::spawn(async move {
1088-
let db = &db;
1089-
loop {
1090-
match db.with_auto_commit(Workload::Internal, |tx| {
1091-
tx.clear_expired_views(VIEWS_EXPIRATION, VIEW_CLEANUP_BUDGET)
1092-
.map_err(DBError::from)
1093-
}) {
1094-
Ok((cleared, total_expired)) => {
1095-
if cleared != total_expired {
1096-
//TODO: Report it as metric
1097-
log::info!(
1098-
"[{}] DATABASE: cleared {} expired views ({} remaining)",
1099-
db.database_identity(),
1100-
cleared,
1101-
total_expired - cleared
1102-
);
1103-
}
1104-
}
1105-
Err(e) => {
1106-
log::error!(
1107-
"[{}] DATABASE: failed to clear expired views: {}",
1087+
fn run_view_cleanup(db: &RelationalDB) {
1088+
match db.with_auto_commit(Workload::Internal, |tx| {
1089+
tx.clear_expired_views(VIEWS_EXPIRATION, VIEW_CLEANUP_BUDGET)
1090+
.map_err(DBError::from)
1091+
}) {
1092+
Ok((cleared, total_expired)) => {
1093+
if cleared != total_expired {
1094+
// TODO: metrics
1095+
log::info!(
1096+
"[{}] DATABASE: cleared {} expired views ({} remaining)",
11081097
db.database_identity(),
1109-
e
1098+
cleared,
1099+
total_expired - cleared
11101100
);
11111101
}
11121102
}
1103+
Err(e) => {
1104+
log::error!(
1105+
"[{}] DATABASE: failed to clear expired views: {}",
1106+
db.database_identity(),
1107+
e
1108+
);
1109+
}
1110+
}
1111+
}
1112+
1113+
tokio::spawn(async move {
1114+
loop {
1115+
// Offload actual cleanup to blocking thread pool, as `VIEW_CLEANUP_BUDGET` is defined
1116+
// in milliseconds, which may be too long for async tasks.
1117+
let db = db.clone();
1118+
let db_identity = db.database_identity();
1119+
tokio::task::spawn_blocking(move || run_view_cleanup(&db))
1120+
.await
1121+
.inspect_err(|e| {
1122+
log::error!("[{}] DATABASE: failed to run view cleanup task: {}", db_identity, e);
1123+
})
1124+
.ok();
11131125

11141126
tokio::time::sleep(VIEWS_EXPIRATION).await;
11151127
}
11161128
})
11171129
.abort_handle()
11181130
}
1119-
11201131
impl RelationalDB {
11211132
pub fn create_table(&self, tx: &mut MutTx, schema: TableSchema) -> Result<TableId, DBError> {
11221133
Ok(self.inner.create_table_mut_tx(tx, schema)?)

0 commit comments

Comments
 (0)