log_unstable.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Copyright 2015 The etcd Authors
  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 pb "go.etcd.io/etcd/raft/raftpb"
  16. // unstable.entries[i] has raft log position i+unstable.offset.
  17. // Note that unstable.offset may be less than the highest log
  18. // position in storage; this means that the next write to storage
  19. // might need to truncate the log before persisting unstable.entries.
  20. type unstable struct {
  21. // the incoming unstable snapshot, if any.
  22. snapshot *pb.Snapshot
  23. // all entries that have not yet been written to storage.
  24. entries []pb.Entry
  25. offset uint64
  26. logger Logger
  27. }
  28. // maybeFirstIndex returns the index of the first possible entry in entries
  29. // if it has a snapshot.
  30. func (u *unstable) maybeFirstIndex() (uint64, bool) {
  31. if u.snapshot != nil {
  32. return u.snapshot.Metadata.Index + 1, true
  33. }
  34. return 0, false
  35. }
  36. // maybeLastIndex returns the last index if it has at least one
  37. // unstable entry or snapshot.
  38. func (u *unstable) maybeLastIndex() (uint64, bool) {
  39. if l := len(u.entries); l != 0 {
  40. return u.offset + uint64(l) - 1, true
  41. }
  42. if u.snapshot != nil {
  43. return u.snapshot.Metadata.Index, true
  44. }
  45. return 0, false
  46. }
  47. // maybeTerm returns the term of the entry at index i, if there
  48. // is any.
  49. func (u *unstable) maybeTerm(i uint64) (uint64, bool) {
  50. if i < u.offset {
  51. if u.snapshot != nil && u.snapshot.Metadata.Index == i {
  52. return u.snapshot.Metadata.Term, true
  53. }
  54. return 0, false
  55. }
  56. last, ok := u.maybeLastIndex()
  57. if !ok {
  58. return 0, false
  59. }
  60. if i > last {
  61. return 0, false
  62. }
  63. return u.entries[i-u.offset].Term, true
  64. }
  65. func (u *unstable) stableTo(i, t uint64) {
  66. gt, ok := u.maybeTerm(i)
  67. if !ok {
  68. return
  69. }
  70. // if i < offset, term is matched with the snapshot
  71. // only update the unstable entries if term is matched with
  72. // an unstable entry.
  73. if gt == t && i >= u.offset {
  74. u.entries = u.entries[i+1-u.offset:]
  75. u.offset = i + 1
  76. u.shrinkEntriesArray()
  77. }
  78. }
  79. // shrinkEntriesArray discards the underlying array used by the entries slice
  80. // if most of it isn't being used. This avoids holding references to a bunch of
  81. // potentially large entries that aren't needed anymore. Simply clearing the
  82. // entries wouldn't be safe because clients might still be using them.
  83. func (u *unstable) shrinkEntriesArray() {
  84. // We replace the array if we're using less than half of the space in
  85. // it. This number is fairly arbitrary, chosen as an attempt to balance
  86. // memory usage vs number of allocations. It could probably be improved
  87. // with some focused tuning.
  88. const lenMultiple = 2
  89. if len(u.entries) == 0 {
  90. u.entries = nil
  91. } else if len(u.entries)*lenMultiple < cap(u.entries) {
  92. newEntries := make([]pb.Entry, len(u.entries))
  93. copy(newEntries, u.entries)
  94. u.entries = newEntries
  95. }
  96. }
  97. func (u *unstable) stableSnapTo(i uint64) {
  98. if u.snapshot != nil && u.snapshot.Metadata.Index == i {
  99. u.snapshot = nil
  100. }
  101. }
  102. func (u *unstable) restore(s pb.Snapshot) {
  103. u.offset = s.Metadata.Index + 1
  104. u.entries = nil
  105. u.snapshot = &s
  106. }
  107. func (u *unstable) truncateAndAppend(ents []pb.Entry) {
  108. after := ents[0].Index
  109. switch {
  110. case after == u.offset+uint64(len(u.entries)):
  111. // after is the next index in the u.entries
  112. // directly append
  113. u.entries = append(u.entries, ents...)
  114. case after <= u.offset:
  115. u.logger.Infof("replace the unstable entries from index %d", after)
  116. // The log is being truncated to before our current offset
  117. // portion, so set the offset and replace the entries
  118. u.offset = after
  119. u.entries = ents
  120. default:
  121. // truncate to after and copy to u.entries
  122. // then append
  123. u.logger.Infof("truncate the unstable entries before index %d", after)
  124. u.entries = append([]pb.Entry{}, u.slice(u.offset, after)...)
  125. u.entries = append(u.entries, ents...)
  126. }
  127. }
  128. func (u *unstable) slice(lo uint64, hi uint64) []pb.Entry {
  129. u.mustCheckOutOfBounds(lo, hi)
  130. return u.entries[lo-u.offset : hi-u.offset]
  131. }
  132. // u.offset <= lo <= hi <= u.offset+len(u.entries)
  133. func (u *unstable) mustCheckOutOfBounds(lo, hi uint64) {
  134. if lo > hi {
  135. u.logger.Panicf("invalid unstable.slice %d > %d", lo, hi)
  136. }
  137. upper := u.offset + uint64(len(u.entries))
  138. if lo < u.offset || hi > upper {
  139. u.logger.Panicf("unstable.slice[%d,%d) out of bound [%d,%d]", lo, hi, u.offset, upper)
  140. }
  141. }