/
readers.go
278 lines (252 loc) · 8.16 KB
/
readers.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
// Copyright (c) 2020 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 bootstrapper
import (
"sync"
"time"
"github.com/m3db/m3/src/dbnode/namespace"
"github.com/m3db/m3/src/dbnode/persist/fs"
"github.com/m3db/m3/src/dbnode/runtime"
"github.com/m3db/m3/src/dbnode/storage/bootstrap"
"github.com/m3db/m3/src/dbnode/storage/bootstrap/result"
"github.com/m3db/m3/src/x/clock"
xtime "github.com/m3db/m3/src/x/time"
"github.com/opentracing/opentracing-go"
opentracinglog "github.com/opentracing/opentracing-go/log"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// TimeWindowReaders are grouped by data block.
type TimeWindowReaders struct {
Ranges result.ShardTimeRanges
Readers map[ShardID]ShardReaders
}
// ShardID is the shard #.
type ShardID uint32
// ShardReaders are the fileset readers for a shard.
type ShardReaders struct {
Readers []fs.DataFileSetReader
}
func newTimeWindowReaders(
ranges result.ShardTimeRanges,
readers map[ShardID]ShardReaders,
) TimeWindowReaders {
return TimeWindowReaders{
Ranges: ranges,
Readers: readers,
}
}
// EnqueueReadersOptions supplies options to enqueue readers.
type EnqueueReadersOptions struct {
NsMD namespace.Metadata
RunOpts bootstrap.RunOptions
RuntimeOpts runtime.Options
FsOpts fs.Options
ShardTimeRanges result.ShardTimeRanges
ReaderPool *ReaderPool
ReadersCh chan<- TimeWindowReaders
BlockSize time.Duration
ReadMetadataOnly bool
Logger *zap.Logger
Span opentracing.Span
NowFn clock.NowFn
Cache bootstrap.Cache
}
// EnqueueReaders into a readers channel grouped by data block.
func EnqueueReaders(opts EnqueueReadersOptions) {
// Close the readers ch if and only if all readers are enqueued.
defer close(opts.ReadersCh)
// Normal run, open readers
enqueueReadersGroupedByBlockSize(
opts.NsMD,
opts.ShardTimeRanges,
opts.ReaderPool,
opts.ReadersCh,
opts.BlockSize,
opts.ReadMetadataOnly,
opts.Logger,
opts.Span,
opts.NowFn,
opts.Cache,
)
}
func enqueueReadersGroupedByBlockSize(
ns namespace.Metadata,
shardTimeRanges result.ShardTimeRanges,
readerPool *ReaderPool,
readersCh chan<- TimeWindowReaders,
blockSize time.Duration,
readMetadataOnly bool,
logger *zap.Logger,
span opentracing.Span,
nowFn clock.NowFn,
cache bootstrap.Cache,
) {
// Group them by block size.
groupFn := NewShardTimeRangesTimeWindowGroups
groupedByBlockSize := groupFn(shardTimeRanges, blockSize)
// Now enqueue across all shards by block size.
for _, group := range groupedByBlockSize {
readers := make(map[ShardID]ShardReaders, group.Ranges.Len())
for shard, tr := range group.Ranges.Iter() {
readInfoFilesResults, err := cache.InfoFilesForShard(ns, shard)
if err != nil {
logger.Error("fs bootstrapper unable to read info files for the shard",
zap.Uint32("shard", shard),
zap.Stringer("namespace", ns.ID()),
zap.Error(err),
zap.String("timeRange", tr.String()),
)
continue
}
shardReaders := newShardReaders(ns, readerPool, shard, tr,
readMetadataOnly, logger, span, nowFn, readInfoFilesResults)
readers[ShardID(shard)] = shardReaders
}
readersCh <- newTimeWindowReaders(group.Ranges, readers)
}
}
func newShardReaders(
ns namespace.Metadata,
readerPool *ReaderPool,
shard uint32,
tr xtime.Ranges,
readMetadataOnly bool,
logger *zap.Logger,
span opentracing.Span,
nowFn clock.NowFn,
readInfoFilesResults []fs.ReadInfoFileResult,
) ShardReaders {
logSpan := func(event string) {
span.LogFields(
opentracinglog.String("event", event),
opentracinglog.Uint32("shard", shard),
opentracinglog.String("tr", tr.String()),
)
}
logFields := []zapcore.Field{
zap.Uint32("shard", shard),
zap.String("tr", tr.String()),
}
if len(readInfoFilesResults) == 0 {
// No readers.
return ShardReaders{}
}
start := nowFn()
logger.Debug("enqueue readers open data readers start", logFields...)
logSpan("enqueue_readers_open_data_readers_start")
readers := make([]fs.DataFileSetReader, 0, len(readInfoFilesResults))
for i := 0; i < len(readInfoFilesResults); i++ {
result := readInfoFilesResults[i]
if err := result.Err.Error(); err != nil {
logger.Error("fs bootstrapper unable to read info file",
zap.Uint32("shard", shard),
zap.Stringer("namespace", ns.ID()),
zap.Error(err),
zap.String("timeRange", tr.String()),
zap.String("path", result.Err.Filepath()),
)
// Errors are marked unfulfilled by markRunResultErrorsAndUnfulfilled
// and will be re-attempted by the next bootstrapper.
continue
}
info := result.Info
blockStart := xtime.UnixNano(info.BlockStart)
if !tr.Overlaps(xtime.Range{
Start: blockStart,
End: blockStart.Add(ns.Options().RetentionOptions().BlockSize()),
}) {
// Errors are marked unfulfilled by markRunResultErrorsAndUnfulfilled
// and will be re-attempted by the next bootstrapper.
continue
}
r, err := readerPool.Get()
if err != nil {
logger.Error("unable to get reader from pool")
// Errors are marked unfulfilled by markRunResultErrorsAndUnfulfilled
// and will be re-attempted by the next bootstrapper.
continue
}
openOpts := fs.DataReaderOpenOptions{
Identifier: fs.NewFileSetFileIdentifier(ns.ID(), blockStart, shard, info.VolumeIndex),
StreamingEnabled: readMetadataOnly,
}
if err := r.Open(openOpts); err != nil {
logger.Error("unable to open fileset files",
zap.Uint32("shard", shard),
zap.Time("blockStart", blockStart.ToTime()),
zap.Error(err),
)
readerPool.Put(r)
// Errors are marked unfulfilled by markRunResultErrorsAndUnfulfilled
// and will be re-attempted by the next bootstrapper.
continue
}
readers = append(readers, r)
}
logger.Debug("enqueue readers open data readers done",
append(logFields, zap.Duration("took", nowFn().Sub(start)))...)
logSpan("enqueue_readers_open_data_readers_done")
return ShardReaders{Readers: readers}
}
// ReaderPool is a lean pool that does not allocate
// instances up front and is used per bootstrap call.
type ReaderPool struct {
sync.Mutex
alloc ReaderPoolAllocFn
values []fs.DataFileSetReader
disableReuse bool
}
// ReaderPoolAllocFn allocates a new fileset reader.
type ReaderPoolAllocFn func() (fs.DataFileSetReader, error)
// NewReaderPoolOptions contains reader pool options.
type NewReaderPoolOptions struct {
Alloc ReaderPoolAllocFn
DisableReuse bool
}
// NewReaderPool creates a new share-able fileset reader pool
func NewReaderPool(
opts NewReaderPoolOptions,
) *ReaderPool {
return &ReaderPool{alloc: opts.Alloc, disableReuse: opts.DisableReuse}
}
// Get gets a fileset reader from the pool in synchronized fashion.
func (p *ReaderPool) Get() (fs.DataFileSetReader, error) {
p.Lock()
defer p.Unlock()
if len(p.values) == 0 {
return p.alloc()
}
length := len(p.values)
value := p.values[length-1]
p.values[length-1] = nil
p.values = p.values[:length-1]
return value, nil
}
// Put returns a fileset reader back the the pool in synchronized fashion.
func (p *ReaderPool) Put(r fs.DataFileSetReader) {
if p.disableReuse {
// Useful for tests.
return
}
p.Lock()
defer p.Unlock()
p.values = append(p.values, r)
}