repair.go 2.7 KB

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