Skip to content

Cache canary results. #351

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

Merged
merged 8 commits into from
May 11, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/proxy-wasm/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ class WasmHandleBase : public std::enable_shared_from_this<WasmHandleBase> {

protected:
std::shared_ptr<WasmBase> wasm_base_;
std::unordered_map<std::string, bool> plugin_canary_cache_;
};

std::string makeVmKey(std::string_view vm_id, std::string_view configuration,
Expand Down
7 changes: 7 additions & 0 deletions src/wasm.cc
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,11 @@ bool WasmHandleBase::canary(const std::shared_ptr<PluginBase> &plugin,
if (this->wasm() == nullptr) {
return false;
}
std::string plugin_key(plugin->key());
auto it = plugin_canary_cache_.find(plugin_key);
if (it != plugin_canary_cache_.end()) {
return it->second;
}
auto configuration_canary_handle = clone_factory(shared_from_this());
if (!configuration_canary_handle) {
this->wasm()->fail(FailState::UnableToCloneVm, "Failed to clone Base Wasm");
Expand All @@ -490,9 +495,11 @@ bool WasmHandleBase::canary(const std::shared_ptr<PluginBase> &plugin,
if (!configuration_canary_handle->wasm()->configure(root_context, plugin)) {
configuration_canary_handle->wasm()->fail(FailState::ConfigureFailed,
"Failed to configure base Wasm plugin");
plugin_canary_cache_[plugin_key] = false;
return false;
}
configuration_canary_handle->kill();
plugin_canary_cache_[plugin_key] = true;
return true;
}

Expand Down
8 changes: 8 additions & 0 deletions test/wasm_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ TEST_P(TestVm, AlwaysApplyCanary) {
// For each create Wasm, canary should be done.
EXPECT_EQ(canary_count, 1);

bool first = true;
std::unordered_set<std::shared_ptr<WasmHandleBase>> reference_holder;

for (const auto &root_id : root_ids) {
Expand All @@ -195,6 +196,13 @@ TEST_P(TestVm, AlwaysApplyCanary) {
auto wasm_handle_comp =
createWasm(vm_key, source, plugin_comp, wasm_handle_factory_comp,
wasm_handle_clone_factory_for_canary, false);
// Validate that canarying is cached for the first baseline plugin variant.
if (first) {
first = false;
EXPECT_EQ(canary_count, 0);
continue;
}

// For each create Wasm, canary should be done.
EXPECT_EQ(canary_count, 1);

Expand Down