/
partition.go
301 lines (267 loc) · 8.03 KB
/
partition.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
// Copyright (c) 2013 Couchbase, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you
// may not use this file except in compliance with the License. You
// may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package main
import (
"fmt"
"math/rand"
"sync"
"sync/atomic"
"unsafe"
"github.com/steveyen/gkvlite"
)
type partitionstore struct {
vbid uint16
parent *bucketstore
lock sync.Mutex // Properties below here are covered by this lock.
keys unsafe.Pointer // *gkvlite.Collection
changes unsafe.Pointer // *gkvlite.Collection
}
// Should only be used by readers.
func (p *partitionstore) colls() (keys, changes *gkvlite.Collection) {
return (*gkvlite.Collection)(atomic.LoadPointer(&p.keys)),
(*gkvlite.Collection)(atomic.LoadPointer(&p.changes))
}
func (p *partitionstore) mutate(cb func(keys, changes *gkvlite.Collection)) {
p.lock.Lock()
defer p.lock.Unlock()
cb((*gkvlite.Collection)(atomic.LoadPointer(&p.keys)),
(*gkvlite.Collection)(atomic.LoadPointer(&p.changes)))
}
func (p *partitionstore) collsPauseSwap(
cb func() (keys, changes *gkvlite.Collection)) {
p.lock.Lock()
defer p.lock.Unlock()
k, c := cb()
// Update the changes first, so that readers see a key index that's older.
atomic.StorePointer(&p.changes, unsafe.Pointer(c))
atomic.StorePointer(&p.keys, unsafe.Pointer(k))
}
func (p *partitionstore) get(key []byte) (*item, error) {
return p.getItem(key, true)
}
func (p *partitionstore) getItem(key []byte, withValue bool) (
i *item, err error) {
for retries := 0; retries < 5; retries++ {
keys, changes := p.colls()
kItem, err := keys.GetItem(key, true)
if err != nil {
return nil, err
}
if kItem == nil {
return nil, nil
}
// TODO: What if a compaction happens in between the lookups,
// and the changes-feed no longer has the item? Answer: compaction
// must not remove items that the key-index references.
i := (*item)(atomic.LoadPointer(&kItem.Transient))
if i != nil {
return i, nil
}
cItem, err := changes.GetItem(kItem.Val, true)
if err != nil {
return nil, err
}
if cItem != nil {
i = (*item)(atomic.LoadPointer(&cItem.Transient))
if i != nil {
atomic.StorePointer(&kItem.Transient, unsafe.Pointer(i))
return i, nil
}
i := &item{key: key}
if err = i.fromValueBytes(cItem.Val); err != nil {
return nil, err
}
atomic.StorePointer(&cItem.Transient, unsafe.Pointer(i))
atomic.StorePointer(&kItem.Transient, unsafe.Pointer(i))
return i, nil
}
// If cItem is nil, perhaps a concurrent set() happened after
// the keys.GetItem() and de-duped the old change. So, retry.
}
return nil, fmt.Errorf("max getItem retries for key: %v", key)
}
func (p *partitionstore) getTotals() (
numItems uint64, numItemBytes uint64, err error) {
keys, changes := p.colls()
numItems, _, err = keys.GetTotals()
if err == nil {
_, numItemBytes, err = changes.GetTotals()
}
return
}
func (p *partitionstore) visitItems(start []byte, withValue bool,
visitor func(*item) bool) (err error) {
keys, changes := p.colls()
var vErr error
v := func(kItem *gkvlite.Item) bool {
i := (*item)(atomic.LoadPointer(&kItem.Transient))
if i != nil {
return visitor(i)
}
var cItem *gkvlite.Item
cItem, vErr = changes.GetItem(kItem.Val, true)
if vErr != nil {
return false
}
if cItem == nil {
return true // TODO: track this case; might have been compacted away.
}
i = (*item)(atomic.LoadPointer(&cItem.Transient))
if i != nil {
atomic.StorePointer(&kItem.Transient, unsafe.Pointer(i))
return visitor(i)
}
i = &item{key: kItem.Key}
if vErr = i.fromValueBytes(cItem.Val); vErr != nil {
return false
}
atomic.StorePointer(&cItem.Transient, unsafe.Pointer(i))
atomic.StorePointer(&kItem.Transient, unsafe.Pointer(i))
return visitor(i)
}
if err := p.visit(keys, start, true, v); err != nil {
return err
}
return vErr
}
func (p *partitionstore) visitChanges(start []byte, withValue bool,
visitor func(*item) bool) (err error) {
_, changes := p.colls()
var vErr error
v := func(cItem *gkvlite.Item) bool {
i := &item{}
if vErr = i.fromValueBytes(cItem.Val); vErr != nil {
return false
}
return visitor(i)
}
if err := p.visit(changes, start, withValue, v); err != nil {
return err
}
return vErr
}
func (p *partitionstore) visit(coll *gkvlite.Collection,
start []byte, withValue bool,
v func(*gkvlite.Item) bool) (err error) {
if start == nil {
i, err := coll.MinItem(false)
if err != nil {
return err
}
if i == nil {
return nil
}
start = i.Key
}
return coll.VisitItemsAscend(start, withValue, v)
}
// All the following mutation methods need to be called while
// single-threaded with respect to the mutating collection.
func (p *partitionstore) set(newItem *item, oldItem *item) (
deltaItemBytes int64, err error) {
return p.setWithCallback(newItem, oldItem, nil)
}
// Callback is invoked while holding the mutate() lock, allowing
// the caller to do more atomic things.
func (p *partitionstore) setWithCallback(newItem *item, oldItem *item,
cb func()) (deltaItemBytes int64, err error) {
cBytes := casBytes(newItem.cas)
cItem := &gkvlite.Item{
Key: cBytes,
Val: newItem.toValueBytes(),
Priority: rand.Int31(),
Transient: unsafe.Pointer(newItem),
}
var kItem *gkvlite.Item
if newItem.key != nil && len(newItem.key) > 0 {
kItem = &gkvlite.Item{
Key: newItem.key,
Val: cBytes,
Priority: rand.Int31(),
Transient: unsafe.Pointer(newItem),
}
}
deltaItemBytes = newItem.NumBytes()
var oldItemCasBytes []byte
if oldItem != nil {
oldItemCasBytes = make([]byte, 8)
casBytesFill(oldItem.cas, oldItemCasBytes)
deltaItemBytes -= oldItem.NumBytes()
}
p.mutate(func(keys, changes *gkvlite.Collection) {
if err = changes.SetItem(cItem); err != nil {
return
}
dirtyForce := false
if newItem.key != nil && len(newItem.key) > 0 {
// TODO: What if we flush between the keys update and changes
// update? That could result in an inconsistent db file?
// Solution idea #1 is to have load-time fixup, that
// incorporates changes into the key-index.
if err = keys.SetItem(kItem); err != nil {
return
}
} else {
dirtyForce = true // An nil/empty key means this is a metadata change.
}
if oldItem != nil {
// TODO: Need a "frozen" CAS point where we don't de-duplicate changes stream.
changes.Delete(oldItemCasBytes)
}
p.parent.dirty(dirtyForce)
if cb != nil {
cb()
}
})
return deltaItemBytes, err
}
func (p *partitionstore) del(key []byte, cas uint64, oldItem *item) (
deltaItemBytes int64, err error) {
cBytes := casBytes(cas)
dItem := &item{key: key, cas: cas}
vBytes := dItem.markAsDeletion().toValueBytes()
cItem := &gkvlite.Item{
Key: cBytes,
Val: vBytes,
Priority: rand.Int31(),
}
deltaItemBytes = dItem.NumBytes()
var oldItemCasBytes []byte
if oldItem != nil {
oldItemCasBytes = make([]byte, 8)
casBytesFill(oldItem.cas, oldItemCasBytes)
deltaItemBytes -= oldItem.NumBytes()
}
p.mutate(func(keys, changes *gkvlite.Collection) {
if err = changes.SetItem(cItem); err != nil {
return
}
dirtyForce := false
if key != nil && len(key) > 0 {
// TODO: What if we flush between the keys update and changes
// update? That could result in an inconsistent db file?
// Solution idea #1 is to have load-time fixup, that
// incorporates changes into the key-index.
if _, err = keys.Delete(key); err != nil {
return
}
} else {
dirtyForce = true // An nil/empty key means this is a metadata change.
}
if oldItem != nil {
// TODO: Need a "frozen" CAS point where we don't de-duplicate changes stream.
changes.Delete(oldItemCasBytes)
}
p.parent.dirty(dirtyForce)
})
return deltaItemBytes, err
}