forked from mozilla-services/heka
/
file_output.go
332 lines (300 loc) · 9.07 KB
/
file_output.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
/***** BEGIN LICENSE BLOCK *****
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# The Initial Developer of the Original Code is the Mozilla Foundation.
# Portions created by the Initial Developer are Copyright (C) 2012-2014
# the Initial Developer. All Rights Reserved.
#
# Contributor(s):
# Rob Miller (rmiller@mozilla.com)
# Mike Trinkala (trink@mozilla.com)
#
# ***** END LICENSE BLOCK *****/
package file
import (
"errors"
"fmt"
. "github.com/mozilla-services/heka/pipeline"
"github.com/mozilla-services/heka/plugins"
"github.com/rafrombrc/go-notify"
"os"
"path/filepath"
"strconv"
"sync"
"time"
)
// Output plugin that writes message contents to a file on the file system.
type FileOutput struct {
*FileOutputConfig
path string
perm os.FileMode
flushOpAnd bool
file *os.File
batchChan chan []byte
backChan chan []byte
folderPerm os.FileMode
timerChan <-chan time.Time
}
// ConfigStruct for FileOutput plugin.
type FileOutputConfig struct {
// Full output file path.
// If date rotation is in use, then the output file name can support
// Go's time.Format syntax to embed timestamps in the filename:
// http://golang.org/pkg/time/#Time.Format
Path string
// Output file permissions (default "644").
Perm string
// Interval at which the output file should be rotated, in hours.
// Only the following values are allowed: 0, 1, 4, 12, 24
// The files will be named relative to midnight of the day.
// (default 0, i.e. disabled). Set to 0 to disable.
RotationInterval uint32 `toml:"rotation_interval"`
// Interval at which accumulated file data should be written to disk, in
// milliseconds (default 1000, i.e. 1 second). Set to 0 to disable.
FlushInterval uint32 `toml:"flush_interval"`
// Number of messages to accumulate until file data should be written to
// disk (default 1, minimum 1).
FlushCount uint32 `toml:"flush_count"`
// Operator describing how the two parameters "flush_interval" and
// "flush_count" are combined. Allowed values are "AND" or "OR" (default is
// "AND").
FlushOperator string `toml:"flush_operator"`
// Permissions to apply to directories created for FileOutput's parent
// directory if it doesn't exist. Must be a string representation of an
// octal integer. Defaults to "700".
FolderPerm string `toml:"folder_perm"`
// Specifies whether or not Heka's stream framing will be applied to the
// output. We do some magic to default to true if ProtobufEncoder is used,
// false otherwise.
UseFraming *bool `toml:"use_framing"`
}
func (o *FileOutput) ConfigStruct() interface{} {
return &FileOutputConfig{
Perm: "644",
RotationInterval: 0,
FlushInterval: 1000,
FlushCount: 1,
FlushOperator: "AND",
FolderPerm: "700",
}
}
func (o *FileOutput) Init(config interface{}) (err error) {
conf := config.(*FileOutputConfig)
o.FileOutputConfig = conf
var intPerm int64
if intPerm, err = strconv.ParseInt(conf.FolderPerm, 8, 32); err != nil {
err = fmt.Errorf("FileOutput '%s' can't parse `folder_perm`, is it an octal integer string?",
o.Path)
return
}
o.folderPerm = os.FileMode(intPerm)
if intPerm, err = strconv.ParseInt(conf.Perm, 8, 32); err != nil {
err = fmt.Errorf("FileOutput '%s' can't parse `perm`, is it an octal integer string?",
o.Path)
return
}
o.perm = os.FileMode(intPerm)
switch conf.RotationInterval {
case 0:
// date rotation is disabled
o.path = o.Path
case 1, 4, 12, 24:
// RotationInterval value is allowed
t := time.Now().Truncate(time.Duration(o.RotationInterval) * time.Hour)
o.path = t.Format(o.Path)
default:
err = fmt.Errorf("Parameter 'rotation_interval' must be one of: 0, 1, 4, 12, 24.")
return
}
if err = o.openFile(); err != nil {
err = fmt.Errorf("FileOutput '%s' error opening file: %s", o.path, err)
return
}
if conf.FlushCount < 1 {
err = fmt.Errorf("Parameter 'flush_count' needs to be greater 1.")
return
}
switch conf.FlushOperator {
case "AND":
o.flushOpAnd = true
case "OR":
o.flushOpAnd = false
default:
err = fmt.Errorf("Parameter 'flush_operator' needs to be either 'AND' or 'OR', is currently: '%s'",
conf.FlushOperator)
return
}
o.batchChan = make(chan []byte)
o.backChan = make(chan []byte, 2) // Never block on the hand-back
return
}
func (o *FileOutput) openFile() (err error) {
basePath := filepath.Dir(o.path)
if err = os.MkdirAll(basePath, o.folderPerm); err != nil {
return fmt.Errorf("Can't create the basepath for the FileOutput plugin: %s", err.Error())
}
if err = plugins.CheckWritePermission(basePath); err != nil {
return
}
o.file, err = os.OpenFile(o.path, os.O_WRONLY|os.O_APPEND|os.O_CREATE, o.perm)
return
}
func (o *FileOutput) tryRotateFile() (err error) {
if o.RotationInterval == 0 {
return
}
t := time.Now().Truncate(time.Duration(o.RotationInterval) * time.Hour)
if path := t.Format(o.Path); path != o.path {
// output file needs to be rotated
o.file.Close()
o.path = path
err = o.openFile()
}
return
}
func (o *FileOutput) Run(or OutputRunner, h PluginHelper) (err error) {
enc := or.Encoder()
if enc == nil {
return errors.New("Encoder required.")
}
if o.UseFraming == nil {
// Nothing was specified, we'll default to framing IFF ProtobufEncoder
// is being used.
if _, ok := enc.(*ProtobufEncoder); ok {
or.SetUseFraming(true)
}
}
var wg sync.WaitGroup
wg.Add(2)
go o.receiver(or, &wg)
go o.committer(or, &wg)
wg.Wait()
return
}
// Runs in a separate goroutine, accepting incoming messages, buffering output
// data until the ticker triggers the buffered data should be put onto the
// committer channel.
func (o *FileOutput) receiver(or OutputRunner, wg *sync.WaitGroup) {
var (
pack *PipelinePack
e error
timer *time.Timer
timerDuration time.Duration
msgCounter uint32
intervalElapsed bool
outBytes []byte
)
ok := true
outBatch := make([]byte, 0, 10000)
inChan := or.InChan()
timerDuration = time.Duration(o.FlushInterval) * time.Millisecond
if o.FlushInterval > 0 {
timer = time.NewTimer(timerDuration)
if o.timerChan == nil { // Tests might have set this already.
o.timerChan = timer.C
}
}
for ok {
select {
case pack, ok = <-inChan:
if !ok {
// Closed inChan => we're shutting down, flush data
if len(outBatch) > 0 {
o.batchChan <- outBatch
}
close(o.batchChan)
break
}
if outBytes, e = or.Encode(pack); e != nil {
or.LogError(e)
} else if outBytes != nil {
outBatch = append(outBatch, outBytes...)
msgCounter++
}
pack.Recycle()
// Trigger immediately when the message count threshold has been
// reached if a) the "OR" operator is in effect or b) the
// flushInterval is 0 or c) the flushInterval has already elapsed.
// at least once since the last flush.
if msgCounter >= o.FlushCount {
if !o.flushOpAnd || o.FlushInterval == 0 || intervalElapsed {
// This will block until the other side is ready to accept
// this batch, freeing us to start on the next one.
o.batchChan <- outBatch
outBatch = <-o.backChan
msgCounter = 0
intervalElapsed = false
if timer != nil {
timer.Reset(timerDuration)
}
}
}
case <-o.timerChan:
if (o.flushOpAnd && msgCounter >= o.FlushCount) ||
(!o.flushOpAnd && msgCounter > 0) {
// This will block until the other side is ready to accept
// this batch, freeing us to start on the next one.
o.batchChan <- outBatch
outBatch = <-o.backChan
msgCounter = 0
intervalElapsed = false
} else {
intervalElapsed = true
}
timer.Reset(timerDuration)
}
}
wg.Done()
}
// Runs in a separate goroutine, waits for buffered data on the committer
// channel, writes it out to the filesystem, and puts the now empty buffer on
// the return channel for reuse.
func (o *FileOutput) committer(or OutputRunner, wg *sync.WaitGroup) {
initBatch := make([]byte, 0, 10000)
o.backChan <- initBatch
var outBatch []byte
var err error
ok := true
hupChan := make(chan interface{})
notify.Start(RELOAD, hupChan)
for ok {
select {
case outBatch, ok = <-o.batchChan:
if !ok {
// Channel is closed => we're shutting down, exit cleanly.
break
}
if err = o.tryRotateFile(); err != nil {
panic(fmt.Sprintf("FileOutput unable to rotate file '%s': %s",
o.path, err))
}
n, err := o.file.Write(outBatch)
if err != nil {
or.LogError(fmt.Errorf("Can't write to %s: %s", o.path, err))
} else if n != len(outBatch) {
or.LogError(fmt.Errorf("Truncated output for %s", o.path))
} else {
o.file.Sync()
}
outBatch = outBatch[:0]
o.backChan <- outBatch
case <-hupChan:
o.file.Close()
if err = o.openFile(); err != nil {
// TODO: Need a way to handle this gracefully, see
// https://github.com/mozilla-services/heka/issues/38
panic(fmt.Sprintf("FileOutput unable to reopen file '%s': %s",
o.path, err))
}
}
}
o.file.Close()
wg.Done()
}
func init() {
RegisterPlugin("FileOutput", func() interface{} {
return new(FileOutput)
})
}