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

used table functions of LuaJIT for better performance. #3673

Merged
merged 1 commit into from
Feb 1, 2019

Conversation

moonming
Copy link
Contributor

What this PR does / why we need it:
used table.new and table.clear functions of LuaJIT for better performance.

Which issue this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close that issue when PR gets merged): fixes #

Special notes for your reviewer:

@k8s-ci-robot k8s-ci-robot added cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/S Denotes a PR that changes 10-29 lines, ignoring generated files. labels Jan 18, 2019
@@ -46,15 +49,14 @@ local function flush(premature)
return
end

local current_metrics_batch = metrics_batch
Copy link
Member

Choose a reason for hiding this comment

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

You are changing the behaviour here. The idea was to take current batch as soon as timer calls flush function and reset the main queue so that it can accept more metrics and continue with encoding and sending the current metrics batch while the other one is being filled.

With your change we will potentially lose more metrics. Imagine timer (flush function) kicks in right before the time when the current batch is full. It'll start encoding the metrics and during this time every new metric will be dropped. Given cjson is quite fast at encoding there'll probably not many metrics being dropped but if we can avoid that why not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

got it. I will fix that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ngx.log(ngx.WARN, "omitting metrics for the request, current batch is full")
return
end

table.insert(metrics_batch, metrics())
metrics_batch[metrics_size + 1] = metrics()
Copy link
Member

Choose a reason for hiding this comment

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

Does this custom table implementation not have insert?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it has the insert method, but the table.insert is an O(n) operation due to the lj_tab_len call inside LuaJIT.
We can avoid it in hot loops for better performance.

-- if an Nginx worker processes more than (MAX_BATCH_SIZE/FLUSH_INTERVAL) RPS then it will start dropping metrics
local MAX_BATCH_SIZE = 10000
local FLUSH_INTERVAL = 1 -- second

local metrics_batch = new_tab(MAX_BATCH_SIZE, 0)
Copy link
Member

Choose a reason for hiding this comment

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

What's the second argument for? Why 0? A link to docs would be sufficient too.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

doc: https://github.com/openresty/luajit2/blob/v2.1-agentzh/doc/extensions.html
table.new(narray, nhash), the second argument is for hash.

@ElvinEfendi
Copy link
Member

Thanks for the contribution @moonming ! Any reason why not use nkeys as well for counting?

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Feb 1, 2019
@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: ElvinEfendi, moonming

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Feb 1, 2019
@k8s-ci-robot k8s-ci-robot merged commit eddbcc7 into kubernetes:master Feb 1, 2019
@moonming
Copy link
Contributor Author

Thanks for the contribution @moonming ! Any reason why not use nkeys as well for counting?

we can use the nkeys after merged this: #3747, which upgrade luajit2 to the lastest version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. size/S Denotes a PR that changes 10-29 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants