Skip to content

Introduce timed flush #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions pkg/extractor/batchers/batcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import (
// ReadAheadBufferSize is the default size of the read-ahead buffer
const ReadAheadBufferSize = 128 * 1024

// AutoFlushTimeout sets time before an auto-flushing reader will write a batch
const AutoFlushTimeout = 250 * time.Millisecond

type Batcher struct {
c chan extractor.InputBatch

Expand Down Expand Up @@ -166,3 +169,43 @@ func (s *Batcher) syncReaderToBatcher(sourceName string, reader io.Reader, batch
s.incReadBytes(readerMetrics.CountReset())
}
}

// syncReaderToBatcherWithTimeFlush is similar to `syncReaderToBatcher`, except if it gets a new line
// it will flush the batch if n time has elapsed since the last flush, irregardless of how many items are in the current batch
// Good for potentially slow or more interactive workloads (tail, stdin, etc)
func (s *Batcher) syncReaderToBatcherWithTimeFlush(sourceName string, reader io.Reader, batchSize int, autoFlush time.Duration) {
readerMetrics := newReaderMetrics(reader)
readahead := readahead.NewImmediate(readerMetrics, ReadAheadBufferSize)
readahead.OnError(func(e error) {
s.incErrors()
logger.Printf("Error reading %s: %v", sourceName, e)
})

batch := make([]extractor.BString, 0, batchSize)
var batchStart uint64 = 1
lastBatchFlush := time.Now()

for readahead.Scan() {
batch = append(batch, readahead.Bytes())
if len(batch) >= batchSize || time.Since(lastBatchFlush) >= autoFlush {
s.c <- extractor.InputBatch{
Batch: batch,
Source: sourceName,
BatchStart: batchStart,
}
batchStart += uint64(len(batch))
batch = make([]extractor.BString, 0, batchSize)

s.incReadBytes(readerMetrics.CountReset())
lastBatchFlush = time.Now()
}
}
if len(batch) > 0 {
s.c <- extractor.InputBatch{
Batch: batch,
Source: sourceName,
BatchStart: batchStart,
}
s.incReadBytes(readerMetrics.CountReset())
}
}
19 changes: 19 additions & 0 deletions pkg/extractor/batchers/batcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package batchers
import (
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -41,3 +42,21 @@ line3`
assert.Equal(t, s.errorCount, 0)
assert.Equal(t, s.ReadBytes(), uint64(17))
}

func TestBatcherWithAutoFlush(t *testing.T) {
s := newBatcher(10)

testData := `line1
line2
line3`

s.syncReaderToBatcherWithTimeFlush("string", strings.NewReader(testData), 2, 1*time.Second)

b1 := <-s.BatchChan()
b2 := <-s.BatchChan()

assert.Len(t, b1.Batch, 2)
assert.Len(t, b2.Batch, 1)
assert.Equal(t, s.errorCount, 0)
assert.Equal(t, s.ReadBytes(), uint64(17))
}
2 changes: 1 addition & 1 deletion pkg/extractor/batchers/readerBatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func OpenReaderToChan(sourceName string, reader io.ReadCloser, batchSize int) *B
defer reader.Close()
defer out.close()
out.startFileReading(sourceName)
out.syncReaderToBatcher(sourceName, reader, batchSize)
out.syncReaderToBatcherWithTimeFlush(sourceName, reader, batchSize, AutoFlushTimeout)
}()

return out
Expand Down
2 changes: 1 addition & 1 deletion pkg/extractor/batchers/tailBatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TailFilesToChan(filenames <-chan string, batchSize int, reopen, poll bool)
return
}

out.syncReaderToBatcher(filename, r, batchSize)
out.syncReaderToBatcherWithTimeFlush(filename, r, batchSize, AutoFlushTimeout)
}(filename)
}

Expand Down