repair_test.go 5.3 KB

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