Skip to content

Commit

Permalink
Merge pull request #5857 from filecoin-project/feat/lt/generic_lru_cache
Browse files Browse the repository at this point in the history
feat: update cache to the new generic version
  • Loading branch information
simlecode committed Mar 24, 2023
2 parents c7cd25d + 05f10f0 commit 0c81d3d
Show file tree
Hide file tree
Showing 11 changed files with 55 additions and 71 deletions.
3 changes: 1 addition & 2 deletions cmd/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"os"
"strconv"

"github.com/filecoin-project/venus/cmd/tablewriter"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
actorstypes "github.com/filecoin-project/go-state-types/actors"
Expand All @@ -20,6 +18,7 @@ import (
"github.com/multiformats/go-multiaddr"

"github.com/filecoin-project/venus/app/node"
"github.com/filecoin-project/venus/cmd/tablewriter"
"github.com/filecoin-project/venus/pkg/constants"
"github.com/filecoin-project/venus/venus-shared/actors/builtin"
"github.com/filecoin-project/venus/venus-shared/types"
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ require (
github.com/hako/durafmt v0.0.0-20200710122514-c0fb7b4da026
github.com/hannahhoward/go-pubsub v0.0.0-20200423002714-8d62886cc36e
github.com/hashicorp/go-multierror v1.1.1
github.com/hashicorp/golang-lru v0.5.4
github.com/hashicorp/golang-lru/v2 v2.0.2
github.com/howeyc/gopass v0.0.0-20190910152052-7cb4b85ec19c
github.com/ipfs-force-community/metrics v1.0.1-0.20211022060227-11142a08b729
github.com/ipfs/go-bitswap v0.10.2
Expand Down Expand Up @@ -121,6 +121,7 @@ require (
github.com/bytedance/sonic v1.8.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/goccy/go-json v0.10.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/ipfs/go-block-format v0.1.1 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,8 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
github.com/hashicorp/golang-lru v0.5.4 h1:YDjusn29QI/Das2iO9M0BHnIbxPeyuCHsjMW+lJfyTc=
github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
github.com/hashicorp/golang-lru/v2 v2.0.2 h1:Dwmkdr5Nc/oBiXgJS3CDHNhJtIHkuZ3DZF5twqnfBdU=
github.com/hashicorp/golang-lru/v2 v2.0.2/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
Expand Down
17 changes: 7 additions & 10 deletions pkg/beacon/drand.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import (
dlog "github.com/drand/drand/log"
"github.com/drand/kyber"
kzap "github.com/go-kit/kit/log/zap"
lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
"go.uber.org/zap/zapcore"

"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/network"

cfg "github.com/filecoin-project/venus/pkg/config"
"github.com/filecoin-project/venus/pkg/constants"
"github.com/filecoin-project/venus/venus-shared/types"
Expand All @@ -42,7 +43,7 @@ type DrandBeacon struct {
filGenTime uint64
filRoundTime uint64

localCache *lru.Cache
localCache *lru.Cache[uint64, *types.BeaconEntry]
}

// DrandHTTPClient interface overrides the user agent used by drand
Expand Down Expand Up @@ -84,7 +85,7 @@ func NewDrandBeacon(genTimeStamp, interval uint64, config cfg.DrandConf) (*Drand
return nil, fmt.Errorf("creating drand client: %v", err)
}

lc, err := lru.New(1024)
lc, err := lru.New[uint64, *types.BeaconEntry](1024)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -136,16 +137,12 @@ func (db *DrandBeacon) Entry(ctx context.Context, round uint64) <-chan Response
}

func (db *DrandBeacon) cacheValue(e types.BeaconEntry) {
db.localCache.Add(e.Round, e)
db.localCache.Add(e.Round, &e)
}

func (db *DrandBeacon) getCachedValue(round uint64) *types.BeaconEntry {
v, ok := db.localCache.Get(round)
if !ok {
return nil
}
e, _ := v.(types.BeaconEntry)
return &e
v, _ := db.localCache.Get(round)
return v
}

func (db *DrandBeacon) VerifyEntry(curr types.BeaconEntry, prev types.BeaconEntry) error {
Expand Down
41 changes: 14 additions & 27 deletions pkg/chain/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,7 @@ import (
"runtime/debug"
"sync"

"github.com/filecoin-project/pubsub"
blockstoreutil "github.com/filecoin-project/venus/venus-shared/blockstore"
lru "github.com/hashicorp/golang-lru"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
blockstore "github.com/ipfs/go-ipfs-blockstore"
cbor "github.com/ipfs/go-ipld-cbor"
blocks "github.com/ipfs/go-libipfs/blocks"
logging "github.com/ipfs/go-log/v2"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/ipld/go-car"
carutil "github.com/ipld/go-car/util"
carv2 "github.com/ipld/go-car/v2"
Expand All @@ -26,10 +18,18 @@ import (
cbg "github.com/whyrusleeping/cbor-gen"
"go.opencensus.io/trace"

"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
blockstore "github.com/ipfs/go-ipfs-blockstore"
cbor "github.com/ipfs/go-ipld-cbor"
blocks "github.com/ipfs/go-libipfs/blocks"
logging "github.com/ipfs/go-log/v2"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/network"
"github.com/filecoin-project/pubsub"
blockadt "github.com/filecoin-project/specs-actors/actors/util/adt"

"github.com/filecoin-project/venus/pkg/constants"
Expand All @@ -49,18 +49,10 @@ import (
"github.com/filecoin-project/venus/venus-shared/actors/builtin/reward"
"github.com/filecoin-project/venus/venus-shared/actors/builtin/verifreg"
"github.com/filecoin-project/venus/venus-shared/actors/policy"
blockstoreutil "github.com/filecoin-project/venus/venus-shared/blockstore"
"github.com/filecoin-project/venus/venus-shared/types"
)

// ErrNoMethod is returned by Get when there is no method signature (eg, transfer).
var ErrNoMethod = errors.New("no method")

// ErrNoActorImpl is returned by Get when the actor implementation doesn't exist, eg
// the actor address is an empty actor, an address that has received a transfer of FIL
// but hasn't yet been upgraded to an account actor. (The actor implementation might
// also genuinely be missing, which is not expected.)
var ErrNoActorImpl = errors.New("no actor implementation")

// GenesisKey is the key at which the genesis Cid is written in the datastore.
var GenesisKey = datastore.NewKey("/consensus/genesisCid")

Expand Down Expand Up @@ -92,10 +84,6 @@ type TSState struct {
Receipts cid.Cid
}

func ActorStore(ctx context.Context, bs blockstore.Blockstore) adt.Store {
return adt.WrapStore(ctx, cbor.NewCborStore(bs))
}

// Store is a generic implementation of the Store interface.
// It works(tm) for now.
type Store struct {
Expand Down Expand Up @@ -139,7 +127,7 @@ type Store struct {
reorgCh chan reorg
reorgNotifeeCh chan ReorgNotifee

tsCache *lru.ARCCache
tsCache *lru.ARCCache[types.TipSetKey, *types.TipSet]
}

// NewStore constructs a new default store.
Expand All @@ -148,7 +136,7 @@ func NewStore(chainDs repo.Datastore,
genesisCid cid.Cid,
circulatiingSupplyCalculator ICirculatingSupplyCalcualtor,
) *Store {
tsCache, _ := lru.NewARC(DefaultTipsetLruCacheSize)
tsCache, _ := lru.NewARC[types.TipSetKey, *types.TipSet](DefaultTipsetLruCacheSize)
store := &Store{
stateAndBlockSource: cbor.NewCborStore(bsstore),
ds: chainDs,
Expand Down Expand Up @@ -330,9 +318,8 @@ func (store *Store) GetTipSet(ctx context.Context, key types.TipSetKey) (*types.
return store.GetHead(), nil
}

val, has := store.tsCache.Get(key)
if has {
return val.(*types.TipSet), nil
if val, has := store.tsCache.Get(key); has {
return val, nil
}

cids := key.Cids()
Expand Down
9 changes: 4 additions & 5 deletions pkg/consensus/block_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ import (

"github.com/Gurpartap/async"
"github.com/hashicorp/go-multierror"
lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/multiformats/go-varint"
"github.com/pkg/errors"
cbg "github.com/whyrusleeping/cbor-gen"
"go.opencensus.io/trace"
Expand All @@ -24,8 +25,6 @@ import (
builtintypes "github.com/filecoin-project/go-state-types/builtin"
acrypto "github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/go-state-types/network"
"github.com/multiformats/go-varint"

blockadt "github.com/filecoin-project/specs-actors/actors/util/adt"
"github.com/filecoin-project/specs-actors/v7/actors/runtime/proof"

Expand Down Expand Up @@ -79,7 +78,7 @@ type BlockValidator struct {
// gasprice for vm
gasPirceSchedule *gas.PricesSchedule
// cache for validate block
validateBlockCache *lru.ARCCache
validateBlockCache *lru.ARCCache[cid.Cid, struct{}]

Stmgr StateTransformer
}
Expand All @@ -98,7 +97,7 @@ func NewBlockValidator(tv TicketValidator,
config *config.NetworkParamsConfig,
gasPirceSchedule *gas.PricesSchedule,
) *BlockValidator {
validateBlockCache, _ := lru.NewARC(2048)
validateBlockCache, _ := lru.NewARC[cid.Cid, struct{}](2048)
return &BlockValidator{
tv: tv,
bstore: bstore,
Expand Down
18 changes: 9 additions & 9 deletions pkg/events/message_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@ import (
"context"
"sync"

"github.com/filecoin-project/venus/venus-shared/types"

lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/ipfs/go-cid"

"github.com/filecoin-project/venus/venus-shared/types"
)

type messageCache struct {
api IEvent

blockMsgLk sync.Mutex
blockMsgCache *lru.ARCCache
blockMsgCache *lru.ARCCache[cid.Cid, *types.BlockMessages]
}

func newMessageCache(api IEvent) *messageCache {
blsMsgCache, _ := lru.NewARC(500)
blsMsgCache, _ := lru.NewARC[cid.Cid, *types.BlockMessages](500)

return &messageCache{
api: api,
Expand All @@ -30,14 +30,14 @@ func (c *messageCache) ChainGetBlockMessages(ctx context.Context, blkCid cid.Cid
c.blockMsgLk.Lock()
defer c.blockMsgLk.Unlock()

msgsI, ok := c.blockMsgCache.Get(blkCid)
msgs, ok := c.blockMsgCache.Get(blkCid)
var err error
if !ok {
msgsI, err = c.api.ChainGetBlockMessages(ctx, blkCid)
msgs, err = c.api.ChainGetBlockMessages(ctx, blkCid)
if err != nil {
return nil, err
}
c.blockMsgCache.Add(blkCid, msgsI)
c.blockMsgCache.Add(blkCid, msgs)
}
return msgsI.(*types.BlockMessages), nil
return msgs, nil
}
11 changes: 6 additions & 5 deletions pkg/messagepool/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import (
"math/rand"
"sort"

lru "github.com/hashicorp/golang-lru/v2"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
builtin2 "github.com/filecoin-project/go-state-types/builtin"
"github.com/filecoin-project/go-state-types/exitcode"
lru "github.com/hashicorp/golang-lru"

builtin2 "github.com/filecoin-project/go-state-types/builtin"
"github.com/filecoin-project/venus/pkg/constants"
"github.com/filecoin-project/venus/pkg/fork"
"github.com/filecoin-project/venus/venus-shared/actors/builtin"
Expand All @@ -27,7 +28,7 @@ const MinGasPremium = 100e3
// const MaxSpendOnFeeDenom = 100

type GasPriceCache struct {
c *lru.TwoQueueCache
c *lru.TwoQueueCache[types.TipSetKey, []GasMeta]
}

type GasMeta struct {
Expand All @@ -37,7 +38,7 @@ type GasMeta struct {

func NewGasPriceCache() *GasPriceCache {
// 50 because we usually won't access more than 40
c, err := lru.New2Q(50)
c, err := lru.New2Q[types.TipSetKey, []GasMeta](50)
if err != nil {
// err only if parameter is bad
panic(err)
Expand All @@ -51,7 +52,7 @@ func NewGasPriceCache() *GasPriceCache {
func (g *GasPriceCache) GetTSGasStats(ctx context.Context, provider Provider, ts *types.TipSet) ([]GasMeta, error) {
i, has := g.c.Get(ts.Key())
if has {
return i.([]GasMeta), nil
return i, nil
}

var prices []GasMeta
Expand Down
19 changes: 7 additions & 12 deletions pkg/messagepool/messagepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ import (
"sync"
"time"

lps "github.com/filecoin-project/pubsub"
"github.com/hashicorp/go-multierror"
lru "github.com/hashicorp/golang-lru"
lru "github.com/hashicorp/golang-lru/v2"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
Expand All @@ -28,6 +27,7 @@ import (
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/crypto"
lps "github.com/filecoin-project/pubsub"

"github.com/filecoin-project/venus/pkg/chain"
"github.com/filecoin-project/venus/pkg/config"
Expand Down Expand Up @@ -162,15 +162,15 @@ type MessagePool struct {
// pruneCooldown is a channel used to allow a cooldown time between prunes
pruneCooldown chan struct{}

blsSigCache *lru.TwoQueueCache
blsSigCache *lru.TwoQueueCache[cid.Cid, crypto.Signature]

changes *lps.PubSub

localMsgs datastore.Datastore

netName string

sigValCache *lru.TwoQueueCache
sigValCache *lru.TwoQueueCache[string, struct{}]

evtTypes [3]journal.EventType
journal journal.Journal
Expand Down Expand Up @@ -388,8 +388,8 @@ func New(ctx context.Context,
netName string,
j journal.Journal,
) (*MessagePool, error) {
cache, _ := lru.New2Q(constants.BlsSignatureCacheSize)
verifcache, _ := lru.New2Q(constants.VerifSigCacheSize)
cache, _ := lru.New2Q[cid.Cid, crypto.Signature](constants.BlsSignatureCacheSize)
verifcache, _ := lru.New2Q[string, struct{}](constants.VerifSigCacheSize)

cfg, err := loadConfig(ctx, ds)
if err != nil {
Expand Down Expand Up @@ -1515,13 +1515,8 @@ func (mp *MessagePool) MessagesForBlocks(ctx context.Context, blks []*types.Bloc
}

func (mp *MessagePool) RecoverSig(msg *types.Message) *types.SignedMessage {
val, ok := mp.blsSigCache.Get(msg.Cid())
if !ok {
return nil
}
sig, ok := val.(crypto.Signature)
sig, ok := mp.blsSigCache.Get(msg.Cid())
if !ok {
log.Errorf("value in signature cache was not a signature (got %T)", val)
return nil
}

Expand Down
1 change: 1 addition & 0 deletions venus-devtool/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/hashicorp/golang-lru/v2 v2.0.2 // indirect
github.com/icza/backscanner v0.0.0-20210726202459-ac2ffc679f94 // indirect
github.com/ipfs/bbloom v0.0.4 // indirect
github.com/ipfs/go-block-format v0.1.1 // indirect
Expand Down
Loading

0 comments on commit 0c81d3d

Please sign in to comment.