github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/config/batch/batch.go (about) 1 // Copyright (c) 2015-2023 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 batch 19 20 import ( 21 "sync" 22 "time" 23 24 "github.com/minio/minio/internal/config" 25 "github.com/minio/pkg/v2/env" 26 ) 27 28 // Batch job environment variables 29 const ( 30 ReplicationWorkersWait = "replication_workers_wait" 31 KeyRotationWorkersWait = "keyrotation_workers_wait" 32 ExpirationWorkersWait = "expiration_workers_wait" 33 34 EnvReplicationWorkersWait = "MINIO_BATCH_REPLICATION_WORKERS_WAIT" 35 EnvKeyRotationWorkersWait = "MINIO_BATCH_KEYROTATION_WORKERS_WAIT" 36 EnvKeyExpirationWorkersWait = "MINIO_BATCH_EXPIRATION_WORKERS_WAIT" 37 ) 38 39 var configMu sync.RWMutex 40 41 // Config represents the batch job settings. 42 type Config struct { 43 ReplicationWorkersWait time.Duration `json:"replicationWorkersWait"` 44 KeyRotationWorkersWait time.Duration `json:"keyRotationWorkersWait"` 45 ExpirationWorkersWait time.Duration `json:"expirationWorkersWait"` 46 } 47 48 // ExpirationWait returns the duration for which a batch expiration worker 49 // would wait before working on next object. 50 func (opts Config) ExpirationWait() time.Duration { 51 configMu.RLock() 52 defer configMu.RUnlock() 53 54 return opts.ExpirationWorkersWait 55 } 56 57 // ReplicationWait returns the duration for which a batch replication worker 58 // would wait before working on next object. 59 func (opts Config) ReplicationWait() time.Duration { 60 configMu.RLock() 61 defer configMu.RUnlock() 62 63 return opts.ReplicationWorkersWait 64 } 65 66 // KeyRotationWait returns the duration for which a batch key-rotation worker 67 // would wait before working on next object. 68 func (opts Config) KeyRotationWait() time.Duration { 69 configMu.RLock() 70 defer configMu.RUnlock() 71 72 return opts.KeyRotationWorkersWait 73 } 74 75 // Clone returns a copy of Config value 76 func (opts Config) Clone() Config { 77 configMu.RLock() 78 defer configMu.RUnlock() 79 80 return Config{ 81 ReplicationWorkersWait: opts.ReplicationWorkersWait, 82 KeyRotationWorkersWait: opts.KeyRotationWorkersWait, 83 ExpirationWorkersWait: opts.ExpirationWorkersWait, 84 } 85 } 86 87 // Update updates opts with nopts 88 func (opts *Config) Update(nopts Config) { 89 configMu.Lock() 90 defer configMu.Unlock() 91 92 opts.ReplicationWorkersWait = nopts.ReplicationWorkersWait 93 opts.KeyRotationWorkersWait = nopts.KeyRotationWorkersWait 94 opts.ExpirationWorkersWait = nopts.ExpirationWorkersWait 95 } 96 97 // DefaultKVS - default KV config for batch job settings 98 var DefaultKVS = config.KVS{ 99 config.KV{ 100 Key: ReplicationWorkersWait, 101 Value: "0ms", // No wait by default between each replication attempts. 102 }, 103 config.KV{ 104 Key: KeyRotationWorkersWait, 105 Value: "0ms", // No wait by default between each key rotation attempts. 106 }, 107 config.KV{ 108 Key: ExpirationWorkersWait, 109 Value: "0ms", // No wait by default between each expiration attempts. 110 }, 111 } 112 113 // LookupConfig - lookup config and override with valid environment settings if any. 114 func LookupConfig(kvs config.KVS) (cfg Config, err error) { 115 if err = config.CheckValidKeys(config.BatchSubSys, kvs, DefaultKVS); err != nil { 116 return cfg, err 117 } 118 119 cfg.ReplicationWorkersWait = 0 120 cfg.KeyRotationWorkersWait = 0 121 cfg.ExpirationWorkersWait = 0 122 123 rduration, err := time.ParseDuration(env.Get(EnvReplicationWorkersWait, kvs.GetWithDefault(ReplicationWorkersWait, DefaultKVS))) 124 if err != nil { 125 return cfg, err 126 } 127 if rduration < 0 { 128 return cfg, config.ErrInvalidBatchReplicationWorkersWait(nil) 129 } 130 131 kduration, err := time.ParseDuration(env.Get(EnvKeyRotationWorkersWait, kvs.GetWithDefault(KeyRotationWorkersWait, DefaultKVS))) 132 if err != nil { 133 return cfg, err 134 } 135 if kduration < 0 { 136 return cfg, config.ErrInvalidBatchKeyRotationWorkersWait(nil) 137 } 138 139 eduration, err := time.ParseDuration(env.Get(EnvKeyExpirationWorkersWait, kvs.GetWithDefault(ExpirationWorkersWait, DefaultKVS))) 140 if err != nil { 141 return cfg, err 142 } 143 if eduration < 0 { 144 return cfg, config.ErrInvalidBatchExpirationWorkersWait(nil) 145 } 146 147 if rduration > 0 { 148 cfg.ReplicationWorkersWait = rduration 149 } 150 151 if kduration > 0 { 152 cfg.KeyRotationWorkersWait = kduration 153 } 154 155 if eduration > 0 { 156 cfg.ExpirationWorkersWait = eduration 157 } 158 159 return cfg, nil 160 }