repair.go 2.7 KB

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