-
Notifications
You must be signed in to change notification settings - Fork 0
/
transaction.go
373 lines (340 loc) · 7.71 KB
/
transaction.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
package main
import (
"errors"
"fmt"
"hash/crc32"
"log"
"sort"
"sync"
"sync/atomic"
)
const (
InReadSet = 1 + iota
InWriteSet
InIndex
Deleted
NotInRWSet
NotExist
)
type Record struct {
key string
last *Version
mu sync.Mutex
}
type Version struct {
key string
value string
wTs uint64
rTs uint64
prev *Version
deleted bool
}
type Operation struct {
cmd uint8
version *Version
}
type WriteSet map[string][]*Operation
type ReadSet map[string]*Version
type Tx struct {
ts uint64
writeSet WriteSet
readSet ReadSet
db *DB
}
func NewTx(db *DB) *Tx {
ts := atomic.AddUint64(&db.tsGenerator, 1)
tx := &Tx{
ts: ts,
writeSet: make(WriteSet),
readSet: make(ReadSet),
db: db,
}
tx.db.aliveTx.mu.Lock()
defer tx.db.aliveTx.mu.Unlock()
db.aliveTx.txs = append(db.aliveTx.txs, ts)
return tx
}
func (tx *Tx) DestructTx() {
tx.writeSet = make(WriteSet)
tx.readSet = make(ReadSet)
tx.db.aliveTx.mu.Lock()
defer tx.db.aliveTx.mu.Unlock()
idx := 0
for i, ts := range tx.db.aliveTx.txs {
if tx.ts == ts {
idx = i
break
}
}
tx.db.aliveTx.txs = append(tx.db.aliveTx.txs[:idx], tx.db.aliveTx.txs[idx+1:]...)
}
func (tx *Tx) Read(key string) (string, error) {
// data in read/write-set
version, where := tx.checkExistence(key)
if where == InWriteSet || where == InReadSet {
return version.value, nil
}
version = &Version{
key: key,
value: "",
wTs: tx.ts,
rTs: tx.ts,
prev: nil,
deleted: true, // prevent phantom problem
}
record := &Record{
key: key,
last: version,
mu: sync.Mutex{},
}
v, exist := tx.db.index.LoadOrStore(key, record)
// data does not exist
if !exist {
tx.readSet[key] = version
return "", errors.New("key doesn't exist")
}
// data in index
record = v.(*Record)
record.mu.Lock()
defer record.mu.Unlock()
cur := record.last
for cur.wTs > tx.ts {
if cur.deleted { // delete flag check
return "", errors.New("key doesn't exist")
}
cur = cur.prev
if cur == nil {
break
}
}
if cur == nil { // cannot traverse
return "", errors.New("key doesn't exist")
}
if cur.deleted { // delete flag check
return "", errors.New("key doesn't exist")
}
cur.rTs = tx.ts
tx.readSet[key] = cur
return cur.value, nil
}
func (tx *Tx) Insert(key, value string) error {
_, where := tx.checkExistence(key) // read/write-set の確認だけにする、ここでdeletedの確認をしたところで、commit時には変わっているかもしれない
if where == NotInRWSet || where == Deleted {
v := Version{
key: key,
value: value,
wTs: tx.ts,
rTs: tx.ts,
prev: nil,
deleted: false,
}
tx.writeSet[key] = append(tx.writeSet[key], &Operation{cmd: INSERT, version: &v})
return nil
}
return errors.New("key already exists")
}
func (tx *Tx) Update(key, value string) error {
_, where := tx.checkExistence(key) // read/write-set の確認だけにする
if where == Deleted {
return errors.New("key doesn't exist")
}
v := Version{
key: key,
value: value,
wTs: tx.ts,
rTs: tx.ts,
prev: nil,
deleted: false,
}
tx.writeSet[key] = append(tx.writeSet[key], &Operation{cmd: UPDATE, version: &v})
return nil
}
func (tx *Tx) Delete(key string) error {
_, where := tx.checkExistence(key) // read/write-set の確認だけにする
if where == Deleted {
return errors.New("key doesn't exist")
}
v := Version{
key: key,
value: "",
wTs: tx.ts,
rTs: tx.ts,
prev: nil,
deleted: true,
}
tx.writeSet[key] = append(tx.writeSet[key], &Operation{cmd: DELETE, version: &v})
return nil
}
func (tx *Tx) Commit() error {
var err error
var sortedWriteSet []*Operation
for _, ops := range tx.writeSet {
for _, op := range ops {
sortedWriteSet = append(sortedWriteSet, op)
}
}
sort.SliceStable(sortedWriteSet, func(i, j int) bool { // prevent deadlock
return sortedWriteSet[i].version.key < sortedWriteSet[j].version.key
})
lockedRecord := make(map[string]*Record, len(sortedWriteSet))
// 一括ロック
for _, op := range sortedWriteSet {
switch op.cmd {
case INSERT:
v, exist := tx.db.index.Load(op.version.key)
if exist {
record := v.(*Record)
record.mu.Lock()
lockedRecord[op.version.key] = record
if !record.last.deleted {
err = errors.New("failed to commit INSERT")
goto unlock
}
continue
}
op.version.deleted = true
record := &Record{
key: op.version.key,
last: op.version,
mu: sync.Mutex{},
}
record.mu.Lock()
lockedRecord[op.version.key] = record
_, exist = tx.db.index.LoadOrStore(op.version.key, record)
if exist {
err = errors.New("failed to commit INSERT")
goto unlock
}
case UPDATE:
_, exist := lockedRecord[op.version.key]
if exist {
continue
}
v, exist := tx.db.index.Load(op.version.key)
if !exist {
err = errors.New("failed to commit UPDATE")
goto unlock
}
record := v.(*Record)
record.mu.Lock()
lockedRecord[op.version.key] = record
if record.last.deleted {
err = errors.New("failed to commit UPDATE")
goto unlock
}
if tx.ts < record.last.rTs {
err = errors.New("failed to commit UPDATE")
goto unlock
}
case DELETE:
_, exist := lockedRecord[op.version.key]
if exist {
continue
}
v, exist := tx.db.index.Load(op.version.key)
if !exist {
err = errors.New("failed to commit DELETE")
goto unlock
}
record := v.(*Record)
record.mu.Lock()
lockedRecord[op.version.key] = record
if record.last.deleted {
err = errors.New("failed to commit DELETE")
goto unlock
}
if tx.ts < record.last.rTs {
err = errors.New("failed to commit DELETE")
goto unlock
}
}
}
// write-set -> wal
if err := tx.SaveWal(); err != nil {
log.Println(err)
}
// write-set -> db-memory
for _, op := range sortedWriteSet {
switch op.cmd {
case INSERT:
record := lockedRecord[op.version.key]
record.last.deleted = false
case UPDATE:
record := lockedRecord[op.version.key]
op.version.prev = record.last
record.last = op.version
case DELETE:
record := lockedRecord[op.version.key]
op.version.prev = record.last
record.last = op.version
}
}
tx.db.versionGC(&sortedWriteSet)
// 一括アンロック
unlock:
for _, record := range lockedRecord {
record.mu.Unlock()
}
return err
}
func (tx *Tx) Abort() {
// delete read/write-set
tx.writeSet = make(WriteSet)
tx.readSet = make(ReadSet)
fmt.Println("Abort!")
}
func (tx *Tx) checkExistence(key string) (*Version, uint) {
// check write-set
for k, operations := range tx.writeSet {
for _, op := range operations {
if key == k {
if op.version.deleted {
return nil, Deleted
}
return op.version, InWriteSet
}
}
}
// check read-set
if version, exist := tx.readSet[key]; exist {
if version.deleted {
return nil, Deleted
}
return version, InReadSet
}
return nil, NotInRWSet
}
func (tx *Tx) SaveWal() error {
// make redo log
buf := make([]byte, 4096)
idx := uint(0) // 書き込み開始位置
for _, operations := range tx.writeSet {
for _, op := range operations {
checksum := crc32.ChecksumIEEE([]byte(op.version.key))
// serialize data
size := serialize(buf, idx, op, checksum)
idx += size
}
}
tx.db.walMu.Lock()
if _, err := tx.db.wALFile.Write(buf); err != nil {
return err
}
if err := tx.db.wALFile.Sync(); err != nil {
return err
}
tx.db.walMu.Unlock()
return nil
}
// read all data in db-memory
func readAll(index *sync.Map) {
fmt.Println("key | value")
fmt.Println("----------------------------")
index.Range(func(k, v interface{}) bool {
key := k.(string)
record := v.(*Record)
fmt.Printf("%s | %s\n", key, record.last.value)
return true
})
fmt.Println("----------------------------")
}