From e403bd3be6912702d98ed106ed344ed5a2353bfb Mon Sep 17 00:00:00 2001 From: Yihau Chen Date: Thu, 14 Mar 2024 20:35:33 +0800 Subject: [PATCH] [anza migration]: add 'agave=info' to default log level (#223) (cherry picked from commit 51dc7e6fb7589344497ce030950663c800685408) # Conflicts: # cargo-registry/src/main.rs --- bench-tps/src/main.rs | 2 +- cargo-registry/src/main.rs | 289 +++++++++++++++++++++++++++++++++ dos/src/main.rs | 2 +- faucet/src/bin/faucet.rs | 2 +- gossip/src/main.rs | 2 +- ledger-tool/src/main.rs | 2 +- logger/src/lib.rs | 7 + runtime/store-tool/src/main.rs | 2 +- scripts/run.sh | 2 +- transaction-dos/src/main.rs | 2 +- validator/src/lib.rs | 5 +- watchtower/src/main.rs | 2 +- 12 files changed, 307 insertions(+), 12 deletions(-) create mode 100644 cargo-registry/src/main.rs diff --git a/bench-tps/src/main.rs b/bench-tps/src/main.rs index 519612bd4237a7..e9cc2100635912 100644 --- a/bench-tps/src/main.rs +++ b/bench-tps/src/main.rs @@ -221,7 +221,7 @@ fn create_client( } fn main() { - solana_logger::setup_with_default("solana=info"); + solana_logger::setup_with_default_filter(); solana_metrics::set_panic_hook("bench-tps", /*version:*/ None); let matches = cli::build_args(solana_version::version!()).get_matches(); diff --git a/cargo-registry/src/main.rs b/cargo-registry/src/main.rs new file mode 100644 index 00000000000000..317e86341927f7 --- /dev/null +++ b/cargo-registry/src/main.rs @@ -0,0 +1,289 @@ +//! The `registry_service` module implements the Solana cargo registry service. +use { + crate::{ + client::Client, + crate_handler::{Error, Program, UnpackedCrate}, + sparse_index::RegistryIndex, + }, + hyper::{ + body, + service::{make_service_fn, service_fn}, + Method, Server, + }, + log::*, + std::{ + net::{IpAddr, Ipv4Addr, SocketAddr}, + sync::Arc, + }, +}; + +mod client; +mod crate_handler; + +mod response_builder; +mod sparse_index; + +const PATH_PREFIX: &str = "/api/v1/crates"; + +pub struct CargoRegistryService {} + +impl CargoRegistryService { + async fn handle_publish_request( + request: hyper::Request, + client: Arc, + index: Arc, + ) -> hyper::Response { + info!("Handling request to publish the crate"); + let bytes = body::to_bytes(request.into_body()).await; + + match bytes { + Ok(data) => { + let Ok(unpacked_crate) = UnpackedCrate::new(data) else { + return response_builder::error_response( + hyper::StatusCode::INTERNAL_SERVER_ERROR, + "Failed to parse the crate information", + ); + }; + let Ok(result) = + tokio::task::spawn_blocking(move || unpacked_crate.publish(client, index)) + .await + else { + return response_builder::error_response( + hyper::StatusCode::INTERNAL_SERVER_ERROR, + "Internal error. Failed to wait for program deployment", + ); + }; + + if result.is_ok() { + info!("Published the crate successfully. {:?}", result); + response_builder::success_response() + } else { + response_builder::error_response( + hyper::StatusCode::BAD_REQUEST, + format!("Failed to publish the crate. {:?}", result).as_str(), + ) + } + } + Err(_) => response_builder::error_response( + hyper::StatusCode::BAD_REQUEST, + "Failed to receive the crate data from the client.", + ), + } + } + + fn get_crate_name_and_version(path: &str) -> Option<(&str, &str, &str)> { + path.rsplit_once('/').and_then(|(remainder, version)| { + remainder + .rsplit_once('/') + .map(|(remainder, name)| (remainder, name, version)) + }) + } + + fn handle_download_crate_request( + path: &str, + _request: &hyper::Request, + client: Arc, + ) -> hyper::Response { + let Some((path, crate_name, version)) = Self::get_crate_name_and_version(path) else { + return response_builder::error_in_parsing(); + }; + + if path.len() != PATH_PREFIX.len() { + return response_builder::error_incorrect_length(); + } + + let package = Program::crate_name_to_program_id(crate_name) + .and_then(|id| UnpackedCrate::fetch(id, version, client).ok()); + + // Return the package to the caller in the response + if let Some((package, _meta)) = package { + response_builder::success_response_bytes(package.0) + } else { + response_builder::error_response( + hyper::StatusCode::BAD_REQUEST, + "Failed to find the package", + ) + } + } + + fn handle_yank_request( + path: &str, + _request: &hyper::Request, + ) -> hyper::Response { + let Some((path, _crate_name, _version)) = Self::get_crate_name_and_version(path) else { + return response_builder::error_in_parsing(); + }; + + if path.len() != PATH_PREFIX.len() { + return response_builder::error_incorrect_length(); + } + + response_builder::error_not_implemented() + } + + fn handle_unyank_request( + path: &str, + _request: &hyper::Request, + ) -> hyper::Response { + let Some((path, _crate_name, _version)) = Self::get_crate_name_and_version(path) else { + return response_builder::error_in_parsing(); + }; + + if path.len() != PATH_PREFIX.len() { + return response_builder::error_incorrect_length(); + } + + response_builder::error_not_implemented() + } + + fn get_crate_name(path: &str) -> Option<(&str, &str)> { + path.rsplit_once('/') + } + + fn handle_get_owners_request( + path: &str, + _request: &hyper::Request, + ) -> hyper::Response { + let Some((path, _crate_name)) = Self::get_crate_name(path) else { + return response_builder::error_in_parsing(); + }; + + if path.len() != PATH_PREFIX.len() { + return response_builder::error_incorrect_length(); + } + + response_builder::error_not_implemented() + } + + fn handle_add_owners_request( + path: &str, + _request: &hyper::Request, + ) -> hyper::Response { + let Some((path, _crate_name)) = Self::get_crate_name(path) else { + return response_builder::error_in_parsing(); + }; + + if path.len() != PATH_PREFIX.len() { + return response_builder::error_incorrect_length(); + } + + response_builder::error_not_implemented() + } + + fn handle_delete_owners_request( + path: &str, + _request: &hyper::Request, + ) -> hyper::Response { + let Some((path, _crate_name)) = Self::get_crate_name(path) else { + return response_builder::error_in_parsing(); + }; + + if path.len() != PATH_PREFIX.len() { + return response_builder::error_incorrect_length(); + } + + response_builder::error_not_implemented() + } + + fn handle_get_crates_request( + path: &str, + _request: &hyper::Request, + ) -> hyper::Response { + // The endpoint for this type of request is `/api/v1/crates` (same as PATH_PREFIX). + // The `crates` substring has already been extracted out of the endpoint string. + // So the path should only contain `/api/v1". The caller already checked that the + // full path started with PATH_PREFIX. So it's sufficient to check that provided + // path is smaller than PATH_PREFIX. + if path.len() >= PATH_PREFIX.len() { + return response_builder::error_incorrect_length(); + } + + response_builder::error_not_implemented() + } + + async fn handler( + index: Arc, + request: hyper::Request, + client: Arc, + ) -> Result, Error> { + let path = request.uri().path(); + if path.starts_with("/git") { + return Ok(response_builder::error_response( + hyper::StatusCode::BAD_REQUEST, + "This registry server does not support GIT index. Please use sparse index.", + )); + } + + if path.starts_with(index.index_root.as_str()) { + return Ok(index.handler(request, client.clone())); + } + + if !path.starts_with(PATH_PREFIX) { + return Ok(response_builder::error_response( + hyper::StatusCode::BAD_REQUEST, + "Invalid path for the request", + )); + } + + let Some((path, endpoint)) = path.rsplit_once('/') else { + return Ok(response_builder::error_response( + hyper::StatusCode::BAD_REQUEST, + "Invalid endpoint in the path", + )); + }; + + Ok(match *request.method() { + Method::PUT => match endpoint { + "new" => { + if path.len() != PATH_PREFIX.len() { + response_builder::error_incorrect_length() + } else { + Self::handle_publish_request(request, client.clone(), index.clone()).await + } + } + "unyank" => Self::handle_unyank_request(path, &request), + "owners" => Self::handle_add_owners_request(path, &request), + _ => response_builder::error_not_allowed(), + }, + Method::GET => match endpoint { + "crates" => Self::handle_get_crates_request(path, &request), + "owners" => Self::handle_get_owners_request(path, &request), + "download" => Self::handle_download_crate_request(path, &request, client.clone()), + _ => response_builder::error_not_allowed(), + }, + Method::DELETE => match endpoint { + "yank" => Self::handle_yank_request(path, &request), + "owners" => Self::handle_delete_owners_request(path, &request), + _ => response_builder::error_not_allowed(), + }, + _ => response_builder::error_not_allowed(), + }) + } +} + +#[tokio::main] +async fn main() { + solana_logger::setup_with_default_filter(); + let client = Arc::new(Client::new().expect("Failed to get RPC Client instance")); + + let bind_addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(0, 0, 0, 0)), client.port); + let index = Arc::new(sparse_index::RegistryIndex::new( + "/index", + &client.server_url, + )); + + let registry_service = make_service_fn(move |_| { + let client_inner = client.clone(); + let index = index.clone(); + async move { + Ok::<_, Error>(service_fn(move |request| { + CargoRegistryService::handler(index.clone(), request, client_inner.clone()) + })) + } + }); + + let server = Server::bind(&bind_addr).serve(registry_service); + info!("Server running on http://{}", bind_addr); + + let _ = server.await; +} diff --git a/dos/src/main.rs b/dos/src/main.rs index 8e6c3c5b2b11b5..6a7968669f4c55 100644 --- a/dos/src/main.rs +++ b/dos/src/main.rs @@ -755,7 +755,7 @@ fn run_dos( } fn main() { - solana_logger::setup_with_default("solana=info"); + solana_logger::setup_with_default_filter(); let cmd_params = build_cli_parameters(); let (nodes, client) = if !cmd_params.skip_gossip { diff --git a/faucet/src/bin/faucet.rs b/faucet/src/bin/faucet.rs index 8e45ef98155a54..56cc7542623185 100644 --- a/faucet/src/bin/faucet.rs +++ b/faucet/src/bin/faucet.rs @@ -19,7 +19,7 @@ use { async fn main() { let default_keypair = solana_cli_config::Config::default().keypair_path; - solana_logger::setup_with_default("solana=info"); + solana_logger::setup_with_default_filter(); solana_metrics::set_panic_hook("faucet", /*version:*/ None); let matches = App::new(crate_name!()) .about(crate_description!()) diff --git a/gossip/src/main.rs b/gossip/src/main.rs index 226fab8d9d43a8..1f31195f431d36 100644 --- a/gossip/src/main.rs +++ b/gossip/src/main.rs @@ -326,7 +326,7 @@ fn process_rpc_url( } fn main() -> Result<(), Box> { - solana_logger::setup_with_default("solana=info"); + solana_logger::setup_with_default_filter(); let matches = parse_matches(); let socket_addr_space = SocketAddrSpace::new(matches.is_present("allow_private_addr")); diff --git a/ledger-tool/src/main.rs b/ledger-tool/src/main.rs index aeed15fc9c9bbf..f7b25ec279636c 100644 --- a/ledger-tool/src/main.rs +++ b/ledger-tool/src/main.rs @@ -1081,7 +1081,7 @@ fn main() { const DEFAULT_MAX_FULL_SNAPSHOT_ARCHIVES_TO_RETAIN: usize = std::usize::MAX; const DEFAULT_MAX_INCREMENTAL_SNAPSHOT_ARCHIVES_TO_RETAIN: usize = std::usize::MAX; - solana_logger::setup_with_default("solana=info"); + solana_logger::setup_with_default_filter(); let starting_slot_arg = Arg::with_name("starting_slot") .long("starting-slot") diff --git a/logger/src/lib.rs b/logger/src/lib.rs index 7b9ae30d881053..aa8dc5ca1db0ef 100644 --- a/logger/src/lib.rs +++ b/logger/src/lib.rs @@ -10,6 +10,8 @@ lazy_static! { Arc::new(RwLock::new(env_logger::Logger::from_default_env())); } +pub const DEFAULT_FILTER: &str = "solana=info,agave=info"; + struct LoggerShim {} impl log::Log for LoggerShim { @@ -49,6 +51,11 @@ pub fn setup_with_default(filter: &str) { replace_logger(logger); } +// Configures logging with the `DEFAULT_FILTER` if RUST_LOG is not set +pub fn setup_with_default_filter() { + setup_with_default(DEFAULT_FILTER); +} + // Configures logging with the default filter "error" if RUST_LOG is not set pub fn setup() { setup_with_default("error"); diff --git a/runtime/store-tool/src/main.rs b/runtime/store-tool/src/main.rs index 98140ed59ba0ea..8c2cf80148882d 100644 --- a/runtime/store-tool/src/main.rs +++ b/runtime/store-tool/src/main.rs @@ -11,7 +11,7 @@ use { }; fn main() { - solana_logger::setup_with_default("solana=info"); + solana_logger::setup_with_default_filter(); let matches = App::new(crate_name!()) .about(crate_description!()) .version(solana_version::version!()) diff --git a/scripts/run.sh b/scripts/run.sh index 2d8e1ec88ac450..70994c921f47ac 100755 --- a/scripts/run.sh +++ b/scripts/run.sh @@ -37,7 +37,7 @@ $ok || { exit 1 } -export RUST_LOG=${RUST_LOG:-solana=info,solana_runtime::message_processor=debug} # if RUST_LOG is unset, default to info +export RUST_LOG=${RUST_LOG:-solana=info,agave=info,solana_runtime::message_processor=debug} # if RUST_LOG is unset, default to info export RUST_BACKTRACE=1 dataDir=$PWD/config/"$(basename "$0" .sh)" ledgerDir=$PWD/config/ledger diff --git a/transaction-dos/src/main.rs b/transaction-dos/src/main.rs index 5d69e9e291b6b5..11f8548227cb58 100644 --- a/transaction-dos/src/main.rs +++ b/transaction-dos/src/main.rs @@ -425,7 +425,7 @@ fn run_transactions_dos( } fn main() { - solana_logger::setup_with_default("solana=info"); + solana_logger::setup_with_default_filter(); let matches = App::new(crate_name!()) .about(crate_description!()) .version(solana_version::version!()) diff --git a/validator/src/lib.rs b/validator/src/lib.rs index 4e7ed43ec78309..fab90fa98e06ca 100644 --- a/validator/src/lib.rs +++ b/validator/src/lib.rs @@ -46,10 +46,9 @@ pub fn redirect_stderr_to_file(logfile: Option) -> Option env::set_var("RUST_BACKTRACE", "1") } - let filter = "solana=info"; match logfile { None => { - solana_logger::setup_with_default(filter); + solana_logger::setup_with_default_filter(); None } Some(logfile) => { @@ -63,7 +62,7 @@ pub fn redirect_stderr_to_file(logfile: Option) -> Option exit(1); }); - solana_logger::setup_with_default(filter); + solana_logger::setup_with_default_filter(); redirect_stderr(&logfile); Some( std::thread::Builder::new() diff --git a/watchtower/src/main.rs b/watchtower/src/main.rs index 341b7903c0a33e..11dd70e27285dc 100644 --- a/watchtower/src/main.rs +++ b/watchtower/src/main.rs @@ -246,7 +246,7 @@ fn get_cluster_info( } fn main() -> Result<(), Box> { - solana_logger::setup_with_default("solana=info"); + solana_logger::setup_with_default_filter(); solana_metrics::set_panic_hook("watchtower", /*version:*/ None); let config = get_config();