Skip to content

add exit_worker_by* feature #927

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

Closed
wants to merge 17 commits into from
Closed

Conversation

lixianliang
Copy link

I hereby granted the copyright of the changes in this pull request
to the authors of this lua-nginx-module project.
你好,我在使用ngx_lua的场景中是一些图片计算或GPU(ai)计算,在init_worker_by阶段会初始GPU或图片Caffe资源,当进行reload的时候,由于worker process exit没有去清理GPU或者Caffe内部的资源,会导致worker出现core,所以我针对此请求再ngx_lua添加了exit_worker_by阶段。希望eixt_worker_by能合并到master。我相信其他的使用者也有可能会用此功能。
我们的技术堆栈为 nginx + lua + (imagexxx.so | aixxx.so ==> 会封转成lua扩展)

@agentzh
Copy link
Member

agentzh commented Dec 13, 2016

@lixianliang Thank you for your contribution! But please use English instead of Chinese. Thanks for your cooperation!

0,
(void *) ngx_http_lua_exit_worker_by_inline },

{ ngx_string("exit_worker_by_lua"),
Copy link
Member

Choose a reason for hiding this comment

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

Nice work!

I believe the newly added Lua handlers should only use the by_block form though, as per the ssl_certificate_by_lua_block demonstrates?

Copy link
Member

@thibaultcha thibaultcha Dec 13, 2016

Choose a reason for hiding this comment

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

I believe you would also need to update ngx_http_lua_{phase | common | util}.c to introduce this new phase context? As well as setting the context itself? I could be wrong.

I believe it will also be requested of you that you introduce tests for this feature.

Copy link
Author

Choose a reason for hiding this comment

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

i reference to init_worker_by*, there have three method(init_worker_by_lua init_worker_by_lua_block init_worker_by_lua_file)

Copy link
Member

Choose a reason for hiding this comment

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

Yes, the init_worker phase was introduced before the *_by_lua_block form and as of today, this module still maintains the older *_by_lua form for backwards compatibility reasons. However, newly introduced phases (as the ssl_certificate one) are only implemented in the *_by_lua_block form.

Copy link
Member

Choose a reason for hiding this comment

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

Hey, on the bright side of things, it's less work for you ;)

Copy link
Author

Choose a reason for hiding this comment

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

ok,thank you. i will add this.

@lixianliang
Copy link
Author

sorry,my english very pool,i need help。latter i will push request again。

@@ -0,0 +1,60 @@

/*
* Copyright (C) Xianliang Li
Copy link
Member

Choose a reason for hiding this comment

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

For this patch to merge, you have to grant your copyright to the authors of this module. So please remove this copyright notice comment.

0,
(void *) ngx_http_lua_exit_worker_by_inline },

{ ngx_string("exit_worker_by_lua"),
Copy link
Member

Choose a reason for hiding this comment

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

Newly added directives should only provide the *_by_lua_block and *_by_lua_file forms. Please remove this exit_worker_by_lua directive. Thank you.

Copy link
Author

Choose a reason for hiding this comment

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

enen,i will correction this。


lmcf->exit_worker_src.data = name;
lmcf->exit_worker_src.len = ngx_strlen(name);
} else {
Copy link
Member

Choose a reason for hiding this comment

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

Style: we need a blank line before the } else { line. See other similar places in the existing code base. Thank you!

@agentzh
Copy link
Member

agentzh commented Dec 13, 2016

Some other suggestions:

  1. Please add corresponding test cases for this new feature to the existing test suite. We always require new tests to cover every new feature :)
  2. Please add some corresponding docs for this new feature to the doc/HttpLuaModule.wiki file and update README.markdown with the ngx-releng tool accordingly.
  3. The ngx-releng tool reports some coding style issues in your patch. Please fix them accordingly.

Thanks!

@agentzh
Copy link
Member

agentzh commented Dec 13, 2016

@thibaultcha Thanks for your review! Next time I'll just wait :)

@thibaultcha
Copy link
Member

@agentzh no problem, I think both of use jumped on this at the same time :)

side note: quite excited to see this as I was thinking about making the same contribution recently :) Just happy to help.

@thibaultcha
Copy link
Member

thibaultcha commented Dec 13, 2016

@lixianliang At some point, you will also be asked to write the documentation for this new phase handler. If you don't feel comfortable enough for providing an English version, I'd be happy to help you.

ngx_int_t ngx_http_lua_exit_worker_by_file(ngx_log_t *log,
ngx_http_lua_main_conf_t *lmcf, lua_State *L);

void ngx_http_lua_exit_worker(ngx_cycle_t *cycle);
Copy link
Contributor

Choose a reason for hiding this comment

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

it only needs one space between void and ngx_http_lua_exit_worker

@lixianliang
Copy link
Author

Hi, I want to add exit_worker feature, This function runs the specified Lua code upon every Nginx worker process's exiting when the master process is enabled. This hook is often used to release resources when init_worker_by* create resources, To prevent the worker exit abnormal

|| lmcf->exit_worker_handler == NULL
|| lmcf->lua == NULL)
{
return;
Copy link
Contributor

Choose a reason for hiding this comment

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

there should be 8 spaces.

Copy link
Author

Choose a reason for hiding this comment

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

ok。

@@ -0,0 +1,60 @@
# vim:set ft= ts=4 sw=4 et fdm=marker:

use Test::Nginx::Socket 'no_plan';
Copy link
Contributor

Choose a reason for hiding this comment

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

use plan tests is more better, its the default way.

Copy link
Author

Choose a reason for hiding this comment

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

sorry, i frist use test-nginx,so something not good.

=== TEST 1: simple exit_worker_by_lua_block
--- http_config
exit_worker_by_lua_block {
ngx.log(ngx.NOTICE, "log from exit_worker_by_lua_block")
Copy link
Contributor

Choose a reason for hiding this comment

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

we need to confirm if the log is output

Copy link
Author

Choose a reason for hiding this comment

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

i fix 151-exit-worker.t base on iresty/test-nginx and commited.

Copy link
Member

Choose a reason for hiding this comment

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

@lixianliang Apparently iresty/test-nginx is a fork and does not count here :) That feature needs to be merged into the official test-nginx repo before this can be merged otherwise the Travis CI would be failing.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, Now i use offical test-nginx.

--- http_config
exit_worker_by_lua_block {
ngx.log(ngx.NOTICE, "log from exit_worker_by_lua_block")
-- grep_error_log chop
Copy link
Contributor

Choose a reason for hiding this comment

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

We'd better delete useless lines from 18-23

Copy link
Author

Choose a reason for hiding this comment

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

I met a problem when write exit_worker test。this log print when worker process eixtting

Copy link
Contributor

@membphis membphis Jan 5, 2017

Choose a reason for hiding this comment

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

I have submitted a PR openresty/test-nginx#52

You can make the test case with this version.

Here is the example:

=== TEST 1: 
--- http_config
--- config
    location /t {
        echo "ok";
    }
--- request
GET /t
--- stop_after_request
--- error_log
received, shutting down

Copy link
Author

Choose a reason for hiding this comment

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

i make test base on iresty/test-nginx and resubmit 151-exit-worker.t

@lixianliang
Copy link
Author

fix code style

README.markdown Outdated

[Back to TOC](#directives)

exit_worker_by_lua_block
Copy link
Member

Choose a reason for hiding this comment

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

Typo? Should be _by_lua_file here instead, right?

Copy link
Author

Choose a reason for hiding this comment

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

My mistake.

@@ -1013,6 +1013,8 @@ Directives
* [init_worker_by_lua](#init_worker_by_lua)
Copy link
Member

Choose a reason for hiding this comment

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

BTW, this file is automatically generated (you can check out the comment at the beginning of this file).

You should edit doc/HttpLuaModule.wiki file instead and use the ngx-releng tool chain to update this markdown file automatically from the .wiki file.

Copy link
Author

Choose a reason for hiding this comment

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

ok

--- response_body
ok
--- stop_after_request
--- error_log
Copy link
Member

Choose a reason for hiding this comment

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

We need to add a --- shutdown_error_log section instead of abusing the request-time --- error_log directive and the hacky --- stop_after_request section IMHO :)

Copy link
Author

Choose a reason for hiding this comment

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

This test cast i update util has shutdown_error_log.

@lixianliang
Copy link
Author

@membphis can you review this exit_worker feature?


Runs the specified Lua code upon every Nginx worker process's exiting when the master process is enabled.

This hook is often used to release resources when init_worker_by* create resources, To prevent the worker exit abnormal.
Copy link
Member

Choose a reason for hiding this comment

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

init_worker_by* => init_worker_by_lua*.

Also need <code></code> around init_worker_by_lua*.

Copy link
Member

Choose a reason for hiding this comment

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

Also, better add a link to the init_worker_by_lua* label.

Copy link
Author

Choose a reason for hiding this comment

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

@agentzh I add init_worker_by_lua* label to init_worker_by_lua_block not init_worker_by_lua. I want recommend others use init_worker_by_lua_block. It has a problem?

GET /t
--- response_body
ok
--- shutdown_error_log
Copy link
Member

Choose a reason for hiding this comment

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

Do we have shutdown_error_log already? It seems that your test plan above explicitly excludes this check.

@membphis I think you were working on this feature for Test::Nginx?

Copy link
Author

Choose a reason for hiding this comment

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

@agentzh shutdown_error_log already use.

Copy link
Contributor

@membphis membphis Feb 10, 2017

Choose a reason for hiding this comment

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

shutdown_error_log does not support the TEST_NGINX_USE_HUP flag.

TEST_NGINX_USE_HUP needs to keep nginx working, shutdown_error_log needs nginx shutdown.
They are in conflict.

Copy link
Author

Choose a reason for hiding this comment

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

ok

@membphis
Copy link
Contributor

membphis commented Feb 9, 2017

@agentzh @lixianliang

I have submitted the PR: openresty/test-nginx#58

--- response_body
ok
--- shutdown_error_log
get 11val: 1000
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 to wrong

Copy link
Author

Choose a reason for hiding this comment

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

OK,I fix, but use --- shutdown_error_log test success,so shutdown_error_log has bug?

ok
--- shutdown_error_log
get val: 100

Copy link
Member

Choose a reason for hiding this comment

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

Better remove this blank line.

Copy link
Author

Choose a reason for hiding this comment

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

ok


repeat_each(2);

plan tests => repeat_each() * (blocks() * 2);
Copy link
Member

@agentzh agentzh Feb 12, 2017

Choose a reason for hiding this comment

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

Because the official test-nginx does not come with shutdown_error_log yet and your branch is currently passing the travis ci tests completely, which means this test plan is wrong since it does not count the shutdown_error_log test in the first place.

Copy link
Author

Choose a reason for hiding this comment

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

Get.

@agentzh
Copy link
Member

agentzh commented Mar 3, 2017

Please rebase to the latest master. Thank you!

@lixianliang
Copy link
Author

@agentzh fix

@agentzh
Copy link
Member

agentzh commented Mar 7, 2017

@lixianliang Great! Thank you! We'll look into this shortly.

@thibaultcha thibaultcha mentioned this pull request Mar 10, 2017
@lixianliang
Copy link
Author

@agentzh
I hope this feature to merge early, Thranks.

@agentzh
Copy link
Member

agentzh commented Apr 19, 2017

@lixianliang Will take care of this as soon as I can manage. Sorry for the delay on my side!

@lixianliang
Copy link
Author

@agentzh ok,Thanks

@rainingmaster
Copy link
Member

@agentzh @doujiang24 it seem other guy need this feature too, openresty/openresty#597, will we merge it into master in future?

Copy link
Member

@doujiang24 doujiang24 left a comment

Choose a reason for hiding this comment

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

@lixianliang Sorry for the long delay, will you please rebase to the latest master? there are some conflicts, thanks :)

ngx_str_t *value;
ngx_http_lua_main_conf_t *lmcf = conf;

dd("enter");
Copy link
Member

Choose a reason for hiding this comment

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

better use more meaning use debug message here. people will see this message in the error log when they opened the DDEBUG.

@mergify
Copy link

mergify bot commented Jun 26, 2020

This pull request is now in conflict :(

@mergify mergify bot added the conflict label Jun 26, 2020
@doujiang24
Copy link
Member

Thanks, already merged in #1682.

@doujiang24 doujiang24 closed this Jul 23, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants