Skip to content

Commit

Permalink
feat(swarm): json-rpc call to delete instance data (#1059)
Browse files Browse the repository at this point in the history
Description
---
feat(swarm): json-rpc call to delete instance data
fix(swarm/webui): fixed tx pool table

Motivation and Context
---
Allow an instance's data to be deleted via the Web UI. This can be
useful during development.

How Has This Been Tested?
---
Manually

What process can a PR reviewer use to test or verify this change?
---
Start a swarm and click delete data on the instance.

Breaking Changes
---

- [x] None
- [ ] Requires data directory to be deleted
- [ ] Other - Please specify
  • Loading branch information
sdbondi authored Jun 25, 2024
1 parent 460de7c commit 3941ac4
Show file tree
Hide file tree
Showing 12 changed files with 619 additions and 495 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2024 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use std::path::PathBuf;

use async_trait::async_trait;
use tokio::process::Command;

Expand All @@ -9,4 +11,7 @@ use super::context::ProcessContext;
#[async_trait]
pub trait ProcessDefinition: Send {
async fn get_command(&self, context: ProcessContext<'_>) -> anyhow::Result<Command>;
fn get_relative_data_path(&self) -> Option<PathBuf> {
None
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2024 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use std::path::PathBuf;

use anyhow::anyhow;
use async_trait::async_trait;
use tokio::process::Command;
Expand Down Expand Up @@ -54,4 +56,8 @@ impl ProcessDefinition for Indexer {

Ok(command)
}

fn get_relative_data_path(&self) -> Option<PathBuf> {
Some("data".into())
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2024 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use std::path::PathBuf;

use async_trait::async_trait;
use log::debug;
use tokio::process::Command;
Expand Down Expand Up @@ -63,7 +65,7 @@ impl ProcessDefinition for MinotariNode {
Ok(command)
}

// fn get_relative_data_path(&self) -> Option<PathBuf> {
// Some(Path::new("network/data"))
// }
fn get_relative_data_path(&self) -> Option<PathBuf> {
Some("data".into())
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2024 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use std::path::PathBuf;

use anyhow::anyhow;
use async_trait::async_trait;
use log::*;
Expand Down Expand Up @@ -65,4 +67,8 @@ impl ProcessDefinition for MinotariWallet {

Ok(command)
}

fn get_relative_data_path(&self) -> Option<PathBuf> {
Some("data".into())
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2024 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use std::path::PathBuf;

use anyhow::anyhow;
use async_trait::async_trait;
use log::debug;
Expand Down Expand Up @@ -63,4 +65,8 @@ impl ProcessDefinition for ValidatorNode {

Ok(command)
}

fn get_relative_data_path(&self) -> Option<PathBuf> {
Some("data".into())
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Copyright 2024 The Tari Project
// SPDX-License-Identifier: BSD-3-Clause

use std::path::PathBuf;

use anyhow::anyhow;
use async_trait::async_trait;
use tokio::process::Command;
Expand Down Expand Up @@ -69,4 +71,8 @@ impl ProcessDefinition for WalletDaemon {

Ok(command)
}

fn get_relative_data_path(&self) -> Option<PathBuf> {
Some("data".into())
}
}
16 changes: 16 additions & 0 deletions applications/tari_swarm_daemon/src/process_manager/handle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ pub enum ProcessManagerRequest {
instance_id: InstanceId,
reply: Reply<()>,
},
DeleteInstanceData {
instance_id: InstanceId,
reply: Reply<()>,
},
MineBlocks {
blocks: u64,
reply: Reply<()>,
Expand Down Expand Up @@ -200,6 +204,18 @@ impl ProcessManagerHandle {
rx_reply.await?
}

pub async fn delete_instance_data(&self, instance_id: InstanceId) -> anyhow::Result<()> {
let (tx_reply, rx_reply) = oneshot::channel();
self.tx_request
.send(ProcessManagerRequest::DeleteInstanceData {
instance_id,
reply: tx_reply,
})
.await?;

rx_reply.await?
}

pub async fn register_validator_node(&self, instance_id: InstanceId) -> anyhow::Result<()> {
let (tx_reply, rx_reply) = oneshot::channel();
self.tx_request
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::{
};

use anyhow::anyhow;
use log::info;
use tari_common::configuration::Network;
use tokio::{
fs,
Expand Down Expand Up @@ -300,6 +301,26 @@ impl InstanceManager {
Ok(())
}

pub async fn delete_instance_data(&mut self, id: InstanceId) -> anyhow::Result<()> {
let instance = self
.instances_mut()
.find(|i| i.id() == id)
.ok_or_else(|| anyhow!("Instance not found"))?;

let definition = get_definition(instance.instance_type());

if let Some(data_path) = definition.get_relative_data_path() {
let path = instance.base_path().join(data_path);
info!(
"Deleting data directory for instance {}: {}",
instance.name(),
path.display()
);
fs::remove_dir_all(path).await?;
}
Ok(())
}

pub fn instances_mut(&mut self) -> impl Iterator<Item = &mut Instance> {
self.minotari_nodes
.values_mut()
Expand Down
6 changes: 6 additions & 0 deletions applications/tari_swarm_daemon/src/process_manager/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ impl ProcessManager {
log::warn!("Request cancelled before response could be sent")
}
},
DeleteInstanceData { instance_id, reply } => {
let result = self.instance_manager.delete_instance_data(instance_id).await;
if reply.send(result).is_err() {
log::warn!("Request cancelled before response could be sent")
}
},
MineBlocks { blocks, reply } => {
let result = self.mine(blocks).await;
if reply.send(result).is_err() {
Expand Down
32 changes: 32 additions & 0 deletions applications/tari_swarm_daemon/src/webserver/rpc/instances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,35 @@ pub async fn stop(context: &HandlerContext, req: StopInstanceRequest) -> Result<

Ok(StopInstanceResponse { success: true })
}

#[derive(Debug, Clone, Deserialize)]
pub struct DeleteInstanceDataRequest {
pub name: String,
}

#[derive(Debug, Clone, Serialize)]
pub struct DeleteInstanceDataResponse {
pub success: bool,
}

pub async fn delete_data(
context: &HandlerContext,
req: DeleteInstanceDataRequest,
) -> Result<DeleteInstanceDataResponse, anyhow::Error> {
let instance = context
.process_manager()
.get_instance_by_name(req.name)
.await?
.ok_or_else(|| {
JsonRpcError::new(
JsonRpcErrorReason::ApplicationError(404),
"Instance not found".to_string(),
serde_json::Value::Null,
)
})?;

context.process_manager().stop_instance(instance.id).await?;
context.process_manager().delete_instance_data(instance.id).await?;

Ok(DeleteInstanceDataResponse { success: true })
}
1 change: 1 addition & 0 deletions applications/tari_swarm_daemon/src/webserver/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ async fn json_rpc_handler(Extension(context): Extension<Arc<HandlerContext>>, va
"add_validator_node" => call_handler(context, value, rpc::validator_nodes::create).await,
"start" => call_handler(context, value, rpc::instances::start).await,
"stop" => call_handler(context, value, rpc::instances::stop).await,
"delete_data" => call_handler(context, value, rpc::instances::delete_data).await,
_ => Ok(value.method_not_found(&value.method)),
}
}
Expand Down
Loading

0 comments on commit 3941ac4

Please sign in to comment.