-
Notifications
You must be signed in to change notification settings - Fork 178
/
gcp_uploader.go
74 lines (57 loc) · 2.13 KB
/
gcp_uploader.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
package uploader
import (
"context"
"fmt"
"cloud.google.com/go/storage"
"github.com/hashicorp/go-multierror"
"github.com/rs/zerolog"
"github.com/onflow/flow-go/engine/execution"
)
type GCPBucketUploader struct {
log zerolog.Logger
bucket *storage.BucketHandle
ctx context.Context
}
func NewGCPBucketUploader(ctx context.Context, bucketName string, log zerolog.Logger) (*GCPBucketUploader, error) {
// no need to close the client according to documentation
// https://pkg.go.dev/cloud.google.com/go/storage#Client.Close
client, err := storage.NewClient(ctx)
if err != nil {
return nil, fmt.Errorf("cannot create GCP Bucket client: %w", err)
}
bucket := client.Bucket(bucketName)
// try accessing buckets to validate settings
_, err = bucket.Attrs(ctx)
if err != nil {
return nil, fmt.Errorf("error while listing bucket attributes: %w", err)
}
return &GCPBucketUploader{
bucket: bucket,
log: log.With().Str("subcomponent", "gcp_bucket_uploader").Logger(),
ctx: ctx,
}, nil
}
// Upload uploads the computation result to the configured GCP bucket.
// All errors returned from this function can be considered benign.
func (u *GCPBucketUploader) Upload(computationResult *execution.ComputationResult) error {
var errs *multierror.Error
objectName := GCPBlockDataObjectName(computationResult)
object := u.bucket.Object(objectName)
writer := object.NewWriter(u.ctx)
// serialize and write computation result to upload stream
err := WriteComputationResultsTo(computationResult, writer)
if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error while writing computation result to GCP object: %w", err))
// fall through because we always want to close the writer
}
// flush and close the stream
err = writer.Close()
// this occasionally fails with HTTP 5xx errors due to flakiness on the network/GCP API
if err != nil {
errs = multierror.Append(errs, fmt.Errorf("error while closing GCP object: %w", err))
}
return errs.ErrorOrNil()
}
func GCPBlockDataObjectName(computationResult *execution.ComputationResult) string {
return fmt.Sprintf("%s.cbor", computationResult.ExecutableBlock.ID().String())
}