Skip to content

Commit

Permalink
node: Add a command 'graphman config provider'
Browse files Browse the repository at this point in the history
  • Loading branch information
lutter committed Aug 5, 2022
1 parent 209786c commit af5f0f1
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 10 deletions.
18 changes: 13 additions & 5 deletions chain/ethereum/src/network.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,31 @@ pub struct EthereumNetworkAdapters {
}

impl EthereumNetworkAdapters {
pub fn cheapest_with(
pub fn all_cheapest_with(
&self,
required_capabilities: &NodeCapabilities,
) -> Result<Arc<EthereumAdapter>, Error> {
) -> impl Iterator<Item = Arc<EthereumAdapter>> + '_ {
let cheapest_sufficient_capability = self
.adapters
.iter()
.find(|adapter| &adapter.capabilities >= required_capabilities)
.map(|adapter| &adapter.capabilities);

// Select randomly from the cheapest adapters that have sufficent capabilities.
self.adapters
.iter()
.filter(|adapter| Some(&adapter.capabilities) == cheapest_sufficient_capability)
.filter(move |adapter| Some(&adapter.capabilities) == cheapest_sufficient_capability)
.filter(|adapter| Arc::strong_count(&adapter.adapter) < adapter.limit)
.choose(&mut rand::thread_rng())
.map(|adapter| adapter.adapter.cheap_clone())
}

pub fn cheapest_with(
&self,
required_capabilities: &NodeCapabilities,
) -> Result<Arc<EthereumAdapter>, Error> {
// Select randomly from the cheapest adapters that have sufficent capabilities.
self.all_cheapest_with(required_capabilities)
.into_iter()
.choose(&mut rand::thread_rng())
.with_context(|| {
anyhow!(
"A matching Ethereum network with {:?} was not found.",
Expand Down
1 change: 1 addition & 0 deletions graph/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub use task_spawn::{

pub use anyhow;
pub use bytes;
pub use itertools;
pub use parking_lot;
pub use petgraph;
pub use prometheus;
Expand Down
17 changes: 17 additions & 0 deletions node/src/bin/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,16 @@ pub enum ConfigCommand {
#[structopt(short, long)]
shard: bool,
},
/// Show eligible providers
///
/// Prints the providers that can be used for a deployment on a given
/// network with the given features. Set the name of the node for which
/// to simulate placement with the toplevel `--node-id` option
Provider {
#[structopt(short, long, default_value = "")]
features: String,
network: String,
},
}

#[derive(Clone, Debug, StructOpt)]
Expand Down Expand Up @@ -481,6 +491,7 @@ impl From<Opt> for config::Opt {
let mut config_opt = config::Opt::default();
config_opt.config = Some(opt.config);
config_opt.store_connection_pool_size = 5;
config_opt.node_id = opt.node_id;
config_opt
}
}
Expand Down Expand Up @@ -807,6 +818,12 @@ async fn main() -> anyhow::Result<()> {
}
Check { print } => commands::config::check(&ctx.config, print),
Pools { nodes, shard } => commands::config::pools(&ctx.config, nodes, shard),
Provider { features, network } => {
let logger = ctx.logger.clone();
let registry = ctx.registry.clone();
commands::config::provider(logger, &ctx.config, registry, features, network)
.await
}
}
}
Remove { name } => commands::remove::run(ctx.subgraph_store(), name),
Expand Down
59 changes: 55 additions & 4 deletions node/src/manager/commands/config.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
use std::collections::BTreeMap;
use std::{collections::BTreeMap, sync::Arc};

use graph::prelude::{
anyhow::{anyhow, Error},
NodeId,
use graph::{
anyhow::bail,
components::metrics::MetricsRegistry,
itertools::Itertools,
prelude::{
anyhow::{anyhow, Error},
NodeId,
},
slog::Logger,
};
use graph_chain_ethereum::{EthereumAdapterTrait, NodeCapabilities};
use graph_store_postgres::DeploymentPlacer;

use crate::config::Config;
Expand Down Expand Up @@ -89,3 +96,47 @@ pub fn pools(config: &Config, nodes: Vec<String>, shard: bool) -> Result<(), Err
}
Ok(())
}

pub async fn provider(
logger: Logger,
config: &Config,
registry: Arc<dyn MetricsRegistry>,
features: String,
network: String,
) -> Result<(), Error> {
// Like NodeCapabilities::from_str but with error checking for typos etc.
fn caps_from_features(features: String) -> Result<NodeCapabilities, Error> {
let mut caps = NodeCapabilities {
archive: false,
traces: false,
};
for feature in features.split(',') {
match feature {
"archive" => caps.archive = true,
"traces" => caps.traces = true,
_ => bail!("unknown feature {}", feature),
}
}
Ok(caps)
}

let caps = caps_from_features(features)?;
let networks =
crate::manager::commands::run::create_ethereum_networks(logger, registry, config, &network)
.await?;
let adapters = networks
.networks
.get(&network)
.ok_or_else(|| anyhow!("unknown network {}", network))?;
let adapters = adapters.all_cheapest_with(&caps);
println!(
"deploy on network {} with features [{}] on node {}\neligible providers: {}",
network,
caps,
config.node.as_str(),
adapters
.map(|adapter| adapter.provider().to_string())
.join(", ")
);
Ok(())
}
2 changes: 1 addition & 1 deletion node/src/manager/commands/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ fn create_ipfs_clients(logger: &Logger, ipfs_addresses: &Vec<String>) -> Vec<Ipf
}

/// Parses an Ethereum connection string and returns the network name and Ethereum adapter.
async fn create_ethereum_networks(
pub async fn create_ethereum_networks(
logger: Logger,
registry: Arc<dyn MetricsRegistryTrait>,
config: &Config,
Expand Down

0 comments on commit af5f0f1

Please sign in to comment.