Merge pull request #1054 from philips/add-wal-doc

wal: add a doc file
release-2.0
Xiang Li 2014-09-11 17:18:53 -07:00
commit 81b5967e0a
4 changed files with 71 additions and 12 deletions

View File

@ -107,7 +107,7 @@ func startRaft(id int64, peerIDs []int64, waldir string) (raft.Node, *wal.WAL) {
// restart a node from previous wal
// TODO(xiangli): check snapshot; not open from zero
w, err := wal.OpenFromIndex(waldir, 0)
w, err := wal.OpenAtIndex(waldir, 0)
if err != nil {
log.Fatal(err)
}

59
wal/doc.go Normal file
View File

@ -0,0 +1,59 @@
// Copyright 2014 CoreOS Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
/*
Package wal provides an implementation of a write ahead log that is used by
etcd.
A WAL is created at a particular directory and is made up of a number of
discrete WAL files. Inside of each file the raft state and entries are appended
to it with the Save method:
w, err := wal.Create("/var/lib/etcd")
...
err := w.Save(s, ents)
When a user has finished using a WAL it must be closed:
w.Close()
WAL files are placed inside of the directory in the following format:
$seq-$index.wal
The first WAL file to be created will be 0000000000000000-0000000000000000.wal
indicating an initial sequence of 0 and an initial raft index of 0.
Periodically a user will want to "cut" the WAL and place new entries into a new
file. This will increment an internal sequence number and cause a new file to
be created. If the last raft index saved was 0x20 and this is the first time
Cut has been called on this WAL then the sequence will increment from 0x0 to
0x1. The new file will be: 0000000000000001-0000000000000020.wal. If a second
Cut is issues 0x10 entries later then the file will be called:
0000000000000002-0000000000000030.wal.
At a later time a WAL can be opened at a particular raft index:
w, err := wal.OpenAtIndex("/var/lib/etcd", 0)
...
Additional items cannot be Saved to this WAL until all of the items from 0 to
the end of the WAL are read first:
id, state, ents, err := w.ReadAll()
This will give you the raft node id, the last raft.State and the slice of
raft.Entry items in the log.
*/
package wal

View File

@ -92,11 +92,11 @@ func Create(dirpath string) (*WAL, error) {
return w, nil
}
// OpenFromIndex opens the WAL files containing all the entries after
// the given index.
// The returned WAL is ready to read. The WAL cannot be appended to before
// reading out all of its previous records.
func OpenFromIndex(dirpath string, index int64) (*WAL, error) {
// OpenAtIndex opens the WAL at the given index.
// 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.
func OpenAtIndex(dirpath string, index int64) (*WAL, error) {
log.Printf("path=%s wal.load index=%d", dirpath, index)
names, err := readDir(dirpath)
if err != nil {

View File

@ -65,7 +65,7 @@ func TestNewForInitedDir(t *testing.T) {
}
}
func TestOpenFromIndex(t *testing.T) {
func TestOpenAtIndex(t *testing.T) {
dir, err := ioutil.TempDir(os.TempDir(), "waltest")
if err != nil {
t.Fatal(err)
@ -78,7 +78,7 @@ func TestOpenFromIndex(t *testing.T) {
}
f.Close()
w, err := OpenFromIndex(dir, 0)
w, err := OpenAtIndex(dir, 0)
if err != nil {
t.Fatalf("err = %v, want nil", err)
}
@ -94,7 +94,7 @@ func TestOpenFromIndex(t *testing.T) {
}
f.Close()
w, err = OpenFromIndex(dir, 5)
w, err = OpenAtIndex(dir, 5)
if err != nil {
t.Fatalf("err = %v, want nil", err)
}
@ -108,7 +108,7 @@ func TestOpenFromIndex(t *testing.T) {
t.Fatal(err)
}
defer os.RemoveAll(emptydir)
if _, err = OpenFromIndex(emptydir, 0); err != ErrNotFound {
if _, err = OpenAtIndex(emptydir, 0); err != ErrNotFound {
t.Errorf("err = %v, want %v", err, ErrNotFound)
}
}
@ -176,7 +176,7 @@ func TestRecover(t *testing.T) {
}
w.Close()
if w, err = OpenFromIndex(p, 0); err != nil {
if w, err = OpenAtIndex(p, 0); err != nil {
t.Fatal(err)
}
id, state, entries, err := w.ReadAll()
@ -301,7 +301,7 @@ func TestRecoverAfterCut(t *testing.T) {
}
for i := 0; i < 10; i++ {
w, err := OpenFromIndex(p, int64(i))
w, err := OpenAtIndex(p, int64(i))
if i <= 3 {
if err != ErrNotFound {
t.Errorf("#%d: err = %v, want %v", i, err, ErrNotFound)