-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkafka_system.go
185 lines (154 loc) · 4.36 KB
/
kafka_system.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
package metamorph
import (
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"time"
)
type resetCompleteEvent struct {
Type string `json:"type"`
}
type KafkaSystem struct {
server *Server
zookeeper *kafkaSystemProcess
kafka *kafkaSystemProcess
consumer *consumer
producer *producer
}
type KafkaConfig struct {
Root string
TemporaryPath string
}
func DefaultKafkaConfig() KafkaConfig {
return KafkaConfig{"", "/tmp"}
}
func NewKafkaSystem(server *Server, config KafkaConfig) *KafkaSystem {
kafkaBinPath := filepath.Join(config.Root, "bin")
kafkaConfigPath := filepath.Join(config.Root, "config")
return &KafkaSystem{
server: server,
zookeeper: newKafkaSystemProcess(
"Zookeeper",
filepath.Join(config.TemporaryPath, "zookeeper"),
filepath.Join(kafkaBinPath, "zookeeper-server-start.sh"),
filepath.Join(kafkaConfigPath, "zookeeper.properties")),
kafka: newKafkaSystemProcess(
"Kafka",
filepath.Join(config.TemporaryPath, "kafka-logs"),
filepath.Join(kafkaBinPath, "kafka-server-start.sh"),
filepath.Join(kafkaConfigPath, "server.properties")),
consumer: nil}
}
func (k *KafkaSystem) Reset(topics []string) {
err := k.StopSystem()
if err != nil {
return
}
k.zookeeper.start()
zookeeperProbe := newProbe("localhost", 2181, 2*time.Second)
if !zookeeperProbe.run(10) {
k.server.SendErrorEvent("Zookeeper process did not respond to probe within the timeout")
return
}
k.kafka.start()
kafkaProbe := newProbe("localhost", 9092, 2*time.Second)
if !kafkaProbe.run(10) {
k.server.SendErrorEvent("Kafka process did not respond to probe within the timeout")
return
}
k.consumer = newConsumer(k.server)
if err := k.consumer.start(topics); err != nil {
log.Println("Could not start consumer:", err)
k.server.SendErrorEvent("Could not start Kafka consumer")
return
}
k.producer, err = newProducer()
if err != nil {
log.Println("Could not start producer:", err)
k.server.SendErrorEvent("Could not start Kafka producer")
return
}
k.sendResetCompleteEvent()
}
func (k *KafkaSystem) StopSystem() error {
err := k.stopProcess(k.kafka)
if err != nil {
return err
}
err = k.stopProcess(k.zookeeper)
if err != nil {
return err
}
return nil
}
func (k *KafkaSystem) Send(value []byte, topic string) {
err := k.producer.sendMessage(value, topic)
if err != nil {
errorMsg := fmt.Sprintf("Could not send message '%s' to topic '%s': %v", value, topic, err)
log.Println(errorMsg)
k.server.SendErrorEvent(errorMsg)
}
}
func (k *KafkaSystem) stopProcess(process *kafkaSystemProcess) error {
chExitSignal := make(chan bool)
if process.isRunning() {
err := process.stop(chExitSignal)
if err != nil {
log.Printf("Could not stop %s", process.name)
k.server.SendErrorEvent("%s could not be stopped")
return fmt.Errorf("Could not stop %s", process.name)
}
select {
case <-chExitSignal:
log.Printf("%s process exited...\n", process.name)
case <-time.After(60 * time.Second):
log.Printf("Wait for %s exit timed out...", process.name)
k.server.SendErrorEvent("%s did not signal exit within timeout")
return fmt.Errorf("Wait for %s exit timed out", process.name)
}
} else {
log.Printf("%s is not running. Will not attempt to stop it.", process.name)
}
return nil
}
func (k *KafkaSystem) sendResetCompleteEvent() {
k.server.SendEvent(resetCompleteEvent{Type: "reset_complete"})
}
func newKafkaSystemProcess(name string, pathToRemove string, cmd string, args ...string) *kafkaSystemProcess {
return &kafkaSystemProcess{process: nil, name: name, cmd: cmd, args: args, pathToRemove: pathToRemove}
}
type kafkaSystemProcess struct {
process *os.Process
name string
cmd string
args []string
pathToRemove string
}
func (z *kafkaSystemProcess) isRunning() bool {
return z.process != nil
}
func (z *kafkaSystemProcess) start() {
log.Printf("Starting %s...", z.name)
os.RemoveAll(z.pathToRemove)
cmd := exec.Command(z.cmd, z.args...)
cmd.Start()
z.process = cmd.Process
}
func (z *kafkaSystemProcess) stop(chExitSignal chan bool) error {
err := z.process.Kill()
if err != nil {
return err
}
go z.signalWhenExited(chExitSignal)
return nil
}
func (z *kafkaSystemProcess) signalWhenExited(chExitSignal chan bool) {
_, err := z.process.Wait()
if err != nil {
log.Printf("Error when waiting for %s to exit: %v\n", z.name, err)
}
z.process = nil
chExitSignal <- true
}