swarm: Chunk refactor (#17659)
Co-authored-by: Janos Guljas <janos@resenje.org> Co-authored-by: Balint Gabor <balint.g@gmail.com> Co-authored-by: Anton Evangelatov <anton.evangelatov@gmail.com> Co-authored-by: Viktor Trón <viktor.tron@gmail.com>
This commit is contained in:
@@ -20,24 +20,17 @@ package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
|
||||
lru "github.com/hashicorp/golang-lru"
|
||||
)
|
||||
|
||||
type MemStore struct {
|
||||
cache *lru.Cache
|
||||
requests *lru.Cache
|
||||
mu sync.RWMutex
|
||||
disabled bool
|
||||
}
|
||||
|
||||
//NewMemStore is instantiating a MemStore cache. We are keeping a record of all outgoing requests for chunks, that
|
||||
//should later be delivered by peer nodes, in the `requests` LRU cache. We are also keeping all frequently requested
|
||||
//NewMemStore is instantiating a MemStore cache keeping all frequently requested
|
||||
//chunks in the `cache` LRU cache.
|
||||
//
|
||||
//`requests` LRU cache capacity should ideally never be reached, this is why for the time being it should be initialised
|
||||
//with the same value as the LDBStore capacity.
|
||||
func NewMemStore(params *StoreParams, _ *LDBStore) (m *MemStore) {
|
||||
if params.CacheCapacity == 0 {
|
||||
return &MemStore{
|
||||
@@ -45,102 +38,48 @@ func NewMemStore(params *StoreParams, _ *LDBStore) (m *MemStore) {
|
||||
}
|
||||
}
|
||||
|
||||
onEvicted := func(key interface{}, value interface{}) {
|
||||
v := value.(*Chunk)
|
||||
<-v.dbStoredC
|
||||
}
|
||||
c, err := lru.NewWithEvict(int(params.CacheCapacity), onEvicted)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
requestEvicted := func(key interface{}, value interface{}) {
|
||||
// temporary remove of the error log, until we figure out the problem, as it is too spamy
|
||||
//log.Error("evict called on outgoing request")
|
||||
}
|
||||
r, err := lru.NewWithEvict(int(params.ChunkRequestsCacheCapacity), requestEvicted)
|
||||
c, err := lru.New(int(params.CacheCapacity))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return &MemStore{
|
||||
cache: c,
|
||||
requests: r,
|
||||
cache: c,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MemStore) Get(ctx context.Context, addr Address) (*Chunk, error) {
|
||||
func (m *MemStore) Get(_ context.Context, addr Address) (Chunk, error) {
|
||||
if m.disabled {
|
||||
return nil, ErrChunkNotFound
|
||||
}
|
||||
|
||||
m.mu.RLock()
|
||||
defer m.mu.RUnlock()
|
||||
|
||||
r, ok := m.requests.Get(string(addr))
|
||||
// it is a request
|
||||
if ok {
|
||||
return r.(*Chunk), nil
|
||||
}
|
||||
|
||||
// it is not a request
|
||||
c, ok := m.cache.Get(string(addr))
|
||||
if !ok {
|
||||
return nil, ErrChunkNotFound
|
||||
}
|
||||
return c.(*Chunk), nil
|
||||
return c.(*chunk), nil
|
||||
}
|
||||
|
||||
func (m *MemStore) Put(ctx context.Context, c *Chunk) {
|
||||
func (m *MemStore) Put(_ context.Context, c Chunk) error {
|
||||
if m.disabled {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
|
||||
m.mu.Lock()
|
||||
defer m.mu.Unlock()
|
||||
|
||||
// it is a request
|
||||
if c.ReqC != nil {
|
||||
select {
|
||||
case <-c.ReqC:
|
||||
if c.GetErrored() != nil {
|
||||
m.requests.Remove(string(c.Addr))
|
||||
return
|
||||
}
|
||||
m.cache.Add(string(c.Addr), c)
|
||||
m.requests.Remove(string(c.Addr))
|
||||
default:
|
||||
m.requests.Add(string(c.Addr), c)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// it is not a request
|
||||
m.cache.Add(string(c.Addr), c)
|
||||
m.requests.Remove(string(c.Addr))
|
||||
m.cache.Add(string(c.Address()), c)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MemStore) setCapacity(n int) {
|
||||
if n <= 0 {
|
||||
m.disabled = true
|
||||
} else {
|
||||
onEvicted := func(key interface{}, value interface{}) {
|
||||
v := value.(*Chunk)
|
||||
<-v.dbStoredC
|
||||
}
|
||||
c, err := lru.NewWithEvict(n, onEvicted)
|
||||
c, err := lru.New(n)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
r, err := lru.New(defaultChunkRequestsCacheCapacity)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
m = &MemStore{
|
||||
cache: c,
|
||||
requests: r,
|
||||
*m = MemStore{
|
||||
cache: c,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user