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/>.
|
2020-06-12 13:28:21 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
2020-12-04 12:32:35 -05:00
|
|
|
"math"
|
2020-09-25 22:21:52 -04:00
|
|
|
"math/rand"
|
2021-02-05 12:57:30 -05:00
|
|
|
"net/http"
|
2020-06-12 13:28:21 -04:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2020-12-04 12:32:35 -05:00
|
|
|
"sync"
|
2020-06-12 13:28:21 -04:00
|
|
|
"time"
|
|
|
|
|
2021-05-27 11:02:39 -04:00
|
|
|
"github.com/bits-and-blooms/bloom/v3"
|
2021-05-06 11:52:02 -04:00
|
|
|
"github.com/minio/madmin-go"
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/bucket/lifecycle"
|
|
|
|
"github.com/minio/minio/internal/bucket/replication"
|
|
|
|
"github.com/minio/minio/internal/color"
|
|
|
|
"github.com/minio/minio/internal/config/heal"
|
|
|
|
"github.com/minio/minio/internal/event"
|
|
|
|
"github.com/minio/minio/internal/hash"
|
|
|
|
"github.com/minio/minio/internal/logger"
|
2021-05-28 18:17:01 -04:00
|
|
|
"github.com/minio/pkg/console"
|
2020-06-12 13:28:21 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-05-11 21:36:15 -04:00
|
|
|
dataScannerSleepPerFolder = time.Millisecond // Time to wait between folders.
|
|
|
|
dataUsageUpdateDirCycles = 16 // Visit all folders every n cycles.
|
|
|
|
dataScannerCompactLeastObject = 500 // Compact when there is less than this many objects in a branch.
|
|
|
|
dataScannerCompactAtChildren = 10000 // Compact when there are this many children in a branch.
|
|
|
|
dataScannerCompactAtFolders = dataScannerCompactAtChildren / 4 // Compact when this many subfolders in a single folder.
|
|
|
|
dataScannerStartDelay = 1 * time.Minute // Time to wait on startup and between cycles.
|
2020-06-12 13:28:21 -04:00
|
|
|
|
2020-09-12 03:08:12 -04:00
|
|
|
healDeleteDangling = true
|
|
|
|
healFolderIncludeProb = 32 // Include a clean folder one in n cycles.
|
|
|
|
healObjectSelectProb = 512 // Overall probability of a file being scanned; one in n.
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2020-12-04 12:32:35 -05:00
|
|
|
globalHealConfig heal.Config
|
|
|
|
globalHealConfigMu sync.Mutex
|
|
|
|
|
2021-02-17 15:04:11 -05:00
|
|
|
dataScannerLeaderLockTimeout = newDynamicTimeout(30*time.Second, 10*time.Second)
|
2020-12-04 12:32:35 -05:00
|
|
|
// Sleeper values are updated when config is loaded.
|
2021-02-17 15:04:11 -05:00
|
|
|
scannerSleeper = newDynamicSleeper(10, 10*time.Second)
|
2021-04-27 11:24:44 -04:00
|
|
|
scannerCycle = &safeDuration{
|
|
|
|
t: dataScannerStartDelay,
|
|
|
|
}
|
2020-06-12 13:28:21 -04:00
|
|
|
)
|
|
|
|
|
2021-02-17 15:04:11 -05:00
|
|
|
// initDataScanner will start the scanner in the background.
|
|
|
|
func initDataScanner(ctx context.Context, objAPI ObjectLayer) {
|
|
|
|
go runDataScanner(ctx, objAPI)
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
|
2021-03-30 16:59:02 -04:00
|
|
|
type safeDuration struct {
|
|
|
|
sync.Mutex
|
|
|
|
t time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *safeDuration) Update(t time.Duration) {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
s.t = t
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *safeDuration) Get() time.Duration {
|
|
|
|
s.Lock()
|
|
|
|
defer s.Unlock()
|
|
|
|
return s.t
|
|
|
|
}
|
|
|
|
|
2021-02-17 15:04:11 -05:00
|
|
|
// runDataScanner will start a data scanner.
|
2020-06-12 13:28:21 -04:00
|
|
|
// The function will block until the context is canceled.
|
2021-02-17 15:04:11 -05:00
|
|
|
// There should only ever be one scanner running per cluster.
|
2021-04-29 23:55:21 -04:00
|
|
|
func runDataScanner(pctx context.Context, objAPI ObjectLayer) {
|
2021-02-17 15:04:11 -05:00
|
|
|
// Make sure only 1 scanner is running on the cluster.
|
|
|
|
locker := objAPI.NewNSLock(minioMetaBucket, "runDataScanner.lock")
|
2021-04-29 23:55:21 -04:00
|
|
|
var ctx context.Context
|
2020-09-25 22:21:52 -04:00
|
|
|
r := rand.New(rand.NewSource(time.Now().UnixNano()))
|
2020-09-18 14:15:54 -04:00
|
|
|
for {
|
2021-04-29 23:55:21 -04:00
|
|
|
lkctx, err := locker.GetLock(pctx, dataScannerLeaderLockTimeout)
|
2020-09-18 14:15:54 -04:00
|
|
|
if err != nil {
|
2021-03-30 16:59:02 -04:00
|
|
|
time.Sleep(time.Duration(r.Float64() * float64(scannerCycle.Get())))
|
2020-09-18 14:15:54 -04:00
|
|
|
continue
|
|
|
|
}
|
2021-04-29 23:55:21 -04:00
|
|
|
ctx = lkctx.Context()
|
|
|
|
defer lkctx.Cancel()
|
2020-09-18 14:15:54 -04:00
|
|
|
break
|
|
|
|
// No unlock for "leader" lock.
|
|
|
|
}
|
|
|
|
|
2020-06-12 13:28:21 -04:00
|
|
|
// Load current bloom cycle
|
|
|
|
nextBloomCycle := intDataUpdateTracker.current() + 1
|
2021-02-05 12:57:30 -05:00
|
|
|
|
|
|
|
br, err := objAPI.GetObjectNInfo(ctx, dataUsageBucket, dataUsageBloomName, nil, http.Header{}, readLock, ObjectOptions{})
|
2020-06-12 13:28:21 -04:00
|
|
|
if err != nil {
|
|
|
|
if !isErrObjectNotFound(err) && !isErrBucketNotFound(err) {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
|
|
|
} else {
|
2021-02-05 12:57:30 -05:00
|
|
|
if br.ObjInfo.Size == 8 {
|
|
|
|
if err = binary.Read(br, binary.LittleEndian, &nextBloomCycle); err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
2021-02-05 12:57:30 -05:00
|
|
|
br.Close()
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
|
2021-03-30 16:59:02 -04:00
|
|
|
scannerTimer := time.NewTimer(scannerCycle.Get())
|
2021-02-26 18:11:42 -05:00
|
|
|
defer scannerTimer.Stop()
|
2020-12-17 15:35:02 -05:00
|
|
|
|
2020-06-12 13:28:21 -04:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
2021-02-26 18:11:42 -05:00
|
|
|
case <-scannerTimer.C:
|
2020-12-17 15:35:02 -05:00
|
|
|
// Reset the timer for next cycle.
|
2021-03-30 16:59:02 -04:00
|
|
|
scannerTimer.Reset(scannerCycle.Get())
|
2020-12-17 15:35:02 -05:00
|
|
|
|
2020-12-17 19:52:47 -05:00
|
|
|
if intDataUpdateTracker.debug {
|
2021-02-17 15:04:11 -05:00
|
|
|
console.Debugln("starting scanner cycle")
|
2020-12-17 19:52:47 -05:00
|
|
|
}
|
|
|
|
|
2020-06-12 13:28:21 -04:00
|
|
|
// Wait before starting next cycle and wait on startup.
|
2021-04-03 12:03:42 -04:00
|
|
|
results := make(chan madmin.DataUsageInfo, 1)
|
2020-06-12 13:28:21 -04:00
|
|
|
go storeDataUsageInBackend(ctx, objAPI, results)
|
|
|
|
bf, err := globalNotificationSys.updateBloomFilter(ctx, nextBloomCycle)
|
|
|
|
logger.LogIf(ctx, err)
|
2021-02-26 18:11:42 -05:00
|
|
|
err = objAPI.NSScanner(ctx, bf, results)
|
2020-06-12 13:28:21 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
if err == nil {
|
|
|
|
// Store new cycle...
|
|
|
|
nextBloomCycle++
|
|
|
|
var tmp [8]byte
|
|
|
|
binary.LittleEndian.PutUint64(tmp[:], nextBloomCycle)
|
pkg/etag: add new package for S3 ETag handling (#11577)
This commit adds a new package `etag` for dealing
with S3 ETags.
Even though ETag is often viewed as MD5 checksum of
an object, handling S3 ETags correctly is a surprisingly
complex task. While it is true that the ETag corresponds
to the MD5 for the most basic S3 API operations, there are
many exceptions in case of multipart uploads or encryption.
In worse, some S3 clients expect very specific behavior when
it comes to ETags. For example, some clients expect that the
ETag is a double-quoted string and fail otherwise.
Non-AWS compliant ETag handling has been a source of many bugs
in the past.
Therefore, this commit adds a dedicated `etag` package that provides
functionality for parsing, generating and converting S3 ETags.
Further, this commit removes the ETag computation from the `hash`
package. Instead, the `hash` package (i.e. `hash.Reader`) should
focus only on computing and verifying the content-sha256.
One core feature of this commit is to provide a mechanism to
communicate a computed ETag from a low-level `io.Reader` to
a high-level `io.Reader`.
This problem occurs when an S3 server receives a request and
has to compute the ETag of the content. However, the server
may also wrap the initial body with several other `io.Reader`,
e.g. when encrypting or compressing the content:
```
reader := Encrypt(Compress(ETag(content)))
```
In such a case, the ETag should be accessible by the high-level
`io.Reader`.
The `etag` provides a mechanism to wrap `io.Reader` implementations
such that the `ETag` can be accessed by a type-check.
This technique is applied to the PUT, COPY and Upload handlers.
2021-02-23 15:31:53 -05:00
|
|
|
r, err := hash.NewReader(bytes.NewReader(tmp[:]), int64(len(tmp)), "", "", int64(len(tmp)))
|
2020-06-12 13:28:21 -04:00
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2021-02-10 11:52:50 -05:00
|
|
|
_, err = objAPI.PutObject(ctx, dataUsageBucket, dataUsageBloomName, NewPutObjReader(r), ObjectOptions{})
|
2020-06-12 13:28:21 -04:00
|
|
|
if !isErrBucketNotFound(err) {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type cachedFolder struct {
|
2020-08-24 16:47:01 -04:00
|
|
|
name string
|
|
|
|
parent *dataUsageHash
|
|
|
|
objectHealProbDiv uint32
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
type folderScanner struct {
|
2021-05-19 17:38:30 -04:00
|
|
|
root string
|
|
|
|
getSize getSizeFn
|
|
|
|
oldCache dataUsageCache
|
|
|
|
newCache dataUsageCache
|
|
|
|
updateCache dataUsageCache
|
|
|
|
withFilter *bloomFilter
|
2020-06-12 13:28:21 -04:00
|
|
|
|
2021-02-26 18:11:42 -05:00
|
|
|
dataUsageScannerDebug bool
|
|
|
|
healFolderInclude uint32 // Include a clean folder one in n cycles.
|
|
|
|
healObjectSelect uint32 // Do a heal check on an object once every n cycles. Must divide into healFolderInclude
|
2020-06-12 13:28:21 -04:00
|
|
|
|
2021-05-11 21:36:15 -04:00
|
|
|
disks []StorageAPI
|
2021-05-19 17:38:30 -04:00
|
|
|
|
|
|
|
// If set updates will be sent regularly to this channel.
|
|
|
|
// Will not be closed when returned.
|
|
|
|
updates chan<- dataUsageEntry
|
|
|
|
lastUpdate time.Time
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
|
2021-05-11 21:36:15 -04:00
|
|
|
// Cache structure and compaction:
|
|
|
|
//
|
|
|
|
// A cache structure will be kept with a tree of usages.
|
|
|
|
// The cache is a tree structure where each keeps track of its children.
|
|
|
|
//
|
|
|
|
// An uncompacted branch contains a count of the files only directly at the
|
|
|
|
// branch level, and contains link to children branches or leaves.
|
|
|
|
//
|
|
|
|
// The leaves are "compacted" based on a number of properties.
|
|
|
|
// A compacted leaf contains the totals of all files beneath it.
|
|
|
|
//
|
|
|
|
// A leaf is only scanned once every dataUsageUpdateDirCycles,
|
|
|
|
// rarer if the bloom filter for the path is clean and no lifecycles are applied.
|
|
|
|
// Skipped leaves have their totals transferred from the previous cycle.
|
|
|
|
//
|
|
|
|
// A clean leaf will be included once every healFolderIncludeProb for partial heal scans.
|
|
|
|
// When selected there is a one in healObjectSelectProb that any object will be chosen for heal scan.
|
|
|
|
//
|
|
|
|
// Compaction happens when either:
|
|
|
|
//
|
|
|
|
// 1) The folder (and subfolders) contains less than dataScannerCompactLeastObject objects.
|
|
|
|
// 2) The folder itself contains more than dataScannerCompactAtFolders folders.
|
|
|
|
// 3) The folder only contains objects and no subfolders.
|
|
|
|
//
|
|
|
|
// A bucket root will never be compacted.
|
|
|
|
//
|
|
|
|
// Furthermore if a has more than dataScannerCompactAtChildren recursive children (uncompacted folders)
|
|
|
|
// the tree will be recursively scanned and the branches with the least number of objects will be
|
|
|
|
// compacted until the limit is reached.
|
|
|
|
//
|
|
|
|
// This ensures that any branch will never contain an unreasonable amount of other branches,
|
|
|
|
// and also that small branches with few objects don't take up unreasonable amounts of space.
|
|
|
|
// This keeps the cache size at a reasonable size for all buckets.
|
|
|
|
//
|
|
|
|
// Whenever a branch is scanned, it is assumed that it will be un-compacted
|
|
|
|
// before it hits any of the above limits.
|
|
|
|
// This will make the branch rebalance itself when scanned if the distribution of objects has changed.
|
|
|
|
|
2021-02-26 18:11:42 -05:00
|
|
|
// scanDataFolder will scanner the basepath+cache.Info.Name and return an updated cache.
|
2020-06-12 13:28:21 -04:00
|
|
|
// The returned cache will always be valid, but may not be updated from the existing.
|
2021-02-17 15:04:11 -05:00
|
|
|
// Before each operation sleepDuration is called which can be used to temporarily halt the scanner.
|
2020-06-12 13:28:21 -04:00
|
|
|
// If the supplied context is canceled the function will return at the first chance.
|
2021-02-26 18:11:42 -05:00
|
|
|
func scanDataFolder(ctx context.Context, basePath string, cache dataUsageCache, getSize getSizeFn) (dataUsageCache, error) {
|
2020-06-12 13:28:21 -04:00
|
|
|
t := UTCNow()
|
|
|
|
|
|
|
|
logPrefix := color.Green("data-usage: ")
|
2020-12-29 04:57:28 -05:00
|
|
|
logSuffix := color.Blue("- %v + %v", basePath, cache.Info.Name)
|
2020-06-12 13:28:21 -04:00
|
|
|
if intDataUpdateTracker.debug {
|
|
|
|
defer func() {
|
2021-02-26 18:11:42 -05:00
|
|
|
console.Debugf(logPrefix+" Scanner time: %v %s\n", time.Since(t), logSuffix)
|
2020-06-12 13:28:21 -04:00
|
|
|
}()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
switch cache.Info.Name {
|
|
|
|
case "", dataUsageRoot:
|
|
|
|
return cache, errors.New("internal error: root scan attempted")
|
|
|
|
}
|
|
|
|
|
|
|
|
s := folderScanner{
|
2021-02-26 18:11:42 -05:00
|
|
|
root: basePath,
|
|
|
|
getSize: getSize,
|
|
|
|
oldCache: cache,
|
|
|
|
newCache: dataUsageCache{Info: cache.Info},
|
2021-05-19 17:38:30 -04:00
|
|
|
updateCache: dataUsageCache{Info: cache.Info},
|
2021-02-26 18:11:42 -05:00
|
|
|
dataUsageScannerDebug: intDataUpdateTracker.debug,
|
|
|
|
healFolderInclude: 0,
|
|
|
|
healObjectSelect: 0,
|
2021-05-19 17:38:30 -04:00
|
|
|
updates: cache.Info.updates,
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
|
2020-12-01 15:07:39 -05:00
|
|
|
// Add disks for set healing.
|
|
|
|
if len(cache.Disks) > 0 {
|
2020-12-01 16:50:33 -05:00
|
|
|
objAPI, ok := newObjectLayerFn().(*erasureServerPools)
|
2020-12-01 15:07:39 -05:00
|
|
|
if ok {
|
|
|
|
s.disks = objAPI.GetDisksID(cache.Disks...)
|
|
|
|
if len(s.disks) != len(cache.Disks) {
|
2020-12-29 04:57:28 -05:00
|
|
|
console.Debugf(logPrefix+"Missing disks, want %d, found %d. Cannot heal. %s\n", len(cache.Disks), len(s.disks), logSuffix)
|
2020-12-01 15:07:39 -05:00
|
|
|
s.disks = s.disks[:0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 16:47:01 -04:00
|
|
|
// Enable healing in XL mode.
|
2021-05-11 21:36:15 -04:00
|
|
|
if globalIsErasure && !cache.Info.SkipHealing {
|
2020-08-24 16:47:01 -04:00
|
|
|
// Include a clean folder one in n cycles.
|
2020-09-12 03:08:12 -04:00
|
|
|
s.healFolderInclude = healFolderIncludeProb
|
2020-08-24 16:47:01 -04:00
|
|
|
// Do a heal check on an object once every n cycles. Must divide into healFolderInclude
|
2020-09-12 03:08:12 -04:00
|
|
|
s.healObjectSelect = healObjectSelectProb
|
2020-08-24 16:47:01 -04:00
|
|
|
}
|
2020-06-12 13:28:21 -04:00
|
|
|
if len(cache.Info.BloomFilter) > 0 {
|
|
|
|
s.withFilter = &bloomFilter{BloomFilter: &bloom.BloomFilter{}}
|
2020-12-27 01:58:06 -05:00
|
|
|
_, err := s.withFilter.ReadFrom(bytes.NewReader(cache.Info.BloomFilter))
|
2020-06-12 13:28:21 -04:00
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err, logPrefix+"Error reading bloom filter")
|
|
|
|
s.withFilter = nil
|
|
|
|
}
|
|
|
|
}
|
2021-02-26 18:11:42 -05:00
|
|
|
if s.dataUsageScannerDebug {
|
|
|
|
console.Debugf(logPrefix+"Start scanning. Bloom filter: %v %s\n", s.withFilter != nil, logSuffix)
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
done := ctx.Done()
|
2021-02-26 18:11:42 -05:00
|
|
|
if s.dataUsageScannerDebug {
|
2020-12-29 04:57:28 -05:00
|
|
|
console.Debugf(logPrefix+"Cycle: %v, Entries: %v %s\n", cache.Info.NextCycle, len(cache.Cache), logSuffix)
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
|
2021-05-11 21:36:15 -04:00
|
|
|
// Read top level in bucket.
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
return cache, ctx.Err()
|
|
|
|
default:
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
2021-05-11 21:36:15 -04:00
|
|
|
root := dataUsageEntry{}
|
|
|
|
folder := cachedFolder{name: cache.Info.Name, objectHealProbDiv: 1}
|
|
|
|
err := s.scanFolder(ctx, folder, &root)
|
|
|
|
if err != nil {
|
|
|
|
// No useful information...
|
|
|
|
return cache, err
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
|
2021-02-26 18:11:42 -05:00
|
|
|
if s.dataUsageScannerDebug {
|
2021-05-11 21:36:15 -04:00
|
|
|
console.Debugf(logPrefix+"Finished scanner, %v entries (%+v) %s \n", len(s.newCache.Cache), *s.newCache.sizeRecursive(s.newCache.Info.Name), logSuffix)
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
s.newCache.Info.LastUpdate = UTCNow()
|
|
|
|
s.newCache.Info.NextCycle++
|
|
|
|
return s.newCache, nil
|
|
|
|
}
|
|
|
|
|
2021-05-19 17:38:30 -04:00
|
|
|
// sendUpdate() should be called on a regular basis when the newCache contains more recent total than previously.
|
|
|
|
// May or may not send an update upstream.
|
|
|
|
func (f *folderScanner) sendUpdate() {
|
|
|
|
// Send at most an update every minute.
|
|
|
|
if f.updates == nil || time.Since(f.lastUpdate) < time.Minute {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if flat := f.updateCache.sizeRecursive(f.newCache.Info.Name); flat != nil {
|
|
|
|
select {
|
|
|
|
case f.updates <- *flat:
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
f.lastUpdate = time.Now()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-11 21:36:15 -04:00
|
|
|
// scanFolder will scan the provided folder.
|
2020-06-12 13:28:21 -04:00
|
|
|
// Files found in the folders will be added to f.newCache.
|
|
|
|
// If final is provided folders will be put into f.newFolders or f.existingFolders.
|
|
|
|
// If final is not provided the folders found are returned from the function.
|
2021-05-11 21:36:15 -04:00
|
|
|
func (f *folderScanner) scanFolder(ctx context.Context, folder cachedFolder, into *dataUsageEntry) error {
|
2020-06-12 13:28:21 -04:00
|
|
|
done := ctx.Done()
|
2020-12-29 04:57:28 -05:00
|
|
|
scannerLogPrefix := color.Green("folder-scanner:")
|
2021-05-11 21:36:15 -04:00
|
|
|
thisHash := hashPath(folder.name)
|
|
|
|
// Store initial compaction state.
|
|
|
|
wasCompacted := into.Compacted
|
2021-05-19 17:38:30 -04:00
|
|
|
|
2021-05-11 21:36:15 -04:00
|
|
|
for {
|
2020-06-12 13:28:21 -04:00
|
|
|
select {
|
|
|
|
case <-done:
|
2021-05-11 21:36:15 -04:00
|
|
|
return ctx.Err()
|
2020-06-12 13:28:21 -04:00
|
|
|
default:
|
|
|
|
}
|
2021-05-11 21:36:15 -04:00
|
|
|
existing, ok := f.oldCache.Cache[thisHash.Key()]
|
|
|
|
var abandonedChildren dataUsageHashMap
|
|
|
|
if !into.Compacted {
|
|
|
|
abandonedChildren = f.oldCache.findChildrenCopy(thisHash)
|
|
|
|
}
|
2020-06-12 13:28:21 -04:00
|
|
|
|
|
|
|
// If there are lifecycle rules for the prefix, remove the filter.
|
|
|
|
filter := f.withFilter
|
2020-12-13 15:05:54 -05:00
|
|
|
_, prefix := path2BucketObjectWithBasePath(f.root, folder.name)
|
2020-06-12 13:28:21 -04:00
|
|
|
var activeLifeCycle *lifecycle.Lifecycle
|
2020-12-13 15:05:54 -05:00
|
|
|
if f.oldCache.Info.lifeCycle != nil && f.oldCache.Info.lifeCycle.HasActiveRules(prefix, true) {
|
2021-02-26 18:11:42 -05:00
|
|
|
if f.dataUsageScannerDebug {
|
2020-12-29 04:57:28 -05:00
|
|
|
console.Debugf(scannerLogPrefix+" Prefix %q has active rules\n", prefix)
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
2020-12-13 15:05:54 -05:00
|
|
|
activeLifeCycle = f.oldCache.Info.lifeCycle
|
|
|
|
filter = nil
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
2021-06-01 22:59:11 -04:00
|
|
|
// If there are replication rules for the prefix, remove the filter.
|
|
|
|
var replicationCfg replicationConfig
|
|
|
|
if !f.oldCache.Info.replication.Empty() && f.oldCache.Info.replication.Config.HasActiveRules(prefix, true) {
|
|
|
|
replicationCfg = f.oldCache.Info.replication
|
|
|
|
filter = nil
|
|
|
|
}
|
2021-05-11 21:36:15 -04:00
|
|
|
// Check if we can skip it due to bloom filter...
|
|
|
|
if filter != nil && ok && existing.Compacted {
|
2020-06-12 13:28:21 -04:00
|
|
|
// If folder isn't in filter and we have data, skip it completely.
|
|
|
|
if folder.name != dataUsageRoot && !filter.containsDir(folder.name) {
|
2021-05-11 21:36:15 -04:00
|
|
|
if f.healObjectSelect == 0 || !thisHash.mod(f.oldCache.Info.NextCycle, f.healFolderInclude/folder.objectHealProbDiv) {
|
2020-08-24 16:47:01 -04:00
|
|
|
f.newCache.copyWithChildren(&f.oldCache, thisHash, folder.parent)
|
2021-05-19 17:38:30 -04:00
|
|
|
f.updateCache.copyWithChildren(&f.oldCache, thisHash, folder.parent)
|
2021-02-26 18:11:42 -05:00
|
|
|
if f.dataUsageScannerDebug {
|
2020-12-29 04:57:28 -05:00
|
|
|
console.Debugf(scannerLogPrefix+" Skipping non-updated folder: %v\n", folder.name)
|
2020-08-24 16:47:01 -04:00
|
|
|
}
|
2021-05-11 21:36:15 -04:00
|
|
|
return nil
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
2021-05-11 21:36:15 -04:00
|
|
|
if f.dataUsageScannerDebug {
|
|
|
|
console.Debugf(scannerLogPrefix+" Adding non-updated folder to heal check: %v\n", folder.name)
|
|
|
|
}
|
|
|
|
// If probability was already scannerHealFolderInclude, keep it.
|
|
|
|
folder.objectHealProbDiv = f.healFolderInclude
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
}
|
2021-02-26 18:11:42 -05:00
|
|
|
scannerSleeper.Sleep(ctx, dataScannerSleepPerFolder)
|
2020-06-12 13:28:21 -04:00
|
|
|
|
2021-05-11 21:36:15 -04:00
|
|
|
var existingFolders, newFolders []cachedFolder
|
|
|
|
var foundObjects bool
|
2020-06-12 13:28:21 -04:00
|
|
|
err := readDirFn(path.Join(f.root, folder.name), func(entName string, typ os.FileMode) error {
|
|
|
|
// Parse
|
2021-04-15 19:32:13 -04:00
|
|
|
entName = pathClean(path.Join(folder.name, entName))
|
|
|
|
if entName == "" {
|
|
|
|
if f.dataUsageScannerDebug {
|
|
|
|
console.Debugf(scannerLogPrefix+" no bucket (%s,%s)\n", f.root, entName)
|
|
|
|
}
|
|
|
|
return errDoneForNow
|
|
|
|
}
|
2020-06-12 13:28:21 -04:00
|
|
|
bucket, prefix := path2BucketObjectWithBasePath(f.root, entName)
|
|
|
|
if bucket == "" {
|
2021-02-26 18:11:42 -05:00
|
|
|
if f.dataUsageScannerDebug {
|
2020-12-29 04:57:28 -05:00
|
|
|
console.Debugf(scannerLogPrefix+" no bucket (%s,%s)\n", f.root, entName)
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
2021-02-17 18:34:42 -05:00
|
|
|
return errDoneForNow
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if isReservedOrInvalidBucket(bucket, false) {
|
2021-02-26 18:11:42 -05:00
|
|
|
if f.dataUsageScannerDebug {
|
2020-12-29 04:57:28 -05:00
|
|
|
console.Debugf(scannerLogPrefix+" invalid bucket: %v, entry: %v\n", bucket, entName)
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
2021-02-17 18:34:42 -05:00
|
|
|
return errDoneForNow
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-done:
|
2021-02-17 18:34:42 -05:00
|
|
|
return errDoneForNow
|
2020-06-12 13:28:21 -04:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
if typ&os.ModeDir != 0 {
|
|
|
|
h := hashPath(entName)
|
|
|
|
_, exists := f.oldCache.Cache[h.Key()]
|
|
|
|
|
2020-08-24 16:47:01 -04:00
|
|
|
this := cachedFolder{name: entName, parent: &thisHash, objectHealProbDiv: folder.objectHealProbDiv}
|
2021-05-11 21:36:15 -04:00
|
|
|
delete(abandonedChildren, h.Key()) // h.Key() already accounted for.
|
|
|
|
if exists {
|
|
|
|
existingFolders = append(existingFolders, this)
|
2021-05-19 17:38:30 -04:00
|
|
|
f.updateCache.copyWithChildren(&f.oldCache, h, &thisHash)
|
2020-06-12 13:28:21 -04:00
|
|
|
} else {
|
2021-05-11 21:36:15 -04:00
|
|
|
newFolders = append(newFolders, this)
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2020-10-22 16:36:24 -04:00
|
|
|
|
2020-06-12 13:28:21 -04:00
|
|
|
// Dynamic time delay.
|
2021-02-17 15:04:11 -05:00
|
|
|
wait := scannerSleeper.Timer(ctx)
|
2020-06-12 13:28:21 -04:00
|
|
|
|
|
|
|
// Get file size, ignore errors.
|
2021-02-26 18:11:42 -05:00
|
|
|
item := scannerItem{
|
2021-06-01 22:59:11 -04:00
|
|
|
Path: path.Join(f.root, entName),
|
|
|
|
Typ: typ,
|
|
|
|
bucket: bucket,
|
|
|
|
prefix: path.Dir(prefix),
|
|
|
|
objectName: path.Base(entName),
|
|
|
|
debug: f.dataUsageScannerDebug,
|
|
|
|
lifeCycle: activeLifeCycle,
|
|
|
|
replication: replicationCfg,
|
|
|
|
heal: thisHash.mod(f.oldCache.Info.NextCycle, f.healObjectSelect/folder.objectHealProbDiv) && globalIsErasure,
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
2021-01-19 05:40:52 -05:00
|
|
|
// if the drive belongs to an erasure set
|
|
|
|
// that is already being healed, skip the
|
|
|
|
// healing attempt on this drive.
|
2021-05-11 21:36:15 -04:00
|
|
|
item.heal = item.heal && f.healObjectSelect > 0
|
2021-01-19 05:40:52 -05:00
|
|
|
|
2021-05-11 21:36:15 -04:00
|
|
|
sz, err := f.getSize(item)
|
2020-08-24 16:47:01 -04:00
|
|
|
if err == errSkipFile {
|
2020-12-28 13:31:00 -05:00
|
|
|
wait() // wait to proceed to next entry.
|
|
|
|
|
2020-06-12 13:28:21 -04:00
|
|
|
return nil
|
|
|
|
}
|
2020-12-28 13:31:00 -05:00
|
|
|
// successfully read means we have a valid object.
|
2021-05-11 21:36:15 -04:00
|
|
|
foundObjects = true
|
2020-12-28 13:31:00 -05:00
|
|
|
// Remove filename i.e is the meta file to construct object name
|
|
|
|
item.transformMetaDir()
|
|
|
|
|
|
|
|
// Object already accounted for, remove from heal map,
|
|
|
|
// simply because getSize() function already heals the
|
|
|
|
// object.
|
2021-05-11 21:36:15 -04:00
|
|
|
delete(abandonedChildren, path.Join(item.bucket, item.objectPath()))
|
2020-12-28 13:31:00 -05:00
|
|
|
|
2021-05-11 21:36:15 -04:00
|
|
|
into.addSizes(sz)
|
|
|
|
into.Objects++
|
2020-06-12 13:28:21 -04:00
|
|
|
|
2020-12-28 13:31:00 -05:00
|
|
|
wait() // wait to proceed to next entry.
|
|
|
|
|
2020-06-12 13:28:21 -04:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
2021-05-11 21:36:15 -04:00
|
|
|
return err
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
2020-08-24 16:47:01 -04:00
|
|
|
|
2021-05-11 21:36:15 -04:00
|
|
|
if foundObjects && globalIsErasure {
|
|
|
|
// If we found an object in erasure mode, we skip subdirs (only datadirs)...
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we have many subfolders, compact ourself.
|
|
|
|
if !into.Compacted &&
|
|
|
|
f.newCache.Info.Name != folder.name &&
|
|
|
|
len(existingFolders)+len(newFolders) >= dataScannerCompactAtFolders {
|
|
|
|
into.Compacted = true
|
|
|
|
newFolders = append(newFolders, existingFolders...)
|
|
|
|
existingFolders = nil
|
|
|
|
if f.dataUsageScannerDebug {
|
|
|
|
console.Debugf(scannerLogPrefix+" Preemptively compacting: %v, entries: %v\n", folder.name, len(existingFolders)+len(newFolders))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
scanFolder := func(folder cachedFolder) {
|
|
|
|
if contextCanceled(ctx) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
dst := into
|
|
|
|
if !into.Compacted {
|
|
|
|
dst = &dataUsageEntry{Compacted: false}
|
|
|
|
}
|
|
|
|
if err := f.scanFolder(ctx, folder, dst); err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !into.Compacted {
|
|
|
|
into.addChild(dataUsageHash(folder.name))
|
|
|
|
}
|
2021-05-19 17:38:30 -04:00
|
|
|
// We scanned a folder, optionally send update.
|
|
|
|
f.sendUpdate()
|
2021-05-11 21:36:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scan new...
|
|
|
|
for _, folder := range newFolders {
|
2021-05-19 17:38:30 -04:00
|
|
|
h := hashPath(folder.name)
|
|
|
|
// Add new folders to the update tree so totals update for these.
|
|
|
|
if !into.Compacted {
|
|
|
|
var foundAny bool
|
|
|
|
parent := thisHash
|
|
|
|
for parent != hashPath(f.updateCache.Info.Name) {
|
|
|
|
e := f.updateCache.find(parent.Key())
|
|
|
|
if e == nil || e.Compacted {
|
|
|
|
foundAny = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if next := f.updateCache.searchParent(parent); next == nil {
|
|
|
|
foundAny = true
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
parent = *next
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !foundAny {
|
|
|
|
// Add non-compacted empty entry.
|
|
|
|
f.updateCache.replaceHashed(h, &thisHash, dataUsageEntry{})
|
|
|
|
}
|
|
|
|
}
|
2021-05-11 21:36:15 -04:00
|
|
|
scanFolder(folder)
|
2021-05-19 17:38:30 -04:00
|
|
|
// Add new folders if this is new and we don't have existing.
|
|
|
|
if !into.Compacted {
|
|
|
|
parent := f.updateCache.find(thisHash.Key())
|
|
|
|
if parent != nil && !parent.Compacted {
|
|
|
|
f.updateCache.deleteRecursive(h)
|
|
|
|
f.updateCache.copyWithChildren(&f.newCache, h, &thisHash)
|
|
|
|
}
|
|
|
|
}
|
2021-05-11 21:36:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Scan existing...
|
|
|
|
for _, folder := range existingFolders {
|
|
|
|
h := hashPath(folder.name)
|
|
|
|
// Check if we should skip scanning folder...
|
|
|
|
// We can only skip if we are not indexing into a compacted destination
|
|
|
|
// and the entry itself is compacted.
|
|
|
|
if !into.Compacted && f.oldCache.isCompacted(h) {
|
|
|
|
if !h.mod(f.oldCache.Info.NextCycle, dataUsageUpdateDirCycles) {
|
2021-05-19 17:38:30 -04:00
|
|
|
if f.healObjectSelect == 0 || !h.mod(f.oldCache.Info.NextCycle, f.healFolderInclude/folder.objectHealProbDiv) {
|
2021-05-11 21:36:15 -04:00
|
|
|
// Transfer and add as child...
|
|
|
|
f.newCache.copyWithChildren(&f.oldCache, h, folder.parent)
|
|
|
|
into.addChild(h)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
folder.objectHealProbDiv = dataUsageUpdateDirCycles
|
|
|
|
}
|
|
|
|
}
|
|
|
|
scanFolder(folder)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan for healing
|
|
|
|
if f.healObjectSelect == 0 || len(abandonedChildren) == 0 {
|
|
|
|
// If we are not heal scanning, return now.
|
|
|
|
break
|
2020-08-24 16:47:01 -04:00
|
|
|
}
|
|
|
|
|
2020-12-01 16:50:33 -05:00
|
|
|
objAPI, ok := newObjectLayerFn().(*erasureServerPools)
|
2020-12-01 15:07:39 -05:00
|
|
|
if !ok || len(f.disks) == 0 {
|
2021-05-11 21:36:15 -04:00
|
|
|
break
|
2020-08-24 16:47:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bgSeq, found := globalBackgroundHealState.getHealSequenceByToken(bgHealingUUID)
|
|
|
|
if !found {
|
2021-05-11 21:36:15 -04:00
|
|
|
break
|
2020-08-24 16:47:01 -04:00
|
|
|
}
|
|
|
|
|
2021-05-11 21:36:15 -04:00
|
|
|
// Whatever remains in 'abandonedChildren' are folders at this level
|
2020-08-24 16:47:01 -04:00
|
|
|
// that existed in the previous run but wasn't found now.
|
|
|
|
//
|
|
|
|
// This may be because of 2 reasons:
|
|
|
|
//
|
|
|
|
// 1) The folder/object was deleted.
|
|
|
|
// 2) We come from another disk and this disk missed the write.
|
|
|
|
//
|
|
|
|
// We therefore perform a heal check.
|
|
|
|
// If that doesn't bring it back we remove the folder and assume it was deleted.
|
|
|
|
// This means that the next run will not look for it.
|
2020-12-01 15:07:39 -05:00
|
|
|
// How to resolve results.
|
|
|
|
resolver := metadataResolutionParams{
|
|
|
|
dirQuorum: getReadQuorum(len(f.disks)),
|
|
|
|
objQuorum: getReadQuorum(len(f.disks)),
|
|
|
|
bucket: "",
|
|
|
|
}
|
|
|
|
|
2020-12-29 04:57:28 -05:00
|
|
|
healObjectsPrefix := color.Green("healObjects:")
|
2021-05-11 21:36:15 -04:00
|
|
|
for k := range abandonedChildren {
|
2020-08-24 16:47:01 -04:00
|
|
|
bucket, prefix := path2BucketObject(k)
|
2021-02-26 18:11:42 -05:00
|
|
|
if f.dataUsageScannerDebug {
|
2020-12-29 04:57:28 -05:00
|
|
|
console.Debugf(scannerLogPrefix+" checking disappeared folder: %v/%v\n", bucket, prefix)
|
2020-08-24 16:47:01 -04:00
|
|
|
}
|
|
|
|
|
2020-12-01 15:07:39 -05:00
|
|
|
resolver.bucket = bucket
|
|
|
|
|
|
|
|
foundObjs := false
|
2020-12-28 13:31:00 -05:00
|
|
|
dangling := false
|
2020-12-01 15:07:39 -05:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2021-03-06 12:25:48 -05:00
|
|
|
|
2020-12-01 15:07:39 -05:00
|
|
|
err := listPathRaw(ctx, listPathRawOptions{
|
|
|
|
disks: f.disks,
|
|
|
|
bucket: bucket,
|
|
|
|
path: prefix,
|
|
|
|
recursive: true,
|
|
|
|
reportNotFound: true,
|
|
|
|
minDisks: len(f.disks), // We want full consistency.
|
|
|
|
// Weird, maybe transient error.
|
|
|
|
agreed: func(entry metaCacheEntry) {
|
2021-02-26 18:11:42 -05:00
|
|
|
if f.dataUsageScannerDebug {
|
2020-12-29 04:57:28 -05:00
|
|
|
console.Debugf(healObjectsPrefix+" got agreement: %v\n", entry.name)
|
2020-12-01 15:07:39 -05:00
|
|
|
}
|
|
|
|
},
|
|
|
|
// Some disks have data for this.
|
|
|
|
partial: func(entries metaCacheEntries, nAgreed int, errs []error) {
|
2021-02-26 18:11:42 -05:00
|
|
|
if f.dataUsageScannerDebug {
|
2020-12-29 04:57:28 -05:00
|
|
|
console.Debugf(healObjectsPrefix+" got partial, %d agreed, errs: %v\n", nAgreed, errs)
|
2020-12-01 15:07:39 -05:00
|
|
|
}
|
2020-12-28 13:31:00 -05:00
|
|
|
|
|
|
|
// agreed value less than expected quorum
|
|
|
|
dangling = nAgreed < resolver.objQuorum || nAgreed < resolver.dirQuorum
|
|
|
|
|
2020-12-01 15:07:39 -05:00
|
|
|
entry, ok := entries.resolve(&resolver)
|
|
|
|
if !ok {
|
|
|
|
for _, err := range errs {
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no errors, queue it for healing.
|
|
|
|
entry, _ = entries.firstFound()
|
|
|
|
}
|
2020-10-22 16:36:24 -04:00
|
|
|
|
2021-02-26 18:11:42 -05:00
|
|
|
if f.dataUsageScannerDebug {
|
2020-12-29 04:57:28 -05:00
|
|
|
console.Debugf(healObjectsPrefix+" resolved to: %v, dir: %v\n", entry.name, entry.isDir())
|
2020-12-01 15:07:39 -05:00
|
|
|
}
|
2020-12-28 13:31:00 -05:00
|
|
|
|
2020-12-01 15:07:39 -05:00
|
|
|
if entry.isDir() {
|
|
|
|
return
|
|
|
|
}
|
2021-04-27 11:24:44 -04:00
|
|
|
|
|
|
|
// wait on timer per object.
|
|
|
|
wait := scannerSleeper.Timer(ctx)
|
|
|
|
|
2020-12-01 15:07:39 -05:00
|
|
|
// We got an entry which we should be able to heal.
|
|
|
|
fiv, err := entry.fileInfoVersions(bucket)
|
|
|
|
if err != nil {
|
2021-04-27 11:24:44 -04:00
|
|
|
wait()
|
2020-12-01 15:07:39 -05:00
|
|
|
err := bgSeq.queueHealTask(healSource{
|
|
|
|
bucket: bucket,
|
|
|
|
object: entry.name,
|
|
|
|
versionID: "",
|
|
|
|
}, madmin.HealItemObject)
|
2020-12-24 18:02:02 -05:00
|
|
|
if !isErrObjectNotFound(err) && !isErrVersionNotFound(err) {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
2020-12-01 15:07:39 -05:00
|
|
|
foundObjs = foundObjs || err == nil
|
|
|
|
return
|
|
|
|
}
|
2021-04-27 11:24:44 -04:00
|
|
|
|
2020-12-01 15:07:39 -05:00
|
|
|
for _, ver := range fiv.Versions {
|
|
|
|
// Sleep and reset.
|
2020-12-04 12:32:35 -05:00
|
|
|
wait()
|
2021-02-17 15:04:11 -05:00
|
|
|
wait = scannerSleeper.Timer(ctx)
|
2021-04-27 11:24:44 -04:00
|
|
|
|
2020-12-01 15:07:39 -05:00
|
|
|
err := bgSeq.queueHealTask(healSource{
|
|
|
|
bucket: bucket,
|
|
|
|
object: fiv.Name,
|
|
|
|
versionID: ver.VersionID,
|
|
|
|
}, madmin.HealItemObject)
|
2020-12-24 18:02:02 -05:00
|
|
|
if !isErrObjectNotFound(err) && !isErrVersionNotFound(err) {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
2020-12-01 15:07:39 -05:00
|
|
|
foundObjs = foundObjs || err == nil
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// Too many disks failed.
|
|
|
|
finished: func(errs []error) {
|
2021-02-26 18:11:42 -05:00
|
|
|
if f.dataUsageScannerDebug {
|
2020-12-29 04:57:28 -05:00
|
|
|
console.Debugf(healObjectsPrefix+" too many errors: %v\n", errs)
|
2020-12-01 15:07:39 -05:00
|
|
|
}
|
|
|
|
cancel()
|
|
|
|
},
|
|
|
|
})
|
2020-10-22 16:36:24 -04:00
|
|
|
|
2021-02-26 18:11:42 -05:00
|
|
|
if f.dataUsageScannerDebug && err != nil && err != errFileNotFound {
|
2020-12-29 04:57:28 -05:00
|
|
|
console.Debugf(healObjectsPrefix+" checking returned value %v (%T)\n", err, err)
|
2020-12-01 15:07:39 -05:00
|
|
|
}
|
2020-08-24 16:47:01 -04:00
|
|
|
|
2020-12-01 15:07:39 -05:00
|
|
|
// If we found one or more disks with this folder, delete it.
|
|
|
|
if err == nil && dangling {
|
2021-02-26 18:11:42 -05:00
|
|
|
if f.dataUsageScannerDebug {
|
2020-12-29 04:57:28 -05:00
|
|
|
console.Debugf(healObjectsPrefix+" deleting dangling directory %s\n", prefix)
|
2020-12-01 15:07:39 -05:00
|
|
|
}
|
2020-12-27 18:42:20 -05:00
|
|
|
|
2021-04-27 11:24:44 -04:00
|
|
|
// wait on timer per object.
|
|
|
|
wait := scannerSleeper.Timer(ctx)
|
|
|
|
|
2020-12-28 13:31:00 -05:00
|
|
|
objAPI.HealObjects(ctx, bucket, prefix, madmin.HealOpts{
|
|
|
|
Recursive: true,
|
2021-02-18 18:16:20 -05:00
|
|
|
Remove: healDeleteDangling,
|
2021-05-11 21:36:15 -04:00
|
|
|
}, func(bucket, object, versionID string) error {
|
|
|
|
// Wait for each heal as per scanner frequency.
|
|
|
|
wait()
|
|
|
|
wait = scannerSleeper.Timer(ctx)
|
|
|
|
return bgSeq.queueHealTask(healSource{
|
|
|
|
bucket: bucket,
|
|
|
|
object: object,
|
|
|
|
versionID: versionID,
|
|
|
|
}, madmin.HealItemObject)
|
|
|
|
})
|
2020-08-24 16:47:01 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add unless healing returned an error.
|
2020-12-01 15:07:39 -05:00
|
|
|
if foundObjs {
|
2021-05-11 21:36:15 -04:00
|
|
|
this := cachedFolder{name: k, parent: &thisHash, objectHealProbDiv: 1}
|
|
|
|
scanFolder(this)
|
2020-08-24 16:47:01 -04:00
|
|
|
}
|
|
|
|
}
|
2021-05-11 21:36:15 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
if !wasCompacted {
|
|
|
|
f.newCache.replaceHashed(thisHash, folder.parent, *into)
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
|
2021-05-11 21:36:15 -04:00
|
|
|
if !into.Compacted && f.newCache.Info.Name != folder.name {
|
|
|
|
flat := f.newCache.sizeRecursive(thisHash.Key())
|
|
|
|
flat.Compacted = true
|
|
|
|
var compact bool
|
|
|
|
if flat.Objects < dataScannerCompactLeastObject {
|
|
|
|
if f.dataUsageScannerDebug && flat.Objects > 1 {
|
|
|
|
// Disabled, rather chatty:
|
|
|
|
//console.Debugf(scannerLogPrefix+" Only %d objects, compacting %s -> %+v\n", flat.Objects, folder.name, flat)
|
|
|
|
}
|
|
|
|
compact = true
|
|
|
|
} else {
|
|
|
|
// Compact if we only have objects as children...
|
|
|
|
compact = true
|
|
|
|
for k := range into.Children {
|
|
|
|
if v, ok := f.newCache.Cache[k]; ok {
|
|
|
|
if len(v.Children) > 0 || v.Objects > 1 {
|
|
|
|
compact = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if f.dataUsageScannerDebug && compact {
|
|
|
|
// Disabled, rather chatty:
|
|
|
|
//console.Debugf(scannerLogPrefix+" Only objects (%d), compacting %s -> %+v\n", flat.Objects, folder.name, flat)
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
2021-01-19 05:40:52 -05:00
|
|
|
}
|
2021-05-11 21:36:15 -04:00
|
|
|
if compact {
|
|
|
|
f.newCache.deleteRecursive(thisHash)
|
|
|
|
f.newCache.replaceHashed(thisHash, folder.parent, *flat)
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
2020-12-29 04:57:28 -05:00
|
|
|
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
2021-05-11 21:36:15 -04:00
|
|
|
// Compact if too many children...
|
|
|
|
if !into.Compacted {
|
|
|
|
f.newCache.reduceChildrenOf(thisHash, dataScannerCompactAtChildren, f.newCache.Info.Name != folder.name)
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
2021-05-19 17:38:30 -04:00
|
|
|
if _, ok := f.updateCache.Cache[thisHash.Key()]; !wasCompacted && ok {
|
|
|
|
// Replace if existed before.
|
|
|
|
if flat := f.newCache.sizeRecursive(thisHash.Key()); flat != nil {
|
|
|
|
f.updateCache.deleteRecursive(thisHash)
|
|
|
|
f.updateCache.replaceHashed(thisHash, folder.parent, *flat)
|
|
|
|
}
|
|
|
|
}
|
2021-05-11 21:36:15 -04:00
|
|
|
|
|
|
|
return nil
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
|
2021-02-26 18:11:42 -05:00
|
|
|
// scannerItem represents each file while walking.
|
|
|
|
type scannerItem struct {
|
2020-06-12 13:28:21 -04:00
|
|
|
Path string
|
|
|
|
Typ os.FileMode
|
|
|
|
|
2021-06-01 22:59:11 -04:00
|
|
|
bucket string // Bucket.
|
|
|
|
prefix string // Only the prefix if any, does not have final object name.
|
|
|
|
objectName string // Only the object name without prefixes.
|
|
|
|
lifeCycle *lifecycle.Lifecycle
|
|
|
|
replication replicationConfig
|
|
|
|
heal bool // Has the object been selected for heal check?
|
|
|
|
debug bool
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
|
2020-12-07 16:47:48 -05:00
|
|
|
type sizeSummary struct {
|
|
|
|
totalSize int64
|
2021-05-11 21:36:15 -04:00
|
|
|
versions uint64
|
2020-12-07 16:47:48 -05:00
|
|
|
replicatedSize int64
|
|
|
|
pendingSize int64
|
|
|
|
failedSize int64
|
|
|
|
replicaSize int64
|
2021-04-03 12:03:42 -04:00
|
|
|
pendingCount uint64
|
|
|
|
failedCount uint64
|
2020-12-07 16:47:48 -05:00
|
|
|
}
|
|
|
|
|
2021-02-26 18:11:42 -05:00
|
|
|
type getSizeFn func(item scannerItem) (sizeSummary, error)
|
2020-06-12 13:28:21 -04:00
|
|
|
|
|
|
|
// transformMetaDir will transform a directory to prefix/file.ext
|
2021-02-26 18:11:42 -05:00
|
|
|
func (i *scannerItem) transformMetaDir() {
|
2020-06-12 13:28:21 -04:00
|
|
|
split := strings.Split(i.prefix, SlashSeparator)
|
|
|
|
if len(split) > 1 {
|
|
|
|
i.prefix = path.Join(split[:len(split)-1]...)
|
|
|
|
} else {
|
|
|
|
i.prefix = ""
|
|
|
|
}
|
|
|
|
// Object name is last element
|
|
|
|
i.objectName = split[len(split)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// actionMeta contains information used to apply actions.
|
|
|
|
type actionMeta struct {
|
2021-02-01 12:52:11 -05:00
|
|
|
oi ObjectInfo
|
|
|
|
bitRotScan bool // indicates if bitrot check was requested.
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
|
2021-02-01 12:52:11 -05:00
|
|
|
var applyActionsLogPrefix = color.Green("applyActions:")
|
|
|
|
|
2021-03-31 05:15:08 -04:00
|
|
|
func (i *scannerItem) applyHealing(ctx context.Context, o ObjectLayer, meta actionMeta) (size int64) {
|
2020-06-12 13:28:21 -04:00
|
|
|
if i.debug {
|
2021-03-31 05:15:08 -04:00
|
|
|
if meta.oi.VersionID != "" {
|
|
|
|
console.Debugf(applyActionsLogPrefix+" heal checking: %v/%v v(%s)\n", i.bucket, i.objectPath(), meta.oi.VersionID)
|
|
|
|
} else {
|
|
|
|
console.Debugf(applyActionsLogPrefix+" heal checking: %v/%v\n", i.bucket, i.objectPath())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
healOpts := madmin.HealOpts{Remove: healDeleteDangling}
|
|
|
|
if meta.bitRotScan {
|
|
|
|
healOpts.ScanMode = madmin.HealDeepScan
|
|
|
|
}
|
|
|
|
res, err := o.HealObject(ctx, i.bucket, i.objectPath(), meta.oi.VersionID, healOpts)
|
|
|
|
if isErrObjectNotFound(err) || isErrVersionNotFound(err) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if err != nil && !errors.Is(err, NotImplemented{}) {
|
2020-06-12 13:28:21 -04:00
|
|
|
logger.LogIf(ctx, err)
|
2021-03-31 05:15:08 -04:00
|
|
|
return 0
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
2021-03-31 05:15:08 -04:00
|
|
|
return res.ObjectSize
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *scannerItem) applyLifecycle(ctx context.Context, o ObjectLayer, meta actionMeta) (applied bool, size int64) {
|
|
|
|
size, err := meta.oi.GetActualSize()
|
|
|
|
if i.debug {
|
|
|
|
logger.LogIf(ctx, err)
|
2020-08-24 16:47:01 -04:00
|
|
|
}
|
2020-06-12 13:28:21 -04:00
|
|
|
if i.lifeCycle == nil {
|
2020-12-13 15:05:54 -05:00
|
|
|
if i.debug {
|
2021-05-11 21:36:15 -04:00
|
|
|
// disabled, very chatty:
|
|
|
|
// console.Debugf(applyActionsLogPrefix+" no lifecycle rules to apply: %q\n", i.objectPath())
|
2020-12-13 15:05:54 -05:00
|
|
|
}
|
2021-03-31 05:15:08 -04:00
|
|
|
return false, size
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
|
2020-06-12 23:04:01 -04:00
|
|
|
versionID := meta.oi.VersionID
|
|
|
|
action := i.lifeCycle.ComputeAction(
|
|
|
|
lifecycle.ObjectOpts{
|
2021-04-19 13:30:42 -04:00
|
|
|
Name: i.objectPath(),
|
|
|
|
UserTags: meta.oi.UserTags,
|
|
|
|
ModTime: meta.oi.ModTime,
|
|
|
|
VersionID: meta.oi.VersionID,
|
|
|
|
DeleteMarker: meta.oi.DeleteMarker,
|
|
|
|
IsLatest: meta.oi.IsLatest,
|
|
|
|
NumVersions: meta.oi.NumVersions,
|
|
|
|
SuccessorModTime: meta.oi.SuccessorModTime,
|
|
|
|
RestoreOngoing: meta.oi.RestoreOngoing,
|
|
|
|
RestoreExpires: meta.oi.RestoreExpires,
|
|
|
|
TransitionStatus: meta.oi.TransitionStatus,
|
|
|
|
RemoteTiersImmediately: globalDebugRemoteTiersImmediately,
|
2020-06-12 23:04:01 -04:00
|
|
|
})
|
2020-06-12 13:28:21 -04:00
|
|
|
if i.debug {
|
2020-12-13 15:05:54 -05:00
|
|
|
if versionID != "" {
|
2020-12-29 04:57:28 -05:00
|
|
|
console.Debugf(applyActionsLogPrefix+" lifecycle: %q (version-id=%s), Initial scan: %v\n", i.objectPath(), versionID, action)
|
2020-12-13 15:05:54 -05:00
|
|
|
} else {
|
2020-12-29 04:57:28 -05:00
|
|
|
console.Debugf(applyActionsLogPrefix+" lifecycle: %q Initial scan: %v\n", i.objectPath(), action)
|
2020-12-13 15:05:54 -05:00
|
|
|
}
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
switch action {
|
2020-07-04 23:56:02 -04:00
|
|
|
case lifecycle.DeleteAction, lifecycle.DeleteVersionAction:
|
2020-11-12 15:12:09 -05:00
|
|
|
case lifecycle.TransitionAction, lifecycle.TransitionVersionAction:
|
|
|
|
case lifecycle.DeleteRestoredAction, lifecycle.DeleteRestoredVersionAction:
|
2020-06-12 13:28:21 -04:00
|
|
|
default:
|
|
|
|
// No action.
|
2020-12-13 15:05:54 -05:00
|
|
|
if i.debug {
|
2020-12-29 04:57:28 -05:00
|
|
|
console.Debugf(applyActionsLogPrefix+" object not expirable: %q\n", i.objectPath())
|
2020-12-13 15:05:54 -05:00
|
|
|
}
|
2021-03-31 05:15:08 -04:00
|
|
|
return false, size
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
|
2020-08-04 02:04:40 -04:00
|
|
|
obj, err := o.GetObjectInfo(ctx, i.bucket, i.objectPath(), ObjectOptions{
|
|
|
|
VersionID: versionID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
switch err.(type) {
|
|
|
|
case MethodNotAllowed: // This happens usually for a delete marker
|
|
|
|
if !obj.DeleteMarker { // if this is not a delete marker log and return
|
|
|
|
// Do nothing - heal in the future.
|
2020-06-12 23:04:01 -04:00
|
|
|
logger.LogIf(ctx, err)
|
2021-03-31 05:15:08 -04:00
|
|
|
return false, size
|
2020-06-12 23:04:01 -04:00
|
|
|
}
|
2021-01-17 16:58:41 -05:00
|
|
|
case ObjectNotFound, VersionNotFound:
|
|
|
|
// object not found or version not found return 0
|
2021-03-31 05:15:08 -04:00
|
|
|
return false, 0
|
2020-06-12 13:28:21 -04:00
|
|
|
default:
|
2020-08-04 02:04:40 -04:00
|
|
|
// All other errors proceed.
|
|
|
|
logger.LogIf(ctx, err)
|
2021-03-31 05:15:08 -04:00
|
|
|
return false, size
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
}
|
2020-08-04 02:04:40 -04:00
|
|
|
|
2021-02-01 12:52:11 -05:00
|
|
|
action = evalActionFromLifecycle(ctx, *i.lifeCycle, obj, i.debug)
|
|
|
|
if action != lifecycle.NoneAction {
|
|
|
|
applied = applyLifecycleAction(ctx, action, o, obj)
|
|
|
|
}
|
|
|
|
|
|
|
|
if applied {
|
2021-03-05 17:15:53 -05:00
|
|
|
switch action {
|
|
|
|
case lifecycle.TransitionAction, lifecycle.TransitionVersionAction:
|
2021-03-31 05:15:08 -04:00
|
|
|
return true, size
|
2021-03-05 17:15:53 -05:00
|
|
|
}
|
2021-03-31 05:15:08 -04:00
|
|
|
// For all other lifecycle actions that remove data
|
|
|
|
return true, 0
|
|
|
|
}
|
|
|
|
|
|
|
|
return false, size
|
|
|
|
}
|
|
|
|
|
2021-06-30 22:32:07 -04:00
|
|
|
// applyTierObjSweep removes remote object pending deletion and the free-version
|
|
|
|
// tracking this information.
|
|
|
|
func (i *scannerItem) applyTierObjSweep(ctx context.Context, o ObjectLayer, meta actionMeta) {
|
|
|
|
if !meta.oi.tierFreeVersion {
|
|
|
|
// 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(ctx, meta.oi.transitionedObjName, meta.oi.transitionVersionID, meta.oi.TransitionTier)
|
|
|
|
if ignoreNotFoundErr(err) != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove this free version
|
|
|
|
opts := ObjectOptions{}
|
|
|
|
opts.VersionID = meta.oi.VersionID
|
|
|
|
_, err = o.DeleteObject(ctx, meta.oi.Bucket, meta.oi.Name, opts)
|
|
|
|
if err == nil {
|
2021-07-01 17:02:44 -04:00
|
|
|
auditLogLifecycle(ctx, meta.oi, ILMFreeVersionDelete)
|
2021-06-30 22:32:07 -04:00
|
|
|
}
|
|
|
|
if ignoreNotFoundErr(err) != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-03-31 05:15:08 -04:00
|
|
|
// applyActions will apply lifecycle checks on to a scanned item.
|
|
|
|
// The resulting size on disk will always be returned.
|
|
|
|
// The metadata will be compared to consensus on the object layer before any changes are applied.
|
|
|
|
// If no metadata is supplied, -1 is returned if no action is taken.
|
2021-04-27 11:24:44 -04:00
|
|
|
func (i *scannerItem) applyActions(ctx context.Context, o ObjectLayer, meta actionMeta, sizeS *sizeSummary) int64 {
|
2021-06-30 22:32:07 -04:00
|
|
|
i.applyTierObjSweep(ctx, o, meta)
|
|
|
|
|
2021-03-31 05:15:08 -04:00
|
|
|
applied, size := i.applyLifecycle(ctx, o, meta)
|
|
|
|
// For instance, an applied lifecycle means we remove/transitioned an object
|
|
|
|
// from the current deployment, which means we don't have to call healing
|
|
|
|
// routine even if we are asked to do via heal flag.
|
2021-04-27 11:24:44 -04:00
|
|
|
if !applied {
|
|
|
|
if i.heal {
|
|
|
|
size = i.applyHealing(ctx, o, meta)
|
|
|
|
}
|
|
|
|
// replicate only if lifecycle rules are not applied.
|
|
|
|
i.healReplication(ctx, o, meta.oi.Clone(), sizeS)
|
2021-02-01 12:52:11 -05:00
|
|
|
}
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
|
|
|
func evalActionFromLifecycle(ctx context.Context, lc lifecycle.Lifecycle, obj ObjectInfo, debug bool) (action lifecycle.Action) {
|
2020-11-12 15:12:09 -05:00
|
|
|
lcOpts := lifecycle.ObjectOpts{
|
2021-04-19 13:30:42 -04:00
|
|
|
Name: obj.Name,
|
|
|
|
UserTags: obj.UserTags,
|
|
|
|
ModTime: obj.ModTime,
|
|
|
|
VersionID: obj.VersionID,
|
|
|
|
DeleteMarker: obj.DeleteMarker,
|
|
|
|
IsLatest: obj.IsLatest,
|
|
|
|
NumVersions: obj.NumVersions,
|
|
|
|
SuccessorModTime: obj.SuccessorModTime,
|
|
|
|
RestoreOngoing: obj.RestoreOngoing,
|
|
|
|
RestoreExpires: obj.RestoreExpires,
|
|
|
|
TransitionStatus: obj.TransitionStatus,
|
|
|
|
RemoteTiersImmediately: globalDebugRemoteTiersImmediately,
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
2021-02-01 12:52:11 -05:00
|
|
|
|
|
|
|
action = lc.ComputeAction(lcOpts)
|
|
|
|
if debug {
|
2020-12-29 04:57:28 -05:00
|
|
|
console.Debugf(applyActionsLogPrefix+" lifecycle: Secondary scan: %v\n", action)
|
2020-08-04 02:04:40 -04:00
|
|
|
}
|
2021-02-01 12:52:11 -05:00
|
|
|
|
|
|
|
if action == lifecycle.NoneAction {
|
|
|
|
return action
|
2020-08-04 02:04:40 -04:00
|
|
|
}
|
2020-06-12 13:28:21 -04:00
|
|
|
|
2020-07-04 23:56:02 -04:00
|
|
|
switch action {
|
2020-11-12 15:12:09 -05:00
|
|
|
case lifecycle.DeleteVersionAction, lifecycle.DeleteRestoredVersionAction:
|
2020-08-04 02:04:40 -04:00
|
|
|
// Defensive code, should never happen
|
|
|
|
if obj.VersionID == "" {
|
2021-02-01 12:52:11 -05:00
|
|
|
return lifecycle.NoneAction
|
2020-08-04 02:04:40 -04:00
|
|
|
}
|
2021-02-01 12:52:11 -05:00
|
|
|
if rcfg, _ := globalBucketObjectLockSys.Get(obj.Bucket); rcfg.LockEnabled {
|
2020-08-04 02:04:40 -04:00
|
|
|
locked := enforceRetentionForDeletion(ctx, obj)
|
|
|
|
if locked {
|
2021-02-01 12:52:11 -05:00
|
|
|
if debug {
|
2020-12-13 15:05:54 -05:00
|
|
|
if obj.VersionID != "" {
|
2021-02-01 12:52:11 -05:00
|
|
|
console.Debugf(applyActionsLogPrefix+" lifecycle: %s v(%s) is locked, not deleting\n", obj.Name, obj.VersionID)
|
2020-12-13 15:05:54 -05:00
|
|
|
} else {
|
2021-02-01 12:52:11 -05:00
|
|
|
console.Debugf(applyActionsLogPrefix+" lifecycle: %s is locked, not deleting\n", obj.Name)
|
2020-12-13 15:05:54 -05:00
|
|
|
}
|
2020-08-04 02:04:40 -04:00
|
|
|
}
|
2021-02-01 12:52:11 -05:00
|
|
|
return lifecycle.NoneAction
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
|
|
|
}
|
2020-07-04 23:56:02 -04:00
|
|
|
}
|
2020-12-13 15:05:54 -05:00
|
|
|
|
2021-02-01 12:52:11 -05:00
|
|
|
return action
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyTransitionAction(ctx context.Context, action lifecycle.Action, objLayer ObjectLayer, obj ObjectInfo) bool {
|
2021-04-19 13:30:42 -04:00
|
|
|
srcOpts := ObjectOptions{}
|
2021-02-01 12:52:11 -05:00
|
|
|
if obj.TransitionStatus == "" {
|
2021-04-19 13:30:42 -04:00
|
|
|
srcOpts.Versioned = globalBucketVersioningSys.Enabled(obj.Bucket)
|
|
|
|
srcOpts.VersionID = obj.VersionID
|
|
|
|
// mark transition as pending
|
|
|
|
obj.UserDefined[ReservedMetadataPrefixLower+TransitionStatus] = lifecycle.TransitionPending
|
|
|
|
obj.metadataOnly = true // Perform only metadata updates.
|
|
|
|
if obj.DeleteMarker {
|
2021-02-01 12:52:11 -05:00
|
|
|
return false
|
2020-11-12 15:12:09 -05:00
|
|
|
}
|
2020-12-13 15:05:54 -05:00
|
|
|
}
|
2021-02-01 12:52:11 -05:00
|
|
|
globalTransitionState.queueTransitionTask(obj)
|
|
|
|
return true
|
2020-12-13 15:05:54 -05:00
|
|
|
|
2021-02-01 12:52:11 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func applyExpiryOnTransitionedObject(ctx context.Context, objLayer ObjectLayer, obj ObjectInfo, restoredObject bool) bool {
|
|
|
|
lcOpts := lifecycle.ObjectOpts{
|
|
|
|
Name: obj.Name,
|
|
|
|
UserTags: obj.UserTags,
|
|
|
|
ModTime: obj.ModTime,
|
|
|
|
VersionID: obj.VersionID,
|
|
|
|
DeleteMarker: obj.DeleteMarker,
|
|
|
|
IsLatest: obj.IsLatest,
|
|
|
|
NumVersions: obj.NumVersions,
|
|
|
|
SuccessorModTime: obj.SuccessorModTime,
|
|
|
|
RestoreOngoing: obj.RestoreOngoing,
|
|
|
|
RestoreExpires: obj.RestoreExpires,
|
|
|
|
TransitionStatus: obj.TransitionStatus,
|
|
|
|
}
|
|
|
|
|
2021-04-19 13:30:42 -04:00
|
|
|
action := expireObj
|
|
|
|
if restoredObject {
|
|
|
|
action = expireRestoredObj
|
|
|
|
}
|
2021-06-03 17:26:51 -04:00
|
|
|
if err := expireTransitionedObject(ctx, objLayer, &obj, lcOpts, action); err != nil {
|
2021-02-01 12:52:11 -05:00
|
|
|
if isErrObjectNotFound(err) || isErrVersionNotFound(err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return false
|
|
|
|
}
|
2021-04-19 13:30:42 -04:00
|
|
|
// Notification already sent in *expireTransitionedObject*, just return 'true' here.
|
2021-02-01 12:52:11 -05:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2021-02-06 19:10:33 -05:00
|
|
|
func applyExpiryOnNonTransitionedObjects(ctx context.Context, objLayer ObjectLayer, obj ObjectInfo, applyOnVersion bool) bool {
|
2021-02-01 12:52:11 -05:00
|
|
|
opts := ObjectOptions{}
|
|
|
|
|
2021-02-06 19:10:33 -05:00
|
|
|
if applyOnVersion {
|
|
|
|
opts.VersionID = obj.VersionID
|
|
|
|
}
|
2021-02-01 12:52:11 -05:00
|
|
|
if opts.VersionID == "" {
|
|
|
|
opts.Versioned = globalBucketVersioningSys.Enabled(obj.Bucket)
|
|
|
|
}
|
|
|
|
|
|
|
|
obj, err := objLayer.DeleteObject(ctx, obj.Bucket, obj.Name, opts)
|
2020-12-13 15:05:54 -05:00
|
|
|
if err != nil {
|
2021-01-17 16:58:41 -05:00
|
|
|
if isErrObjectNotFound(err) || isErrVersionNotFound(err) {
|
2021-02-01 12:52:11 -05:00
|
|
|
return false
|
2021-01-17 16:58:41 -05:00
|
|
|
}
|
2020-12-13 15:05:54 -05:00
|
|
|
// Assume it is still there.
|
|
|
|
logger.LogIf(ctx, err)
|
2021-02-01 12:52:11 -05:00
|
|
|
return false
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
|
2021-04-23 12:51:12 -04:00
|
|
|
// Send audit for the lifecycle delete operation
|
2021-07-01 17:02:44 -04:00
|
|
|
auditLogLifecycle(ctx, obj, ILMExpiry)
|
2021-04-23 12:51:12 -04:00
|
|
|
|
2020-10-17 00:22:12 -04:00
|
|
|
eventName := event.ObjectRemovedDelete
|
|
|
|
if obj.DeleteMarker {
|
|
|
|
eventName = event.ObjectRemovedDeleteMarkerCreated
|
|
|
|
}
|
|
|
|
|
2020-06-12 13:28:21 -04:00
|
|
|
// Notify object deleted event.
|
|
|
|
sendEvent(eventArgs{
|
2020-10-17 00:22:12 -04:00
|
|
|
EventName: eventName,
|
2021-02-01 12:52:11 -05:00
|
|
|
BucketName: obj.Bucket,
|
2020-06-12 23:04:01 -04:00
|
|
|
Object: obj,
|
|
|
|
Host: "Internal: [ILM-EXPIRY]",
|
2020-06-12 13:28:21 -04:00
|
|
|
})
|
2021-02-01 12:52:11 -05:00
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Apply object, object version, restored object or restored object version action on the given object
|
2021-02-06 19:10:33 -05:00
|
|
|
func applyExpiryRule(ctx context.Context, objLayer ObjectLayer, obj ObjectInfo, restoredObject, applyOnVersion bool) bool {
|
2021-02-01 12:52:11 -05:00
|
|
|
if obj.TransitionStatus != "" {
|
|
|
|
return applyExpiryOnTransitionedObject(ctx, objLayer, obj, restoredObject)
|
|
|
|
}
|
2021-02-06 19:10:33 -05:00
|
|
|
return applyExpiryOnNonTransitionedObjects(ctx, objLayer, obj, applyOnVersion)
|
2021-02-01 12:52:11 -05:00
|
|
|
}
|
|
|
|
|
2021-03-05 17:15:53 -05:00
|
|
|
// Perform actions (removal or transitioning of objects), return true the action is successfully performed
|
2021-02-01 12:52:11 -05:00
|
|
|
func applyLifecycleAction(ctx context.Context, action lifecycle.Action, objLayer ObjectLayer, obj ObjectInfo) (success bool) {
|
|
|
|
switch action {
|
|
|
|
case lifecycle.DeleteVersionAction, lifecycle.DeleteAction:
|
2021-02-06 19:10:33 -05:00
|
|
|
success = applyExpiryRule(ctx, objLayer, obj, false, action == lifecycle.DeleteVersionAction)
|
2021-02-01 12:52:11 -05:00
|
|
|
case lifecycle.DeleteRestoredAction, lifecycle.DeleteRestoredVersionAction:
|
2021-02-06 19:10:33 -05:00
|
|
|
success = applyExpiryRule(ctx, objLayer, obj, true, action == lifecycle.DeleteRestoredVersionAction)
|
2021-02-01 12:52:11 -05:00
|
|
|
case lifecycle.TransitionAction, lifecycle.TransitionVersionAction:
|
|
|
|
success = applyTransitionAction(ctx, action, objLayer, obj)
|
|
|
|
}
|
|
|
|
return
|
2020-06-12 13:28:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// objectPath returns the prefix and object name.
|
2021-02-26 18:11:42 -05:00
|
|
|
func (i *scannerItem) objectPath() string {
|
2020-06-12 13:28:21 -04:00
|
|
|
return path.Join(i.prefix, i.objectName)
|
|
|
|
}
|
|
|
|
|
2020-07-21 20:49:56 -04:00
|
|
|
// healReplication will heal a scanned item that has failed replication.
|
2021-02-26 18:11:42 -05:00
|
|
|
func (i *scannerItem) healReplication(ctx context.Context, o ObjectLayer, oi ObjectInfo, sizeS *sizeSummary) {
|
2021-06-01 22:59:11 -04:00
|
|
|
existingObjResync := i.replication.Resync(ctx, oi)
|
2020-12-28 13:31:00 -05:00
|
|
|
if oi.DeleteMarker || !oi.VersionPurgeStatus.Empty() {
|
2020-12-13 15:05:54 -05:00
|
|
|
// heal delete marker replication failure or versioned delete replication failure
|
2020-12-28 13:31:00 -05:00
|
|
|
if oi.ReplicationStatus == replication.Pending ||
|
|
|
|
oi.ReplicationStatus == replication.Failed ||
|
|
|
|
oi.VersionPurgeStatus == Failed || oi.VersionPurgeStatus == Pending {
|
2021-06-01 22:59:11 -04:00
|
|
|
i.healReplicationDeletes(ctx, o, oi, existingObjResync)
|
2020-11-19 21:43:58 -05:00
|
|
|
return
|
|
|
|
}
|
2021-06-01 22:59:11 -04:00
|
|
|
// if replication status is Complete on DeleteMarker and existing object resync required
|
|
|
|
if existingObjResync && oi.ReplicationStatus == replication.Completed {
|
|
|
|
i.healReplicationDeletes(ctx, o, oi, existingObjResync)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
roi := ReplicateObjectInfo{ObjectInfo: oi, OpType: replication.HealReplicationType}
|
|
|
|
if existingObjResync {
|
|
|
|
roi.OpType = replication.ExistingObjectReplicationType
|
|
|
|
roi.ResetID = i.replication.ResetID
|
2020-11-19 21:43:58 -05:00
|
|
|
}
|
2020-12-28 13:31:00 -05:00
|
|
|
switch oi.ReplicationStatus {
|
2020-12-07 16:47:48 -05:00
|
|
|
case replication.Pending:
|
2021-04-03 12:03:42 -04:00
|
|
|
sizeS.pendingCount++
|
2020-12-28 13:31:00 -05:00
|
|
|
sizeS.pendingSize += oi.Size
|
2021-06-01 22:59:11 -04:00
|
|
|
globalReplicationPool.queueReplicaTask(roi)
|
|
|
|
return
|
2020-12-07 16:47:48 -05:00
|
|
|
case replication.Failed:
|
2020-12-28 13:31:00 -05:00
|
|
|
sizeS.failedSize += oi.Size
|
2021-04-03 12:03:42 -04:00
|
|
|
sizeS.failedCount++
|
2021-06-01 22:59:11 -04:00
|
|
|
globalReplicationPool.queueReplicaTask(roi)
|
|
|
|
return
|
2021-01-13 14:52:28 -05:00
|
|
|
case replication.Completed, "COMPLETE":
|
2020-12-28 13:31:00 -05:00
|
|
|
sizeS.replicatedSize += oi.Size
|
2020-12-07 16:47:48 -05:00
|
|
|
case replication.Replica:
|
2020-12-28 13:31:00 -05:00
|
|
|
sizeS.replicaSize += oi.Size
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
2021-06-01 22:59:11 -04:00
|
|
|
if existingObjResync {
|
|
|
|
globalReplicationPool.queueReplicaTask(roi)
|
|
|
|
}
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
2020-11-19 21:43:58 -05:00
|
|
|
|
|
|
|
// healReplicationDeletes will heal a scanned deleted item that failed to replicate deletes.
|
2021-06-01 22:59:11 -04:00
|
|
|
func (i *scannerItem) healReplicationDeletes(ctx context.Context, o ObjectLayer, oi ObjectInfo, existingObject bool) {
|
2020-11-19 21:43:58 -05:00
|
|
|
// handle soft delete and permanent delete failures here.
|
2020-12-28 13:31:00 -05:00
|
|
|
if oi.DeleteMarker || !oi.VersionPurgeStatus.Empty() {
|
2020-11-19 21:43:58 -05:00
|
|
|
versionID := ""
|
|
|
|
dmVersionID := ""
|
2020-12-28 13:31:00 -05:00
|
|
|
if oi.VersionPurgeStatus.Empty() {
|
|
|
|
dmVersionID = oi.VersionID
|
2020-11-19 21:43:58 -05:00
|
|
|
} else {
|
2020-12-28 13:31:00 -05:00
|
|
|
versionID = oi.VersionID
|
2020-11-19 21:43:58 -05:00
|
|
|
}
|
2021-06-01 22:59:11 -04:00
|
|
|
doi := DeletedObjectReplicationInfo{
|
2020-11-19 21:43:58 -05:00
|
|
|
DeletedObject: DeletedObject{
|
2020-12-28 13:31:00 -05:00
|
|
|
ObjectName: oi.Name,
|
2020-11-19 21:43:58 -05:00
|
|
|
DeleteMarkerVersionID: dmVersionID,
|
|
|
|
VersionID: versionID,
|
2020-12-28 13:31:00 -05:00
|
|
|
DeleteMarkerReplicationStatus: string(oi.ReplicationStatus),
|
|
|
|
DeleteMarkerMTime: DeleteMarkerMTime{oi.ModTime},
|
|
|
|
DeleteMarker: oi.DeleteMarker,
|
|
|
|
VersionPurgeStatus: oi.VersionPurgeStatus,
|
2020-11-19 21:43:58 -05:00
|
|
|
},
|
2020-12-28 13:31:00 -05:00
|
|
|
Bucket: oi.Bucket,
|
2021-06-01 22:59:11 -04:00
|
|
|
}
|
|
|
|
if existingObject {
|
|
|
|
doi.OpType = replication.ExistingObjectReplicationType
|
|
|
|
doi.ResetID = i.replication.ResetID
|
|
|
|
}
|
|
|
|
globalReplicationPool.queueReplicaDeleteTask(doi)
|
2020-11-19 21:43:58 -05:00
|
|
|
}
|
|
|
|
}
|
2020-12-04 12:32:35 -05:00
|
|
|
|
|
|
|
type dynamicSleeper struct {
|
|
|
|
mu sync.RWMutex
|
|
|
|
|
|
|
|
// Sleep factor
|
|
|
|
factor float64
|
|
|
|
|
|
|
|
// maximum sleep cap,
|
|
|
|
// set to <= 0 to disable.
|
|
|
|
maxSleep time.Duration
|
|
|
|
|
|
|
|
// Don't sleep at all, if time taken is below this value.
|
|
|
|
// This is to avoid too small costly sleeps.
|
|
|
|
minSleep time.Duration
|
|
|
|
|
|
|
|
// cycle will be closed
|
|
|
|
cycle chan struct{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// newDynamicSleeper
|
|
|
|
func newDynamicSleeper(factor float64, maxWait time.Duration) *dynamicSleeper {
|
|
|
|
return &dynamicSleeper{
|
|
|
|
factor: factor,
|
|
|
|
cycle: make(chan struct{}),
|
|
|
|
maxSleep: maxWait,
|
|
|
|
minSleep: 100 * time.Microsecond,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Timer returns a timer that has started.
|
|
|
|
// When the returned function is called it will wait.
|
|
|
|
func (d *dynamicSleeper) Timer(ctx context.Context) func() {
|
|
|
|
t := time.Now()
|
|
|
|
return func() {
|
|
|
|
doneAt := time.Now()
|
|
|
|
for {
|
|
|
|
// Grab current values
|
|
|
|
d.mu.RLock()
|
|
|
|
minWait, maxWait := d.minSleep, d.maxSleep
|
|
|
|
factor := d.factor
|
|
|
|
cycle := d.cycle
|
|
|
|
d.mu.RUnlock()
|
|
|
|
elapsed := doneAt.Sub(t)
|
|
|
|
// Don't sleep for really small amount of time
|
|
|
|
wantSleep := time.Duration(float64(elapsed) * factor)
|
|
|
|
if wantSleep <= minWait {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if maxWait > 0 && wantSleep > maxWait {
|
|
|
|
wantSleep = maxWait
|
|
|
|
}
|
|
|
|
timer := time.NewTimer(wantSleep)
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
if !timer.Stop() {
|
|
|
|
<-timer.C
|
|
|
|
}
|
|
|
|
return
|
|
|
|
case <-timer.C:
|
|
|
|
return
|
|
|
|
case <-cycle:
|
|
|
|
if !timer.Stop() {
|
|
|
|
// We expired.
|
|
|
|
<-timer.C
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sleep sleeps the specified time multiplied by the sleep factor.
|
|
|
|
// If the factor is updated the sleep will be done again with the new factor.
|
|
|
|
func (d *dynamicSleeper) Sleep(ctx context.Context, base time.Duration) {
|
|
|
|
for {
|
|
|
|
// Grab current values
|
|
|
|
d.mu.RLock()
|
|
|
|
minWait, maxWait := d.minSleep, d.maxSleep
|
|
|
|
factor := d.factor
|
|
|
|
cycle := d.cycle
|
|
|
|
d.mu.RUnlock()
|
|
|
|
// Don't sleep for really small amount of time
|
|
|
|
wantSleep := time.Duration(float64(base) * factor)
|
|
|
|
if wantSleep <= minWait {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if maxWait > 0 && wantSleep > maxWait {
|
|
|
|
wantSleep = maxWait
|
|
|
|
}
|
|
|
|
timer := time.NewTimer(wantSleep)
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
if !timer.Stop() {
|
|
|
|
<-timer.C
|
|
|
|
}
|
|
|
|
return
|
|
|
|
case <-timer.C:
|
|
|
|
return
|
|
|
|
case <-cycle:
|
|
|
|
if !timer.Stop() {
|
|
|
|
// We expired.
|
|
|
|
<-timer.C
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the current settings and cycle all waiting.
|
|
|
|
// Parameters are the same as in the contructor.
|
|
|
|
func (d *dynamicSleeper) Update(factor float64, maxWait time.Duration) error {
|
|
|
|
d.mu.Lock()
|
|
|
|
defer d.mu.Unlock()
|
|
|
|
if math.Abs(d.factor-factor) < 1e-10 && d.maxSleep == maxWait {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Update values and cycle waiting.
|
|
|
|
close(d.cycle)
|
|
|
|
d.factor = factor
|
|
|
|
d.maxSleep = maxWait
|
|
|
|
d.cycle = make(chan struct{})
|
|
|
|
return nil
|
|
|
|
}
|
2021-04-23 12:51:12 -04:00
|
|
|
|
2021-06-30 22:32:07 -04:00
|
|
|
const (
|
2021-07-01 17:02:44 -04:00
|
|
|
// ILMExpiry - audit trail for ILM expiry
|
|
|
|
ILMExpiry = "ilm:expiry"
|
|
|
|
// ILMFreeVersionDelete - audit trail for ILM free-version delete
|
|
|
|
ILMFreeVersionDelete = "ilm:free-version-delete"
|
2021-06-30 22:32:07 -04:00
|
|
|
)
|
2021-06-29 02:58:08 -04:00
|
|
|
|
2021-07-01 17:02:44 -04:00
|
|
|
func auditLogLifecycle(ctx context.Context, oi ObjectInfo, trigger string) {
|
2021-06-30 22:32:07 -04:00
|
|
|
var apiName string
|
|
|
|
switch trigger {
|
2021-07-01 17:02:44 -04:00
|
|
|
case ILMExpiry:
|
|
|
|
apiName = "ILMExpiry"
|
|
|
|
case ILMFreeVersionDelete:
|
|
|
|
apiName = "ILMFreeVersionDelete"
|
2021-06-30 22:32:07 -04:00
|
|
|
}
|
2021-07-01 17:02:44 -04:00
|
|
|
auditLogInternal(ctx, oi.Bucket, oi.Name, AuditLogOptions{
|
2021-06-30 22:32:07 -04:00
|
|
|
Trigger: trigger,
|
|
|
|
APIName: apiName,
|
2021-07-01 17:02:44 -04:00
|
|
|
VersionID: oi.VersionID,
|
2021-06-29 02:58:08 -04:00
|
|
|
})
|
2021-04-23 12:51:12 -04:00
|
|
|
}
|