repair.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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/filepath"
  19. "github.com/coreos/etcd/pkg/fileutil"
  20. "github.com/coreos/etcd/wal/walpb"
  21. "go.uber.org/zap"
  22. )
  23. // Repair tries to repair ErrUnexpectedEOF in the
  24. // last wal file by truncating.
  25. func Repair(lg *zap.Logger, dirpath string) bool {
  26. f, err := openLast(lg, dirpath)
  27. if err != nil {
  28. return false
  29. }
  30. defer f.Close()
  31. rec := &walpb.Record{}
  32. decoder := newDecoder(f)
  33. for {
  34. lastOffset := decoder.lastOffset()
  35. err := decoder.decode(rec)
  36. switch err {
  37. case nil:
  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. if lg != nil {
  54. lg.Info("repairing", zap.String("path", f.Name()))
  55. } else {
  56. plog.Noticef("repairing %v", f.Name())
  57. }
  58. bf, bferr := os.Create(f.Name() + ".broken")
  59. if bferr != nil {
  60. if lg != nil {
  61. lg.Warn("failed to create backup file", zap.String("path", f.Name()+".broken"))
  62. } else {
  63. plog.Errorf("could not repair %v, failed to create backup file", f.Name())
  64. }
  65. return false
  66. }
  67. defer bf.Close()
  68. if _, err = f.Seek(0, io.SeekStart); err != nil {
  69. if lg != nil {
  70. lg.Warn("failed to read file", zap.String("path", f.Name()))
  71. } else {
  72. plog.Errorf("could not repair %v, failed to read file", f.Name())
  73. }
  74. return false
  75. }
  76. if _, err = io.Copy(bf, f); err != nil {
  77. if lg != nil {
  78. lg.Warn("failed to copy", zap.String("from", f.Name()+".broken"), zap.String("to", f.Name()))
  79. } else {
  80. plog.Errorf("could not repair %v, failed to copy file", f.Name())
  81. }
  82. return false
  83. }
  84. if err = f.Truncate(lastOffset); err != nil {
  85. if lg != nil {
  86. lg.Warn("failed to truncate", zap.String("path", f.Name()))
  87. } else {
  88. plog.Errorf("could not repair %v, failed to truncate file", f.Name())
  89. }
  90. return false
  91. }
  92. if err = fileutil.Fsync(f.File); err != nil {
  93. if lg != nil {
  94. lg.Warn("failed to fsync", zap.String("path", f.Name()))
  95. } else {
  96. plog.Errorf("could not repair %v, failed to sync file", f.Name())
  97. }
  98. return false
  99. }
  100. return true
  101. default:
  102. if lg != nil {
  103. lg.Warn("failed to repair", zap.Error(err))
  104. } else {
  105. plog.Errorf("could not repair error (%v)", err)
  106. }
  107. return false
  108. }
  109. }
  110. }
  111. // openLast opens the last wal file for read and write.
  112. func openLast(lg *zap.Logger, dirpath string) (*fileutil.LockedFile, error) {
  113. names, err := readWalNames(lg, dirpath)
  114. if err != nil {
  115. return nil, err
  116. }
  117. last := filepath.Join(dirpath, names[len(names)-1])
  118. return fileutil.LockFile(last, os.O_RDWR, fileutil.PrivateFileMode)
  119. }