Skip to content

Commit 9759016

Browse files
committed
node, store: Allow finding unused deployments that were recorded a while ago
1 parent 5ddc3e1 commit 9759016

File tree

4 files changed

+39
-6
lines changed

4 files changed

+39
-6
lines changed

node/src/bin/manager.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{collections::HashMap, env, num::ParseIntError, sync::Arc, time::Durati
22

33
use config::PoolSize;
44
use git_testament::{git_testament, render_testament};
5-
use graph::{data::graphql::effort::LoadManager, prometheus::Registry};
5+
use graph::{data::graphql::effort::LoadManager, prelude::chrono, prometheus::Registry};
66
use graph_core::MetricsRegistry;
77
use graph_graphql::prelude::GraphQlRunner;
88
use lazy_static::lazy_static;
@@ -200,6 +200,9 @@ pub enum UnusedCommand {
200200
/// Remove a specific deployment
201201
#[structopt(short, long, conflicts_with = "count")]
202202
deployment: Option<String>,
203+
/// Remove unused deployments that were recorded at least this many minutes ago
204+
#[structopt(short, long)]
205+
older: Option<u32>,
203206
},
204207
}
205208

@@ -531,9 +534,14 @@ async fn main() {
531534
match cmd {
532535
List { existing } => commands::unused_deployments::list(store, existing),
533536
Record => commands::unused_deployments::record(store),
534-
Remove { count, deployment } => {
537+
Remove {
538+
count,
539+
deployment,
540+
older,
541+
} => {
535542
let count = count.unwrap_or(1_000_000);
536-
commands::unused_deployments::remove(store, count, deployment)
543+
let older = older.map(|older| chrono::Duration::minutes(older as i64));
544+
commands::unused_deployments::remove(store, count, deployment, older)
537545
}
538546
}
539547
}

node/src/manager/commands/unused_deployments.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::{sync::Arc, time::Instant};
22

3-
use graph::prelude::anyhow::Error;
3+
use graph::prelude::{anyhow::Error, chrono};
44
use graph_store_postgres::{unused, SubgraphStore, UnusedDeployment};
55

66
use crate::manager::display::List;
@@ -77,8 +77,13 @@ pub fn remove(
7777
store: Arc<SubgraphStore>,
7878
count: usize,
7979
deployment: Option<String>,
80+
older: Option<chrono::Duration>,
8081
) -> Result<(), Error> {
81-
let unused = store.list_unused_deployments(unused::Filter::New)?;
82+
let filter = match older {
83+
Some(duration) => unused::Filter::UnusedLongerThan(duration),
84+
None => unused::Filter::New,
85+
};
86+
let unused = store.list_unused_deployments(filter)?;
8287
let unused = match &deployment {
8388
None => unused,
8489
Some(deployment) => unused

store/postgres/src/primary.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use diesel::{
2424
};
2525
use graph::{
2626
components::store::DeploymentId as GraphDeploymentId,
27-
prelude::{CancelHandle, CancelToken},
27+
prelude::{chrono, CancelHandle, CancelToken},
2828
};
2929
use graph::{
3030
components::store::DeploymentLocator,
@@ -1392,6 +1392,21 @@ impl<'a> Connection<'a> {
13921392
.filter(u::removed_at.is_null())
13931393
.order_by(u::entity_count)
13941394
.load(self.conn.as_ref())?),
1395+
UnusedLongerThan(duration) => {
1396+
let ts = chrono::offset::Local::now()
1397+
.checked_sub_signed(duration)
1398+
.ok_or_else(|| {
1399+
StoreError::ConstraintViolation(format!(
1400+
"duration {} is too large",
1401+
duration
1402+
))
1403+
})?;
1404+
Ok(u::table
1405+
.filter(u::removed_at.is_null())
1406+
.filter(u::unused_at.lt(ts))
1407+
.order_by(u::entity_count)
1408+
.load(self.conn.as_ref())?)
1409+
}
13951410
}
13961411
}
13971412

store/postgres/src/subgraph_store.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,16 @@ pub trait DeploymentPlacer {
125125

126126
/// Tools for managing unused deployments
127127
pub mod unused {
128+
use graph::prelude::chrono::Duration;
129+
128130
pub enum Filter {
129131
/// List all unused deployments
130132
All,
131133
/// List only deployments that are unused but have not been removed yet
132134
New,
135+
/// List only deployments that were recorded as unused at least this
136+
/// long ago but have not been removed at
137+
UnusedLongerThan(Duration),
133138
}
134139
}
135140

0 commit comments

Comments
 (0)