Skip to content

Add block cache pruning #126

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sqlchain/blockindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

type blockNode struct {
parent *blockNode
block *types.Block // TODO(leventeliu): cleanup history blocks to release memory.
block *types.Block
hash hash.Hash
height int32 // height is the chain height of the head
count int32 // count counts the blocks (except genesis) at this head
Expand Down
29 changes: 29 additions & 0 deletions sqlchain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ import (
"github.com/syndtr/goleveldb/leveldb/util"
)

const (
minBlockCacheTTL = int32(100)
)

var (
metaState = [4]byte{'S', 'T', 'A', 'T'}
metaBlockIndex = [4]byte{'B', 'L', 'C', 'K'}
Expand Down Expand Up @@ -359,6 +363,7 @@ func LoadChain(c *Config) (chain *Chain, err error) {
st.node = last
chain.rt.setHead(st)
chain.st.InitTx(id)
chain.pruneBlockCache()

// Read queries and rebuild memory index
respIter := chain.tdb.NewIterator(util.BytesPrefix(metaResponseIndex[:]), nil)
Expand Down Expand Up @@ -695,6 +700,7 @@ func (c *Chain) syncHead() {
// runCurrentTurn does the check and runs block producing if its my turn.
func (c *Chain) runCurrentTurn(now time.Time) {
defer func() {
c.pruneBlockCache()
c.rt.setNextTurn()
c.qi.advanceBarrier(c.rt.getMinValidHeight())
c.ai.advance(c.rt.getMinValidHeight())
Expand Down Expand Up @@ -1187,6 +1193,11 @@ func (c *Chain) getBilling(low, high int32) (req *pt.BillingRequest, err error)
}

for ; n != nil && n.height >= low; n = n.parent {
// TODO(leventeliu): block maybe released, use persistence version in this case.
if n.block == nil {
continue
}

if lowBlock == nil {
lowBlock = n.block
}
Expand Down Expand Up @@ -1521,3 +1532,21 @@ func (c *Chain) register(ack *types.SignedAckHeader) (err error) {
func (c *Chain) remove(ack *types.SignedAckHeader) (err error) {
return c.ai.remove(c.rt.getHeightFromTime(ack.SignedRequestHeader().Timestamp), ack)
}

func (c *Chain) pruneBlockCache() {
var (
head = c.rt.getHead().node
lastCnt int32
)
if head == nil {
return
}
lastCnt = head.count - c.rt.blockCacheTTL
// Move to last count position
for ; head != nil && head.count > lastCnt; head = head.parent {
}
// Prune block references
for ; head != nil && head.block != nil; head = head.parent {
head.block = nil
}
}
2 changes: 2 additions & 0 deletions sqlchain/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type Config struct {
// QueryTTL sets the unacknowledged query TTL in block periods.
QueryTTL int32

BlockCacheTTL int32

// DBAccount info
TokenType pt.TokenType
GasPrice uint64
Expand Down
18 changes: 13 additions & 5 deletions sqlchain/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type runtime struct {
tick time.Duration
// queryTTL sets the unacknowledged query TTL in block periods.
queryTTL int32
// blockCacheTTL sets the cached block numbers.
blockCacheTTL int32
// muxServer is the multiplexing service of sql-chain PRC.
muxService *MuxService
// price sets query price in gases.
Expand Down Expand Up @@ -85,11 +87,17 @@ type runtime struct {
// newRunTime returns a new sql-chain runtime instance with the specified config.
func newRunTime(c *Config) (r *runtime) {
r = &runtime{
stopCh: make(chan struct{}),
databaseID: c.DatabaseID,
period: c.Period,
tick: c.Tick,
queryTTL: c.QueryTTL,
stopCh: make(chan struct{}),
databaseID: c.DatabaseID,
period: c.Period,
tick: c.Tick,
queryTTL: c.QueryTTL,
blockCacheTTL: func() int32 {
if c.BlockCacheTTL < minBlockCacheTTL {
return minBlockCacheTTL
}
return c.BlockCacheTTL
}(),
muxService: c.MuxService,
price: c.Price,
producingReward: c.ProducingReward,
Expand Down