doc.go 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. /*
  14. Package wal provides an implementation of a write ahead log that is used by
  15. etcd.
  16. A WAL is created at a particular directory and is made up of a number of
  17. discrete WAL files. Inside of each file the raft state and entries are appended
  18. to it with the Save method:
  19. metadata := []byte{}
  20. w, err := wal.Create("/var/lib/etcd", metadata)
  21. ...
  22. err := w.Save(s, ents)
  23. When a user has finished using a WAL it must be closed:
  24. w.Close()
  25. WAL files are placed inside of the directory in the following format:
  26. $seq-$index.wal
  27. The first WAL file to be created will be 0000000000000000-0000000000000000.wal
  28. indicating an initial sequence of 0 and an initial raft index of 0. The first
  29. entry written to WAL MUST have raft index 0.
  30. Periodically a user will want to "cut" the WAL and place new entries into a new
  31. file. This will increment an internal sequence number and cause a new file to
  32. be created. If the last raft index saved was 0x20 and this is the first time
  33. Cut has been called on this WAL then the sequence will increment from 0x0 to
  34. 0x1. The new file will be: 0000000000000001-0000000000000021.wal. If a second
  35. Cut issues 0x10 entries with incremental index later then the file will be called:
  36. 0000000000000002-0000000000000031.wal.
  37. At a later time a WAL can be opened at a particular raft index:
  38. w, err := wal.OpenAtIndex("/var/lib/etcd", 0)
  39. ...
  40. The raft index must have been written to the WAL. When opening without a
  41. snapshot the raft index should always be 0. When opening with a snapshot
  42. the raft index should be the index of the last entry covered by the snapshot.
  43. Additional items cannot be Saved to this WAL until all of the items from 0 to
  44. the end of the WAL are read first:
  45. id, state, ents, err := w.ReadAll()
  46. This will give you the raft node id, the last raft.State and the slice of
  47. raft.Entry items in the log.
  48. */
  49. package wal