2024-03-04 21:50:24 -05:00
|
|
|
// Copyright (c) 2015-2024 MinIO, Inc.
|
2021-04-18 15:41:13 -04:00
|
|
|
//
|
|
|
|
// 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-07-19 16:20:33 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-11-12 15:12:09 -05:00
|
|
|
"context"
|
|
|
|
"encoding/xml"
|
2021-05-19 21:51:23 -04:00
|
|
|
"errors"
|
2020-11-12 15:12:09 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2023-03-30 13:47:15 -04:00
|
|
|
"strconv"
|
2020-11-12 15:12:09 -05:00
|
|
|
"strings"
|
2021-04-16 17:09:25 -04:00
|
|
|
"sync"
|
2021-08-17 13:21:19 -04:00
|
|
|
"sync/atomic"
|
2020-11-12 15:12:09 -05:00
|
|
|
"time"
|
|
|
|
|
2021-04-19 13:30:42 -04:00
|
|
|
"github.com/google/uuid"
|
2023-06-19 20:53:08 -04:00
|
|
|
"github.com/minio/madmin-go/v3"
|
2020-11-12 15:12:09 -05:00
|
|
|
"github.com/minio/minio-go/v7/pkg/tags"
|
2022-09-07 10:24:54 -04:00
|
|
|
"github.com/minio/minio/internal/amztime"
|
2021-06-01 17:59:40 -04:00
|
|
|
sse "github.com/minio/minio/internal/bucket/encryption"
|
|
|
|
"github.com/minio/minio/internal/bucket/lifecycle"
|
|
|
|
"github.com/minio/minio/internal/event"
|
|
|
|
xhttp "github.com/minio/minio/internal/http"
|
|
|
|
"github.com/minio/minio/internal/logger"
|
|
|
|
"github.com/minio/minio/internal/s3select"
|
2023-11-27 01:18:09 -05:00
|
|
|
xnet "github.com/minio/pkg/v2/net"
|
2023-11-10 05:15:13 -05:00
|
|
|
"github.com/zeebo/xxh3"
|
2019-07-19 16:20:33 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Disabled means the lifecycle rule is inactive
|
|
|
|
Disabled = "Disabled"
|
2021-04-19 13:30:42 -04:00
|
|
|
// TransitionStatus status of transition
|
|
|
|
TransitionStatus = "transition-status"
|
|
|
|
// TransitionedObjectName name of transitioned object
|
|
|
|
TransitionedObjectName = "transitioned-object"
|
2021-06-03 17:26:51 -04:00
|
|
|
// TransitionedVersionID is version of remote object
|
|
|
|
TransitionedVersionID = "transitioned-versionID"
|
2021-04-19 13:30:42 -04:00
|
|
|
// TransitionTier name of transition storage class
|
|
|
|
TransitionTier = "transition-tier"
|
2019-07-19 16:20:33 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// LifecycleSys - Bucket lifecycle subsystem.
|
2020-05-20 13:18:15 -04:00
|
|
|
type LifecycleSys struct{}
|
2019-07-19 16:20:33 -04:00
|
|
|
|
2019-08-09 13:02:41 -04:00
|
|
|
// Get - gets lifecycle config associated to a given bucket name.
|
2020-05-19 16:53:54 -04:00
|
|
|
func (sys *LifecycleSys) Get(bucketName string) (lc *lifecycle.Lifecycle, err error) {
|
2023-05-25 01:52:39 -04:00
|
|
|
lc, _, err = globalBucketMetadataSys.GetLifecycleConfig(bucketName)
|
|
|
|
return lc, err
|
2019-07-19 16:20:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewLifecycleSys - creates new lifecycle system.
|
|
|
|
func NewLifecycleSys() *LifecycleSys {
|
2020-05-20 13:18:15 -04:00
|
|
|
return &LifecycleSys{}
|
2019-07-19 16:20:33 -04:00
|
|
|
}
|
2020-11-12 15:12:09 -05:00
|
|
|
|
2023-04-11 22:22:32 -04:00
|
|
|
func ilmTrace(startTime time.Time, duration time.Duration, oi ObjectInfo, event string) madmin.TraceInfo {
|
|
|
|
return madmin.TraceInfo{
|
|
|
|
TraceType: madmin.TraceILM,
|
|
|
|
Time: startTime,
|
|
|
|
NodeName: globalLocalNodeName,
|
|
|
|
FuncName: event,
|
|
|
|
Duration: duration,
|
|
|
|
Path: pathJoin(oi.Bucket, oi.Name),
|
|
|
|
Error: "",
|
|
|
|
Message: getSource(4),
|
|
|
|
Custom: map[string]string{"version-id": oi.VersionID},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sys *LifecycleSys) trace(oi ObjectInfo) func(event string) {
|
|
|
|
startTime := time.Now()
|
|
|
|
return func(event string) {
|
|
|
|
duration := time.Since(startTime)
|
|
|
|
if globalTrace.NumSubscribers(madmin.TraceILM) > 0 {
|
|
|
|
globalTrace.Publish(ilmTrace(startTime, duration, oi, event))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-06 19:10:33 -05:00
|
|
|
type expiryTask struct {
|
2023-04-26 20:49:00 -04:00
|
|
|
objInfo ObjectInfo
|
|
|
|
event lifecycle.Event
|
2023-05-22 18:28:56 -04:00
|
|
|
src lcEventSrc
|
2021-02-06 19:10:33 -05:00
|
|
|
}
|
|
|
|
|
2024-03-02 00:11:03 -05:00
|
|
|
// expiryStats records metrics related to ILM expiry activities
|
|
|
|
type expiryStats struct {
|
|
|
|
missedExpiryTasks atomic.Int64
|
|
|
|
missedFreeVersTasks atomic.Int64
|
|
|
|
missedTierJournalTasks atomic.Int64
|
|
|
|
workers atomic.Int32
|
|
|
|
}
|
|
|
|
|
|
|
|
// MissedTasks returns the number of ILM expiry tasks that were missed since
|
|
|
|
// there were no available workers.
|
|
|
|
func (e *expiryStats) MissedTasks() int64 {
|
|
|
|
return e.missedExpiryTasks.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MissedFreeVersTasks returns the number of free version collection tasks that
|
|
|
|
// were missed since there were no available workers.
|
|
|
|
func (e *expiryStats) MissedFreeVersTasks() int64 {
|
|
|
|
return e.missedFreeVersTasks.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MissedTierJournalTasks returns the number of tasks to remove tiered objects
|
|
|
|
// that were missed since there were no available workers.
|
|
|
|
func (e *expiryStats) MissedTierJournalTasks() int64 {
|
|
|
|
return e.missedTierJournalTasks.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
// NumWorkers returns the number of active workers executing one of ILM expiry
|
|
|
|
// tasks or free version collection tasks.
|
|
|
|
func (e *expiryStats) NumWorkers() int32 {
|
|
|
|
return e.workers.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
type expiryOp interface {
|
|
|
|
OpHash() uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
type freeVersionTask struct {
|
|
|
|
ObjectInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f freeVersionTask) OpHash() uint64 {
|
|
|
|
return xxh3.HashString(f.TransitionedObject.Tier + f.TransitionedObject.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (n newerNoncurrentTask) OpHash() uint64 {
|
|
|
|
return xxh3.HashString(n.bucket + n.versions[0].ObjectV.ObjectName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j jentry) OpHash() uint64 {
|
|
|
|
return xxh3.HashString(j.TierName + j.ObjName)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (e expiryTask) OpHash() uint64 {
|
|
|
|
return xxh3.HashString(e.objInfo.Bucket + e.objInfo.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
// expiryState manages all ILM related expiration activities.
|
2021-02-01 12:52:11 -05:00
|
|
|
type expiryState struct {
|
2024-03-02 00:11:03 -05:00
|
|
|
mu sync.RWMutex
|
|
|
|
workers atomic.Pointer[[]chan expiryOp]
|
|
|
|
|
|
|
|
ctx context.Context
|
|
|
|
objAPI ObjectLayer
|
|
|
|
|
|
|
|
stats expiryStats
|
2021-02-01 12:52:11 -05:00
|
|
|
}
|
|
|
|
|
2021-08-17 13:21:19 -04:00
|
|
|
// PendingTasks returns the number of pending ILM expiry tasks.
|
|
|
|
func (es *expiryState) PendingTasks() int {
|
2024-03-02 00:11:03 -05:00
|
|
|
w := es.workers.Load()
|
|
|
|
if w == nil || len(*w) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
var tasks int
|
|
|
|
for _, wrkr := range *w {
|
|
|
|
tasks += len(wrkr)
|
|
|
|
}
|
|
|
|
return tasks
|
|
|
|
}
|
|
|
|
|
|
|
|
// enqueueTierJournalEntry enqueues a tier journal entry referring to a remote
|
|
|
|
// object corresponding to a 'replaced' object versions. This applies only to
|
|
|
|
// non-versioned or version suspended buckets.
|
|
|
|
func (es *expiryState) enqueueTierJournalEntry(je jentry) {
|
|
|
|
wrkr := es.getWorkerCh(je.OpHash())
|
|
|
|
if wrkr == nil {
|
|
|
|
es.stats.missedTierJournalTasks.Add(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-GlobalContext.Done():
|
|
|
|
case wrkr <- je:
|
|
|
|
default:
|
|
|
|
es.stats.missedTierJournalTasks.Add(1)
|
|
|
|
}
|
2021-08-17 13:21:19 -04:00
|
|
|
}
|
|
|
|
|
2024-03-02 00:11:03 -05:00
|
|
|
// enqueueFreeVersion enqueues a free version to be deleted
|
|
|
|
func (es *expiryState) enqueueFreeVersion(oi ObjectInfo) {
|
|
|
|
task := freeVersionTask{ObjectInfo: oi}
|
|
|
|
wrkr := es.getWorkerCh(task.OpHash())
|
|
|
|
if wrkr == nil {
|
|
|
|
es.stats.missedFreeVersTasks.Add(1)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-GlobalContext.Done():
|
|
|
|
case wrkr <- task:
|
|
|
|
default:
|
|
|
|
es.stats.missedFreeVersTasks.Add(1)
|
|
|
|
}
|
2021-11-19 20:54:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// enqueueByDays enqueues object versions expired by days for expiry.
|
2023-05-22 18:28:56 -04:00
|
|
|
func (es *expiryState) enqueueByDays(oi ObjectInfo, event lifecycle.Event, src lcEventSrc) {
|
2024-03-02 00:11:03 -05:00
|
|
|
task := expiryTask{objInfo: oi, event: event, src: src}
|
|
|
|
wrkr := es.getWorkerCh(task.OpHash())
|
|
|
|
if wrkr == nil {
|
|
|
|
es.stats.missedExpiryTasks.Add(1)
|
|
|
|
return
|
|
|
|
}
|
2021-02-01 12:52:11 -05:00
|
|
|
select {
|
2021-04-16 17:09:25 -04:00
|
|
|
case <-GlobalContext.Done():
|
2024-03-02 00:11:03 -05:00
|
|
|
case wrkr <- task:
|
2021-02-01 12:52:11 -05:00
|
|
|
default:
|
2024-03-02 00:11:03 -05:00
|
|
|
es.stats.missedExpiryTasks.Add(1)
|
2021-02-01 12:52:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-14 12:41:44 -05:00
|
|
|
// enqueueByNewerNoncurrent enqueues object versions expired by
|
|
|
|
// NewerNoncurrentVersions limit for expiry.
|
2023-05-02 15:56:33 -04:00
|
|
|
func (es *expiryState) enqueueByNewerNoncurrent(bucket string, versions []ObjectToDelete, lcEvent lifecycle.Event) {
|
2024-03-19 23:10:58 -04:00
|
|
|
if len(versions) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-03-02 00:11:03 -05:00
|
|
|
task := newerNoncurrentTask{bucket: bucket, versions: versions, event: lcEvent}
|
|
|
|
wrkr := es.getWorkerCh(task.OpHash())
|
|
|
|
if wrkr == nil {
|
|
|
|
es.stats.missedExpiryTasks.Add(1)
|
|
|
|
return
|
|
|
|
}
|
2021-11-19 20:54:10 -05:00
|
|
|
select {
|
|
|
|
case <-GlobalContext.Done():
|
2024-03-02 00:11:03 -05:00
|
|
|
case wrkr <- task:
|
2021-11-19 20:54:10 -05:00
|
|
|
default:
|
2024-03-02 00:11:03 -05:00
|
|
|
es.stats.missedExpiryTasks.Add(1)
|
2021-11-19 20:54:10 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-02 00:11:03 -05:00
|
|
|
// globalExpiryState is the per-node instance which manages all ILM expiry tasks.
|
|
|
|
var globalExpiryState *expiryState
|
2021-02-01 12:52:11 -05:00
|
|
|
|
2024-03-02 00:11:03 -05:00
|
|
|
// newExpiryState creates an expiryState with buffered channels allocated for
|
|
|
|
// each ILM expiry task type.
|
|
|
|
func newExpiryState(ctx context.Context, objAPI ObjectLayer, n int) *expiryState {
|
|
|
|
es := &expiryState{
|
|
|
|
ctx: ctx,
|
|
|
|
objAPI: objAPI,
|
2021-02-01 12:52:11 -05:00
|
|
|
}
|
2024-03-02 00:11:03 -05:00
|
|
|
workers := make([]chan expiryOp, 0, n)
|
|
|
|
es.workers.Store(&workers)
|
|
|
|
es.ResizeWorkers(n)
|
|
|
|
return es
|
2021-02-01 12:52:11 -05:00
|
|
|
}
|
|
|
|
|
2024-03-02 00:11:03 -05:00
|
|
|
func (es *expiryState) getWorkerCh(h uint64) chan<- expiryOp {
|
|
|
|
w := es.workers.Load()
|
|
|
|
if w == nil || len(*w) == 0 {
|
|
|
|
return nil
|
2023-05-16 11:08:00 -04:00
|
|
|
}
|
2024-03-02 00:11:03 -05:00
|
|
|
workers := *w
|
|
|
|
return workers[h%uint64(len(workers))]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (es *expiryState) ResizeWorkers(n int) {
|
|
|
|
// Lock to avoid multiple resizes to happen at the same time.
|
|
|
|
es.mu.Lock()
|
|
|
|
defer es.mu.Unlock()
|
|
|
|
var workers []chan expiryOp
|
|
|
|
if v := es.workers.Load(); v != nil {
|
|
|
|
// Copy to new array.
|
|
|
|
workers = append(workers, *v...)
|
2023-03-30 13:47:15 -04:00
|
|
|
}
|
|
|
|
|
2024-03-02 00:11:03 -05:00
|
|
|
if n == len(workers) || n < 1 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for len(workers) < n {
|
|
|
|
input := make(chan expiryOp, 10000)
|
|
|
|
workers = append(workers, input)
|
|
|
|
go es.Worker(input)
|
|
|
|
es.stats.workers.Add(1)
|
2023-03-30 13:47:15 -04:00
|
|
|
}
|
|
|
|
|
2024-03-02 00:11:03 -05:00
|
|
|
for len(workers) > n {
|
|
|
|
worker := workers[len(workers)-1]
|
|
|
|
workers = workers[:len(workers)-1]
|
|
|
|
worker <- expiryOp(nil)
|
|
|
|
es.stats.workers.Add(-1)
|
|
|
|
}
|
|
|
|
// Atomically replace workers.
|
|
|
|
es.workers.Store(&workers)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Worker handles 4 types of expiration tasks.
|
|
|
|
// 1. Expiry of objects, includes regular and transitioned objects
|
|
|
|
// 2. Expiry of noncurrent versions due to NewerNoncurrentVersions
|
|
|
|
// 3. Expiry of free-versions, for remote objects of transitioned object which have been expired since.
|
|
|
|
// 4. Expiry of remote objects corresponding to objects in a
|
|
|
|
// non-versioned/version suspended buckets
|
|
|
|
func (es *expiryState) Worker(input <-chan expiryOp) {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-es.ctx.Done():
|
|
|
|
return
|
|
|
|
case v, ok := <-input:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if v == nil {
|
|
|
|
// ResizeWorkers signaling worker to quit
|
|
|
|
return
|
|
|
|
}
|
|
|
|
switch v := v.(type) {
|
|
|
|
case expiryTask:
|
|
|
|
if v.objInfo.TransitionedObject.Status != "" {
|
|
|
|
applyExpiryOnTransitionedObject(es.ctx, es.objAPI, v.objInfo, v.event, v.src)
|
2023-03-30 13:47:15 -04:00
|
|
|
} else {
|
2024-03-02 00:11:03 -05:00
|
|
|
applyExpiryOnNonTransitionedObjects(es.ctx, es.objAPI, v.objInfo, v.event, v.src)
|
2023-03-30 13:47:15 -04:00
|
|
|
}
|
2024-03-02 00:11:03 -05:00
|
|
|
case newerNoncurrentTask:
|
|
|
|
deleteObjectVersions(es.ctx, es.objAPI, v.bucket, v.versions, v.event)
|
|
|
|
case jentry:
|
2024-04-04 08:04:40 -04:00
|
|
|
transitionLogIf(es.ctx, deleteObjectFromRemoteTier(es.ctx, v.ObjName, v.VersionID, v.TierName))
|
2024-03-02 00:11:03 -05:00
|
|
|
case freeVersionTask:
|
|
|
|
oi := v.ObjectInfo
|
|
|
|
traceFn := globalLifecycleSys.trace(oi)
|
|
|
|
if !oi.TransitionedObject.FreeVersion {
|
|
|
|
// nothing to be done
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ignoreNotFoundErr := func(err error) error {
|
|
|
|
switch {
|
|
|
|
case isErrVersionNotFound(err), isErrObjectNotFound(err):
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Remove the remote object
|
|
|
|
err := deleteObjectFromRemoteTier(es.ctx, oi.TransitionedObject.Name, oi.TransitionedObject.VersionID, oi.TransitionedObject.Tier)
|
|
|
|
if ignoreNotFoundErr(err) != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
transitionLogIf(es.ctx, err)
|
2024-03-02 00:11:03 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove this free version
|
|
|
|
_, err = es.objAPI.DeleteObject(es.ctx, oi.Bucket, oi.Name, ObjectOptions{
|
|
|
|
VersionID: oi.VersionID,
|
|
|
|
InclFreeVersions: true,
|
|
|
|
})
|
|
|
|
if err == nil {
|
|
|
|
auditLogLifecycle(es.ctx, oi, ILMFreeVersionDelete, nil, traceFn)
|
|
|
|
}
|
|
|
|
if ignoreNotFoundErr(err) != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
transitionLogIf(es.ctx, err)
|
2024-03-02 00:11:03 -05:00
|
|
|
}
|
|
|
|
default:
|
2024-04-04 08:04:40 -04:00
|
|
|
bugLogIf(es.ctx, fmt.Errorf("Invalid work type - %v", v))
|
2024-03-02 00:11:03 -05:00
|
|
|
}
|
2021-11-19 20:54:10 -05:00
|
|
|
}
|
2024-03-02 00:11:03 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initBackgroundExpiry(ctx context.Context, objectAPI ObjectLayer) {
|
2024-03-04 21:50:24 -05:00
|
|
|
globalExpiryState = newExpiryState(ctx, objectAPI, globalILMConfig.getExpirationWorkers())
|
2021-11-19 20:54:10 -05:00
|
|
|
}
|
|
|
|
|
2021-12-14 12:41:44 -05:00
|
|
|
// newerNoncurrentTask encapsulates arguments required by worker to expire objects
|
|
|
|
// by NewerNoncurrentVersions
|
|
|
|
type newerNoncurrentTask struct {
|
2021-11-19 20:54:10 -05:00
|
|
|
bucket string
|
|
|
|
versions []ObjectToDelete
|
2023-05-02 15:56:33 -04:00
|
|
|
event lifecycle.Event
|
2021-02-01 12:52:11 -05:00
|
|
|
}
|
|
|
|
|
2022-10-21 13:46:53 -04:00
|
|
|
type transitionTask struct {
|
|
|
|
objInfo ObjectInfo
|
2023-05-22 18:28:56 -04:00
|
|
|
src lcEventSrc
|
2023-04-26 20:49:00 -04:00
|
|
|
event lifecycle.Event
|
2022-10-21 13:46:53 -04:00
|
|
|
}
|
|
|
|
|
2020-11-12 15:12:09 -05:00
|
|
|
type transitionState struct {
|
2022-10-21 13:46:53 -04:00
|
|
|
transitionCh chan transitionTask
|
2021-08-12 01:23:56 -04:00
|
|
|
|
|
|
|
ctx context.Context
|
|
|
|
objAPI ObjectLayer
|
|
|
|
mu sync.Mutex
|
|
|
|
numWorkers int
|
|
|
|
killCh chan struct{}
|
2021-08-17 13:21:19 -04:00
|
|
|
|
2023-12-02 16:02:12 -05:00
|
|
|
activeTasks atomic.Int64
|
|
|
|
missedImmediateTasks atomic.Int64
|
2022-01-26 17:33:10 -05:00
|
|
|
|
|
|
|
lastDayMu sync.RWMutex
|
|
|
|
lastDayStats map[string]*lastDayTierStats
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
|
|
|
|
2023-12-02 16:02:12 -05:00
|
|
|
func (t *transitionState) queueTransitionTask(oi ObjectInfo, event lifecycle.Event, src lcEventSrc) {
|
2023-11-17 19:16:46 -05:00
|
|
|
task := transitionTask{objInfo: oi, event: event, src: src}
|
2023-12-02 16:02:12 -05:00
|
|
|
select {
|
|
|
|
case <-t.ctx.Done():
|
|
|
|
case t.transitionCh <- task:
|
|
|
|
default:
|
|
|
|
switch src {
|
|
|
|
case lcEventSrc_s3PutObject, lcEventSrc_s3CopyObject, lcEventSrc_s3CompleteMultipartUpload:
|
|
|
|
// Update missed immediate tasks only for incoming requests.
|
|
|
|
t.missedImmediateTasks.Add(1)
|
2023-11-17 19:16:46 -05:00
|
|
|
}
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 12:15:06 -05:00
|
|
|
var globalTransitionState *transitionState
|
2020-11-12 15:12:09 -05:00
|
|
|
|
2023-02-08 02:11:42 -05:00
|
|
|
// newTransitionState returns a transitionState object ready to be initialized
|
|
|
|
// via its Init method.
|
|
|
|
func newTransitionState(ctx context.Context) *transitionState {
|
2021-04-16 17:09:25 -04:00
|
|
|
return &transitionState{
|
2023-11-17 19:16:46 -05:00
|
|
|
transitionCh: make(chan transitionTask, 100000),
|
2021-08-12 01:23:56 -04:00
|
|
|
ctx: ctx,
|
|
|
|
killCh: make(chan struct{}),
|
2022-01-26 17:33:10 -05:00
|
|
|
lastDayStats: make(map[string]*lastDayTierStats),
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-08 02:11:42 -05:00
|
|
|
// Init initializes t with given objAPI and instantiates the configured number
|
|
|
|
// of transition workers.
|
|
|
|
func (t *transitionState) Init(objAPI ObjectLayer) {
|
|
|
|
n := globalAPIConfig.getTransitionWorkers()
|
2024-03-04 21:50:24 -05:00
|
|
|
// Prefer ilm.transition_workers over now deprecated api.transition_workers
|
|
|
|
if tw := globalILMConfig.getTransitionWorkers(); tw > 0 {
|
|
|
|
n = tw
|
|
|
|
}
|
2023-02-08 02:11:42 -05:00
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
|
|
|
|
t.objAPI = objAPI
|
|
|
|
t.updateWorkers(n)
|
|
|
|
}
|
|
|
|
|
2021-08-17 13:21:19 -04:00
|
|
|
// PendingTasks returns the number of ILM transition tasks waiting for a worker
|
|
|
|
// goroutine.
|
|
|
|
func (t *transitionState) PendingTasks() int {
|
2023-02-08 02:11:42 -05:00
|
|
|
return len(t.transitionCh)
|
2021-08-17 13:21:19 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ActiveTasks returns the number of active (ongoing) ILM transition tasks.
|
2023-12-02 16:02:12 -05:00
|
|
|
func (t *transitionState) ActiveTasks() int64 {
|
|
|
|
return t.activeTasks.Load()
|
|
|
|
}
|
|
|
|
|
|
|
|
// MissedImmediateTasks returns the number of tasks - deferred to scanner due
|
|
|
|
// to tasks channel being backlogged.
|
|
|
|
func (t *transitionState) MissedImmediateTasks() int64 {
|
|
|
|
return t.missedImmediateTasks.Load()
|
2021-08-17 13:21:19 -04:00
|
|
|
}
|
|
|
|
|
2021-08-12 01:23:56 -04:00
|
|
|
// worker waits for transition tasks
|
2023-02-16 01:09:25 -05:00
|
|
|
func (t *transitionState) worker(objectAPI ObjectLayer) {
|
2021-08-12 01:23:56 -04:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.killCh:
|
|
|
|
return
|
2023-02-16 01:09:25 -05:00
|
|
|
case <-t.ctx.Done():
|
2021-08-12 01:23:56 -04:00
|
|
|
return
|
2022-10-21 13:46:53 -04:00
|
|
|
case task, ok := <-t.transitionCh:
|
2021-08-12 01:23:56 -04:00
|
|
|
if !ok {
|
2020-11-12 15:12:09 -05:00
|
|
|
return
|
2021-08-12 01:23:56 -04:00
|
|
|
}
|
2023-12-02 16:02:12 -05:00
|
|
|
t.activeTasks.Add(1)
|
2023-05-22 18:28:56 -04:00
|
|
|
if err := transitionObject(t.ctx, objectAPI, task.objInfo, newLifecycleAuditEvent(task.src, task.event)); err != nil {
|
2023-11-27 01:18:09 -05:00
|
|
|
if !isErrVersionNotFound(err) && !isErrObjectNotFound(err) && !xnet.IsNetworkOrHostDown(err, false) {
|
|
|
|
if !strings.Contains(err.Error(), "use of closed network connection") {
|
2024-04-04 08:04:40 -04:00
|
|
|
transitionLogIf(t.ctx, fmt.Errorf("Transition to %s failed for %s/%s version:%s with %w",
|
2023-11-27 01:18:09 -05:00
|
|
|
task.event.StorageClass, task.objInfo.Bucket, task.objInfo.Name, task.objInfo.VersionID, err))
|
|
|
|
}
|
2023-08-26 02:34:16 -04:00
|
|
|
}
|
2022-02-16 20:29:12 -05:00
|
|
|
} else {
|
|
|
|
ts := tierStats{
|
2022-10-21 13:46:53 -04:00
|
|
|
TotalSize: uint64(task.objInfo.Size),
|
2022-02-16 20:29:12 -05:00
|
|
|
NumVersions: 1,
|
|
|
|
}
|
2022-10-21 13:46:53 -04:00
|
|
|
if task.objInfo.IsLatest {
|
2022-02-16 20:29:12 -05:00
|
|
|
ts.NumObjects = 1
|
|
|
|
}
|
2023-04-26 20:49:00 -04:00
|
|
|
t.addLastDayStats(task.event.StorageClass, ts)
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
2023-12-02 16:02:12 -05:00
|
|
|
t.activeTasks.Add(-1)
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
2021-08-12 01:23:56 -04:00
|
|
|
}
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
|
|
|
|
2022-01-26 17:33:10 -05:00
|
|
|
func (t *transitionState) addLastDayStats(tier string, ts tierStats) {
|
|
|
|
t.lastDayMu.Lock()
|
|
|
|
defer t.lastDayMu.Unlock()
|
|
|
|
|
|
|
|
if _, ok := t.lastDayStats[tier]; !ok {
|
|
|
|
t.lastDayStats[tier] = &lastDayTierStats{}
|
|
|
|
}
|
|
|
|
t.lastDayStats[tier].addStats(ts)
|
|
|
|
}
|
|
|
|
|
2022-04-13 17:21:55 -04:00
|
|
|
func (t *transitionState) getDailyAllTierStats() DailyAllTierStats {
|
2022-01-26 17:33:10 -05:00
|
|
|
t.lastDayMu.RLock()
|
|
|
|
defer t.lastDayMu.RUnlock()
|
|
|
|
|
2022-04-13 17:21:55 -04:00
|
|
|
res := make(DailyAllTierStats, len(t.lastDayStats))
|
2022-01-26 17:33:10 -05:00
|
|
|
for tier, st := range t.lastDayStats {
|
|
|
|
res[tier] = st.clone()
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2021-08-12 01:23:56 -04:00
|
|
|
// UpdateWorkers at the end of this function leaves n goroutines waiting for
|
|
|
|
// transition tasks
|
|
|
|
func (t *transitionState) UpdateWorkers(n int) {
|
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
2023-02-08 22:13:34 -05:00
|
|
|
if t.objAPI == nil { // Init hasn't been called yet.
|
|
|
|
return
|
|
|
|
}
|
2023-02-08 02:11:42 -05:00
|
|
|
t.updateWorkers(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *transitionState) updateWorkers(n int) {
|
2021-08-12 01:23:56 -04:00
|
|
|
for t.numWorkers < n {
|
2023-02-16 01:09:25 -05:00
|
|
|
go t.worker(t.objAPI)
|
2021-08-12 01:23:56 -04:00
|
|
|
t.numWorkers++
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
|
|
|
|
2021-08-12 01:23:56 -04:00
|
|
|
for t.numWorkers > n {
|
|
|
|
go func() { t.killCh <- struct{}{} }()
|
|
|
|
t.numWorkers--
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-19 21:51:23 -04:00
|
|
|
var errInvalidStorageClass = errors.New("invalid storage class")
|
|
|
|
|
2021-07-21 22:12:44 -04:00
|
|
|
func validateTransitionTier(lc *lifecycle.Lifecycle) error {
|
|
|
|
for _, rule := range lc.Rules {
|
2021-10-01 14:58:17 -04:00
|
|
|
if rule.Transition.StorageClass != "" {
|
|
|
|
if valid := globalTierConfigMgr.IsTierValid(rule.Transition.StorageClass); !valid {
|
|
|
|
return errInvalidStorageClass
|
|
|
|
}
|
2021-05-19 21:51:23 -04:00
|
|
|
}
|
2021-10-01 14:58:17 -04:00
|
|
|
if rule.NoncurrentVersionTransition.StorageClass != "" {
|
|
|
|
if valid := globalTierConfigMgr.IsTierValid(rule.NoncurrentVersionTransition.StorageClass); !valid {
|
|
|
|
return errInvalidStorageClass
|
|
|
|
}
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
|
|
|
}
|
2021-04-19 13:30:42 -04:00
|
|
|
return nil
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
|
|
|
|
2021-10-01 14:58:17 -04:00
|
|
|
// enqueueTransitionImmediate enqueues obj for transition if eligible.
|
|
|
|
// This is to be called after a successful upload of an object (version).
|
2023-05-22 18:28:56 -04:00
|
|
|
func enqueueTransitionImmediate(obj ObjectInfo, src lcEventSrc) {
|
2021-10-01 14:58:17 -04:00
|
|
|
if lc, err := globalLifecycleSys.Get(obj.Bucket); err == nil {
|
2023-04-26 20:49:00 -04:00
|
|
|
switch event := lc.Eval(obj.ToLifecycleOpts()); event.Action {
|
2021-10-01 14:58:17 -04:00
|
|
|
case lifecycle.TransitionAction, lifecycle.TransitionVersionAction:
|
2023-12-02 16:02:12 -05:00
|
|
|
globalTransitionState.queueTransitionTask(obj, event, src)
|
2021-10-01 14:58:17 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 13:30:42 -04:00
|
|
|
// expireTransitionedObject handles expiry of transitioned/restored objects
|
|
|
|
// (versions) in one of the following situations:
|
|
|
|
//
|
|
|
|
// 1. when a restored (via PostRestoreObject API) object expires.
|
|
|
|
// 2. when a transitioned object expires (based on an ILM rule).
|
2024-02-22 18:00:32 -05:00
|
|
|
func expireTransitionedObject(ctx context.Context, objectAPI ObjectLayer, oi *ObjectInfo, lcEvent lifecycle.Event, src lcEventSrc) error {
|
2023-04-11 22:22:32 -04:00
|
|
|
traceFn := globalLifecycleSys.trace(*oi)
|
2024-02-22 18:00:32 -05:00
|
|
|
opts := ObjectOptions{
|
|
|
|
Versioned: globalBucketVersioningSys.PrefixEnabled(oi.Bucket, oi.Name),
|
|
|
|
Expiration: ExpirationOptions{Expire: true},
|
|
|
|
}
|
|
|
|
if lcEvent.Action.DeleteVersioned() {
|
|
|
|
opts.VersionID = oi.VersionID
|
|
|
|
}
|
2023-05-22 18:28:56 -04:00
|
|
|
tags := newLifecycleAuditEvent(src, lcEvent).Tags()
|
2023-04-26 20:49:00 -04:00
|
|
|
if lcEvent.Action.DeleteRestored() {
|
2020-11-12 15:12:09 -05:00
|
|
|
// delete locally restored copy of object or object version
|
|
|
|
// from the source, while leaving metadata behind. The data on
|
|
|
|
// transitioned tier lies untouched and still accessible
|
2021-04-19 13:30:42 -04:00
|
|
|
opts.Transition.ExpireRestored = true
|
2021-06-03 17:26:51 -04:00
|
|
|
_, err := objectAPI.DeleteObject(ctx, oi.Bucket, oi.Name, opts)
|
2023-04-26 20:49:00 -04:00
|
|
|
if err == nil {
|
|
|
|
// TODO consider including expiry of restored object to events we
|
|
|
|
// notify.
|
|
|
|
auditLogLifecycle(ctx, *oi, ILMExpiry, tags, traceFn)
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2024-03-02 00:11:03 -05:00
|
|
|
|
|
|
|
// Delete remote object from warm-tier
|
|
|
|
err := deleteObjectFromRemoteTier(ctx, oi.TransitionedObject.Name, oi.TransitionedObject.VersionID, oi.TransitionedObject.Tier)
|
|
|
|
if err == nil {
|
|
|
|
// Skip adding free version since we successfully deleted the
|
|
|
|
// remote object
|
|
|
|
opts.SkipFreeVersion = true
|
|
|
|
} else {
|
2024-04-04 08:04:40 -04:00
|
|
|
transitionLogIf(ctx, err)
|
2023-04-26 20:49:00 -04:00
|
|
|
}
|
2024-03-02 00:11:03 -05:00
|
|
|
|
|
|
|
// Now, delete object from hot-tier namespace
|
2023-04-26 20:49:00 -04:00
|
|
|
if _, err := objectAPI.DeleteObject(ctx, oi.Bucket, oi.Name, opts); err != nil {
|
2020-11-12 15:12:09 -05:00
|
|
|
return err
|
2021-02-01 12:52:11 -05:00
|
|
|
}
|
2021-01-25 17:04:41 -05:00
|
|
|
|
2023-04-26 20:49:00 -04:00
|
|
|
// Send audit for the lifecycle delete operation
|
|
|
|
defer auditLogLifecycle(ctx, *oi, ILMExpiry, tags, traceFn)
|
|
|
|
|
|
|
|
eventName := event.ObjectRemovedDelete
|
2024-02-22 18:00:32 -05:00
|
|
|
if oi.DeleteMarker {
|
2023-04-26 20:49:00 -04:00
|
|
|
eventName = event.ObjectRemovedDeleteMarkerCreated
|
|
|
|
}
|
|
|
|
objInfo := ObjectInfo{
|
|
|
|
Name: oi.Name,
|
2024-02-22 18:00:32 -05:00
|
|
|
VersionID: oi.VersionID,
|
|
|
|
DeleteMarker: oi.DeleteMarker,
|
2023-04-26 20:49:00 -04:00
|
|
|
}
|
|
|
|
// Notify object deleted event.
|
|
|
|
sendEvent(eventArgs{
|
|
|
|
EventName: eventName,
|
|
|
|
BucketName: oi.Bucket,
|
|
|
|
Object: objInfo,
|
|
|
|
UserAgent: "Internal: [ILM-Expiry]",
|
|
|
|
Host: globalLocalNodeName,
|
|
|
|
})
|
|
|
|
|
2021-04-19 13:30:42 -04:00
|
|
|
return nil
|
|
|
|
}
|
2021-01-25 17:04:41 -05:00
|
|
|
|
2021-04-19 13:30:42 -04:00
|
|
|
// generate an object name for transitioned object
|
2021-07-20 13:49:52 -04:00
|
|
|
func genTransitionObjName(bucket string) (string, error) {
|
2021-04-19 13:30:42 -04:00
|
|
|
u, err := uuid.NewRandom()
|
2021-02-01 12:52:11 -05:00
|
|
|
if err != nil {
|
2021-04-19 13:30:42 -04:00
|
|
|
return "", err
|
2021-02-01 12:52:11 -05:00
|
|
|
}
|
2021-04-19 13:30:42 -04:00
|
|
|
us := u.String()
|
2023-11-10 05:15:13 -05:00
|
|
|
hash := xxh3.HashString(pathJoin(globalDeploymentID(), bucket))
|
|
|
|
obj := fmt.Sprintf("%s/%s/%s/%s", strconv.FormatUint(hash, 16), us[0:2], us[2:4], us)
|
2021-04-19 13:30:42 -04:00
|
|
|
return obj, nil
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// transition object to target specified by the transition ARN. When an object is transitioned to another
|
|
|
|
// storage specified by the transition ARN, the metadata is left behind on source cluster and original content
|
|
|
|
// is moved to the transition tier. Note that in the case of encrypted objects, entire encrypted stream is moved
|
|
|
|
// to the transition tier without decrypting or re-encrypting.
|
2024-02-22 18:00:32 -05:00
|
|
|
func transitionObject(ctx context.Context, objectAPI ObjectLayer, oi ObjectInfo, lae lcAuditEvent) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
globalScannerMetrics.timeILM(lae.Action)(1)
|
|
|
|
}()
|
|
|
|
|
2021-06-03 17:26:51 -04:00
|
|
|
opts := ObjectOptions{
|
|
|
|
Transition: TransitionOptions{
|
|
|
|
Status: lifecycle.TransitionPending,
|
2023-05-22 18:28:56 -04:00
|
|
|
Tier: lae.StorageClass,
|
2021-06-03 17:26:51 -04:00
|
|
|
ETag: oi.ETag,
|
|
|
|
},
|
2023-05-22 18:28:56 -04:00
|
|
|
LifecycleAuditEvent: lae,
|
|
|
|
VersionID: oi.VersionID,
|
|
|
|
Versioned: globalBucketVersioningSys.PrefixEnabled(oi.Bucket, oi.Name),
|
|
|
|
VersionSuspended: globalBucketVersioningSys.PrefixSuspended(oi.Bucket, oi.Name),
|
|
|
|
MTime: oi.ModTime,
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
2022-10-21 13:46:53 -04:00
|
|
|
return objectAPI.TransitionObject(ctx, oi.Bucket, oi.Name, opts)
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
|
|
|
|
2022-08-30 00:33:59 -04:00
|
|
|
type auditTierOp struct {
|
|
|
|
Tier string `json:"tier"`
|
|
|
|
TimeToResponseNS int64 `json:"timeToResponseNS"`
|
|
|
|
OutputBytes int64 `json:"tx,omitempty"`
|
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func auditTierActions(ctx context.Context, tier string, bytes int64) func(err error) {
|
|
|
|
startTime := time.Now()
|
|
|
|
return func(err error) {
|
|
|
|
// Record only when audit targets configured.
|
|
|
|
if len(logger.AuditTargets()) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
op := auditTierOp{
|
|
|
|
Tier: tier,
|
|
|
|
OutputBytes: bytes,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil {
|
2023-12-20 23:13:40 -05:00
|
|
|
since := time.Since(startTime)
|
|
|
|
op.TimeToResponseNS = since.Nanoseconds()
|
|
|
|
globalTierMetrics.Observe(tier, since)
|
|
|
|
globalTierMetrics.logSuccess(tier)
|
2022-08-30 00:33:59 -04:00
|
|
|
} else {
|
|
|
|
op.Error = err.Error()
|
2023-12-20 23:13:40 -05:00
|
|
|
globalTierMetrics.logFailure(tier)
|
2022-08-30 00:33:59 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
logger.GetReqInfo(ctx).AppendTags("tierStats", op)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-12 15:12:09 -05:00
|
|
|
// getTransitionedObjectReader returns a reader from the transitioned tier.
|
|
|
|
func getTransitionedObjectReader(ctx context.Context, bucket, object string, rs *HTTPRangeSpec, h http.Header, oi ObjectInfo, opts ObjectOptions) (gr *GetObjectReader, err error) {
|
2024-04-17 01:09:58 -04:00
|
|
|
tgtClient, err := globalTierConfigMgr.getDriver(ctx, oi.TransitionedObject.Tier)
|
2020-11-12 15:12:09 -05:00
|
|
|
if err != nil {
|
2024-04-17 01:09:58 -04:00
|
|
|
return nil, fmt.Errorf("transition storage class not configured: %w", err)
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
2021-04-30 21:37:58 -04:00
|
|
|
|
2020-11-12 15:12:09 -05:00
|
|
|
fn, off, length, err := NewGetObjectReader(rs, oi, opts)
|
|
|
|
if err != nil {
|
|
|
|
return nil, ErrorRespToObjectError(err, bucket, object)
|
|
|
|
}
|
2021-04-19 13:30:42 -04:00
|
|
|
gopts := WarmBackendGetOpts{}
|
2020-11-12 15:12:09 -05:00
|
|
|
|
2021-04-19 13:30:42 -04:00
|
|
|
// get correct offsets for object
|
2020-11-12 15:12:09 -05:00
|
|
|
if off >= 0 && length >= 0 {
|
2021-04-19 13:30:42 -04:00
|
|
|
gopts.startOffset = off
|
|
|
|
gopts.length = length
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
|
|
|
|
2022-08-30 00:33:59 -04:00
|
|
|
timeTierAction := auditTierActions(ctx, oi.TransitionedObject.Tier, length)
|
2021-08-17 10:50:00 -04:00
|
|
|
reader, err := tgtClient.Get(ctx, oi.TransitionedObject.Name, remoteVersionID(oi.TransitionedObject.VersionID), gopts)
|
2020-11-12 15:12:09 -05:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-04-28 01:42:18 -04:00
|
|
|
closer := func() {
|
2022-08-30 00:33:59 -04:00
|
|
|
timeTierAction(reader.Close())
|
2021-04-28 01:42:18 -04:00
|
|
|
}
|
2021-06-24 12:44:00 -04:00
|
|
|
return fn(reader, h, closer)
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// RestoreRequestType represents type of restore.
|
|
|
|
type RestoreRequestType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// SelectRestoreRequest specifies select request. This is the only valid value
|
|
|
|
SelectRestoreRequest RestoreRequestType = "SELECT"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Encryption specifies encryption setting on restored bucket
|
|
|
|
type Encryption struct {
|
2021-09-21 12:02:15 -04:00
|
|
|
EncryptionType sse.Algorithm `xml:"EncryptionType"`
|
|
|
|
KMSContext string `xml:"KMSContext,omitempty"`
|
|
|
|
KMSKeyID string `xml:"KMSKeyId,omitempty"`
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// MetadataEntry denotes name and value.
|
|
|
|
type MetadataEntry struct {
|
|
|
|
Name string `xml:"Name"`
|
|
|
|
Value string `xml:"Value"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// S3Location specifies s3 location that receives result of a restore object request
|
|
|
|
type S3Location struct {
|
|
|
|
BucketName string `xml:"BucketName,omitempty"`
|
|
|
|
Encryption Encryption `xml:"Encryption,omitempty"`
|
|
|
|
Prefix string `xml:"Prefix,omitempty"`
|
|
|
|
StorageClass string `xml:"StorageClass,omitempty"`
|
|
|
|
Tagging *tags.Tags `xml:"Tagging,omitempty"`
|
|
|
|
UserMetadata []MetadataEntry `xml:"UserMetadata"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// OutputLocation specifies bucket where object needs to be restored
|
|
|
|
type OutputLocation struct {
|
|
|
|
S3 S3Location `xml:"S3,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEmpty returns true if output location not specified.
|
|
|
|
func (o *OutputLocation) IsEmpty() bool {
|
|
|
|
return o.S3.BucketName == ""
|
|
|
|
}
|
|
|
|
|
|
|
|
// SelectParameters specifies sql select parameters
|
|
|
|
type SelectParameters struct {
|
|
|
|
s3select.S3Select
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsEmpty returns true if no select parameters set
|
|
|
|
func (sp *SelectParameters) IsEmpty() bool {
|
2021-04-07 16:29:27 -04:00
|
|
|
return sp == nil
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
|
|
|
|
2022-01-02 12:15:06 -05:00
|
|
|
var selectParamsXMLName = "SelectParameters"
|
2020-11-12 15:12:09 -05:00
|
|
|
|
|
|
|
// UnmarshalXML - decodes XML data.
|
|
|
|
func (sp *SelectParameters) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
|
|
|
// Essentially the same as S3Select barring the xml name.
|
|
|
|
if start.Name.Local == selectParamsXMLName {
|
|
|
|
start.Name = xml.Name{Space: "", Local: "SelectRequest"}
|
|
|
|
}
|
|
|
|
return sp.S3Select.UnmarshalXML(d, start)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RestoreObjectRequest - xml to restore a transitioned object
|
|
|
|
type RestoreObjectRequest struct {
|
|
|
|
XMLName xml.Name `xml:"http://s3.amazonaws.com/doc/2006-03-01/ RestoreRequest" json:"-"`
|
|
|
|
Days int `xml:"Days,omitempty"`
|
|
|
|
Type RestoreRequestType `xml:"Type,omitempty"`
|
2022-12-05 14:18:50 -05:00
|
|
|
Tier string `xml:"Tier"`
|
2020-11-12 15:12:09 -05:00
|
|
|
Description string `xml:"Description,omitempty"`
|
|
|
|
SelectParameters *SelectParameters `xml:"SelectParameters,omitempty"`
|
|
|
|
OutputLocation OutputLocation `xml:"OutputLocation,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Maximum 2MiB size per restore object request.
|
|
|
|
const maxRestoreObjectRequestSize = 2 << 20
|
|
|
|
|
|
|
|
// parseRestoreRequest parses RestoreObjectRequest from xml
|
|
|
|
func parseRestoreRequest(reader io.Reader) (*RestoreObjectRequest, error) {
|
|
|
|
req := RestoreObjectRequest{}
|
|
|
|
if err := xml.NewDecoder(io.LimitReader(reader, maxRestoreObjectRequestSize)).Decode(&req); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &req, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate a RestoreObjectRequest as per AWS S3 spec https://docs.aws.amazon.com/AmazonS3/latest/API/API_RestoreObject.html
|
|
|
|
func (r *RestoreObjectRequest) validate(ctx context.Context, objAPI ObjectLayer) error {
|
|
|
|
if r.Type != SelectRestoreRequest && !r.SelectParameters.IsEmpty() {
|
|
|
|
return fmt.Errorf("Select parameters can only be specified with SELECT request type")
|
|
|
|
}
|
|
|
|
if r.Type == SelectRestoreRequest && r.SelectParameters.IsEmpty() {
|
|
|
|
return fmt.Errorf("SELECT restore request requires select parameters to be specified")
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Type != SelectRestoreRequest && !r.OutputLocation.IsEmpty() {
|
|
|
|
return fmt.Errorf("OutputLocation required only for SELECT request type")
|
|
|
|
}
|
|
|
|
if r.Type == SelectRestoreRequest && r.OutputLocation.IsEmpty() {
|
|
|
|
return fmt.Errorf("OutputLocation required for SELECT requests")
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Days != 0 && r.Type == SelectRestoreRequest {
|
|
|
|
return fmt.Errorf("Days cannot be specified with SELECT restore request")
|
|
|
|
}
|
|
|
|
if r.Days == 0 && r.Type != SelectRestoreRequest {
|
|
|
|
return fmt.Errorf("restoration days should be at least 1")
|
|
|
|
}
|
|
|
|
// Check if bucket exists.
|
|
|
|
if !r.OutputLocation.IsEmpty() {
|
2022-07-25 20:51:32 -04:00
|
|
|
if _, err := objAPI.GetBucketInfo(ctx, r.OutputLocation.S3.BucketName, BucketOptions{}); err != nil {
|
2020-11-12 15:12:09 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if r.OutputLocation.S3.Prefix == "" {
|
|
|
|
return fmt.Errorf("Prefix is a required parameter in OutputLocation")
|
|
|
|
}
|
2020-12-22 12:19:32 -05:00
|
|
|
if r.OutputLocation.S3.Encryption.EncryptionType != xhttp.AmzEncryptionAES {
|
2020-11-12 15:12:09 -05:00
|
|
|
return NotImplemented{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-04-19 13:30:42 -04:00
|
|
|
// postRestoreOpts returns ObjectOptions with version-id from the POST restore object request for a given bucket and object.
|
|
|
|
func postRestoreOpts(ctx context.Context, r *http.Request, bucket, object string) (opts ObjectOptions, err error) {
|
2022-05-06 22:05:28 -04:00
|
|
|
versioned := globalBucketVersioningSys.PrefixEnabled(bucket, object)
|
|
|
|
versionSuspended := globalBucketVersioningSys.PrefixSuspended(bucket, object)
|
2021-08-08 01:43:01 -04:00
|
|
|
vid := strings.TrimSpace(r.Form.Get(xhttp.VersionID))
|
2021-04-19 13:30:42 -04:00
|
|
|
if vid != "" && vid != nullVersionID {
|
|
|
|
_, err := uuid.Parse(vid)
|
|
|
|
if err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
s3LogIf(ctx, err)
|
2021-04-19 13:30:42 -04:00
|
|
|
return opts, InvalidVersionID{
|
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
VersionID: vid,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !versioned && !versionSuspended {
|
|
|
|
return opts, InvalidArgument{
|
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
Err: fmt.Errorf("version-id specified %s but versioning is not enabled on %s", opts.VersionID, bucket),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ObjectOptions{
|
|
|
|
Versioned: versioned,
|
|
|
|
VersionSuspended: versionSuspended,
|
|
|
|
VersionID: vid,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2020-11-12 15:12:09 -05:00
|
|
|
// set ObjectOptions for PUT call to restore temporary copy of transitioned data
|
|
|
|
func putRestoreOpts(bucket, object string, rreq *RestoreObjectRequest, objInfo ObjectInfo) (putOpts ObjectOptions) {
|
|
|
|
meta := make(map[string]string)
|
|
|
|
sc := rreq.OutputLocation.S3.StorageClass
|
|
|
|
if sc == "" {
|
|
|
|
sc = objInfo.StorageClass
|
|
|
|
}
|
|
|
|
meta[strings.ToLower(xhttp.AmzStorageClass)] = sc
|
|
|
|
|
|
|
|
if rreq.Type == SelectRestoreRequest {
|
|
|
|
for _, v := range rreq.OutputLocation.S3.UserMetadata {
|
2023-07-06 19:02:08 -04:00
|
|
|
if !stringsHasPrefixFold(v.Name, "x-amz-meta") {
|
2020-11-12 15:12:09 -05:00
|
|
|
meta["x-amz-meta-"+v.Name] = v.Value
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
meta[v.Name] = v.Value
|
|
|
|
}
|
2021-04-10 12:13:12 -04:00
|
|
|
if tags := rreq.OutputLocation.S3.Tagging.String(); tags != "" {
|
|
|
|
meta[xhttp.AmzObjectTagging] = tags
|
|
|
|
}
|
2020-11-12 15:12:09 -05:00
|
|
|
if rreq.OutputLocation.S3.Encryption.EncryptionType != "" {
|
2020-12-22 12:19:32 -05:00
|
|
|
meta[xhttp.AmzServerSideEncryption] = xhttp.AmzEncryptionAES
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
|
|
|
return ObjectOptions{
|
2022-05-06 22:05:28 -04:00
|
|
|
Versioned: globalBucketVersioningSys.PrefixEnabled(bucket, object),
|
|
|
|
VersionSuspended: globalBucketVersioningSys.PrefixSuspended(bucket, object),
|
2020-11-12 15:12:09 -05:00
|
|
|
UserDefined: meta,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for k, v := range objInfo.UserDefined {
|
|
|
|
meta[k] = v
|
|
|
|
}
|
2021-04-10 12:13:12 -04:00
|
|
|
if len(objInfo.UserTags) != 0 {
|
|
|
|
meta[xhttp.AmzObjectTagging] = objInfo.UserTags
|
|
|
|
}
|
2023-01-30 21:23:45 -05:00
|
|
|
// Set restore object status
|
|
|
|
restoreExpiry := lifecycle.ExpectedExpiryTime(time.Now().UTC(), rreq.Days)
|
|
|
|
meta[xhttp.AmzRestore] = completedRestoreObj(restoreExpiry).String()
|
2020-11-12 15:12:09 -05:00
|
|
|
return ObjectOptions{
|
2022-05-06 22:05:28 -04:00
|
|
|
Versioned: globalBucketVersioningSys.PrefixEnabled(bucket, object),
|
|
|
|
VersionSuspended: globalBucketVersioningSys.PrefixSuspended(bucket, object),
|
2020-11-12 15:12:09 -05:00
|
|
|
UserDefined: meta,
|
|
|
|
VersionID: objInfo.VersionID,
|
|
|
|
MTime: objInfo.ModTime,
|
|
|
|
Expires: objInfo.Expires,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-19 13:30:42 -04:00
|
|
|
var errRestoreHDRMalformed = fmt.Errorf("x-amz-restore header malformed")
|
|
|
|
|
2021-05-04 11:40:42 -04:00
|
|
|
// IsRemote returns true if this object version's contents are in its remote
|
|
|
|
// tier.
|
|
|
|
func (fi FileInfo) IsRemote() bool {
|
|
|
|
if fi.TransitionStatus != lifecycle.TransitionComplete {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return !isRestoredObjectOnDisk(fi.Metadata)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsRemote returns true if this object version's contents are in its remote
|
|
|
|
// tier.
|
|
|
|
func (oi ObjectInfo) IsRemote() bool {
|
2021-08-17 10:50:00 -04:00
|
|
|
if oi.TransitionedObject.Status != lifecycle.TransitionComplete {
|
2021-05-04 11:40:42 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return !isRestoredObjectOnDisk(oi.UserDefined)
|
|
|
|
}
|
|
|
|
|
2021-04-19 13:30:42 -04:00
|
|
|
// restoreObjStatus represents a restore-object's status. It can be either
|
|
|
|
// ongoing or completed.
|
|
|
|
type restoreObjStatus struct {
|
|
|
|
ongoing bool
|
|
|
|
expiry time.Time
|
|
|
|
}
|
2020-11-12 15:12:09 -05:00
|
|
|
|
2021-04-19 13:30:42 -04:00
|
|
|
// ongoingRestoreObj constructs restoreObjStatus for an ongoing restore-object.
|
|
|
|
func ongoingRestoreObj() restoreObjStatus {
|
|
|
|
return restoreObjStatus{
|
|
|
|
ongoing: true,
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
2021-04-19 13:30:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// completeRestoreObj constructs restoreObjStatus for a completed restore-object with given expiry.
|
|
|
|
func completedRestoreObj(expiry time.Time) restoreObjStatus {
|
|
|
|
return restoreObjStatus{
|
|
|
|
ongoing: false,
|
|
|
|
expiry: expiry.UTC(),
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
2021-04-19 13:30:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns x-amz-restore compatible representation of r.
|
|
|
|
func (r restoreObjStatus) String() string {
|
|
|
|
if r.Ongoing() {
|
2022-02-09 16:17:41 -05:00
|
|
|
return `ongoing-request="true"`
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
2022-02-09 16:17:41 -05:00
|
|
|
return fmt.Sprintf(`ongoing-request="false", expiry-date="%s"`, r.expiry.Format(http.TimeFormat))
|
2021-04-19 13:30:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Expiry returns expiry of restored object and true if restore-object has completed.
|
|
|
|
// Otherwise returns zero value of time.Time and false.
|
|
|
|
func (r restoreObjStatus) Expiry() (time.Time, bool) {
|
|
|
|
if r.Ongoing() {
|
|
|
|
return time.Time{}, false
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
2021-04-19 13:30:42 -04:00
|
|
|
return r.expiry, true
|
|
|
|
}
|
2020-11-12 15:12:09 -05:00
|
|
|
|
2021-04-19 13:30:42 -04:00
|
|
|
// Ongoing returns true if restore-object is ongoing.
|
|
|
|
func (r restoreObjStatus) Ongoing() bool {
|
|
|
|
return r.ongoing
|
|
|
|
}
|
|
|
|
|
|
|
|
// OnDisk returns true if restored object contents exist in MinIO. Otherwise returns false.
|
|
|
|
// The restore operation could be in one of the following states,
|
|
|
|
// - in progress (no content on MinIO's disks yet)
|
|
|
|
// - completed
|
|
|
|
// - completed but expired (again, no content on MinIO's disks)
|
|
|
|
func (r restoreObjStatus) OnDisk() bool {
|
|
|
|
if expiry, ok := r.Expiry(); ok && time.Now().UTC().Before(expiry) {
|
|
|
|
// completed
|
|
|
|
return true
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
2021-04-19 13:30:42 -04:00
|
|
|
return false // in progress or completed but expired
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
|
|
|
|
2021-04-19 13:30:42 -04:00
|
|
|
// parseRestoreObjStatus parses restoreHdr from AmzRestore header. If the value is valid it returns a
|
|
|
|
// restoreObjStatus value with the status and expiry (if any). Otherwise returns
|
|
|
|
// the empty value and an error indicating the parse failure.
|
|
|
|
func parseRestoreObjStatus(restoreHdr string) (restoreObjStatus, error) {
|
|
|
|
tokens := strings.SplitN(restoreHdr, ",", 2)
|
|
|
|
progressTokens := strings.SplitN(tokens[0], "=", 2)
|
|
|
|
if len(progressTokens) != 2 {
|
|
|
|
return restoreObjStatus{}, errRestoreHDRMalformed
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
2021-04-19 13:30:42 -04:00
|
|
|
if strings.TrimSpace(progressTokens[0]) != "ongoing-request" {
|
|
|
|
return restoreObjStatus{}, errRestoreHDRMalformed
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
2021-04-19 13:30:42 -04:00
|
|
|
|
|
|
|
switch progressTokens[1] {
|
2022-02-09 16:17:41 -05:00
|
|
|
case "true", `"true"`: // true without double quotes is deprecated in Feb 2022
|
2021-04-19 13:30:42 -04:00
|
|
|
if len(tokens) == 1 {
|
|
|
|
return ongoingRestoreObj(), nil
|
|
|
|
}
|
2022-02-09 16:17:41 -05:00
|
|
|
case "false", `"false"`: // false without double quotes is deprecated in Feb 2022
|
2021-04-19 13:30:42 -04:00
|
|
|
if len(tokens) != 2 {
|
|
|
|
return restoreObjStatus{}, errRestoreHDRMalformed
|
|
|
|
}
|
|
|
|
expiryTokens := strings.SplitN(tokens[1], "=", 2)
|
|
|
|
if len(expiryTokens) != 2 {
|
|
|
|
return restoreObjStatus{}, errRestoreHDRMalformed
|
|
|
|
}
|
|
|
|
if strings.TrimSpace(expiryTokens[0]) != "expiry-date" {
|
|
|
|
return restoreObjStatus{}, errRestoreHDRMalformed
|
|
|
|
}
|
2022-09-07 10:24:54 -04:00
|
|
|
expiry, err := amztime.ParseHeader(strings.Trim(expiryTokens[1], `"`))
|
2021-04-19 13:30:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return restoreObjStatus{}, errRestoreHDRMalformed
|
|
|
|
}
|
|
|
|
return completedRestoreObj(expiry), nil
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
2021-04-19 13:30:42 -04:00
|
|
|
return restoreObjStatus{}, errRestoreHDRMalformed
|
|
|
|
}
|
2020-11-12 15:12:09 -05:00
|
|
|
|
2021-04-19 13:30:42 -04:00
|
|
|
// isRestoredObjectOnDisk returns true if the restored object is on disk. Note
|
|
|
|
// this function must be called only if object version's transition status is
|
|
|
|
// complete.
|
|
|
|
func isRestoredObjectOnDisk(meta map[string]string) (onDisk bool) {
|
|
|
|
if restoreHdr, ok := meta[xhttp.AmzRestore]; ok {
|
|
|
|
if restoreStatus, err := parseRestoreObjStatus(restoreHdr); err == nil {
|
|
|
|
return restoreStatus.OnDisk()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return onDisk
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
2021-07-20 20:36:55 -04:00
|
|
|
|
|
|
|
// ToLifecycleOpts returns lifecycle.ObjectOpts value for oi.
|
|
|
|
func (oi ObjectInfo) ToLifecycleOpts() lifecycle.ObjectOpts {
|
|
|
|
return lifecycle.ObjectOpts{
|
2021-10-01 14:58:17 -04:00
|
|
|
Name: oi.Name,
|
|
|
|
UserTags: oi.UserTags,
|
|
|
|
VersionID: oi.VersionID,
|
|
|
|
ModTime: oi.ModTime,
|
2023-11-22 16:42:39 -05:00
|
|
|
Size: oi.Size,
|
2021-10-01 14:58:17 -04:00
|
|
|
IsLatest: oi.IsLatest,
|
|
|
|
NumVersions: oi.NumVersions,
|
|
|
|
DeleteMarker: oi.DeleteMarker,
|
|
|
|
SuccessorModTime: oi.SuccessorModTime,
|
|
|
|
RestoreOngoing: oi.RestoreOngoing,
|
|
|
|
RestoreExpires: oi.RestoreExpires,
|
|
|
|
TransitionStatus: oi.TransitionedObject.Status,
|
2021-07-20 20:36:55 -04:00
|
|
|
}
|
|
|
|
}
|