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 (
|
|
|
|
"context"
|
2020-10-30 12:33:16 -04:00
|
|
|
"errors"
|
2021-02-11 13:22:03 -05:00
|
|
|
"fmt"
|
2020-10-28 12:18:35 -04:00
|
|
|
"io"
|
2021-02-11 13:22:03 -05:00
|
|
|
"os"
|
2020-10-28 12:18:35 -04:00
|
|
|
"path"
|
2021-02-26 12:52:27 -05:00
|
|
|
pathutil "path"
|
2021-01-13 12:44:11 -05:00
|
|
|
"strings"
|
2020-10-28 12:18:35 -04:00
|
|
|
"sync"
|
2020-11-18 13:28:22 -05:00
|
|
|
"time"
|
2020-10-28 12:18:35 -04:00
|
|
|
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/logger"
|
2020-10-28 12:18:35 -04:00
|
|
|
)
|
|
|
|
|
2021-02-11 13:22:03 -05:00
|
|
|
func renameAllBucketMetacache(epPath string) error {
|
|
|
|
// Rename all previous `.minio.sys/buckets/<bucketname>/.metacache` to
|
|
|
|
// to `.minio.sys/tmp/` for deletion.
|
2021-02-17 18:34:42 -05:00
|
|
|
return readDirFn(pathJoin(epPath, minioMetaBucket, bucketMetaPrefix), func(name string, typ os.FileMode) error {
|
2021-02-11 13:22:03 -05:00
|
|
|
if typ == os.ModeDir {
|
2021-02-26 12:52:27 -05:00
|
|
|
tmpMetacacheOld := pathutil.Join(epPath, minioMetaTmpDeletedBucket, mustGetUUID())
|
2021-02-11 13:22:03 -05:00
|
|
|
if err := renameAll(pathJoin(epPath, minioMetaBucket, metacachePrefixForID(name, slashSeparator)),
|
|
|
|
tmpMetacacheOld); err != nil && err != errFileNotFound {
|
|
|
|
return fmt.Errorf("unable to rename (%s -> %s) %w",
|
|
|
|
pathJoin(epPath, minioMetaBucket+metacachePrefixForID(minioMetaBucket, slashSeparator)),
|
|
|
|
tmpMetacacheOld,
|
|
|
|
osErrToFileErr(err))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-28 12:18:35 -04:00
|
|
|
// listPath will return the requested entries.
|
|
|
|
// If no more entries are in the listing io.EOF is returned,
|
|
|
|
// otherwise nil or an unexpected error is returned.
|
|
|
|
// The listPathOptions given will be checked and modified internally.
|
|
|
|
// Required important fields are Bucket, Prefix, Separator.
|
|
|
|
// Other important fields are Limit, Marker.
|
|
|
|
// List ID always derived from the Marker.
|
2020-12-01 16:50:33 -05:00
|
|
|
func (z *erasureServerPools) listPath(ctx context.Context, o listPathOptions) (entries metaCacheEntriesSorted, err error) {
|
2020-10-28 12:18:35 -04:00
|
|
|
if err := checkListObjsArgs(ctx, o.Bucket, o.Prefix, o.Marker, z); err != nil {
|
|
|
|
return entries, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Marker is set validate pre-condition.
|
|
|
|
if o.Marker != "" && o.Prefix != "" {
|
|
|
|
// Marker not common with prefix is not implemented. Send an empty response
|
|
|
|
if !HasPrefix(o.Marker, o.Prefix) {
|
|
|
|
return entries, io.EOF
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// With max keys of zero we have reached eof, return right here.
|
|
|
|
if o.Limit == 0 {
|
|
|
|
return entries, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
// For delimiter and prefix as '/' we do not list anything at all
|
|
|
|
// along // with the prefix. On a flat namespace with 'prefix'
|
|
|
|
// as '/' we don't have any entries, since all the keys are
|
|
|
|
// of form 'keyName/...'
|
2021-01-13 12:44:11 -05:00
|
|
|
if strings.HasPrefix(o.Prefix, SlashSeparator) {
|
2020-10-28 12:18:35 -04:00
|
|
|
return entries, io.EOF
|
|
|
|
}
|
|
|
|
|
|
|
|
// If delimiter is slashSeparator we must return directories of
|
|
|
|
// the non-recursive scan unless explicitly requested.
|
|
|
|
o.IncludeDirectories = o.Separator == slashSeparator
|
|
|
|
if (o.Separator == slashSeparator || o.Separator == "") && !o.Recursive {
|
|
|
|
o.Recursive = o.Separator != slashSeparator
|
|
|
|
o.Separator = slashSeparator
|
|
|
|
} else {
|
|
|
|
// Default is recursive, if delimiter is set then list non recursive.
|
|
|
|
o.Recursive = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Decode and get the optional list id from the marker.
|
|
|
|
o.Marker, o.ID = parseMarker(o.Marker)
|
|
|
|
o.Create = o.ID == ""
|
|
|
|
if o.ID == "" {
|
|
|
|
o.ID = mustGetUUID()
|
|
|
|
}
|
|
|
|
o.BaseDir = baseDirFromPrefix(o.Prefix)
|
2020-12-15 14:25:36 -05:00
|
|
|
if o.discardResult {
|
2020-11-13 19:58:20 -05:00
|
|
|
// Override for single object.
|
|
|
|
o.BaseDir = o.Prefix
|
|
|
|
}
|
2020-10-28 12:18:35 -04:00
|
|
|
|
2020-12-15 14:25:36 -05:00
|
|
|
// For very small recursive listings, don't same cache.
|
|
|
|
// Attempts to avoid expensive listings to run for a long
|
|
|
|
// while when clients aren't interested in results.
|
|
|
|
// If the client DOES resume the listing a full cache
|
|
|
|
// will be generated due to the marker without ID and this check failing.
|
|
|
|
if o.Limit < 10 && o.Marker == "" && o.Create && o.Recursive {
|
|
|
|
o.discardResult = true
|
2021-02-25 01:24:38 -05:00
|
|
|
o.Transient = true
|
2020-12-15 14:25:36 -05:00
|
|
|
}
|
|
|
|
|
2020-10-28 12:18:35 -04:00
|
|
|
var cache metacache
|
|
|
|
// If we don't have a list id we must ask the server if it has a cache or create a new.
|
|
|
|
if o.Create {
|
|
|
|
o.CurrentCycle = intDataUpdateTracker.current()
|
|
|
|
o.OldestCycle = globalNotificationSys.findEarliestCleanBloomFilter(ctx, path.Join(o.Bucket, o.BaseDir))
|
|
|
|
var cache metacache
|
|
|
|
rpc := globalNotificationSys.restClientFromHash(o.Bucket)
|
2020-11-03 15:47:52 -05:00
|
|
|
if isReservedOrInvalidBucket(o.Bucket, false) {
|
|
|
|
rpc = nil
|
2021-02-25 01:24:38 -05:00
|
|
|
o.Transient = true
|
2020-11-03 15:47:52 -05:00
|
|
|
}
|
2020-11-18 13:44:18 -05:00
|
|
|
// Apply prefix filter if enabled.
|
|
|
|
o.SetFilter()
|
2021-02-25 01:24:38 -05:00
|
|
|
if rpc == nil || o.Transient {
|
2020-10-28 12:18:35 -04:00
|
|
|
// Local
|
2020-11-03 15:47:52 -05:00
|
|
|
cache = localMetacacheMgr.findCache(ctx, o)
|
2020-10-28 12:18:35 -04:00
|
|
|
} else {
|
2020-11-18 13:28:22 -05:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
defer cancel()
|
2020-10-28 12:18:35 -04:00
|
|
|
c, err := rpc.GetMetacacheListing(ctx, o)
|
|
|
|
if err != nil {
|
2020-11-18 13:28:22 -05:00
|
|
|
if errors.Is(err, context.Canceled) {
|
2020-10-30 12:33:16 -04:00
|
|
|
// Context is canceled, return at once.
|
2020-11-03 11:53:48 -05:00
|
|
|
// request canceled, no entries to return
|
|
|
|
return entries, io.EOF
|
2020-10-30 12:33:16 -04:00
|
|
|
}
|
2020-11-18 13:28:22 -05:00
|
|
|
if !errors.Is(err, context.DeadlineExceeded) {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
2021-02-25 01:24:38 -05:00
|
|
|
o.Transient = true
|
2020-11-03 15:47:52 -05:00
|
|
|
cache = localMetacacheMgr.findCache(ctx, o)
|
2020-10-28 12:18:35 -04:00
|
|
|
} else {
|
|
|
|
cache = *c
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if cache.fileNotFound {
|
2020-11-03 11:53:48 -05:00
|
|
|
// No cache found, no entries found.
|
|
|
|
return entries, io.EOF
|
2020-10-28 12:18:35 -04:00
|
|
|
}
|
|
|
|
// Only create if we created a new.
|
|
|
|
o.Create = o.ID == cache.id
|
|
|
|
o.ID = cache.id
|
|
|
|
}
|
|
|
|
|
|
|
|
var mu sync.Mutex
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
var errs []error
|
|
|
|
allAtEOF := true
|
|
|
|
mu.Lock()
|
|
|
|
// Ask all sets and merge entries.
|
2021-01-06 12:35:47 -05:00
|
|
|
for _, pool := range z.serverPools {
|
|
|
|
for _, set := range pool.sets {
|
2020-10-28 12:18:35 -04:00
|
|
|
wg.Add(1)
|
|
|
|
go func(i int, set *erasureObjects) {
|
|
|
|
defer wg.Done()
|
|
|
|
e, err := set.listPath(ctx, o)
|
|
|
|
mu.Lock()
|
|
|
|
defer mu.Unlock()
|
|
|
|
if err == nil {
|
|
|
|
allAtEOF = false
|
|
|
|
}
|
|
|
|
errs[i] = err
|
|
|
|
entries.merge(e, -1)
|
|
|
|
|
|
|
|
// Resolve non-trivial conflicts
|
|
|
|
entries.deduplicate(func(existing, other *metaCacheEntry) (replace bool) {
|
|
|
|
if existing.isDir() {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
eFIV, err := existing.fileInfo(o.Bucket)
|
|
|
|
if err != nil {
|
|
|
|
return true
|
|
|
|
}
|
2021-05-19 12:21:18 -04:00
|
|
|
oFIV, err := other.fileInfo(o.Bucket)
|
2020-10-28 12:18:35 -04:00
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
2021-05-19 12:21:18 -04:00
|
|
|
// Replace if modtime is newer
|
|
|
|
if !oFIV.ModTime.Equal(eFIV.ModTime) {
|
|
|
|
return oFIV.ModTime.After(eFIV.ModTime)
|
|
|
|
}
|
|
|
|
// Use NumVersions as a final tiebreaker.
|
|
|
|
return oFIV.NumVersions > eFIV.NumVersions
|
2020-10-28 12:18:35 -04:00
|
|
|
})
|
|
|
|
if entries.len() > o.Limit {
|
|
|
|
allAtEOF = false
|
|
|
|
entries.truncate(o.Limit)
|
|
|
|
}
|
|
|
|
}(len(errs), set)
|
|
|
|
errs = append(errs, nil)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mu.Unlock()
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if isAllNotFound(errs) {
|
|
|
|
// All sets returned not found.
|
2020-11-18 13:28:22 -05:00
|
|
|
go func() {
|
|
|
|
// Update master cache with that information.
|
|
|
|
cache.status = scanStateSuccess
|
|
|
|
cache.fileNotFound = true
|
|
|
|
o.updateMetacacheListing(cache, globalNotificationSys.restClientFromHash(o.Bucket))
|
|
|
|
}()
|
2020-11-03 11:53:48 -05:00
|
|
|
// cache returned not found, entries truncated.
|
|
|
|
return entries, io.EOF
|
2020-10-28 12:18:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, err := range errs {
|
|
|
|
if err == nil {
|
|
|
|
allAtEOF = false
|
|
|
|
continue
|
|
|
|
}
|
2020-10-28 16:19:53 -04:00
|
|
|
if err.Error() == io.EOF.Error() {
|
2020-10-28 12:18:35 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return entries, err
|
|
|
|
}
|
|
|
|
truncated := entries.len() > o.Limit || !allAtEOF
|
|
|
|
entries.truncate(o.Limit)
|
2020-12-15 14:25:36 -05:00
|
|
|
if !o.discardResult {
|
|
|
|
entries.listID = o.ID
|
|
|
|
}
|
2020-10-28 12:18:35 -04:00
|
|
|
if !truncated {
|
|
|
|
return entries, io.EOF
|
|
|
|
}
|
|
|
|
return entries, nil
|
|
|
|
}
|