repair_test.go 4.1 KB

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