-
Notifications
You must be signed in to change notification settings - Fork 325
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
Changes from 3 commits
099b52e
e276d52
b6b4365
1f104cd
68cbd80
9645bdd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import ( | |
"sort" | ||
"sync" | ||
"sync/atomic" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/prometheus/client_golang/prometheus" | ||
|
@@ -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" | ||
) | ||
|
||
|
@@ -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 | ||
|
@@ -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]) | ||
|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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...) | ||
} | ||
|
@@ -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) | ||
|
@@ -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 | ||
|
@@ -412,6 +458,21 @@ func (ap *actPool) validate(ctx context.Context, selp *action.SealedEnvelope) er | |
return nil | ||
} | ||
|
||
func (ap *actPool) removeReplacedActs(acts []*action.SealedEnvelope) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is same as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as discussed, will delete the |
||
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() | ||
|
@@ -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[:])) | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
@@ -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 | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is this 10GB?
There was a problem hiding this comment.
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