[#1464] frostfsid: Cache subject not found
error
All checks were successful
Vulncheck / Vulncheck (pull_request) Successful in 3m6s
Pre-commit hooks / Pre-commit (pull_request) Successful in 3m47s
Tests and linters / Tests (pull_request) Successful in 3m47s
Tests and linters / Tests with -race (pull_request) Successful in 5m48s
DCO action / DCO (pull_request) Successful in 6m8s
Tests and linters / Run gofumpt (pull_request) Successful in 6m33s
Build / Build Components (pull_request) Successful in 6m47s
Tests and linters / Staticcheck (pull_request) Successful in 6m53s
Tests and linters / gopls check (pull_request) Successful in 7m10s
Tests and linters / Lint (pull_request) Successful in 8m1s
All checks were successful
Vulncheck / Vulncheck (pull_request) Successful in 3m6s
Pre-commit hooks / Pre-commit (pull_request) Successful in 3m47s
Tests and linters / Tests (pull_request) Successful in 3m47s
Tests and linters / Tests with -race (pull_request) Successful in 5m48s
DCO action / DCO (pull_request) Successful in 6m8s
Tests and linters / Run gofumpt (pull_request) Successful in 6m33s
Build / Build Components (pull_request) Successful in 6m47s
Tests and linters / Staticcheck (pull_request) Successful in 6m53s
Tests and linters / gopls check (pull_request) Successful in 7m10s
Tests and linters / Lint (pull_request) Successful in 8m1s
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
parent
cb156fd4c9
commit
32be596c2d
1 changed files with 42 additions and 11 deletions
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.frostfs.info/TrueCloudLab/frostfs-contract/frostfsid/client"
|
||||
|
@ -9,10 +10,22 @@ import (
|
|||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
)
|
||||
|
||||
type subjectWithError struct {
|
||||
subject *client.Subject
|
||||
err error
|
||||
}
|
||||
|
||||
type subjectExtWithError struct {
|
||||
subject *client.SubjectExtended
|
||||
err error
|
||||
}
|
||||
|
||||
type morphFrostfsIDCache struct {
|
||||
subjProvider frostfsidcore.SubjectProvider
|
||||
|
||||
subjCache *expirable.LRU[util.Uint160, *client.Subject]
|
||||
subjCache *expirable.LRU[util.Uint160, subjectWithError]
|
||||
|
||||
subjExtCache *expirable.LRU[util.Uint160, subjectExtWithError]
|
||||
|
||||
metrics cacheMetrics
|
||||
}
|
||||
|
@ -21,7 +34,9 @@ func newMorphFrostfsIDCache(subjProvider frostfsidcore.SubjectProvider, size int
|
|||
return &morphFrostfsIDCache{
|
||||
subjProvider: subjProvider,
|
||||
|
||||
subjCache: expirable.NewLRU(size, func(util.Uint160, *client.Subject) {}, ttl),
|
||||
subjCache: expirable.NewLRU(size, func(util.Uint160, subjectWithError) {}, ttl),
|
||||
|
||||
subjExtCache: expirable.NewLRU(size, func(util.Uint160, subjectExtWithError) {}, ttl),
|
||||
|
||||
metrics: metrics,
|
||||
}
|
||||
|
@ -37,16 +52,21 @@ func (m *morphFrostfsIDCache) GetSubject(addr util.Uint160) (*client.Subject, er
|
|||
result, found := m.subjCache.Get(addr)
|
||||
if found {
|
||||
hit = true
|
||||
return result, nil
|
||||
return result.subject, result.err
|
||||
}
|
||||
|
||||
result, err := m.subjProvider.GetSubject(addr)
|
||||
subj, err := m.subjProvider.GetSubject(addr)
|
||||
if err != nil {
|
||||
if m.isCacheableError(err) {
|
||||
m.subjCache.Add(addr, subjectWithError{
|
||||
err: err,
|
||||
})
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m.subjCache.Add(addr, result)
|
||||
return result, nil
|
||||
m.subjCache.Add(addr, subjectWithError{subject: subj})
|
||||
return subj, nil
|
||||
}
|
||||
|
||||
func (m *morphFrostfsIDCache) GetSubjectExtended(addr util.Uint160) (*client.SubjectExtended, error) {
|
||||
|
@ -59,21 +79,32 @@ func (m *morphFrostfsIDCache) GetSubjectExtended(addr util.Uint160) (*client.Sub
|
|||
result, found := m.subjExtCache.Get(addr)
|
||||
if found {
|
||||
hit = true
|
||||
return result, nil
|
||||
return result.subject, result.err
|
||||
}
|
||||
|
||||
var err error
|
||||
subjExt, err = m.subjProvider.GetSubjectExtended(addr)
|
||||
subjExt, err := m.subjProvider.GetSubjectExtended(addr)
|
||||
if err != nil {
|
||||
if m.isCacheableError(err) {
|
||||
m.subjExtCache.Add(addr, subjectExtWithError{
|
||||
err: err,
|
||||
})
|
||||
m.subjCache.Add(addr, subjectWithError{
|
||||
err: err,
|
||||
})
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
|
||||
m.subjExtCache.Add(addr, subjExt)
|
||||
m.subjCache.Add(addr, subjectFromSubjectExtended(subjExt))
|
||||
m.subjExtCache.Add(addr, subjectExtWithError{subject: subjExt})
|
||||
m.subjCache.Add(addr, subjectWithError{subject: subjectFromSubjectExtended(subjExt)})
|
||||
|
||||
return subjExt, nil
|
||||
}
|
||||
|
||||
func (m *morphFrostfsIDCache) isCacheableError(err error) bool {
|
||||
return strings.Contains(err.Error(), frostfsidcore.SubjectNotFoundErrorMessage)
|
||||
}
|
||||
|
||||
func subjectFromSubjectExtended(subjExt *client.SubjectExtended) *client.Subject {
|
||||
return &client.Subject{
|
||||
PrimaryKey: subjExt.PrimaryKey,
|
||||
|
|
Loading…
Reference in a new issue