github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/config/heal/heal.go (about) 1 // Copyright (c) 2015-2021 MinIO, Inc. 2 // 3 // This file is part of MinIO Object Storage stack 4 // 5 // This program is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU Affero General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU Affero General Public License for more details. 14 // 15 // You should have received a copy of the GNU Affero General Public License 16 // along with this program. If not, see <http://www.gnu.org/licenses/>. 17 18 package heal 19 20 import ( 21 "errors" 22 "fmt" 23 "strconv" 24 "strings" 25 "sync" 26 "time" 27 28 "github.com/minio/minio/internal/config" 29 "github.com/minio/pkg/v2/env" 30 ) 31 32 // Compression environment variables 33 const ( 34 Bitrot = "bitrotscan" 35 Sleep = "max_sleep" 36 IOCount = "max_io" 37 DriveWorkers = "drive_workers" 38 39 EnvBitrot = "MINIO_HEAL_BITROTSCAN" 40 EnvSleep = "MINIO_HEAL_MAX_SLEEP" 41 EnvIOCount = "MINIO_HEAL_MAX_IO" 42 EnvDriveWorkers = "MINIO_HEAL_DRIVE_WORKERS" 43 ) 44 45 var configMutex sync.RWMutex 46 47 // Config represents the heal settings. 48 type Config struct { 49 // Bitrot will perform bitrot scan on local disk when checking objects. 50 Bitrot string `json:"bitrotscan"` 51 52 // maximum sleep duration between objects to slow down heal operation. 53 Sleep time.Duration `json:"sleep"` 54 IOCount int `json:"iocount"` 55 56 DriveWorkers int `json:"drive_workers"` 57 58 // Cached value from Bitrot field 59 cache struct { 60 // -1: bitrot enabled, 0: bitrot disabled, > 0: bitrot cycle 61 bitrotCycle time.Duration 62 } 63 } 64 65 // BitrotScanCycle returns the configured cycle for the scanner healing 66 // -1 for not enabled 67 // 68 // 0 for contiunous bitrot scanning 69 // 70 // >0 interval duration between cycles 71 func (opts Config) BitrotScanCycle() (d time.Duration) { 72 configMutex.RLock() 73 defer configMutex.RUnlock() 74 return opts.cache.bitrotCycle 75 } 76 77 // Clone safely the heal configuration 78 func (opts Config) Clone() (int, time.Duration, string) { 79 configMutex.RLock() 80 defer configMutex.RUnlock() 81 return opts.IOCount, opts.Sleep, opts.Bitrot 82 } 83 84 // GetWorkers returns the number of workers, -1 is none configured 85 func (opts Config) GetWorkers() int { 86 configMutex.RLock() 87 defer configMutex.RUnlock() 88 return opts.DriveWorkers 89 } 90 91 // Update updates opts with nopts 92 func (opts *Config) Update(nopts Config) { 93 configMutex.Lock() 94 defer configMutex.Unlock() 95 96 opts.Bitrot = nopts.Bitrot 97 opts.IOCount = nopts.IOCount 98 opts.Sleep = nopts.Sleep 99 opts.DriveWorkers = nopts.DriveWorkers 100 101 opts.cache.bitrotCycle, _ = parseBitrotConfig(nopts.Bitrot) 102 } 103 104 // DefaultKVS - default KV config for heal settings 105 var DefaultKVS = config.KVS{ 106 config.KV{ 107 Key: Bitrot, 108 Value: config.EnableOff, 109 }, 110 config.KV{ 111 Key: Sleep, 112 Value: "250ms", 113 }, 114 config.KV{ 115 Key: IOCount, 116 Value: "100", 117 }, 118 config.KV{ 119 Key: DriveWorkers, 120 Value: "", 121 }, 122 } 123 124 const minimumBitrotCycleInMonths = 1 125 126 func parseBitrotConfig(s string) (time.Duration, error) { 127 // Try to parse as a boolean 128 enabled, err := config.ParseBool(s) 129 if err == nil { 130 switch enabled { 131 case true: 132 return 0, nil 133 case false: 134 return -1, nil 135 } 136 } 137 138 // Try to parse as a number of months 139 if !strings.HasSuffix(s, "m") { 140 return -1, errors.New("unknown format") 141 } 142 143 months, err := strconv.Atoi(strings.TrimSuffix(s, "m")) 144 if err != nil { 145 return -1, err 146 } 147 148 if months < minimumBitrotCycleInMonths { 149 return -1, fmt.Errorf("minimum bitrot cycle is %d month(s)", minimumBitrotCycleInMonths) 150 } 151 152 return time.Duration(months) * 30 * 24 * time.Hour, nil 153 } 154 155 // LookupConfig - lookup config and override with valid environment settings if any. 156 func LookupConfig(kvs config.KVS) (cfg Config, err error) { 157 if err = config.CheckValidKeys(config.HealSubSys, kvs, DefaultKVS); err != nil { 158 return cfg, err 159 } 160 cfg.Bitrot = env.Get(EnvBitrot, kvs.GetWithDefault(Bitrot, DefaultKVS)) 161 _, err = parseBitrotConfig(cfg.Bitrot) 162 if err != nil { 163 return cfg, fmt.Errorf("'heal:bitrotscan' value invalid: %w", err) 164 } 165 cfg.Sleep, err = time.ParseDuration(env.Get(EnvSleep, kvs.GetWithDefault(Sleep, DefaultKVS))) 166 if err != nil { 167 return cfg, fmt.Errorf("'heal:max_sleep' value invalid: %w", err) 168 } 169 cfg.IOCount, err = strconv.Atoi(env.Get(EnvIOCount, kvs.GetWithDefault(IOCount, DefaultKVS))) 170 if err != nil { 171 return cfg, fmt.Errorf("'heal:max_io' value invalid: %w", err) 172 } 173 if ws := env.Get(EnvDriveWorkers, kvs.GetWithDefault(DriveWorkers, DefaultKVS)); ws != "" { 174 w, err := strconv.Atoi(ws) 175 if err != nil { 176 return cfg, fmt.Errorf("'heal:drive_workers' value invalid: %w", err) 177 } 178 if w < 1 { 179 return cfg, fmt.Errorf("'heal:drive_workers' value invalid: zero or negative integer unsupported") 180 } 181 cfg.DriveWorkers = w 182 } else { 183 cfg.DriveWorkers = -1 184 } 185 186 return cfg, nil 187 }