This repository has been archived by the owner on Feb 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
supervisor.go
331 lines (284 loc) · 8.54 KB
/
supervisor.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
package butteredscones
import (
"os"
"path/filepath"
"sync"
"time"
"github.com/digitalocean/butteredscones/client"
"github.com/technoweenie/grohl"
)
const (
supervisorReaderChunkSize = 64
)
type Supervisor struct {
files []FileConfiguration
clients []client.Client
snapshotter Snapshotter
// Optional settings
SpoolSize int
MaxLength int
// How frequently to glob for new files that may have appeared
GlobRefresh time.Duration
globTimer *time.Timer
readerPool *FileReaderPool
readyChunks chan *readyChunk
// A separate channel for retries to avoid deadlocking when multiple clients
// need to retry.
retryChunks chan *readyChunk
stopRequest chan interface{}
routineWg sync.WaitGroup
}
type readyChunk struct {
Chunk []*FileData
LockedReaders []*FileReader
}
func NewSupervisor(files []FileConfiguration, clients []client.Client, snapshotter Snapshotter, maxLength int) *Supervisor {
spoolSize := 1024
return &Supervisor{
files: files,
clients: clients,
snapshotter: snapshotter,
// Can be adjusted by clients later before calling Start
SpoolSize: spoolSize,
MaxLength: maxLength,
GlobRefresh: 10 * time.Second,
}
}
// Start pulls things together and plays match-maker.
func (s *Supervisor) Start() {
s.stopRequest = make(chan interface{})
s.readerPool = NewFileReaderPool()
s.readyChunks = make(chan *readyChunk, len(s.clients))
s.retryChunks = make(chan *readyChunk, len(s.clients))
s.routineWg.Add(1)
go func() {
s.populateReaderPool()
s.routineWg.Done()
}()
s.routineWg.Add(1)
go func() {
s.populateReadyChunks()
s.routineWg.Done()
}()
for _, cli := range s.clients {
s.routineWg.Add(1)
go func(c client.Client) {
s.sendReadyChunksToClient(c)
s.routineWg.Done()
}(cli)
}
}
// Stop stops the supervisor cleanly, making sure all progress has been snapshotted
// before exiting.
func (s *Supervisor) Stop() {
close(s.stopRequest)
s.routineWg.Wait()
}
// Reads chunks from available file readers, putting together ready 'chunks'
// that can be sent to clients.
func (s *Supervisor) populateReadyChunks() {
logger := grohl.NewContext(grohl.Data{"ns": "Supervisor", "fn": "populateReadyChunks"})
backoff := &ExponentialBackoff{Minimum: 50 * time.Millisecond, Maximum: 5000 * time.Millisecond}
for {
available, locked := s.readerPool.Counts()
GlobalStatistics.UpdateFileReaderPoolStatistics(available, locked)
currentChunk := &readyChunk{
Chunk: make([]*FileData, 0),
LockedReaders: make([]*FileReader, 0),
}
for len(currentChunk.Chunk) < s.SpoolSize {
if reader := s.readerPool.LockNext(); reader != nil {
select {
case <-s.stopRequest:
return
case chunk := <-reader.C:
if chunk != nil {
currentChunk.Chunk = append(currentChunk.Chunk, chunk...)
currentChunk.LockedReaders = append(currentChunk.LockedReaders, reader)
if len(chunk) > 0 {
if hwm := chunk[len(chunk)-1].HighWaterMark; hwm != nil {
GlobalStatistics.SetFilePosition(hwm.FilePath, hwm.Position)
}
}
} else {
// The reader hit EOF or another error. Remove it and it'll get
// picked up by populateReaderPool again if it still needs to be
// read.
logger.Log(grohl.Data{"status": "EOF", "file": reader.FilePath()})
s.readerPool.Remove(reader)
GlobalStatistics.DeleteFileStatistics(reader.FilePath())
}
default:
// The reader didn't have anything queued up for us. Unlock the
// reader and move on.
s.readerPool.Unlock(reader)
}
} else {
// If there are no more readers, send the chunk ASAP so we can get
// the next chunk in line
logger.Log(grohl.Data{"msg": "no readers available", "resolution": "sending current chunk"})
break
}
}
if len(currentChunk.Chunk) > 0 {
select {
case <-s.stopRequest:
return
case s.readyChunks <- currentChunk:
backoff.Reset()
}
} else {
select {
case <-s.stopRequest:
return
case <-time.After(backoff.Next()):
grohl.Log(grohl.Data{"msg": "no lines available to send", "resolution": "backing off"})
}
}
}
}
// sendReadyChunksToClient reads from the readyChunks channel for a particular
// client, sending those chunks to the remote system. This function is also
// responsible for snapshotting progress and unlocking the readers after it has
// successfully sent.
func (s *Supervisor) sendReadyChunksToClient(client client.Client) {
backoff := &ExponentialBackoff{Minimum: 50 * time.Millisecond, Maximum: 5000 * time.Millisecond}
for {
var readyChunk *readyChunk
select {
case <-s.stopRequest:
return
case readyChunk = <-s.retryChunks:
// got a retry chunk; use it
default:
// pull from the default readyChunk queue
select {
case <-s.stopRequest:
return
case readyChunk = <-s.readyChunks:
// got a chunk
}
}
if readyChunk != nil {
GlobalStatistics.SetClientStatus(client.Name(), clientStatusSending)
if err := s.sendChunk(client, readyChunk.Chunk); err != nil {
grohl.Report(err, grohl.Data{"msg": "failed to send chunk", "resolution": "retrying"})
GlobalStatistics.SetClientStatus(client.Name(), clientStatusRetrying)
// Put the chunk back on the queue for someone else to try
select {
case <-s.stopRequest:
return
case s.retryChunks <- readyChunk:
// continue
}
// Backoff
select {
case <-s.stopRequest:
return
case <-time.After(backoff.Next()):
// continue
}
} else {
backoff.Reset()
GlobalStatistics.IncrementClientLinesSent(client.Name(), len(readyChunk.Chunk))
// Snapshot progress
if err := s.acknowledgeChunk(readyChunk.Chunk); err != nil {
grohl.Report(err, grohl.Data{"msg": "failed to acknowledge progress", "resolution": "skipping"})
}
s.readerPool.UnlockAll(readyChunk.LockedReaders)
}
}
}
}
func (s *Supervisor) sendChunk(c client.Client, chunk []*FileData) error {
lines := make([]client.Data, 0, len(chunk))
for _, fileData := range chunk {
lines = append(lines, fileData.Data)
}
return c.Send(lines)
}
func (s *Supervisor) acknowledgeChunk(chunk []*FileData) error {
marks := make([]*HighWaterMark, 0, len(chunk))
for _, fileData := range chunk {
marks = append(marks, fileData.HighWaterMark)
}
err := s.snapshotter.SetHighWaterMarks(marks)
if err == nil {
// Update statistics
for _, mark := range marks {
GlobalStatistics.SetFileSnapshotPosition(mark.FilePath, mark.Position)
}
}
return err
}
// populateReaderPool periodically globs for new files or files that previously
// hit EOF and creates file readers for them.
func (s *Supervisor) populateReaderPool() {
logger := grohl.NewContext(grohl.Data{"ns": "Supervisor", "fn": "populateReaderPool"})
timer := time.NewTimer(0)
for {
select {
case <-s.stopRequest:
return
case <-timer.C:
logTimer := logger.Timer(grohl.Data{})
for _, config := range s.files {
for _, path := range config.Paths {
matches, err := filepath.Glob(path)
if err != nil {
logger.Report(err, grohl.Data{"path": path, "msg": "failed to glob", "resolution": "skipping path"})
continue
}
for _, filePath := range matches {
if err = s.startFileReader(filePath, config.Fields); err != nil {
logger.Report(err, grohl.Data{"path": path, "filePath": filePath, "msg": "failed to start reader", "resolution": "skipping file"})
}
}
}
}
logTimer.Finish()
timer.Reset(s.GlobRefresh)
}
}
}
// startFileReader starts an individual file reader at a given path, if one
// isn't already running.
func (s *Supervisor) startFileReader(filePath string, fields map[string]string) error {
// There's already a reader in the pool for this path
if s.readerPool.IsPathInPool(filePath) {
return nil
}
highWaterMark, err := s.snapshotter.HighWaterMark(filePath)
if err != nil {
return err
}
file, err := os.Open(filePath)
if err != nil {
return err
}
stat, err := file.Stat()
if err != nil {
file.Close()
return err
}
// If the file's current size isn't beyond the high water mark, it'll
// immediately EOF so there's no use in creating a reader for it.
if stat.Size() <= highWaterMark.Position {
file.Close()
return nil
}
_, err = file.Seek(highWaterMark.Position, os.SEEK_SET)
if err != nil {
file.Close()
return err
}
GlobalStatistics.SetFilePosition(filePath, highWaterMark.Position)
GlobalStatistics.SetFileSnapshotPosition(filePath, highWaterMark.Position)
reader, err := NewFileReader(file, fields, supervisorReaderChunkSize, s.MaxLength)
if err != nil {
file.Close()
return err
}
s.readerPool.Add(reader)
return nil
}