repair_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. "fmt"
  17. "io"
  18. "io/ioutil"
  19. "os"
  20. "testing"
  21. "github.com/coreos/etcd/raft/raftpb"
  22. "github.com/coreos/etcd/wal/walpb"
  23. )
  24. type corruptFunc func(string, int64) error
  25. // TestRepairTruncate ensures a truncated file can be repaired
  26. func TestRepairTruncate(t *testing.T) {
  27. corruptf := func(p string, offset int64) error {
  28. f, err := openLast(p)
  29. if err != nil {
  30. return err
  31. }
  32. defer f.Close()
  33. return f.Truncate(offset - 4)
  34. }
  35. testRepair(t, makeEnts(10), corruptf, 9)
  36. }
  37. func testRepair(t *testing.T, ents [][]raftpb.Entry, corrupt corruptFunc, expectedEnts int) {
  38. p, err := ioutil.TempDir(os.TempDir(), "waltest")
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. defer os.RemoveAll(p)
  43. // create WAL
  44. w, err := Create(p, nil)
  45. defer w.Close()
  46. if err != nil {
  47. t.Fatal(err)
  48. }
  49. for _, es := range ents {
  50. if err = w.Save(raftpb.HardState{}, es); err != nil {
  51. t.Fatal(err)
  52. }
  53. }
  54. offset, err := w.tail().Seek(0, os.SEEK_CUR)
  55. if err != nil {
  56. t.Fatal(err)
  57. }
  58. w.Close()
  59. err = corrupt(p, offset)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. // verify we broke the wal
  64. w, err = Open(p, walpb.Snapshot{})
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. _, _, _, err = w.ReadAll()
  69. if err != io.ErrUnexpectedEOF {
  70. t.Fatalf("err = %v, want error %v", err, io.ErrUnexpectedEOF)
  71. }
  72. w.Close()
  73. // repair the wal
  74. if ok := Repair(p); !ok {
  75. t.Fatalf("fix = %t, want %t", ok, true)
  76. }
  77. // read it back
  78. w, err = Open(p, walpb.Snapshot{})
  79. if err != nil {
  80. t.Fatal(err)
  81. }
  82. _, _, walEnts, err := w.ReadAll()
  83. if err != nil {
  84. t.Fatal(err)
  85. }
  86. if len(walEnts) != expectedEnts {
  87. t.Fatalf("len(ents) = %d, want %d", len(walEnts), expectedEnts)
  88. }
  89. // write some more entries to repaired log
  90. for i := 1; i <= 10; i++ {
  91. es := []raftpb.Entry{{Index: uint64(expectedEnts + i)}}
  92. if err = w.Save(raftpb.HardState{}, es); err != nil {
  93. t.Fatal(err)
  94. }
  95. }
  96. w.Close()
  97. // read back entries following repair, ensure it's all there
  98. w, err = Open(p, walpb.Snapshot{})
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. _, _, walEnts, err = w.ReadAll()
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. if len(walEnts) != expectedEnts+10 {
  107. t.Fatalf("len(ents) = %d, want %d", len(walEnts), expectedEnts+10)
  108. }
  109. }
  110. func makeEnts(ents int) (ret [][]raftpb.Entry) {
  111. for i := 1; i <= ents; i++ {
  112. ret = append(ret, []raftpb.Entry{{Index: uint64(i)}})
  113. }
  114. return ret
  115. }
  116. // TestRepairWriteTearLast repairs the WAL in case the last record is a torn write
  117. // that straddled two sectors.
  118. func TestRepairWriteTearLast(t *testing.T) {
  119. corruptf := func(p string, offset int64) error {
  120. f, err := openLast(p)
  121. if err != nil {
  122. return err
  123. }
  124. defer f.Close()
  125. // 512 bytes perfectly aligns the last record, so use 1024
  126. if offset < 1024 {
  127. return fmt.Errorf("got offset %d, expected >1024", offset)
  128. }
  129. if terr := f.Truncate(1024); terr != nil {
  130. return terr
  131. }
  132. if terr := f.Truncate(offset); terr != nil {
  133. return terr
  134. }
  135. return nil
  136. }
  137. testRepair(t, makeEnts(50), corruptf, 40)
  138. }
  139. // TestRepairWriteTearMiddle repairs the WAL when there is write tearing
  140. // in the middle of a record.
  141. func TestRepairWriteTearMiddle(t *testing.T) {
  142. corruptf := func(p string, offset int64) error {
  143. f, err := openLast(p)
  144. if err != nil {
  145. return err
  146. }
  147. defer f.Close()
  148. // corrupt middle of 2nd record
  149. _, werr := f.WriteAt(make([]byte, 512), 4096+512)
  150. return werr
  151. }
  152. ents := makeEnts(5)
  153. // 4096 bytes of data so a middle sector is easy to corrupt
  154. dat := make([]byte, 4096)
  155. for i := range dat {
  156. dat[i] = byte(i)
  157. }
  158. for i := range ents {
  159. ents[i][0].Data = dat
  160. }
  161. testRepair(t, ents, corruptf, 1)
  162. }