-
Notifications
You must be signed in to change notification settings - Fork 451
/
coldflush.go
228 lines (198 loc) · 6.77 KB
/
coldflush.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
// 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 storage
import (
"sync"
"github.com/m3db/m3/src/dbnode/persist"
xerrors "github.com/m3db/m3/src/x/errors"
"github.com/m3db/m3/src/x/instrument"
xtime "github.com/m3db/m3/src/x/time"
"github.com/uber-go/tally"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
type coldFlushManager struct {
databaseCleanupManager
sync.RWMutex
log *zap.Logger
database database
pm persist.Manager
opts Options
// Retain using fileOpStatus here to be consistent w/ the
// filesystem manager since both are filesystem processes.
status fileOpStatus
isColdFlushing tally.Gauge
enabled bool
}
func newColdFlushManager(
database database,
pm persist.Manager,
opts Options,
) databaseColdFlushManager {
instrumentOpts := opts.InstrumentOptions()
scope := instrumentOpts.MetricsScope().SubScope("fs")
// NB(bodu): cold flush cleanup doesn't require commit logs.
cm := newCleanupManager(database, nil, scope)
return &coldFlushManager{
databaseCleanupManager: cm,
log: instrumentOpts.Logger(),
database: database,
pm: pm,
opts: opts,
status: fileOpNotStarted,
isColdFlushing: scope.Gauge("cold-flush"),
enabled: true,
}
}
func (m *coldFlushManager) Disable() fileOpStatus {
m.Lock()
status := m.status
m.enabled = false
m.Unlock()
return status
}
func (m *coldFlushManager) Enable() fileOpStatus {
m.Lock()
status := m.status
m.enabled = true
m.Unlock()
return status
}
func (m *coldFlushManager) Status() fileOpStatus {
m.RLock()
status := m.status
m.RUnlock()
return status
}
func (m *coldFlushManager) Run(t xtime.UnixNano) bool {
m.Lock()
if !m.shouldRunWithLock() {
m.Unlock()
return false
}
m.status = fileOpInProgress
m.Unlock()
defer func() {
m.Lock()
m.status = fileOpNotStarted
m.Unlock()
}()
debugLog := m.log.Check(zapcore.DebugLevel, "cold flush run")
if debugLog != nil {
debugLog.Write(zap.String("status", "starting cold flush"), zap.Time("time", t.ToTime()))
}
// NB(xichen): perform data cleanup and flushing sequentially to minimize the impact of disk seeks.
// NB(r): Use invariant here since flush errors were introduced
// and not caught in CI or integration tests.
// When an invariant occurs in CI tests it panics so as to fail
// the build.
if err := m.ColdFlushCleanup(t); err != nil {
instrument.EmitAndLogInvariantViolation(m.opts.InstrumentOptions(),
func(l *zap.Logger) {
l.Error("error when cleaning up cold flush data",
zap.Time("time", t.ToTime()), zap.Error(err))
})
}
if err := m.trackedColdFlush(); err != nil {
instrument.EmitAndLogInvariantViolation(m.opts.InstrumentOptions(),
func(l *zap.Logger) {
l.Error("error when cold flushing data",
zap.Time("time", t.ToTime()), zap.Error(err))
})
}
if debugLog != nil {
debugLog.Write(zap.String("status", "completed cold flush"), zap.Time("time", t.ToTime()))
}
return true
}
func (m *coldFlushManager) trackedColdFlush() error {
// The cold flush process will persist any data that has been "loaded" into memory via
// the Load() API but has not yet been persisted durably. As a result, if the cold flush
// process completes without error, then we want to "decrement" the number of tracked bytes
// by however many were outstanding right before the cold flush began.
//
// For example:
// t0: Load 100 bytes --> (numLoadedBytes == 100, numPendingLoadedBytes == 0)
// t1: memTracker.MarkLoadedAsPending() --> (numLoadedBytes == 100, numPendingLoadedBytes == 100)
// t2: Load 200 bytes --> (numLoadedBytes == 300, numPendingLoadedBytes == 100)
// t3: ColdFlushStart()
// t4: Load 300 bytes --> (numLoadedBytes == 600, numPendingLoadedBytes == 100)
// t5: ColdFlushEnd()
// t6: memTracker.DecPendingLoadedBytes() --> (numLoadedBytes == 500, numPendingLoadedBytes == 0)
// t7: memTracker.MarkLoadedAsPending() --> (numLoadedBytes == 500, numPendingLoadedBytes == 500)
// t8: ColdFlushStart()
// t9: ColdFlushError()
// t10: memTracker.MarkLoadedAsPending() --> (numLoadedBytes == 500, numPendingLoadedBytes == 500)
// t11: ColdFlushStart()
// t12: ColdFlushEnd()
// t13: memTracker.DecPendingLoadedBytes() --> (numLoadedBytes == 0, numPendingLoadedBytes == 0)
memTracker := m.opts.MemoryTracker()
memTracker.MarkLoadedAsPending()
if err := m.coldFlush(); err != nil {
return err
}
// Only decrement if the cold flush was a success. In this case, the decrement will reduce the
// value by however many bytes had been tracked when the cold flush began.
memTracker.DecPendingLoadedBytes()
return nil
}
func (m *coldFlushManager) coldFlush() error {
namespaces, err := m.database.OwnedNamespaces()
if err != nil {
return err
}
flushPersist, err := m.pm.StartFlushPersist()
if err != nil {
return err
}
multiErr := xerrors.NewMultiError()
for _, ns := range namespaces {
if err = ns.ColdFlush(flushPersist); err != nil {
multiErr = multiErr.Add(err)
}
}
multiErr = multiErr.Add(flushPersist.DoneFlush())
err = multiErr.FinalError()
return err
}
func (m *coldFlushManager) Report() {
m.databaseCleanupManager.Report()
m.RLock()
status := m.status
m.RUnlock()
if status == fileOpInProgress {
m.isColdFlushing.Update(1)
} else {
m.isColdFlushing.Update(0)
}
}
func (m *coldFlushManager) shouldRunWithLock() bool {
return m.enabled && m.status != fileOpInProgress && m.database.IsBootstrapped()
}
type coldFlushNsOpts struct {
reuseResources bool
}
// NewColdFlushNsOpts returns a new ColdFlushNsOpts.
func NewColdFlushNsOpts(reuseResources bool) ColdFlushNsOpts {
return &coldFlushNsOpts{reuseResources: reuseResources}
}
func (o *coldFlushNsOpts) ReuseResources() bool {
return o.reuseResources
}