Skip to content
This repository was archived by the owner on Aug 2, 2021. It is now read-only.

Commit 26f6626

Browse files
committed
storage/fcds: add offsetCache ttl
1 parent db658c7 commit 26f6626

File tree

4 files changed

+56
-13
lines changed

4 files changed

+56
-13
lines changed

storage/fcds/fcds.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ type Option func(*Store)
7070

7171
// WithCache is an optional argument to New constructor that enables
7272
// in memory cache of free chunk data positions in files
73-
func WithCache(yes bool) Option {
73+
func WithCache(yes bool, ttl time.Duration) Option {
7474
return func(s *Store) {
7575
if yes {
76-
s.freeCache = newOffsetCache(shardCount)
76+
s.freeCache = newOffsetCache(shardCount, ttl)
7777
} else {
7878
s.freeCache = nil
7979
}

storage/fcds/offsetcache.go

+51-9
Original file line numberDiff line numberDiff line change
@@ -16,24 +16,35 @@
1616

1717
package fcds
1818

19-
import "sync"
19+
import (
20+
"sync"
21+
"time"
22+
)
2023

2124
// offsetCache is a simple cache of offset integers
2225
// by shard files.
2326
type offsetCache struct {
24-
m map[uint8]map[int64]struct{}
25-
mu sync.RWMutex
27+
m map[uint8]map[int64]time.Time
28+
ttl time.Duration
29+
mu sync.RWMutex
30+
quit chan struct{}
31+
quitOnce sync.Once
2632
}
2733

2834
// newOffsetCache constructs offsetCache for a fixed number of shards.
29-
func newOffsetCache(shardCount uint8) (c *offsetCache) {
30-
m := make(map[uint8]map[int64]struct{})
35+
func newOffsetCache(shardCount uint8, ttl time.Duration) (c *offsetCache) {
36+
m := make(map[uint8]map[int64]time.Time)
3137
for i := uint8(0); i < shardCount; i++ {
32-
m[i] = make(map[int64]struct{})
38+
m[i] = make(map[int64]time.Time)
3339
}
34-
return &offsetCache{
35-
m: m,
40+
c = &offsetCache{
41+
m: m,
42+
quit: make(chan struct{}),
3643
}
44+
if ttl > 0 {
45+
go c.cleanup(30 * time.Second)
46+
}
47+
return c
3748
}
3849

3950
// get returns a free offset in a shard. If the returned
@@ -52,7 +63,7 @@ func (c *offsetCache) get(shard uint8) (offset int64) {
5263
// set sets a free offset for a shard file.
5364
func (c *offsetCache) set(shard uint8, offset int64) {
5465
c.mu.Lock()
55-
c.m[shard][offset] = struct{}{}
66+
c.m[shard][offset] = time.Now().Add(c.ttl)
5667
c.mu.Unlock()
5768
}
5869

@@ -62,3 +73,34 @@ func (c *offsetCache) remove(shard uint8, offset int64) {
6273
delete(c.m[shard], offset)
6374
c.mu.Unlock()
6475
}
76+
77+
// close stops parallel processing created
78+
// by offsetCache.
79+
func (c *offsetCache) close() {
80+
c.quitOnce.Do(func() {
81+
close(c.quit)
82+
})
83+
}
84+
85+
func (c *offsetCache) cleanup(period time.Duration) {
86+
ticker := time.NewTicker(period)
87+
defer ticker.Stop()
88+
89+
for {
90+
select {
91+
case <-ticker.C:
92+
now := time.Now()
93+
c.mu.Lock()
94+
for _, s := range c.m {
95+
for offset, expiration := range s {
96+
if now.After(expiration) {
97+
delete(s, offset)
98+
}
99+
}
100+
}
101+
c.mu.Unlock()
102+
case <-c.quit:
103+
return
104+
}
105+
}
106+
}

storage/fcds/test/store.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"os"
2626
"sync"
2727
"testing"
28+
"time"
2829

2930
"github.com/ethersphere/swarm/chunk"
3031
chunktesting "github.com/ethersphere/swarm/chunk/testing"
@@ -297,7 +298,7 @@ func NewFCDSStore(t *testing.T, path string, metaStore fcds.MetaStore) (s *fcds.
297298
t.Fatal(err)
298299
}
299300

300-
s, err = fcds.New(path, chunk.DefaultSize, metaStore, fcds.WithCache(!*noCacheFlag))
301+
s, err = fcds.New(path, chunk.DefaultSize, metaStore, fcds.WithCache(!*noCacheFlag, time.Hour))
301302
if err != nil {
302303
os.RemoveAll(path)
303304
t.Fatal(err)

storage/localstore/localstore.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func New(path string, baseKey []byte, o *Options) (db *DB, err error) {
230230
filepath.Join(path, "data"),
231231
chunk.DefaultSize+8, // chunk data has additional 8 bytes prepended
232232
metaStore,
233-
fcds.WithCache(true),
233+
fcds.WithCache(true, time.Hour),
234234
)
235235
if err != nil {
236236
return nil, err

0 commit comments

Comments
 (0)