app: Refactor the default copies number setting #109

Merged
alexvanin merged 1 commit from ironbee/frostfs-s3-gw:refactor_set-copies-number into master 2023-05-17 12:43:03 +00:00
4 changed files with 35 additions and 18 deletions

View file

@ -34,7 +34,7 @@ type (
XMLDecoder XMLDecoderProvider
DefaultMaxAge int
NotificatorEnabled bool
CopiesNumber uint32
DefaultCopiesNumbers []uint32
CopiesNumbers map[string][]uint32
ResolveZoneList []string
IsResolveListAllow bool // True if ResolveZoneList contains allowed zones
@ -84,9 +84,9 @@ func New(log *zap.Logger, obj layer.Client, notificator Notificator, cfg *Config
}
// pickCopiesNumbers chooses the return values following this logic:
// 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
// 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.
func (h *handler) pickCopiesNumbers(metadata map[string]string, locationConstraint string) ([]uint32, error) {
copiesNumbersStr, ok := metadata[layer.AttributeFrostfsCopiesNumber]
if ok {
@ -102,7 +102,7 @@ func (h *handler) pickCopiesNumbers(metadata map[string]string, locationConstrai
return copiesNumbers, nil
}
return []uint32{h.cfg.CopiesNumber}, nil
return h.cfg.DefaultCopiesNumbers, nil
}
func parseCopiesNumbers(copiesNumbersStr string) ([]uint32, error) {

View file

@ -13,8 +13,8 @@ func TestCopiesNumberPicker(t *testing.T) {
locConstraints[locationConstraint1] = []uint32{2, 3, 4}
config := &Config{
CopiesNumber: 1,
CopiesNumbers: locConstraints,
DefaultCopiesNumbers: []uint32{1},
CopiesNumbers: locConstraints,
}
h := handler{
cfg: config,

View file

@ -634,11 +634,11 @@ func getAccessBoxCacheConfig(v *viper.Viper, l *zap.Logger) *cache.Config {
func (a *App) initHandler() {
cfg := &handler.Config{
Policy: a.settings.policies,
DefaultMaxAge: handler.DefaultMaxAge,
NotificatorEnabled: a.cfg.GetBool(cfgEnableNATS),
CopiesNumber: handler.DefaultCopiesNumber,
XMLDecoder: a.settings.xmlDecoder,
Policy: a.settings.policies,
DefaultMaxAge: handler.DefaultMaxAge,
NotificatorEnabled: a.cfg.GetBool(cfgEnableNATS),
DefaultCopiesNumbers: []uint32{handler.DefaultCopiesNumber},
XMLDecoder: a.settings.xmlDecoder,
}
if a.cfg.IsSet(cfgDefaultMaxAge) {
@ -654,9 +654,10 @@ func (a *App) initHandler() {
cfg.CopiesNumbers = fetchCopiesNumbers(a.log, a.cfg)
if val := a.cfg.GetUint32(cfgSetCopiesNumber); val > 0 {
cfg.CopiesNumber = val
if val := fetchDefaultCopiesNumbers(a.log, a.cfg); len(val) > 0 {
cfg.DefaultCopiesNumbers = val
}
a.log.Info("setting default copies numbers", zap.Uint32s("vector", cfg.DefaultCopiesNumbers))
cfg.ResolveZoneList = a.cfg.GetStringSlice(cfgResolveBucketAllow)
cfg.IsResolveListAllow = len(cfg.ResolveZoneList) > 0

View file

@ -152,6 +152,22 @@ var ignore = map[string]struct{}{
cmdVersion: {},
}
func fetchDefaultCopiesNumbers(l *zap.Logger, v *viper.Viper) []uint32 {
unparsed := v.GetStringSlice(cfgSetCopiesNumber)
var result []uint32
for i := range unparsed {
parsedValue, err := strconv.ParseUint(unparsed[i], 10, 32)
if err != nil {
l.Error("cannot parse default copies numbers", zap.Error(err))
return make([]uint32, 0)
}
result = append(result, uint32(parsedValue))
}
return result
}
func fetchCopiesNumbers(l *zap.Logger, v *viper.Viper) map[string][]uint32 {
var copiesNums = make(map[string][]uint32)
LOOP:
@ -160,6 +176,10 @@ LOOP:
constraint := v.GetString(key + "location_constraint")
vector := v.GetStringSlice(key + "vector")
if constraint == "" || len(vector) == 0 {
break
}
vector32 := make([]uint32, len(vector))
for j := range vector {
parsedValue, err := strconv.ParseUint(vector[j], 10, 32)
@ -170,10 +190,6 @@ LOOP:
vector32[j] = uint32(parsedValue)
}
if constraint == "" || len(vector) == 0 {
break
}
copiesNums[constraint] = vector32
l.Debug("added constraint", zap.String("location", constraint), zap.Strings("copies numbers", vector))
}