2020-07-24 16:10:41 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2023-03-02 14:54:33 +00:00
|
|
|
"encoding/xml"
|
2020-08-06 10:49:25 +00:00
|
|
|
"errors"
|
2023-04-24 23:49:12 +00:00
|
|
|
"fmt"
|
2023-03-02 14:54:33 +00:00
|
|
|
"io"
|
2023-04-24 23:49:12 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2022-11-08 09:12:55 +00:00
|
|
|
"time"
|
2020-07-24 16:10:41 +00:00
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
|
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
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/netmap"
|
2020-07-24 16:10:41 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
handler struct {
|
2022-04-29 13:08:22 +00:00
|
|
|
log *zap.Logger
|
|
|
|
obj layer.Client
|
|
|
|
notificator Notificator
|
|
|
|
cfg *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
Notificator interface {
|
|
|
|
SendNotifications(topics map[string]string, p *SendNotificationParams) error
|
2022-11-08 09:12:55 +00:00
|
|
|
SendTestNotification(topic, bucketName, requestID, HostID string, now time.Time) error
|
2021-08-23 18:39:15 +00:00
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// Config contains data which handler needs to keep.
|
2021-08-23 18:39:15 +00:00
|
|
|
Config struct {
|
2023-03-20 08:44:35 +00:00
|
|
|
Policy PlacementPolicy
|
|
|
|
XMLDecoder XMLDecoderProvider
|
|
|
|
DefaultMaxAge int
|
|
|
|
NotificatorEnabled bool
|
|
|
|
ResolveZoneList []string
|
|
|
|
IsResolveListAllow bool // True if ResolveZoneList contains allowed zones
|
|
|
|
CompleteMultipartKeepalive time.Duration
|
2023-07-10 09:17:44 +00:00
|
|
|
Kludge KludgeSettings
|
2023-10-02 08:52:07 +00:00
|
|
|
Features layer.FeatureSettings
|
2020-07-24 16:10:41 +00:00
|
|
|
}
|
2022-11-03 06:34:18 +00:00
|
|
|
|
2022-11-14 09:37:47 +00:00
|
|
|
PlacementPolicy interface {
|
2023-05-18 13:20:00 +00:00
|
|
|
DefaultPlacementPolicy() netmap.PlacementPolicy
|
|
|
|
PlacementPolicy(string) (netmap.PlacementPolicy, bool)
|
|
|
|
CopiesNumbers(string) ([]uint32, bool)
|
|
|
|
DefaultCopiesNumbers() []uint32
|
2022-11-03 06:34:18 +00:00
|
|
|
}
|
2023-03-02 14:54:33 +00:00
|
|
|
|
|
|
|
XMLDecoderProvider interface {
|
|
|
|
NewCompleteMultipartDecoder(io.Reader) *xml.Decoder
|
|
|
|
}
|
2023-07-10 09:17:44 +00:00
|
|
|
|
|
|
|
KludgeSettings interface {
|
|
|
|
BypassContentEncodingInChunks() bool
|
|
|
|
}
|
2020-07-24 16:10:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var _ api.Handler = (*handler)(nil)
|
|
|
|
|
2021-05-13 20:25:31 +00:00
|
|
|
// New creates new api.Handler using given logger and client.
|
2022-04-29 13:08:22 +00:00
|
|
|
func New(log *zap.Logger, obj layer.Client, notificator Notificator, cfg *Config) (api.Handler, error) {
|
2020-07-24 16:10:41 +00:00
|
|
|
switch {
|
2020-08-06 10:49:25 +00:00
|
|
|
case obj == nil:
|
2022-12-20 08:38:58 +00:00
|
|
|
return nil, errors.New("empty FrostFS Object Layer")
|
2020-08-06 10:49:25 +00:00
|
|
|
case log == nil:
|
2020-07-24 16:10:41 +00:00
|
|
|
return nil, errors.New("empty logger")
|
|
|
|
}
|
|
|
|
|
2022-06-24 14:34:44 +00:00
|
|
|
if !cfg.NotificatorEnabled {
|
2023-08-23 11:07:52 +00:00
|
|
|
log.Warn(logs.NotificatorIsDisabledS3WontProduceNotificationEvents)
|
2022-06-24 14:34:44 +00:00
|
|
|
} else if notificator == nil {
|
2022-04-29 13:08:22 +00:00
|
|
|
return nil, errors.New("empty notificator")
|
|
|
|
}
|
|
|
|
|
2020-07-24 16:10:41 +00:00
|
|
|
return &handler{
|
2022-04-29 13:08:22 +00:00
|
|
|
log: log,
|
|
|
|
obj: obj,
|
|
|
|
cfg: cfg,
|
|
|
|
notificator: notificator,
|
2020-07-24 16:10:41 +00:00
|
|
|
}, nil
|
|
|
|
}
|
2023-04-24 23:49:12 +00:00
|
|
|
|
|
|
|
// pickCopiesNumbers chooses the return values following this logic:
|
2023-05-16 14:24:10 +00:00
|
|
|
// 1) array of copies numbers sent in request's header has the highest priority.
|
|
|
|
// 2) array of copies numbers with corresponding location constraint provided in the config file.
|
|
|
|
// 3) default copies number from the config file wrapped into array.
|
2023-04-24 23:49:12 +00:00
|
|
|
func (h *handler) pickCopiesNumbers(metadata map[string]string, locationConstraint string) ([]uint32, error) {
|
|
|
|
copiesNumbersStr, ok := metadata[layer.AttributeFrostfsCopiesNumber]
|
|
|
|
if ok {
|
|
|
|
result, err := parseCopiesNumbers(copiesNumbersStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2023-05-18 13:20:00 +00:00
|
|
|
copiesNumbers, ok := h.cfg.Policy.CopiesNumbers(locationConstraint)
|
2023-04-24 23:49:12 +00:00
|
|
|
if ok {
|
|
|
|
return copiesNumbers, nil
|
|
|
|
}
|
|
|
|
|
2023-05-18 13:20:00 +00:00
|
|
|
return h.cfg.Policy.DefaultCopiesNumbers(), nil
|
2023-04-24 23:49:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func parseCopiesNumbers(copiesNumbersStr string) ([]uint32, error) {
|
|
|
|
var result []uint32
|
|
|
|
copiesNumbersSplit := strings.Split(copiesNumbersStr, ",")
|
|
|
|
|
|
|
|
for i := range copiesNumbersSplit {
|
|
|
|
item := strings.ReplaceAll(copiesNumbersSplit[i], " ", "")
|
|
|
|
if len(item) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
copiesNumber, err := strconv.ParseUint(item, 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("pasrse copies number: %w", err)
|
|
|
|
}
|
|
|
|
result = append(result, uint32(copiesNumber))
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|