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

[ADDED] Ability to express the max_file_store and max_memory_store as a string (100M, etc..) #2777

Merged
merged 5 commits into from
Jan 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 44 additions & 2 deletions server/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,40 @@ func parseJetStreamForAccount(v interface{}, acc *Account, errors *[]error, warn
return nil
}

// takes in a storage size as either an int or a string and returns an int64 value based on the input.
func getStorageSize(v interface{}) (int64, error) {
_, ok := v.(int64)
if ok {
return v.(int64), nil
}

s, ok := v.(string)
if !ok {
return 0, fmt.Errorf("must be int64 or string")
}

if s == "" {
return 0, nil
}

suffix := s[len(s)-1:]
prefix := s[:len(s)-1]
num, err := strconv.ParseInt(prefix, 10, 64)
if err != nil {
return 0, err
}

suffixMap := map[string]int64{"K": 10, "M": 20, "G": 30, "T": 40}

mult, ok := suffixMap[suffix]
if !ok {
return 0, fmt.Errorf("sizes defined as strings must end in K, M, G, T")
}
num *= 1 << mult

return num, nil
}

// Parse enablement of jetstream for a server.
func parseJetStream(v interface{}, opts *Options, errors *[]error, warnings *[]error) error {
var lt token
Expand Down Expand Up @@ -1711,10 +1745,18 @@ func parseJetStream(v interface{}, opts *Options, errors *[]error, warnings *[]e
}
opts.StoreDir = mv.(string)
case "max_memory_store", "max_mem_store", "max_mem":
opts.JetStreamMaxMemory = mv.(int64)
s, err := getStorageSize(mv)
if err != nil {
return &configErr{tk, fmt.Sprintf("max_mem_store %s", err)}
}
opts.JetStreamMaxMemory = s
opts.maxMemSet = true
case "max_file_store", "max_file":
opts.JetStreamMaxStore = mv.(int64)
s, err := getStorageSize(mv)
if err != nil {
return &configErr{tk, fmt.Sprintf("max_file_store %s", err)}
}
opts.JetStreamMaxStore = s
opts.maxStoreSet = true
case "domain":
opts.JetStreamDomain = mv.(string)
Expand Down
29 changes: 29 additions & 0 deletions server/opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3256,3 +3256,32 @@ func TestMaxSubTokens(t *testing.T) {
t.Fatal("Did not get the permissions error")
}
}

func TestGetStorageSize(t *testing.T) {
tt := []struct {
input string
want int64
err bool
}{
{"1K", 1024, false},
{"1M", 1048576, false},
{"1G", 1073741824, false},
{"1T", 1099511627776, false},
{"1L", 0, true},
hooksie1 marked this conversation as resolved.
Show resolved Hide resolved
{"TT", 0, true},
{"", 0, false},
}

for _, v := range tt {
var testErr bool
got, err := getStorageSize(v.input)
if err != nil {
testErr = true
}

if got != v.want || v.err != testErr {
t.Errorf("Got: %v, want %v with error: %v", got, v.want, testErr)
}
}

}