Skip to content

Commit c7b910d

Browse files
committed
writecache: change size estimations
Now size of write-cache is a sum of every object currently stored in WS. Closes #3101. Signed-off-by: Andrey Butusov <[email protected]>
1 parent 02848c9 commit c7b910d

File tree

7 files changed

+40
-25
lines changed

7 files changed

+40
-25
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Changelog for NeoFS Node
2121

2222
### Changed
2323
- Number of cuncurrenly handled notifications from the chain was increased from 10 to 300 for IR (#3068)
24+
- Write-cache size estimations (#3106)
2425

2526
### Removed
2627
- Drop creating new eacl tables with public keys (#3096)

pkg/local_object_storage/writecache/delete.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func (c *cache) Delete(addr oid.Address) error {
2424
storagelog.StorageTypeField(wcStorageType),
2525
storagelog.OpField("DELETE"),
2626
)
27-
c.objCounters.DecFS()
27+
c.objCounters.Delete(addr)
2828
}
2929

3030
return err

pkg/local_object_storage/writecache/options.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type options struct {
3838
// maxCacheSize is the maximum total size of all objects saved in cache.
3939
// 1 GiB by default.
4040
maxCacheSize uint64
41-
// objCounters contains atomic counters for the number of objects stored in cache.
41+
// objCounters contains object list along with sizes and overall size of cache.
4242
objCounters counters
4343
// noSync is true iff FSTree allows unsynchronized writes.
4444
noSync bool

pkg/local_object_storage/writecache/put.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ func (c *cache) Put(addr oid.Address, obj *objectSDK.Object, data []byte) error
3939

4040
// put writes object to FSTree and pushes it to the flush workers queue.
4141
func (c *cache) put(addr oid.Address, obj objectInfo) error {
42-
cacheSz := c.estimateCacheSize()
43-
if c.maxCacheSize < c.incSizeFS(cacheSz) {
42+
cacheSz := c.objCounters.Size()
43+
objSz := obj.obj.PayloadSize()
44+
if c.maxCacheSize < cacheSz+objSz {
4445
return ErrOutOfSpace
4546
}
4647

@@ -54,7 +55,7 @@ func (c *cache) put(addr oid.Address, obj objectInfo) error {
5455
c.compressFlags[obj.addr] = struct{}{}
5556
c.mtx.Unlock()
5657
}
57-
c.objCounters.IncFS()
58+
c.objCounters.Add(addr, objSz)
5859
storagelog.Write(c.log,
5960
storagelog.AddressField(obj.addr),
6061
storagelog.StorageTypeField(wcStorageType),

pkg/local_object_storage/writecache/state.go

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,41 +2,51 @@ package writecache
22

33
import (
44
"fmt"
5-
"math"
6-
"sync/atomic"
5+
"sync"
6+
7+
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
78
)
89

9-
func (c *cache) estimateCacheSize() uint64 {
10-
return c.objCounters.FS() * c.maxObjectSize
10+
type counters struct {
11+
mu sync.Mutex
12+
objMap map[oid.Address]uint64
13+
size uint64
1114
}
1215

13-
func (c *cache) incSizeFS(sz uint64) uint64 {
14-
return sz + c.maxObjectSize
15-
}
16+
func (x *counters) Add(addr oid.Address, size uint64) {
17+
x.mu.Lock()
18+
defer x.mu.Unlock()
1619

17-
type counters struct {
18-
cFS atomic.Uint64
20+
x.size += size
21+
x.objMap[addr] = size
1922
}
2023

21-
func (x *counters) IncFS() {
22-
x.cFS.Add(1)
23-
}
24+
func (x *counters) Delete(addr oid.Address) {
25+
x.mu.Lock()
26+
defer x.mu.Unlock()
2427

25-
func (x *counters) DecFS() {
26-
x.cFS.Add(math.MaxUint32)
28+
x.size -= x.objMap[addr]
29+
delete(x.objMap, addr)
2730
}
2831

29-
func (x *counters) FS() uint64 {
30-
return x.cFS.Load()
32+
func (x *counters) Size() uint64 {
33+
x.mu.Lock()
34+
defer x.mu.Unlock()
35+
return x.size
3136
}
3237

3338
func (c *cache) initCounters() error {
34-
inFS, err := c.fsTree.NumberOfObjects()
39+
var objHandler = func(addr oid.Address, data []byte, _ []byte) error {
40+
c.objCounters.Add(addr, uint64(len(data)))
41+
return nil
42+
}
43+
44+
var errorHandler = func(oid.Address, error) error { return nil }
45+
46+
err := c.fsTree.Iterate(objHandler, errorHandler)
3547
if err != nil {
3648
return fmt.Errorf("could not read write-cache FS counter: %w", err)
3749
}
3850

39-
c.objCounters.cFS.Store(inFS)
40-
4151
return nil
4252
}

pkg/local_object_storage/writecache/storage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (c *cache) deleteFromDisk(keys []string) []string {
9797
storagelog.StorageTypeField(wcStorageType),
9898
storagelog.OpField("DELETE"),
9999
)
100-
c.objCounters.DecFS()
100+
c.objCounters.Delete(addr)
101101
}
102102
}
103103

pkg/local_object_storage/writecache/writecache.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ func New(opts ...Option) Cache {
101101
log: zap.NewNop(),
102102
maxObjectSize: defaultMaxObjectSize,
103103
maxCacheSize: defaultMaxCacheSize,
104+
objCounters: counters{
105+
objMap: make(map[oid.Address]uint64),
106+
},
104107
},
105108
}
106109

0 commit comments

Comments
 (0)