wal.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. package wal
  14. import (
  15. "errors"
  16. "fmt"
  17. "hash/crc32"
  18. "io"
  19. "log"
  20. "os"
  21. "path"
  22. "sort"
  23. "github.com/coreos/etcd/raft/raftpb"
  24. "github.com/coreos/etcd/wal/walpb"
  25. )
  26. const (
  27. infoType int64 = iota + 1
  28. entryType
  29. stateType
  30. crcType
  31. // the owner can make/remove files inside the directory
  32. privateDirMode = 0700
  33. )
  34. var (
  35. ErrIdMismatch = errors.New("wal: unmatch id")
  36. ErrNotFound = errors.New("wal: file is not found")
  37. ErrCRCMismatch = errors.New("wal: crc mismatch")
  38. crcTable = crc32.MakeTable(crc32.Castagnoli)
  39. )
  40. // WAL is a logical repersentation of the stable storage.
  41. // WAL is either in read mode or append mode but not both.
  42. // A newly created WAL is in append mode, and ready for appending records.
  43. // A just opened WAL is in read mode, and ready for reading records.
  44. // The WAL will be ready for appending after reading out all the previous records.
  45. type WAL struct {
  46. dir string // the living directory of the underlay files
  47. ri int64 // index of entry to start reading
  48. decoder *decoder // decoder to decode records
  49. f *os.File // underlay file opened for appending, sync
  50. seq int64 // current sequence of the wal file
  51. encoder *encoder // encoder to encode records
  52. }
  53. // Create creates a WAL ready for appending records.
  54. func Create(dirpath string) (*WAL, error) {
  55. log.Printf("path=%s wal.create", dirpath)
  56. if Exist(dirpath) {
  57. return nil, os.ErrExist
  58. }
  59. if err := os.MkdirAll(dirpath, privateDirMode); err != nil {
  60. return nil, err
  61. }
  62. p := path.Join(dirpath, fmt.Sprintf("%016x-%016x.wal", 0, 0))
  63. f, err := os.OpenFile(p, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
  64. if err != nil {
  65. return nil, err
  66. }
  67. w := &WAL{
  68. dir: dirpath,
  69. seq: 0,
  70. f: f,
  71. encoder: newEncoder(f, 0),
  72. }
  73. if err := w.saveCrc(0); err != nil {
  74. return nil, err
  75. }
  76. return w, nil
  77. }
  78. // OpenFromIndex opens the WAL files containing all the entries after
  79. // the given index.
  80. // The returned WAL is ready to read. The WAL cannot be appended to before
  81. // reading out all of its previous records.
  82. func OpenFromIndex(dirpath string, index int64) (*WAL, error) {
  83. log.Printf("path=%s wal.load index=%d", dirpath, index)
  84. names, err := readDir(dirpath)
  85. if err != nil {
  86. return nil, err
  87. }
  88. names = checkWalNames(names)
  89. if len(names) == 0 {
  90. return nil, ErrNotFound
  91. }
  92. sort.Sort(sort.StringSlice(names))
  93. nameIndex, ok := searchIndex(names, index)
  94. if !ok || !isValidSeq(names[nameIndex:]) {
  95. return nil, ErrNotFound
  96. }
  97. // open the wal files for reading
  98. rcs := make([]io.ReadCloser, 0)
  99. for _, name := range names[nameIndex:] {
  100. f, err := os.Open(path.Join(dirpath, name))
  101. if err != nil {
  102. return nil, err
  103. }
  104. rcs = append(rcs, f)
  105. }
  106. rc := MultiReadCloser(rcs...)
  107. // open the lastest wal file for appending
  108. last := path.Join(dirpath, names[len(names)-1])
  109. f, err := os.OpenFile(last, os.O_WRONLY|os.O_APPEND, 0)
  110. if err != nil {
  111. rc.Close()
  112. return nil, err
  113. }
  114. // create a WAL ready for reading
  115. w := &WAL{
  116. ri: index,
  117. decoder: newDecoder(rc),
  118. f: f,
  119. }
  120. return w, nil
  121. }
  122. // ReadAll reads out all records of the current WAL.
  123. // After ReadAll, the WAL will be ready for appending new records.
  124. func (w *WAL) ReadAll() (id int64, state raftpb.State, ents []raftpb.Entry, err error) {
  125. rec := &walpb.Record{}
  126. decoder := w.decoder
  127. for err = decoder.decode(rec); err == nil; err = decoder.decode(rec) {
  128. switch rec.Type {
  129. case entryType:
  130. e := mustUnmarshalEntry(rec.Data)
  131. if e.Index > w.ri {
  132. ents = append(ents[:e.Index-w.ri-1], e)
  133. }
  134. case stateType:
  135. state = mustUnmarshalState(rec.Data)
  136. case infoType:
  137. i := mustUnmarshalInfo(rec.Data)
  138. if id != 0 && id != i.Id {
  139. state.Reset()
  140. return 0, state, nil, ErrIdMismatch
  141. }
  142. id = i.Id
  143. case crcType:
  144. crc := decoder.crc.Sum32()
  145. // current crc of decoder must match the crc of the record.
  146. // do no need to match 0 crc, since the decoder is a new one at this case.
  147. if crc != 0 && rec.Validate(crc) != nil {
  148. state.Reset()
  149. return 0, state, nil, ErrCRCMismatch
  150. }
  151. decoder.updateCRC(rec.Crc)
  152. default:
  153. state.Reset()
  154. return 0, state, nil, fmt.Errorf("unexpected block type %d", rec.Type)
  155. }
  156. }
  157. if err != io.EOF {
  158. state.Reset()
  159. return 0, state, nil, err
  160. }
  161. // close decoder, disable reading
  162. w.decoder.close()
  163. w.ri = 0
  164. // create encoder (chain crc with the decoder), enable appending
  165. w.encoder = newEncoder(w.f, w.decoder.lastCRC())
  166. w.decoder = nil
  167. return id, state, ents, nil
  168. }
  169. // index should be the index of last log entry.
  170. // Cut closes current file written and creates a new one ready to append.
  171. func (w *WAL) Cut(index int64) error {
  172. log.Printf("wal.cut index=%d", index)
  173. // create a new wal file with name sequence + 1
  174. fpath := path.Join(w.dir, fmt.Sprintf("%016x-%016x.wal", w.seq+1, index))
  175. f, err := os.OpenFile(fpath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0600)
  176. if err != nil {
  177. return err
  178. }
  179. w.Sync()
  180. w.f.Close()
  181. // update writer and save the previous crc
  182. w.f = f
  183. w.seq++
  184. prevCrc := w.encoder.crc.Sum32()
  185. w.encoder = newEncoder(w.f, prevCrc)
  186. return w.saveCrc(prevCrc)
  187. }
  188. func (w *WAL) Sync() error {
  189. if w.encoder != nil {
  190. if err := w.encoder.flush(); err != nil {
  191. return err
  192. }
  193. }
  194. return w.f.Sync()
  195. }
  196. func (w *WAL) Close() {
  197. log.Printf("path=%s wal.close", w.f.Name())
  198. if w.f != nil {
  199. w.Sync()
  200. w.f.Close()
  201. }
  202. }
  203. func (w *WAL) SaveInfo(i *raftpb.Info) error {
  204. log.Printf("path=%s wal.saveInfo id=%d", w.f.Name(), i.Id)
  205. b, err := i.Marshal()
  206. if err != nil {
  207. panic(err)
  208. }
  209. rec := &walpb.Record{Type: infoType, Data: b}
  210. return w.encoder.encode(rec)
  211. }
  212. func (w *WAL) SaveEntry(e *raftpb.Entry) error {
  213. b, err := e.Marshal()
  214. if err != nil {
  215. panic(err)
  216. }
  217. rec := &walpb.Record{Type: entryType, Data: b}
  218. return w.encoder.encode(rec)
  219. }
  220. func (w *WAL) SaveState(s *raftpb.State) error {
  221. log.Printf("path=%s wal.saveState state=\"%+v\"", w.f.Name(), s)
  222. b, err := s.Marshal()
  223. if err != nil {
  224. panic(err)
  225. }
  226. rec := &walpb.Record{Type: stateType, Data: b}
  227. return w.encoder.encode(rec)
  228. }
  229. func (w *WAL) Save(st raftpb.State, ents []raftpb.Entry) {
  230. // TODO(xiangli): no more reference operator
  231. w.SaveState(&st)
  232. for i := range ents {
  233. w.SaveEntry(&ents[i])
  234. }
  235. w.Sync()
  236. }
  237. func (w *WAL) saveCrc(prevCrc uint32) error {
  238. return w.encoder.encode(&walpb.Record{Type: crcType, Crc: prevCrc})
  239. }