-
Notifications
You must be signed in to change notification settings - Fork 122
/
Copy pathConsistentHash.js
139 lines (106 loc) · 2.93 KB
/
ConsistentHash.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/**
* 一致性哈希
*
* http://www.cnblogs.com/gaochundong/p/consistent_hashing.html#top
*/
import crypto from 'crypto';
export default class ConsistentHash {
replicas = 160;
algorithm = 'md5';
ring = {};
keys = [];
constructor(nodes, opts = {}){
if(opts.replicas) {
this.replicas = opts.replicas;
}
if(opts.algorithm){
this.algorithm = opts.algorithm;
}
for(let node of nodes){
this.addNode(node);
}
}
addNode(node){
for(let i = 0; i < this.replicas; ++i){
let id = node;
if(typeof node === 'object' && node.id) id = node.id;
let key = this.crypto(`${ id }:${ i }`);
this.ring[key] = node;
}
this.keys = Object.keys(this.ring).sort();
}
removeNode(node){
for(let i = 0; i < this.replicas; ++i){
let id = node;
if(typeof node === 'object' && node.id) id = node.id;
let key = this.crpto(`${ id }:${ i }`);
if(!this.ring.hasOwnProperty(key)) return null;
delete this.ring[key];
}
this.keys = Object.keys(this.ring).sort();
}
// todo replace this with jump consistent hash
crypto(str){
return crypto.createHash(this.algorithm).update(str).digest('hex');
}
get length(){
return Object.keys(this.ring).length;
}
getNode(key){
if(!this.length) return 0;
let hash = this.crypto(key + '');
let pos = this._getClockwiseNearestNode(hash);
return this.ring[this.keys[pos]];
}
_getClockwiseNearestNode(hash){
let high = this.length - 1;
let low = 0;
let mid = -1;
if(high === 0) return 0;
while(low <= high){
mid = (low + high) >> 1;
let compare = this.compare(this.keys[mid], hash);
if(compare === 0) return mid;
else if(compare < 0) low = mid + 1;
else high = mid - 1;
}
if(high < 0) high = this.length - 1;
return high;
}
compare(a, b){
return a > b ? 1 : a < b ? -1 : 0;
}
}
let servers = [];
for(let i = 0 ; i < 5; ++i){
servers.push({
id: i
});
}
let consistentHash = new ConsistentHash(servers, {
replicas: 200,
algorithm: 'md5'
});
let buckets = [];
for(let i = 0; i < 10; ++i){
let id = consistentHash.getNode(i).id;
buckets.push(id);
console.log(`item[${ i }] is in node[${ buckets[i] }]`);
}
var cons = new ConsistentHash(["node1", "node2", "node3"]);
var nodes = {};
var chars = [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
];
chars.forEach(function(c) {
var node = cons.getNode(c);
if (nodes[node]) {
nodes[node].push(c);
} else {
nodes[node] = [];
nodes[node].push(c);
}
});
console.log(nodes);