-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathfswatch.go
More file actions
661 lines (569 loc) · 20.3 KB
/
Copy pathfswatch.go
File metadata and controls
661 lines (569 loc) · 20.3 KB
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
// Licensed to Elasticsearch B.V. under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. Elasticsearch B.V. licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package filestream
import (
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"hash"
"io"
"os"
"path/filepath"
"sync"
"sync/atomic"
"time"
"github.com/elastic/go-concert/unison"
"github.com/elastic/beats/v7/filebeat/input/file"
loginp "github.com/elastic/beats/v7/filebeat/input/filestream/internal/input-logfile"
commonfile "github.com/elastic/beats/v7/libbeat/common/file"
"github.com/elastic/beats/v7/libbeat/common/match"
"github.com/elastic/elastic-agent-libs/logp"
)
const (
RecursiveGlobDepth = 8
DefaultFingerprintSize int64 = 1024 // 1KB
scannerDebugKey = "scanner"
watcherDebugKey = "file_watcher"
)
var (
errFileTooSmall = errors.New("file size is too small for ingestion")
errFileEmpty = errors.New("file is empty")
)
type fileWatcherConfig struct {
// Interval is the time between two scans.
Interval time.Duration `config:"check_interval"`
// ResendOnModTime if a file has been changed according to modtime but the size is the same
// it is still considered truncation.
ResendOnModTime bool `config:"resend_on_touch"`
// Scanner is the configuration of the scanner.
Scanner fileScannerConfig `config:",inline"`
}
// fileWatcher gets the list of files from a FSWatcher and creates events by
// comparing the files between its last two runs.
type fileWatcher struct {
cfg fileWatcherConfig
prev map[string]loginp.FileDescriptor
scanner loginp.FSScanner
log *logp.Logger
events chan loginp.FSEvent
notifyChan chan loginp.HarvesterStatus
fileIdentifier fileIdentifier
sourceIdentifier *loginp.SourceIdentifier
// closedHarvesters is a map of harvester ID to the current
// offset of the file
closedHarvesters map[string]int64
// closedHarvestersMutex controls access to closedHarvesters
closedHarvestersMutex sync.Mutex
}
// Ensure fileWatcher implements loginp.FSWatcher
var _ loginp.FSWatcher = &fileWatcher{}
func newFileWatcher(
logger *logp.Logger,
paths []string,
config fileWatcherConfig,
fi fileIdentifier,
srci *loginp.SourceIdentifier,
) (*fileWatcher, error) {
scanner, err := newFileScanner(logger, paths, config.Scanner)
if err != nil {
return nil, err
}
return &fileWatcher{
log: logger.Named(watcherDebugKey),
cfg: config,
prev: make(map[string]loginp.FileDescriptor, 0),
scanner: scanner,
events: make(chan loginp.FSEvent),
closedHarvesters: map[string]int64{},
// notifyChan is a buffered channel to prevent the harvester from
// blocking while waiting for the fileWatcher to read from the channel
notifyChan: make(chan loginp.HarvesterStatus, 5), // magic number
fileIdentifier: fi,
sourceIdentifier: srci,
}, nil
}
func defaultFileWatcherConfig() fileWatcherConfig {
return fileWatcherConfig{
Interval: 10 * time.Second,
ResendOnModTime: false,
Scanner: defaultFileScannerConfig(),
}
}
func (w *fileWatcher) NotifyChan() chan loginp.HarvesterStatus {
return w.notifyChan
}
func (w *fileWatcher) Run(ctx unison.Canceler) {
defer close(w.events)
// run initial scan before starting regular
w.watch(ctx)
// Read from notifyChan in a separate goroutine becase
// there are cases when w.watch can take minutes or even
// hours, so we do not want to block the harvesters
go func() {
for {
select {
case evt := <-w.notifyChan:
w.processNotification(evt)
case <-ctx.Done():
return
}
}
}()
tick := time.Tick(w.cfg.Interval)
for {
select {
case <-tick:
w.watch(ctx)
case <-ctx.Done():
return
}
}
}
func (w *fileWatcher) processNotification(evt loginp.HarvesterStatus) {
w.log.Debugf("Harvester Closed notification received. ID: %s, Size: %d", evt.ID, evt.Size)
w.closedHarvestersMutex.Lock()
w.closedHarvesters[evt.ID] = evt.Size
w.closedHarvestersMutex.Unlock()
}
func (w *fileWatcher) watch(ctx unison.Canceler) {
w.log.Debug("Start next scan")
paths := w.scanner.GetFiles()
// for debugging purposes
writtenCount := 0
truncatedCount := 0
renamedCount := 0
removedCount := 0
createdCount := 0
newFilesByName := make(map[string]*loginp.FileDescriptor)
newFilesByID := make(map[string]*loginp.FileDescriptor)
for path, fd := range paths {
// if the scanner found a new path or an existing path
// with a different file, it is a new file
prevDesc, ok := w.prev[path]
sfd := fd // to avoid memory aliasing
if !ok || !loginp.SameFile(&prevDesc, &sfd) {
newFilesByName[path] = &sfd
newFilesByID[fd.FileID()] = &sfd
continue
}
// srcID is the file identity (harvester ID/registry key), resolved lazily via
// ensureSrcID: an unchanged file that produces no event never needs one, saving a
// per-file identity allocation on every idle scan.
var srcID string
ensureSrcID := func() string {
if srcID == "" { // getFileIdentity never returns ""
srcID = w.getFileIdentity(fd)
}
return srcID
}
// closedHarvesters is empty in the steady state, so this reconciliation is usually
// skipped and the file identity is never resolved.
if w.hasClosedHarvesters() {
w.reconcileClosedHarvester(&prevDesc, ensureSrcID())
}
var e loginp.FSEvent
switch {
// the new size is smaller, the file was truncated
case prevDesc.Info.Size() > fd.Info.Size():
e = truncateEvent(path, fd, ensureSrcID())
truncatedCount++
// the size is the same, timestamps are different, the file was touched
case prevDesc.Info.Size() == fd.Info.Size() && prevDesc.Info.ModTime() != fd.Info.ModTime():
if w.cfg.ResendOnModTime {
e = truncateEvent(path, fd, ensureSrcID())
truncatedCount++
}
// the new size is larger, something was written.
// If a harvester for this file was closed recently,
// we use its state instead of the one we have cached.
case prevDesc.SizeOrBytesIngested() < fd.Info.Size():
e = writeEvent(path, fd, ensureSrcID())
writtenCount++
}
// if none of the conditions were true, the file remained unchanged and we don't need to create an event
if e.Op != loginp.OpDone {
select {
case <-ctx.Done():
return
case w.events <- e:
}
}
// delete from previous state to mark that we've seen the existing file again
delete(w.prev, path)
}
// remaining files in the prev map are the ones that are missing
// either because they have been deleted or renamed
for remainingPath, remainingDesc := range w.prev {
var e loginp.FSEvent
id := remainingDesc.FileID()
srcID := w.getFileIdentity(remainingDesc)
if newDesc, renamed := newFilesByID[id]; renamed {
e = renamedEvent(remainingPath, newDesc.Filename, *newDesc, srcID)
delete(newFilesByName, newDesc.Filename)
delete(newFilesByID, id)
renamedCount++
} else {
e = deleteEvent(remainingPath, remainingDesc, srcID)
removedCount++
w.closedHarvestersMutex.Lock()
delete(w.closedHarvesters, srcID)
w.closedHarvestersMutex.Unlock()
}
select {
case <-ctx.Done():
return
case w.events <- e:
}
}
// remaining files in newFiles are newly created files
for path, fd := range newFilesByName {
select {
case <-ctx.Done():
return
case w.events <- createEvent(path, *fd, w.getFileIdentity(*fd)):
createdCount++
}
}
w.log.Debugw("File scan complete",
"total", len(paths),
"written", writtenCount,
"truncated", truncatedCount,
"renamed", renamedCount,
"removed", removedCount,
"created", createdCount,
)
w.prev = paths
}
// hasClosedHarvesters reports whether any harvester-close notification awaits reconciliation.
// It is a cheap, lock-guarded length check so the watch loop can skip resolving a file identity
// for every scanned file when there is nothing to reconcile (the common steady-state case).
func (w *fileWatcher) hasClosedHarvesters() bool {
w.closedHarvestersMutex.Lock()
defer w.closedHarvestersMutex.Unlock()
return len(w.closedHarvesters) > 0
}
// reconcileClosedHarvester folds a recently-closed harvester's ingested offset (from
// closedHarvesters) into prevDesc so a restarted harvester resumes from the right position,
// then drops the entry.
//
// This prevents a race condition: when the reader/harvester reaches EOF it blocks on a backoff.
// If during this time [logFile.shouldBeClosed] marks the file inactive and closes the reader
// context, once the backoff expires the reader and harvester are closed without ingesting any
// more data. If the fileWatcher sends a write event while the harvester was blocked, no new
// harvester is started because one is already running, yet the fileWatcher updates its internal
// state and won't send further write events until more data is added, so some lines can be
// missed. By applying the offset reported when the harvester closed we realign our state and
// start a new harvester if needed.
func (w *fileWatcher) reconcileClosedHarvester(prevDesc *loginp.FileDescriptor, id string) {
w.closedHarvestersMutex.Lock()
defer w.closedHarvestersMutex.Unlock()
size, ok := w.closedHarvesters[id]
if !ok {
return
}
w.log.Debugf("Updating previous state because harvester was closed. '%s': %d", id, size)
prevDesc.SetBytesIngested(size)
delete(w.closedHarvesters, id)
}
// getFileIdentity mimics the same algorithm used by the harvester to generate
// the file identity to any given file.
// See 'startHarvester' on internal/input-logfile/harvester.go.
func (w *fileWatcher) getFileIdentity(d loginp.FileDescriptor) string {
src := w.fileIdentifier.GetSource(loginp.FSEvent{Descriptor: d})
return w.sourceIdentifier.ID(src)
}
func createEvent(path string, fd loginp.FileDescriptor, srcID string) loginp.FSEvent {
return loginp.FSEvent{Op: loginp.OpCreate, OldPath: "", NewPath: path, Descriptor: fd, SrcID: srcID}
}
func writeEvent(path string, fd loginp.FileDescriptor, srcID string) loginp.FSEvent {
return loginp.FSEvent{Op: loginp.OpWrite, OldPath: path, NewPath: path, Descriptor: fd, SrcID: srcID}
}
func truncateEvent(path string, fd loginp.FileDescriptor, srcID string) loginp.FSEvent {
return loginp.FSEvent{Op: loginp.OpTruncate, OldPath: path, NewPath: path, Descriptor: fd, SrcID: srcID}
}
func renamedEvent(oldPath, path string, fd loginp.FileDescriptor, srcID string) loginp.FSEvent {
return loginp.FSEvent{Op: loginp.OpRename, OldPath: oldPath, NewPath: path, Descriptor: fd, SrcID: srcID}
}
func deleteEvent(path string, fd loginp.FileDescriptor, srcID string) loginp.FSEvent {
return loginp.FSEvent{Op: loginp.OpDelete, OldPath: path, NewPath: "", Descriptor: fd, SrcID: srcID}
}
func (w *fileWatcher) Event() loginp.FSEvent {
return <-w.events
}
func (w *fileWatcher) GetFiles() map[string]loginp.FileDescriptor {
return w.scanner.GetFiles()
}
type fingerprintConfig struct {
Enabled bool `config:"enabled"`
Offset int64 `config:"offset"`
Length int64 `config:"length"`
}
type fileScannerConfig struct {
ExcludedFiles []match.Matcher `config:"exclude_files"`
IncludedFiles []match.Matcher `config:"include_files"`
Symlinks bool `config:"symlinks"`
RecursiveGlob bool `config:"recursive_glob"`
Fingerprint fingerprintConfig `config:"fingerprint"`
}
func defaultFileScannerConfig() fileScannerConfig {
return fileScannerConfig{
Symlinks: false,
RecursiveGlob: true,
Fingerprint: fingerprintConfig{
Enabled: false,
Offset: 0,
Length: DefaultFingerprintSize,
},
}
}
// fileScanner looks for files which match the patterns in paths.
// It is able to exclude files and symlinks.
type fileScanner struct {
smallFilesWarned atomic.Bool
paths []string
cfg fileScannerConfig
log *logp.Logger
hasher hash.Hash
readBuffer []byte
// lastCount is the number of unique files the previous scan produced.
// It pre-sizes the per-scan maps to avoid repeated grow/rehash allocations
// on a stable file set.
lastCount int
}
func newFileScanner(logger *logp.Logger, paths []string, config fileScannerConfig) (*fileScanner, error) {
s := fileScanner{
paths: paths,
cfg: config,
log: logger.Named(scannerDebugKey),
hasher: sha256.New(),
}
if s.cfg.Fingerprint.Enabled {
if s.cfg.Fingerprint.Length < sha256.BlockSize {
err := fmt.Errorf("fingerprint size %d bytes cannot be smaller than %d bytes", config.Fingerprint.Length, sha256.BlockSize)
return nil, fmt.Errorf("error while reading configuration of fingerprint: %w", err)
}
s.log.Debugf("fingerprint mode enabled: offset %d, length %d", s.cfg.Fingerprint.Offset, s.cfg.Fingerprint.Length)
s.readBuffer = make([]byte, s.cfg.Fingerprint.Length)
}
err := s.resolveRecursiveGlobs(config)
if err != nil {
return nil, err
}
err = s.normalizeGlobPatterns()
if err != nil {
return nil, err
}
return &s, nil
}
// resolveRecursiveGlobs expands `**` from the globs in multiple patterns
func (s *fileScanner) resolveRecursiveGlobs(c fileScannerConfig) error {
if !c.RecursiveGlob {
s.log.Debug("recursive glob disabled")
return nil
}
s.log.Debug("recursive glob enabled")
var paths []string
for _, path := range s.paths {
patterns, err := file.GlobPatterns(path, RecursiveGlobDepth)
if err != nil {
return err
}
if len(patterns) > 1 {
s.log.Debugf("%q expanded to %#v", path, patterns)
}
paths = append(paths, patterns...)
}
s.paths = paths
return nil
}
// normalizeGlobPatterns calls `filepath.Abs` on all the globs from config
func (s *fileScanner) normalizeGlobPatterns() error {
paths := make([]string, len(s.paths))
for i, path := range s.paths {
pathAbs, err := filepath.Abs(path)
if err != nil {
return fmt.Errorf("failed to get the absolute path for %s: %w", path, err)
}
paths[i] = pathAbs
}
s.paths = paths
return nil
}
// GetFiles returns a map of file descriptors by filenames that
// match the configured paths.
func (s *fileScanner) GetFiles() map[string]loginp.FileDescriptor {
// Pre-size the per-scan maps from the previous scan's unique-file count.
fdByName := make(map[string]loginp.FileDescriptor, s.lastCount)
// used to determine if a symlink resolves in a already known target
uniqueIDs := make(map[string]string, s.lastCount)
// used to filter out duplicate matches
uniqueFiles := make(map[string]struct{}, s.lastCount)
for _, path := range s.paths {
matches, err := filepath.Glob(path)
if err != nil {
s.log.Errorf("glob(%s) failed: %v", path, err)
continue
}
for _, filename := range matches {
// in case multiple globs match on the same file we filter out duplicates
if _, knownFile := uniqueFiles[filename]; knownFile {
continue
}
uniqueFiles[filename] = struct{}{}
it, err := s.getIngestTarget(filename)
if err != nil {
if !errors.Is(err, errFileEmpty) {
s.log.Debugf("cannot create an ingest target for file %q: %s", filename, err)
}
continue
}
fd, err := s.toFileDescriptor(&it)
if errors.Is(err, errFileTooSmall) {
if s.smallFilesWarned.CompareAndSwap(false, true) {
s.log.Warnf("ingestion from some files will be delayed, files need to be at "+
"least %d in size for ingestion to start. To change this "+
"behaviour set 'prospector.scanner.fingerprint.length' and "+
"'prospector.scanner.fingerprint.offset'. "+
"Enable debug logging to see all file names of delayed files.",
s.cfg.Fingerprint.Offset+s.cfg.Fingerprint.Length)
}
s.log.Debugf("cannot start ingesting from file %q: %s", filename, err)
continue
}
if err != nil {
s.log.Warnf("cannot create a file descriptor for an ingest target %q: %s", filename, err)
continue
}
fileID := fd.FileID()
if knownFilename, exists := uniqueIDs[fileID]; exists {
s.log.Warnf("%q points to an already known ingest target %q [%s==%s]. Skipping", fd.Filename, knownFilename, fileID, fileID)
continue
}
uniqueIDs[fileID] = fd.Filename
fdByName[filename] = fd
}
}
s.lastCount = len(fdByName)
return fdByName
}
type ingestTarget struct {
filename string
originalFilename string
symlink bool
info commonfile.ExtendedFileInfo
}
func (s *fileScanner) getIngestTarget(filename string) (it ingestTarget, err error) {
if s.isFileExcluded(filename) {
return it, fmt.Errorf("file %q is excluded from ingestion", filename)
}
if !s.isFileIncluded(filename) {
return it, fmt.Errorf("file %q is not included in ingestion", filename)
}
it.filename = filename
it.originalFilename = filename
info, err := os.Lstat(it.filename) // to determine if it's a symlink
if err != nil {
return it, fmt.Errorf("failed to lstat %q: %w", it.filename, err)
}
if info.IsDir() {
return it, fmt.Errorf("file %q is a directory", it.filename)
}
symlink := info.Mode()&os.ModeSymlink > 0
// we don't need to process empty files
if !symlink && info.Size() == 0 {
return it, errFileEmpty
}
it.info = commonfile.ExtendFileInfo(info)
it.symlink = symlink
if it.symlink {
if !s.cfg.Symlinks {
return it, fmt.Errorf("file %q is a symlink and they're disabled", it.filename)
}
// now we know it's a symlink, we stat with link resolution
info, err := os.Stat(it.filename)
if err != nil {
return it, fmt.Errorf("failed to stat the symlink %q: %w", it.filename, err)
}
// we don't need to process empty files
if info.Size() == 0 {
return it, errFileEmpty
}
it.info = commonfile.ExtendFileInfo(info)
it.originalFilename, err = filepath.EvalSymlinks(it.filename)
if err != nil {
s.log.Debugf("finding path to original file has failed %s: %+v", it.filename, err)
it.originalFilename = it.filename
}
if s.isFileExcluded(it.originalFilename) {
return it, fmt.Errorf("file %q->%q is excluded from ingestion", it.filename, it.originalFilename)
}
if !s.isFileIncluded(it.originalFilename) {
return it, fmt.Errorf("file %q->%q is not included in ingestion", it.filename, it.originalFilename)
}
}
return it, nil
}
func (s *fileScanner) toFileDescriptor(it *ingestTarget) (fd loginp.FileDescriptor, err error) {
fd.Filename = it.filename
fd.Info = it.info
if s.cfg.Fingerprint.Enabled {
fileSize := it.info.Size()
// we should not open the file if we know it's too small
minSize := s.cfg.Fingerprint.Offset + s.cfg.Fingerprint.Length
if fileSize < minSize {
return fd, fmt.Errorf("filesize of %q is %d bytes, expected at least %d bytes for fingerprinting: %w", fd.Filename, fileSize, minSize, errFileTooSmall)
}
file, err := os.Open(it.originalFilename)
if err != nil {
return fd, fmt.Errorf("failed to open %q for fingerprinting: %w", it.originalFilename, err)
}
defer file.Close()
if s.cfg.Fingerprint.Offset != 0 {
_, err = file.Seek(s.cfg.Fingerprint.Offset, io.SeekStart)
if err != nil {
return fd, fmt.Errorf("failed to seek %q for fingerprinting: %w", fd.Filename, err)
}
}
s.hasher.Reset()
lr := io.LimitReader(file, s.cfg.Fingerprint.Length)
written, err := io.CopyBuffer(s.hasher, lr, s.readBuffer)
if err != nil {
return fd, fmt.Errorf("failed to compute hash for first %d bytes of %q: %w", s.cfg.Fingerprint.Length, fd.Filename, err)
}
if written != s.cfg.Fingerprint.Length {
return fd, fmt.Errorf("failed to read %d bytes from %q to compute fingerprint, read only %d", written, fd.Filename, s.cfg.Fingerprint.Length)
}
fd.Fingerprint = hex.EncodeToString(s.hasher.Sum(nil))
}
return fd, nil
}
func (s *fileScanner) isFileExcluded(file string) bool {
return len(s.cfg.ExcludedFiles) > 0 && s.matchAny(s.cfg.ExcludedFiles, file)
}
func (s *fileScanner) isFileIncluded(file string) bool {
return len(s.cfg.IncludedFiles) == 0 || s.matchAny(s.cfg.IncludedFiles, file)
}
// matchAny checks if the text matches any of the regular expressions
func (s *fileScanner) matchAny(matchers []match.Matcher, text string) bool {
for _, m := range matchers {
if m.MatchString(text) {
return true
}
}
return false
}