repair_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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. "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("fix = %t, want %t", ok, true)
  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. if terr := f.Truncate(offset); terr != nil {
  138. return terr
  139. }
  140. return nil
  141. }
  142. testRepair(t, makeEnts(50), corruptf, 40)
  143. }
  144. // TestRepairWriteTearMiddle repairs the WAL when there is write tearing
  145. // in the middle of a record.
  146. func TestRepairWriteTearMiddle(t *testing.T) {
  147. corruptf := func(p string, offset int64) error {
  148. f, err := openLast(zap.NewExample(), p)
  149. if err != nil {
  150. return err
  151. }
  152. defer f.Close()
  153. // corrupt middle of 2nd record
  154. _, werr := f.WriteAt(make([]byte, 512), 4096+512)
  155. return werr
  156. }
  157. ents := makeEnts(5)
  158. // 4096 bytes of data so a middle sector is easy to corrupt
  159. dat := make([]byte, 4096)
  160. for i := range dat {
  161. dat[i] = byte(i)
  162. }
  163. for i := range ents {
  164. ents[i][0].Data = dat
  165. }
  166. testRepair(t, ents, corruptf, 1)
  167. }