Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tools 2979: Add Special Validation for Size Fields #61

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
44 changes: 44 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 size128M = 1024 * 1024 * 128

var portRegex = regexp.MustCompile("port")

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

default:
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%size128M == 0 &&
(specFieldValue/size128M&(specFieldValue/size128M-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
Loading