| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"net/url" |
| 9 |
"strings" |
| 10 |
"time" |
| 11 |
|
| 12 |
minio "github.com/minio/minio-go/v6" |
| 13 |
"github.com/minio/minio-go/v6/pkg/credentials" |
| 14 |
"github.com/pkg/errors" |
| 15 |
"github.com/sirupsen/logrus" |
| 16 |
) |
| 17 |
|
| 18 |
var _ WriteStore = S3Store{} |
| 19 |
|
| 20 |
// S3StoreBase is the base object for all chunk and index stores with S3 backing |
| 21 |
type S3StoreBase struct { |
| 22 |
Location string |
| 23 |
client *minio.Client |
| 24 |
bucket string |
| 25 |
prefix string |
| 26 |
opt StoreOptions |
| 27 |
converters Converters |
| 28 |
|
| 29 |
// Chunk file extension, derived from the converters at construction |
| 30 |
extension string |
| 31 |
} |
| 32 |
|
| 33 |
// S3Store is a read-write store with S3 backing |
| 34 |
type S3Store struct { |
| 35 |
S3StoreBase |
| 36 |
} |
| 37 |
|
| 38 |
// NewS3StoreBase initializes a base object used for chunk or index stores backed by S3. |
| 39 |
func NewS3StoreBase(u *url.URL, s3Creds *credentials.Credentials, region string, opt StoreOptions, lookupType minio.BucketLookupType) (S3StoreBase, error) { |
| 40 |
converters, err := opt.StorageConverters() |
| 41 |
if err != nil { |
| 42 |
return S3StoreBase{}, err |
| 43 |
} |
| 44 |
s := S3StoreBase{Location: u.String(), opt: opt, converters: converters, extension: converters.storageExtension()} |
| 45 |
if !strings.HasPrefix(u.Scheme, "s3+http") { |
| 46 |
return s, fmt.Errorf("invalid scheme '%s', expected 's3+http' or 's3+https'", u.Scheme) |
| 47 |
} |
| 48 |
var useSSL bool |
| 49 |
if strings.Contains(u.Scheme, "https") { |
| 50 |
useSSL = true |
| 51 |
} |
| 52 |
|
| 53 |
// Pull the bucket as well as the prefix from a path-style URL |
| 54 |
bPath := strings.Trim(u.Path, "/") |
| 55 |
if bPath == "" { |
| 56 |
return s, fmt.Errorf("expected bucket name in path of '%s'", u.Scheme) |
| 57 |
} |
| 58 |
f := strings.Split(bPath, "/") |
| 59 |
s.bucket = f[0] |
| 60 |
s.prefix = strings.Join(f[1:], "/") |
| 61 |
|
| 62 |
if s.prefix != "" { |
| 63 |
s.prefix += "/" |
| 64 |
} |
| 65 |
|
| 66 |
s.client, err = minio.NewWithOptions(u.Host, &minio.Options{ |
| 67 |
Creds: s3Creds, |
| 68 |
Secure: useSSL, |
| 69 |
Region: region, |
| 70 |
BucketLookup: lookupType, |
| 71 |
}) |
| 72 |
if err != nil { |
| 73 |
return s, errors.Wrap(err, u.String()) |
| 74 |
} |
| 75 |
return s, nil |
| 76 |
} |
| 77 |
|
| 78 |
func (s S3StoreBase) String() string { |
| 79 |
return s.Location |
| 80 |
} |
| 81 |
|
| 82 |
// Close the S3 base store. NOP operation but needed to implement the store interface. |
| 83 |
func (s S3StoreBase) Close() error { return nil } |
| 84 |
|
| 85 |
// NewS3Store creates a chunk store with S3 backing. The URL |
| 86 |
// should be provided like this: s3+http://host:port/bucket |
| 87 |
// Credentials are passed in via the environment variables S3_ACCESS_KEY |
| 88 |
// and S3_SECRET_KEY, or via the desync config file. |
| 89 |
func NewS3Store(location *url.URL, s3Creds *credentials.Credentials, region string, opt StoreOptions, lookupType minio.BucketLookupType) (s S3Store, e error) { |
| 90 |
b, err := NewS3StoreBase(location, s3Creds, region, opt, lookupType) |
| 91 |
if err != nil { |
| 92 |
return s, err |
| 93 |
} |
| 94 |
return S3Store{b}, nil |
| 95 |
} |
| 96 |
|
| 97 |
// GetChunk reads and returns one chunk from the store |
| 98 |
func (s S3Store) GetChunk(id ChunkID) (*Chunk, error) { |
| 99 |
name := s.nameFromID(id) |
| 100 |
var attempt int |
| 101 |
retry: |
| 102 |
attempt++ |
| 103 |
obj, err := s.client.GetObject(s.bucket, name, minio.GetObjectOptions{}) |
| 104 |
if err != nil { |
| 105 |
if attempt <= s.opt.ErrorRetry { |
| 106 |
time.Sleep(time.Duration(attempt) * s.opt.ErrorRetryBaseInterval) |
| 107 |
goto retry |
| 108 |
} |
| 109 |
return nil, errors.Wrap(err, s.String()) |
| 110 |
} |
| 111 |
defer obj.Close() |
| 112 |
|
| 113 |
b, err := io.ReadAll(obj) |
| 114 |
if err != nil { |
| 115 |
// Don't retry if the chunk or the bucket doesn't exist, those aren't |
| 116 |
// transient errors. A missing chunk in particular is a normal |
| 117 |
// occurrence when this store is behind a router or cache. |
| 118 |
if e, ok := err.(minio.ErrorResponse); ok { |
| 119 |
switch e.Code { |
| 120 |
case "NoSuchBucket": |
| 121 |
return nil, fmt.Errorf("bucket '%s' does not exist", s.bucket) |
| 122 |
case "NoSuchKey": |
| 123 |
return nil, ChunkMissing{ID: id} |
| 124 |
} |
| 125 |
} |
| 126 |
if attempt <= s.opt.ErrorRetry { |
| 127 |
obj.Close() |
| 128 |
time.Sleep(time.Duration(attempt) * s.opt.ErrorRetryBaseInterval) |
| 129 |
goto retry |
| 130 |
} |
| 131 |
// Without ListBucket perms in AWS, we get Permission Denied for a missing chunk, not 404 |
| 132 |
return nil, errors.Wrap(err, fmt.Sprintf("chunk %s could not be retrieved from s3 store", id)) |
| 133 |
} |
| 134 |
|
| 135 |
// A short read of the chunk body (e.g. flaky transport/endpoint) can leave us |
| 136 |
// with truncated data that fails to decompress or hash. Treat that the same as |
| 137 |
// other transient errors and retry under the --error-retry policy. |
| 138 |
chunk, err := NewChunkFromStorage(id, b, s.converters, s.opt.SkipVerify) |
| 139 |
if err != nil { |
| 140 |
if attempt <= s.opt.ErrorRetry { |
| 141 |
Log.WithFields(logrus.Fields{ |
| 142 |
"chunk": id, |
| 143 |
"object": name, |
| 144 |
"attempt": attempt, |
| 145 |
}).WithError(err).Info("chunk failed validation, retrying") |
| 146 |
obj.Close() |
| 147 |
time.Sleep(time.Duration(attempt) * s.opt.ErrorRetryBaseInterval) |
| 148 |
goto retry |
| 149 |
} |
| 150 |
return nil, errors.Wrap(err, s.String()) |
| 151 |
} |
| 152 |
return chunk, nil |
| 153 |
} |
| 154 |
|
| 155 |
// StoreChunk adds a new chunk to the store |
| 156 |
func (s S3Store) StoreChunk(chunk *Chunk) error { |
| 157 |
contentType := "application/zstd" |
| 158 |
name := s.nameFromID(chunk.ID()) |
| 159 |
b, err := chunk.Storage(s.converters) |
| 160 |
if err != nil { |
| 161 |
return err |
| 162 |
} |
| 163 |
var attempt int |
| 164 |
retry: |
| 165 |
attempt++ |
| 166 |
_, err = s.client.PutObject(s.bucket, name, bytes.NewReader(b), int64(len(b)), minio.PutObjectOptions{ContentType: contentType}) |
| 167 |
if err != nil { |
| 168 |
if attempt < s.opt.ErrorRetry { |
| 169 |
time.Sleep(time.Duration(attempt) * s.opt.ErrorRetryBaseInterval) |
| 170 |
goto retry |
| 171 |
} |
| 172 |
} |
| 173 |
return errors.Wrap(err, s.String()) |
| 174 |
} |
| 175 |
|
| 176 |
// HasChunk returns true if the chunk is in the store |
| 177 |
func (s S3Store) HasChunk(id ChunkID) (bool, error) { |
| 178 |
name := s.nameFromID(id) |
| 179 |
_, err := s.client.StatObject(s.bucket, name, minio.StatObjectOptions{}) |
| 180 |
return err == nil, nil |
| 181 |
} |
| 182 |
|
| 183 |
// RemoveChunk deletes a chunk, typically an invalid one, from the filesystem. |
| 184 |
// Used when verifying and repairing caches. |
| 185 |
func (s S3Store) RemoveChunk(id ChunkID) error { |
| 186 |
name := s.nameFromID(id) |
| 187 |
return s.client.RemoveObject(s.bucket, name) |
| 188 |
} |
| 189 |
|
| 190 |
// Prune removes any chunks from the store that are not contained in a list (map) |
| 191 |
func (s S3Store) Prune(ctx context.Context, ids map[ChunkID]struct{}) error { |
| 192 |
doneCh := make(chan struct{}) |
| 193 |
defer close(doneCh) |
| 194 |
objectCh := s.client.ListObjectsV2(s.bucket, s.prefix, true, doneCh) |
| 195 |
for object := range objectCh { |
| 196 |
if object.Err != nil { |
| 197 |
return object.Err |
| 198 |
} |
| 199 |
// See if we're meant to stop |
| 200 |
select { |
| 201 |
case <-ctx.Done(): |
| 202 |
return Interrupted{} |
| 203 |
default: |
| 204 |
} |
| 205 |
|
| 206 |
id, err := chunkIDFromObjectName(object.Key, s.prefix, s.extension) |
| 207 |
if err != nil { |
| 208 |
continue |
| 209 |
} |
| 210 |
|
| 211 |
// Drop the chunk if it's not on the list |
| 212 |
if _, ok := ids[id]; !ok { |
| 213 |
if err = s.RemoveChunk(id); err != nil { |
| 214 |
return err |
| 215 |
} |
| 216 |
} |
| 217 |
} |
| 218 |
return nil |
| 219 |
} |
| 220 |
|
| 221 |
func (s S3Store) nameFromID(id ChunkID) string { |
| 222 |
sID := id.String() |
| 223 |
name := s.prefix + sID[0:4] + "/" + sID + s.extension |
| 224 |
return name |
| 225 |
} |