Skip to content

Commit

Permalink
add init manager to WasmFactoryContextImpl (envoyproxy#218)
Browse files Browse the repository at this point in the history
Signed-off-by: Yan Xue <yxyan@google.com>
  • Loading branch information
yxue authored and jplevyak committed Sep 30, 2019
1 parent d4a4540 commit 36cb583
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 17 deletions.
6 changes: 6 additions & 0 deletions include/envoy/server/wasm_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "envoy/api/api.h"
#include "envoy/common/pure.h"
#include "envoy/event/dispatcher.h"
#include "envoy/init/manager.h"
#include "envoy/server/wasm.h"
#include "envoy/thread_local/thread_local.h"
#include "envoy/upstream/cluster_manager.h"
Expand All @@ -22,6 +23,11 @@ class WasmFactoryContext {
*/
virtual Upstream::ClusterManager& clusterManager() PURE;

/**
* @return Init:Manager& used by synchronizing the WASM initialization.
*/
virtual Init::Manager& initManager() PURE;

/**
* @return Event::Dispatcher& the main thread's dispatcher. This dispatcher should be used
* for all singleton processing.
Expand Down
4 changes: 2 additions & 2 deletions source/server/server.cc
Original file line number Diff line number Diff line change
Expand Up @@ -401,8 +401,8 @@ void InstanceImpl::initialize(const Options& options,
scope = Stats::ScopeSharedPtr(stats_store_.createScope(config.stat_prefix()));
}
Configuration::WasmFactoryContextImpl wasm_factory_context(
clusterManager(), *dispatcher_, thread_local_, api(), stats_store_, scope,
*local_info_);
clusterManager(), initManager(), *dispatcher_, thread_local_, api(), stats_store_,
scope, *local_info_);
auto wasm = factory->createWasm(config, wasm_factory_context);
if (wasm) {
// If not nullptr, this is a singleton WASM service.
Expand Down
13 changes: 8 additions & 5 deletions source/server/wasm_config_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ namespace Configuration {

class WasmFactoryContextImpl : public WasmFactoryContext {
public:
WasmFactoryContextImpl(Upstream::ClusterManager& cluster_manager, Event::Dispatcher& dispatcher,
ThreadLocal::SlotAllocator& tls, Api::Api& api, Stats::Scope& scope,
Stats::ScopeSharedPtr owned_scope, const LocalInfo::LocalInfo& local_info)
: cluster_manager_(cluster_manager), dispatcher_(dispatcher), tls_(tls), api_(api),
scope_(scope), owned_scope_(owned_scope), local_info_(local_info) {}
WasmFactoryContextImpl(Upstream::ClusterManager& cluster_manager, Init::Manager& init_manager,
Event::Dispatcher& dispatcher, ThreadLocal::SlotAllocator& tls,
Api::Api& api, Stats::Scope& scope, Stats::ScopeSharedPtr owned_scope,
const LocalInfo::LocalInfo& local_info)
: cluster_manager_(cluster_manager), init_manager_(init_manager), dispatcher_(dispatcher),
tls_(tls), api_(api), scope_(scope), owned_scope_(owned_scope), local_info_(local_info) {}

Upstream::ClusterManager& clusterManager() override { return cluster_manager_; }
Init::Manager& initManager() override { return init_manager_; }
Event::Dispatcher& dispatcher() override { return dispatcher_; }
ThreadLocal::SlotAllocator& threadLocal() override { return tls_; }
Api::Api& api() override { return api_; }
Expand All @@ -25,6 +27,7 @@ class WasmFactoryContextImpl : public WasmFactoryContext {

private:
Upstream::ClusterManager& cluster_manager_;
Init::Manager& init_manager_;
Event::Dispatcher& dispatcher_;
ThreadLocal::SlotAllocator& tls_;
Api::Api& api_;
Expand Down
25 changes: 15 additions & 10 deletions test/extensions/wasm/config_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,15 @@ TEST_P(WasmFactoryTest, CreateWasmFromWASM) {
"{{ test_rundir }}/test/extensions/wasm/test_data/logging_cpp.wasm"));
config.set_singleton(true);
Upstream::MockClusterManager cluster_manager;
Init::MockManager init_manager;
Event::MockDispatcher dispatcher;
ThreadLocal::MockInstance tls;
Stats::IsolatedStoreImpl stats_store;
NiceMock<LocalInfo::MockLocalInfo> local_info;
Api::ApiPtr api = Api::createApiForTest(stats_store);
auto scope = Stats::ScopeSharedPtr(stats_store.createScope("wasm."));
Server::Configuration::WasmFactoryContextImpl context(cluster_manager, dispatcher, tls, *api,
*scope, scope, local_info);
Server::Configuration::WasmFactoryContextImpl context(cluster_manager, init_manager, dispatcher,
tls, *api, *scope, scope, local_info);
auto wasm = factory->createWasm(config, context);
EXPECT_NE(wasm, nullptr);
}
Expand All @@ -67,14 +68,15 @@ TEST_P(WasmFactoryTest, CreateWasmFromWASMPerThread) {
config.mutable_vm_config()->mutable_code()->set_filename(TestEnvironment::substitute(
"{{ test_rundir }}/test/extensions/wasm/test_data/logging_cpp.wasm"));
Upstream::MockClusterManager cluster_manager;
Init::MockManager init_manager;
Event::MockDispatcher dispatcher;
testing::NiceMock<ThreadLocal::MockInstance> tls;
Stats::IsolatedStoreImpl stats_store;
NiceMock<LocalInfo::MockLocalInfo> local_info;
Api::ApiPtr api = Api::createApiForTest(stats_store);
auto scope = Stats::ScopeSharedPtr(stats_store.createScope("wasm."));
Server::Configuration::WasmFactoryContextImpl context(cluster_manager, dispatcher, tls, *api,
*scope, scope, local_info);
Server::Configuration::WasmFactoryContextImpl context(cluster_manager, init_manager, dispatcher,
tls, *api, *scope, scope, local_info);
auto wasm = factory->createWasm(config, context);
EXPECT_EQ(wasm, nullptr);
}
Expand All @@ -89,14 +91,15 @@ TEST_P(WasmFactoryTest, MissingImport) {
"{{ test_rundir }}/test/extensions/wasm/test_data/missing_cpp.wasm"));
config.set_singleton(true);
Upstream::MockClusterManager cluster_manager;
Init::MockManager init_manager;
Event::MockDispatcher dispatcher;
ThreadLocal::MockInstance tls;
Stats::IsolatedStoreImpl stats_store;
NiceMock<LocalInfo::MockLocalInfo> local_info;
Api::ApiPtr api = Api::createApiForTest(stats_store);
auto scope = Stats::ScopeSharedPtr(stats_store.createScope("wasm."));
Server::Configuration::WasmFactoryContextImpl context(cluster_manager, dispatcher, tls, *api,
*scope, scope, local_info);
Server::Configuration::WasmFactoryContextImpl context(cluster_manager, init_manager, dispatcher,
tls, *api, *scope, scope, local_info);
EXPECT_THROW_WITH_REGEX(factory->createWasm(config, context),
Extensions::Common::Wasm::WasmException,
"Failed to load WASM module due to a missing import: env._missing.*");
Expand All @@ -114,14 +117,15 @@ TEST_P(WasmFactoryTest, UnspecifiedRuntime) {
"{{ test_rundir }}/test/extensions/wasm/test_data/logging_cpp.wasm"));
config.set_singleton(true);
Upstream::MockClusterManager cluster_manager;
Init::MockManager init_manager;
Event::MockDispatcher dispatcher;
ThreadLocal::MockInstance tls;
Stats::IsolatedStoreImpl stats_store;
NiceMock<LocalInfo::MockLocalInfo> local_info;
Api::ApiPtr api = Api::createApiForTest(stats_store);
auto scope = Stats::ScopeSharedPtr(stats_store.createScope("wasm."));
Server::Configuration::WasmFactoryContextImpl context(cluster_manager, dispatcher, tls, *api,
*scope, scope, local_info);
Server::Configuration::WasmFactoryContextImpl context(cluster_manager, init_manager, dispatcher,
tls, *api, *scope, scope, local_info);
#if defined(ENVOY_WASM_V8) == defined(ENVOY_WASM_WAVM)
EXPECT_THROW_WITH_MESSAGE(factory->createWasm(config, context),
Extensions::Common::Wasm::WasmException,
Expand All @@ -142,14 +146,15 @@ TEST_P(WasmFactoryTest, UnknownRuntime) {
"{{ test_rundir }}/test/extensions/wasm/test_data/logging_cpp.wasm"));
config.set_singleton(true);
Upstream::MockClusterManager cluster_manager;
Init::MockManager init_manager;
Event::MockDispatcher dispatcher;
ThreadLocal::MockInstance tls;
Stats::IsolatedStoreImpl stats_store;
NiceMock<LocalInfo::MockLocalInfo> local_info;
Api::ApiPtr api = Api::createApiForTest(stats_store);
auto scope = Stats::ScopeSharedPtr(stats_store.createScope("wasm."));
Server::Configuration::WasmFactoryContextImpl context(cluster_manager, dispatcher, tls, *api,
*scope, scope, local_info);
Server::Configuration::WasmFactoryContextImpl context(cluster_manager, init_manager, dispatcher,
tls, *api, *scope, scope, local_info);
EXPECT_THROW_WITH_MESSAGE(factory->createWasm(config, context),
Extensions::Common::Wasm::WasmException,
"Failed to create WASM VM using envoy.wasm.vm.invalid runtime. "
Expand Down

0 comments on commit 36cb583

Please sign in to comment.