3rd-Eden / node-hashring

hashring is a consistent hashing algorithm for Node.js that is compatible with libketama and python's hash_ring package
MIT License
350 stars 61 forks source link

Dynamically create hash ring #25

Open klinquist opened 9 years ago

klinquist commented 9 years ago

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 commented 9 years ago

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 commented 9 years ago

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!