Skip to content

cache: add cache package and AddressCache for dcrpg #1051

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 6 commits into from
Feb 28, 2019

Conversation

chappjc
Copy link
Member

@chappjc chappjc commented Feb 27, 2019

Add a new cache package, starting with addresscache.go, which includes
AddressCache and AddressCacheItem.
Use AddressCache in ChainDB.
Remove DevFundBalance since it benefits from the general AddressCache.
Remove addressCounts since AddressCache includes balance.
To update an address' data, add RetrieveAllMainchainAddressTxns and
RetrieveAllMainchainAddressMergedTxns, which do not use offset or N.
Also cache transaction view type row counts via the new
(*ChainDB).CountTransactions function, which use cache or DB queries to
get the row counts for any of the supported txn view types.

Also:

  • Use vouts table primary key in SelectAddressUnspentWithTxn.
  • Avoid height query in AddressUTXO, using latest stored height instead.
  • fix: RetrieveAddressUTXOs forgot the block time
  • Rename AddrTxnType to AddrTxnViewType.
  • Remove unnecessary TimeDefs in scanAddressMergedRows and
    scanAddressQueryRows.
  • Add Height() int64 to explorer/explorerDataSource interface to use the
    no-query getter. This speeds up BlockHashPathOrIndexCtx middleware.

@chappjc chappjc changed the title cache: add cache package and AddressCache for dcrpg [WIP] cache: add cache package and AddressCache for dcrpg Feb 27, 2019
@chappjc chappjc changed the title [WIP] cache: add cache package and AddressCache for dcrpg cache: add cache package and AddressCache for dcrpg Feb 27, 2019
@chappjc chappjc requested a review from buck54321 February 27, 2019 21:42
@chappjc chappjc force-pushed the insight-api-cache branch 3 times, most recently from ed32871 to 1ca4477 Compare February 28, 2019 14:44
Copy link
Member Author

@chappjc chappjc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annotated for review.

return
}

height := c.BlockData.ChainDB.Height()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no reason for DB queries when the best block info is stored in ChainDB.

}
}

// TryLock will attempt to obtain either an exclusive updating lock. Trylock
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will revise this comment

func (d *AddressCacheItem) Balance() (*dbtypes.AddressBalance, *BlockID) {
d.RLock()
defer d.RUnlock()
if d.balance == nil {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important to return a nil *BlockID in the event of a cache miss as checking this pointer is now the method for detecting a cache miss.

DevAddress string
}

// NewAddressCache constructs a AddressCache.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need to doc the capacity arg

func (ac *AddressCache) Transactions(addr string, N, offset int64, txnType dbtypes.AddrTxnViewType) ([]*dbtypes.AddressRow, *BlockID, error) {
aci := ac.addressCacheItem(addr)
if aci == nil {
return nil, nil, nil /*fmt.Errorf("uninitialized address cache")*/
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can remove this comment. It's just a cache miss here, not an "uninitialized address cache".

@@ -2540,6 +2658,17 @@ func (pgb *ChainDB) StoreBlock(msgBlock *wire.MsgBlock, winningTickets []string,
numVouts = errStk.numVouts + errReg.numVouts
numAddresses = errStk.numAddresses + errReg.numAddresses

// Merge the affected addresses.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should explain this is for cache expiration

@@ -2578,9 +2707,9 @@ func (pgb *ChainDB) StoreBlock(msgBlock *wire.MsgBlock, winningTickets []string,
return
}

// If not in batch sync, lazy update the dev fund balance
// If not in batch sync, lazy update the dev fund balance.
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should update this comment too.

@@ -1585,12 +1616,11 @@ func scanAddressQueryRows(rows *sql.Rows, queryType int) (ids []uint64, addressR
for rows.Next() {
var id uint64
var addr dbtypes.AddressRow
var txHash sql.NullString
var blockTime dbtypes.TimeDef
var matchingTxHash sql.NullString
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

txHash is never nul or empty. matchingTxHash is often empty, but I'm not even sure it is allowed to be null. This is just to be safe.

addr.AtomsCredit = addr.Value
case debitQuery:
// addr.IsFunding == false
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can remove these two commented lines. IsFunding is set directly by rows.Scan

"", ExpStatusError)
return
}
maxHeight = exp.explorerSource.Height()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another unnecessary DB query.

Copy link
Member

@buck54321 buck54321 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm having trouble finding anything to complain about. Looks great.

use vouts primary key in SelectAddressUnspentWithTxn

avoid height query in AddressUTXO, using latest stored height instead

fix: RetrieveAddressUTXOs forgot the block time

rename AddrTxnType to AddrTxnViewType

add addresscache.go with AddressCache and AddressCacheItem

Use AddressCache in ChainDB.
Remove DevFundBalance since it benefits from the general AddressCache.
Remove addressCounts since AddressCache includes balance.
Remove unnecessary TimeDefs in scanAddressMergedRows and
scanAddressQueryRows.
To update an address' data, add RetrieveAllMainchainAddressTxns and
RetrieveAllMainchainAddressMergedTxns, which do not use offset or N.
Add Height() int64 to explorer/explorerDataSource interface to use the
no-query getter. This speeds up BlockHashPathOrIndexCtx middleware.

make cache package in db/cache

set capacity of cap, and respect it when adding items

When enforcing capacity, do not purge the project fund cache item.

remove redundant and conflicting functions
@chappjc chappjc merged commit 4432f59 into decred:master Feb 28, 2019
@chappjc chappjc deleted the insight-api-cache branch February 28, 2019 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants