| 1 |
package desync |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"context" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"net/url" |
| 9 |
"strings" |
| 10 |
|
| 11 |
"cloud.google.com/go/storage" |
| 12 |
"github.com/pkg/errors" |
| 13 |
"github.com/sirupsen/logrus" |
| 14 |
"google.golang.org/api/iterator" |
| 15 |
) |
| 16 |
|
| 17 |
var _ WriteStore = GCStore{} |
| 18 |
|
| 19 |
// GCStoreBase is the base object for all chunk and index stores with Google |
| 20 |
// Storage backing |
| 21 |
type GCStoreBase struct { |
| 22 |
Location string |
| 23 |
client *storage.BucketHandle |
| 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 |
// GCStore is a read-write store with Google Storage backing |
| 34 |
type GCStore struct { |
| 35 |
GCStoreBase |
| 36 |
} |
| 37 |
|
| 38 |
// normalizeGCPrefix converts path to a regular format, |
| 39 |
// where there never is a leading slash, |
| 40 |
// and every folder name always is followed by a slash |
| 41 |
// so example outputs will be: |
| 42 |
// |
| 43 |
// <blank string> |
| 44 |
// folder1/ |
| 45 |
// folder1/folder2/folder3/ |
| 46 |
func normalizeGCPrefix(path string) string { |
| 47 |
prefix := strings.Trim(path, "/") |
| 48 |
|
| 49 |
if prefix != "" { |
| 50 |
prefix += "/" |
| 51 |
} |
| 52 |
|
| 53 |
return prefix |
| 54 |
} |
| 55 |
|
| 56 |
// NewGCStoreBase initializes a base object used for chunk or index stores |
| 57 |
// backed by Google Storage. |
| 58 |
func NewGCStoreBase(u *url.URL, opt StoreOptions) (GCStoreBase, error) { |
| 59 |
ctx := context.TODO() |
| 60 |
converters, err := opt.StorageConverters() |
| 61 |
if err != nil { |
| 62 |
return GCStoreBase{}, err |
| 63 |
} |
| 64 |
s := GCStoreBase{Location: u.String(), opt: opt, converters: converters, extension: converters.storageExtension()} |
| 65 |
if u.Scheme != "gs" { |
| 66 |
return s, fmt.Errorf("invalid scheme '%s', expected 'gs'", u.Scheme) |
| 67 |
} |
| 68 |
|
| 69 |
// Pull the bucket as well as the prefix from a path-style URL |
| 70 |
s.bucket = u.Host |
| 71 |
s.prefix = normalizeGCPrefix(u.Path) |
| 72 |
|
| 73 |
client, err := storage.NewClient(ctx) |
| 74 |
if err != nil { |
| 75 |
return s, errors.Wrap(err, s.String()) |
| 76 |
} |
| 77 |
|
| 78 |
s.client = client.Bucket(s.bucket) |
| 79 |
return s, nil |
| 80 |
} |
| 81 |
|
| 82 |
func (s GCStoreBase) String() string { |
| 83 |
return s.Location |
| 84 |
} |
| 85 |
|
| 86 |
// Close the GCS base store. NOP operation but needed to implement the store interface. |
| 87 |
func (s GCStoreBase) Close() error { return nil } |
| 88 |
|
| 89 |
// NewGCStore creates a chunk store with Google Storage backing. The URL |
| 90 |
// should be provided like this: gs://bucketname/prefix |
| 91 |
// Credentials are passed in via the environment variables. TODO |
| 92 |
func NewGCStore(location *url.URL, opt StoreOptions) (s GCStore, e error) { |
| 93 |
b, err := NewGCStoreBase(location, opt) |
| 94 |
if err != nil { |
| 95 |
return s, err |
| 96 |
} |
| 97 |
return GCStore{b}, nil |
| 98 |
} |
| 99 |
|
| 100 |
// GetChunk reads and returns one chunk from the store |
| 101 |
func (s GCStore) GetChunk(id ChunkID) (*Chunk, error) { |
| 102 |
ctx := context.TODO() |
| 103 |
name := s.nameFromID(id) |
| 104 |
|
| 105 |
var ( |
| 106 |
log = Log.WithFields(logrus.Fields{ |
| 107 |
"bucket": s.bucket, |
| 108 |
"name": name, |
| 109 |
}) |
| 110 |
) |
| 111 |
|
| 112 |
rc, err := s.client.Object(name).NewReader(ctx) |
| 113 |
|
| 114 |
if errors.Is(err, storage.ErrObjectNotExist) { |
| 115 |
log.Warning("Unable to create reader for object in GCS bucket; the object may not exist, or the bucket may not exist, or you may not have permission to access it") |
| 116 |
return nil, ChunkMissing{ID: id} |
| 117 |
} else if err != nil { |
| 118 |
log.WithError(err).Error("Unable to retrieve object from GCS bucket") |
| 119 |
return nil, errors.Wrap(err, s.String()) |
| 120 |
} |
| 121 |
defer rc.Close() |
| 122 |
|
| 123 |
b, err := io.ReadAll(rc) |
| 124 |
|
| 125 |
if errors.Is(err, storage.ErrObjectNotExist) { |
| 126 |
log.Warning("Unable to read from object in GCS bucket; the object may not exist, or the bucket may not exist, or you may not have permission to access it") |
| 127 |
return nil, ChunkMissing{ID: id} |
| 128 |
} else if err != nil { |
| 129 |
log.WithError(err).Error("Unable to retrieve object from GCS bucket") |
| 130 |
return nil, errors.Wrap(err, fmt.Sprintf("chunk %s could not be retrieved from GCS bucket", id)) |
| 131 |
} |
| 132 |
|
| 133 |
log.Debug("Retrieved chunk from GCS bucket") |
| 134 |
|
| 135 |
return NewChunkFromStorage(id, b, s.converters, s.opt.SkipVerify) |
| 136 |
} |
| 137 |
|
| 138 |
// StoreChunk adds a new chunk to the store |
| 139 |
func (s GCStore) StoreChunk(chunk *Chunk) error { |
| 140 |
|
| 141 |
ctx := context.TODO() |
| 142 |
contentType := "application/zstd" |
| 143 |
name := s.nameFromID(chunk.ID()) |
| 144 |
|
| 145 |
var ( |
| 146 |
log = Log.WithFields(logrus.Fields{ |
| 147 |
"bucket": s.bucket, |
| 148 |
"name": name, |
| 149 |
}) |
| 150 |
) |
| 151 |
|
| 152 |
b, err := chunk.Storage(s.converters) |
| 153 |
if err != nil { |
| 154 |
log.WithError(err).Error("Cannot retrieve chunk data") |
| 155 |
return err |
| 156 |
} |
| 157 |
|
| 158 |
r := bytes.NewReader(b) |
| 159 |
w := s.client.Object(name).NewWriter(ctx) |
| 160 |
w.ContentType = contentType |
| 161 |
_, err = io.Copy(w, r) |
| 162 |
|
| 163 |
if err != nil { |
| 164 |
log.WithError(err).Error("Error when copying data from local filesystem to object in GCS bucket") |
| 165 |
return errors.Wrap(err, s.String()) |
| 166 |
} |
| 167 |
|
| 168 |
err = w.Close() |
| 169 |
if err != nil { |
| 170 |
log.WithError(err).Error("Error when finalizing copying of data from local filesystem to object in GCS bucket") |
| 171 |
return errors.Wrap(err, s.String()) |
| 172 |
} |
| 173 |
|
| 174 |
log.Debug("Uploaded chunk to GCS bucket") |
| 175 |
return nil |
| 176 |
} |
| 177 |
|
| 178 |
// HasChunk returns true if the chunk is in the store |
| 179 |
func (s GCStore) HasChunk(id ChunkID) (bool, error) { |
| 180 |
|
| 181 |
ctx := context.TODO() |
| 182 |
name := s.nameFromID(id) |
| 183 |
|
| 184 |
var ( |
| 185 |
log = Log.WithFields(logrus.Fields{ |
| 186 |
"bucket": s.bucket, |
| 187 |
"name": name, |
| 188 |
}) |
| 189 |
) |
| 190 |
|
| 191 |
_, err := s.client.Object(name).Attrs(ctx) |
| 192 |
|
| 193 |
if errors.Is(err, storage.ErrObjectNotExist) { |
| 194 |
log.WithField("exists", false).Debug("Chunk does not exist in GCS bucket") |
| 195 |
return false, nil |
| 196 |
} else if err != nil { |
| 197 |
log.WithError(err).Error("Unable to query attributes for object in GCS bucket") |
| 198 |
return false, err |
| 199 |
} else { |
| 200 |
log.WithField("exists", true).Debug("Chunk exists in GCS bucket") |
| 201 |
return true, nil |
| 202 |
} |
| 203 |
} |
| 204 |
|
| 205 |
// RemoveChunk deletes a chunk, typically an invalid one, from the filesystem. |
| 206 |
// Used when verifying and repairing caches. |
| 207 |
func (s GCStore) RemoveChunk(id ChunkID) error { |
| 208 |
ctx := context.TODO() |
| 209 |
name := s.nameFromID(id) |
| 210 |
|
| 211 |
var ( |
| 212 |
log = Log.WithFields(logrus.Fields{ |
| 213 |
"bucket": s.bucket, |
| 214 |
"name": name, |
| 215 |
}) |
| 216 |
) |
| 217 |
|
| 218 |
err := s.client.Object(name).Delete(ctx) |
| 219 |
|
| 220 |
if err != nil { |
| 221 |
log.WithError(err).Error("Unable to delete object in GCS bucket") |
| 222 |
return err |
| 223 |
} else { |
| 224 |
log.Debug("Removed chunk from GCS bucket") |
| 225 |
return nil |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
// Prune removes any chunks from the store that are not contained in a list (map) |
| 230 |
func (s GCStore) Prune(ctx context.Context, ids map[ChunkID]struct{}) error { |
| 231 |
query := &storage.Query{Prefix: s.prefix} |
| 232 |
it := s.client.Objects(ctx, query) |
| 233 |
for { |
| 234 |
attrs, err := it.Next() |
| 235 |
if err == iterator.Done { |
| 236 |
break |
| 237 |
} |
| 238 |
if err != nil { |
| 239 |
return err |
| 240 |
} |
| 241 |
|
| 242 |
id, err := chunkIDFromObjectName(attrs.Name, s.prefix, s.extension) |
| 243 |
if err != nil { |
| 244 |
continue |
| 245 |
} |
| 246 |
|
| 247 |
// Drop the chunk if it's not on the list |
| 248 |
if _, ok := ids[id]; !ok { |
| 249 |
if err = s.RemoveChunk(id); err != nil { |
| 250 |
return err |
| 251 |
} |
| 252 |
} |
| 253 |
} |
| 254 |
return nil |
| 255 |
} |
| 256 |
|
| 257 |
func (s GCStore) nameFromID(id ChunkID) string { |
| 258 |
sID := id.String() |
| 259 |
name := s.prefix + sID[0:4] + "/" + sID + s.extension |
| 260 |
return name |
| 261 |
} |