Skip to content

Commit 15f1e69

Browse files
committed
Add json-rpc api support for dropping subgraphs
1 parent 1642eaf commit 15f1e69

File tree

6 files changed

+67
-0
lines changed

6 files changed

+67
-0
lines changed

core/src/subgraph/registrar.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,14 @@ where
455455
Ok(())
456456
}
457457

458+
async fn remove_deployment(&self, id: &DeploymentHash) -> Result<(), SubgraphRegistrarError> {
459+
self.store.drop_subgraph(id)?;
460+
461+
debug!(self.logger, "Removing deployment(s)"; "hash" => id.to_string());
462+
463+
Ok(())
464+
}
465+
458466
/// Reassign a subgraph deployment to a different node.
459467
///
460468
/// Reassigning to a nodeId that does not match any reachable graph-nodes will effectively pause the

docker/bin/drop

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#! /bin/bash
2+
3+
if [ $# != 1 ]; then
4+
echo "usage: drop <ipfs_hash>"
5+
exit 1
6+
fi
7+
8+
api="http://index-node.default/"
9+
10+
echo "Dropping deployment $1"
11+
data=$(printf '{"jsonrpc": "2.0", "method": "subgraph_drop", "params": {"ipfs_hash":"%s"}, "id":"1"}' "$1")
12+
curl -s -H "content-type: application/json" --data "$data" "$api"

graph/src/components/store/traits.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ pub trait SubgraphStore: Send + Sync + 'static {
102102
/// their assignment, but keep the deployments themselves around
103103
fn remove_subgraph(&self, name: SubgraphName) -> Result<(), StoreError>;
104104

105+
/// Remove all the subgraph's versions, assignments and data.
106+
fn drop_subgraph(&self, name: &DeploymentHash) -> Result<(), StoreError>;
107+
105108
/// Assign the subgraph with `id` to the node `node_id`. If there is no
106109
/// assignment for the given deployment, report an error.
107110
fn reassign_subgraph(

graph/src/components/subgraph/registrar.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ pub trait SubgraphRegistrar: Send + Sync + 'static {
4848
) -> Result<DeploymentLocator, SubgraphRegistrarError>;
4949

5050
async fn remove_subgraph(&self, name: SubgraphName) -> Result<(), SubgraphRegistrarError>;
51+
async fn remove_deployment(&self, hash: &DeploymentHash) -> Result<(), SubgraphRegistrarError>;
5152

5253
async fn reassign_subgraph(
5354
&self,

server/json-rpc/src/lib.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,11 @@ impl JsonRpcServer {
6363
state.remove_handler(params.parse()?).await
6464
})
6565
.unwrap();
66+
rpc_module
67+
.register_async_method("subgraph_drop", |params, state| async move {
68+
state.drop_handler(params.parse()?).await
69+
})
70+
.unwrap();
6671
rpc_module
6772
.register_async_method("subgraph_reassign", |params, state| async move {
6873
state.reassign_handler(params.parse()?).await
@@ -150,6 +155,22 @@ impl<R: SubgraphRegistrar> ServerState<R> {
150155
}
151156
}
152157

158+
/// Handler for the `subgraph_drop` endpoint.
159+
async fn drop_handler(&self, params: SubgraphDropParams) -> JsonRpcResult<GraphValue> {
160+
info!(&self.logger, "Received subgraph_drop request"; "params" => format!("{:?}", params));
161+
162+
match self.registrar.remove_deployment(&params.ipfs_hash).await {
163+
Ok(_) => Ok(Value::Null),
164+
Err(e) => Err(json_rpc_error(
165+
&self.logger,
166+
"subgraph_drop",
167+
e,
168+
Self::REMOVE_ERROR,
169+
params,
170+
)),
171+
}
172+
}
173+
153174
/// Handler for the `subgraph_remove` endpoint.
154175
async fn remove_handler(&self, params: SubgraphRemoveParams) -> JsonRpcResult<GraphValue> {
155176
info!(&self.logger, "Received subgraph_remove request"; "params" => format!("{:?}", params));
@@ -289,6 +310,11 @@ struct SubgraphRemoveParams {
289310
name: SubgraphName,
290311
}
291312

313+
#[derive(Debug, Deserialize)]
314+
struct SubgraphDropParams {
315+
ipfs_hash: DeploymentHash,
316+
}
317+
292318
#[derive(Debug, Deserialize)]
293319
struct SubgraphReassignParams {
294320
ipfs_hash: DeploymentHash,

store/postgres/src/subgraph_store.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,23 @@ impl SubgraphStoreTrait for SubgraphStore {
13511351
})
13521352
}
13531353

1354+
fn drop_subgraph(&self, hash: &DeploymentHash) -> Result<(), StoreError> {
1355+
let deployments = self.subgraphs_for_deployment_hash(hash)?;
1356+
for (deployment, _) in deployments.into_iter() {
1357+
self.remove_subgraph(
1358+
SubgraphName::new(deployment.clone())
1359+
.map_err(|_| StoreError::DeploymentNotFound(deployment))?,
1360+
)?;
1361+
}
1362+
1363+
let locators = self.locators(hash)?;
1364+
for loc in locators.iter() {
1365+
self.inner.remove_deployment(loc.id.into())?;
1366+
}
1367+
1368+
Ok(())
1369+
}
1370+
13541371
fn reassign_subgraph(
13551372
&self,
13561373
deployment: &DeploymentLocator,

0 commit comments

Comments
 (0)