2023-08-29 14:27:23 -04:00
|
|
|
// Copyright (c) 2015-2023 MinIO, Inc.
|
2022-10-03 05:10:15 -04:00
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/binary"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math/rand"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
2024-07-02 04:17:52 -04:00
|
|
|
"path/filepath"
|
2023-02-13 15:07:58 -05:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2022-10-19 00:22:21 -04:00
|
|
|
"strings"
|
2022-10-03 05:10:15 -04:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/dustin/go-humanize"
|
|
|
|
"github.com/lithammer/shortuuid/v4"
|
2023-06-19 20:53:08 -04:00
|
|
|
"github.com/minio/madmin-go/v3"
|
2023-09-22 14:11:50 -04:00
|
|
|
"github.com/minio/minio-go/v7"
|
2022-10-03 05:10:15 -04:00
|
|
|
miniogo "github.com/minio/minio-go/v7"
|
|
|
|
"github.com/minio/minio-go/v7/pkg/credentials"
|
2023-03-31 13:48:36 -04:00
|
|
|
"github.com/minio/minio-go/v7/pkg/encrypt"
|
2022-10-19 00:22:21 -04:00
|
|
|
"github.com/minio/minio-go/v7/pkg/tags"
|
2023-12-02 05:51:33 -05:00
|
|
|
"github.com/minio/minio/internal/config/batch"
|
2023-03-31 13:48:36 -04:00
|
|
|
"github.com/minio/minio/internal/crypto"
|
|
|
|
"github.com/minio/minio/internal/hash"
|
2022-10-03 05:10:15 -04:00
|
|
|
xhttp "github.com/minio/minio/internal/http"
|
2023-08-11 16:12:35 -04:00
|
|
|
"github.com/minio/minio/internal/ioutil"
|
2024-01-28 13:04:17 -05:00
|
|
|
xioutil "github.com/minio/minio/internal/ioutil"
|
2024-05-24 19:05:23 -04:00
|
|
|
"github.com/minio/pkg/v3/console"
|
|
|
|
"github.com/minio/pkg/v3/env"
|
|
|
|
"github.com/minio/pkg/v3/policy"
|
|
|
|
"github.com/minio/pkg/v3/workers"
|
2024-01-08 18:22:28 -05:00
|
|
|
"gopkg.in/yaml.v3"
|
2022-10-03 05:10:15 -04:00
|
|
|
)
|
|
|
|
|
2023-12-02 05:51:33 -05:00
|
|
|
var globalBatchConfig batch.Config
|
|
|
|
|
2024-07-02 04:17:52 -04:00
|
|
|
const (
|
|
|
|
// Keep the completed/failed job stats 3 days before removing it
|
|
|
|
oldJobsExpiration = 3 * 24 * time.Hour
|
|
|
|
)
|
|
|
|
|
2022-10-03 05:10:15 -04:00
|
|
|
// BatchJobRequest this is an internal data structure not for external consumption.
|
|
|
|
type BatchJobRequest struct {
|
|
|
|
ID string `yaml:"-" json:"name"`
|
|
|
|
User string `yaml:"-" json:"user"`
|
|
|
|
Started time.Time `yaml:"-" json:"started"`
|
|
|
|
Replicate *BatchJobReplicateV1 `yaml:"replicate" json:"replicate"`
|
2023-04-04 13:56:54 -04:00
|
|
|
KeyRotate *BatchJobKeyRotateV1 `yaml:"keyrotate" json:"keyrotate"`
|
2023-12-02 05:51:33 -05:00
|
|
|
Expire *BatchJobExpire `yaml:"expire" json:"expire"`
|
2023-03-18 02:42:43 -04:00
|
|
|
ctx context.Context `msg:"-"`
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
|
2023-08-11 14:34:43 -04:00
|
|
|
func notifyEndpoint(ctx context.Context, ri *batchJobInfo, endpoint, token string) error {
|
|
|
|
if endpoint == "" {
|
2022-10-03 05:10:15 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-11 14:34:43 -04:00
|
|
|
buf, err := json.Marshal(ri)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-03 05:10:15 -04:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2023-08-11 14:34:43 -04:00
|
|
|
req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(buf))
|
2022-10-03 05:10:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-11 14:34:43 -04:00
|
|
|
if token != "" {
|
|
|
|
req.Header.Set("Authorization", token)
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
2023-08-11 14:34:43 -04:00
|
|
|
req.Header.Set("Content-Type", "application/json")
|
2022-10-03 05:10:15 -04:00
|
|
|
|
2024-04-03 14:27:05 -04:00
|
|
|
clnt := http.Client{Transport: getRemoteInstanceTransport()}
|
2022-10-03 05:10:15 -04:00
|
|
|
resp, err := clnt.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
xhttp.DrainBody(resp.Body)
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
return errors.New(resp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-11 14:34:43 -04:00
|
|
|
// Notify notifies notification endpoint if configured regarding job failure or success.
|
|
|
|
func (r BatchJobReplicateV1) Notify(ctx context.Context, ri *batchJobInfo) error {
|
|
|
|
return notifyEndpoint(ctx, ri, r.Flags.Notify.Endpoint, r.Flags.Notify.Token)
|
|
|
|
}
|
|
|
|
|
2022-10-03 05:10:15 -04:00
|
|
|
// ReplicateFromSource - this is not implemented yet where source is 'remote' and target is local.
|
2023-06-26 12:21:29 -04:00
|
|
|
func (r *BatchJobReplicateV1) ReplicateFromSource(ctx context.Context, api ObjectLayer, core *miniogo.Core, srcObjInfo ObjectInfo, retry bool) error {
|
2023-03-31 13:48:36 -04:00
|
|
|
srcBucket := r.Source.Bucket
|
|
|
|
tgtBucket := r.Target.Bucket
|
|
|
|
srcObject := srcObjInfo.Name
|
|
|
|
tgtObject := srcObjInfo.Name
|
|
|
|
if r.Target.Prefix != "" {
|
2023-09-16 22:08:59 -04:00
|
|
|
tgtObject = pathJoin(r.Target.Prefix, srcObjInfo.Name)
|
2023-03-31 13:48:36 -04:00
|
|
|
}
|
|
|
|
|
2023-05-03 01:52:35 -04:00
|
|
|
versionID := srcObjInfo.VersionID
|
|
|
|
if r.Target.Type == BatchJobReplicateResourceS3 || r.Source.Type == BatchJobReplicateResourceS3 {
|
|
|
|
versionID = ""
|
|
|
|
}
|
2023-03-31 13:48:36 -04:00
|
|
|
if srcObjInfo.DeleteMarker {
|
|
|
|
_, err := api.DeleteObject(ctx, tgtBucket, tgtObject, ObjectOptions{
|
2023-08-04 15:09:10 -04:00
|
|
|
VersionID: versionID,
|
|
|
|
// Since we are preserving a delete marker, we have to make sure this is always true.
|
|
|
|
// regardless of the current configuration of the bucket we must preserve all versions
|
|
|
|
// on the pool being batch replicated from source.
|
|
|
|
Versioned: true,
|
2023-03-31 13:48:36 -04:00
|
|
|
MTime: srcObjInfo.ModTime,
|
|
|
|
DeleteMarker: srcObjInfo.DeleteMarker,
|
|
|
|
ReplicationRequest: true,
|
|
|
|
})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
opts := ObjectOptions{
|
2023-08-04 15:09:10 -04:00
|
|
|
VersionID: srcObjInfo.VersionID,
|
|
|
|
MTime: srcObjInfo.ModTime,
|
|
|
|
PreserveETag: srcObjInfo.ETag,
|
|
|
|
UserDefined: srcObjInfo.UserDefined,
|
2023-03-31 13:48:36 -04:00
|
|
|
}
|
2023-05-03 01:52:35 -04:00
|
|
|
if r.Target.Type == BatchJobReplicateResourceS3 || r.Source.Type == BatchJobReplicateResourceS3 {
|
|
|
|
opts.VersionID = ""
|
|
|
|
}
|
2023-03-31 13:48:36 -04:00
|
|
|
if crypto.S3.IsEncrypted(srcObjInfo.UserDefined) {
|
|
|
|
opts.ServerSideEncryption = encrypt.NewSSE()
|
|
|
|
}
|
|
|
|
slc := strings.Split(srcObjInfo.ETag, "-")
|
|
|
|
if len(slc) == 2 {
|
|
|
|
partsCount, err := strconv.Atoi(slc[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return r.copyWithMultipartfromSource(ctx, api, core, srcObjInfo, opts, partsCount)
|
|
|
|
}
|
2023-06-26 12:21:29 -04:00
|
|
|
gopts := miniogo.GetObjectOptions{
|
2023-03-31 13:48:36 -04:00
|
|
|
VersionID: srcObjInfo.VersionID,
|
|
|
|
}
|
|
|
|
if err := gopts.SetMatchETag(srcObjInfo.ETag); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rd, objInfo, _, err := core.GetObject(ctx, srcBucket, srcObject, gopts)
|
|
|
|
if err != nil {
|
2023-11-10 19:12:35 -05:00
|
|
|
return ErrorRespToObjectError(err, srcBucket, srcObject, srcObjInfo.VersionID)
|
2023-03-31 13:48:36 -04:00
|
|
|
}
|
|
|
|
defer rd.Close()
|
|
|
|
|
2023-09-18 13:00:54 -04:00
|
|
|
hr, err := hash.NewReader(ctx, rd, objInfo.Size, "", "", objInfo.Size)
|
2023-03-31 13:48:36 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pReader := NewPutObjReader(hr)
|
|
|
|
_, err = api.PutObject(ctx, tgtBucket, tgtObject, pReader, opts)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-26 12:21:29 -04:00
|
|
|
func (r *BatchJobReplicateV1) copyWithMultipartfromSource(ctx context.Context, api ObjectLayer, c *miniogo.Core, srcObjInfo ObjectInfo, opts ObjectOptions, partsCount int) (err error) {
|
2023-03-31 13:48:36 -04:00
|
|
|
srcBucket := r.Source.Bucket
|
|
|
|
tgtBucket := r.Target.Bucket
|
|
|
|
srcObject := srcObjInfo.Name
|
|
|
|
tgtObject := srcObjInfo.Name
|
|
|
|
if r.Target.Prefix != "" {
|
2023-09-16 22:08:59 -04:00
|
|
|
tgtObject = pathJoin(r.Target.Prefix, srcObjInfo.Name)
|
2023-03-31 13:48:36 -04:00
|
|
|
}
|
2023-05-03 01:52:35 -04:00
|
|
|
if r.Target.Type == BatchJobReplicateResourceS3 || r.Source.Type == BatchJobReplicateResourceS3 {
|
|
|
|
opts.VersionID = ""
|
|
|
|
}
|
2023-03-31 13:48:36 -04:00
|
|
|
var uploadedParts []CompletePart
|
|
|
|
res, err := api.NewMultipartUpload(context.Background(), tgtBucket, tgtObject, opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
// block and abort remote upload upon failure.
|
|
|
|
attempts := 1
|
|
|
|
for attempts <= 3 {
|
|
|
|
aerr := api.AbortMultipartUpload(ctx, tgtBucket, tgtObject, res.UploadID, ObjectOptions{})
|
|
|
|
if aerr == nil {
|
|
|
|
return
|
|
|
|
}
|
2024-04-04 08:04:40 -04:00
|
|
|
batchLogIf(ctx,
|
2023-03-31 13:48:36 -04:00
|
|
|
fmt.Errorf("trying %s: Unable to cleanup failed multipart replication %s on remote %s/%s: %w - this may consume space on remote cluster",
|
|
|
|
humanize.Ordinal(attempts), res.UploadID, tgtBucket, tgtObject, aerr))
|
|
|
|
attempts++
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
var (
|
|
|
|
hr *hash.Reader
|
|
|
|
pInfo PartInfo
|
|
|
|
)
|
|
|
|
|
|
|
|
for i := 0; i < partsCount; i++ {
|
2023-06-26 12:21:29 -04:00
|
|
|
gopts := miniogo.GetObjectOptions{
|
2023-03-31 13:48:36 -04:00
|
|
|
VersionID: srcObjInfo.VersionID,
|
|
|
|
PartNumber: i + 1,
|
|
|
|
}
|
|
|
|
if err := gopts.SetMatchETag(srcObjInfo.ETag); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
rd, objInfo, _, err := c.GetObject(ctx, srcBucket, srcObject, gopts)
|
|
|
|
if err != nil {
|
2023-11-10 19:12:35 -05:00
|
|
|
return ErrorRespToObjectError(err, srcBucket, srcObject, srcObjInfo.VersionID)
|
2023-03-31 13:48:36 -04:00
|
|
|
}
|
|
|
|
defer rd.Close()
|
|
|
|
|
2023-09-18 13:00:54 -04:00
|
|
|
hr, err = hash.NewReader(ctx, io.LimitReader(rd, objInfo.Size), objInfo.Size, "", "", objInfo.Size)
|
2023-03-31 13:48:36 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
pReader := NewPutObjReader(hr)
|
|
|
|
opts.PreserveETag = ""
|
|
|
|
pInfo, err = api.PutObjectPart(ctx, tgtBucket, tgtObject, res.UploadID, i+1, pReader, opts)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if pInfo.Size != objInfo.Size {
|
|
|
|
return fmt.Errorf("Part size mismatch: got %d, want %d", pInfo.Size, objInfo.Size)
|
|
|
|
}
|
|
|
|
uploadedParts = append(uploadedParts, CompletePart{
|
|
|
|
PartNumber: pInfo.PartNumber,
|
|
|
|
ETag: pInfo.ETag,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_, err = api.CompleteMultipartUpload(ctx, tgtBucket, tgtObject, res.UploadID, uploadedParts, opts)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartFromSource starts the batch replication job from remote source, resumes if there was a pending job via "job.ID"
|
|
|
|
func (r *BatchJobReplicateV1) StartFromSource(ctx context.Context, api ObjectLayer, job BatchJobRequest) error {
|
|
|
|
ri := &batchJobInfo{
|
|
|
|
JobID: job.ID,
|
|
|
|
JobType: string(job.Type()),
|
|
|
|
StartTime: job.Started,
|
|
|
|
}
|
2024-07-02 04:17:52 -04:00
|
|
|
if err := ri.loadOrInit(ctx, api, job); err != nil {
|
2023-03-31 13:48:36 -04:00
|
|
|
return err
|
|
|
|
}
|
2023-11-13 11:15:00 -05:00
|
|
|
if ri.Complete {
|
|
|
|
return nil
|
|
|
|
}
|
2023-03-31 13:48:36 -04:00
|
|
|
globalBatchJobsMetrics.save(job.ID, ri)
|
|
|
|
|
|
|
|
delay := job.Replicate.Flags.Retry.Delay
|
|
|
|
if delay == 0 {
|
|
|
|
delay = batchReplJobDefaultRetryDelay
|
|
|
|
}
|
|
|
|
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
|
2024-03-19 23:21:15 -04:00
|
|
|
hasTags := len(r.Flags.Filter.Tags) != 0
|
2023-09-22 14:11:50 -04:00
|
|
|
isMetadata := len(r.Flags.Filter.Metadata) != 0
|
|
|
|
isStorageClassOnly := len(r.Flags.Filter.Metadata) == 1 && strings.EqualFold(r.Flags.Filter.Metadata[0].Key, xhttp.AmzStorageClass)
|
|
|
|
|
2023-03-31 13:48:36 -04:00
|
|
|
skip := func(oi ObjectInfo) (ok bool) {
|
2024-07-23 03:05:53 -04:00
|
|
|
if r.Flags.Filter.OlderThan > 0 && time.Since(oi.ModTime) < r.Flags.Filter.OlderThan.D() {
|
2023-03-31 13:48:36 -04:00
|
|
|
// skip all objects that are newer than specified older duration
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-07-23 03:05:53 -04:00
|
|
|
if r.Flags.Filter.NewerThan > 0 && time.Since(oi.ModTime) >= r.Flags.Filter.NewerThan.D() {
|
2023-03-31 13:48:36 -04:00
|
|
|
// skip all objects that are older than specified newer duration
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !r.Flags.Filter.CreatedAfter.IsZero() && r.Flags.Filter.CreatedAfter.Before(oi.ModTime) {
|
|
|
|
// skip all objects that are created before the specified time.
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if !r.Flags.Filter.CreatedBefore.IsZero() && r.Flags.Filter.CreatedBefore.After(oi.ModTime) {
|
|
|
|
// skip all objects that are created after the specified time.
|
|
|
|
return true
|
|
|
|
}
|
2023-09-22 14:11:50 -04:00
|
|
|
|
2024-03-19 23:21:15 -04:00
|
|
|
if hasTags {
|
2023-03-31 13:48:36 -04:00
|
|
|
// Only parse object tags if tags filter is specified.
|
|
|
|
tagMap := map[string]string{}
|
|
|
|
tagStr := oi.UserTags
|
|
|
|
if len(tagStr) != 0 {
|
|
|
|
t, err := tags.ParseObjectTags(tagStr)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
tagMap = t.ToMap()
|
|
|
|
}
|
|
|
|
for _, kv := range r.Flags.Filter.Tags {
|
|
|
|
for t, v := range tagMap {
|
2023-08-29 14:27:23 -04:00
|
|
|
if kv.Match(BatchJobKV{Key: t, Value: v}) {
|
2023-03-31 13:48:36 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// None of the provided tags filter match skip the object
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-09-22 14:11:50 -04:00
|
|
|
for _, kv := range r.Flags.Filter.Metadata {
|
|
|
|
for k, v := range oi.UserDefined {
|
|
|
|
if !stringsHasPrefixFold(k, "x-amz-meta-") && !isStandardHeader(k) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// We only need to match x-amz-meta or standardHeaders
|
|
|
|
if kv.Match(BatchJobKV{Key: k, Value: v}) {
|
|
|
|
return true
|
2023-03-31 13:48:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-22 14:11:50 -04:00
|
|
|
// None of the provided filters match
|
2023-03-31 13:48:36 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(r.Source.Endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cred := r.Source.Creds
|
|
|
|
|
|
|
|
c, err := miniogo.New(u.Host, &miniogo.Options{
|
2023-05-21 18:16:31 -04:00
|
|
|
Creds: credentials.NewStaticV4(cred.AccessKey, cred.SecretKey, cred.SessionToken),
|
|
|
|
Secure: u.Scheme == "https",
|
2024-04-03 14:27:05 -04:00
|
|
|
Transport: getRemoteInstanceTransport(),
|
2023-05-21 18:16:31 -04:00
|
|
|
BucketLookup: lookupStyle(r.Source.Path),
|
2023-03-31 13:48:36 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.SetAppInfo("minio-"+batchJobPrefix, r.APIVersion+" "+job.ID)
|
2023-06-26 12:21:29 -04:00
|
|
|
core := &miniogo.Core{Client: c}
|
2023-03-31 13:48:36 -04:00
|
|
|
|
|
|
|
workerSize, err := strconv.Atoi(env.Get("_MINIO_BATCH_REPLICATION_WORKERS", strconv.Itoa(runtime.GOMAXPROCS(0)/2)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
wk, err := workers.New(workerSize)
|
|
|
|
if err != nil {
|
|
|
|
// invalid worker size.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
retryAttempts := ri.RetryAttempts
|
|
|
|
retry := false
|
|
|
|
for attempts := 1; attempts <= retryAttempts; attempts++ {
|
|
|
|
attempts := attempts
|
2023-05-03 01:52:35 -04:00
|
|
|
// one of source/target is s3, skip delete marker and all versions under the same object name.
|
|
|
|
s3Type := r.Target.Type == BatchJobReplicateResourceS3 || r.Source.Type == BatchJobReplicateResourceS3
|
|
|
|
minioSrc := r.Source.Type == BatchJobReplicateResourceMinIO
|
2023-03-31 13:48:36 -04:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2024-08-01 08:53:30 -04:00
|
|
|
|
|
|
|
objInfoCh := make(chan miniogo.ObjectInfo, 1)
|
|
|
|
go func() {
|
2024-09-02 08:36:43 -04:00
|
|
|
prefixes := r.Source.Prefix.F()
|
|
|
|
if len(prefixes) == 0 {
|
|
|
|
prefixes = []string{""}
|
|
|
|
}
|
|
|
|
for _, prefix := range prefixes {
|
2024-08-01 08:53:30 -04:00
|
|
|
prefixObjInfoCh := c.ListObjects(ctx, r.Source.Bucket, miniogo.ListObjectsOptions{
|
|
|
|
Prefix: strings.TrimSpace(prefix),
|
|
|
|
WithVersions: minioSrc,
|
|
|
|
Recursive: true,
|
|
|
|
WithMetadata: true,
|
|
|
|
})
|
|
|
|
for obj := range prefixObjInfoCh {
|
|
|
|
objInfoCh <- obj
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xioutil.SafeClose(objInfoCh)
|
|
|
|
}()
|
|
|
|
|
2023-05-03 01:52:35 -04:00
|
|
|
prevObj := ""
|
|
|
|
skipReplicate := false
|
|
|
|
|
2023-03-31 13:48:36 -04:00
|
|
|
for obj := range objInfoCh {
|
|
|
|
oi := toObjectInfo(r.Source.Bucket, obj.Key, obj)
|
2023-05-03 01:52:35 -04:00
|
|
|
if !minioSrc {
|
2023-09-22 14:11:50 -04:00
|
|
|
// Check if metadata filter was requested and it is expected to have
|
|
|
|
// all user metadata or just storageClass. If its only storageClass
|
|
|
|
// List() already returns relevant information for filter to be applied.
|
|
|
|
if isMetadata && !isStorageClassOnly {
|
|
|
|
oi2, err := c.StatObject(ctx, r.Source.Bucket, obj.Key, miniogo.StatObjectOptions{})
|
|
|
|
if err == nil {
|
|
|
|
oi = toObjectInfo(r.Source.Bucket, obj.Key, oi2)
|
|
|
|
} else {
|
|
|
|
if !isErrMethodNotAllowed(ErrorRespToObjectError(err, r.Source.Bucket, obj.Key)) &&
|
|
|
|
!isErrObjectNotFound(ErrorRespToObjectError(err, r.Source.Bucket, obj.Key)) {
|
2024-04-04 08:04:40 -04:00
|
|
|
batchLogIf(ctx, err)
|
2023-09-22 14:11:50 -04:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2024-03-19 23:21:15 -04:00
|
|
|
if hasTags {
|
2023-09-22 14:11:50 -04:00
|
|
|
tags, err := c.GetObjectTagging(ctx, r.Source.Bucket, obj.Key, minio.GetObjectTaggingOptions{})
|
|
|
|
if err == nil {
|
|
|
|
oi.UserTags = tags.String()
|
|
|
|
} else {
|
|
|
|
if !isErrMethodNotAllowed(ErrorRespToObjectError(err, r.Source.Bucket, obj.Key)) &&
|
|
|
|
!isErrObjectNotFound(ErrorRespToObjectError(err, r.Source.Bucket, obj.Key)) {
|
2024-04-04 08:04:40 -04:00
|
|
|
batchLogIf(ctx, err)
|
2023-09-22 14:11:50 -04:00
|
|
|
}
|
2023-05-03 01:52:35 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-31 13:48:36 -04:00
|
|
|
if skip(oi) {
|
|
|
|
continue
|
|
|
|
}
|
2023-05-03 01:52:35 -04:00
|
|
|
if obj.Key != prevObj {
|
|
|
|
prevObj = obj.Key
|
|
|
|
// skip replication of delete marker and all versions under the same object name if one of source or target is s3.
|
|
|
|
skipReplicate = obj.IsDeleteMarker && s3Type
|
|
|
|
}
|
|
|
|
if skipReplicate {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-03-31 13:48:36 -04:00
|
|
|
wk.Take()
|
|
|
|
go func() {
|
|
|
|
defer wk.Give()
|
2023-12-02 05:51:33 -05:00
|
|
|
stopFn := globalBatchJobsMetrics.trace(batchJobMetricReplication, job.ID, attempts)
|
2023-03-31 13:48:36 -04:00
|
|
|
success := true
|
|
|
|
if err := r.ReplicateFromSource(ctx, api, core, oi, retry); err != nil {
|
|
|
|
// object must be deleted concurrently, allow these failures but do not count them
|
|
|
|
if isErrVersionNotFound(err) || isErrObjectNotFound(err) {
|
|
|
|
return
|
|
|
|
}
|
2023-12-02 05:51:33 -05:00
|
|
|
stopFn(oi, err)
|
2024-04-04 08:04:40 -04:00
|
|
|
batchLogIf(ctx, err)
|
2023-03-31 13:48:36 -04:00
|
|
|
success = false
|
|
|
|
} else {
|
2023-12-02 05:51:33 -05:00
|
|
|
stopFn(oi, nil)
|
2023-03-31 13:48:36 -04:00
|
|
|
}
|
2024-05-06 16:27:52 -04:00
|
|
|
ri.trackCurrentBucketObject(r.Target.Bucket, oi, success, attempts)
|
2023-03-31 13:48:36 -04:00
|
|
|
globalBatchJobsMetrics.save(job.ID, ri)
|
|
|
|
// persist in-memory state to disk after every 10secs.
|
2024-04-04 08:04:40 -04:00
|
|
|
batchLogIf(ctx, ri.updateAfter(ctx, api, 10*time.Second, job))
|
2023-12-02 05:51:33 -05:00
|
|
|
|
|
|
|
if wait := globalBatchConfig.ReplicationWait(); wait > 0 {
|
|
|
|
time.Sleep(wait)
|
|
|
|
}
|
2023-03-31 13:48:36 -04:00
|
|
|
}()
|
|
|
|
}
|
|
|
|
wk.Wait()
|
|
|
|
|
|
|
|
ri.RetryAttempts = attempts
|
|
|
|
ri.Complete = ri.ObjectsFailed == 0
|
|
|
|
ri.Failed = ri.ObjectsFailed > 0
|
|
|
|
|
|
|
|
globalBatchJobsMetrics.save(job.ID, ri)
|
|
|
|
// persist in-memory state to disk.
|
2024-04-04 08:04:40 -04:00
|
|
|
batchLogIf(ctx, ri.updateAfter(ctx, api, 0, job))
|
2023-03-31 13:48:36 -04:00
|
|
|
|
2023-08-11 14:34:43 -04:00
|
|
|
if err := r.Notify(ctx, ri); err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
batchLogIf(ctx, fmt.Errorf("unable to notify %v", err))
|
2023-03-31 13:48:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
if ri.Failed {
|
|
|
|
ri.ObjectsFailed = 0
|
|
|
|
ri.Bucket = ""
|
|
|
|
ri.Object = ""
|
|
|
|
ri.Objects = 0
|
|
|
|
ri.BytesFailed = 0
|
|
|
|
ri.BytesTransferred = 0
|
|
|
|
retry = true // indicate we are retrying..
|
|
|
|
time.Sleep(delay + time.Duration(rnd.Float64()*float64(delay)))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
2022-10-03 05:10:15 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-31 13:48:36 -04:00
|
|
|
// toObjectInfo converts minio.ObjectInfo to ObjectInfo
|
2023-06-26 12:21:29 -04:00
|
|
|
func toObjectInfo(bucket, object string, objInfo miniogo.ObjectInfo) ObjectInfo {
|
2023-03-31 13:48:36 -04:00
|
|
|
tags, _ := tags.MapToObjectTags(objInfo.UserTags)
|
|
|
|
oi := ObjectInfo{
|
|
|
|
Bucket: bucket,
|
|
|
|
Name: object,
|
|
|
|
ModTime: objInfo.LastModified,
|
|
|
|
Size: objInfo.Size,
|
|
|
|
ETag: objInfo.ETag,
|
|
|
|
VersionID: objInfo.VersionID,
|
|
|
|
IsLatest: objInfo.IsLatest,
|
|
|
|
DeleteMarker: objInfo.IsDeleteMarker,
|
|
|
|
ContentType: objInfo.ContentType,
|
|
|
|
Expires: objInfo.Expires,
|
|
|
|
StorageClass: objInfo.StorageClass,
|
|
|
|
ReplicationStatusInternal: objInfo.ReplicationStatus,
|
|
|
|
UserTags: tags.String(),
|
|
|
|
}
|
2023-09-22 14:11:50 -04:00
|
|
|
|
2023-03-31 13:48:36 -04:00
|
|
|
oi.UserDefined = make(map[string]string, len(objInfo.Metadata))
|
|
|
|
for k, v := range objInfo.Metadata {
|
|
|
|
oi.UserDefined[k] = v[0]
|
|
|
|
}
|
2023-09-22 14:11:50 -04:00
|
|
|
|
2023-03-31 13:48:36 -04:00
|
|
|
ce, ok := oi.UserDefined[xhttp.ContentEncoding]
|
|
|
|
if !ok {
|
|
|
|
ce, ok = oi.UserDefined[strings.ToLower(xhttp.ContentEncoding)]
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
oi.ContentEncoding = ce
|
|
|
|
}
|
2023-09-22 14:11:50 -04:00
|
|
|
|
|
|
|
_, ok = oi.UserDefined[xhttp.AmzStorageClass]
|
|
|
|
if !ok {
|
|
|
|
oi.UserDefined[xhttp.AmzStorageClass] = objInfo.StorageClass
|
|
|
|
}
|
|
|
|
|
2023-11-18 22:12:44 -05:00
|
|
|
for k, v := range objInfo.UserMetadata {
|
|
|
|
oi.UserDefined[k] = v
|
|
|
|
}
|
|
|
|
|
2023-03-31 13:48:36 -04:00
|
|
|
return oi
|
|
|
|
}
|
|
|
|
|
2024-07-29 03:59:50 -04:00
|
|
|
func (r BatchJobReplicateV1) writeAsArchive(ctx context.Context, objAPI ObjectLayer, remoteClnt *minio.Client, entries []ObjectInfo, prefix string) error {
|
2023-11-22 13:51:46 -05:00
|
|
|
input := make(chan minio.SnowballObject, 1)
|
|
|
|
opts := minio.SnowballOptions{
|
|
|
|
Opts: minio.PutObjectOptions{},
|
|
|
|
InMemory: *r.Source.Snowball.InMemory,
|
|
|
|
Compress: *r.Source.Snowball.Compress,
|
|
|
|
SkipErrs: *r.Source.Snowball.SkipErrs,
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
2024-01-28 13:04:17 -05:00
|
|
|
defer xioutil.SafeClose(input)
|
2023-11-22 13:51:46 -05:00
|
|
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
gr, err := objAPI.GetObjectNInfo(ctx, r.Source.Bucket,
|
|
|
|
entry.Name, nil, nil, ObjectOptions{
|
|
|
|
VersionID: entry.VersionID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
batchLogIf(ctx, err)
|
2023-11-22 13:51:46 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-07-29 03:59:50 -04:00
|
|
|
if prefix != "" {
|
|
|
|
entry.Name = pathJoin(prefix, entry.Name)
|
|
|
|
}
|
|
|
|
|
2023-11-22 13:51:46 -05:00
|
|
|
snowballObj := minio.SnowballObject{
|
|
|
|
// Create path to store objects within the bucket.
|
|
|
|
Key: entry.Name,
|
|
|
|
Size: entry.Size,
|
|
|
|
ModTime: entry.ModTime,
|
|
|
|
VersionID: entry.VersionID,
|
|
|
|
Content: gr,
|
|
|
|
Headers: make(http.Header),
|
|
|
|
Close: func() {
|
|
|
|
gr.Close()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
opts, err := batchReplicationOpts(ctx, "", gr.ObjInfo)
|
|
|
|
if err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
batchLogIf(ctx, err)
|
2023-11-22 13:51:46 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, vals := range opts.Header() {
|
|
|
|
for _, v := range vals {
|
|
|
|
snowballObj.Headers.Add(k, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
input <- snowballObj
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Collect and upload all entries.
|
|
|
|
return remoteClnt.PutObjectsSnowball(ctx, r.Target.Bucket, opts, input)
|
|
|
|
}
|
|
|
|
|
2022-10-03 05:10:15 -04:00
|
|
|
// ReplicateToTarget read from source and replicate to configured target
|
|
|
|
func (r *BatchJobReplicateV1) ReplicateToTarget(ctx context.Context, api ObjectLayer, c *miniogo.Core, srcObjInfo ObjectInfo, retry bool) error {
|
|
|
|
srcBucket := r.Source.Bucket
|
|
|
|
tgtBucket := r.Target.Bucket
|
2022-10-11 17:36:06 -04:00
|
|
|
tgtPrefix := r.Target.Prefix
|
2022-10-03 05:10:15 -04:00
|
|
|
srcObject := srcObjInfo.Name
|
2023-05-03 01:52:35 -04:00
|
|
|
s3Type := r.Target.Type == BatchJobReplicateResourceS3 || r.Source.Type == BatchJobReplicateResourceS3
|
2022-10-03 05:10:15 -04:00
|
|
|
|
2022-10-13 17:42:10 -04:00
|
|
|
if srcObjInfo.DeleteMarker || !srcObjInfo.VersionPurgeStatus.Empty() {
|
2023-05-03 01:52:35 -04:00
|
|
|
if retry && !s3Type {
|
2022-10-13 17:42:10 -04:00
|
|
|
if _, err := c.StatObject(ctx, tgtBucket, pathJoin(tgtPrefix, srcObject), miniogo.StatObjectOptions{
|
|
|
|
VersionID: srcObjInfo.VersionID,
|
|
|
|
Internal: miniogo.AdvancedGetOptions{
|
|
|
|
ReplicationProxyRequest: "false",
|
|
|
|
},
|
|
|
|
}); isErrMethodNotAllowed(ErrorRespToObjectError(err, tgtBucket, pathJoin(tgtPrefix, srcObject))) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
versionID := srcObjInfo.VersionID
|
|
|
|
dmVersionID := ""
|
|
|
|
if srcObjInfo.VersionPurgeStatus.Empty() {
|
|
|
|
dmVersionID = srcObjInfo.VersionID
|
|
|
|
}
|
2023-05-03 01:52:35 -04:00
|
|
|
if r.Target.Type == BatchJobReplicateResourceS3 || r.Source.Type == BatchJobReplicateResourceS3 {
|
|
|
|
dmVersionID = ""
|
|
|
|
versionID = ""
|
|
|
|
}
|
2022-10-13 17:42:10 -04:00
|
|
|
return c.RemoveObject(ctx, tgtBucket, pathJoin(tgtPrefix, srcObject), miniogo.RemoveObjectOptions{
|
|
|
|
VersionID: versionID,
|
|
|
|
Internal: miniogo.AdvancedRemoveOptions{
|
|
|
|
ReplicationDeleteMarker: dmVersionID != "",
|
|
|
|
ReplicationMTime: srcObjInfo.ModTime,
|
|
|
|
ReplicationStatus: miniogo.ReplicationStatusReplica,
|
|
|
|
ReplicationRequest: true, // always set this to distinguish between `mc mirror` replication and serverside
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-05-03 01:52:35 -04:00
|
|
|
if retry && !s3Type { // when we are retrying avoid copying if necessary.
|
2022-10-03 05:10:15 -04:00
|
|
|
gopts := miniogo.GetObjectOptions{}
|
|
|
|
if err := gopts.SetMatchETag(srcObjInfo.ETag); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-10-13 17:42:10 -04:00
|
|
|
if _, err := c.StatObject(ctx, tgtBucket, pathJoin(tgtPrefix, srcObject), gopts); err == nil {
|
2022-10-03 05:10:15 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-13 17:42:10 -04:00
|
|
|
versioned := globalBucketVersioningSys.PrefixEnabled(srcBucket, srcObject)
|
|
|
|
versionSuspended := globalBucketVersioningSys.PrefixSuspended(srcBucket, srcObject)
|
|
|
|
|
|
|
|
opts := ObjectOptions{
|
|
|
|
VersionID: srcObjInfo.VersionID,
|
|
|
|
Versioned: versioned,
|
|
|
|
VersionSuspended: versionSuspended,
|
|
|
|
}
|
2023-04-17 15:16:37 -04:00
|
|
|
rd, err := api.GetObjectNInfo(ctx, srcBucket, srcObject, nil, http.Header{}, opts)
|
2022-10-03 05:10:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rd.Close()
|
|
|
|
objInfo := rd.ObjInfo
|
|
|
|
|
|
|
|
size, err := objInfo.GetActualSize()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
putOpts, err := batchReplicationOpts(ctx, "", objInfo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-03 01:52:35 -04:00
|
|
|
if r.Target.Type == BatchJobReplicateResourceS3 || r.Source.Type == BatchJobReplicateResourceS3 {
|
|
|
|
putOpts.Internal = miniogo.AdvancedPutOptions{}
|
|
|
|
}
|
2022-10-03 05:10:15 -04:00
|
|
|
if objInfo.isMultipart() {
|
2022-10-11 17:36:06 -04:00
|
|
|
if err := replicateObjectWithMultipart(ctx, c, tgtBucket, pathJoin(tgtPrefix, objInfo.Name), rd, objInfo, putOpts); err != nil {
|
2022-10-03 05:10:15 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
2022-10-11 17:36:06 -04:00
|
|
|
if _, err = c.PutObject(ctx, tgtBucket, pathJoin(tgtPrefix, objInfo.Name), rd, size, "", "", putOpts); err != nil {
|
2022-10-03 05:10:15 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:generate msgp -file $GOFILE -unexported
|
|
|
|
|
|
|
|
// batchJobInfo current batch replication information
|
|
|
|
type batchJobInfo struct {
|
2023-02-13 15:07:58 -05:00
|
|
|
mu sync.RWMutex `json:"-" msg:"-"`
|
|
|
|
|
2022-10-03 05:10:15 -04:00
|
|
|
Version int `json:"-" msg:"v"`
|
|
|
|
JobID string `json:"jobID" msg:"jid"`
|
|
|
|
JobType string `json:"jobType" msg:"jt"`
|
|
|
|
StartTime time.Time `json:"startTime" msg:"st"`
|
|
|
|
LastUpdate time.Time `json:"lastUpdate" msg:"lu"`
|
|
|
|
RetryAttempts int `json:"retryAttempts" msg:"ra"`
|
2024-05-06 16:27:52 -04:00
|
|
|
Attempts int `json:"attempts" msg:"at"`
|
2022-10-03 05:10:15 -04:00
|
|
|
|
|
|
|
Complete bool `json:"complete" msg:"cmp"`
|
|
|
|
Failed bool `json:"failed" msg:"fld"`
|
|
|
|
|
|
|
|
// Last bucket/object batch replicated
|
|
|
|
Bucket string `json:"-" msg:"lbkt"`
|
|
|
|
Object string `json:"-" msg:"lobj"`
|
|
|
|
|
|
|
|
// Verbose information
|
2022-10-13 17:42:10 -04:00
|
|
|
Objects int64 `json:"objects" msg:"ob"`
|
|
|
|
DeleteMarkers int64 `json:"deleteMarkers" msg:"dm"`
|
|
|
|
ObjectsFailed int64 `json:"objectsFailed" msg:"obf"`
|
|
|
|
DeleteMarkersFailed int64 `json:"deleteMarkersFailed" msg:"dmf"`
|
|
|
|
BytesTransferred int64 `json:"bytesTransferred" msg:"bt"`
|
|
|
|
BytesFailed int64 `json:"bytesFailed" msg:"bf"`
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2024-03-07 13:58:22 -05:00
|
|
|
batchReplName = "batch-replicate.bin"
|
|
|
|
batchReplFormat = 1
|
|
|
|
batchReplVersionV1 = 1
|
|
|
|
batchReplVersion = batchReplVersionV1
|
|
|
|
batchJobName = "job.bin"
|
|
|
|
batchJobPrefix = "batch-jobs"
|
|
|
|
batchJobReportsPrefix = batchJobPrefix + "/reports"
|
2022-10-03 05:10:15 -04:00
|
|
|
|
|
|
|
batchReplJobAPIVersion = "v1"
|
|
|
|
batchReplJobDefaultRetries = 3
|
2024-07-26 08:55:50 -04:00
|
|
|
batchReplJobDefaultRetryDelay = time.Second
|
2022-10-03 05:10:15 -04:00
|
|
|
)
|
|
|
|
|
2024-07-02 04:17:52 -04:00
|
|
|
func getJobPath(job BatchJobRequest) string {
|
|
|
|
return pathJoin(batchJobPrefix, job.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ri *batchJobInfo) getJobReportPath() (string, error) {
|
2023-04-04 13:56:54 -04:00
|
|
|
var fileName string
|
2024-07-02 04:17:52 -04:00
|
|
|
switch madmin.BatchJobType(ri.JobType) {
|
|
|
|
case madmin.BatchJobReplicate:
|
2023-04-04 13:56:54 -04:00
|
|
|
fileName = batchReplName
|
2024-07-02 04:17:52 -04:00
|
|
|
case madmin.BatchJobKeyRotate:
|
2024-03-07 13:58:22 -05:00
|
|
|
fileName = batchKeyRotationName
|
2024-07-02 04:17:52 -04:00
|
|
|
case madmin.BatchJobExpire:
|
2024-03-07 13:58:22 -05:00
|
|
|
fileName = batchExpireName
|
2024-07-02 04:17:52 -04:00
|
|
|
default:
|
|
|
|
return "", fmt.Errorf("unknown job type: %v", ri.JobType)
|
2024-03-07 13:58:22 -05:00
|
|
|
}
|
2024-07-02 04:17:52 -04:00
|
|
|
return pathJoin(batchJobReportsPrefix, ri.JobID, fileName), nil
|
2024-03-07 13:58:22 -05:00
|
|
|
}
|
|
|
|
|
2024-07-02 04:17:52 -04:00
|
|
|
func (ri *batchJobInfo) loadOrInit(ctx context.Context, api ObjectLayer, job BatchJobRequest) error {
|
|
|
|
err := ri.load(ctx, api, job)
|
|
|
|
if errors.Is(err, errNoSuchJob) {
|
|
|
|
switch {
|
|
|
|
case job.Replicate != nil:
|
|
|
|
ri.Version = batchReplVersionV1
|
|
|
|
ri.RetryAttempts = batchReplJobDefaultRetries
|
|
|
|
if job.Replicate.Flags.Retry.Attempts > 0 {
|
|
|
|
ri.RetryAttempts = job.Replicate.Flags.Retry.Attempts
|
|
|
|
}
|
|
|
|
case job.KeyRotate != nil:
|
|
|
|
ri.Version = batchKeyRotateVersionV1
|
|
|
|
ri.RetryAttempts = batchKeyRotateJobDefaultRetries
|
|
|
|
if job.KeyRotate.Flags.Retry.Attempts > 0 {
|
|
|
|
ri.RetryAttempts = job.KeyRotate.Flags.Retry.Attempts
|
|
|
|
}
|
|
|
|
case job.Expire != nil:
|
|
|
|
ri.Version = batchExpireVersionV1
|
|
|
|
ri.RetryAttempts = batchExpireJobDefaultRetries
|
|
|
|
if job.Expire.Retry.Attempts > 0 {
|
|
|
|
ri.RetryAttempts = job.Expire.Retry.Attempts
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
2024-03-07 13:58:22 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ri *batchJobInfo) load(ctx context.Context, api ObjectLayer, job BatchJobRequest) error {
|
2024-07-02 04:17:52 -04:00
|
|
|
path, err := job.getJobReportPath()
|
|
|
|
if err != nil {
|
|
|
|
batchLogIf(ctx, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return ri.loadByPath(ctx, api, path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ri *batchJobInfo) loadByPath(ctx context.Context, api ObjectLayer, path string) error {
|
2024-03-07 13:58:22 -05:00
|
|
|
var format, version uint16
|
2024-07-02 04:17:52 -04:00
|
|
|
switch filepath.Base(path) {
|
|
|
|
case batchReplName:
|
2023-04-04 13:56:54 -04:00
|
|
|
version = batchReplVersionV1
|
|
|
|
format = batchReplFormat
|
2024-07-02 04:17:52 -04:00
|
|
|
case batchKeyRotationName:
|
2023-04-04 13:56:54 -04:00
|
|
|
version = batchKeyRotateVersionV1
|
|
|
|
format = batchKeyRotationFormat
|
2024-07-02 04:17:52 -04:00
|
|
|
case batchExpireName:
|
2023-12-02 05:51:33 -05:00
|
|
|
version = batchExpireVersionV1
|
|
|
|
format = batchExpireFormat
|
|
|
|
default:
|
|
|
|
return errors.New("no supported batch job request specified")
|
2023-04-04 13:56:54 -04:00
|
|
|
}
|
2024-07-02 04:17:52 -04:00
|
|
|
|
|
|
|
data, err := readConfig(ctx, api, path)
|
2022-10-03 05:10:15 -04:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errConfigNotFound) || isErrObjectNotFound(err) {
|
2024-07-02 04:17:52 -04:00
|
|
|
return errNoSuchJob
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if len(data) == 0 {
|
|
|
|
// Seems to be empty create a new batchRepl object.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if len(data) <= 4 {
|
2023-04-04 13:56:54 -04:00
|
|
|
return fmt.Errorf("%s: no data", ri.JobType)
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
// Read header
|
|
|
|
switch binary.LittleEndian.Uint16(data[0:2]) {
|
2023-04-04 13:56:54 -04:00
|
|
|
case format:
|
2022-10-03 05:10:15 -04:00
|
|
|
default:
|
2023-04-04 13:56:54 -04:00
|
|
|
return fmt.Errorf("%s: unknown format: %d", ri.JobType, binary.LittleEndian.Uint16(data[0:2]))
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
switch binary.LittleEndian.Uint16(data[2:4]) {
|
2023-04-04 13:56:54 -04:00
|
|
|
case version:
|
2022-10-03 05:10:15 -04:00
|
|
|
default:
|
2023-04-04 13:56:54 -04:00
|
|
|
return fmt.Errorf("%s: unknown version: %d", ri.JobType, binary.LittleEndian.Uint16(data[2:4]))
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
|
2023-02-13 15:07:58 -05:00
|
|
|
ri.mu.Lock()
|
|
|
|
defer ri.mu.Unlock()
|
|
|
|
|
2022-10-03 05:10:15 -04:00
|
|
|
// OK, parse data.
|
|
|
|
if _, err = ri.UnmarshalMsg(data[4:]); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ri.Version {
|
|
|
|
case batchReplVersionV1:
|
|
|
|
default:
|
2023-04-04 13:56:54 -04:00
|
|
|
return fmt.Errorf("unexpected batch %s meta version: %d", ri.JobType, ri.Version)
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-13 15:07:58 -05:00
|
|
|
func (ri *batchJobInfo) clone() *batchJobInfo {
|
|
|
|
ri.mu.RLock()
|
|
|
|
defer ri.mu.RUnlock()
|
|
|
|
|
|
|
|
return &batchJobInfo{
|
2022-10-03 05:10:15 -04:00
|
|
|
Version: ri.Version,
|
|
|
|
JobID: ri.JobID,
|
|
|
|
JobType: ri.JobType,
|
|
|
|
RetryAttempts: ri.RetryAttempts,
|
|
|
|
Complete: ri.Complete,
|
|
|
|
Failed: ri.Failed,
|
|
|
|
StartTime: ri.StartTime,
|
|
|
|
LastUpdate: ri.LastUpdate,
|
|
|
|
Bucket: ri.Bucket,
|
|
|
|
Object: ri.Object,
|
|
|
|
Objects: ri.Objects,
|
|
|
|
ObjectsFailed: ri.ObjectsFailed,
|
|
|
|
BytesTransferred: ri.BytesTransferred,
|
|
|
|
BytesFailed: ri.BytesFailed,
|
2024-05-06 16:27:52 -04:00
|
|
|
Attempts: ri.Attempts,
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-06 16:27:52 -04:00
|
|
|
func (ri *batchJobInfo) countItem(size int64, dmarker, success bool, attempt int) {
|
2022-10-03 05:10:15 -04:00
|
|
|
if ri == nil {
|
|
|
|
return
|
|
|
|
}
|
2024-05-06 16:27:52 -04:00
|
|
|
ri.Attempts++
|
2022-10-03 05:10:15 -04:00
|
|
|
if success {
|
2022-10-13 17:42:10 -04:00
|
|
|
if dmarker {
|
|
|
|
ri.DeleteMarkers++
|
|
|
|
} else {
|
|
|
|
ri.Objects++
|
|
|
|
ri.BytesTransferred += size
|
|
|
|
}
|
2024-05-06 16:27:52 -04:00
|
|
|
if attempt > 1 {
|
|
|
|
if dmarker {
|
|
|
|
ri.DeleteMarkersFailed--
|
|
|
|
} else {
|
|
|
|
ri.ObjectsFailed--
|
|
|
|
ri.BytesFailed += size
|
|
|
|
}
|
|
|
|
}
|
2022-10-03 05:10:15 -04:00
|
|
|
} else {
|
2024-05-06 16:27:52 -04:00
|
|
|
if attempt > 1 {
|
|
|
|
// Only count first attempt
|
|
|
|
return
|
|
|
|
}
|
2022-10-13 17:42:10 -04:00
|
|
|
if dmarker {
|
|
|
|
ri.DeleteMarkersFailed++
|
|
|
|
} else {
|
|
|
|
ri.ObjectsFailed++
|
|
|
|
ri.BytesFailed += size
|
|
|
|
}
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-04 13:56:54 -04:00
|
|
|
func (ri *batchJobInfo) updateAfter(ctx context.Context, api ObjectLayer, duration time.Duration, job BatchJobRequest) error {
|
2022-10-03 05:10:15 -04:00
|
|
|
if ri == nil {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
now := UTCNow()
|
2023-02-13 15:07:58 -05:00
|
|
|
ri.mu.Lock()
|
2023-04-04 13:56:54 -04:00
|
|
|
var (
|
2024-03-07 13:58:22 -05:00
|
|
|
format, version uint16
|
|
|
|
jobTyp string
|
2023-04-04 13:56:54 -04:00
|
|
|
)
|
|
|
|
|
2022-10-03 05:10:15 -04:00
|
|
|
if now.Sub(ri.LastUpdate) >= duration {
|
2023-04-04 13:56:54 -04:00
|
|
|
switch job.Type() {
|
|
|
|
case madmin.BatchJobReplicate:
|
|
|
|
format = batchReplFormat
|
|
|
|
version = batchReplVersion
|
|
|
|
jobTyp = string(job.Type())
|
|
|
|
ri.Version = batchReplVersionV1
|
|
|
|
case madmin.BatchJobKeyRotate:
|
|
|
|
format = batchKeyRotationFormat
|
|
|
|
version = batchKeyRotateVersion
|
|
|
|
jobTyp = string(job.Type())
|
|
|
|
ri.Version = batchKeyRotateVersionV1
|
2023-12-02 05:51:33 -05:00
|
|
|
case madmin.BatchJobExpire:
|
|
|
|
format = batchExpireFormat
|
|
|
|
version = batchExpireVersion
|
|
|
|
jobTyp = string(job.Type())
|
|
|
|
ri.Version = batchExpireVersionV1
|
2023-04-04 13:56:54 -04:00
|
|
|
default:
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
2022-10-03 05:10:15 -04:00
|
|
|
if serverDebugLog {
|
2023-04-04 13:56:54 -04:00
|
|
|
console.Debugf("%s: persisting info on drive: threshold:%s, %s:%#v\n", jobTyp, now.Sub(ri.LastUpdate), jobTyp, ri)
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
ri.LastUpdate = now
|
2023-02-13 15:07:58 -05:00
|
|
|
|
|
|
|
data := make([]byte, 4, ri.Msgsize()+4)
|
|
|
|
|
|
|
|
// Initialize the header.
|
2023-04-04 13:56:54 -04:00
|
|
|
binary.LittleEndian.PutUint16(data[0:2], format)
|
|
|
|
binary.LittleEndian.PutUint16(data[2:4], version)
|
2023-02-13 15:07:58 -05:00
|
|
|
|
|
|
|
buf, err := ri.MarshalMsg(data)
|
|
|
|
ri.mu.Unlock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-07-02 04:17:52 -04:00
|
|
|
path, err := ri.getJobReportPath()
|
|
|
|
if err != nil {
|
|
|
|
batchLogIf(ctx, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return saveConfig(ctx, api, path, buf)
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
2023-02-13 15:07:58 -05:00
|
|
|
ri.mu.Unlock()
|
2022-10-03 05:10:15 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-02 05:51:33 -05:00
|
|
|
// Note: to be used only with batch jobs that affect multiple versions through
|
|
|
|
// a single action. e.g batch-expire has an option to expire all versions of an
|
|
|
|
// object which matches the given filters.
|
|
|
|
func (ri *batchJobInfo) trackMultipleObjectVersions(bucket string, info ObjectInfo, success bool) {
|
|
|
|
if success {
|
|
|
|
ri.Objects += int64(info.NumVersions)
|
|
|
|
} else {
|
|
|
|
ri.ObjectsFailed += int64(info.NumVersions)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-06 16:27:52 -04:00
|
|
|
func (ri *batchJobInfo) trackCurrentBucketObject(bucket string, info ObjectInfo, success bool, attempt int) {
|
2022-10-03 05:10:15 -04:00
|
|
|
if ri == nil {
|
|
|
|
return
|
|
|
|
}
|
2023-02-13 15:07:58 -05:00
|
|
|
|
|
|
|
ri.mu.Lock()
|
|
|
|
defer ri.mu.Unlock()
|
|
|
|
|
2024-07-08 21:45:54 -04:00
|
|
|
if success {
|
|
|
|
ri.Bucket = bucket
|
|
|
|
ri.Object = info.Name
|
|
|
|
}
|
2024-05-06 16:27:52 -04:00
|
|
|
ri.countItem(info.Size, info.DeleteMarker, success, attempt)
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
|
2024-02-13 10:33:27 -05:00
|
|
|
func (ri *batchJobInfo) trackCurrentBucketBatch(bucket string, batch []ObjectInfo) {
|
|
|
|
if ri == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ri.mu.Lock()
|
|
|
|
defer ri.mu.Unlock()
|
|
|
|
|
|
|
|
ri.Bucket = bucket
|
|
|
|
for i := range batch {
|
|
|
|
ri.Object = batch[i].Name
|
2024-05-06 16:27:52 -04:00
|
|
|
ri.countItem(batch[i].Size, batch[i].DeleteMarker, true, 1)
|
2024-02-13 10:33:27 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 05:10:15 -04:00
|
|
|
// Start start the batch replication job, resumes if there was a pending job via "job.ID"
|
|
|
|
func (r *BatchJobReplicateV1) Start(ctx context.Context, api ObjectLayer, job BatchJobRequest) error {
|
|
|
|
ri := &batchJobInfo{
|
|
|
|
JobID: job.ID,
|
|
|
|
JobType: string(job.Type()),
|
|
|
|
StartTime: job.Started,
|
|
|
|
}
|
2024-07-02 04:17:52 -04:00
|
|
|
if err := ri.loadOrInit(ctx, api, job); err != nil {
|
2022-10-03 05:10:15 -04:00
|
|
|
return err
|
|
|
|
}
|
2023-11-13 11:15:00 -05:00
|
|
|
if ri.Complete {
|
|
|
|
return nil
|
|
|
|
}
|
2023-02-13 15:07:58 -05:00
|
|
|
globalBatchJobsMetrics.save(job.ID, ri)
|
2022-10-03 05:10:15 -04:00
|
|
|
lastObject := ri.Object
|
|
|
|
|
|
|
|
delay := job.Replicate.Flags.Retry.Delay
|
2024-07-26 08:55:50 -04:00
|
|
|
if delay < time.Second {
|
2022-10-03 05:10:15 -04:00
|
|
|
delay = batchReplJobDefaultRetryDelay
|
|
|
|
}
|
|
|
|
rnd := rand.New(rand.NewSource(time.Now().UnixNano()))
|
|
|
|
|
2023-09-22 14:11:50 -04:00
|
|
|
selectObj := func(info FileInfo) (ok bool) {
|
2024-07-23 03:05:53 -04:00
|
|
|
if r.Flags.Filter.OlderThan > 0 && time.Since(info.ModTime) < r.Flags.Filter.OlderThan.D() {
|
2022-10-03 05:10:15 -04:00
|
|
|
// skip all objects that are newer than specified older duration
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-07-23 03:05:53 -04:00
|
|
|
if r.Flags.Filter.NewerThan > 0 && time.Since(info.ModTime) >= r.Flags.Filter.NewerThan.D() {
|
2022-10-03 05:10:15 -04:00
|
|
|
// skip all objects that are older than specified newer duration
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !r.Flags.Filter.CreatedAfter.IsZero() && r.Flags.Filter.CreatedAfter.Before(info.ModTime) {
|
|
|
|
// skip all objects that are created before the specified time.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if !r.Flags.Filter.CreatedBefore.IsZero() && r.Flags.Filter.CreatedBefore.After(info.ModTime) {
|
|
|
|
// skip all objects that are created after the specified time.
|
|
|
|
return false
|
2022-10-19 00:22:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.Flags.Filter.Tags) > 0 {
|
|
|
|
// Only parse object tags if tags filter is specified.
|
|
|
|
tagMap := map[string]string{}
|
|
|
|
tagStr := info.Metadata[xhttp.AmzObjectTagging]
|
|
|
|
if len(tagStr) != 0 {
|
|
|
|
t, err := tags.ParseObjectTags(tagStr)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
tagMap = t.ToMap()
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, kv := range r.Flags.Filter.Tags {
|
|
|
|
for t, v := range tagMap {
|
2023-08-29 14:27:23 -04:00
|
|
|
if kv.Match(BatchJobKV{Key: t, Value: v}) {
|
2022-10-19 00:22:21 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// None of the provided tags filter match skip the object
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(r.Flags.Filter.Metadata) > 0 {
|
|
|
|
for _, kv := range r.Flags.Filter.Metadata {
|
|
|
|
for k, v := range info.Metadata {
|
2023-07-06 19:02:08 -04:00
|
|
|
if !stringsHasPrefixFold(k, "x-amz-meta-") && !isStandardHeader(k) {
|
2022-10-19 00:22:21 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// We only need to match x-amz-meta or standardHeaders
|
2023-08-29 14:27:23 -04:00
|
|
|
if kv.Match(BatchJobKV{Key: k, Value: v}) {
|
2022-10-19 00:22:21 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// None of the provided metadata filters match skip the object.
|
|
|
|
return false
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
|
2023-12-06 21:17:03 -05:00
|
|
|
// if one of source or target is non MinIO, just replicate the top most version like `mc mirror`
|
|
|
|
return !((r.Target.Type == BatchJobReplicateResourceS3 || r.Source.Type == BatchJobReplicateResourceS3) && !info.IsLatest)
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(r.Target.Endpoint)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
cred := r.Target.Creds
|
|
|
|
|
|
|
|
c, err := miniogo.NewCore(u.Host, &miniogo.Options{
|
2023-05-21 18:16:31 -04:00
|
|
|
Creds: credentials.NewStaticV4(cred.AccessKey, cred.SecretKey, cred.SessionToken),
|
|
|
|
Secure: u.Scheme == "https",
|
2024-04-03 14:27:05 -04:00
|
|
|
Transport: getRemoteInstanceTransport(),
|
2023-05-21 18:16:31 -04:00
|
|
|
BucketLookup: lookupStyle(r.Target.Path),
|
2022-10-03 05:10:15 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-22 13:51:46 -05:00
|
|
|
|
2023-02-14 16:19:30 -05:00
|
|
|
c.SetAppInfo("minio-"+batchJobPrefix, r.APIVersion+" "+job.ID)
|
2022-10-03 05:10:15 -04:00
|
|
|
|
fix panic in batch replicate (#20014)
Fixes:
```
panic: send on closed channel
panic: close of closed channel
goroutine 878 [running]:
github.com/minio/minio/internal/ioutil.SafeClose[...](...)
/Users/kp/code/src/github.com/minio/minio/internal/ioutil/ioutil.go:407
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2229 +0xc0
panic({0x108c25e60?, 0x1090b28d0?})
/usr/local/go/src/runtime/panic.go:770 +0x124
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.3({{0x1400e397316, 0x5}, {0x1400d88b8a8, 0x8}, {0x1f99d80, 0xede101c42, 0x0}, 0x3bc, 0x0, 0x0, ...})
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2235 +0xb4
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2277 +0xabc
created by github.com/minio/minio/cmd.(*erasureServerPools).Walk in goroutine 575
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2210 +0x33c
```
2024-06-28 21:20:47 -04:00
|
|
|
retryAttempts := ri.RetryAttempts
|
|
|
|
retry := false
|
|
|
|
for attempts := 1; attempts <= retryAttempts; attempts++ {
|
|
|
|
attempts := attempts
|
|
|
|
var (
|
|
|
|
walkCh = make(chan itemOrErr[ObjectInfo], 100)
|
|
|
|
slowCh = make(chan itemOrErr[ObjectInfo], 100)
|
|
|
|
)
|
2023-11-22 13:51:46 -05:00
|
|
|
|
2024-07-26 08:55:50 -04:00
|
|
|
if r.Source.Snowball.Disable != nil && !*r.Source.Snowball.Disable && r.Source.Type.isMinio() && r.Target.Type.isMinio() {
|
fix panic in batch replicate (#20014)
Fixes:
```
panic: send on closed channel
panic: close of closed channel
goroutine 878 [running]:
github.com/minio/minio/internal/ioutil.SafeClose[...](...)
/Users/kp/code/src/github.com/minio/minio/internal/ioutil/ioutil.go:407
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2229 +0xc0
panic({0x108c25e60?, 0x1090b28d0?})
/usr/local/go/src/runtime/panic.go:770 +0x124
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.3({{0x1400e397316, 0x5}, {0x1400d88b8a8, 0x8}, {0x1f99d80, 0xede101c42, 0x0}, 0x3bc, 0x0, 0x0, ...})
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2235 +0xb4
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2277 +0xabc
created by github.com/minio/minio/cmd.(*erasureServerPools).Walk in goroutine 575
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2210 +0x33c
```
2024-06-28 21:20:47 -04:00
|
|
|
go func() {
|
|
|
|
// Snowball currently needs the high level minio-go Client, not the Core one
|
|
|
|
cl, err := miniogo.New(u.Host, &miniogo.Options{
|
|
|
|
Creds: credentials.NewStaticV4(cred.AccessKey, cred.SecretKey, cred.SessionToken),
|
|
|
|
Secure: u.Scheme == "https",
|
|
|
|
Transport: getRemoteInstanceTransport(),
|
|
|
|
BucketLookup: lookupStyle(r.Target.Path),
|
|
|
|
})
|
|
|
|
if err != nil {
|
2024-07-26 08:55:50 -04:00
|
|
|
batchLogOnceIf(ctx, err, job.ID+"miniogo.New")
|
fix panic in batch replicate (#20014)
Fixes:
```
panic: send on closed channel
panic: close of closed channel
goroutine 878 [running]:
github.com/minio/minio/internal/ioutil.SafeClose[...](...)
/Users/kp/code/src/github.com/minio/minio/internal/ioutil/ioutil.go:407
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2229 +0xc0
panic({0x108c25e60?, 0x1090b28d0?})
/usr/local/go/src/runtime/panic.go:770 +0x124
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.3({{0x1400e397316, 0x5}, {0x1400d88b8a8, 0x8}, {0x1f99d80, 0xede101c42, 0x0}, 0x3bc, 0x0, 0x0, ...})
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2235 +0xb4
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2277 +0xabc
created by github.com/minio/minio/cmd.(*erasureServerPools).Walk in goroutine 575
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2210 +0x33c
```
2024-06-28 21:20:47 -04:00
|
|
|
return
|
|
|
|
}
|
2023-11-22 13:51:46 -05:00
|
|
|
|
fix panic in batch replicate (#20014)
Fixes:
```
panic: send on closed channel
panic: close of closed channel
goroutine 878 [running]:
github.com/minio/minio/internal/ioutil.SafeClose[...](...)
/Users/kp/code/src/github.com/minio/minio/internal/ioutil/ioutil.go:407
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2229 +0xc0
panic({0x108c25e60?, 0x1090b28d0?})
/usr/local/go/src/runtime/panic.go:770 +0x124
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.3({{0x1400e397316, 0x5}, {0x1400d88b8a8, 0x8}, {0x1f99d80, 0xede101c42, 0x0}, 0x3bc, 0x0, 0x0, ...})
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2235 +0xb4
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2277 +0xabc
created by github.com/minio/minio/cmd.(*erasureServerPools).Walk in goroutine 575
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2210 +0x33c
```
2024-06-28 21:20:47 -04:00
|
|
|
// Already validated before arriving here
|
|
|
|
smallerThan, _ := humanize.ParseBytes(*r.Source.Snowball.SmallerThan)
|
2023-11-22 13:51:46 -05:00
|
|
|
|
fix panic in batch replicate (#20014)
Fixes:
```
panic: send on closed channel
panic: close of closed channel
goroutine 878 [running]:
github.com/minio/minio/internal/ioutil.SafeClose[...](...)
/Users/kp/code/src/github.com/minio/minio/internal/ioutil/ioutil.go:407
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2229 +0xc0
panic({0x108c25e60?, 0x1090b28d0?})
/usr/local/go/src/runtime/panic.go:770 +0x124
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.3({{0x1400e397316, 0x5}, {0x1400d88b8a8, 0x8}, {0x1f99d80, 0xede101c42, 0x0}, 0x3bc, 0x0, 0x0, ...})
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2235 +0xb4
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2277 +0xabc
created by github.com/minio/minio/cmd.(*erasureServerPools).Walk in goroutine 575
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2210 +0x33c
```
2024-06-28 21:20:47 -04:00
|
|
|
batch := make([]ObjectInfo, 0, *r.Source.Snowball.Batch)
|
|
|
|
writeFn := func(batch []ObjectInfo) {
|
|
|
|
if len(batch) > 0 {
|
2024-07-29 03:59:50 -04:00
|
|
|
if err := r.writeAsArchive(ctx, api, cl, batch, r.Target.Prefix); err != nil {
|
2024-07-26 08:55:50 -04:00
|
|
|
batchLogOnceIf(ctx, err, job.ID+"writeAsArchive")
|
fix panic in batch replicate (#20014)
Fixes:
```
panic: send on closed channel
panic: close of closed channel
goroutine 878 [running]:
github.com/minio/minio/internal/ioutil.SafeClose[...](...)
/Users/kp/code/src/github.com/minio/minio/internal/ioutil/ioutil.go:407
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2229 +0xc0
panic({0x108c25e60?, 0x1090b28d0?})
/usr/local/go/src/runtime/panic.go:770 +0x124
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.3({{0x1400e397316, 0x5}, {0x1400d88b8a8, 0x8}, {0x1f99d80, 0xede101c42, 0x0}, 0x3bc, 0x0, 0x0, ...})
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2235 +0xb4
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2277 +0xabc
created by github.com/minio/minio/cmd.(*erasureServerPools).Walk in goroutine 575
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2210 +0x33c
```
2024-06-28 21:20:47 -04:00
|
|
|
for _, b := range batch {
|
|
|
|
slowCh <- itemOrErr[ObjectInfo]{Item: b}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ri.trackCurrentBucketBatch(r.Source.Bucket, batch)
|
|
|
|
globalBatchJobsMetrics.save(job.ID, ri)
|
|
|
|
// persist in-memory state to disk after every 10secs.
|
2024-07-26 08:55:50 -04:00
|
|
|
batchLogOnceIf(ctx, ri.updateAfter(ctx, api, 10*time.Second, job), job.ID+"updateAfter")
|
2023-11-22 13:51:46 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
fix panic in batch replicate (#20014)
Fixes:
```
panic: send on closed channel
panic: close of closed channel
goroutine 878 [running]:
github.com/minio/minio/internal/ioutil.SafeClose[...](...)
/Users/kp/code/src/github.com/minio/minio/internal/ioutil/ioutil.go:407
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2229 +0xc0
panic({0x108c25e60?, 0x1090b28d0?})
/usr/local/go/src/runtime/panic.go:770 +0x124
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.3({{0x1400e397316, 0x5}, {0x1400d88b8a8, 0x8}, {0x1f99d80, 0xede101c42, 0x0}, 0x3bc, 0x0, 0x0, ...})
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2235 +0xb4
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2277 +0xabc
created by github.com/minio/minio/cmd.(*erasureServerPools).Walk in goroutine 575
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2210 +0x33c
```
2024-06-28 21:20:47 -04:00
|
|
|
for obj := range walkCh {
|
|
|
|
if obj.Item.DeleteMarker || !obj.Item.VersionPurgeStatus.Empty() || obj.Item.Size >= int64(smallerThan) {
|
|
|
|
slowCh <- obj
|
|
|
|
continue
|
|
|
|
}
|
2024-03-21 19:13:43 -04:00
|
|
|
|
fix panic in batch replicate (#20014)
Fixes:
```
panic: send on closed channel
panic: close of closed channel
goroutine 878 [running]:
github.com/minio/minio/internal/ioutil.SafeClose[...](...)
/Users/kp/code/src/github.com/minio/minio/internal/ioutil/ioutil.go:407
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2229 +0xc0
panic({0x108c25e60?, 0x1090b28d0?})
/usr/local/go/src/runtime/panic.go:770 +0x124
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.3({{0x1400e397316, 0x5}, {0x1400d88b8a8, 0x8}, {0x1f99d80, 0xede101c42, 0x0}, 0x3bc, 0x0, 0x0, ...})
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2235 +0xb4
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2277 +0xabc
created by github.com/minio/minio/cmd.(*erasureServerPools).Walk in goroutine 575
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2210 +0x33c
```
2024-06-28 21:20:47 -04:00
|
|
|
batch = append(batch, obj.Item)
|
2024-03-21 19:13:43 -04:00
|
|
|
|
fix panic in batch replicate (#20014)
Fixes:
```
panic: send on closed channel
panic: close of closed channel
goroutine 878 [running]:
github.com/minio/minio/internal/ioutil.SafeClose[...](...)
/Users/kp/code/src/github.com/minio/minio/internal/ioutil/ioutil.go:407
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2229 +0xc0
panic({0x108c25e60?, 0x1090b28d0?})
/usr/local/go/src/runtime/panic.go:770 +0x124
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.3({{0x1400e397316, 0x5}, {0x1400d88b8a8, 0x8}, {0x1f99d80, 0xede101c42, 0x0}, 0x3bc, 0x0, 0x0, ...})
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2235 +0xb4
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2277 +0xabc
created by github.com/minio/minio/cmd.(*erasureServerPools).Walk in goroutine 575
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2210 +0x33c
```
2024-06-28 21:20:47 -04:00
|
|
|
if len(batch) < *r.Source.Snowball.Batch {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
writeFn(batch)
|
|
|
|
batch = batch[:0]
|
2024-03-21 19:13:43 -04:00
|
|
|
}
|
|
|
|
writeFn(batch)
|
fix panic in batch replicate (#20014)
Fixes:
```
panic: send on closed channel
panic: close of closed channel
goroutine 878 [running]:
github.com/minio/minio/internal/ioutil.SafeClose[...](...)
/Users/kp/code/src/github.com/minio/minio/internal/ioutil/ioutil.go:407
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2229 +0xc0
panic({0x108c25e60?, 0x1090b28d0?})
/usr/local/go/src/runtime/panic.go:770 +0x124
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.3({{0x1400e397316, 0x5}, {0x1400d88b8a8, 0x8}, {0x1f99d80, 0xede101c42, 0x0}, 0x3bc, 0x0, 0x0, ...})
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2235 +0xb4
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2277 +0xabc
created by github.com/minio/minio/cmd.(*erasureServerPools).Walk in goroutine 575
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2210 +0x33c
```
2024-06-28 21:20:47 -04:00
|
|
|
xioutil.SafeClose(slowCh)
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
slowCh = walkCh
|
|
|
|
}
|
2023-02-14 16:19:30 -05:00
|
|
|
|
fix panic in batch replicate (#20014)
Fixes:
```
panic: send on closed channel
panic: close of closed channel
goroutine 878 [running]:
github.com/minio/minio/internal/ioutil.SafeClose[...](...)
/Users/kp/code/src/github.com/minio/minio/internal/ioutil/ioutil.go:407
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2229 +0xc0
panic({0x108c25e60?, 0x1090b28d0?})
/usr/local/go/src/runtime/panic.go:770 +0x124
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.3({{0x1400e397316, 0x5}, {0x1400d88b8a8, 0x8}, {0x1f99d80, 0xede101c42, 0x0}, 0x3bc, 0x0, 0x0, ...})
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2235 +0xb4
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2277 +0xabc
created by github.com/minio/minio/cmd.(*erasureServerPools).Walk in goroutine 575
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2210 +0x33c
```
2024-06-28 21:20:47 -04:00
|
|
|
workerSize, err := strconv.Atoi(env.Get("_MINIO_BATCH_REPLICATION_WORKERS", strconv.Itoa(runtime.GOMAXPROCS(0)/2)))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-27 20:20:04 -05:00
|
|
|
|
fix panic in batch replicate (#20014)
Fixes:
```
panic: send on closed channel
panic: close of closed channel
goroutine 878 [running]:
github.com/minio/minio/internal/ioutil.SafeClose[...](...)
/Users/kp/code/src/github.com/minio/minio/internal/ioutil/ioutil.go:407
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2229 +0xc0
panic({0x108c25e60?, 0x1090b28d0?})
/usr/local/go/src/runtime/panic.go:770 +0x124
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.3({{0x1400e397316, 0x5}, {0x1400d88b8a8, 0x8}, {0x1f99d80, 0xede101c42, 0x0}, 0x3bc, 0x0, 0x0, ...})
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2235 +0xb4
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2277 +0xabc
created by github.com/minio/minio/cmd.(*erasureServerPools).Walk in goroutine 575
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2210 +0x33c
```
2024-06-28 21:20:47 -04:00
|
|
|
wk, err := workers.New(workerSize)
|
|
|
|
if err != nil {
|
|
|
|
// invalid worker size.
|
|
|
|
return err
|
|
|
|
}
|
2023-02-13 15:07:58 -05:00
|
|
|
|
fix panic in batch replicate (#20014)
Fixes:
```
panic: send on closed channel
panic: close of closed channel
goroutine 878 [running]:
github.com/minio/minio/internal/ioutil.SafeClose[...](...)
/Users/kp/code/src/github.com/minio/minio/internal/ioutil/ioutil.go:407
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2229 +0xc0
panic({0x108c25e60?, 0x1090b28d0?})
/usr/local/go/src/runtime/panic.go:770 +0x124
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2.3({{0x1400e397316, 0x5}, {0x1400d88b8a8, 0x8}, {0x1f99d80, 0xede101c42, 0x0}, 0x3bc, 0x0, 0x0, ...})
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2235 +0xb4
github.com/minio/minio/cmd.(*erasureServerPools).Walk.func2()
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2277 +0xabc
created by github.com/minio/minio/cmd.(*erasureServerPools).Walk in goroutine 575
/Users/kp/code/src/github.com/minio/minio/cmd/erasure-server-pool.go:2210 +0x33c
```
2024-06-28 21:20:47 -04:00
|
|
|
walkQuorum := env.Get("_MINIO_BATCH_REPLICATION_WALK_QUORUM", "strict")
|
|
|
|
if walkQuorum == "" {
|
|
|
|
walkQuorum = "strict"
|
|
|
|
}
|
2024-08-01 08:53:30 -04:00
|
|
|
ctx, cancelCause := context.WithCancelCause(ctx)
|
2023-05-03 01:52:35 -04:00
|
|
|
// one of source/target is s3, skip delete marker and all versions under the same object name.
|
|
|
|
s3Type := r.Target.Type == BatchJobReplicateResourceS3 || r.Source.Type == BatchJobReplicateResourceS3
|
2022-10-03 05:10:15 -04:00
|
|
|
|
2024-08-01 08:53:30 -04:00
|
|
|
go func() {
|
2024-09-05 07:57:23 -04:00
|
|
|
prefixes := r.Source.Prefix.F()
|
|
|
|
if len(prefixes) == 0 {
|
|
|
|
prefixes = []string{""}
|
|
|
|
}
|
|
|
|
for _, prefix := range prefixes {
|
2024-08-01 08:53:30 -04:00
|
|
|
prefixWalkCh := make(chan itemOrErr[ObjectInfo], 100)
|
|
|
|
if err := api.Walk(ctx, r.Source.Bucket, strings.TrimSpace(prefix), prefixWalkCh, WalkOptions{
|
|
|
|
Marker: lastObject,
|
|
|
|
Filter: selectObj,
|
|
|
|
AskDisks: walkQuorum,
|
|
|
|
}); err != nil {
|
|
|
|
cancelCause(err)
|
|
|
|
xioutil.SafeClose(walkCh)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for obj := range prefixWalkCh {
|
|
|
|
walkCh <- obj
|
|
|
|
}
|
|
|
|
}
|
|
|
|
xioutil.SafeClose(walkCh)
|
|
|
|
}()
|
2022-10-03 05:10:15 -04:00
|
|
|
|
2023-05-03 01:52:35 -04:00
|
|
|
prevObj := ""
|
|
|
|
|
|
|
|
skipReplicate := false
|
2024-05-06 16:27:52 -04:00
|
|
|
for res := range slowCh {
|
|
|
|
if res.Err != nil {
|
|
|
|
ri.Failed = true
|
2024-07-26 08:55:50 -04:00
|
|
|
batchLogOnceIf(ctx, res.Err, job.ID+"res.Err")
|
2024-05-06 16:27:52 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
result := res.Item
|
2023-05-03 01:52:35 -04:00
|
|
|
if result.Name != prevObj {
|
|
|
|
prevObj = result.Name
|
|
|
|
skipReplicate = result.DeleteMarker && s3Type
|
|
|
|
}
|
|
|
|
if skipReplicate {
|
|
|
|
continue
|
|
|
|
}
|
2023-02-13 15:07:58 -05:00
|
|
|
wk.Take()
|
|
|
|
go func() {
|
|
|
|
defer wk.Give()
|
|
|
|
|
2023-12-02 05:51:33 -05:00
|
|
|
stopFn := globalBatchJobsMetrics.trace(batchJobMetricReplication, job.ID, attempts)
|
2023-02-13 15:07:58 -05:00
|
|
|
success := true
|
|
|
|
if err := r.ReplicateToTarget(ctx, api, c, result, retry); err != nil {
|
2023-02-14 10:22:08 -05:00
|
|
|
if miniogo.ToErrorResponse(err).Code == "PreconditionFailed" {
|
|
|
|
// pre-condition failed means we already have the object copied over.
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// object must be deleted concurrently, allow these failures but do not count them
|
2023-02-13 15:07:58 -05:00
|
|
|
if isErrVersionNotFound(err) || isErrObjectNotFound(err) {
|
|
|
|
return
|
|
|
|
}
|
2023-12-02 05:51:33 -05:00
|
|
|
stopFn(result, err)
|
2024-07-26 08:55:50 -04:00
|
|
|
batchLogOnceIf(ctx, err, job.ID+"ReplicateToTarget")
|
2023-02-13 15:07:58 -05:00
|
|
|
success = false
|
|
|
|
} else {
|
2023-12-02 05:51:33 -05:00
|
|
|
stopFn(result, nil)
|
2022-10-13 17:42:10 -04:00
|
|
|
}
|
2024-05-06 16:27:52 -04:00
|
|
|
ri.trackCurrentBucketObject(r.Source.Bucket, result, success, attempts)
|
2023-02-13 15:07:58 -05:00
|
|
|
globalBatchJobsMetrics.save(job.ID, ri)
|
|
|
|
// persist in-memory state to disk after every 10secs.
|
2024-07-26 08:55:50 -04:00
|
|
|
batchLogOnceIf(ctx, ri.updateAfter(ctx, api, 10*time.Second, job), job.ID+"updateAfter2")
|
2023-12-02 05:51:33 -05:00
|
|
|
|
|
|
|
if wait := globalBatchConfig.ReplicationWait(); wait > 0 {
|
|
|
|
time.Sleep(wait)
|
|
|
|
}
|
2023-02-13 15:07:58 -05:00
|
|
|
}()
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
2023-02-13 15:07:58 -05:00
|
|
|
wk.Wait()
|
2024-08-01 08:53:30 -04:00
|
|
|
// Do not need to retry if we can't list objects on source.
|
|
|
|
if context.Cause(ctx) != nil {
|
|
|
|
return context.Cause(ctx)
|
|
|
|
}
|
2022-10-03 05:10:15 -04:00
|
|
|
ri.RetryAttempts = attempts
|
|
|
|
ri.Complete = ri.ObjectsFailed == 0
|
|
|
|
ri.Failed = ri.ObjectsFailed > 0
|
|
|
|
|
2023-02-13 15:07:58 -05:00
|
|
|
globalBatchJobsMetrics.save(job.ID, ri)
|
|
|
|
// persist in-memory state to disk.
|
2024-07-26 08:55:50 -04:00
|
|
|
batchLogOnceIf(ctx, ri.updateAfter(ctx, api, 0, job), job.ID+"updateAfter3")
|
2022-10-03 05:10:15 -04:00
|
|
|
|
2023-08-11 14:34:43 -04:00
|
|
|
if err := r.Notify(ctx, ri); err != nil {
|
2024-07-26 08:55:50 -04:00
|
|
|
batchLogOnceIf(ctx, fmt.Errorf("unable to notify %v", err), job.ID+"notify")
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
|
2024-08-01 08:53:30 -04:00
|
|
|
cancelCause(nil)
|
2022-10-03 05:10:15 -04:00
|
|
|
if ri.Failed {
|
|
|
|
ri.ObjectsFailed = 0
|
|
|
|
ri.Bucket = ""
|
|
|
|
ri.Object = ""
|
|
|
|
ri.Objects = 0
|
|
|
|
ri.BytesFailed = 0
|
|
|
|
ri.BytesTransferred = 0
|
|
|
|
retry = true // indicate we are retrying..
|
|
|
|
time.Sleep(delay + time.Duration(rnd.Float64()*float64(delay)))
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-15 14:58:31 -04:00
|
|
|
//msgp:ignore batchReplicationJobError
|
|
|
|
type batchReplicationJobError struct {
|
|
|
|
Code string
|
|
|
|
Description string
|
|
|
|
HTTPStatusCode int
|
2024-05-05 12:56:21 -04:00
|
|
|
ObjectSize int64
|
2022-10-15 14:58:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (e batchReplicationJobError) Error() string {
|
|
|
|
return e.Description
|
|
|
|
}
|
|
|
|
|
2022-10-03 05:10:15 -04:00
|
|
|
// Validate validates the job definition input
|
2023-02-14 16:19:30 -05:00
|
|
|
func (r *BatchJobReplicateV1) Validate(ctx context.Context, job BatchJobRequest, o ObjectLayer) error {
|
2022-10-03 05:10:15 -04:00
|
|
|
if r == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.APIVersion != batchReplJobAPIVersion {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
2024-04-08 10:11:38 -04:00
|
|
|
if r.Source.Endpoint != "" && r.Target.Endpoint != "" {
|
2022-10-03 05:10:15 -04:00
|
|
|
return errInvalidArgument
|
|
|
|
}
|
2024-04-08 10:11:38 -04:00
|
|
|
|
|
|
|
if r.Source.Creds.Empty() && r.Target.Creds.Empty() {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Source.Bucket == "" || r.Target.Bucket == "" {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
2023-04-04 13:50:11 -04:00
|
|
|
var isRemoteToLocal bool
|
2023-03-31 13:48:36 -04:00
|
|
|
localBkt := r.Source.Bucket
|
|
|
|
if r.Source.Endpoint != "" {
|
|
|
|
localBkt = r.Target.Bucket
|
2023-04-04 13:50:11 -04:00
|
|
|
isRemoteToLocal = true
|
2023-03-31 13:48:36 -04:00
|
|
|
}
|
|
|
|
info, err := o.GetBucketInfo(ctx, localBkt, BucketOptions{})
|
2022-10-03 05:10:15 -04:00
|
|
|
if err != nil {
|
2022-10-15 14:58:31 -04:00
|
|
|
if isErrBucketNotFound(err) {
|
|
|
|
return batchReplicationJobError{
|
|
|
|
Code: "NoSuchSourceBucket",
|
2023-04-04 13:50:11 -04:00
|
|
|
Description: fmt.Sprintf("The specified bucket %s does not exist", localBkt),
|
2022-10-15 14:58:31 -04:00
|
|
|
HTTPStatusCode: http.StatusNotFound,
|
|
|
|
}
|
|
|
|
}
|
2022-10-03 05:10:15 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.Source.Type.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-11-22 13:51:46 -05:00
|
|
|
if err := r.Source.Snowball.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-03-31 13:48:36 -04:00
|
|
|
|
|
|
|
if !r.Source.Creds.Empty() {
|
|
|
|
if err := r.Source.Creds.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if r.Target.Endpoint == "" && !r.Target.Creds.Empty() {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
2022-10-03 05:10:15 -04:00
|
|
|
|
2023-03-31 13:48:36 -04:00
|
|
|
if r.Source.Endpoint == "" && !r.Source.Creds.Empty() {
|
2022-10-03 05:10:15 -04:00
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
2023-05-21 18:16:31 -04:00
|
|
|
if r.Source.Endpoint != "" && !r.Source.Type.isMinio() && !r.Source.ValidPath() {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Target.Endpoint != "" && !r.Target.Type.isMinio() && !r.Target.ValidPath() {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
2022-10-03 05:10:15 -04:00
|
|
|
|
2023-03-31 13:48:36 -04:00
|
|
|
if !r.Target.Creds.Empty() {
|
|
|
|
if err := r.Target.Creds.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 05:10:15 -04:00
|
|
|
if err := r.Target.Type.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tag := range r.Flags.Filter.Tags {
|
|
|
|
if err := tag.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, meta := range r.Flags.Filter.Metadata {
|
|
|
|
if err := meta.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := r.Flags.Retry.Validate(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-31 13:48:36 -04:00
|
|
|
remoteEp := r.Target.Endpoint
|
|
|
|
remoteBkt := r.Target.Bucket
|
|
|
|
cred := r.Target.Creds
|
2023-05-21 18:16:31 -04:00
|
|
|
pathStyle := r.Target.Path
|
2023-03-31 13:48:36 -04:00
|
|
|
|
|
|
|
if r.Source.Endpoint != "" {
|
|
|
|
remoteEp = r.Source.Endpoint
|
|
|
|
cred = r.Source.Creds
|
|
|
|
remoteBkt = r.Source.Bucket
|
2023-05-21 18:16:31 -04:00
|
|
|
pathStyle = r.Source.Path
|
|
|
|
|
2023-03-31 13:48:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
u, err := url.Parse(remoteEp)
|
2022-10-15 14:58:31 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := miniogo.NewCore(u.Host, &miniogo.Options{
|
2023-05-21 18:16:31 -04:00
|
|
|
Creds: credentials.NewStaticV4(cred.AccessKey, cred.SecretKey, cred.SessionToken),
|
|
|
|
Secure: u.Scheme == "https",
|
2024-04-03 14:27:05 -04:00
|
|
|
Transport: getRemoteInstanceTransport(),
|
2023-05-21 18:16:31 -04:00
|
|
|
BucketLookup: lookupStyle(pathStyle),
|
2022-10-15 14:58:31 -04:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-14 16:19:30 -05:00
|
|
|
c.SetAppInfo("minio-"+batchJobPrefix, r.APIVersion+" "+job.ID)
|
2022-10-15 14:58:31 -04:00
|
|
|
|
2023-03-31 13:48:36 -04:00
|
|
|
vcfg, err := c.GetBucketVersioning(ctx, remoteBkt)
|
2022-10-15 14:58:31 -04:00
|
|
|
if err != nil {
|
|
|
|
if miniogo.ToErrorResponse(err).Code == "NoSuchBucket" {
|
|
|
|
return batchReplicationJobError{
|
|
|
|
Code: "NoSuchTargetBucket",
|
|
|
|
Description: "The specified target bucket does not exist",
|
|
|
|
HTTPStatusCode: http.StatusNotFound,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2023-05-03 01:52:35 -04:00
|
|
|
// if both source and target are minio instances
|
|
|
|
minioType := r.Target.Type == BatchJobReplicateResourceMinIO && r.Source.Type == BatchJobReplicateResourceMinIO
|
2023-04-04 13:50:11 -04:00
|
|
|
// If source has versioning enabled, target must have versioning enabled
|
2023-05-03 01:52:35 -04:00
|
|
|
if minioType && ((info.Versioning && !vcfg.Enabled() && !isRemoteToLocal) || (!info.Versioning && vcfg.Enabled() && isRemoteToLocal)) {
|
2022-10-15 14:58:31 -04:00
|
|
|
return batchReplicationJobError{
|
|
|
|
Code: "InvalidBucketState",
|
|
|
|
Description: fmt.Sprintf("The source '%s' has versioning enabled, target '%s' must have versioning enabled",
|
|
|
|
r.Source.Bucket, r.Target.Bucket),
|
|
|
|
HTTPStatusCode: http.StatusBadRequest,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r.clnt = c
|
2022-10-03 05:10:15 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type returns type of batch job, currently only supports 'replicate'
|
|
|
|
func (j BatchJobRequest) Type() madmin.BatchJobType {
|
2023-04-04 13:56:54 -04:00
|
|
|
switch {
|
|
|
|
case j.Replicate != nil:
|
2022-10-03 05:10:15 -04:00
|
|
|
return madmin.BatchJobReplicate
|
2023-04-04 13:56:54 -04:00
|
|
|
case j.KeyRotate != nil:
|
|
|
|
return madmin.BatchJobKeyRotate
|
2023-12-02 05:51:33 -05:00
|
|
|
case j.Expire != nil:
|
|
|
|
return madmin.BatchJobExpire
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
return madmin.BatchJobType("unknown")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate validates the current job, used by 'save()' before
|
|
|
|
// persisting the job request
|
|
|
|
func (j BatchJobRequest) Validate(ctx context.Context, o ObjectLayer) error {
|
2023-04-04 13:56:54 -04:00
|
|
|
switch {
|
|
|
|
case j.Replicate != nil:
|
2023-02-14 16:19:30 -05:00
|
|
|
return j.Replicate.Validate(ctx, j, o)
|
2023-04-04 13:56:54 -04:00
|
|
|
case j.KeyRotate != nil:
|
|
|
|
return j.KeyRotate.Validate(ctx, j, o)
|
2023-12-02 05:51:33 -05:00
|
|
|
case j.Expire != nil:
|
|
|
|
return j.Expire.Validate(ctx, j, o)
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j BatchJobRequest) delete(ctx context.Context, api ObjectLayer) {
|
2024-03-07 13:58:22 -05:00
|
|
|
deleteConfig(ctx, api, getJobPath(j))
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
|
2024-07-02 04:17:52 -04:00
|
|
|
func (j BatchJobRequest) getJobReportPath() (string, error) {
|
|
|
|
var fileName string
|
|
|
|
switch {
|
|
|
|
case j.Replicate != nil:
|
|
|
|
fileName = batchReplName
|
|
|
|
case j.KeyRotate != nil:
|
|
|
|
fileName = batchKeyRotationName
|
|
|
|
case j.Expire != nil:
|
|
|
|
fileName = batchExpireName
|
|
|
|
default:
|
|
|
|
return "", errors.New("unknown job type")
|
|
|
|
}
|
|
|
|
return pathJoin(batchJobReportsPrefix, j.ID, fileName), nil
|
|
|
|
}
|
|
|
|
|
2022-10-03 05:10:15 -04:00
|
|
|
func (j *BatchJobRequest) save(ctx context.Context, api ObjectLayer) error {
|
2023-12-02 05:51:33 -05:00
|
|
|
if j.Replicate == nil && j.KeyRotate == nil && j.Expire == nil {
|
2022-10-03 05:10:15 -04:00
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := j.Validate(ctx, api); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
job, err := j.MarshalMsg(nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-07 13:58:22 -05:00
|
|
|
return saveConfig(ctx, api, getJobPath(*j), job)
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func (j *BatchJobRequest) load(ctx context.Context, api ObjectLayer, name string) error {
|
|
|
|
if j == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
job, err := readConfig(ctx, api, name)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errConfigNotFound) || isErrObjectNotFound(err) {
|
|
|
|
err = errNoSuchJob
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = j.UnmarshalMsg(job)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func batchReplicationOpts(ctx context.Context, sc string, objInfo ObjectInfo) (putOpts miniogo.PutObjectOptions, err error) {
|
|
|
|
// TODO: support custom storage class for remote replication
|
2024-06-06 05:36:42 -04:00
|
|
|
putOpts, err = putReplicationOpts(ctx, "", objInfo, 0)
|
2022-10-03 05:10:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return putOpts, err
|
|
|
|
}
|
|
|
|
putOpts.Internal = miniogo.AdvancedPutOptions{
|
2023-04-27 16:43:18 -04:00
|
|
|
SourceVersionID: objInfo.VersionID,
|
|
|
|
SourceMTime: objInfo.ModTime,
|
|
|
|
SourceETag: objInfo.ETag,
|
|
|
|
ReplicationRequest: true,
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
return putOpts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListBatchJobs - lists all currently active batch jobs, optionally takes {jobType}
|
|
|
|
// input to list only active batch jobs of 'jobType'
|
|
|
|
func (a adminAPIHandlers) ListBatchJobs(w http.ResponseWriter, r *http.Request) {
|
2023-07-13 17:52:21 -04:00
|
|
|
ctx := r.Context()
|
2022-10-03 05:10:15 -04:00
|
|
|
|
2023-09-14 17:50:16 -04:00
|
|
|
objectAPI, _ := validateAdminReq(ctx, w, r, policy.ListBatchJobsAction)
|
2022-10-03 05:10:15 -04:00
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jobType := r.Form.Get("jobType")
|
|
|
|
if jobType == "" {
|
|
|
|
jobType = string(madmin.BatchJobReplicate)
|
|
|
|
}
|
|
|
|
|
2024-05-06 16:27:52 -04:00
|
|
|
resultCh := make(chan itemOrErr[ObjectInfo])
|
2022-10-03 05:10:15 -04:00
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
defer cancel()
|
|
|
|
|
2023-11-27 20:20:04 -05:00
|
|
|
if err := objectAPI.Walk(ctx, minioMetaBucket, batchJobPrefix, resultCh, WalkOptions{}); err != nil {
|
2022-10-03 05:10:15 -04:00
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
listResult := madmin.ListBatchJobsResult{}
|
|
|
|
for result := range resultCh {
|
2024-05-06 16:27:52 -04:00
|
|
|
if result.Err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, result.Err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2024-07-17 12:42:32 -04:00
|
|
|
if strings.HasPrefix(result.Item.Name, batchJobReportsPrefix+slashSeparator) {
|
|
|
|
continue
|
|
|
|
}
|
2022-10-03 05:10:15 -04:00
|
|
|
req := &BatchJobRequest{}
|
2024-05-06 16:27:52 -04:00
|
|
|
if err := req.load(ctx, objectAPI, result.Item.Name); err != nil {
|
2022-10-03 05:10:15 -04:00
|
|
|
if !errors.Is(err, errNoSuchJob) {
|
2024-04-04 08:04:40 -04:00
|
|
|
batchLogIf(ctx, err)
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if jobType == string(req.Type()) {
|
|
|
|
listResult.Jobs = append(listResult.Jobs, madmin.BatchJobResult{
|
|
|
|
ID: req.ID,
|
|
|
|
Type: req.Type(),
|
|
|
|
Started: req.Started,
|
|
|
|
User: req.User,
|
|
|
|
Elapsed: time.Since(req.Started),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-04-04 08:04:40 -04:00
|
|
|
batchLogIf(ctx, json.NewEncoder(w).Encode(&listResult))
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
|
2024-07-02 04:17:52 -04:00
|
|
|
// BatchJobStatus - returns the status of a batch job saved in the disk
|
|
|
|
func (a adminAPIHandlers) BatchJobStatus(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
|
|
|
|
objectAPI, _ := validateAdminReq(ctx, w, r, policy.ListBatchJobsAction)
|
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
jobID := r.Form.Get("jobId")
|
|
|
|
if jobID == "" {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, errInvalidArgument), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req := BatchJobRequest{ID: jobID}
|
|
|
|
if i := strings.Index(jobID, "-"); i > 0 {
|
|
|
|
switch madmin.BatchJobType(jobID[:i]) {
|
|
|
|
case madmin.BatchJobReplicate:
|
|
|
|
req.Replicate = &BatchJobReplicateV1{}
|
|
|
|
case madmin.BatchJobKeyRotate:
|
|
|
|
req.KeyRotate = &BatchJobKeyRotateV1{}
|
|
|
|
case madmin.BatchJobExpire:
|
|
|
|
req.Expire = &BatchJobExpire{}
|
|
|
|
default:
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, errors.New("job ID format unrecognized")), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ri := &batchJobInfo{}
|
|
|
|
if err := ri.load(ctx, objectAPI, req); err != nil {
|
|
|
|
if !errors.Is(err, errNoSuchJob) {
|
|
|
|
batchLogIf(ctx, err)
|
|
|
|
}
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := json.Marshal(madmin.BatchJobStatus{LastMetric: ri.metric()})
|
|
|
|
if err != nil {
|
|
|
|
batchLogIf(ctx, err)
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(buf)
|
|
|
|
}
|
|
|
|
|
2022-10-03 05:10:15 -04:00
|
|
|
var errNoSuchJob = errors.New("no such job")
|
|
|
|
|
|
|
|
// DescribeBatchJob returns the currently active batch job definition
|
|
|
|
func (a adminAPIHandlers) DescribeBatchJob(w http.ResponseWriter, r *http.Request) {
|
2023-07-13 17:52:21 -04:00
|
|
|
ctx := r.Context()
|
2022-10-03 05:10:15 -04:00
|
|
|
|
2023-09-14 17:50:16 -04:00
|
|
|
objectAPI, _ := validateAdminReq(ctx, w, r, policy.DescribeBatchJobAction)
|
2022-10-03 05:10:15 -04:00
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-11 16:12:35 -04:00
|
|
|
jobID := r.Form.Get("jobId")
|
|
|
|
if jobID == "" {
|
2022-10-03 05:10:15 -04:00
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, errInvalidArgument), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &BatchJobRequest{}
|
2023-08-11 16:12:35 -04:00
|
|
|
if err := req.load(ctx, objectAPI, pathJoin(batchJobPrefix, jobID)); err != nil {
|
2022-10-03 05:10:15 -04:00
|
|
|
if !errors.Is(err, errNoSuchJob) {
|
2024-04-04 08:04:40 -04:00
|
|
|
batchLogIf(ctx, err)
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err := yaml.Marshal(req)
|
|
|
|
if err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
batchLogIf(ctx, err)
|
2022-10-03 05:10:15 -04:00
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Write(buf)
|
|
|
|
}
|
|
|
|
|
2024-08-13 14:23:33 -04:00
|
|
|
// StartBatchJob queue a new job for execution
|
2022-10-03 05:10:15 -04:00
|
|
|
func (a adminAPIHandlers) StartBatchJob(w http.ResponseWriter, r *http.Request) {
|
2023-07-13 17:52:21 -04:00
|
|
|
ctx := r.Context()
|
2022-10-03 05:10:15 -04:00
|
|
|
|
2023-09-14 17:50:16 -04:00
|
|
|
objectAPI, creds := validateAdminReq(ctx, w, r, policy.StartBatchJobAction)
|
2022-10-03 05:10:15 -04:00
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-08-11 16:12:35 -04:00
|
|
|
buf, err := io.ReadAll(ioutil.HardLimitReader(r.Body, humanize.MiByte*4))
|
2022-10-03 05:10:15 -04:00
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user := creds.AccessKey
|
|
|
|
if creds.ParentUser != "" {
|
|
|
|
user = creds.ParentUser
|
|
|
|
}
|
|
|
|
|
|
|
|
job := &BatchJobRequest{}
|
2024-01-08 18:22:28 -05:00
|
|
|
if err = yaml.Unmarshal(buf, job); err != nil {
|
2022-10-03 05:10:15 -04:00
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-11-22 13:51:46 -05:00
|
|
|
// Fill with default values
|
|
|
|
if job.Replicate != nil {
|
|
|
|
if job.Replicate.Source.Snowball.Disable == nil {
|
|
|
|
job.Replicate.Source.Snowball.Disable = ptr(false)
|
|
|
|
}
|
|
|
|
if job.Replicate.Source.Snowball.Batch == nil {
|
|
|
|
job.Replicate.Source.Snowball.Batch = ptr(100)
|
|
|
|
}
|
|
|
|
if job.Replicate.Source.Snowball.InMemory == nil {
|
|
|
|
job.Replicate.Source.Snowball.InMemory = ptr(true)
|
|
|
|
}
|
|
|
|
if job.Replicate.Source.Snowball.Compress == nil {
|
|
|
|
job.Replicate.Source.Snowball.Compress = ptr(false)
|
|
|
|
}
|
|
|
|
if job.Replicate.Source.Snowball.SmallerThan == nil {
|
|
|
|
job.Replicate.Source.Snowball.SmallerThan = ptr("5MiB")
|
|
|
|
}
|
|
|
|
if job.Replicate.Source.Snowball.SkipErrs == nil {
|
|
|
|
job.Replicate.Source.Snowball.SkipErrs = ptr(true)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-22 19:33:11 -05:00
|
|
|
// Validate the incoming job request
|
|
|
|
if err := job.Validate(ctx, objectAPI); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-07-02 04:17:52 -04:00
|
|
|
job.ID = fmt.Sprintf("%s-%s%s%d", job.Type(), shortuuid.New(), getKeySeparator(), GetProxyEndpointLocalIndex(globalProxyEndpoints))
|
2023-11-22 19:33:11 -05:00
|
|
|
job.User = user
|
|
|
|
job.Started = time.Now()
|
|
|
|
|
2022-10-03 05:10:15 -04:00
|
|
|
if err := job.save(ctx, objectAPI); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = globalBatchJobPool.queueJob(job); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
buf, err = json.Marshal(&madmin.BatchJobResult{
|
|
|
|
ID: job.ID,
|
|
|
|
Type: job.Type(),
|
|
|
|
Started: job.Started,
|
|
|
|
User: job.User,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
writeSuccessResponseJSON(w, buf)
|
|
|
|
}
|
|
|
|
|
2023-03-18 02:42:43 -04:00
|
|
|
// CancelBatchJob cancels a job in progress
|
|
|
|
func (a adminAPIHandlers) CancelBatchJob(w http.ResponseWriter, r *http.Request) {
|
2023-07-13 17:52:21 -04:00
|
|
|
ctx := r.Context()
|
2023-03-18 02:42:43 -04:00
|
|
|
|
2023-09-14 17:50:16 -04:00
|
|
|
objectAPI, _ := validateAdminReq(ctx, w, r, policy.CancelBatchJobAction)
|
2023-03-18 02:42:43 -04:00
|
|
|
if objectAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
2023-08-11 16:12:35 -04:00
|
|
|
|
2023-03-18 02:42:43 -04:00
|
|
|
jobID := r.Form.Get("id")
|
|
|
|
if jobID == "" {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, errInvalidArgument), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2023-08-11 16:12:35 -04:00
|
|
|
|
|
|
|
if _, success := proxyRequestByToken(ctx, w, r, jobID); success {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-03-18 02:42:43 -04:00
|
|
|
if err := globalBatchJobPool.canceler(jobID, true); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErrWithErr(ErrInvalidRequest, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2023-08-11 16:12:35 -04:00
|
|
|
|
2023-03-18 02:42:43 -04:00
|
|
|
j := BatchJobRequest{
|
2024-03-07 13:58:22 -05:00
|
|
|
ID: jobID,
|
2023-03-18 02:42:43 -04:00
|
|
|
}
|
2023-08-11 16:12:35 -04:00
|
|
|
|
2023-03-18 02:42:43 -04:00
|
|
|
j.delete(ctx, objectAPI)
|
|
|
|
|
|
|
|
writeSuccessNoContent(w)
|
|
|
|
}
|
|
|
|
|
2022-10-03 05:10:15 -04:00
|
|
|
//msgp:ignore BatchJobPool
|
|
|
|
|
|
|
|
// BatchJobPool batch job pool
|
|
|
|
type BatchJobPool struct {
|
|
|
|
ctx context.Context
|
|
|
|
objLayer ObjectLayer
|
|
|
|
once sync.Once
|
|
|
|
mu sync.Mutex
|
|
|
|
jobCh chan *BatchJobRequest
|
2023-03-18 02:42:43 -04:00
|
|
|
jmu sync.Mutex // protects jobCancelers
|
|
|
|
jobCancelers map[string]context.CancelFunc
|
2022-10-03 05:10:15 -04:00
|
|
|
workerKillCh chan struct{}
|
|
|
|
workerSize int
|
|
|
|
}
|
|
|
|
|
|
|
|
var globalBatchJobPool *BatchJobPool
|
|
|
|
|
|
|
|
// newBatchJobPool creates a pool of job manifest workers of specified size
|
|
|
|
func newBatchJobPool(ctx context.Context, o ObjectLayer, workers int) *BatchJobPool {
|
|
|
|
jpool := &BatchJobPool{
|
|
|
|
ctx: ctx,
|
|
|
|
objLayer: o,
|
|
|
|
jobCh: make(chan *BatchJobRequest, 10000),
|
|
|
|
workerKillCh: make(chan struct{}, workers),
|
2023-03-18 02:42:43 -04:00
|
|
|
jobCancelers: make(map[string]context.CancelFunc),
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
jpool.ResizeWorkers(workers)
|
2024-07-02 04:17:52 -04:00
|
|
|
|
|
|
|
randomWait := func() time.Duration {
|
2024-07-03 03:16:05 -04:00
|
|
|
// randomWait depends on the number of nodes to avoid triggering resume and cleanups at the same time.
|
2024-07-02 04:17:52 -04:00
|
|
|
return time.Duration(rand.Float64() * float64(time.Duration(globalEndpoints.NEndpoints())*time.Hour))
|
|
|
|
}
|
|
|
|
|
2024-07-03 03:16:05 -04:00
|
|
|
go func() {
|
|
|
|
jpool.resume(randomWait)
|
|
|
|
jpool.cleanupReports(randomWait)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return jpool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *BatchJobPool) cleanupReports(randomWait func() time.Duration) {
|
2024-07-02 04:17:52 -04:00
|
|
|
t := time.NewTimer(randomWait())
|
|
|
|
defer t.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-GlobalContext.Done():
|
|
|
|
return
|
|
|
|
case <-t.C:
|
|
|
|
results := make(chan itemOrErr[ObjectInfo], 100)
|
|
|
|
ctx, cancel := context.WithCancel(j.ctx)
|
|
|
|
defer cancel()
|
|
|
|
if err := j.objLayer.Walk(ctx, minioMetaBucket, batchJobReportsPrefix, results, WalkOptions{}); err != nil {
|
|
|
|
batchLogIf(j.ctx, err)
|
|
|
|
t.Reset(randomWait())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for result := range results {
|
|
|
|
if result.Err != nil {
|
|
|
|
batchLogIf(j.ctx, result.Err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ri := &batchJobInfo{}
|
|
|
|
if err := ri.loadByPath(ctx, j.objLayer, result.Item.Name); err != nil {
|
|
|
|
batchLogIf(ctx, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if (ri.Complete || ri.Failed) && time.Since(ri.LastUpdate) > oldJobsExpiration {
|
|
|
|
deleteConfig(ctx, j.objLayer, result.Item.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Reset(randomWait())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-03 03:16:05 -04:00
|
|
|
func (j *BatchJobPool) resume(randomWait func() time.Duration) {
|
|
|
|
time.Sleep(randomWait())
|
|
|
|
|
2024-05-06 16:27:52 -04:00
|
|
|
results := make(chan itemOrErr[ObjectInfo], 100)
|
2022-10-03 05:10:15 -04:00
|
|
|
ctx, cancel := context.WithCancel(j.ctx)
|
|
|
|
defer cancel()
|
2023-11-27 20:20:04 -05:00
|
|
|
if err := j.objLayer.Walk(ctx, minioMetaBucket, batchJobPrefix, results, WalkOptions{}); err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
batchLogIf(j.ctx, err)
|
2022-10-03 05:10:15 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
for result := range results {
|
2024-05-06 16:27:52 -04:00
|
|
|
if result.Err != nil {
|
|
|
|
batchLogIf(j.ctx, result.Err)
|
|
|
|
continue
|
|
|
|
}
|
2024-07-17 12:42:32 -04:00
|
|
|
if strings.HasPrefix(result.Item.Name, batchJobReportsPrefix+slashSeparator) {
|
|
|
|
continue
|
|
|
|
}
|
2023-04-04 13:56:54 -04:00
|
|
|
// ignore batch-replicate.bin and batch-rotate.bin entries
|
2024-05-06 16:27:52 -04:00
|
|
|
if strings.HasSuffix(result.Item.Name, slashSeparator) {
|
2023-04-04 13:56:54 -04:00
|
|
|
continue
|
|
|
|
}
|
2022-10-03 05:10:15 -04:00
|
|
|
req := &BatchJobRequest{}
|
2024-05-06 16:27:52 -04:00
|
|
|
if err := req.load(ctx, j.objLayer, result.Item.Name); err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
batchLogIf(ctx, err)
|
2022-10-03 05:10:15 -04:00
|
|
|
continue
|
|
|
|
}
|
2023-08-11 16:12:35 -04:00
|
|
|
_, nodeIdx := parseRequestToken(req.ID)
|
|
|
|
if nodeIdx > -1 && GetProxyEndpointLocalIndex(globalProxyEndpoints) != nodeIdx {
|
|
|
|
// This job doesn't belong on this node.
|
|
|
|
continue
|
|
|
|
}
|
2022-10-03 05:10:15 -04:00
|
|
|
if err := j.queueJob(req); err != nil {
|
2024-04-04 08:04:40 -04:00
|
|
|
batchLogIf(ctx, err)
|
2022-10-03 05:10:15 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddWorker adds a replication worker to the pool
|
|
|
|
func (j *BatchJobPool) AddWorker() {
|
|
|
|
if j == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-j.ctx.Done():
|
|
|
|
return
|
|
|
|
case job, ok := <-j.jobCh:
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
2023-12-02 05:51:33 -05:00
|
|
|
switch {
|
|
|
|
case job.Replicate != nil:
|
2023-03-31 13:48:36 -04:00
|
|
|
if job.Replicate.RemoteToLocal() {
|
|
|
|
if err := job.Replicate.StartFromSource(job.ctx, j.objLayer, *job); err != nil {
|
|
|
|
if !isErrBucketNotFound(err) {
|
2024-04-04 08:04:40 -04:00
|
|
|
batchLogIf(j.ctx, err)
|
2023-03-31 13:48:36 -04:00
|
|
|
j.canceler(job.ID, false)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Bucket not found proceed to delete such a job.
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if err := job.Replicate.Start(job.ctx, j.objLayer, *job); err != nil {
|
|
|
|
if !isErrBucketNotFound(err) {
|
2024-04-04 08:04:40 -04:00
|
|
|
batchLogIf(j.ctx, err)
|
2023-03-31 13:48:36 -04:00
|
|
|
j.canceler(job.ID, false)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Bucket not found proceed to delete such a job.
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
}
|
2023-12-02 05:51:33 -05:00
|
|
|
case job.KeyRotate != nil:
|
2023-04-04 13:56:54 -04:00
|
|
|
if err := job.KeyRotate.Start(job.ctx, j.objLayer, *job); err != nil {
|
|
|
|
if !isErrBucketNotFound(err) {
|
2024-04-04 08:04:40 -04:00
|
|
|
batchLogIf(j.ctx, err)
|
2023-04-04 13:56:54 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2023-12-02 05:51:33 -05:00
|
|
|
case job.Expire != nil:
|
|
|
|
if err := job.Expire.Start(job.ctx, j.objLayer, *job); err != nil {
|
|
|
|
if !isErrBucketNotFound(err) {
|
2024-04-04 08:04:40 -04:00
|
|
|
batchLogIf(j.ctx, err)
|
2023-12-02 05:51:33 -05:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
2023-04-04 13:56:54 -04:00
|
|
|
}
|
2022-10-03 05:10:15 -04:00
|
|
|
job.delete(j.ctx, j.objLayer)
|
2023-03-18 02:42:43 -04:00
|
|
|
j.canceler(job.ID, false)
|
2022-10-03 05:10:15 -04:00
|
|
|
case <-j.workerKillCh:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ResizeWorkers sets replication workers pool to new size
|
|
|
|
func (j *BatchJobPool) ResizeWorkers(n int) {
|
|
|
|
if j == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
j.mu.Lock()
|
|
|
|
defer j.mu.Unlock()
|
|
|
|
|
|
|
|
for j.workerSize < n {
|
|
|
|
j.workerSize++
|
|
|
|
go j.AddWorker()
|
|
|
|
}
|
|
|
|
for j.workerSize > n {
|
|
|
|
j.workerSize--
|
|
|
|
go func() { j.workerKillCh <- struct{}{} }()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *BatchJobPool) queueJob(req *BatchJobRequest) error {
|
|
|
|
if j == nil {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
2023-03-18 02:42:43 -04:00
|
|
|
jctx, jcancel := context.WithCancel(j.ctx)
|
|
|
|
j.jmu.Lock()
|
|
|
|
j.jobCancelers[req.ID] = jcancel
|
|
|
|
j.jmu.Unlock()
|
|
|
|
req.ctx = jctx
|
|
|
|
|
2022-10-03 05:10:15 -04:00
|
|
|
select {
|
|
|
|
case <-j.ctx.Done():
|
|
|
|
j.once.Do(func() {
|
2024-01-28 13:04:17 -05:00
|
|
|
xioutil.SafeClose(j.jobCh)
|
2022-10-03 05:10:15 -04:00
|
|
|
})
|
|
|
|
case j.jobCh <- req:
|
|
|
|
default:
|
|
|
|
return fmt.Errorf("batch job queue is currently full please try again later %#v", req)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-03-18 02:42:43 -04:00
|
|
|
// delete canceler from the map, cancel job if requested
|
|
|
|
func (j *BatchJobPool) canceler(jobID string, cancel bool) error {
|
|
|
|
if j == nil {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
j.jmu.Lock()
|
|
|
|
defer j.jmu.Unlock()
|
|
|
|
if canceler, ok := j.jobCancelers[jobID]; ok {
|
|
|
|
if cancel {
|
|
|
|
canceler()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
delete(j.jobCancelers, jobID)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-10-03 05:10:15 -04:00
|
|
|
//msgp:ignore batchJobMetrics
|
|
|
|
type batchJobMetrics struct {
|
|
|
|
sync.RWMutex
|
2023-02-13 15:07:58 -05:00
|
|
|
metrics map[string]*batchJobInfo
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
|
2023-04-04 13:56:54 -04:00
|
|
|
//msgp:ignore batchJobMetric
|
|
|
|
//go:generate stringer -type=batchJobMetric -trimprefix=batchJobMetric $GOFILE
|
|
|
|
type batchJobMetric uint8
|
2022-10-03 05:10:15 -04:00
|
|
|
|
|
|
|
const (
|
2023-12-02 05:51:33 -05:00
|
|
|
batchJobMetricReplication batchJobMetric = iota
|
|
|
|
batchJobMetricKeyRotation
|
|
|
|
batchJobMetricExpire
|
2022-10-03 05:10:15 -04:00
|
|
|
)
|
|
|
|
|
2023-12-02 05:51:33 -05:00
|
|
|
func batchJobTrace(d batchJobMetric, job string, startTime time.Time, duration time.Duration, info objTraceInfoer, attempts int, err error) madmin.TraceInfo {
|
2022-10-03 05:10:15 -04:00
|
|
|
var errStr string
|
|
|
|
if err != nil {
|
|
|
|
errStr = err.Error()
|
|
|
|
}
|
2023-04-04 13:56:54 -04:00
|
|
|
traceType := madmin.TraceBatchReplication
|
2023-12-02 05:51:33 -05:00
|
|
|
switch d {
|
|
|
|
case batchJobMetricKeyRotation:
|
2023-04-04 13:56:54 -04:00
|
|
|
traceType = madmin.TraceBatchKeyRotation
|
2023-12-02 05:51:33 -05:00
|
|
|
case batchJobMetricExpire:
|
|
|
|
traceType = madmin.TraceBatchExpire
|
2023-04-04 13:56:54 -04:00
|
|
|
}
|
2023-12-02 05:51:33 -05:00
|
|
|
funcName := fmt.Sprintf("%s() (job-name=%s)", d.String(), job)
|
2022-10-03 05:10:15 -04:00
|
|
|
if attempts > 0 {
|
2023-12-02 05:51:33 -05:00
|
|
|
funcName = fmt.Sprintf("%s() (job-name=%s,attempts=%s)", d.String(), job, humanize.Ordinal(attempts))
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
return madmin.TraceInfo{
|
2023-04-04 13:56:54 -04:00
|
|
|
TraceType: traceType,
|
2022-10-03 05:10:15 -04:00
|
|
|
Time: startTime,
|
|
|
|
NodeName: globalLocalNodeName,
|
|
|
|
FuncName: funcName,
|
|
|
|
Duration: duration,
|
2023-12-02 05:51:33 -05:00
|
|
|
Path: fmt.Sprintf("%s (versionID=%s)", info.TraceObjName(), info.TraceVersionID()),
|
2022-10-03 05:10:15 -04:00
|
|
|
Error: errStr,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-02 05:51:33 -05:00
|
|
|
func (ri *batchJobInfo) metric() madmin.JobMetric {
|
|
|
|
m := madmin.JobMetric{
|
|
|
|
JobID: ri.JobID,
|
|
|
|
JobType: ri.JobType,
|
|
|
|
StartTime: ri.StartTime,
|
|
|
|
LastUpdate: ri.LastUpdate,
|
|
|
|
RetryAttempts: ri.RetryAttempts,
|
|
|
|
Complete: ri.Complete,
|
|
|
|
Failed: ri.Failed,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ri.JobType {
|
|
|
|
case string(madmin.BatchJobReplicate):
|
|
|
|
m.Replicate = &madmin.ReplicateInfo{
|
|
|
|
Bucket: ri.Bucket,
|
|
|
|
Object: ri.Object,
|
|
|
|
Objects: ri.Objects,
|
|
|
|
ObjectsFailed: ri.ObjectsFailed,
|
|
|
|
BytesTransferred: ri.BytesTransferred,
|
|
|
|
BytesFailed: ri.BytesFailed,
|
|
|
|
}
|
|
|
|
case string(madmin.BatchJobKeyRotate):
|
|
|
|
m.KeyRotate = &madmin.KeyRotationInfo{
|
|
|
|
Bucket: ri.Bucket,
|
|
|
|
Object: ri.Object,
|
|
|
|
Objects: ri.Objects,
|
|
|
|
ObjectsFailed: ri.ObjectsFailed,
|
|
|
|
}
|
|
|
|
case string(madmin.BatchJobExpire):
|
|
|
|
m.Expired = &madmin.ExpirationInfo{
|
|
|
|
Bucket: ri.Bucket,
|
|
|
|
Object: ri.Object,
|
|
|
|
Objects: ri.Objects,
|
|
|
|
ObjectsFailed: ri.ObjectsFailed,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2022-10-03 05:10:15 -04:00
|
|
|
func (m *batchJobMetrics) report(jobID string) (metrics *madmin.BatchJobMetrics) {
|
|
|
|
metrics = &madmin.BatchJobMetrics{CollectedAt: time.Now(), Jobs: make(map[string]madmin.JobMetric)}
|
|
|
|
m.RLock()
|
|
|
|
defer m.RUnlock()
|
2023-08-15 15:22:30 -04:00
|
|
|
|
2023-12-02 05:51:33 -05:00
|
|
|
if jobID != "" {
|
|
|
|
if job, ok := m.metrics[jobID]; ok {
|
|
|
|
metrics.Jobs[jobID] = job.metric()
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
2023-12-02 05:51:33 -05:00
|
|
|
return metrics
|
|
|
|
}
|
2023-08-15 15:22:30 -04:00
|
|
|
|
2023-12-02 05:51:33 -05:00
|
|
|
for id, job := range m.metrics {
|
|
|
|
metrics.Jobs[id] = job.metric()
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
return metrics
|
|
|
|
}
|
|
|
|
|
2023-08-15 15:22:30 -04:00
|
|
|
// keep job metrics for some time after the job is completed
|
|
|
|
// in-case some one wants to look at the older results.
|
|
|
|
func (m *batchJobMetrics) purgeJobMetrics() {
|
|
|
|
t := time.NewTicker(6 * time.Hour)
|
|
|
|
defer t.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-GlobalContext.Done():
|
|
|
|
return
|
|
|
|
case <-t.C:
|
|
|
|
var toDeleteJobMetrics []string
|
|
|
|
m.RLock()
|
|
|
|
for id, metrics := range m.metrics {
|
2024-07-02 04:17:52 -04:00
|
|
|
if time.Since(metrics.LastUpdate) > oldJobsExpiration && (metrics.Complete || metrics.Failed) {
|
2023-08-15 15:22:30 -04:00
|
|
|
toDeleteJobMetrics = append(toDeleteJobMetrics, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m.RUnlock()
|
|
|
|
for _, jobID := range toDeleteJobMetrics {
|
|
|
|
m.delete(jobID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 05:10:15 -04:00
|
|
|
func (m *batchJobMetrics) delete(jobID string) {
|
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
|
|
|
delete(m.metrics, jobID)
|
|
|
|
}
|
|
|
|
|
2023-02-13 15:07:58 -05:00
|
|
|
func (m *batchJobMetrics) save(jobID string, ri *batchJobInfo) {
|
2022-10-03 05:10:15 -04:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
|
|
|
|
2023-02-13 15:07:58 -05:00
|
|
|
m.metrics[jobID] = ri.clone()
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
|
2023-12-02 05:51:33 -05:00
|
|
|
type objTraceInfoer interface {
|
|
|
|
TraceObjName() string
|
|
|
|
TraceVersionID() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// TraceObjName returns name of object being traced
|
|
|
|
func (td ObjectToDelete) TraceObjName() string {
|
|
|
|
return td.ObjectName
|
|
|
|
}
|
|
|
|
|
|
|
|
// TraceVersionID returns version-id of object being traced
|
|
|
|
func (td ObjectToDelete) TraceVersionID() string {
|
|
|
|
return td.VersionID
|
|
|
|
}
|
|
|
|
|
|
|
|
// TraceObjName returns name of object being traced
|
|
|
|
func (oi ObjectInfo) TraceObjName() string {
|
|
|
|
return oi.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
// TraceVersionID returns version-id of object being traced
|
|
|
|
func (oi ObjectInfo) TraceVersionID() string {
|
|
|
|
return oi.VersionID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *batchJobMetrics) trace(d batchJobMetric, job string, attempts int) func(info objTraceInfoer, err error) {
|
2022-10-03 05:10:15 -04:00
|
|
|
startTime := time.Now()
|
2023-12-02 05:51:33 -05:00
|
|
|
return func(info objTraceInfoer, err error) {
|
2022-10-03 05:10:15 -04:00
|
|
|
duration := time.Since(startTime)
|
2023-12-02 05:51:33 -05:00
|
|
|
if globalTrace.NumSubscribers(madmin.TraceBatch) > 0 {
|
|
|
|
globalTrace.Publish(batchJobTrace(d, job, startTime, duration, info, attempts, err))
|
|
|
|
return
|
|
|
|
}
|
2023-04-04 13:56:54 -04:00
|
|
|
switch d {
|
2023-12-02 05:51:33 -05:00
|
|
|
case batchJobMetricReplication:
|
2023-04-04 13:56:54 -04:00
|
|
|
if globalTrace.NumSubscribers(madmin.TraceBatchReplication) > 0 {
|
|
|
|
globalTrace.Publish(batchJobTrace(d, job, startTime, duration, info, attempts, err))
|
|
|
|
}
|
2023-12-02 05:51:33 -05:00
|
|
|
case batchJobMetricKeyRotation:
|
2023-04-04 13:56:54 -04:00
|
|
|
if globalTrace.NumSubscribers(madmin.TraceBatchKeyRotation) > 0 {
|
|
|
|
globalTrace.Publish(batchJobTrace(d, job, startTime, duration, info, attempts, err))
|
|
|
|
}
|
2023-12-02 05:51:33 -05:00
|
|
|
case batchJobMetricExpire:
|
|
|
|
if globalTrace.NumSubscribers(madmin.TraceBatchExpire) > 0 {
|
|
|
|
globalTrace.Publish(batchJobTrace(d, job, startTime, duration, info, attempts, err))
|
|
|
|
}
|
2022-10-03 05:10:15 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-21 18:16:31 -04:00
|
|
|
|
|
|
|
func lookupStyle(s string) miniogo.BucketLookupType {
|
|
|
|
var lookup miniogo.BucketLookupType
|
|
|
|
switch s {
|
|
|
|
case "on":
|
|
|
|
lookup = miniogo.BucketLookupPath
|
|
|
|
case "off":
|
|
|
|
lookup = miniogo.BucketLookupDNS
|
|
|
|
default:
|
|
|
|
lookup = miniogo.BucketLookupAuto
|
|
|
|
|
|
|
|
}
|
|
|
|
return lookup
|
|
|
|
}
|
2024-08-01 08:53:30 -04:00
|
|
|
|
|
|
|
// BatchJobPrefix - to support prefix field yaml unmarshalling with string or slice of strings
|
|
|
|
type BatchJobPrefix []string
|
|
|
|
|
|
|
|
var _ yaml.Unmarshaler = &BatchJobPrefix{}
|
|
|
|
|
|
|
|
// UnmarshalYAML - to support prefix field yaml unmarshalling with string or slice of strings
|
|
|
|
func (b *BatchJobPrefix) UnmarshalYAML(value *yaml.Node) error {
|
|
|
|
// try slice first
|
|
|
|
tmpSlice := []string{}
|
|
|
|
if err := value.Decode(&tmpSlice); err == nil {
|
|
|
|
*b = tmpSlice
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// try string
|
|
|
|
tmpStr := ""
|
|
|
|
if err := value.Decode(&tmpStr); err == nil {
|
|
|
|
*b = []string{tmpStr}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("unable to decode %s", value.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// F - return prefix(es) as slice
|
|
|
|
func (b *BatchJobPrefix) F() []string {
|
|
|
|
return *b
|
|
|
|
}
|