2024-03-14 06:38:33 -04:00
// Copyright (c) 2015-2024 MinIO, Inc.
2021-04-18 15:41:13 -04:00
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
2019-10-07 01:50:24 -04:00
package storageclass
import (
"encoding/json"
"fmt"
"strconv"
"strings"
2021-04-04 16:32:31 -04:00
"sync"
2019-10-07 01:50:24 -04:00
2021-06-01 17:59:40 -04:00
"github.com/minio/minio/internal/config"
2023-09-04 15:57:37 -04:00
"github.com/minio/pkg/v2/env"
2019-10-07 01:50:24 -04:00
)
// Standard constants for all storage class
const (
// Reduced redundancy storage class
RRS = "REDUCED_REDUNDANCY"
// Standard storage class
STANDARD = "STANDARD"
)
// Standard constats for config info storage class
const (
2019-10-23 01:59:13 -04:00
ClassStandard = "standard"
ClassRRS = "rrs"
2024-03-14 06:38:33 -04:00
ClassOptimize = "optimize"
2019-10-23 01:59:13 -04:00
2019-10-07 01:50:24 -04:00
// Reduced redundancy storage class environment variable
RRSEnv = "MINIO_STORAGE_CLASS_RRS"
// Standard storage class environment variable
StandardEnv = "MINIO_STORAGE_CLASS_STANDARD"
2024-03-14 06:38:33 -04:00
// Optimize storage class environment variable
OptimizeEnv = "MINIO_STORAGE_CLASS_OPTIMIZE"
2019-10-07 01:50:24 -04:00
// Supported storage class scheme is EC
schemePrefix = "EC"
2023-08-15 19:04:20 -04:00
// Min parity drives
minParityDrives = 0
2019-10-07 01:50:24 -04:00
// Default RRS parity is always minimum parity.
2022-06-27 23:22:18 -04:00
defaultRRSParity = 1
2019-10-07 01:50:24 -04:00
)
2019-10-23 01:59:13 -04:00
// DefaultKVS - default storage class config
var (
DefaultKVS = config . KVS {
2019-11-20 18:10:24 -05:00
config . KV {
Key : ClassStandard ,
Value : "" ,
} ,
config . KV {
Key : ClassRRS ,
2022-06-27 23:22:18 -04:00
Value : "EC:1" ,
2019-11-20 18:10:24 -05:00
} ,
2024-03-14 06:38:33 -04:00
config . KV {
Key : ClassOptimize ,
Value : "availability" ,
} ,
2019-10-23 01:59:13 -04:00
}
)
2019-10-07 01:50:24 -04:00
// StorageClass - holds storage class information
type StorageClass struct {
Parity int
}
2021-04-04 16:32:31 -04:00
// ConfigLock is a global lock for storage-class config
2022-06-27 23:22:18 -04:00
var ConfigLock sync . RWMutex
2021-04-04 16:32:31 -04:00
2019-10-07 01:50:24 -04:00
// Config storage class configuration
type Config struct {
2023-05-04 17:44:30 -04:00
Standard StorageClass ` json:"standard" `
RRS StorageClass ` json:"rrs" `
2024-03-14 06:38:33 -04:00
Optimize string ` json:"optimize" `
2023-05-04 17:44:30 -04:00
initialized bool
2019-10-07 01:50:24 -04:00
}
// UnmarshalJSON - Validate SS and RRS parity when unmarshalling JSON.
func ( sCfg * Config ) UnmarshalJSON ( data [ ] byte ) error {
type Alias Config
aux := & struct {
* Alias
} {
Alias : ( * Alias ) ( sCfg ) ,
}
return json . Unmarshal ( data , & aux )
}
// IsValid - returns true if input string is a valid
// storage class kind supported.
func IsValid ( sc string ) bool {
2021-01-16 15:08:02 -05:00
return sc == RRS || sc == STANDARD
2019-10-07 01:50:24 -04:00
}
// UnmarshalText unmarshals storage class from its textual form into
// storageClass structure.
func ( sc * StorageClass ) UnmarshalText ( b [ ] byte ) error {
scStr := string ( b )
if scStr == "" {
return nil
}
s , err := parseStorageClass ( scStr )
if err != nil {
return err
}
sc . Parity = s . Parity
return nil
}
// MarshalText - marshals storage class string.
func ( sc * StorageClass ) MarshalText ( ) ( [ ] byte , error ) {
if sc . Parity != 0 {
return [ ] byte ( fmt . Sprintf ( "%s:%d" , schemePrefix , sc . Parity ) ) , nil
}
2021-01-16 15:08:02 -05:00
return [ ] byte { } , nil
2019-10-07 01:50:24 -04:00
}
func ( sc * StorageClass ) String ( ) string {
if sc . Parity != 0 {
return fmt . Sprintf ( "%s:%d" , schemePrefix , sc . Parity )
}
2021-01-16 15:08:02 -05:00
return ""
2019-10-07 01:50:24 -04:00
}
// Parses given storageClassEnv and returns a storageClass structure.
2022-08-04 19:10:08 -04:00
// Supported Storage Class format is "Scheme:Number of parity drives".
2019-10-07 01:50:24 -04:00
// Currently only supported scheme is "EC".
func parseStorageClass ( storageClassEnv string ) ( sc StorageClass , err error ) {
s := strings . Split ( storageClassEnv , ":" )
2022-08-04 19:10:08 -04:00
// only two elements allowed in the string - "scheme" and "number of parity drives"
2019-10-07 01:50:24 -04:00
if len ( s ) > 2 {
return StorageClass { } , config . ErrStorageClassValue ( nil ) . Msg ( "Too many sections in " + storageClassEnv )
} else if len ( s ) < 2 {
return StorageClass { } , config . ErrStorageClassValue ( nil ) . Msg ( "Too few sections in " + storageClassEnv )
}
// only allowed scheme is "EC"
if s [ 0 ] != schemePrefix {
return StorageClass { } , config . ErrStorageClassValue ( nil ) . Msg ( "Unsupported scheme " + s [ 0 ] + ". Supported scheme is EC" )
}
2023-08-15 19:04:20 -04:00
// Number of parity drives should be integer
parityDrives , err := strconv . Atoi ( s [ 1 ] )
2019-10-07 01:50:24 -04:00
if err != nil {
return StorageClass { } , config . ErrStorageClassValue ( err )
}
2023-08-15 19:04:20 -04:00
if parityDrives < 0 {
2022-06-27 23:22:18 -04:00
return StorageClass { } , config . ErrStorageClassValue ( nil ) . Msg ( "Unsupported parity value " + s [ 1 ] + " provided" )
}
2019-10-07 01:50:24 -04:00
return StorageClass {
2023-08-15 19:04:20 -04:00
Parity : parityDrives ,
2019-10-07 01:50:24 -04:00
} , nil
}
2021-01-29 14:40:55 -05:00
// ValidateParity validate standard storage class parity.
func ValidateParity ( ssParity , setDriveCount int ) error {
2023-08-15 19:04:20 -04:00
// SS parity drives should be greater than or equal to minParityDrives.
// Parity below minParityDrives is not supported.
if ssParity > 0 && ssParity < minParityDrives {
2022-11-11 22:40:45 -05:00
return fmt . Errorf ( "parity %d should be greater than or equal to %d" ,
2023-08-15 19:04:20 -04:00
ssParity , minParityDrives )
2021-01-29 14:40:55 -05:00
}
if ssParity > setDriveCount / 2 {
2022-11-11 22:40:45 -05:00
return fmt . Errorf ( "parity %d should be less than or equal to %d" , ssParity , setDriveCount / 2 )
2021-01-29 14:40:55 -05:00
}
return nil
}
2023-08-15 19:04:20 -04:00
// Validates the parity drives.
2020-08-26 22:29:35 -04:00
func validateParity ( ssParity , rrsParity , setDriveCount int ) ( err error ) {
2023-08-15 19:04:20 -04:00
// SS parity drives should be greater than or equal to minParityDrives.
// Parity below minParityDrives is not supported.
if ssParity > 0 && ssParity < minParityDrives {
2019-10-07 01:50:24 -04:00
return fmt . Errorf ( "Standard storage class parity %d should be greater than or equal to %d" ,
2023-08-15 19:04:20 -04:00
ssParity , minParityDrives )
2019-10-07 01:50:24 -04:00
}
2023-08-15 19:04:20 -04:00
// RRS parity drives should be greater than or equal to minParityDrives.
// Parity below minParityDrives is not supported.
if rrsParity > 0 && rrsParity < minParityDrives {
return fmt . Errorf ( "Reduced redundancy storage class parity %d should be greater than or equal to %d" , rrsParity , minParityDrives )
2019-10-07 01:50:24 -04:00
}
2022-09-26 12:04:54 -04:00
if setDriveCount > 2 {
if ssParity > setDriveCount / 2 {
return fmt . Errorf ( "Standard storage class parity %d should be less than or equal to %d" , ssParity , setDriveCount / 2 )
}
2019-10-07 01:50:24 -04:00
2022-09-26 12:04:54 -04:00
if rrsParity > setDriveCount / 2 {
return fmt . Errorf ( "Reduced redundancy storage class parity %d should be less than or equal to %d" , rrsParity , setDriveCount / 2 )
}
2019-10-07 01:50:24 -04:00
}
if ssParity > 0 && rrsParity > 0 {
2023-03-23 17:06:22 -04:00
if ssParity < rrsParity {
2022-08-04 19:10:08 -04:00
return fmt . Errorf ( "Standard storage class parity drives %d should be greater than or equal to Reduced redundancy storage class parity drives %d" , ssParity , rrsParity )
2019-10-07 01:50:24 -04:00
}
}
return nil
}
// GetParityForSC - Returns the data and parity drive count based on storage class
2021-01-16 15:08:02 -05:00
// If storage class is set using the env vars MINIO_STORAGE_CLASS_RRS and
// MINIO_STORAGE_CLASS_STANDARD or server config fields corresponding values are
// returned.
//
// -- if input storage class is empty then standard is assumed
2022-08-26 15:52:29 -04:00
//
2023-05-04 17:44:30 -04:00
// -- if input is RRS but RRS is not configured/initialized '-1' parity
2022-08-26 15:52:29 -04:00
//
2023-05-04 17:44:30 -04:00
// for RRS is assumed, the caller is expected to choose the right parity
// at that point.
//
// -- if input is STANDARD but STANDARD is not configured/initialized '-1' parity
2022-08-26 15:52:29 -04:00
//
// is returned, the caller is expected to choose the right parity
// at that point.
2023-10-24 11:15:41 -04:00
func ( sCfg * Config ) GetParityForSC ( sc string ) ( parity int ) {
2021-04-04 16:32:31 -04:00
ConfigLock . RLock ( )
defer ConfigLock . RUnlock ( )
2019-10-07 01:50:24 -04:00
switch strings . TrimSpace ( sc ) {
case RRS :
2023-05-04 17:44:30 -04:00
if ! sCfg . initialized {
return - 1
}
2019-10-07 01:50:24 -04:00
return sCfg . RRS . Parity
default :
2023-05-04 17:44:30 -04:00
if ! sCfg . initialized {
return - 1
}
2019-10-07 01:50:24 -04:00
return sCfg . Standard . Parity
}
}
2024-03-14 06:38:33 -04:00
// CapacityOptimized - returns true if the storage-class is capacity optimized
// meaning we will not use additional parities when drives are offline.
//
// Default is "availability" optimized, unless this is configured.
func ( sCfg * Config ) CapacityOptimized ( ) bool {
ConfigLock . RLock ( )
defer ConfigLock . RUnlock ( )
if ! sCfg . initialized {
return false
}
return sCfg . Optimize == "capacity"
}
// AvailabilityOptimized - returns true if the storage-class is availability
// optimized, meaning we will use additional parities when drives are offline
// to retain parity SLA.
//
// Default is "availability" optimized.
func ( sCfg * Config ) AvailabilityOptimized ( ) bool {
ConfigLock . RLock ( )
defer ConfigLock . RUnlock ( )
if ! sCfg . initialized {
return true
}
return sCfg . Optimize == "availability" || sCfg . Optimize == ""
}
2021-04-04 16:32:31 -04:00
// Update update storage-class with new config
2021-12-29 01:48:43 -05:00
func ( sCfg * Config ) Update ( newCfg Config ) {
2021-04-04 16:32:31 -04:00
ConfigLock . Lock ( )
defer ConfigLock . Unlock ( )
sCfg . RRS = newCfg . RRS
sCfg . Standard = newCfg . Standard
2024-03-14 06:38:33 -04:00
sCfg . Optimize = newCfg . Optimize
2023-05-04 17:44:30 -04:00
sCfg . initialized = true
2021-04-04 16:32:31 -04:00
}
2023-05-04 17:44:30 -04:00
// Enabled returns if storageClass is enabled is enabled.
2019-12-04 18:32:37 -05:00
func Enabled ( kvs config . KVS ) bool {
ssc := kvs . Get ( ClassStandard )
rrsc := kvs . Get ( ClassRRS )
return ssc != "" || rrsc != ""
}
2022-06-27 23:22:18 -04:00
// DefaultParityBlocks returns default parity blocks for 'drive' count
func DefaultParityBlocks ( drive int ) int {
switch drive {
case 1 :
return 0
case 3 , 2 :
return 1
case 4 , 5 :
return 2
case 6 , 7 :
return 3
default :
return 4
}
}
2019-10-07 01:50:24 -04:00
// LookupConfig - lookup storage class config and override with valid environment settings if any.
2020-12-01 14:59:03 -05:00
func LookupConfig ( kvs config . KVS , setDriveCount int ) ( cfg Config , err error ) {
2019-10-23 01:59:13 -04:00
cfg = Config { }
2023-06-22 13:04:02 -04:00
deprecatedKeys := [ ] string {
"dma" ,
}
2021-07-28 14:20:16 -04:00
2023-06-22 13:04:02 -04:00
if err = config . CheckValidKeys ( config . StorageClassSubSys , kvs , DefaultKVS , deprecatedKeys ... ) ; err != nil {
2020-08-14 22:48:04 -04:00
return Config { } , err
2019-10-23 01:59:13 -04:00
}
2019-11-11 15:01:21 -05:00
ssc := env . Get ( StandardEnv , kvs . Get ( ClassStandard ) )
rrsc := env . Get ( RRSEnv , kvs . Get ( ClassRRS ) )
2019-10-07 01:50:24 -04:00
// Check for environment variables and parse into storageClass struct
2019-11-11 15:01:21 -05:00
if ssc != "" {
2019-10-07 01:50:24 -04:00
cfg . Standard , err = parseStorageClass ( ssc )
if err != nil {
2020-08-14 22:48:04 -04:00
return Config { } , err
2019-10-07 01:50:24 -04:00
}
2022-09-26 12:04:54 -04:00
} else {
cfg . Standard . Parity = DefaultParityBlocks ( setDriveCount )
2019-10-09 02:11:15 -04:00
}
2019-10-07 01:50:24 -04:00
2019-11-11 15:01:21 -05:00
if rrsc != "" {
2019-10-07 01:50:24 -04:00
cfg . RRS , err = parseStorageClass ( rrsc )
if err != nil {
2020-08-14 22:48:04 -04:00
return Config { } , err
2019-10-07 01:50:24 -04:00
}
2022-09-26 12:04:54 -04:00
} else {
2019-10-09 02:11:15 -04:00
cfg . RRS . Parity = defaultRRSParity
2022-09-26 12:04:54 -04:00
if setDriveCount == 1 {
cfg . RRS . Parity = 0
}
2022-06-27 23:22:18 -04:00
}
2019-10-07 01:50:24 -04:00
// Validation is done after parsing both the storage classes. This is needed because we need one
// storage class value to deduce the correct value of the other storage class.
2020-08-26 22:29:35 -04:00
if err = validateParity ( cfg . Standard . Parity , cfg . RRS . Parity , setDriveCount ) ; err != nil {
2023-01-10 02:07:45 -05:00
return Config { } , err
2019-10-07 01:50:24 -04:00
}
2023-05-04 17:44:30 -04:00
cfg . initialized = true
2024-03-14 06:38:33 -04:00
cfg . Optimize = env . Get ( OptimizeEnv , kvs . Get ( ClassOptimize ) )
2019-10-07 01:50:24 -04:00
return cfg , nil
}