github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/settings/int.go (about) 1 // Copyright 2017 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package settings 12 13 import "github.com/cockroachdb/errors" 14 15 // IntSetting is the interface of a setting variable that will be 16 // updated automatically when the corresponding cluster-wide setting 17 // of type "int" is updated. 18 type IntSetting struct { 19 common 20 defaultValue int64 21 validateFn func(int64) error 22 } 23 24 var _ extendedSetting = &IntSetting{} 25 26 // Get retrieves the int value in the setting. 27 func (i *IntSetting) Get(sv *Values) int64 { 28 return sv.container.getInt64(i.slotIdx) 29 } 30 31 func (i *IntSetting) String(sv *Values) string { 32 return EncodeInt(i.Get(sv)) 33 } 34 35 // Encoded returns the encoded value of the current value of the setting. 36 func (i *IntSetting) Encoded(sv *Values) string { 37 return i.String(sv) 38 } 39 40 // EncodedDefault returns the encoded value of the default value of the setting. 41 func (i *IntSetting) EncodedDefault() string { 42 return EncodeInt(i.defaultValue) 43 } 44 45 // Typ returns the short (1 char) string denoting the type of setting. 46 func (*IntSetting) Typ() string { 47 return "i" 48 } 49 50 // Validate that a value conforms with the validation function. 51 func (i *IntSetting) Validate(v int64) error { 52 if i.validateFn != nil { 53 if err := i.validateFn(v); err != nil { 54 return err 55 } 56 } 57 return nil 58 } 59 60 // Override changes the setting without validation and also overrides the 61 // default value. 62 // 63 // For testing usage only. 64 func (i *IntSetting) Override(sv *Values, v int64) { 65 sv.setInt64(i.slotIdx, v) 66 sv.setDefaultOverrideInt64(i.slotIdx, v) 67 } 68 69 func (i *IntSetting) set(sv *Values, v int64) error { 70 if err := i.Validate(v); err != nil { 71 return err 72 } 73 sv.setInt64(i.slotIdx, v) 74 return nil 75 } 76 77 func (i *IntSetting) setToDefault(sv *Values) { 78 // See if the default value was overridden. 79 ok, val, _ := sv.getDefaultOverride(i.slotIdx) 80 if ok { 81 // As per the semantics of override, these values don't go through 82 // validation. 83 _ = i.set(sv, val) 84 return 85 } 86 if err := i.set(sv, i.defaultValue); err != nil { 87 panic(err) 88 } 89 } 90 91 // Default returns the default value. 92 func (i *IntSetting) Default() int64 { 93 return i.defaultValue 94 } 95 96 // RegisterIntSetting defines a new setting with type int. 97 func RegisterIntSetting(key, desc string, defaultValue int64) *IntSetting { 98 return RegisterValidatedIntSetting(key, desc, defaultValue, nil) 99 } 100 101 // RegisterPublicIntSetting defines a new setting with type int and makes it public. 102 func RegisterPublicIntSetting(key, desc string, defaultValue int64) *IntSetting { 103 s := RegisterValidatedIntSetting(key, desc, defaultValue, nil) 104 s.SetVisibility(Public) 105 return s 106 } 107 108 // RegisterNonNegativeIntSetting defines a new setting with type int. 109 func RegisterNonNegativeIntSetting(key, desc string, defaultValue int64) *IntSetting { 110 return RegisterValidatedIntSetting(key, desc, defaultValue, func(v int64) error { 111 if v < 0 { 112 return errors.Errorf("cannot set %s to a negative value: %d", key, v) 113 } 114 return nil 115 }) 116 } 117 118 // RegisterPositiveIntSetting defines a new setting with type int. 119 func RegisterPositiveIntSetting(key, desc string, defaultValue int64) *IntSetting { 120 return RegisterValidatedIntSetting(key, desc, defaultValue, func(v int64) error { 121 if v < 1 { 122 return errors.Errorf("cannot set %s to a value < 1: %d", key, v) 123 } 124 return nil 125 }) 126 } 127 128 // RegisterValidatedIntSetting defines a new setting with type int with a 129 // validation function. 130 func RegisterValidatedIntSetting( 131 key, desc string, defaultValue int64, validateFn func(int64) error, 132 ) *IntSetting { 133 if validateFn != nil { 134 if err := validateFn(defaultValue); err != nil { 135 panic(errors.Wrap(err, "invalid default")) 136 } 137 } 138 setting := &IntSetting{ 139 defaultValue: defaultValue, 140 validateFn: validateFn, 141 } 142 register(key, desc, setting) 143 return setting 144 }