2022-10-03 14:33:49 +00:00
|
|
|
package layer
|
|
|
|
|
|
|
|
import (
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/cache"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2023-03-07 14:38:08 +00:00
|
|
|
cid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/user"
|
2022-10-03 14:33:49 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Cache struct {
|
|
|
|
logger *zap.Logger
|
|
|
|
listsCache *cache.ObjectsListCache
|
|
|
|
objCache *cache.ObjectsCache
|
|
|
|
namesCache *cache.ObjectsNameCache
|
|
|
|
bucketCache *cache.BucketCache
|
|
|
|
systemCache *cache.SystemCache
|
|
|
|
accessCache *cache.AccessControlCache
|
|
|
|
}
|
|
|
|
|
|
|
|
// CachesConfig contains params for caches.
|
|
|
|
type CachesConfig struct {
|
|
|
|
Logger *zap.Logger
|
|
|
|
Objects *cache.Config
|
|
|
|
ObjectsList *cache.Config
|
|
|
|
Names *cache.Config
|
|
|
|
Buckets *cache.Config
|
|
|
|
System *cache.Config
|
|
|
|
AccessControl *cache.Config
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultCachesConfigs returns filled configs.
|
|
|
|
func DefaultCachesConfigs(logger *zap.Logger) *CachesConfig {
|
|
|
|
return &CachesConfig{
|
|
|
|
Logger: logger,
|
|
|
|
Objects: cache.DefaultObjectsConfig(logger),
|
|
|
|
ObjectsList: cache.DefaultObjectsListConfig(logger),
|
|
|
|
Names: cache.DefaultObjectsNameConfig(logger),
|
|
|
|
Buckets: cache.DefaultBucketConfig(logger),
|
|
|
|
System: cache.DefaultSystemConfig(logger),
|
|
|
|
AccessControl: cache.DefaultAccessControlConfig(logger),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewCache(cfg *CachesConfig) *Cache {
|
|
|
|
return &Cache{
|
|
|
|
logger: cfg.Logger,
|
|
|
|
listsCache: cache.NewObjectsListCache(cfg.ObjectsList),
|
|
|
|
objCache: cache.New(cfg.Objects),
|
|
|
|
namesCache: cache.NewObjectsNameCache(cfg.Names),
|
|
|
|
bucketCache: cache.NewBucketCache(cfg.Buckets),
|
|
|
|
systemCache: cache.NewSystemCache(cfg.System),
|
|
|
|
accessCache: cache.NewAccessControlCache(cfg.AccessControl),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-16 12:10:51 +00:00
|
|
|
func (c *Cache) GetBucket(ns, name string) *data.BucketInfo {
|
|
|
|
return c.bucketCache.Get(ns, name)
|
2022-10-03 14:33:49 +00:00
|
|
|
}
|
|
|
|
|
2023-11-16 12:10:51 +00:00
|
|
|
func (c *Cache) PutBucket(ns string, bktInfo *data.BucketInfo) {
|
|
|
|
if err := c.bucketCache.Put(ns, bktInfo); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
c.logger.Warn(logs.CouldntPutBucketInfoIntoCache,
|
2023-11-16 12:10:51 +00:00
|
|
|
zap.String("namespace", ns),
|
2022-10-03 14:33:49 +00:00
|
|
|
zap.String("bucket name", bktInfo.Name),
|
|
|
|
zap.Stringer("bucket cid", bktInfo.CID),
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-16 12:10:51 +00:00
|
|
|
func (c *Cache) DeleteBucket(ns, name string) {
|
|
|
|
c.bucketCache.Delete(ns, name)
|
2022-10-03 14:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) CleanListCacheEntriesContainingObject(objectName string, cnrID cid.ID) {
|
|
|
|
c.listsCache.CleanCacheEntriesContainingObject(objectName, cnrID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) DeleteObjectName(cnrID cid.ID, bktName, objName string) {
|
|
|
|
c.namesCache.Delete(bktName + "/" + objName)
|
|
|
|
c.listsCache.CleanCacheEntriesContainingObject(objName, cnrID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) DeleteObject(addr oid.Address) {
|
|
|
|
c.objCache.Delete(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) GetObject(owner user.ID, addr oid.Address) *data.ExtendedObjectInfo {
|
|
|
|
if !c.accessCache.Get(owner, addr.String()) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.objCache.GetObject(addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) GetLastObject(owner user.ID, bktName, objName string) *data.ExtendedObjectInfo {
|
|
|
|
addr := c.namesCache.Get(bktName + "/" + objName)
|
|
|
|
if addr == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.GetObject(owner, *addr)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) PutObject(owner user.ID, extObjInfo *data.ExtendedObjectInfo) {
|
|
|
|
if err := c.objCache.PutObject(extObjInfo); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
c.logger.Warn(logs.CouldntAddObjectToCache, zap.Error(err),
|
2022-10-03 14:33:49 +00:00
|
|
|
zap.String("object_name", extObjInfo.ObjectInfo.Name), zap.String("bucket_name", extObjInfo.ObjectInfo.Bucket),
|
|
|
|
zap.String("cid", extObjInfo.ObjectInfo.CID.EncodeToString()), zap.String("oid", extObjInfo.ObjectInfo.ID.EncodeToString()))
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.accessCache.Put(owner, extObjInfo.ObjectInfo.Address().EncodeToString()); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
c.logger.Warn(logs.CouldntCacheAccessControlOperation, zap.Error(err))
|
2022-10-03 14:33:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) PutObjectWithName(owner user.ID, extObjInfo *data.ExtendedObjectInfo) {
|
|
|
|
c.PutObject(owner, extObjInfo)
|
|
|
|
|
|
|
|
if err := c.namesCache.Put(extObjInfo.ObjectInfo.NiceName(), extObjInfo.ObjectInfo.Address()); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
c.logger.Warn(logs.CouldntPutObjAddressToNameCache,
|
2022-10-03 14:33:49 +00:00
|
|
|
zap.String("obj nice name", extObjInfo.ObjectInfo.NiceName()),
|
|
|
|
zap.Error(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) GetList(owner user.ID, key cache.ObjectsListKey) []*data.NodeVersion {
|
|
|
|
if !c.accessCache.Get(owner, key.String()) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.listsCache.GetVersions(key)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) PutList(owner user.ID, key cache.ObjectsListKey, list []*data.NodeVersion) {
|
|
|
|
if err := c.listsCache.PutVersions(key, list); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
c.logger.Warn(logs.CouldntCacheListOfObjects, zap.Error(err))
|
2022-10-03 14:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.accessCache.Put(owner, key.String()); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
c.logger.Warn(logs.CouldntCacheAccessControlOperation, zap.Error(err))
|
2022-10-03 14:33:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-06 08:46:53 +00:00
|
|
|
func (c *Cache) GetTagging(owner user.ID, key string) map[string]string {
|
|
|
|
if !c.accessCache.Get(owner, key) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-03 14:33:49 +00:00
|
|
|
return c.systemCache.GetTagging(key)
|
|
|
|
}
|
|
|
|
|
2022-10-06 08:46:53 +00:00
|
|
|
func (c *Cache) PutTagging(owner user.ID, key string, tags map[string]string) {
|
2022-10-03 14:33:49 +00:00
|
|
|
if err := c.systemCache.PutTagging(key, tags); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
c.logger.Error(logs.CouldntCacheTags, zap.Error(err))
|
2022-10-03 14:33:49 +00:00
|
|
|
}
|
2022-10-06 08:46:53 +00:00
|
|
|
|
|
|
|
if err := c.accessCache.Put(owner, key); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
c.logger.Warn(logs.CouldntCacheAccessControlOperation, zap.Error(err))
|
2022-10-06 08:46:53 +00:00
|
|
|
}
|
2022-10-03 14:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) DeleteTagging(key string) {
|
|
|
|
c.systemCache.Delete(key)
|
|
|
|
}
|
|
|
|
|
2022-10-06 08:46:53 +00:00
|
|
|
func (c *Cache) GetLockInfo(owner user.ID, key string) *data.LockInfo {
|
|
|
|
if !c.accessCache.Get(owner, key) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-03 14:33:49 +00:00
|
|
|
return c.systemCache.GetLockInfo(key)
|
|
|
|
}
|
|
|
|
|
2022-10-06 08:46:53 +00:00
|
|
|
func (c *Cache) PutLockInfo(owner user.ID, key string, lockInfo *data.LockInfo) {
|
2022-10-03 14:33:49 +00:00
|
|
|
if err := c.systemCache.PutLockInfo(key, lockInfo); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
c.logger.Error(logs.CouldntCacheLockInfo, zap.Error(err))
|
2022-10-03 14:33:49 +00:00
|
|
|
}
|
2022-10-06 08:46:53 +00:00
|
|
|
|
|
|
|
if err := c.accessCache.Put(owner, key); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
c.logger.Warn(logs.CouldntCacheAccessControlOperation, zap.Error(err))
|
2022-10-06 08:46:53 +00:00
|
|
|
}
|
2022-10-03 14:33:49 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 08:46:53 +00:00
|
|
|
func (c *Cache) GetSettings(owner user.ID, bktInfo *data.BucketInfo) *data.BucketSettings {
|
2022-10-03 14:33:49 +00:00
|
|
|
key := bktInfo.Name + bktInfo.SettingsObjectName()
|
2022-10-06 08:46:53 +00:00
|
|
|
|
|
|
|
if !c.accessCache.Get(owner, key) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-03 14:33:49 +00:00
|
|
|
return c.systemCache.GetSettings(key)
|
|
|
|
}
|
|
|
|
|
2022-10-06 08:46:53 +00:00
|
|
|
func (c *Cache) PutSettings(owner user.ID, bktInfo *data.BucketInfo, settings *data.BucketSettings) {
|
2022-10-03 14:33:49 +00:00
|
|
|
key := bktInfo.Name + bktInfo.SettingsObjectName()
|
|
|
|
if err := c.systemCache.PutSettings(key, settings); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
c.logger.Warn(logs.CouldntCacheBucketSettings, zap.String("bucket", bktInfo.Name), zap.Error(err))
|
2022-10-03 14:33:49 +00:00
|
|
|
}
|
2022-10-06 08:46:53 +00:00
|
|
|
|
|
|
|
if err := c.accessCache.Put(owner, key); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
c.logger.Warn(logs.CouldntCacheAccessControlOperation, zap.Error(err))
|
2022-10-06 08:46:53 +00:00
|
|
|
}
|
2022-10-03 14:33:49 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 08:46:53 +00:00
|
|
|
func (c *Cache) GetCORS(owner user.ID, bkt *data.BucketInfo) *data.CORSConfiguration {
|
|
|
|
key := bkt.Name + bkt.CORSObjectName()
|
|
|
|
|
|
|
|
if !c.accessCache.Get(owner, key) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.systemCache.GetCORS(key)
|
2022-10-03 14:33:49 +00:00
|
|
|
}
|
|
|
|
|
2022-10-06 08:46:53 +00:00
|
|
|
func (c *Cache) PutCORS(owner user.ID, bkt *data.BucketInfo, cors *data.CORSConfiguration) {
|
|
|
|
key := bkt.Name + bkt.CORSObjectName()
|
|
|
|
|
|
|
|
if err := c.systemCache.PutCORS(key, cors); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
c.logger.Warn(logs.CouldntCacheCors, zap.String("bucket", bkt.Name), zap.Error(err))
|
2022-10-03 14:33:49 +00:00
|
|
|
}
|
2022-10-06 08:46:53 +00:00
|
|
|
|
|
|
|
if err := c.accessCache.Put(owner, key); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
c.logger.Warn(logs.CouldntCacheAccessControlOperation, zap.Error(err))
|
2022-10-06 08:46:53 +00:00
|
|
|
}
|
2022-10-03 14:33:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Cache) DeleteCORS(bktInfo *data.BucketInfo) {
|
|
|
|
c.systemCache.Delete(bktInfo.Name + bktInfo.CORSObjectName())
|
|
|
|
}
|
|
|
|
|
2022-10-06 08:46:53 +00:00
|
|
|
func (c *Cache) GetNotificationConfiguration(owner user.ID, bktInfo *data.BucketInfo) *data.NotificationConfiguration {
|
2022-10-03 14:33:49 +00:00
|
|
|
key := bktInfo.Name + bktInfo.NotificationConfigurationObjectName()
|
2022-10-06 08:46:53 +00:00
|
|
|
|
|
|
|
if !c.accessCache.Get(owner, key) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-03 14:33:49 +00:00
|
|
|
return c.systemCache.GetNotificationConfiguration(key)
|
|
|
|
}
|
|
|
|
|
2022-10-06 08:46:53 +00:00
|
|
|
func (c *Cache) PutNotificationConfiguration(owner user.ID, bktInfo *data.BucketInfo, configuration *data.NotificationConfiguration) {
|
2022-10-03 14:33:49 +00:00
|
|
|
key := bktInfo.Name + bktInfo.NotificationConfigurationObjectName()
|
|
|
|
if err := c.systemCache.PutNotificationConfiguration(key, configuration); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
c.logger.Warn(logs.CouldntCacheNotificationConfiguration, zap.String("bucket", bktInfo.Name), zap.Error(err))
|
2022-10-03 14:33:49 +00:00
|
|
|
}
|
2022-10-06 08:46:53 +00:00
|
|
|
|
|
|
|
if err := c.accessCache.Put(owner, key); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
c.logger.Warn(logs.CouldntCacheAccessControlOperation, zap.Error(err))
|
2022-10-06 08:46:53 +00:00
|
|
|
}
|
2022-10-03 14:33:49 +00:00
|
|
|
}
|