-
Notifications
You must be signed in to change notification settings - Fork 863
/
client.go
207 lines (175 loc) · 7.24 KB
/
client.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//go:generate mockgen -copyright_file ../../../../LICENSE -package $GOPACKAGE -source $GOFILE -destination client_mock.go
package connector
import (
"bytes"
"context"
"errors"
"io"
"os"
"cloud.google.com/go/storage"
"go.uber.org/multierr"
"google.golang.org/api/iterator"
"go.temporal.io/server/common/archiver"
"go.temporal.io/server/common/config"
)
var (
// ErrBucketNotFound is non retryable error that is thrown when the bucket doesn't exist
ErrBucketNotFound = errors.New("bucket not found")
errObjectNotFound = errors.New("object not found")
)
type (
// Precondition is a function that allow you to filter a query result.
// If subject match params conditions then return true, else return false.
Precondition func(subject interface{}) bool
// Client is a wrapper around Google cloud storages client library.
Client interface {
Upload(ctx context.Context, URI archiver.URI, fileName string, file []byte) error
Get(ctx context.Context, URI archiver.URI, file string) ([]byte, error)
Query(ctx context.Context, URI archiver.URI, fileNamePrefix string) ([]string, error)
QueryWithFilters(ctx context.Context, URI archiver.URI, fileNamePrefix string, pageSize, offset int, filters []Precondition) ([]string, bool, int, error)
Exist(ctx context.Context, URI archiver.URI, fileName string) (bool, error)
}
storageWrapper struct {
client GcloudStorageClient
}
)
// NewClient return a Temporal gcloudstorage.Client based on default google service account creadentials (ScopeFullControl required).
// Bucket must be created by Iaas scripts, in other words, this library doesn't create the required Bucket.
// Optionaly you can set your credential path throught "GOOGLE_APPLICATION_CREDENTIALS" environment variable or through temporal config file.
// You can find more info about "Google Setting Up Authentication for Server to Server Production Applications" under the following link
// https://cloud.google.com/docs/authentication/production
func NewClient(ctx context.Context, config *config.GstorageArchiver) (Client, error) {
if credentialsPath := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS"); credentialsPath != "" {
clientDelegate, err := newClientDelegateWithCredentials(ctx, credentialsPath)
return &storageWrapper{client: clientDelegate}, err
}
if config.CredentialsPath != "" {
clientDelegate, err := newClientDelegateWithCredentials(ctx, config.CredentialsPath)
return &storageWrapper{client: clientDelegate}, err
}
clientDelegate, err := newDefaultClientDelegate(ctx)
return &storageWrapper{client: clientDelegate}, err
}
// NewClientWithParams return a gcloudstorage.Client based on input parameters
func NewClientWithParams(clientD GcloudStorageClient) (Client, error) {
return &storageWrapper{client: clientD}, nil
}
// Upload push a file to gcloud storage bucket (sinkPath)
// example:
// Upload(ctx, mockBucketHandleClient, "gs://my-bucket-cad/temporal_archival/development", "45273645-fileName.history", fileReader)
func (s *storageWrapper) Upload(ctx context.Context, URI archiver.URI, fileName string, file []byte) (err error) {
bucket := s.client.Bucket(URI.Hostname())
writer := bucket.Object(formatSinkPath(URI.Path()) + "/" + fileName).NewWriter(ctx)
_, err = io.Copy(writer, bytes.NewReader(file))
if err == nil {
err = writer.Close()
}
return err
}
// Exist check if a bucket or an object exist
// If fileName is empty, then 'Exist' function will only check if the given bucket exist.
func (s *storageWrapper) Exist(ctx context.Context, URI archiver.URI, fileName string) (exists bool, err error) {
bucket := s.client.Bucket(URI.Hostname())
if _, err := bucket.Attrs(ctx); err != nil {
return false, err
}
if fileName == "" {
return true, nil
}
if _, err = bucket.Object(fileName).Attrs(ctx); err != nil {
return false, errObjectNotFound
}
return true, nil
}
// Get retrieve a file
func (s *storageWrapper) Get(ctx context.Context, URI archiver.URI, fileName string) (fileContent []byte, err error) {
bucket := s.client.Bucket(URI.Hostname())
reader, err := bucket.Object(formatSinkPath(URI.Path()) + "/" + fileName).NewReader(ctx)
if err != nil {
return nil, err
}
defer func() {
err = multierr.Combine(err, reader.Close())
}()
return io.ReadAll(reader)
}
// Query, retieves file names by provided storage query
func (s *storageWrapper) Query(ctx context.Context, URI archiver.URI, fileNamePrefix string) (fileNames []string, err error) {
fileNames = make([]string, 0)
bucket := s.client.Bucket(URI.Hostname())
var attrs = new(storage.ObjectAttrs)
it := bucket.Objects(ctx, &storage.Query{
Prefix: formatSinkPath(URI.Path()) + "/" + fileNamePrefix,
})
for {
attrs, err = it.Next()
if err == iterator.Done {
return fileNames, nil
}
fileNames = append(fileNames, attrs.Name)
}
}
// QueryWithFilters, retieves filenames that match filter parameters. PageSize is optional, 0 means all records.
func (s *storageWrapper) QueryWithFilters(ctx context.Context, URI archiver.URI, fileNamePrefix string, pageSize, offset int, filters []Precondition) ([]string, bool, int, error) {
var err error
currentPos := offset
resultSet := make([]string, 0)
bucket := s.client.Bucket(URI.Hostname())
var attrs = new(storage.ObjectAttrs)
it := bucket.Objects(ctx, &storage.Query{
Prefix: formatSinkPath(URI.Path()) + "/" + fileNamePrefix,
})
for {
attrs, err = it.Next()
if err == iterator.Done {
return resultSet, true, currentPos, nil
}
if completed := isPageCompleted(pageSize, len(resultSet)); completed {
return resultSet, completed, currentPos, err
}
valid := true
for _, f := range filters {
if valid = f(attrs.Name); !valid {
break
}
}
if valid {
if offset > 0 {
offset--
continue
}
// if match parsedQuery criteria and current cursor position is the last known position (offset is zero), append fileName to resultSet
resultSet = append(resultSet, attrs.Name)
currentPos++
}
}
}
func isPageCompleted(pageSize, currentPosition int) bool {
return pageSize != 0 && currentPosition > 0 && pageSize <= currentPosition
}
func formatSinkPath(sinkPath string) string {
return sinkPath[1:]
}