wal_test.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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 wal
  15. import (
  16. "bytes"
  17. "io"
  18. "io/ioutil"
  19. "os"
  20. "path"
  21. "reflect"
  22. "testing"
  23. "github.com/coreos/etcd/pkg/fileutil"
  24. "github.com/coreos/etcd/pkg/pbutil"
  25. "github.com/coreos/etcd/raft/raftpb"
  26. "github.com/coreos/etcd/wal/walpb"
  27. )
  28. func TestNew(t *testing.T) {
  29. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  30. if err != nil {
  31. t.Fatal(err)
  32. }
  33. defer os.RemoveAll(p)
  34. w, err := Create(p, []byte("somedata"))
  35. if err != nil {
  36. t.Fatalf("err = %v, want nil", err)
  37. }
  38. if g := path.Base(w.tail().Name()); g != walName(0, 0) {
  39. t.Errorf("name = %+v, want %+v", g, walName(0, 0))
  40. }
  41. defer w.Close()
  42. // file is preallocated to segment size; only read data written by wal
  43. off, err := w.tail().Seek(0, os.SEEK_CUR)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. gd := make([]byte, off)
  48. f, err := os.Open(path.Join(p, path.Base(w.tail().Name())))
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. defer f.Close()
  53. if _, err = io.ReadFull(f, gd); err != nil {
  54. t.Fatalf("err = %v, want nil", err)
  55. }
  56. var wb bytes.Buffer
  57. e := newEncoder(&wb, 0, 0)
  58. err = e.encode(&walpb.Record{Type: crcType, Crc: 0})
  59. if err != nil {
  60. t.Fatalf("err = %v, want nil", err)
  61. }
  62. err = e.encode(&walpb.Record{Type: metadataType, Data: []byte("somedata")})
  63. if err != nil {
  64. t.Fatalf("err = %v, want nil", err)
  65. }
  66. r := &walpb.Record{
  67. Type: snapshotType,
  68. Data: pbutil.MustMarshal(&walpb.Snapshot{}),
  69. }
  70. if err = e.encode(r); err != nil {
  71. t.Fatalf("err = %v, want nil", err)
  72. }
  73. e.flush()
  74. if !bytes.Equal(gd, wb.Bytes()) {
  75. t.Errorf("data = %v, want %v", gd, wb.Bytes())
  76. }
  77. }
  78. func TestNewForInitedDir(t *testing.T) {
  79. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  80. if err != nil {
  81. t.Fatal(err)
  82. }
  83. defer os.RemoveAll(p)
  84. os.Create(path.Join(p, walName(0, 0)))
  85. if _, err = Create(p, nil); err == nil || err != os.ErrExist {
  86. t.Errorf("err = %v, want %v", err, os.ErrExist)
  87. }
  88. }
  89. func TestOpenAtIndex(t *testing.T) {
  90. dir, err := ioutil.TempDir(os.TempDir(), "waltest")
  91. if err != nil {
  92. t.Fatal(err)
  93. }
  94. defer os.RemoveAll(dir)
  95. f, err := os.Create(path.Join(dir, walName(0, 0)))
  96. if err != nil {
  97. t.Fatal(err)
  98. }
  99. f.Close()
  100. w, err := Open(dir, walpb.Snapshot{})
  101. if err != nil {
  102. t.Fatalf("err = %v, want nil", err)
  103. }
  104. if g := path.Base(w.tail().Name()); g != walName(0, 0) {
  105. t.Errorf("name = %+v, want %+v", g, walName(0, 0))
  106. }
  107. if w.seq() != 0 {
  108. t.Errorf("seq = %d, want %d", w.seq(), 0)
  109. }
  110. w.Close()
  111. wname := walName(2, 10)
  112. f, err = os.Create(path.Join(dir, wname))
  113. if err != nil {
  114. t.Fatal(err)
  115. }
  116. f.Close()
  117. w, err = Open(dir, walpb.Snapshot{Index: 5})
  118. if err != nil {
  119. t.Fatalf("err = %v, want nil", err)
  120. }
  121. if g := path.Base(w.tail().Name()); g != wname {
  122. t.Errorf("name = %+v, want %+v", g, wname)
  123. }
  124. if w.seq() != 2 {
  125. t.Errorf("seq = %d, want %d", w.seq(), 2)
  126. }
  127. w.Close()
  128. emptydir, err := ioutil.TempDir(os.TempDir(), "waltestempty")
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. defer os.RemoveAll(emptydir)
  133. if _, err = Open(emptydir, walpb.Snapshot{}); err != ErrFileNotFound {
  134. t.Errorf("err = %v, want %v", err, ErrFileNotFound)
  135. }
  136. }
  137. // TODO: split it into smaller tests for better readability
  138. func TestCut(t *testing.T) {
  139. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  140. if err != nil {
  141. t.Fatal(err)
  142. }
  143. defer os.RemoveAll(p)
  144. w, err := Create(p, nil)
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. defer w.Close()
  149. state := raftpb.HardState{Term: 1}
  150. if err = w.Save(state, nil); err != nil {
  151. t.Fatal(err)
  152. }
  153. if err = w.cut(); err != nil {
  154. t.Fatal(err)
  155. }
  156. wname := walName(1, 1)
  157. if g := path.Base(w.tail().Name()); g != wname {
  158. t.Errorf("name = %s, want %s", g, wname)
  159. }
  160. es := []raftpb.Entry{{Index: 1, Term: 1, Data: []byte{1}}}
  161. if err = w.Save(raftpb.HardState{}, es); err != nil {
  162. t.Fatal(err)
  163. }
  164. if err = w.cut(); err != nil {
  165. t.Fatal(err)
  166. }
  167. snap := walpb.Snapshot{Index: 2, Term: 1}
  168. if err = w.SaveSnapshot(snap); err != nil {
  169. t.Fatal(err)
  170. }
  171. wname = walName(2, 2)
  172. if g := path.Base(w.tail().Name()); g != wname {
  173. t.Errorf("name = %s, want %s", g, wname)
  174. }
  175. // check the state in the last WAL
  176. // We do check before closing the WAL to ensure that Cut syncs the data
  177. // into the disk.
  178. f, err := os.Open(path.Join(p, wname))
  179. if err != nil {
  180. t.Fatal(err)
  181. }
  182. defer f.Close()
  183. nw := &WAL{
  184. decoder: newDecoder(f),
  185. start: snap,
  186. }
  187. _, gst, _, err := nw.ReadAll()
  188. if err != nil {
  189. t.Fatal(err)
  190. }
  191. if !reflect.DeepEqual(gst, state) {
  192. t.Errorf("state = %+v, want %+v", gst, state)
  193. }
  194. }
  195. func TestRecover(t *testing.T) {
  196. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  197. if err != nil {
  198. t.Fatal(err)
  199. }
  200. defer os.RemoveAll(p)
  201. w, err := Create(p, []byte("metadata"))
  202. if err != nil {
  203. t.Fatal(err)
  204. }
  205. if err = w.SaveSnapshot(walpb.Snapshot{}); err != nil {
  206. t.Fatal(err)
  207. }
  208. ents := []raftpb.Entry{{Index: 1, Term: 1, Data: []byte{1}}, {Index: 2, Term: 2, Data: []byte{2}}}
  209. if err = w.Save(raftpb.HardState{}, ents); err != nil {
  210. t.Fatal(err)
  211. }
  212. sts := []raftpb.HardState{{Term: 1, Vote: 1, Commit: 1}, {Term: 2, Vote: 2, Commit: 2}}
  213. for _, s := range sts {
  214. if err = w.Save(s, nil); err != nil {
  215. t.Fatal(err)
  216. }
  217. }
  218. w.Close()
  219. if w, err = Open(p, walpb.Snapshot{}); err != nil {
  220. t.Fatal(err)
  221. }
  222. metadata, state, entries, err := w.ReadAll()
  223. if err != nil {
  224. t.Fatal(err)
  225. }
  226. if !bytes.Equal(metadata, []byte("metadata")) {
  227. t.Errorf("metadata = %s, want %s", metadata, "metadata")
  228. }
  229. if !reflect.DeepEqual(entries, ents) {
  230. t.Errorf("ents = %+v, want %+v", entries, ents)
  231. }
  232. // only the latest state is recorded
  233. s := sts[len(sts)-1]
  234. if !reflect.DeepEqual(state, s) {
  235. t.Errorf("state = %+v, want %+v", state, s)
  236. }
  237. w.Close()
  238. }
  239. func TestSearchIndex(t *testing.T) {
  240. tests := []struct {
  241. names []string
  242. index uint64
  243. widx int
  244. wok bool
  245. }{
  246. {
  247. []string{
  248. "0000000000000000-0000000000000000.wal",
  249. "0000000000000001-0000000000001000.wal",
  250. "0000000000000002-0000000000002000.wal",
  251. },
  252. 0x1000, 1, true,
  253. },
  254. {
  255. []string{
  256. "0000000000000001-0000000000004000.wal",
  257. "0000000000000002-0000000000003000.wal",
  258. "0000000000000003-0000000000005000.wal",
  259. },
  260. 0x4000, 1, true,
  261. },
  262. {
  263. []string{
  264. "0000000000000001-0000000000002000.wal",
  265. "0000000000000002-0000000000003000.wal",
  266. "0000000000000003-0000000000005000.wal",
  267. },
  268. 0x1000, -1, false,
  269. },
  270. }
  271. for i, tt := range tests {
  272. idx, ok := searchIndex(tt.names, tt.index)
  273. if idx != tt.widx {
  274. t.Errorf("#%d: idx = %d, want %d", i, idx, tt.widx)
  275. }
  276. if ok != tt.wok {
  277. t.Errorf("#%d: ok = %v, want %v", i, ok, tt.wok)
  278. }
  279. }
  280. }
  281. func TestScanWalName(t *testing.T) {
  282. tests := []struct {
  283. str string
  284. wseq, windex uint64
  285. wok bool
  286. }{
  287. {"0000000000000000-0000000000000000.wal", 0, 0, true},
  288. {"0000000000000000.wal", 0, 0, false},
  289. {"0000000000000000-0000000000000000.snap", 0, 0, false},
  290. }
  291. for i, tt := range tests {
  292. s, index, err := parseWalName(tt.str)
  293. if g := err == nil; g != tt.wok {
  294. t.Errorf("#%d: ok = %v, want %v", i, g, tt.wok)
  295. }
  296. if s != tt.wseq {
  297. t.Errorf("#%d: seq = %d, want %d", i, s, tt.wseq)
  298. }
  299. if index != tt.windex {
  300. t.Errorf("#%d: index = %d, want %d", i, index, tt.windex)
  301. }
  302. }
  303. }
  304. func TestRecoverAfterCut(t *testing.T) {
  305. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  306. if err != nil {
  307. t.Fatal(err)
  308. }
  309. defer os.RemoveAll(p)
  310. md, err := Create(p, []byte("metadata"))
  311. if err != nil {
  312. t.Fatal(err)
  313. }
  314. for i := 0; i < 10; i++ {
  315. if err = md.SaveSnapshot(walpb.Snapshot{Index: uint64(i)}); err != nil {
  316. t.Fatal(err)
  317. }
  318. es := []raftpb.Entry{{Index: uint64(i)}}
  319. if err = md.Save(raftpb.HardState{}, es); err != nil {
  320. t.Fatal(err)
  321. }
  322. if err = md.cut(); err != nil {
  323. t.Fatal(err)
  324. }
  325. }
  326. md.Close()
  327. if err := os.Remove(path.Join(p, walName(4, 4))); err != nil {
  328. t.Fatal(err)
  329. }
  330. for i := 0; i < 10; i++ {
  331. w, err := Open(p, walpb.Snapshot{Index: uint64(i)})
  332. if err != nil {
  333. if i <= 4 {
  334. if err != ErrFileNotFound {
  335. t.Errorf("#%d: err = %v, want %v", i, err, ErrFileNotFound)
  336. }
  337. } else {
  338. t.Errorf("#%d: err = %v, want nil", i, err)
  339. }
  340. continue
  341. }
  342. metadata, _, entries, err := w.ReadAll()
  343. if err != nil {
  344. t.Errorf("#%d: err = %v, want nil", i, err)
  345. continue
  346. }
  347. if !bytes.Equal(metadata, []byte("metadata")) {
  348. t.Errorf("#%d: metadata = %s, want %s", i, metadata, "metadata")
  349. }
  350. for j, e := range entries {
  351. if e.Index != uint64(j+i+1) {
  352. t.Errorf("#%d: ents[%d].Index = %+v, want %+v", i, j, e.Index, j+i+1)
  353. }
  354. }
  355. w.Close()
  356. }
  357. }
  358. func TestOpenAtUncommittedIndex(t *testing.T) {
  359. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  360. if err != nil {
  361. t.Fatal(err)
  362. }
  363. defer os.RemoveAll(p)
  364. w, err := Create(p, nil)
  365. if err != nil {
  366. t.Fatal(err)
  367. }
  368. if err = w.SaveSnapshot(walpb.Snapshot{}); err != nil {
  369. t.Fatal(err)
  370. }
  371. if err = w.Save(raftpb.HardState{}, []raftpb.Entry{{Index: 0}}); err != nil {
  372. t.Fatal(err)
  373. }
  374. w.Close()
  375. w, err = Open(p, walpb.Snapshot{})
  376. if err != nil {
  377. t.Fatal(err)
  378. }
  379. // commit up to index 0, try to read index 1
  380. if _, _, _, err = w.ReadAll(); err != nil {
  381. t.Errorf("err = %v, want nil", err)
  382. }
  383. w.Close()
  384. }
  385. // TestOpenForRead tests that OpenForRead can load all files.
  386. // The tests creates WAL directory, and cut out multiple WAL files. Then
  387. // it releases the lock of part of data, and excepts that OpenForRead
  388. // can read out all files even if some are locked for write.
  389. func TestOpenForRead(t *testing.T) {
  390. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  391. if err != nil {
  392. t.Fatal(err)
  393. }
  394. defer os.RemoveAll(p)
  395. // create WAL
  396. w, err := Create(p, nil)
  397. if err != nil {
  398. t.Fatal(err)
  399. }
  400. defer w.Close()
  401. // make 10 separate files
  402. for i := 0; i < 10; i++ {
  403. es := []raftpb.Entry{{Index: uint64(i)}}
  404. if err = w.Save(raftpb.HardState{}, es); err != nil {
  405. t.Fatal(err)
  406. }
  407. if err = w.cut(); err != nil {
  408. t.Fatal(err)
  409. }
  410. }
  411. // release the lock to 5
  412. unlockIndex := uint64(5)
  413. w.ReleaseLockTo(unlockIndex)
  414. // All are available for read
  415. w2, err := OpenForRead(p, walpb.Snapshot{})
  416. if err != nil {
  417. t.Fatal(err)
  418. }
  419. defer w2.Close()
  420. _, _, ents, err := w2.ReadAll()
  421. if err != nil {
  422. t.Fatalf("err = %v, want nil", err)
  423. }
  424. if g := ents[len(ents)-1].Index; g != 9 {
  425. t.Errorf("last index read = %d, want %d", g, 9)
  426. }
  427. }
  428. func TestSaveEmpty(t *testing.T) {
  429. var buf bytes.Buffer
  430. var est raftpb.HardState
  431. w := WAL{
  432. encoder: newEncoder(&buf, 0, 0),
  433. }
  434. if err := w.saveState(&est); err != nil {
  435. t.Errorf("err = %v, want nil", err)
  436. }
  437. if len(buf.Bytes()) != 0 {
  438. t.Errorf("buf.Bytes = %d, want 0", len(buf.Bytes()))
  439. }
  440. }
  441. func TestReleaseLockTo(t *testing.T) {
  442. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  443. if err != nil {
  444. t.Fatal(err)
  445. }
  446. defer os.RemoveAll(p)
  447. // create WAL
  448. w, err := Create(p, nil)
  449. defer w.Close()
  450. if err != nil {
  451. t.Fatal(err)
  452. }
  453. // make 10 separate files
  454. for i := 0; i < 10; i++ {
  455. es := []raftpb.Entry{{Index: uint64(i)}}
  456. if err = w.Save(raftpb.HardState{}, es); err != nil {
  457. t.Fatal(err)
  458. }
  459. if err = w.cut(); err != nil {
  460. t.Fatal(err)
  461. }
  462. }
  463. // release the lock to 5
  464. unlockIndex := uint64(5)
  465. w.ReleaseLockTo(unlockIndex)
  466. // expected remaining are 4,5,6,7,8,9,10
  467. if len(w.locks) != 7 {
  468. t.Errorf("len(w.locks) = %d, want %d", len(w.locks), 7)
  469. }
  470. for i, l := range w.locks {
  471. var lockIndex uint64
  472. _, lockIndex, err = parseWalName(path.Base(l.Name()))
  473. if err != nil {
  474. t.Fatal(err)
  475. }
  476. if lockIndex != uint64(i+4) {
  477. t.Errorf("#%d: lockindex = %d, want %d", i, lockIndex, uint64(i+4))
  478. }
  479. }
  480. // release the lock to 15
  481. unlockIndex = uint64(15)
  482. w.ReleaseLockTo(unlockIndex)
  483. // expected remaining is 10
  484. if len(w.locks) != 1 {
  485. t.Errorf("len(w.locks) = %d, want %d", len(w.locks), 1)
  486. }
  487. _, lockIndex, err := parseWalName(path.Base(w.locks[0].Name()))
  488. if err != nil {
  489. t.Fatal(err)
  490. }
  491. if lockIndex != uint64(10) {
  492. t.Errorf("lockindex = %d, want %d", lockIndex, 10)
  493. }
  494. }
  495. // TestTailWriteNoSlackSpace ensures that tail writes append if there's no preallocated space.
  496. func TestTailWriteNoSlackSpace(t *testing.T) {
  497. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  498. if err != nil {
  499. t.Fatal(err)
  500. }
  501. defer os.RemoveAll(p)
  502. // create initial WAL
  503. w, err := Create(p, []byte("metadata"))
  504. if err != nil {
  505. t.Fatal(err)
  506. }
  507. // write some entries
  508. for i := 1; i <= 5; i++ {
  509. es := []raftpb.Entry{{Index: uint64(i), Term: 1, Data: []byte{byte(i)}}}
  510. if err = w.Save(raftpb.HardState{Term: 1}, es); err != nil {
  511. t.Fatal(err)
  512. }
  513. }
  514. // get rid of slack space by truncating file
  515. off, serr := w.tail().Seek(0, os.SEEK_CUR)
  516. if serr != nil {
  517. t.Fatal(serr)
  518. }
  519. if terr := w.tail().Truncate(off); terr != nil {
  520. t.Fatal(terr)
  521. }
  522. w.Close()
  523. // open, write more
  524. w, err = Open(p, walpb.Snapshot{})
  525. if err != nil {
  526. t.Fatal(err)
  527. }
  528. _, _, ents, rerr := w.ReadAll()
  529. if rerr != nil {
  530. t.Fatal(rerr)
  531. }
  532. if len(ents) != 5 {
  533. t.Fatalf("got entries %+v, expected 5 entries", ents)
  534. }
  535. // write more entries
  536. for i := 6; i <= 10; i++ {
  537. es := []raftpb.Entry{{Index: uint64(i), Term: 1, Data: []byte{byte(i)}}}
  538. if err = w.Save(raftpb.HardState{Term: 1}, es); err != nil {
  539. t.Fatal(err)
  540. }
  541. }
  542. w.Close()
  543. // confirm all writes
  544. w, err = Open(p, walpb.Snapshot{})
  545. if err != nil {
  546. t.Fatal(err)
  547. }
  548. _, _, ents, rerr = w.ReadAll()
  549. if rerr != nil {
  550. t.Fatal(rerr)
  551. }
  552. if len(ents) != 10 {
  553. t.Fatalf("got entries %+v, expected 10 entries", ents)
  554. }
  555. w.Close()
  556. }
  557. // TestRestartCreateWal ensures that an interrupted WAL initialization is clobbered on restart
  558. func TestRestartCreateWal(t *testing.T) {
  559. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  560. if err != nil {
  561. t.Fatal(err)
  562. }
  563. defer os.RemoveAll(p)
  564. // make temporary directory so it looks like initialization is interrupted
  565. tmpdir := path.Clean(p) + ".tmp"
  566. if err = os.Mkdir(tmpdir, fileutil.PrivateDirMode); err != nil {
  567. t.Fatal(err)
  568. }
  569. if _, err = os.OpenFile(path.Join(tmpdir, "test"), os.O_WRONLY|os.O_CREATE, fileutil.PrivateFileMode); err != nil {
  570. t.Fatal(err)
  571. }
  572. w, werr := Create(p, []byte("abc"))
  573. if werr != nil {
  574. t.Fatal(werr)
  575. }
  576. w.Close()
  577. if Exist(tmpdir) {
  578. t.Fatalf("got %q exists, expected it to not exist", tmpdir)
  579. }
  580. if w, err = OpenForRead(p, walpb.Snapshot{}); err != nil {
  581. t.Fatal(err)
  582. }
  583. defer w.Close()
  584. if meta, _, _, rerr := w.ReadAll(); rerr != nil || string(meta) != "abc" {
  585. t.Fatalf("got error %v and meta %q, expected nil and %q", rerr, meta, "abc")
  586. }
  587. }
  588. // TestOpenOnTornWrite ensures that entries past the torn write are truncated.
  589. func TestOpenOnTornWrite(t *testing.T) {
  590. maxEntries := 40
  591. clobberIdx := 20
  592. overwriteEntries := 5
  593. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  594. if err != nil {
  595. t.Fatal(err)
  596. }
  597. defer os.RemoveAll(p)
  598. w, err := Create(p, nil)
  599. defer w.Close()
  600. if err != nil {
  601. t.Fatal(err)
  602. }
  603. // get offset of end of each saved entry
  604. offsets := make([]int64, maxEntries)
  605. for i := range offsets {
  606. es := []raftpb.Entry{{Index: uint64(i)}}
  607. if err = w.Save(raftpb.HardState{}, es); err != nil {
  608. t.Fatal(err)
  609. }
  610. if offsets[i], err = w.tail().Seek(0, os.SEEK_CUR); err != nil {
  611. t.Fatal(err)
  612. }
  613. }
  614. fn := path.Join(p, path.Base(w.tail().Name()))
  615. w.Close()
  616. // clobber some entry with 0's to simulate a torn write
  617. f, ferr := os.OpenFile(fn, os.O_WRONLY, fileutil.PrivateFileMode)
  618. if ferr != nil {
  619. t.Fatal(ferr)
  620. }
  621. defer f.Close()
  622. _, err = f.Seek(offsets[clobberIdx], os.SEEK_SET)
  623. if err != nil {
  624. t.Fatal(err)
  625. }
  626. zeros := make([]byte, offsets[clobberIdx+1]-offsets[clobberIdx])
  627. _, err = f.Write(zeros)
  628. if err != nil {
  629. t.Fatal(err)
  630. }
  631. f.Close()
  632. w, err = Open(p, walpb.Snapshot{})
  633. if err != nil {
  634. t.Fatal(err)
  635. }
  636. // seek up to clobbered entry
  637. _, _, _, err = w.ReadAll()
  638. if err != nil {
  639. t.Fatal(err)
  640. }
  641. // write a few entries past the clobbered entry
  642. for i := 0; i < overwriteEntries; i++ {
  643. // Index is different from old, truncated entries
  644. es := []raftpb.Entry{{Index: uint64(i + clobberIdx), Data: []byte("new")}}
  645. if err = w.Save(raftpb.HardState{}, es); err != nil {
  646. t.Fatal(err)
  647. }
  648. }
  649. w.Close()
  650. // read back the entries, confirm number of entries matches expectation
  651. w, err = OpenForRead(p, walpb.Snapshot{})
  652. if err != nil {
  653. t.Fatal(err)
  654. }
  655. _, _, ents, rerr := w.ReadAll()
  656. if rerr != nil {
  657. // CRC error? the old entries were likely never truncated away
  658. t.Fatal(rerr)
  659. }
  660. wEntries := (clobberIdx - 1) + overwriteEntries
  661. if len(ents) != wEntries {
  662. t.Fatalf("expected len(ents) = %d, got %d", wEntries, len(ents))
  663. }
  664. }