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
|
package server
import (
"context"
"os"
"strings"
"time"
"github.com/mimecast/dtail/internal/config"
"github.com/mimecast/dtail/internal/io/line"
"github.com/mimecast/dtail/internal/io/logger"
"github.com/mimecast/dtail/internal/mapr"
"github.com/mimecast/dtail/internal/mapr/logformat"
)
// Aggregate is for aggregating mapreduce data on the DTail server side.
type Aggregate struct {
// Log lines to process (parsing MAPREDUCE lines).
Lines chan line.Line
// Hostname of the current server (used to populate $hostname field).
hostname string
// Signals to serialize data.
serialize chan struct{}
// Signals to flush data.
flush chan struct{}
// The mapr query
query *mapr.Query
// The mapr log format parser
parser *logformat.Parser
cancel context.CancelFunc
ctx context.Context
}
// NewAggregate return a new server side aggregator.
func NewAggregate(queryStr string) (*Aggregate, error) {
query, err := mapr.NewQuery(queryStr)
if err != nil {
return nil, err
}
fqdn, err := os.Hostname()
if err != nil {
logger.Error(err)
}
s := strings.Split(fqdn, ".")
parserName := config.Server.MapreduceLogFormat
if query.Table == "" {
parserName = "generic"
}
logger.Info("Creating mapr log format parser", parserName)
logParser, err := logformat.NewParser(parserName)
if err != nil {
logger.FatalExit("Could not create mapr log format parser", err)
}
ctx, cancel := context.WithCancel(context.Background())
a := Aggregate{
Lines: make(chan line.Line, 100),
serialize: make(chan struct{}),
flush: make(chan struct{}),
hostname: s[0],
query: query,
parser: logParser,
ctx: ctx,
cancel: cancel,
}
return &a, nil
}
// Start an aggregation.
func (a *Aggregate) Start(ctx context.Context, maprLines chan<- string) {
defer a.cancel()
fieldsCh := a.linesToFields(ctx)
go a.fieldsToMaprLines(ctx, fieldsCh, maprLines)
a.periodicAggregateTimer(ctx)
}
// Cancel the aggregation.
func (a *Aggregate) Cancel() {
a.cancel()
}
func (a *Aggregate) periodicAggregateTimer(ctx context.Context) {
for {
select {
case <-time.After(a.query.Interval):
a.Serialize(ctx)
case <-ctx.Done():
return
case <-a.ctx.Done():
return
}
}
}
func (a *Aggregate) linesToFields(ctx context.Context) <-chan map[string]string {
fieldsCh := make(chan map[string]string)
go func() {
defer close(fieldsCh)
for {
select {
case line, ok := <-a.Lines:
if !ok {
return
}
maprLine := strings.TrimSpace(string(line.Content))
fields, err := a.parser.MakeFields(maprLine)
if err != nil {
logger.Error(err)
continue
}
if !a.query.WhereClause(fields) {
continue
}
select {
case fieldsCh <- fields:
case <-ctx.Done():
}
case <-ctx.Done():
return
case <-a.ctx.Done():
return
}
}
}()
return fieldsCh
}
func (a *Aggregate) fieldsToMaprLines(ctx context.Context, fieldsCh <-chan map[string]string, maprLines chan<- string) {
group := mapr.NewGroupSet()
serialize := func() {
logger.Info("Serializing mapreduce result")
group.Serialize(ctx, maprLines)
group = mapr.NewGroupSet()
logger.Info("Done serializing mapreduce result")
}
for {
select {
case fields, ok := <-fieldsCh:
if !ok {
serialize()
return
}
a.aggregate(group, fields)
case <-a.serialize:
serialize()
case <-a.flush:
logger.Info("Flushing mapreduce result")
serialize()
a.flush <- struct{}{}
logger.Info("Done flushing mapreduce result")
case <-ctx.Done():
return
case <-a.ctx.Done():
logger.Info("Flushing mapreduce result")
serialize()
a.flush <- struct{}{}
logger.Info("Done flushing mapreduce result")
return
}
}
}
func (a *Aggregate) aggregate(group *mapr.GroupSet, fields map[string]string) {
//logger.Trace("Aggregating", group, fields)
var sb strings.Builder
for i, field := range a.query.GroupBy {
if i > 0 {
sb.WriteString(" ")
}
if val, ok := fields[field]; ok {
sb.WriteString(val)
}
}
groupKey := sb.String()
set := group.GetSet(groupKey)
var addedSample bool
for _, sc := range a.query.Select {
if val, ok := fields[sc.Field]; ok {
/*
if sc.Field == "$line" {
// Complete log line as to arrive untouched on the client side.
val = base64.StdEncoding.EncodeToString([]byte(val))
}
*/
if err := set.Aggregate(sc.FieldStorage, sc.Operation, val, false); err != nil {
logger.Error(err)
continue
}
addedSample = true
}
}
if addedSample {
set.Samples++
return
}
logger.Trace("Aggregated data locally without adding new samples")
}
// Serialize all the aggregated data.
func (a *Aggregate) Serialize(ctx context.Context) {
select {
case a.serialize <- struct{}{}:
case <-ctx.Done():
}
}
// Flush all data.
func (a *Aggregate) Flush() {
select {
case <-a.ctx.Done():
return
case a.flush <- struct{}{}:
case <-time.After(time.Minute):
return
}
select {
case <-a.flush:
case <-time.After(time.Minute):
}
}
|