Skip to content

Commit

Permalink
fix: TOOLS-2979 add 128M multiple validation
Browse files Browse the repository at this point in the history
  • Loading branch information
a-spiker committed Oct 29, 2024
1 parent 48b424c commit e952fdb
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
8 changes: 8 additions & 0 deletions asconfig/conffilereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down
42 changes: 42 additions & 0 deletions asconfig/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ const (
NONE sysproptype = "NONE"
)

const SIZE_128M = 1024 * 1024 * 128

Check warning on line 32 in asconfig/utils.go

View workflow job for this annotation

GitHub Actions / lint

var-naming: don't use ALL_CAPS in Go names; use CamelCase (revive)

var portRegex = regexp.MustCompile("port")

type humanize func(string) (uint64, error)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e952fdb

Please sign in to comment.