Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New rpc interface and other breaking changes #1432

Merged
merged 16 commits into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ rm -r wasm-rpc/wit/deps
mkdir wasm-rpc/wit/deps
cp wit/deps/io wasm-rpc/wit/deps
cp wit/deps/clocks wasm-rpc/wit/deps
cp wit/deps/wasm-rpc wasm-rpc/wit/deps
cp wit/deps/golem-1.x wasm-rpc/wit/deps
cp wit/deps/golem-rpc wasm-rpc/wit/deps
"""

[tasks.wit-host]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
syntax = "proto3";

import "golem/component/component_type.proto";

package golem.component;

message DynamicLinkedInstance {
Expand All @@ -9,5 +11,11 @@ message DynamicLinkedInstance {
}

message DynamicLinkedWasmRpc {
map<string, string> target_interface_name = 1;
map<string, WasmRpcTarget> targets = 1;
}

message WasmRpcTarget {
string interface_name = 1;
string component_name = 2;
ComponentType component_type = 3;
}
4 changes: 4 additions & 0 deletions golem-client/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ fn generate(yaml_path: PathBuf, out_dir: OsString) {
"TypeAnnotatedValue",
"golem_wasm_rpc::protobuf::type_annotated_value::TypeAnnotatedValue",
),
(
"WasmRpcTarget",
"golem_common::model::component_metadata::WasmRpcTarget",
),
("WorkerFilter", "golem_common::model::WorkerFilter"),
("WorkerId", "golem_common::model::WorkerId"),
(
Expand Down
81 changes: 70 additions & 11 deletions golem-common/src/model/component_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use bincode::{Decode, Encode};
use std::collections::HashMap;
use std::fmt::{self, Display, Formatter};

use crate::model::ComponentType;
use crate::{virtual_exports, SafeDisplay};
use golem_wasm_ast::analysis::AnalysedFunctionParameter;
use golem_wasm_ast::core::Mem;
Expand Down Expand Up @@ -61,17 +62,41 @@ pub enum DynamicLinkedInstance {
#[cfg_attr(feature = "poem", oai(rename_all = "camelCase"))]
#[serde(rename_all = "camelCase")]
pub struct DynamicLinkedWasmRpc {
/// Maps resource names within the dynamic linked interface to target interfaces
pub target_interface_name: HashMap<String, String>,
/// Maps resource names within the dynamic linked interface to target information
pub targets: HashMap<String, WasmRpcTarget>,
}

impl DynamicLinkedWasmRpc {
pub fn target_site(&self, stub_resource: &str) -> Result<ParsedFunctionSite, String> {
let target_interface = self
.target_interface_name
.get(stub_resource)
.ok_or("Resource not found in dynamic linked interface")?;
ParsedFunctionSite::parse(target_interface)
pub fn target(&self, stub_resource: &str) -> Result<WasmRpcTarget, String> {
self.targets.get(stub_resource).cloned().ok_or_else(|| {
format!("Resource '{stub_resource}' not found in dynamic linked interface")
})
}
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Encode, Decode)]
#[cfg_attr(feature = "poem", derive(poem_openapi::Object))]
#[cfg_attr(feature = "poem", oai(rename_all = "camelCase"))]
#[serde(rename_all = "camelCase")]
pub struct WasmRpcTarget {
pub interface_name: String,
pub component_name: String,
pub component_type: ComponentType,
}

impl WasmRpcTarget {
pub fn site(&self) -> Result<ParsedFunctionSite, String> {
ParsedFunctionSite::parse(&self.interface_name)
}
}

impl Display for WasmRpcTarget {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
f,
"{}@{}({})",
self.interface_name, self.component_name, self.component_type
)
}
}

Expand Down Expand Up @@ -343,7 +368,7 @@ fn add_virtual_exports(exports: &mut Vec<AnalysedExport>) {
mod protobuf {
use crate::model::component_metadata::{
ComponentMetadata, DynamicLinkedInstance, DynamicLinkedWasmRpc, LinearMemory,
ProducerField, Producers, VersionedName,
ProducerField, Producers, VersionedName, WasmRpcTarget,
};
use std::collections::HashMap;

Expand Down Expand Up @@ -508,7 +533,11 @@ mod protobuf {
impl From<DynamicLinkedWasmRpc> for golem_api_grpc::proto::golem::component::DynamicLinkedWasmRpc {
fn from(value: DynamicLinkedWasmRpc) -> Self {
Self {
target_interface_name: value.target_interface_name,
targets: value
.targets
.into_iter()
.map(|(key, target)| (key, target.into()))
.collect(),
}
}
}
Expand All @@ -522,7 +551,37 @@ mod protobuf {
value: golem_api_grpc::proto::golem::component::DynamicLinkedWasmRpc,
) -> Result<Self, Self::Error> {
Ok(Self {
target_interface_name: value.target_interface_name,
targets: value
.targets
.into_iter()
.map(|(key, target)| target.try_into().map(|target| (key, target)))
.collect::<Result<HashMap<_, _>, _>>()?,
})
}
}

impl From<WasmRpcTarget> for golem_api_grpc::proto::golem::component::WasmRpcTarget {
fn from(value: WasmRpcTarget) -> Self {
Self {
interface_name: value.interface_name,
component_name: value.component_name,
component_type: golem_api_grpc::proto::golem::component::ComponentType::from(
value.component_type,
) as i32,
}
}
}

impl TryFrom<golem_api_grpc::proto::golem::component::WasmRpcTarget> for WasmRpcTarget {
type Error = String;

fn try_from(
value: golem_api_grpc::proto::golem::component::WasmRpcTarget,
) -> Result<Self, Self::Error> {
Ok(Self {
interface_name: value.interface_name,
component_name: value.component_name,
component_type: value.component_type.try_into()?,
})
}
}
Expand Down
42 changes: 41 additions & 1 deletion golem-common/src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1846,7 +1846,7 @@ impl Display for WorkerEvent {
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, Encode, Decode, Serialize, Deserialize)]
#[cfg_attr(feature = "poem", derive(poem_openapi::Enum))]
#[repr(i32)]
pub enum ComponentType {
Expand Down Expand Up @@ -2129,6 +2129,46 @@ impl TryFrom<String> for GatewayBindingType {
}
}

impl From<crate::model::WorkerId> for golem_wasm_rpc::WorkerId {
fn from(worker_id: crate::model::WorkerId) -> Self {
golem_wasm_rpc::WorkerId {
component_id: worker_id.component_id.into(),
worker_name: worker_id.worker_name,
}
}
}

impl From<golem_wasm_rpc::WorkerId> for crate::model::WorkerId {
fn from(host: golem_wasm_rpc::WorkerId) -> Self {
Self {
component_id: host.component_id.into(),
worker_name: host.worker_name,
}
}
}

impl From<golem_wasm_rpc::ComponentId> for crate::model::ComponentId {
fn from(host: golem_wasm_rpc::ComponentId) -> Self {
let high_bits = host.uuid.high_bits;
let low_bits = host.uuid.low_bits;

Self(uuid::Uuid::from_u64_pair(high_bits, low_bits))
}
}

impl From<crate::model::ComponentId> for golem_wasm_rpc::ComponentId {
fn from(component_id: crate::model::ComponentId) -> Self {
let (high_bits, low_bits) = component_id.0.as_u64_pair();

golem_wasm_rpc::ComponentId {
uuid: golem_wasm_rpc::Uuid {
high_bits,
low_bits,
},
}
}
}

#[cfg(test)]
mod tests {
use std::collections::HashSet;
Expand Down
10 changes: 10 additions & 0 deletions golem-rib/src/function_name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,16 @@ impl ParsedFunctionName {
_ => None,
}
}

pub fn is_static_method(&self) -> Option<&str> {
match &self.function {
ParsedFunctionReference::RawResourceStaticMethod { resource, .. }
| ParsedFunctionReference::IndexedResourceStaticMethod { resource, .. } => {
Some(resource)
}
_ => None,
}
}
}

#[cfg(feature = "protobuf")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ fn to_http_dynamic_linking(
DynamicLinkedInstance::WasmRpc(link) => {
golem_client::model::DynamicLinkedInstance::WasmRpc(
golem_client::model::DynamicLinkedWasmRpc {
target_interface_name: link.target_interface_name.clone(),
targets: link.targets.clone(),
},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use crate::workerctx::WorkerCtx;
use golem_common::model::oplog::DurableFunctionType;
use wasmtime_wasi::bindings::cli::environment::Host;

// NOTE: No need to persist the results of these functions as the result values are persisted as part of the initial Create oplog entry
#[async_trait]
impl<Ctx: WorkerCtx> Host for DurableWorkerCtx<Ctx> {
async fn get_environment(&mut self) -> anyhow::Result<Vec<(String, String)>> {
Expand Down Expand Up @@ -60,7 +61,7 @@ impl<Ctx: WorkerCtx> Host for DurableWorkerCtx<Ctx> {
let durability = Durability::<Option<String>, SerializableError>::new(
self,
"golem_environment",
"get_arguments", // TODO: fix in 2.0 - for backward compatibility with Golem 1.0
"initial_cwd",
DurableFunctionType::ReadLocal,
)
.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl<Ctx: WorkerCtx> Host for DurableWorkerCtx<Ctx> {
let durability = Durability::<Instant, SerializableError>::new(
self,
"monotonic_clock",
"now", // TODO: fix in 2.0 - should be 'subscribe_duration' but have to keep for backward compatibility with Golem 1.0
"subscribe_duration",
DurableFunctionType::ReadLocal,
)
.await?;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::services::component::ComponentMetadata;
use crate::workerctx::{DynamicLinking, WorkerCtx};
use async_trait::async_trait;
use golem_common::model::component_metadata::DynamicLinkedInstance;
use golem_wasm_rpc::golem_rpc_0_1_x::types::HostFutureInvokeResult;
use golem_wasm_rpc::golem_rpc_0_2_x::types::HostFutureInvokeResult;
use golem_wasm_rpc::HostWasmRpc;
use wasmtime::component::types::ComponentItem;
use wasmtime::component::{Component, Linker};
Expand Down
Loading