Skip to content

Commit

Permalink
Add json-rpc api support for dropping subgraphs
Browse files Browse the repository at this point in the history
  • Loading branch information
mangas committed Sep 29, 2024
1 parent 990ef4d commit d235309
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 0 deletions.
8 changes: 8 additions & 0 deletions core/src/subgraph/registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,14 @@ where
Ok(())
}

async fn remove_deployment(&self, id: &DeploymentHash) -> Result<(), SubgraphRegistrarError> {
self.store.drop_subgraph(id)?;

debug!(self.logger, "Removing deployment(s)"; "hash" => id.to_string());

Ok(())
}

/// Reassign a subgraph deployment to a different node.
///
/// Reassigning to a nodeId that does not match any reachable graph-nodes will effectively pause the
Expand Down
12 changes: 12 additions & 0 deletions docker/bin/drop
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#! /bin/bash

if [ $# != 1 ]; then
echo "usage: drop <ipfs_hash>"
exit 1
fi

api="http://index-node.default/"

echo "Dropping deployment $1"
data=$(printf '{"jsonrpc": "2.0", "method": "subgraph_drop", "params": {"ipfs_hash":"%s"}, "id":"1"}' "$1")
curl -s -H "content-type: application/json" --data "$data" "$api"
3 changes: 3 additions & 0 deletions graph/src/components/store/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ pub trait SubgraphStore: Send + Sync + 'static {
/// their assignment, but keep the deployments themselves around
fn remove_subgraph(&self, name: SubgraphName) -> Result<(), StoreError>;

/// Remove all the subgraph's versions, assignments and data.
fn drop_subgraph(&self, name: &DeploymentHash) -> Result<(), StoreError>;

/// Assign the subgraph with `id` to the node `node_id`. If there is no
/// assignment for the given deployment, report an error.
fn reassign_subgraph(
Expand Down
1 change: 1 addition & 0 deletions graph/src/components/subgraph/registrar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ pub trait SubgraphRegistrar: Send + Sync + 'static {
) -> Result<DeploymentLocator, SubgraphRegistrarError>;

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

async fn reassign_subgraph(
&self,
Expand Down
26 changes: 26 additions & 0 deletions server/json-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ impl JsonRpcServer {
state.remove_handler(params.parse()?).await
})
.unwrap();
rpc_module
.register_async_method("subgraph_drop", |params, state| async move {
state.drop_handler(params.parse()?).await
})
.unwrap();
rpc_module
.register_async_method("subgraph_reassign", |params, state| async move {
state.reassign_handler(params.parse()?).await
Expand Down Expand Up @@ -150,6 +155,22 @@ impl<R: SubgraphRegistrar> ServerState<R> {
}
}

/// Handler for the `subgraph_drop` endpoint.
async fn drop_handler(&self, params: SubgraphDropParams) -> JsonRpcResult<GraphValue> {
info!(&self.logger, "Received subgraph_drop request"; "params" => format!("{:?}", params));

match self.registrar.remove_deployment(&params.ipfs_hash).await {
Ok(_) => Ok(Value::Null),
Err(e) => Err(json_rpc_error(
&self.logger,
"subgraph_drop",
e,
Self::REMOVE_ERROR,
params,
)),
}
}

/// Handler for the `subgraph_remove` endpoint.
async fn remove_handler(&self, params: SubgraphRemoveParams) -> JsonRpcResult<GraphValue> {
info!(&self.logger, "Received subgraph_remove request"; "params" => format!("{:?}", params));
Expand Down Expand Up @@ -289,6 +310,11 @@ struct SubgraphRemoveParams {
name: SubgraphName,
}

#[derive(Debug, Deserialize)]
struct SubgraphDropParams {
ipfs_hash: DeploymentHash,
}

#[derive(Debug, Deserialize)]
struct SubgraphReassignParams {
ipfs_hash: DeploymentHash,
Expand Down
17 changes: 17 additions & 0 deletions store/postgres/src/subgraph_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1351,6 +1351,23 @@ impl SubgraphStoreTrait for SubgraphStore {
})
}

fn drop_subgraph(&self, hash: &DeploymentHash) -> Result<(), StoreError> {
let deployments = self.subgraphs_for_deployment_hash(hash)?;
for (deployment, _) in deployments.into_iter() {
self.remove_subgraph(
SubgraphName::new(deployment.clone())
.map_err(|_| StoreError::DeploymentNotFound(deployment))?,
)?;
}

let locators = self.locators(hash)?;
for loc in locators.iter() {
self.inner.remove_deployment(loc.id.into())?;
}

Ok(())
}

fn reassign_subgraph(
&self,
deployment: &DeploymentLocator,
Expand Down

0 comments on commit d235309

Please sign in to comment.