log_unstable_test.go 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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 TestUnstableMaybeFirstIndex(t *testing.T) {
  21. tests := []struct {
  22. entries []pb.Entry
  23. offset uint64
  24. snap *pb.Snapshot
  25. wok bool
  26. windex uint64
  27. }{
  28. // no snapshot
  29. {
  30. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  31. false, 0,
  32. },
  33. {
  34. []pb.Entry{}, 0, nil,
  35. false, 0,
  36. },
  37. // has snapshot
  38. {
  39. []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  40. true, 5,
  41. },
  42. {
  43. []pb.Entry{}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  44. true, 5,
  45. },
  46. }
  47. for i, tt := range tests {
  48. u := unstable{
  49. entries: tt.entries,
  50. offset: tt.offset,
  51. snapshot: tt.snap,
  52. logger: raftLogger,
  53. }
  54. index, ok := u.maybeFirstIndex()
  55. if ok != tt.wok {
  56. t.Errorf("#%d: ok = %t, want %t", i, ok, tt.wok)
  57. }
  58. if index != tt.windex {
  59. t.Errorf("#%d: index = %d, want %d", i, index, tt.windex)
  60. }
  61. }
  62. }
  63. func TestMaybeLastIndex(t *testing.T) {
  64. tests := []struct {
  65. entries []pb.Entry
  66. offset uint64
  67. snap *pb.Snapshot
  68. wok bool
  69. windex uint64
  70. }{
  71. // last in entries
  72. {
  73. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  74. true, 5,
  75. },
  76. {
  77. []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  78. true, 5,
  79. },
  80. // last in snapshot
  81. {
  82. []pb.Entry{}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  83. true, 4,
  84. },
  85. // empty unstable
  86. {
  87. []pb.Entry{}, 0, nil,
  88. false, 0,
  89. },
  90. }
  91. for i, tt := range tests {
  92. u := unstable{
  93. entries: tt.entries,
  94. offset: tt.offset,
  95. snapshot: tt.snap,
  96. logger: raftLogger,
  97. }
  98. index, ok := u.maybeLastIndex()
  99. if ok != tt.wok {
  100. t.Errorf("#%d: ok = %t, want %t", i, ok, tt.wok)
  101. }
  102. if index != tt.windex {
  103. t.Errorf("#%d: index = %d, want %d", i, index, tt.windex)
  104. }
  105. }
  106. }
  107. func TestUnstableMaybeTerm(t *testing.T) {
  108. tests := []struct {
  109. entries []pb.Entry
  110. offset uint64
  111. snap *pb.Snapshot
  112. index uint64
  113. wok bool
  114. wterm uint64
  115. }{
  116. // term from entries
  117. {
  118. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  119. 5,
  120. true, 1,
  121. },
  122. {
  123. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  124. 6,
  125. false, 0,
  126. },
  127. {
  128. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  129. 4,
  130. false, 0,
  131. },
  132. {
  133. []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  134. 5,
  135. true, 1,
  136. },
  137. {
  138. []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  139. 6,
  140. false, 0,
  141. },
  142. // term from snapshot
  143. {
  144. []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  145. 4,
  146. true, 1,
  147. },
  148. {
  149. []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  150. 3,
  151. false, 0,
  152. },
  153. {
  154. []pb.Entry{}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  155. 5,
  156. false, 0,
  157. },
  158. {
  159. []pb.Entry{}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  160. 4,
  161. true, 1,
  162. },
  163. {
  164. []pb.Entry{}, 0, nil,
  165. 5,
  166. false, 0,
  167. },
  168. }
  169. for i, tt := range tests {
  170. u := unstable{
  171. entries: tt.entries,
  172. offset: tt.offset,
  173. snapshot: tt.snap,
  174. logger: raftLogger,
  175. }
  176. term, ok := u.maybeTerm(tt.index)
  177. if ok != tt.wok {
  178. t.Errorf("#%d: ok = %t, want %t", i, ok, tt.wok)
  179. }
  180. if term != tt.wterm {
  181. t.Errorf("#%d: term = %d, want %d", i, term, tt.wterm)
  182. }
  183. }
  184. }
  185. func TestUnstableRestore(t *testing.T) {
  186. u := unstable{
  187. entries: []pb.Entry{{Index: 5, Term: 1}},
  188. offset: 5,
  189. snapshot: &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  190. logger: raftLogger,
  191. }
  192. s := pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 6, Term: 2}}
  193. u.restore(s)
  194. if u.offset != s.Metadata.Index+1 {
  195. t.Errorf("offset = %d, want %d", u.offset, s.Metadata.Index+1)
  196. }
  197. if len(u.entries) != 0 {
  198. t.Errorf("len = %d, want 0", len(u.entries))
  199. }
  200. if !reflect.DeepEqual(u.snapshot, &s) {
  201. t.Errorf("snap = %v, want %v", u.snapshot, &s)
  202. }
  203. }
  204. func TestUnstableStableTo(t *testing.T) {
  205. tests := []struct {
  206. entries []pb.Entry
  207. offset uint64
  208. snap *pb.Snapshot
  209. index, term uint64
  210. woffset uint64
  211. wlen int
  212. }{
  213. {
  214. []pb.Entry{}, 0, nil,
  215. 5, 1,
  216. 0, 0,
  217. },
  218. {
  219. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  220. 5, 1, // stable to the first entry
  221. 6, 0,
  222. },
  223. {
  224. []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}}, 5, nil,
  225. 5, 1, // stable to the first entry
  226. 6, 1,
  227. },
  228. {
  229. []pb.Entry{{Index: 6, Term: 2}}, 6, nil,
  230. 6, 1, // stable to the first entry and term mismatch
  231. 6, 1,
  232. },
  233. {
  234. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  235. 4, 1, // stable to old entry
  236. 5, 1,
  237. },
  238. {
  239. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  240. 4, 2, // stable to old entry
  241. 5, 1,
  242. },
  243. // with snapshot
  244. {
  245. []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  246. 5, 1, // stable to the first entry
  247. 6, 0,
  248. },
  249. {
  250. []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  251. 5, 1, // stable to the first entry
  252. 6, 1,
  253. },
  254. {
  255. []pb.Entry{{Index: 6, Term: 2}}, 6, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 5, Term: 1}},
  256. 6, 1, // stable to the first entry and term mismatch
  257. 6, 1,
  258. },
  259. {
  260. []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  261. 4, 1, // stable to snapshot
  262. 5, 1,
  263. },
  264. {
  265. []pb.Entry{{Index: 5, Term: 2}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 2}},
  266. 4, 1, // stable to old entry
  267. 5, 1,
  268. },
  269. }
  270. for i, tt := range tests {
  271. u := unstable{
  272. entries: tt.entries,
  273. offset: tt.offset,
  274. snapshot: tt.snap,
  275. logger: raftLogger,
  276. }
  277. u.stableTo(tt.index, tt.term)
  278. if u.offset != tt.woffset {
  279. t.Errorf("#%d: offset = %d, want %d", i, u.offset, tt.woffset)
  280. }
  281. if len(u.entries) != tt.wlen {
  282. t.Errorf("#%d: len = %d, want %d", i, len(u.entries), tt.wlen)
  283. }
  284. }
  285. }
  286. func TestUnstableTruncateAndAppend(t *testing.T) {
  287. tests := []struct {
  288. entries []pb.Entry
  289. offset uint64
  290. snap *pb.Snapshot
  291. toappend []pb.Entry
  292. woffset uint64
  293. wentries []pb.Entry
  294. }{
  295. // append to the end
  296. {
  297. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  298. []pb.Entry{{Index: 6, Term: 1}, {Index: 7, Term: 1}},
  299. 5, []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}, {Index: 7, Term: 1}},
  300. },
  301. // replace the unstable entries
  302. {
  303. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  304. []pb.Entry{{Index: 5, Term: 2}, {Index: 6, Term: 2}},
  305. 5, []pb.Entry{{Index: 5, Term: 2}, {Index: 6, Term: 2}},
  306. },
  307. {
  308. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  309. []pb.Entry{{Index: 4, Term: 2}, {Index: 5, Term: 2}, {Index: 6, Term: 2}},
  310. 4, []pb.Entry{{Index: 4, Term: 2}, {Index: 5, Term: 2}, {Index: 6, Term: 2}},
  311. },
  312. // truncate the existing entries and append
  313. {
  314. []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}, {Index: 7, Term: 1}}, 5, nil,
  315. []pb.Entry{{Index: 6, Term: 2}},
  316. 5, []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 2}},
  317. },
  318. {
  319. []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}, {Index: 7, Term: 1}}, 5, nil,
  320. []pb.Entry{{Index: 7, Term: 2}, {Index: 8, Term: 2}},
  321. 5, []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}, {Index: 7, Term: 2}, {Index: 8, Term: 2}},
  322. },
  323. }
  324. for i, tt := range tests {
  325. u := unstable{
  326. entries: tt.entries,
  327. offset: tt.offset,
  328. snapshot: tt.snap,
  329. logger: raftLogger,
  330. }
  331. u.truncateAndAppend(tt.toappend)
  332. if u.offset != tt.woffset {
  333. t.Errorf("#%d: offset = %d, want %d", i, u.offset, tt.woffset)
  334. }
  335. if !reflect.DeepEqual(u.entries, tt.wentries) {
  336. t.Errorf("#%d: entries = %v, want %v", i, u.entries, tt.wentries)
  337. }
  338. }
  339. }