-
Notifications
You must be signed in to change notification settings - Fork 1
/
tailor.go
361 lines (295 loc) · 7.78 KB
/
tailor.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
// Copyright 2019 Yegor Myskin. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tailor
import (
"bufio"
"context"
"io"
"os"
"sync/atomic"
"time"
"github.com/pkg/errors"
)
type Tailor struct {
fileName string
file *os.File
opts options
// stats
lastPos int64
lastSize int64
lag int64
lines chan Line
errs chan error
working int32
}
// New prepares the instance of Tailor. It mergers default options with the given options.
func New(filename string, opts ...Option) *Tailor {
t := &Tailor{
fileName: filename,
}
for _, p := range [][]Option{withDefaultOptions(), opts} {
for _, o := range p {
o(&t.opts)
}
}
return t
}
// Run starts the tailing procedure.
// 1. Opens a file in RO mode and seeks to the newest position by default(can be changed in options).
// 2. Then reads the file line by line and produces lines and errors through the channels.
// If the file has been logrotated, Tailor will follow the first file to the end and after reopen it.
// If error happens file will be closed.
// Tailor makes an exponential sleep to reduce stat syscalls.
func (t *Tailor) Run(ctx context.Context) error {
if !atomic.CompareAndSwapInt32(&t.working, 0, 1) {
return errors.New("already working")
}
failFinalizer := func() {
if t.file != nil {
_ = t.file.Close()
}
atomic.StoreInt32(&t.working, 0)
}
err := t.openFile(t.opts.runOffset, t.opts.runWhence)
if err != nil {
failFinalizer()
return errors.Wrap(err, "can't open file for tailing")
}
t.readLoop(ctx)
return nil
}
// readLoop starts goroutine, which reads the given file and send to the line chan tailed strings.
func (t *Tailor) readLoop(ctx context.Context) {
t.lines = make(chan Line)
t.errs = make(chan error)
go func() {
defer func() {
if t.file != nil {
err := t.file.Close()
if err != nil {
t.errs <- errors.Wrap(err, "error closing file")
}
}
close(t.lines)
close(t.errs)
if t.opts.rateLimiter != nil {
t.opts.rateLimiter.Close()
}
atomic.StoreInt32(&t.working, 0)
}()
r := bufio.NewReaderSize(t.file, t.opts.bufioReaderPoolSize)
lagReporter := time.NewTicker(t.opts.updateLagInterval)
defer lagReporter.Stop()
pollerTimeout := t.opts.pollerTimeout
for {
select {
case <-ctx.Done():
return
case <-lagReporter.C:
err := t.updateFileStatus()
if err != nil {
t.errs <- errors.Wrap(err, "error getting file status")
break
}
default:
}
var (
part []byte
line []byte
err error
)
// a Line can be read partially
// so wait until the new one comes
// if the line wasn't read after 5 tries return it
for i := 0; i < 5; i++ {
part = part[:0]
part, err = r.ReadBytes('\n')
if err == nil || err == io.EOF {
// if Line is new and finished, don't allocate buffer, just copy ref
if len(line) == 0 && len(part) > 0 && part[len(part)-1] == '\n' {
line = part
break
}
} else {
t.errs <- errors.Wrap(err, "error reading line")
return
}
line = append(line, part...)
if len(line) == 0 && err == io.EOF {
break
}
if line[len(line)-1] == '\n' {
break
}
pollerTimeout = t.exponentialSleep(pollerTimeout, time.Second)
}
// check that logrotate swapped the file
if len(line) == 0 && err == io.EOF {
isSameFile, err := t.isFileStillTheSame()
if err != nil {
t.errs <- errors.Wrap(err, "error checking that file is the same")
return
}
if !isSameFile {
err := t.file.Close()
if err != nil {
t.errs <- errors.Wrap(err, "error closing current file")
}
err = t.openFile(t.opts.reopenOffset, t.opts.reopenWhence)
if err != nil {
t.errs <- errors.Wrap(err, "error reopening file")
return
}
r = bufio.NewReaderSize(t.file, t.opts.bufioReaderPoolSize)
pollerTimeout = t.opts.pollerTimeout
continue
}
pollerTimeout = t.exponentialSleep(pollerTimeout, 5*time.Second)
continue
}
pollerTimeout = t.opts.pollerTimeout
if t.opts.rateLimiter == nil || t.opts.rateLimiter.Allow() {
line := Line{
line: line,
fileName: t.fileName,
}
if t.opts.leakyBucket {
select {
case t.lines <- line:
default:
}
continue
}
t.lines <- line
}
}
}()
}
// Lines returns chanel of read lines.
func (t *Tailor) Lines() chan Line {
return t.lines
}
// Errors returns chanel of errors, associated with reading files.
func (t *Tailor) Errors() chan error {
return t.errs
}
// exponentialSleep sleeps for pollerTimeout and returns new exponential grown timeout <= maxWait.
func (t *Tailor) exponentialSleep(pollerTimeout time.Duration, maxWait time.Duration) time.Duration {
time.Sleep(pollerTimeout)
// use exponential poller duration to reduce the load
if pollerTimeout < maxWait {
return pollerTimeout * 2
}
return maxWait
}
// isFileStillTheSame checks that opened file wasn't swapped.
func (t *Tailor) isFileStillTheSame() (isSameFile bool, err error) {
var maybeNewFileInfo os.FileInfo
// maybe the current file is being rotated with a small lag, check it for some tries
for i := 0; i < 2; i++ {
maybeNewFileInfo, err = os.Stat(t.fileName)
if os.IsNotExist(err) {
time.Sleep(time.Second)
continue
}
if err != nil {
return false, errors.Wrap(err, "error stating maybe new file by name")
}
break
}
currentFileInfo, err := t.file.Stat()
if err != nil {
return false, errors.Wrap(err, "error stating current file")
}
return os.SameFile(currentFileInfo, maybeNewFileInfo), nil
}
// FileName returns the name of the tailed file.
func (t Tailor) FileName() string {
return t.fileName
}
// Lag returns approximate lag, updater per interval.
func (t Tailor) Lag() int64 {
return atomic.LoadInt64(&t.lag)
}
// openFile opens the file for reading, seeks to the beginning of the line at opts.*offset and updates the lag.
func (t *Tailor) openFile(offset int64, whence int) (err error) {
t.file, err = os.Open(t.fileName)
if err != nil {
return errors.Wrap(err, "error opening file")
}
err = t.seekToLineStart(offset, whence)
if err != nil {
return errors.Wrap(err, "error seeking to line start")
}
err = t.updateFileStatus()
if err != nil {
return errors.Wrap(err, "error updating file status")
}
return nil
}
// seekToLineStart seeks the cursor at the beginning of a line at offset. Internally this function uses a buffer
// to find the beginning of a line. If the byte at offset equals \n, so the next line will be selected.
func (t *Tailor) seekToLineStart(offset int64, whence int) error {
const (
bufSize int64 = 256
)
initialOffset, err := t.file.Seek(offset, whence)
if initialOffset == 0 {
return nil
}
if err == io.EOF {
err = nil
}
if err != nil {
return err
}
min := func(a, b int64) int64 {
if a < b {
return a
}
return b
}
var current int64 = 0
Loop:
for {
current += min(bufSize, initialOffset-current)
buf := make([]byte, min(current, bufSize))
n, err := t.file.ReadAt(buf, initialOffset-current)
if err != nil && err != io.EOF {
return err
}
buf = buf[:n]
current -= int64(n)
for i := int64(len(buf)) - 1; i >= 0; i-- {
if buf[i] == '\n' {
break Loop
}
current++
}
if initialOffset-current == 0 {
break
}
}
_, err = t.file.Seek(-current, io.SeekCurrent)
if err == io.EOF {
err = nil
}
return err
}
// updateFileStatus update a current seek from the file an an actual file size.
func (t *Tailor) updateFileStatus() (err error) {
fi, err := t.file.Stat()
if err != nil {
return err
}
pos, err := t.file.Seek(0, io.SeekCurrent)
if err != nil {
return err
}
size := fi.Size()
atomic.StoreInt64(&t.lastPos, pos)
atomic.StoreInt64(&t.lastSize, size)
atomic.StoreInt64(&t.lag, size-pos)
return nil
}