forked from minio/minio
/
warm-backend.go
156 lines (139 loc) · 3.83 KB
/
warm-backend.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) 2015-2021 MinIO, Inc.
//
// This file is part of MinIO Object Storage stack
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package cmd
import (
"bytes"
"context"
"errors"
"fmt"
"io"
"github.com/minio/madmin-go/v2"
xhttp "github.com/minio/minio/internal/http"
)
// WarmBackendGetOpts is used to express byte ranges within an object. The zero
// value represents the entire byte range of an object.
type WarmBackendGetOpts struct {
startOffset int64
length int64
}
// WarmBackend provides interface to be implemented by remote tier backends
type WarmBackend interface {
Put(ctx context.Context, object string, r io.Reader, length int64) (remoteVersionID, error)
Get(ctx context.Context, object string, rv remoteVersionID, opts WarmBackendGetOpts) (io.ReadCloser, error)
Remove(ctx context.Context, object string, rv remoteVersionID) error
InUse(ctx context.Context) (bool, error)
}
const probeObject = "probeobject"
// checkWarmBackend checks if tier config credentials have sufficient privileges
// to perform all operations defined in the WarmBackend interface.
func checkWarmBackend(ctx context.Context, w WarmBackend) error {
var empty bytes.Reader
rv, err := w.Put(ctx, probeObject, &empty, 0)
if err != nil {
if _, ok := err.(BackendDown); ok {
return err
}
return tierPermErr{
Op: tierPut,
Err: err,
}
}
r, err := w.Get(ctx, probeObject, rv, WarmBackendGetOpts{})
xhttp.DrainBody(r)
if err != nil {
if _, ok := err.(BackendDown); ok {
return err
}
switch {
case isErrBucketNotFound(err):
return errTierBucketNotFound
case isErrSignatureDoesNotMatch(err):
return errTierInvalidCredentials
default:
return tierPermErr{
Op: tierGet,
Err: err,
}
}
}
if err = w.Remove(ctx, probeObject, rv); err != nil {
if _, ok := err.(BackendDown); ok {
return err
}
return tierPermErr{
Op: tierDelete,
Err: err,
}
}
return err
}
type tierOp uint8
const (
_ tierOp = iota
tierGet
tierPut
tierDelete
)
func (op tierOp) String() string {
switch op {
case tierGet:
return "GET"
case tierPut:
return "PUT"
case tierDelete:
return "DELETE"
}
return "UNKNOWN"
}
type tierPermErr struct {
Op tierOp
Err error
}
func (te tierPermErr) Error() string {
return fmt.Sprintf("failed to perform %s %v", te.Op, te.Err)
}
func errIsTierPermError(err error) bool {
var tpErr tierPermErr
return errors.As(err, &tpErr)
}
// remoteVersionID represents the version id of an object in the remote tier.
// Its usage is remote tier cloud implementation specific.
type remoteVersionID string
// newWarmBackend instantiates the tier type specific WarmBackend, runs
// checkWarmBackend on it.
func newWarmBackend(ctx context.Context, tier madmin.TierConfig) (d WarmBackend, err error) {
switch tier.Type {
case madmin.S3:
d, err = newWarmBackendS3(*tier.S3, tier.Name)
case madmin.Azure:
d, err = newWarmBackendAzure(*tier.Azure, tier.Name)
case madmin.GCS:
d, err = newWarmBackendGCS(*tier.GCS, tier.Name)
case madmin.MinIO:
d, err = newWarmBackendMinIO(*tier.MinIO, tier.Name)
default:
return nil, errTierTypeUnsupported
}
if err != nil {
return nil, errTierTypeUnsupported
}
err = checkWarmBackend(ctx, d)
if err != nil {
return nil, err
}
return d, nil
}