-
Notifications
You must be signed in to change notification settings - Fork 61
/
persister.go
138 lines (103 loc) · 3.41 KB
/
persister.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
// 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 (
"sync/atomic"
"time"
)
// runPersister() implements the persister task.
func (m *collection) runPersister() {
defer func() {
close(m.donePersisterCh)
atomic.AddUint64(&m.stats.TotPersisterEnd, 1)
}()
if m.options.LowerLevelUpdate == nil {
return
}
OUTER:
for {
atomic.AddUint64(&m.stats.TotPersisterLoop, 1)
m.m.Lock()
for m.stackDirtyBase == nil && !m.isClosed() {
// There's a concurrency scenario where imagine that
// persistence takes a long time. Also, imagine that
// there are no more incoming batches (so, stackDirtyTop
// is empty).
//
// That allows the merger to complete a merging cycle (so,
// stackDirtyMid is non-empty with unpersisted data) and
// the merger is now just waiting for either more incoming
// batches or waiting to be awoken.
//
// So, we notify/awake the merger here so that it can feed
// stackDirtyMid down to the persister as stackDirtyBase.
if m.waitDirtyIncomingCh != nil && // Merger is indeed asleep.
(m.stackDirtyMid != nil && len(m.stackDirtyMid.a) > 0) &&
(m.stackDirtyTop == nil || len(m.stackDirtyTop.a) <= 0) {
m.NotifyMerger("from-persister", false)
}
atomic.AddUint64(&m.stats.TotPersisterWaitBeg, 1)
m.stackDirtyBaseCond.Wait()
atomic.AddUint64(&m.stats.TotPersisterWaitEnd, 1)
}
stackDirtyBase := m.stackDirtyBase
m.m.Unlock()
if m.isClosed() {
return
}
startTime := time.Now()
atomic.AddUint64(&m.stats.TotPersisterLowerLevelUpdateBeg, 1)
llssNext, err := m.options.LowerLevelUpdate(stackDirtyBase)
if err != nil {
atomic.AddUint64(&m.stats.TotPersisterLowerLevelUpdateErr, 1)
m.Logf("collection: runPersister, LowerLevelUpdate, err: %v", err)
m.OnError(err)
continue OUTER
}
atomic.AddUint64(&m.stats.TotPersisterLowerLevelUpdateEnd, 1)
var stackDirtyBasePrev *segmentStack
var stackCleanPrev *segmentStack
m.m.Lock()
m.invalidateLatestSnapshotLOCKED()
stackCleanPrev = m.stackClean
if m.options.CachePersisted {
m.stackClean = m.stackDirtyBase
} else {
m.stackClean = nil
stackDirtyBasePrev = m.stackDirtyBase
}
m.stackDirtyBase = nil
waitDirtyOutgoingCh := m.waitDirtyOutgoingCh
m.waitDirtyOutgoingCh = nil
llssPrev := m.lowerLevelSnapshot
m.lowerLevelSnapshot = NewSnapshotWrapper(llssNext, nil)
m.m.Unlock()
if stackDirtyBasePrev != nil {
stackDirtyBasePrev.Close()
}
if stackCleanPrev != nil {
stackCleanPrev.Close()
}
if llssPrev != nil {
llssPrev.Close()
}
if waitDirtyOutgoingCh != nil {
close(waitDirtyOutgoingCh)
}
// ---------------------------------------------
atomic.AddUint64(&m.stats.TotPersisterLoopRepeat, 1)
m.fireEvent(EventKindPersisterProgress, time.Now().Sub(startTime))
}
// TODO: More advanced eviction of stackClean.
// TODO: Timer based eviction of stackClean?
// TODO: Randomized eviction?
// TODO: Merging of stackClean to 1 level?
// TODO: WaitForMerger() also considers stackClean?
// TODO: Track popular Get() keys?
// TODO: Track shadowing during merges for writes.
}