Skip to content

Commit

Permalink
fix: asd
Browse files Browse the repository at this point in the history
  • Loading branch information
istae committed Oct 27, 2023
1 parent 52ea4bc commit 0fee48b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
34 changes: 17 additions & 17 deletions pkg/postage/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,24 +195,10 @@ func (ps *service) HandleStampExpiry(id []byte) {

// SetExpired removes all expired batches from the stamp issuers.
func (ps *service) SetExpired(ctx context.Context) error {
ps.lock.Lock()
defer ps.lock.Unlock()

for _, issuer := range ps.issuers {
exists, err := ps.postageStore.Exists(issuer.ID())
if err != nil {
return fmt.Errorf("set expired: checking if batch exists for stamp issuer %s: %w", hex.EncodeToString(issuer.ID()), err)
}
if !exists {
if err := ps.store.Delete(&StampIssuerItem{Issuer: issuer}); err != nil {
return fmt.Errorf("set expired: delete stamp data for batch %s: %w", hex.EncodeToString(issuer.ID()), err)
}
ps.logger.Debug("removed expired stamp issuer", "id", hex.EncodeToString(issuer.ID()))
}
}
ps.logger.Debug("removing expired stamp data from stamperstore. this may take a while if the node has not been restarted in a while.")

deleteItemC := make(chan *StampItem)

go func() {
for item := range deleteItemC {
_ = ps.store.Delete(item)
Expand All @@ -226,8 +212,6 @@ func (ps *service) SetExpired(ctx context.Context) error {
ps.logger.Debug("removed expired stamps", "count", count)
}()

ps.logger.Debug("removing expired stamp data from stamperstore. this may take a while if the node has not been restarted in a while.")

err := ps.store.Iterate(
storage.Query{
Factory: func() storage.Item {
Expand Down Expand Up @@ -261,6 +245,22 @@ func (ps *service) SetExpired(ctx context.Context) error {
}
}()

ps.lock.Lock()
defer ps.lock.Unlock()

for _, issuer := range ps.issuers {
exists, err := ps.postageStore.Exists(issuer.ID())
if err != nil {
return fmt.Errorf("set expired: checking if batch exists for stamp issuer %s: %w", hex.EncodeToString(issuer.ID()), err)
}
if !exists {
if err := ps.store.Delete(&StampIssuerItem{Issuer: issuer}); err != nil {
return fmt.Errorf("set expired: delete stamp data for batch %s: %w", hex.EncodeToString(issuer.ID()), err)
}
ps.logger.Debug("removed expired stamp issuer", "id", hex.EncodeToString(issuer.ID()))
}
}

return ps.reload()
}

Expand Down
15 changes: 5 additions & 10 deletions pkg/storer/cachestore.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,12 @@ func (db *DB) cacheWorker(ctx context.Context) {
select {
case <-overCapTrigger:

capc := db.cacheObj.Capacity()

if db.cacheObj.Size() <= capc {
var size, capc = db.cacheObj.Size(), db.cacheObj.Capacity()
if size <= capc {
continue
}

evict := (float64(capc) * 0.01) // evict 1% of the capacty
evict := (size - capc) + uint64(float64(capc)*0.01) // evict (size - cap) + some buffer

dur := captureDuration(time.Now())
err := db.Execute(ctx, func(s internal.Storage) error {
Expand All @@ -49,7 +48,7 @@ func (db *DB) cacheWorker(ctx context.Context) {
db.metrics.MethodCalls.WithLabelValues("cachestore", "RemoveOldest", "failure").Inc()
db.logger.Warning("cache eviction failure", "error", err)
} else {
db.logger.Debug("cache eviction finished", "evicted", evict, "duration", dur())
db.logger.Debug("cache eviction finished", "evicted", evict, "duration_sec", dur())
db.metrics.MethodCalls.WithLabelValues("cachestore", "RemoveOldest", "success").Inc()
}
db.triggerCacheEviction()
Expand Down Expand Up @@ -116,11 +115,7 @@ func (db *DB) CacheShallowCopy(ctx context.Context, store internal.Storage, addr

func (db *DB) triggerCacheEviction() {

var (
size = db.cacheObj.Size()
capc = db.cacheObj.Capacity()
)

var size, capc = db.cacheObj.Size(), db.cacheObj.Capacity()
db.metrics.CacheSize.Set(float64(size))

if size > capc {
Expand Down

0 comments on commit 0fee48b

Please sign in to comment.