summaryrefslogtreecommitdiff
path: root/internal/io/fs/readfile_processor_optimized.go
blob: 2e880e7f9d536824118363803b781430c948c9c3 (plain)
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
package fs

import (
	"bufio"
	"bytes"
	"context"
	"io"
	"os"
	"time"

	"github.com/mimecast/dtail/internal/io/dlog"
	"github.com/mimecast/dtail/internal/io/line"
	"github.com/mimecast/dtail/internal/io/pool"
	"github.com/mimecast/dtail/internal/lcontext"
	"github.com/mimecast/dtail/internal/regex"
)

// readWithProcessorOptimized reads from the file using buffered line reading
// instead of byte-by-byte reading for better performance
func (f *readFile) readWithProcessorOptimized(ctx context.Context, fd *os.File, reader *bufio.Reader,
	truncate <-chan struct{}, ltx lcontext.LContext, processor line.Processor, re regex.Regex) error {

	// Create a line filter processor that wraps the given processor
	filterProcessor := &filteringProcessor{
		processor: processor,
		re:        re,
		ltx:       ltx,
		stats:     &f.stats,
		globID:    f.globID,
	}

	// Use a scanner for efficient line reading
	scanner := bufio.NewScanner(reader)

	// Get a buffer from the pool instead of allocating a new one
	bufPtr := pool.GetScannerBuffer()
	buf := *bufPtr
	maxTokenSize := 1024 * 1024 // 1MB max token size
	scanner.Buffer(buf, maxTokenSize)

	// Ensure we return the buffer to the pool when done
	defer pool.PutScannerBuffer(bufPtr)

	// Use custom split function that preserves line endings
	scanner.Split(f.scanLinesPreserveEndings)

	// Track truncation checks
	lastTruncateCheck := time.Now()
	truncateCheckInterval := 3 * time.Second

	lineCount := 0
	for scanner.Scan() {
		// Check context cancellation
		select {
		case <-ctx.Done():
			return nil
		default:
		}

		// Check for file truncation periodically
		if time.Since(lastTruncateCheck) > truncateCheckInterval {
			select {
			case <-truncate:
				if isTruncated, err := f.truncated(fd); isTruncated {
					return err
				}
			default:
			}
			lastTruncateCheck = time.Now()
		}

		// Get the line data
		lineData := scanner.Bytes()

		// Get a buffer from the pool and copy the data
		lineBuf := pool.BytesBuffer.Get().(*bytes.Buffer)
		lineBuf.Write(lineData)

		// Process the line
		f.updatePosition()
		if err := filterProcessor.ProcessFilteredLine(lineBuf); err != nil {
			return err
		}

		lineCount++
	}

	// Check for scanner errors
	if err := scanner.Err(); err != nil {
		// Handle EOF specially for tailing
		if err == io.EOF && f.seekEOF {
			// For tail mode, we want to keep reading
			return nil
		}
		return err
	}

	return nil
}

// scanLinesPreserveEndings is a custom split function that preserves original line endings
// and respects MaxLineLength
func (f *readFile) scanLinesPreserveEndings(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}

	maxLineLen := f.lineLimit()

	// Look for a newline
	if i := bytes.IndexByte(data, '\n'); i >= 0 {
		// Check if the line before the newline exceeds max length
		if i > maxLineLen {
			// Line is too long, split it at maxLineLen
			// In turbo mode, we handle long lines silently
			return maxLineLen, data[0:maxLineLen], nil
		}

		// Line is within limit, include the line ending in the token
		// Check if there's a \r before the \n
		if i > 0 && data[i-1] == '\r' {
			// Windows line ending (\r\n) - include both in token
			return i + 1, data[0 : i+1], nil
		}
		// Unix line ending (\n) - include it in token
		return i + 1, data[0 : i+1], nil
	}

	// If we're at EOF, we have a final, non-terminated line
	if atEOF {
		if len(data) > maxLineLen {
			// Even at EOF, respect max line length
			// In turbo mode, we handle long lines silently
			return maxLineLen, data[0:maxLineLen], nil
		}
		return len(data), data, nil
	}

	// If the line is too long, split it
	if len(data) >= maxLineLen {
		// Return a chunk up to MaxLineLength
		// In turbo mode, we handle long lines silently
		return maxLineLen, data[0:maxLineLen], nil
	}

	// Request more data
	return 0, nil, nil
}

// scanLinesWithMaxLength is a custom split function for bufio.Scanner that respects MaxLineLength
func (f *readFile) scanLinesWithMaxLength(data []byte, atEOF bool) (advance int, token []byte, err error) {
	if atEOF && len(data) == 0 {
		return 0, nil, nil
	}

	maxLineLen := f.lineLimit()

	// Look for a newline
	if i := bytes.IndexByte(data, '\n'); i >= 0 {
		// Check if the line before the newline exceeds max length
		if i > maxLineLen {
			// Line is too long, split it at maxLineLen
			if !f.warnedAboutLongLine {
				if f.serverMessages != nil {
					f.serverMessages <- dlog.Common.Warn(f.filePath,
						"Long log line, splitting into multiple lines") + "\n"
				}
				f.warnedAboutLongLine = true
			}
			return maxLineLen, data[0:maxLineLen], nil
		}
		// We have a full line within the limit
		f.warnedAboutLongLine = false // Reset warning for next long line sequence
		return i + 1, data[0:i], nil
	}

	// If we're at EOF, we have a final, non-terminated line
	if atEOF {
		if len(data) > maxLineLen {
			// Even at EOF, respect max line length
			if !f.warnedAboutLongLine {
				if f.serverMessages != nil {
					f.serverMessages <- dlog.Common.Warn(f.filePath,
						"Long log line, splitting into multiple lines") + "\n"
				}
				f.warnedAboutLongLine = true
			}
			return maxLineLen, data[0:maxLineLen], nil
		}
		return len(data), data, nil
	}

	// If the line is too long, split it
	if len(data) >= maxLineLen {
		// Warn about long line (only once)
		if !f.warnedAboutLongLine {
			f.serverMessages <- dlog.Common.Warn(f.filePath,
				"Long log line, splitting into multiple lines") + "\n"
			f.warnedAboutLongLine = true
		}

		// Return a chunk up to MaxLineLength
		return maxLineLen, data[0:maxLineLen], nil
	}

	// Request more data
	return 0, nil, nil
}

// StartWithProcessorOptimized starts reading a log file using an optimized LineProcessor implementation.
// This version uses buffered line reading instead of byte-by-byte reading.
func (f *readFile) StartWithProcessorOptimized(ctx context.Context, ltx lcontext.LContext,
	processor line.Processor, re regex.Regex) error {

	reader, fd, decompressor, err := f.makeReader()
	if fd != nil {
		defer fd.Close()
	}
	if decompressor != nil {
		defer func() {
			if closeErr := decompressor.Close(); closeErr != nil {
				dlog.Common.Warn(f.filePath, "Unable to close compressed reader", closeErr)
			}
		}()
	}
	if err != nil {
		return err
	}

	// Create a cancelable context for the truncate check goroutine
	truncateCtx, cancelTruncate := context.WithCancel(ctx)
	defer cancelTruncate()

	truncate := make(chan struct{})

	go f.periodicTruncateCheck(truncateCtx, truncate)

	// For tail mode, we need to handle continuous reading
	if f.seekEOF {
		return f.tailWithProcessorOptimized(ctx, fd, reader, truncate, ltx, processor, re)
	}

	// For cat/grep mode, just read once
	err = f.readWithProcessorOptimized(ctx, fd, reader, truncate, ltx, processor, re)

	// Ensure any buffered data is flushed
	if flushErr := processor.Flush(); flushErr != nil && err == nil {
		err = flushErr
	}

	return err
}

// tailWithProcessorOptimized handles continuous reading for tail mode
func (f *readFile) tailWithProcessorOptimized(ctx context.Context, fd *os.File, reader *bufio.Reader,
	truncate <-chan struct{}, ltx lcontext.LContext, processor line.Processor, re regex.Regex) error {

	// Create a line filter processor
	filterProcessor := &filteringProcessor{
		processor: processor,
		re:        re,
		ltx:       ltx,
		stats:     &f.stats,
		globID:    f.globID,
	}

	// Buffer for partial lines
	partialLine := pool.BytesBuffer.Get().(*bytes.Buffer)
	defer pool.RecycleBytesBuffer(partialLine)

	// Get a buffer from the pool for reading
	bufPtr := pool.GetMediumBuffer()
	defer pool.PutMediumBuffer(bufPtr)

	for {
		// Read available data using pooled buffer
		buf := (*bufPtr)[:cap(*bufPtr)] // Reset to full capacity
		n, err := reader.Read(buf)

		if n > 0 {
			// Process the data we read
			data := buf[:n]

			// Process complete lines
			for len(data) > 0 {
				// Find newline
				idx := bytes.IndexByte(data, '\n')

				if idx >= 0 {
					// Complete line found
					partialLine.Write(data[:idx])

					// Process the line if it's not empty
					if partialLine.Len() > 0 {
						f.updatePosition()
						lineBuf := pool.BytesBuffer.Get().(*bytes.Buffer)
						lineBuf.Write(partialLine.Bytes())

						if err := filterProcessor.ProcessFilteredLine(lineBuf); err != nil {
							return err
						}
					}

					partialLine.Reset()
					data = data[idx+1:]

					// Reset long line warning
					f.warnedAboutLongLine = false
				} else {
					// No newline, add to partial line
					partialLine.Write(data)

					// Check if line is too long
					if partialLine.Len() >= f.lineLimit() {
						if !f.warnedAboutLongLine {
							f.serverMessages <- dlog.Common.Warn(f.filePath,
								"Long log line, splitting into multiple lines") + "\n"
							f.warnedAboutLongLine = true
						}

						// Process the partial line
						f.updatePosition()
						lineBuf := pool.BytesBuffer.Get().(*bytes.Buffer)
						lineBuf.Write(partialLine.Bytes())

						if err := filterProcessor.ProcessFilteredLine(lineBuf); err != nil {
							return err
						}

						partialLine.Reset()
					}

					break
				}
			}

			// Flush processor periodically
			if err := processor.Flush(); err != nil {
				return err
			}
		}

		// Handle read errors
		if err != nil {
			if err != io.EOF {
				return err
			}

			// EOF handling
			select {
			case <-ctx.Done():
				return nil
			case <-truncate:
				if isTruncated, err := f.truncated(fd); isTruncated {
					return err
				}
			case <-time.After(100 * time.Millisecond):
				// Continue reading after a short delay
			}
		}

		// Check for cancellation
		select {
		case <-ctx.Done():
			// Process any remaining partial line
			if partialLine.Len() > 0 {
				f.updatePosition()
				lineBuf := pool.BytesBuffer.Get().(*bytes.Buffer)
				lineBuf.Write(partialLine.Bytes())
				if err := filterProcessor.ProcessFilteredLine(lineBuf); err != nil {
					return err
				}
			}
			return nil
		default:
		}
	}
}