-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathingest_file.go
129 lines (117 loc) · 3.25 KB
/
ingest_file.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
/*
* Copyright (C) 2021 IBM, Inc.
*
* Licensed 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 ingest
import (
"bufio"
"fmt"
"os"
"time"
"github.com/netobserv/flowlogs-pipeline/pkg/config"
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline/decode"
"github.com/netobserv/flowlogs-pipeline/pkg/pipeline/utils"
log "github.com/sirupsen/logrus"
)
type ingestFile struct {
params config.Ingest
decoder decode.Decoder
exitChan <-chan struct{}
PrevRecords []config.GenericMap
TotalRecords int
}
const (
delaySeconds = 10
chunkLines = 100
)
// Ingest ingests entries from a file and resends the same data every delaySeconds seconds
func (ingestF *ingestFile) Ingest(out chan<- config.GenericMap) {
var filename string
if ingestF.params.File != nil {
filename = ingestF.params.File.Filename
}
var lines [][]byte
file, err := os.Open(filename)
if err != nil {
log.Fatal(err)
}
defer func() {
_ = file.Close()
}()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
text := scanner.Text()
log.Debugf("%s", text)
lines = append(lines, []byte(text))
}
log.Debugf("Ingesting %d log lines from %s", len(lines), filename)
switch ingestF.params.Type {
case "file":
ingestF.sendAllLines(lines, out)
case "file_loop":
// loop forever
ticker := time.NewTicker(time.Duration(delaySeconds) * time.Second)
for {
select {
case <-ingestF.exitChan:
log.Debugf("exiting ingestFile because of signal")
return
case <-ticker.C:
ingestF.sendAllLines(lines, out)
}
}
case "file_chunks":
// sends the lines in chunks. Useful for testing parallelization
ingestF.TotalRecords = len(lines)
for len(lines) > 0 {
if len(lines) > chunkLines {
ingestF.sendAllLines(lines[:chunkLines], out)
lines = lines[chunkLines:]
} else {
ingestF.sendAllLines(lines, out)
lines = nil
}
}
}
}
func (ingestF *ingestFile) sendAllLines(lines [][]byte, out chan<- config.GenericMap) {
log.Debugf("ingestFile sending %d lines", len(lines))
ingestF.TotalRecords = len(lines)
for _, line := range lines {
decoded, err := ingestF.decoder.Decode(line)
if err != nil {
log.WithError(err).Warnf("ignoring line")
continue
}
out <- decoded
}
}
// NewIngestFile create a new ingester
func NewIngestFile(params config.StageParam) (Ingester, error) {
log.Debugf("entering NewIngestFile")
if params.Ingest == nil || params.Ingest.File == nil || params.Ingest.File.Filename == "" {
return nil, fmt.Errorf("ingest filename not specified")
}
log.Debugf("input file name = %s", params.Ingest.File.Filename)
decoder, err := decode.GetDecoder(params.Ingest.File.Decoder)
if err != nil {
return nil, err
}
return &ingestFile{
params: *params.Ingest,
exitChan: utils.ExitChannel(),
decoder: decoder,
}, nil
}