2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// 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-04 13:35:33 -04:00
|
|
|
|
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2019-10-23 01:59:13 -04:00
|
|
|
"errors"
|
2019-10-04 13:35:33 -04:00
|
|
|
"strconv"
|
|
|
|
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/config"
|
2021-05-28 18:17:01 -04:00
|
|
|
"github.com/minio/pkg/env"
|
2019-10-04 13:35:33 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Cache ENVs
|
|
|
|
const (
|
2020-02-23 08:33:39 -05:00
|
|
|
Drives = "drives"
|
|
|
|
Exclude = "exclude"
|
|
|
|
Expiry = "expiry"
|
|
|
|
MaxUse = "maxuse"
|
|
|
|
Quota = "quota"
|
|
|
|
After = "after"
|
|
|
|
WatermarkLow = "watermark_low"
|
|
|
|
WatermarkHigh = "watermark_high"
|
2020-06-29 16:25:29 -04:00
|
|
|
Range = "range"
|
2020-11-02 13:00:45 -05:00
|
|
|
Commit = "commit"
|
2020-02-23 08:33:39 -05:00
|
|
|
|
|
|
|
EnvCacheDrives = "MINIO_CACHE_DRIVES"
|
|
|
|
EnvCacheExclude = "MINIO_CACHE_EXCLUDE"
|
|
|
|
EnvCacheExpiry = "MINIO_CACHE_EXPIRY"
|
|
|
|
EnvCacheMaxUse = "MINIO_CACHE_MAXUSE"
|
|
|
|
EnvCacheQuota = "MINIO_CACHE_QUOTA"
|
|
|
|
EnvCacheAfter = "MINIO_CACHE_AFTER"
|
|
|
|
EnvCacheWatermarkLow = "MINIO_CACHE_WATERMARK_LOW"
|
|
|
|
EnvCacheWatermarkHigh = "MINIO_CACHE_WATERMARK_HIGH"
|
2020-06-29 16:25:29 -04:00
|
|
|
EnvCacheRange = "MINIO_CACHE_RANGE"
|
2020-11-02 13:00:45 -05:00
|
|
|
EnvCacheCommit = "MINIO_CACHE_COMMIT"
|
2019-10-23 01:59:13 -04:00
|
|
|
|
2021-04-22 11:45:30 -04:00
|
|
|
EnvCacheEncryptionKey = "MINIO_CACHE_ENCRYPTION_SECRET_KEY"
|
2019-10-23 01:59:13 -04:00
|
|
|
|
2020-02-23 08:33:39 -05:00
|
|
|
DefaultExpiry = "90"
|
|
|
|
DefaultQuota = "80"
|
|
|
|
DefaultAfter = "0"
|
|
|
|
DefaultWaterMarkLow = "70"
|
|
|
|
DefaultWaterMarkHigh = "80"
|
2019-10-23 01:59:13 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultKVS - default KV settings for caching.
|
|
|
|
var (
|
|
|
|
DefaultKVS = config.KVS{
|
2019-11-20 18:10:24 -05:00
|
|
|
config.KV{
|
|
|
|
Key: Drives,
|
|
|
|
Value: "",
|
|
|
|
},
|
|
|
|
config.KV{
|
|
|
|
Key: Exclude,
|
|
|
|
Value: "",
|
|
|
|
},
|
|
|
|
config.KV{
|
|
|
|
Key: Expiry,
|
|
|
|
Value: DefaultExpiry,
|
|
|
|
},
|
|
|
|
config.KV{
|
|
|
|
Key: Quota,
|
|
|
|
Value: DefaultQuota,
|
|
|
|
},
|
2020-02-03 22:40:01 -05:00
|
|
|
config.KV{
|
|
|
|
Key: After,
|
|
|
|
Value: DefaultAfter,
|
|
|
|
},
|
2020-02-23 08:33:39 -05:00
|
|
|
config.KV{
|
|
|
|
Key: WatermarkLow,
|
|
|
|
Value: DefaultWaterMarkLow,
|
|
|
|
},
|
|
|
|
config.KV{
|
|
|
|
Key: WatermarkHigh,
|
|
|
|
Value: DefaultWaterMarkHigh,
|
|
|
|
},
|
2020-06-29 16:25:29 -04:00
|
|
|
config.KV{
|
|
|
|
Key: Range,
|
|
|
|
Value: config.EnableOn,
|
|
|
|
},
|
2020-11-02 13:00:45 -05:00
|
|
|
config.KV{
|
|
|
|
Key: Commit,
|
2021-12-15 19:48:34 -05:00
|
|
|
Value: "",
|
2020-11-02 13:00:45 -05:00
|
|
|
},
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
2019-10-04 13:35:33 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-11-20 18:10:24 -05:00
|
|
|
cacheDelimiter = ","
|
2019-10-04 13:35:33 -04:00
|
|
|
)
|
|
|
|
|
2019-12-04 18:32:37 -05:00
|
|
|
// Enabled returns if cache is enabled.
|
|
|
|
func Enabled(kvs config.KVS) bool {
|
|
|
|
drives := kvs.Get(Drives)
|
|
|
|
return drives != ""
|
|
|
|
}
|
|
|
|
|
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-12-03 13:50:20 -05:00
|
|
|
drives := env.Get(EnvCacheDrives, kvs.Get(Drives))
|
2019-12-04 18:32:37 -05:00
|
|
|
if len(drives) == 0 {
|
2019-10-23 01:59:13 -04:00
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
2019-12-03 13:50:20 -05:00
|
|
|
var err error
|
|
|
|
cfg.Drives, err = parseCacheDrives(drives)
|
2019-10-23 01:59:13 -04:00
|
|
|
if err != nil {
|
2019-12-03 13:50:20 -05:00
|
|
|
return cfg, err
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Enabled = true
|
|
|
|
if excludes := env.Get(EnvCacheExclude, kvs.Get(Exclude)); excludes != "" {
|
2019-12-03 13:50:20 -05:00
|
|
|
cfg.Exclude, err = parseCacheExcludes(excludes)
|
2019-10-04 13:35:33 -04:00
|
|
|
if err != nil {
|
2019-12-03 13:50:20 -05:00
|
|
|
return cfg, err
|
2019-10-04 13:35:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
2020-01-10 19:57:18 -05:00
|
|
|
} else if quotaStr := env.Get(EnvCacheQuota, kvs.Get(Quota)); quotaStr != "" {
|
2019-10-23 01:59:13 -04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-02-03 22:40:01 -05:00
|
|
|
if afterStr := env.Get(EnvCacheAfter, kvs.Get(After)); afterStr != "" {
|
|
|
|
cfg.After, err = strconv.Atoi(afterStr)
|
|
|
|
if err != nil {
|
|
|
|
return cfg, config.ErrInvalidCacheAfter(err)
|
|
|
|
}
|
|
|
|
// after should be a valid value >= 0.
|
|
|
|
if cfg.After < 0 {
|
|
|
|
err := errors.New("cache after value cannot be less than 0")
|
|
|
|
return cfg, config.ErrInvalidCacheAfter(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 08:33:39 -05:00
|
|
|
if lowWMStr := env.Get(EnvCacheWatermarkLow, kvs.Get(WatermarkLow)); lowWMStr != "" {
|
|
|
|
cfg.WatermarkLow, err = strconv.Atoi(lowWMStr)
|
|
|
|
if err != nil {
|
|
|
|
return cfg, config.ErrInvalidCacheWatermarkLow(err)
|
|
|
|
}
|
|
|
|
// WatermarkLow should be a valid percentage.
|
|
|
|
if cfg.WatermarkLow < 0 || cfg.WatermarkLow > 100 {
|
|
|
|
err := errors.New("config min watermark value should be between 0 and 100")
|
|
|
|
return cfg, config.ErrInvalidCacheWatermarkLow(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if highWMStr := env.Get(EnvCacheWatermarkHigh, kvs.Get(WatermarkHigh)); highWMStr != "" {
|
|
|
|
cfg.WatermarkHigh, err = strconv.Atoi(highWMStr)
|
|
|
|
if err != nil {
|
|
|
|
return cfg, config.ErrInvalidCacheWatermarkHigh(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaxWatermark should be a valid percentage.
|
|
|
|
if cfg.WatermarkHigh < 0 || cfg.WatermarkHigh > 100 {
|
|
|
|
err := errors.New("config high watermark value should be between 0 and 100")
|
|
|
|
return cfg, config.ErrInvalidCacheWatermarkHigh(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cfg.WatermarkLow > cfg.WatermarkHigh {
|
|
|
|
err := errors.New("config high watermark value should be greater than low watermark value")
|
|
|
|
return cfg, config.ErrInvalidCacheWatermarkHigh(err)
|
|
|
|
}
|
2020-06-29 16:25:29 -04:00
|
|
|
|
|
|
|
cfg.Range = true // by default range caching is enabled.
|
|
|
|
if rangeStr := env.Get(EnvCacheRange, kvs.Get(Range)); rangeStr != "" {
|
|
|
|
rng, err := config.ParseBool(rangeStr)
|
|
|
|
if err != nil {
|
|
|
|
return cfg, config.ErrInvalidCacheRange(err)
|
|
|
|
}
|
|
|
|
cfg.Range = rng
|
|
|
|
}
|
2020-11-02 13:00:45 -05:00
|
|
|
if commit := env.Get(EnvCacheCommit, kvs.Get(Commit)); commit != "" {
|
2021-11-01 11:11:58 -04:00
|
|
|
cfg.CacheCommitMode, err = parseCacheCommitMode(commit)
|
2020-11-02 13:00:45 -05:00
|
|
|
if err != nil {
|
|
|
|
return cfg, err
|
|
|
|
}
|
2021-11-02 17:20:52 -04:00
|
|
|
if cfg.After > 0 && cfg.CacheCommitMode != WriteThrough {
|
2020-11-30 23:53:27 -05:00
|
|
|
err := errors.New("cache after cannot be used with commit writeback")
|
|
|
|
return cfg, config.ErrInvalidCacheSetting(err)
|
|
|
|
}
|
2020-11-02 13:00:45 -05:00
|
|
|
}
|
2020-06-29 16:25:29 -04:00
|
|
|
|
2019-10-04 13:35:33 -04:00
|
|
|
return cfg, nil
|
|
|
|
}
|