-
Notifications
You must be signed in to change notification settings - Fork 367
/
aggregator_context.go
216 lines (191 loc) · 6.47 KB
/
aggregator_context.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
// Copyright 2021 iLogtail Authors
//
// 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 context
import (
"strings"
"sync"
"time"
"github.com/alibaba/ilogtail/pkg/pipeline"
"github.com/alibaba/ilogtail/pkg/protocol"
"github.com/alibaba/ilogtail/pkg/util"
)
const (
MaxLogCount = 1024
MaxLogGroupSize = 3 * 1024 * 1024
SmallPackIDTimeout = time.Duration(24*30) * time.Hour
BigPackIDTimeout = time.Duration(24) * time.Hour
PackIDMapLenThresh = 100000
)
type LogPackSeqInfo struct {
seq int64
lastUpdateTime time.Time
}
type AggregatorContext struct {
MaxLogGroupCount int // the maximum log group count per source to trigger flush operation
MaxLogCount int // the maximum log in a log group
Topic string // the output topic
ContextPreservationToleranceSize int // the maximum number of log source per config where logGroupPoolMap will not be cleared periodically
PackFlag bool // whether to add __pack_id__ as a tag
lock *sync.Mutex
logGroupPoolMap map[string][]*protocol.LogGroup
nowLogGroupSizeMap map[string]int
packIDMap map[string]*LogPackSeqInfo
defaultPack string
context pipeline.Context
queue pipeline.LogGroupQueue
packIDMapCleanInterval time.Duration
packIDTimeout time.Duration
lastCleanPackIDMapTime time.Time
}
// Init method would be trigger before working.
// 1. context store the metadata of this Logstore config
// 2. que is a transfer channel for flushing LogGroup when reaches the maximum in the cache.
func (p *AggregatorContext) Init(context pipeline.Context, que pipeline.LogGroupQueue) (int, error) {
p.defaultPack = util.NewPackIDPrefix(context.GetConfigName())
p.context = context
p.queue = que
p.lastCleanPackIDMapTime = time.Now()
return 0, nil
}
func (*AggregatorContext) Description() string {
return "context aggregator for logtail"
}
// Add adds @log with @ctx to aggregator.
func (p *AggregatorContext) Add(log *protocol.Log, ctx map[string]interface{}) error {
p.lock.Lock()
defer p.lock.Unlock()
source := p.defaultPack
if _, ok := ctx["source"]; ok {
source = ctx["source"].(string)
if !strings.HasSuffix(source, "-") {
source += "-"
}
}
topic := p.Topic
if _, ok := ctx["topic"]; ok {
topic = ctx["topic"].(string)
}
logGroupList := p.logGroupPoolMap[source]
if logGroupList == nil {
logGroupList = make([]*protocol.LogGroup, 0, p.MaxLogGroupCount)
}
if len(logGroupList) == 0 {
logGroupList = append(logGroupList, p.newLogGroup(source, topic))
}
nowLogGroup := logGroupList[len(logGroupList)-1]
logSize := p.evaluateLogSize(log)
// When current log group is full (log count or no more capacity for current log),
// allocate a new log group.
if len(nowLogGroup.Logs) >= p.MaxLogCount || p.nowLogGroupSizeMap[source]+logSize > MaxLogGroupSize {
// The number of log group exceeds limit, make a quick flush.
if len(logGroupList) == p.MaxLogGroupCount {
// Quick flush to avoid becoming bottleneck when large logs come.
if err := p.queue.Add(logGroupList[0]); err == nil {
// add success, remove head log group
logGroupList = logGroupList[1:]
} else {
return err
}
}
// New log group, reset size.
p.nowLogGroupSizeMap[source] = 0
logGroupList = append(logGroupList, p.newLogGroup(source, topic))
nowLogGroup = logGroupList[len(logGroupList)-1]
}
// add log size
p.nowLogGroupSizeMap[source] += logSize
nowLogGroup.Logs = append(nowLogGroup.Logs, log)
p.logGroupPoolMap[source] = logGroupList
return nil
}
// Flush ...
func (p *AggregatorContext) Flush() []*protocol.LogGroup {
p.lock.Lock()
defer p.lock.Unlock()
var ret []*protocol.LogGroup
for pack, logGroupList := range p.logGroupPoolMap {
if len(logGroupList) == 0 {
continue
}
ret = append(ret, logGroupList...)
delete(p.logGroupPoolMap, pack)
delete(p.nowLogGroupSizeMap, pack)
}
curTime := time.Now()
if len(p.packIDMap) > p.ContextPreservationToleranceSize && time.Since(p.lastCleanPackIDMapTime) > p.packIDMapCleanInterval {
if len(p.packIDMap) > PackIDMapLenThresh {
p.packIDTimeout = BigPackIDTimeout
} else {
p.packIDTimeout = SmallPackIDTimeout
}
for k, v := range p.packIDMap {
if curTime.Sub(v.lastUpdateTime) > p.packIDTimeout {
delete(p.packIDMap, k)
}
}
p.lastCleanPackIDMapTime = curTime
}
return ret
}
// Reset ...
func (p *AggregatorContext) Reset() {
p.lock.Lock()
defer p.lock.Unlock()
p.logGroupPoolMap = make(map[string][]*protocol.LogGroup)
}
func (p *AggregatorContext) newLogGroup(pack string, topic string) *protocol.LogGroup {
logGroup := &protocol.LogGroup{
Logs: make([]*protocol.Log, 0, p.MaxLogCount),
Topic: topic,
}
info := p.packIDMap[pack]
if info == nil {
info = &LogPackSeqInfo{
seq: 1,
}
}
if p.PackFlag {
logGroup.LogTags = append(logGroup.LogTags, util.NewLogTagForPackID(pack, &info.seq))
}
info.lastUpdateTime = time.Now()
p.packIDMap[pack] = info
return logGroup
}
func (*AggregatorContext) evaluateLogSize(log *protocol.Log) int {
var logSize = 6
for _, logC := range log.Contents {
logSize += 5 + len(logC.Key) + len(logC.Value)
}
return logSize
}
// NewAggregatorContext create a default aggregator with default value.
func NewAggregatorContext() *AggregatorContext {
return &AggregatorContext{
MaxLogGroupCount: 2,
MaxLogCount: MaxLogCount,
ContextPreservationToleranceSize: 10,
PackFlag: true,
logGroupPoolMap: make(map[string][]*protocol.LogGroup),
nowLogGroupSizeMap: make(map[string]int),
packIDMap: make(map[string]*LogPackSeqInfo),
packIDMapCleanInterval: time.Duration(600) * time.Second,
lock: &sync.Mutex{},
}
}
// Register the plugin to the Aggregators array.
func init() {
pipeline.Aggregators["aggregator_context"] = func() pipeline.Aggregator {
return NewAggregatorContext()
}
}