log.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. package raft
  2. import "fmt"
  3. const (
  4. Normal int64 = iota
  5. AddNode
  6. RemoveNode
  7. )
  8. const (
  9. defaultCompactThreshold = 10000
  10. )
  11. type Entry struct {
  12. Type int64
  13. Term int64
  14. Data []byte
  15. }
  16. func (e *Entry) isConfig() bool {
  17. return e.Type == AddNode || e.Type == RemoveNode
  18. }
  19. type log struct {
  20. ents []Entry
  21. committed int64
  22. applied int64
  23. offset int64
  24. // want a compact after the number of entries exceeds the threshold
  25. // TODO(xiangli) size might be a better criteria
  26. compactThreshold int64
  27. }
  28. func newLog() *log {
  29. return &log{
  30. ents: make([]Entry, 1),
  31. committed: 0,
  32. applied: 0,
  33. compactThreshold: defaultCompactThreshold,
  34. }
  35. }
  36. func (l *log) maybeAppend(index, logTerm, committed int64, ents ...Entry) bool {
  37. if l.matchTerm(index, logTerm) {
  38. l.append(index, ents...)
  39. l.committed = committed
  40. return true
  41. }
  42. return false
  43. }
  44. func (l *log) append(after int64, ents ...Entry) int64 {
  45. l.ents = append(l.slice(l.offset, after+1), ents...)
  46. return l.lastIndex()
  47. }
  48. func (l *log) lastIndex() int64 {
  49. return int64(len(l.ents)) - 1 + l.offset
  50. }
  51. func (l *log) term(i int64) int64 {
  52. if e := l.at(i); e != nil {
  53. return e.Term
  54. }
  55. return -1
  56. }
  57. func (l *log) entries(i int64) []Entry {
  58. // never send out the first entry
  59. // first entry is only used for matching
  60. // prevLogTerm
  61. if i == l.offset {
  62. panic("cannot return the first entry in log")
  63. }
  64. return l.slice(i, l.lastIndex()+1)
  65. }
  66. func (l *log) isUpToDate(i, term int64) bool {
  67. e := l.at(l.lastIndex())
  68. return term > e.Term || (term == e.Term && i >= l.lastIndex())
  69. }
  70. func (l *log) matchTerm(i, term int64) bool {
  71. if e := l.at(i); e != nil {
  72. return e.Term == term
  73. }
  74. return false
  75. }
  76. func (l *log) maybeCommit(maxIndex, term int64) bool {
  77. if maxIndex > l.committed && l.term(maxIndex) == term {
  78. l.committed = maxIndex
  79. return true
  80. }
  81. return false
  82. }
  83. // nextEnts returns all the available entries for execution.
  84. // all the returned entries will be marked as applied.
  85. func (l *log) nextEnts() (ents []Entry) {
  86. if l.committed > l.applied {
  87. ents = l.slice(l.applied+1, l.committed+1)
  88. l.applied = l.committed
  89. }
  90. return ents
  91. }
  92. // compact removes the log entries before i, exclusive.
  93. // i must be not smaller than the index of the first entry
  94. // and not greater than the index of the last entry.
  95. // the number of entries after compaction will be returned.
  96. func (l *log) compact(i int64) int64 {
  97. if l.isOutOfBounds(i) {
  98. panic(fmt.Sprintf("compact %d out of bounds [%d:%d]", i, l.offset, l.lastIndex()))
  99. }
  100. l.ents = l.slice(i, l.lastIndex()+1)
  101. l.offset = i
  102. return int64(len(l.ents))
  103. }
  104. func (l *log) shouldCompact() bool {
  105. return (l.applied - l.offset) > l.compactThreshold
  106. }
  107. func (l *log) restore(index, term int64) {
  108. l.ents = []Entry{{Term: term}}
  109. l.committed = index
  110. l.applied = index
  111. l.offset = index
  112. }
  113. func (l *log) at(i int64) *Entry {
  114. if l.isOutOfBounds(i) {
  115. return nil
  116. }
  117. return &l.ents[i-l.offset]
  118. }
  119. // slice get a slice of log entries from lo through hi-1, inclusive.
  120. func (l *log) slice(lo int64, hi int64) []Entry {
  121. if lo >= hi {
  122. return nil
  123. }
  124. if l.isOutOfBounds(lo) || l.isOutOfBounds(hi-1) {
  125. return nil
  126. }
  127. return l.ents[lo-l.offset : hi-l.offset]
  128. }
  129. func (l *log) isOutOfBounds(i int64) bool {
  130. if i < l.offset || i > l.lastIndex() {
  131. return true
  132. }
  133. return false
  134. }