repair.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 io.ErrUnexpectedEOF:
  53. plog.Noticef("repairing %v", f.Name())
  54. bf, bferr := os.Create(f.Name() + ".broken")
  55. if bferr != nil {
  56. plog.Errorf("could not repair %v, failed to create backup file", f.Name())
  57. return false
  58. }
  59. defer bf.Close()
  60. if _, err = f.Seek(0, os.SEEK_SET); err != nil {
  61. plog.Errorf("could not repair %v, failed to read file", f.Name())
  62. return false
  63. }
  64. if _, err = io.Copy(bf, f); err != nil {
  65. plog.Errorf("could not repair %v, failed to copy file", f.Name())
  66. return false
  67. }
  68. if err = f.Truncate(int64(n)); err != nil {
  69. plog.Errorf("could not repair %v, failed to truncate file", f.Name())
  70. return false
  71. }
  72. if err = fileutil.Fsync(f); err != nil {
  73. plog.Errorf("could not repair %v, failed to sync file", f.Name())
  74. return false
  75. }
  76. return true
  77. default:
  78. plog.Errorf("could not repair error (%v)", err)
  79. return false
  80. }
  81. }
  82. }
  83. // openLast opens the last wal file for read and write.
  84. func openLast(dirpath string) (*os.File, error) {
  85. names, err := fileutil.ReadDir(dirpath)
  86. if err != nil {
  87. return nil, err
  88. }
  89. names = checkWalNames(names)
  90. if len(names) == 0 {
  91. return nil, ErrFileNotFound
  92. }
  93. last := path.Join(dirpath, names[len(names)-1])
  94. return os.OpenFile(last, os.O_RDWR, 0)
  95. }