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

Add set_consistent_hashed_upstream #29

Conversation

charliechang
Copy link

Hi
I added the support of consistent hash by reuse the directive set_hashed_upstream. I am not sure if there is any alternative for this. (Maybe can be done by Lua?)

@agentzh
Copy link
Member

agentzh commented Mar 3, 2016

@charliechang Thanks for sharing.

@doujiang24 Will you have a quick look at this PR please? Thanks!

for (i = 0; i < hashConf->nodeLen; i++) {
hashConf->hashNodes[i].name = ul->elts[i];
hashConf->hashNodes[i].hash = ngx_hash_key_lc(hashConf->hashNodes[i].name->data,
hashConf->hashNodes[i].name->len);
Copy link
Member

Choose a reason for hiding this comment

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

One real node should have a number of virtual nodes, usually the number is 160, like nginx chash does: https://github.com/nginx/nginx/blob/master/src/http/modules/ngx_http_upstream_hash_module.c#L299

Otherwise, the consistent hash will have uneven distribution.

@doujiang24
Copy link
Member

@charliechang Thanks for sharing. Yes, I think it's better to be done in Lua by lua-resty-chash.
https://github.com/agentzh/lua-resty-chash

The way does in this PR can be work too, but it can be bad performance. May be we can give up this PR? Or may be my wrong? Thoughts?

@doujiang24
Copy link
Member

@charliechang Sorry, I mean bad performance is not so good, but in some simple case it's good enough.

@charliechang
Copy link
Author

@doujiang24
Thanks for your suggestion. You are right. This method will result in uneven distribution. I will fix it in my side, Regarding the performance, I found there is a const-time implementation of consistent hash. Maybe it can help the problem?
https://github.com/replay/ngx_http_consistent_hash/blob/master/ngx_http_upstream_consistent_hash_module.c

@charliechang
Copy link
Author

@doujiang24
Anyway, you can give up this PR or we can keep discussion. It is your call. Thanks for review.

@doujiang24
Copy link
Member

@charliechang Sorry, I make a stupid mistake here, the performance should not be a problem, please forgive me. Let's continue discuss.
I also found some code style things, Better use the ngx-releng tool from the nginx-devel-utils repos to check your code.

switch (type) {
case ngx_http_set_misc_distribution_modula:
return (uint32_t) hash % (uint32_t) ul->nelts;
case ngx_http_set_misc_distribution_modula:
Copy link
Member

Choose a reason for hiding this comment

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

case should left align with switch

@charliechang
Copy link
Author

@doujiang24
I added the support of virtual nodes and fixed the problems you mentioned above. Please let me know if there is anything I can make it better!

@@ -1,27 +1,71 @@
#ifndef DDEBUG
#define DDEBUG 0
#endif

Copy link
Member

Choose a reason for hiding this comment

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

unnecessary blank line here.

@doujiang24
Copy link
Member

@charliechang
Nice, it looks good to me now, but some code style things, hope you won't be impatient about this.
You'd better use ngx-releng to check the basic code style, this is the recommended way.
Will you add some test case for this? like t/hashed-upstream.t in this repos.
Some more you may need :)

  1. https://openresty.gitbooks.io/programming-openresty/content/testing/index.html
  2. https://github.com/openresty/test-nginx

@charliechang
Copy link
Author

@doujiang24
No problem, I will check the coding style and add test cases.

ngx_http_set_hashed_upstream_consistent_hash_node *nodes, uint32_t node_len,
uint32_t target)
{
uint32_t m = 0, n = node_len, l;
Copy link
Member

Choose a reason for hiding this comment

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

Style: nginx coding style requires each C function local variable to be defined in separate lines.

@agentzh
Copy link
Member

agentzh commented Mar 22, 2016

@charliechang There are so many coding style issues. Please check the existing code for the right style. Thank you for your contribution!


if (hash_conf->hash_nodes == NULL) {
return NGX_CONF_ERROR;
}
Copy link
Author

Choose a reason for hiding this comment

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

@agentzh Could you point out where are the indent problems around here? Thanks

Copy link
Member

Choose a reason for hiding this comment

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

@charliechang Sorry, my mistake. I thought the long line breaks were indentation problems.

@charliechang
Copy link
Author

@agentzh I fixed the coding style issues accordingly. Please check it again. Thanks!

@charliechang
Copy link
Author

@agentzh
Is this PR still valid? since it is been a while...

@jbergstroem
Copy link

I this this would be a good addition. Any comments on why it stalled? Better implemented elsewhere?

@agentzh
Copy link
Member

agentzh commented Jun 27, 2017

@jbergstroem Yeah, now that we have balancer_by_lua* and such hashing is already implemented in lua-resty-balancer:

https://github.com/agentzh/lua-resty-balancer/

It's way more flexible and powerful.

@jbergstroem
Copy link

@agentzh said:
@jbergstroem Yeah, now that we have balancer_by_lua* and such hashing is already implemented in lua-resty-balancer

Of course. Thanks for the pointer. So much to learn! [suggesting we close this then]

@agentzh agentzh closed this Jun 28, 2017
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.

None yet

4 participants