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(core) expose API to obtain node_id #3234

Merged
merged 2 commits into from
Feb 19, 2018
Merged

refactor(core) expose API to obtain node_id #3234

merged 2 commits into from
Feb 19, 2018

Conversation

hishamhm
Copy link
Contributor

@hishamhm hishamhm commented Feb 19, 2018

Move the unique identifier of the Kong node from the kong.cluster_events module into an internal value, accessible via kong.tools.public.get_node_id().

This way, the same unique identifier can be used for different features that need it, such as cluster events and identifying per-node data in health check reporting.

@hishamhm hishamhm requested a review from thibaultcha February 19, 2018 17:50
@hishamhm hishamhm force-pushed the feat/node_id branch 3 times, most recently from df81a9e to 1acbc5a Compare February 19, 2018 20:44
@hishamhm hishamhm changed the title refactor(core) store node_id as a singleton refactor(core) expose API to obtain node_id Feb 19, 2018
-- Obtain the unique node id for this node.
do
local node_id
function _M.get_node_id()
Copy link
Member

Choose a reason for hiding this comment

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

I'm a bit worried that we will see this utility being used in the init phase soon - and since we do nothing to guard against it, the issue would silently go unnoticed. This sort of issue has happened so many times in this project that it is one of the reasons behind my suggestion to put this in a controlled API if it is to be consumed publicly.

Until the proper API (with init and init_worker seeding and the proper initialization mechanisms are in place) - can we make this function throw an error if called in init?

if ngx.get_phase() == "init" then
  error("API disabled in the context of init_by_lua", 2)
end

Copy link
Member

Choose a reason for hiding this comment

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

A proper init function for the API would also avoid making this API return errors, and always succeed (since the shm dance is either moved to the initialization of the module, or removed altogether as discussed 1-1) - let's no go down that road yet though...

@@ -56,6 +57,7 @@ return {
tagline = tagline,
version = version,
hostname = utils.get_hostname(),
node_id = public.get_node_id(),
Copy link
Member

Choose a reason for hiding this comment

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

Should we handle the error here? Granted it is unlikely that get_node_id() will never be invoked before this particular call, but there is no safeguard against it if things ever change...

Copy link
Contributor Author

@hishamhm hishamhm Feb 19, 2018

Choose a reason for hiding this comment

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

I thought about that, but if we can't get a node_id here there's not much we can do other than just not returning the field, so returning nil naturally "erases" that field from the result (the only other thing that comes to mind would be to log the error; we could do that, but it seems overkill, and we don't for utils.get_hostname() anyway).

Copy link
Member

Choose a reason for hiding this comment

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

True, but when raising this concern, I am mostly worried about writing reference code - another typical pattern we have seen in this project has been the copy-pasting of countless bits of Lua code around the project - spreading bad patterns all around the place...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Knowing when to handle an error and when not to is not a bad pattern per se, but it takes experience -- so I see what you mean. Added the check here, no biggie.

another typical pattern we have seen in this project has been the copy-pasting of countless bits of Lua code around the project - spreading bad patterns all around the place...

Yeah, and ultimately, we should strive to reduce the copy-pasting rather than just providing people better bits to replicate. :)

Copy link
Member

Choose a reason for hiding this comment

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

Yeah, and ultimately, we should strive to reduce the copy-pasting rather than just providing people better bits to replicate. :)

My struggle for the past 3 years :) Never did we get allocated time to actually work on this - glad we will soon

Move the unique identifier of the Kong node from the
`kong.cluster_events` module into an internal value,
accessible via `kong.tools.public.get_node_id()`.

This way, the same unique identifier can be used for different
features that need it, such as cluster events and identifying
per-node data in health check reporting.
This adds a `"node_id"` entry to the `/` status endpoint.
This is a UUID generated on Kong start (each restart of Kong
will result in a new `node_id`).
@hishamhm hishamhm merged commit ee783a3 into master Feb 19, 2018
@Tieske Tieske deleted the feat/node_id branch February 19, 2018 23:37
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.

2 participants