log_test.go 22 KB

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