frostfs-s3-gw/internal/frostfs/source.go
Marina Biryukova 95d847d611
All checks were successful
/ DCO (pull_request) Successful in 4m12s
/ Vulncheck (pull_request) Successful in 4m31s
/ Builds (pull_request) Successful in 2m28s
/ Lint (pull_request) Successful in 2m52s
/ Tests (pull_request) Successful in 2m34s
/ Vulncheck (push) Successful in 1m41s
/ Builds (push) Successful in 2m4s
/ Lint (push) Successful in 6m4s
/ Tests (push) Successful in 5m20s
[#577] Update SDK to support new tree/pool version
Signed-off-by: Marina Biryukova <m.biryukova@yadro.com>
2024-12-20 13:50:31 +03:00

66 lines
1.7 KiB
Go

package frostfs
import (
"context"
"fmt"
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer/frostfs"
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware"
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
)
type Source struct {
frostFS *FrostFS
cache *layer.Cache
}
func NewSource(frostFS *FrostFS, cache *layer.Cache) *Source {
return &Source{
frostFS: frostFS,
cache: cache,
}
}
func (s *Source) NetMapSnapshot(ctx context.Context) (netmap.NetMap, error) {
cachedNetmap := s.cache.GetNetmap()
if cachedNetmap != nil {
return *cachedNetmap, nil
}
netmapSnapshot, err := s.frostFS.NetmapSnapshot(ctx)
if err != nil {
return netmap.NetMap{}, fmt.Errorf("get netmap: %w", err)
}
s.cache.PutNetmap(netmapSnapshot)
return netmapSnapshot, nil
}
func (s *Source) PlacementPolicy(ctx context.Context, cnrID cid.ID) (netmap.PlacementPolicy, error) {
cachedPolicy := s.cache.GetPlacementPolicy(cnrID)
if cachedPolicy != nil {
return *cachedPolicy, nil
}
prm := frostfs.PrmContainer{
ContainerID: cnrID,
}
if bd, err := middleware.GetBoxData(ctx); err == nil && bd.Gate != nil {
prm.SessionToken = bd.Gate.SessionToken()
}
res, err := s.frostFS.Container(ctx, prm)
if err != nil {
return netmap.PlacementPolicy{}, fmt.Errorf("get container: %w", err)
}
// We don't put container back to the cache to keep cache
// coherent to the requests made by S3 users. FrostFS Source
// is being used by SDK Tree Pool and it should not fill cache
// with possibly irrelevant container values.
return res.PlacementPolicy(), nil
}