Skip to content

Commit d563eca

Browse files
committed
Backport corrupted WAL error fix
This PR introduces the fix added to [lightstep/opentelemetry-prometheus-sidecar here](lightstep/opentelemetry-prometheus-sidecar#71). Closes Stackdriver#268
1 parent 94d4795 commit d563eca

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ require (
55
contrib.go.opencensus.io/exporter/prometheus v0.1.0
66
contrib.go.opencensus.io/exporter/stackdriver v0.13.4
77
github.com/Azure/azure-sdk-for-go v36.2.0+incompatible // indirect
8-
github.com/Azure/go-autorest v13.3.0+incompatible // indirect
8+
github.com/Azure/go-autorest v13.3.0+incompatible // indirect
99
github.com/Azure/go-autorest/autorest/adal v0.8.0 // indirect
1010
github.com/Azure/go-autorest/autorest/to v0.3.0 // indirect
1111
github.com/Azure/go-autorest/autorest/validation v0.2.0 // indirect
@@ -48,6 +48,7 @@ require (
4848
github.com/samuel/go-zookeeper v0.0.0-20190801204459-3c104360edc8 // indirect
4949
github.com/spaolacci/murmur3 v1.1.0 // indirect
5050
github.com/spf13/pflag v1.0.5 // indirect
51+
github.com/stretchr/testify v1.4.0
5152
go.opencensus.io v0.22.4
5253
golang.org/x/crypto v0.0.0-20191128160524-b544559bb6d1 // indirect
5354
golang.org/x/net v0.0.0-20191126235420-ef20fe5d7933 // indirect

tail/tail.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -207,19 +207,19 @@ func (t *Tailer) Read(b []byte) (int, error) {
207207
}
208208
}
209209

210+
// openSegment finds a WAL segment with a name that parses to n. This
211+
// way we do not need to know how wide the segment filename is (i.e.,
212+
// how many zeros to pad with).
210213
func openSegment(dir string, n int) (io.ReadCloser, error) {
211214
files, err := fileutil.ReadDir(dir)
212215
if err != nil {
213216
return nil, err
214217
}
215218
for _, fn := range files {
216219
k, err := strconv.Atoi(fn)
217-
if err != nil || k < n {
220+
if err != nil || k != n {
218221
continue
219222
}
220-
if k > n {
221-
return nil, errors.Errorf("next segment %d too high, expected %d", n, k)
222-
}
223223
return wal.OpenReadSegment(filepath.Join(dir, fn))
224224
}
225225
return nil, tsdb.ErrNotFound

tail/tail_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,44 @@ package tail
1717
import (
1818
"bytes"
1919
"context"
20+
"fmt"
2021
"io/ioutil"
2122
"math/rand"
2223
"os"
24+
"path"
2325
"testing"
2426
"time"
2527

2628
"github.com/prometheus/tsdb/wal"
29+
"github.com/stretchr/testify/require"
2730
)
2831

32+
func TestOpenSegment(t *testing.T) {
33+
dir, err := ioutil.TempDir("", "test_open_segment")
34+
if err != nil {
35+
t.Fatal(err)
36+
}
37+
defer os.RemoveAll(dir)
38+
39+
require.NoError(t, ioutil.WriteFile(path.Join(dir, "000000000000000000000nonsense"), []byte("bad"), 0777))
40+
41+
for i := 0; i < 10; i++ {
42+
require.NoError(t, ioutil.WriteFile(path.Join(dir, fmt.Sprint("000000000000000000000", i)), []byte(fmt.Sprint(i)), 0777))
43+
}
44+
for i := 19; i >= 10; i-- {
45+
require.NoError(t, ioutil.WriteFile(path.Join(dir, fmt.Sprint("000000000000000000000", i)), []byte(fmt.Sprint(i)), 0777))
46+
}
47+
48+
for i := 0; i < 20; i++ {
49+
rc, err := openSegment(dir, i)
50+
require.NoError(t, err)
51+
body, err := ioutil.ReadAll(rc)
52+
require.NoError(t, err)
53+
require.Equal(t, fmt.Sprint(i), string(body))
54+
require.NoError(t, rc.Close())
55+
}
56+
}
57+
2958
func TestTailFuzz(t *testing.T) {
3059
dir, err := ioutil.TempDir("", "test_tail")
3160
if err != nil {

0 commit comments

Comments
 (0)