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

Dynamically create hash ring #25

Open
klinquist opened this issue Dec 17, 2014 · 2 comments
Open

Dynamically create hash ring #25

klinquist opened this issue Dec 17, 2014 · 2 comments

Comments

@klinquist
Copy link

Assuming I have X servers for a given service - and I want all the parameters (vnodes, weight) to be the same, is there any way to just say...

var correct_server_number = ring.getresult(number_of_servers,"foo bar")

Right now I'm doing something that seems ridiculous, but it may be the most efficient way.. supports up to 10 servers...

var ring = [       new HashRing(["1"]),
             new HashRing(["1","2"])
             new HashRing(["1","2","3"])
             new HashRing(["1","2","3","4"])
             new HashRing(["1","2","3","4","5"])
             new HashRing(["1","2","3","4","5","6"])
             new HashRing(["1","2","3","4","5","6","7"])
             new HashRing(["1","2","3","4","5","6","7","8"])
             new HashRing(["1","2","3","4","5","6","7","8","9"])
             new HashRing(["1","2","3","4","5","6","7","8","9","10"])
           ]


var correct_server_number = ring[number_of_servers].get("foo bar");
@3rd-Eden
Copy link
Owner

I'm not exactly sure what you want, but the range method might be useful here: https://github.com/3rd-Eden/node-hashring/blob/master/index.js#L252-L303

@klinquist
Copy link
Author

3rd-eden: Thanks - with the range function, the hashring needs to already have been defined (if I'm reading it correctly).

I've got half a dozen different server types and for each server type, I'll be scaling up and down instances dynamically based on load. I'll make a db query to find out how many instances there currently are for a given server type and then I'd like to know which instance to assign a user to based on their userid.

I want a function that generates a hashring with (X) "servers" with the "servers" simply being named ("1" through "X")

My code above works by generating 10 hashrings (supporting services for up to 10 servers) allowing me to say "tell me the appropriate server for a service that has 4 instances".

Hope that clarifies things!

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

No branches or pull requests

2 participants