-
Notifications
You must be signed in to change notification settings - Fork 180
/
nodedb_oec.go
375 lines (311 loc) · 9.39 KB
/
nodedb_oec.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
374
375
package iavl
import (
"bytes"
"container/list"
"encoding/binary"
"fmt"
cmap "github.com/orcaman/concurrent-map"
"time"
"github.com/go-errors/errors"
"github.com/okex/exchain/libs/system/trace"
dbm "github.com/okex/exchain/libs/tm-db"
"github.com/tendermint/go-amino"
)
const (
FlagIavlCacheInitRatio = "iavl-cache-init-ratio"
FlagIavlCommitAsyncNoBatch = "iavl-commit-async-no-batch"
)
var (
IavlCacheInitRatio float64 = 0
IavlCommitAsyncNoBatch bool = false
)
type tppItem struct {
nodeMap map[string]*Node
listItem *list.Element
}
func (ndb *nodeDB) SaveOrphans(batch dbm.Batch, version int64, orphans []*Node) {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()
toVersion := ndb.getPreviousVersion(version)
for _, node := range orphans {
ndb.log(IavlDebug, "SAVEORPHAN", "version", node.version, "toVersion", toVersion, "hash", amino.BytesHexStringer(node.hash))
ndb.saveOrphan(batch, node.hash, node.version, toVersion)
}
}
func (ndb *nodeDB) dbGet(k []byte) ([]byte, error) {
ndb.addDBReadCount()
ts := time.Now()
defer func() {
ndb.addDBReadTime(time.Now().Sub(ts).Nanoseconds())
}()
return ndb.db.Get(k)
}
func (ndb *nodeDB) makeNodeFromDbByHash(hash []byte) *Node {
k := ndb.nodeKey(hash)
ndb.addDBReadCount()
ts := time.Now()
defer func() {
ndb.addDBReadTime(time.Now().Sub(ts).Nanoseconds())
}()
v, err := ndb.db.GetUnsafeValue(k, func(buf []byte) (interface{}, error) {
if buf == nil {
panic(fmt.Sprintf("Value missing for hash %x corresponding to nodeKey %x", hash, ndb.nodeKey(hash)))
}
node, err := MakeNode(buf)
if err != nil {
panic(fmt.Sprintf("Error reading Node. bytes: %x, error: %v", buf, err))
}
return node, nil
})
if err != nil {
panic(fmt.Sprintf("can't get node %X: %v", hash, err))
}
return v.(*Node)
}
func (ndb *nodeDB) saveNodeToPrePersistCache(node *Node) {
if node.hash == nil {
panic("Expected to find node.hash, but none found.")
}
if node.persisted || node.prePersisted {
panic("Shouldn't be calling save on an already persisted node.")
}
node.prePersisted = true
ndb.mtx.Lock()
ndb.prePersistNodeCache[string(node.hash)] = node
ndb.mtx.Unlock()
}
func (ndb *nodeDB) persistTpp(event *commitEvent, writeToDB bool, trc *trace.Tracer) {
batch := event.batch
tpp := event.tpp
trc.Pin("batchSet-node")
if !writeToDB {
for _, node := range tpp {
ndb.batchSet(node, batch)
}
} else {
for _, node := range tpp {
err := ndb.saveNodeToDB(node)
if err != nil {
panic(err)
}
}
}
ndb.state.increasePersistedCount(len(tpp))
ndb.addDBWriteCount(int64(len(tpp)))
trc.Pin("batchSet-fss")
if err := ndb.saveFastNodeVersion(batch, event.fnc, event.version); err != nil {
panic(err)
}
trc.Pin("batchCommit")
if err := ndb.Commit(batch); err != nil {
panic(err)
}
ndb.asyncPersistTppFinised(event, trc)
}
func (ndb *nodeDB) asyncPersistTppStart(version int64) (map[string]*Node, *fastNodeChanges) {
ndb.log(IavlDebug, "moving prePersistCache to tempPrePersistCache", "size", len(ndb.prePersistNodeCache))
ndb.mtx.Lock()
tpp := ndb.prePersistNodeCache
ndb.prePersistNodeCache = make(map[string]*Node, len(tpp))
ndb.tpp.pushToTpp(version, tpp)
var tempPersistFastNode *fastNodeChanges
if GetEnableFastStorage() {
tempPersistFastNode = ndb.prePersistFastNode
ndb.tpfv.add(version, tempPersistFastNode)
ndb.prePersistFastNode = newFastNodeChanges()
}
ndb.mtx.Unlock()
for _, node := range tpp {
if node.persisted || !node.prePersisted {
panic("unexpected node state")
}
node.persisted = true
}
return tpp, tempPersistFastNode
}
func (ndb *nodeDB) asyncPersistTppFinised(event *commitEvent, trc *trace.Tracer) {
version := event.version
iavlHeight := event.iavlHeight
nodeNum := ndb.tpp.getTppNodesNum()
ndb.tpp.removeFromTpp(version)
ndb.tpfv.remove(event.version)
var fssAddNum, fssDelNum int
if event.fnc != nil {
fssAddNum = len(event.fnc.additions)
fssDelNum = len(event.fnc.removals)
}
ndb.log(IavlInfo, "CommitSchedule", "Height", version,
"Tree", ndb.name,
"IavlHeight", iavlHeight,
"NodeNum", nodeNum,
"tpp", len(event.tpp),
"fss-add", fssAddNum,
"fss-rm", fssDelNum,
"trc", trc.Format())
}
func nodeToDBValue(node *Node) []byte {
if node.hash == nil {
panic("Expected to find node.hash, but none found.")
}
if !node.persisted {
panic("Should set node.persisted to true before batchSet.")
}
if !node.prePersisted {
panic("Should be calling save on an prePersisted node.")
}
// Save node bytes to db.
var buf bytes.Buffer
buf.Grow(node.aminoSize())
if err := node.writeBytesToBuffer(&buf); err != nil {
panic(err)
}
return buf.Bytes()
}
// SaveNode saves a node to disk.
func (ndb *nodeDB) batchSet(node *Node, batch dbm.Batch) {
nodeKey := ndb.nodeKey(node.hash)
nodeValue := nodeToDBValue(node)
batch.Set(nodeKey, nodeValue)
ndb.state.increasePersistedSize(len(nodeKey) + len(nodeValue))
ndb.log(IavlDebug, "BATCH SAVE", "hash", node.hash)
//node.persisted = true // move to function MovePrePersistCacheToTempCache
}
// SaveNode saves a node to disk.
func (ndb *nodeDB) saveNodeToDB(node *Node) error {
nodeKey := ndb.nodeKey(node.hash)
nodeValue := nodeToDBValue(node)
err := ndb.db.Set(nodeKey, nodeValue)
ndb.state.increasePersistedSize(len(nodeKey) + len(nodeValue))
ndb.log(IavlDebug, "SAVE NODE", "hash", node.hash)
//node.persisted = true // move to function MovePrePersistCacheToTempCache
return err
}
func (ndb *nodeDB) NewBatch() dbm.Batch {
return ndb.db.NewBatch()
}
// Saves orphaned nodes to disk under a special prefix.
// version: the new version being saved.
// orphans: the orphan nodes created since version-1
func (ndb *nodeDB) saveCommitOrphans(batch dbm.Batch, version int64, orphans []commitOrphan, writeToDB bool) error {
ndb.log(IavlDebug, "saving committed orphan node log to disk")
toVersion := ndb.getPreviousVersion(version)
if !writeToDB {
for _, orphan := range orphans {
// ndb.log(IavlDebug, "SAVEORPHAN", "from", orphan.Version, "to", toVersion, "hash", amino.BytesHexStringer(orphan.NodeHash))
ndb.saveOrphan(batch, orphan.NodeHash, orphan.Version, toVersion)
}
} else {
for _, orphan := range orphans {
// ndb.log(IavlDebug, "SAVEORPHAN", "from", orphan.Version, "to", toVersion, "hash", amino.BytesHexStringer(orphan.NodeHash))
err := ndb.saveOrphanToDB(orphan.NodeHash, orphan.Version, toVersion)
if err != nil {
return err
}
}
}
return nil
}
func (ndb *nodeDB) getRootWithCacheAndDB(version int64) ([]byte, error) {
if EnableAsyncCommit {
root, ok := ndb.findRootHash(version)
if ok {
return root, nil
}
}
return ndb.getRoot(version)
}
// DeleteVersion deletes a tree version from disk.
func (ndb *nodeDB) DeleteVersion(batch dbm.Batch, version int64, checkLatestVersion bool) error {
err := ndb.checkoutVersionReaders(version)
if err != nil {
return err
}
ndb.deleteOrphans(batch, version)
ndb.deleteRoot(batch, version, checkLatestVersion)
return nil
}
func (ndb *nodeDB) checkoutVersionReaders(version int64) error {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()
if ndb.versionReaders[version] > 0 {
return errors.Errorf("unable to delete version %v, it has %v active readers", version, ndb.versionReaders[version])
}
return nil
}
func (ndb *nodeDB) syncUnCacheNode(hash []byte) {
ndb.mtx.Lock()
defer ndb.mtx.Unlock()
ndb.uncacheNode(hash)
}
// orphanKeyFast generates key for an orphan node,
// result must be equal to orphanKeyFormat.Key(toVersion, fromVersion, hash)
func orphanKeyFast(fromVersion, toVersion int64, hash []byte) []byte {
key := make([]byte, orphanKeyFormat.length)
key[0] = orphanKeyFormat.prefix
n := 1
if orphanKeyFormat.layout[0] != int64Size {
panic("unexpected layout")
}
binary.BigEndian.PutUint64(key[n:n+int64Size], uint64(toVersion))
n += int64Size
if orphanKeyFormat.layout[1] != int64Size {
panic("unexpected layout")
}
binary.BigEndian.PutUint64(key[n:n+int64Size], uint64(fromVersion))
n += int64Size
hashLen := orphanKeyFormat.layout[2]
if hashLen < len(hash) {
panic("hash is too long")
}
copy(key[n+hashLen-len(hash):n+hashLen], hash)
return key
}
func (ndb *nodeDB) cacheNode(node *Node) {
ndb.nc.cache(node)
}
func (ndb *nodeDB) cacheWithKey(key string, node *Node) {
ndb.nc.cacheWithKey(key, node)
}
func (ndb *nodeDB) uncacheNode(hash []byte) {
ndb.nc.uncache(hash)
}
func (ndb *nodeDB) getFastNodeFromCache(key []byte) (*FastNode, bool) {
node := ndb.fastNodeCache.get(key, true)
return node, node != nil
}
func (ndb *nodeDB) uncacheFastNode(key []byte) {
ndb.fastNodeCache.uncache(key)
}
// Add a node to the cache and pop the least recently used node if we've
// reached the cache size limit.
func (ndb *nodeDB) cacheFastNode(node *FastNode) {
ndb.fastNodeCache.cache(node)
}
func (ndb *nodeDB) getNodeFromCache(hash []byte, promoteRecentNode bool) (n *Node) {
return ndb.nc.get(hash, promoteRecentNode)
}
func (ndb *nodeDB) uncacheNodeRontine(n []*Node) {
for _, node := range n {
ndb.uncacheNode(node.hash)
}
}
func (ndb *nodeDB) initPreWriteCache() {
if ndb.preWriteNodeCache == nil {
ndb.preWriteNodeCache = cmap.New()
}
}
func (ndb *nodeDB) cacheNodeToPreWriteCache(n *Node) {
ndb.preWriteNodeCache.Set(string(n.hash), n)
}
func (ndb *nodeDB) finishPreWriteCache() {
ndb.preWriteNodeCache.IterCb(func(key string, v interface{}) {
ndb.cacheWithKey(key, v.(*Node))
})
ndb.preWriteNodeCache = nil
}
func (ndb *nodeDB) prePersistNodeCacheLen() (l int) {
ndb.mtx.Lock()
l = len(ndb.prePersistNodeCache)
ndb.mtx.Unlock()
return
}