Skip to content
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

[actpool] persistent storage for actions #4366

Merged
merged 6 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
30 changes: 15 additions & 15 deletions actpool/blobstore.go → actpool/actionstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ const (
)

type (
blobStore struct {
actionStore struct {
lifecycle.Readiness
config blobStoreConfig // Configuration for the blob store
config actionStoreConfig // Configuration for the blob store

store billy.Database // Persistent data store for the tx
stored uint64 // Useful data size of all transactions on disk
Expand All @@ -53,7 +53,7 @@ type (
encode encodeAction // Encoder for the tx
decode decodeAction // Decoder for the tx
}
blobStoreConfig struct {
actionStoreConfig struct {
Datadir string `yaml:"datadir"` // Data directory containing the currently executable blobs
Datacap uint64 `yaml:"datacap"` // Soft-cap of database storage (hard cap is larger due to overhead)
}
Expand All @@ -68,24 +68,24 @@ var (
errStoreNotOpen = fmt.Errorf("blob store is not open")
)

var defaultBlobStoreConfig = blobStoreConfig{
Datadir: "blobpool",
var defaultActionStoreConfig = actionStoreConfig{
Datadir: "actionstore",
Datacap: 10 * 1024 * 1024 * 1024,
Copy link
Member

Choose a reason for hiding this comment

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

is this 10GB?

Copy link
Member Author

@envestcc envestcc Sep 11, 2024

Choose a reason for hiding this comment

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

ok, maybe 1GB(around 5000 blob tx) is enough

}

func newBlobStore(cfg blobStoreConfig, encode encodeAction, decode decodeAction) (*blobStore, error) {
func newBlobStore(cfg actionStoreConfig, encode encodeAction, decode decodeAction) (*actionStore, error) {
envestcc marked this conversation as resolved.
Show resolved Hide resolved
if len(cfg.Datadir) == 0 {
return nil, errors.New("datadir is empty")
}
return &blobStore{
return &actionStore{
config: cfg,
lookup: make(map[hash.Hash256]uint64),
encode: encode,
decode: decode,
}, nil
}

func (s *blobStore) Open(onData onAction) error {
func (s *actionStore) Open(onData onAction) error {
s.lock.Lock()
defer s.lock.Unlock()

Expand Down Expand Up @@ -131,7 +131,7 @@ func (s *blobStore) Open(onData onAction) error {
return s.TurnOn()
}

func (s *blobStore) Close() error {
func (s *actionStore) Close() error {
s.lock.Lock()
defer s.lock.Unlock()

Expand All @@ -141,7 +141,7 @@ func (s *blobStore) Close() error {
return s.store.Close()
}

func (s *blobStore) Get(hash hash.Hash256) (*action.SealedEnvelope, error) {
func (s *actionStore) Get(hash hash.Hash256) (*action.SealedEnvelope, error) {
if !s.IsReady() {
return nil, errors.Wrap(errStoreNotOpen, "")
}
Expand All @@ -159,7 +159,7 @@ func (s *blobStore) Get(hash hash.Hash256) (*action.SealedEnvelope, error) {
return s.decode(blob)
}

func (s *blobStore) Put(act *action.SealedEnvelope) error {
func (s *actionStore) Put(act *action.SealedEnvelope) error {
if !s.IsReady() {
return errors.Wrap(errStoreNotOpen, "")
}
Expand Down Expand Up @@ -189,7 +189,7 @@ func (s *blobStore) Put(act *action.SealedEnvelope) error {
return nil
}

func (s *blobStore) Delete(hash hash.Hash256) error {
func (s *actionStore) Delete(hash hash.Hash256) error {
if !s.IsReady() {
return errors.Wrap(errStoreNotOpen, "")
}
Expand All @@ -208,7 +208,7 @@ func (s *blobStore) Delete(hash hash.Hash256) error {
}

// Range iterates over all stored with hashes
func (s *blobStore) Range(fn func(hash.Hash256) bool) {
func (s *actionStore) Range(fn func(hash.Hash256) bool) {
if !s.IsReady() {
return
}
Expand All @@ -222,7 +222,7 @@ func (s *blobStore) Range(fn func(hash.Hash256) bool) {
}
}

func (s *blobStore) drop() {
func (s *actionStore) drop() {
h, ok := s.evict()
if !ok {
log.L().Debug("no worst action found")
Expand All @@ -241,7 +241,7 @@ func (s *blobStore) drop() {
}

// TODO: implement a proper eviction policy
func (s *blobStore) evict() (hash.Hash256, bool) {
func (s *actionStore) evict() (hash.Hash256, bool) {
for h := range s.lookup {
return h, true
}
Expand Down
4 changes: 2 additions & 2 deletions actpool/blobstore_test.go → actpool/actionstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (

func TestBlobStore(t *testing.T) {
r := require.New(t)
cfg := blobStoreConfig{
cfg := actionStoreConfig{
Datadir: t.TempDir(),
Datacap: 10 * 1024 * 1024,
}
Expand Down Expand Up @@ -83,7 +83,7 @@ func BenchmarkDatabase(b *testing.B) {
}

b.Run("billy-put", func(b *testing.B) {
cfg := blobStoreConfig{
cfg := actionStoreConfig{
Datadir: b.TempDir(),
Datacap: 1024,
}
Expand Down
90 changes: 89 additions & 1 deletion actpool/actpool.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"sort"
"sync"
"sync/atomic"
"time"

"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
Expand All @@ -28,6 +29,7 @@ import (
"github.com/iotexproject/iotex-core/blockchain/genesis"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/prometheustimer"
"github.com/iotexproject/iotex-core/pkg/routine"
"github.com/iotexproject/iotex-core/pkg/tracer"
)

Expand Down Expand Up @@ -103,6 +105,9 @@ type actPool struct {
senderBlackList map[string]bool
jobQueue []chan workerJob
worker []*queueWorker

store *actionStore // store is the persistent cache for actpool
storeSync *routine.RecurringTask // storeSync is the recurring task to sync actions from store to memory
}

// NewActPool constructs a new actpool
Expand Down Expand Up @@ -142,7 +147,7 @@ func NewActPool(g genesis.Genesis, sf protocol.StateReader, cfg Config, opts ...
return nil, err
}
ap.timerFactory = timerFactory

// TODO: move to Start
for i := 0; i < _numWorker; i++ {
ap.jobQueue[i] = make(chan workerJob, ap.cfg.WorkerBufferSize)
ap.worker[i] = newQueueWorker(ap, ap.jobQueue[i])
Expand All @@ -153,6 +158,33 @@ func NewActPool(g genesis.Genesis, sf protocol.StateReader, cfg Config, opts ...
return ap, nil
}

func (ap *actPool) Start(ctx context.Context) error {
// open action store and load all actions
if ap.store != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

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

early return

err := ap.store.Open(func(selp *action.SealedEnvelope) error {
return ap.add(ctx, selp)
})
if err != nil {
return err
}
return ap.storeSync.Start(ctx)
}
return nil
}

func (ap *actPool) Stop(ctx context.Context) error {
for i := 0; i < _numWorker; i++ {
if err := ap.worker[i].Stop(); err != nil {
return err
}
}
if ap.store != nil {
ap.storeSync.Stop(ctx)
return ap.store.Close()
}
return nil
}

func (ap *actPool) AddActionEnvelopeValidators(fs ...action.SealedEnvelopeValidator) {
ap.actionEnvelopeValidators = append(ap.actionEnvelopeValidators, fs...)
}
Expand Down Expand Up @@ -217,6 +249,10 @@ func (ap *actPool) PendingActionMap() map[string][]*action.SealedEnvelope {
}

func (ap *actPool) Add(ctx context.Context, act *action.SealedEnvelope) error {
return ap.add(ctx, act)
}

func (ap *actPool) add(ctx context.Context, act *action.SealedEnvelope) error {
ctx, span := tracer.NewSpan(ap.context(ctx), "actPool.Add")
defer span.End()
ctx = ap.context(ctx)
Expand Down Expand Up @@ -345,6 +381,16 @@ func (ap *actPool) GetUnconfirmedActs(addrStr string) []*action.SealedEnvelope {
func (ap *actPool) GetActionByHash(hash hash.Hash256) (*action.SealedEnvelope, error) {
act, ok := ap.allActions.Get(hash)
if !ok {
if ap.store != nil {
envestcc marked this conversation as resolved.
Show resolved Hide resolved
act, err := ap.store.Get(hash)
switch errors.Cause(err) {
case nil:
return act, nil
case errBlobNotFound:
default:
return nil, err
}
}
return nil, errors.Wrapf(action.ErrNotFound, "action hash %x does not exist in pool", hash)
}
return act.(*action.SealedEnvelope), nil
Expand Down Expand Up @@ -412,6 +458,21 @@ func (ap *actPool) validate(ctx context.Context, selp *action.SealedEnvelope) er
return nil
}

func (ap *actPool) removeReplacedActs(acts []*action.SealedEnvelope) {
Copy link
Member

Choose a reason for hiding this comment

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

this is same as removeInvalidActs, see comment below?

Copy link
Member Author

Choose a reason for hiding this comment

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

as discussed, will delete the removeReplacedActs

for _, act := range acts {
hash, err := act.Hash()
if err != nil {
log.L().Debug("Skipping action due to hash error", zap.Error(err))
continue
}
log.L().Debug("Removed replaced action.", log.Hex("hash", hash[:]))
ap.allActions.Delete(hash)
intrinsicGas, _ := act.IntrinsicGas()
atomic.AddUint64(&ap.gasInPool, ^uint64(intrinsicGas-1))
ap.accountDesActs.delete(act)
envestcc marked this conversation as resolved.
Show resolved Hide resolved
}
}

func (ap *actPool) removeInvalidActs(acts []*action.SealedEnvelope) {
envestcc marked this conversation as resolved.
Show resolved Hide resolved
for _, act := range acts {
hash, err := act.Hash()
Expand All @@ -424,6 +485,11 @@ func (ap *actPool) removeInvalidActs(acts []*action.SealedEnvelope) {
intrinsicGas, _ := act.IntrinsicGas()
atomic.AddUint64(&ap.gasInPool, ^uint64(intrinsicGas-1))
ap.accountDesActs.delete(act)
if ap.store != nil {
envestcc marked this conversation as resolved.
Show resolved Hide resolved
if err = ap.store.Delete(hash); err != nil {
log.L().Warn("Failed to delete action from store", zap.Error(err), log.Hex("hash", hash[:]))
}
}
}
}

Expand Down Expand Up @@ -461,6 +527,28 @@ func (ap *actPool) allocatedWorker(senderAddr address.Address) int {
return int(lastByte) % _numWorker
}

func (ap *actPool) syncFromStore() {
if ap.store == nil {
return
}
ap.store.Range(func(h hash.Hash256) bool {
if _, exist := ap.allActions.Get(h); !exist {
act, err := ap.store.Get(h)
if err != nil {
log.L().Warn("Failed to get action from store", zap.Error(err))
return true
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := ap.add(ctx, act); err != nil {
log.L().Warn("Failed to add action to pool", zap.Error(err))
return true
}
}
return true
})
}

type destinationMap struct {
mu sync.Mutex
acts map[string]map[hash.Hash256]*action.SealedEnvelope
Expand Down
13 changes: 13 additions & 0 deletions actpool/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type Config struct {
MinGasPriceStr string `yaml:"minGasPrice"`
// BlackList lists the account address that are banned from initiating actions
BlackList []string `yaml:"blackList"`
// Store defines the config for persistent cache
Store *StoreConfig `yaml:"store"`
}

// MinGasPrice returns the minimal gas price threshold
Expand All @@ -47,3 +49,14 @@ func (ap Config) MinGasPrice() *big.Int {
}
return mgp
}

// StoreConfig is the configuration for the blob store
type StoreConfig struct {
Store actionStoreConfig `yaml:"store"`
ReadInterval time.Duration `yaml:"readInterval"` // Interval to read from store to actpool memory
}

var defaultStoreConfig = StoreConfig{
Store: defaultActionStoreConfig,
ReadInterval: 10 * time.Minute,
}
21 changes: 21 additions & 0 deletions actpool/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
package actpool

import (
"errors"
"time"

"github.com/facebookgo/clock"

"github.com/iotexproject/iotex-core/pkg/routine"
)

// ActQueueOption is the option for actQueue.
Expand All @@ -33,3 +36,21 @@ func WithTimeOut(ttl time.Duration) interface{ ActQueueOption } {
}

func (o *ttlOption) SetActQueueOption(aq *actQueue) { aq.ttl = o.ttl }

// WithStore is the option to set store encode and decode functions.
func WithStore(cfg StoreConfig, encode encodeAction, decode decodeAction) func(*actPool) error {
return func(a *actPool) error {
if encode == nil || decode == nil {
return errors.New("encode and decode functions must be provided")
}
store, err := newBlobStore(cfg.Store, encode, decode)
if err != nil {
return err
}
a.store = store
a.storeSync = routine.NewRecurringTask(func() {
a.syncFromStore()
}, cfg.ReadInterval)
return nil
}
}
8 changes: 7 additions & 1 deletion actpool/queueworker.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ func (worker *queueWorker) Handle(job workerJob) error {
}

worker.ap.allActions.Set(actHash, act)
isBlobTx := false // TODO: only store blob tx
if worker.ap.store != nil && isBlobTx {
if err := worker.ap.store.Put(act); err != nil {
log.L().Warn("failed to store action", zap.Error(err), log.Hex("hash", actHash[:]))
}
}

if desAddress, ok := act.Destination(); ok && !strings.EqualFold(sender, desAddress) {
if err := worker.ap.accountDesActs.addAction(act); err != nil {
Expand All @@ -123,7 +129,7 @@ func (worker *queueWorker) Handle(job workerJob) error {
log.L().Warn("UNEXPECTED ERROR: action pool is full, but no action to drop")
return nil
}
worker.ap.removeInvalidActs([]*action.SealedEnvelope{actToReplace})
worker.ap.removeReplacedActs([]*action.SealedEnvelope{actToReplace})
if actToReplace.SenderAddress().String() == sender && actToReplace.Nonce() == nonce {
err = action.ErrTxPoolOverflow
_actpoolMtc.WithLabelValues("overMaxNumActsPerPool").Inc()
Expand Down
Loading
Loading