From e24bc3f2ce248588fbc8b2f60a59b1c6efb3a870 Mon Sep 17 00:00:00 2001 From: Artem Tataurov Date: Tue, 16 May 2023 17:24:10 +0300 Subject: [PATCH] [#101] app: Refactor the default copies number setting Signed-off-by: Artem Tataurov --- api/handler/api.go | 10 +++++----- api/handler/api_test.go | 4 ++-- cmd/s3-gw/app.go | 15 ++++++++------- cmd/s3-gw/app_settings.go | 24 ++++++++++++++++++++---- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/api/handler/api.go b/api/handler/api.go index 59810abaa..7e3845037 100644 --- a/api/handler/api.go +++ b/api/handler/api.go @@ -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) { diff --git a/api/handler/api_test.go b/api/handler/api_test.go index 46ed8c8c6..0c86eb118 100644 --- a/api/handler/api_test.go +++ b/api/handler/api_test.go @@ -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, diff --git a/cmd/s3-gw/app.go b/cmd/s3-gw/app.go index 0c3a0350e..7f23a4836 100644 --- a/cmd/s3-gw/app.go +++ b/cmd/s3-gw/app.go @@ -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 diff --git a/cmd/s3-gw/app_settings.go b/cmd/s3-gw/app_settings.go index d75054664..5575623df 100644 --- a/cmd/s3-gw/app_settings.go +++ b/cmd/s3-gw/app_settings.go @@ -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)) }