2019-10-04 19:35:33 +02: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 07:59:13 +02:00
|
|
|
"errors"
|
2019-10-04 19:35:33 +02:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/minio/minio/cmd/config"
|
|
|
|
"github.com/minio/minio/pkg/env"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Cache ENVs
|
|
|
|
const (
|
2020-02-23 14:33:39 +01:00
|
|
|
Drives = "drives"
|
|
|
|
Exclude = "exclude"
|
|
|
|
Expiry = "expiry"
|
|
|
|
MaxUse = "maxuse"
|
|
|
|
Quota = "quota"
|
|
|
|
After = "after"
|
|
|
|
WatermarkLow = "watermark_low"
|
|
|
|
WatermarkHigh = "watermark_high"
|
2020-06-29 22:25:29 +02:00
|
|
|
Range = "range"
|
2020-02-23 14:33:39 +01: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 22:25:29 +02:00
|
|
|
EnvCacheRange = "MINIO_CACHE_RANGE"
|
2019-10-23 07:59:13 +02:00
|
|
|
|
2019-10-04 19:35:33 +02:00
|
|
|
EnvCacheEncryptionMasterKey = "MINIO_CACHE_ENCRYPTION_MASTER_KEY"
|
2019-10-23 07:59:13 +02:00
|
|
|
|
2020-02-23 14:33:39 +01:00
|
|
|
DefaultExpiry = "90"
|
|
|
|
DefaultQuota = "80"
|
|
|
|
DefaultAfter = "0"
|
|
|
|
DefaultWaterMarkLow = "70"
|
|
|
|
DefaultWaterMarkHigh = "80"
|
2019-10-23 07:59:13 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultKVS - default KV settings for caching.
|
|
|
|
var (
|
|
|
|
DefaultKVS = config.KVS{
|
2019-11-21 00:10:24 +01: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-04 04:40:01 +01:00
|
|
|
config.KV{
|
|
|
|
Key: After,
|
|
|
|
Value: DefaultAfter,
|
|
|
|
},
|
2020-02-23 14:33:39 +01:00
|
|
|
config.KV{
|
|
|
|
Key: WatermarkLow,
|
|
|
|
Value: DefaultWaterMarkLow,
|
|
|
|
},
|
|
|
|
config.KV{
|
|
|
|
Key: WatermarkHigh,
|
|
|
|
Value: DefaultWaterMarkHigh,
|
|
|
|
},
|
2020-06-29 22:25:29 +02:00
|
|
|
config.KV{
|
|
|
|
Key: Range,
|
|
|
|
Value: config.EnableOn,
|
|
|
|
},
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
2019-10-04 19:35:33 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-11-21 00:10:24 +01:00
|
|
|
cacheDelimiter = ","
|
2019-10-04 19:35:33 +02:00
|
|
|
)
|
|
|
|
|
2019-12-05 00:32:37 +01:00
|
|
|
// Enabled returns if cache is enabled.
|
|
|
|
func Enabled(kvs config.KVS) bool {
|
|
|
|
drives := kvs.Get(Drives)
|
|
|
|
return drives != ""
|
|
|
|
}
|
|
|
|
|
2019-10-04 19:35:33 +02:00
|
|
|
// LookupConfig - extracts cache configuration provided by environment
|
|
|
|
// variables and merge them with provided CacheConfiguration.
|
2019-10-23 07:59:13 +02: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 19:35:33 +02:00
|
|
|
}
|
|
|
|
|
2019-12-03 19:50:20 +01:00
|
|
|
drives := env.Get(EnvCacheDrives, kvs.Get(Drives))
|
2019-12-05 00:32:37 +01:00
|
|
|
if len(drives) == 0 {
|
2019-10-23 07:59:13 +02:00
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
2019-12-03 19:50:20 +01:00
|
|
|
var err error
|
|
|
|
cfg.Drives, err = parseCacheDrives(drives)
|
2019-10-23 07:59:13 +02:00
|
|
|
if err != nil {
|
2019-12-03 19:50:20 +01:00
|
|
|
return cfg, err
|
2019-10-23 07:59:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
cfg.Enabled = true
|
|
|
|
if excludes := env.Get(EnvCacheExclude, kvs.Get(Exclude)); excludes != "" {
|
2019-12-03 19:50:20 +01:00
|
|
|
cfg.Exclude, err = parseCacheExcludes(excludes)
|
2019-10-04 19:35:33 +02:00
|
|
|
if err != nil {
|
2019-12-03 19:50:20 +01:00
|
|
|
return cfg, err
|
2019-10-04 19:35:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 07:59:13 +02:00
|
|
|
if expiryStr := env.Get(EnvCacheExpiry, kvs.Get(Expiry)); expiryStr != "" {
|
|
|
|
cfg.Expiry, err = strconv.Atoi(expiryStr)
|
2019-10-04 19:35:33 +02:00
|
|
|
if err != nil {
|
|
|
|
return cfg, config.ErrInvalidCacheExpiryValue(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-23 07:59:13 +02:00
|
|
|
if maxUseStr := env.Get(EnvCacheMaxUse, kvs.Get(MaxUse)); maxUseStr != "" {
|
|
|
|
cfg.MaxUse, err = strconv.Atoi(maxUseStr)
|
2019-10-04 19:35:33 +02:00
|
|
|
if err != nil {
|
2019-10-23 07:59:13 +02:00
|
|
|
return cfg, config.ErrInvalidCacheQuota(err)
|
2019-10-04 19:35:33 +02:00
|
|
|
}
|
|
|
|
// maxUse should be a valid percentage.
|
2019-10-23 07:59:13 +02: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-11 01:57:18 +01:00
|
|
|
} else if quotaStr := env.Get(EnvCacheQuota, kvs.Get(Quota)); quotaStr != "" {
|
2019-10-23 07:59:13 +02: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 19:35:33 +02:00
|
|
|
}
|
2019-10-23 07:59:13 +02:00
|
|
|
cfg.MaxUse = cfg.Quota
|
2019-10-04 19:35:33 +02:00
|
|
|
}
|
|
|
|
|
2020-02-04 04:40:01 +01: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 14:33:39 +01: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 22:25:29 +02: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
|
|
|
|
}
|
|
|
|
|
2019-10-04 19:35:33 +02:00
|
|
|
return cfg, nil
|
|
|
|
}
|