2019-10-07 01:50:24 -04:00
/ *
* MinIO Cloud Storage , ( C ) 2019 MinIO , Inc .
*
* 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 storageclass
import (
"encoding/json"
2020-10-30 14:04:29 -04:00
"errors"
2019-10-07 01:50:24 -04:00
"fmt"
"strconv"
"strings"
2021-04-04 16:32:31 -04:00
"sync"
2019-10-07 01:50:24 -04:00
"github.com/minio/minio/cmd/config"
"github.com/minio/minio/pkg/env"
)
// Standard constants for all storage class
const (
// Reduced redundancy storage class
RRS = "REDUCED_REDUNDANCY"
// Standard storage class
STANDARD = "STANDARD"
2020-10-30 14:04:29 -04:00
// DMA storage class
DMA = "DMA"
2020-11-02 20:21:56 -05:00
// Valid values are "write" and "read+write"
2020-10-30 14:04:29 -04:00
DMAWrite = "write"
2020-11-02 20:21:56 -05:00
DMAReadWrite = "read+write"
2019-10-07 01:50:24 -04:00
)
// Standard constats for config info storage class
const (
2019-10-23 01:59:13 -04:00
ClassStandard = "standard"
ClassRRS = "rrs"
2020-10-30 14:04:29 -04:00
ClassDMA = "dma"
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"
2020-10-30 14:04:29 -04:00
// DMA storage class environment variable
DMAEnv = "MINIO_STORAGE_CLASS_DMA"
2019-10-07 01:50:24 -04:00
// Supported storage class scheme is EC
schemePrefix = "EC"
// Min parity disks
minParityDisks = 2
// Default RRS parity is always minimum parity.
defaultRRSParity = minParityDisks
2020-10-30 14:04:29 -04:00
// Default DMA value
2021-01-22 18:38:21 -05:00
defaultDMA = DMAReadWrite
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 ,
Value : "EC:2" ,
} ,
2020-10-30 14:04:29 -04:00
config . KV {
Key : ClassDMA ,
Value : defaultDMA ,
} ,
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
var ConfigLock = sync . RWMutex { }
2019-10-07 01:50:24 -04:00
// Config storage class configuration
type Config struct {
Standard StorageClass ` json:"standard" `
RRS StorageClass ` json:"rrs" `
2021-01-16 15:08:02 -05:00
DMA string ` json:"dma" `
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.
// Supported Storage Class format is "Scheme:Number of parity disks".
// Currently only supported scheme is "EC".
func parseStorageClass ( storageClassEnv string ) ( sc StorageClass , err error ) {
s := strings . Split ( storageClassEnv , ":" )
// only two elements allowed in the string - "scheme" and "number of parity disks"
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" )
}
// Number of parity disks should be integer
parityDisks , err := strconv . Atoi ( s [ 1 ] )
if err != nil {
return StorageClass { } , config . ErrStorageClassValue ( err )
}
return StorageClass {
Parity : parityDisks ,
} , nil
}
2021-01-29 14:40:55 -05:00
// ValidateParity validate standard storage class parity.
func ValidateParity ( ssParity , setDriveCount int ) error {
// SS parity disks should be greater than or equal to minParityDisks.
// Parity below minParityDisks is not supported.
if ssParity > 0 && ssParity < minParityDisks {
return fmt . Errorf ( "Standard storage class parity %d should be greater than or equal to %d" ,
ssParity , minParityDisks )
}
if ssParity > setDriveCount / 2 {
return fmt . Errorf ( "Standard storage class parity %d should be less than or equal to %d" , ssParity , setDriveCount / 2 )
}
return nil
}
2019-10-07 01:50:24 -04:00
// Validates the parity disks.
2020-08-26 22:29:35 -04:00
func validateParity ( ssParity , rrsParity , setDriveCount int ) ( err error ) {
2019-10-07 01:50:24 -04:00
// SS parity disks should be greater than or equal to minParityDisks.
// Parity below minParityDisks is not supported.
2021-01-16 20:32:25 -05:00
if ssParity > 0 && ssParity < minParityDisks {
2019-10-07 01:50:24 -04:00
return fmt . Errorf ( "Standard storage class parity %d should be greater than or equal to %d" ,
ssParity , minParityDisks )
}
// RRS parity disks should be greater than or equal to minParityDisks.
// Parity below minParityDisks is not supported.
2021-01-16 20:32:25 -05:00
if rrsParity > 0 && rrsParity < minParityDisks {
2019-10-07 01:50:24 -04:00
return fmt . Errorf ( "Reduced redundancy storage class parity %d should be greater than or equal to %d" , rrsParity , minParityDisks )
}
2020-08-26 22:29:35 -04:00
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
}
2020-08-26 22:29:35 -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 {
2021-01-16 20:32:25 -05:00
if ssParity > 0 && ssParity < rrsParity {
2019-10-07 01:50:24 -04:00
return fmt . Errorf ( "Standard storage class parity disks %d should be greater than or equal to Reduced redundancy storage class parity disks %d" , ssParity , rrsParity )
}
}
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
// -- if input is RRS but RRS is not configured default '2' parity
// for RRS is assumed
// -- if input is STANDARD but STANDARD is not configured '0' parity
// is returned, the caller is expected to choose the right parity
// at that point.
2019-10-07 01:50:24 -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 :
// set the rrs parity if available
if sCfg . RRS . Parity == 0 {
return defaultRRSParity
}
return sCfg . RRS . Parity
default :
return sCfg . Standard . Parity
}
}
2021-04-04 16:32:31 -04:00
// Update update storage-class with new config
func ( sCfg Config ) Update ( newCfg Config ) {
ConfigLock . Lock ( )
defer ConfigLock . Unlock ( )
sCfg . RRS = newCfg . RRS
sCfg . DMA = newCfg . DMA
sCfg . Standard = newCfg . Standard
}
2020-10-30 14:04:29 -04:00
// GetDMA - returns DMA configuration.
func ( sCfg Config ) GetDMA ( ) string {
2021-04-04 16:32:31 -04:00
ConfigLock . RLock ( )
defer ConfigLock . RUnlock ( )
2021-01-16 15:08:02 -05:00
return sCfg . DMA
2020-10-30 14:04:29 -04:00
}
2019-12-04 18:32:37 -05:00
// Enabled returns if etcd is enabled.
func Enabled ( kvs config . KVS ) bool {
ssc := kvs . Get ( ClassStandard )
rrsc := kvs . Get ( ClassRRS )
return ssc != "" || rrsc != ""
}
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 { }
if err = config . CheckValidKeys ( config . StorageClassSubSys , kvs , DefaultKVS ) ; 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 ) )
2020-10-30 14:04:29 -04:00
dma := env . Get ( DMAEnv , kvs . Get ( ClassDMA ) )
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
}
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
}
2019-10-09 02:11:15 -04:00
}
if cfg . RRS . Parity == 0 {
cfg . RRS . Parity = defaultRRSParity
2019-10-07 01:50:24 -04:00
}
2020-10-30 14:04:29 -04:00
if dma == "" {
dma = defaultDMA
}
if dma != DMAReadWrite && dma != DMAWrite {
return Config { } , errors . New ( ` valid dma values are "read-write" and "write" ` )
}
2021-01-16 15:08:02 -05:00
cfg . DMA = dma
2020-10-30 14:04:29 -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 {
2020-08-14 22:48:04 -04:00
return Config { } , err
2019-10-07 01:50:24 -04:00
}
return cfg , nil
}