Skip to content

Commit

Permalink
new: Add MOON_DEBUG_REMOTE.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Jan 27, 2025
1 parent 308560b commit 896fd4c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 41 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
based endpoints. Can also be used to set headers for all requests.
- Added support for Depot cloud-based caching: https://depot.dev/docs/cache/overview
- Added support for the HTTP protocol: https://bazel.build/remote/caching#http-caching
- Added a `MOON_DEBUG_REMOTE` environment variable, which can be used to debug internal errors for
diagnosing connection/integration issues.

## 1.31.3

Expand Down
73 changes: 42 additions & 31 deletions crates/remote/src/grpc_remote_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,20 @@ use bazel_remote_apis::build::bazel::remote::execution::v2::{
use miette::IntoDiagnostic;
use moon_common::color;
use moon_config::RemoteConfig;
use starbase_utils::env::bool_var;
use std::{env, path::Path, str::FromStr};
use tonic::{
metadata::{KeyAndValueRef, MetadataKey, MetadataMap, MetadataValue},
transport::{Channel, Endpoint},
Code, Request, Status,
};
use tracing::{debug, trace, warn};

fn map_transport_error(error: tonic::transport::Error) -> RemoteError {
// dbg!(&error);
RemoteError::GrpcConnectFailed {
error: Box::new(error),
}
}

fn map_status_error(error: tonic::Status) -> RemoteError {
// dbg!(&error);
RemoteError::GrpcCallFailed {
error: Box::new(error),
}
}
use tracing::{debug, error, trace, warn};

#[derive(Default)]
pub struct GrpcRemoteClient {
channel: Option<Channel>,
config: RemoteConfig,
debug: bool,
headers: MetadataMap,
}

Expand Down Expand Up @@ -96,6 +84,26 @@ impl GrpcRemoteClient {

Ok(req)
}

fn map_status_error(&self, method: &str, error: tonic::Status) -> RemoteError {
if self.debug {
error!("{method}: {:#?}", error);
}

RemoteError::GrpcCallFailed {
error: Box::new(error),
}
}

fn map_transport_error(&self, method: &str, error: tonic::transport::Error) -> RemoteError {
if self.debug {
error!("{method}: {:#?}", error);
}

RemoteError::GrpcConnectFailed {
error: Box::new(error),
}
}
}

#[async_trait::async_trait]
Expand All @@ -105,6 +113,8 @@ impl RemoteClient for GrpcRemoteClient {
config: &RemoteConfig,
workspace_root: &Path,
) -> miette::Result<bool> {
self.debug = bool_var("MOON_DEBUG_REMOTE");

let host = &config.host;

debug!(
Expand All @@ -131,23 +141,23 @@ impl RemoteClient for GrpcRemoteClient {
};

let mut endpoint = Endpoint::from_shared(url)
.map_err(map_transport_error)?
.map_err(|error| self.map_transport_error("host", error))?
.user_agent("moon")
.map_err(map_transport_error)?
.map_err(|error| self.map_transport_error("user_agent", error))?
.keep_alive_while_idle(true);

if let Some(mtls) = &config.mtls {
endpoint = endpoint
.tls_config(create_mtls_config(mtls, workspace_root)?)
.map_err(map_transport_error)?;
.map_err(|error| self.map_transport_error("tls", error))?;
} else if let Some(tls) = &config.tls {
endpoint = endpoint
.tls_config(create_tls_config(tls, workspace_root)?)
.map_err(map_transport_error)?;
.map_err(|error| self.map_transport_error("mtls", error))?;
} else if config.is_secure_protocol() {
endpoint = endpoint
.tls_config(create_native_tls_config()?)
.map_err(map_transport_error)?;
.map_err(|error| self.map_transport_error("auth", error))?;
}

if config.is_localhost() {
Expand All @@ -169,7 +179,12 @@ impl RemoteClient for GrpcRemoteClient {
if self.config.is_bearer_auth() {
self.channel = Some(endpoint.connect_lazy());
} else {
self.channel = Some(endpoint.connect().await.map_err(map_transport_error)?);
self.channel = Some(
endpoint
.connect()
.await
.map_err(|error| self.map_transport_error("connect_to_host", error))?,
);
}

Ok(enabled)
Expand All @@ -189,7 +204,7 @@ impl RemoteClient for GrpcRemoteClient {
instance_name: self.config.cache.instance_name.clone(),
})
.await
.map_err(map_status_error)?;
.map_err(|error| self.map_status_error("load_capabilities", error))?;

Ok(response.into_inner())
}
Expand Down Expand Up @@ -245,8 +260,7 @@ impl RemoteClient for GrpcRemoteClient {

Ok(None)
} else {
// dbg!("get_action_result", digest);
Err(map_status_error(status).into())
Err(self.map_status_error("get_action_result", status).into())
}
}
}
Expand Down Expand Up @@ -309,8 +323,7 @@ impl RemoteClient for GrpcRemoteClient {

Ok(None)
} else {
// dbg!("update_action_result", digest);
Err(map_status_error(status).into())
Err(self.map_status_error("update_action_result", status).into())
}
}
}
Expand All @@ -332,7 +345,7 @@ impl RemoteClient for GrpcRemoteClient {
.await
{
Ok(response) => Ok(response.into_inner().missing_blob_digests),
Err(status) => Err(map_status_error(status).into()),
Err(status) => Err(self.map_status_error("find_missing_blobs", status).into()),
}
}

Expand Down Expand Up @@ -373,8 +386,7 @@ impl RemoteClient for GrpcRemoteClient {

Ok(vec![])
} else {
// dbg!("batch_read_blobs", digest);
Err(map_status_error(status).into())
Err(self.map_status_error("batch_read_blobs", status).into())
};
}
};
Expand Down Expand Up @@ -483,8 +495,7 @@ impl RemoteClient for GrpcRemoteClient {

Ok(vec![])
} else {
// dbg!("batch_update_blobs", digest);
Err(map_status_error(status).into())
Err(self.map_status_error("batch_update_blobs", status).into())
};
}
};
Expand Down
42 changes: 32 additions & 10 deletions crates/remote/src/http_remote_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@ use moon_common::color;
use moon_config::{RemoteCompression, RemoteConfig};
use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use reqwest::Client;
use starbase_utils::env::bool_var;
use std::env;
use std::sync::Arc;
use std::time::Duration;
use std::{path::Path, sync::OnceLock};
use tokio::sync::Semaphore;
use tracing::{debug, trace, warn};
use tracing::{debug, error, trace, warn};

pub struct HttpRemoteClient {
client: OnceLock<Arc<Client>>,
config: RemoteConfig,
debug: bool,

// Since HTTP doesn't support batching, we will most likely
// end up up/downloading too many files in parallel, triggering a
Expand All @@ -35,6 +37,7 @@ impl Default for HttpRemoteClient {
Self {
client: OnceLock::new(),
config: RemoteConfig::default(),
debug: false,
semaphore: Arc::new(Semaphore::new(100)),
}
}
Expand Down Expand Up @@ -89,7 +92,9 @@ impl HttpRemoteClient {
client = create_native_tls_config(client)?;
}

let client = client.build().into_diagnostic()?;
let client = client
.build()
.map_err(|error| self.map_error("create_client", error))?;

Ok(Some(client))
}
Expand All @@ -104,6 +109,16 @@ impl HttpRemoteClient {
self.config.host, self.config.cache.instance_name
)
}

fn map_error(&self, method: &str, error: reqwest::Error) -> RemoteError {
if self.debug {
error!("{method}: {:#?}", error);
}

RemoteError::HttpCallFailed {
error: Box::new(error),
}
}
}

#[async_trait::async_trait]
Expand All @@ -113,6 +128,8 @@ impl RemoteClient for HttpRemoteClient {
config: &RemoteConfig,
workspace_root: &Path,
) -> miette::Result<bool> {
self.debug = bool_var("MOON_DEBUG_REMOTE");

let host = &config.host;

debug!(
Expand Down Expand Up @@ -230,10 +247,7 @@ impl RemoteClient for HttpRemoteClient {
Ok(None)
}
}
Err(error) => Err(RemoteError::HttpCallFailed {
error: Box::new(error),
}
.into()),
Err(error) => Err(self.map_error("get_action_result", error).into()),
}
}

Expand Down Expand Up @@ -279,10 +293,7 @@ impl RemoteClient for HttpRemoteClient {
Ok(None)
}
}
Err(error) => Err(RemoteError::HttpCallFailed {
error: Box::new(error),
}
.into()),
Err(error) => Err(self.map_error("update_action_result", error).into()),
}
}

Expand All @@ -305,6 +316,7 @@ impl RemoteClient for HttpRemoteClient {
);

let mut requests = vec![];
let debug_enabled = self.debug;

for blob_digest in blob_digests {
let client = self.get_client();
Expand Down Expand Up @@ -342,6 +354,10 @@ impl RemoteClient for HttpRemoteClient {
blob_hash = &blob_digest.hash,
"Failed to download blob: {error}",
);

if debug_enabled {
trace!("read_blob: {:?}", error);
}
}
}

Expand Down Expand Up @@ -383,6 +399,8 @@ impl RemoteClient for HttpRemoteClient {
blobs.len()
);

let debug_enabled = self.debug;

for blob in blobs {
let client = self.get_client();
let action_hash = digest.hash.clone();
Expand Down Expand Up @@ -414,6 +432,10 @@ impl RemoteClient for HttpRemoteClient {
blob_hash = &blob.digest.hash,
"Failed to upload blob: {error}",
);

if debug_enabled {
trace!("update_blob: {:?}", error);
}
}
}

Expand Down

0 comments on commit 896fd4c

Please sign in to comment.