Skip to content

Commit

Permalink
Backport corrupted WAL error fix
Browse files Browse the repository at this point in the history
This PR introduces the fix added to [lightstep/opentelemetry-prometheus-sidecar here](lightstep/opentelemetry-prometheus-sidecar#71).

Closes Stackdriver#268
  • Loading branch information
eseliger committed Aug 10, 2021
1 parent 94d4795 commit d563eca
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ require (
contrib.go.opencensus.io/exporter/prometheus v0.1.0
contrib.go.opencensus.io/exporter/stackdriver v0.13.4
github.com/Azure/azure-sdk-for-go v36.2.0+incompatible // indirect
github.com/Azure/go-autorest v13.3.0+incompatible // indirect
github.com/Azure/go-autorest v13.3.0+incompatible // indirect
github.com/Azure/go-autorest/autorest/adal v0.8.0 // indirect
github.com/Azure/go-autorest/autorest/to v0.3.0 // indirect
github.com/Azure/go-autorest/autorest/validation v0.2.0 // indirect
Expand Down Expand Up @@ -48,6 +48,7 @@ require (
github.com/samuel/go-zookeeper v0.0.0-20190801204459-3c104360edc8 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/testify v1.4.0
go.opencensus.io v0.22.4
golang.org/x/crypto v0.0.0-20191128160524-b544559bb6d1 // indirect
golang.org/x/net v0.0.0-20191126235420-ef20fe5d7933 // indirect
Expand Down
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
29 changes: 29 additions & 0 deletions tail/tail_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,44 @@ package tail
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path"
"testing"
"time"

"github.com/prometheus/tsdb/wal"
"github.com/stretchr/testify/require"
)

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

require.NoError(t, ioutil.WriteFile(path.Join(dir, "000000000000000000000nonsense"), []byte("bad"), 0777))

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

for i := 0; i < 20; i++ {
rc, err := openSegment(dir, i)
require.NoError(t, err)
body, err := ioutil.ReadAll(rc)
require.NoError(t, err)
require.Equal(t, fmt.Sprint(i), string(body))
require.NoError(t, rc.Close())
}
}

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

0 comments on commit d563eca

Please sign in to comment.