2019-10-04 13:35:33 -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 cache
|
|
|
|
|
|
|
|
import (
|
2019-10-23 01:59:13 -04:00
|
|
|
"errors"
|
2019-10-04 13:35:33 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/minio/minio/cmd/config"
|
|
|
|
"github.com/minio/minio/pkg/env"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Cache ENVs
|
|
|
|
const (
|
2019-10-23 01:59:13 -04:00
|
|
|
Drives = "drives"
|
|
|
|
Exclude = "exclude"
|
|
|
|
Expiry = "expiry"
|
|
|
|
MaxUse = "maxuse"
|
|
|
|
Quota = "quota"
|
|
|
|
|
|
|
|
EnvCacheState = "MINIO_CACHE_STATE"
|
2019-10-04 13:35:33 -04:00
|
|
|
EnvCacheDrives = "MINIO_CACHE_DRIVES"
|
|
|
|
EnvCacheExclude = "MINIO_CACHE_EXCLUDE"
|
|
|
|
EnvCacheExpiry = "MINIO_CACHE_EXPIRY"
|
|
|
|
EnvCacheMaxUse = "MINIO_CACHE_MAXUSE"
|
2019-10-23 01:59:13 -04:00
|
|
|
EnvCacheQuota = "MINIO_CACHE_QUOTA"
|
2019-10-04 13:35:33 -04:00
|
|
|
EnvCacheEncryptionMasterKey = "MINIO_CACHE_ENCRYPTION_MASTER_KEY"
|
2019-10-23 01:59:13 -04:00
|
|
|
|
|
|
|
DefaultExpiry = "90"
|
|
|
|
DefaultQuota = "80"
|
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultKVS - default KV settings for caching.
|
|
|
|
var (
|
|
|
|
DefaultKVS = config.KVS{
|
|
|
|
config.State: config.StateOff,
|
|
|
|
config.Comment: "This is a default cache configuration, only applicable in gateway setups",
|
|
|
|
Drives: "",
|
|
|
|
Exclude: "",
|
|
|
|
Expiry: DefaultExpiry,
|
|
|
|
Quota: DefaultQuota,
|
|
|
|
}
|
2019-10-04 13:35:33 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-10-23 01:59:13 -04:00
|
|
|
cacheDelimiter = ";"
|
2019-10-04 13:35:33 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// LookupConfig - extracts cache configuration provided by environment
|
|
|
|
// variables and merge them with provided CacheConfiguration.
|
2019-10-23 01:59:13 -04:00
|
|
|
func LookupConfig(kvs config.KVS) (Config, error) {
|
|
|
|
cfg := Config{}
|
|
|
|
|
|
|
|
if err := config.CheckValidKeys(config.CacheSubSys, kvs, DefaultKVS); err != nil {
|
|
|
|
return cfg, err
|
2019-10-04 13:35:33 -04:00
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
// Check if cache is explicitly disabled
|
|
|
|
stateBool, err := config.ParseBool(env.Get(EnvCacheState, kvs.Get(config.State)))
|
|
|
|
if err != nil {
|
2019-11-13 20:38:05 -05:00
|
|
|
// Parsing failures happen due to empty KVS, ignore it.
|
|
|
|
if kvs.Empty() {
|
|
|
|
return cfg, nil
|
|
|
|
}
|
2019-10-23 01:59:13 -04:00
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
|
|
|
|
drives := env.Get(EnvCacheDrives, kvs.Get(Drives))
|
2019-10-30 03:04:39 -04:00
|
|
|
if stateBool {
|
|
|
|
if len(drives) == 0 {
|
|
|
|
return cfg, config.Error("'drives' key cannot be empty if you wish to enable caching")
|
|
|
|
}
|
|
|
|
}
|
2019-10-23 01:59:13 -04:00
|
|
|
if len(drives) == 0 {
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Drives, err = parseCacheDrives(strings.Split(drives, cacheDelimiter))
|
|
|
|
if err != nil {
|
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Enabled = true
|
|
|
|
if excludes := env.Get(EnvCacheExclude, kvs.Get(Exclude)); excludes != "" {
|
|
|
|
cfg.Exclude, err = parseCacheExcludes(strings.Split(excludes, cacheDelimiter))
|
2019-10-04 13:35:33 -04:00
|
|
|
if err != nil {
|
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
if expiryStr := env.Get(EnvCacheExpiry, kvs.Get(Expiry)); expiryStr != "" {
|
|
|
|
cfg.Expiry, err = strconv.Atoi(expiryStr)
|
2019-10-04 13:35:33 -04:00
|
|
|
if err != nil {
|
|
|
|
return cfg, config.ErrInvalidCacheExpiryValue(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
if maxUseStr := env.Get(EnvCacheMaxUse, kvs.Get(MaxUse)); maxUseStr != "" {
|
|
|
|
cfg.MaxUse, err = strconv.Atoi(maxUseStr)
|
2019-10-04 13:35:33 -04:00
|
|
|
if err != nil {
|
2019-10-23 01:59:13 -04:00
|
|
|
return cfg, config.ErrInvalidCacheQuota(err)
|
2019-10-04 13:35:33 -04:00
|
|
|
}
|
|
|
|
// maxUse should be a valid percentage.
|
2019-10-23 01:59:13 -04:00
|
|
|
if cfg.MaxUse < 0 || cfg.MaxUse > 100 {
|
|
|
|
err := errors.New("config max use value should not be null or negative")
|
|
|
|
return cfg, config.ErrInvalidCacheQuota(err)
|
|
|
|
}
|
|
|
|
cfg.Quota = cfg.MaxUse
|
|
|
|
}
|
|
|
|
|
|
|
|
if quotaStr := env.Get(EnvCacheQuota, kvs.Get(Quota)); quotaStr != "" {
|
|
|
|
cfg.Quota, err = strconv.Atoi(quotaStr)
|
|
|
|
if err != nil {
|
|
|
|
return cfg, config.ErrInvalidCacheQuota(err)
|
|
|
|
}
|
|
|
|
// quota should be a valid percentage.
|
|
|
|
if cfg.Quota < 0 || cfg.Quota > 100 {
|
|
|
|
err := errors.New("config quota value should not be null or negative")
|
|
|
|
return cfg, config.ErrInvalidCacheQuota(err)
|
2019-10-04 13:35:33 -04:00
|
|
|
}
|
2019-10-23 01:59:13 -04:00
|
|
|
cfg.MaxUse = cfg.Quota
|
2019-10-04 13:35:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
}
|