2019-02-19 06:33:57 -08:00
|
|
|
// Copyright 2019 The Prometheus Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2022-10-10 08:08:46 -07:00
|
|
|
package wlog
|
2019-02-19 06:33:57 -08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2023-08-14 14:42:02 -07:00
|
|
|
"crypto/rand"
|
2019-02-19 06:33:57 -08:00
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
|
|
|
"hash/crc32"
|
|
|
|
"io"
|
2023-08-14 14:42:02 -07:00
|
|
|
"math/big"
|
2019-02-19 06:33:57 -08:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-06-11 09:17:59 -07:00
|
|
|
"github.com/go-kit/log"
|
2020-10-29 02:43:23 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-10-22 02:00:08 -07:00
|
|
|
|
2019-08-13 01:34:14 -07:00
|
|
|
tsdb_errors "github.com/prometheus/prometheus/tsdb/errors"
|
2019-08-14 02:07:02 -07:00
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
2019-02-19 06:33:57 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
type reader interface {
|
|
|
|
Next() bool
|
|
|
|
Err() error
|
|
|
|
Record() []byte
|
|
|
|
Offset() int64
|
|
|
|
}
|
|
|
|
|
2019-09-19 02:15:41 -07:00
|
|
|
type rec struct {
|
2019-02-19 06:33:57 -08:00
|
|
|
t recType
|
|
|
|
b []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
var readerConstructors = map[string]func(io.Reader) reader{
|
|
|
|
"Reader": func(r io.Reader) reader {
|
|
|
|
return NewReader(r)
|
|
|
|
},
|
|
|
|
"LiveReader": func(r io.Reader) reader {
|
2019-07-03 05:10:10 -07:00
|
|
|
lr := NewLiveReader(log.NewNopLogger(), NewLiveReaderMetrics(nil), r)
|
2019-02-19 06:33:57 -08:00
|
|
|
lr.eofNonErr = true
|
|
|
|
return lr
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-10-22 01:06:44 -07:00
|
|
|
var (
|
|
|
|
data = make([]byte, 100000)
|
|
|
|
testReaderCases = []struct {
|
|
|
|
t []rec
|
|
|
|
exp [][]byte
|
|
|
|
fail bool
|
|
|
|
}{
|
|
|
|
// Sequence of valid records.
|
|
|
|
{
|
|
|
|
t: []rec{
|
|
|
|
{recFull, data[0:200]},
|
|
|
|
{recFirst, data[200:300]},
|
|
|
|
{recLast, data[300:400]},
|
|
|
|
{recFirst, data[400:800]},
|
|
|
|
{recMiddle, data[800:900]},
|
|
|
|
{recPageTerm, make([]byte, pageSize-900-recordHeaderSize*5-1)}, // exactly lines up with page boundary.
|
|
|
|
{recLast, data[900:900]},
|
|
|
|
{recFirst, data[900:1000]},
|
|
|
|
{recMiddle, data[1000:1200]},
|
|
|
|
{recMiddle, data[1200:30000]},
|
|
|
|
{recMiddle, data[30000:30001]},
|
|
|
|
{recMiddle, data[30001:30001]},
|
|
|
|
{recLast, data[30001:32000]},
|
|
|
|
},
|
|
|
|
exp: [][]byte{
|
|
|
|
data[0:200],
|
|
|
|
data[200:400],
|
|
|
|
data[400:900],
|
|
|
|
data[900:32000],
|
|
|
|
},
|
2019-02-19 06:33:57 -08:00
|
|
|
},
|
2021-10-22 01:06:44 -07:00
|
|
|
// Exactly at the limit of one page minus the header size
|
|
|
|
{
|
|
|
|
t: []rec{
|
|
|
|
{recFull, data[0 : pageSize-recordHeaderSize]},
|
|
|
|
},
|
|
|
|
exp: [][]byte{
|
|
|
|
data[:pageSize-recordHeaderSize],
|
|
|
|
},
|
2019-02-19 06:33:57 -08:00
|
|
|
},
|
2021-10-22 01:06:44 -07:00
|
|
|
// More than a full page, this exceeds our buffer and can never happen
|
|
|
|
// when written by the WAL.
|
|
|
|
{
|
|
|
|
t: []rec{
|
|
|
|
{recFull, data[0 : pageSize+1]},
|
|
|
|
},
|
|
|
|
fail: true,
|
2019-02-19 06:33:57 -08:00
|
|
|
},
|
2021-10-22 01:06:44 -07:00
|
|
|
// Two records the together are too big for a page.
|
|
|
|
// NB currently the non-live reader succeeds on this. I think this is a bug.
|
|
|
|
// but we've seen it in production.
|
|
|
|
{
|
|
|
|
t: []rec{
|
|
|
|
{recFull, data[:pageSize/2]},
|
|
|
|
{recFull, data[:pageSize/2]},
|
|
|
|
},
|
|
|
|
exp: [][]byte{
|
|
|
|
data[:pageSize/2],
|
|
|
|
data[:pageSize/2],
|
|
|
|
},
|
2019-02-19 06:33:57 -08:00
|
|
|
},
|
2021-10-22 01:06:44 -07:00
|
|
|
// Invalid orders of record types.
|
|
|
|
{
|
|
|
|
t: []rec{{recMiddle, data[:200]}},
|
|
|
|
fail: true,
|
2019-02-19 06:33:57 -08:00
|
|
|
},
|
2021-10-22 01:06:44 -07:00
|
|
|
{
|
|
|
|
t: []rec{{recLast, data[:200]}},
|
|
|
|
fail: true,
|
2019-02-19 06:33:57 -08:00
|
|
|
},
|
2021-10-22 01:06:44 -07:00
|
|
|
{
|
|
|
|
t: []rec{
|
|
|
|
{recFirst, data[:200]},
|
|
|
|
{recFull, data[200:400]},
|
|
|
|
},
|
|
|
|
fail: true,
|
2019-02-19 06:33:57 -08:00
|
|
|
},
|
2021-10-22 01:06:44 -07:00
|
|
|
{
|
|
|
|
t: []rec{
|
|
|
|
{recFirst, data[:100]},
|
|
|
|
{recMiddle, data[100:200]},
|
|
|
|
{recFull, data[200:400]},
|
|
|
|
},
|
|
|
|
fail: true,
|
2019-02-19 06:33:57 -08:00
|
|
|
},
|
2021-10-22 01:06:44 -07:00
|
|
|
// Non-zero data after page termination.
|
|
|
|
{
|
|
|
|
t: []rec{
|
|
|
|
{recFull, data[:100]},
|
|
|
|
{recPageTerm, append(make([]byte, pageSize-recordHeaderSize-102), 1)},
|
|
|
|
},
|
|
|
|
exp: [][]byte{data[:100]},
|
|
|
|
fail: true,
|
2019-02-19 06:33:57 -08:00
|
|
|
},
|
2021-10-22 01:06:44 -07:00
|
|
|
}
|
|
|
|
)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
|
|
|
func encodedRecord(t recType, b []byte) []byte {
|
|
|
|
if t == recPageTerm {
|
|
|
|
return append([]byte{0}, b...)
|
|
|
|
}
|
|
|
|
r := make([]byte, recordHeaderSize)
|
|
|
|
r[0] = byte(t)
|
|
|
|
binary.BigEndian.PutUint16(r[1:], uint16(len(b)))
|
|
|
|
binary.BigEndian.PutUint32(r[3:], crc32.Checksum(b, castagnoliTable))
|
|
|
|
return append(r, b...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TestReader feeds the reader a stream of encoded records with different types.
|
|
|
|
func TestReader(t *testing.T) {
|
|
|
|
for name, fn := range readerConstructors {
|
|
|
|
for i, c := range testReaderCases {
|
|
|
|
t.Run(fmt.Sprintf("%s/%d", name, i), func(t *testing.T) {
|
|
|
|
var buf []byte
|
|
|
|
for _, r := range c.t {
|
|
|
|
buf = append(buf, encodedRecord(r.t, r.b)...)
|
|
|
|
}
|
|
|
|
r := fn(bytes.NewReader(buf))
|
|
|
|
|
|
|
|
for j := 0; r.Next(); j++ {
|
|
|
|
t.Logf("record %d", j)
|
|
|
|
rec := r.Record()
|
|
|
|
|
2021-09-02 08:43:54 -07:00
|
|
|
require.Less(t, j, len(c.exp), "received more records than expected")
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Equal(t, c.exp[j], rec, "Bytes within record did not match expected Bytes")
|
2019-02-19 06:33:57 -08:00
|
|
|
}
|
2021-09-02 08:43:54 -07:00
|
|
|
if !c.fail {
|
2024-02-01 06:18:01 -08:00
|
|
|
require.NoError(t, r.Err())
|
|
|
|
} else {
|
|
|
|
require.Error(t, r.Err())
|
2019-02-19 06:33:57 -08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReader_Live(t *testing.T) {
|
|
|
|
logger := testutil.NewLogger(t)
|
|
|
|
|
|
|
|
for i := range testReaderCases {
|
|
|
|
t.Run(strconv.Itoa(i), func(t *testing.T) {
|
2022-04-27 02:24:36 -07:00
|
|
|
writeFd, err := os.CreateTemp("", "TestReader_Live")
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
defer os.Remove(writeFd.Name())
|
|
|
|
|
|
|
|
go func(i int) {
|
|
|
|
for _, rec := range testReaderCases[i].t {
|
|
|
|
rec := encodedRecord(rec.t, rec.b)
|
|
|
|
_, err := writeFd.Write(rec)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
runtime.Gosched()
|
|
|
|
}
|
|
|
|
writeFd.Close()
|
|
|
|
}(i)
|
|
|
|
|
|
|
|
// Read from a second FD on the same file.
|
|
|
|
readFd, err := os.Open(writeFd.Name())
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-07-03 05:10:10 -07:00
|
|
|
reader := NewLiveReader(logger, NewLiveReaderMetrics(nil), readFd)
|
2019-02-19 06:33:57 -08:00
|
|
|
for _, exp := range testReaderCases[i].exp {
|
|
|
|
for !reader.Next() {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Equal(t, io.EOF, reader.Err(), "expect EOF, got: %v", reader.Err())
|
2019-02-19 06:33:57 -08:00
|
|
|
runtime.Gosched()
|
|
|
|
}
|
|
|
|
|
|
|
|
actual := reader.Record()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Equal(t, exp, actual, "read wrong record")
|
2019-02-19 06:33:57 -08:00
|
|
|
}
|
|
|
|
|
2020-10-29 02:43:23 -07:00
|
|
|
require.False(t, reader.Next(), "unexpected record")
|
2019-02-19 06:33:57 -08:00
|
|
|
if testReaderCases[i].fail {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Error(t, reader.Err())
|
2019-02-19 06:33:57 -08:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const fuzzLen = 500
|
|
|
|
|
2022-10-10 08:08:46 -07:00
|
|
|
func generateRandomEntries(w *WL, records chan []byte) error {
|
2019-02-19 06:33:57 -08:00
|
|
|
var recs [][]byte
|
|
|
|
for i := 0; i < fuzzLen; i++ {
|
|
|
|
var sz int64
|
|
|
|
switch i % 5 {
|
|
|
|
case 0, 1:
|
|
|
|
sz = 50
|
|
|
|
case 2, 3:
|
|
|
|
sz = pageSize
|
|
|
|
default:
|
|
|
|
sz = pageSize * 8
|
|
|
|
}
|
2023-08-14 14:42:02 -07:00
|
|
|
n, err := rand.Int(rand.Reader, big.NewInt(sz))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rec := make([]byte, n.Int64())
|
2019-02-19 06:33:57 -08:00
|
|
|
if _, err := rand.Read(rec); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
records <- rec
|
|
|
|
|
|
|
|
// Randomly batch up records.
|
|
|
|
recs = append(recs, rec)
|
2023-08-14 14:42:02 -07:00
|
|
|
n, err = rand.Int(rand.Reader, big.NewInt(int64(4)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if int(n.Int64()) < 3 {
|
2019-02-19 06:33:57 -08:00
|
|
|
if err := w.Log(recs...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
recs = recs[:0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return w.Log(recs...)
|
|
|
|
}
|
|
|
|
|
2019-03-19 06:31:57 -07:00
|
|
|
type multiReadCloser struct {
|
|
|
|
reader io.Reader
|
|
|
|
closers []io.Closer
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *multiReadCloser) Read(p []byte) (n int, err error) {
|
|
|
|
return m.reader.Read(p)
|
|
|
|
}
|
2021-10-22 01:06:44 -07:00
|
|
|
|
2019-03-19 06:31:57 -07:00
|
|
|
func (m *multiReadCloser) Close() error {
|
2020-10-28 08:24:58 -07:00
|
|
|
return tsdb_errors.NewMulti(tsdb_errors.CloseAll(m.closers)).Err()
|
2019-03-19 06:31:57 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func allSegments(dir string) (io.ReadCloser, error) {
|
2019-02-19 06:33:57 -08:00
|
|
|
seg, err := listSegments(dir)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var readers []io.Reader
|
2019-03-19 06:31:57 -07:00
|
|
|
var closers []io.Closer
|
2019-02-19 06:33:57 -08:00
|
|
|
for _, r := range seg {
|
|
|
|
f, err := os.Open(filepath.Join(dir, r.name))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
readers = append(readers, f)
|
2019-03-19 06:31:57 -07:00
|
|
|
closers = append(closers, f)
|
2019-02-19 06:33:57 -08:00
|
|
|
}
|
2019-03-19 06:31:57 -07:00
|
|
|
|
|
|
|
return &multiReadCloser{
|
|
|
|
reader: io.MultiReader(readers...),
|
|
|
|
closers: closers,
|
|
|
|
}, nil
|
2019-02-19 06:33:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReaderFuzz(t *testing.T) {
|
|
|
|
for name, fn := range readerConstructors {
|
2023-07-11 05:57:57 -07:00
|
|
|
for _, compress := range []CompressionType{CompressionNone, CompressionSnappy, CompressionZstd} {
|
|
|
|
t.Run(fmt.Sprintf("%s,compress=%s", name, compress), func(t *testing.T) {
|
2021-10-31 23:58:18 -07:00
|
|
|
dir := t.TempDir()
|
2019-06-19 06:46:24 -07:00
|
|
|
|
|
|
|
w, err := NewSize(nil, nil, dir, 128*pageSize, compress)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-06-19 06:46:24 -07:00
|
|
|
|
|
|
|
// Buffering required as we're not reading concurrently.
|
|
|
|
input := make(chan []byte, fuzzLen)
|
|
|
|
err = generateRandomEntries(w, input)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-06-19 06:46:24 -07:00
|
|
|
close(input)
|
|
|
|
|
|
|
|
err = w.Close()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-06-19 06:46:24 -07:00
|
|
|
|
|
|
|
sr, err := allSegments(w.Dir())
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-06-19 06:46:24 -07:00
|
|
|
defer sr.Close()
|
|
|
|
|
|
|
|
reader := fn(sr)
|
|
|
|
for expected := range input {
|
2020-10-29 02:43:23 -07:00
|
|
|
require.True(t, reader.Next(), "expected record: %v", reader.Err())
|
2021-10-22 23:55:48 -07:00
|
|
|
r := reader.Record()
|
|
|
|
// Expected value may come as nil or empty slice, so it requires special comparison.
|
|
|
|
if len(expected) == 0 {
|
2023-12-07 03:35:01 -08:00
|
|
|
require.Empty(t, r)
|
2021-10-22 23:55:48 -07:00
|
|
|
} else {
|
|
|
|
require.Equal(t, expected, r, "read wrong record")
|
|
|
|
}
|
2019-06-19 06:46:24 -07:00
|
|
|
}
|
2020-10-29 02:43:23 -07:00
|
|
|
require.False(t, reader.Next(), "unexpected record")
|
2019-06-19 06:46:24 -07:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestReaderFuzz_Live(t *testing.T) {
|
|
|
|
logger := testutil.NewLogger(t)
|
2023-07-11 05:57:57 -07:00
|
|
|
for _, compress := range []CompressionType{CompressionNone, CompressionSnappy, CompressionZstd} {
|
|
|
|
t.Run(fmt.Sprintf("compress=%s", compress), func(t *testing.T) {
|
2021-10-31 23:58:18 -07:00
|
|
|
dir := t.TempDir()
|
2019-02-19 06:33:57 -08:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
w, err := NewSize(nil, nil, dir, 128*pageSize, compress)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-06-19 06:46:24 -07:00
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
// In the background, generate a stream of random records and write them
|
|
|
|
// to the WAL.
|
|
|
|
input := make(chan []byte, fuzzLen/10) // buffering required as we sometimes batch WAL writes.
|
|
|
|
done := make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
err := generateRandomEntries(w, input)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-06-19 06:46:24 -07:00
|
|
|
time.Sleep(100 * time.Millisecond)
|
|
|
|
close(done)
|
|
|
|
}()
|
2019-02-19 06:33:57 -08:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
// Tail the WAL and compare the results.
|
2020-09-01 02:16:57 -07:00
|
|
|
m, _, err := Segments(w.Dir())
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
seg, err := OpenReadSegment(SegmentName(dir, m))
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-06-19 06:46:24 -07:00
|
|
|
defer seg.Close()
|
2019-02-19 06:33:57 -08:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
r := NewLiveReader(logger, nil, seg)
|
|
|
|
segmentTicker := time.NewTicker(100 * time.Millisecond)
|
|
|
|
readTicker := time.NewTicker(10 * time.Millisecond)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
readSegment := func(r *LiveReader) bool {
|
|
|
|
for r.Next() {
|
|
|
|
rec := r.Record()
|
|
|
|
expected, ok := <-input
|
2020-10-29 02:43:23 -07:00
|
|
|
require.True(t, ok, "unexpected record")
|
2021-10-22 23:55:48 -07:00
|
|
|
// Expected value may come as nil or empty slice, so it requires special comparison.
|
|
|
|
if len(expected) == 0 {
|
2023-12-07 03:35:01 -08:00
|
|
|
require.Empty(t, rec)
|
2021-10-22 23:55:48 -07:00
|
|
|
} else {
|
|
|
|
require.Equal(t, expected, rec, "record does not match expected")
|
|
|
|
}
|
2019-06-19 06:46:24 -07:00
|
|
|
}
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Equal(t, io.EOF, r.Err(), "expected EOF, got: %v", r.Err())
|
2019-06-19 06:46:24 -07:00
|
|
|
return true
|
2019-02-19 06:33:57 -08:00
|
|
|
}
|
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
outer:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-segmentTicker.C:
|
|
|
|
// check if new segments exist
|
2020-09-01 02:16:57 -07:00
|
|
|
_, last, err := Segments(w.Dir())
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-06-19 06:46:24 -07:00
|
|
|
if last <= seg.i {
|
|
|
|
continue
|
|
|
|
}
|
2019-02-19 06:33:57 -08:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
// read to end of segment.
|
|
|
|
readSegment(r)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
fi, err := os.Stat(SegmentName(dir, seg.i))
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, r.Offset(), fi.Size(), "expected to have read whole segment, but read %d of %d", r.Offset(), fi.Size())
|
2019-02-19 06:33:57 -08:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
seg, err = OpenReadSegment(SegmentName(dir, seg.i+1))
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-06-19 06:46:24 -07:00
|
|
|
defer seg.Close()
|
|
|
|
r = NewLiveReader(logger, nil, seg)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
case <-readTicker.C:
|
|
|
|
readSegment(r)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
2019-06-19 06:46:24 -07:00
|
|
|
case <-done:
|
|
|
|
readSegment(r)
|
|
|
|
break outer
|
|
|
|
}
|
2019-02-19 06:33:57 -08:00
|
|
|
}
|
|
|
|
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Equal(t, io.EOF, r.Err(), "expected EOF")
|
2019-06-19 06:46:24 -07:00
|
|
|
})
|
2019-02-19 06:33:57 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestLiveReaderCorrupt_ShortFile(t *testing.T) {
|
|
|
|
// Write a corrupt WAL segment, there is one record of pageSize in length,
|
|
|
|
// but the segment is only half written.
|
|
|
|
logger := testutil.NewLogger(t)
|
2021-10-31 23:58:18 -07:00
|
|
|
dir := t.TempDir()
|
2019-02-19 06:33:57 -08:00
|
|
|
|
2023-07-11 05:57:57 -07:00
|
|
|
w, err := NewSize(nil, nil, dir, pageSize, CompressionNone)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
|
|
|
rec := make([]byte, pageSize-recordHeaderSize)
|
|
|
|
_, err = rand.Read(rec)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
|
|
|
err = w.Log(rec)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
|
|
|
err = w.Close()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
2021-10-22 01:06:44 -07:00
|
|
|
segmentFile, err := os.OpenFile(filepath.Join(dir, "00000000"), os.O_RDWR, 0o666)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
|
|
|
err = segmentFile.Truncate(pageSize / 2)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
|
|
|
err = segmentFile.Close()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
|
|
|
// Try and LiveReader it.
|
2020-09-01 02:16:57 -07:00
|
|
|
m, _, err := Segments(w.Dir())
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
|
|
|
seg, err := OpenReadSegment(SegmentName(dir, m))
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-03-19 06:31:57 -07:00
|
|
|
defer seg.Close()
|
2019-02-19 06:33:57 -08:00
|
|
|
|
2019-06-03 03:27:30 -07:00
|
|
|
r := NewLiveReader(logger, nil, seg)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.False(t, r.Next(), "expected no records")
|
|
|
|
require.Equal(t, io.EOF, r.Err(), "expected error, got: %v", r.Err())
|
2019-02-19 06:33:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestLiveReaderCorrupt_RecordTooLongAndShort(t *testing.T) {
|
|
|
|
// Write a corrupt WAL segment, when record len > page size.
|
|
|
|
logger := testutil.NewLogger(t)
|
2021-10-31 23:58:18 -07:00
|
|
|
dir := t.TempDir()
|
2019-02-19 06:33:57 -08:00
|
|
|
|
2023-07-11 05:57:57 -07:00
|
|
|
w, err := NewSize(nil, nil, dir, pageSize*2, CompressionNone)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
|
|
|
rec := make([]byte, pageSize-recordHeaderSize)
|
|
|
|
_, err = rand.Read(rec)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
|
|
|
err = w.Log(rec)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
|
|
|
err = w.Close()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
2021-10-22 01:06:44 -07:00
|
|
|
segmentFile, err := os.OpenFile(filepath.Join(dir, "00000000"), os.O_RDWR, 0o666)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
|
|
|
// Override the record length
|
|
|
|
buf := make([]byte, 3)
|
|
|
|
buf[0] = byte(recFull)
|
|
|
|
binary.BigEndian.PutUint16(buf[1:], 0xFFFF)
|
|
|
|
_, err = segmentFile.WriteAt(buf, 0)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
|
|
|
err = segmentFile.Close()
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
|
|
|
// Try and LiveReader it.
|
2020-09-01 02:16:57 -07:00
|
|
|
m, _, err := Segments(w.Dir())
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
|
|
|
seg, err := OpenReadSegment(SegmentName(dir, m))
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-03-19 06:31:57 -07:00
|
|
|
defer seg.Close()
|
2019-02-19 06:33:57 -08:00
|
|
|
|
2019-07-03 05:10:10 -07:00
|
|
|
r := NewLiveReader(logger, NewLiveReaderMetrics(nil), seg)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.False(t, r.Next(), "expected no records")
|
|
|
|
require.EqualError(t, r.Err(), "record length greater than a single page: 65542 > 32768", "expected error, got: %v", r.Err())
|
2019-02-19 06:33:57 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestReaderData(t *testing.T) {
|
|
|
|
dir := os.Getenv("WALDIR")
|
|
|
|
if dir == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, fn := range readerConstructors {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
2023-07-11 05:57:57 -07:00
|
|
|
w, err := New(nil, nil, dir, CompressionSnappy)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
|
|
|
sr, err := allSegments(dir)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
|
|
|
|
reader := fn(sr)
|
2023-10-31 04:35:13 -07:00
|
|
|
for reader.Next() {
|
2019-02-19 06:33:57 -08:00
|
|
|
}
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, reader.Err())
|
2019-02-19 06:33:57 -08:00
|
|
|
|
|
|
|
err = w.Repair(reader.Err())
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-02-19 06:33:57 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|