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-10-28 12:18:35 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2021-02-25 01:24:38 -05:00
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-10-28 12:18:35 -04:00
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2021-02-25 01:24:38 -05:00
|
|
|
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/logger"
|
2020-10-28 12:18:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
type scanStatus uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
scanStateNone scanStatus = iota
|
|
|
|
scanStateStarted
|
|
|
|
scanStateSuccess
|
|
|
|
scanStateError
|
|
|
|
|
|
|
|
// Time in which the initiator of a scan must have reported back.
|
|
|
|
metacacheMaxRunningAge = time.Minute
|
|
|
|
|
|
|
|
// metacacheBlockSize is the number of file/directory entries to have in each block.
|
|
|
|
metacacheBlockSize = 5000
|
2020-11-18 13:44:18 -05:00
|
|
|
|
|
|
|
// metacacheSharePrefix controls whether prefixes on dirty paths are always shared.
|
|
|
|
// This will make `test/a` and `test/b` share listings if they are concurrent.
|
|
|
|
// Enabling this will make cache sharing more likely and cause less IO,
|
|
|
|
// but may cause additional latency to some calls.
|
|
|
|
metacacheSharePrefix = false
|
2020-10-28 12:18:35 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
//go:generate msgp -file $GOFILE -unexported
|
|
|
|
|
|
|
|
// metacache contains a tracked cache entry.
|
|
|
|
type metacache struct {
|
|
|
|
id string `msg:"id"`
|
|
|
|
bucket string `msg:"b"`
|
|
|
|
root string `msg:"root"`
|
|
|
|
recursive bool `msg:"rec"`
|
2020-11-18 13:44:18 -05:00
|
|
|
filter string `msg:"flt"`
|
2020-10-28 12:18:35 -04:00
|
|
|
status scanStatus `msg:"stat"`
|
|
|
|
fileNotFound bool `msg:"fnf"`
|
|
|
|
error string `msg:"err"`
|
|
|
|
started time.Time `msg:"st"`
|
|
|
|
ended time.Time `msg:"end"`
|
|
|
|
lastUpdate time.Time `msg:"u"`
|
|
|
|
lastHandout time.Time `msg:"lh"`
|
|
|
|
dataVersion uint8 `msg:"v"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *metacache) finished() bool {
|
|
|
|
return !m.ended.IsZero()
|
|
|
|
}
|
|
|
|
|
|
|
|
// worthKeeping indicates if the cache by itself is worth keeping.
|
2021-07-05 18:34:41 -04:00
|
|
|
func (m *metacache) worthKeeping() bool {
|
2020-10-28 12:18:35 -04:00
|
|
|
if m == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
cache := m
|
|
|
|
switch {
|
|
|
|
case !cache.finished() && time.Since(cache.lastUpdate) > metacacheMaxRunningAge:
|
|
|
|
// Not finished and update for metacacheMaxRunningAge, discard it.
|
|
|
|
return false
|
2021-07-05 18:34:41 -04:00
|
|
|
case cache.finished() && time.Since(cache.lastHandout) > 30*time.Minute:
|
|
|
|
// Keep only for 30 minutes.
|
2020-10-28 12:18:35 -04:00
|
|
|
return false
|
|
|
|
case cache.status == scanStateError || cache.status == scanStateNone:
|
2021-02-11 13:22:03 -05:00
|
|
|
// Remove failed listings after 5 minutes.
|
2021-07-05 18:34:41 -04:00
|
|
|
return time.Since(cache.lastUpdate) > 5*time.Minute
|
2020-10-28 12:18:35 -04:00
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// baseDirFromPrefix will return the base directory given an object path.
|
|
|
|
// For example an object with name prefix/folder/object.ext will return `prefix/folder/`.
|
|
|
|
func baseDirFromPrefix(prefix string) string {
|
|
|
|
b := path.Dir(prefix)
|
|
|
|
if b == "." || b == "./" || b == "/" {
|
|
|
|
b = ""
|
|
|
|
}
|
|
|
|
if !strings.Contains(prefix, slashSeparator) {
|
|
|
|
b = ""
|
|
|
|
}
|
|
|
|
if len(b) > 0 && !strings.HasSuffix(b, slashSeparator) {
|
|
|
|
b += slashSeparator
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
2020-11-03 15:47:52 -05:00
|
|
|
|
2020-11-05 10:34:08 -05:00
|
|
|
// update cache with new status.
|
|
|
|
// The updates are conditional so multiple callers can update with different states.
|
|
|
|
func (m *metacache) update(update metacache) {
|
|
|
|
m.lastUpdate = UTCNow()
|
|
|
|
|
|
|
|
if m.status == scanStateStarted && update.status == scanStateSuccess {
|
|
|
|
m.ended = UTCNow()
|
|
|
|
}
|
|
|
|
|
|
|
|
if m.status == scanStateStarted && update.status != scanStateStarted {
|
|
|
|
m.status = update.status
|
|
|
|
}
|
|
|
|
|
2021-07-05 18:34:41 -04:00
|
|
|
if m.status == scanStateStarted && time.Since(m.lastHandout) > 15*time.Minute {
|
|
|
|
m.status = scanStateError
|
|
|
|
m.error = "client not seen"
|
|
|
|
}
|
|
|
|
|
2020-11-05 10:34:08 -05:00
|
|
|
if m.error == "" && update.error != "" {
|
|
|
|
m.error = update.error
|
|
|
|
m.status = scanStateError
|
|
|
|
m.ended = UTCNow()
|
|
|
|
}
|
|
|
|
m.fileNotFound = m.fileNotFound || update.fileNotFound
|
|
|
|
}
|
2021-02-25 01:24:38 -05:00
|
|
|
|
|
|
|
// delete all cache data on disks.
|
|
|
|
func (m *metacache) delete(ctx context.Context) {
|
|
|
|
if m.bucket == "" || m.id == "" {
|
|
|
|
logger.LogIf(ctx, fmt.Errorf("metacache.delete: bucket (%s) or id (%s) empty", m.bucket, m.id))
|
|
|
|
}
|
|
|
|
objAPI := newObjectLayerFn()
|
|
|
|
if objAPI == nil {
|
|
|
|
logger.LogIf(ctx, errors.New("metacache.delete: no object layer"))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ez, ok := objAPI.(*erasureServerPools)
|
|
|
|
if !ok {
|
|
|
|
logger.LogIf(ctx, errors.New("metacache.delete: expected objAPI to be *erasureServerPools"))
|
|
|
|
return
|
|
|
|
}
|
2021-08-19 12:16:14 -04:00
|
|
|
ez.renameAll(ctx, minioMetaBucket, metacachePrefixForID(m.bucket, m.id))
|
2021-02-25 01:24:38 -05:00
|
|
|
}
|