Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package conf
import (
"fmt"
"clusterd/pkg/common/constant"
)
const (
MinFaultWindowHours = 1
MaxFaultWindowHours = 720
MinFaultThreshold = 1
MaxFaultThreshold = 50
MinFaultFreeHours = 1
MaxFaultFreeHours = 240
NotRelease = -1
)
var config GlobalConfig
type GlobalConfig struct {
ManuallySeparatePolicy
}
type ManuallySeparatePolicy struct {
Enabled bool `yaml:"enabled"`
Separate struct {
FaultWindowHours int `yaml:"fault_window_hours"`
FaultThreshold int `yaml:"fault_threshold"`
} `yaml:"separate"`
Release struct {
FaultFreeHours int `yaml:"fault_free_hours"`
} `yaml:"release"`
}
func GetManualEnabled() bool {
return config.ManuallySeparatePolicy.Enabled
}
func GetSeparateWindow() int64 {
return int64(config.ManuallySeparatePolicy.Separate.FaultWindowHours * constant.HoursToMilliseconds)
}
func GetSeparateThreshold() int {
return config.ManuallySeparatePolicy.Separate.FaultThreshold
}
func GetReleaseDuration() int64 {
if IsReleaseEnable() {
return int64(config.ManuallySeparatePolicy.Release.FaultFreeHours * constant.HoursToMilliseconds)
}
return NotRelease
}
func SetManualSeparatePolicy(policy ManuallySeparatePolicy) {
config.ManuallySeparatePolicy = policy
}
func IsReleaseEnable() bool {
return config.ManuallySeparatePolicy.Release.FaultFreeHours != NotRelease
}
func Check(policy ManuallySeparatePolicy) error {
if policy.Separate.FaultWindowHours < MinFaultWindowHours || policy.Separate.FaultWindowHours > MaxFaultWindowHours {
return fmt.Errorf("fault_window_hours must be in [%d, %d]", MinFaultWindowHours, MaxFaultWindowHours)
}
if policy.Separate.FaultThreshold < MinFaultThreshold || policy.Separate.FaultThreshold > MaxFaultThreshold {
return fmt.Errorf("fault_threshold must be in [%d, %d]", MinFaultThreshold, MaxFaultThreshold)
}
if policy.Release.FaultFreeHours == NotRelease {
return nil
}
if policy.Release.FaultFreeHours < MinFaultFreeHours || policy.Release.FaultFreeHours > MaxFaultFreeHours {
return fmt.Errorf("fault_free_hours must be in [%d, %d] or %d", MinFaultFreeHours, MaxFaultFreeHours, NotRelease)
}
return nil
}