Skip to content

Commit

Permalink
Do not call worker enumeration for invokes (#1159)
Browse files Browse the repository at this point in the history
* Do not call worker enumeration for invokes

* Fix
  • Loading branch information
vigoo authored Dec 9, 2024
1 parent b20dcf0 commit 68407d5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 35 deletions.
4 changes: 4 additions & 0 deletions golem-cli/src/clients/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ pub trait WorkerClient {
async fn simulated_crash(&self, worker_urn: WorkerUrn) -> Result<(), GolemError>;
async fn delete(&self, worker_urn: WorkerUrn) -> Result<(), GolemError>;
async fn get_metadata(&self, worker_urn: WorkerUrn) -> Result<WorkerMetadata, GolemError>;
async fn get_metadata_opt(
&self,
worker_urn: WorkerUrn,
) -> Result<Option<WorkerMetadata>, GolemError>;
async fn find_metadata(
&self,
component_urn: ComponentUrn,
Expand Down
20 changes: 20 additions & 0 deletions golem-cli/src/oss/clients/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,26 @@ impl<C: golem_client::api::WorkerClient + Sync + Send> WorkerClient for WorkerCl
.into())
}

async fn get_metadata_opt(
&self,
worker_urn: WorkerUrn,
) -> Result<Option<WorkerMetadata>, GolemError> {
info!("Getting worker {worker_urn} metadata");

match self
.client
.get_worker_metadata(
&worker_urn.id.component_id.0,
&worker_name_required(&worker_urn)?,
)
.await
{
Ok(metadata) => Ok(Some(metadata.into())),
Err(Error::Item(WorkerError::Error404(_))) => Ok(None),
Err(err) => Err(err.into()),
}
}

async fn find_metadata(
&self,
component_urn: ComponentUrn,
Expand Down
44 changes: 12 additions & 32 deletions golem-cli/src/service/worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use crate::model::{
};
use crate::service::component::ComponentService;
use async_trait::async_trait;
use golem_client::model::{AnalysedType, InvokeParameters, InvokeResult, ScanCursor, WorkerFilter};
use golem_common::model::{StringFilterComparator, TargetWorkerId, WorkerNameFilter};
use golem_client::model::{AnalysedType, InvokeParameters, InvokeResult, ScanCursor};
use golem_common::model::TargetWorkerId;
use golem_common::uri::oss::uri::{ComponentUri, WorkerUri};
use golem_common::uri::oss::url::{ComponentUrl, WorkerUrl};
use golem_common::uri::oss::urn::{ComponentUrn, WorkerUrn};
Expand Down Expand Up @@ -216,37 +216,17 @@ async fn resolve_worker_component_version<ProjectContext: Send + Sync>(
components: &(dyn ComponentService<ProjectContext = ProjectContext> + Send + Sync),
worker_urn: WorkerUrn,
) -> Result<Option<Component>, GolemError> {
let TargetWorkerId {
component_id,
worker_name,
} = worker_urn.id;

if let Some(worker_name) = worker_name {
let component_urn = ComponentUrn { id: component_id };

let worker_meta = client
.find_metadata(
component_urn.clone(),
Some(WorkerFilter::Name(WorkerNameFilter {
comparator: StringFilterComparator::Equal,
value: worker_name,
})),
None,
Some(2),
Some(true),
)
.await?;
if worker_urn.id.worker_name.is_some() {
let component_urn = ComponentUrn {
id: worker_urn.id.component_id.clone(),
};

if worker_meta.workers.len() > 1 {
Err(GolemError(
"Multiple workers with the same name".to_string(),
))
} else if let Some(worker) = worker_meta.workers.first() {
Ok(Some(
components
.get_metadata(&component_urn, worker.component_version)
.await?,
))
let worker_metadata = client.get_metadata_opt(worker_urn).await?;
if let Some(worker_metadata) = worker_metadata {
let component_metadata = components
.get_metadata(&component_urn, worker_metadata.component_version)
.await?;
Ok(Some(component_metadata))
} else {
Ok(None)
}
Expand Down
11 changes: 8 additions & 3 deletions golem-worker-service-base/src/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,14 @@ impl From<WorkerServiceError> for WorkerApiBaseError {
| ServiceError::WorkerNotFound(_) => WorkerApiBaseError::NotFound(Json(ErrorBody {
error: error.to_safe_string(),
})),
ServiceError::Golem(golem_error) => {
WorkerApiBaseError::InternalError(Json(GolemErrorBody { golem_error }))
}
ServiceError::Golem(golem_error) => match golem_error {
GolemError::WorkerNotFound(error) => {
WorkerApiBaseError::NotFound(Json(ErrorBody {
error: error.to_safe_string(),
}))
}
_ => WorkerApiBaseError::InternalError(Json(GolemErrorBody { golem_error })),
},
ServiceError::Component(error) => error.into(),
ServiceError::InternalCallError(_) => internal(error.to_safe_string()),
ServiceError::FileNotFound(_) => WorkerApiBaseError::NotFound(Json(ErrorBody {
Expand Down

0 comments on commit 68407d5

Please sign in to comment.