/
trunk.go
170 lines (145 loc) · 4.03 KB
/
trunk.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
package store
import (
"sync/atomic"
"github.com/dterei/gotsc"
"github.com/smartbch/moeingads/datatree"
"github.com/smartbch/moeingads/store/types"
adstypes "github.com/smartbch/moeingads/types"
)
var PhaseTrunkTime, PhaseEndWriteTime, tscOverhead uint64 //nolint:unused
func init() {
tscOverhead = gotsc.TSCOverhead()
}
// TrunkStore use a temporal cache to buffer the written data from
// successive transactions, such that the latter transactions can read
// the data which were written by previous transactions.
// When these transactions are running, isWriting is always 0.
// When a transaction is writting back to TrunkStore, isWriting is setting to 1.
// When the writting process is finished, isWritting is setting back to 0.
// The client code should manages the transactions to make sure their writing-back
// processes are performed in serial.
// When a TrunkStore closes after servicing all the transactions, it may write
// the temporal cache's content to the underlying RootStore, if isReadOnly==false.
// A transaction usually access a TrunkStore through a RabbitStore.
type TrunkStore struct {
cache *CacheStore
root types.RootStoreI
isWriting int64
isReadOnly bool
}
func (ts *TrunkStore) Get(key []byte) []byte {
if atomic.LoadInt64(&ts.isWriting) != 0 {
panic("Is Writing")
}
res, status := ts.cache.Get(key)
switch status {
case types.JustDeleted:
return nil
case types.Hit:
newRes := make([]byte, len(res))
copy(newRes, res)
return newRes
case types.Missed:
return ts.root.Get(key)
default:
panic("Invalid Status")
}
}
func (ts *TrunkStore) RLock() {
ts.root.RLock()
}
func (ts *TrunkStore) RUnlock() {
ts.root.RUnlock()
}
func (ts *TrunkStore) CacheSize() int {
return ts.cache.Size()
}
func (ts *TrunkStore) GetAtHeight(key []byte, height uint64) []byte {
return ts.root.GetAtHeight(key, height)
}
func (ts *TrunkStore) PrepareForUpdate(key []byte) {
if atomic.LoadInt64(&ts.isWriting) != 0 {
panic("Is Writing")
}
ts.root.PrepareForUpdate(key)
}
func (ts *TrunkStore) PrepareForDeletion(key []byte) {
if atomic.LoadInt64(&ts.isWriting) != 0 {
panic("Is Writing")
}
ts.root.PrepareForDeletion(key)
}
func (ts *TrunkStore) Update(updater func(db types.SetDeleter)) {
if atomic.LoadInt64(&ts.isWriting) != 0 {
panic("Is Writing")
}
updater(ts.cache)
}
func (ts *TrunkStore) writeBack() {
//@ start := gotsc.BenchStart()
if atomic.AddInt64(&ts.isWriting, 1) != 1 {
panic("Conflict During Writing")
}
ts.root.BeginWrite()
datatree.ParallelRun(adstypes.ShardCount, func(shardID int) {
ts.cache.ScanAllEntriesInShard(shardID, func(key, value []byte, isDeleted bool) {
if isDeleted {
ts.root.Delete(key)
} else {
ts.root.Set(key, value)
}
})
})
//@ PhaseTrunkTime += gotsc.BenchEnd() - start - tscOverhead
//@ start = gotsc.BenchStart()
ts.root.EndWrite()
//@ PhaseEndWriteTime += gotsc.BenchEnd() - start - tscOverhead
if atomic.AddInt64(&ts.isWriting, -1) != 0 {
panic("Conflict During Writing")
}
}
func (ts *TrunkStore) WriteBack() {
ts.root.Lock()
ts.writeBack()
ts.root.Unlock()
}
func (ts *TrunkStore) Close(writeBack bool) {
if ts.isReadOnly && writeBack {
panic("A Readonly TrunkStore cannot be written back")
}
if writeBack {
ts.WriteBack()
}
ts.cache.Close()
ts.cache = nil
ts.root = nil
}
func (ts *TrunkStore) ActiveCount() int {
return ts.root.ActiveCount()
}
func (ts *TrunkStore) GetCacheContent() map[string]string {
return ts.cache.m
}
func SyncUpdateTo(m map[string]string, root types.RootStoreI) {
cache := &CacheStore{m: m}
datatree.ParallelRun(adstypes.ShardCount, func(shardID int) {
cache.ScanAllEntriesInShard(shardID, func(key, value []byte, isDeleted bool) {
if isDeleted {
root.PrepareForDeletion(key)
} else {
root.PrepareForUpdate(key)
}
})
})
root.BeginWrite()
datatree.ParallelRun(adstypes.ShardCount, func(shardID int) {
cache.ScanAllEntriesInShard(shardID, func(key, value []byte, isDeleted bool) {
if isDeleted {
root.Delete(key)
} else {
root.Set(key, value)
}
})
})
root.EndWrite()
}