log_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. // Copyright 2015 The etcd Authors
  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(), raftLogger)
  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(), raftLogger)
  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, raftLogger)
  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(), raftLogger)
  226. raftLog.append(previousEnts...)
  227. raftLog.committed = commit
  228. func() {
  229. defer func() {
  230. if r := recover(); r != nil {
  231. if !tt.wpanic {
  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 functionality 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, raftLogger)
  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 TestHasNextEnts(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. hasNext bool
  327. }{
  328. {0, true},
  329. {3, true},
  330. {4, true},
  331. {5, false},
  332. }
  333. for i, tt := range tests {
  334. storage := NewMemoryStorage()
  335. storage.ApplySnapshot(snap)
  336. raftLog := newLog(storage, raftLogger)
  337. raftLog.append(ents...)
  338. raftLog.maybeCommit(5, 1)
  339. raftLog.appliedTo(tt.applied)
  340. hasNext := raftLog.hasNextEnts()
  341. if hasNext != tt.hasNext {
  342. t.Errorf("#%d: hasNext = %v, want %v", i, hasNext, tt.hasNext)
  343. }
  344. }
  345. }
  346. func TestNextEnts(t *testing.T) {
  347. snap := pb.Snapshot{
  348. Metadata: pb.SnapshotMetadata{Term: 1, Index: 3},
  349. }
  350. ents := []pb.Entry{
  351. {Term: 1, Index: 4},
  352. {Term: 1, Index: 5},
  353. {Term: 1, Index: 6},
  354. }
  355. tests := []struct {
  356. applied uint64
  357. wents []pb.Entry
  358. }{
  359. {0, ents[:2]},
  360. {3, ents[:2]},
  361. {4, ents[1:2]},
  362. {5, nil},
  363. }
  364. for i, tt := range tests {
  365. storage := NewMemoryStorage()
  366. storage.ApplySnapshot(snap)
  367. raftLog := newLog(storage, raftLogger)
  368. raftLog.append(ents...)
  369. raftLog.maybeCommit(5, 1)
  370. raftLog.appliedTo(tt.applied)
  371. nents := raftLog.nextEnts()
  372. if !reflect.DeepEqual(nents, tt.wents) {
  373. t.Errorf("#%d: nents = %+v, want %+v", i, nents, tt.wents)
  374. }
  375. }
  376. }
  377. // TestUnstableEnts ensures unstableEntries returns the unstable part of the
  378. // entries correctly.
  379. func TestUnstableEnts(t *testing.T) {
  380. previousEnts := []pb.Entry{{Term: 1, Index: 1}, {Term: 2, Index: 2}}
  381. tests := []struct {
  382. unstable uint64
  383. wents []pb.Entry
  384. }{
  385. {3, nil},
  386. {1, previousEnts},
  387. }
  388. for i, tt := range tests {
  389. // append stable entries to storage
  390. storage := NewMemoryStorage()
  391. storage.Append(previousEnts[:tt.unstable-1])
  392. // append unstable entries to raftlog
  393. raftLog := newLog(storage, raftLogger)
  394. raftLog.append(previousEnts[tt.unstable-1:]...)
  395. ents := raftLog.unstableEntries()
  396. if l := len(ents); l > 0 {
  397. raftLog.stableTo(ents[l-1].Index, ents[l-i].Term)
  398. }
  399. if !reflect.DeepEqual(ents, tt.wents) {
  400. t.Errorf("#%d: unstableEnts = %+v, want %+v", i, ents, tt.wents)
  401. }
  402. w := previousEnts[len(previousEnts)-1].Index + 1
  403. if g := raftLog.unstable.offset; g != w {
  404. t.Errorf("#%d: unstable = %d, want %d", i, g, w)
  405. }
  406. }
  407. }
  408. func TestCommitTo(t *testing.T) {
  409. previousEnts := []pb.Entry{{Term: 1, Index: 1}, {Term: 2, Index: 2}, {Term: 3, Index: 3}}
  410. commit := uint64(2)
  411. tests := []struct {
  412. commit uint64
  413. wcommit uint64
  414. wpanic bool
  415. }{
  416. {3, 3, false},
  417. {1, 2, false}, // never decrease
  418. {4, 0, true}, // commit out of range -> panic
  419. }
  420. for i, tt := range tests {
  421. func() {
  422. defer func() {
  423. if r := recover(); r != nil {
  424. if !tt.wpanic {
  425. t.Errorf("%d: panic = %v, want %v", i, true, tt.wpanic)
  426. }
  427. }
  428. }()
  429. raftLog := newLog(NewMemoryStorage(), raftLogger)
  430. raftLog.append(previousEnts...)
  431. raftLog.committed = commit
  432. raftLog.commitTo(tt.commit)
  433. if raftLog.committed != tt.wcommit {
  434. t.Errorf("#%d: committed = %d, want %d", i, raftLog.committed, tt.wcommit)
  435. }
  436. }()
  437. }
  438. }
  439. func TestStableTo(t *testing.T) {
  440. tests := []struct {
  441. stablei uint64
  442. stablet uint64
  443. wunstable uint64
  444. }{
  445. {1, 1, 2},
  446. {2, 2, 3},
  447. {2, 1, 1}, // bad term
  448. {3, 1, 1}, // bad index
  449. }
  450. for i, tt := range tests {
  451. raftLog := newLog(NewMemoryStorage(), raftLogger)
  452. raftLog.append([]pb.Entry{{Index: 1, Term: 1}, {Index: 2, Term: 2}}...)
  453. raftLog.stableTo(tt.stablei, tt.stablet)
  454. if raftLog.unstable.offset != tt.wunstable {
  455. t.Errorf("#%d: unstable = %d, want %d", i, raftLog.unstable.offset, tt.wunstable)
  456. }
  457. }
  458. }
  459. func TestStableToWithSnap(t *testing.T) {
  460. snapi, snapt := uint64(5), uint64(2)
  461. tests := []struct {
  462. stablei uint64
  463. stablet uint64
  464. newEnts []pb.Entry
  465. wunstable uint64
  466. }{
  467. {snapi + 1, snapt, nil, snapi + 1},
  468. {snapi, snapt, nil, snapi + 1},
  469. {snapi - 1, snapt, nil, snapi + 1},
  470. {snapi + 1, snapt + 1, nil, snapi + 1},
  471. {snapi, snapt + 1, nil, snapi + 1},
  472. {snapi - 1, snapt + 1, nil, snapi + 1},
  473. {snapi + 1, snapt, []pb.Entry{{Index: snapi + 1, Term: snapt}}, snapi + 2},
  474. {snapi, snapt, []pb.Entry{{Index: snapi + 1, Term: snapt}}, snapi + 1},
  475. {snapi - 1, snapt, []pb.Entry{{Index: snapi + 1, Term: snapt}}, snapi + 1},
  476. {snapi + 1, snapt + 1, []pb.Entry{{Index: snapi + 1, Term: snapt}}, snapi + 1},
  477. {snapi, snapt + 1, []pb.Entry{{Index: snapi + 1, Term: snapt}}, snapi + 1},
  478. {snapi - 1, snapt + 1, []pb.Entry{{Index: snapi + 1, Term: snapt}}, snapi + 1},
  479. }
  480. for i, tt := range tests {
  481. s := NewMemoryStorage()
  482. s.ApplySnapshot(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: snapi, Term: snapt}})
  483. raftLog := newLog(s, raftLogger)
  484. raftLog.append(tt.newEnts...)
  485. raftLog.stableTo(tt.stablei, tt.stablet)
  486. if raftLog.unstable.offset != tt.wunstable {
  487. t.Errorf("#%d: unstable = %d, want %d", i, raftLog.unstable.offset, tt.wunstable)
  488. }
  489. }
  490. }
  491. //TestCompaction ensures that the number of log entries is correct after compactions.
  492. func TestCompaction(t *testing.T) {
  493. tests := []struct {
  494. lastIndex uint64
  495. compact []uint64
  496. wleft []int
  497. wallow bool
  498. }{
  499. // out of upper bound
  500. {1000, []uint64{1001}, []int{-1}, false},
  501. {1000, []uint64{300, 500, 800, 900}, []int{700, 500, 200, 100}, true},
  502. // out of lower bound
  503. {1000, []uint64{300, 299}, []int{700, -1}, false},
  504. }
  505. for i, tt := range tests {
  506. func() {
  507. defer func() {
  508. if r := recover(); r != nil {
  509. if tt.wallow {
  510. t.Errorf("%d: allow = %v, want %v: %v", i, false, true, r)
  511. }
  512. }
  513. }()
  514. storage := NewMemoryStorage()
  515. for i := uint64(1); i <= tt.lastIndex; i++ {
  516. storage.Append([]pb.Entry{{Index: i}})
  517. }
  518. raftLog := newLog(storage, raftLogger)
  519. raftLog.maybeCommit(tt.lastIndex, 0)
  520. raftLog.appliedTo(raftLog.committed)
  521. for j := 0; j < len(tt.compact); j++ {
  522. err := storage.Compact(tt.compact[j])
  523. if err != nil {
  524. if tt.wallow {
  525. t.Errorf("#%d.%d allow = %t, want %t", i, j, false, tt.wallow)
  526. }
  527. continue
  528. }
  529. if len(raftLog.allEntries()) != tt.wleft[j] {
  530. t.Errorf("#%d.%d len = %d, want %d", i, j, len(raftLog.allEntries()), tt.wleft[j])
  531. }
  532. }
  533. }()
  534. }
  535. }
  536. func TestLogRestore(t *testing.T) {
  537. index := uint64(1000)
  538. term := uint64(1000)
  539. snap := pb.SnapshotMetadata{Index: index, Term: term}
  540. storage := NewMemoryStorage()
  541. storage.ApplySnapshot(pb.Snapshot{Metadata: snap})
  542. raftLog := newLog(storage, raftLogger)
  543. if len(raftLog.allEntries()) != 0 {
  544. t.Errorf("len = %d, want 0", len(raftLog.allEntries()))
  545. }
  546. if raftLog.firstIndex() != index+1 {
  547. t.Errorf("firstIndex = %d, want %d", raftLog.firstIndex(), index+1)
  548. }
  549. if raftLog.committed != index {
  550. t.Errorf("committed = %d, want %d", raftLog.committed, index)
  551. }
  552. if raftLog.unstable.offset != index+1 {
  553. t.Errorf("unstable = %d, want %d", raftLog.unstable.offset, index+1)
  554. }
  555. if mustTerm(raftLog.term(index)) != term {
  556. t.Errorf("term = %d, want %d", mustTerm(raftLog.term(index)), term)
  557. }
  558. }
  559. func TestIsOutOfBounds(t *testing.T) {
  560. offset := uint64(100)
  561. num := uint64(100)
  562. storage := NewMemoryStorage()
  563. storage.ApplySnapshot(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: offset}})
  564. l := newLog(storage, raftLogger)
  565. for i := uint64(1); i <= num; i++ {
  566. l.append(pb.Entry{Index: i + offset})
  567. }
  568. first := offset + 1
  569. tests := []struct {
  570. lo, hi uint64
  571. wpanic bool
  572. wErrCompacted bool
  573. }{
  574. {
  575. first - 2, first + 1,
  576. false,
  577. true,
  578. },
  579. {
  580. first - 1, first + 1,
  581. false,
  582. true,
  583. },
  584. {
  585. first, first,
  586. false,
  587. false,
  588. },
  589. {
  590. first + num/2, first + num/2,
  591. false,
  592. false,
  593. },
  594. {
  595. first + num - 1, first + num - 1,
  596. false,
  597. false,
  598. },
  599. {
  600. first + num, first + num,
  601. false,
  602. false,
  603. },
  604. {
  605. first + num, first + num + 1,
  606. true,
  607. false,
  608. },
  609. {
  610. first + num + 1, first + num + 1,
  611. true,
  612. false,
  613. },
  614. }
  615. for i, tt := range tests {
  616. func() {
  617. defer func() {
  618. if r := recover(); r != nil {
  619. if !tt.wpanic {
  620. t.Errorf("%d: panic = %v, want %v: %v", i, true, false, r)
  621. }
  622. }
  623. }()
  624. err := l.mustCheckOutOfBounds(tt.lo, tt.hi)
  625. if tt.wpanic {
  626. t.Errorf("%d: panic = %v, want %v", i, false, true)
  627. }
  628. if tt.wErrCompacted && err != ErrCompacted {
  629. t.Errorf("%d: err = %v, want %v", i, err, ErrCompacted)
  630. }
  631. if !tt.wErrCompacted && err != nil {
  632. t.Errorf("%d: unexpected err %v", i, err)
  633. }
  634. }()
  635. }
  636. }
  637. func TestTerm(t *testing.T) {
  638. var i uint64
  639. offset := uint64(100)
  640. num := uint64(100)
  641. storage := NewMemoryStorage()
  642. storage.ApplySnapshot(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: offset, Term: 1}})
  643. l := newLog(storage, raftLogger)
  644. for i = 1; i < num; i++ {
  645. l.append(pb.Entry{Index: offset + i, Term: i})
  646. }
  647. tests := []struct {
  648. index uint64
  649. w uint64
  650. }{
  651. {offset - 1, 0},
  652. {offset, 1},
  653. {offset + num/2, num / 2},
  654. {offset + num - 1, num - 1},
  655. {offset + num, 0},
  656. }
  657. for j, tt := range tests {
  658. term := mustTerm(l.term(tt.index))
  659. if !reflect.DeepEqual(term, tt.w) {
  660. t.Errorf("#%d: at = %d, want %d", j, term, tt.w)
  661. }
  662. }
  663. }
  664. func TestTermWithUnstableSnapshot(t *testing.T) {
  665. storagesnapi := uint64(100)
  666. unstablesnapi := storagesnapi + 5
  667. storage := NewMemoryStorage()
  668. storage.ApplySnapshot(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: storagesnapi, Term: 1}})
  669. l := newLog(storage, raftLogger)
  670. l.restore(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: unstablesnapi, Term: 1}})
  671. tests := []struct {
  672. index uint64
  673. w uint64
  674. }{
  675. // cannot get term from storage
  676. {storagesnapi, 0},
  677. // cannot get term from the gap between storage ents and unstable snapshot
  678. {storagesnapi + 1, 0},
  679. {unstablesnapi - 1, 0},
  680. // get term from unstable snapshot index
  681. {unstablesnapi, 1},
  682. }
  683. for i, tt := range tests {
  684. term := mustTerm(l.term(tt.index))
  685. if !reflect.DeepEqual(term, tt.w) {
  686. t.Errorf("#%d: at = %d, want %d", i, term, tt.w)
  687. }
  688. }
  689. }
  690. func TestSlice(t *testing.T) {
  691. var i uint64
  692. offset := uint64(100)
  693. num := uint64(100)
  694. last := offset + num
  695. half := offset + num/2
  696. halfe := pb.Entry{Index: half, Term: half}
  697. storage := NewMemoryStorage()
  698. storage.ApplySnapshot(pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: offset}})
  699. for i = 1; i < num/2; i++ {
  700. storage.Append([]pb.Entry{{Index: offset + i, Term: offset + i}})
  701. }
  702. l := newLog(storage, raftLogger)
  703. for i = num / 2; i < num; i++ {
  704. l.append(pb.Entry{Index: offset + i, Term: offset + i})
  705. }
  706. tests := []struct {
  707. from uint64
  708. to uint64
  709. limit uint64
  710. w []pb.Entry
  711. wpanic bool
  712. }{
  713. // test no limit
  714. {offset - 1, offset + 1, noLimit, nil, false},
  715. {offset, offset + 1, noLimit, nil, false},
  716. {half - 1, half + 1, noLimit, []pb.Entry{{Index: half - 1, Term: half - 1}, {Index: half, Term: half}}, false},
  717. {half, half + 1, noLimit, []pb.Entry{{Index: half, Term: half}}, false},
  718. {last - 1, last, noLimit, []pb.Entry{{Index: last - 1, Term: last - 1}}, false},
  719. {last, last + 1, noLimit, nil, true},
  720. // test limit
  721. {half - 1, half + 1, 0, []pb.Entry{{Index: half - 1, Term: half - 1}}, false},
  722. {half - 1, half + 1, uint64(halfe.Size() + 1), []pb.Entry{{Index: half - 1, Term: half - 1}}, false},
  723. {half - 2, half + 1, uint64(halfe.Size() + 1), []pb.Entry{{Index: half - 2, Term: half - 2}}, false},
  724. {half - 1, half + 1, uint64(halfe.Size() * 2), []pb.Entry{{Index: half - 1, Term: half - 1}, {Index: half, Term: half}}, false},
  725. {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},
  726. {half, half + 2, uint64(halfe.Size()), []pb.Entry{{Index: half, Term: half}}, false},
  727. {half, half + 2, uint64(halfe.Size() * 2), []pb.Entry{{Index: half, Term: half}, {Index: half + 1, Term: half + 1}}, false},
  728. }
  729. for j, tt := range tests {
  730. func() {
  731. defer func() {
  732. if r := recover(); r != nil {
  733. if !tt.wpanic {
  734. t.Errorf("%d: panic = %v, want %v: %v", j, true, false, r)
  735. }
  736. }
  737. }()
  738. g, err := l.slice(tt.from, tt.to, tt.limit)
  739. if tt.from <= offset && err != ErrCompacted {
  740. t.Fatalf("#%d: err = %v, want %v", j, err, ErrCompacted)
  741. }
  742. if tt.from > offset && err != nil {
  743. t.Fatalf("#%d: unexpected error %v", j, err)
  744. }
  745. if !reflect.DeepEqual(g, tt.w) {
  746. t.Errorf("#%d: from %d to %d = %v, want %v", j, tt.from, tt.to, g, tt.w)
  747. }
  748. }()
  749. }
  750. }
  751. func mustTerm(term uint64, err error) uint64 {
  752. if err != nil {
  753. panic(err)
  754. }
  755. return term
  756. }