-
Notifications
You must be signed in to change notification settings - Fork 451
/
types.go
420 lines (356 loc) · 13.8 KB
/
types.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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
// 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 bootstrap
import (
"sync"
"time"
"github.com/m3db/m3/src/dbnode/namespace"
"github.com/m3db/m3/src/dbnode/persist"
"github.com/m3db/m3/src/dbnode/storage/bootstrap/result"
"github.com/m3db/m3/src/dbnode/storage/series"
"github.com/m3db/m3/src/dbnode/topology"
"github.com/m3db/m3/src/x/context"
xerrors "github.com/m3db/m3/src/x/errors"
"github.com/m3db/m3/src/x/ident"
xtime "github.com/m3db/m3/src/x/time"
)
// ProcessProvider constructs a bootstrap process that can execute a
// bootstrap run.
type ProcessProvider interface {
// SetBootstrapper sets the bootstrapper provider to use when running the
// process.
SetBootstrapperProvider(bootstrapper BootstrapperProvider)
// Bootstrapper returns the current bootstrappe provider to use when
// running the process.
BootstrapperProvider() BootstrapperProvider
// Provide constructs a bootstrap process.
Provide() (Process, error)
}
// Process represents the bootstrap process. Note that a bootstrap process can and will
// be reused so it is important to not rely on state stored in the bootstrap itself
// with the mindset that it will always be set to default values from the constructor.
type Process interface {
// Run runs the bootstrap process, returning the bootstrap result and any error encountered.
Run(
ctx context.Context,
start time.Time,
namespaces []ProcessNamespace,
) (NamespaceResults, error)
}
// ProcessNamespace is a namespace to pass to the bootstrap process.
type ProcessNamespace struct {
// Metadata of the namespace being bootstrapped.
Metadata namespace.Metadata
// Shards is the shards to bootstrap for the bootstrap process.
Shards []uint32
// DataAccumulator is the data accumulator for the shards.
DataAccumulator NamespaceDataAccumulator
// Hooks is a set of namespace bootstrap hooks.
Hooks NamespaceHooks
}
// NamespaceHooks is a set of namespace bootstrap hooks.
type NamespaceHooks struct {
opts NamespaceHooksOptions
}
// Hook wraps a runnable callback.
type Hook interface {
Run() error
}
// NamespaceHooksOptions is a set of hooks options.
type NamespaceHooksOptions struct {
BootstrapSourceBegin Hook
BootstrapSourceEnd Hook
}
// NewNamespaceHooks returns a new set of bootstrap hooks.
func NewNamespaceHooks(opts NamespaceHooksOptions) NamespaceHooks {
return NamespaceHooks{opts: opts}
}
// BootstrapSourceBegin is a hook to call when a bootstrap source starts.
func (h NamespaceHooks) BootstrapSourceBegin() error {
if h.opts.BootstrapSourceBegin == nil {
return nil
}
return h.opts.BootstrapSourceBegin.Run()
}
// BootstrapSourceEnd is a hook to call when a bootstrap source ends.
func (h NamespaceHooks) BootstrapSourceEnd() error {
if h.opts.BootstrapSourceEnd == nil {
return nil
}
return h.opts.BootstrapSourceEnd.Run()
}
// Namespaces are a set of namespaces being bootstrapped.
type Namespaces struct {
// Namespaces are the namespaces being bootstrapped.
Namespaces *NamespacesMap
}
// Hooks returns namespaces hooks for the set of namespaces.
func (n Namespaces) Hooks() NamespacesHooks {
return NamespacesHooks{namespaces: n.Namespaces}
}
// NamespacesHooks is a helper to run hooks for a set of namespaces.
type NamespacesHooks struct {
namespaces *NamespacesMap
}
// BootstrapSourceBegin is a hook to call when a bootstrap source starts.
func (h NamespacesHooks) BootstrapSourceBegin() error {
if h.namespaces == nil {
return nil
}
var (
wg sync.WaitGroup
multiErrLock sync.Mutex
multiErr xerrors.MultiError
)
for _, elem := range h.namespaces.Iter() {
ns := elem.Value()
wg.Add(1)
go func() {
defer wg.Done()
// Run bootstrap source end hook.
err := ns.Hooks.BootstrapSourceBegin()
if err == nil {
return
}
multiErrLock.Lock()
defer multiErrLock.Unlock()
multiErr = multiErr.Add(err)
}()
}
wg.Wait()
return multiErr.FinalError()
}
// BootstrapSourceEnd is a hook to call when a bootstrap source starts.
func (h NamespacesHooks) BootstrapSourceEnd() error {
if h.namespaces == nil {
return nil
}
var (
wg sync.WaitGroup
multiErrLock sync.Mutex
multiErr xerrors.MultiError
)
for _, elem := range h.namespaces.Iter() {
ns := elem.Value()
wg.Add(1)
go func() {
defer wg.Done()
// Run bootstrap source end hook.
err := ns.Hooks.BootstrapSourceEnd()
if err == nil {
return
}
multiErrLock.Lock()
defer multiErrLock.Unlock()
multiErr = multiErr.Add(err)
}()
}
wg.Wait()
return multiErr.FinalError()
}
// Namespace is a namespace that is being bootstrapped.
type Namespace struct {
// Metadata of the namespace being bootstrapped.
Metadata namespace.Metadata
// Shards is the shards for the namespace being bootstrapped.
Shards []uint32
// DataAccumulator is the data accumulator for the shards.
DataAccumulator NamespaceDataAccumulator
// Hooks is a set of namespace bootstrap hooks.
Hooks NamespaceHooks
// DataTargetRange is the data target bootstrap range.
DataTargetRange TargetRange
// IndexTargetRange is the index target bootstrap range.
IndexTargetRange TargetRange
// DataRunOptions are the options for the data bootstrap for this
// namespace.
DataRunOptions NamespaceRunOptions
// IndexRunOptions are the options for the index bootstrap for this
// namespace.
IndexRunOptions NamespaceRunOptions
}
// NamespaceRunOptions are the run options for a bootstrap process run.
type NamespaceRunOptions struct {
// ShardTimeRanges are the time ranges for the shards that should be fulfilled
// by the bootstrapper. This changes each bootstrapper pass as time ranges are fulfilled.
ShardTimeRanges result.ShardTimeRanges
// TargetShardTimeRanges are the original target time ranges for shards and does not change
// each bootstrapper pass.
// NB(bodu): This is used by the commit log bootstrapper as it needs to run for the entire original
// target shard time ranges.
TargetShardTimeRanges result.ShardTimeRanges
// RunOptions are the run options for the bootstrap run.
RunOptions RunOptions
}
// NamespaceDataAccumulator is the namespace data accumulator.
// TODO(r): Consider rename this to a better name.
type NamespaceDataAccumulator interface {
// CheckoutSeriesWithoutLock retrieves a series for writing to
// and when the accumulator is closed it will ensure that the
// series is released.
//
// If indexing is not enabled, tags is still required, simply pass
// ident.EmptyTagIterator.
//
// Returns the result, whether the node owns the specified shard, along with
// an error if any. This allows callers to handle unowned shards differently
// than other errors. If owned == false, err should not be nil.
//
// Note: Without lock variant does not perform any locking and callers
// must ensure non-parallel access themselves, this helps avoid
// overhead of the lock for the commit log bootstrapper which reads
// in a single threaded manner.
CheckoutSeriesWithoutLock(
shardID uint32,
id ident.ID,
tags ident.TagIterator,
) (result CheckoutSeriesResult, owned bool, err error)
// CheckoutSeriesWithLock is the "with lock" version of
// CheckoutSeriesWithoutLock.
// Note: With lock variant performs locking and callers do not need
// to be concerned about parallel access.
CheckoutSeriesWithLock(
shardID uint32,
id ident.ID,
tags ident.TagIterator,
) (result CheckoutSeriesResult, owned bool, err error)
// Close will close the data accumulator and will release
// all series read/write refs.
Close() error
}
// CheckoutSeriesResult is the result of a checkout series operation.
type CheckoutSeriesResult struct {
// Series is the series for the checkout operation.
Series series.DatabaseSeries
// Shard is the shard for the series.
Shard uint32
// UniqueIndex is the unique index for the series.
UniqueIndex uint64
}
// NamespaceResults is the result of a bootstrap process.
type NamespaceResults struct {
// Results is the result of a bootstrap process.
Results *NamespaceResultsMap
}
// NamespaceResult is the result of a bootstrap process for a given namespace.
type NamespaceResult struct {
Metadata namespace.Metadata
Shards []uint32
DataResult result.DataBootstrapResult
IndexResult result.IndexBootstrapResult
}
// TargetRange is a bootstrap target range.
type TargetRange struct {
// Range is the time range to bootstrap for.
Range xtime.Range
// RunOptions is the bootstrap run options specific to the target range.
RunOptions RunOptions
}
// PersistConfig is the configuration for a bootstrap with persistence.
type PersistConfig struct {
// If enabled bootstrappers are allowed to write out bootstrapped data
// to disk on their own instead of just returning result in-memory.
Enabled bool
// If enabled, what type of persistence files should be generated during
// the process.
FileSetType persist.FileSetType
}
// ProcessOptions is a set of options for a bootstrap provider.
type ProcessOptions interface {
// SetCacheSeriesMetadata sets whether bootstrappers created by this
// provider should cache series metadata between runs.
SetCacheSeriesMetadata(value bool) ProcessOptions
// CacheSeriesMetadata returns whether bootstrappers created by this
// provider should cache series metadata between runs.
CacheSeriesMetadata() bool
// SetTopologyMapProvider sets the TopologyMapProvider.
SetTopologyMapProvider(value topology.MapProvider) ProcessOptions
// TopologyMapProvider returns the TopologyMapProvider.
TopologyMapProvider() topology.MapProvider
// SetOrigin sets the origin.
SetOrigin(value topology.Host) ProcessOptions
// Origin returns the origin.
Origin() topology.Host
// Validate validates that the ProcessOptions are correct.
Validate() error
}
// RunOptions is a set of options for a bootstrap run.
type RunOptions interface {
// SetPersistConfig sets persistence configuration for this bootstrap.
SetPersistConfig(value PersistConfig) RunOptions
// PersistConfig returns the persistence configuration for this bootstrap.
PersistConfig() PersistConfig
// SetCacheSeriesMetadata sets whether bootstrappers created by this
// provider should cache series metadata between runs.
SetCacheSeriesMetadata(value bool) RunOptions
// CacheSeriesMetadata returns whether bootstrappers created by this
// provider should cache series metadata between runs.
CacheSeriesMetadata() bool
// SetInitialTopologyState sets the initial topology state as it was
// measured before the bootstrap process began.
SetInitialTopologyState(value *topology.StateSnapshot) RunOptions
// InitialTopologyState returns the initial topology as it was measured
// before the bootstrap process began.
InitialTopologyState() *topology.StateSnapshot
}
// BootstrapperProvider constructs a bootstrapper.
type BootstrapperProvider interface {
// String returns the name of the bootstrapper.
String() string
// Provide constructs a bootstrapper.
Provide() (Bootstrapper, error)
}
// Bootstrapper is the interface for different bootstrapping mechanisms. Note that a bootstrapper
// can and will be reused so it is important to not rely on state stored in the bootstrapper itself
// with the mindset that it will always be set to default values from the constructor.
type Bootstrapper interface {
// String returns the name of the bootstrapper
String() string
// Bootstrap performs bootstrapping of series data and index metadata
// for the given time ranges, returning the bootstrapped series data
// and index blocks for the time ranges it's unable to fulfill in parallel.
// A bootstrapper should only return an error should it want to entirely
// cancel the bootstrapping of the node, i.e. non-recoverable situation
// like not being able to read from the filesystem.
Bootstrap(ctx context.Context, namespaces Namespaces) (NamespaceResults, error)
}
// Source represents a bootstrap source. Note that a source can and will be reused so
// it is important to not rely on state stored in the source itself with the mindset
// that it will always be set to default values from the constructor.
type Source interface {
// AvailableData returns what time ranges are available for bootstrapping a given set of shards.
AvailableData(
ns namespace.Metadata,
shardsTimeRanges result.ShardTimeRanges,
runOpts RunOptions,
) (result.ShardTimeRanges, error)
// AvailableIndex returns what time ranges are available for bootstrapping.
AvailableIndex(
ns namespace.Metadata,
shardsTimeRanges result.ShardTimeRanges,
opts RunOptions,
) (result.ShardTimeRanges, error)
// Read returns series data and index metadata for a given set of shards
// and specified time ranges and the time ranges it's unable to fulfill.
// A bootstrapper source should only return an error should it want to
// entirely cancel the bootstrapping of the node, i.e. non-recoverable
// situation like not being able to read from the filesystem.
Read(ctx context.Context, namespaces Namespaces) (NamespaceResults, error)
}