-
Notifications
You must be signed in to change notification settings - Fork 5
/
journal.go
330 lines (293 loc) · 6.52 KB
/
journal.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
package rbolt
import (
"bytes"
"sort"
"sync"
"github.com/boltdb/bolt"
)
type Op int
func (op Op) String() string {
switch op {
case OpCreateBucket:
return "CreateBucket"
case OpCreateBucketIfNotExists:
return "CreateBucketIfNotExists"
case OpDelete:
return "Delete"
case OpDeleteBucket:
return "DeleteBucket"
case OpPut:
return "Put"
case OpBucketCursor:
return "BucketCursor"
case OpCursorDelete:
return "CursorDelete"
case OpCursorFirst:
return "CursorFirst"
case OpCursorLast:
return "CursorLast"
case OpCursorNext:
return "CursorNext"
case OpCursorPrev:
return "CursorPrev"
case OpCursorSeek:
return "CursorSeek"
default:
return ""
}
}
const (
OpCreateBucket Op = iota + 1
OpCreateBucketIfNotExists
OpDelete
OpDeleteBucket
OpPut
OpBucketCursor
OpCursorDelete
OpCursorFirst
OpCursorLast
OpCursorNext
OpCursorPrev
OpCursorSeek
)
type Journal struct {
LSN int
TxID int
Ws []W
Type JournalType
cursors map[string][]*bolt.Cursor
}
type JournalType int
const (
JournalTypeUpdate JournalType = iota + 1
JournalTypeCommit
JournalTypeRollback
)
func (t JournalType) String() string {
switch t {
case JournalTypeUpdate:
return "update"
case JournalTypeCommit:
return "commit"
case JournalTypeRollback:
return "rollback"
default:
return ""
}
}
func (j *Journal) Play(tx *bolt.Tx) error {
j.cursors = make(map[string][]*bolt.Cursor)
for _, w := range j.Ws {
if err := j.playW(tx, w); err != nil {
return err
}
}
return nil
}
func pkey(p [][]byte) string {
return string(bytes.Join(p, []byte("::")))
}
func (j *Journal) playW(tx *bolt.Tx, w W) error {
tailKey := w.Path[len(w.Path)-1]
switch w.Op {
case OpCreateBucket:
return j.opCreateBucket(tx, w, false)
case OpCreateBucketIfNotExists:
return j.opCreateBucket(tx, w, true)
case OpDelete:
b := j.bucketOrTx(tx, w.Path)
// b can't be nil
return b.Delete(tailKey)
case OpDeleteBucket:
b := j.bucketOrTx(tx, w.Path)
if b == nil {
return tx.DeleteBucket(tailKey)
}
return b.DeleteBucket(tailKey)
case OpPut:
b := j.bucketOrTx(tx, w.Path)
// b can't be nil
return b.Put(tailKey, w.Value)
case OpBucketCursor:
// we add an empty key since bucketOrTx() works for keys, and we have only a bucket path here.
b := j.bucketOrTx(tx, append(w.Path, []byte{}))
// b can't be nil, we don't process tx's cursors
k := pkey(w.Path)
j.cursors[k] = append(j.cursors[k], b.Cursor())
case OpCursorDelete:
c := j.cursor(tx, w.Path, w.CursorID)
if err := c.Delete(); err != nil {
return err
}
case OpCursorFirst:
c := j.cursor(tx, w.Path, w.CursorID)
c.First()
case OpCursorLast:
c := j.cursor(tx, w.Path, w.CursorID)
c.Last()
case OpCursorNext:
c := j.cursor(tx, w.Path, w.CursorID)
c.Next()
case OpCursorPrev:
c := j.cursor(tx, w.Path, w.CursorID)
c.Prev()
case OpCursorSeek:
c := j.cursor(tx, w.Path, w.CursorID)
c.Seek(w.Value)
}
return nil
}
func (j *Journal) bucketOrTx(tx *bolt.Tx, p [][]byte) *bolt.Bucket {
if len(p) == 1 {
return nil
}
b := tx.Bucket(p[0])
for _, k := range p[1 : len(p)-1] {
b = b.Bucket(k)
}
return b
}
func (j *Journal) opCreateBucket(tx *bolt.Tx, w W, withExists bool) error {
b := j.bucketOrTx(tx, w.Path)
if b == nil {
if withExists {
_, err := tx.CreateBucketIfNotExists(w.Path[0])
return err
}
_, err := tx.CreateBucket(w.Path[0])
return err
}
if withExists {
_, err := b.CreateBucketIfNotExists(w.Path[len(w.Path)-1])
return err
}
_, err := b.CreateBucket(w.Path[len(w.Path)-1])
return err
}
func (j *Journal) cursor(tx *bolt.Tx, p [][]byte, id int) *bolt.Cursor {
k := pkey(p)
return j.cursors[k][id]
}
type W struct {
Op Op
Path [][]byte
CursorID int
Value []byte
}
type JournalBuffer struct {
l sync.Mutex
updates map[int]*Journal // txID => Journal
journals []*Journal // Journal commits or rollbacks sorted by LSN
db *bolt.DB
}
type byLSN []*Journal
func (js byLSN) Len() int { return len(js) }
func (js byLSN) Swap(i, j int) { js[i], js[j] = js[j], js[i] }
func (js byLSN) Less(i, j int) bool { return js[i].LSN < js[j].LSN }
func NewJournalBuffer(db *bolt.DB) *JournalBuffer {
return &JournalBuffer{updates: make(map[int]*Journal), db: db}
}
func (jb *JournalBuffer) Flush() error {
jb.l.Lock()
defer jb.l.Unlock()
sort.Sort(byLSN(jb.journals))
var journals []*Journal
defer func() {
jb.journals = journals
}()
for i, j := range jb.journals {
txj, ok := jb.updates[j.TxID]
if !ok {
// we may have got the commit/rollback journal before the update journal
// we must interrupt now to not mix the order journals are played
journals = append(journals, jb.journals[i:]...)
return nil
}
switch j.Type {
case JournalTypeCommit:
if ok {
if err := jb.db.Update(func(tx *bolt.Tx) error { return txj.Play(tx) }); err != nil {
// keep this journal and the remaining ones to retry later
journals = append(journals, jb.journals[i:]...)
return err
}
delete(jb.updates, j.TxID)
}
case JournalTypeRollback:
delete(jb.updates, j.TxID)
}
}
return nil
}
func (jb *JournalBuffer) WriteJournal(j *Journal) {
jb.l.Lock()
defer jb.l.Unlock()
switch j.Type {
case JournalTypeUpdate:
jb.updates[j.TxID] = j
case JournalTypeCommit, JournalTypeRollback:
jb.journals = append(jb.journals, j)
}
}
func Update(db *bolt.DB, transport Transport, lsn LSNGenerator, fn func(tx *Tx) error) error {
var txID int
if err := db.Update(func(tx *bolt.Tx) (err error) {
rtx := RTx(tx)
txID = rtx.ID()
tx.OnCommit(func() {
commitJournal := &Journal{
TxID: txID,
Type: JournalTypeCommit,
LSN: lsn.NextLSN(),
}
transport.Send(commitJournal)
})
defer func() {
j := rtx.Journal()
j.LSN = lsn.NextLSN()
transport.Send(j)
}()
return fn(rtx)
}); err != nil {
rollbackJournal := &Journal{
TxID: txID,
Type: JournalTypeRollback,
LSN: lsn.NextLSN(),
}
transport.Send(rollbackJournal)
return err
}
return nil
}
type Transport interface {
Send(*Journal)
}
type NullTransport struct{}
func (t NullTransport) Send(_ *Journal) {}
type LocalTransport struct {
JournalBuffer *JournalBuffer
}
func (t *LocalTransport) Send(j *Journal) {
t.JournalBuffer.WriteJournal(j)
}
type MultiTransport struct {
transports []Transport
}
func (mt *MultiTransport) Send(j *Journal) {
for _, t := range mt.transports {
t.Send(j)
}
}
type LSNGenerator interface {
NextLSN() int
}
type MonotonicLSN struct {
l sync.Mutex
lsn int
}
func (l *MonotonicLSN) NextLSN() int {
l.l.Lock()
defer l.l.Unlock()
l.lsn++
return l.lsn
}