Skip to content

Commit e1fe5e9

Browse files
authored
Cache canary results. (#351)
Signed-off-by: Kuat Yessenov <[email protected]>
1 parent 5574daf commit e1fe5e9

File tree

3 files changed

+17
-4
lines changed

3 files changed

+17
-4
lines changed

include/proxy-wasm/wasm.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,7 @@ class WasmHandleBase : public std::enable_shared_from_this<WasmHandleBase> {
337337

338338
protected:
339339
std::shared_ptr<WasmBase> wasm_base_;
340+
std::unordered_map<std::string, bool> plugin_canary_cache_;
340341
};
341342

342343
std::string makeVmKey(std::string_view vm_id, std::string_view configuration,

src/wasm.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -472,6 +472,10 @@ bool WasmHandleBase::canary(const std::shared_ptr<PluginBase> &plugin,
472472
if (this->wasm() == nullptr) {
473473
return false;
474474
}
475+
auto it = plugin_canary_cache_.find(plugin->key());
476+
if (it != plugin_canary_cache_.end()) {
477+
return it->second;
478+
}
475479
auto configuration_canary_handle = clone_factory(shared_from_this());
476480
if (!configuration_canary_handle) {
477481
this->wasm()->fail(FailState::UnableToCloneVm, "Failed to clone Base Wasm");
@@ -490,9 +494,11 @@ bool WasmHandleBase::canary(const std::shared_ptr<PluginBase> &plugin,
490494
if (!configuration_canary_handle->wasm()->configure(root_context, plugin)) {
491495
configuration_canary_handle->wasm()->fail(FailState::ConfigureFailed,
492496
"Failed to configure base Wasm plugin");
497+
plugin_canary_cache_[plugin->key()] = false;
493498
return false;
494499
}
495500
configuration_canary_handle->kill();
501+
plugin_canary_cache_[plugin->key()] = true;
496502
return true;
497503
}
498504

test/wasm_test.cc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ TEST_P(TestVm, AlwaysApplyCanary) {
172172
// For each create Wasm, canary should be done.
173173
EXPECT_EQ(canary_count, 1);
174174

175+
bool first = true;
175176
std::unordered_set<std::shared_ptr<WasmHandleBase>> reference_holder;
176177

177178
for (const auto &root_id : root_ids) {
@@ -196,8 +197,15 @@ TEST_P(TestVm, AlwaysApplyCanary) {
196197
auto wasm_handle_comp =
197198
createWasm(vm_key, source, plugin_comp, wasm_handle_factory_comp,
198199
wasm_handle_clone_factory_for_canary, false);
199-
// For each create Wasm, canary should be done.
200-
EXPECT_EQ(canary_count, 1);
200+
// Validate that canarying is cached for the first baseline plugin variant.
201+
if (first) {
202+
first = false;
203+
EXPECT_EQ(canary_count, 0);
204+
} else {
205+
// For each create Wasm, canary should be done.
206+
EXPECT_EQ(canary_count, 1);
207+
EXPECT_TRUE(TestContext::isGlobalLogged("onConfigure: " + root_id));
208+
}
201209

202210
if (plugin_config.empty()) {
203211
// canary_check.wasm should raise the error at `onConfigure` in canary when the
@@ -212,8 +220,6 @@ TEST_P(TestVm, AlwaysApplyCanary) {
212220
// destroyed for each iteration.
213221
reference_holder.insert(wasm_handle_comp);
214222

215-
EXPECT_TRUE(TestContext::isGlobalLogged("onConfigure: " + root_id));
216-
217223
// Wasm VM is unique for vm_key.
218224
if (vm_key == vm_key_baseline) {
219225
EXPECT_EQ(wasm_handle_baseline->wasm(), wasm_handle_comp->wasm());

0 commit comments

Comments
 (0)