/
filedest.go
263 lines (240 loc) · 5.73 KB
/
filedest.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
package dests
import (
"context"
"io"
"os"
"path/filepath"
"strings"
"sync"
"text/template"
"time"
"github.com/inconshreveable/log15"
"github.com/stephane-martin/skewer/conf"
"github.com/stephane-martin/skewer/encoders"
"github.com/stephane-martin/skewer/model"
"github.com/stephane-martin/skewer/utils"
"github.com/stephane-martin/skewer/utils/ctrie/filetrie"
"github.com/stephane-martin/skewer/utils/eerrors"
"github.com/valyala/bytebufferpool"
)
type filesMap struct {
fm *filetrie.Ctrie
}
func newFilesMap() *filesMap {
return &filesMap{fm: filetrie.New(nil)}
}
func (m *filesMap) Clear() {
m.fm.ForEach(func(e filetrie.Entry) {
if e.Value.Release() {
openedFilesGauge.Dec()
}
})
m.fm.Clear()
}
func (m *filesMap) Put(fname string, f *utils.OFile) {
f.Acquire()
m.fm.Set(fname, f)
}
func (m *filesMap) Get(fname string) *utils.OFile {
f, ok := m.fm.Lookup(fname)
if !ok {
return nil
}
f.Acquire()
return f
}
func (m *filesMap) Remove(fname string) {
f, ok := m.fm.Remove(fname)
if ok {
if f.Release() {
openedFilesGauge.Dec()
}
}
}
func (m *filesMap) ForEach(f func(string, *utils.OFile)) {
g := func(e filetrie.Entry) {
f(e.Key, e.Value)
}
m.fm.ForEach(g)
}
func (m *filesMap) Filter(f func(string, *utils.OFile) bool) (ret chan *utils.OFile) {
ret = make(chan *utils.OFile)
ch := make(chan filetrie.Entry)
g := func(e filetrie.Entry) bool {
return f(e.Key, e.Value)
}
go func() {
m.fm.Filter(g, ch)
close(ch)
}()
go func() {
for e := range ch {
ret <- e.Value
}
close(ret)
}()
return ret
}
type openedFiles struct {
files *filesMap
filesMu sync.Mutex
timeout time.Duration
logger log15.Logger
bufferSize int
flushPeriod time.Duration
syncPeriod time.Duration
gzip bool
gziplevel int
}
func newOpenedFiles(ctx context.Context, c conf.FileDestConfig, l log15.Logger) *openedFiles {
o := openedFiles{
files: newFilesMap(),
timeout: c.OpenFileTimeout,
bufferSize: c.BufferSize,
flushPeriod: c.FlushPeriod,
syncPeriod: c.SyncPeriod,
gzip: c.Gzip,
gziplevel: c.GzipLevel,
logger: l,
}
go func() {
// flush the buffers periodically
lastSync := time.Now()
for {
select {
case <-ctx.Done():
return
case <-time.After(o.flushPeriod):
}
now := time.Now()
doSync := now.Sub(lastSync) > o.syncPeriod
o.filesMu.Lock()
o.files.ForEach(func(fname string, f *utils.OFile) {
err := f.Flush()
if err != nil {
o.logger.Error("Error flushing file destination buffers. Message loss may have occurred.", "error", err)
}
// sync to disk if necessary
if doSync {
err = f.Sync()
if err != nil {
o.logger.Error("Error in sync files. Message loss may have occurred.", "error", err)
}
}
})
if doSync {
lastSync = now
}
o.filesMu.Unlock()
}
}()
go func() {
// every second we check if some opened files are inactive and need to be closed
for {
select {
case <-ctx.Done():
return
case <-time.After(time.Second):
}
o.filesMu.Lock()
for f := range o.files.Filter(func(fname string, fi *utils.OFile) bool {
return fi.Expired()
}) {
o.files.Remove(f.Name)
}
o.filesMu.Unlock()
}
}()
return &o
}
func (o *openedFiles) open(filename string) (fi *utils.OFile, err error) {
filename, err = filepath.Abs(filename)
if err != nil {
return nil, err
}
fi = o.files.Get(filename)
if fi != nil {
// fastpath
fi.Postpone(o.timeout)
return fi, nil
}
dirname := filepath.Dir(filename)
err = os.MkdirAll(dirname, 0755)
if err != nil {
return nil, err
}
o.logger.Debug("Opening file", "filename", filename)
o.filesMu.Lock()
defer o.filesMu.Unlock()
f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, err
}
openedFilesGauge.Inc()
fi = utils.NewOFile(f, filename, time.Now().Add(o.timeout), o.bufferSize, o.gzip, o.gziplevel, o.logger)
o.files.Put(filename, fi)
fi.Acquire()
return fi, nil
}
func (o *openedFiles) closeall() {
o.files.Clear()
}
type FileDestination struct {
*baseDestination
filenameTmpl *template.Template
files *openedFiles
}
func NewFileDestination(ctx context.Context, e *Env) (Destination, error) {
dest := &FileDestination{
baseDestination: newBaseDestination(conf.File, "file", e),
files: newOpenedFiles(ctx, e.config.FileDest, e.logger),
}
err := dest.setFormat(e.config.FileDest.Format)
if err != nil {
return nil, err
}
fname := e.config.FileDest.Filename
if e.confined {
fname = filepath.Join("/tmp", "filedest", fname)
}
dest.filenameTmpl, err = template.New("filename").Parse(fname)
if err != nil {
return nil, err
}
return dest, nil
}
func (d *FileDestination) sendOne(ctx context.Context, message *model.FullMessage) (err error) {
if len(message.Fields.AppName) == 0 {
message.Fields.AppName = "unknown"
}
buf := bytebufferpool.Get()
err = d.filenameTmpl.Execute(buf, message.Fields)
if err != nil {
d.logger.Warn("Error calculating filename", "error", err)
return encoders.EncodingError(err)
}
filename := strings.TrimSpace(buf.String())
bytebufferpool.Put(buf)
encoded, err := encoders.ChainEncode(d.encoder, message, "\n")
if err != nil {
d.logger.Warn("Error encoding message", "error", err)
return encoders.EncodingError(err)
}
f, err := d.files.open(filename)
if err != nil {
d.logger.Warn("Error opening file", "filename", filename, "error", err)
return err
}
_, err = io.WriteString(f, encoded)
if f.Release() {
openedFilesGauge.Dec()
}
return err
}
func (d *FileDestination) Close() error {
d.files.closeall()
return nil
}
func (d *FileDestination) Send(ctx context.Context, msgs []model.OutputMsg) (err eerrors.ErrorSlice) {
return d.ForEach(ctx, d.sendOne, true, true, msgs)
}