-
Notifications
You must be signed in to change notification settings - Fork 74
/
mirror_maker_test.go
278 lines (244 loc) · 9.23 KB
/
mirror_maker_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
/* Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package go_kafka_client
import (
"fmt"
"github.com/Shopify/sarama"
"io/ioutil"
"os"
"sync"
"testing"
"time"
)
func TestMirrorMakerWorks(t *testing.T) {
topic := fmt.Sprintf("mirror-maker-works-%d", time.Now().Unix())
prefix := "mirror_"
destTopic := fmt.Sprintf("%s%s", prefix, topic)
consumeMessages := 100
timeout := 1 * time.Minute
consumeStatus := make(chan int)
CreateMultiplePartitionsTopic(localZk, topic, 1)
EnsureHasLeader(localZk, topic)
CreateMultiplePartitionsTopic(localZk, destTopic, 1)
EnsureHasLeader(localZk, destTopic)
config := NewMirrorMakerConfig()
consumerConfigLocation := createConsumerConfig(t, 1)
defer os.Remove(consumerConfigLocation)
config.ConsumerConfigs = []string{consumerConfigLocation}
config.NumProducers = 1
config.NumStreams = 1
config.PreservePartitions = false
producerConfigLocation := createProducerConfig(t, 1)
defer os.Remove(producerConfigLocation)
config.ProducerConfig = producerConfigLocation
config.TopicPrefix = "mirror_"
config.Whitelist = fmt.Sprintf("^%s$", topic)
mirrorMaker := NewMirrorMaker(config)
go mirrorMaker.Start()
messages := make([]string, 0)
for i := 0; i < 100; i++ {
messages = append(messages, fmt.Sprintf("mirror-%d", i))
}
produce(t, messages, topic, localBroker, sarama.CompressionNone)
consumerConfig := testConsumerConfig()
consumerConfig.Strategy = newCountingStrategy(t, consumeMessages, timeout, consumeStatus)
consumer := NewConsumer(consumerConfig)
go consumer.StartStatic(map[string]int{topic: 1})
if actual := <-consumeStatus; actual != consumeMessages {
t.Errorf("Failed to consume %d messages within %s. Actual messages = %d", consumeMessages, timeout, actual)
}
closeWithin(t, 10*time.Second, consumer)
mirrorMaker.Stop()
}
func TestMirrorMakerPreservesPartitions(t *testing.T) {
topic := fmt.Sprintf("mirror-maker-partitions-%d", time.Now().Unix())
prefix := "mirror_"
destTopic := fmt.Sprintf("%s%s", prefix, topic)
consumeMessages := 100
timeout := 1 * time.Minute
sourceConsumeStatus := make(chan int)
destConsumeStatus := make(chan int)
sourceConsumedMessages := 0
destConsumedMessages := 0
CreateMultiplePartitionsTopic(localZk, topic, 5)
EnsureHasLeader(localZk, topic)
CreateMultiplePartitionsTopic(localZk, destTopic, 5)
EnsureHasLeader(localZk, destTopic)
config := NewMirrorMakerConfig()
consumerConfigLocation := createConsumerConfig(t, 1)
defer os.Remove(consumerConfigLocation)
config.ConsumerConfigs = []string{consumerConfigLocation}
config.NumProducers = 1
config.NumStreams = 1
config.PreservePartitions = true
producerConfigLocation := createProducerConfig(t, 1)
defer os.Remove(producerConfigLocation)
config.ProducerConfig = producerConfigLocation
config.TopicPrefix = prefix
config.Whitelist = fmt.Sprintf("^%s$", topic)
mirrorMaker := NewMirrorMaker(config)
go mirrorMaker.Start()
produceN(t, consumeMessages, topic, localBroker)
//consume all messages with a regular consumer and make a map of message partitions
messagePartitions := make(map[string]int32)
sourceConsumerConfig := testConsumerConfig()
sourceConsumerConfig.Strategy = func(_ *Worker, msg *Message, id TaskId) WorkerResult {
messagePartitions[string(msg.Value)] = msg.Partition
sourceConsumedMessages++
if sourceConsumedMessages == consumeMessages {
sourceConsumeStatus <- sourceConsumedMessages
}
return NewSuccessfulResult(id)
}
sourceConsumer := NewConsumer(sourceConsumerConfig)
go sourceConsumer.StartStatic(map[string]int{topic: 1})
select {
case <-sourceConsumeStatus:
case <-time.After(timeout):
t.Errorf("Failed to consume %d messages within %s", consumeMessages, timeout)
}
closeWithin(t, 10*time.Second, sourceConsumer)
//now consume the mirrored topic and make sure all messages are in correct partitions
destConsumerConfig := testConsumerConfig()
destConsumerConfig.Strategy = func(_ *Worker, msg *Message, id TaskId) WorkerResult {
expectedPartition := messagePartitions[string(msg.Value)]
assert(t, msg.Partition, expectedPartition)
destConsumedMessages++
if destConsumedMessages == consumeMessages {
destConsumeStatus <- destConsumedMessages
}
return NewSuccessfulResult(id)
}
destConsumer := NewConsumer(destConsumerConfig)
go destConsumer.StartStatic(map[string]int{destTopic: 1})
select {
case <-destConsumeStatus:
case <-time.After(timeout):
t.Errorf("Failed to consume %d messages within %s", consumeMessages, timeout)
}
closeWithin(t, 10*time.Second, destConsumer)
mirrorMaker.Stop()
}
func TestMirrorMakerPreservesOrder(t *testing.T) {
topic := fmt.Sprintf("mirror-maker-order-%d", time.Now().Unix())
prefix := "mirror_"
destTopic := fmt.Sprintf("%s%s", prefix, topic)
consumeMessages := 100
timeout := 1 * time.Minute
sourceConsumeStatus := make(chan int)
destConsumeStatus := make(chan int)
sourceConsumedMessages := 0
destConsumedMessages := 0
CreateMultiplePartitionsTopic(localZk, topic, 5)
EnsureHasLeader(localZk, topic)
CreateMultiplePartitionsTopic(localZk, destTopic, 5)
EnsureHasLeader(localZk, destTopic)
config := NewMirrorMakerConfig()
consumerConfigLocation := createConsumerConfig(t, 1)
defer os.Remove(consumerConfigLocation)
config.ConsumerConfigs = []string{consumerConfigLocation}
config.NumProducers = 3
config.NumStreams = 1
config.PreservePartitions = true
config.PreserveOrder = true
producerConfigLocation := createProducerConfig(t, 1)
defer os.Remove(producerConfigLocation)
config.ProducerConfig = producerConfigLocation
config.TopicPrefix = prefix
config.Whitelist = fmt.Sprintf("^%s$", topic)
mirrorMaker := NewMirrorMaker(config)
go mirrorMaker.Start()
produceN(t, consumeMessages, topic, localBroker)
//consume all messages with a regular consumer and make a map of message partitions
messageOrder := make(map[int32][]string)
var messageOrderLock sync.Mutex
sourceConsumerConfig := testConsumerConfig()
sourceConsumerConfig.Strategy = func(_ *Worker, msg *Message, id TaskId) WorkerResult {
messageOrderLock.Lock()
defer messageOrderLock.Unlock()
if _, exists := messageOrder[msg.Partition]; !exists {
messageOrder[msg.Partition] = make([]string, 0)
}
messageOrder[msg.Partition] = append(messageOrder[msg.Partition], string(msg.Value))
sourceConsumedMessages++
if sourceConsumedMessages == consumeMessages {
sourceConsumeStatus <- sourceConsumedMessages
}
return NewSuccessfulResult(id)
}
sourceConsumer := NewConsumer(sourceConsumerConfig)
go sourceConsumer.StartStatic(map[string]int{topic: 1})
select {
case <-sourceConsumeStatus:
case <-time.After(timeout):
t.Errorf("Failed to consume %d messages within %s", consumeMessages, timeout)
}
closeWithin(t, 10*time.Second, sourceConsumer)
//now consume the mirrored topic and make sure all messages are in correct partitions
destConsumerConfig := testConsumerConfig()
destConsumerConfig.Strategy = func(_ *Worker, msg *Message, id TaskId) WorkerResult {
messageOrderLock.Lock()
defer messageOrderLock.Unlock()
expectedMessage := messageOrder[msg.Partition][0]
assert(t, string(msg.Value), expectedMessage)
messageOrder[msg.Partition] = messageOrder[msg.Partition][1:]
destConsumedMessages++
if destConsumedMessages == consumeMessages {
destConsumeStatus <- destConsumedMessages
}
return NewSuccessfulResult(id)
}
destConsumer := NewConsumer(destConsumerConfig)
go destConsumer.StartStatic(map[string]int{destTopic: 1})
select {
case <-destConsumeStatus:
case <-time.After(timeout):
t.Errorf("Failed to consume %d messages within %s", consumeMessages, timeout)
}
closeWithin(t, 10*time.Second, destConsumer)
mirrorMaker.Stop()
}
func createConsumerConfig(t *testing.T, id int) string {
tmpPath, err := ioutil.TempDir("", "go_kafka_client")
if err != nil {
t.Fatal(err)
}
os.Mkdir(tmpPath, 0700)
fileName := fmt.Sprintf("consumer-%d.properties", id)
contents := fmt.Sprintf(`zookeeper.connect=%s
zookeeper.connection.timeout.ms=6000
group.id=%s
`, localZk, fmt.Sprintf("test-mirrormaker-%d", time.Now().Unix()))
configPath := fmt.Sprintf("%s/%s", tmpPath, fileName)
err = ioutil.WriteFile(configPath, []byte(contents), 0700)
if err != nil {
panic(err)
}
return configPath
}
func createProducerConfig(t *testing.T, id int) string {
tmpPath, err := ioutil.TempDir("", "go_kafka_client")
if err != nil {
t.Fatal(err)
}
os.Mkdir(tmpPath, 0700)
fileName := fmt.Sprintf("producer-%d.properties", id)
contents := fmt.Sprintf(`bootstrap.servers=%s
`, localBroker)
configPath := fmt.Sprintf("%s/%s", tmpPath, fileName)
err = ioutil.WriteFile(configPath, []byte(contents), 0700)
if err != nil {
panic(err)
}
return configPath
}