From e952fdbecc54c69c479b8ed5b50f974afdceb4ec Mon Sep 17 00:00:00 2001 From: Akash Chandra Date: Tue, 29 Oct 2024 12:29:58 +0530 Subject: [PATCH] fix: TOOLS-2979 add 128M multiple validation --- asconfig/conffilereader.go | 8 ++++++++ asconfig/utils.go | 42 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/asconfig/conffilereader.go b/asconfig/conffilereader.go index 28754f9..0f26774 100644 --- a/asconfig/conffilereader.go +++ b/asconfig/conffilereader.go @@ -181,8 +181,16 @@ func writeConf(log logr.Logger, tok []string, conf Conf) error { // Handle human readable content if ok, humanizeFn := isSizeOrTime(cfgName); ok { conf[cfgName], _ = humanizeFn(tok[1]) + + // TOOLS-2979 Handle special case for sindex-stage-size + err := is128MPowerOf2MultipleValid(cfgName, conf[cfgName]) + if err != nil { + return err + } + return nil } + // More special Case handling switch cfgName { case "context": diff --git a/asconfig/utils.go b/asconfig/utils.go index 3fc2e18..0d0fa20 100644 --- a/asconfig/utils.go +++ b/asconfig/utils.go @@ -29,6 +29,8 @@ const ( NONE sysproptype = "NONE" ) +const SIZE_128M = 1024 * 1024 * 128 + var portRegex = regexp.MustCompile("port") type humanize func(string) (uint64, error) @@ -759,6 +761,46 @@ func isSizeOrTime(key string) (bool, humanize) { } } +// Special size based fields generally like 128M, 256M etc. +// Allowed values are possible with power of 2s multiple 128M. +func is128MPowerOf2MultipleField(key string) bool { + switch key { + case "sindex-stage-size": + return true + } + + return false +} + +// is128MPowerOf2MultipleValid checks if the given value is a valid multiple of 128M and a power of 2. +// It first verifies if the key is a 128M power of 2 multiple field. If not, it returns nil. +// Then, it checks if the value is of type uint64. If not, it returns an error indicating an invalid value type. +// Finally, it checks if the value is a non-zero multiple of 128M and a power of 2. If so, it returns nil. +// Otherwise, it returns an error indicating an invalid value. +// +// Parameters: +// - key: The key to check if it is a 128M power of 2 multiple field. +// - val: The value to validate. +// +// Returns: +// - error: An error if the value is invalid, otherwise nil. +func is128MPowerOf2MultipleValid(key string, val interface{}) error { + if !is128MPowerOf2MultipleField(key) { + return nil + } + + specFieldValue, ok := val.(uint64) + if !ok { + return fmt.Errorf("invalid value type for %s: %v", key, val) + } + + if specFieldValue != 0 && (specFieldValue%SIZE_128M == 0 && (specFieldValue/SIZE_128M&(specFieldValue/SIZE_128M-1)) == 0) { + return nil + } + + return fmt.Errorf("invalid value for %s: %v", key, val) +} + func isStorageEngineKey(key string) bool { if key == keyStorageEngine || strings.Contains(key, keyStorageEngine+".") { return true