forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
balancer_test.go
279 lines (261 loc) · 7.3 KB
/
balancer_test.go
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package kafka
import (
"fmt"
"hash"
"hash/crc32"
"testing"
)
func TestHashBalancer(t *testing.T) {
testCases := map[string]struct {
Key []byte
Hasher hash.Hash32
Partitions []int
Partition int
}{
"nil": {
Key: nil,
Partitions: []int{0, 1, 2},
Partition: 0,
},
"partition-0": {
Key: []byte("blah"),
Partitions: []int{0, 1},
Partition: 0,
},
"partition-1": {
Key: []byte("blah"),
Partitions: []int{0, 1, 2},
Partition: 1,
},
"partition-2": {
Key: []byte("boop"),
Partitions: []int{0, 1, 2},
Partition: 2,
},
"custom hash": {
Key: []byte("boop"),
Hasher: crc32.NewIEEE(),
Partitions: []int{0, 1, 2},
Partition: 1,
},
// in a previous version, this test would select a different partition
// than sarama's hash partitioner.
"hash code with MSB set": {
Key: []byte("20"),
Partitions: []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15},
Partition: 1,
},
}
for label, test := range testCases {
t.Run(label, func(t *testing.T) {
msg := Message{Key: test.Key}
h := Hash{
Hasher: test.Hasher,
}
partition := h.Balance(msg, test.Partitions...)
if partition != test.Partition {
t.Errorf("expected %v; got %v", test.Partition, partition)
}
})
}
}
func TestCRC32Balancer(t *testing.T) {
// These tests are taken from the default "consistent_random" partitioner from
// https://github.com/edenhill/librdkafka/blob/master/tests/0048-partitioner.c
partitionCount := 17
var partitions []int
for i := 0; i < partitionCount; i++ {
partitions = append(partitions, i*i)
}
testCases := map[string]struct {
Key []byte
Partitions []int
Partition int
}{
"nil": {
Key: nil,
Partitions: partitions,
Partition: -1,
},
"empty": {
Key: []byte{},
Partitions: partitions,
Partition: -1,
},
"unaligned": {
Key: []byte("23456"),
Partitions: partitions,
Partition: partitions[0xb1b451d7%partitionCount],
},
"long key": {
Key: []byte("this is another string with more length to it perhaps"),
Partitions: partitions,
Partition: partitions[0xb0150df7%partitionCount],
},
"short key": {
Key: []byte("hejsan"),
Partitions: partitions,
Partition: partitions[0xd077037e%partitionCount],
},
}
t.Run("default", func(t *testing.T) {
for label, test := range testCases {
t.Run(label, func(t *testing.T) {
b := CRC32Balancer{}
b.random.mock = -1
msg := Message{Key: test.Key}
partition := b.Balance(msg, test.Partitions...)
if partition != test.Partition {
t.Errorf("expected %v; got %v", test.Partition, partition)
}
})
}
})
t.Run("consistent", func(t *testing.T) {
b := CRC32Balancer{Consistent: true}
b.random.mock = -1
p := b.Balance(Message{}, partitions...)
if p < 0 {
t.Fatal("should not have gotten a random partition")
}
for i := 0; i < 10; i++ {
if p != b.Balance(Message{}, partitions...) {
t.Fatal("nil key should always hash consistently")
}
if p != b.Balance(Message{Key: []byte{}}, partitions...) {
t.Fatal("empty key should always hash consistently and have same result as nil key")
}
}
})
}
func TestMurmur2(t *testing.T) {
// These tests are taken from the "murmur2" implementation from
// https://github.com/edenhill/librdkafka/blob/master/src/rdmurmur2.c
testCases := []struct {
Key []byte
JavaMurmur2Result uint32
}{
{Key: []byte("kafka"), JavaMurmur2Result: 0xd067cf64},
{Key: []byte("giberish123456789"), JavaMurmur2Result: 0x8f552b0c},
{Key: []byte("1234"), JavaMurmur2Result: 0x9fc97b14},
{Key: []byte("234"), JavaMurmur2Result: 0xe7c009ca},
{Key: []byte("34"), JavaMurmur2Result: 0x873930da},
{Key: []byte("4"), JavaMurmur2Result: 0x5a4b5ca1},
{Key: []byte("PreAmbleWillBeRemoved,ThePrePartThatIs"), JavaMurmur2Result: 0x78424f1c},
{Key: []byte("reAmbleWillBeRemoved,ThePrePartThatIs"), JavaMurmur2Result: 0x4a62b377},
{Key: []byte("eAmbleWillBeRemoved,ThePrePartThatIs"), JavaMurmur2Result: 0xe0e4e09e},
{Key: []byte("AmbleWillBeRemoved,ThePrePartThatIs"), JavaMurmur2Result: 0x62b8b43f},
{Key: []byte(""), JavaMurmur2Result: 0x106e08d9},
{Key: nil, JavaMurmur2Result: 0x106e08d9},
}
for _, test := range testCases {
t.Run(fmt.Sprintf("key:%s", test.Key), func(t *testing.T) {
got := murmur2(test.Key)
if got != test.JavaMurmur2Result {
t.Errorf("expected %v; got %v", test.JavaMurmur2Result, got)
}
})
}
}
func TestMurmur2Balancer(t *testing.T) {
// These tests are taken from the "murmur2_random" partitioner from
// https://github.com/edenhill/librdkafka/blob/master/tests/0048-partitioner.c
partitionCount := 17
librdkafkaPartitions := make([]int, partitionCount)
for i := 0; i < partitionCount; i++ {
librdkafkaPartitions[i] = i * i
}
// These tests are taken from the Murmur2Partitioner Python class from
// https://github.com/dpkp/kafka-python/blob/master/test/test_partitioner.py
pythonPartitions := make([]int, 1000)
for i := 0; i < 1000; i++ {
pythonPartitions[i] = i
}
testCases := map[string]struct {
Key []byte
Partitions []int
Partition int
}{
"librdkafka-nil": {
Key: nil,
Partitions: librdkafkaPartitions,
Partition: 123,
},
"librdkafka-empty": {
Key: []byte{},
Partitions: librdkafkaPartitions,
Partition: librdkafkaPartitions[0x106e08d9%partitionCount],
},
"librdkafka-unaligned": {
Key: []byte("23456"),
Partitions: librdkafkaPartitions,
Partition: librdkafkaPartitions[0x058d780f%partitionCount],
},
"librdkafka-long key": {
Key: []byte("this is another string with more length to it perhaps"),
Partitions: librdkafkaPartitions,
Partition: librdkafkaPartitions[0x4f7703da%partitionCount],
},
"librdkafka-short key": {
Key: []byte("hejsan"),
Partitions: librdkafkaPartitions,
Partition: librdkafkaPartitions[0x5ec19395%partitionCount],
},
"python-empty": {
Key: []byte(""),
Partitions: pythonPartitions,
Partition: 681,
},
"python-a": {
Key: []byte("a"),
Partitions: pythonPartitions,
Partition: 524,
},
"python-ab": {
Key: []byte("ab"),
Partitions: pythonPartitions,
Partition: 434,
},
"python-abc": {
Key: []byte("abc"),
Partitions: pythonPartitions,
Partition: 107,
},
"python-123456789": {
Key: []byte("123456789"),
Partitions: pythonPartitions,
Partition: 566,
},
"python-\x00 ": {
Key: []byte{0, 32},
Partitions: pythonPartitions,
Partition: 742,
},
}
t.Run("default", func(t *testing.T) {
for label, test := range testCases {
t.Run(label, func(t *testing.T) {
b := Murmur2Balancer{}
b.random.mock = 123
msg := Message{Key: test.Key}
partition := b.Balance(msg, test.Partitions...)
if partition != test.Partition {
t.Errorf("expected %v; got %v", test.Partition, partition)
}
})
}
})
t.Run("consistent", func(t *testing.T) {
b := Murmur2Balancer{Consistent: true}
b.random.mock = -1
p := b.Balance(Message{}, librdkafkaPartitions...)
if p < 0 {
t.Fatal("should not have gotten a random partition")
}
for i := 0; i < 10; i++ {
if p != b.Balance(Message{}, librdkafkaPartitions...) {
t.Fatal("nil key should always hash consistently")
}
}
})
}