Merge pull request #1861 from yichengq/250

wal: sync after writing data to disk in Cut function
release-2.0
Yicheng Qin 2014-12-04 23:49:07 -08:00
commit a16dd7ea67
2 changed files with 10 additions and 4 deletions

View File

@ -240,7 +240,10 @@ func (w *WAL) Cut() error {
if err := w.encoder.encode(&walpb.Record{Type: metadataType, Data: w.metadata}); err != nil {
return err
}
return w.SaveState(&w.state)
if err := w.SaveState(&w.state); err != nil {
return err
}
return w.sync()
}
func (w *WAL) sync() error {

View File

@ -131,6 +131,7 @@ func TestOpenAtIndex(t *testing.T) {
}
}
// TODO: split it into smaller tests for better readability
func TestCut(t *testing.T) {
p, err := ioutil.TempDir(os.TempDir(), "waltest")
if err != nil {
@ -142,6 +143,7 @@ func TestCut(t *testing.T) {
if err != nil {
t.Fatal(err)
}
defer w.Close()
// TODO(unihorn): remove this when cut can operate on an empty file
if err := w.SaveEntry(&raftpb.Entry{}); err != nil {
@ -170,19 +172,20 @@ func TestCut(t *testing.T) {
if g := path.Base(w.f.Name()); g != wname {
t.Errorf("name = %s, want %s", g, wname)
}
w.Close()
// check the state in the last WAL
// We do check before closing the WAL to ensure that Cut syncs the data
// into the disk.
f, err := os.Open(path.Join(p, wname))
if err != nil {
t.Fatal(err)
}
defer f.Close()
w = &WAL{
nw := &WAL{
decoder: newDecoder(f),
ri: 2,
}
_, gst, _, err := w.ReadAll()
_, gst, _, err := nw.ReadAll()
if err != nil {
t.Fatal(err)
}