log_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package raft
  14. import (
  15. "reflect"
  16. "testing"
  17. pb "github.com/coreos/etcd/raft/raftpb"
  18. )
  19. func TestFindConflict(t *testing.T) {
  20. previousEnts := []pb.Entry{{Term: 1}, {Term: 2}, {Term: 3}}
  21. tests := []struct {
  22. from uint64
  23. ents []pb.Entry
  24. wconflict uint64
  25. }{
  26. // no conflict, empty ent
  27. {1, []pb.Entry{}, 0},
  28. {3, []pb.Entry{}, 0},
  29. // no conflict
  30. {1, []pb.Entry{{Term: 1}, {Term: 2}, {Term: 3}}, 0},
  31. {2, []pb.Entry{{Term: 2}, {Term: 3}}, 0},
  32. {3, []pb.Entry{{Term: 3}}, 0},
  33. // no conflict, but has new entries
  34. {1, []pb.Entry{{Term: 1}, {Term: 2}, {Term: 3}, {Term: 4}, {Term: 4}}, 4},
  35. {2, []pb.Entry{{Term: 2}, {Term: 3}, {Term: 4}, {Term: 4}}, 4},
  36. {3, []pb.Entry{{Term: 3}, {Term: 4}, {Term: 4}}, 4},
  37. {4, []pb.Entry{{Term: 4}, {Term: 4}}, 4},
  38. // conflicts with existing entries
  39. {1, []pb.Entry{{Term: 4}, {Term: 4}}, 1},
  40. {2, []pb.Entry{{Term: 1}, {Term: 4}, {Term: 4}}, 2},
  41. {3, []pb.Entry{{Term: 1}, {Term: 2}, {Term: 4}, {Term: 4}}, 3},
  42. }
  43. for i, tt := range tests {
  44. raftLog := newLog()
  45. raftLog.append(raftLog.lastIndex(), previousEnts...)
  46. gconflict := raftLog.findConflict(tt.from, tt.ents)
  47. if gconflict != tt.wconflict {
  48. t.Errorf("#%d: conflict = %d, want %d", i, gconflict, tt.wconflict)
  49. }
  50. }
  51. }
  52. func TestIsUpToDate(t *testing.T) {
  53. previousEnts := []pb.Entry{{Term: 1}, {Term: 2}, {Term: 3}}
  54. raftLog := newLog()
  55. raftLog.append(raftLog.lastIndex(), previousEnts...)
  56. tests := []struct {
  57. lastIndex uint64
  58. term uint64
  59. wUpToDate bool
  60. }{
  61. // greater term, ignore lastIndex
  62. {raftLog.lastIndex() - 1, 4, true},
  63. {raftLog.lastIndex(), 4, true},
  64. {raftLog.lastIndex() + 1, 4, true},
  65. // smaller term, ignore lastIndex
  66. {raftLog.lastIndex() - 1, 2, false},
  67. {raftLog.lastIndex(), 2, false},
  68. {raftLog.lastIndex() + 1, 2, false},
  69. // equal term, lager lastIndex wins
  70. {raftLog.lastIndex() - 1, 3, false},
  71. {raftLog.lastIndex(), 3, true},
  72. {raftLog.lastIndex() + 1, 3, true},
  73. }
  74. for i, tt := range tests {
  75. gUpToDate := raftLog.isUpToDate(tt.lastIndex, tt.term)
  76. if gUpToDate != tt.wUpToDate {
  77. t.Errorf("#%d: uptodate = %v, want %v", i, gUpToDate, tt.wUpToDate)
  78. }
  79. }
  80. }
  81. func TestAppend(t *testing.T) {
  82. previousEnts := []pb.Entry{{Term: 1}, {Term: 2}}
  83. previousUnstable := uint64(3)
  84. tests := []struct {
  85. after uint64
  86. ents []pb.Entry
  87. windex uint64
  88. wents []pb.Entry
  89. wunstable uint64
  90. }{
  91. {
  92. 2,
  93. []pb.Entry{},
  94. 2,
  95. []pb.Entry{{Term: 1}, {Term: 2}},
  96. 3,
  97. },
  98. {
  99. 2,
  100. []pb.Entry{{Term: 2}},
  101. 3,
  102. []pb.Entry{{Term: 1}, {Term: 2}, {Term: 2}},
  103. 3,
  104. },
  105. // conflicts with index 1
  106. {
  107. 0,
  108. []pb.Entry{{Term: 2}},
  109. 1,
  110. []pb.Entry{{Term: 2}},
  111. 1,
  112. },
  113. // conflicts with index 2
  114. {
  115. 1,
  116. []pb.Entry{{Term: 3}, {Term: 3}},
  117. 3,
  118. []pb.Entry{{Term: 1}, {Term: 3}, {Term: 3}},
  119. 2,
  120. },
  121. }
  122. for i, tt := range tests {
  123. raftLog := newLog()
  124. raftLog.append(raftLog.lastIndex(), previousEnts...)
  125. raftLog.unstable = previousUnstable
  126. index := raftLog.append(tt.after, tt.ents...)
  127. if index != tt.windex {
  128. t.Errorf("#%d: lastIndex = %d, want %d", i, index, tt.windex)
  129. }
  130. if g := raftLog.entries(1); !reflect.DeepEqual(g, tt.wents) {
  131. t.Errorf("#%d: logEnts = %+v, want %+v", i, g, tt.wents)
  132. }
  133. if g := raftLog.unstable; g != tt.wunstable {
  134. t.Errorf("#%d: unstable = %d, want %d", i, g, tt.wunstable)
  135. }
  136. }
  137. }
  138. // TestLogMaybeAppend ensures:
  139. // If the given (index, term) matches with the existing log:
  140. // 1. If an existing entry conflicts with a new one (same index
  141. // but different terms), delete the existing entry and all that
  142. // follow it
  143. // 2.Append any new entries not already in the log
  144. // If the given (index, term) does not match with the existing log:
  145. // return false
  146. func TestLogMaybeAppend(t *testing.T) {
  147. previousEnts := []pb.Entry{{Term: 1}, {Term: 2}, {Term: 3}}
  148. lastindex := uint64(3)
  149. lastterm := uint64(3)
  150. tests := []struct {
  151. logTerm uint64
  152. index uint64
  153. committed uint64
  154. ents []pb.Entry
  155. wlasti uint64
  156. wappend bool
  157. wcommit uint64
  158. }{
  159. // not match: term is different
  160. {
  161. lastterm - 1, lastindex, lastindex, []pb.Entry{{Term: 4}},
  162. 0, false, 0,
  163. },
  164. // not match: index out of bound
  165. {
  166. lastterm, lastindex + 1, lastindex, []pb.Entry{{Term: 4}},
  167. 0, false, 0,
  168. },
  169. // match with the last existing entry
  170. {
  171. lastterm, lastindex, lastindex, nil,
  172. lastindex, true, lastindex,
  173. },
  174. {
  175. lastterm, lastindex, lastindex + 1, nil,
  176. lastindex, true, lastindex, // do not increase commit higher than lastnewi
  177. },
  178. {
  179. lastterm, lastindex, lastindex - 1, nil,
  180. lastindex, true, lastindex - 1, // commit up to the commit in the message
  181. },
  182. {
  183. lastterm, lastindex, lastindex, []pb.Entry{{Term: 4}},
  184. lastindex + 1, true, lastindex,
  185. },
  186. {
  187. lastterm, lastindex, lastindex + 1, []pb.Entry{{Term: 4}},
  188. lastindex + 1, true, lastindex + 1,
  189. },
  190. {
  191. lastterm, lastindex, lastindex + 2, []pb.Entry{{Term: 4}},
  192. lastindex + 1, true, lastindex + 1, // do not increase commit higher than lastnewi
  193. },
  194. {
  195. lastterm, lastindex, lastindex + 2, []pb.Entry{{Term: 4}, {Term: 4}},
  196. lastindex + 2, true, lastindex + 2,
  197. },
  198. // match with the the entry in the middle
  199. {
  200. lastterm - 1, lastindex - 1, lastindex, []pb.Entry{{Term: 4}},
  201. lastindex, true, lastindex,
  202. },
  203. {
  204. lastterm - 2, lastindex - 2, lastindex, []pb.Entry{{Term: 4}},
  205. lastindex - 1, true, lastindex - 1,
  206. },
  207. {
  208. lastterm - 3, lastindex - 3, lastindex, []pb.Entry{{Term: 4}},
  209. lastindex - 2, true, lastindex - 2,
  210. },
  211. {
  212. lastterm - 2, lastindex - 2, lastindex, []pb.Entry{{Term: 4}, {Term: 4}},
  213. lastindex, true, lastindex,
  214. },
  215. }
  216. for i, tt := range tests {
  217. raftLog := newLog()
  218. raftLog.append(raftLog.lastIndex(), previousEnts...)
  219. glasti, gappend := raftLog.maybeAppend(tt.index, tt.logTerm, tt.committed, tt.ents...)
  220. gcommit := raftLog.committed
  221. if glasti != tt.wlasti {
  222. t.Errorf("#%d: lastindex = %d, want %d", i, glasti, tt.wlasti)
  223. }
  224. if gappend != tt.wappend {
  225. t.Errorf("#%d: append = %v, want %v", i, gappend, tt.wappend)
  226. }
  227. if gcommit != tt.wcommit {
  228. t.Errorf("#%d: committed = %d, want %d", i, gcommit, tt.wcommit)
  229. }
  230. if gappend {
  231. gents := raftLog.slice(raftLog.lastIndex()-uint64(len(tt.ents))+1, raftLog.lastIndex()+1)
  232. if !reflect.DeepEqual(tt.ents, gents) {
  233. t.Errorf("%d: appended entries = %v, want %v", i, gents, tt.ents)
  234. }
  235. }
  236. }
  237. }
  238. // TestCompactionSideEffects ensures that all the log related funcationality works correctly after
  239. // a compaction.
  240. func TestCompactionSideEffects(t *testing.T) {
  241. var i uint64
  242. lastIndex := uint64(1000)
  243. lastTerm := lastIndex
  244. raftLog := newLog()
  245. for i = 0; i < lastIndex; i++ {
  246. raftLog.append(uint64(i), pb.Entry{Term: uint64(i + 1), Index: uint64(i + 1)})
  247. }
  248. raftLog.maybeCommit(lastIndex, lastTerm)
  249. raftLog.resetNextEnts()
  250. raftLog.compact(500)
  251. if raftLog.lastIndex() != lastIndex {
  252. t.Errorf("lastIndex = %d, want %d", raftLog.lastIndex(), lastIndex)
  253. }
  254. for i := raftLog.offset; i <= raftLog.lastIndex(); i++ {
  255. if raftLog.term(i) != i {
  256. t.Errorf("term(%d) = %d, want %d", i, raftLog.term(i), i)
  257. }
  258. }
  259. for i := raftLog.offset; i <= raftLog.lastIndex(); i++ {
  260. if !raftLog.matchTerm(i, i) {
  261. t.Errorf("matchTerm(%d) = false, want true", i)
  262. }
  263. }
  264. unstableEnts := raftLog.unstableEnts()
  265. if g := len(unstableEnts); g != 500 {
  266. t.Errorf("len(unstableEntries) = %d, want = %d", g, 500)
  267. }
  268. if unstableEnts[0].Index != 501 {
  269. t.Errorf("Index = %d, want = %d", unstableEnts[0].Index, 501)
  270. }
  271. prev := raftLog.lastIndex()
  272. raftLog.append(raftLog.lastIndex(), pb.Entry{Term: raftLog.lastIndex() + 1})
  273. if raftLog.lastIndex() != prev+1 {
  274. t.Errorf("lastIndex = %d, want = %d", raftLog.lastIndex(), prev+1)
  275. }
  276. ents := raftLog.entries(raftLog.lastIndex())
  277. if len(ents) != 1 {
  278. t.Errorf("len(entries) = %d, want = %d", len(ents), 1)
  279. }
  280. }
  281. func TestUnstableEnts(t *testing.T) {
  282. previousEnts := []pb.Entry{{Term: 1, Index: 1}, {Term: 2, Index: 2}}
  283. tests := []struct {
  284. unstable uint64
  285. wents []pb.Entry
  286. wunstable uint64
  287. }{
  288. {3, nil, 3},
  289. {1, previousEnts, 3},
  290. }
  291. for i, tt := range tests {
  292. raftLog := newLog()
  293. raftLog.append(0, previousEnts...)
  294. raftLog.unstable = tt.unstable
  295. ents := raftLog.unstableEnts()
  296. raftLog.resetUnstable()
  297. if !reflect.DeepEqual(ents, tt.wents) {
  298. t.Errorf("#%d: unstableEnts = %+v, want %+v", i, ents, tt.wents)
  299. }
  300. if g := raftLog.unstable; g != tt.wunstable {
  301. t.Errorf("#%d: unstable = %d, want %d", i, g, tt.wunstable)
  302. }
  303. }
  304. }
  305. //TestCompaction ensures that the number of log entreis is correct after compactions.
  306. func TestCompaction(t *testing.T) {
  307. tests := []struct {
  308. applied uint64
  309. lastIndex uint64
  310. compact []uint64
  311. wleft []int
  312. wallow bool
  313. }{
  314. // out of upper bound
  315. {1000, 1000, []uint64{1001}, []int{-1}, false},
  316. {1000, 1000, []uint64{300, 500, 800, 900}, []int{701, 501, 201, 101}, true},
  317. // out of lower bound
  318. {1000, 1000, []uint64{300, 299}, []int{701, -1}, false},
  319. {0, 1000, []uint64{1}, []int{-1}, false},
  320. }
  321. for i, tt := range tests {
  322. func() {
  323. defer func() {
  324. if r := recover(); r != nil {
  325. if tt.wallow == true {
  326. t.Errorf("%d: allow = %v, want %v", i, false, true)
  327. }
  328. }
  329. }()
  330. raftLog := newLog()
  331. for i := uint64(0); i < tt.lastIndex; i++ {
  332. raftLog.append(uint64(i), pb.Entry{})
  333. }
  334. raftLog.maybeCommit(tt.applied, 0)
  335. raftLog.resetNextEnts()
  336. for j := 0; j < len(tt.compact); j++ {
  337. raftLog.compact(tt.compact[j])
  338. if len(raftLog.ents) != tt.wleft[j] {
  339. t.Errorf("#%d.%d len = %d, want %d", i, j, len(raftLog.ents), tt.wleft[j])
  340. }
  341. }
  342. }()
  343. }
  344. }
  345. func TestLogRestore(t *testing.T) {
  346. var i uint64
  347. raftLog := newLog()
  348. for i = 0; i < 100; i++ {
  349. raftLog.append(i, pb.Entry{Term: i + 1})
  350. }
  351. index := uint64(1000)
  352. term := uint64(1000)
  353. raftLog.restore(pb.Snapshot{Index: index, Term: term})
  354. // only has the guard entry
  355. if len(raftLog.ents) != 1 {
  356. t.Errorf("len = %d, want 0", len(raftLog.ents))
  357. }
  358. if raftLog.offset != index {
  359. t.Errorf("offset = %d, want %d", raftLog.offset, index)
  360. }
  361. if raftLog.applied != index {
  362. t.Errorf("applied = %d, want %d", raftLog.applied, index)
  363. }
  364. if raftLog.committed != index {
  365. t.Errorf("comitted = %d, want %d", raftLog.committed, index)
  366. }
  367. if raftLog.unstable != index+1 {
  368. t.Errorf("unstable = %d, want %d", raftLog.unstable, index+1)
  369. }
  370. if raftLog.term(index) != term {
  371. t.Errorf("term = %d, want %d", raftLog.term(index), term)
  372. }
  373. }
  374. func TestIsOutOfBounds(t *testing.T) {
  375. offset := uint64(100)
  376. num := uint64(100)
  377. l := &raftLog{offset: offset, ents: make([]pb.Entry, num)}
  378. tests := []struct {
  379. index uint64
  380. w bool
  381. }{
  382. {offset - 1, true},
  383. {offset, false},
  384. {offset + num/2, false},
  385. {offset + num - 1, false},
  386. {offset + num, true},
  387. }
  388. for i, tt := range tests {
  389. g := l.isOutOfBounds(tt.index)
  390. if g != tt.w {
  391. t.Errorf("#%d: isOutOfBounds = %v, want %v", i, g, tt.w)
  392. }
  393. }
  394. }
  395. func TestAt(t *testing.T) {
  396. var i uint64
  397. offset := uint64(100)
  398. num := uint64(100)
  399. l := &raftLog{offset: offset}
  400. for i = 0; i < num; i++ {
  401. l.ents = append(l.ents, pb.Entry{Term: i})
  402. }
  403. tests := []struct {
  404. index uint64
  405. w *pb.Entry
  406. }{
  407. {offset - 1, nil},
  408. {offset, &pb.Entry{Term: 0}},
  409. {offset + num/2, &pb.Entry{Term: num / 2}},
  410. {offset + num - 1, &pb.Entry{Term: num - 1}},
  411. {offset + num, nil},
  412. }
  413. for i, tt := range tests {
  414. g := l.at(tt.index)
  415. if !reflect.DeepEqual(g, tt.w) {
  416. t.Errorf("#%d: at = %v, want %v", i, g, tt.w)
  417. }
  418. }
  419. }
  420. func TestSlice(t *testing.T) {
  421. var i uint64
  422. offset := uint64(100)
  423. num := uint64(100)
  424. l := &raftLog{offset: offset}
  425. for i = 0; i < num; i++ {
  426. l.ents = append(l.ents, pb.Entry{Term: i})
  427. }
  428. tests := []struct {
  429. from uint64
  430. to uint64
  431. w []pb.Entry
  432. }{
  433. {offset - 1, offset + 1, nil},
  434. {offset, offset + 1, []pb.Entry{{Term: 0}}},
  435. {offset + num/2, offset + num/2 + 1, []pb.Entry{{Term: num / 2}}},
  436. {offset + num - 1, offset + num, []pb.Entry{{Term: num - 1}}},
  437. {offset + num, offset + num + 1, nil},
  438. {offset + num/2, offset + num/2, nil},
  439. {offset + num/2, offset + num/2 - 1, nil},
  440. }
  441. for i, tt := range tests {
  442. g := l.slice(tt.from, tt.to)
  443. if !reflect.DeepEqual(g, tt.w) {
  444. t.Errorf("#%d: from %d to %d = %v, want %v", i, tt.from, tt.to, g, tt.w)
  445. }
  446. }
  447. }