log_unstable_test.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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 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{}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  150. 5,
  151. false, 0,
  152. },
  153. {
  154. []pb.Entry{}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  155. 4,
  156. true, 1,
  157. },
  158. {
  159. []pb.Entry{}, 0, nil,
  160. 5,
  161. false, 0,
  162. },
  163. }
  164. for i, tt := range tests {
  165. u := unstable{
  166. entries: tt.entries,
  167. offset: tt.offset,
  168. snapshot: tt.snap,
  169. logger: raftLogger,
  170. }
  171. term, ok := u.maybeTerm(tt.index)
  172. if ok != tt.wok {
  173. t.Errorf("#%d: ok = %t, want %t", i, ok, tt.wok)
  174. }
  175. if term != tt.wterm {
  176. t.Errorf("#%d: term = %d, want %d", i, term, tt.wterm)
  177. }
  178. }
  179. }
  180. func TestUnstableRestore(t *testing.T) {
  181. u := unstable{
  182. entries: []pb.Entry{{Index: 5, Term: 1}},
  183. offset: 5,
  184. snapshot: &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  185. logger: raftLogger,
  186. }
  187. s := pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 6, Term: 2}}
  188. u.restore(s)
  189. if u.offset != s.Metadata.Index+1 {
  190. t.Errorf("offset = %d, want %d", u.offset, s.Metadata.Index+1)
  191. }
  192. if len(u.entries) != 0 {
  193. t.Errorf("len = %d, want 0", len(u.entries))
  194. }
  195. if !reflect.DeepEqual(u.snapshot, &s) {
  196. t.Errorf("snap = %v, want %v", u.snapshot, &s)
  197. }
  198. }
  199. func TestUnstableStableTo(t *testing.T) {
  200. tests := []struct {
  201. entries []pb.Entry
  202. offset uint64
  203. snap *pb.Snapshot
  204. index, term uint64
  205. woffset uint64
  206. wlen int
  207. }{
  208. {
  209. []pb.Entry{}, 0, nil,
  210. 5, 1,
  211. 0, 0,
  212. },
  213. {
  214. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  215. 5, 1, // stable to the first entry
  216. 6, 0,
  217. },
  218. {
  219. []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}}, 5, nil,
  220. 5, 1, // stable to the first entry
  221. 6, 1,
  222. },
  223. {
  224. []pb.Entry{{Index: 6, Term: 2}}, 5, nil,
  225. 6, 1, // stable to the first entry and term mismatch
  226. 5, 1,
  227. },
  228. {
  229. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  230. 4, 1, // stable to old entry
  231. 5, 1,
  232. },
  233. {
  234. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  235. 4, 2, // stable to old entry
  236. 5, 1,
  237. },
  238. // with snapshot
  239. {
  240. []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  241. 5, 1, // stable to the first entry
  242. 6, 0,
  243. },
  244. {
  245. []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  246. 5, 1, // stable to the first entry
  247. 6, 1,
  248. },
  249. {
  250. []pb.Entry{{Index: 6, Term: 2}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 5, Term: 1}},
  251. 6, 1, // stable to the first entry and term mismatch
  252. 5, 1,
  253. },
  254. {
  255. []pb.Entry{{Index: 5, Term: 1}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 1}},
  256. 4, 1, // stable to snapshot
  257. 5, 1,
  258. },
  259. {
  260. []pb.Entry{{Index: 5, Term: 2}}, 5, &pb.Snapshot{Metadata: pb.SnapshotMetadata{Index: 4, Term: 2}},
  261. 4, 1, // stable to old entry
  262. 5, 1,
  263. },
  264. }
  265. for i, tt := range tests {
  266. u := unstable{
  267. entries: tt.entries,
  268. offset: tt.offset,
  269. snapshot: tt.snap,
  270. logger: raftLogger,
  271. }
  272. u.stableTo(tt.index, tt.term)
  273. if u.offset != tt.woffset {
  274. t.Errorf("#%d: offset = %d, want %d", i, u.offset, tt.woffset)
  275. }
  276. if len(u.entries) != tt.wlen {
  277. t.Errorf("#%d: len = %d, want %d", i, len(u.entries), tt.wlen)
  278. }
  279. }
  280. }
  281. func TestUnstableTruncateAndAppend(t *testing.T) {
  282. tests := []struct {
  283. entries []pb.Entry
  284. offset uint64
  285. snap *pb.Snapshot
  286. toappend []pb.Entry
  287. woffset uint64
  288. wentries []pb.Entry
  289. }{
  290. // append to the end
  291. {
  292. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  293. []pb.Entry{{Index: 6, Term: 1}, {Index: 7, Term: 1}},
  294. 5, []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}, {Index: 7, Term: 1}},
  295. },
  296. // replace the unstable entries
  297. {
  298. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  299. []pb.Entry{{Index: 5, Term: 2}, {Index: 6, Term: 2}},
  300. 5, []pb.Entry{{Index: 5, Term: 2}, {Index: 6, Term: 2}},
  301. },
  302. {
  303. []pb.Entry{{Index: 5, Term: 1}}, 5, nil,
  304. []pb.Entry{{Index: 4, Term: 2}, {Index: 5, Term: 2}, {Index: 6, Term: 2}},
  305. 4, []pb.Entry{{Index: 4, Term: 2}, {Index: 5, Term: 2}, {Index: 6, Term: 2}},
  306. },
  307. // truncate the existing entries and append
  308. {
  309. []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}, {Index: 7, Term: 1}}, 5, nil,
  310. []pb.Entry{{Index: 6, Term: 2}},
  311. 5, []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 2}},
  312. },
  313. {
  314. []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}, {Index: 7, Term: 1}}, 5, nil,
  315. []pb.Entry{{Index: 7, Term: 2}, {Index: 8, Term: 2}},
  316. 5, []pb.Entry{{Index: 5, Term: 1}, {Index: 6, Term: 1}, {Index: 7, Term: 2}, {Index: 8, Term: 2}},
  317. },
  318. }
  319. for i, tt := range tests {
  320. u := unstable{
  321. entries: tt.entries,
  322. offset: tt.offset,
  323. snapshot: tt.snap,
  324. logger: raftLogger,
  325. }
  326. u.truncateAndAppend(tt.toappend)
  327. if u.offset != tt.woffset {
  328. t.Errorf("#%d: offset = %d, want %d", i, u.offset, tt.woffset)
  329. }
  330. if !reflect.DeepEqual(u.entries, tt.wentries) {
  331. t.Errorf("#%d: entries = %v, want %v", i, u.entries, tt.wentries)
  332. }
  333. }
  334. }