repair.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2015 CoreOS, Inc.
  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. "io"
  17. "os"
  18. "path"
  19. "github.com/coreos/etcd/pkg/fileutil"
  20. "github.com/coreos/etcd/wal/walpb"
  21. )
  22. // Repair tries to repair ErrUnexpectedEOF in the
  23. // last wal file by truncating.
  24. func Repair(dirpath string) bool {
  25. f, err := openLast(dirpath)
  26. if err != nil {
  27. return false
  28. }
  29. defer f.Close()
  30. n := 0
  31. rec := &walpb.Record{}
  32. decoder := newDecoder(f)
  33. for {
  34. err := decoder.decode(rec)
  35. switch err {
  36. case nil:
  37. n += 8 + rec.Size()
  38. // update crc of the decoder when necessary
  39. switch rec.Type {
  40. case crcType:
  41. crc := decoder.crc.Sum32()
  42. // current crc of decoder must match the crc of the record.
  43. // do no need to match 0 crc, since the decoder is a new one at this case.
  44. if crc != 0 && rec.Validate(crc) != nil {
  45. return false
  46. }
  47. decoder.updateCRC(rec.Crc)
  48. }
  49. continue
  50. case io.EOF:
  51. return true
  52. case ErrZeroTrailer:
  53. plog.Noticef("found zero trailer in %v", f.Name())
  54. fallthrough
  55. case io.ErrUnexpectedEOF:
  56. plog.Noticef("repairing %v", f.Name())
  57. bf, bferr := os.Create(f.Name() + ".broken")
  58. if bferr != nil {
  59. plog.Errorf("could not repair %v, failed to create backup file", f.Name())
  60. return false
  61. }
  62. defer bf.Close()
  63. if _, err = f.Seek(0, os.SEEK_SET); err != nil {
  64. plog.Errorf("could not repair %v, failed to read file", f.Name())
  65. return false
  66. }
  67. if _, err = io.Copy(bf, f); err != nil {
  68. plog.Errorf("could not repair %v, failed to copy file", f.Name())
  69. return false
  70. }
  71. if err = f.Truncate(int64(n)); err != nil {
  72. plog.Errorf("could not repair %v, failed to truncate file", f.Name())
  73. return false
  74. }
  75. if err = f.Sync(); err != nil {
  76. plog.Errorf("could not repair %v, failed to sync file", f.Name())
  77. return false
  78. }
  79. return true
  80. default:
  81. plog.Errorf("could not repair error (%v)", err)
  82. return false
  83. }
  84. }
  85. }
  86. // openLast opens the last wal file for read and write.
  87. func openLast(dirpath string) (*os.File, error) {
  88. names, err := fileutil.ReadDir(dirpath)
  89. if err != nil {
  90. return nil, err
  91. }
  92. names = checkWalNames(names)
  93. if len(names) == 0 {
  94. return nil, ErrFileNotFound
  95. }
  96. last := path.Join(dirpath, names[len(names)-1])
  97. return os.OpenFile(last, os.O_RDWR, 0)
  98. }