Skip to content
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

Merged
merged 11 commits into from
Oct 17, 2024
Merged

wasm: clean up the code #36556

merged 11 commits into from
Oct 17, 2024

Conversation

wbpcode
Copy link
Member

@wbpcode wbpcode commented Oct 12, 2024

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.

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>
Signed-off-by: wangbaiping <wangbaiping@bytedance.com>
Copy link
Contributor

@kyessenov kyessenov left a 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.

source/extensions/common/wasm/wasm.cc Show resolved Hide resolved
init_manager, context.mainThreadDispatcher(), context.api(),
context.lifecycleNotifier(), remote_data_provider_,
std::move(callback))) {
throw Common::Wasm::WasmException(
Copy link
Contributor

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?

Copy link
Member Author

@wbpcode wbpcode Oct 15, 2024

Choose a reason for hiding this comment

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

Great suggestion.

Copy link
Member Author

@wbpcode wbpcode Oct 15, 2024

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.");
Copy link
Contributor

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?

Copy link
Member Author

@wbpcode wbpcode Oct 15, 2024

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);
Copy link
Contributor

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?

Copy link
Member Author

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>
kyessenov
kyessenov previously approved these changes Oct 15, 2024
@wbpcode
Copy link
Member Author

wbpcode commented Oct 16, 2024

ping @mpwarres for final review of this wasm related changes. :)

Copy link
Contributor

@mpwarres mpwarres left a 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!

source/extensions/access_loggers/wasm/config.cc Outdated Show resolved Hide resolved
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.

source/extensions/bootstrap/wasm/config.cc Outdated Show resolved Hide resolved
source/extensions/bootstrap/wasm/config.h Outdated Show resolved Hide resolved
plugin_handle_initialized_ = true;

if (base_wasm == nullptr) {
ENVOY_LOG(critical, "Plugin {} failed to load", plugin_->name_);
Copy link
Contributor

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?

Copy link
Contributor

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.

Copy link
Member Author

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.

Copy link
Member Author

@wbpcode wbpcode Oct 16, 2024

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.)

Copy link
Contributor

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();
Copy link
Contributor

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()?

Copy link
Member Author

@wbpcode wbpcode Oct 16, 2024

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.

Copy link
Contributor

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 Show resolved Hide resolved
const envoy::config::core::v3::Metadata* metadata, bool singleton);

std::shared_ptr<Context> createContext();
Wasm* wasmOfHandle();
Copy link
Contributor

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.

Copy link
Member Author

@wbpcode wbpcode Oct 16, 2024

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.

Copy link
Contributor

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.

source/extensions/common/wasm/wasm.h Outdated Show resolved Hide resolved
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>
wbpcode added a commit to wbpcode/envoy that referenced this pull request Oct 16, 2024
Signed-off-by: wangbaiping <wangbaiping@bytedance.com>
@wbpcode wbpcode enabled auto-merge (squash) October 17, 2024 03:07
@wbpcode
Copy link
Member Author

wbpcode commented Oct 17, 2024

friendly ping for another approval @kyessenov :)

Copy link
Contributor

@kyessenov kyessenov left a comment

Choose a reason for hiding this comment

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

Thanks!

@wbpcode wbpcode merged commit f63b61a into envoyproxy:main Oct 17, 2024
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants