/
test_hashing.py
200 lines (174 loc) · 8.7 KB
/
test_hashing.py
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import unittest
from carbon.hashing import ConsistentHashRing
class HashIntegrityTest(unittest.TestCase):
def test_2_node_positional_integrity(self):
"""Make a cluster, verify we don't have positional collisions"""
ring = ConsistentHashRing([])
for n in range(2):
ring.add_node(("192.168.10.%s" % str(10 + n), "%s" % str(10 + n)))
self.assertEqual(
len([n[0] for n in ring.ring]),
len(set([n[0] for n in ring.ring])))
def test_3_node_positional_integrity(self):
"""Make a cluster, verify we don't have positional collisions"""
ring = ConsistentHashRing([])
for n in range(3):
ring.add_node(("192.168.10.%s" % str(10 + n), "%s" % str(10 + n)))
self.assertEqual(
len([n[0] for n in ring.ring]),
len(set([n[0] for n in ring.ring])))
def test_4_node_positional_integrity(self):
"""Make a cluster, verify we don't have positional collisions"""
ring = ConsistentHashRing([])
for n in range(4):
ring.add_node(("192.168.10.%s" % str(10 + n), "%s" % str(10 + n)))
self.assertEqual(
len([n[0] for n in ring.ring]),
len(set([n[0] for n in ring.ring])))
def test_5_node_positional_integrity(self):
"""Make a cluster, verify we don't have positional collisions"""
ring = ConsistentHashRing([])
for n in range(5):
ring.add_node(("192.168.10.%s" % str(10 + n), "%s" % str(10 + n)))
self.assertEqual(
len([n[0] for n in ring.ring]),
len(set([n[0] for n in ring.ring])))
def test_6_node_positional_integrity(self):
"""Make a cluster, verify we don't have positional collisions"""
ring = ConsistentHashRing([])
for n in range(6):
ring.add_node(("192.168.10.%s" % str(10 + n), "%s" % str(10 + n)))
self.assertEqual(
len([n[0] for n in ring.ring]),
len(set([n[0] for n in ring.ring])))
def test_7_node_positional_integrity(self):
"""Make a cluster, verify we don't have positional collisions"""
ring = ConsistentHashRing([])
for n in range(7):
ring.add_node(("192.168.10.%s" % str(10 + n), "%s" % str(10 + n)))
self.assertEqual(
len([n[0] for n in ring.ring]),
len(set([n[0] for n in ring.ring])))
def test_8_node_positional_integrity(self):
"""Make a cluster, verify we don't have positional collisions"""
ring = ConsistentHashRing([])
for n in range(8):
ring.add_node(("192.168.10.%s" % str(10 + n), "%s" % str(10 + n)))
self.assertEqual(
len([n[0] for n in ring.ring]),
len(set([n[0] for n in ring.ring])))
def test_9_node_positional_integrity(self):
"""Make a cluster, verify we don't have positional collisions"""
ring = ConsistentHashRing([])
for n in range(9):
ring.add_node(("192.168.10.%s" % str(10 + n), "%s" % str(10 + n)))
self.assertEqual(
len([n[0] for n in ring.ring]),
len(set([n[0] for n in ring.ring])))
def test_10_get_node(self):
"""Trigger bisect on identical first key, see: issues/766"""
ring = ConsistentHashRing([], replica_count=1)
ring.add_node(("1", "1"))
n = ring.get_node("('1', '1'):0")
self.assertEqual(('1', '1'), n)
def test_11_get_nodes(self):
"""Trigger bisect on identical first key, see: issues/766"""
ring = ConsistentHashRing([], replica_count=1)
ring.add_node(("1", "1"))
n = ring.get_nodes("('1', '1'):0")
self.assertEqual([('1', '1')], list(n))
class FNVHashIntegrityTest(unittest.TestCase):
def test_2_node_positional_integrity(self):
"""Make a cluster, verify we don't have positional collisions"""
ring = ConsistentHashRing([], hash_type='fnv1a_ch')
for n in range(2):
ring.add_node(("192.168.10.%s" % str(10 + n), "%s" % str(10 + n)))
self.assertEqual(
len([n[0] for n in ring.ring]),
len(set([n[0] for n in ring.ring])))
def test_3_node_positional_integrity(self):
"""Make a cluster, verify we don't have positional collisions"""
ring = ConsistentHashRing([], hash_type='fnv1a_ch')
for n in range(3):
ring.add_node(("192.168.10.%s" % str(10 + n), "%s" % str(10 + n)))
self.assertEqual(
len([n[0] for n in ring.ring]),
len(set([n[0] for n in ring.ring])))
def test_4_node_positional_integrity(self):
"""Make a cluster, verify we don't have positional collisions"""
ring = ConsistentHashRing([], hash_type='fnv1a_ch')
for n in range(4):
ring.add_node(("192.168.10.%s" % str(10 + n), "%s" % str(10 + n)))
self.assertEqual(
len([n[0] for n in ring.ring]),
len(set([n[0] for n in ring.ring])))
def test_5_node_positional_integrity(self):
"""Make a cluster, verify we don't have positional collisions"""
ring = ConsistentHashRing([], hash_type='fnv1a_ch')
for n in range(5):
ring.add_node(("192.168.10.%s" % str(10 + n), "%s" % str(10 + n)))
self.assertEqual(
len([n[0] for n in ring.ring]),
len(set([n[0] for n in ring.ring])))
def test_6_node_positional_integrity(self):
"""Make a cluster, verify we don't have positional collisions"""
ring = ConsistentHashRing([], hash_type='fnv1a_ch')
for n in range(6):
ring.add_node(("192.168.10.%s" % str(10 + n), "%s" % str(10 + n)))
self.assertEqual(
len([n[0] for n in ring.ring]),
len(set([n[0] for n in ring.ring])))
def test_7_node_positional_integrity(self):
"""Make a cluster, verify we don't have positional collisions"""
ring = ConsistentHashRing([], hash_type='fnv1a_ch')
for n in range(7):
ring.add_node(("192.168.10.%s" % str(10 + n), "%s" % str(10 + n)))
self.assertEqual(
len([n[0] for n in ring.ring]),
len(set([n[0] for n in ring.ring])))
def test_8_node_positional_integrity(self):
"""Make a cluster, verify we don't have positional collisions"""
ring = ConsistentHashRing([], hash_type='fnv1a_ch')
for n in range(8):
ring.add_node(("192.168.10.%s" % str(10 + n), "%s" % str(10 + n)))
self.assertEqual(
len([n[0] for n in ring.ring]),
len(set([n[0] for n in ring.ring])))
def test_9_node_positional_integrity(self):
"""Make a cluster, verify we don't have positional collisions"""
ring = ConsistentHashRing([], hash_type='fnv1a_ch')
for n in range(9):
ring.add_node(("192.168.10.%s" % str(10 + n), "%s" % str(10 + n)))
self.assertEqual(
len([n[0] for n in ring.ring]),
len(set([n[0] for n in ring.ring])))
class ConsistentHashRingTestFNV1A(unittest.TestCase):
def test_chr_compute_ring_position_fnv1a(self):
hosts = [("127.0.0.1", "ba603c36342304ed77953f84ac4d357b"),
("127.0.0.2", "5dd63865534f84899c6e5594dba6749a"),
("127.0.0.3", "866a18b81f2dc4649517a1df13e26f28")]
hashring = ConsistentHashRing(hosts, hash_type='fnv1a_ch')
self.assertEqual(hashring.compute_ring_position('hosts.worker1.cpu'),
59573)
self.assertEqual(hashring.compute_ring_position('hosts.worker1.load'),
57163)
self.assertEqual(hashring.compute_ring_position('hosts.worker2.cpu'),
35749)
self.assertEqual(hashring.compute_ring_position('hosts.worker2.network'),
43584)
self.assertEqual(hashring.compute_ring_position('hosts.worker3.cpu'),
12600)
self.assertEqual(hashring.compute_ring_position('hosts.worker3.irq'),
10052)
def test_chr_get_node_fnv1a(self):
hosts = [("127.0.0.1", "ba603c36342304ed77953f84ac4d357b"),
("127.0.0.2", "5dd63865534f84899c6e5594dba6749a"),
("127.0.0.3", "866a18b81f2dc4649517a1df13e26f28")]
hashring = ConsistentHashRing(hosts, hash_type='fnv1a_ch')
self.assertEqual(hashring.get_node('hosts.worker1.cpu'),
('127.0.0.1', 'ba603c36342304ed77953f84ac4d357b'))
self.assertEqual(hashring.get_node('hosts.worker2.cpu'),
('127.0.0.3', '866a18b81f2dc4649517a1df13e26f28'))
self.assertEqual(hashring.get_node(
'stats.checkout.cluster.padamski-wro.api.v1.payment-initialize.count'),
('127.0.0.3', '866a18b81f2dc4649517a1df13e26f28'))