repair.go 2.7 KB

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