log_unstable.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 pb "github.com/coreos/etcd/raft/raftpb"
  16. // unstable.entris[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. }
  27. // maybeFirstIndex returns the index of the first possible entry in entries
  28. // if it has a snapshot.
  29. func (u *unstable) maybeFirstIndex() (uint64, bool) {
  30. if u.snapshot != nil {
  31. return u.snapshot.Metadata.Index + 1, true
  32. }
  33. return 0, false
  34. }
  35. // maybeLastIndex returns the last index if it has at least one
  36. // unstable entry or snapshot.
  37. func (u *unstable) maybeLastIndex() (uint64, bool) {
  38. if l := len(u.entries); l != 0 {
  39. return u.offset + uint64(l) - 1, true
  40. }
  41. if u.snapshot != nil {
  42. return u.snapshot.Metadata.Index, true
  43. }
  44. return 0, false
  45. }
  46. // myabeTerm returns the term of the entry at index i, if there
  47. // is any.
  48. func (u *unstable) maybeTerm(i uint64) (uint64, bool) {
  49. if i < u.offset {
  50. if u.snapshot == nil {
  51. return 0, false
  52. }
  53. if u.snapshot.Metadata.Index == i {
  54. return u.snapshot.Metadata.Term, true
  55. }
  56. return 0, false
  57. }
  58. last, ok := u.maybeLastIndex()
  59. if !ok {
  60. return 0, false
  61. }
  62. if i > last {
  63. return 0, false
  64. }
  65. return u.entries[i-u.offset].Term, true
  66. }
  67. func (u *unstable) stableTo(i, t uint64) {
  68. gt, ok := u.maybeTerm(i)
  69. if !ok {
  70. return
  71. }
  72. // if i < offest, term is matched with the snapshot
  73. // only update the unstalbe entries if term is matched with
  74. // an unstable entry.
  75. if gt == t && i >= u.offset {
  76. u.entries = u.entries[i+1-u.offset:]
  77. u.offset = i + 1
  78. }
  79. }
  80. func (u *unstable) stableSnapTo(i uint64) {
  81. if u.snapshot != nil && u.snapshot.Metadata.Index == i {
  82. u.snapshot = nil
  83. }
  84. }
  85. func (u *unstable) restore(s pb.Snapshot) {
  86. u.offset = s.Metadata.Index + 1
  87. u.entries = nil
  88. u.snapshot = &s
  89. }
  90. func (u *unstable) truncateAndAppend(ents []pb.Entry) {
  91. after := ents[0].Index - 1
  92. switch {
  93. case after == u.offset+uint64(len(u.entries))-1:
  94. // after is the last index in the u.entries
  95. // directly append
  96. u.entries = append(u.entries, ents...)
  97. case after < u.offset:
  98. raftLogger.Infof("replace the unstable entries from index %d", after+1)
  99. // The log is being truncated to before our current offset
  100. // portion, so set the offset and replace the entries
  101. u.offset = after + 1
  102. u.entries = ents
  103. default:
  104. // truncate to after and copy to u.entries
  105. // then append
  106. raftLogger.Infof("truncate the unstable entries to index %d", after)
  107. u.entries = append([]pb.Entry{}, u.slice(u.offset, after+1)...)
  108. u.entries = append(u.entries, ents...)
  109. }
  110. }
  111. func (u *unstable) slice(lo uint64, hi uint64) []pb.Entry {
  112. u.mustCheckOutOfBounds(lo, hi)
  113. return u.entries[lo-u.offset : hi-u.offset]
  114. }
  115. // u.offset <= lo <= hi <= u.offset+len(u.offset)
  116. func (u *unstable) mustCheckOutOfBounds(lo, hi uint64) {
  117. if lo > hi {
  118. raftLogger.Panicf("invalid unstable.slice %d > %d", lo, hi)
  119. }
  120. upper := u.offset + uint64(len(u.entries))
  121. if lo < u.offset || hi > upper {
  122. raftLogger.Panicf("unstable.slice[%d,%d) out of bound [%d,%d]", lo, hi, u.offset, upper)
  123. }
  124. }