-
Notifications
You must be signed in to change notification settings - Fork 567
/
main.go
378 lines (313 loc) · 8.93 KB
/
main.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
package main
import (
"crypto/md5"
"encoding/hex"
"flag"
"fmt"
"io"
"log"
"os"
"strconv"
"time"
"github.com/streadway/amqp"
client "github.com/pachyderm/pachyderm/v2/src/client"
)
const defaultPrefetch = 500
const defaultFlushInterval = 10000 // ms
const defaultExtension = "ndjson"
const defaultBranch = "staging"
const defaultSwitchBranch = "master"
const defaultSwitchInterval = 60000 // ms
var logger log.Logger
func writeFiles(pc *client.APIClient, opts *options, buffer []amqp.Delivery) error {
log.Print("writing buffer...")
log.Printf("buffer_size %d", len(buffer))
// hash messages to ensure there are no duplicated
var maxMessage amqp.Delivery
msgMap := make(map[string]amqp.Delivery)
// groupby hash
groupHash := md5.New()
// Hash messages to ensure there are no duplicates
for _, msg := range buffer {
hash := md5.New()
hash.Write(msg.Body)
groupHash.Write(msg.Body)
// Hash to ensure uniqueness of the messages
name := hex.EncodeToString(hash.Sum(nil))
// Keep track of the highest delivery tag
if msg.DeliveryTag > maxMessage.DeliveryTag {
maxMessage = msg
}
msgMap[name] = msg
}
// Give a warning if we found duplicates. Not a big deal
if len(msgMap) != len(buffer) {
log.Printf("duplicate messages in buffer: %d vs %d", len(msgMap), len(buffer))
}
// Start of writing
log.Print("Writing messages...")
mfc, err := pc.NewModifyFileClient(client.NewCommit(opts.repoName, opts.commitBranch, ""))
if err != nil {
return fmt.Errorf("unable to create new PutFileClient: %v", err)
}
defer mfc.Close()
// Name - to be used for groupby operations
name := opts.topic + "-" + hex.EncodeToString(groupHash.Sum(nil)) + "." + opts.ext
reader, writer := io.Pipe()
errors := make(chan error, 1)
// Write messages into a pip so they can be read by pach client
go func() {
for _, msg := range msgMap {
// Write each message followed by newline - you may need to do initial parsing
// for safety here if your messages have newline characters or change this up entirely.
writer.Write(msg.Body)
writer.Write([]byte("\n"))
}
// Close writer. Log errors.
if err := writer.Close(); err != nil {
errors <- err
}
}()
// Write to PFS.
if opts.overwrite {
err := mfc.PutFile(name, reader)
if err != nil {
return err
}
} else {
err := mfc.PutFile(name, reader, client.WithAppendPutFile())
if err != nil {
return err
}
}
// Was there an error? If so, send back to main since we aren't handling it.
select {
case err := <-errors:
return err
default:
}
log.Print("wrote messages.")
// If test mode is on, this "nacks" and requeues messages so you don't have to keep filling up your test queue.
if opts.test {
log.Print("Nack")
maxMessage.Nack(true, true)
} else {
// Bulk acknowledge all messages writen since last flush.
log.Print("Ack")
maxMessage.Ack(true)
}
// yay
return nil
}
func bufferReceive(pc *client.APIClient, msgC <-chan amqp.Delivery, opts *options, errors chan<- error) {
// Flush the read messages once every
flushC := time.NewTicker(time.Duration(opts.flushInterval) * time.Millisecond)
buffer := make([]amqp.Delivery, 0, cap(msgC))
for {
select {
case msg := <-msgC: // New message to read
buffer = append(buffer, msg)
// Check for bull buffer
if len(buffer) == cap(buffer) {
err := writeFiles(pc, opts, buffer)
if err != nil {
errors <- err
}
buffer = buffer[:0]
//flushC = time.NewTicker(time.Duration(flushInterval) * time.Millisecond)
flushC.Reset(time.Duration(opts.flushInterval) * time.Millisecond)
}
case <-flushC.C: // On a timer
if len(buffer) > 0 {
// Flush the buffer
err := writeFiles(pc, opts, buffer)
if err != nil {
errors <- err
}
buffer = buffer[:0]
}
}
}
}
func switchBranch(pc *client.APIClient, opts *options, errors chan<- error) {
log.Print("started branch switching routine.")
switchC := time.NewTicker(time.Duration(opts.switchInterval) * time.Millisecond)
// Switches master to staging at regular intervals
for range switchC.C {
log.Print(fmt.Sprintf("switching branch %s HEAD to %s", opts.switchBranch, opts.commitBranch))
err := pc.CreateBranch(opts.repoName, opts.switchBranch, opts.commitBranch, "", nil)
if err != nil {
errors <- err
return
}
}
}
func consume(pc *client.APIClient, msgs <-chan amqp.Delivery, opts *options, errors chan<- error) {
// Messages to write to file.
msgC := make(chan amqp.Delivery, opts.prefetch)
// Simple goroutine to read incoming messages from a channel
go bufferReceive(pc, msgC, opts, errors)
// This may look funny, but the msgs channel is unbuffered. I don't like debugging channel deadlocks, do you?
for msg := range msgs {
// Write message to the buffer
msgC <- msg
}
}
type options struct {
prefetch int
flushInterval int
switchInterval int
topic string
err error
test bool
overwrite bool
repoName string
commitBranch string
switchBranch string
ext string
}
func parseOptions(opts *options) {
var err error
// Prefetch
prefetchStr, ok := os.LookupEnv("PREFETCH")
if ok {
opts.prefetch, err = strconv.Atoi(prefetchStr)
if err != nil {
log.Fatal("unable to parse PREFETCH")
}
}
// Flush Interval
flushIntStr, ok := os.LookupEnv("FLUSH_INTERVAL_MS")
if ok {
opts.flushInterval, err = strconv.Atoi(flushIntStr)
if err != nil {
log.Fatal("unable to parse FLUSH_INTERVAL_MS")
}
}
// Branch Switch Interval
switchIntStr, ok := os.LookupEnv("SWITCH_INTERVAL_MS")
if ok {
opts.switchInterval, err = strconv.Atoi(switchIntStr)
if err != nil {
log.Fatal("unable to parse SWITCH_INTERVAL_MS")
}
}
// File Extension
extStr, ok := os.LookupEnv("EXTENSION")
if ok {
opts.ext = extStr
}
testStr, ok := os.LookupEnv("TEST")
if ok {
opts.test, err = strconv.ParseBool(testStr)
if err != nil {
log.Fatal("unable to parse TEST")
}
}
switchBranchName, ok := os.LookupEnv("SWITCH_BRANCH")
if ok {
opts.switchBranch = switchBranchName
}
commitBranchName, ok := os.LookupEnv("COMMIT_BRANCH")
if ok {
opts.commitBranch = commitBranchName
}
// Topic/Queue for this to read from
opts.topic = os.Getenv("TOPIC")
opts.repoName = os.Getenv("PPS_PIPELINE_NAME")
}
func dialRabbit() (*amqp.Connection, error) {
rabbitURL := os.Getenv("RABBITMQ_HOST")
if rabbitURL == "" {
return nil, fmt.Errorf("RABBITMQ_HOST environment variable not set")
}
rabbitUser := os.Getenv("RABBITMQ_USER")
if rabbitUser == "" {
return nil, fmt.Errorf("RABBITMQ_USER environment variable not set")
}
rabbitPassword := os.Getenv("RABBITMQ_PASSWORD")
if rabbitPassword == "" {
return nil, fmt.Errorf("RABBITMQ_PASSWORD environment variable not set")
}
rabbitProtocol := os.Getenv("RABBITMQ_PROTOCOL")
if rabbitProtocol == "" {
rabbitProtocol = "amqp"
}
return amqp.Dial(rabbitProtocol + "://" + rabbitUser + ":" + rabbitPassword + "@" + rabbitURL)
}
func main() {
// Buffered channel for errors. 50 should do it.
errors := make(chan error, 50)
// Create a Pachd client
pc, err := client.NewInWorker()
if err != nil {
log.Fatalf("Unable to create pachd client: %v", err)
return
}
// Close if this exits, log errors if closing fails
defer func() {
err := pc.Close()
if err != nil {
errors <- err
}
}()
// Option defaults
opts := options{
prefetch: defaultPrefetch,
flushInterval: defaultFlushInterval,
ext: defaultExtension,
test: false,
}
parseOptions(&opts)
flag.StringVar(&opts.topic, "topic", "", "RabbitMQ Topic")
flag.BoolVar(&opts.overwrite, "overwrite", true, "overwrite existing files")
flag.Parse()
if opts.topic == "" {
log.Fatal("topic to read from not specified")
}
// Dial rabbit to get a connection
conn, err := dialRabbit()
if err != nil {
log.Fatalf("unable to dial RabbitMQ: %v", err)
}
defer conn.Close()
// Create a channel.
channel, err := conn.Channel()
if err != nil {
log.Fatalf("unable to open a channel: %v", err)
}
defer channel.Close()
// Set prefetch (max messages unacknowledged)
channel.Qos(opts.prefetch, 0, false)
// Declare queue (idempotent. Safe even if you know queue exists)
q, err := channel.QueueDeclare(
opts.topic,
true, // durable
false, // auto delete
false, // non-exclusive
false, // wait for queue
nil, // no args
)
if err != nil {
log.Fatalf("unable to create or access queue with name '%s': %v", opts.topic, err)
}
// Finally, consume from the queue. This is a channel of amqp.Delivery
msgs, err := channel.Consume(
q.Name,
"", // use primary exchange
false, // don't autoack
false, // not exclusive (can be multiple consumers)
false, // *does nothing?*
false, // wait for server to confirm
nil, // no args
)
if err != nil {
log.Fatalf("unable to consume on channel for queue '%s': %v", q.Name, err)
}
// Switch branch on a timer
go switchBranch(pc, &opts, errors)
// Consume from RabbitMQ
go consume(pc, msgs, &opts, errors)
// Log any errors.
log.Fatal(<-errors)
}