wal_test.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 wal
  15. import (
  16. "bytes"
  17. "io/ioutil"
  18. "os"
  19. "path"
  20. "reflect"
  21. "testing"
  22. "github.com/coreos/etcd/pkg/pbutil"
  23. "github.com/coreos/etcd/raft/raftpb"
  24. "github.com/coreos/etcd/wal/walpb"
  25. )
  26. func TestNew(t *testing.T) {
  27. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. defer os.RemoveAll(p)
  32. w, err := Create(p, []byte("somedata"))
  33. if err != nil {
  34. t.Fatalf("err = %v, want nil", err)
  35. }
  36. if g := path.Base(w.f.Name()); g != walName(0, 0) {
  37. t.Errorf("name = %+v, want %+v", g, walName(0, 0))
  38. }
  39. defer w.Close()
  40. gd, err := ioutil.ReadFile(w.f.Name())
  41. if err != nil {
  42. t.Fatalf("err = %v, want nil", err)
  43. }
  44. var wb bytes.Buffer
  45. e := newEncoder(&wb, 0)
  46. err = e.encode(&walpb.Record{Type: crcType, Crc: 0})
  47. if err != nil {
  48. t.Fatalf("err = %v, want nil", err)
  49. }
  50. err = e.encode(&walpb.Record{Type: metadataType, Data: []byte("somedata")})
  51. if err != nil {
  52. t.Fatalf("err = %v, want nil", err)
  53. }
  54. r := &walpb.Record{
  55. Type: snapshotType,
  56. Data: pbutil.MustMarshal(&walpb.Snapshot{}),
  57. }
  58. if err = e.encode(r); err != nil {
  59. t.Fatalf("err = %v, want nil", err)
  60. }
  61. e.flush()
  62. if !reflect.DeepEqual(gd, wb.Bytes()) {
  63. t.Errorf("data = %v, want %v", gd, wb.Bytes())
  64. }
  65. }
  66. func TestNewForInitedDir(t *testing.T) {
  67. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  68. if err != nil {
  69. t.Fatal(err)
  70. }
  71. defer os.RemoveAll(p)
  72. os.Create(path.Join(p, walName(0, 0)))
  73. if _, err = Create(p, nil); err == nil || err != os.ErrExist {
  74. t.Errorf("err = %v, want %v", err, os.ErrExist)
  75. }
  76. }
  77. func TestOpenAtIndex(t *testing.T) {
  78. dir, err := ioutil.TempDir(os.TempDir(), "waltest")
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. defer os.RemoveAll(dir)
  83. f, err := os.Create(path.Join(dir, walName(0, 0)))
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. f.Close()
  88. w, err := Open(dir, walpb.Snapshot{})
  89. if err != nil {
  90. t.Fatalf("err = %v, want nil", err)
  91. }
  92. if g := path.Base(w.f.Name()); g != walName(0, 0) {
  93. t.Errorf("name = %+v, want %+v", g, walName(0, 0))
  94. }
  95. if w.seq != 0 {
  96. t.Errorf("seq = %d, want %d", w.seq, 0)
  97. }
  98. w.Close()
  99. wname := walName(2, 10)
  100. f, err = os.Create(path.Join(dir, wname))
  101. if err != nil {
  102. t.Fatal(err)
  103. }
  104. f.Close()
  105. w, err = Open(dir, walpb.Snapshot{Index: 5})
  106. if err != nil {
  107. t.Fatalf("err = %v, want nil", err)
  108. }
  109. if g := path.Base(w.f.Name()); g != wname {
  110. t.Errorf("name = %+v, want %+v", g, wname)
  111. }
  112. if w.seq != 2 {
  113. t.Errorf("seq = %d, want %d", w.seq, 2)
  114. }
  115. w.Close()
  116. emptydir, err := ioutil.TempDir(os.TempDir(), "waltestempty")
  117. if err != nil {
  118. t.Fatal(err)
  119. }
  120. defer os.RemoveAll(emptydir)
  121. if _, err = Open(emptydir, walpb.Snapshot{}); err != ErrFileNotFound {
  122. t.Errorf("err = %v, want %v", err, ErrFileNotFound)
  123. }
  124. }
  125. // TODO: split it into smaller tests for better readability
  126. func TestCut(t *testing.T) {
  127. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  128. if err != nil {
  129. t.Fatal(err)
  130. }
  131. defer os.RemoveAll(p)
  132. w, err := Create(p, nil)
  133. if err != nil {
  134. t.Fatal(err)
  135. }
  136. defer w.Close()
  137. state := raftpb.HardState{Term: 1}
  138. // TODO(unihorn): remove this when cut can operate on an empty file
  139. if err := w.Save(state, []raftpb.Entry{{}}); err != nil {
  140. t.Fatal(err)
  141. }
  142. if err := w.Cut(); err != nil {
  143. t.Fatal(err)
  144. }
  145. wname := walName(1, 1)
  146. if g := path.Base(w.f.Name()); g != wname {
  147. t.Errorf("name = %s, want %s", g, wname)
  148. }
  149. es := []raftpb.Entry{{Index: 1, Term: 1, Data: []byte{1}}}
  150. if err := w.Save(raftpb.HardState{}, es); err != nil {
  151. t.Fatal(err)
  152. }
  153. if err := w.Cut(); err != nil {
  154. t.Fatal(err)
  155. }
  156. snap := walpb.Snapshot{Index: 2, Term: 1}
  157. if err := w.SaveSnapshot(snap); err != nil {
  158. t.Fatal(err)
  159. }
  160. wname = walName(2, 2)
  161. if g := path.Base(w.f.Name()); g != wname {
  162. t.Errorf("name = %s, want %s", g, wname)
  163. }
  164. // check the state in the last WAL
  165. // We do check before closing the WAL to ensure that Cut syncs the data
  166. // into the disk.
  167. f, err := os.Open(path.Join(p, wname))
  168. if err != nil {
  169. t.Fatal(err)
  170. }
  171. defer f.Close()
  172. nw := &WAL{
  173. decoder: newDecoder(f),
  174. start: snap,
  175. }
  176. _, gst, _, err := nw.ReadAll()
  177. if err != nil {
  178. t.Fatal(err)
  179. }
  180. if !reflect.DeepEqual(gst, state) {
  181. t.Errorf("state = %+v, want %+v", gst, state)
  182. }
  183. }
  184. func TestRecover(t *testing.T) {
  185. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  186. if err != nil {
  187. t.Fatal(err)
  188. }
  189. defer os.RemoveAll(p)
  190. w, err := Create(p, []byte("metadata"))
  191. if err != nil {
  192. t.Fatal(err)
  193. }
  194. if err := w.SaveSnapshot(walpb.Snapshot{}); err != nil {
  195. t.Fatal(err)
  196. }
  197. ents := []raftpb.Entry{{Index: 1, Term: 1, Data: []byte{1}}, {Index: 2, Term: 2, Data: []byte{2}}}
  198. if err = w.Save(raftpb.HardState{}, ents); err != nil {
  199. t.Fatal(err)
  200. }
  201. sts := []raftpb.HardState{{Term: 1, Vote: 1, Commit: 1}, {Term: 2, Vote: 2, Commit: 2}}
  202. for _, s := range sts {
  203. if err = w.Save(s, nil); err != nil {
  204. t.Fatal(err)
  205. }
  206. }
  207. w.Close()
  208. if w, err = Open(p, walpb.Snapshot{}); err != nil {
  209. t.Fatal(err)
  210. }
  211. metadata, state, entries, err := w.ReadAll()
  212. if err != nil {
  213. t.Fatal(err)
  214. }
  215. if !reflect.DeepEqual(metadata, []byte("metadata")) {
  216. t.Errorf("metadata = %s, want %s", metadata, "metadata")
  217. }
  218. if !reflect.DeepEqual(entries, ents) {
  219. t.Errorf("ents = %+v, want %+v", entries, ents)
  220. }
  221. // only the latest state is recorded
  222. s := sts[len(sts)-1]
  223. if !reflect.DeepEqual(state, s) {
  224. t.Errorf("state = %+v, want %+v", state, s)
  225. }
  226. w.Close()
  227. }
  228. func TestSearchIndex(t *testing.T) {
  229. tests := []struct {
  230. names []string
  231. index uint64
  232. widx int
  233. wok bool
  234. }{
  235. {
  236. []string{
  237. "0000000000000000-0000000000000000.wal",
  238. "0000000000000001-0000000000001000.wal",
  239. "0000000000000002-0000000000002000.wal",
  240. },
  241. 0x1000, 1, true,
  242. },
  243. {
  244. []string{
  245. "0000000000000001-0000000000004000.wal",
  246. "0000000000000002-0000000000003000.wal",
  247. "0000000000000003-0000000000005000.wal",
  248. },
  249. 0x4000, 1, true,
  250. },
  251. {
  252. []string{
  253. "0000000000000001-0000000000002000.wal",
  254. "0000000000000002-0000000000003000.wal",
  255. "0000000000000003-0000000000005000.wal",
  256. },
  257. 0x1000, -1, false,
  258. },
  259. }
  260. for i, tt := range tests {
  261. idx, ok := searchIndex(tt.names, tt.index)
  262. if idx != tt.widx {
  263. t.Errorf("#%d: idx = %d, want %d", i, idx, tt.widx)
  264. }
  265. if ok != tt.wok {
  266. t.Errorf("#%d: ok = %v, want %v", i, ok, tt.wok)
  267. }
  268. }
  269. }
  270. func TestScanWalName(t *testing.T) {
  271. tests := []struct {
  272. str string
  273. wseq, windex uint64
  274. wok bool
  275. }{
  276. {"0000000000000000-0000000000000000.wal", 0, 0, true},
  277. {"0000000000000000.wal", 0, 0, false},
  278. {"0000000000000000-0000000000000000.snap", 0, 0, false},
  279. }
  280. for i, tt := range tests {
  281. s, index, err := parseWalName(tt.str)
  282. if g := err == nil; g != tt.wok {
  283. t.Errorf("#%d: ok = %v, want %v", i, g, tt.wok)
  284. }
  285. if s != tt.wseq {
  286. t.Errorf("#%d: seq = %d, want %d", i, s, tt.wseq)
  287. }
  288. if index != tt.windex {
  289. t.Errorf("#%d: index = %d, want %d", i, index, tt.windex)
  290. }
  291. }
  292. }
  293. func TestRecoverAfterCut(t *testing.T) {
  294. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  295. if err != nil {
  296. t.Fatal(err)
  297. }
  298. defer os.RemoveAll(p)
  299. md, err := Create(p, []byte("metadata"))
  300. if err != nil {
  301. t.Fatal(err)
  302. }
  303. for i := 0; i < 10; i++ {
  304. if err = md.SaveSnapshot(walpb.Snapshot{Index: uint64(i)}); err != nil {
  305. t.Fatal(err)
  306. }
  307. es := []raftpb.Entry{{Index: uint64(i)}}
  308. if err = md.Save(raftpb.HardState{}, es); err != nil {
  309. t.Fatal(err)
  310. }
  311. if err = md.Cut(); err != nil {
  312. t.Fatal(err)
  313. }
  314. }
  315. md.Close()
  316. if err := os.Remove(path.Join(p, walName(4, 4))); err != nil {
  317. t.Fatal(err)
  318. }
  319. for i := 0; i < 10; i++ {
  320. w, err := Open(p, walpb.Snapshot{Index: uint64(i)})
  321. if err != nil {
  322. if i <= 4 {
  323. if err != ErrFileNotFound {
  324. t.Errorf("#%d: err = %v, want %v", i, err, ErrFileNotFound)
  325. }
  326. } else {
  327. t.Errorf("#%d: err = %v, want nil", i, err)
  328. }
  329. continue
  330. }
  331. metadata, _, entries, err := w.ReadAll()
  332. if err != nil {
  333. t.Errorf("#%d: err = %v, want nil", i, err)
  334. continue
  335. }
  336. if !reflect.DeepEqual(metadata, []byte("metadata")) {
  337. t.Errorf("#%d: metadata = %s, want %s", i, metadata, "metadata")
  338. }
  339. for j, e := range entries {
  340. if e.Index != uint64(j+i+1) {
  341. t.Errorf("#%d: ents[%d].Index = %+v, want %+v", i, j, e.Index, j+i+1)
  342. }
  343. }
  344. w.Close()
  345. }
  346. }
  347. func TestOpenAtUncommittedIndex(t *testing.T) {
  348. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  349. if err != nil {
  350. t.Fatal(err)
  351. }
  352. defer os.RemoveAll(p)
  353. w, err := Create(p, nil)
  354. if err != nil {
  355. t.Fatal(err)
  356. }
  357. if err := w.SaveSnapshot(walpb.Snapshot{}); err != nil {
  358. t.Fatal(err)
  359. }
  360. if err := w.Save(raftpb.HardState{}, []raftpb.Entry{{Index: 0}}); err != nil {
  361. t.Fatal(err)
  362. }
  363. w.Close()
  364. w, err = Open(p, walpb.Snapshot{})
  365. if err != nil {
  366. t.Fatal(err)
  367. }
  368. // commit up to index 0, try to read index 1
  369. if _, _, _, err := w.ReadAll(); err != nil {
  370. t.Errorf("err = %v, want nil", err)
  371. }
  372. w.Close()
  373. }
  374. // TestOpenNotInUse tests that OpenNotInUse can load all files that are
  375. // not in use at that point.
  376. // The tests creates WAL directory, and cut out multiple WAL files. Then
  377. // it releases the lock of part of data, and excepts that OpenNotInUse
  378. // can read out all unlocked data.
  379. func TestOpenNotInUse(t *testing.T) {
  380. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  381. if err != nil {
  382. t.Fatal(err)
  383. }
  384. defer os.RemoveAll(p)
  385. // create WAL
  386. w, err := Create(p, nil)
  387. defer w.Close()
  388. if err != nil {
  389. t.Fatal(err)
  390. }
  391. // make 10 seperate files
  392. for i := 0; i < 10; i++ {
  393. es := []raftpb.Entry{{Index: uint64(i)}}
  394. if err = w.Save(raftpb.HardState{}, es); err != nil {
  395. t.Fatal(err)
  396. }
  397. if err = w.Cut(); err != nil {
  398. t.Fatal(err)
  399. }
  400. }
  401. // release the lock to 5
  402. unlockIndex := uint64(5)
  403. w.ReleaseLockTo(unlockIndex)
  404. // 1,2,3 are avaliable.
  405. w2, err := OpenNotInUse(p, walpb.Snapshot{})
  406. defer w2.Close()
  407. if err != nil {
  408. t.Fatal(err)
  409. }
  410. _, _, ents, err := w2.ReadAll()
  411. if err != nil {
  412. t.Fatalf("err = %v, want nil", err)
  413. }
  414. if g := ents[len(ents)-1].Index; g != unlockIndex-2 {
  415. t.Errorf("last index read = %d, want %d", g, unlockIndex-2)
  416. }
  417. }
  418. func TestSaveEmpty(t *testing.T) {
  419. var buf bytes.Buffer
  420. var est raftpb.HardState
  421. w := WAL{
  422. encoder: newEncoder(&buf, 0),
  423. }
  424. if err := w.saveState(&est); err != nil {
  425. t.Errorf("err = %v, want nil", err)
  426. }
  427. if len(buf.Bytes()) != 0 {
  428. t.Errorf("buf.Bytes = %d, want 0", len(buf.Bytes()))
  429. }
  430. }
  431. func TestReleaseLockTo(t *testing.T) {
  432. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  433. if err != nil {
  434. t.Fatal(err)
  435. }
  436. defer os.RemoveAll(p)
  437. // create WAL
  438. w, err := Create(p, nil)
  439. defer w.Close()
  440. if err != nil {
  441. t.Fatal(err)
  442. }
  443. // make 10 seperate files
  444. for i := 0; i < 10; i++ {
  445. es := []raftpb.Entry{{Index: uint64(i)}}
  446. if err = w.Save(raftpb.HardState{}, es); err != nil {
  447. t.Fatal(err)
  448. }
  449. if err = w.Cut(); err != nil {
  450. t.Fatal(err)
  451. }
  452. }
  453. // release the lock to 5
  454. unlockIndex := uint64(5)
  455. w.ReleaseLockTo(unlockIndex)
  456. // expected remaining are 4,5,6,7,8,9,10
  457. if len(w.locks) != 7 {
  458. t.Errorf("len(w.locks) = %d, want %d", len(w.locks), 7)
  459. }
  460. for i, l := range w.locks {
  461. _, lockIndex, err := parseWalName(path.Base(l.Name()))
  462. if err != nil {
  463. t.Fatal(err)
  464. }
  465. if lockIndex != uint64(i+4) {
  466. t.Errorf("#%d: lockindex = %d, want %d", i, lockIndex, uint64(i+4))
  467. }
  468. }
  469. // release the lock to 15
  470. unlockIndex = uint64(15)
  471. w.ReleaseLockTo(unlockIndex)
  472. // expected remaining is 10
  473. if len(w.locks) != 1 {
  474. t.Errorf("len(w.locks) = %d, want %d", len(w.locks), 1)
  475. }
  476. _, lockIndex, err := parseWalName(path.Base(w.locks[0].Name()))
  477. if err != nil {
  478. t.Fatal(err)
  479. }
  480. if lockIndex != uint64(10) {
  481. t.Errorf("lockindex = %d, want %d", lockIndex, 10)
  482. }
  483. }