Merge pull request #13200 from bboreham/wlog-defer

tsdb/wlog: close segment files sooner
pull/14611/head^2
Bryan Boreham 2024-08-13 14:11:38 +01:00 committed by GitHub
commit aa4b056ad0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 4 additions and 3 deletions

View File

@ -689,10 +689,11 @@ func (w *Watcher) readCheckpoint(checkpointDir string, readFn segmentReadFn) err
if err != nil { if err != nil {
return fmt.Errorf("unable to open segment: %w", err) return fmt.Errorf("unable to open segment: %w", err)
} }
defer sr.Close()
r := NewLiveReader(w.logger, w.readerMetrics, sr) r := NewLiveReader(w.logger, w.readerMetrics, sr)
if err := readFn(w, r, index, false); err != nil && !errors.Is(err, io.EOF) { err = readFn(w, r, index, false)
sr.Close()
if err != nil && !errors.Is(err, io.EOF) {
return fmt.Errorf("readSegment: %w", err) return fmt.Errorf("readSegment: %w", err)
} }

View File

@ -230,11 +230,11 @@ func TestTailSamples(t *testing.T) {
for i := first; i <= last; i++ { for i := first; i <= last; i++ {
segment, err := OpenReadSegment(SegmentName(watcher.walDir, i)) segment, err := OpenReadSegment(SegmentName(watcher.walDir, i))
require.NoError(t, err) require.NoError(t, err)
defer segment.Close()
reader := NewLiveReader(nil, NewLiveReaderMetrics(nil), segment) reader := NewLiveReader(nil, NewLiveReaderMetrics(nil), segment)
// Use tail true so we can ensure we got the right number of samples. // Use tail true so we can ensure we got the right number of samples.
watcher.readSegment(reader, i, true) watcher.readSegment(reader, i, true)
require.NoError(t, segment.Close())
} }
expectedSeries := seriesCount expectedSeries := seriesCount