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

feat(timerng) integrate the lua-resty-timer-ng #8912

Merged
merged 1 commit into from
Jun 10, 2022

Conversation

ADD-SP
Copy link
Contributor

@ADD-SP ADD-SP commented Jun 7, 2022

Summary

Integrating https://github.com/Kong/lua-resty-timer-ng into Kong-CE.

Full changelog

  • fix(plugin_servers) bypass timer library
  • feat(AdminAPI) add /timers route
  • chore(rockspec) add resty.timerng as dependency
  • tests(unit/db/cache_warmup) fix
  • tests(integration/cmd/quit) fix
  • tests(integration/hybrid_mode/ocsp) fix
  • tests(plugin/rate-limiting/access) fix
  • tests(integration/db/query-semaphore) fix
  • tests(integration/admin/timers) add tests for http://kong_admin/timers
  • tests(integration/cmd/start_stop) fix
  • tests(integration/proxy/router) fix
  • docs(CHANGELOG) update

Response of http://kong_admin/timers

{
    "flamegraph": {
        "running": "@./kong/runloop/balancer/targets.lua:248:Lua() 0\n@./kong/plugins/prometheus/handler.lua:15:Lua();@./kong/plugins/prometheus/exporter.lua:153:init_worker();@./kong/plugins/prometheus/prometheus.lua:673:init_worker();@/build/luarocks/share/lua/5.1/resty/counter.lua:71:new() 0\n@/build/luarocks/share/lua/5.1/resty/worker/events.lua:386:Lua() 0\n@./kong/runloop/handler.lua:1087:Lua();@./kong/runloop/balancer/init.lua:228:init();@./kong/runloop/balancer/targets.lua:47:init() 0\n=init_worker_by_lua:2:main();@./kong/init.lua:617:init_worker();@./kong/global.lua:172:init_worker_events();@/build/luarocks/share/lua/5.1/resty/worker/events.lua:620:configure();@/build/luarocks/share/lua/5.1/resty/worker/events.lua:386:do_timer() 0\n=init_worker_by_lua:2:main();@./kong/init.lua:706:init_worker();@./kong/runloop/handler.lua:1086:before() 0\n",
        "elapsed_time": "@./kong/runloop/balancer/targets.lua:248:Lua() 0\n@./kong/plugins/prometheus/handler.lua:15:Lua();@./kong/plugins/prometheus/exporter.lua:153:init_worker();@./kong/plugins/prometheus/prometheus.lua:673:init_worker();@/build/luarocks/share/lua/5.1/resty/counter.lua:71:new() 0\n@/build/luarocks/share/lua/5.1/resty/worker/events.lua:386:Lua() 0\n@./kong/runloop/handler.lua:1087:Lua();@./kong/runloop/balancer/init.lua:228:init();@./kong/runloop/balancer/targets.lua:47:init() 0\n=init_worker_by_lua:2:main();@./kong/init.lua:617:init_worker();@./kong/global.lua:172:init_worker_events();@/build/luarocks/share/lua/5.1/resty/worker/events.lua:620:configure();@/build/luarocks/share/lua/5.1/resty/worker/events.lua:386:do_timer() 2\n=init_worker_by_lua:2:main();@./kong/init.lua:706:init_worker();@./kong/runloop/handler.lua:1086:before() 17\n",
        "pending": "@./kong/runloop/balancer/targets.lua:248:Lua() 0\n@./kong/plugins/prometheus/handler.lua:15:Lua();@./kong/plugins/prometheus/exporter.lua:153:init_worker();@./kong/plugins/prometheus/prometheus.lua:673:init_worker();@/build/luarocks/share/lua/5.1/resty/counter.lua:71:new() 0\n@/build/luarocks/share/lua/5.1/resty/worker/events.lua:386:Lua() 0\n@./kong/runloop/handler.lua:1087:Lua();@./kong/runloop/balancer/init.lua:228:init();@./kong/runloop/balancer/targets.lua:47:init() 0\n=init_worker_by_lua:2:main();@./kong/init.lua:617:init_worker();@./kong/global.lua:172:init_worker_events();@/build/luarocks/share/lua/5.1/resty/worker/events.lua:620:configure();@/build/luarocks/share/lua/5.1/resty/worker/events.lua:386:do_timer() 0\n=init_worker_by_lua:2:main();@./kong/init.lua:706:init_worker();@./kong/runloop/handler.lua:1086:before() 0\n"
    },
    "sys": {
        "running": 0,
        "runs": 7,
        "pending": 0,
        "waiting": 7,
        "total": 7
    },
    "timers": {
        "unix_timestamp=1654583694012.000000;counter=6:meta=@/build/luarocks/share/lua/5.1/resty/counter.lua:71:new()": {
            "name": "unix_timestamp=1654583694012.000000;counter=6:meta=@/build/luarocks/share/lua/5.1/resty/counter.lua:71:new()",
            "meta": {
                "name": "@/build/luarocks/share/lua/5.1/resty/counter.lua:71:new()",
                "callstack": "@./kong/plugins/prometheus/handler.lua:15:Lua();@./kong/plugins/prometheus/exporter.lua:153:init_worker();@./kong/plugins/prometheus/prometheus.lua:673:init_worker();@/build/luarocks/share/lua/5.1/resty/counter.lua:71:new()"
            },
            "stats": {
                "finish": 2,
                "runs": 2,
                "elapsed_time": {
                    "min": 0,
                    "max": 0,
                    "avg": 0,
                    "variance": 0
                },
                "last_err_msg": ""
            }
        },
        "unix_timestamp=1654583693919.000000;counter=0:meta=@./kong/db/init.lua:167:init_worker()": {
            "name": "unix_timestamp=1654583693919.000000;counter=0:meta=@./kong/db/init.lua:167:init_worker()",
            "meta": {
                "name": "@./kong/db/init.lua:167:init_worker()",
                "callstack": "=init_worker_by_lua:2:main();@./kong/init.lua:599:init_worker();@./kong/db/init.lua:167:init_worker()"
            },
            "stats": {
                "finish": 0,
                "runs": 0,
                "elapsed_time": {
                    "min": 9999999,
                    "max": -1,
                    "avg": 0,
                    "variance": 0
                },
                "last_err_msg": ""
            }
        },
        "unix_timestamp=1654583694010.000000;counter=5:meta=@./kong/runloop/handler.lua:1181:before()": {
            "name": "unix_timestamp=1654583694010.000000;counter=5:meta=@./kong/runloop/handler.lua:1181:before()",
            "meta": {
                "name": "@./kong/runloop/handler.lua:1181:before()",
                "callstack": "=init_worker_by_lua:2:main();@./kong/init.lua:706:init_worker();@./kong/runloop/handler.lua:1181:before()"
            },
            "stats": {
                "finish": 0,
                "runs": 0,
                "elapsed_time": {
                    "min": 9999999,
                    "max": -1,
                    "avg": 0,
                    "variance": 0
                },
                "last_err_msg": ""
            }
        },
        "unix_timestamp=1654583694009.000000;counter=4:meta=@./kong/runloop/handler.lua:1154:before()": {
            "name": "unix_timestamp=1654583694009.000000;counter=4:meta=@./kong/runloop/handler.lua:1154:before()",
            "meta": {
                "name": "@./kong/runloop/handler.lua:1154:before()",
                "callstack": "=init_worker_by_lua:2:main();@./kong/init.lua:706:init_worker();@./kong/runloop/handler.lua:1154:before()"
            },
            "stats": {
                "finish": 0,
                "runs": 0,
                "elapsed_time": {
                    "min": 9999999,
                    "max": -1,
                    "avg": 0,
                    "variance": 0
                },
                "last_err_msg": ""
            }
        },
        "unix_timestamp=1654583695942.000000;counter=10:meta=@/build/luarocks/share/lua/5.1/resty/worker/events.lua:386:Lua()": {
            "name": "unix_timestamp=1654583695942.000000;counter=10:meta=@/build/luarocks/share/lua/5.1/resty/worker/events.lua:386:Lua()",
            "meta": {
                "name": "@/build/luarocks/share/lua/5.1/resty/worker/events.lua:386:Lua()",
                "callstack": "@/build/luarocks/share/lua/5.1/resty/worker/events.lua:386:Lua()"
            },
            "stats": {
                "finish": 0,
                "runs": 0,
                "elapsed_time": {
                    "min": 9999999,
                    "max": -1,
                    "avg": 0,
                    "variance": 0
                },
                "last_err_msg": ""
            }
        },
        "unix_timestamp=1654583696043.000000;counter=11:meta=@./kong/runloop/balancer/targets.lua:248:Lua()": {
            "name": "unix_timestamp=1654583696043.000000;counter=11:meta=@./kong/runloop/balancer/targets.lua:248:Lua()",
            "meta": {
                "name": "@./kong/runloop/balancer/targets.lua:248:Lua()",
                "callstack": "@./kong/runloop/balancer/targets.lua:248:Lua()"
            },
            "stats": {
                "finish": 0,
                "runs": 0,
                "elapsed_time": {
                    "min": 9999999,
                    "max": -1,
                    "avg": 0,
                    "variance": 0
                },
                "last_err_msg": ""
            }
        },
        "unix_timestamp=1654583693982.000000;counter=2:meta=@./kong/cluster_events/init.lua:207:subscribe()": {
            "name": "unix_timestamp=1654583693982.000000;counter=2:meta=@./kong/cluster_events/init.lua:207:subscribe()",
            "meta": {
                "name": "@./kong/cluster_events/init.lua:207:subscribe()",
                "callstack": "=init_worker_by_lua:2:main();@./kong/init.lua:633:init_worker();@./kong/cache/init.lua:138:init_cache();@./kong/cluster_events/init.lua:207:subscribe()"
            },
            "stats": {
                "finish": 0,
                "runs": 0,
                "elapsed_time": {
                    "min": 9999999,
                    "max": -1,
                    "avg": 0,
                    "variance": 0
                },
                "last_err_msg": ""
            }
        }
    }
}

@ADD-SP ADD-SP requested a review from bungle June 7, 2022 05:20
@ADD-SP ADD-SP marked this pull request as ready for review June 7, 2022 05:20
@ADD-SP ADD-SP requested a review from a team as a code owner June 7, 2022 05:20
@ADD-SP ADD-SP marked this pull request as draft June 7, 2022 05:20
@ADD-SP ADD-SP added this to the 3.0 milestone Jun 7, 2022
@ADD-SP ADD-SP marked this pull request as ready for review June 7, 2022 06:38
kong/api/routes/kong.lua Outdated Show resolved Hide resolved
-- old_post(self, n)
-- ngx.sleep(0)
-- end
-- end
Copy link
Member

Choose a reason for hiding this comment

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

shouldn't this be enabled in case opf legacy timers?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

#7704

This patch looks like it's meant to fix some OpenResty bug, and nothing tested wrong when I disabled it. It doesn't seem to be timer-related.

Copy link
Member

Choose a reason for hiding this comment

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

This was a fix for a lot of flaky tests. @javierguerragiraldez might know better if this can be removed or not.

Here's the underlying issue: openresty/lua-resty-core#337

Copy link
Contributor

Choose a reason for hiding this comment

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

that "some openresty bug" is actually in the docs (https://github.com/openresty/lua-resty-core/blob/master/lib/ngx/semaphore.md#post):

Releases n (default to 1) "resources" to the semaphore instance.
This will not yield the current running "light thread".

but most code seems to assume that yield, and so we get a long runs before the woken-up thread gets some CPU time. if the new time library does include that yield, then yes, this one should be removed. if not, then it does help a lot.

Copy link
Member

Choose a reason for hiding this comment

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

that is not the bug, I would expect the current coro to NOT yield. That is fine. The problem is that if there is nothing else to do, OpenResty simply exits without EVER resuming the released thread.

spec/03-plugins/23-rate-limiting/04-access_spec.lua Outdated Show resolved Hide resolved
@@ -31,6 +31,6 @@ describe("kong quit", function()
assert(helpers.kong_exec("quit --wait 2 --prefix " .. helpers.test_conf.prefix))
ngx.update_time()
local duration = ngx.now() - start
assert.is.near(2, duration, 1.6)
assert.is.near(2, duration, 5)
Copy link
Member

Choose a reason for hiding this comment

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

why does exiting now take more time?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because the new timer library starts many OpenResty's timers, each timer waits for a semaphore (one-second timeout). Increasing to five seconds is mainly in dealing with the slow CI

Copy link
Member

Choose a reason for hiding this comment

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

Ok, but please note that semaphores are buggy, see the other comment and this issue: openresty/lua-resty-core#337

Copy link
Contributor

Choose a reason for hiding this comment

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

i don't think this would make any PM happy. slow exits can be considered a low-priority bug. make sure to raise an issue/ticket to fix this.

Copy link
Contributor

Choose a reason for hiding this comment

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

@ADD-SP let's make sure this behaviour is recorded in JIRA and fix before final release

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have created the FT-3010.

kong/api/routes/kong.lua Outdated Show resolved Hide resolved
kong/conf_loader/init.lua Outdated Show resolved Hide resolved
kong/globalpatches.lua Outdated Show resolved Hide resolved
kong/globalpatches.lua Outdated Show resolved Hide resolved
kong/init.lua Outdated Show resolved Hide resolved
@ADD-SP ADD-SP force-pushed the add_sp/feat/integrate-timer-ng branch 2 times, most recently from 8d75eec to 978b301 Compare June 8, 2022 11:42
kong/conf_loader/init.lua Outdated Show resolved Hide resolved
@ADD-SP ADD-SP force-pushed the add_sp/feat/integrate-timer-ng branch 2 times, most recently from 2565d4c to 79e8f92 Compare June 9, 2022 02:49
@ADD-SP ADD-SP force-pushed the add_sp/feat/integrate-timer-ng branch 7 times, most recently from 369dd66 to 68adc31 Compare June 9, 2022 05:59
kong/globalpatches.lua Outdated Show resolved Hide resolved
kong/globalpatches.lua Outdated Show resolved Hide resolved
@ADD-SP ADD-SP force-pushed the add_sp/feat/integrate-timer-ng branch 4 times, most recently from 215fbd6 to f7cbea3 Compare June 9, 2022 09:16
@fffonion
Copy link
Contributor

fffonion commented Jun 9, 2022

As discussed with @ADD-SP offline, we will first merge in this one, and iterrate on the tests side; Qiqi is working on a determinisitic approach in spec/helpers to ensure timer jobs are executed instead of simply add ngx.sleep. But I don't want to block on this PR and let it become a last minute merge just because of this.

cc @javierguerragiraldez @Tieske how do you feel? (Though you don't leave Request Changes, but I'm asking since your review comments express worry-ness on it)

* fix(plugin_servers) bypass timer library
* feat(AdminAPI) add `/timer` route
* feat(conf) add new config `legacy_timer_mechanism` (boolean)
* chore(rockspec) add `resty.timerng` as dependency
* tests(*) add `wait_timers_*()` for helper
* tests(plugin/zipkin) fix
* tests(unit/db/cache_warmup) fix
* tests(integration/cmd/quit) fix
* tests(integration/hybrid_mode/ocsp) fix
* tests(plugin/rate-limiting/access) fix
* tests(integration/db/query-semaphore) fix
* tests(integration/admin/timers) add tests for `http://kong_admin/timers`
* tests(integration/cmd/start_stop) fix
* tests(integration/proxy/router) fix
* docs(CHANGELOG) update

Co-authored-by: Mayo <i@shoujo.io>
Co-authored-by: Wangchong Zhou <fffonion@gmail.com>
@ADD-SP ADD-SP force-pushed the add_sp/feat/integrate-timer-ng branch from b4ad69d to a48033a Compare June 9, 2022 17:39
@fffonion fffonion merged commit 6780491 into master Jun 10, 2022
@fffonion fffonion deleted the add_sp/feat/integrate-timer-ng branch June 10, 2022 02:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants