2016-12-22 06:18:33 -08:00
|
|
|
package tsdb
|
|
|
|
|
|
|
|
import (
|
2017-02-14 15:54:52 -08:00
|
|
|
"encoding/binary"
|
|
|
|
"fmt"
|
2016-12-22 06:18:33 -08:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
"github.com/coreos/etcd/pkg/fileutil"
|
2016-12-22 06:18:33 -08:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
func TestWAL_initSegments(t *testing.T) {
|
|
|
|
tmpdir, err := ioutil.TempDir("", "test_wal_open")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
|
|
|
df, err := fileutil.OpenDir(tmpdir)
|
|
|
|
require.NoError(t, err)
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
w := &WAL{dirFile: df}
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
// Create segment files with an appropriate header.
|
|
|
|
for i := 1; i <= 5; i++ {
|
|
|
|
metab := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint32(metab[:4], WALMagic)
|
|
|
|
metab[4] = WALFormatDefault
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
f, err := os.Create(fmt.Sprintf("%s/000%d", tmpdir, i))
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = f.Write(metab)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, f.Close())
|
2016-12-22 06:18:33 -08:00
|
|
|
}
|
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
// Initialize 5 correct segment files.
|
|
|
|
require.NoError(t, w.initSegments())
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
require.Equal(t, 5, len(w.files), "unexpected number of segments loaded")
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
// Validate that files are locked properly.
|
|
|
|
for _, of := range w.files {
|
|
|
|
f, err := os.Open(of.Name())
|
|
|
|
require.NoError(t, err, "open locked segment %s", f.Name())
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
_, err = f.Read([]byte{0})
|
|
|
|
require.NoError(t, err, "read locked segment %s", f.Name())
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
_, err = f.Write([]byte{0})
|
|
|
|
require.Error(t, err, "write to tail segment file %s", f.Name())
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
require.NoError(t, f.Close())
|
|
|
|
}
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
for _, f := range w.files {
|
|
|
|
require.NoError(t, f.Close())
|
|
|
|
}
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
// Make initialization fail by corrupting the header of one file.
|
|
|
|
f, err := os.OpenFile(w.files[3].Name(), os.O_WRONLY, 0666)
|
|
|
|
require.NoError(t, err)
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
_, err = f.WriteAt([]byte{0}, 4)
|
|
|
|
require.NoError(t, err)
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
w = &WAL{dirFile: df}
|
|
|
|
require.Error(t, w.initSegments(), "init corrupted segments")
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
for _, f := range w.files {
|
|
|
|
require.NoError(t, f.Close())
|
|
|
|
}
|
|
|
|
}
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
func TestWAL_cut(t *testing.T) {
|
|
|
|
tmpdir, err := ioutil.TempDir("", "test_wal_cut")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer os.RemoveAll(tmpdir)
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
// This calls cut() implicitly the first time without a previous tail.
|
|
|
|
w, err := OpenWAL(tmpdir, nil, 0)
|
|
|
|
require.NoError(t, err)
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
require.NoError(t, w.entry(WALEntrySeries, 1, []byte("Hello World!!")))
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
require.NoError(t, w.cut(), "cut failed")
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
// Cutting creates a new file and close the previous tail file.
|
|
|
|
require.Equal(t, 2, len(w.files))
|
|
|
|
require.Equal(t, os.ErrInvalid.Error(), w.files[0].Close().Error())
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
require.NoError(t, w.entry(WALEntrySeries, 1, []byte("Hello World!!")))
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
require.NoError(t, w.Close())
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
for _, of := range w.files {
|
|
|
|
f, err := os.Open(of.Name())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Verify header data.
|
|
|
|
metab := make([]byte, 8)
|
|
|
|
_, err = f.Read(metab)
|
|
|
|
require.NoError(t, err, "read meta data %s", f.Name())
|
|
|
|
require.Equal(t, WALMagic, binary.BigEndian.Uint32(metab[:4]), "verify magic")
|
|
|
|
require.Equal(t, WALFormatDefault, metab[4], "verify format flag")
|
2016-12-22 06:18:33 -08:00
|
|
|
|
2017-02-14 15:54:52 -08:00
|
|
|
// We cannot actually check for correct pre-allocation as it is
|
|
|
|
// optional per filesystem and handled transparently.
|
|
|
|
et, flag, b, err := newWALDecoder(f, nil).entry()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, WALEntrySeries, et)
|
|
|
|
require.Equal(t, flag, byte(walSeriesSimple))
|
|
|
|
require.Equal(t, []byte("Hello World!!"), b)
|
|
|
|
}
|
2016-12-22 06:18:33 -08:00
|
|
|
}
|