-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
wasm: clean up the code #36556
wasm: clean up the code #36556
Conversation
Signed-off-by: wangbaiping <wangbaiping@bytedance.com>
Signed-off-by: wangbaiping <wangbaiping@bytedance.com>
Signed-off-by: wangbaiping <wangbaiping@bytedance.com>
Signed-off-by: wangbaiping <wangbaiping@bytedance.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM but not 100% if code is exactly the same. I can take a look again.
init_manager, context.mainThreadDispatcher(), context.api(), | ||
context.lifecycleNotifier(), remote_data_provider_, | ||
std::move(callback))) { | ||
throw Common::Wasm::WasmException( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we restructure this to avoid exceptions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great suggestion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I finally add a TODO first. Because this is in the constructor and we need to use creation_status to handle the error. That means we need to change code of lots of different positions at where this PluginConfig is used.
We can put it to separated PR.
|
||
if (is_singleton_handle_) { | ||
// Use critical log because this error should not happen in production. | ||
ENVOY_LOG(critical, "CreateContext() only works for thread local plugins."); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not release assert if this never should be called by the implementation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check is finally removed at #36456. Finally, we use the same way to treat the singleton handle and thread local handle.
So, I think we can leave it there for now?
return singleton_handle_ != nullptr ? singleton_handle_->wasmOfHandle() : nullptr; | ||
} | ||
|
||
ASSERT(thread_local_handle_ != nullptr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RELEASE_ASSERT since it would crash anyways?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
From the current code base, seems we prefer the ASSERT even in this type case? Let's me know if you think this is a block point.
Signed-off-by: wangbaiping <wangbaiping@bytedance.com>
ping @mpwarres for final review of this wasm related changes. :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great idea for refactoring!
throw Common::Wasm::WasmException( | ||
fmt::format("Unable to create Wasm service {}", plugin->name_)); | ||
} | ||
wasm_service_ = std::make_unique<Common::Wasm::PluginConfig>( |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
plugin_handle_initialized_ = true; | ||
|
||
if (base_wasm == nullptr) { | ||
ENVOY_LOG(critical, "Plugin {} failed to load", plugin_->name_); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some cases of the old logic that this refactoring consolidates used different log levels here depending on whether or not plugin->fail_open_
was true. Can that be preserved?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also (optionally) you might consider having the PluginConfig constructor take an additional string param for the role of the plugin (e.g. "stat sink" vs. "access logger" vs. "HTTP filter" etc.), so that can be retained in the log message here. While it isn't strictly necessary functionality-wise, I think it is useful information in the log message, since plugin failure could occur in a number of contexts.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some cases of the old logic that this refactoring consolidates used different log levels here depending on whether or not
plugin->fail_open_
was true. Can that be preserved?
The fail_open self will be deprecated in favor of new FailurePolicy in the Envoy. And I think it acutally make no big sense to keep that logic.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also (optionally) you might consider having the PluginConfig constructor take an additional string param for the role of the plugin (e.g. "stat sink" vs. "access logger" vs. "HTTP filter" etc.), so that can be retained in the log message here. While it isn't strictly necessary functionality-wise, I think it is useful information in the log message, since plugin failure could occur in a number of contexts.
I personally think the plugin name should be enough to distinguish different extension and their type. But I don't oppose it. I will add a TODO first until some acutally user ask it. (By the way, we created wasm bootstrap extension, wasm network extension, http extension, logger extension, etc, but we don't know how much of them is what the users actually want. At least from recently problems/bug, I think even the http wasm extension is not be accepted/used widely. We do too much things before get actual practising feedbacks and requirements.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The fail_open self will be deprecated in favor of new FailurePolicy in the Envoy. And I think it acutally make no big sense to keep that logic.
SG--didn't know about that!
I personally think the plugin name should be enough to distinguish different extension and their type. But I don't oppose it. I will add a TODO first until some acutally user ask it.
Fair enough--SGTM.
return nullptr; | ||
} | ||
|
||
Wasm* wasm = plugin_holder->handle->wasmOfHandle(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like most/all of the logic in this method up to this point is the same as PluginConfig::wasmOfHandle()
. Could the first half of this method be replaced by a call to PluginConfig::wasmOfHandle()
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need both Wasm* and PluginHandle here, which make we cannot call the wasmOfHandle directly here. But this is optimized at #36456.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it, SG.
source/extensions/common/wasm/wasm.h
Outdated
const envoy::config::core::v3::Metadata* metadata, bool singleton); | ||
|
||
std::shared_ptr<Context> createContext(); | ||
Wasm* wasmOfHandle(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about making this WasmHandleSharedPtr wasmHandle()
? That would avoid the possibility of lifetime issues, as well as the name wasmOfHandle
which sounds a little awkward to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we return a WasmHandleSharedPtr
here, the WasmHandleSharedPtr self may be null. It make the caller need to do additional check to get a Wasm* pointer.
But I agree the naming is not good, may call it wasm() directly?
And the lifetime is another problem I think we should to improve at this code base. We should only return shared pointer if we hope the caller to longer the lifetime of the returned object. This method here is not designed to work at that way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks, I agree wasm()
is a better name, LG.
Signed-off-by: wangbaiping <wangbaiping@bytedance.com>
Signed-off-by: wangbaiping <wangbaiping@bytedance.com>
Signed-off-by: wangbaiping <wangbaiping@bytedance.com>
Signed-off-by: wangbaiping <wangbaiping@bytedance.com>
friendly ping for another approval @kyessenov :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!
Commit Message: wasm: clean up the code
Additional Description:
When I doing the #36456, I found there are lots of redundant code in the wasm extensions. And the wasm loading and creations are spread out in multiple different positions.
This redundancy and fragmentation make #36456 become more and more complex.
Finally, I split the code clean up out as an independent PR.
This PR doesn't change any logic but only merge duplicated logic.
Risk Level: n/a.
Testing: n/a.
Docs Changes: n/a.
Release Notes: n/a.