2020-04-30 18:55:54 -04:00
|
|
|
/*
|
|
|
|
* MinIO Cloud Storage, (C) 2020 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 cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/minio/minio/cmd/config"
|
|
|
|
"github.com/minio/minio/cmd/logger"
|
|
|
|
"github.com/minio/minio/pkg/env"
|
|
|
|
"github.com/minio/minio/pkg/event"
|
|
|
|
"github.com/minio/minio/pkg/madmin"
|
|
|
|
)
|
|
|
|
|
|
|
|
// BucketQuotaSys - map of bucket and quota configuration.
|
2020-05-27 09:45:43 -04:00
|
|
|
type BucketQuotaSys struct {
|
|
|
|
bucketStorageCache timedValue
|
|
|
|
}
|
2020-04-30 18:55:54 -04:00
|
|
|
|
|
|
|
// Get - Get quota configuration.
|
2020-05-20 13:18:15 -04:00
|
|
|
func (sys *BucketQuotaSys) Get(bucketName string) (*madmin.BucketQuota, error) {
|
2020-05-19 16:53:54 -04:00
|
|
|
if globalIsGateway {
|
|
|
|
objAPI := newObjectLayerFn()
|
|
|
|
if objAPI == nil {
|
2020-05-20 13:18:15 -04:00
|
|
|
return nil, errServerNotInitialized
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
2020-05-20 13:18:15 -04:00
|
|
|
return &madmin.BucketQuota{}, nil
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
2020-04-30 18:55:54 -04:00
|
|
|
|
2020-05-20 13:18:15 -04:00
|
|
|
return globalBucketMetadataSys.GetQuotaConfig(bucketName)
|
2020-04-30 18:55:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewBucketQuotaSys returns initialized BucketQuotaSys
|
|
|
|
func NewBucketQuotaSys() *BucketQuotaSys {
|
2020-05-20 13:18:15 -04:00
|
|
|
return &BucketQuotaSys{}
|
2020-04-30 18:55:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// parseBucketQuota parses BucketQuota from json
|
2020-05-20 13:18:15 -04:00
|
|
|
func parseBucketQuota(bucket string, data []byte) (quotaCfg *madmin.BucketQuota, err error) {
|
|
|
|
quotaCfg = &madmin.BucketQuota{}
|
|
|
|
if err = json.Unmarshal(data, quotaCfg); err != nil {
|
2020-05-19 16:53:54 -04:00
|
|
|
return quotaCfg, err
|
2020-04-30 18:55:54 -04:00
|
|
|
}
|
2020-05-20 13:18:15 -04:00
|
|
|
if !quotaCfg.IsValid() {
|
|
|
|
return quotaCfg, fmt.Errorf("Invalid quota config %#v", quotaCfg)
|
2020-04-30 18:55:54 -04:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-27 09:45:43 -04:00
|
|
|
func (sys *BucketQuotaSys) check(ctx context.Context, bucket string, size int64) error {
|
|
|
|
objAPI := newObjectLayerWithoutSafeModeFn()
|
|
|
|
if objAPI == nil {
|
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
q, err := sys.Get(bucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if q.Type == madmin.FIFOQuota {
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-30 18:55:54 -04:00
|
|
|
|
2020-05-20 13:18:15 -04:00
|
|
|
if q.Quota == 0 {
|
|
|
|
// No quota set return quickly.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-27 09:45:43 -04:00
|
|
|
sys.bucketStorageCache.Once.Do(func() {
|
|
|
|
sys.bucketStorageCache.TTL = 10 * time.Second
|
|
|
|
sys.bucketStorageCache.Update = func() (interface{}, error) {
|
|
|
|
return loadDataUsageFromBackend(ctx, objAPI)
|
2020-04-30 18:55:54 -04:00
|
|
|
}
|
2020-05-27 09:45:43 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
v, err := sys.bucketStorageCache.Get()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
dui := v.(DataUsageInfo)
|
|
|
|
|
|
|
|
bui, ok := dui.BucketsUsage[bucket]
|
|
|
|
if !ok {
|
|
|
|
// bucket not found, cannot enforce quota
|
|
|
|
// call will fail anyways later.
|
|
|
|
return nil
|
2020-04-30 18:55:54 -04:00
|
|
|
}
|
2020-05-27 09:45:43 -04:00
|
|
|
|
|
|
|
if (bui.Size + uint64(size)) > q.Quota {
|
2020-04-30 18:55:54 -04:00
|
|
|
return BucketQuotaExceeded{Bucket: bucket}
|
|
|
|
}
|
2020-05-27 09:45:43 -04:00
|
|
|
|
2020-04-30 18:55:54 -04:00
|
|
|
return nil
|
|
|
|
}
|
2020-05-27 09:45:43 -04:00
|
|
|
|
2020-04-30 18:55:54 -04:00
|
|
|
func enforceBucketQuota(ctx context.Context, bucket string, size int64) error {
|
|
|
|
if size < 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2020-05-19 16:53:54 -04:00
|
|
|
|
2020-05-27 09:45:43 -04:00
|
|
|
return globalBucketQuotaSys.check(ctx, bucket, size)
|
2020-04-30 18:55:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
bgQuotaInterval = 1 * time.Hour
|
|
|
|
)
|
|
|
|
|
|
|
|
// initQuotaEnforcement starts the routine that deletes objects in bucket
|
|
|
|
// that exceeds the FIFO quota
|
|
|
|
func initQuotaEnforcement(ctx context.Context, objAPI ObjectLayer) {
|
|
|
|
if env.Get(envDataUsageCrawlConf, config.EnableOn) == config.EnableOn {
|
|
|
|
go startBucketQuotaEnforcement(ctx, objAPI)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func startBucketQuotaEnforcement(ctx context.Context, objAPI ObjectLayer) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-time.NewTimer(bgQuotaInterval).C:
|
2020-06-12 23:04:01 -04:00
|
|
|
enforceFIFOQuota(ctx, objAPI)
|
2020-04-30 18:55:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// enforceFIFOQuota deletes objects in FIFO order until sufficient objects
|
|
|
|
// have been deleted so as to bring bucket usage within quota
|
2020-06-12 23:04:01 -04:00
|
|
|
func enforceFIFOQuota(ctx context.Context, objectAPI ObjectLayer) {
|
2020-04-30 18:55:54 -04:00
|
|
|
// Turn off quota enforcement if data usage info is unavailable.
|
|
|
|
if env.Get(envDataUsageCrawlConf, config.EnableOn) == config.EnableOff {
|
2020-06-12 23:04:01 -04:00
|
|
|
return
|
2020-04-30 18:55:54 -04:00
|
|
|
}
|
2020-05-27 09:45:43 -04:00
|
|
|
|
2020-05-20 13:18:15 -04:00
|
|
|
buckets, err := objectAPI.ListBuckets(ctx)
|
|
|
|
if err != nil {
|
2020-06-12 23:04:01 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
2020-05-27 09:45:43 -04:00
|
|
|
|
|
|
|
dataUsageInfo, err := loadDataUsageFromBackend(ctx, objectAPI)
|
|
|
|
if err != nil {
|
2020-06-12 23:04:01 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return
|
2020-05-27 09:45:43 -04:00
|
|
|
}
|
|
|
|
|
2020-05-20 13:18:15 -04:00
|
|
|
for _, binfo := range buckets {
|
|
|
|
bucket := binfo.Name
|
2020-05-27 09:45:43 -04:00
|
|
|
|
|
|
|
bui, ok := dataUsageInfo.BucketsUsage[bucket]
|
|
|
|
if !ok {
|
|
|
|
// bucket doesn't exist anymore, or we
|
|
|
|
// do not have any information to proceed.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-30 18:55:54 -04:00
|
|
|
// Check if the current bucket has quota restrictions, if not skip it
|
2020-05-19 16:53:54 -04:00
|
|
|
cfg, err := globalBucketQuotaSys.Get(bucket)
|
|
|
|
if err != nil {
|
2020-04-30 18:55:54 -04:00
|
|
|
continue
|
|
|
|
}
|
2020-05-27 09:45:43 -04:00
|
|
|
|
2020-04-30 18:55:54 -04:00
|
|
|
if cfg.Type != madmin.FIFOQuota {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var toFree uint64
|
2020-05-27 09:45:43 -04:00
|
|
|
if bui.Size > cfg.Quota && cfg.Quota > 0 {
|
|
|
|
toFree = bui.Size - cfg.Quota
|
2020-04-30 18:55:54 -04:00
|
|
|
}
|
2020-05-19 16:53:54 -04:00
|
|
|
|
2020-05-18 12:59:45 -04:00
|
|
|
if toFree == 0 {
|
2020-04-30 18:55:54 -04:00
|
|
|
continue
|
|
|
|
}
|
2020-05-19 16:53:54 -04:00
|
|
|
|
2020-04-30 18:55:54 -04:00
|
|
|
// Allocate new results channel to receive ObjectInfo.
|
|
|
|
objInfoCh := make(chan ObjectInfo)
|
|
|
|
|
|
|
|
// Walk through all objects
|
|
|
|
if err := objectAPI.Walk(ctx, bucket, "", objInfoCh); err != nil {
|
2020-06-12 23:04:01 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
continue
|
2020-04-30 18:55:54 -04:00
|
|
|
}
|
2020-05-27 09:45:43 -04:00
|
|
|
|
2020-04-30 18:55:54 -04:00
|
|
|
// reuse the fileScorer used by disk cache to score entries by
|
|
|
|
// ModTime to find the oldest objects in bucket to delete. In
|
|
|
|
// the context of bucket quota enforcement - number of hits are
|
|
|
|
// irrelevant.
|
|
|
|
scorer, err := newFileScorer(toFree, time.Now().Unix(), 1)
|
|
|
|
if err != nil {
|
2020-06-12 23:04:01 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
continue
|
2020-04-30 18:55:54 -04:00
|
|
|
}
|
|
|
|
|
2020-05-19 16:53:54 -04:00
|
|
|
rcfg, _ := globalBucketObjectLockSys.Get(bucket)
|
2020-04-30 18:55:54 -04:00
|
|
|
for obj := range objInfoCh {
|
2020-06-12 23:04:01 -04:00
|
|
|
if obj.DeleteMarker {
|
|
|
|
// Delete markers are automatically added for FIFO purge.
|
|
|
|
scorer.addFileWithObjInfo(obj, 1)
|
|
|
|
continue
|
|
|
|
}
|
2020-04-30 18:55:54 -04:00
|
|
|
// skip objects currently under retention
|
2020-05-19 16:53:54 -04:00
|
|
|
if rcfg.LockEnabled && enforceRetentionForDeletion(ctx, obj) {
|
2020-04-30 18:55:54 -04:00
|
|
|
continue
|
|
|
|
}
|
2020-06-12 23:04:01 -04:00
|
|
|
scorer.addFileWithObjInfo(obj, 1)
|
2020-04-30 18:55:54 -04:00
|
|
|
}
|
2020-06-12 23:04:01 -04:00
|
|
|
|
|
|
|
versioned := globalBucketVersioningSys.Enabled(bucket)
|
|
|
|
|
|
|
|
var objects []ObjectToDelete
|
|
|
|
numKeys := len(scorer.fileObjInfos())
|
|
|
|
for i, obj := range scorer.fileObjInfos() {
|
|
|
|
objects = append(objects, ObjectToDelete{
|
|
|
|
ObjectName: obj.Name,
|
|
|
|
VersionID: obj.VersionID,
|
|
|
|
})
|
2020-05-25 19:51:32 -04:00
|
|
|
if len(objects) < maxDeleteList && (i < numKeys-1) {
|
2020-06-12 23:04:01 -04:00
|
|
|
// skip deletion until maxDeleteList or end of slice
|
2020-04-30 18:55:54 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(objects) == 0 {
|
|
|
|
break
|
|
|
|
}
|
2020-06-12 23:04:01 -04:00
|
|
|
|
2020-04-30 18:55:54 -04:00
|
|
|
// Deletes a list of objects.
|
2020-06-12 23:04:01 -04:00
|
|
|
_, deleteErrs := objectAPI.DeleteObjects(ctx, bucket, objects, ObjectOptions{
|
|
|
|
Versioned: versioned,
|
|
|
|
})
|
|
|
|
for i := range deleteErrs {
|
|
|
|
if deleteErrs[i] != nil {
|
|
|
|
logger.LogIf(ctx, deleteErrs[i])
|
|
|
|
continue
|
2020-04-30 18:55:54 -04:00
|
|
|
}
|
2020-06-12 23:04:01 -04:00
|
|
|
|
|
|
|
// Notify object deleted event.
|
|
|
|
sendEvent(eventArgs{
|
|
|
|
EventName: event.ObjectRemovedDelete,
|
|
|
|
BucketName: bucket,
|
|
|
|
Object: obj,
|
|
|
|
Host: "Internal: [FIFO-QUOTA-EXPIRY]",
|
|
|
|
})
|
2020-04-30 18:55:54 -04:00
|
|
|
}
|
2020-06-12 23:04:01 -04:00
|
|
|
objects = nil
|
2020-04-30 18:55:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|