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

refactor(proxy-wasm) better instance/ctx lifecycle management #422

Merged
merged 3 commits into from
Nov 8, 2023

Conversation

thibaultcha
Copy link
Member

@thibaultcha thibaultcha commented Sep 29, 2023

  • Allow for recycling the root instance (which is not managed by isolation modes).
  • Allow for smoother recycling of http instances (including when the root instance has trapped, e.g. during on_tick).
  • Also recycle trapped instances in the busy queue.
  • Less verbose context creation of fresh and/or recycled instances starting in atypical steps (e.g. on_response_headers).
  • Sweep trapped instances sooner (during runtime instead of worker shutdown).
  • Greatly simplify on_context_create supporting code and unifies it between root and http instances which now both use the same context creation code. This makes instance recycling much more robust in a number of tricky situations with low isolation modes.

@codecov
Copy link

codecov bot commented Sep 29, 2023

Codecov Report

Merging #422 (194d97d) into main (689a460) will increase coverage by 0.06451%.
Report is 1 commits behind head on main.
The diff coverage is 91.80328%.

Additional details and impacted files

Impacted file tree graph

@@                 Coverage Diff                 @@
##                main        #422         +/-   ##
===================================================
+ Coverage   90.11787%   90.18238%   +0.06450%     
===================================================
  Files             46          46                 
  Lines           8399        8444         +45     
===================================================
+ Hits            7569        7615         +46     
+ Misses           830         829          -1     
Files Coverage Δ
src/common/proxy_wasm/ngx_proxy_wasm.h 92.30769% <ø> (ø)
src/common/proxy_wasm/ngx_proxy_wasm_properties.c 93.53846% <ø> (ø)
src/common/proxy_wasm/ngx_proxy_wasm_util.c 93.64407% <100.00000%> (+0.63994%) ⬆️
src/http/ngx_http_wasm_module.c 96.36872% <100.00000%> (-0.02018%) ⬇️
src/http/ngx_http_wasm_util.c 85.80442% <100.00000%> (ø)
src/http/proxy_wasm/ngx_http_proxy_wasm.h 90.00000% <100.00000%> (ø)
src/http/proxy_wasm/ngx_http_proxy_wasm_dispatch.c 91.76136% <100.00000%> (ø)
src/wasm/vm/ngx_wavm.c 86.07595% <100.00000%> (ø)
src/common/proxy_wasm/ngx_proxy_wasm_host.c 93.33333% <81.81818%> (ø)
src/common/proxy_wasm/ngx_proxy_wasm.c 92.92390% <91.66667%> (+0.40591%) ⬆️

@thibaultcha thibaultcha added the pr/work-in-progress PR: Work in progress label Sep 29, 2023
@thibaultcha thibaultcha force-pushed the refactor/proxy-wasm branch 14 times, most recently from 22ac50f to 69e8801 Compare October 5, 2023 05:04
@thibaultcha thibaultcha added pr/on-hold PR: Do not merge/close and removed pr/work-in-progress PR: Work in progress labels Oct 5, 2023
@thibaultcha thibaultcha force-pushed the refactor/proxy-wasm branch 2 times, most recently from 979dd72 to 752e100 Compare October 8, 2023 16:16
@thibaultcha thibaultcha removed the pr/on-hold PR: Do not merge/close label Oct 23, 2023
@thibaultcha thibaultcha force-pushed the refactor/proxy-wasm branch 6 times, most recently from a944270 to 72960d7 Compare October 31, 2023 22:25
@thibaultcha thibaultcha force-pushed the refactor/proxy-wasm branch 6 times, most recently from 54f7d26 to ea6d6eb Compare November 6, 2023 18:11
@thibaultcha thibaultcha added the pr/merge-in-progress PR: Merge in progress (do not push) label Nov 6, 2023
* Allow for recycling the root instance (which is not managed by isolation modes).
* Allow for smoother recycling of http instances (including when the
  root instance has trapped, e.g. during on_tick).
* Also recycle trapped instances in the busy queue.
* Less verbose context creation of fresh and/or recycled instances
  starting in atypical steps (e.g. on_response_headers).
Prior, the sweeping queue was only destroyed at worker shutdown, but it
was intended for it to be swept sooner.

This removes the sweeping queue and frees instances on the spot instead.
@thibaultcha thibaultcha force-pushed the refactor/proxy-wasm branch 2 times, most recently from 31fe7cf to 1f908bc Compare November 8, 2023 02:07
The main goal of this overhaul is to simplify `on_context_create`, make
it fully re-entrant *and* properly handle instance recycling at the same
time.

The way to do so, in my opinion, was to move `pwexec` creation where
`rexec` already was. In other words, always lookup the context id in the
instance rbtree, and if not found, create it. This means that
surrounding code also needed big overhauls. It also removes the
reference counting poor man's GC of the older implementation. The code
became really ugly by then so I took the time to also review this
module's code structure instead of making a *very* ugly commit.

This new ngx_proxy_wasm.c file should be much easier to read and follow
now.

One change I do not fully like is moving the `next_id` to a global
counter, but we do not have a "global proxy-wasm conf" object yet. I
also started thinking about pre-allocating a number of `pwexecs` (like
`worker_connections`) and use free/busy queue that all filter chains can
dip into to get a context id + context memory zone. Perhaps for a later
time.
@thibaultcha thibaultcha merged commit ecd7896 into main Nov 8, 2023
34 checks passed
@thibaultcha thibaultcha deleted the refactor/proxy-wasm branch November 8, 2023 17:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr/merge-in-progress PR: Merge in progress (do not push)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant