Skip to content

Commit

Permalink
[anza migration]: add 'agave=info' to default log level (#223)
Browse files Browse the repository at this point in the history
(cherry picked from commit 51dc7e6)

# Conflicts:
#	cargo-registry/src/main.rs
  • Loading branch information
yihau authored and mergify[bot] committed Mar 14, 2024
1 parent 3809dc0 commit e403bd3
Show file tree
Hide file tree
Showing 12 changed files with 307 additions and 12 deletions.
2 changes: 1 addition & 1 deletion bench-tps/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
289 changes: 289 additions & 0 deletions cargo-registry/src/main.rs
Original file line number Diff line number Diff line change
@@ -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<hyper::Body>,
client: Arc<Client>,
index: Arc<RegistryIndex>,
) -> hyper::Response<hyper::Body> {
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<hyper::Body>,
client: Arc<Client>,
) -> hyper::Response<hyper::Body> {
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::Body>,
) -> hyper::Response<hyper::Body> {
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::Body>,
) -> hyper::Response<hyper::Body> {
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::Body>,
) -> hyper::Response<hyper::Body> {
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::Body>,
) -> hyper::Response<hyper::Body> {
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::Body>,
) -> hyper::Response<hyper::Body> {
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::Body>,
) -> hyper::Response<hyper::Body> {
// 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<sparse_index::RegistryIndex>,
request: hyper::Request<hyper::Body>,
client: Arc<Client>,
) -> Result<hyper::Response<hyper::Body>, 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;
}
2 changes: 1 addition & 1 deletion dos/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -755,7 +755,7 @@ fn run_dos<T: 'static + BenchTpsClient + Send + Sync>(
}

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 {
Expand Down
2 changes: 1 addition & 1 deletion faucet/src/bin/faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!())
Expand Down
2 changes: 1 addition & 1 deletion gossip/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ fn process_rpc_url(
}

fn main() -> Result<(), Box<dyn error::Error>> {
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"));
Expand Down
2 changes: 1 addition & 1 deletion ledger-tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 7 additions & 0 deletions logger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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");
Expand Down
2 changes: 1 addition & 1 deletion runtime/store-tool/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!())
Expand Down
2 changes: 1 addition & 1 deletion scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion transaction-dos/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!())
Expand Down
Loading

0 comments on commit e403bd3

Please sign in to comment.