mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
wal: add walName function; cleanup test
This commit is contained in:
parent
cec1956b8f
commit
1d09c25f5f
@ -83,6 +83,10 @@ func parseWalName(str string) (seq, index int64, err error) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func walName(seq, index int64) string {
|
||||||
|
return fmt.Sprintf("%016x-%016x.wal", seq, index)
|
||||||
|
}
|
||||||
|
|
||||||
func max(a, b int64) int64 {
|
func max(a, b int64) int64 {
|
||||||
if a > b {
|
if a > b {
|
||||||
return a
|
return a
|
||||||
|
@ -76,7 +76,7 @@ func Create(dirpath string) (*WAL, error) {
|
|||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
p := path.Join(dirpath, fmt.Sprintf("%016x-%016x.wal", 0, 0))
|
p := path.Join(dirpath, walName(0, 0))
|
||||||
f, err := os.OpenFile(p, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
|
f, err := os.OpenFile(p, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -207,7 +207,7 @@ func (w *WAL) Cut() error {
|
|||||||
log.Printf("wal.cut index=%d", w.enti+1)
|
log.Printf("wal.cut index=%d", w.enti+1)
|
||||||
|
|
||||||
// create a new wal file with name sequence + 1
|
// create a new wal file with name sequence + 1
|
||||||
fpath := path.Join(w.dir, fmt.Sprintf("%016x-%016x.wal", w.seq+1, w.enti+1))
|
fpath := path.Join(w.dir, walName(w.seq+1, w.enti+1))
|
||||||
f, err := os.OpenFile(fpath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
|
f, err := os.OpenFile(fpath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -18,7 +18,6 @@ package wal
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
@ -31,8 +30,6 @@ import (
|
|||||||
var (
|
var (
|
||||||
infoData = []byte("\b\xef\xfd\x02")
|
infoData = []byte("\b\xef\xfd\x02")
|
||||||
infoRecord = append([]byte("\x0e\x00\x00\x00\x00\x00\x00\x00\b\x01\x10\x99\xb5\xe4\xd0\x03\x1a\x04"), infoData...)
|
infoRecord = append([]byte("\x0e\x00\x00\x00\x00\x00\x00\x00\b\x01\x10\x99\xb5\xe4\xd0\x03\x1a\x04"), infoData...)
|
||||||
|
|
||||||
firstWalName = "0000000000000000-0000000000000000.wal"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNew(t *testing.T) {
|
func TestNew(t *testing.T) {
|
||||||
@ -46,8 +43,8 @@ func TestNew(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("err = %v, want nil", err)
|
t.Fatalf("err = %v, want nil", err)
|
||||||
}
|
}
|
||||||
if g := path.Base(w.f.Name()); g != firstWalName {
|
if g := path.Base(w.f.Name()); g != walName(0, 0) {
|
||||||
t.Errorf("name = %+v, want %+v", g, firstWalName)
|
t.Errorf("name = %+v, want %+v", g, walName(0, 0))
|
||||||
}
|
}
|
||||||
w.Close()
|
w.Close()
|
||||||
}
|
}
|
||||||
@ -59,7 +56,7 @@ func TestNewForInitedDir(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer os.RemoveAll(p)
|
defer os.RemoveAll(p)
|
||||||
|
|
||||||
os.Create(path.Join(p, firstWalName))
|
os.Create(path.Join(p, walName(0, 0)))
|
||||||
if _, err = Create(p); err == nil || err != os.ErrExist {
|
if _, err = Create(p); err == nil || err != os.ErrExist {
|
||||||
t.Errorf("err = %v, want %v", err, os.ErrExist)
|
t.Errorf("err = %v, want %v", err, os.ErrExist)
|
||||||
}
|
}
|
||||||
@ -72,13 +69,7 @@ func TestOpenAtIndex(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer os.RemoveAll(dir)
|
defer os.RemoveAll(dir)
|
||||||
|
|
||||||
f, err := os.Create(path.Join(dir, firstWalName))
|
f, err := os.Create(path.Join(dir, walName(0, 0)))
|
||||||
if err != nil {
|
|
||||||
t.Fatal(err)
|
|
||||||
}
|
|
||||||
f.Close()
|
|
||||||
writef := fmt.Sprintf("%016x-%016x.wal", 1, 4)
|
|
||||||
f, err = os.Create(path.Join(dir, writef))
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
@ -88,15 +79,15 @@ func TestOpenAtIndex(t *testing.T) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("err = %v, want nil", err)
|
t.Fatalf("err = %v, want nil", err)
|
||||||
}
|
}
|
||||||
if g := path.Base(w.f.Name()); g != writef {
|
if g := path.Base(w.f.Name()); g != walName(0, 0) {
|
||||||
t.Errorf("name = %+v, want %+v", g, writef)
|
t.Errorf("name = %+v, want %+v", g, walName(0, 0))
|
||||||
}
|
}
|
||||||
if w.seq != 1 {
|
if w.seq != 0 {
|
||||||
t.Errorf("seq = %d, want %d", w.seq, 1)
|
t.Errorf("seq = %d, want %d", w.seq, 0)
|
||||||
}
|
}
|
||||||
w.Close()
|
w.Close()
|
||||||
|
|
||||||
wname := fmt.Sprintf("%016x-%016x.wal", 2, 10)
|
wname := walName(2, 10)
|
||||||
f, err = os.Create(path.Join(dir, wname))
|
f, err = os.Create(path.Join(dir, wname))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
@ -110,6 +101,9 @@ func TestOpenAtIndex(t *testing.T) {
|
|||||||
if g := path.Base(w.f.Name()); g != wname {
|
if g := path.Base(w.f.Name()); g != wname {
|
||||||
t.Errorf("name = %+v, want %+v", g, wname)
|
t.Errorf("name = %+v, want %+v", g, wname)
|
||||||
}
|
}
|
||||||
|
if w.seq != 2 {
|
||||||
|
t.Errorf("seq = %d, want %d", w.seq, 2)
|
||||||
|
}
|
||||||
w.Close()
|
w.Close()
|
||||||
|
|
||||||
emptydir, err := ioutil.TempDir(os.TempDir(), "waltestempty")
|
emptydir, err := ioutil.TempDir(os.TempDir(), "waltestempty")
|
||||||
@ -142,7 +136,7 @@ func TestCut(t *testing.T) {
|
|||||||
if err := w.Cut(); err != nil {
|
if err := w.Cut(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
wname := fmt.Sprintf("%016x-%016x.wal", 1, 1)
|
wname := walName(1, 1)
|
||||||
if g := path.Base(w.f.Name()); g != wname {
|
if g := path.Base(w.f.Name()); g != wname {
|
||||||
t.Errorf("name = %s, want %s", g, wname)
|
t.Errorf("name = %s, want %s", g, wname)
|
||||||
}
|
}
|
||||||
@ -154,7 +148,7 @@ func TestCut(t *testing.T) {
|
|||||||
if err := w.Cut(); err != nil {
|
if err := w.Cut(); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
wname = fmt.Sprintf("%016x-%016x.wal", 2, 2)
|
wname = walName(2, 2)
|
||||||
if g := path.Base(w.f.Name()); g != wname {
|
if g := path.Base(w.f.Name()); g != wname {
|
||||||
t.Errorf("name = %s, want %s", g, wname)
|
t.Errorf("name = %s, want %s", g, wname)
|
||||||
}
|
}
|
||||||
@ -313,7 +307,7 @@ func TestRecoverAfterCut(t *testing.T) {
|
|||||||
}
|
}
|
||||||
w.Close()
|
w.Close()
|
||||||
|
|
||||||
if err := os.Remove(path.Join(p, "0000000000000004-0000000000000004.wal")); err != nil {
|
if err := os.Remove(path.Join(p, walName(4, 4))); err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user