-
Notifications
You must be signed in to change notification settings - Fork 451
/
iterator.go
216 lines (191 loc) · 5.44 KB
/
iterator.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package commitlog
import (
"errors"
"io"
"github.com/m3db/m3/src/dbnode/persist"
"github.com/m3db/m3/src/dbnode/ts"
xtime "github.com/m3db/m3/src/x/time"
"github.com/uber-go/tally"
"go.uber.org/zap"
)
var (
errIndexDoesNotMatch = errors.New("commit log file index does not match filename")
)
type iteratorMetrics struct {
readsErrors tally.Counter
}
type iterator struct {
opts Options
scope tally.Scope
metrics iteratorMetrics
log *zap.Logger
files []persist.CommitLogFile
reader commitLogReader
read iteratorRead
err error
seriesPred SeriesFilterPredicate
setRead bool
closed bool
}
type iteratorRead struct {
series ts.Series
datapoint ts.Datapoint
unit xtime.Unit
annotation []byte
}
// ReadAllPredicate can be passed as the ReadCommitLogPredicate for callers
// that want a convenient way to read all the commitlogs
func ReadAllPredicate() FileFilterPredicate {
return func(f FileFilterInfo) bool { return true }
}
// NewIterator creates a new commit log iterator
func NewIterator(iterOpts IteratorOpts) (iter Iterator, corruptFiles []ErrorWithPath, err error) {
opts := iterOpts.CommitLogOptions
iops := opts.InstrumentOptions()
iops = iops.SetMetricsScope(iops.MetricsScope().SubScope("iterator"))
files, corruptFiles, err := Files(opts)
if err != nil {
return nil, nil, err
}
filteredFiles := filterFiles(files, iterOpts.FileFilterPredicate)
filteredCorruptFiles := filterCorruptFiles(corruptFiles, iterOpts.FileFilterPredicate)
scope := iops.MetricsScope()
return &iterator{
opts: opts,
scope: scope,
metrics: iteratorMetrics{
readsErrors: scope.Counter("reads.errors"),
},
log: iops.Logger(),
files: filteredFiles,
seriesPred: iterOpts.SeriesFilterPredicate,
}, filteredCorruptFiles, nil
}
func (i *iterator) Next() bool {
if i.hasError() || i.closed {
return false
}
if i.reader == nil {
if !i.nextReader() {
return false
}
}
var err error
i.read.series, i.read.datapoint, i.read.unit, i.read.annotation, err = i.reader.Read()
if err == io.EOF {
closeErr := i.closeAndResetReader()
if closeErr != nil {
i.err = closeErr
}
// Try the next reader
return i.Next()
}
if err != nil {
// Try the next reader, this enables restoring with best effort from commit logs
i.metrics.readsErrors.Inc(1)
i.log.Error("commit log reader returned error, iterator moving to next file", zap.Error(err))
i.err = err
closeErr := i.closeAndResetReader()
if closeErr != nil {
i.err = closeErr
}
return i.Next()
}
i.setRead = true
return true
}
func (i *iterator) Current() (ts.Series, ts.Datapoint, xtime.Unit, ts.Annotation) {
read := i.read
if i.hasError() || i.closed || !i.setRead {
read = iteratorRead{}
}
return read.series, read.datapoint, read.unit, read.annotation
}
func (i *iterator) Err() error {
return i.err
}
// TODO: Refactor codebase so that it can handle Close() returning an error
func (i *iterator) Close() {
if i.closed {
return
}
i.closed = true
i.closeAndResetReader()
}
func (i *iterator) hasError() bool {
return i.err != nil
}
func (i *iterator) nextReader() bool {
if len(i.files) == 0 {
return false
}
err := i.closeAndResetReader()
if err != nil {
i.err = err
return false
}
file := i.files[0]
i.files = i.files[1:]
reader := newCommitLogReader(i.opts, i.seriesPred)
index, err := reader.Open(file.FilePath)
if err != nil {
i.err = err
return false
}
if index != file.Index {
i.err = errIndexDoesNotMatch
return false
}
i.reader = reader
return true
}
func filterFiles(files []persist.CommitLogFile, predicate FileFilterPredicate) []persist.CommitLogFile {
filtered := make([]persist.CommitLogFile, 0, len(files))
for _, f := range files {
info := FileFilterInfo{File: f}
if predicate(info) {
filtered = append(filtered, f)
}
}
return filtered
}
func filterCorruptFiles(corruptFiles []ErrorWithPath, predicate FileFilterPredicate) []ErrorWithPath {
filtered := make([]ErrorWithPath, 0, len(corruptFiles))
for _, errWithPath := range corruptFiles {
info := FileFilterInfo{
Err: errWithPath,
IsCorrupt: true,
}
if predicate(info) {
filtered = append(filtered, errWithPath)
}
}
return filtered
}
func (i *iterator) closeAndResetReader() error {
if i.reader == nil {
return nil
}
reader := i.reader
i.reader = nil
return reader.Close()
}