Skip to content

Commit

Permalink
Fix wasm-rpc dynamic linking metadata and auto-fill it from golem-cli
Browse files Browse the repository at this point in the history
  • Loading branch information
vigoo committed Jan 6, 2025
1 parent 4c92f38 commit 75cad7c
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ message DynamicLinkedInstance {
}

message DynamicLinkedWasmRpc {
string target_interface_name = 1;
map<string, string> target_interface_name = 1;
}
34 changes: 15 additions & 19 deletions golem-cli/src/command/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ impl<
&component_name.0,
)?;

let dynamic_linking = ctx.dynamic_linking();
let dynamic_linking = ctx.dynamic_linking()?;
let component = service
.add(
component_name,
Expand Down Expand Up @@ -407,7 +407,7 @@ impl<
&component_name,
)?;

let dynamic_linking = ctx.dynamic_linking();
let dynamic_linking = ctx.dynamic_linking()?;
let mut result = service
.update(
component_name_or_uri.clone(),
Expand Down Expand Up @@ -576,8 +576,8 @@ impl ApplicationComponentContext {
})
}

fn dynamic_linking(&mut self) -> Option<DynamicLinking> {
let mapping = Vec::new();
fn dynamic_linking(&mut self) -> Result<Option<DynamicLinking>, GolemError> {
let mut mapping = Vec::new();

let wasm_rpc_deps = self
.app_ctx
Expand All @@ -589,30 +589,26 @@ impl ApplicationComponentContext {
let ifaces = self
.app_ctx
.component_stub_interfaces(&wasm_rpc_dep)
.unwrap(); // TODO
println!("{:?}", ifaces);
// let target_props = self
// .app_ctx
// .application
// .component_properties(&self.name, self.build_profile.as_ref());

// TODO: figure out source->target names and add to `mapping`
// TODO: use: self.app_ctx.component_stub_interfaces(wasm_rpc_dep);
.map_err(|err| GolemError(err.to_string()))?;

mapping.push(ifaces);
}

if mapping.is_empty() {
None
Ok(None)
} else {
Some(DynamicLinking {
dynamic_linking: HashMap::from_iter(mapping.into_iter().map(|(from, to)| {
Ok(Some(DynamicLinking {
dynamic_linking: HashMap::from_iter(mapping.into_iter().map(|stub_interfaces| {
(
from,
stub_interfaces.stub_interface_name,
DynamicLinkedInstance::WasmRpc(DynamicLinkedWasmRpc {
target_interface_name: to,
target_interface_name: HashMap::from_iter(
stub_interfaces.exported_interfaces_per_stub_resource,
),
}),
)
})),
})
}))
}
}
}
2 changes: 1 addition & 1 deletion golem-common/src/model/component_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ impl DynamicLinkedWasmRpc {
.target_interface_name
.get(stub_resource)
.ok_or("Resource not found in dynamic linked interface")?;
ParsedFunctionSite::parse(&target_interface)
ParsedFunctionSite::parse(target_interface)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,20 @@ pub fn dynamic_wasm_rpc_link<Ctx: WorkerCtx + HostWasmRpc + HostFutureInvokeResu
)?;
}
Some(DynamicRpcResource::Stub) | Some(DynamicRpcResource::ResourceStub) => {
let interface_name_clone = rpc_metadata.target_interface_name.clone();
let interface_name = rpc_metadata
.target_interface_name
.get(&resource_name)
.cloned()
.ok_or(anyhow!(
"Failed to get target interface name for resource {resource_name}"
))?;
let resource_name_clone = resource_name.clone();

instance.resource_async(
&resource_name,
ResourceType::host::<WasmRpcEntry>(),
move |store, rep| {
let interface_name = interface_name_clone.clone();
let interface_name = interface_name.clone();
let resource_name = resource_name_clone.clone();

Box::new(
Expand Down Expand Up @@ -624,8 +630,8 @@ impl DynamicRpcCall {
Some(DynamicRpcResource::Stub) => Ok(Some(DynamicRpcCall::GlobalStubConstructor)),
Some(DynamicRpcResource::ResourceStub) => {
let target_constructor_name = ParsedFunctionName {
site: rpc_metadata.target_site().map_err(|err| anyhow!("Failed to parse mapped target site ({}) from dynamic linking metadata: {}",
rpc_metadata.target_interface_name,
site: rpc_metadata.target_site(resource_name).map_err(|err| anyhow!("Failed to get mapped target site ({}) from dynamic linking metadata: {}",
rpc_metadata.target_interface_name.iter().map(|(k, v)| format!("{k}=>{v}")).collect::<Vec<_>>().join(", "),
err
))?,
function: ParsedFunctionReference::RawResourceConstructor {
Expand Down Expand Up @@ -674,8 +680,8 @@ impl DynamicRpcCall {
};

let target_function_name = ParsedFunctionName {
site: rpc_metadata.target_site().map_err(|err| anyhow!("Failed to parse mapped target site ({}) from dynamic linking metadata: {}",
rpc_metadata.target_interface_name,
site: rpc_metadata.target_site(resource_name).map_err(|err| anyhow!("Failed to get mapped target site ({}) from dynamic linking metadata: {}",
rpc_metadata.target_interface_name.iter().map(|(k, v)| format!("{k}=>{v}")).collect::<Vec<_>>().join(", "),
err
))?,
function: target_function,
Expand Down Expand Up @@ -728,13 +734,16 @@ impl DynamicRpcResource {
if Self::first_parameter_is_uri(&constructor.params) {
if constructor.params.len() > 1 {
Ok(Some(DynamicRpcResource::ResourceStub))
} else if rpc_metadata
.target_interface_name
.ends_with(&format!("/{resource_name}"))
} else if let Some(target_interface_name) =
rpc_metadata.target_interface_name.get(resource_name)
{
Ok(Some(DynamicRpcResource::Stub))
if target_interface_name.ends_with(&format!("/{resource_name}")) {
Ok(Some(DynamicRpcResource::Stub))
} else {
Ok(Some(DynamicRpcResource::ResourceStub))
}
} else {
Ok(Some(DynamicRpcResource::ResourceStub))
Ok(Some(DynamicRpcResource::Stub))
}
} else {
// First constructor parameter is not a Uri => not a stub
Expand Down
Loading

0 comments on commit 75cad7c

Please sign in to comment.