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/>.
2016-06-01 19:43:31 -04:00
2016-08-18 19:23:42 -04:00
package cmd
2016-05-20 23:48:47 -04:00
import (
2021-03-29 20:00:55 -04:00
"bytes"
2018-03-14 15:01:47 -04:00
"context"
2024-06-10 11:31:51 -04:00
"encoding/base64"
2021-01-12 01:36:51 -05:00
"errors"
2020-03-02 19:29:30 -05:00
"fmt"
2016-05-20 23:48:47 -04:00
"io"
2018-09-20 22:22:09 -04:00
"net/http"
2016-05-20 23:48:47 -04:00
"path"
2022-10-27 12:05:24 -04:00
"runtime"
2021-05-27 16:38:04 -04:00
"strconv"
2020-11-12 15:12:09 -05:00
"strings"
2020-03-11 11:56:36 -04:00
"sync"
2023-04-19 10:47:42 -04:00
"time"
2016-05-20 23:48:47 -04:00
2022-01-14 13:01:25 -05:00
"github.com/klauspost/readahead"
2023-06-19 20:53:08 -04:00
"github.com/minio/madmin-go/v3"
2020-07-14 12:38:05 -04:00
"github.com/minio/minio-go/v7/pkg/tags"
2021-06-01 17:59:40 -04:00
"github.com/minio/minio/internal/bucket/lifecycle"
2022-04-11 16:25:32 -04:00
"github.com/minio/minio/internal/bucket/object/lock"
2021-06-01 17:59:40 -04:00
"github.com/minio/minio/internal/bucket/replication"
2024-02-28 20:44:30 -05:00
"github.com/minio/minio/internal/config/storageclass"
2023-06-15 15:43:26 -04:00
"github.com/minio/minio/internal/crypto"
2021-06-01 17:59:40 -04:00
"github.com/minio/minio/internal/event"
2024-04-01 19:42:09 -04:00
"github.com/minio/minio/internal/grid"
2021-06-01 17:59:40 -04:00
"github.com/minio/minio/internal/hash"
xhttp "github.com/minio/minio/internal/http"
xioutil "github.com/minio/minio/internal/ioutil"
"github.com/minio/minio/internal/logger"
2024-05-24 19:05:23 -04:00
"github.com/minio/pkg/v3/mimedb"
"github.com/minio/pkg/v3/sync/errgroup"
2024-09-11 14:35:37 -04:00
"github.com/minio/sio"
2016-05-20 23:48:47 -04:00
)
2016-11-20 19:57:12 -05:00
// list all errors which can be ignored in object operations.
2023-11-22 16:46:17 -05:00
var objectOpIgnoredErrs = append ( baseIgnoredErrs , errDiskAccessDenied , errUnformattedDisk , errDiskOngoingReq )
2016-11-20 19:57:12 -05:00
2021-11-16 12:28:29 -05:00
// Object Operations
2016-05-20 23:48:47 -04:00
2021-03-24 17:19:52 -04:00
func countOnlineDisks ( onlineDisks [ ] StorageAPI ) ( online int ) {
for _ , onlineDisk := range onlineDisks {
if onlineDisk != nil && onlineDisk . IsOnline ( ) {
online ++
}
}
return online
}
2016-12-26 19:29:26 -05:00
// CopyObject - copy object source object to destination object.
// if source object and destination object are same we only
// update metadata.
2021-03-03 21:36:43 -05:00
func ( er erasureObjects ) CopyObject ( ctx context . Context , srcBucket , srcObject , dstBucket , dstObject string , srcInfo ObjectInfo , srcOpts , dstOpts ObjectOptions ) ( oi ObjectInfo , err error ) {
2024-03-06 06:43:16 -05:00
if ! dstOpts . NoAuditLog {
2024-08-13 18:22:04 -04:00
auditObjectErasureSet ( ctx , "CopyObject" , dstObject , & er )
2024-03-06 06:43:16 -05:00
}
2022-05-04 03:45:27 -04:00
2020-08-03 19:21:10 -04:00
// This call shouldn't be used for anything other than metadata updates or adding self referential versions.
2020-05-28 17:36:38 -04:00
if ! srcInfo . metadataOnly {
return oi , NotImplemented { }
}
2020-10-28 03:09:15 -04:00
2021-04-19 13:30:42 -04:00
if ! dstOpts . NoLock {
lk := er . NewNSLock ( dstBucket , dstObject )
2021-04-29 23:55:21 -04:00
lkctx , err := lk . GetLock ( ctx , globalOperationTimeout )
2021-04-19 13:30:42 -04:00
if err != nil {
return oi , err
}
2021-04-29 23:55:21 -04:00
ctx = lkctx . Context ( )
2022-12-23 22:49:07 -05:00
defer lk . Unlock ( lkctx )
2020-09-15 23:44:48 -04:00
}
2020-05-28 17:36:38 -04:00
// Read metadata associated with the object from all disks.
2020-06-12 23:04:01 -04:00
storageDisks := er . getDisks ( )
2016-12-26 19:29:26 -05:00
2022-04-20 15:49:05 -04:00
var metaArr [ ] FileInfo
var errs [ ] error
// Read metadata associated with the object from all disks.
if srcOpts . VersionID != "" {
2024-01-30 15:43:25 -05:00
metaArr , errs = readAllFileInfo ( ctx , storageDisks , "" , srcBucket , srcObject , srcOpts . VersionID , true , false )
2022-04-20 15:49:05 -04:00
} else {
2024-08-08 11:29:58 -04:00
metaArr , errs = readAllXL ( ctx , storageDisks , srcBucket , srcObject , true , false )
2022-04-20 15:49:05 -04:00
}
2021-01-16 15:08:02 -05:00
readQuorum , writeQuorum , err := objectQuorumFromMeta ( ctx , metaArr , errs , er . defaultParityCount )
2020-05-28 17:36:38 -04:00
if err != nil {
2022-04-20 15:49:05 -04:00
if errors . Is ( err , errErasureReadQuorum ) && ! strings . HasPrefix ( srcBucket , minioMetaBucket ) {
2023-12-04 17:54:34 -05:00
_ , derr := er . deleteIfDangling ( context . Background ( ) , srcBucket , srcObject , metaArr , errs , nil , srcOpts )
2024-05-09 13:15:03 -04:00
if derr == nil {
if srcOpts . VersionID != "" {
err = errFileVersionNotFound
} else {
err = errFileNotFound
}
2022-04-20 15:49:05 -04:00
}
}
return ObjectInfo { } , toObjectErr ( err , srcBucket , srcObject )
2020-05-28 17:36:38 -04:00
}
2016-12-26 19:29:26 -05:00
2020-05-28 17:36:38 -04:00
// List all online disks.
2023-06-17 22:18:20 -04:00
onlineDisks , modTime , etag := listOnlineDisks ( storageDisks , metaArr , errs , readQuorum )
2016-12-26 19:29:26 -05:00
2020-05-28 17:36:38 -04:00
// Pick latest valid metadata.
2023-06-17 22:18:20 -04:00
fi , err := pickValidFileInfo ( ctx , metaArr , modTime , etag , readQuorum )
2020-05-28 17:36:38 -04:00
if err != nil {
return oi , toObjectErr ( err , srcBucket , srcObject )
}
2020-06-12 23:04:01 -04:00
if fi . Deleted {
if srcOpts . VersionID == "" {
return oi , toObjectErr ( errFileNotFound , srcBucket , srcObject )
}
2022-05-31 05:57:57 -04:00
return fi . ToObjectInfo ( srcBucket , srcObject , srcOpts . Versioned || srcOpts . VersionSuspended ) , toObjectErr ( errMethodNotAllowed , srcBucket , srcObject )
2020-05-28 17:36:38 -04:00
}
2018-03-02 20:24:02 -05:00
2021-11-21 13:41:30 -05:00
filterOnlineDisksInplace ( fi , metaArr , onlineDisks )
2020-08-03 19:21:10 -04:00
versionID := srcInfo . VersionID
if srcInfo . versionOnly {
versionID = dstOpts . VersionID
// preserve destination versionId if specified.
if versionID == "" {
versionID = mustGetUUID ( )
2022-04-20 13:22:05 -04:00
fi . IsLatest = true // we are creating a new version so this is latest.
2020-08-03 19:21:10 -04:00
}
}
2022-04-20 13:22:05 -04:00
2023-10-25 17:30:56 -04:00
modTime = UTCNow ( ) // We only preserve modTime if dstOpts.MTime is true.
// in all other cases mtime is latest.
2020-08-03 19:21:10 -04:00
fi . VersionID = versionID // set any new versionID we might have created
fi . ModTime = modTime // set modTime for the new versionID
2020-11-19 14:50:22 -05:00
if ! dstOpts . MTime . IsZero ( ) {
modTime = dstOpts . MTime
fi . ModTime = dstOpts . MTime
}
2024-04-11 02:42:51 -04:00
// check inline before overwriting metadata.
inlineData := fi . InlineData ( )
2023-10-25 17:30:56 -04:00
2021-01-29 17:49:18 -05:00
fi . Metadata = srcInfo . UserDefined
2020-09-15 23:44:48 -04:00
srcInfo . UserDefined [ "etag" ] = srcInfo . ETag
2022-05-24 20:27:45 -04:00
freeVersionID := fi . TierFreeVersionID ( )
freeVersionMarker := fi . TierFreeVersion ( )
2020-06-12 23:04:01 -04:00
// Update `xl.meta` content on each disks.
for index := range metaArr {
2020-11-20 12:10:48 -05:00
if metaArr [ index ] . IsValid ( ) {
metaArr [ index ] . ModTime = modTime
metaArr [ index ] . VersionID = versionID
2022-04-20 13:22:05 -04:00
if ! metaArr [ index ] . InlineData ( ) {
// If the data is not inlined, we may end up incorrectly
// inlining the data here, that leads to an inconsistent
// situation where some objects are were not inlined
// were now inlined, make sure to `nil` the Data such
// that xl.meta is written as expected.
metaArr [ index ] . Data = nil
}
2022-05-24 20:27:45 -04:00
metaArr [ index ] . Metadata = srcInfo . UserDefined
// Preserve existing values
if inlineData {
metaArr [ index ] . SetInlineData ( )
}
if freeVersionID != "" {
metaArr [ index ] . SetTierFreeVersionID ( freeVersionID )
}
if freeVersionMarker {
metaArr [ index ] . SetTierFreeVersion ( )
}
2020-11-20 12:10:48 -05:00
}
2020-06-12 23:04:01 -04:00
}
2016-12-26 19:29:26 -05:00
2021-04-02 01:12:03 -04:00
// Write unique `xl.meta` for each disk.
2024-01-30 15:43:25 -05:00
if _ , err = writeUniqueFileInfo ( ctx , onlineDisks , "" , srcBucket , srcObject , metaArr , writeQuorum ) ; err != nil {
2021-04-02 01:12:03 -04:00
return oi , toObjectErr ( err , srcBucket , srcObject )
2016-12-26 19:29:26 -05:00
}
2022-05-31 05:57:57 -04:00
return fi . ToObjectInfo ( srcBucket , srcObject , srcOpts . Versioned || srcOpts . VersionSuspended ) , nil
2016-12-26 19:29:26 -05:00
}
2018-09-20 22:22:09 -04:00
// GetObjectNInfo - returns object info and an object
// Read(Closer). When err != nil, the returned reader is always nil.
2023-04-17 15:16:37 -04:00
func ( er erasureObjects ) GetObjectNInfo ( ctx context . Context , bucket , object string , rs * HTTPRangeSpec , h http . Header , opts ObjectOptions ) ( gr * GetObjectReader , err error ) {
2024-03-06 06:43:16 -05:00
if ! opts . NoAuditLog {
2024-08-13 18:22:04 -04:00
auditObjectErasureSet ( ctx , "GetObject" , object , & er )
2024-03-06 06:43:16 -05:00
}
2022-05-04 03:45:27 -04:00
2020-09-14 18:57:13 -04:00
var unlockOnDefer bool
2022-01-02 12:15:06 -05:00
nsUnlocker := func ( ) { }
2020-09-14 18:57:13 -04:00
defer func ( ) {
if unlockOnDefer {
nsUnlocker ( )
}
} ( )
// Acquire lock
2023-04-17 15:16:37 -04:00
if ! opts . NoLock {
2020-11-04 11:25:42 -05:00
lock := er . NewNSLock ( bucket , object )
2023-04-17 15:16:37 -04:00
lkctx , err := lock . GetRLock ( ctx , globalOperationTimeout )
if err != nil {
return nil , err
2020-09-14 18:57:13 -04:00
}
2023-04-17 15:16:37 -04:00
ctx = lkctx . Context ( )
// Release lock when the metadata is verified, and reader
// is ready to be read.
//
// This is possible to be lock free because
// - xl.meta for inlined objects has already read the data
// into memory, any mutation on xl.meta subsequently is
// inconsequential to the overall read operation.
// - xl.meta metadata is still verified for quorum under lock()
// however writing the response doesn't need to serialize
// concurrent writers
2020-09-14 18:57:13 -04:00
unlockOnDefer = true
2023-04-17 15:16:37 -04:00
nsUnlocker = func ( ) { lock . RUnlock ( lkctx ) }
2020-09-14 18:57:13 -04:00
}
2021-01-07 22:27:31 -05:00
fi , metaArr , onlineDisks , err := er . getObjectFileInfo ( ctx , bucket , object , opts , true )
2018-09-20 22:22:09 -04:00
if err != nil {
return nil , toObjectErr ( err , bucket , object )
}
2022-05-31 05:57:57 -04:00
objInfo := fi . ToObjectInfo ( bucket , object , opts . Versioned || opts . VersionSuspended )
2020-07-02 19:17:27 -04:00
if objInfo . DeleteMarker {
if opts . VersionID == "" {
return & GetObjectReader {
ObjInfo : objInfo ,
} , toObjectErr ( errFileNotFound , bucket , object )
}
// Make sure to return object info to provide extra information.
return & GetObjectReader {
ObjInfo : objInfo ,
} , toObjectErr ( errMethodNotAllowed , bucket , object )
}
2023-04-17 15:16:37 -04:00
2024-03-28 13:44:56 -04:00
// Set NoDecryption for SSE-C objects and if replication request
if crypto . SSEC . IsEncrypted ( objInfo . UserDefined ) && opts . ReplicationRequest {
opts . NoDecryption = true
}
2024-05-05 12:56:21 -04:00
if objInfo . Size == 0 {
if _ , _ , err := rs . GetOffsetLength ( objInfo . Size ) ; err != nil {
// Make sure to return object info to provide extra information.
return & GetObjectReader {
ObjInfo : objInfo ,
} , err
}
// Zero byte objects don't even need to further initialize pipes etc.
return NewGetObjectReaderFromReader ( bytes . NewReader ( nil ) , objInfo , opts )
}
2021-05-04 11:40:42 -04:00
if objInfo . IsRemote ( ) {
gr , err := getTransitionedObjectReader ( ctx , bucket , object , rs , h , objInfo , opts )
if err != nil {
return nil , err
2020-11-12 15:12:09 -05:00
}
2021-05-04 11:40:42 -04:00
unlockOnDefer = false
return gr . WithCleanupFuncs ( nsUnlocker ) , nil
2020-11-12 15:12:09 -05:00
}
2021-04-30 21:37:58 -04:00
2024-06-10 11:31:51 -04:00
fn , off , length , err := NewGetObjectReader ( rs , objInfo , opts , h )
2021-04-30 21:37:58 -04:00
if err != nil {
return nil , err
2018-09-20 22:22:09 -04:00
}
2023-04-17 15:16:37 -04:00
if unlockOnDefer {
2024-07-23 06:53:03 -04:00
unlockOnDefer = fi . InlineData ( ) || len ( fi . Data ) > 0
2023-04-17 15:16:37 -04:00
}
2021-05-11 12:18:37 -04:00
pr , pw := xioutil . WaitPipe ( )
2018-09-20 22:22:09 -04:00
go func ( ) {
2021-04-30 21:37:58 -04:00
pw . CloseWithError ( er . getObjectWithFileInfo ( ctx , bucket , object , off , length , pw , fi , metaArr , onlineDisks ) )
2018-09-20 22:22:09 -04:00
} ( )
2020-04-21 01:01:59 -04:00
2018-09-21 14:42:06 -04:00
// Cleanup function to cause the go routine above to exit, in
// case of incomplete read.
2021-05-11 12:18:37 -04:00
pipeCloser := func ( ) {
pr . CloseWithError ( nil )
}
2018-09-20 22:22:09 -04:00
2023-04-17 15:16:37 -04:00
if ! unlockOnDefer {
return fn ( pr , h , pipeCloser , nsUnlocker )
}
return fn ( pr , h , pipeCloser )
2018-09-20 22:22:09 -04:00
}
2020-10-28 12:18:35 -04:00
func ( er erasureObjects ) getObjectWithFileInfo ( ctx context . Context , bucket , object string , startOffset int64 , length int64 , writer io . Writer , fi FileInfo , metaArr [ ] FileInfo , onlineDisks [ ] StorageAPI ) error {
2020-10-28 03:09:15 -04:00
// Reorder online disks based on erasure distribution order.
2016-07-25 01:49:27 -04:00
// Reorder parts metadata based on erasure distribution order.
2021-03-15 23:03:13 -04:00
onlineDisks , metaArr = shuffleDisksAndPartsMetadataByIndex ( onlineDisks , metaArr , fi )
2016-05-25 19:42:31 -04:00
2016-12-21 14:29:32 -05:00
// For negative length read everything.
if length < 0 {
2020-06-12 23:04:01 -04:00
length = fi . Size - startOffset
2016-07-08 10:46:49 -04:00
}
2016-12-21 14:29:32 -05:00
// Reply back invalid range if the input offset and length fall out of range.
2020-06-12 23:04:01 -04:00
if startOffset > fi . Size || startOffset + length > fi . Size {
return InvalidRange { startOffset , length , fi . Size }
2016-07-08 10:46:49 -04:00
}
2016-06-19 16:35:26 -04:00
// Get start part index and offset.
2020-06-12 23:04:01 -04:00
partIndex , partOffset , err := fi . ObjectToPartOffset ( ctx , startOffset )
2016-05-20 23:48:47 -04:00
if err != nil {
2020-06-12 23:04:01 -04:00
return InvalidRange { startOffset , length , fi . Size }
2016-05-20 23:48:47 -04:00
}
2016-05-31 23:23:31 -04:00
2017-01-27 13:51:02 -05:00
// Calculate endOffset according to length
endOffset := startOffset
if length > 0 {
endOffset += length - 1
}
2016-06-19 16:35:26 -04:00
// Get last part index to read given length.
2020-06-12 23:04:01 -04:00
lastPartIndex , _ , err := fi . ObjectToPartOffset ( ctx , endOffset )
2016-06-19 16:35:26 -04:00
if err != nil {
2020-06-12 23:04:01 -04:00
return InvalidRange { startOffset , length , fi . Size }
2016-06-19 16:35:26 -04:00
}
2017-02-24 12:20:40 -05:00
var totalBytesRead int64
2020-06-12 23:04:01 -04:00
erasure , err := NewErasure ( ctx , fi . Erasure . DataBlocks , fi . Erasure . ParityBlocks , fi . Erasure . BlockSize )
2017-08-14 21:08:42 -04:00
if err != nil {
return toObjectErr ( err , bucket , object )
}
2021-05-25 19:33:06 -04:00
Prefer local disks when fetching data blocks (#9563)
If the requested server is part of the set this will always read
from the local disk, even if the disk contains a parity shard.
In default setup there is a 50% chance that at least
one shard that otherwise would have been fetched remotely
will be read locally instead.
It basically trades RPC call overhead for reed-solomon.
On distributed localhost this seems to be fairly break-even,
with a very small gain in throughput and latency.
However on networked servers this should be a bigger
1MB objects, before:
```
Operation: GET. Concurrency: 32. Hosts: 4.
Requests considered: 76257:
* Avg: 25ms 50%: 24ms 90%: 32ms 99%: 42ms Fastest: 7ms Slowest: 67ms
* First Byte: Average: 23ms, Median: 22ms, Best: 5ms, Worst: 65ms
Throughput:
* Average: 1213.68 MiB/s, 1272.63 obj/s (59.948s, starting 14:45:44 CEST)
```
After:
```
Operation: GET. Concurrency: 32. Hosts: 4.
Requests considered: 78845:
* Avg: 24ms 50%: 24ms 90%: 31ms 99%: 39ms Fastest: 8ms Slowest: 62ms
* First Byte: Average: 22ms, Median: 21ms, Best: 6ms, Worst: 57ms
Throughput:
* Average: 1255.11 MiB/s, 1316.08 obj/s (59.938s, starting 14:43:58 CEST)
```
Bonus fix: Only ask for heal once on an object.
2020-05-26 19:47:23 -04:00
var healOnce sync . Once
2020-11-12 15:12:09 -05:00
2016-06-19 16:35:26 -04:00
for ; partIndex <= lastPartIndex ; partIndex ++ {
2016-06-21 17:34:11 -04:00
if length == totalBytesRead {
break
}
2020-03-02 19:29:30 -05:00
2020-06-12 23:04:01 -04:00
partNumber := fi . Parts [ partIndex ] . Number
2020-03-02 19:29:30 -05:00
2016-05-31 23:23:31 -04:00
// Save the current part name and size.
2020-06-12 23:04:01 -04:00
partSize := fi . Parts [ partIndex ] . Size
2016-06-22 12:05:03 -04:00
2018-08-06 18:14:08 -04:00
partLength := partSize - partOffset
// partLength should be adjusted so that we don't write more data than what was requested.
if partLength > ( length - totalBytesRead ) {
partLength = length - totalBytesRead
2016-06-19 16:35:26 -04:00
}
2016-05-31 23:23:31 -04:00
2020-06-12 23:04:01 -04:00
tillOffset := erasure . ShardFileOffset ( partOffset , partLength , partSize )
2016-07-16 11:35:30 -04:00
// Get the checksums of the current part.
2019-01-17 07:58:18 -05:00
readers := make ( [ ] io . ReaderAt , len ( onlineDisks ) )
Prefer local disks when fetching data blocks (#9563)
If the requested server is part of the set this will always read
from the local disk, even if the disk contains a parity shard.
In default setup there is a 50% chance that at least
one shard that otherwise would have been fetched remotely
will be read locally instead.
It basically trades RPC call overhead for reed-solomon.
On distributed localhost this seems to be fairly break-even,
with a very small gain in throughput and latency.
However on networked servers this should be a bigger
1MB objects, before:
```
Operation: GET. Concurrency: 32. Hosts: 4.
Requests considered: 76257:
* Avg: 25ms 50%: 24ms 90%: 32ms 99%: 42ms Fastest: 7ms Slowest: 67ms
* First Byte: Average: 23ms, Median: 22ms, Best: 5ms, Worst: 65ms
Throughput:
* Average: 1213.68 MiB/s, 1272.63 obj/s (59.948s, starting 14:45:44 CEST)
```
After:
```
Operation: GET. Concurrency: 32. Hosts: 4.
Requests considered: 78845:
* Avg: 24ms 50%: 24ms 90%: 31ms 99%: 39ms Fastest: 8ms Slowest: 62ms
* First Byte: Average: 22ms, Median: 21ms, Best: 6ms, Worst: 57ms
Throughput:
* Average: 1255.11 MiB/s, 1316.08 obj/s (59.938s, starting 14:43:58 CEST)
```
Bonus fix: Only ask for heal once on an object.
2020-05-26 19:47:23 -04:00
prefer := make ( [ ] bool , len ( onlineDisks ) )
2018-08-06 18:14:08 -04:00
for index , disk := range onlineDisks {
2017-08-14 21:08:42 -04:00
if disk == OfflineDisk {
2016-07-25 01:49:27 -04:00
continue
}
2020-11-20 12:10:48 -05:00
if ! metaArr [ index ] . IsValid ( ) {
continue
}
2023-06-18 21:20:15 -04:00
if ! metaArr [ index ] . Erasure . Equal ( fi . Erasure ) {
continue
}
2020-03-02 19:29:30 -05:00
checksumInfo := metaArr [ index ] . Erasure . GetChecksumInfo ( partNumber )
2023-06-17 22:18:20 -04:00
partPath := pathJoin ( object , metaArr [ index ] . DataDir , fmt . Sprintf ( "part.%d" , partNumber ) )
2021-05-14 19:50:47 -04:00
readers [ index ] = newBitrotReader ( disk , metaArr [ index ] . Data , bucket , partPath , tillOffset ,
2020-03-02 19:29:30 -05:00
checksumInfo . Algorithm , checksumInfo . Hash , erasure . ShardSize ( ) )
Prefer local disks when fetching data blocks (#9563)
If the requested server is part of the set this will always read
from the local disk, even if the disk contains a parity shard.
In default setup there is a 50% chance that at least
one shard that otherwise would have been fetched remotely
will be read locally instead.
It basically trades RPC call overhead for reed-solomon.
On distributed localhost this seems to be fairly break-even,
with a very small gain in throughput and latency.
However on networked servers this should be a bigger
1MB objects, before:
```
Operation: GET. Concurrency: 32. Hosts: 4.
Requests considered: 76257:
* Avg: 25ms 50%: 24ms 90%: 32ms 99%: 42ms Fastest: 7ms Slowest: 67ms
* First Byte: Average: 23ms, Median: 22ms, Best: 5ms, Worst: 65ms
Throughput:
* Average: 1213.68 MiB/s, 1272.63 obj/s (59.948s, starting 14:45:44 CEST)
```
After:
```
Operation: GET. Concurrency: 32. Hosts: 4.
Requests considered: 78845:
* Avg: 24ms 50%: 24ms 90%: 31ms 99%: 39ms Fastest: 8ms Slowest: 62ms
* First Byte: Average: 22ms, Median: 21ms, Best: 6ms, Worst: 57ms
Throughput:
* Average: 1255.11 MiB/s, 1316.08 obj/s (59.938s, starting 14:43:58 CEST)
```
Bonus fix: Only ask for heal once on an object.
2020-05-26 19:47:23 -04:00
// Prefer local disks
prefer [ index ] = disk . Hostname ( ) == ""
2016-07-16 11:35:30 -04:00
}
2021-01-27 13:21:14 -05:00
written , err := erasure . Decode ( ctx , writer , readers , partOffset , partLength , partSize , prefer )
2020-06-12 23:04:01 -04:00
// Note: we should not be defer'ing the following closeBitrotReaders() call as
// we are inside a for loop i.e if we use defer, we would accumulate a lot of open files by the time
2019-01-17 07:58:18 -05:00
// we return from this function.
closeBitrotReaders ( readers )
2016-05-31 23:23:31 -04:00
if err != nil {
2021-01-27 13:21:14 -05:00
// If we have successfully written all the content that was asked
// by the client, but we still see an error - this would mean
// that we have some parts or data blocks missing or corrupted
// - attempt a heal to successfully heal them for future calls.
if written == partLength {
2024-08-13 18:26:05 -04:00
if errors . Is ( err , errFileNotFound ) || errors . Is ( err , errFileCorrupt ) {
2021-01-27 13:21:14 -05:00
healOnce . Do ( func ( ) {
2024-08-13 18:26:05 -04:00
globalMRFState . addPartialOp ( PartialOperation {
Bucket : bucket ,
Object : object ,
VersionID : fi . VersionID ,
Queued : time . Now ( ) ,
SetIndex : er . setIndex ,
PoolIndex : er . poolIndex ,
BitrotScan : errors . Is ( err , errFileCorrupt ) ,
2023-08-09 15:51:47 -04:00
} )
2021-01-27 13:21:14 -05:00
} )
2021-10-13 22:49:14 -04:00
// Healing is triggered and we have written
// successfully the content to client for
// the specific part, we should `nil` this error
// and proceed forward, instead of throwing errors.
err = nil
2021-01-27 13:21:14 -05:00
}
2020-04-01 15:14:00 -04:00
}
if err != nil {
return toObjectErr ( err , bucket , object )
}
2016-05-31 23:23:31 -04:00
}
2016-07-28 05:20:34 -04:00
// Track total bytes read from disk and written to the client.
2018-08-06 18:14:08 -04:00
totalBytesRead += partLength
2016-06-22 12:05:03 -04:00
// partOffset will be valid only for the first part, hence reset it to 0 for
// the remaining parts.
2016-05-31 23:23:31 -04:00
partOffset = 0
2016-06-01 19:43:31 -04:00
} // End of read all parts loop.
2016-05-31 23:23:31 -04:00
// Return success.
2016-05-28 18:13:15 -04:00
return nil
2016-05-20 23:48:47 -04:00
}
2016-06-01 19:43:31 -04:00
// GetObjectInfo - reads object metadata and replies back ObjectInfo.
2020-06-12 23:04:01 -04:00
func ( er erasureObjects ) GetObjectInfo ( ctx context . Context , bucket , object string , opts ObjectOptions ) ( info ObjectInfo , err error ) {
2024-03-06 06:43:16 -05:00
if ! opts . NoAuditLog {
2024-08-13 18:22:04 -04:00
auditObjectErasureSet ( ctx , "GetObjectInfo" , object , & er )
2024-03-06 06:43:16 -05:00
}
2022-05-04 03:45:27 -04:00
2021-02-16 05:43:47 -05:00
if ! opts . NoLock {
// Lock the object before reading.
lk := er . NewNSLock ( bucket , object )
2021-04-29 23:55:21 -04:00
lkctx , err := lk . GetRLock ( ctx , globalOperationTimeout )
2021-03-03 21:36:43 -05:00
if err != nil {
2021-02-16 05:43:47 -05:00
return ObjectInfo { } , err
}
2021-04-29 23:55:21 -04:00
ctx = lkctx . Context ( )
2022-12-23 22:49:07 -05:00
defer lk . RUnlock ( lkctx )
2020-09-14 18:57:13 -04:00
}
2020-07-02 19:17:27 -04:00
return er . getObjectInfo ( ctx , bucket , object , opts )
2016-05-20 23:48:47 -04:00
}
2024-08-13 18:22:04 -04:00
func auditDanglingObjectDeletion ( ctx context . Context , bucket , object , versionID string , tags map [ string ] string ) {
2022-10-24 14:35:07 -04:00
if len ( logger . AuditTargets ( ) ) == 0 {
return
}
opts := AuditLogOptions {
Event : "DeleteDanglingObject" ,
Bucket : bucket ,
Object : object ,
VersionID : versionID ,
Tags : tags ,
}
auditLogInternal ( ctx , opts )
}
2024-08-13 18:22:04 -04:00
func joinErrs ( errs [ ] error ) string {
var s string
2023-12-28 01:27:04 -05:00
for i := range s {
2024-08-13 18:22:04 -04:00
if s != "" {
s += ","
}
2023-12-28 01:27:04 -05:00
if errs [ i ] == nil {
2024-08-13 18:22:04 -04:00
s += "<nil>"
2023-12-28 01:27:04 -05:00
} else {
2024-08-13 18:22:04 -04:00
s += errs [ i ] . Error ( )
2023-10-13 18:14:16 -04:00
}
}
2023-12-28 01:27:04 -05:00
return s
2023-10-13 18:14:16 -04:00
}
2024-06-10 11:51:27 -04:00
func ( er erasureObjects ) deleteIfDangling ( ctx context . Context , bucket , object string , metaArr [ ] FileInfo , errs [ ] error , dataErrsByPart map [ int ] [ ] int , opts ObjectOptions ) ( FileInfo , error ) {
m , ok := isObjectDangling ( metaArr , errs , dataErrsByPart )
2024-05-09 13:15:03 -04:00
if ! ok {
// We only come here if we cannot figure out if the object
// can be deleted safely, in such a scenario return ReadQuorum error.
return FileInfo { } , errErasureReadQuorum
}
2024-08-13 18:22:04 -04:00
tags := make ( map [ string ] string , 16 )
tags [ "set" ] = strconv . Itoa ( er . setIndex )
tags [ "pool" ] = strconv . Itoa ( er . poolIndex )
2024-05-09 13:15:03 -04:00
tags [ "merrs" ] = joinErrs ( errs )
2024-08-13 18:22:04 -04:00
tags [ "derrs" ] = fmt . Sprintf ( "%v" , dataErrsByPart )
2024-05-09 13:15:03 -04:00
if m . IsValid ( ) {
2024-08-13 18:22:04 -04:00
tags [ "sz" ] = strconv . FormatInt ( m . Size , 10 )
tags [ "mt" ] = m . ModTime . Format ( iso8601Format )
tags [ "d:p" ] = fmt . Sprintf ( "%d:%d" , m . Erasure . DataBlocks , m . Erasure . ParityBlocks )
2024-05-09 13:15:03 -04:00
} else {
2024-08-13 18:22:04 -04:00
tags [ "invalid" ] = "1"
tags [ "d:p" ] = fmt . Sprintf ( "%d:%d" , er . setDriveCount - er . defaultParityCount , er . defaultParityCount )
2024-05-09 13:15:03 -04:00
}
2023-12-28 01:27:04 -05:00
2024-05-09 13:15:03 -04:00
// count the number of offline disks
offline := 0
2024-06-10 11:51:27 -04:00
for i := 0 ; i < len ( errs ) ; i ++ {
var found bool
switch {
case errors . Is ( errs [ i ] , errDiskNotFound ) :
found = true
default :
for p := range dataErrsByPart {
if dataErrsByPart [ p ] [ i ] == checkPartDiskNotFound {
found = true
break
}
}
}
if found {
2024-05-09 13:15:03 -04:00
offline ++
2023-09-29 12:08:24 -04:00
}
2024-05-09 13:15:03 -04:00
}
if offline > 0 {
2024-08-13 18:22:04 -04:00
tags [ "offline" ] = strconv . Itoa ( offline )
2024-05-09 13:15:03 -04:00
}
2023-09-29 12:08:24 -04:00
2024-05-09 13:15:03 -04:00
_ , file , line , cok := runtime . Caller ( 1 )
if cok {
tags [ "caller" ] = fmt . Sprintf ( "%s:%d" , file , line )
}
2022-10-27 12:05:24 -04:00
2024-05-09 13:15:03 -04:00
defer auditDanglingObjectDeletion ( ctx , bucket , object , m . VersionID , tags )
2022-10-24 14:35:07 -04:00
2024-05-09 13:15:03 -04:00
fi := FileInfo {
VersionID : m . VersionID ,
}
if opts . VersionID != "" {
fi . VersionID = opts . VersionID
}
fi . SetTierFreeVersionID ( mustGetUUID ( ) )
disks := er . getDisks ( )
g := errgroup . WithNErrs ( len ( disks ) )
for index := range disks {
index := index
g . Go ( func ( ) error {
if disks [ index ] == nil {
return errDiskNotFound
}
return disks [ index ] . DeleteVersion ( ctx , bucket , object , fi , false , DeleteOptions { } )
} , index )
}
2022-02-08 23:08:23 -05:00
2024-05-09 13:15:03 -04:00
for index , err := range g . Wait ( ) {
2024-08-13 18:22:04 -04:00
var errStr string
2024-05-09 13:15:03 -04:00
if err != nil {
errStr = err . Error ( )
} else {
errStr = "<nil>"
2022-02-08 23:08:23 -05:00
}
2024-08-13 18:22:04 -04:00
tags [ fmt . Sprintf ( "ddisk-%d" , index ) ] = errStr
2022-02-08 23:08:23 -05:00
}
2024-08-13 18:22:04 -04:00
2024-05-09 13:15:03 -04:00
return m , nil
2022-02-08 23:08:23 -05:00
}
2024-08-08 11:29:58 -04:00
func fileInfoFromRaw ( ri RawFileInfo , bucket , object string , readData , inclFreeVers bool ) ( FileInfo , error ) {
2024-06-17 10:29:18 -04:00
return getFileInfo ( ri . Buf , bucket , object , "" , fileInfoOpts {
Data : readData ,
InclFreeVersions : inclFreeVers ,
} )
2023-11-22 16:46:17 -05:00
}
func readAllRawFileInfo ( ctx context . Context , disks [ ] StorageAPI , bucket , object string , readData bool ) ( [ ] RawFileInfo , [ ] error ) {
rawFileInfos := make ( [ ] RawFileInfo , len ( disks ) )
2022-04-20 15:49:05 -04:00
g := errgroup . WithNErrs ( len ( disks ) )
for index := range disks {
index := index
g . Go ( func ( ) ( err error ) {
if disks [ index ] == nil {
return errDiskNotFound
}
2024-01-30 21:11:45 -05:00
rf , err := disks [ index ] . ReadXL ( ctx , bucket , object , readData )
2022-04-20 15:49:05 -04:00
if err != nil {
return err
}
2023-11-22 16:46:17 -05:00
rawFileInfos [ index ] = rf
2022-04-20 15:49:05 -04:00
return nil
} , index )
}
2023-11-22 16:46:17 -05:00
return rawFileInfos , g . Wait ( )
}
2024-08-08 11:29:58 -04:00
func pickLatestQuorumFilesInfo ( ctx context . Context , rawFileInfos [ ] RawFileInfo , errs [ ] error , bucket , object string , readData , inclFreeVers bool ) ( [ ] FileInfo , [ ] error ) {
2023-11-22 16:46:17 -05:00
metadataArray := make ( [ ] * xlMetaV2 , len ( rawFileInfos ) )
metaFileInfos := make ( [ ] FileInfo , len ( rawFileInfos ) )
metadataShallowVersions := make ( [ ] [ ] xlMetaV2ShallowVersion , len ( rawFileInfos ) )
var v2bufs [ ] [ ] byte
if ! readData {
v2bufs = make ( [ ] [ ] byte , len ( rawFileInfos ) )
2022-09-02 15:47:17 -04:00
}
2023-11-22 16:46:17 -05:00
// Read `xl.meta` in parallel across disks.
for index := range rawFileInfos {
rf := rawFileInfos [ index ]
if rf . Buf == nil {
2022-06-06 20:33:41 -04:00
continue
}
2023-11-22 16:46:17 -05:00
if ! readData {
// Save the buffer so we can reuse it.
v2bufs [ index ] = rf . Buf
}
var xl xlMetaV2
if err := xl . LoadOrConvert ( rf . Buf ) ; err != nil {
errs [ index ] = err
continue
}
metadataArray [ index ] = & xl
2024-02-05 04:04:26 -05:00
metaFileInfos [ index ] = FileInfo { }
2022-06-06 18:15:11 -04:00
}
2022-04-20 15:49:05 -04:00
for index := range metadataArray {
if metadataArray [ index ] != nil {
metadataShallowVersions [ index ] = metadataArray [ index ] . versions
}
}
2023-11-22 16:46:17 -05:00
readQuorum := ( len ( rawFileInfos ) + 1 ) / 2
2022-05-30 15:43:54 -04:00
meta := & xlMetaV2 { versions : mergeXLV2Versions ( readQuorum , false , 1 , metadataShallowVersions ... ) }
2024-08-08 11:29:58 -04:00
lfi , err := meta . ToFileInfo ( bucket , object , "" , inclFreeVers , true )
2022-05-30 15:43:54 -04:00
if err != nil {
for i := range errs {
if errs [ i ] == nil {
errs [ i ] = err
}
}
return metaFileInfos , errs
}
if ! lfi . IsValid ( ) {
for i := range errs {
if errs [ i ] == nil {
2024-04-28 13:53:50 -04:00
errs [ i ] = errFileCorrupt
2022-05-30 15:43:54 -04:00
}
}
return metaFileInfos , errs
}
2022-07-29 13:03:53 -04:00
versionID := lfi . VersionID
if versionID == "" {
versionID = nullVersionID
}
2022-04-20 15:49:05 -04:00
for index := range metadataArray {
if metadataArray [ index ] == nil {
continue
}
// make sure to preserve this for diskmtime based healing bugfix.
2024-08-08 11:29:58 -04:00
metaFileInfos [ index ] , errs [ index ] = metadataArray [ index ] . ToFileInfo ( bucket , object , versionID , inclFreeVers , true )
2022-05-30 15:43:54 -04:00
if errs [ index ] != nil {
continue
}
2022-07-29 13:03:53 -04:00
if readData {
metaFileInfos [ index ] . Data = metadataArray [ index ] . data . find ( versionID )
2022-04-20 15:49:05 -04:00
}
}
2023-07-06 19:02:08 -04:00
if ! readData {
for i := range v2bufs {
metaDataPoolPut ( v2bufs [ i ] )
}
}
2022-04-20 15:49:05 -04:00
// Return all the metadata.
return metaFileInfos , errs
}
2023-11-16 11:39:53 -05:00
// Checking if an object is dangling costs some IOPS; hence implementing this function
// which decides which condition it is useful to check if an object is dangling
//
// errs: errors from reading xl.meta in all disks
// err: reduced errs
// bucket: the object name in question
func shouldCheckForDangling ( err error , errs [ ] error , bucket string ) bool {
// Avoid data in .minio.sys for now
if bucket == minioMetaBucket {
return false
}
switch {
// Check if we have a read quorum issue
case errors . Is ( err , errErasureReadQuorum ) :
return true
// Check if the object is inexistent in most disks but not all of them
case errors . Is ( err , errFileNotFound ) || errors . Is ( err , errFileVersionNotFound ) :
for i := range errs {
if errs [ i ] == nil {
return true
}
}
}
return false
}
2024-08-08 11:29:58 -04:00
func readAllXL ( ctx context . Context , disks [ ] StorageAPI , bucket , object string , readData , inclFreeVers bool ) ( [ ] FileInfo , [ ] error ) {
2023-11-22 16:46:17 -05:00
rawFileInfos , errs := readAllRawFileInfo ( ctx , disks , bucket , object , readData )
2024-08-08 11:29:58 -04:00
return pickLatestQuorumFilesInfo ( ctx , rawFileInfos , errs , bucket , object , readData , inclFreeVers )
2023-11-22 16:46:17 -05:00
}
func ( er erasureObjects ) getObjectFileInfo ( ctx context . Context , bucket , object string , opts ObjectOptions , readData bool ) ( FileInfo , [ ] FileInfo , [ ] StorageAPI , error ) {
rawArr := make ( [ ] RawFileInfo , er . setDriveCount )
metaArr := make ( [ ] FileInfo , er . setDriveCount )
errs := make ( [ ] error , er . setDriveCount )
for i := range errs {
errs [ i ] = errDiskOngoingReq
}
done := make ( chan bool , er . setDriveCount )
2020-06-12 23:04:01 -04:00
disks := er . getDisks ( )
2018-10-19 14:00:09 -04:00
2023-11-22 16:46:17 -05:00
ropts := ReadOptions {
2024-06-17 10:29:18 -04:00
ReadData : readData ,
InclFreeVersions : opts . InclFreeVersions ,
Healing : false ,
2023-11-22 16:46:17 -05:00
}
2024-01-24 04:08:23 -05:00
mrfCheck := make ( chan FileInfo )
2024-01-28 13:04:17 -05:00
defer xioutil . SafeClose ( mrfCheck )
2024-01-24 04:08:23 -05:00
var rw sync . Mutex
2023-11-22 16:46:17 -05:00
// Ask for all disks first;
go func ( ) {
ctx , cancel := context . WithCancel ( ctx )
defer cancel ( )
wg := sync . WaitGroup { }
for i , disk := range disks {
if disk == nil {
done <- false
continue
}
2024-01-24 04:08:23 -05:00
if ! disk . IsOnline ( ) {
done <- false
continue
}
2023-11-22 16:46:17 -05:00
wg . Add ( 1 )
go func ( i int , disk StorageAPI ) {
defer wg . Done ( )
2024-01-24 04:08:23 -05:00
var (
fi FileInfo
rfi RawFileInfo
err error
)
2023-11-22 16:46:17 -05:00
if opts . VersionID != "" {
// Read a specific version ID
2024-01-30 21:11:45 -05:00
fi , err = disk . ReadVersion ( ctx , "" , bucket , object , opts . VersionID , ropts )
2023-11-22 16:46:17 -05:00
} else {
// Read the latest version
2024-01-30 21:11:45 -05:00
rfi , err = disk . ReadXL ( ctx , bucket , object , readData )
2023-11-22 16:46:17 -05:00
if err == nil {
2024-08-08 11:29:58 -04:00
fi , err = fileInfoFromRaw ( rfi , bucket , object , readData , opts . InclFreeVersions )
2023-11-22 16:46:17 -05:00
}
}
2024-01-24 04:08:23 -05:00
rw . Lock ( )
rawArr [ i ] = rfi
metaArr [ i ] , errs [ i ] = fi , err
rw . Unlock ( )
2023-11-22 16:46:17 -05:00
done <- err == nil
} ( i , disk )
}
wg . Wait ( )
2024-01-28 13:04:17 -05:00
xioutil . SafeClose ( done )
2023-11-22 16:46:17 -05:00
fi , ok := <- mrfCheck
if ! ok {
return
}
2024-01-24 04:08:23 -05:00
2023-11-22 16:46:17 -05:00
if fi . Deleted {
return
}
2024-01-24 04:08:23 -05:00
2023-11-22 16:46:17 -05:00
// if one of the disk is offline, return right here no need
// to attempt a heal on the object.
if countErrs ( errs , errDiskNotFound ) > 0 {
return
}
2024-01-24 04:08:23 -05:00
2023-11-22 16:46:17 -05:00
var missingBlocks int
for i := range errs {
2024-01-30 21:11:45 -05:00
if IsErr ( errs [ i ] ,
errFileNotFound ,
errFileVersionNotFound ,
errFileCorrupt ,
) {
2023-11-22 16:46:17 -05:00
missingBlocks ++
}
}
2024-01-24 04:08:23 -05:00
2023-11-22 16:46:17 -05:00
// if missing metadata can be reconstructed, attempt to reconstruct.
// additionally do not heal delete markers inline, let them be
// healed upon regular heal process.
if missingBlocks > 0 && missingBlocks < fi . Erasure . DataBlocks {
2024-08-13 18:26:05 -04:00
globalMRFState . addPartialOp ( PartialOperation {
Bucket : fi . Volume ,
Object : fi . Name ,
VersionID : fi . VersionID ,
Queued : time . Now ( ) ,
SetIndex : er . setIndex ,
PoolIndex : er . poolIndex ,
2023-11-22 16:46:17 -05:00
} )
}
return
} ( )
validResp := 0
totalResp := 0
// minDisks value is only to reduce the number of calls
// to the disks; this value is not accurate because we do
// not know the storage class of the object yet
minDisks := 0
if p := globalStorageClass . GetParityForSC ( "" ) ; p > - 1 {
minDisks = er . setDriveCount - p
2022-04-20 15:49:05 -04:00
} else {
2023-11-22 16:46:17 -05:00
minDisks = er . setDriveCount - er . defaultParityCount
2022-04-20 15:49:05 -04:00
}
2018-10-19 14:00:09 -04:00
2024-01-24 04:08:23 -05:00
calcQuorum := func ( metaArr [ ] FileInfo , errs [ ] error ) ( FileInfo , [ ] FileInfo , [ ] StorageAPI , time . Time , string , error ) {
2023-11-22 16:46:17 -05:00
readQuorum , _ , err := objectQuorumFromMeta ( ctx , metaArr , errs , er . defaultParityCount )
if err != nil {
return FileInfo { } , nil , nil , time . Time { } , "" , err
}
2024-01-24 04:08:23 -05:00
if err := reduceReadQuorumErrs ( ctx , errs , objectOpIgnoredErrs , readQuorum ) ; err != nil {
2023-11-22 16:46:17 -05:00
return FileInfo { } , nil , nil , time . Time { } , "" , err
}
onlineDisks , modTime , etag := listOnlineDisks ( disks , metaArr , errs , readQuorum )
fi , err := pickValidFileInfo ( ctx , metaArr , modTime , etag , readQuorum )
if err != nil {
return FileInfo { } , nil , nil , time . Time { } , "" , err
}
onlineMeta := make ( [ ] FileInfo , len ( metaArr ) )
2024-01-24 04:08:23 -05:00
for i , disk := range onlineDisks {
if disk != nil {
onlineMeta [ i ] = metaArr [ i ]
}
}
2023-11-22 16:46:17 -05:00
return fi , onlineMeta , onlineDisks , modTime , etag , nil
}
var (
modTime time . Time
etag string
fi FileInfo
onlineMeta [ ] FileInfo
onlineDisks [ ] StorageAPI
err error
)
for success := range done {
totalResp ++
if success {
validResp ++
}
2024-05-15 12:52:29 -04:00
if totalResp >= minDisks && opts . FastGetObjInfo {
rw . Lock ( )
ok := countErrs ( errs , errFileNotFound ) >= minDisks || countErrs ( errs , errFileVersionNotFound ) >= minDisks
rw . Unlock ( )
if ok {
err = errFileNotFound
if opts . VersionID != "" {
err = errFileVersionNotFound
}
break
}
}
2023-11-22 16:46:17 -05:00
if totalResp < er . setDriveCount {
if ! opts . FastGetObjInfo {
continue
}
if validResp < minDisks {
continue
2022-02-08 23:08:23 -05:00
}
}
2024-01-24 04:08:23 -05:00
rw . Lock ( )
2024-07-23 06:53:03 -04:00
// when its a versioned bucket and empty versionID - at totalResp == setDriveCount
// we must use rawFileInfo to resolve versions to figure out the latest version.
2023-11-22 16:46:17 -05:00
if opts . VersionID == "" && totalResp == er . setDriveCount {
2024-01-24 04:08:23 -05:00
fi , onlineMeta , onlineDisks , modTime , etag , err = calcQuorum ( pickLatestQuorumFilesInfo ( ctx ,
2024-08-08 11:29:58 -04:00
rawArr , errs , bucket , object , readData , opts . InclFreeVersions ) )
2024-01-24 04:08:23 -05:00
} else {
fi , onlineMeta , onlineDisks , modTime , etag , err = calcQuorum ( metaArr , errs )
2023-11-22 16:46:17 -05:00
}
2024-01-24 04:08:23 -05:00
rw . Unlock ( )
2024-07-23 06:53:03 -04:00
if err == nil && ( fi . InlineData ( ) || len ( fi . Data ) > 0 ) {
2023-11-22 16:46:17 -05:00
break
}
2018-02-01 13:47:49 -05:00
}
2023-11-22 16:46:17 -05:00
if err != nil {
2024-01-24 04:08:23 -05:00
// We can only look for dangling if we received all the responses, if we did
// not we simply ignore it, since we can't tell for sure if its dangling object.
if totalResp == er . setDriveCount && shouldCheckForDangling ( err , errs , bucket ) {
2023-12-04 17:54:34 -05:00
_ , derr := er . deleteIfDangling ( context . Background ( ) , bucket , object , metaArr , errs , nil , opts )
2024-05-09 13:15:03 -04:00
if derr == nil {
if opts . VersionID != "" {
err = errFileVersionNotFound
} else {
err = errFileNotFound
}
2023-01-27 15:43:45 -05:00
}
2020-11-05 14:48:55 -05:00
}
2023-11-22 16:46:17 -05:00
return fi , nil , nil , toObjectErr ( err , bucket , object )
2016-05-20 23:48:47 -04:00
}
2021-01-27 13:21:14 -05:00
2023-03-20 12:08:42 -04:00
if ! fi . Deleted && len ( fi . Erasure . Distribution ) != len ( onlineDisks ) {
err := fmt . Errorf ( "unexpected file distribution (%v) from online disks (%v), looks like backend disks have been manually modified refusing to heal %s/%s(%s)" ,
fi . Erasure . Distribution , onlineDisks , bucket , object , opts . VersionID )
2024-04-04 08:04:40 -04:00
storageLogOnceIf ( ctx , err , "get-object-file-info-manually-modified" )
2023-03-20 12:08:42 -04:00
return fi , nil , nil , toObjectErr ( err , bucket , object , opts . VersionID )
}
2023-11-22 16:46:17 -05:00
filterOnlineDisksInplace ( fi , onlineMeta , onlineDisks )
for i := range onlineMeta {
2023-06-20 16:18:24 -04:00
// verify metadata is valid, it has similar erasure info
// as well as common modtime, if modtime is not possible
2024-01-18 02:03:17 -05:00
// verify if it has common "etag" at least.
2023-11-22 16:46:17 -05:00
if onlineMeta [ i ] . IsValid ( ) && onlineMeta [ i ] . Erasure . Equal ( fi . Erasure ) {
ok := onlineMeta [ i ] . ModTime . Equal ( modTime )
2023-06-20 16:18:24 -04:00
if modTime . IsZero ( ) || modTime . Equal ( timeSentinel ) {
ok = etag != "" && etag == fi . Metadata [ "etag" ]
}
if ok {
continue
}
} // in all other cases metadata is corrupt, do not read from it.
2023-11-22 16:46:17 -05:00
onlineMeta [ i ] = FileInfo { }
2022-05-30 15:43:54 -04:00
onlineDisks [ i ] = nil
2021-01-27 13:21:14 -05:00
}
2024-02-20 18:00:35 -05:00
select {
case mrfCheck <- fi . ShallowCopy ( ) :
case <- ctx . Done ( ) :
return fi , onlineMeta , onlineDisks , toObjectErr ( ctx . Err ( ) , bucket , object )
}
2021-01-27 13:21:14 -05:00
2023-11-22 16:46:17 -05:00
return fi , onlineMeta , onlineDisks , nil
2020-05-27 19:14:26 -04:00
}
// getObjectInfo - wrapper for reading object metadata and constructs ObjectInfo.
2020-06-12 23:04:01 -04:00
func ( er erasureObjects ) getObjectInfo ( ctx context . Context , bucket , object string , opts ObjectOptions ) ( objInfo ObjectInfo , err error ) {
2021-01-07 22:27:31 -05:00
fi , _ , _ , err := er . getObjectFileInfo ( ctx , bucket , object , opts , false )
2020-05-27 19:14:26 -04:00
if err != nil {
2020-07-02 19:17:27 -04:00
return objInfo , toObjectErr ( err , bucket , object )
2020-05-27 19:14:26 -04:00
}
2022-05-31 05:57:57 -04:00
objInfo = fi . ToObjectInfo ( bucket , object , opts . Versioned || opts . VersionSuspended )
2020-06-12 23:04:01 -04:00
if fi . Deleted {
2020-11-19 21:43:58 -05:00
if opts . VersionID == "" || opts . DeleteMarker {
2020-06-12 23:04:01 -04:00
return objInfo , toObjectErr ( errFileNotFound , bucket , object )
}
// Make sure to return object info to provide extra information.
2020-06-17 11:33:14 -04:00
return objInfo , toObjectErr ( errMethodNotAllowed , bucket , object )
2020-06-12 23:04:01 -04:00
}
2020-11-22 02:48:50 -05:00
return objInfo , nil
2016-05-20 23:48:47 -04:00
}
2024-02-22 01:26:06 -05:00
// getObjectInfoAndQuorum - wrapper for reading object metadata and constructs ObjectInfo, additionally returns write quorum for the object.
2021-09-01 11:57:42 -04:00
func ( er erasureObjects ) getObjectInfoAndQuorum ( ctx context . Context , bucket , object string , opts ObjectOptions ) ( objInfo ObjectInfo , wquorum int , err error ) {
fi , _ , _ , err := er . getObjectFileInfo ( ctx , bucket , object , opts , false )
if err != nil {
2021-12-28 15:41:52 -05:00
return objInfo , er . defaultWQuorum ( ) , toObjectErr ( err , bucket , object )
2021-09-01 11:57:42 -04:00
}
2022-09-15 15:43:49 -04:00
wquorum = fi . WriteQuorum ( er . defaultWQuorum ( ) )
2021-09-01 11:57:42 -04:00
2022-05-31 05:57:57 -04:00
objInfo = fi . ToObjectInfo ( bucket , object , opts . Versioned || opts . VersionSuspended )
2021-09-18 16:31:35 -04:00
if ! fi . VersionPurgeStatus ( ) . Empty ( ) && opts . VersionID != "" {
2021-09-01 11:57:42 -04:00
// Make sure to return object info to provide extra information.
return objInfo , wquorum , toObjectErr ( errMethodNotAllowed , bucket , object )
}
if fi . Deleted {
if opts . VersionID == "" || opts . DeleteMarker {
return objInfo , wquorum , toObjectErr ( errFileNotFound , bucket , object )
}
// Make sure to return object info to provide extra information.
return objInfo , wquorum , toObjectErr ( errMethodNotAllowed , bucket , object )
}
return objInfo , wquorum , nil
}
2020-06-12 23:04:01 -04:00
// Similar to rename but renames data from srcEntry to dstEntry at dataDir
2024-04-23 13:15:52 -04:00
func renameData ( ctx context . Context , disks [ ] StorageAPI , srcBucket , srcEntry string , metadata [ ] FileInfo , dstBucket , dstEntry string , writeQuorum int ) ( [ ] StorageAPI , [ ] byte , string , error ) {
2020-06-12 23:04:01 -04:00
g := errgroup . WithNErrs ( len ( disks ) )
2021-06-30 22:32:07 -04:00
fvID := mustGetUUID ( )
for index := range disks {
metadata [ index ] . SetTierFreeVersionID ( fvID )
}
2022-09-05 19:51:37 -04:00
2024-04-23 13:15:52 -04:00
diskVersions := make ( [ ] [ ] byte , len ( disks ) )
dataDirs := make ( [ ] string , len ( disks ) )
2020-06-12 23:04:01 -04:00
// Rename file on all underlying storage disks.
for index := range disks {
index := index
g . Go ( func ( ) error {
if disks [ index ] == nil {
return errDiskNotFound
}
2022-09-05 19:51:37 -04:00
2021-04-20 13:44:39 -04:00
// Pick one FileInfo for a disk at index.
fi := metadata [ index ]
// Assign index when index is initialized
if fi . Erasure . Index == 0 {
fi . Erasure . Index = index + 1
2020-06-12 23:04:01 -04:00
}
2021-06-30 22:32:07 -04:00
2022-09-05 19:51:37 -04:00
if ! fi . IsValid ( ) {
return errFileCorrupt
2021-04-20 13:44:39 -04:00
}
2024-04-23 13:15:52 -04:00
resp , err := disks [ index ] . RenameData ( ctx , srcBucket , srcEntry , fi , dstBucket , dstEntry , RenameOptions { } )
2022-09-05 19:51:37 -04:00
if err != nil {
return err
}
2024-04-23 13:15:52 -04:00
diskVersions [ index ] = resp . Sign
dataDirs [ index ] = resp . OldDataDir
2022-09-05 19:51:37 -04:00
return nil
2020-06-12 23:04:01 -04:00
} , index )
}
// Wait for all renames to finish.
errs := g . Wait ( )
2022-09-07 10:25:39 -04:00
err := reduceWriteQuorumErrs ( ctx , errs , objectOpIgnoredErrs , writeQuorum )
2023-12-04 14:33:39 -05:00
if err != nil {
2023-12-04 17:54:34 -05:00
dg := errgroup . WithNErrs ( len ( disks ) )
2023-12-04 14:33:39 -05:00
for index , nerr := range errs {
2023-12-04 17:54:34 -05:00
if nerr != nil {
continue
}
index := index
2023-12-04 14:33:39 -05:00
// When we are going to return error, attempt to delete success
// on some of the drives, if we cannot we do not have to notify
// caller this dangling object will be now scheduled to be removed
// via active healing.
2023-12-04 17:54:34 -05:00
dg . Go ( func ( ) error {
2024-04-23 13:15:52 -04:00
return disks [ index ] . DeleteVersion ( context . Background ( ) , dstBucket , dstEntry , metadata [ index ] , false , DeleteOptions {
UndoWrite : true ,
OldDataDir : dataDirs [ index ] ,
} )
2023-12-04 17:54:34 -05:00
} , index )
2023-12-04 14:33:39 -05:00
}
2023-12-04 17:54:34 -05:00
dg . Wait ( )
2023-12-04 14:33:39 -05:00
}
2024-04-23 13:15:52 -04:00
var dataDir string
var versions [ ] byte
2022-09-07 10:25:39 -04:00
if err == nil {
2024-04-23 13:15:52 -04:00
versions = reduceCommonVersions ( diskVersions , writeQuorum )
2023-01-19 03:54:22 -05:00
for index , dversions := range diskVersions {
if errs [ index ] != nil {
continue
}
2024-04-23 13:15:52 -04:00
if ! bytes . Equal ( dversions , versions ) {
if len ( dversions ) > len ( versions ) {
versions = dversions
}
2022-12-16 11:52:12 -05:00
break
2022-09-07 10:25:39 -04:00
}
2022-09-05 19:51:37 -04:00
}
2024-04-23 13:15:52 -04:00
dataDir = reduceCommonDataDir ( dataDirs , writeQuorum )
2022-09-05 19:51:37 -04:00
}
2022-02-23 14:59:13 -05:00
// We can safely allow RenameData errors up to len(er.getDisks()) - writeQuorum
2022-09-07 10:25:39 -04:00
// otherwise return failure.
2024-04-23 13:15:52 -04:00
return evalDisks ( disks , errs ) , versions , dataDir , err
2020-06-12 23:04:01 -04:00
}
2021-08-09 09:58:54 -04:00
func ( er erasureObjects ) putMetacacheObject ( ctx context . Context , key string , r * PutObjReader , opts ObjectOptions ) ( objInfo ObjectInfo , err error ) {
data := r . Reader
// No metadata is set, allocate a new one.
if opts . UserDefined == nil {
opts . UserDefined = make ( map [ string ] string )
}
storageDisks := er . getDisks ( )
// Get parity and data drive count based on storage class metadata
parityDrives := globalStorageClass . GetParityForSC ( opts . UserDefined [ xhttp . AmzStorageClass ] )
2022-06-27 23:22:18 -04:00
if parityDrives < 0 {
2021-08-09 09:58:54 -04:00
parityDrives = er . defaultParityCount
}
dataDrives := len ( storageDisks ) - parityDrives
// we now know the number of blocks this object needs for data and parity.
// writeQuorum is dataBlocks + 1
writeQuorum := dataDrives
if dataDrives == parityDrives {
writeQuorum ++
}
// Validate input data size and it can never be less than zero.
if data . Size ( ) < - 1 {
2024-04-04 08:04:40 -04:00
bugLogIf ( ctx , errInvalidArgument , logger . ErrorKind )
2021-08-09 09:58:54 -04:00
return ObjectInfo { } , toObjectErr ( errInvalidArgument )
}
// Initialize parts metadata
partsMetadata := make ( [ ] FileInfo , len ( storageDisks ) )
fi := newFileInfo ( pathJoin ( minioMetaBucket , key ) , dataDrives , parityDrives )
fi . DataDir = mustGetUUID ( )
// Initialize erasure metadata.
for index := range partsMetadata {
partsMetadata [ index ] = fi
}
// Order disks according to erasure distribution
var onlineDisks [ ] StorageAPI
onlineDisks , partsMetadata = shuffleDisksAndPartsMetadata ( storageDisks , partsMetadata , fi )
erasure , err := NewErasure ( ctx , fi . Erasure . DataBlocks , fi . Erasure . ParityBlocks , fi . Erasure . BlockSize )
if err != nil {
return ObjectInfo { } , toObjectErr ( err , minioMetaBucket , key )
}
// Fetch buffer for I/O, returns from the pool if not allocates a new one and returns.
var buffer [ ] byte
switch size := data . Size ( ) ; {
case size == 0 :
2024-01-18 02:03:17 -05:00
buffer = make ( [ ] byte , 1 ) // Allocate at least a byte to reach EOF
2021-08-09 09:58:54 -04:00
case size >= fi . Erasure . BlockSize :
2024-04-19 12:44:59 -04:00
buffer = globalBytePoolCap . Load ( ) . Get ( )
defer globalBytePoolCap . Load ( ) . Put ( buffer )
2021-08-09 09:58:54 -04:00
case size < fi . Erasure . BlockSize :
// No need to allocate fully blockSizeV1 buffer if the incoming data is smaller.
buffer = make ( [ ] byte , size , 2 * size + int64 ( fi . Erasure . ParityBlocks + fi . Erasure . DataBlocks - 1 ) )
}
if len ( buffer ) > int ( fi . Erasure . BlockSize ) {
buffer = buffer [ : fi . Erasure . BlockSize ]
}
writers := make ( [ ] io . Writer , len ( onlineDisks ) )
inlineBuffers := make ( [ ] * bytes . Buffer , len ( onlineDisks ) )
for i , disk := range onlineDisks {
if disk == nil {
continue
}
2022-01-12 21:49:01 -05:00
if disk . IsOnline ( ) {
2024-04-01 19:42:09 -04:00
buf := grid . GetByteBufferCap ( int ( erasure . ShardFileSize ( data . Size ( ) ) ) + 64 )
inlineBuffers [ i ] = bytes . NewBuffer ( buf [ : 0 ] )
defer grid . PutByteBuffer ( buf )
2022-01-12 21:49:01 -05:00
writers [ i ] = newStreamingBitrotWriterBuffer ( inlineBuffers [ i ] , DefaultBitrotAlgorithm , erasure . ShardSize ( ) )
}
2021-08-09 09:58:54 -04:00
}
n , erasureErr := erasure . Encode ( ctx , data , writers , buffer , writeQuorum )
closeBitrotWriters ( writers )
if erasureErr != nil {
return ObjectInfo { } , toObjectErr ( erasureErr , minioMetaBucket , key )
}
// Should return IncompleteBody{} error when reader has fewer bytes
// than specified in request header.
if n < data . Size ( ) {
return ObjectInfo { } , IncompleteBody { Bucket : minioMetaBucket , Object : key }
}
2022-07-11 20:30:56 -04:00
var index [ ] byte
if opts . IndexCB != nil {
index = opts . IndexCB ( )
}
2021-08-09 09:58:54 -04:00
2022-07-19 21:56:24 -04:00
modTime := UTCNow ( )
2021-08-09 09:58:54 -04:00
for i , w := range writers {
if w == nil {
2022-01-12 21:49:01 -05:00
// Make sure to avoid writing to disks which we couldn't complete in erasure.Encode()
2021-08-09 09:58:54 -04:00
onlineDisks [ i ] = nil
continue
}
2021-08-10 14:12:22 -04:00
partsMetadata [ i ] . Data = inlineBuffers [ i ] . Bytes ( )
2022-08-29 19:57:16 -04:00
partsMetadata [ i ] . AddObjectPart ( 1 , "" , n , data . ActualSize ( ) , modTime , index , nil )
2021-08-09 09:58:54 -04:00
}
// Fill all the necessary metadata.
// Update `xl.meta` content on each disks.
for index := range partsMetadata {
partsMetadata [ index ] . Size = n
2021-08-10 14:12:22 -04:00
partsMetadata [ index ] . Fresh = true
2021-08-09 09:58:54 -04:00
partsMetadata [ index ] . ModTime = modTime
2021-08-10 14:12:22 -04:00
partsMetadata [ index ] . Metadata = opts . UserDefined
2021-08-09 09:58:54 -04:00
}
// Set an additional header when data is inlined.
for index := range partsMetadata {
partsMetadata [ index ] . SetInlineData ( )
}
for i := 0 ; i < len ( onlineDisks ) ; i ++ {
if onlineDisks [ i ] != nil && onlineDisks [ i ] . IsOnline ( ) {
// Object info is the same in all disks, so we can pick
// the first meta from online disk
fi = partsMetadata [ i ]
break
}
}
2024-01-30 15:43:25 -05:00
if _ , err = writeUniqueFileInfo ( ctx , onlineDisks , "" , minioMetaBucket , key , partsMetadata , writeQuorum ) ; err != nil {
2021-08-10 14:12:22 -04:00
return ObjectInfo { } , toObjectErr ( err , minioMetaBucket , key )
2021-08-09 09:58:54 -04:00
}
2022-05-31 05:57:57 -04:00
return fi . ToObjectInfo ( minioMetaBucket , key , opts . Versioned || opts . VersionSuspended ) , nil
2021-08-09 09:58:54 -04:00
}
2016-06-01 19:43:31 -04:00
// PutObject - creates an object upon reading from the input stream
// until EOF, erasure codes the data across all disk and additionally
2020-06-12 23:04:01 -04:00
// writes `xl.meta` which carries the necessary metadata for future
2016-06-01 19:43:31 -04:00
// object operations.
2020-06-12 23:04:01 -04:00
func ( er erasureObjects ) PutObject ( ctx context . Context , bucket string , object string , data * PutObjReader , opts ObjectOptions ) ( objInfo ObjectInfo , err error ) {
return er . putObject ( ctx , bucket , object , data , opts )
2018-01-12 23:34:52 -05:00
}
2020-06-12 23:04:01 -04:00
// putObject wrapper for erasureObjects PutObject
func ( er erasureObjects ) putObject ( ctx context . Context , bucket string , object string , r * PutObjReader , opts ObjectOptions ) ( objInfo ObjectInfo , err error ) {
2024-03-06 06:43:16 -05:00
if ! opts . NoAuditLog {
2024-08-13 18:22:04 -04:00
auditObjectErasureSet ( ctx , "PutObject" , object , & er )
2024-03-06 06:43:16 -05:00
}
2022-05-04 03:45:27 -04:00
2023-05-23 10:57:57 -04:00
data := r . Reader
2022-09-14 21:44:04 -04:00
if opts . CheckPrecondFn != nil {
2023-06-18 21:20:15 -04:00
if ! opts . NoLock {
ns := er . NewNSLock ( bucket , object )
lkctx , err := ns . GetLock ( ctx , globalOperationTimeout )
if err != nil {
return ObjectInfo { } , err
}
ctx = lkctx . Context ( )
defer ns . Unlock ( lkctx )
opts . NoLock = true
2022-09-14 21:44:04 -04:00
}
2023-06-18 21:20:15 -04:00
obj , err := er . getObjectInfo ( ctx , bucket , object , opts )
if err == nil && opts . CheckPrecondFn ( obj ) {
2022-09-14 21:44:04 -04:00
return objInfo , PreConditionFailed { }
}
2023-06-18 21:20:15 -04:00
if err != nil && ! isErrVersionNotFound ( err ) && ! isErrObjectNotFound ( err ) && ! isErrReadQuorum ( err ) {
return objInfo , err
}
2022-09-14 21:44:04 -04:00
}
2023-05-23 10:57:57 -04:00
// Validate input data size and it can never be less than -1.
if data . Size ( ) < - 1 {
2024-04-04 08:04:40 -04:00
bugLogIf ( ctx , errInvalidArgument , logger . ErrorKind )
2023-05-23 10:57:57 -04:00
return ObjectInfo { } , toObjectErr ( errInvalidArgument )
}
2018-11-14 20:36:41 -05:00
2022-05-05 07:14:41 -04:00
userDefined := cloneMSS ( opts . UserDefined )
2018-01-29 21:43:13 -05:00
2020-06-12 23:04:01 -04:00
storageDisks := er . getDisks ( )
2019-10-07 01:50:24 -04:00
2024-03-14 06:38:33 -04:00
// Get parity and data drive count based on storage class metadata
parityDrives := globalStorageClass . GetParityForSC ( userDefined [ xhttp . AmzStorageClass ] )
if parityDrives < 0 {
parityDrives = er . defaultParityCount
}
if opts . MaxParity {
parityDrives = len ( storageDisks ) / 2
}
if ! opts . MaxParity && globalStorageClass . AvailabilityOptimized ( ) {
2021-05-27 14:38:09 -04:00
// If we have offline disks upgrade the number of erasure codes for this object.
2021-05-27 16:38:04 -04:00
parityOrig := parityDrives
2021-07-08 04:04:37 -04:00
2023-10-10 16:47:35 -04:00
var offlineDrives int
2021-05-27 14:38:09 -04:00
for _ , disk := range storageDisks {
2024-03-14 06:38:33 -04:00
if disk == nil || ! disk . IsOnline ( ) {
2023-10-10 16:47:35 -04:00
parityDrives ++
offlineDrives ++
2021-07-08 04:04:37 -04:00
continue
2021-05-27 14:38:09 -04:00
}
2021-07-08 04:04:37 -04:00
}
2023-10-10 16:47:35 -04:00
if offlineDrives >= ( len ( storageDisks ) + 1 ) / 2 {
2023-05-23 10:57:57 -04:00
// if offline drives are more than 50% of the drives
// we have no quorum, we shouldn't proceed just
// fail at that point.
return ObjectInfo { } , toObjectErr ( errErasureWriteQuorum , bucket , object )
}
2021-07-08 04:04:37 -04:00
if parityDrives >= len ( storageDisks ) / 2 {
parityDrives = len ( storageDisks ) / 2
2021-05-27 14:38:09 -04:00
}
2023-10-10 16:47:35 -04:00
2021-05-27 16:38:04 -04:00
if parityOrig != parityDrives {
2022-05-05 07:14:41 -04:00
userDefined [ minIOErasureUpgraded ] = strconv . Itoa ( parityOrig ) + "->" + strconv . Itoa ( parityDrives )
2021-05-27 14:38:09 -04:00
}
2019-10-07 01:50:24 -04:00
}
dataDrives := len ( storageDisks ) - parityDrives
2018-01-29 21:43:13 -05:00
// we now know the number of blocks this object needs for data and parity.
// writeQuorum is dataBlocks + 1
2020-06-09 22:19:03 -04:00
writeQuorum := dataDrives
if dataDrives == parityDrives {
2021-01-16 15:08:02 -05:00
writeQuorum ++
2020-06-09 22:19:03 -04:00
}
2018-01-29 21:43:13 -05:00
2017-01-30 18:44:42 -05:00
// Initialize parts metadata
2020-12-07 13:04:07 -05:00
partsMetadata := make ( [ ] FileInfo , len ( storageDisks ) )
2020-06-12 23:04:01 -04:00
2021-02-25 13:11:31 -05:00
fi := newFileInfo ( pathJoin ( bucket , object ) , dataDrives , parityDrives )
2021-04-19 13:30:42 -04:00
fi . VersionID = opts . VersionID
if opts . Versioned && fi . VersionID == "" {
fi . VersionID = mustGetUUID ( )
2020-06-12 23:04:01 -04:00
}
2021-05-15 22:54:07 -04:00
2020-06-12 23:04:01 -04:00
fi . DataDir = mustGetUUID ( )
2023-04-28 11:26:32 -04:00
fi . Checksum = opts . WantChecksum . AppendTo ( nil , nil )
2022-08-31 11:13:23 -04:00
if opts . EncryptFn != nil {
fi . Checksum = opts . EncryptFn ( "object-checksum" , fi . Checksum )
}
2024-06-10 11:31:51 -04:00
if userDefined [ ReplicationSsecChecksumHeader ] != "" {
if v , err := base64 . StdEncoding . DecodeString ( userDefined [ ReplicationSsecChecksumHeader ] ) ; err == nil {
fi . Checksum = v
}
}
delete ( userDefined , ReplicationSsecChecksumHeader )
2021-05-15 22:54:07 -04:00
uniqueID := mustGetUUID ( )
tempObj := uniqueID
2016-07-13 14:56:25 -04:00
2020-06-12 23:04:01 -04:00
// Initialize erasure metadata.
2017-01-30 18:44:42 -05:00
for index := range partsMetadata {
2020-06-12 23:04:01 -04:00
partsMetadata [ index ] = fi
2017-01-30 18:44:42 -05:00
}
// Order disks according to erasure distribution
2020-10-28 03:09:15 -04:00
var onlineDisks [ ] StorageAPI
2021-04-21 22:06:08 -04:00
onlineDisks , partsMetadata = shuffleDisksAndPartsMetadata ( storageDisks , partsMetadata , fi )
2017-01-30 18:44:42 -05:00
2020-06-12 23:04:01 -04:00
erasure , err := NewErasure ( ctx , fi . Erasure . DataBlocks , fi . Erasure . ParityBlocks , fi . Erasure . BlockSize )
2017-08-14 21:08:42 -04:00
if err != nil {
return ObjectInfo { } , toObjectErr ( err , bucket , object )
}
2017-10-06 12:38:01 -04:00
2018-02-15 20:45:57 -05:00
// Fetch buffer for I/O, returns from the pool if not allocates a new one and returns.
2018-06-13 14:55:12 -04:00
var buffer [ ] byte
switch size := data . Size ( ) ; {
case size == 0 :
2024-01-18 02:03:17 -05:00
buffer = make ( [ ] byte , 1 ) // Allocate at least a byte to reach EOF
2022-07-13 10:52:15 -04:00
case size >= fi . Erasure . BlockSize || size == - 1 :
2024-04-19 12:44:59 -04:00
buffer = globalBytePoolCap . Load ( ) . Get ( )
defer globalBytePoolCap . Load ( ) . Put ( buffer )
2020-06-12 23:04:01 -04:00
case size < fi . Erasure . BlockSize :
2018-06-13 14:55:12 -04:00
// No need to allocate fully blockSizeV1 buffer if the incoming data is smaller.
2020-06-12 23:04:01 -04:00
buffer = make ( [ ] byte , size , 2 * size + int64 ( fi . Erasure . ParityBlocks + fi . Erasure . DataBlocks - 1 ) )
2018-06-13 14:55:12 -04:00
}
2017-10-06 12:38:01 -04:00
2020-06-12 23:04:01 -04:00
if len ( buffer ) > int ( fi . Erasure . BlockSize ) {
buffer = buffer [ : fi . Erasure . BlockSize ]
2018-08-06 18:14:08 -04:00
}
2019-05-14 15:33:18 -04:00
partName := "part.1"
2020-06-12 23:04:01 -04:00
tempErasureObj := pathJoin ( uniqueID , fi . DataDir , partName )
2017-01-30 18:44:42 -05:00
2023-08-27 12:57:11 -04:00
defer er . deleteAll ( context . Background ( ) , minioMetaTmpBucket , tempObj )
2021-03-24 17:19:52 -04:00
2024-09-12 08:24:04 -04:00
var inlineBuffers [ ] * bytes . Buffer
if globalStorageClass . ShouldInline ( erasure . ShardFileSize ( data . ActualSize ( ) ) , opts . Versioned ) {
inlineBuffers = make ( [ ] * bytes . Buffer , len ( onlineDisks ) )
2024-03-26 18:06:19 -04:00
}
2024-09-12 08:24:04 -04:00
shardFileSize := erasure . ShardFileSize ( data . Size ( ) )
2019-05-14 15:33:18 -04:00
writers := make ( [ ] io . Writer , len ( onlineDisks ) )
for i , disk := range onlineDisks {
if disk == nil {
continue
2018-09-27 23:36:17 -04:00
}
2021-03-29 20:00:55 -04:00
2022-01-12 21:49:01 -05:00
if ! disk . IsOnline ( ) {
continue
}
2021-03-29 20:00:55 -04:00
if len ( inlineBuffers ) > 0 {
2024-04-01 19:42:09 -04:00
buf := grid . GetByteBufferCap ( int ( shardFileSize ) + 64 )
inlineBuffers [ i ] = bytes . NewBuffer ( buf [ : 0 ] )
defer grid . PutByteBuffer ( buf )
2021-03-29 20:00:55 -04:00
writers [ i ] = newStreamingBitrotWriterBuffer ( inlineBuffers [ i ] , DefaultBitrotAlgorithm , erasure . ShardSize ( ) )
continue
}
2022-01-12 21:49:01 -05:00
2024-01-30 15:43:25 -05:00
writers [ i ] = newBitrotWriter ( disk , bucket , minioMetaTmpBucket , tempErasureObj , shardFileSize , DefaultBitrotAlgorithm , erasure . ShardSize ( ) )
2019-05-14 15:33:18 -04:00
}
2018-09-27 23:36:17 -04:00
2022-01-14 13:01:25 -05:00
toEncode := io . Reader ( data )
2024-04-01 19:42:09 -04:00
if data . Size ( ) >= bigFileThreshold {
2022-01-14 13:01:25 -05:00
// We use 2 buffers, so we always have a full buffer of input.
2024-04-19 12:44:59 -04:00
pool := globalBytePoolCap . Load ( )
bufA := pool . Get ( )
bufB := pool . Get ( )
defer pool . Put ( bufA )
defer pool . Put ( bufB )
2022-01-14 13:01:25 -05:00
ra , err := readahead . NewReaderBuffer ( data , [ ] [ ] byte { bufA [ : fi . Erasure . BlockSize ] , bufB [ : fi . Erasure . BlockSize ] } )
if err == nil {
toEncode = ra
defer ra . Close ( )
}
2024-04-04 08:04:40 -04:00
bugLogIf ( ctx , err )
2022-01-14 13:01:25 -05:00
}
n , erasureErr := erasure . Encode ( ctx , toEncode , writers , buffer , writeQuorum )
2019-05-14 15:33:18 -04:00
closeBitrotWriters ( writers )
if erasureErr != nil {
2024-01-30 15:43:25 -05:00
return ObjectInfo { } , toObjectErr ( erasureErr , bucket , object )
2019-05-14 15:33:18 -04:00
}
2017-08-14 21:08:42 -04:00
2019-05-14 15:33:18 -04:00
// Should return IncompleteBody{} error when reader has fewer bytes
// than specified in request header.
if n < data . Size ( ) {
2020-09-08 17:22:04 -04:00
return ObjectInfo { } , IncompleteBody { Bucket : bucket , Object : object }
2019-05-14 15:33:18 -04:00
}
2017-01-31 18:34:49 -05:00
2022-07-11 20:30:56 -04:00
var compIndex [ ] byte
if opts . IndexCB != nil {
compIndex = opts . IndexCB ( )
}
2020-12-24 18:02:02 -05:00
if ! opts . NoLock {
lk := er . NewNSLock ( bucket , object )
2021-04-29 23:55:21 -04:00
lkctx , err := lk . GetLock ( ctx , globalOperationTimeout )
2021-03-03 21:36:43 -05:00
if err != nil {
2020-12-24 18:02:02 -05:00
return ObjectInfo { } , err
}
2021-04-29 23:55:21 -04:00
ctx = lkctx . Context ( )
2022-12-23 22:49:07 -05:00
defer lk . Unlock ( lkctx )
2020-09-15 23:44:48 -04:00
}
2022-07-19 21:56:24 -04:00
modTime := opts . MTime
if opts . MTime . IsZero ( ) {
modTime = UTCNow ( )
}
2024-09-12 08:24:04 -04:00
kind , encrypted := crypto . IsEncrypted ( userDefined )
2024-09-11 14:35:37 -04:00
actualSize := data . ActualSize ( )
if actualSize < 0 {
compressed := fi . IsCompressed ( )
switch {
case compressed :
// ... nothing changes for compressed stream.
2024-09-12 08:24:04 -04:00
// if actualSize is -1 we have no known way to
// determine what is the actualSize.
2024-09-11 14:35:37 -04:00
case encrypted :
decSize , err := sio . DecryptedSize ( uint64 ( n ) )
if err == nil {
actualSize = int64 ( decSize )
}
default :
actualSize = n
}
}
2019-05-14 15:33:18 -04:00
for i , w := range writers {
if w == nil {
onlineDisks [ i ] = nil
continue
2017-01-30 18:44:42 -05:00
}
2021-03-29 20:00:55 -04:00
if len ( inlineBuffers ) > 0 && inlineBuffers [ i ] != nil {
partsMetadata [ i ] . Data = inlineBuffers [ i ] . Bytes ( )
2021-04-21 22:06:08 -04:00
} else {
partsMetadata [ i ] . Data = nil
2021-03-29 20:00:55 -04:00
}
2022-08-29 19:57:16 -04:00
// No need to add checksum to part. We already have it on the object.
2024-09-11 14:35:37 -04:00
partsMetadata [ i ] . AddObjectPart ( 1 , "" , n , actualSize , modTime , compIndex , nil )
2023-08-27 12:57:11 -04:00
partsMetadata [ i ] . Versioned = opts . Versioned || opts . VersionSuspended
2016-07-18 22:06:48 -04:00
}
2022-07-16 22:35:24 -04:00
2022-09-14 21:44:04 -04:00
userDefined [ "etag" ] = r . MD5CurrentHexString ( )
if opts . PreserveETag != "" {
2023-06-15 15:43:26 -04:00
if ! opts . ReplicationRequest {
userDefined [ "etag" ] = opts . PreserveETag
} else if kind != crypto . S3 {
// if we have a replication request
// and SSE-S3 is specified do not preserve
// the incoming etag.
userDefined [ "etag" ] = opts . PreserveETag
}
2020-08-12 20:32:24 -04:00
}
2016-06-01 19:43:31 -04:00
2016-06-17 00:42:02 -04:00
// Guess content-type from the extension if possible.
2022-05-05 07:14:41 -04:00
if userDefined [ "content-type" ] == "" {
userDefined [ "content-type" ] = mimedb . TypeByExtension ( path . Ext ( object ) )
2016-05-20 23:48:47 -04:00
}
2024-02-28 20:44:30 -05:00
// if storageClass is standard no need to save it as part of metadata.
if userDefined [ xhttp . AmzStorageClass ] == storageclass . STANDARD {
delete ( userDefined , xhttp . AmzStorageClass )
}
2016-05-26 22:55:48 -04:00
// Fill all the necessary metadata.
2020-06-12 23:04:01 -04:00
// Update `xl.meta` content on each disks.
2016-05-31 23:23:31 -04:00
for index := range partsMetadata {
2022-05-05 07:14:41 -04:00
partsMetadata [ index ] . Metadata = userDefined
2020-06-12 23:04:01 -04:00
partsMetadata [ index ] . Size = n
partsMetadata [ index ] . ModTime = modTime
2024-02-03 17:03:30 -05:00
if len ( inlineBuffers ) > 0 {
2021-07-16 12:38:27 -04:00
partsMetadata [ index ] . SetInlineData ( )
2021-05-25 19:33:06 -04:00
}
2024-02-03 17:03:30 -05:00
if opts . DataMovement {
partsMetadata [ index ] . SetDataMov ( )
}
2021-05-25 19:33:06 -04:00
}
2021-04-02 01:12:03 -04:00
// Rename the successfully written temporary object to final location.
2024-04-23 13:15:52 -04:00
onlineDisks , versions , oldDataDir , err := renameData ( ctx , onlineDisks , minioMetaTmpBucket , tempObj , partsMetadata , bucket , object , writeQuorum )
2022-12-16 11:52:12 -05:00
if err != nil {
if errors . Is ( err , errFileNotFound ) {
2024-04-28 13:53:50 -04:00
// An in-quorum errFileNotFound means that client stream
// prematurely closed and we do not find any xl.meta or
// part.1's - in such a scenario we must return as if client
// disconnected. This means that erasure.Encode() CreateFile()
// did not do anything.
return ObjectInfo { } , IncompleteBody { Bucket : bucket , Object : object }
2022-12-16 11:52:12 -05:00
}
return ObjectInfo { } , toObjectErr ( err , bucket , object )
}
2024-05-14 06:29:17 -04:00
if err = er . commitRenameDataDir ( ctx , bucket , object , oldDataDir , onlineDisks , writeQuorum ) ; err != nil {
2024-04-23 13:15:52 -04:00
return ObjectInfo { } , toObjectErr ( err , bucket , object )
}
2020-01-15 21:30:32 -05:00
for i := 0 ; i < len ( onlineDisks ) ; i ++ {
2020-12-22 12:16:43 -05:00
if onlineDisks [ i ] != nil && onlineDisks [ i ] . IsOnline ( ) {
2021-07-16 01:32:06 -04:00
// Object info is the same in all disks, so we can pick
// the first meta from online disk
fi = partsMetadata [ i ]
break
2020-01-15 21:30:32 -05:00
}
}
2022-02-23 14:59:13 -05:00
// For speedtest objects do not attempt to heal them.
if ! opts . Speedtest {
2023-12-04 14:13:50 -05:00
// When there is versions disparity we are healing
// the content implicitly for all versions, we can
// avoid triggering another MRF heal for offline drives.
2024-04-23 13:15:52 -04:00
if len ( versions ) == 0 {
2023-12-04 14:13:50 -05:00
// Whether a disk was initially or becomes offline
// during this upload, send it to the MRF list.
for i := 0 ; i < len ( onlineDisks ) ; i ++ {
if onlineDisks [ i ] != nil && onlineDisks [ i ] . IsOnline ( ) {
continue
}
2022-11-14 21:35:26 -05:00
2023-12-04 14:13:50 -05:00
er . addPartial ( bucket , object , fi . VersionID )
break
}
} else {
2024-08-13 18:26:05 -04:00
globalMRFState . addPartialOp ( PartialOperation {
Bucket : bucket ,
Object : object ,
Queued : time . Now ( ) ,
Versions : versions ,
SetIndex : er . setIndex ,
PoolIndex : er . poolIndex ,
2023-06-24 22:31:04 -04:00
} )
2022-09-07 10:25:39 -04:00
}
}
2021-09-18 16:31:35 -04:00
fi . ReplicationState = opts . PutReplicationState ( )
2020-06-12 23:04:01 -04:00
2021-09-22 22:17:09 -04:00
// we are adding a new version to this object under the namespace lock, so this is the latest version.
fi . IsLatest = true
2022-05-31 05:57:57 -04:00
return fi . ToObjectInfo ( bucket , object , opts . Versioned || opts . VersionSuspended ) , nil
2020-06-12 23:04:01 -04:00
}
2022-08-18 19:41:59 -04:00
func ( er erasureObjects ) deleteObjectVersion ( ctx context . Context , bucket , object string , fi FileInfo , forceDelMarker bool ) error {
2020-06-12 23:04:01 -04:00
disks := er . getDisks ( )
2022-08-18 19:41:59 -04:00
// Assume (N/2 + 1) quorum for Delete()
// this is a theoretical assumption such that
// for delete's we do not need to honor storage
// class for objects that have reduced quorum
// due to storage class - this only needs to be honored
// for Read() requests alone that we already do.
writeQuorum := len ( disks ) / 2 + 1
2020-06-12 23:04:01 -04:00
g := errgroup . WithNErrs ( len ( disks ) )
for index := range disks {
index := index
g . Go ( func ( ) error {
if disks [ index ] == nil {
return errDiskNotFound
}
2023-12-29 18:52:41 -05:00
return disks [ index ] . DeleteVersion ( ctx , bucket , object , fi , forceDelMarker , DeleteOptions { } )
2020-06-12 23:04:01 -04:00
} , index )
2016-09-02 15:18:35 -04:00
}
2020-06-12 23:04:01 -04:00
// return errors if any during deletion
return reduceWriteQuorumErrs ( ctx , g . Wait ( ) , objectOpIgnoredErrs , writeQuorum )
2016-05-20 23:48:47 -04:00
}
2020-06-12 23:04:01 -04:00
// DeleteObjects deletes objects/versions in bulk, this function will still automatically split objects list
// into smaller bulks if some object names are found to be duplicated in the delete list, splitting
// into smaller bulks will avoid holding twice the write lock of the duplicated object names.
func ( er erasureObjects ) DeleteObjects ( ctx context . Context , bucket string , objects [ ] ObjectToDelete , opts ObjectOptions ) ( [ ] DeletedObject , [ ] error ) {
2024-03-06 06:43:16 -05:00
if ! opts . NoAuditLog {
for _ , obj := range objects {
2024-08-13 18:22:04 -04:00
auditObjectErasureSet ( ctx , "DeleteObjects" , obj . ObjectV . ObjectName , & er )
2024-03-06 06:43:16 -05:00
}
2022-05-04 03:45:27 -04:00
}
2020-06-12 23:04:01 -04:00
errs := make ( [ ] error , len ( objects ) )
dobjects := make ( [ ] DeletedObject , len ( objects ) )
writeQuorums := make ( [ ] int , len ( objects ) )
2020-04-27 13:06:21 -04:00
2020-06-12 23:04:01 -04:00
storageDisks := er . getDisks ( )
for i := range objects {
// Assume (N/2 + 1) quorums for all objects
// this is a theoretical assumption such that
// for delete's we do not need to honor storage
// class for objects which have reduced quorum
// storage class only needs to be honored for
// Read() requests alone which we already do.
2022-01-27 20:00:15 -05:00
writeQuorums [ i ] = len ( storageDisks ) / 2 + 1
2019-05-13 15:25:49 -04:00
}
2021-11-01 13:50:07 -04:00
versionsMap := make ( map [ string ] FileInfoVersions , len ( objects ) )
2020-06-12 23:04:01 -04:00
for i := range objects {
2021-11-01 13:50:07 -04:00
// Construct the FileInfo data that needs to be preserved on the disk.
vr := FileInfo {
Name : objects [ i ] . ObjectName ,
VersionID : objects [ i ] . VersionID ,
ReplicationState : objects [ i ] . ReplicationState ( ) ,
// save the index to set correct error at this index.
Idx : i ,
}
vr . SetTierFreeVersionID ( mustGetUUID ( ) )
// VersionID is not set means delete is not specific about
// any version, look for if the bucket is versioned or not.
2020-06-12 23:04:01 -04:00
if objects [ i ] . VersionID == "" {
2022-05-06 22:05:28 -04:00
// MinIO extension to bucket version configuration
suspended := opts . VersionSuspended
versioned := opts . Versioned
if opts . PrefixEnabledFn != nil {
versioned = opts . PrefixEnabledFn ( objects [ i ] . ObjectName )
}
if versioned || suspended {
2021-11-01 13:50:07 -04:00
// Bucket is versioned and no version was explicitly
// mentioned for deletes, create a delete marker instead.
vr . ModTime = UTCNow ( )
vr . Deleted = true
// Versioning suspended means that we add a `null` version
// delete marker, if not add a new version for this delete
// marker.
2022-05-08 01:06:44 -04:00
if versioned {
2021-11-01 13:50:07 -04:00
vr . VersionID = mustGetUUID ( )
2020-06-12 23:04:01 -04:00
}
}
}
2021-11-01 13:50:07 -04:00
// De-dup same object name to collect multiple versions for same object.
v , ok := versionsMap [ objects [ i ] . ObjectName ]
if ok {
v . Versions = append ( v . Versions , vr )
} else {
v = FileInfoVersions {
Name : vr . Name ,
Versions : [ ] FileInfo { vr } ,
}
2020-06-12 23:04:01 -04:00
}
2021-11-01 13:50:07 -04:00
if vr . Deleted {
dobjects [ i ] = DeletedObject {
DeleteMarker : vr . Deleted ,
DeleteMarkerVersionID : vr . VersionID ,
DeleteMarkerMTime : DeleteMarkerMTime { vr . ModTime } ,
ObjectName : vr . Name ,
ReplicationState : vr . ReplicationState ,
}
} else {
dobjects [ i ] = DeletedObject {
ObjectName : vr . Name ,
VersionID : vr . VersionID ,
ReplicationState : vr . ReplicationState ,
}
}
versionsMap [ objects [ i ] . ObjectName ] = v
}
dedupVersions := make ( [ ] FileInfoVersions , 0 , len ( versionsMap ) )
for _ , version := range versionsMap {
dedupVersions = append ( dedupVersions , version )
2020-06-12 23:04:01 -04:00
}
2020-03-06 16:44:24 -05:00
2019-05-13 15:25:49 -04:00
// Initialize list of errors.
2022-01-02 12:15:06 -05:00
delObjErrs := make ( [ ] [ ] error , len ( storageDisks ) )
2020-06-12 23:04:01 -04:00
var wg sync . WaitGroup
// Remove versions in bulk for each disk
for index , disk := range storageDisks {
2020-03-11 11:56:36 -04:00
wg . Add ( 1 )
go func ( index int , disk StorageAPI ) {
defer wg . Done ( )
2021-11-01 13:50:07 -04:00
delObjErrs [ index ] = make ( [ ] error , len ( objects ) )
2020-11-29 00:15:45 -05:00
if disk == nil {
2021-11-01 13:50:07 -04:00
for i := range objects {
2020-11-29 00:15:45 -05:00
delObjErrs [ index ] [ i ] = errDiskNotFound
}
return
}
2023-12-29 18:52:41 -05:00
errs := disk . DeleteVersions ( ctx , bucket , dedupVersions , DeleteOptions { } )
2021-11-01 13:50:07 -04:00
for i , err := range errs {
if err == nil {
continue
}
for _ , v := range dedupVersions [ i ] . Versions {
2022-01-06 13:47:49 -05:00
if err == errFileNotFound || err == errFileVersionNotFound {
if ! dobjects [ v . Idx ] . DeleteMarker {
// Not delete marker, if not found, ok.
continue
}
}
2021-11-01 13:50:07 -04:00
delObjErrs [ index ] [ v . Idx ] = err
}
}
2020-06-12 23:04:01 -04:00
} ( index , disk )
2019-05-13 15:25:49 -04:00
}
2020-03-11 11:56:36 -04:00
wg . Wait ( )
2019-05-13 15:25:49 -04:00
// Reduce errors for each object
for objIndex := range objects {
2020-06-12 23:04:01 -04:00
diskErrs := make ( [ ] error , len ( storageDisks ) )
2020-03-11 11:56:36 -04:00
// Iterate over disks to fetch the error
// of deleting of the current object
2019-05-13 15:25:49 -04:00
for i := range delObjErrs {
2020-03-11 11:56:36 -04:00
// delObjErrs[i] is not nil when disks[i] is also not nil
2019-05-13 15:25:49 -04:00
if delObjErrs [ i ] != nil {
2020-09-02 03:19:03 -04:00
diskErrs [ i ] = delObjErrs [ i ] [ objIndex ]
2019-05-13 15:25:49 -04:00
}
}
2020-11-29 00:15:45 -05:00
err := reduceWriteQuorumErrs ( ctx , diskErrs , objectOpIgnoredErrs , writeQuorums [ objIndex ] )
if objects [ objIndex ] . VersionID != "" {
errs [ objIndex ] = toObjectErr ( err , bucket , objects [ objIndex ] . ObjectName , objects [ objIndex ] . VersionID )
} else {
errs [ objIndex ] = toObjectErr ( err , bucket , objects [ objIndex ] . ObjectName )
}
2019-05-13 15:25:49 -04:00
}
2020-06-29 16:07:26 -04:00
// Check failed deletes across multiple objects
2021-11-01 13:50:07 -04:00
for i , dobj := range dobjects {
2023-03-31 11:15:41 -04:00
// This object errored, we should attempt a heal just in case.
if errs [ i ] != nil && ! isErrVersionNotFound ( errs [ i ] ) && ! isErrObjectNotFound ( errs [ i ] ) {
// all other direct versionId references we should
// ensure no dangling file is left over.
2023-04-19 10:47:42 -04:00
er . addPartial ( bucket , dobj . ObjectName , dobj . VersionID )
2021-11-01 13:50:07 -04:00
continue
}
2020-06-29 16:07:26 -04:00
// Check if there is any offline disk and add it to the MRF list
for _ , disk := range storageDisks {
2020-12-22 12:16:43 -05:00
if disk != nil && disk . IsOnline ( ) {
// Skip attempted heal on online disks.
continue
2020-06-29 16:07:26 -04:00
}
2020-12-22 12:16:43 -05:00
// all other direct versionId references we should
// ensure no dangling file is left over.
2023-04-19 10:47:42 -04:00
er . addPartial ( bucket , dobj . ObjectName , dobj . VersionID )
2020-12-22 12:16:43 -05:00
break
2020-06-29 16:07:26 -04:00
}
}
2020-06-12 23:04:01 -04:00
return dobjects , errs
2019-05-13 15:25:49 -04:00
}
2024-05-14 06:29:17 -04:00
func ( er erasureObjects ) commitRenameDataDir ( ctx context . Context , bucket , object , dataDir string , onlineDisks [ ] StorageAPI , writeQuorum int ) error {
2024-04-23 13:15:52 -04:00
if dataDir == "" {
return nil
}
g := errgroup . WithNErrs ( len ( onlineDisks ) )
for index := range onlineDisks {
index := index
g . Go ( func ( ) error {
if onlineDisks [ index ] == nil {
return nil
}
return onlineDisks [ index ] . Delete ( ctx , bucket , pathJoin ( object , dataDir ) , DeleteOptions {
Recursive : true ,
} )
} , index )
}
2024-05-14 06:29:17 -04:00
return reduceWriteQuorumErrs ( ctx , g . Wait ( ) , objectOpIgnoredErrs , writeQuorum )
2024-04-23 13:15:52 -04:00
}
2021-06-15 21:43:14 -04:00
func ( er erasureObjects ) deletePrefix ( ctx context . Context , bucket , prefix string ) error {
disks := er . getDisks ( )
2024-05-07 01:44:46 -04:00
// Assume (N/2 + 1) quorum for Delete()
// this is a theoretical assumption such that
// for delete's we do not need to honor storage
// class for objects that have reduced quorum
// due to storage class - this only needs to be honored
// for Read() requests alone that we already do.
writeQuorum := len ( disks ) / 2 + 1
2021-06-15 21:43:14 -04:00
g := errgroup . WithNErrs ( len ( disks ) )
for index := range disks {
index := index
g . Go ( func ( ) error {
if disks [ index ] == nil {
return nil
}
2022-07-11 12:15:54 -04:00
return disks [ index ] . Delete ( ctx , bucket , prefix , DeleteOptions {
Recursive : true ,
2023-11-29 01:35:16 -05:00
Immediate : true ,
2022-07-11 12:15:54 -04:00
} )
2021-06-15 21:43:14 -04:00
} , index )
}
2024-05-07 01:44:46 -04:00
// return errors if any during deletion
return reduceWriteQuorumErrs ( ctx , g . Wait ( ) , objectOpIgnoredErrs , writeQuorum )
2021-06-15 21:43:14 -04:00
}
2016-06-01 19:43:31 -04:00
// DeleteObject - deletes an object, this call doesn't necessary reply
// any error as it is not necessary for the handler to reply back a
// response to the client request.
2020-06-12 23:04:01 -04:00
func ( er erasureObjects ) DeleteObject ( ctx context . Context , bucket , object string , opts ObjectOptions ) ( objInfo ObjectInfo , err error ) {
2024-03-06 06:43:16 -05:00
if ! opts . NoAuditLog {
2024-08-13 18:22:04 -04:00
auditObjectErasureSet ( ctx , "DeleteObject" , object , & er )
2024-03-06 06:43:16 -05:00
}
2022-05-04 03:45:27 -04:00
2021-08-27 20:06:47 -04:00
var lc * lifecycle . Lifecycle
2022-04-11 16:25:32 -04:00
var rcfg lock . Retention
2023-10-06 08:55:15 -04:00
var replcfg * replication . Config
2021-08-27 20:06:47 -04:00
if opts . Expiration . Expire {
// Check if the current bucket has a configured lifecycle policy
2024-04-22 13:49:30 -04:00
lc , err = globalLifecycleSys . Get ( bucket )
if err != nil && ! errors . Is ( err , BucketLifecycleNotFound { Bucket : bucket } ) {
return objInfo , err
}
rcfg , err = globalBucketObjectLockSys . Get ( bucket )
if err != nil {
return objInfo , err
}
replcfg , err = getReplicationConfig ( ctx , bucket )
if err != nil {
return objInfo , err
}
2021-08-27 20:06:47 -04:00
}
// expiration attempted on a bucket with no lifecycle
// rules shall be rejected.
if lc == nil && opts . Expiration . Expire {
if opts . VersionID != "" {
return objInfo , VersionNotFound {
Bucket : bucket ,
Object : object ,
VersionID : opts . VersionID ,
}
}
return objInfo , ObjectNotFound {
Bucket : bucket ,
Object : object ,
}
}
2024-03-28 02:44:52 -04:00
if opts . DeletePrefix {
if opts . Expiration . Expire {
// Expire all versions expiration must still verify the state() on disk
// via a getObjectInfo() call as follows, any read quorum issues we
// must not proceed further for safety reasons. attempt a MRF heal
// while we see such quorum errors.
goi , _ , gerr := er . getObjectInfoAndQuorum ( ctx , bucket , object , opts )
if gerr != nil && goi . Name == "" {
if _ , ok := gerr . ( InsufficientReadQuorum ) ; ok {
// Add an MRF heal for next time.
er . addPartial ( bucket , object , opts . VersionID )
return objInfo , InsufficientWriteQuorum { }
}
return objInfo , gerr
}
// Add protection and re-verify the ILM rules for qualification
// based on the latest objectInfo and see if the object still
// qualifies for deletion.
if gerr == nil {
var isErr bool
2024-04-30 21:11:10 -04:00
evt := evalActionFromLifecycle ( ctx , * lc , rcfg , replcfg , goi )
2024-03-28 02:44:52 -04:00
switch evt . Action {
2024-04-30 21:11:10 -04:00
case lifecycle . DeleteAllVersionsAction , lifecycle . DelMarkerDeleteAllVersionsAction :
// opts.DeletePrefix is used only in the above lifecycle Expiration actions.
default :
// object has been modified since lifecycle action was previously evaluated
2024-03-28 02:44:52 -04:00
isErr = true
}
if isErr {
if goi . VersionID != "" {
return goi , VersionNotFound {
Bucket : bucket ,
Object : object ,
VersionID : goi . VersionID ,
}
}
return goi , ObjectNotFound {
Bucket : bucket ,
Object : object ,
}
}
}
} // Delete marker and any latest that qualifies shall be expired permanently.
return ObjectInfo { } , toObjectErr ( er . deletePrefix ( ctx , bucket , object ) , bucket , object )
}
2022-08-18 19:41:59 -04:00
storageDisks := er . getDisks ( )
2022-12-08 21:15:16 -05:00
versionFound := true
objInfo = ObjectInfo { VersionID : opts . VersionID } // version id needed in Delete API response.
goi , _ , gerr := er . getObjectInfoAndQuorum ( ctx , bucket , object , opts )
2024-04-25 20:31:12 -04:00
tryDel := false
2022-12-08 21:15:16 -05:00
if gerr != nil && goi . Name == "" {
2023-03-06 11:56:10 -05:00
if _ , ok := gerr . ( InsufficientReadQuorum ) ; ok {
2024-04-25 20:31:12 -04:00
if opts . Versioned || opts . VersionSuspended || countOnlineDisks ( storageDisks ) < len ( storageDisks ) / 2 + 1 {
// Add an MRF heal for next time.
er . addPartial ( bucket , object , opts . VersionID )
return objInfo , InsufficientWriteQuorum { }
}
tryDel = true // only for unversioned objects if there is write quorum
2022-12-08 21:15:16 -05:00
}
// For delete marker replication, versionID being replicated will not exist on disk
if opts . DeleteMarker {
versionFound = false
2024-04-25 20:31:12 -04:00
} else if ! tryDel {
2022-12-08 21:15:16 -05:00
return objInfo , gerr
}
}
2024-03-28 02:44:52 -04:00
2023-07-10 10:57:56 -04:00
if opts . EvalMetadataFn != nil {
2024-04-25 20:31:12 -04:00
dsc , err := opts . EvalMetadataFn ( & goi , gerr )
2023-07-10 10:57:56 -04:00
if err != nil {
return ObjectInfo { } , err
}
if dsc . ReplicateAny ( ) {
opts . SetDeleteReplicationState ( dsc , opts . VersionID )
goi . replicationDecision = opts . DeleteReplication . ReplicateDecisionStr
}
}
if opts . EvalRetentionBypassFn != nil {
if err := opts . EvalRetentionBypassFn ( goi , gerr ) ; err != nil {
return ObjectInfo { } , err
}
}
2022-10-26 19:09:27 -04:00
2021-08-27 20:06:47 -04:00
if opts . Expiration . Expire {
2022-12-08 21:15:16 -05:00
if gerr == nil {
2023-10-06 08:55:15 -04:00
evt := evalActionFromLifecycle ( ctx , * lc , rcfg , replcfg , goi )
2022-08-18 19:41:59 -04:00
var isErr bool
2022-10-21 13:46:53 -04:00
switch evt . Action {
2022-08-18 19:41:59 -04:00
case lifecycle . NoneAction :
isErr = true
case lifecycle . TransitionAction , lifecycle . TransitionVersionAction :
isErr = true
2021-08-27 20:06:47 -04:00
}
2022-08-18 19:41:59 -04:00
if isErr {
if goi . VersionID != "" {
return goi , VersionNotFound {
Bucket : bucket ,
Object : object ,
VersionID : goi . VersionID ,
}
}
return goi , ObjectNotFound {
Bucket : bucket ,
Object : object ,
}
2021-08-27 20:06:47 -04:00
}
}
2020-09-14 18:57:13 -04:00
}
2021-08-27 20:06:47 -04:00
2022-12-08 21:15:16 -05:00
// Determine whether to mark object deleted for replication
markDelete := goi . VersionID != ""
2021-09-01 11:57:42 -04:00
2020-11-19 21:43:58 -05:00
// Default deleteMarker to true if object is under versioning
2022-12-29 01:48:33 -05:00
deleteMarker := opts . Versioned
2021-02-09 18:11:43 -05:00
2022-12-29 01:48:33 -05:00
if opts . VersionID != "" {
2020-11-19 21:43:58 -05:00
// case where replica version needs to be deleted on target cluster
2021-09-18 16:31:35 -04:00
if versionFound && opts . DeleteMarkerReplicationStatus ( ) == replication . Replica {
2020-11-19 21:43:58 -05:00
markDelete = false
}
2021-09-18 16:31:35 -04:00
if opts . VersionPurgeStatus ( ) . Empty ( ) && opts . DeleteMarkerReplicationStatus ( ) . Empty ( ) {
2020-11-19 21:43:58 -05:00
markDelete = false
}
2021-09-18 16:31:35 -04:00
if opts . VersionPurgeStatus ( ) == Complete {
2020-11-19 21:43:58 -05:00
markDelete = false
}
2022-12-29 01:48:33 -05:00
// now, since VersionPurgeStatus() is already set, we can let the
// lower layers decide this. This fixes a regression that was introduced
// in PR #14555 where !VersionPurgeStatus.Empty() is automatically
// considered as Delete marker true to avoid listing such objects by
// regular ListObjects() calls. However for delete replication this
// ends up being a problem because "upon" a successful delete this
// ends up creating a new delete marker that is spurious and unnecessary.
//
// Regression introduced by #14555 was reintroduced in #15564
if versionFound {
if ! goi . VersionPurgeStatus . Empty ( ) {
deleteMarker = false
} else if ! goi . DeleteMarker { // implies a versioned delete of object
deleteMarker = false
}
}
2020-11-19 21:43:58 -05:00
}
2018-08-29 16:36:19 -04:00
2020-11-19 21:43:58 -05:00
modTime := opts . MTime
if opts . MTime . IsZero ( ) {
modTime = UTCNow ( )
}
2021-06-30 22:32:07 -04:00
fvID := mustGetUUID ( )
2022-08-22 18:59:06 -04:00
2023-03-31 11:15:41 -04:00
defer func ( ) {
// attempt a heal before returning if there are offline disks
// for both del marker and permanent delete situations.
for _ , disk := range storageDisks {
if disk != nil && disk . IsOnline ( ) {
continue
}
2023-04-19 10:47:42 -04:00
er . addPartial ( bucket , object , opts . VersionID )
2023-03-31 11:15:41 -04:00
break
}
} ( )
2022-08-18 19:41:59 -04:00
if markDelete && ( opts . Versioned || opts . VersionSuspended ) {
2022-12-29 01:48:33 -05:00
if ! deleteMarker {
// versioning suspended means we add `null` version as
// delete marker, if its not decided already.
deleteMarker = opts . VersionSuspended && opts . VersionID == ""
}
2022-08-18 19:41:59 -04:00
fi := FileInfo {
Name : object ,
2022-12-29 01:48:33 -05:00
Deleted : deleteMarker ,
2022-08-18 19:41:59 -04:00
MarkDeleted : markDelete ,
ModTime : modTime ,
ReplicationState : opts . DeleteReplication ,
TransitionStatus : opts . Transition . Status ,
ExpireRestored : opts . Transition . ExpireRestored ,
}
fi . SetTierFreeVersionID ( fvID )
2024-03-02 00:11:03 -05:00
if opts . SkipFreeVersion {
fi . SetSkipTierFreeVersion ( )
}
2022-12-08 21:15:16 -05:00
if opts . VersionID != "" {
fi . VersionID = opts . VersionID
} else if opts . Versioned {
2022-08-18 19:41:59 -04:00
fi . VersionID = mustGetUUID ( )
2018-08-31 16:16:35 -04:00
}
2022-08-18 19:41:59 -04:00
// versioning suspended means we add `null` version as
// delete marker. Add delete marker, since we don't have
// any version specified explicitly. Or if a particular
// version id needs to be replicated.
if err = er . deleteObjectVersion ( ctx , bucket , object , fi , opts . DeleteMarker ) ; err != nil {
return objInfo , toObjectErr ( err , bucket , object )
}
2023-07-10 10:57:56 -04:00
oi := fi . ToObjectInfo ( bucket , object , opts . Versioned || opts . VersionSuspended )
oi . replicationDecision = goi . replicationDecision
return oi , nil
2018-08-29 16:36:19 -04:00
}
2016-06-17 01:18:43 -04:00
2020-06-12 23:04:01 -04:00
// Delete the object version on all disks.
2021-06-30 22:32:07 -04:00
dfi := FileInfo {
2021-09-18 16:31:35 -04:00
Name : object ,
VersionID : opts . VersionID ,
MarkDeleted : markDelete ,
Deleted : deleteMarker ,
ModTime : modTime ,
ReplicationState : opts . DeleteReplication ,
TransitionStatus : opts . Transition . Status ,
ExpireRestored : opts . Transition . ExpireRestored ,
2021-06-30 22:32:07 -04:00
}
dfi . SetTierFreeVersionID ( fvID )
2024-03-02 00:11:03 -05:00
if opts . SkipFreeVersion {
dfi . SetSkipTierFreeVersion ( )
}
2022-08-18 19:41:59 -04:00
if err = er . deleteObjectVersion ( ctx , bucket , object , dfi , opts . DeleteMarker ) ; err != nil {
2020-06-12 23:04:01 -04:00
return objInfo , toObjectErr ( err , bucket , object )
2016-06-07 14:35:03 -04:00
}
2022-08-18 19:41:59 -04:00
return dfi . ToObjectInfo ( bucket , object , opts . Versioned || opts . VersionSuspended ) , nil
2018-02-09 18:19:30 -05:00
}
2020-01-15 21:30:32 -05:00
2020-06-29 16:07:26 -04:00
// Send the successful but partial upload/delete, however ignore
2020-01-15 21:30:32 -05:00
// if the channel is blocked by other items.
2023-04-19 10:47:42 -04:00
func ( er erasureObjects ) addPartial ( bucket , object , versionID string ) {
2024-08-13 18:26:05 -04:00
globalMRFState . addPartialOp ( PartialOperation {
Bucket : bucket ,
Object : object ,
VersionID : versionID ,
Queued : time . Now ( ) ,
2021-07-16 01:32:06 -04:00
} )
2020-01-15 21:30:32 -05:00
}
2020-01-20 11:45:59 -05:00
2021-04-04 16:32:31 -04:00
func ( er erasureObjects ) PutObjectMetadata ( ctx context . Context , bucket , object string , opts ObjectOptions ) ( ObjectInfo , error ) {
2021-12-21 13:08:26 -05:00
if ! opts . NoLock {
// Lock the object before updating metadata.
lk := er . NewNSLock ( bucket , object )
lkctx , err := lk . GetLock ( ctx , globalOperationTimeout )
if err != nil {
return ObjectInfo { } , err
}
ctx = lkctx . Context ( )
2022-12-23 22:49:07 -05:00
defer lk . Unlock ( lkctx )
2021-02-01 16:52:51 -05:00
}
2020-06-12 23:04:01 -04:00
disks := er . getDisks ( )
2020-01-20 11:45:59 -05:00
2022-04-20 15:49:05 -04:00
var metaArr [ ] FileInfo
var errs [ ] error
2020-01-20 11:45:59 -05:00
// Read metadata associated with the object from all disks.
2022-04-20 15:49:05 -04:00
if opts . VersionID != "" {
2024-01-30 15:43:25 -05:00
metaArr , errs = readAllFileInfo ( ctx , disks , "" , bucket , object , opts . VersionID , false , false )
2022-04-20 15:49:05 -04:00
} else {
2024-08-08 11:29:58 -04:00
metaArr , errs = readAllXL ( ctx , disks , bucket , object , false , false )
2022-04-20 15:49:05 -04:00
}
2020-06-12 23:04:01 -04:00
2021-04-04 16:32:31 -04:00
readQuorum , _ , err := objectQuorumFromMeta ( ctx , metaArr , errs , er . defaultParityCount )
2020-06-12 23:04:01 -04:00
if err != nil {
2022-04-20 15:49:05 -04:00
if errors . Is ( err , errErasureReadQuorum ) && ! strings . HasPrefix ( bucket , minioMetaBucket ) {
2023-12-04 17:54:34 -05:00
_ , derr := er . deleteIfDangling ( context . Background ( ) , bucket , object , metaArr , errs , nil , opts )
2024-05-09 13:15:03 -04:00
if derr == nil {
if opts . VersionID != "" {
err = errFileVersionNotFound
} else {
err = errFileNotFound
}
2022-04-20 15:49:05 -04:00
}
}
2021-02-01 16:52:51 -05:00
return ObjectInfo { } , toObjectErr ( err , bucket , object )
2020-06-12 23:04:01 -04:00
}
2020-01-20 11:45:59 -05:00
2020-06-12 23:04:01 -04:00
// List all online disks.
2023-06-17 22:18:20 -04:00
onlineDisks , modTime , etag := listOnlineDisks ( disks , metaArr , errs , readQuorum )
2020-06-12 23:04:01 -04:00
// Pick latest valid metadata.
2023-06-17 22:18:20 -04:00
fi , err := pickValidFileInfo ( ctx , metaArr , modTime , etag , readQuorum )
2020-01-20 11:45:59 -05:00
if err != nil {
2021-02-01 16:52:51 -05:00
return ObjectInfo { } , toObjectErr ( err , bucket , object )
2020-01-20 11:45:59 -05:00
}
2021-11-21 13:41:30 -05:00
2020-06-12 23:04:01 -04:00
if fi . Deleted {
2021-02-01 16:52:51 -05:00
return ObjectInfo { } , toObjectErr ( errMethodNotAllowed , bucket , object )
2020-06-12 23:04:01 -04:00
}
2021-11-21 13:41:30 -05:00
filterOnlineDisksInplace ( fi , metaArr , onlineDisks )
2021-10-30 11:22:04 -04:00
// if version-id is not specified retention is supposed to be set on the latest object.
if opts . VersionID == "" {
opts . VersionID = fi . VersionID
}
2022-05-31 05:57:57 -04:00
objInfo := fi . ToObjectInfo ( bucket , object , opts . Versioned || opts . VersionSuspended )
2021-10-30 11:22:04 -04:00
if opts . EvalMetadataFn != nil {
2023-07-10 10:57:56 -04:00
if _ , err := opts . EvalMetadataFn ( & objInfo , err ) ; err != nil {
2021-10-30 11:22:04 -04:00
return ObjectInfo { } , err
}
}
for k , v := range objInfo . UserDefined {
2021-04-04 16:32:31 -04:00
fi . Metadata [ k ] = v
2021-04-02 01:12:03 -04:00
}
2021-04-04 16:32:31 -04:00
fi . ModTime = opts . MTime
fi . VersionID = opts . VersionID
2021-04-02 01:12:03 -04:00
2021-11-21 13:41:30 -05:00
if err = er . updateObjectMeta ( ctx , bucket , object , fi , onlineDisks ) ; err != nil {
2021-02-01 16:52:51 -05:00
return ObjectInfo { } , toObjectErr ( err , bucket , object )
2020-01-20 11:45:59 -05:00
}
2022-05-31 05:57:57 -04:00
return fi . ToObjectInfo ( bucket , object , opts . Versioned || opts . VersionSuspended ) , nil
2020-01-20 11:45:59 -05:00
}
2021-04-04 16:32:31 -04:00
// PutObjectTags - replace or add tags to an existing object
func ( er erasureObjects ) PutObjectTags ( ctx context . Context , bucket , object string , tags string , opts ObjectOptions ) ( ObjectInfo , error ) {
// Lock the object before updating tags.
lk := er . NewNSLock ( bucket , object )
2021-04-29 23:55:21 -04:00
lkctx , err := lk . GetLock ( ctx , globalOperationTimeout )
2021-04-04 16:32:31 -04:00
if err != nil {
return ObjectInfo { } , err
2020-10-28 12:18:35 -04:00
}
2021-04-29 23:55:21 -04:00
ctx = lkctx . Context ( )
2022-12-23 22:49:07 -05:00
defer lk . Unlock ( lkctx )
2021-04-04 16:32:31 -04:00
2020-10-28 12:18:35 -04:00
disks := er . getDisks ( )
2022-04-20 15:49:05 -04:00
var metaArr [ ] FileInfo
var errs [ ] error
2020-10-28 12:18:35 -04:00
// Read metadata associated with the object from all disks.
2022-04-20 15:49:05 -04:00
if opts . VersionID != "" {
2024-01-30 15:43:25 -05:00
metaArr , errs = readAllFileInfo ( ctx , disks , "" , bucket , object , opts . VersionID , false , false )
2022-04-20 15:49:05 -04:00
} else {
2024-08-08 11:29:58 -04:00
metaArr , errs = readAllXL ( ctx , disks , bucket , object , false , false )
2022-04-20 15:49:05 -04:00
}
2020-10-28 12:18:35 -04:00
2021-04-04 16:32:31 -04:00
readQuorum , _ , err := objectQuorumFromMeta ( ctx , metaArr , errs , er . defaultParityCount )
2020-10-28 12:18:35 -04:00
if err != nil {
2022-04-20 15:49:05 -04:00
if errors . Is ( err , errErasureReadQuorum ) && ! strings . HasPrefix ( bucket , minioMetaBucket ) {
2023-12-04 17:54:34 -05:00
_ , derr := er . deleteIfDangling ( context . Background ( ) , bucket , object , metaArr , errs , nil , opts )
2024-05-09 13:15:03 -04:00
if derr == nil {
if opts . VersionID != "" {
err = errFileVersionNotFound
} else {
err = errFileNotFound
}
2022-04-20 15:49:05 -04:00
}
}
2021-04-04 16:32:31 -04:00
return ObjectInfo { } , toObjectErr ( err , bucket , object )
2020-10-28 12:18:35 -04:00
}
// List all online disks.
2023-06-17 22:18:20 -04:00
onlineDisks , modTime , etag := listOnlineDisks ( disks , metaArr , errs , readQuorum )
2020-10-28 12:18:35 -04:00
// Pick latest valid metadata.
2023-06-17 22:18:20 -04:00
fi , err := pickValidFileInfo ( ctx , metaArr , modTime , etag , readQuorum )
2020-10-28 12:18:35 -04:00
if err != nil {
2021-04-04 16:32:31 -04:00
return ObjectInfo { } , toObjectErr ( err , bucket , object )
}
if fi . Deleted {
if opts . VersionID == "" {
return ObjectInfo { } , toObjectErr ( errFileNotFound , bucket , object )
}
return ObjectInfo { } , toObjectErr ( errMethodNotAllowed , bucket , object )
2020-10-28 12:18:35 -04:00
}
2021-11-21 13:41:30 -05:00
filterOnlineDisksInplace ( fi , metaArr , onlineDisks )
2021-04-04 16:32:31 -04:00
fi . Metadata [ xhttp . AmzObjectTagging ] = tags
2022-01-10 22:06:10 -05:00
fi . ReplicationState = opts . PutReplicationState ( )
2021-04-04 16:32:31 -04:00
for k , v := range opts . UserDefined {
2020-10-28 12:18:35 -04:00
fi . Metadata [ k ] = v
}
2021-11-21 13:41:30 -05:00
if err = er . updateObjectMeta ( ctx , bucket , object , fi , onlineDisks ) ; err != nil {
2021-04-04 16:32:31 -04:00
return ObjectInfo { } , toObjectErr ( err , bucket , object )
2020-10-28 12:18:35 -04:00
}
2022-05-31 05:57:57 -04:00
return fi . ToObjectInfo ( bucket , object , opts . Versioned || opts . VersionSuspended ) , nil
2021-04-04 16:32:31 -04:00
}
2020-10-28 12:18:35 -04:00
2023-08-25 10:58:11 -04:00
func ( er erasureObjects ) updateObjectMetaWithOpts ( ctx context . Context , bucket , object string , fi FileInfo , onlineDisks [ ] StorageAPI , opts UpdateMetadataOpts ) error {
2021-04-04 16:32:31 -04:00
if len ( fi . Metadata ) == 0 {
return nil
2020-10-28 12:18:35 -04:00
}
2021-11-21 13:41:30 -05:00
g := errgroup . WithNErrs ( len ( onlineDisks ) )
2021-04-02 01:12:03 -04:00
2021-04-04 16:32:31 -04:00
// Start writing `xl.meta` to all disks in parallel.
2021-11-21 13:41:30 -05:00
for index := range onlineDisks {
2021-04-04 16:32:31 -04:00
index := index
g . Go ( func ( ) error {
2021-11-21 13:41:30 -05:00
if onlineDisks [ index ] == nil {
2021-04-04 16:32:31 -04:00
return errDiskNotFound
}
2023-08-25 10:58:11 -04:00
return onlineDisks [ index ] . UpdateMetadata ( ctx , bucket , object , fi , opts )
2021-04-04 16:32:31 -04:00
} , index )
2021-04-02 01:12:03 -04:00
}
2021-04-04 16:32:31 -04:00
// Wait for all the routines.
mErrs := g . Wait ( )
2020-10-28 12:18:35 -04:00
2024-02-06 00:44:40 -05:00
return reduceWriteQuorumErrs ( ctx , mErrs , objectOpIgnoredErrs , fi . WriteQuorum ( er . defaultWQuorum ( ) ) )
2020-10-28 12:18:35 -04:00
}
2023-08-25 10:58:11 -04:00
// updateObjectMeta will update the metadata of a file.
func ( er erasureObjects ) updateObjectMeta ( ctx context . Context , bucket , object string , fi FileInfo , onlineDisks [ ] StorageAPI ) error {
return er . updateObjectMetaWithOpts ( ctx , bucket , object , fi , onlineDisks , UpdateMetadataOpts { } )
}
2020-05-23 14:09:35 -04:00
// DeleteObjectTags - delete object tags from an existing object
2021-02-01 16:52:51 -05:00
func ( er erasureObjects ) DeleteObjectTags ( ctx context . Context , bucket , object string , opts ObjectOptions ) ( ObjectInfo , error ) {
2020-06-12 23:04:01 -04:00
return er . PutObjectTags ( ctx , bucket , object , "" , opts )
2020-01-20 11:45:59 -05:00
}
2020-05-23 14:09:35 -04:00
// GetObjectTags - get object tags from an existing object
2020-06-12 23:04:01 -04:00
func ( er erasureObjects ) GetObjectTags ( ctx context . Context , bucket , object string , opts ObjectOptions ) ( * tags . Tags , error ) {
2020-01-20 11:45:59 -05:00
// GetObjectInfo will return tag value as well
2020-06-12 23:04:01 -04:00
oi , err := er . GetObjectInfo ( ctx , bucket , object , opts )
2020-01-20 11:45:59 -05:00
if err != nil {
2020-05-05 17:18:13 -04:00
return nil , err
2020-01-20 11:45:59 -05:00
}
2020-05-05 17:18:13 -04:00
return tags . ParseObjectTags ( oi . UserTags )
2020-01-20 11:45:59 -05:00
}
2021-04-19 13:30:42 -04:00
// TransitionObject - transition object content to target tier.
func ( er erasureObjects ) TransitionObject ( ctx context . Context , bucket , object string , opts ObjectOptions ) error {
2024-04-17 01:09:58 -04:00
tgtClient , err := globalTierConfigMgr . getDriver ( ctx , opts . Transition . Tier )
2021-04-19 13:30:42 -04:00
if err != nil {
return err
}
2021-05-17 11:25:48 -04:00
2021-04-19 13:30:42 -04:00
// Acquire write lock before starting to transition the object.
lk := er . NewNSLock ( bucket , object )
2021-04-29 23:55:21 -04:00
lkctx , err := lk . GetLock ( ctx , globalDeleteOperationTimeout )
2021-04-19 13:30:42 -04:00
if err != nil {
return err
}
2021-04-29 23:55:21 -04:00
ctx = lkctx . Context ( )
2022-12-23 22:49:07 -05:00
defer lk . Unlock ( lkctx )
2021-04-19 13:30:42 -04:00
fi , metaArr , onlineDisks , err := er . getObjectFileInfo ( ctx , bucket , object , opts , true )
if err != nil {
return toObjectErr ( err , bucket , object )
}
if fi . Deleted {
if opts . VersionID == "" {
return toObjectErr ( errFileNotFound , bucket , object )
}
// Make sure to return object info to provide extra information.
return toObjectErr ( errMethodNotAllowed , bucket , object )
}
// verify that the object queued for transition is identical to that on disk.
if ! opts . MTime . Equal ( fi . ModTime ) || ! strings . EqualFold ( opts . Transition . ETag , extractETag ( fi . Metadata ) ) {
return toObjectErr ( errFileNotFound , bucket , object )
}
// if object already transitioned, return
if fi . TransitionStatus == lifecycle . TransitionComplete {
return nil
}
2021-08-24 15:24:00 -04:00
2021-04-19 13:30:42 -04:00
if fi . XLV1 {
if _ , err = er . HealObject ( ctx , bucket , object , "" , madmin . HealOpts { NoLock : true } ) ; err != nil {
return err
}
// Fetch FileInfo again. HealObject migrates object the latest
// format. Among other things this changes fi.DataDir and
// possibly fi.Data (if data is inlined).
fi , metaArr , onlineDisks , err = er . getObjectFileInfo ( ctx , bucket , object , opts , true )
if err != nil {
return toObjectErr ( err , bucket , object )
}
}
2023-04-11 22:22:32 -04:00
traceFn := globalLifecycleSys . trace ( fi . ToObjectInfo ( bucket , object , opts . Versioned || opts . VersionSuspended ) )
2021-07-16 12:38:27 -04:00
2021-07-20 13:49:52 -04:00
destObj , err := genTransitionObjName ( bucket )
2021-04-19 13:30:42 -04:00
if err != nil {
return err
}
2021-05-11 12:18:37 -04:00
pr , pw := xioutil . WaitPipe ( )
2021-04-19 13:30:42 -04:00
go func ( ) {
err := er . getObjectWithFileInfo ( ctx , bucket , object , 0 , fi . Size , pw , fi , metaArr , onlineDisks )
pw . CloseWithError ( err )
} ( )
2021-05-11 12:18:37 -04:00
2021-06-03 17:26:51 -04:00
var rv remoteVersionID
rv , err = tgtClient . Put ( ctx , destObj , pr , fi . Size )
2021-05-11 12:18:37 -04:00
pr . CloseWithError ( err )
if err != nil {
2021-04-19 13:30:42 -04:00
return err
}
fi . TransitionStatus = lifecycle . TransitionComplete
fi . TransitionedObjName = destObj
fi . TransitionTier = opts . Transition . Tier
2021-06-03 17:26:51 -04:00
fi . TransitionVersionID = string ( rv )
2021-04-19 13:30:42 -04:00
eventName := event . ObjectTransitionComplete
storageDisks := er . getDisks ( )
2021-09-01 11:57:42 -04:00
2022-08-18 19:41:59 -04:00
if err = er . deleteObjectVersion ( ctx , bucket , object , fi , false ) ; err != nil {
2021-04-19 13:30:42 -04:00
eventName = event . ObjectTransitionFailed
}
2021-08-24 15:24:00 -04:00
2021-04-19 13:30:42 -04:00
for _ , disk := range storageDisks {
if disk != nil && disk . IsOnline ( ) {
continue
}
2023-04-19 10:47:42 -04:00
er . addPartial ( bucket , object , opts . VersionID )
2021-04-19 13:30:42 -04:00
break
}
2021-07-30 15:45:25 -04:00
2022-05-31 05:57:57 -04:00
objInfo := fi . ToObjectInfo ( bucket , object , opts . Versioned || opts . VersionSuspended )
2021-04-19 13:30:42 -04:00
sendEvent ( eventArgs {
EventName : eventName ,
BucketName : bucket ,
2021-07-30 15:45:25 -04:00
Object : objInfo ,
2023-04-06 13:20:53 -04:00
UserAgent : "Internal: [ILM-Transition]" ,
Host : globalLocalNodeName ,
2021-04-19 13:30:42 -04:00
} )
2023-05-22 18:28:56 -04:00
tags := opts . LifecycleAuditEvent . Tags ( )
2023-04-26 20:49:00 -04:00
auditLogLifecycle ( ctx , objInfo , ILMTransition , tags , traceFn )
2021-04-19 13:30:42 -04:00
return err
}
// RestoreTransitionedObject - restore transitioned object content locally on this cluster.
// This is similar to PostObjectRestore from AWS GLACIER
// storage class. When PostObjectRestore API is called, a temporary copy of the object
// is restored locally to the bucket on source cluster until the restore expiry date.
// The copy that was transitioned continues to reside in the transitioned tier.
func ( er erasureObjects ) RestoreTransitionedObject ( ctx context . Context , bucket , object string , opts ObjectOptions ) error {
return er . restoreTransitionedObject ( ctx , bucket , object , opts )
}
// update restore status header in the metadata
2023-01-30 21:23:45 -05:00
func ( er erasureObjects ) updateRestoreMetadata ( ctx context . Context , bucket , object string , objInfo ObjectInfo , opts ObjectOptions ) error {
2021-04-19 13:30:42 -04:00
oi := objInfo . Clone ( )
oi . metadataOnly = true // Perform only metadata updates.
2023-01-30 21:23:45 -05:00
// allow retry in the case of failure to restore
delete ( oi . UserDefined , xhttp . AmzRestore )
2021-04-19 13:30:42 -04:00
if _ , err := er . CopyObject ( ctx , bucket , object , bucket , object , oi , ObjectOptions {
VersionID : oi . VersionID ,
} , ObjectOptions {
VersionID : oi . VersionID ,
} ) ; err != nil {
2024-04-04 08:04:40 -04:00
storageLogIf ( ctx , fmt . Errorf ( "Unable to update transition restore metadata for %s/%s(%s): %s" , bucket , object , oi . VersionID , err ) )
2021-04-19 13:30:42 -04:00
return err
}
return nil
}
// restoreTransitionedObject for multipart object chunks the file stream from remote tier into the same number of parts
// as in the xl.meta for this version and rehydrates the part.n into the fi.DataDir for this version as in the xl.meta
func ( er erasureObjects ) restoreTransitionedObject ( ctx context . Context , bucket string , object string , opts ObjectOptions ) error {
2021-04-23 13:52:26 -04:00
setRestoreHeaderFn := func ( oi ObjectInfo , rerr error ) error {
2023-01-30 21:23:45 -05:00
if rerr == nil {
return nil // nothing to do; restore object was successful
}
er . updateRestoreMetadata ( ctx , bucket , object , oi , opts )
2021-04-19 13:30:42 -04:00
return rerr
}
var oi ObjectInfo
// get the file info on disk for transitioned object
actualfi , _ , _ , err := er . getObjectFileInfo ( ctx , bucket , object , opts , false )
if err != nil {
2021-04-23 13:52:26 -04:00
return setRestoreHeaderFn ( oi , toObjectErr ( err , bucket , object ) )
2021-04-19 13:30:42 -04:00
}
2022-05-31 05:57:57 -04:00
oi = actualfi . ToObjectInfo ( bucket , object , opts . Versioned || opts . VersionSuspended )
2021-04-24 22:07:27 -04:00
ropts := putRestoreOpts ( bucket , object , opts . Transition . RestoreRequest , oi )
2021-04-19 13:30:42 -04:00
if len ( oi . Parts ) == 1 {
var rs * HTTPRangeSpec
gr , err := getTransitionedObjectReader ( ctx , bucket , object , rs , http . Header { } , oi , opts )
if err != nil {
2021-04-23 13:52:26 -04:00
return setRestoreHeaderFn ( oi , toObjectErr ( err , bucket , object ) )
2021-04-19 13:30:42 -04:00
}
defer gr . Close ( )
2023-09-18 13:00:54 -04:00
hashReader , err := hash . NewReader ( ctx , gr , gr . ObjInfo . Size , "" , "" , gr . ObjInfo . Size )
2021-04-19 13:30:42 -04:00
if err != nil {
2021-04-23 13:52:26 -04:00
return setRestoreHeaderFn ( oi , toObjectErr ( err , bucket , object ) )
2021-04-19 13:30:42 -04:00
}
pReader := NewPutObjReader ( hashReader )
_ , err = er . PutObject ( ctx , bucket , object , pReader , ropts )
2021-04-23 13:52:26 -04:00
return setRestoreHeaderFn ( oi , toObjectErr ( err , bucket , object ) )
2021-04-19 13:30:42 -04:00
}
2021-04-24 22:07:27 -04:00
2022-08-29 19:57:16 -04:00
res , err := er . NewMultipartUpload ( ctx , bucket , object , ropts )
2021-04-19 13:30:42 -04:00
if err != nil {
2021-04-23 13:52:26 -04:00
return setRestoreHeaderFn ( oi , err )
2021-04-19 13:30:42 -04:00
}
2021-04-24 22:07:27 -04:00
2021-04-19 13:30:42 -04:00
var uploadedParts [ ] CompletePart
var rs * HTTPRangeSpec
// get reader from the warm backend - note that even in the case of encrypted objects, this stream is still encrypted.
gr , err := getTransitionedObjectReader ( ctx , bucket , object , rs , http . Header { } , oi , opts )
if err != nil {
2021-04-23 13:52:26 -04:00
return setRestoreHeaderFn ( oi , err )
2021-04-19 13:30:42 -04:00
}
defer gr . Close ( )
2021-04-24 22:07:27 -04:00
2021-04-19 13:30:42 -04:00
// rehydrate the parts back on disk as per the original xl.meta prior to transition
for _ , partInfo := range oi . Parts {
2023-09-18 13:00:54 -04:00
hr , err := hash . NewReader ( ctx , io . LimitReader ( gr , partInfo . Size ) , partInfo . Size , "" , "" , partInfo . Size )
2021-04-19 13:30:42 -04:00
if err != nil {
2021-04-23 13:52:26 -04:00
return setRestoreHeaderFn ( oi , err )
2021-04-19 13:30:42 -04:00
}
2022-08-29 19:57:16 -04:00
pInfo , err := er . PutObjectPart ( ctx , bucket , object , res . UploadID , partInfo . Number , NewPutObjReader ( hr ) , ObjectOptions { } )
2021-04-19 13:30:42 -04:00
if err != nil {
2021-04-23 13:52:26 -04:00
return setRestoreHeaderFn ( oi , err )
2021-04-19 13:30:42 -04:00
}
2021-04-26 21:24:06 -04:00
if pInfo . Size != partInfo . Size {
return setRestoreHeaderFn ( oi , InvalidObjectState { Bucket : bucket , Object : object } )
}
2021-04-19 13:30:42 -04:00
uploadedParts = append ( uploadedParts , CompletePart {
PartNumber : pInfo . PartNumber ,
ETag : pInfo . ETag ,
} )
}
2022-08-29 19:57:16 -04:00
_ , err = er . CompleteMultipartUpload ( ctx , bucket , object , res . UploadID , uploadedParts , ObjectOptions {
2022-01-02 12:15:06 -05:00
MTime : oi . ModTime ,
} )
2021-04-26 21:24:06 -04:00
return setRestoreHeaderFn ( oi , err )
2021-04-19 13:30:42 -04:00
}
2023-03-16 10:48:05 -04:00
// DecomTieredObject - moves tiered object to another pool during decommissioning.
func ( er erasureObjects ) DecomTieredObject ( ctx context . Context , bucket , object string , fi FileInfo , opts ObjectOptions ) error {
if opts . UserDefined == nil {
opts . UserDefined = make ( map [ string ] string )
}
// overlay Erasure info for this set of disks
storageDisks := er . getDisks ( )
// Get parity and data drive count based on storage class metadata
parityDrives := globalStorageClass . GetParityForSC ( opts . UserDefined [ xhttp . AmzStorageClass ] )
if parityDrives < 0 {
parityDrives = er . defaultParityCount
}
dataDrives := len ( storageDisks ) - parityDrives
// we now know the number of blocks this object needs for data and parity.
// writeQuorum is dataBlocks + 1
writeQuorum := dataDrives
if dataDrives == parityDrives {
writeQuorum ++
}
// Initialize parts metadata
partsMetadata := make ( [ ] FileInfo , len ( storageDisks ) )
fi2 := newFileInfo ( pathJoin ( bucket , object ) , dataDrives , parityDrives )
fi . Erasure = fi2 . Erasure
// Initialize erasure metadata.
for index := range partsMetadata {
partsMetadata [ index ] = fi
partsMetadata [ index ] . Erasure . Index = index + 1
}
// Order disks according to erasure distribution
var onlineDisks [ ] StorageAPI
onlineDisks , partsMetadata = shuffleDisksAndPartsMetadata ( storageDisks , partsMetadata , fi )
2024-01-30 15:43:25 -05:00
if _ , err := writeUniqueFileInfo ( ctx , onlineDisks , "" , bucket , object , partsMetadata , writeQuorum ) ; err != nil {
2023-03-16 10:48:05 -04:00
return toObjectErr ( err , bucket , object )
}
return nil
}