wal: refactor

release-2.0
Xiang Li 2014-09-14 21:27:52 -07:00
parent 77fbd2610c
commit 69f2d5c590
2 changed files with 9 additions and 6 deletions

View File

@ -48,6 +48,10 @@ At a later time a WAL can be opened at a particular raft index:
w, err := wal.OpenAtIndex("/var/lib/etcd", 0)
...
The raft index must have been written to the WAL. When opening without a
snapshot the raft index should always be 0. When opening with a snapshot
the raft index should be the index of the last entry covered by the snapshot.
Additional items cannot be Saved to this WAL until all of the items from 0 to
the end of the WAL are read first:

View File

@ -60,8 +60,8 @@ type WAL struct {
decoder *decoder // decoder to decode records
f *os.File // underlay file opened for appending, sync
seq int64 // the sequence of the current writting wal file
enti int64 // index of the last entry that has been saved to wal
seq int64 // sequence of the wal file currently used for writes
enti int64 // index of the last entry saved to the wal
encoder *encoder // encoder to encode records
}
@ -94,7 +94,7 @@ func Create(dirpath string) (*WAL, error) {
}
// OpenAtIndex opens the WAL at the given index.
// There MUST be an entry with given index written to WAL before.
// The index MUST have been previously committed to the WAL.
// The returned WAL is ready to read and the first record will be the given
// index. The WAL cannot be appended to before reading out all of its
// previous records.
@ -205,18 +205,17 @@ func (w *WAL) ReadAll() (id int64, state raftpb.State, ents []raftpb.Entry, err
// Cut closes current file written and creates a new one ready to append.
func (w *WAL) Cut() error {
log.Printf("wal.cut index=%d", w.enti+1)
// create a new wal file with name sequence + 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)
if err != nil {
return err
}
w.Sync()
w.f.Close()
log.Printf("wal.cut index=%d prevfile=%s curfile=%s", w.enti, w.f.Name(), f.Name())
// update writer and save the previous crc
w.f = f
w.seq++