Skip to content

Commit

Permalink
Renamings and fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
afsalthaj committed Oct 3, 2024
1 parent 0044a1e commit 0b92194
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 33 deletions.
2 changes: 2 additions & 0 deletions golem-rib/src/type_inference/rib_input_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ use poem_openapi::Object;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};

// RibInputTypeInfo refers to the required global inputs to a RibScript
// with its type information. Example: `request` variable which should be of the type `Record`.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Encode, Decode, Object)]
pub struct RibInputTypeInfo {
pub types: HashMap<String, AnalysedType>,
Expand Down
25 changes: 14 additions & 11 deletions golem-worker-service-base/src/api/custom_http_request_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::worker_bridge_execution::WorkerRequestExecutor;
// This is a common API projects can make use of, similar to healthcheck service
#[derive(Clone)]
pub struct CustomHttpRequestApi {
pub evaluator: Arc<dyn WorkerServiceRibInterpreter + Sync + Send>,
pub worker_service_rib_interpreter: Arc<dyn WorkerServiceRibInterpreter + Sync + Send>,
pub api_definition_lookup_service:
Arc<dyn ApiDefinitionsLookup<InputHttpRequest, CompiledHttpApiDefinition> + Sync + Send>,
}
Expand All @@ -36,7 +36,7 @@ impl CustomHttpRequestApi {
));

Self {
evaluator,
worker_service_rib_interpreter: evaluator,
api_definition_lookup_service,
}
}
Expand Down Expand Up @@ -71,7 +71,7 @@ impl CustomHttpRequestApi {
}
};

let api_request = InputHttpRequest {
let input_http_request = InputHttpRequest {
input_path: ApiInputPath {
base_path: uri.path().to_string(),
query_path: uri.query().map(|x| x.to_string()),
Expand All @@ -83,25 +83,28 @@ impl CustomHttpRequestApi {

let possible_api_definitions = match self
.api_definition_lookup_service
.get(api_request.clone())
.get(input_http_request.clone())
.await
{
Ok(api_definition) => api_definition,
Err(err) => {
error!("API request host: {} - error: {}", host, err);
Ok(api_defs) => api_defs,
Err(api_defs_lookup_error) => {
error!(
"API request host: {} - error: {}",
host, api_defs_lookup_error
);
return Response::builder()
.status(StatusCode::INTERNAL_SERVER_ERROR)
.body(Body::from_string("Internal error".to_string()));
}
};

match api_request
match input_http_request
.resolve_worker_binding(possible_api_definitions)
.await
{
Ok(resolved_worker_request) => {
resolved_worker_request
.interpret_response_mapping::<poem::Response>(&self.evaluator)
Ok(resolved_worker_binding) => {
resolved_worker_binding
.interpret_response_mapping(&self.worker_service_rib_interpreter)
.await
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ impl From<CompiledGolemWorkerBinding> for GolemWorkerBindingWithTypeInfo {
.response_rib_expr
.to_string(),
response_mapping_input: Some(worker_binding.response_compiled.rib_input),
worker_name_input: Some(worker_binding.worker_name_compiled.rib_input),
worker_name_input: Some(worker_binding.worker_name_compiled.rib_input_type_info),
idempotency_key_input: value
.idempotency_key_compiled
.map(|idempotency_key_compiled| idempotency_key_compiled.rib_input),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ impl From<CompiledHttpApiDefinition> for HttpApiDefinition {
}
}

// The Rib Expressions that exists in various parts of HttpApiDefinition (mainly in Routes)
// are compiled to form CompiledHttpApiDefinition.
// The Compilation happens during API definition registration,
// and is persisted, so that custom http requests are served by looking up
// CompiledHttpApiDefinition
#[derive(Debug, Clone, PartialEq)]
pub struct CompiledHttpApiDefinition {
pub id: ApiDefinitionId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl CompiledGolemWorkerBinding {
pub struct WorkerNameCompiled {
pub worker_name: Expr,
pub compiled_worker_name: RibByteCode,
pub rib_input: RibInputTypeInfo,
pub rib_input_type_info: RibInputTypeInfo,
}

impl WorkerNameCompiled {
Expand All @@ -60,7 +60,7 @@ impl WorkerNameCompiled {
Ok(WorkerNameCompiled {
worker_name: worker_name.clone(),
compiled_worker_name: worker_name_compiled.byte_code,
rib_input: worker_name_compiled.global_input_type_info,
rib_input_type_info: worker_name_compiled.global_input_type_info,
})
}
}
Expand Down Expand Up @@ -153,7 +153,7 @@ impl TryFrom<golem_api_grpc::proto::golem::apidefinition::CompiledWorkerBinding>
.ok_or("Missing worker name".to_string())
.and_then(Expr::try_from)?,
compiled_worker_name: worker_name_compiled,
rib_input: worker_name_input,
rib_input_type_info: worker_name_input,
};

let idempotency_key_compiled = match (idempotency_key_compiled, idempotency_key_input) {
Expand Down Expand Up @@ -197,7 +197,7 @@ impl TryFrom<CompiledGolemWorkerBinding>
let worker_name = Some(value.worker_name_compiled.worker_name.into());
let compiled_worker_name_expr =
Some(value.worker_name_compiled.compiled_worker_name.into());
let worker_name_rib_input = Some(value.worker_name_compiled.rib_input.into());
let worker_name_rib_input = Some(value.worker_name_compiled.rib_input_type_info.into());
let (idempotency_key, compiled_idempotency_key_expr, idempotency_key_rib_input) =
match value.idempotency_key_compiled {
Some(x) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ use rib::RibInputTypeInfo;
use std::collections::HashMap;
use std::fmt::Display;

// `RibInputValueResolver` is responsible
// for converting to RibInputValue which is in the right shape
// to act as input for Rib Script. Example: HttpRequestDetails
// can be converted to RibInputValue
pub trait RibInputValueResolver {
fn resolve_rib_input_value(
&self,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,9 @@ use crate::worker_binding::rib_input_value_resolver::RibInputValueResolver;
use crate::worker_binding::{RequestDetails, ResponseMappingCompiled, RibInputTypeMismatch};
use crate::worker_bridge_execution::to_response::ToResponse;

// Every request (http or others) can have an instance of this resolver
// to resolve to a single worker-binding with additional-info which is required for worker_service_rib_interpreter
// TODO; It will be better if worker binding resolver
// able to deal with only one API definition
// as the first stage resolution can take place (based on host, input request (route resolution)
// up the stage
// Every type of request (example: InputHttpRequest (which corresponds to a Route)) can have an instance of this resolver,
// to resolve a single worker-binding is then executed with the help of worker_service_rib_interpreter, which internally
// calls the worker function.
#[async_trait]
pub trait RequestToWorkerBindingResolver<ApiDefinition> {
async fn resolve_worker_binding(
Expand Down Expand Up @@ -127,15 +124,15 @@ impl ResolvedWorkerBindingFromRequest {
impl RequestToWorkerBindingResolver<CompiledHttpApiDefinition> for InputHttpRequest {
async fn resolve_worker_binding(
&self,
api_definition: Vec<CompiledHttpApiDefinition>,
compiled_api_definitions: Vec<CompiledHttpApiDefinition>,
) -> Result<ResolvedWorkerBindingFromRequest, WorkerBindingResolutionError> {
let routes = api_definition
let compiled_routes = compiled_api_definitions
.iter()
.flat_map(|x| x.routes.clone())
.collect::<Vec<_>>();

let api_request = self;
let router = router::build(routes);
let router = router::build(compiled_routes);
let path: Vec<&str> = RouterPattern::split(&api_request.input_path.base_path).collect();
let request_query_variables = self.input_path.query_components().unwrap_or_default();
let request_body = &self.req_body;
Expand All @@ -156,7 +153,7 @@ impl RequestToWorkerBindingResolver<CompiledHttpApiDefinition> for InputHttpRequ
.collect()
};

let request_details = RequestDetails::from(
let http_request_details = RequestDetails::from(
&zipped_path_params,
&request_query_variables,
query_params,
Expand All @@ -165,19 +162,24 @@ impl RequestToWorkerBindingResolver<CompiledHttpApiDefinition> for InputHttpRequ
)
.map_err(|err| format!("Failed to fetch input request details {}", err.join(", ")))?;

let resolve_rib_input = request_details
.resolve_rib_input_value(&binding.worker_name_compiled.rib_input)
.map_err(|err| format!("Failed to resolve rib input value {}", err))?;
let resolve_rib_input = http_request_details
.resolve_rib_input_value(&binding.worker_name_compiled.rib_input_type_info)
.map_err(|err| {
format!(
"Failed to resolve rib input value from http request details {}",
err
)
})?;

// To evaluate worker-name, most probably
let worker_name: String = rib::interpret_pure(
&binding.worker_name_compiled.compiled_worker_name,
&resolve_rib_input.value,
)
.await
.map_err(|err| format!("Failed to evaluate worker name expression. {}", err))?
.map_err(|err| format!("Failed to evaluate worker name rib expression. {}", err))?
.get_literal()
.ok_or("Worker name is not a String".to_string())?
.ok_or("Worker name is not a Rib expression that resolves to String".to_string())?
.as_string();

let component_id = &binding.component_id;
Expand Down Expand Up @@ -212,7 +214,7 @@ impl RequestToWorkerBindingResolver<CompiledHttpApiDefinition> for InputHttpRequ

let resolved_binding = ResolvedWorkerBindingFromRequest {
worker_detail,
request_details,
request_details: http_request_details,
compiled_response_mapping: binding.response_compiled.clone(),
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::worker_bridge_execution::{WorkerRequest, WorkerRequestExecutor};
#[async_trait]
pub trait WorkerServiceRibInterpreter {
// Evaluate a Rib byte against a specific worker.
// RibByteCode may have actual function calls in a worker.
// RibByteCode may have actual function calls.
async fn evaluate(
&self,
worker_name: &str,
Expand Down

0 comments on commit 0b92194

Please sign in to comment.