Skip to content

Commit

Permalink
fix: fuel handling (#2946)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbeesley committed Jan 19, 2023
1 parent 267d7ae commit ee29950
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
13 changes: 9 additions & 4 deletions crates/fluvio-smartengine/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ impl SmartModuleChainInstance {
for instance in instances {
// pass raw inputs to transform instance
// each raw input may result in multiple records
let starting_fuel = self.store.get_current_fuel();
self.store.top_up_fuel();
let output = instance.process(next_input, &mut self.store)?;
let fuel_used = starting_fuel - self.store.get_current_fuel();
let fuel_used = self.store.get_used_fuel();
debug!(fuel_used, "fuel used");
metric.add_fuel_used(fuel_used);

Expand All @@ -140,9 +140,9 @@ impl SmartModuleChainInstance {
}
}

let starting_fuel = self.store.get_current_fuel();
self.store.top_up_fuel();
let output = last.process(next_input, &mut self.store)?;
let fuel_used = starting_fuel - self.store.get_current_fuel();
let fuel_used = self.store.get_used_fuel();
debug!(fuel_used, "fuel used");
metric.add_fuel_used(fuel_used);
let records_out = output.successes.len();
Expand Down Expand Up @@ -300,6 +300,8 @@ mod chaining_test {
assert_eq!(output.successes[0].value.as_ref(), b"APPLE");
assert_eq!(output.successes[1].value.as_ref(), b"BANANA");
assert!(metrics.fuel_used() > 0);
chain.store.top_up_fuel();
assert_eq!(chain.store.get_used_fuel(), 0);
}

const SM_AGGEGRATE: &str = "fluvio_smartmodule_aggregate";
Expand Down Expand Up @@ -371,6 +373,9 @@ mod chaining_test {
let mut chain = chain_builder
.initialize(&engine)
.expect("failed to build chain");

assert_eq!(chain.store.get_used_fuel(), 0);

let record = vec![Record::new("input")];
let input = SmartModuleInput::try_from(record).expect("valid input record");
let metrics = SmartModuleChainMetrics::default();
Expand Down
1 change: 0 additions & 1 deletion crates/fluvio-smartengine/src/instance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ impl SmartModuleInstance {
input: SmartModuleInput,
store: &mut WasmState,
) -> Result<SmartModuleOutput> {
store.top_up_fuel();
self.transform.process(input, &mut self.ctx, store)
}

Expand Down
11 changes: 7 additions & 4 deletions crates/fluvio-smartengine/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ use wasmtime::{
StoreContextMut,
};

const DEFAULT_FUEL: u64 = i64::MAX as u64 - 1;
// DO NOT INCREASE THIS VALUE HIGHER THAN i64::MAX / 2.
// WASMTIME keeps fuel as i64 and has some strange behavior with `add_fuel` if trying to top fuel
// up to a values close to i64:MAX
const DEFAULT_FUEL: u64 = i64::MAX as u64 / 2;

#[cfg(not(feature = "wasi"))]
pub type WasmState = WasmStore<()>;
Expand Down Expand Up @@ -40,10 +43,10 @@ impl WasmState {
}
}

// Get current fuel
pub fn get_current_fuel(&mut self) -> u64 {
// Get amount of fuel used since last top up
pub fn get_used_fuel(&mut self) -> u64 {
if let Ok(current_fuel) = self.0.consume_fuel(0) {
current_fuel
max(DEFAULT_FUEL - current_fuel, 0)
} else {
0
}
Expand Down

2 comments on commit ee29950

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: ee29950 Previous: 324b2db Ratio
vecu8 encoding 379629 ns/iter (± 479038) 382457 ns/iter (± 603900) 0.99
vecu8 decoding 575561 ns/iter (± 31382) 590478 ns/iter (± 32321) 0.97
bytebuf encoding 14919 ns/iter (± 946) 15572 ns/iter (± 746) 0.96
bytebuf decoding 14612 ns/iter (± 1116) 15335 ns/iter (± 1694) 0.95

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmark

Benchmark suite Current: ee29950 Previous: 324b2db Ratio
encode wasm file 290355 ns/iter (± 18898) 293479 ns/iter (± 20632) 0.99

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.