Skip to content
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

Backport corrupted WAL error fix #288

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions tail/tail.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,19 +207,19 @@ func (t *Tailer) Read(b []byte) (int, error) {
}
}

// openSegment finds a WAL segment with a name that parses to n. This
// way we do not need to know how wide the segment filename is (i.e.,
// how many zeros to pad with).
func openSegment(dir string, n int) (io.ReadCloser, error) {
files, err := fileutil.ReadDir(dir)
if err != nil {
return nil, err
}
for _, fn := range files {
k, err := strconv.Atoi(fn)
if err != nil || k < n {
if err != nil || k != n {
continue
}
if k > n {
return nil, errors.Errorf("next segment %d too high, expected %d", n, k)
}
return wal.OpenReadSegment(filepath.Join(dir, fn))
}
return nil, tsdb.ErrNotFound
Expand Down
42 changes: 42 additions & 0 deletions tail/tail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,57 @@ package tail
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path"
"testing"
"time"

"github.com/prometheus/tsdb/wal"
)

func TestOpenSegment(t *testing.T) {
dir, err := ioutil.TempDir("", "test_open_segment")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)

if err := ioutil.WriteFile(path.Join(dir, "000000000000000000000nonsense"), []byte("bad"), 0777); err != nil {
t.Fatal(err)
}

for i := 0; i < 10; i++ {
if err := ioutil.WriteFile(path.Join(dir, fmt.Sprint("000000000000000000000", i)), []byte(fmt.Sprint(i)), 0777); err != nil {
t.Fatal(err)
}
}
for i := 19; i >= 10; i-- {
if err := ioutil.WriteFile(path.Join(dir, fmt.Sprint("000000000000000000000", i)), []byte(fmt.Sprint(i)), 0777); err != nil {
t.Fatal(err)
}
}

for i := 0; i < 20; i++ {
rc, err := openSegment(dir, i)
if err != nil {
t.Fatal(err)
}
body, err := ioutil.ReadAll(rc)
if err != nil {
t.Fatal(err)
}
if want, have := fmt.Sprint(i), string(body); want != have {
t.Fatalf("invalid body read want=%q have=%q", want, have)
}
if err := rc.Close(); err != nil {
t.Fatal(err)
}
}
}

func TestTailFuzz(t *testing.T) {
dir, err := ioutil.TempDir("", "test_tail")
if err != nil {
Expand Down