-
Notifications
You must be signed in to change notification settings - Fork 520
/
memory.go
332 lines (301 loc) · 8.19 KB
/
memory.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
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
// Copyright 2019 tsuru authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package applog
import (
"context"
"strings"
"sync"
"time"
"unsafe"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/tsuru/config"
"github.com/tsuru/tsuru/set"
appTypes "github.com/tsuru/tsuru/types/app"
)
const (
defaultMaxAppBufferSize = 1 * 1024 * 1024 // 1 MiB
watchBufferSize = 1000
watchWarningInterval = 30 * time.Second
baseLogSize = unsafe.Sizeof(appTypes.Applog{}) + unsafe.Sizeof(ringEntry{})
logMemorySubsytem = "logs_memory"
)
var (
logsMemoryReceived = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: promNamespace,
Subsystem: logMemorySubsytem,
Name: "received_total",
Help: "The number of in memory log entries received for processing.",
}, []string{"app"})
logsMemoryEvicted = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: promNamespace,
Subsystem: logMemorySubsytem,
Name: "evicted_total",
Help: "The number of in memory log entries removed due to full buffer.",
}, []string{"app"})
logsMemoryDroppedWatch = promauto.NewCounterVec(prometheus.CounterOpts{
Namespace: promNamespace,
Subsystem: logMemorySubsytem,
Name: "watch_dropped_total",
Help: "The number of messages dropped in watchers due to a slow client.",
}, []string{"app"})
logsMemorySize = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: promNamespace,
Subsystem: logMemorySubsytem,
Name: "size",
Help: "The size in bytes for in memory log entries of a given app.",
}, []string{"app"})
logsMemoryLength = promauto.NewGaugeVec(prometheus.GaugeOpts{
Namespace: promNamespace,
Subsystem: logMemorySubsytem,
Name: "length",
Help: "The number of in memory log entries for a given app.",
}, []string{"app"})
)
type memoryLogService struct {
bufferMap sync.Map
}
func memoryAppLogService() (appTypes.AppLogService, error) {
return &memoryLogService{}, nil
}
func (s *memoryLogService) Enqueue(entry *appTypes.Applog) error {
buffer := s.getAppBuffer(entry.Name)
buffer.add(entry)
return nil
}
func (s *memoryLogService) Add(appName, message, source, unit string) error {
messages := strings.Split(message, "\n")
logs := make([]*appTypes.Applog, 0, len(messages))
for _, msg := range messages {
if msg != "" {
l := &appTypes.Applog{
Date: time.Now().In(time.UTC),
Message: msg,
Source: source,
Name: appName,
Unit: unit,
}
logs = append(logs, l)
}
}
if len(logs) == 0 {
return nil
}
for _, log := range logs {
err := s.Enqueue(log)
if err != nil {
return err
}
}
return nil
}
func (s *memoryLogService) List(ctx context.Context, args appTypes.ListLogArgs) ([]appTypes.Applog, error) {
if args.Name == "" {
return nil, errors.New("app name required to list logs")
}
if args.Limit < 0 {
return []appTypes.Applog{}, nil
}
buffer := s.getAppBuffer(args.Name)
return buffer.list(args), nil
}
func (s *memoryLogService) Watch(ctx context.Context, args appTypes.ListLogArgs) (appTypes.LogWatcher, error) {
buffer := s.getAppBuffer(args.Name)
watcher := &memoryWatcher{
buffer: buffer,
ch: make(chan appTypes.Applog, watchBufferSize),
quit: make(chan struct{}),
wg: &sync.WaitGroup{},
nextNotify: time.NewTimer(0),
filter: args,
unitsSet: set.FromSlice(args.Units),
}
buffer.addWatcher(watcher)
return watcher, nil
}
func (s *memoryLogService) getAppBuffer(appName string) *appLogBuffer {
// Use a simple Load first to avoid unnecessary allocations and the common
// case is Load being successful.
buffer, ok := s.bufferMap.Load(appName)
if !ok {
buffer, _ = s.bufferMap.LoadOrStore(appName, &appLogBuffer{
appName: appName,
receivedCounter: logsMemoryReceived.WithLabelValues(appName),
evictedCounter: logsMemoryEvicted.WithLabelValues(appName),
droppedCounter: logsMemoryDroppedWatch.WithLabelValues(appName),
sizeGauge: logsMemorySize.WithLabelValues(appName),
lengthGauge: logsMemoryLength.WithLabelValues(appName),
bufferMaxSize: appBufferSize(),
})
}
return buffer.(*appLogBuffer)
}
func appBufferSize() uint {
bufferSize, _ := config.GetUint("log:app-log-memory-buffer-bytes")
if bufferSize == 0 {
bufferSize = defaultMaxAppBufferSize
}
return bufferSize
}
type ringEntry struct {
log *appTypes.Applog
size uint
next, prev *ringEntry
}
type appLogBuffer struct {
mu sync.RWMutex
appName string
size uint
length int
bufferMaxSize uint
start, end *ringEntry
watchers []*memoryWatcher
receivedCounter prometheus.Counter
evictedCounter prometheus.Counter
droppedCounter prometheus.Counter
sizeGauge prometheus.Gauge
lengthGauge prometheus.Gauge
}
func (b *appLogBuffer) list(args appTypes.ListLogArgs) []appTypes.Applog {
b.mu.RLock()
defer b.mu.RUnlock()
if b.length == 0 {
return []appTypes.Applog{}
}
if args.Limit == 0 || b.length < args.Limit {
args.Limit = b.length
}
logs := make([]appTypes.Applog, args.Limit)
var count int
unitsSet := set.FromSlice(args.Units)
for current := b.end; count < args.Limit; {
if (args.Source == "" || (args.Source == current.log.Source) != args.InvertSource) &&
(len(args.Units) == 0 || unitsSet.Includes(current.log.Unit)) {
logs[len(logs)-count-1] = *current.log
count++
}
current = current.prev
if current == b.end {
break
}
}
return logs[len(logs)-count:]
}
func (b *appLogBuffer) add(entry *appTypes.Applog) {
b.mu.Lock()
defer b.mu.Unlock()
b.receivedCounter.Inc()
next := &ringEntry{
log: entry,
size: entrySize(entry),
}
if next.size > b.bufferMaxSize {
return
}
if b.start == nil {
b.start = next
b.end = next
}
next.next = b.start
next.prev = b.end
b.start.prev = next
b.end.next = next
b.end = b.end.next
b.length++
newFullSize := b.size + next.size
for newFullSize > b.bufferMaxSize {
newFullSize -= b.start.size
b.start = b.start.next
b.start.prev = b.end
b.end.next = b.start
b.length--
b.evictedCounter.Inc()
}
b.size = newFullSize
b.sizeGauge.Set(float64(b.size))
b.lengthGauge.Set(float64(b.length))
for _, w := range b.watchers {
w.notify(entry, b.droppedCounter)
}
}
func (b *appLogBuffer) addWatcher(watcher *memoryWatcher) {
b.mu.Lock()
defer b.mu.Unlock()
b.watchers = append(b.watchers, watcher)
}
func (b *appLogBuffer) removeWatcher(watcher *memoryWatcher) bool {
b.mu.Lock()
defer b.mu.Unlock()
for i := range b.watchers {
if b.watchers[i] == watcher {
b.watchers[i] = b.watchers[len(b.watchers)-1]
b.watchers = b.watchers[:len(b.watchers)-1]
return true
}
}
return false
}
func entrySize(entry *appTypes.Applog) uint {
return uint(len(entry.Name) +
len(entry.Message) +
len(entry.MongoID) +
len(entry.Source) +
len(entry.Unit) +
int(baseLogSize))
}
type memoryWatcher struct {
buffer *appLogBuffer
ch chan appTypes.Applog
quit chan struct{}
wg *sync.WaitGroup
nextNotify *time.Timer
filter appTypes.ListLogArgs
unitsSet set.Set
}
func (w *memoryWatcher) notify(entry *appTypes.Applog, dropCounter prometheus.Counter) {
if w.filter.Source != "" && ((w.filter.Source != entry.Source) != w.filter.InvertSource) {
return
}
if len(w.filter.Units) > 0 && !w.unitsSet.Includes(entry.Unit) {
return
}
select {
case w.ch <- *entry:
default:
dropCounter.Inc()
select {
case <-w.nextNotify.C:
w.wg.Add(1)
go func() {
defer w.wg.Done()
select {
case w.ch <- slowWatcherWarning(entry.Name):
case <-w.quit:
}
w.nextNotify.Reset(watchWarningInterval)
}()
default:
}
}
}
func (w *memoryWatcher) Chan() <-chan appTypes.Applog {
return w.ch
}
func (w *memoryWatcher) Close() {
if w.buffer.removeWatcher(w) {
close(w.quit)
w.wg.Wait()
close(w.ch)
}
}
func slowWatcherWarning(appName string) appTypes.Applog {
return appTypes.Applog{
Name: appName,
Date: time.Now(),
Message: "Log messages dropped due to slow tail client or too many messages being produced.",
Source: "tsuru",
Unit: "api",
}
}