-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathdebug_merge_logs.go
655 lines (612 loc) · 15.5 KB
/
debug_merge_logs.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
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
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the CockroachDB Software License
// included in the /LICENSE file.
package cli
import (
"bufio"
"bytes"
"container/heap"
"context"
"io"
"io/fs"
"os"
"path/filepath"
"regexp"
"sort"
"sync"
"time"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/log/logpb"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/ttycolor"
"golang.org/x/sync/errgroup"
)
type logStream interface {
fileInfo() *fileInfo // FileInfo for the current entry available in peek.
peek() (logpb.Entry, bool)
pop() (logpb.Entry, bool) // If called after peek, must return the same values.
error() error
}
// writeLogStream pops messages off of s and writes them to out prepending
// prefix per message and filtering messages which match filter.
func writeLogStream(
s logStream,
out io.Writer,
filter *regexp.Regexp,
keepRedactable bool,
cp ttycolor.Profile,
tenantIDsFilter []string,
) error {
const chanSize = 1 << 16 // 64k
const maxWriteBufSize = 1 << 18 // 256kB
type entryInfo struct {
logpb.Entry
*fileInfo
}
tenantIDFilterSet := make(map[string]struct{}, len(tenantIDsFilter))
for _, tID := range tenantIDsFilter {
tenantIDFilterSet[tID] = struct{}{}
}
render := func(ei entryInfo, w io.Writer) (err error) {
// TODO(postamar): add support for other output formats
// Currently, `render` applies the `crdb-v1-tty` format regardless of the
// output logging format defined for the stderr sink. It should instead
// apply the selected output format.
err = log.FormatLegacyEntryPrefix(ei.prefix, w, cp)
if err != nil {
return err
}
return log.FormatLegacyEntryWithOptionalColors(ei.Entry, w, cp)
}
g, ctx := errgroup.WithContext(context.Background())
entryChan := make(chan entryInfo, chanSize) // read -> bufferWrites
writeChan := make(chan *bytes.Buffer) // bufferWrites -> write
read := func() error {
defer close(entryChan)
for e, ok := s.peek(); ok; e, ok = s.peek() {
select {
case entryChan <- entryInfo{Entry: e, fileInfo: s.fileInfo()}:
case <-ctx.Done():
return nil
}
s.pop()
}
return s.error()
}
bufferWrites := func() error {
defer close(writeChan)
writing, pending := &bytes.Buffer{}, &bytes.Buffer{}
for {
send, recv := writeChan, entryChan
var scratch []byte
if pending.Len() == 0 {
send = nil
if recv == nil {
return nil
}
} else if pending.Len() > maxWriteBufSize {
recv = nil
}
select {
case ei, open := <-recv:
if !open {
entryChan = nil
break
}
if len(tenantIDsFilter) != 0 {
if _, ok := tenantIDFilterSet[ei.TenantID]; !ok {
break
}
}
startLen := pending.Len()
if err := render(ei, pending); err != nil {
return err
}
if filter != nil {
matches := filter.FindSubmatch(pending.Bytes()[startLen:])
if matches == nil {
// Did not match.
pending.Truncate(startLen)
} else if len(matches) > 1 {
// Matched and there are capturing groups (if there
// aren't any we'll want to print the whole message). We
// only want to print what was captured. This is mildly
// awkward since we can't output anything directly, so
// we write the submatches back into the buffer and then
// discard what we've looked at so far.
//
// NB: it's tempting to try to write into `pending`
// directly, and while it's probably safe to do so
// (despite truncating before writing back from the
// truncated buffer into itself), it's not worth the
// complexity in this code. Just use a scratch buffer.
scratch = scratch[:0]
for _, b := range matches[1:] {
scratch = append(scratch, b...)
}
pending.Truncate(startLen)
_, _ = pending.Write(scratch)
pending.WriteByte('\n')
}
}
case send <- pending:
writing.Reset()
pending, writing = writing, pending
case <-ctx.Done():
return nil
}
}
}
write := func() error {
for buf := range writeChan {
if _, err := out.Write(buf.Bytes()); err != nil {
return err
}
}
return nil
}
g.Go(read)
g.Go(bufferWrites)
g.Go(write)
return g.Wait()
}
// mergedStream is a merged heap of log streams.
type mergedStream []logStream
// newMergedStreamFromPatterns creates a new logStream by first glob
// expanding pattern, then filtering for matching files which conform to the
// filePattern and if program is non-nil, they match the program which is
// extracted from matching files via the named capture group with the name
// "program". The returned stream will only return log entries in [from, to].
// If no program capture group exists all files match.
func newMergedStreamFromPatterns(
ctx context.Context,
patterns []string,
filePattern, programFilter *regexp.Regexp,
from, to time.Time,
editMode log.EditSensitiveData,
format string,
prefixer filePrefixer,
) (logStream, error) {
paths, err := expandPatterns(patterns)
if err != nil {
return nil, err
}
files, err := findLogFiles(
paths, filePattern, programFilter, groupIndex(filePattern, "program"),
)
if err != nil {
return nil, err
}
prefixer.PopulatePrefixes(files)
return newMergedStream(ctx, files, from, to, editMode, format)
}
func groupIndex(re *regexp.Regexp, groupName string) int {
for i, n := range re.SubexpNames() {
if n == groupName {
return i
}
}
return -1
}
func newMergedStream(
ctx context.Context,
files []fileInfo,
from, to time.Time,
editMode log.EditSensitiveData,
format string,
) (*mergedStream, error) {
// TODO(ajwerner): think about clock movement and PID
const maxConcurrentFiles = 256 // should be far less than the FD limit
sem := make(chan struct{}, maxConcurrentFiles)
res := make(mergedStream, len(files))
g, _ := errgroup.WithContext(ctx)
createFileStream := func(i int) func() error {
return func() error {
sem <- struct{}{}
defer func() { <-sem }()
s, err := newFileLogStream(files[i], from, to, editMode, format)
if s != nil {
res[i] = s
}
return err
}
}
for i := range files {
g.Go(createFileStream(i))
}
if err := g.Wait(); err != nil {
return nil, err
}
g, ctx = errgroup.WithContext(ctx)
filtered := res[:0] // filter nil streams
for _, s := range res {
if s != nil {
s = newBufferedLogStream(ctx, g, s)
filtered = append(filtered, s)
}
}
res = filtered
heap.Init(&res)
return &res, nil
}
func (l mergedStream) Len() int { return len(l) }
func (l mergedStream) Swap(i, j int) { l[i], l[j] = l[j], l[i] }
func (l mergedStream) Less(i, j int) bool {
ie, iok := l[i].peek()
je, jok := l[j].peek()
if iok && jok {
return ie.Time < je.Time
}
return !iok && jok
}
func (l *mergedStream) Push(s interface{}) {
*l = append(*l, s.(logStream))
}
func (l *mergedStream) Pop() (v interface{}) {
n := len(*l) - 1
v = (*l)[n]
*l = (*l)[:n]
return
}
func (l *mergedStream) peek() (logpb.Entry, bool) {
if len(*l) == 0 {
return logpb.Entry{}, false
}
return (*l)[0].peek()
}
func (l *mergedStream) pop() (logpb.Entry, bool) {
e, ok := l.peek()
if !ok {
return logpb.Entry{}, false
}
s := (*l)[0]
s.pop()
if _, stillOk := s.peek(); stillOk {
heap.Push(l, heap.Pop(l))
} else if err := s.error(); err != nil && err != io.EOF {
return logpb.Entry{}, false
} else {
heap.Pop(l)
}
return e, true
}
func (l *mergedStream) fileInfo() *fileInfo {
if len(*l) == 0 {
return nil
}
return (*l)[0].fileInfo()
}
func (l *mergedStream) error() error {
if len(*l) == 0 {
return nil
}
return (*l)[0].error()
}
func expandPatterns(patterns []string) ([]string, error) {
var paths []string
for _, p := range patterns {
matches, err := filepath.Glob(p)
if err != nil {
return nil, err
}
paths = append(paths, matches...)
}
return removeDuplicates(paths), nil
}
func removeDuplicates(strings []string) (filtered []string) {
filtered = strings[:0]
prev := ""
for _, s := range strings {
if s == prev {
continue
}
filtered = append(filtered, s)
prev = s
}
return filtered
}
func getLogFileInfo(path string, filePattern *regexp.Regexp) (fileInfo, bool) {
if matches := filePattern.FindStringSubmatchIndex(path); matches != nil {
return fileInfo{path: path, matches: matches, pattern: filePattern}, true
}
return fileInfo{}, false
}
type fileInfo struct {
path string
prefix []byte
pattern *regexp.Regexp
matches []int
}
func findLogFiles(
paths []string, filePattern, programFilter *regexp.Regexp, programGroup int,
) ([]fileInfo, error) {
if programGroup == 0 || programFilter == nil {
programGroup = 0
}
var files []fileInfo
for _, p := range paths {
// NB: come go1.16, we should use WalkDir here as it is more efficient.
if err := filepath.WalkDir(p, func(p string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
if d.IsDir() {
// Don't act on the directory itself, Walk will visit it for us.
return nil
}
// We're looking at a file.
fi, ok := getLogFileInfo(p, filePattern)
if !ok {
return nil
}
if programGroup > 0 {
program := fi.path[fi.matches[2*programGroup]:fi.matches[2*programGroup+1]]
if !programFilter.MatchString(program) {
return nil
}
}
files = append(files, fi)
return nil
}); err != nil {
return nil, err
}
}
return files, nil
}
// newBufferedLogStream creates a new logStream which will create a goroutine to
// pop off of s and buffer in memory up to readChanSize entries.
// The channel and goroutine are created lazily upon the first read after the
// first call to pop. The initial peek does not consume resources.
func newBufferedLogStream(ctx context.Context, g *errgroup.Group, s logStream) logStream {
bs := &bufferedLogStream{ctx: ctx, logStream: s, g: g}
// Fill the initial entry value to prevent initial peek from running the
// goroutine.
bs.e, bs.ok = s.peek()
bs.read = true
s.pop()
return bs
}
type bufferedLogStream struct {
logStream
runOnce sync.Once
e logpb.Entry
read bool
ok bool
c chan logpb.Entry
ctx context.Context
g *errgroup.Group
}
func (bs *bufferedLogStream) run() {
const readChanSize = 512
bs.c = make(chan logpb.Entry, readChanSize)
bs.g.Go(func() error {
defer close(bs.c)
for {
e, ok := bs.logStream.pop()
if !ok {
if err := bs.error(); err != io.EOF {
return err
}
return nil
}
select {
case bs.c <- e:
case <-bs.ctx.Done():
return nil
}
}
})
}
func (bs *bufferedLogStream) peek() (logpb.Entry, bool) {
if bs.ok && !bs.read {
if bs.c == nil { // indicates that run has not been called
bs.runOnce.Do(bs.run)
}
bs.e, bs.ok = <-bs.c
bs.read = true
}
if !bs.ok {
return logpb.Entry{}, false
}
return bs.e, true
}
func (bs *bufferedLogStream) pop() (logpb.Entry, bool) {
e, ok := bs.peek()
bs.read = false
return e, ok
}
// fileLogStream represents a logStream from a single file.
type fileLogStream struct {
from, to time.Time
prevTime int64
fi fileInfo
f *os.File
d log.EntryDecoder
read bool
editMode log.EditSensitiveData
format string
e logpb.Entry
err error
}
// newFileLogStream constructs a *fileLogStream and then peeks at the file to
// ensure that it contains a valid entry after from. If the file contains only
// data which precedes from, (nil, nil) is returned. If an io error is
// encountered during the initial peek, that error is returned. The underlying
// file is always closed before returning from this constructor so the initial
// peek does not consume resources.
func newFileLogStream(
fi fileInfo, from, to time.Time, editMode log.EditSensitiveData, format string,
) (logStream, error) {
s := &fileLogStream{
fi: fi,
from: from,
to: to,
editMode: editMode,
format: format,
}
if _, ok := s.peek(); !ok {
if err := s.error(); err != io.EOF {
return nil, err
}
return nil, nil
}
s.close()
return s, nil
}
func (s *fileLogStream) close() {
s.f.Close()
s.f = nil
s.d = nil
}
func (s *fileLogStream) open() bool {
const readBufSize = 1024
if s.f, s.err = os.Open(s.fi.path); s.err != nil {
return false
}
if s.format == "" {
if _, s.format, s.err = log.ReadFormatFromLogFile(s.f); s.err != nil {
return false
}
if _, s.err = s.f.Seek(0, io.SeekStart); s.err != nil {
return false
}
}
if s.err = seekToFirstAfterFrom(s.f, s.from, s.editMode, s.format); s.err != nil {
return false
}
if s.d, s.err = log.NewEntryDecoderWithFormat(bufio.NewReaderSize(s.f, readBufSize), s.editMode, s.format); s.err != nil {
return false
}
return true
}
func (s *fileLogStream) peek() (logpb.Entry, bool) {
for !s.read && s.err == nil {
justOpened := false
if s.d == nil {
if !s.open() {
return logpb.Entry{}, false
}
justOpened = true
}
var e logpb.Entry
if s.err = s.d.Decode(&e); s.err != nil {
s.close()
s.e = logpb.Entry{}
break
}
// Upon re-opening the file, we'll read s.e again.
if justOpened && e == s.e {
continue
}
s.e = e
if s.e.Time < s.prevTime {
s.e.Time = s.prevTime
} else {
s.prevTime = s.e.Time
}
afterTo := !s.to.IsZero() && s.e.Time > s.to.UnixNano()
if afterTo {
s.close()
s.e = logpb.Entry{}
s.err = io.EOF
} else {
beforeFrom := !s.from.IsZero() && s.e.Time < s.from.UnixNano()
s.read = !beforeFrom
}
}
return s.e, s.err == nil
}
func (s *fileLogStream) pop() (e logpb.Entry, ok bool) {
if e, ok = s.peek(); !ok {
return
}
s.read = false
return e, ok
}
func (s *fileLogStream) fileInfo() *fileInfo { return &s.fi }
func (s *fileLogStream) error() error { return s.err }
// seekToFirstAfterFrom uses binary search to seek to an offset after all
// entries which occur before from.
func seekToFirstAfterFrom(
f *os.File, from time.Time, editMode log.EditSensitiveData, format string,
) (err error) {
if from.IsZero() {
return nil
}
fi, err := f.Stat()
if err != nil {
return err
}
size := fi.Size()
defer func() {
if r := recover(); r != nil {
err = r.(error)
}
}()
offset := sort.Search(int(size), func(i int) bool {
if _, err := f.Seek(int64(i), io.SeekStart); err != nil {
panic(err)
}
var e logpb.Entry
d, err := log.NewEntryDecoderWithFormat(f, editMode, format)
if err != nil {
panic(errors.WithMessagef(err, "error while processing file %s", f.Name()))
}
if err := d.Decode(&e); err != nil {
if err == io.EOF {
return true
}
panic(errors.WithMessagef(err, "error while processing file %s", f.Name()))
}
return e.Time >= from.UnixNano()
})
if _, err := f.Seek(int64(offset), io.SeekStart); err != nil {
return err
}
var e logpb.Entry
d, err := log.NewEntryDecoderWithFormat(f, editMode, format)
if err != nil {
return err
}
if err := d.Decode(&e); err != nil {
return err
}
_, err = f.Seek(int64(offset), io.SeekStart)
return err
}
type forceColor int
const (
forceColorAuto forceColor = iota
forceColorOn
forceColorOff
)
// Type implements the pflag.Value interface.
func (c *forceColor) Type() string { return "<true/false/auto>" }
// String implements the pflag.Value interface.
func (c *forceColor) String() string {
switch *c {
case forceColorAuto:
return "auto"
case forceColorOn:
return "true"
case forceColorOff:
return "false"
default:
panic(errors.AssertionFailedf("unknown value: %v", int(*c)))
}
}
// Set implements the pflag.Value interface.
func (c *forceColor) Set(v string) error {
switch v {
case "on", "true":
*c = forceColorOn
case "off", "false":
*c = forceColorOff
case "auto":
*c = forceColorAuto
default:
return errors.Newf("unknown value: %v (supported: true/false/auto)", v)
}
return nil
}