storage.go 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2015 CoreOS, Inc.
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package raft
  15. import (
  16. "errors"
  17. "log"
  18. "sync"
  19. pb "github.com/coreos/etcd/raft/raftpb"
  20. )
  21. // ErrCompacted is returned by Storage.Entries/Compact when a requested
  22. // index is unavailable because it predates the last snapshot.
  23. var ErrCompacted = errors.New("requested index is unavailable due to compaction")
  24. var ErrUnavailable = errors.New("requested entry at index is unavailable")
  25. // Storage is an interface that may be implemented by the application
  26. // to retrieve log entries from storage.
  27. //
  28. // If any Storage method returns an error, the raft instance will
  29. // become inoperable and refuse to participate in elections; the
  30. // application is responsible for cleanup and recovery in this case.
  31. type Storage interface {
  32. // InitialState returns the saved HardState and ConfState information.
  33. InitialState() (pb.HardState, pb.ConfState, error)
  34. // Entries returns a slice of log entries in the range [lo,hi).
  35. Entries(lo, hi uint64) ([]pb.Entry, error)
  36. // Term returns the term of entry i, which must be in the range
  37. // [FirstIndex()-1, LastIndex()]. The term of the entry before
  38. // FirstIndex is retained for matching purposes even though the
  39. // rest of that entry may not be available.
  40. Term(i uint64) (uint64, error)
  41. // LastIndex returns the index of the last entry in the log.
  42. LastIndex() (uint64, error)
  43. // FirstIndex returns the index of the first log entry that is
  44. // possibly available via Entries (older entries have been incorporated
  45. // into the latest Snapshot; if storage only contains the dummy entry the
  46. // first log entry is not available).
  47. FirstIndex() (uint64, error)
  48. // Snapshot returns the most recent snapshot.
  49. Snapshot() (pb.Snapshot, error)
  50. }
  51. // MemoryStorage implements the Storage interface backed by an
  52. // in-memory array.
  53. type MemoryStorage struct {
  54. // Protects access to all fields. Most methods of MemoryStorage are
  55. // run on the raft goroutine, but Append() is run on an application
  56. // goroutine.
  57. sync.Mutex
  58. hardState pb.HardState
  59. snapshot pb.Snapshot
  60. // ents[i] has raft log position i+snapshot.Metadata.Index
  61. ents []pb.Entry
  62. }
  63. // NewMemoryStorage creates an empty MemoryStorage.
  64. func NewMemoryStorage() *MemoryStorage {
  65. return &MemoryStorage{
  66. // When starting from scratch populate the list with a dummy entry at term zero.
  67. ents: make([]pb.Entry, 1),
  68. }
  69. }
  70. // InitialState implements the Storage interface.
  71. func (ms *MemoryStorage) InitialState() (pb.HardState, pb.ConfState, error) {
  72. return ms.hardState, ms.snapshot.Metadata.ConfState, nil
  73. }
  74. // SetHardState saves the current HardState.
  75. func (ms *MemoryStorage) SetHardState(st pb.HardState) error {
  76. ms.hardState = st
  77. return nil
  78. }
  79. // Entries implements the Storage interface.
  80. func (ms *MemoryStorage) Entries(lo, hi uint64) ([]pb.Entry, error) {
  81. ms.Lock()
  82. defer ms.Unlock()
  83. offset := ms.snapshot.Metadata.Index
  84. if lo <= offset {
  85. return nil, ErrCompacted
  86. }
  87. // only contains dummy entries.
  88. if len(ms.ents) == 1 {
  89. return nil, ErrUnavailable
  90. }
  91. return ms.ents[lo-offset : hi-offset], nil
  92. }
  93. // Term implements the Storage interface.
  94. func (ms *MemoryStorage) Term(i uint64) (uint64, error) {
  95. ms.Lock()
  96. defer ms.Unlock()
  97. offset := ms.snapshot.Metadata.Index
  98. if i < offset {
  99. return 0, ErrCompacted
  100. }
  101. return ms.ents[i-offset].Term, nil
  102. }
  103. // LastIndex implements the Storage interface.
  104. func (ms *MemoryStorage) LastIndex() (uint64, error) {
  105. ms.Lock()
  106. defer ms.Unlock()
  107. return ms.snapshot.Metadata.Index + uint64(len(ms.ents)) - 1, nil
  108. }
  109. // FirstIndex implements the Storage interface.
  110. func (ms *MemoryStorage) FirstIndex() (uint64, error) {
  111. ms.Lock()
  112. defer ms.Unlock()
  113. return ms.snapshot.Metadata.Index + 1, nil
  114. }
  115. // Snapshot implements the Storage interface.
  116. func (ms *MemoryStorage) Snapshot() (pb.Snapshot, error) {
  117. ms.Lock()
  118. defer ms.Unlock()
  119. return ms.snapshot, nil
  120. }
  121. // ApplySnapshot overwrites the contents of this Storage object with
  122. // those of the given snapshot.
  123. func (ms *MemoryStorage) ApplySnapshot(snap pb.Snapshot) error {
  124. ms.Lock()
  125. defer ms.Unlock()
  126. ms.snapshot = snap
  127. ms.ents = []pb.Entry{{Term: snap.Metadata.Term, Index: snap.Metadata.Index}}
  128. return nil
  129. }
  130. // Compact discards all log entries prior to i. Creates a snapshot
  131. // which can be retrieved with the Snapshot() method and can be used
  132. // to reconstruct the state at that point.
  133. // If any configuration changes have been made since the last compaction,
  134. // the result of the last ApplyConfChange must be passed in.
  135. // It is the application's responsibility to not attempt to compact an index
  136. // greater than raftLog.applied.
  137. func (ms *MemoryStorage) Compact(i uint64, cs *pb.ConfState, data []byte) error {
  138. ms.Lock()
  139. defer ms.Unlock()
  140. offset := ms.snapshot.Metadata.Index
  141. if i <= offset {
  142. return ErrCompacted
  143. }
  144. if i > offset+uint64(len(ms.ents))-1 {
  145. log.Panicf("compact %d is out of bound lastindex(%d)", i, offset+uint64(len(ms.ents))-1)
  146. }
  147. i -= offset
  148. ents := make([]pb.Entry, 1, 1+uint64(len(ms.ents))-i)
  149. ents[0].Term = ms.ents[i].Term
  150. ents = append(ents, ms.ents[i+1:]...)
  151. ms.ents = ents
  152. ms.snapshot.Metadata.Index += i
  153. ms.snapshot.Metadata.Term = ents[0].Term
  154. if cs != nil {
  155. ms.snapshot.Metadata.ConfState = *cs
  156. }
  157. ms.snapshot.Data = data
  158. return nil
  159. }
  160. // Append the new entries to storage.
  161. func (ms *MemoryStorage) Append(entries []pb.Entry) error {
  162. ms.Lock()
  163. defer ms.Unlock()
  164. if len(entries) == 0 {
  165. return nil
  166. }
  167. first := ms.snapshot.Metadata.Index + 1
  168. last := entries[0].Index + uint64(len(entries)) - 1
  169. // shortcut if there is no new entry.
  170. if last < first {
  171. return nil
  172. }
  173. // truncate old entries
  174. if first > entries[0].Index {
  175. entries = entries[first-entries[0].Index:]
  176. }
  177. offset := entries[0].Index - ms.snapshot.Metadata.Index
  178. switch {
  179. case uint64(len(ms.ents)) > offset:
  180. ms.ents = append([]pb.Entry{}, ms.ents[:offset]...)
  181. ms.ents = append(ms.ents, entries...)
  182. case uint64(len(ms.ents)) == offset:
  183. ms.ents = append(ms.ents, entries...)
  184. default:
  185. log.Panicf("missing log entry [last: %d, append at: %d]",
  186. ms.snapshot.Metadata.Index+uint64(len(ms.ents)), entries[0].Index)
  187. }
  188. return nil
  189. }