-
Notifications
You must be signed in to change notification settings - Fork 453
/
bootstrap.go
203 lines (173 loc) · 6.84 KB
/
bootstrap.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
// 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 storage
import (
"errors"
"sync"
"time"
"github.com/m3db/m3/src/dbnode/clock"
"github.com/m3db/m3/src/dbnode/storage/bootstrap"
xerrors "github.com/m3db/m3/src/x/errors"
"github.com/uber-go/tally"
"go.uber.org/zap"
)
var (
// errNamespaceIsBootstrapping raised when trying to bootstrap a namespace that's being bootstrapped.
errNamespaceIsBootstrapping = errors.New("namespace is bootstrapping")
// errNamespaceNotBootstrapped raised when trying to flush/snapshot data for a namespace that's not yet bootstrapped.
errNamespaceNotBootstrapped = errors.New("namespace is not yet bootstrapped")
// errShardIsBootstrapping raised when trying to bootstrap a shard that's being bootstrapped.
errShardIsBootstrapping = errors.New("shard is bootstrapping")
// errShardNotBootstrappedToFlush raised when trying to flush data for a shard that's not yet bootstrapped.
errShardNotBootstrappedToFlush = errors.New("shard is not yet bootstrapped to flush")
// errShardNotBootstrappedToSnapshot raised when trying to snapshot data for a shard that's not yet bootstrapped.
errShardNotBootstrappedToSnapshot = errors.New("shard is not yet bootstrapped to snapshot")
// errShardNotBootstrappedToRead raised when trying to read data for a shard that's not yet bootstrapped.
errShardNotBootstrappedToRead = errors.New("shard is not yet bootstrapped to read")
// errIndexNotBootstrappedToRead raised when trying to read the index before being bootstrapped.
errIndexNotBootstrappedToRead = errors.New("index is not yet bootstrapped to read")
// errBootstrapEnqueued raised when trying to bootstrap and bootstrap becomes enqueued.
errBootstrapEnqueued = errors.New("database bootstrapping enqueued bootstrap")
)
type bootstrapManager struct {
sync.RWMutex
database database
mediator databaseMediator
opts Options
log *zap.Logger
nowFn clock.NowFn
processProvider bootstrap.ProcessProvider
state BootstrapState
hasPending bool
status tally.Gauge
lastBootstrapCompletionTime time.Time
}
func newBootstrapManager(
database database,
mediator databaseMediator,
opts Options,
) databaseBootstrapManager {
scope := opts.InstrumentOptions().MetricsScope()
return &bootstrapManager{
database: database,
mediator: mediator,
opts: opts,
log: opts.InstrumentOptions().Logger(),
nowFn: opts.ClockOptions().NowFn(),
processProvider: opts.BootstrapProcessProvider(),
status: scope.Gauge("bootstrapped"),
}
}
func (m *bootstrapManager) IsBootstrapped() bool {
m.RLock()
state := m.state
m.RUnlock()
return state == Bootstrapped
}
func (m *bootstrapManager) LastBootstrapCompletionTime() (time.Time, bool) {
return m.lastBootstrapCompletionTime, !m.lastBootstrapCompletionTime.IsZero()
}
func (m *bootstrapManager) Bootstrap() error {
m.Lock()
switch m.state {
case Bootstrapping:
// NB(r): Already bootstrapping, now a consequent bootstrap
// request comes in - we queue this up to bootstrap again
// once the current bootstrap has completed.
// This is an edge case that can occur if during either an
// initial bootstrap or a resharding bootstrap if a new
// reshard occurs and we need to bootstrap more shards.
m.hasPending = true
m.Unlock()
return errBootstrapEnqueued
default:
m.state = Bootstrapping
}
m.Unlock()
// NB(xichen): disable filesystem manager before we bootstrap to minimize
// the impact of file operations on bootstrapping performance
m.mediator.DisableFileOps()
defer m.mediator.EnableFileOps()
// Keep performing bootstraps until none pending
multiErr := xerrors.NewMultiError()
for {
err := m.bootstrap()
if err != nil {
multiErr = multiErr.Add(err)
}
m.Lock()
currPending := m.hasPending
if currPending {
// New bootstrap calls should now enqueue another pending bootstrap
m.hasPending = false
} else {
m.state = Bootstrapped
}
m.Unlock()
if !currPending {
break
}
}
// NB(xichen): in order for bootstrapped data to be flushed to disk, a tick
// needs to happen to drain the in-memory buffers and a consequent flush will
// flush all the data onto disk. However, this has shown to be too intensive
// to do immediately after bootstrap due to bootstrapping nodes simultaneously
// attempting to tick through their series and flushing data, adding significant
// load to the cluster. It turns out to be better to let ticking happen naturally
// on its own course so that the load of ticking and flushing is more spread out
// across the cluster.
m.lastBootstrapCompletionTime = m.nowFn()
return multiErr.FinalError()
}
func (m *bootstrapManager) Report() {
if m.IsBootstrapped() {
m.status.Update(1)
} else {
m.status.Update(0)
}
}
func (m *bootstrapManager) bootstrap() error {
// NB(r): construct new instance of the bootstrap process to avoid
// state being kept around by bootstrappers.
process, err := m.processProvider.Provide()
if err != nil {
return err
}
// NB(xichen): each bootstrapper should be responsible for choosing the most
// efficient way of bootstrapping database shards, be it sequential or parallel.
multiErr := xerrors.NewMultiError()
namespaces, err := m.database.GetOwnedNamespaces()
if err != nil {
return err
}
startBootstrap := m.nowFn()
for _, namespace := range namespaces {
startNamespaceBootstrap := m.nowFn()
if err := namespace.Bootstrap(startBootstrap, process); err != nil {
multiErr = multiErr.Add(err)
}
took := m.nowFn().Sub(startNamespaceBootstrap)
m.log.Info("bootstrap finished",
zap.String("namespace", namespace.ID().String()),
zap.Duration("duration", took),
)
}
return multiErr.FinalError()
}