-
Notifications
You must be signed in to change notification settings - Fork 61
/
collection_merger.go
381 lines (290 loc) · 9.93 KB
/
collection_merger.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
// Copyright 2016-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package moss
import (
"math"
"sync/atomic"
"time"
)
// NotifyMerger sends a message (optionally synchronously) to the merger
// to run another cycle. Providing a kind of "mergeAll" forces a full
// merge and can be useful for applications that are no longer
// performing mutations and that want to optimize for retrievals.
func (m *collection) NotifyMerger(kind string, synchronous bool) error {
atomic.AddUint64(&m.stats.TotNotifyMergerBeg, 1)
var pongCh chan struct{}
if synchronous {
pongCh = make(chan struct{})
}
m.pingMergerCh <- ping{
kind: kind,
pongCh: pongCh,
}
if pongCh != nil {
<-pongCh
}
atomic.AddUint64(&m.stats.TotNotifyMergerEnd, 1)
return nil
}
// ------------------------------------------------------
// runMerger() implements the background merger task.
func (m *collection) runMerger() {
defer func() {
close(m.doneMergerCh)
atomic.AddUint64(&m.stats.TotMergerEnd, 1)
}()
maxPreMergerBatches := m.options.MaxPreMergerBatches
if maxPreMergerBatches <= 0 {
maxPreMergerBatches =
DefaultCollectionOptions.MaxPreMergerBatches
}
if maxPreMergerBatches >= math.MaxInt32 {
return // Way to disable merger.
}
pings := []ping{}
defer func() {
replyToPings(pings)
pings = pings[0:0]
}()
go m.idleMergerWaker()
OUTER:
for {
atomic.AddUint64(&m.stats.TotMergerLoop, 1)
// ---------------------------------------------
// Notify ping'ers from the previous loop.
replyToPings(pings)
pings = pings[0:0]
// ---------------------------------------------
// Wait for new stackDirtyTop entries and/or pings.
var stopped, mergeAll bool
stopped, mergeAll, pings = m.mergerWaitForWork(pings)
if stopped {
return
}
// ---------------------------------------------
// Atomically ingest stackDirtyTop into stackDirtyMid.
var stackDirtyTopPrev *segmentStack
var stackDirtyMidPrev *segmentStack
var stackDirtyBase *segmentStack
stackDirtyMid, _, _, _, _ :=
m.snapshot(snapshotSkipClean|snapshotSkipDirtyBase,
func(ss *segmentStack) {
m.invalidateLatestSnapshotLOCKED()
// m.stackDirtyMid takes 1 refs, and
// stackDirtyMid takes 1 refs.
ss.refs++
stackDirtyTopPrev = m.stackDirtyTop
m.stackDirtyTop = nil
stackDirtyMidPrev = m.stackDirtyMid
m.stackDirtyMid = ss
stackDirtyBase = m.stackDirtyBase
if stackDirtyBase != nil {
// While waiting for persistence, might as well do
mergeAll = true // a full merge to optimize reads.
stackDirtyBase.addRef()
}
// Awake writers waiting for space in stackDirtyTop.
m.stackDirtyTopCond.Broadcast()
},
false) // The collection level lock needs to be acquired.
stackDirtyTopPrev.Close()
stackDirtyMidPrev.Close()
// ---------------------------------------------
// Merge multiple stackDirtyMid layers.
startTime := time.Now()
mergerWasOk := m.mergerMain(stackDirtyMid, stackDirtyBase, mergeAll)
if !mergerWasOk {
continue OUTER
}
m.histograms["MergerUsecs"].Add(
uint64(time.Since(startTime).Nanoseconds()/1000), 1)
// ---------------------------------------------
// Notify persister.
m.mergerNotifyPersister()
// ---------------------------------------------
atomic.AddUint64(&m.stats.TotMergerLoopRepeat, 1)
m.fireEvent(EventKindMergerProgress, time.Now().Sub(startTime))
}
// TODO: Concurrent merging of disjoint slices of stackDirtyMid
// instead of the current, single-threaded merger?
//
// TODO: A busy merger means no feeding of the persister?
//
// TODO: Delay merger until lots of deletion tombstones?
//
// TODO: The base layer is likely the largest, so instead of heap
// merging the base layer entries, treat the base layer with
// special case to binary search to find better start points?
//
// TODO: Dynamically calc'ed soft max dirty top height, for
// read-heavy (favor lower) versus write-heavy (favor higher)
// situations?
}
// ------------------------------------------------------
func (m *collection) idleMergerWaker() {
idleTimeout := m.options.MergerIdleRunTimeoutMS
if idleTimeout == 0 {
idleTimeout = DefaultCollectionOptions.MergerIdleRunTimeoutMS
}
if idleTimeout <= 0 { // -1 will disable idle compactions.
return
}
var prevRunID uint64 // Helps run idle merger IFF new data has come in.
for {
if atomic.LoadUint64(&m.stats.TotCloseBeg) > 0 {
return
}
atomic.AddUint64(&m.stats.TotMergerIdleSleeps, 1)
napIDBefore := atomic.LoadUint64(&m.stats.TotMergerWaitIncomingEnd)
time.Sleep(time.Duration(idleTimeout) * time.Millisecond)
napIDAfter := atomic.LoadUint64(&m.stats.TotMergerWaitIncomingEnd)
napID := atomic.LoadUint64(&m.stats.TotMergerWaitIncomingBeg)
if napID == napIDAfter+1 && // Merger is indeed asleep.
napIDBefore == napIDAfter && // Nap only while merger naps.
atomic.LoadUint64(&m.stats.TotMergerInternalEnd) > prevRunID { // New data.
m.NotifyMerger("from-idle-merger", false)
prevRunID = atomic.LoadUint64(&m.stats.TotMergerInternalEnd)
}
}
}
// mergerWaitForWork() is a helper method that blocks until there's
// either pings or incoming segments (from ExecuteBatch()) of work for
// the merger.
func (m *collection) mergerWaitForWork(pings []ping) (
stopped, mergeAll bool, pingsOut []ping) {
var waitDirtyIncomingCh chan struct{}
m.m.Lock()
if m.stackDirtyTop == nil || len(m.stackDirtyTop.a) <= 0 {
m.waitDirtyIncomingCh = make(chan struct{})
waitDirtyIncomingCh = m.waitDirtyIncomingCh
}
m.m.Unlock()
if waitDirtyIncomingCh != nil {
atomic.AddUint64(&m.stats.TotMergerWaitIncomingBeg, 1)
select {
case <-m.stopCh:
atomic.AddUint64(&m.stats.TotMergerWaitIncomingStop, 1)
return true, mergeAll, pings
case pingVal := <-m.pingMergerCh:
pings = append(pings, pingVal)
if pingVal.kind == "mergeAll" {
mergeAll = true
} else if pingVal.kind == "from-idle-merger" {
atomic.AddUint64(&m.stats.TotMergerIdleRuns, 1)
mergeAll = true
}
case <-waitDirtyIncomingCh:
// NO-OP.
}
atomic.AddUint64(&m.stats.TotMergerWaitIncomingEnd, 1)
} else {
atomic.AddUint64(&m.stats.TotMergerWaitIncomingSkip, 1)
}
pings, mergeAll = receivePings(m.pingMergerCh, pings, "mergeAll", mergeAll)
return false, mergeAll, pings
}
// ------------------------------------------------------
// mergerMain() is a helper method that performs the merging work on
// the stackDirtyMid and swaps the merged result into the collection.
func (m *collection) mergerMain(stackDirtyMid, stackDirtyBase *segmentStack,
mergeAll bool) (ok bool) {
if stackDirtyMid != nil && !stackDirtyMid.isEmpty() {
atomic.AddUint64(&m.stats.TotMergerInternalBeg, 1)
mergedStackDirtyMid, numFullMerges, err := stackDirtyMid.merge(mergeAll,
stackDirtyBase)
if err != nil {
atomic.AddUint64(&m.stats.TotMergerInternalErr, 1)
m.Logf("collection: mergerMain stackDirtyMid.merge,"+
" numFullMerges: %d, err: %v", numFullMerges, err)
m.OnError(err)
stackDirtyMid.Close()
stackDirtyBase.Close()
return false
}
atomic.AddUint64(&m.stats.TotMergerAll, numFullMerges)
atomic.AddUint64(&m.stats.TotMergerInternalEnd, 1)
stackDirtyMid.Close()
mergedStackDirtyMid.addRef()
stackDirtyMid = mergedStackDirtyMid
m.m.Lock()
stackDirtyMidPrev := m.stackDirtyMid
m.stackDirtyMid = mergedStackDirtyMid
m.m.Unlock()
stackDirtyMidPrev.Close()
} else {
if stackDirtyMid != nil && stackDirtyMid.isEmpty() {
// Do this only for idle-compactions.
atomic.AddUint64(&m.stats.TotMergerEmptyDirtyMid, 1)
m.m.Lock() // Allow an empty stackDirtyMid to kick persistence.
stackDirtyMidPrev := m.stackDirtyMid
m.stackDirtyMid = stackDirtyMid
m.m.Unlock()
stackDirtyMidPrev.Close()
}
atomic.AddUint64(&m.stats.TotMergerInternalSkip, 1)
}
stackDirtyBase.Close()
lenDirtyMid := len(stackDirtyMid.a)
if lenDirtyMid > 0 {
topDirtyMid := stackDirtyMid.a[lenDirtyMid-1]
m.Logf("collection: mergerMain, dirtyMid height: %2d,"+
" dirtyMid top # entries: %d", lenDirtyMid, topDirtyMid.Len())
}
stackDirtyMid.Close()
return true
}
// ------------------------------------------------------
// mergerNotifyPersister() is a helper method that notifies the
// optional persister goroutine that there's a dirty segment stack
// that needs persistence.
func (m *collection) mergerNotifyPersister() {
if m.options.LowerLevelUpdate == nil {
return
}
m.m.Lock()
if m.stackDirtyBase == nil && m.stackDirtyMid != nil {
atomic.AddUint64(&m.stats.TotMergerLowerLevelNotify, 1)
m.stackDirtyBase = m.stackDirtyMid
m.stackDirtyMid = nil
prevLowerLevelSnapshot := m.stackDirtyBase.lowerLevelSnapshot
m.stackDirtyBase.lowerLevelSnapshot = m.lowerLevelSnapshot.addRef()
if prevLowerLevelSnapshot != nil {
prevLowerLevelSnapshot.decRef()
}
if m.waitDirtyOutgoingCh != nil {
close(m.waitDirtyOutgoingCh)
}
m.waitDirtyOutgoingCh = make(chan struct{})
m.stackDirtyBaseCond.Broadcast()
} else {
atomic.AddUint64(&m.stats.TotMergerLowerLevelNotifySkip, 1)
}
var waitDirtyOutgoingCh chan struct{}
if m.options.MaxDirtyOps > 0 || m.options.MaxDirtyKeyValBytes > 0 {
cs := CollectionStats{}
m.statsSegmentsLOCKED(&cs)
if cs.CurDirtyOps > m.options.MaxDirtyOps ||
cs.CurDirtyBytes > m.options.MaxDirtyKeyValBytes {
waitDirtyOutgoingCh = m.waitDirtyOutgoingCh
}
}
m.m.Unlock()
if waitDirtyOutgoingCh != nil {
atomic.AddUint64(&m.stats.TotMergerWaitOutgoingBeg, 1)
select {
case <-m.stopCh:
atomic.AddUint64(&m.stats.TotMergerWaitOutgoingStop, 1)
return
case <-waitDirtyOutgoingCh:
// NO-OP.
}
atomic.AddUint64(&m.stats.TotMergerWaitOutgoingEnd, 1)
} else {
atomic.AddUint64(&m.stats.TotMergerWaitOutgoingSkip, 1)
}
}