-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* base template service in progress * reuse code from base, and simplify errors in template service
- Loading branch information
1 parent
4efb355
commit c95f2cd
Showing
20 changed files
with
245 additions
and
211 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[package] | ||
name = "golem-template-service-base" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
async-trait = { workspace = true } | ||
golem-api-grpc = { path = "../golem-api-grpc", version = "0.0.0" } | ||
golem-common = { path = "../golem-common", version = "0.0.0" } | ||
golem-service-base = { path = "../golem-service-base" } | ||
golem-wasm-ast = { workspace = true } | ||
http_02 = { workspace = true } | ||
serde = { workspace = true } | ||
thiserror = { workspace = true } | ||
tracing = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
use serde::Deserialize; | ||
|
||
#[derive(Clone, Debug, Deserialize)] | ||
#[serde(tag = "type", content = "config")] | ||
#[derive(Default)] | ||
pub enum TemplateCompilationConfig { | ||
Enabled(TemplateCompilationEnabledConfig), | ||
#[default] | ||
Disabled, | ||
} | ||
|
||
#[derive(Clone, Debug, Deserialize)] | ||
pub struct TemplateCompilationEnabledConfig { | ||
pub host: String, | ||
pub port: u16, | ||
} | ||
|
||
impl TemplateCompilationEnabledConfig { | ||
pub fn uri(&self) -> http_02::Uri { | ||
http_02::Uri::builder() | ||
.scheme("http") | ||
.authority(format!("{}:{}", self.host, self.port).as_str()) | ||
.path_and_query("/") | ||
.build() | ||
.expect("Failed to build TemplateCompilationService URI") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod config; | ||
pub mod service; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod template_compilation; | ||
pub mod template_processor; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
golem-template-service-base/src/service/template_processor.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use std::fmt::{self, Display, Formatter}; | ||
|
||
use golem_service_base::model::TemplateMetadata; | ||
use golem_wasm_ast::{ | ||
analysis::{AnalysedExport, AnalysedFunction, AnalysisContext, AnalysisFailure}, | ||
component::Component, | ||
IgnoreAllButMetadata, | ||
}; | ||
|
||
pub fn process_template(data: &[u8]) -> Result<TemplateMetadata, TemplateProcessingError> { | ||
let component = Component::<IgnoreAllButMetadata>::from_bytes(data) | ||
.map_err(TemplateProcessingError::Parsing)?; | ||
|
||
let producers = component | ||
.get_all_producers() | ||
.into_iter() | ||
.map(|producers| producers.into()) | ||
.collect::<Vec<_>>(); | ||
|
||
let state = AnalysisContext::new(component); | ||
|
||
let mut exports = state | ||
.get_top_level_exports() | ||
.map_err(TemplateProcessingError::Analysis)?; | ||
|
||
add_resource_drops(&mut exports); | ||
|
||
let exports = exports | ||
.into_iter() | ||
.map(|export| export.into()) | ||
.collect::<Vec<_>>(); | ||
|
||
Ok(TemplateMetadata { exports, producers }) | ||
} | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum TemplateProcessingError { | ||
Parsing(String), | ||
Analysis(AnalysisFailure), | ||
} | ||
|
||
impl Display for TemplateProcessingError { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
match self { | ||
TemplateProcessingError::Parsing(e) => write!(f, "Parsing error: {}", e), | ||
TemplateProcessingError::Analysis(source) => { | ||
let AnalysisFailure::Failed(error) = source; | ||
write!(f, "Analysis error: {}", error) | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn add_resource_drops(exports: &mut Vec<AnalysedExport>) { | ||
// Components are not exporting explicit drop functions for exported resources, but | ||
// worker executor does. So we keep golem-wasm-ast as an universal library and extend | ||
// its result with the explicit drops here, for each resource, identified by an exported | ||
// constructor. | ||
|
||
let mut to_add = Vec::new(); | ||
for export in exports.iter_mut() { | ||
match export { | ||
AnalysedExport::Function(fun) => { | ||
if fun.is_constructor() { | ||
let drop_name = fun.name.replace("[constructor]", "[drop]"); | ||
to_add.push(AnalysedExport::Function(AnalysedFunction { | ||
name: drop_name, | ||
..fun.clone() | ||
})); | ||
} | ||
} | ||
AnalysedExport::Instance(instance) => { | ||
let mut to_add = Vec::new(); | ||
for fun in &instance.funcs { | ||
if fun.is_constructor() { | ||
let drop_name = fun.name.replace("[constructor]", "[drop]"); | ||
to_add.push(AnalysedFunction { | ||
name: drop_name, | ||
..fun.clone() | ||
}); | ||
} | ||
} | ||
instance.funcs.extend(to_add.into_iter()); | ||
} | ||
} | ||
} | ||
|
||
exports.extend(to_add); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.