blob: 15afed01744d9ea56c35042788259493bddc4f50 [file] [log] [blame]
khenaidooab1f7bd2019-11-14 14:00:27 -05001// 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
15package wal
16
17import (
18 "io"
19 "os"
20 "path/filepath"
21
22 "go.etcd.io/etcd/pkg/fileutil"
23 "go.etcd.io/etcd/wal/walpb"
24
25 "go.uber.org/zap"
26)
27
28// Repair tries to repair ErrUnexpectedEOF in the
29// last wal file by truncating.
30func Repair(lg *zap.Logger, dirpath string) bool {
31 f, err := openLast(lg, dirpath)
32 if err != nil {
33 return false
34 }
35 defer f.Close()
36
37 if lg != nil {
38 lg.Info("repairing", zap.String("path", f.Name()))
39 } else {
40 plog.Noticef("repairing %v", f.Name())
41 }
42
43 rec := &walpb.Record{}
44 decoder := newDecoder(f)
45 for {
46 lastOffset := decoder.lastOffset()
47 err := decoder.decode(rec)
48 switch err {
49 case nil:
50 // update crc of the decoder when necessary
51 switch rec.Type {
52 case crcType:
53 crc := decoder.crc.Sum32()
54 // current crc of decoder must match the crc of the record.
55 // do no need to match 0 crc, since the decoder is a new one at this case.
56 if crc != 0 && rec.Validate(crc) != nil {
57 return false
58 }
59 decoder.updateCRC(rec.Crc)
60 }
61 continue
62
63 case io.EOF:
64 if lg != nil {
65 lg.Info("repaired", zap.String("path", f.Name()), zap.Error(io.EOF))
66 }
67 return true
68
69 case io.ErrUnexpectedEOF:
70 bf, bferr := os.Create(f.Name() + ".broken")
71 if bferr != nil {
72 if lg != nil {
73 lg.Warn("failed to create backup file", zap.String("path", f.Name()+".broken"), zap.Error(bferr))
74 } else {
75 plog.Errorf("could not repair %v, failed to create backup file", f.Name())
76 }
77 return false
78 }
79 defer bf.Close()
80
81 if _, err = f.Seek(0, io.SeekStart); err != nil {
82 if lg != nil {
83 lg.Warn("failed to read file", zap.String("path", f.Name()), zap.Error(err))
84 } else {
85 plog.Errorf("could not repair %v, failed to read file", f.Name())
86 }
87 return false
88 }
89
90 if _, err = io.Copy(bf, f); err != nil {
91 if lg != nil {
92 lg.Warn("failed to copy", zap.String("from", f.Name()+".broken"), zap.String("to", f.Name()), zap.Error(err))
93 } else {
94 plog.Errorf("could not repair %v, failed to copy file", f.Name())
95 }
96 return false
97 }
98
99 if err = f.Truncate(lastOffset); err != nil {
100 if lg != nil {
101 lg.Warn("failed to truncate", zap.String("path", f.Name()), zap.Error(err))
102 } else {
103 plog.Errorf("could not repair %v, failed to truncate file", f.Name())
104 }
105 return false
106 }
107
108 if err = fileutil.Fsync(f.File); err != nil {
109 if lg != nil {
110 lg.Warn("failed to fsync", zap.String("path", f.Name()), zap.Error(err))
111 } else {
112 plog.Errorf("could not repair %v, failed to sync file", f.Name())
113 }
114 return false
115 }
116
117 if lg != nil {
118 lg.Info("repaired", zap.String("path", f.Name()), zap.Error(io.ErrUnexpectedEOF))
119 }
120 return true
121
122 default:
123 if lg != nil {
124 lg.Warn("failed to repair", zap.String("path", f.Name()), zap.Error(err))
125 } else {
126 plog.Errorf("could not repair error (%v)", err)
127 }
128 return false
129 }
130 }
131}
132
133// openLast opens the last wal file for read and write.
134func openLast(lg *zap.Logger, dirpath string) (*fileutil.LockedFile, error) {
135 names, err := readWALNames(lg, dirpath)
136 if err != nil {
137 return nil, err
138 }
139 last := filepath.Join(dirpath, names[len(names)-1])
140 return fileutil.LockFile(last, os.O_RDWR, fileutil.PrivateFileMode)
141}