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 authored 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.