log_test.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  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 (
  16. "reflect"
  17. "testing"
  18. pb "github.com/coreos/etcd/raft/raftpb"
  19. )
  20. func TestFindConflict(t *testing.T) {
  21. previousEnts := []pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}, {Index: 3, Term: 3}}
  22. tests := []struct {
  23. ents []pb.Entry
  24. wconflict uint64
  25. }{
  26. // no conflict, empty ent
  27. {[]pb.Entry{}, 0},
  28. {[]pb.Entry{}, 0},
  29. // no conflict
  30. {[]pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}, {Index: 3, Term: 3}}, 0},
  31. {[]pb.Entry{{Index: 2, Term: 2}, {Index: 3, Term: 3}}, 0},
  32. {[]pb.Entry{{Index: 3, Term: 3}}, 0},
  33. // no conflict, but has new entries
  34. {[]pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}, {Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 4}}, 4},
  35. {[]pb.Entry{{Index: 2, Term: 2}, {Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 4}}, 4},
  36. {[]pb.Entry{{Index: 3, Term: 3}, {Index: 4, Term: 4}, {Index: 5, Term: 4}}, 4},
  37. {[]pb.Entry{{Index: 4, Term: 4}, {Index: 5, Term: 4}}, 4},
  38. // conflicts with existing entries
  39. {[]pb.Entry{{Index: 1, Term: 4}, {Index: 2, Term: 4}}, 1},
  40. {[]pb.Entry{{Index: 2, Term: 1}, {Index: 3, Term: 4}, {Index: 4, Term: 4}}, 2},
  41. {[]pb.Entry{{Index: 3, Term: 1}, {Index: 4, Term: 2}, {Index: 5, Term: 4}, {Index: 6, Term: 4}}, 3},
  42. }
  43. for i, tt := range tests {
  44. raftLog := newLog(NewMemoryStorage())
  45. raftLog.append(previousEnts...)
  46. gconflict := raftLog.findConflict(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{{Index: 1, Term: 1}, {Index: 2, Term: 2}, {Index: 3, Term: 3}}
  54. raftLog := newLog(NewMemoryStorage())
  55. raftLog.append(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{{Index: 1, Term: 1}, {Index: 2, Term: 2}}
  83. tests := []struct {
  84. ents []pb.Entry
  85. windex uint64
  86. wents []pb.Entry
  87. wunstable uint64
  88. }{
  89. {
  90. []pb.Entry{},
  91. 2,
  92. []pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}},
  93. 3,
  94. },
  95. {
  96. []pb.Entry{{Index: 3, Term: 2}},
  97. 3,
  98. []pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}, {Index: 3, Term: 2}},
  99. 3,
  100. },
  101. // conflicts with index 1
  102. {
  103. []pb.Entry{{Index: 1, Term: 2}},
  104. 1,
  105. []pb.Entry{{Index: 1, Term: 2}},
  106. 1,
  107. },
  108. // conflicts with index 2
  109. {
  110. []pb.Entry{{Index: 2, Term: 3}, {Index: 3, Term: 3}},
  111. 3,
  112. []pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 3}, {Index: 3, Term: 3}},
  113. 2,
  114. },
  115. }
  116. for i, tt := range tests {
  117. storage := NewMemoryStorage()
  118. storage.Append(previousEnts)
  119. raftLog := newLog(storage)
  120. index := raftLog.append(tt.ents...)
  121. if index != tt.windex {
  122. t.Errorf("#%d: lastIndex = %d, want %d", i, index, tt.windex)
  123. }
  124. g, err := raftLog.entries(1, noLimit)
  125. if err != nil {
  126. t.Fatalf("#%d: unexpected error %v", i, err)
  127. }
  128. if !reflect.DeepEqual(g, tt.wents) {
  129. t.Errorf("#%d: logEnts = %+v, want %+v", i, g, tt.wents)
  130. }
  131. if goff := raftLog.unstable.offset; goff != tt.wunstable {
  132. t.Errorf("#%d: unstable = %d, want %d", i, goff, tt.wunstable)
  133. }
  134. }
  135. }
  136. // TestLogMaybeAppend ensures:
  137. // If the given (index, term) matches with the existing log:
  138. // 1. If an existing entry conflicts with a new one (same index
  139. // but different terms), delete the existing entry and all that
  140. // follow it
  141. // 2.Append any new entries not already in the log
  142. // If the given (index, term) does not match with the existing log:
  143. // return false
  144. func TestLogMaybeAppend(t *testing.T) {
  145. previousEnts := []pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}, {Index: 3, Term: 3}}
  146. lastindex := uint64(3)
  147. lastterm := uint64(3)
  148. commit := uint64(1)
  149. tests := []struct {
  150. logTerm uint64
  151. index uint64
  152. committed uint64
  153. ents []pb.Entry
  154. wlasti uint64
  155. wappend bool
  156. wcommit uint64
  157. wpanic bool
  158. }{
  159. // not match: term is different
  160. {
  161. lastterm - 1, lastindex, lastindex, []pb.Entry{{Index: lastindex + 1, Term: 4}},
  162. 0, false, commit, false,
  163. },
  164. // not match: index out of bound
  165. {
  166. lastterm, lastindex + 1, lastindex, []pb.Entry{{Index: lastindex + 2, Term: 4}},
  167. 0, false, commit, false,
  168. },
  169. // match with the last existing entry
  170. {
  171. lastterm, lastindex, lastindex, nil,
  172. lastindex, true, lastindex, false,
  173. },
  174. {
  175. lastterm, lastindex, lastindex + 1, nil,
  176. lastindex, true, lastindex, false, // do not increase commit higher than lastnewi
  177. },
  178. {
  179. lastterm, lastindex, lastindex - 1, nil,
  180. lastindex, true, lastindex - 1, false, // commit up to the commit in the message
  181. },
  182. {
  183. lastterm, lastindex, 0, nil,
  184. lastindex, true, commit, false, // commit do not decrease
  185. },
  186. {
  187. 0, 0, lastindex, nil,
  188. 0, true, commit, false, // commit do not decrease
  189. },
  190. {
  191. lastterm, lastindex, lastindex, []pb.Entry{{Index: lastindex + 1, Term: 4}},
  192. lastindex + 1, true, lastindex, false,
  193. },
  194. {
  195. lastterm, lastindex, lastindex + 1, []pb.Entry{{Index: lastindex + 1, Term: 4}},
  196. lastindex + 1, true, lastindex + 1, false,
  197. },
  198. {
  199. lastterm, lastindex, lastindex + 2, []pb.Entry{{Index: lastindex + 1, Term: 4}},
  200. lastindex + 1, true, lastindex + 1, false, // do not increase commit higher than lastnewi
  201. },
  202. {
  203. lastterm, lastindex, lastindex + 2, []pb.Entry{{Index: lastindex + 1, Term: 4}, {Index: lastindex + 2, Term: 4}},
  204. lastindex + 2, true, lastindex + 2, false,
  205. },
  206. // match with the the entry in the middle
  207. {
  208. lastterm - 1, lastindex - 1, lastindex, []pb.Entry{{Index: lastindex, Term: 4}},
  209. lastindex, true, lastindex, false,
  210. },
  211. {
  212. lastterm - 2, lastindex - 2, lastindex, []pb.Entry{{Index: lastindex - 1, Term: 4}},
  213. lastindex - 1, true, lastindex - 1, false,
  214. },
  215. {
  216. lastterm - 3, lastindex - 3, lastindex, []pb.Entry{{Index: lastindex - 2, Term: 4}},
  217. lastindex - 2, true, lastindex - 2, true, // conflict with existing committed entry
  218. },
  219. {
  220. lastterm - 2, lastindex - 2, lastindex, []pb.Entry{{Index: lastindex - 1, Term: 4}, {Index: lastindex, Term: 4}},
  221. lastindex, true, lastindex, false,
  222. },
  223. }
  224. for i, tt := range tests {
  225. raftLog := newLog(NewMemoryStorage())
  226. raftLog.append(previousEnts...)
  227. raftLog.committed = commit
  228. func() {
  229. defer func() {
  230. if r := recover(); r != nil {
  231. if tt.wpanic != true {
  232. t.Errorf("%d: panic = %v, want %v", i, true, tt.wpanic)
  233. }
  234. }
  235. }()
  236. glasti, gappend := raftLog.maybeAppend(tt.index, tt.logTerm, tt.committed, tt.ents...)
  237. gcommit := raftLog.committed
  238. if glasti != tt.wlasti {
  239. t.Errorf("#%d: lastindex = %d, want %d", i, glasti, tt.wlasti)
  240. }
  241. if gappend != tt.wappend {
  242. t.Errorf("#%d: append = %v, want %v", i, gappend, tt.wappend)
  243. }
  244. if gcommit != tt.wcommit {
  245. t.Errorf("#%d: committed = %d, want %d", i, gcommit, tt.wcommit)
  246. }
  247. if gappend && len(tt.ents) != 0 {
  248. gents, err := raftLog.slice(raftLog.lastIndex()-uint64(len(tt.ents))+1, raftLog.lastIndex()+1, noLimit)
  249. if err != nil {
  250. t.Fatalf("unexpected error %v", err)
  251. }
  252. if !reflect.DeepEqual(tt.ents, gents) {
  253. t.Errorf("%d: appended entries = %v, want %v", i, gents, tt.ents)
  254. }
  255. }
  256. }()
  257. }
  258. }
  259. // TestCompactionSideEffects ensures that all the log related funcationality works correctly after
  260. // a compaction.
  261. func TestCompactionSideEffects(t *testing.T) {
  262. var i uint64
  263. // Populate the log with 1000 entries; 750 in stable storage and 250 in unstable.
  264. lastIndex := uint64(1000)
  265. unstableIndex := uint64(750)
  266. lastTerm := lastIndex
  267. storage := NewMemoryStorage()
  268. for i = 1; i <= unstableIndex; i++ {
  269. storage.Append([]pb.Entry{{Term: uint64(i), Index: uint64(i)}})
  270. }
  271. raftLog := newLog(storage)
  272. for i = unstableIndex; i < lastIndex; i++ {
  273. raftLog.append(pb.Entry{Term: uint64(i + 1), Index: uint64(i + 1)})
  274. }
  275. ok := raftLog.maybeCommit(lastIndex, lastTerm)
  276. if !ok {
  277. t.Fatalf("maybeCommit returned false")
  278. }
  279. raftLog.appliedTo(raftLog.committed)
  280. offset := uint64(500)
  281. storage.Compact(offset)
  282. if raftLog.lastIndex() != lastIndex {
  283. t.Errorf("lastIndex = %d, want %d", raftLog.lastIndex(), lastIndex)
  284. }
  285. for j := offset; j <= raftLog.lastIndex(); j++ {
  286. if mustTerm(raftLog.term(j)) != j {
  287. t.Errorf("term(%d) = %d, want %d", j, mustTerm(raftLog.term(j)), j)
  288. }
  289. }
  290. for j := offset; j <= raftLog.lastIndex(); j++ {
  291. if !raftLog.matchTerm(j, j) {
  292. t.Errorf("matchTerm(%d) = false, want true", j)
  293. }
  294. }
  295. unstableEnts := raftLog.unstableEntries()
  296. if g := len(unstableEnts); g != 250 {
  297. t.Errorf("len(unstableEntries) = %d, want = %d", g, 250)
  298. }
  299. if unstableEnts[0].Index != 751 {
  300. t.Errorf("Index = %d, want = %d", unstableEnts[0].Index, 751)
  301. }
  302. prev := raftLog.lastIndex()
  303. raftLog.append(pb.Entry{Index: raftLog.lastIndex() + 1, Term: raftLog.lastIndex() + 1})
  304. if raftLog.lastIndex() != prev+1 {
  305. t.Errorf("lastIndex = %d, want = %d", raftLog.lastIndex(), prev+1)
  306. }
  307. ents, err := raftLog.entries(raftLog.lastIndex(), noLimit)
  308. if err != nil {
  309. t.Fatalf("unexpected error %v", err)
  310. }
  311. if len(ents) != 1 {
  312. t.Errorf("len(entries) = %d, want = %d", len(ents), 1)
  313. }
  314. }
  315. func TestNextEnts(t *testing.T) {
  316. snap := pb.Snapshot{
  317. Metadata: pb.SnapshotMetadata{Term: 1, Index: 3},
  318. }
  319. ents := []pb.Entry{
  320. {Term: 1, Index: 4},
  321. {Term: 1, Index: 5},
  322. {Term: 1, Index: 6},
  323. }
  324. tests := []struct {
  325. applied uint64
  326. wents []pb.Entry
  327. }{
  328. {0, ents[:2]},
  329. {3, ents[:2]},
  330. {4, ents[1:2]},
  331. {5, nil},
  332. }
  333. for i, tt := range tests {
  334. storage := NewMemoryStorage()
  335. storage.ApplySnapshot(snap)
  336. raftLog := newLog(storage)
  337. raftLog.append(ents...)
  338. raftLog.maybeCommit(5, 1)
  339. raftLog.appliedTo(tt.applied)
  340. nents := raftLog.nextEnts()
  341. if !reflect.DeepEqual(nents, tt.wents) {
  342. t.Errorf("#%d: nents = %+v, want %+v", i, nents, tt.wents)
  343. }
  344. }
  345. }
  346. // TestUnstableEnts ensures unstableEntries returns the unstable part of the
  347. // entries correctly.
  348. func TestUnstableEnts(t *testing.T) {
  349. previousEnts := []pb.Entry{{Term: 1, Index: 1}, {Term: 2, Index: 2}}
  350. tests := []struct {
  351. unstable uint64
  352. wents []pb.Entry
  353. }{
  354. {3, nil},
  355. {1, previousEnts},
  356. }
  357. for i, tt := range tests {
  358. // append stable entries to storage
  359. storage := NewMemoryStorage()
  360. storage.Append(previousEnts[:tt.unstable-1])
  361. // append unstable entries to raftlog
  362. raftLog := newLog(storage)
  363. raftLog.append(previousEnts[tt.unstable-1:]...)
  364. ents := raftLog.unstableEntries()
  365. if l := len(ents); l > 0 {
  366. raftLog.stableTo(ents[l-1].Index, ents[l-i].Term)
  367. }
  368. if !reflect.DeepEqual(ents, tt.wents) {
  369. t.Errorf("#%d: unstableEnts = %+v, want %+v", i, ents, tt.wents)
  370. }
  371. w := previousEnts[len(previousEnts)-1].Index + 1
  372. if g := raftLog.unstable.offset; g != w {
  373. t.Errorf("#%d: unstable = %d, want %d", i, g, w)
  374. }
  375. }
  376. }
  377. func TestCommitTo(t *testing.T) {
  378. previousEnts := []pb.Entry{{Term: 1, Index: 1}, {Term: 2, Index: 2}, {Term: 3, Index: 3}}
  379. commit := uint64(2)
  380. tests := []struct {
  381. commit uint64
  382. wcommit uint64
  383. wpanic bool
  384. }{
  385. {3, 3, false},
  386. {1, 2, false}, // never decrease
  387. {4, 0, true}, // commit out of range -> panic
  388. }
  389. for i, tt := range tests {
  390. func() {
  391. defer func() {
  392. if r := recover(); r != nil {
  393. if tt.wpanic != true {
  394. t.Errorf("%d: panic = %v, want %v", i, true, tt.wpanic)
  395. }
  396. }
  397. }()
  398. raftLog := newLog(NewMemoryStorage())
  399. raftLog.append(previousEnts...)
  400. raftLog.committed = commit
  401. raftLog.commitTo(tt.commit)
  402. if raftLog.committed != tt.wcommit {
  403. t.Errorf("#%d: committed = %d, want %d", i, raftLog.committed, tt.wcommit)
  404. }
  405. }()
  406. }
  407. }
  408. func TestStableTo(t *testing.T) {
  409. tests := []struct {
  410. stablei uint64
  411. stablet uint64
  412. wunstable uint64
  413. }{
  414. {1, 1, 2},
  415. {2, 2, 3},
  416. {2, 1, 1}, // bad term
  417. {3, 1, 1}, // bad index
  418. }
  419. for i, tt := range tests {
  420. raftLog := newLog(NewMemoryStorage())
  421. raftLog.append([]pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}}...)
  422. raftLog.stableTo(tt.stablei, tt.stablet)
  423. if raftLog.unstable.offset != tt.wunstable {
  424. t.Errorf("#%d: unstable = %d, want %d", i, raftLog.unstable.offset, tt.wunstable)
  425. }
  426. }
  427. }
  428. func TestStableToWithSnap(t *testing.T) {
  429. snapi, snapt := uint64(5), uint64(2)
  430. tests := []struct {
  431. stablei uint64
  432. stablet uint64
  433. newEnts []pb.Entry
  434. wunstable uint64
  435. }{
  436. {snapi + 1, snapt, nil, snapi + 1},
  437. {snapi, snapt, nil, snapi + 1},
  438. {snapi - 1, snapt, nil, snapi + 1},
  439. {snapi + 1, snapt + 1, nil, snapi + 1},
  440. {snapi, snapt + 1, nil, snapi + 1},
  441. {snapi - 1, snapt + 1, nil, snapi + 1},
  442. {snapi + 1, snapt, []pb.Entry{{Index: snapi + 1, Term: snapt}}, snapi + 2},
  443. {snapi, snapt, []pb.Entry{{Index: snapi + 1, Term: snapt}}, snapi + 1},
  444. {snapi - 1, snapt, []pb.Entry{{Index: snapi + 1, Term: snapt}}, snapi + 1},
  445. {snapi + 1, snapt + 1, []pb.Entry{{Index: snapi + 1, Term: snapt}}, snapi + 1},
  446. {snapi, snapt + 1, []pb.Entry{{Index: snapi + 1, Term: snapt}}, snapi + 1},
  447. {snapi - 1, snapt + 1, []pb.Entry{{Index: snapi + 1, Term: snapt}}, snapi + 1},
  448. }
  449. for i, tt := range tests {
  450. s := NewMemoryStorage()
  451. s.ApplySnapshot(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: snapi, Term: snapt}})
  452. raftLog := newLog(s)
  453. raftLog.append(tt.newEnts...)
  454. raftLog.stableTo(tt.stablei, tt.stablet)
  455. if raftLog.unstable.offset != tt.wunstable {
  456. t.Errorf("#%d: unstable = %d, want %d", i, raftLog.unstable.offset, tt.wunstable)
  457. }
  458. }
  459. }
  460. //TestCompaction ensures that the number of log entries is correct after compactions.
  461. func TestCompaction(t *testing.T) {
  462. tests := []struct {
  463. lastIndex uint64
  464. compact []uint64
  465. wleft []int
  466. wallow bool
  467. }{
  468. // out of upper bound
  469. {1000, []uint64{1001}, []int{-1}, false},
  470. {1000, []uint64{300, 500, 800, 900}, []int{700, 500, 200, 100}, true},
  471. // out of lower bound
  472. {1000, []uint64{300, 299}, []int{700, -1}, false},
  473. }
  474. for i, tt := range tests {
  475. func() {
  476. defer func() {
  477. if r := recover(); r != nil {
  478. if tt.wallow == true {
  479. t.Errorf("%d: allow = %v, want %v: %v", i, false, true, r)
  480. }
  481. }
  482. }()
  483. storage := NewMemoryStorage()
  484. for i := uint64(1); i <= tt.lastIndex; i++ {
  485. storage.Append([]pb.Entry{{Index: i}})
  486. }
  487. raftLog := newLog(storage)
  488. raftLog.maybeCommit(tt.lastIndex, 0)
  489. raftLog.appliedTo(raftLog.committed)
  490. for j := 0; j < len(tt.compact); j++ {
  491. err := storage.Compact(tt.compact[j])
  492. if err != nil {
  493. if tt.wallow {
  494. t.Errorf("#%d.%d allow = %t, want %t", i, j, false, tt.wallow)
  495. }
  496. continue
  497. }
  498. if len(raftLog.allEntries()) != tt.wleft[j] {
  499. t.Errorf("#%d.%d len = %d, want %d", i, j, len(raftLog.allEntries()), tt.wleft[j])
  500. }
  501. }
  502. }()
  503. }
  504. }
  505. func TestLogRestore(t *testing.T) {
  506. index := uint64(1000)
  507. term := uint64(1000)
  508. snap := pb.SnapshotMetadata{Index: index, Term: term}
  509. storage := NewMemoryStorage()
  510. storage.ApplySnapshot(pb.Snapshot{Metadata: snap})
  511. raftLog := newLog(storage)
  512. if len(raftLog.allEntries()) != 0 {
  513. t.Errorf("len = %d, want 0", len(raftLog.allEntries()))
  514. }
  515. if raftLog.firstIndex() != index+1 {
  516. t.Errorf("firstIndex = %d, want %d", raftLog.firstIndex(), index+1)
  517. }
  518. if raftLog.committed != index {
  519. t.Errorf("committed = %d, want %d", raftLog.committed, index)
  520. }
  521. if raftLog.unstable.offset != index+1 {
  522. t.Errorf("unstable = %d, want %d", raftLog.unstable, index+1)
  523. }
  524. if mustTerm(raftLog.term(index)) != term {
  525. t.Errorf("term = %d, want %d", mustTerm(raftLog.term(index)), term)
  526. }
  527. }
  528. func TestIsOutOfBounds(t *testing.T) {
  529. offset := uint64(100)
  530. num := uint64(100)
  531. storage := NewMemoryStorage()
  532. storage.ApplySnapshot(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: offset}})
  533. l := newLog(storage)
  534. for i := uint64(1); i <= num; i++ {
  535. l.append(pb.Entry{Index: i + offset})
  536. }
  537. first := offset + 1
  538. tests := []struct {
  539. lo, hi uint64
  540. wpanic bool
  541. wErrCompacted bool
  542. }{
  543. {
  544. first - 2, first + 1,
  545. false,
  546. true,
  547. },
  548. {
  549. first - 1, first + 1,
  550. false,
  551. true,
  552. },
  553. {
  554. first, first,
  555. false,
  556. false,
  557. },
  558. {
  559. first + num/2, first + num/2,
  560. false,
  561. false,
  562. },
  563. {
  564. first + num - 1, first + num - 1,
  565. false,
  566. false,
  567. },
  568. {
  569. first + num, first + num,
  570. false,
  571. false,
  572. },
  573. {
  574. first + num, first + num + 1,
  575. true,
  576. false,
  577. },
  578. {
  579. first + num + 1, first + num + 1,
  580. true,
  581. false,
  582. },
  583. }
  584. for i, tt := range tests {
  585. func() {
  586. defer func() {
  587. if r := recover(); r != nil {
  588. if !tt.wpanic {
  589. t.Errorf("%d: panic = %v, want %v: %v", i, true, false, r)
  590. }
  591. }
  592. }()
  593. err := l.mustCheckOutOfBounds(tt.lo, tt.hi)
  594. if tt.wpanic {
  595. t.Errorf("%d: panic = %v, want %v", i, false, true)
  596. }
  597. if tt.wErrCompacted && err != ErrCompacted {
  598. t.Errorf("%d: err = %v, want %v", i, err, ErrCompacted)
  599. }
  600. if !tt.wErrCompacted && err != nil {
  601. t.Errorf("%d: unexpected err %v", i, err)
  602. }
  603. }()
  604. }
  605. }
  606. func TestTerm(t *testing.T) {
  607. var i uint64
  608. offset := uint64(100)
  609. num := uint64(100)
  610. storage := NewMemoryStorage()
  611. storage.ApplySnapshot(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: offset, Term: 1}})
  612. l := newLog(storage)
  613. for i = 1; i < num; i++ {
  614. l.append(pb.Entry{Index: offset + i, Term: i})
  615. }
  616. tests := []struct {
  617. index uint64
  618. w uint64
  619. }{
  620. {offset - 1, 0},
  621. {offset, 1},
  622. {offset + num/2, num / 2},
  623. {offset + num - 1, num - 1},
  624. {offset + num, 0},
  625. }
  626. for j, tt := range tests {
  627. term := mustTerm(l.term(tt.index))
  628. if !reflect.DeepEqual(term, tt.w) {
  629. t.Errorf("#%d: at = %d, want %d", j, term, tt.w)
  630. }
  631. }
  632. }
  633. func TestTermWithUnstableSnapshot(t *testing.T) {
  634. storagesnapi := uint64(100)
  635. unstablesnapi := storagesnapi + 5
  636. storage := NewMemoryStorage()
  637. storage.ApplySnapshot(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: storagesnapi, Term: 1}})
  638. l := newLog(storage)
  639. l.restore(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: unstablesnapi, Term: 1}})
  640. tests := []struct {
  641. index uint64
  642. w uint64
  643. }{
  644. // cannot get term from storage
  645. {storagesnapi, 0},
  646. // cannot get term from the gap between storage ents and unstable snapshot
  647. {storagesnapi + 1, 0},
  648. {unstablesnapi - 1, 0},
  649. // get term from unstable snapshot index
  650. {unstablesnapi, 1},
  651. }
  652. for i, tt := range tests {
  653. term := mustTerm(l.term(tt.index))
  654. if !reflect.DeepEqual(term, tt.w) {
  655. t.Errorf("#%d: at = %d, want %d", i, term, tt.w)
  656. }
  657. }
  658. }
  659. func TestSlice(t *testing.T) {
  660. var i uint64
  661. offset := uint64(100)
  662. num := uint64(100)
  663. last := offset + num
  664. half := offset + num/2
  665. halfe := pb.Entry{Index: half, Term: half}
  666. storage := NewMemoryStorage()
  667. storage.ApplySnapshot(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: offset}})
  668. for i = 1; i < num/2; i++ {
  669. storage.Append([]pb.Entry{{Index: offset + i, Term: offset + i}})
  670. }
  671. l := newLog(storage)
  672. for i = num / 2; i < num; i++ {
  673. l.append(pb.Entry{Index: offset + i, Term: offset + i})
  674. }
  675. tests := []struct {
  676. from uint64
  677. to uint64
  678. limit uint64
  679. w []pb.Entry
  680. wpanic bool
  681. }{
  682. // test no limit
  683. {offset - 1, offset + 1, noLimit, nil, false},
  684. {offset, offset + 1, noLimit, nil, false},
  685. {half - 1, half + 1, noLimit, []pb.Entry{{Index: half - 1, Term: half - 1}, {Index: half, Term: half}}, false},
  686. {half, half + 1, noLimit, []pb.Entry{{Index: half, Term: half}}, false},
  687. {last - 1, last, noLimit, []pb.Entry{{Index: last - 1, Term: last - 1}}, false},
  688. {last, last + 1, noLimit, nil, true},
  689. // test limit
  690. {half - 1, half + 1, 0, []pb.Entry{{Index: half - 1, Term: half - 1}}, false},
  691. {half - 1, half + 1, uint64(halfe.Size() + 1), []pb.Entry{{Index: half - 1, Term: half - 1}}, false},
  692. {half - 1, half + 1, uint64(halfe.Size() * 2), []pb.Entry{{Index: half - 1, Term: half - 1}, {Index: half, Term: half}}, false},
  693. {half - 1, half + 2, uint64(halfe.Size() * 3), []pb.Entry{{Index: half - 1, Term: half - 1}, {Index: half, Term: half}, {Index: half + 1, Term: half + 1}}, false},
  694. {half, half + 2, uint64(halfe.Size()), []pb.Entry{{Index: half, Term: half}}, false},
  695. {half, half + 2, uint64(halfe.Size() * 2), []pb.Entry{{Index: half, Term: half}, {Index: half + 1, Term: half + 1}}, false},
  696. }
  697. for j, tt := range tests {
  698. func() {
  699. defer func() {
  700. if r := recover(); r != nil {
  701. if !tt.wpanic {
  702. t.Errorf("%d: panic = %v, want %v: %v", j, true, false, r)
  703. }
  704. }
  705. }()
  706. g, err := l.slice(tt.from, tt.to, tt.limit)
  707. if tt.from <= offset && err != ErrCompacted {
  708. t.Fatalf("#%d: err = %v, want %v", j, err, ErrCompacted)
  709. }
  710. if tt.from > offset && err != nil {
  711. t.Fatalf("#%d: unexpected error %v", j, err)
  712. }
  713. if !reflect.DeepEqual(g, tt.w) {
  714. t.Errorf("#%d: from %d to %d = %v, want %v", j, tt.from, tt.to, g, tt.w)
  715. }
  716. }()
  717. }
  718. }
  719. func mustTerm(term uint64, err error) uint64 {
  720. if err != nil {
  721. panic(err)
  722. }
  723. return term
  724. }