2019-08-09 13:02:41 -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 cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/minio/minio/cmd/logger"
|
2020-01-27 17:12:34 -05:00
|
|
|
"github.com/minio/minio/pkg/bucket/lifecycle"
|
2019-11-04 18:52:03 -05:00
|
|
|
"github.com/minio/minio/pkg/event"
|
2019-08-09 13:02:41 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
bgLifecycleInterval = 24 * time.Hour
|
|
|
|
)
|
|
|
|
|
|
|
|
// initDailyLifecycle starts the routine that receives the daily
|
|
|
|
// listing of all objects and applies any matching bucket lifecycle
|
|
|
|
// rules.
|
2020-03-22 15:16:36 -04:00
|
|
|
func initDailyLifecycle(ctx context.Context, objAPI ObjectLayer) {
|
|
|
|
go startDailyLifecycle(ctx, objAPI)
|
2019-08-09 13:02:41 -04:00
|
|
|
}
|
|
|
|
|
2020-03-22 15:16:36 -04:00
|
|
|
func startDailyLifecycle(ctx context.Context, objAPI ObjectLayer) {
|
2019-08-09 13:02:41 -04:00
|
|
|
for {
|
2020-03-22 15:16:36 -04:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-time.NewTimer(bgLifecycleInterval).C:
|
|
|
|
// Perform one lifecycle operation
|
2020-03-27 21:57:50 -04:00
|
|
|
logger.LogIf(ctx, lifecycleRound(ctx, objAPI))
|
2019-08-09 13:02:41 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-19 14:22:32 -04:00
|
|
|
func lifecycleRound(ctx context.Context, objAPI ObjectLayer) error {
|
2019-08-09 13:02:41 -04:00
|
|
|
buckets, err := objAPI.ListBuckets(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, bucket := range buckets {
|
|
|
|
// Check if the current bucket has a configured lifecycle policy, skip otherwise
|
|
|
|
l, ok := globalLifecycleSys.Get(bucket.Name)
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-05-08 16:44:44 -04:00
|
|
|
_, bucketHasLockConfig := globalBucketObjectLockSys.Get(bucket.Name)
|
2020-04-09 12:28:57 -04:00
|
|
|
|
2019-08-09 13:02:41 -04:00
|
|
|
// Calculate the common prefix of all lifecycle rules
|
|
|
|
var prefixes []string
|
|
|
|
for _, rule := range l.Rules {
|
2020-02-06 02:50:10 -05:00
|
|
|
prefixes = append(prefixes, rule.Prefix())
|
2019-08-09 13:02:41 -04:00
|
|
|
}
|
|
|
|
commonPrefix := lcp(prefixes)
|
|
|
|
|
2020-02-25 10:52:28 -05:00
|
|
|
// Allocate new results channel to receive ObjectInfo.
|
|
|
|
objInfoCh := make(chan ObjectInfo)
|
|
|
|
|
|
|
|
// Walk through all objects
|
|
|
|
if err := objAPI.Walk(ctx, bucket.Name, commonPrefix, objInfoCh); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-09 13:02:41 -04:00
|
|
|
for {
|
2019-09-10 15:08:44 -04:00
|
|
|
var objects []string
|
2020-02-25 10:52:28 -05:00
|
|
|
for obj := range objInfoCh {
|
|
|
|
if len(objects) == maxObjectList {
|
|
|
|
// Reached maximum delete requests, attempt a delete for now.
|
|
|
|
break
|
|
|
|
}
|
2019-08-09 13:02:41 -04:00
|
|
|
// Find the action that need to be executed
|
2020-02-25 10:52:28 -05:00
|
|
|
if l.ComputeAction(obj.Name, obj.UserTags, obj.ModTime) == lifecycle.DeleteAction {
|
2020-04-30 18:55:54 -04:00
|
|
|
if bucketHasLockConfig && enforceRetentionForDeletion(ctx, obj) {
|
2020-04-09 12:28:57 -04:00
|
|
|
continue
|
|
|
|
}
|
2019-09-10 15:08:44 -04:00
|
|
|
objects = append(objects, obj.Name)
|
2019-08-09 13:02:41 -04:00
|
|
|
}
|
|
|
|
}
|
2019-11-04 18:52:03 -05:00
|
|
|
|
2020-02-25 10:52:28 -05:00
|
|
|
// Nothing to do.
|
|
|
|
if len(objects) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2020-03-24 15:43:40 -04:00
|
|
|
waitForLowHTTPReq(int32(globalEndpoints.NEndpoints()))
|
2020-02-25 10:52:28 -05:00
|
|
|
|
2019-09-10 15:08:44 -04:00
|
|
|
// Deletes a list of objects.
|
2019-11-04 18:52:03 -05:00
|
|
|
deleteErrs, err := objAPI.DeleteObjects(ctx, bucket.Name, objects)
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
} else {
|
|
|
|
for i := range deleteErrs {
|
|
|
|
if deleteErrs[i] != nil {
|
|
|
|
logger.LogIf(ctx, deleteErrs[i])
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Notify object deleted event.
|
|
|
|
sendEvent(eventArgs{
|
|
|
|
EventName: event.ObjectRemovedDelete,
|
|
|
|
BucketName: bucket.Name,
|
|
|
|
Object: ObjectInfo{
|
|
|
|
Name: objects[i],
|
|
|
|
},
|
|
|
|
Host: "Internal: [ILM-EXPIRY]",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2019-08-09 13:02:41 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|