log_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. package raft
  2. import (
  3. "reflect"
  4. "testing"
  5. pb "github.com/coreos/etcd/raft/raftpb"
  6. )
  7. // TestAppend ensures:
  8. // 1. If an existing entry conflicts with a new one (same index
  9. // but different terms), delete the existing entry and all that
  10. // follow it
  11. // 2.Append any new entries not already in the log
  12. func TestAppend(t *testing.T) {
  13. previousEnts := []pb.Entry{{Term: 1}, {Term: 2}}
  14. previousUnstable := int64(3)
  15. tests := []struct {
  16. after int64
  17. ents []pb.Entry
  18. windex int64
  19. wents []pb.Entry
  20. wunstable int64
  21. }{
  22. {
  23. 2,
  24. []pb.Entry{},
  25. 2,
  26. []pb.Entry{{Term: 1}, {Term: 2}},
  27. 3,
  28. },
  29. {
  30. 2,
  31. []pb.Entry{{Term: 2}},
  32. 3,
  33. []pb.Entry{{Term: 1}, {Term: 2}, {Term: 2}},
  34. 3,
  35. },
  36. // conflicts with index 1
  37. {
  38. 0,
  39. []pb.Entry{{Term: 2}},
  40. 1,
  41. []pb.Entry{{Term: 2}},
  42. 1,
  43. },
  44. // conflicts with index 2
  45. {
  46. 1,
  47. []pb.Entry{{Term: 3}, {Term: 3}},
  48. 3,
  49. []pb.Entry{{Term: 1}, {Term: 3}, {Term: 3}},
  50. 2,
  51. },
  52. }
  53. for i, tt := range tests {
  54. raftLog := newLog()
  55. raftLog.ents = append(raftLog.ents, previousEnts...)
  56. raftLog.unstable = previousUnstable
  57. index := raftLog.append(tt.after, tt.ents...)
  58. if index != tt.windex {
  59. t.Errorf("#%d: lastIndex = %d, want %d", i, index, tt.windex)
  60. }
  61. if g := raftLog.entries(1); !reflect.DeepEqual(g, tt.wents) {
  62. t.Errorf("#%d: logEnts = %+v, want %+v", i, g, tt.wents)
  63. }
  64. if g := raftLog.unstable; g != tt.wunstable {
  65. t.Errorf("#%d: unstable = %d, want %d", i, g, tt.wunstable)
  66. }
  67. }
  68. }
  69. // TestCompactionSideEffects ensures that all the log related funcationality works correctly after
  70. // a compaction.
  71. func TestCompactionSideEffects(t *testing.T) {
  72. var i int64
  73. lastIndex := int64(1000)
  74. raftLog := newLog()
  75. for i = 0; i < lastIndex; i++ {
  76. raftLog.append(int64(i), pb.Entry{Term: int64(i + 1), Index: int64(i + 1)})
  77. }
  78. raftLog.compact(500)
  79. if raftLog.lastIndex() != lastIndex {
  80. t.Errorf("lastIndex = %d, want %d", raftLog.lastIndex(), lastIndex)
  81. }
  82. for i := raftLog.offset; i <= raftLog.lastIndex(); i++ {
  83. if raftLog.term(i) != i {
  84. t.Errorf("term(%d) = %d, want %d", i, raftLog.term(i), i)
  85. }
  86. }
  87. for i := raftLog.offset; i <= raftLog.lastIndex(); i++ {
  88. if !raftLog.matchTerm(i, i) {
  89. t.Errorf("matchTerm(%d) = false, want true", i)
  90. }
  91. }
  92. unstableEnts := raftLog.unstableEnts()
  93. if g := len(unstableEnts); g != 500 {
  94. t.Errorf("len(unstableEntries) = %d, want = %d", g, 500)
  95. }
  96. if unstableEnts[0].Index != 501 {
  97. t.Errorf("Index = %d, want = %d", unstableEnts[0].Index, 501)
  98. }
  99. prev := raftLog.lastIndex()
  100. raftLog.append(raftLog.lastIndex(), pb.Entry{Term: raftLog.lastIndex() + 1})
  101. if raftLog.lastIndex() != prev+1 {
  102. t.Errorf("lastIndex = %d, want = %d", raftLog.lastIndex(), prev+1)
  103. }
  104. ents := raftLog.entries(raftLog.lastIndex())
  105. if len(ents) != 1 {
  106. t.Errorf("len(entries) = %d, want = %d", len(ents), 1)
  107. }
  108. }
  109. func TestUnstableEnts(t *testing.T) {
  110. previousEnts := []pb.Entry{{Term: 1, Index: 1}, {Term: 2, Index: 2}}
  111. tests := []struct {
  112. unstable int64
  113. wents []pb.Entry
  114. wunstable int64
  115. }{
  116. {3, nil, 3},
  117. {1, previousEnts, 3},
  118. }
  119. for i, tt := range tests {
  120. raftLog := newLog()
  121. raftLog.ents = append(raftLog.ents, previousEnts...)
  122. raftLog.unstable = tt.unstable
  123. ents := raftLog.unstableEnts()
  124. raftLog.resetUnstable()
  125. if !reflect.DeepEqual(ents, tt.wents) {
  126. t.Errorf("#%d: unstableEnts = %+v, want %+v", i, ents, tt.wents)
  127. }
  128. if g := raftLog.unstable; g != tt.wunstable {
  129. t.Errorf("#%d: unstable = %d, want %d", i, g, tt.wunstable)
  130. }
  131. }
  132. }
  133. //TestCompaction ensures that the number of log entreis is correct after compactions.
  134. func TestCompaction(t *testing.T) {
  135. tests := []struct {
  136. app int
  137. compact []int64
  138. wleft []int
  139. wallow bool
  140. }{
  141. // out of upper bound
  142. {1000, []int64{1001}, []int{-1}, false},
  143. {1000, []int64{300, 500, 800, 900}, []int{701, 501, 201, 101}, true},
  144. // out of lower bound
  145. {1000, []int64{300, 299}, []int{701, -1}, false},
  146. }
  147. for i, tt := range tests {
  148. func() {
  149. defer func() {
  150. if r := recover(); r != nil {
  151. if tt.wallow == true {
  152. t.Errorf("%d: allow = %v, want %v", i, false, true)
  153. }
  154. }
  155. }()
  156. raftLog := newLog()
  157. for i := 0; i < tt.app; i++ {
  158. raftLog.append(int64(i), pb.Entry{})
  159. }
  160. for j := 0; j < len(tt.compact); j++ {
  161. raftLog.compact(tt.compact[j])
  162. if len(raftLog.ents) != tt.wleft[j] {
  163. t.Errorf("#%d.%d len = %d, want %d", i, j, len(raftLog.ents), tt.wleft[j])
  164. }
  165. }
  166. }()
  167. }
  168. }
  169. func TestLogRestore(t *testing.T) {
  170. var i int64
  171. raftLog := newLog()
  172. for i = 0; i < 100; i++ {
  173. raftLog.append(i, pb.Entry{Term: i + 1})
  174. }
  175. index := int64(1000)
  176. term := int64(1000)
  177. raftLog.restore(pb.Snapshot{Index: index, Term: term})
  178. // only has the guard entry
  179. if len(raftLog.ents) != 1 {
  180. t.Errorf("len = %d, want 0", len(raftLog.ents))
  181. }
  182. if raftLog.offset != index {
  183. t.Errorf("offset = %d, want %d", raftLog.offset, index)
  184. }
  185. if raftLog.applied != index {
  186. t.Errorf("applied = %d, want %d", raftLog.applied, index)
  187. }
  188. if raftLog.committed != index {
  189. t.Errorf("comitted = %d, want %d", raftLog.committed, index)
  190. }
  191. if raftLog.unstable != index+1 {
  192. t.Errorf("unstable = %d, want %d", raftLog.unstable, index+1)
  193. }
  194. if raftLog.term(index) != term {
  195. t.Errorf("term = %d, want %d", raftLog.term(index), term)
  196. }
  197. }
  198. func TestIsOutOfBounds(t *testing.T) {
  199. offset := int64(100)
  200. num := int64(100)
  201. l := &raftLog{offset: offset, ents: make([]pb.Entry, num)}
  202. tests := []struct {
  203. index int64
  204. w bool
  205. }{
  206. {offset - 1, true},
  207. {offset, false},
  208. {offset + num/2, false},
  209. {offset + num - 1, false},
  210. {offset + num, true},
  211. }
  212. for i, tt := range tests {
  213. g := l.isOutOfBounds(tt.index)
  214. if g != tt.w {
  215. t.Errorf("#%d: isOutOfBounds = %v, want %v", i, g, tt.w)
  216. }
  217. }
  218. }
  219. func TestAt(t *testing.T) {
  220. var i int64
  221. offset := int64(100)
  222. num := int64(100)
  223. l := &raftLog{offset: offset}
  224. for i = 0; i < num; i++ {
  225. l.ents = append(l.ents, pb.Entry{Term: i})
  226. }
  227. tests := []struct {
  228. index int64
  229. w *pb.Entry
  230. }{
  231. {offset - 1, nil},
  232. {offset, &pb.Entry{Term: 0}},
  233. {offset + num/2, &pb.Entry{Term: num / 2}},
  234. {offset + num - 1, &pb.Entry{Term: num - 1}},
  235. {offset + num, nil},
  236. }
  237. for i, tt := range tests {
  238. g := l.at(tt.index)
  239. if !reflect.DeepEqual(g, tt.w) {
  240. t.Errorf("#%d: at = %v, want %v", i, g, tt.w)
  241. }
  242. }
  243. }
  244. func TestSlice(t *testing.T) {
  245. var i int64
  246. offset := int64(100)
  247. num := int64(100)
  248. l := &raftLog{offset: offset}
  249. for i = 0; i < num; i++ {
  250. l.ents = append(l.ents, pb.Entry{Term: i})
  251. }
  252. tests := []struct {
  253. from int64
  254. to int64
  255. w []pb.Entry
  256. }{
  257. {offset - 1, offset + 1, nil},
  258. {offset, offset + 1, []pb.Entry{{Term: 0}}},
  259. {offset + num/2, offset + num/2 + 1, []pb.Entry{{Term: num / 2}}},
  260. {offset + num - 1, offset + num, []pb.Entry{{Term: num - 1}}},
  261. {offset + num, offset + num + 1, nil},
  262. {offset + num/2, offset + num/2, nil},
  263. {offset + num/2, offset + num/2 - 1, nil},
  264. }
  265. for i, tt := range tests {
  266. g := l.slice(tt.from, tt.to)
  267. if !reflect.DeepEqual(g, tt.w) {
  268. t.Errorf("#%d: from %d to %d = %v, want %v", i, tt.from, tt.to, g, tt.w)
  269. }
  270. }
  271. }