├── .gitignore
├── .travis.yml
├── CHANGELOG
├── LICENSE
├── Makefile
├── README.md
├── benchmarks
├── benchmark.js
└── methods.js
├── doc
└── index.html
├── index.js
├── package.json
└── tests
├── api.test.js
├── distribution.test.js
└── fixtures
├── hash_ring.py
├── hash_ring.txt
├── ketama.py
├── ketama.txt
├── libmemcached.json
├── pylibmc.txt
└── test_pylibmc.py
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | build
3 | coverage
4 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | sudo: false
2 | language: node_js
3 | node_js:
4 | - "0.8"
5 | - "0.10"
6 | - "0.12"
7 | - "iojs"
8 | before_install:
9 | - 'if [ "${TRAVIS_NODE_VERSION}" == "0.8" ] ; then npm install -g npm@2.7.3; fi'
10 | script:
11 | - "npm run test-travis"
12 |
--------------------------------------------------------------------------------
/CHANGELOG:
--------------------------------------------------------------------------------
1 | Aug 21, 2015 (3.2.0)
2 | - Changes to the .travis.yml
3 | - Binary add-on is removed infavor of a pure JS solution, this also fixes build
4 | issues with IO.JS 3.0
5 |
6 | Unknown, 2015 (3.1.0)
7 | - Replica's can no longer be set to 0, it should be 1 at a minimum.
8 |
9 | Jan 16, 2015 (3.1.0)
10 | - Bump NaN dependency.
11 | - Removed 0.6 from Travis as it's getting install errors for certain dependencies
12 | consider support for 0.6 removed.
13 |
14 | Sept 22, 2014 (3.0.0)
15 | - libketama removes the default supplied port before hashing keys, we now also do
16 | this to ensure libketama compatibility.
17 | - Upgraded all dependencies to the latest versions.
18 |
19 | May 17, 2014 (2.0.0)
20 | - Upgraded Nan and removed node 0.4 support.
21 |
22 | Oct 16, 2013 (1.0.3)
23 | - Use the bindings module for proper DEBUG builds.
24 |
25 | Sep 13, 2013 (1.0.2)
26 | - Node.js 0.11 support
27 |
28 | Mar 14, 2013 (1.0.1)
29 | - Node.js 0.10 support
30 |
31 | Feb 24, 2013 (1.0.0)
32 | - Refactored the whole driver, these changes are incompatible with the 0.0.x
33 | releases as they have changed the distribution of keys.
34 |
35 | Jan 11, 2013
36 | - Added LRU caching instead of a plain object for caching, this prevents memory
37 | leaks
38 |
39 | May 03, 2012
40 | - Updated test suites
41 | - Fixed zero arguments bug
42 |
43 | May 05, 2011
44 | - Added the missing `removeServer` method.
45 |
46 | April 22, 2011
47 | - Externalized the bisection and applied indutny / donnerjack's patches.
48 | - Added test suite
49 | - Optimized nodes lookup
50 |
51 | April 21, 2011
52 |
53 | - Fixed some issues related to the cache addition as the cache needs to be
54 | cleared when new servers are added and replaced.
55 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2010/2011 Arnout Kazemier,3rd-Eden
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining a copy
4 | of this software and associated documentation files (the "Software"), to deal
5 | in the Software without restriction, including without limitation the rights
6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 | copies of the Software, and to permit persons to whom the Software is
8 | furnished to do so, subject to the following conditions:
9 |
10 | The above copyright notice and this permission notice shall be included in
11 | all copies or substantial portions of the Software.
12 |
13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 | THE SOFTWARE.
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | doc:
2 | dox --title "node-hashring" lib/* > doc/index.html
3 |
4 | .PHONY: doc
5 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # HashRing
2 |
3 | The HashRing module provides consistent hashing that is compatible with the
4 | original libketama library that was developed at last.fm. In addition to beeing
5 | compatible with `libketama` it's also compatible with the `hash_ring` module for
6 | Python. See the compatiblity section of the API for more details on this.
7 |
8 | ### Build status
9 |
10 | [](http://travis-ci.org/3rd-Eden/node-hashring)
11 |
12 | ## Installation
13 |
14 | The advised installation of module is done through the Node package manager (npm).
15 |
16 | ```
17 | npm install hashring --save
18 | ```
19 |
20 | The `--save` parameter tells npm that it should automatically add the module to
21 | the `dependencies` field in your package.json.
22 |
23 | ## Usage
24 |
25 | ```js
26 | var HashRing = require('hashring');
27 | ```
28 |
29 | The HashRing constructor is designed to handle different argument types as a
30 | consistent hash ring can be use for different use cases. You can supply the
31 | constructor with:
32 |
33 | #### String
34 |
35 | A single server, possible, but pointless in most cases if you only use one
36 | server, then done use the HashRing at all, it only adds overhead.
37 |
38 | ```js
39 | var ring = new HashRing('127.0.0.1:11211');
40 | ```
41 |
42 | #### Array
43 |
44 | Multiple servers for the HashRing.
45 |
46 | ```js
47 | var ring = new HashRing(['127.0.0.1:11211', '127.0.0.2:11211']);
48 | ```
49 |
50 | #### Object
51 |
52 | An Object where the keys of the Object are the servers and the value can be a
53 | `Number` and it will be seen as weight for server. The value can also be an
54 | Object. Where the key can be a weight or a vnode.
55 |
56 | Weights or vnodes are used to give servers a bigger distribution in the
57 | HashRing. For example you have 3 servers where you want to distribute your keys
58 | over but not all servers are equal in capacity as 2 of those machines have 200mb
59 | of memory and the other has 3.2 gig of memory. The last server is substantially
60 | bigger and there for should receive a greater distrubtion in the ring.
61 |
62 | For a rule of thumb use the amount of memory as weight:
63 |
64 | ```js
65 | var HashRing = require('hashring');
66 |
67 | var ring = new HashRing({
68 | '127.0.0.1:11211': 200,
69 | '127.0.0.2:11211': { weight: 200 }, // same as above
70 | '127.0.0.3:11211': 3200
71 | });
72 | ```
73 |
74 | If you want create a server with multiple vnodes (virtual nodes):
75 |
76 | ```js
77 | var HashRing = require('hashring');
78 |
79 | var ring = new HashRing({
80 | '127.0.0.1:11211': { vnodes: 50 },
81 | '127.0.0.2:11211': { vnodes: 200 },
82 | '127.0.0.3:11211': { vnodes: 100 }
83 | });
84 | ```
85 |
86 | ### Algorithm
87 |
88 | With the second argument you can configure the algorithm that is used to hash
89 | the keys. It defaults to `md5` and can only contain values that are accepted in
90 | Node's `crypto` API. Alternatively you can supply it with a function for a
91 | custom hasher. But do note that the hashValue will be calculated on the result.
92 |
93 | ### Options
94 |
95 | - `vnode count` The amount of virtual nodes per server, defaults to 40 as this
96 | generates 160 points per server as used by ketama hashing.
97 | - `compatiblity` Allows you to force a compatibility mode of the HashRing. It
98 | default to ketama hash rings but if you are coming from a python world you
99 | might want compatibility with the `hash_ring` module. There's a small diff
100 | between `hash_ring` and `ketama` and that's the amount of replica's of a server.
101 | Ketama uses 4 and `hash_ring` uses 3. Set this to `hash_ring` if you want to
102 | use 3.
103 | - `replicas` The amount of replicas per server. Defaults to 4.
104 | - `max cache size` We use a simple LRU cache inside the module to speed up
105 | frequent key lookups, you can customize the amount of keys that need to be
106 | cached. It defaults to 5000.
107 | - `default port` The default port number which will removed from the server
108 | address to provide ketama compatibility.
109 |
110 | ```js
111 | 'use strict';
112 |
113 | // require the module, it returns a HashRing constructor
114 | var HashRing = require('hashring');
115 |
116 | // Setup hash rings with your servers, in this example I just assume that all
117 | // servers are equal, and we want to bump the cache size to 10.000 items.
118 | var ring = new HashRing([
119 | '127.0.0.1',
120 | '127.0.0.2',
121 | '127.0.0.3',
122 | '127.0.0.4'
123 | ], 'md5', {
124 | 'max cache size': 10000
125 | });
126 |
127 | // Now we are going to get some a server for a key
128 | ring.get('foo bar banana'); // returns 127.0.0.x
129 |
130 | // Or if you might want to do some replication scheme and store/fetch data from
131 | // multiple servers
132 | ring.range('foo bar banana', 2).forEach(function forEach(server) {
133 | console.log(server); // do stuff with your server
134 | });
135 |
136 | // Add or remove a new a server to the ring, they accept the same arguments as
137 | // the constructor
138 | ring.add('127.0.0.7').remove('127.0.0.1');
139 | ```
140 |
141 | ### API's Table of Contents
142 |
143 | - [HashRing.continuum()](#hashringcontinuum)
144 | - [HashRing.get()](#hashringgetkey)
145 | - [HashRing.range()](#hashringrangekey-size-unique)
146 | - [HashRing.swap()](#hashringswapfrom-to)
147 | - [HashRing.add()](#hashringaddserver)
148 | - [HashRing.remove()](#hashringremoveserver)
149 | - [HashRing.reset()](#hashringreset)
150 | - [HashRing.end()](#hashringend)
151 |
152 | #### HashRing.continuum()
153 |
154 | Generates the continuum of server a.k.a as the Hash Ring based on their weights
155 | and virtual nodes assigned.
156 |
157 | ---
158 |
159 | #### HashRing.get(**key**)
160 |
161 | Find the correct node for which the key is closest to the point after what the
162 | given key hashes to.
163 |
164 | - **key** String, Random key that needs to be searched in the hash ring
165 |
166 | **returns:** The matching server address.
167 |
168 | ---
169 |
170 | #### HashRing.range(**key**, **size**, **unique**)
171 |
172 | Returns a range of servers. Could be useful for replication.
173 |
174 | - **key** String, Random key that needs to be searched in the hash ring
175 | - **size** Number, Amount items to be returned (maximum). Defaults to the amount
176 | of servers that are in the hashring.
177 | - **unique** Boolean, Don't return duplicate servers. Defaults to true.
178 |
179 | **returns:** The array of servers that we found.
180 |
181 | ---
182 |
183 | #### HashRing.swap(**from*, **to**)
184 |
185 | Hotswap identical servers with each other. This doesn't require the cache to be
186 | completely nuked and the hash ring distribution to be re-calculated.
187 |
188 | Please note that removing the server and adding a new server could potentially
189 | create a different distribution.
190 |
191 | - **from** String, The server that needs to be replaced
192 | - **to** String. The server that replaces the server
193 |
194 | ---
195 |
196 | #### HashRing.add(**server**)
197 |
198 | Add a new server to ring without having to re-initialize the hashring. It
199 | accepts the same arguments as you can use in the constructor.
200 |
201 | - **server** Server that need to be added to the ring.
202 |
203 | ---
204 |
205 | #### HashRing.remove(**server**)
206 |
207 | Remove a server from the hash ring.
208 |
209 | - **server** Server that need to be removed from the ring.
210 |
211 | ---
212 |
213 | #### HashRing.has(**server**)
214 |
215 | Checks if a given server exists in the hash ring.
216 |
217 | - **server** Server for whose existence we're checking.
218 |
219 | ---
220 |
221 | #### HashRing.reset()
222 |
223 | Reset the HashRing and clean up it's references.
224 |
225 | ---
226 |
227 | ### HashRing.end()
228 |
229 | Resets the HashRing and closes the ring.
230 |
231 | ---
232 |
233 | #### HashRing.find(**hashValue**) (private)
234 |
235 | Finds the correct position of the given hashValue in the hashring.
236 |
237 | - **hashValue** Number, The hashValue from the HashRing#hashValue method.
238 |
239 | **returns:** Index of the value in the ring.
240 |
241 | ---
242 |
243 | #### HashRing.hash(**key**) (private)
244 |
245 | Generates the hash for the key.
246 |
247 | - **key** String, Random key that needs to be hashed.
248 |
249 | **returns:** The hashed valued.
250 |
251 | ---
252 |
253 | #### HashRing.digest(**key**) (private)
254 |
255 | Digest hash so we can make a numeric representation from the hash. So it can be
256 | fed in to our hashValue.
257 |
258 | - **key** String, Random key that needs to be hashed.
259 |
260 | **returns:** An array of charCodeAt(0) converted chars.
261 |
262 | ---
263 |
264 | #### HashRing.hashValue(**key**) (private)
265 |
266 | Get the hashed value of the given key, it does the digesting, hashing yo.
267 |
268 | - **key** String, Random key that needs to be hashed.
269 |
270 | **returns:** The hash value of the key.
271 |
272 | ---
273 |
274 | #### HashRing.points(**servers**)
275 |
276 | Returns the points per server.
277 |
278 | - **servers** Optional server that you want to filter for
279 |
280 | **returns:** A Object with server -> Array of points mapping
281 |
282 | ## Upgrading from 0.0.x to 1.x.x
283 |
284 | The 0.0.x releases had some serious flaws that causes it to be incompatible
285 | with the 1.0.0 release. These flaws are the reason that 1.0.0 got released. They
286 | are not backwards compatible as they change the way that keys are hashed. The
287 | following incompatible changes have been made for the sake of consistency:
288 |
289 | - Only accepts hashers that are build in to node (for now). As it can only
290 | guarantee proper hashing of values.
291 | - The replace function was actually doing swaps of keys, so it's original
292 | functionality has been renamed to `swap`. The replace API is now removing the
293 | given server and adds it again. As this causes the servers to be properly
294 | re-hashed.
295 | - The module now requires a C++ compiler to be installed on your server as
296 | hashing the value requires support for 64bit bitshifting and JavaScript as a
297 | language only supports 32bit bitshifting.
298 | - It adds 4 replica's instead 3 for the servers. This is how libketama
299 | originally did it, if you want to have 3 replica's in the hash ring you can
300 | set the compatibility option to `hash_ring`.
301 | - The API's have be renamed, deprecation notices are added in to place and they
302 | will be removed in the next minor version bump (1.1.0)
303 | - Added human readable configuration options instead of camelcase. This
304 | increases readability of the module.
305 | - CRC32 was removed as crypto engine because it was too unstable.
306 |
--------------------------------------------------------------------------------
/benchmarks/benchmark.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | /**
4 | * Benchmark dependencies.
5 | */
6 | var benchmark = require('benchmark');
7 |
8 | /**
9 | * Different hashring drivers.
10 | */
11 | var HashRing = require('../')
12 | // , Hash_ring = require('hash_ring')
13 | , nodes = {'192.168.0.102:11212': 1, '192.168.0.103:11212': 1, '192.168.0.104:11212': 1};
14 |
15 | /**
16 | * Logger.
17 | */
18 | var logger = new(require('devnull'))({ timestamp: false, namespacing: 0 });
19 |
20 | /**
21 | * prebuild hashrings.
22 | */
23 | var ring1 = new HashRing(nodes);
24 | //, ring2 = new Hash_ring(nodes);
25 |
26 | /**
27 | * Benchmark the constructing and generating of a hashring.
28 | */
29 | (
30 | new benchmark.Suite()
31 | ).add('hashring', function(){
32 | var r = new HashRing(nodes);
33 | //}).add('hash_ring', function(){
34 | // var r = new Hash_ring(nodes);
35 | }).on('cycle', function cycle(e) {
36 | var details = e.target;
37 |
38 | logger.log('Finished benchmarking: "%s"', details.name);
39 | logger.metric('Count (%d), Cycles (%d), Elapsed (%d), Hz (%d)'
40 | , details.count
41 | , details.cycles
42 | , details.times.elapsed
43 | , details.hz
44 | );
45 | }).on('complete', function completed() {
46 | logger.info('Benchmark: "%s" is was the fastest.'
47 | , this.filter('fastest').pluck('name')
48 | );
49 | }).run();
50 |
51 | (
52 | new benchmark.Suite()
53 | ).add('hashring', function(){
54 | ring1.get('key' + Math.random());
55 | //}).add('hash_ring', function(){
56 | // ring2.getNode('key' + Math.random());
57 | }).on('cycle', function cycle(e) {
58 | var details = e.target;
59 |
60 | logger.log('Finished benchmarking: "%s"', details.name);
61 | logger.metric('Count (%d), Cycles (%d), Elapsed (%d), Hz (%d)'
62 | , details.count
63 | , details.cycles
64 | , details.times.elapsed
65 | , details.hz
66 | );
67 | }).on('complete', function completed() {
68 | logger.info('Benchmark: "%s" is was the fastest.'
69 | , this.filter('fastest').pluck('name')
70 | );
71 | }).run();
72 |
73 | (
74 | new benchmark.Suite()
75 | ).add('hashring', function(){
76 | ring1.get('key');
77 | //}).add('hash_ring', function(){
78 | // ring2.getNode('key');
79 | }).on('cycle', function cycle(e) {
80 | var details = e.target;
81 |
82 | logger.log('Finished benchmarking: "%s"', details.name);
83 | logger.metric('Count (%d), Cycles (%d), Elapsed (%d), Hz (%d)'
84 | , details.count
85 | , details.cycles
86 | , details.times.elapsed
87 | , details.hz
88 | );
89 | }).on('complete', function completed() {
90 | logger.info('Benchmark: "%s" is was the fastest.'
91 | , this.filter('fastest').pluck('name')
92 | );
93 | }).run();
94 |
--------------------------------------------------------------------------------
/benchmarks/methods.js:
--------------------------------------------------------------------------------
1 | /**
2 | * Benchmark dependencies
3 | */
4 | var Benchmark = require('benchmark');
5 |
6 | /**
7 | * Different hashring drivers
8 | */
9 | var hashring = require('hashring')
10 | , nodes = {'192.168.0.102:11212': 1, '192.168.0.103:11212': 1, '192.168.0.104:11212': 1};
11 |
12 | /**
13 | * prebuild hashrings
14 | */
15 | var ring = new hashring(nodes)
16 | , md5 = new hashring(nodes, 'md5')
17 | , crc32 = new hashring(nodes, 'crc32');
18 | /**
19 | * Benchmark the constructing and generating of a hashring
20 | */
21 | var constructing = new Benchmark.Suite;
22 | constructing
23 | .add('constructing', function(){
24 | var r = new hashring(nodes);
25 | })
26 | .on('cycle', function(bench){
27 | console.log('Executing benchmark: ' + bench + '\n');
28 | })
29 | .on('complete', function(){
30 | // run the next benchmark if it exists
31 | var next = benchmarks.shift();
32 | if (next && next.run) next.run();
33 | });
34 |
35 | var random = new Benchmark.Suite;
36 | random
37 | .add('random key', function(){
38 | ring.getNode('key' + Math.random())
39 | })
40 | .on('cycle', function(bench){
41 | console.log('Executing benchmark: ' + bench + '\n');
42 | })
43 | .on('complete', function(){
44 | // run the next benchmark if it exists
45 | var next = benchmarks.shift();
46 | if (next && next.run) next.run();
47 | });
48 |
49 | var same = new Benchmark.Suite;
50 | same
51 | .add('same key', function(){
52 | ring.getNode('key')
53 | })
54 | .on('cycle', function(bench){
55 | console.log('Executing benchmark: ' + bench + '\n');
56 | })
57 | .on('complete', function(){
58 | // run the next benchmark if it exists
59 | var next = benchmarks.shift();
60 | if (next && next.run) next.run();
61 | });
62 |
63 | var regenerating = new Benchmark.Suite;
64 | regenerating
65 | .add('generate ring', function(){
66 | ring.generateRing()
67 | })
68 | .on('cycle', function(bench){
69 | console.log('Executing benchmark: ' + bench + '\n');
70 | })
71 | .on('complete', function(){
72 | // run the next benchmark if it exists
73 | var next = benchmarks.shift();
74 | if (next && next.run) next.run();
75 | });
76 |
77 | var algorithmMD5 = new Benchmark.Suite;
78 | algorithmMD5
79 | .add('md5 hashing', function(){
80 | md5.generateKey('key')
81 | })
82 | .on('cycle', function(bench){
83 | console.log('Executing benchmark: ' + bench + '\n');
84 | })
85 | .on('complete', function(){
86 | // run the next benchmark if it exists
87 | var next = benchmarks.shift();
88 | if (next && next.run) next.run();
89 | });
90 |
91 | var algorithmCRC32 = new Benchmark.Suite;
92 | algorithmCRC32
93 | .add('crc32 hashing', function(){
94 | crc32.generateKey('key')
95 | })
96 | .on('cycle', function(bench){
97 | console.log('Executing benchmark: ' + bench + '\n');
98 | })
99 | .on('complete', function(){
100 | // run the next benchmark if it exists
101 | var next = benchmarks.shift();
102 | if (next && next.run) next.run();
103 | });
104 |
105 | var range = new Benchmark.Suite;
106 | range
107 | .add('range', function(){
108 | ring.createRange('key', 2, true);
109 | })
110 | .on('cycle', function(bench){
111 | console.log('Executing benchmark: ' + bench + '\n');
112 | })
113 | .on('complete', function(){
114 | // run the next benchmark if it exists
115 | var next = benchmarks.shift();
116 | if (next && next.run) next.run();
117 | });
118 |
119 |
120 | /**
121 | * Add all benchmarks that that need to be run.
122 | */
123 | var benchmarks = [constructing, random, same, regenerating, algorithmMD5, algorithmCRC32, range];
124 |
125 | // run benchmarks
126 | if (benchmarks.length) benchmarks.shift().run();
127 |
--------------------------------------------------------------------------------
/doc/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 | node-hashring
4 |
5 |
99 |
109 |
110 |
111 | node-hashring | |
| lib/hashring.js |
112 |
113 | Module dependencies
114 |
115 | |
116 |
117 | var CreateHash = require('crypto').createHash
118 | , StringDecoder = require('string_decoder').StringDecoder
119 | , Bisection = require('bisection');
120 | |
121 |
122 |
123 |
124 | Creates a hashring for key => server lookups. It uses crc32 as default algorithm
125 | as it creates nice dense key / server distribution. A other algorithm that could be used
126 | is MD5 or SHA1 . But take in account that the hashing can take allot time when choosing
127 | the algorithm. The JavaScript based crc32 algorithm beats MD5 in performance.
128 |
129 |
130 |
131 | param: mixed arg The server / weights / servers for the hashring param: String [algorithm] The algorithm that should be used to hash the keys. param: Object [options] Extra configuration options for the hash ring. api: public
132 | |
133 |
134 | function hashRing(args, algorithm, options){
135 |
136 | var nodes = []
137 | , weights = {};
138 |
139 | switch (Object.prototype.toString.call(args)){
140 | case '[object String]':
141 | nodes.push(args);
142 | break;
143 | case '[object Object]':
144 | weights = args;
145 | nodes = Object.keys(args)
146 | break;
147 | case '[object Array]':
148 | default:
149 | nodes = args;
150 | break;
151 | }
152 |
153 | this.ring = {};
154 | this.cache = {};
155 | this.sortedKeys = [];
156 |
157 | this.nodes = nodes;
158 | this.weights = weights;
159 | this.algorithm = algorithm || 'crc32';
160 |
161 |
162 | if (this.algorithm === 'crc32'){
163 | this.hashKey = this.crc32HashKey;
164 | }
165 |
166 | this.options = {vnode_count: 40};
167 | if (options && options.vnode_count) {
168 | this.options.vnode_count = options.vnode_count;
169 | }
170 |
171 | this.generateRing();
172 | };
173 |
174 | var HashRing = hashRing.prototype;
175 | |
176 |
177 |
178 |
179 | returns the correct node for the key based on the hashing, or false if it fails to get
180 | the node
181 |
182 |
183 |
184 |
185 | |
186 |
187 | HashRing.getNode = function(key){
188 |
189 | if (this.cache[key]) return this.cache[key];
190 |
191 | var position = this.getNodePosition(key)
192 |
193 |
194 | , node = position === false ? false : this.ring[this.sortedKeys[position]];
195 |
196 | if (!node) return false;
197 | return this.cache[key] = node;
198 | };
199 | |
200 |
201 |
202 |
203 | Returns the position of the key inside the keyring
204 |
205 |
206 |
207 |
208 | |
209 |
210 | HashRing.getNodePosition = function(key){
211 | if (!this.sortedKeys.length){
212 | return false;
213 | }
214 |
215 | var keys = this.generateKey(key)
216 | , nodes = this.sortedKeys
217 | , position = Bisection(nodes, keys);
218 |
219 | return position === nodes.length ? 0 : position;
220 | };
221 | |
222 |
223 |
224 |
225 | Replaces a assigned server of the ring with a new server
226 | hot swapping servers
227 |
228 |
229 |
230 |
231 | |
232 |
233 | HashRing.replaceServer = function(oldServer, newServer){
234 | var self = this;
235 |
236 |
237 | Object.keys(this.ring).forEach(function(key){
238 | if (self.ring[key] === oldServer){
239 | self.ring[key] = newServer;
240 | }
241 | });
242 |
243 |
244 |
245 | Object.keys(this.cache).forEach(function(key){
246 | if (self.cache[key] === oldServer){
247 | self.cache[key] = newServer;
248 | }
249 | });
250 |
251 |
252 | this.nodes.splice(this.nodes.indexOf(oldServer), 1, newServer);
253 | };
254 | |
255 |
256 |
257 |
258 | Adds a server and regenerates the ring
259 |
260 |
263 | |
264 |
265 | HashRing.addServer = function(server, weights){
266 | if (this.nodes.indexOf(server) !== -1) return;
267 |
268 |
269 | if (weights){
270 | for(var key in weights){
271 | this.weights[key] = weights[key];
272 | }
273 | }
274 |
275 | Array.prototype.push.apply(this.nodes,server);
276 | this.sortedKeys.length = 0;
277 |
278 |
279 | this.ring = {};
280 | this.cache = {};
281 | this.generateRing();
282 | };
283 | |
284 |
285 |
286 |
287 | Removes a server from the ring and regenerates the ring.
288 |
289 |
290 |
291 |
292 | |
293 |
294 | HashRing.removeServer = function(server){
295 | var index = this.nodes.indexOf(server);
296 | if (index == -1) return;
297 |
298 | this.nodes.splice(index, 1);
299 | if (this.weights[server]){
300 | delete this.weights[server];
301 | }
302 |
303 | this.ring = {};
304 | this.cache = {};
305 | this.generateRing();
306 | };
307 | |
308 |
309 |
310 |
311 | Iterates over the nodes for a give key, can be used to create redundancy support.
312 |
313 |
314 |
315 | param: String key The key that should get a range param: Number size The maxium amount of nodes to return param: Boolean distinct Remove duplicate nodes returns: Array The nodes api: public
316 | |
317 |
318 | HashRing.createRange = function(key, size, distinct){
319 | if (!Object.keys(this.ring).length) return false;
320 |
321 | distinct = distinct === 'undefined' ? true : distinct;
322 |
323 | var returnvalues = {}
324 | , returnnodes = []
325 | , position = this.getNodePosition(key)
326 | , slices = this.sortedKeys.slice(position)
327 |
328 | , distinctFilter = function(value){
329 | if (!returnvalues[value]){
330 | returnvalues[value] = true;
331 | return value;
332 | }
333 | }
334 | , value
335 | , i = 0
336 | , length = slices.length;
337 |
338 | for(; i < length; i++){
339 | value = distinct ? distinctFilter(this.ring[slices[i]]) : this.ring[slices[i]];
340 | if (value) returnnodes.push(value);
341 | if (size && returnnodes.length >= size) break;
342 | };
343 |
344 |
345 | if(!size || returnnodes.length < size){
346 | for(i = 0, length = this.sortedKeys.length; i < length; i++){
347 | if (i < position){
348 | value = distinct ? distinctFilter(this.ring[this.sortedKeys[i]]) : this.ring[this.sortedKeys[i]];
349 | if (value) returnnodes.push(value);
350 | if(size && returnnodes.length >= size) break;
351 | } else {
352 | break;
353 | }
354 | }
355 | }
356 |
357 |
358 | return returnnodes;
359 |
360 | };
361 | |
362 |
363 |
364 |
365 | Generates a long value of the key that represents a place on the hash ring.
366 |
367 |
368 |
369 |
370 | |
371 |
372 | HashRing.generateKey = function(hash){
373 | return this.hashValue(this.hashKey(hash), function(x){return x});
374 | };
375 | |
376 |
377 |
378 |
379 | Changes the returned key to a long value by using the compare function
380 |
381 |
382 |
383 | param: String key The key that needs to be calculated param: Function compare The calculation function returns: String The hash value api: public
384 | |
385 |
386 | HashRing.hashValue = function(key, compare){
387 | return (
388 | (key[compare(3)] << 24) |
389 | (key[compare(2)] << 16) |
390 | (key[compare(1)] << 8) |
391 | key[compare(0)]
392 | )
393 | };
394 | |
395 |
396 |
397 |
398 | Generates a hash from a key
399 |
400 |
401 |
402 |
403 | |
404 |
405 | HashRing.hashKey = function(key){
406 | return CreateHash(this.algorithm).update(key).digest('hex').split('').map(function(v){ return v.charCodeAt(0) })
407 | };
408 | |
409 |
410 |
411 |
412 | Generates a crc32 value of a string.
413 |
414 |
415 |
416 |
417 | |
418 |
419 | HashRing.crc32HashKey = function(str){
420 | str = new StringDecoder('utf8').write(str);
421 |
422 | var crc = 0 ^ (-1)
423 | , i = 0
424 | , length = str.length
425 | , map = '00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F 63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC 51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E 7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D 806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA 11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F 30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D';
426 |
427 | for(; i < length; i++){
428 | crc = (crc >>> 8) ^ ('0x' + map.substr(((crc ^ str.charCodeAt(i)) & xFF) * 9, 8));
429 | }
430 |
431 | crc = crc ^ (-1);
432 | return (crc < 0 ? crc += 4294967296 : crc).toString().split('').map(function(v){ return v.charCodeAt(0) })
433 | };
434 | |
435 |
436 |
437 |
438 | Library version
439 |
440 | |
441 |
442 | hashRing.version = '0.0.5';
443 |
444 | module.exports = hashRing;
445 | |
446 |
447 |
--------------------------------------------------------------------------------
/index.js:
--------------------------------------------------------------------------------
1 | 'use strict';
2 |
3 | var SimpleCache = require("simple-lru-cache")
4 | , parse = require('connection-parse')
5 | , crypto = require('crypto');
6 |
7 | /**
8 | * Generate the hash of the value.
9 | *
10 | * @api private
11 | */
12 | function hashValueHash(a, b, c, d) {
13 | return ((a << 24) | (b << 16) | (c << 8) | d) >>> 0;
14 | }
15 |
16 | /**
17 | * Add a virtual node parser to the connection string parser.
18 | *
19 | * @param {Object} data server data
20 | * @param {Mixed} value optional value
21 | * @api private
22 | */
23 | parse.extension('vnodes', function vnode(data, value) {
24 | if (typeof value === 'object' && !Array.isArray(value) && 'vnodes' in value) {
25 | data.vnodes = +value.vnodes || 0;
26 | } else {
27 | data.vnodes = 0;
28 | }
29 | });
30 |
31 | /**
32 | * HashRing implements consistent hashing so adding or removing servers of one
33 | * slot does not significantly change the mapping of the key to slots. The
34 | * consistent hashing algorithm is based on ketama or libketama.
35 | *
36 | * @constructor
37 | * @param {Mixed} server Servers that need to be added to the ring.
38 | * @param {Mixed} algorithm Either a Crypto compatible algorithm or custom hasher.
39 | * @param {Object} options Optional configuration and options for the ring.
40 | * @api public
41 | */
42 | function HashRing(servers, algorithm, options) {
43 | options = options || {};
44 |
45 | //
46 | // These properties can be configured
47 | //
48 | this.vnode = 'vnode count' in options ? options['vnode count'] : 40;
49 | this.algorithm = algorithm || 'md5';
50 |
51 | //
52 | // If the default port is set, and a host uses it, then it is excluded from
53 | // the hash.
54 | //
55 | this.defaultport = options['default port'] || null;
56 |
57 | //
58 | // There's a slight difference between libketama and python's hash_ring
59 | // module, libketama creates 160 points per server:
60 | //
61 | // 40 hashes (vnodes) and 4 replicas per hash = 160 points per server.
62 | //
63 | // The hash_ring module only uses 120 points per server:
64 | //
65 | // 40 hashes (vnodes) and 3 replicas per hash = 120 points per server.
66 | //
67 | // And that's the only difference between the original ketama hash and the
68 | // hash_ring package. Small, but important.
69 | //
70 | this.replicas = options.compatibility
71 | ? (options.compatibility === 'hash_ring' ? 3 : 4)
72 | : ('replicas' in options ? +options.replicas : 4);
73 |
74 | //
75 | // Replicas cannot be 0 as it means we have nothing to iterate over when
76 | // creating the initial hash ring.
77 | //
78 | if (this.replicas <= 0) this.replicas = 1;
79 |
80 | // Private properties.
81 | var connections = parse(servers);
82 |
83 | this.ring = [];
84 | this.size = 0;
85 | this.vnodes = connections.vnodes;
86 | this.servers = connections.servers;
87 |
88 | // Set up a cache as we don't want to preform a hashing operation every single
89 | // time we lookup a key.
90 | this.cache = new SimpleCache({
91 | maxSize: 'max cache size' in options ? options['max cache size'] : 5000
92 | });
93 |
94 | // Override the hashing function if people want to use a hashing algorithm
95 | // that is not supported by Node, for example if you want to MurMur hashing or
96 | // something else exotic.
97 | if ('function' === typeof this.algorithm) {
98 | this.hash = this.algorithm;
99 | }
100 |
101 | // Generate the continuum of the HashRing.
102 | this.continuum();
103 | }
104 |
105 | /**
106 | * Generates the continuum of server a.k.a. the Hash Ring based on their weights
107 | * and virtual nodes assigned.
108 | *
109 | * @returns {HashRing}
110 | * @api private
111 | */
112 | HashRing.prototype.continuum = function generate() {
113 | var servers = this.servers
114 | , self = this
115 | , index = 0
116 | , total = 0;
117 |
118 | // No servers, bailout.
119 | if (!servers.length) return this;
120 |
121 | // Generate the total weight of all the servers.
122 | total = servers.reduce(function reduce(total, server) {
123 | return total + server.weight;
124 | }, 0);
125 |
126 | servers.forEach(function each(server) {
127 | var percentage = server.weight / total
128 | , vnodes = self.vnodes[server.string] || self.vnode
129 | , length = Math.floor(percentage * vnodes * servers.length)
130 | , key
131 | , x;
132 |
133 | // If you supply us with a custom vnode size, we will use that instead of
134 | // our computed distribution.
135 | if (vnodes !== self.vnode) length = vnodes;
136 |
137 | for (var i = 0; i < length; i++) {
138 | if (self.defaultport && server.port === self.defaultport) {
139 | x = self.digest(server.host +'-'+ i);
140 | } else {
141 | x = self.digest(server.string +'-'+ i);
142 | }
143 |
144 | for (var j = 0; j < self.replicas; j++) {
145 | key = hashValueHash(x[3 + j * 4], x[2 + j * 4], x[1 + j * 4], x[j * 4]);
146 | self.ring[index] = new Node(key, server.string);
147 | index++;
148 | }
149 | }
150 | });
151 |
152 | // Sort the keys using the continuum points compare that is used in ketama
153 | // hashing.
154 | this.ring = this.ring.sort(function sorted(a, b) {
155 | if (a.value === b.value) return 0;
156 | else if (a.value > b.value) return 1;
157 |
158 | return -1;
159 | });
160 |
161 | this.size = this.ring.length;
162 | return this;
163 | };
164 |
165 | /**
166 | * Find the correct node for which the key is closest to the point after what
167 | * the given key hashes to.
168 | *
169 | * @param {String} key Key whose server we need to figure out.
170 | * @returns {String} Server address.
171 | * @api public
172 | */
173 | HashRing.prototype.get = function get(key) {
174 | var cache = this.cache.get(key);
175 | if (cache) return cache;
176 |
177 | var node = this.ring[this.find(this.hashValue(key))];
178 | if (!node) return undefined;
179 |
180 | this.cache.set(key, node.server);
181 | return node.server;
182 | };
183 |
184 | /**
185 | * Returns the position of the hashValue in the hashring.
186 | *
187 | * @param {Number} hashValue Find the nearest server close to this hash.
188 | * @returns {Number} Position of the server in the hash ring.
189 | * @api public
190 | */
191 | HashRing.prototype.find = function find(hashValue) {
192 | var ring = this.ring
193 | , high = this.size
194 | , low = 0
195 | , middle
196 | , prev
197 | , mid;
198 |
199 | // Perform a search on the array to find the server with the next biggest
200 | // point after what the given key hashes to.
201 | while (true) {
202 | mid = (low + high) >> 1;
203 |
204 | if (mid === this.size) return 0;
205 |
206 | middle = ring[mid].value;
207 | prev = mid === 0 ? 0 : ring[mid - 1].value;
208 |
209 | if (hashValue <= middle && hashValue > prev) return mid;
210 |
211 | if (middle < hashValue) {
212 | low = mid + 1;
213 | } else {
214 | high = mid - 1;
215 | }
216 |
217 | if (low > high) return 0;
218 | }
219 | };
220 |
221 | /**
222 | * Generates a hash of the string.
223 | *
224 | * @param {String} key
225 | * @returns {String|Buffer} Hash, depends on node version.
226 | * @api private
227 | */
228 | HashRing.prototype.hash = function hash(key) {
229 | return crypto.createHash(this.algorithm).update(key).digest();
230 | };
231 |
232 | /**
233 | * Digest hash so we can make a numeric representation from the hash.
234 | *
235 | * @param {String} key The key that needs to be hashed.
236 | * @returns {Array}
237 | * @api private
238 | */
239 | HashRing.prototype.digest = function digest(key) {
240 | var hash = this.hash(key +'');
241 |
242 | // Support for Node 0.10 which returns buffers so we don't need charAt
243 | // lookups.
244 | if ('string' !== typeof hash) return hash;
245 |
246 | return hash.split('').map(function charCode(char) {
247 | return char.charCodeAt(0);
248 | });
249 | };
250 |
251 | /**
252 | * Get the hashed value for the given key.
253 | *
254 | * @param {String} key
255 | * @returns {Number}
256 | * @api private
257 | */
258 | HashRing.prototype.hashValue = function hasher(key) {
259 | var x = this.digest(key);
260 |
261 | return hashValueHash(x[3], x[2], x[1], x[0]);
262 | };
263 |
264 | /**
265 | * None ketama:
266 | *
267 | * The following changes are not ported from the ketama algorithm and are hash
268 | * ring specific. Add, remove or replace servers with as less disruption as
269 | * possible.
270 | */
271 |
272 | /**
273 | * Get a range of different servers.
274 | *
275 | * @param {String} key
276 | * @param {Number} size Amount of servers it should return.
277 | * @param {Boolean} unique Return only unique keys.
278 | * @return {Array}
279 | * @api public
280 | */
281 | HashRing.prototype.range = function range(key, size, unique) {
282 | if (!this.size) return [];
283 |
284 | size = size || this.servers.length;
285 | unique = unique || 'undefined' === typeof unique;
286 |
287 | var position = this.find(this.hashValue(key))
288 | , length = this.ring.length
289 | , servers = []
290 | , node;
291 |
292 | // Start searching for servers from the position of the key to the end of
293 | // HashRing.
294 | for (var i = position; i < length; i++) {
295 | node = this.ring[i];
296 |
297 | // Do we need to make sure that we retrieve a unique list of servers?
298 | if (unique) {
299 | if (!~servers.indexOf(node.server)) servers.push(node.server);
300 | } else {
301 | servers.push(node.server);
302 | }
303 |
304 | if (servers.length === size) return servers;
305 | }
306 |
307 | // Not enough results yet, so iterate from the start of the hash ring to the
308 | // position of the hash ring. So we reach full circle again.
309 | for (i = 0; i < position; i++) {
310 | node = this.ring[i];
311 |
312 | // Do we need to make sure that we retrieve a unique list of servers?
313 | if (unique) {
314 | if (!~servers.indexOf(node.server)) servers.push(node.server);
315 | } else {
316 | servers.push(node.server);
317 | }
318 |
319 | if (servers.length === size) return servers;
320 | }
321 |
322 | return servers;
323 | };
324 |
325 | /**
326 | * Returns the points per server.
327 | *
328 | * @param {String} server Optional server to filter down.
329 | * @returns {Object} server -> Array(points).
330 | * @api public
331 | */
332 | HashRing.prototype.points = function points(servers) {
333 | servers = Array.isArray(servers) ? servers : Object.keys(this.vnodes);
334 |
335 | var nodes = Object.create(null)
336 | , node;
337 |
338 | servers.forEach(function servers(server) {
339 | nodes[server] = [];
340 | });
341 |
342 | for (var i = 0; i < this.size; i++) {
343 | node = this.ring[i];
344 |
345 | if (node.server in nodes) {
346 | nodes[node.server].push(node.value);
347 | }
348 | }
349 |
350 | return nodes;
351 | };
352 |
353 | /**
354 | * Hotswap identical servers with each other. This doesn't require the cache to
355 | * be completely nuked and the hash ring distribution to be re-calculated.
356 | *
357 | * Please note that removing the server and adding a new server could
358 | * potentially create a different distribution.
359 | *
360 | * @param {String} from The server that needs to be replaced.
361 | * @param {String} to The server that replaces the server.
362 | * @returns {HashRing}
363 | * @api public
364 | */
365 | HashRing.prototype.swap = function swap(from, to) {
366 | var connection = parse(to).servers.pop()
367 | , self = this;
368 |
369 | this.ring.forEach(function forEach(node) {
370 | if (node.server === from) node.server = to;
371 | });
372 |
373 | this.cache.forEach(function forEach(value, key) {
374 | if (value === from) self.cache.set(key, to);
375 | }, this);
376 |
377 | // Update the virtual nodes
378 | this.vnodes[to] = this.vnodes[from];
379 | delete this.vnodes[from];
380 |
381 | // Update the servers
382 | this.servers = this.servers.map(function mapswap(server) {
383 | if (server.string === from) {
384 | server.string = to;
385 | server.host = connection.host;
386 | server.port = connection.port;
387 | }
388 |
389 | return server;
390 | });
391 |
392 | return this;
393 | };
394 |
395 | /**
396 | * Add a new server to ring without having to re-initialize the hashring. It
397 | * accepts the same arguments as you can use in the constructor.
398 | *
399 | * @param {Mixed} servers Servers that need to be added to the ring.
400 | * @returns {HashRing}
401 | * @api public
402 | */
403 | HashRing.prototype.add = function add(servers) {
404 | var connections = Object.create(null);
405 |
406 | // Add the current servers to the set.
407 | this.servers.forEach(function forEach(server) {
408 | connections[server.string] = server;
409 | });
410 |
411 | parse(servers).servers.forEach(function forEach(server) {
412 | // Don't add duplicate servers
413 | if (server.string in connections) return;
414 | connections[server.string] = server;
415 | });
416 |
417 | // Now that we generated a complete set of servers, we can update the re-parse
418 | // the set and correctly added all the servers again.
419 | connections = parse(connections);
420 | this.vnodes = connections.vnodes;
421 | this.servers = connections.servers;
422 |
423 | // Rebuild the hash ring.
424 | this.reset();
425 | return this.continuum();
426 | };
427 |
428 | /**
429 | * Remove a server from the hashring.
430 | *
431 | * @param {Mixed} server The sever we want to remove.
432 | * @returns {HashRing}
433 | * @api public
434 | */
435 | HashRing.prototype.remove = function remove(server) {
436 | var connection = parse(server).servers.pop();
437 |
438 | delete this.vnodes[connection.string];
439 | this.servers = this.servers.map(function map(server) {
440 | if (server.string === connection.string) return undefined;
441 |
442 | return server;
443 | }).filter(Boolean);
444 |
445 | // Rebuild the hash ring
446 | this.reset();
447 | return this.continuum();
448 | };
449 |
450 | /**
451 | * Checks if a given server exists in the hash ring.
452 | *
453 | * @param {String} server Server for whose existence we're checking
454 | * @returns {Boolean} Indication if we have that server.
455 | * @api public
456 | */
457 | HashRing.prototype.has = function has(server) {
458 | for (var i = 0; i < this.ring.length; i++) {
459 | if (this.ring[i].server === server) return true;
460 | }
461 |
462 | return false;
463 | };
464 |
465 | /**
466 | * Reset the HashRing to clean up all references
467 | *
468 | * @returns {HashRing}
469 | * @api public
470 | */
471 | HashRing.prototype.reset = function reset() {
472 | this.ring.length = 0;
473 | this.size = 0;
474 | this.cache.reset();
475 |
476 | return this;
477 | };
478 |
479 | /**
480 | * End the hashring and clean up all of its references.
481 | *
482 | * @returns {HashRing}
483 | * @api public
484 | */
485 | HashRing.prototype.end = function end() {
486 | this.reset();
487 |
488 | this.vnodes = {};
489 | this.servers.length = 0;
490 |
491 | return this;
492 | };
493 |
494 | /**
495 | * A single Node in our hash ring.
496 | *
497 | * @constructor
498 | * @param {Number} hashvalue
499 | * @param {String} server
500 | * @api private
501 | */
502 | function Node(hashvalue, server) {
503 | this.value = hashvalue;
504 | this.server = server;
505 | }
506 |
507 | //
508 | // Set up the legacy API aliases. These will be deprecated in the next release.
509 | //
510 | [
511 | { from: 'replaceServer' },
512 | { from: 'replace' },
513 | { from: 'removeServer', to: 'remove' },
514 | { from: 'addServer', to: 'add' },
515 | { from: 'getNode', to: 'get' },
516 | { from: 'getNodePosition', to: 'find' },
517 | { from: 'position', to: 'find' }
518 | ].forEach(function depricate(api) {
519 | var notified = false;
520 |
521 | HashRing.prototype[api.from] = function depricating() {
522 | if (!notified) {
523 | console.warn();
524 | console.warn('[depricated] HashRing#'+ api.from +' is removed.');
525 |
526 | // Not every API has a replacement API that should be used
527 | if (api.to) {
528 | console.warn('[depricated] use HashRing#'+ api.to +' as replacement.');
529 | } else {
530 | console.warn('[depricated] the API has no replacement');
531 | }
532 |
533 | console.warn();
534 | notified = true;
535 | }
536 |
537 | if (api.to) return HashRing.prototype[api.to].apply(this, arguments);
538 | };
539 | });
540 |
541 | /**
542 | * Expose the current version number.
543 | *
544 | * @type {String}
545 | * @public
546 | */
547 | HashRing.version = require('./package.json').version;
548 |
549 | /**
550 | * Expose the module.
551 | *
552 | * @api public
553 | */
554 | module.exports = HashRing;
555 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "hashring",
3 | "version": "3.2.0",
4 | "author": "Arnout Kazemier",
5 | "description": "A consistent hashring compatible with ketama hashing and python's hash_ring",
6 | "main": "./index.js",
7 | "keywords": [
8 | "hashring",
9 | "hash ring",
10 | "hashing",
11 | "hashvalue",
12 | "ketama",
13 | "hash_ring",
14 | "hash",
15 | "consistent hashing",
16 | "libketama"
17 | ],
18 | "license": "MIT",
19 | "bugs": "http://github.com/3rd-Eden/node-hashring/issues",
20 | "homepage": "http://github.com/3rd-Eden/node-hashring/",
21 | "repository": {
22 | "type": "git",
23 | "url": "http://github.com/3rd-Eden/node-hashring.git"
24 | },
25 | "dependencies": {
26 | "connection-parse": "0.0.x",
27 | "simple-lru-cache": "0.0.x"
28 | },
29 | "devDependencies": {
30 | "assume": "1.2.x",
31 | "benchmark": "1.0.x",
32 | "devnull": "0.0.x",
33 | "istanbul": "0.3.x",
34 | "mocha": "2.2.x",
35 | "pre-commit": "1.1.x"
36 | },
37 | "scripts": {
38 | "100%": "istanbul check-coverage --statements 100 --functions 100 --lines 100 --branches 100",
39 | "test": "mocha tests/api.test.js tests/distribution.test.js",
40 | "watch": "mocha --watch tests/api.test.js tests/distribution.test.js",
41 | "coverage": "istanbul cover ./node_modules/.bin/_mocha -- tests/api.test.js tests/distribution.test.js",
42 | "test-travis": "istanbul cover node_modules/.bin/_mocha --report lcovonly -- tests/api.test.js tests/distribution.test.js"
43 | }
44 | }
45 |
--------------------------------------------------------------------------------
/tests/api.test.js:
--------------------------------------------------------------------------------
1 | describe('HashRing', function () {
2 | "use strict";
3 |
4 | var Hashring = require('../')
5 | , assume = require('assume');
6 |
7 | it('exposes the library version', function () {
8 | assume(Hashring.version).matches(/^\d+\.\d+\.\d+$/);
9 | });
10 |
11 | it('libmemcached compatiblity', function () {
12 | var ring = new Hashring({
13 | '10.0.1.1:11211': 600,
14 | '10.0.1.2:11211': 300,
15 | '10.0.1.3:11211': 200,
16 | '10.0.1.4:11211': 350,
17 | '10.0.1.5:11211': 1000,
18 | '10.0.1.6:11211': 800,
19 | '10.0.1.7:11211': 950,
20 | '10.0.1.8:11211': 100
21 | });
22 |
23 | // VDEAAAAA hashes to fffcd1b5, after the last continuum point, and lets us
24 | // test the boundary wraparound.
25 | assume(ring.find(ring.hashValue('VDEAAAAA'))).equals(0);
26 | });
27 |
28 | describe('API', function () {
29 | it('constructs with a string', function () {
30 | var ring = new Hashring('192.168.0.102:11212');
31 |
32 | assume(ring.servers).has.length(1);
33 | assume(ring.ring.length).is.above(1);
34 | });
35 |
36 | it('constructs with a array', function () {
37 | var ring = new Hashring([
38 | '192.168.0.102:11212'
39 | , '192.168.0.103:11212'
40 | , '192.168.0.104:11212'
41 | ]);
42 |
43 | assume(ring.servers).has.length(3);
44 | assume(ring.ring.length).is.above(1);
45 | });
46 |
47 | it('constructs with a object', function () {
48 | var ring = new Hashring({
49 | '192.168.0.102:11212': 2
50 | , '192.168.0.103:11212': 2
51 | , '192.168.0.104:11212': 2
52 | });
53 |
54 | assume(ring.servers).has.length(3);
55 | assume(ring.ring.length).is.above(1);
56 |
57 | ring.servers.forEach(function (server) {
58 | assume(server.weight).is.above(1);
59 | });
60 | });
61 |
62 | it('constructs with a object with per node vnodes', function () {
63 | var ring = new Hashring({
64 | '192.168.0.102:11212': { 'vnodes': 40 }
65 | , '192.168.0.103:11212': { 'vnodes': 50 }
66 | , '192.168.0.104:11212': { 'vnodes': 5 }
67 | });
68 |
69 | assume(ring.servers).is.length(3);
70 | assume(ring.ring.length).equals((40 + 50 + 5) * ring.replicas);
71 |
72 | ring = new Hashring({
73 | '192.168.0.102:11212': { 'vnodes': 4 }
74 | , '192.168.0.103:11212': { 'vnodes': 3 }
75 | , '192.168.0.104:11212': { 'vnodes': 5 }
76 | });
77 |
78 | assume(ring.servers).has.length(3);
79 | assume(ring.ring.length).equals((4 + 3 + 5) * ring.replicas);
80 | });
81 |
82 | it('constructs with a default vnode value', function () {
83 | var ring = new Hashring([
84 | '192.168.0.102:11212'
85 | , '192.168.0.103:11212'
86 | ], 'md5', { 'vnode count': 60 });
87 |
88 | assume(ring.ring.length).equals(60 * ring.replicas * ring.servers.length);
89 | });
90 |
91 | it('constructs with no arguments', function () {
92 | var ring = new Hashring();
93 |
94 | assume(ring.servers).has.length(0);
95 | assume(ring.ring).has.length(0);
96 | });
97 |
98 | it('normalizes the replicas to 1', function () {
99 | var ring = new Hashring([
100 | '192.168.0.102:11212'
101 | , '192.168.0.103:11212'
102 | ], 'md5', { 'replicas': 0 });
103 |
104 | assume(ring.replicas).equals(1);
105 | });
106 |
107 | it('accepts different algorithms', function () {
108 | var ring = new Hashring('192.168.0.102:11212', 'sha1');
109 |
110 | assume(ring.servers).has.length(1);
111 | assume(ring.algorithm).equals('sha1');
112 | assume(ring.ring.length).is.above(1);
113 | });
114 |
115 | it('generates the correct amount of points', function () {
116 | var ring = new Hashring({
117 | '127.0.0.1:11211': 600
118 | , '127.0.0.1:11212': 400
119 | });
120 |
121 | assume(ring.ring.length).equals(160 * 2);
122 | });
123 |
124 | describe("#add", function () {
125 | it('adds server after zero-argument constructor', function () {
126 | var ring = new Hashring();
127 | ring.add('192.168.0.102:11212');
128 |
129 | assume(ring.servers).has.length(1);
130 | assume(ring.ring.length).is.above(1);
131 | });
132 | });
133 |
134 | describe('#get', function () {
135 | it('looks up keys', function () {
136 | var ring = new Hashring([
137 | '192.168.0.102:11212'
138 | , '192.168.0.103:11212'
139 | , '192.168.0.104:11212'
140 | ]);
141 |
142 | assume(ring.find(ring.hashValue('foo'))).is.above(-1);
143 |
144 | // NOTE we are going to do some flaky testing ;P
145 | assume(ring.get('foo')).equals('192.168.0.102:11212');
146 | assume(ring.get('pewpew')).equals('192.168.0.103:11212');
147 |
148 | // we are not gonna verify the results we are just gonna test if we don't
149 | // fuck something up in the code, so it throws errors or whatever
150 |
151 | // unicode keys, just because people roll like that
152 | assume(ring.find(ring.hashValue('привет мир, Memcached и nodejs для победы'))).is.above(-1);
153 |
154 | // other odd keys
155 | assume(ring.find(ring.hashValue(1))).is.above(-1);
156 | assume(ring.find(ring.hashValue(0))).is.above(-1);
157 | assume(ring.find(ring.hashValue([]))).is.above(-1);
158 | assume(ring.find(ring.hashValue({wtf:'lol'}))).is.above(-1);
159 |
160 | // this should work as both objects are converted to [object Object] by
161 | // the .toString() constructor
162 | assume(ring.get({wtf:'lol'})).equals(ring.get({wtf:'amazing .toStringing'}));
163 | });
164 | });
165 |
166 | describe('#hashValue', function () {
167 | it('should create the correct long value for a given key', function () {
168 | var ring = new Hashring({
169 | '127.0.0.1:11211': 600
170 | , '127.0.0.1:11212': 400
171 | });
172 |
173 | assume(ring.hashValue('test')).equals(3446378249);
174 | });
175 | });
176 |
177 | describe('#find', function () {
178 | it('should find the correct long value for a given key', function () {
179 | var ring = new Hashring({
180 | '127.0.0.1:11211': 600
181 | , '127.0.0.1:11212': 400
182 | });
183 |
184 | assume(ring.ring[ring.find(ring.hashValue('test'))].value).equals(3454255383);
185 | });
186 | });
187 |
188 | describe('#swap', function () {
189 | it('swaps servers', function () {
190 | var ring = new Hashring([
191 | '192.168.0.102:11212'
192 | , '192.168.0.103:11212'
193 | , '192.168.0.104:11212'
194 | ])
195 | , amazon = ring.get('justdied')
196 | , skynet = '192.168.0.128:11212';
197 |
198 | ring.swap(amazon, skynet);
199 | assume(ring.cache.get('justdied')).equals(skynet);
200 |
201 | // After a cleared cache, it should still resolve to the same server
202 | ring.cache.reset();
203 | assume(ring.get('justdied')).equals(skynet);
204 | });
205 | });
206 |
207 | describe('#remove', function () {
208 | it('removes servers', function () {
209 | var ring = new Hashring([
210 | '192.168.0.102:11212'
211 | , '192.168.0.103:11212'
212 | , '192.168.0.104:11212'
213 | ]);
214 |
215 | ring.remove('192.168.0.102:11212');
216 | ring.ring.forEach(function (node) {
217 | assume(node.server).does.not.equal('192.168.0.102:11212');
218 | });
219 | });
220 |
221 | it('Removes the last server', function () {
222 | var ring = new Hashring('192.168.0.102:11212');
223 | ring.remove('192.168.0.102:11212');
224 |
225 | assume(ring.servers).has.length(0);
226 | assume(ring.ring).has.length(0);
227 | });
228 | });
229 |
230 | describe('#has', function () {
231 | it('has a server', function () {
232 | var ring = new Hashring([
233 | '192.168.0.102:11212'
234 | , '192.168.0.103:11212'
235 | , '192.168.0.104:11212'
236 | ]);
237 |
238 | assume(ring.has('192.168.0.102:11212')).is.true();
239 | });
240 |
241 | it('does not have a server', function () {
242 | var ring = new Hashring([
243 | '192.168.0.102:11212'
244 | , '192.168.0.103:11212'
245 | , '192.168.0.104:11212'
246 | ]);
247 |
248 | assume(ring.has('192.168.0.105:11212')).is.false();
249 | });
250 | });
251 |
252 | describe('#range', function () {
253 | it('returns 20 servers', function () {
254 | var ring = new Hashring([
255 | '192.168.0.102:11212'
256 | , '192.168.0.103:11212'
257 | , '192.168.0.104:11212'
258 | ]);
259 |
260 | assume(ring.range('foo', 20, false)).is.length(20);
261 | });
262 |
263 | it('returns 3 servers as we only want unique servers', function () {
264 | var ring = new Hashring([
265 | '192.168.0.102:11212'
266 | , '192.168.0.103:11212'
267 | , '192.168.0.104:11212'
268 | ]);
269 |
270 | assume(ring.range('foo', 20, false)).is.length(20);
271 | });
272 | });
273 | });
274 | });
275 |
--------------------------------------------------------------------------------
/tests/distribution.test.js:
--------------------------------------------------------------------------------
1 | describe('Hashring distributions', function () {
2 | "use strict";
3 |
4 | var Hashring = require('../')
5 | , assume = require('assume');
6 |
7 | this.timeout(60000);
8 |
9 | it('hashes to the exact same output as hash_ring for python', function () {
10 | var fixture = require('fs').readFileSync(__dirname +'/fixtures/hash_ring.txt')
11 | .toString().split('\n');
12 |
13 | var ring = new Hashring({
14 | '0.0.0.1' : 1,
15 | '0.0.0.2' : 2,
16 | '0.0.0.3' : 3,
17 | '0.0.0.4' : 4,
18 | '0.0.0.5' : 5
19 | }, 'md5', { compatibility: 'hash_ring' });
20 |
21 | for (var i=0; i < 100000; i++){
22 | assume(i + ' ' + ring.get(i)).equals(fixture[i]);
23 | }
24 | });
25 |
26 | it('hashes to the exact same output as ketama for python', function () {
27 | var fixture = require('fs').readFileSync(__dirname +'/fixtures/ketama.txt')
28 | .toString().split('\n');
29 |
30 | var ring = new Hashring({
31 | '0.0.0.1' : 1,
32 | '0.0.0.2' : 2,
33 | '0.0.0.3' : 3,
34 | '0.0.0.4' : 4,
35 | '0.0.0.5' : 5
36 | }, 'md5');
37 |
38 | for (var i=0; i < 100000; i++){
39 | assume(i + ' ' + ring.get(i)).equals(fixture[i]);
40 | }
41 | });
42 |
43 | it('hashes to the exact same output as pylibmc', function () {
44 | var fixture = require('fs').readFileSync(__dirname +'/fixtures/pylibmc.txt')
45 | .toString().split('\n');
46 |
47 | var ring = new Hashring([
48 | '0.0.0.1:11211',
49 | '0.0.0.2:11211',
50 | '0.0.0.3',
51 | '0.0.0.4:11213',
52 | '0.0.0.5:11212'
53 | ], 'md5', { 'default port': 11211 });
54 |
55 | for (var i=0; i < 100000; i++){
56 | assume(i + ' ' + ring.get(i)).equals(fixture[i]);
57 | }
58 | });
59 |
60 | it('has an even distribution', function () {
61 | var iterations = 100000
62 | , nodes = {
63 | '192.168.0.102:11212': 1
64 | , '192.168.0.103:11212': 1
65 | , '192.168.0.104:11212': 1
66 | }
67 | , ring = new Hashring(nodes);
68 |
69 | function genCode (length) {
70 | length = length || 10;
71 | var chars = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm1234567890"
72 | , numChars = chars.length
73 | , ret = ""
74 | , i = 0;
75 |
76 | for (; i < length; i++) {
77 | ret += chars[parseInt(Math.random() * numChars, 10)];
78 | }
79 |
80 | return ret;
81 | }
82 |
83 | var counts = {}
84 | , node
85 | , i
86 | , len
87 | , word;
88 |
89 | for (i = 0, len = nodes.length; i < len; i++) {
90 | node = nodes[i];
91 | counts[node] = 0;
92 | }
93 |
94 | for (i = 0, len = iterations; i < len; i++) {
95 | word = genCode(10);
96 | node = ring.get(word);
97 | counts[node] = counts[node] || 0;
98 | counts[node]++;
99 | }
100 |
101 | var total = Object.keys(counts).reduce(function reduce (sum, node) {
102 | return sum += counts[node];
103 | }, 0.0);
104 |
105 | var delta = 0.05
106 | , lower = 1.0 / 3 - 0.05
107 | , upper = 1.0 / 3 + 0.05;
108 |
109 | for (node in counts) {
110 | assume(counts[node] / total).is.within(lower, upper);
111 | }
112 | });
113 | });
114 |
--------------------------------------------------------------------------------
/tests/fixtures/hash_ring.py:
--------------------------------------------------------------------------------
1 | from hash_ring import HashRing
2 | import unittest
3 |
4 | class HashRingTest(unittest.TestCase):
5 |
6 | def setUp(self):
7 | self.weights = {
8 | '0.0.0.1': 1,
9 | '0.0.0.2': 2,
10 | '0.0.0.3': 3,
11 | '0.0.0.4': 4,
12 | '0.0.0.5': 5
13 | }
14 |
15 | def test_points(self):
16 | ring = HashRing(self.weights.keys(), self.weights)
17 |
18 | for i in xrange(0, 100000):
19 | print i, ring.get_node(str(i))
20 |
21 | if __name__ == "__main__":
22 | unittest.main()
23 |
--------------------------------------------------------------------------------
/tests/fixtures/ketama.py:
--------------------------------------------------------------------------------
1 | import ketama
2 | import unittest
3 | import os
4 |
5 | class KetamaTest(unittest.TestCase):
6 | def setUp(self):
7 | self.server_list_file = os.tmpnam()
8 | self.server_list = file(self.server_list_file, "w")
9 | self.server_list.write("0.0.0.1\t1\n")
10 | self.server_list.write("0.0.0.2\t2\n")
11 | self.server_list.write("0.0.0.3\t3\n")
12 | self.server_list.write("0.0.0.4\t4\n")
13 | self.server_list.write("0.0.0.5\t5\n")
14 | self.server_list.flush()
15 |
16 | def tearDown(self):
17 | self.server_list.close()
18 | os.unlink(self.server_list_file)
19 |
20 | def test_points(self):
21 | ring = ketama.Continuum(self.server_list_file)
22 |
23 | for i in xrange(0, 100000):
24 | print i, ring.get_server(str(i))[1]
25 |
26 | if __name__ == "__main__":
27 | unittest.main()
28 |
--------------------------------------------------------------------------------
/tests/fixtures/libmemcached.json:
--------------------------------------------------------------------------------
1 | [
2 | [ "SVa_]_V41)", 443691461, 445379617, "10.0.1.7" ],
3 | [ "*/Z;?V(.\\8", 1422915503, 1428303028, "10.0.1.1" ],
4 | [ "30C1*Z*S/_", 1473165754, 1480075959, "10.0.1.2" ],
5 | [ "ERR:EC58G>", 2148406511, 2168579133, "10.0.1.7" ],
6 | [ "1I=cTMNTKF", 2882686667, 2885206587, "10.0.1.5" ],
7 | [ "]VG<`I*Z8)", 1103544263, 1104827657, "10.0.1.5" ],
8 | [ "UUTC`-V159", 3716288206, 3727224240, "10.0.1.5" ],
9 | [ "@7RU6C6T+Z", 3862737685, 3871917949, "10.0.1.5" ],
10 | [ "/XLN0@+36;", 1623269830, 1627683651, "10.0.1.1" ],
11 | [ "4(`X;\\V.^c", 373546328, 383925769, "10.0.1.1" ],
12 | [ "726bW=9*a4", 4213440020, 4213950705, "10.0.1.7" ],
13 | [ "\\`)32", 536016577, 539988520, "10.0.1.7" ],
27 | [ "U))Fb-(`,.", 4128682289, 4136854163, "10.0.1.7" ],
28 | [ "R-08RNTaRT", 3718170086, 3727224240, "10.0.1.5" ],
29 | [ "(LHcO203I3", 1007779411, 1014643570, "10.0.1.5" ],
30 | [ "=256P+;Qc8", 3976201210, 3976304873, "10.0.1.5" ],
31 | [ "OI5XZ_BBT(", 2155922164, 2168579133, "10.0.1.7" ],
32 | [ "2TLRL/UL;:", 1086800909, 1095659802, "10.0.1.7" ],
33 | [ "WHD\\O1`ZRW", 3087923411, 3095471560, "10.0.1.5" ],
34 | [ ".=54)_c;=T", 2497691631, 2502731301, "10.0.1.1" ],
35 | [ ";GE`)FT\\4", 580747448, 581063326, "10.0.1.2" ],
37 | [ "HZAU*;P*N]", 2564670474, 2565697267, "10.0.1.7" ],
38 | [ "NZ@ZE=O84_", 533335275, 539988520, "10.0.1.7" ],
39 | [ "6,cEI`F_P>", 3972869246, 3974773167, "10.0.1.6" ],
40 | [ "c,5AQ/T5)6", 2835605783, 2847870057, "10.0.1.8" ],
41 | [ ".O,>>BT)RX", 3857978174, 3871917949, "10.0.1.5" ],
42 | [ "XY\\X::LX50", 1749241099, 1752196488, "10.0.1.6" ],
43 | [ "+550F^/.01", 3781824099, 3783248219, "10.0.1.6" ],
44 | [ "<.X9E2S5+9", 3232479481, 3234387706, "10.0.1.7" ],
45 | [ "]\\.UH8_0a1", 2419699252, 2423002920, "10.0.1.4" ],
46 | [ "8(6=(T0/Z0", 728266737, 729026070, "10.0.1.7" ],
47 | [ "8*6a;Sc*X+", 4223431086, 4230156966, "10.0.1.2" ],
48 | [ "J5/", 2776949824, 2784182515, "10.0.1.7" ],
63 | [ "[>RZHG97Q9", 71954686, 72034069, "10.0.1.6" ],
64 | [ "J3/G[)9<^Z", 2799896459, 2805183696, "10.0.1.7" ],
65 | [ "N-)88>[O`,", 50404102, 51792557, "10.0.1.5" ],
66 | [ "NP:=FR\\OaA", 3837333776, 3837792034, "10.0.1.7" ],
67 | [ "`@L+W;a,O[", 1512157148, 1522285852, "10.0.1.6" ],
68 | [ "W2`P:-+1T[", 2945171975, 2946196424, "10.0.1.5" ],
69 | [ "-6G7K^YDIN", 3168617340, 3170513015, "10.0.1.7" ],
70 | [ "U>*>9ZI6V5", 668514946, 674097631, "10.0.1.6" ],
71 | [ ".I?^6Ic9RK", 938419020, 942832691, "10.0.1.6" ],
72 | [ "0OZH^9BKM[", 3682518606, 3686781297, "10.0.1.8" ],
73 | [ "5?50UGZ:ML", 868610882, 869425986, "10.0.1.5" ],
74 | [ "?K2NF@3=IU", 381218851, 383925769, "10.0.1.1" ],
75 | [ "YI@G-2X?UB", 3688706179, 3693197681, "10.0.1.5" ],
76 | [ "7cY", 2590140172, 2598117636, "10.0.1.7" ],
90 | [ "\\[a\\^=V_M0", 689410119, 698690782, "10.0.1.6" ],
91 | [ "7;RM+8J9YC", 1530175299, 1531107082, "10.0.1.7" ],
92 | [ "4*=.SPR[AV", 3928582722, 3928853792, "10.0.1.1" ],
93 | [ "-2F+^88P4U", 3023552752, 3025823613, "10.0.1.7" ],
94 | [ "X;-F`(N?9D", 570465234, 572485994, "10.0.1.7" ],
95 | [ "R=F_D-K2a]", 1287750228, 1290935562, "10.0.1.7" ],
96 | [ "X*+2aaC.EG", 3200948713, 3201088518, "10.0.1.5" ],
97 | [ "[1ZXONX2]a", 4108881567, 4109865744, "10.0.1.4" ],
98 | [ "FL;\\GWacaV", 458449508, 467374054, "10.0.1.4" ],
99 | [ "\\MQ_XNT7L-", 1259349383, 1259509450, "10.0.1.7" ],
100 | [ "VD6D0]ba_\\", 3842502950, 3842588691, "10.0.1.1" ]
101 | ]
102 |
--------------------------------------------------------------------------------
/tests/fixtures/test_pylibmc.py:
--------------------------------------------------------------------------------
1 | import pylibmc
2 | import unittest
3 |
4 | class PylibmcTest(unittest.TestCase):
5 | def setUp(self):
6 | self.servers = (
7 | '0.0.0.1:11211',
8 | '0.0.0.2:11211',
9 | '0.0.0.3',
10 | '0.0.0.4:11213',
11 | '0.0.0.5:11212',
12 | )
13 | self.mc = pylibmc.Client(self.servers, behaviors={"ketama_weighted": True})
14 |
15 | def test_points(self):
16 | for i in xrange(0, 100000):
17 | server_index = self.mc.hash(str(i))
18 | print i, self.servers[server_index]
19 |
20 | if __name__ == "__main__":
21 | unittest.main()
22 |
--------------------------------------------------------------------------------