-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge PR: refactor orphan map (#1969)
* refactor orphan map (#1960) * refactor orphan map * func (oi *OrphanInfo) enqueueResult(res int64) * rename * rename * rm oi.orphanItemCacheQueue * rm oi.orphanItemCacheQueue * type NodeCache struct * simplify names * simplify names
- v2.0.0
- v1.8.1
- v1.8.0
- v1.7.9
- v1.7.8
- v1.7.7
- v1.7.6
- v1.7.5
- v1.7.4
- v1.7.3
- v1.7.2
- v1.7.1.1
- v1.7.1
- v1.7.0.5
- v1.7.0.4
- v1.7.0.3
- v1.7.0.2
- v1.7.0.1
- v1.7.0
- v1.7.0-browser
- v1.6.8.6
- v1.6.8.5
- v1.6.8.4
- v1.6.8.3
- v1.6.8.2
- v1.6.8.1
- v1.6.8
- v1.6.7.8
- v1.6.7.7
- v1.6.7.6
- v1.6.7.5
- v1.6.7.4
- v1.6.7.3
- v1.6.7.2
- v1.6.7.1
- v1.6.7
- v1.6.6
- v1.6.5.13
- v1.6.5.12
- v1.6.5.11
- v1.6.5.10
- v1.6.5.9
- v1.6.5.8
- v1.6.5.7
- v1.6.5.6
- v1.6.5.5
- v1.6.5.4
- v1.6.5.3
- v1.6.5.1
- v1.6.4
- v1.6.3
- v1.6.2.1
- v1.6.2
- v1.6.1.1
- v1.6.1
- v1.6.0
- v1.5.6
- v1.5.5
- v1.5.4
- v1.5.3
- v1.5.2
- v1.5.1
- v1.5.0
- v1.3.1
- v0.1.1
- v0.1.0
1 parent
5076a1f
commit c1d1ef0
Showing
10 changed files
with
303 additions
and
169 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package iavl | ||
|
||
import ( | ||
"github.com/tendermint/go-amino" | ||
"sync/atomic" | ||
) | ||
|
||
func (ndb *nodeDB) enqueueOrphanTask(version int64, rootHash []byte, newOrphans []*Node) { | ||
|
||
ndb.addOrphanItem(version, rootHash) | ||
|
||
task := func() { | ||
ndb.mtx.Lock() | ||
defer ndb.mtx.Unlock() | ||
ndb.saveNewOrphans(version, newOrphans, false) | ||
ndb.oi.removeOldOrphans(version) | ||
ndb.oi.enqueueResult(version) | ||
} | ||
|
||
ndb.oi.enqueueTask(task) | ||
} | ||
|
||
func (ndb *nodeDB) addOrphanItem(version int64, rootHash []byte) { | ||
ndb.mtx.Lock() | ||
defer ndb.mtx.Unlock() | ||
ndb.oi.addOrphanItem(version, rootHash) | ||
} | ||
|
||
func (ndb *nodeDB) saveNewOrphans(version int64, orphans []*Node, lock bool) { | ||
|
||
if orphans == nil { | ||
return | ||
} | ||
|
||
version-- | ||
ndb.log(IavlDebug, "saving orphan node to OrphanCache", "size", len(orphans)) | ||
atomic.AddInt64(&ndb.totalOrphanCount, int64(len(orphans))) | ||
|
||
if lock { | ||
ndb.mtx.Lock() | ||
defer ndb.mtx.Unlock() | ||
} | ||
|
||
ndb.oi.feedOrphansMap(version, orphans) | ||
for _, node := range orphans { | ||
ndb.oi.feedOrphanNodeCache(node) | ||
delete(ndb.prePersistNodeCache, amino.BytesToStr(node.hash)) | ||
node.leftNode = nil | ||
node.rightNode = nil | ||
} | ||
ndb.uncacheNodeRontine(orphans) | ||
} | ||
|
||
func (ndb *nodeDB) sanityCheckHandleOrphansResult(version int64) { | ||
ndb.oi.wait4Result(version) | ||
} | ||
|
||
func (ndb *nodeDB) findRootHash(version int64) (res []byte, found bool) { | ||
ndb.mtx.RLock() | ||
defer ndb.mtx.RUnlock() | ||
return ndb.oi.findRootHash(version) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package iavl | ||
|
||
import ( | ||
"fmt" | ||
"github.com/tendermint/go-amino" | ||
) | ||
|
||
type OrphanInfo struct { | ||
ndb *nodeDB | ||
orphanNodeCache map[string]*Node | ||
orphanItemMap map[int64]*orphanItem | ||
itemSize int | ||
|
||
orphanTaskChan chan func() | ||
resultChan chan int64 | ||
} | ||
|
||
type orphanItem struct { | ||
rootHash []byte | ||
orphans []*Node | ||
} | ||
|
||
func newOrphanInfo(ndb *nodeDB) *OrphanInfo { | ||
|
||
oi := &OrphanInfo{ | ||
ndb: ndb, | ||
orphanNodeCache: make(map[string]*Node), | ||
orphanItemMap: make(map[int64]*orphanItem), | ||
itemSize: HeightOrphansCacheSize, | ||
orphanTaskChan: make(chan func(), 1), | ||
resultChan: make(chan int64, 1), | ||
} | ||
|
||
oi.enqueueResult(0) | ||
go oi.handleOrphansRoutine() | ||
return oi | ||
} | ||
|
||
func (oi *OrphanInfo) enqueueResult(res int64) { | ||
oi.resultChan <- res | ||
} | ||
|
||
func (oi *OrphanInfo) enqueueTask(t func()) { | ||
oi.orphanTaskChan <- t | ||
} | ||
|
||
func (oi *OrphanInfo) handleOrphansRoutine() { | ||
for task := range oi.orphanTaskChan { | ||
task() | ||
} | ||
} | ||
|
||
func (oi *OrphanInfo) wait4Result(version int64) { | ||
|
||
version-- | ||
for versionCompleted := range oi.resultChan { | ||
if versionCompleted == version { | ||
break | ||
} else if versionCompleted == 0 { | ||
break | ||
} | ||
} | ||
} | ||
|
||
func (oi *OrphanInfo) addOrphanItem(version int64, rootHash []byte) { | ||
if rootHash == nil { | ||
rootHash = []byte{} | ||
} | ||
orphanObj := &orphanItem{ | ||
rootHash: rootHash, | ||
} | ||
_, ok := oi.orphanItemMap[version] | ||
if ok { | ||
panic(fmt.Sprintf("unexpected orphanItemMap, version: %d", version)) | ||
} | ||
oi.orphanItemMap[version] = orphanObj | ||
} | ||
|
||
|
||
func (oi *OrphanInfo) removeOldOrphans(version int64) { | ||
expiredVersion := version-int64(oi.itemSize) | ||
expiredItem, ok := oi.orphanItemMap[expiredVersion] | ||
if !ok { | ||
return | ||
} | ||
for _, node := range expiredItem.orphans { | ||
delete(oi.orphanNodeCache, amino.BytesToStr(node.hash)) | ||
} | ||
delete(oi.orphanItemMap, expiredVersion) | ||
} | ||
|
||
func (oi *OrphanInfo) feedOrphansMap(version int64, orphans []*Node) { | ||
v, ok := oi.orphanItemMap[version] | ||
if !ok { | ||
return | ||
} | ||
v.orphans = orphans | ||
} | ||
|
||
func (oi *OrphanInfo) feedOrphanNodeCache(node *Node) { | ||
oi.orphanNodeCache[string(node.hash)] = node | ||
} | ||
|
||
|
||
func (oi *OrphanInfo) getNodeFromOrphanCache(hash []byte) *Node { | ||
elem, ok := oi.orphanNodeCache[string(hash)] | ||
if ok { | ||
return elem | ||
} | ||
return nil | ||
} | ||
|
||
func (oi *OrphanInfo) orphanNodeCacheLen() int { | ||
return len(oi.orphanNodeCache) | ||
} | ||
|
||
func (oi *OrphanInfo) findRootHash(version int64) (res []byte, found bool) { | ||
v, ok := oi.orphanItemMap[version] | ||
if ok { | ||
res = v.rootHash | ||
found = true | ||
} | ||
return | ||
} |