log_test.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  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(NewMemoryStorage())
  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(NewMemoryStorage())
  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. tests := []struct {
  84. after uint64
  85. ents []pb.Entry
  86. windex uint64
  87. wents []pb.Entry
  88. wunstable uint64
  89. }{
  90. {
  91. 2,
  92. []pb.Entry{},
  93. 2,
  94. []pb.Entry{{Term: 1}, {Term: 2}},
  95. 3,
  96. },
  97. {
  98. 2,
  99. []pb.Entry{{Term: 2}},
  100. 3,
  101. []pb.Entry{{Term: 1}, {Term: 2}, {Term: 2}},
  102. 3,
  103. },
  104. // conflicts with index 1
  105. {
  106. 0,
  107. []pb.Entry{{Term: 2}},
  108. 1,
  109. []pb.Entry{{Term: 2}},
  110. 1,
  111. },
  112. // conflicts with index 2
  113. {
  114. 1,
  115. []pb.Entry{{Term: 3}, {Term: 3}},
  116. 3,
  117. []pb.Entry{{Term: 1}, {Term: 3}, {Term: 3}},
  118. 2,
  119. },
  120. }
  121. for i, tt := range tests {
  122. storage := NewMemoryStorage()
  123. storage.Append(previousEnts)
  124. raftLog := newLog(storage)
  125. index := raftLog.append(tt.after, tt.ents...)
  126. if index != tt.windex {
  127. t.Errorf("#%d: lastIndex = %d, want %d", i, index, tt.windex)
  128. }
  129. if g := raftLog.entries(1); !reflect.DeepEqual(g, tt.wents) {
  130. t.Errorf("#%d: logEnts = %+v, want %+v", i, g, tt.wents)
  131. }
  132. if g := raftLog.unstable; g != tt.wunstable {
  133. t.Errorf("#%d: unstable = %d, want %d", i, g, tt.wunstable)
  134. }
  135. }
  136. }
  137. // TestLogMaybeAppend ensures:
  138. // If the given (index, term) matches with the existing log:
  139. // 1. If an existing entry conflicts with a new one (same index
  140. // but different terms), delete the existing entry and all that
  141. // follow it
  142. // 2.Append any new entries not already in the log
  143. // If the given (index, term) does not match with the existing log:
  144. // return false
  145. func TestLogMaybeAppend(t *testing.T) {
  146. previousEnts := []pb.Entry{{Term: 1}, {Term: 2}, {Term: 3}}
  147. lastindex := uint64(3)
  148. lastterm := uint64(3)
  149. commit := uint64(1)
  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. wpanic bool
  159. }{
  160. // not match: term is different
  161. {
  162. lastterm - 1, lastindex, lastindex, []pb.Entry{{Term: 4}},
  163. 0, false, commit, false,
  164. },
  165. // not match: index out of bound
  166. {
  167. lastterm, lastindex + 1, lastindex, []pb.Entry{{Term: 4}},
  168. 0, false, commit, false,
  169. },
  170. // match with the last existing entry
  171. {
  172. lastterm, lastindex, lastindex, nil,
  173. lastindex, true, lastindex, false,
  174. },
  175. {
  176. lastterm, lastindex, lastindex + 1, nil,
  177. lastindex, true, lastindex, false, // do not increase commit higher than lastnewi
  178. },
  179. {
  180. lastterm, lastindex, lastindex - 1, nil,
  181. lastindex, true, lastindex - 1, false, // commit up to the commit in the message
  182. },
  183. {
  184. lastterm, lastindex, 0, nil,
  185. lastindex, true, commit, false, // commit do not decrease
  186. },
  187. {
  188. 0, 0, lastindex, nil,
  189. 0, true, commit, false, // commit do not decrease
  190. },
  191. {
  192. lastterm, lastindex, lastindex, []pb.Entry{{Term: 4}},
  193. lastindex + 1, true, lastindex, false,
  194. },
  195. {
  196. lastterm, lastindex, lastindex + 1, []pb.Entry{{Term: 4}},
  197. lastindex + 1, true, lastindex + 1, false,
  198. },
  199. {
  200. lastterm, lastindex, lastindex + 2, []pb.Entry{{Term: 4}},
  201. lastindex + 1, true, lastindex + 1, false, // do not increase commit higher than lastnewi
  202. },
  203. {
  204. lastterm, lastindex, lastindex + 2, []pb.Entry{{Term: 4}, {Term: 4}},
  205. lastindex + 2, true, lastindex + 2, false,
  206. },
  207. // match with the the entry in the middle
  208. {
  209. lastterm - 1, lastindex - 1, lastindex, []pb.Entry{{Term: 4}},
  210. lastindex, true, lastindex, false,
  211. },
  212. {
  213. lastterm - 2, lastindex - 2, lastindex, []pb.Entry{{Term: 4}},
  214. lastindex - 1, true, lastindex - 1, false,
  215. },
  216. {
  217. lastterm - 3, lastindex - 3, lastindex, []pb.Entry{{Term: 4}},
  218. lastindex - 2, true, lastindex - 2, true, // conflict with existing committed entry
  219. },
  220. {
  221. lastterm - 2, lastindex - 2, lastindex, []pb.Entry{{Term: 4}, {Term: 4}},
  222. lastindex, true, lastindex, false,
  223. },
  224. }
  225. for i, tt := range tests {
  226. raftLog := newLog(NewMemoryStorage())
  227. raftLog.append(raftLog.lastIndex(), previousEnts...)
  228. raftLog.committed = commit
  229. func() {
  230. defer func() {
  231. if r := recover(); r != nil {
  232. if tt.wpanic != true {
  233. t.Errorf("%d: panic = %v, want %v", i, true, tt.wpanic)
  234. }
  235. }
  236. }()
  237. glasti, gappend := raftLog.maybeAppend(tt.index, tt.logTerm, tt.committed, tt.ents...)
  238. gcommit := raftLog.committed
  239. if glasti != tt.wlasti {
  240. t.Errorf("#%d: lastindex = %d, want %d", i, glasti, tt.wlasti)
  241. }
  242. if gappend != tt.wappend {
  243. t.Errorf("#%d: append = %v, want %v", i, gappend, tt.wappend)
  244. }
  245. if gcommit != tt.wcommit {
  246. t.Errorf("#%d: committed = %d, want %d", i, gcommit, tt.wcommit)
  247. }
  248. if gappend {
  249. gents := raftLog.slice(raftLog.lastIndex()-uint64(len(tt.ents))+1, raftLog.lastIndex()+1)
  250. if !reflect.DeepEqual(tt.ents, gents) {
  251. t.Errorf("%d: appended entries = %v, want %v", i, gents, tt.ents)
  252. }
  253. }
  254. }()
  255. }
  256. }
  257. // TestCompactionSideEffects ensures that all the log related funcationality works correctly after
  258. // a compaction.
  259. func TestCompactionSideEffects(t *testing.T) {
  260. var i uint64
  261. // Populate the log with 1000 entries; 750 in stable storage and 250 in unstable.
  262. lastIndex := uint64(1000)
  263. unstableIndex := uint64(750)
  264. lastTerm := lastIndex
  265. storage := NewMemoryStorage()
  266. for i = 1; i <= unstableIndex; i++ {
  267. storage.Append([]pb.Entry{{Term: uint64(i), Index: uint64(i)}})
  268. }
  269. raftLog := newLog(storage)
  270. for i = unstableIndex; i < lastIndex; i++ {
  271. raftLog.append(i, pb.Entry{Term: uint64(i + 1), Index: uint64(i + 1)})
  272. }
  273. ok := raftLog.maybeCommit(lastIndex, lastTerm)
  274. if !ok {
  275. t.Fatalf("maybeCommit returned false")
  276. }
  277. raftLog.appliedTo(raftLog.committed)
  278. offset := uint64(500)
  279. raftLog.compact(offset)
  280. if raftLog.lastIndex() != lastIndex {
  281. t.Errorf("lastIndex = %d, want %d", raftLog.lastIndex(), lastIndex)
  282. }
  283. for i := offset; i <= raftLog.lastIndex(); i++ {
  284. if raftLog.term(i) != i {
  285. t.Errorf("term(%d) = %d, want %d", i, raftLog.term(i), i)
  286. }
  287. }
  288. for i := offset; i <= raftLog.lastIndex(); i++ {
  289. if !raftLog.matchTerm(i, i) {
  290. t.Errorf("matchTerm(%d) = false, want true", i)
  291. }
  292. }
  293. unstableEnts := raftLog.unstableEntries()
  294. if g := len(unstableEnts); g != 250 {
  295. t.Errorf("len(unstableEntries) = %d, want = %d", g, 250)
  296. }
  297. if unstableEnts[0].Index != 751 {
  298. t.Errorf("Index = %d, want = %d", unstableEnts[0].Index, 751)
  299. }
  300. prev := raftLog.lastIndex()
  301. raftLog.append(raftLog.lastIndex(), pb.Entry{Term: raftLog.lastIndex() + 1})
  302. if raftLog.lastIndex() != prev+1 {
  303. t.Errorf("lastIndex = %d, want = %d", raftLog.lastIndex(), prev+1)
  304. }
  305. ents := raftLog.entries(raftLog.lastIndex())
  306. if len(ents) != 1 {
  307. t.Errorf("len(entries) = %d, want = %d", len(ents), 1)
  308. }
  309. }
  310. func TestUnstableEnts(t *testing.T) {
  311. previousEnts := []pb.Entry{{Term: 1, Index: 1}, {Term: 2, Index: 2}}
  312. tests := []struct {
  313. unstable uint64
  314. wents []pb.Entry
  315. wunstable uint64
  316. }{
  317. {3, nil, 3},
  318. {1, previousEnts, 3},
  319. }
  320. for i, tt := range tests {
  321. storage := NewMemoryStorage()
  322. if tt.unstable > 0 {
  323. storage.Append(previousEnts[:tt.unstable-1])
  324. }
  325. raftLog := newLog(storage)
  326. raftLog.append(raftLog.lastIndex(), previousEnts[tt.unstable-1:]...)
  327. ents := raftLog.unstableEntries()
  328. if l := len(ents); l > 0 {
  329. raftLog.stableTo(ents[l-1].Index)
  330. }
  331. if !reflect.DeepEqual(ents, tt.wents) {
  332. t.Errorf("#%d: unstableEnts = %+v, want %+v", i, ents, tt.wents)
  333. }
  334. if g := raftLog.unstable; g != tt.wunstable {
  335. t.Errorf("#%d: unstable = %d, want %d", i, g, tt.wunstable)
  336. }
  337. }
  338. }
  339. func TestStableTo(t *testing.T) {
  340. tests := []struct {
  341. stable uint64
  342. wunstable uint64
  343. }{
  344. {1, 2},
  345. {2, 3},
  346. }
  347. for i, tt := range tests {
  348. raftLog := newLog(NewMemoryStorage())
  349. raftLog.append(0, []pb.Entry{{}, {}}...)
  350. raftLog.stableTo(tt.stable)
  351. if raftLog.unstable != tt.wunstable {
  352. t.Errorf("#%d: unstable = %d, want %d", i, raftLog.unstable, tt.wunstable)
  353. }
  354. }
  355. }
  356. //TestCompaction ensures that the number of log entries is correct after compactions.
  357. func TestCompaction(t *testing.T) {
  358. tests := []struct {
  359. applied uint64
  360. lastIndex uint64
  361. compact []uint64
  362. wleft []int
  363. wallow bool
  364. }{
  365. // out of upper bound
  366. {1000, 1000, []uint64{1001}, []int{-1}, false},
  367. {1000, 1000, []uint64{300, 500, 800, 900}, []int{700, 500, 200, 100}, true},
  368. // out of lower bound
  369. {1000, 1000, []uint64{300, 299}, []int{700, -1}, false},
  370. {0, 1000, []uint64{1}, []int{-1}, false},
  371. }
  372. for i, tt := range tests {
  373. func() {
  374. defer func() {
  375. if r := recover(); r != nil {
  376. if tt.wallow == true {
  377. t.Errorf("%d: allow = %v, want %v: %v", i, false, true, r)
  378. }
  379. }
  380. }()
  381. storage := NewMemoryStorage()
  382. for i := uint64(1); i <= tt.lastIndex; i++ {
  383. storage.Append([]pb.Entry{{}})
  384. }
  385. raftLog := newLog(storage)
  386. raftLog.maybeCommit(tt.applied, 0)
  387. raftLog.appliedTo(raftLog.committed)
  388. for j := 0; j < len(tt.compact); j++ {
  389. raftLog.compact(tt.compact[j])
  390. if len(raftLog.allEntries()) != tt.wleft[j] {
  391. t.Errorf("#%d.%d len = %d, want %d", i, j, len(raftLog.allEntries()), tt.wleft[j])
  392. }
  393. }
  394. }()
  395. }
  396. }
  397. func TestLogRestore(t *testing.T) {
  398. var i uint64
  399. raftLog := newLog(NewMemoryStorage())
  400. for i = 0; i < 100; i++ {
  401. raftLog.append(i, pb.Entry{Term: i + 1})
  402. }
  403. index := uint64(1000)
  404. term := uint64(1000)
  405. raftLog.restore(pb.Snapshot{Index: index, Term: term})
  406. // only has the guard entry
  407. if len(raftLog.allEntries()) != 0 {
  408. t.Errorf("len = %d, want 1", len(raftLog.allEntries()))
  409. }
  410. if raftLog.firstIndex() != index+1 {
  411. t.Errorf("firstIndex = %d, want %d", raftLog.firstIndex(), index+1)
  412. }
  413. if raftLog.applied != index {
  414. t.Errorf("applied = %d, want %d", raftLog.applied, index)
  415. }
  416. if raftLog.committed != index {
  417. t.Errorf("comitted = %d, want %d", raftLog.committed, index)
  418. }
  419. if raftLog.unstable != index+1 {
  420. t.Errorf("unstable = %d, want %d", raftLog.unstable, index+1)
  421. }
  422. if raftLog.term(index) != term {
  423. t.Errorf("term = %d, want %d", raftLog.term(index), term)
  424. }
  425. }
  426. func TestIsOutOfBounds(t *testing.T) {
  427. offset := uint64(100)
  428. num := uint64(100)
  429. l := newLog(NewMemoryStorage())
  430. l.restore(pb.Snapshot{Index: offset})
  431. l.append(offset, make([]pb.Entry, num)...)
  432. tests := []struct {
  433. index uint64
  434. w bool
  435. }{
  436. {offset - 1, true},
  437. {offset, true},
  438. {offset + num/2, false},
  439. {offset + num, false},
  440. {offset + num + 1, true},
  441. }
  442. for i, tt := range tests {
  443. g := l.isOutOfBounds(tt.index)
  444. if g != tt.w {
  445. t.Errorf("#%d: isOutOfBounds = %v, want %v", i, g, tt.w)
  446. }
  447. }
  448. }
  449. func TestAt(t *testing.T) {
  450. var i uint64
  451. offset := uint64(100)
  452. num := uint64(100)
  453. l := newLog(NewMemoryStorage())
  454. l.restore(pb.Snapshot{Index: offset})
  455. for i = 1; i < num; i++ {
  456. l.append(offset+i-1, pb.Entry{Term: i})
  457. }
  458. tests := []struct {
  459. index uint64
  460. w *pb.Entry
  461. }{
  462. {offset - 1, nil},
  463. {offset, nil},
  464. {offset + num/2, &pb.Entry{Term: num / 2}},
  465. {offset + num - 1, &pb.Entry{Term: num - 1}},
  466. {offset + num, nil},
  467. }
  468. for i, tt := range tests {
  469. g := l.at(tt.index)
  470. if !reflect.DeepEqual(g, tt.w) {
  471. t.Errorf("#%d: at = %v, want %v", i, g, tt.w)
  472. }
  473. }
  474. }
  475. func TestTerm(t *testing.T) {
  476. var i uint64
  477. offset := uint64(100)
  478. num := uint64(100)
  479. l := newLog(NewMemoryStorage())
  480. l.restore(pb.Snapshot{Index: offset})
  481. for i = 1; i < num; i++ {
  482. l.append(offset+i-1, pb.Entry{Term: i})
  483. }
  484. tests := []struct {
  485. index uint64
  486. w uint64
  487. }{
  488. {offset - 1, 0},
  489. {offset, 0},
  490. {offset + num/2, num / 2},
  491. {offset + num - 1, num - 1},
  492. {offset + num, 0},
  493. }
  494. for i, tt := range tests {
  495. term := l.term(tt.index)
  496. if !reflect.DeepEqual(term, tt.w) {
  497. t.Errorf("#%d: at = %d, want %d", i, term, tt.w)
  498. }
  499. }
  500. }
  501. func TestSlice(t *testing.T) {
  502. var i uint64
  503. offset := uint64(100)
  504. num := uint64(100)
  505. l := newLog(NewMemoryStorage())
  506. l.restore(pb.Snapshot{Index: offset})
  507. for i = 1; i < num; i++ {
  508. l.append(offset+i-1, pb.Entry{Term: i})
  509. }
  510. tests := []struct {
  511. from uint64
  512. to uint64
  513. w []pb.Entry
  514. }{
  515. {offset - 1, offset + 1, nil},
  516. {offset, offset + 1, nil},
  517. {offset + num/2, offset + num/2 + 1, []pb.Entry{{Term: num / 2}}},
  518. {offset + num - 1, offset + num, []pb.Entry{{Term: num - 1}}},
  519. {offset + num, offset + num + 1, nil},
  520. {offset + num/2, offset + num/2, nil},
  521. {offset + num/2, offset + num/2 - 1, nil},
  522. }
  523. for i, tt := range tests {
  524. g := l.slice(tt.from, tt.to)
  525. if !reflect.DeepEqual(g, tt.w) {
  526. t.Errorf("#%d: from %d to %d = %v, want %v", i, tt.from, tt.to, g, tt.w)
  527. }
  528. }
  529. }