-
Notifications
You must be signed in to change notification settings - Fork 783
/
output_cloud_storage.go
353 lines (298 loc) · 10.7 KB
/
output_cloud_storage.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package gcp
import (
"context"
"errors"
"fmt"
"path"
"sync"
"time"
"cloud.google.com/go/storage"
"github.com/gofrs/uuid"
"go.uber.org/multierr"
"github.com/benthosdev/benthos/v4/internal/component/output"
"github.com/benthosdev/benthos/v4/public/service"
)
const (
// Cloud Storage Output Fields
csoFieldBucket = "bucket"
csoFieldPath = "path"
csoFieldContentType = "content_type"
csoFieldContentEncoding = "content_encoding"
csoFieldChunkSize = "chunk_size"
csoFieldMaxInFlight = "max_in_flight"
csoFieldBatching = "batching"
csoFieldCollisionMode = "collision_mode"
csoFieldTimeout = "timeout"
// GCPCloudStorageErrorIfExistsCollisionMode - error-if-exists.
GCPCloudStorageErrorIfExistsCollisionMode = "error-if-exists"
// GCPCloudStorageAppendCollisionMode - append.
GCPCloudStorageAppendCollisionMode = "append"
// GCPCloudStorageIgnoreCollisionMode - ignore.
GCPCloudStorageIgnoreCollisionMode = "ignore"
// GCPCloudStorageOverwriteCollisionMode - overwrite.
GCPCloudStorageOverwriteCollisionMode = "overwrite"
)
type csoConfig struct {
Bucket string
Path *service.InterpolatedString
ContentType *service.InterpolatedString
ContentEncoding *service.InterpolatedString
ChunkSize int
CollisionMode string
Timeout time.Duration
}
func csoConfigFromParsed(pConf *service.ParsedConfig) (conf csoConfig, err error) {
if conf.Bucket, err = pConf.FieldString(csoFieldBucket); err != nil {
return
}
if conf.Path, err = pConf.FieldInterpolatedString(csoFieldPath); err != nil {
return
}
if conf.ContentType, err = pConf.FieldInterpolatedString(csoFieldContentType); err != nil {
return
}
if conf.ContentEncoding, err = pConf.FieldInterpolatedString(csoFieldContentEncoding); err != nil {
return
}
if conf.ChunkSize, err = pConf.FieldInt(csoFieldChunkSize); err != nil {
return
}
if conf.CollisionMode, err = pConf.FieldString(csoFieldCollisionMode); err != nil {
return
}
if conf.Timeout, err = pConf.FieldDuration(csoFieldTimeout); err != nil {
return
}
return
}
func csoSpec() *service.ConfigSpec {
return service.NewConfigSpec().
Beta().
Version("3.43.0").
Categories("Services", "GCP").
Summary(`Sends message parts as objects to a Google Cloud Storage bucket. Each object is uploaded with the path specified with the `+"`path`"+` field.`).
Description(output.Description(true, true, `
In order to have a different path for each object you should use function interpolations described [here](/docs/configuration/interpolation#bloblang-queries), which are calculated per message of a batch.
### Metadata
Metadata fields on messages will be sent as headers, in order to mutate these values (or remove them) check out the [metadata docs](/docs/configuration/metadata).
### Credentials
By default Benthos will use a shared credentials file when connecting to GCP services. You can find out more [in this document](/docs/guides/cloud/gcp).
### Batching
It's common to want to upload messages to Google Cloud Storage as batched archives, the easiest way to do this is to batch your messages at the output level and join the batch of messages with an `+"[`archive`](/docs/components/processors/archive)"+` and/or `+"[`compress`](/docs/components/processors/compress)"+` processor.
For example, if we wished to upload messages as a .tar.gz archive of documents we could achieve that with the following config:
`+"```yaml"+`
output:
gcp_cloud_storage:
bucket: TODO
path: ${!count("files")}-${!timestamp_unix_nano()}.tar.gz
batching:
count: 100
period: 10s
processors:
- archive:
format: tar
- compress:
algorithm: gzip
`+"```"+`
Alternatively, if we wished to upload JSON documents as a single large document containing an array of objects we can do that with:
`+"```yaml"+`
output:
gcp_cloud_storage:
bucket: TODO
path: ${!count("files")}-${!timestamp_unix_nano()}.json
batching:
count: 100
processors:
- archive:
format: json_array
`+"```"+``)).
Fields(
service.NewStringField(csoFieldBucket).
Description("The bucket to upload messages to."),
service.NewInterpolatedStringField(csoFieldPath).
Description("The path of each message to upload.").
Example(`${!count("files")}-${!timestamp_unix_nano()}.txt`).
Example(`${!meta("kafka_key")}.json`).
Example(`${!json("doc.namespace")}/${!json("doc.id")}.json`).
Default(`${!count("files")}-${!timestamp_unix_nano()}.txt`),
service.NewInterpolatedStringField(csoFieldContentType).
Description("The content type to set for each object.").
Default("application/octet-stream"),
service.NewInterpolatedStringField(csoFieldContentEncoding).
Description("An optional content encoding to set for each object.").
Default("").
Advanced(),
service.NewStringAnnotatedEnumField(csoFieldCollisionMode, map[string]string{
"overwrite": "Replace the existing file with the new one.",
"append": "Append the message bytes to the original file.",
"error-if-exists": "Return an error, this is the equivalent of a nack.",
"ignore": "Do not modify the original file, the new data will be dropped.",
}).
Description(`Determines how file path collisions should be dealt with.`).
Version("3.53.0").
Default(GCPCloudStorageOverwriteCollisionMode),
service.NewIntField(csoFieldChunkSize).
Description("An optional chunk size which controls the maximum number of bytes of the object that the Writer will attempt to send to the server in a single request. If ChunkSize is set to zero, chunking will be disabled.").
Advanced().
Default(16*1024*1024), // googleapi.DefaultUploadChunkSize
service.NewDurationField(csoFieldTimeout).
Description("The maximum period to wait on an upload before abandoning it and reattempting.").
Example("1s").
Example("500ms").
Default("3s"),
service.NewOutputMaxInFlightField().
Description("The maximum number of message batches to have in flight at a given time. Increase this to improve throughput."),
service.NewBatchPolicyField(csoFieldBatching),
)
}
func init() {
err := service.RegisterBatchOutput("gcp_cloud_storage", csoSpec(),
func(conf *service.ParsedConfig, mgr *service.Resources) (out service.BatchOutput, batchPolicy service.BatchPolicy, maxInFlight int, err error) {
if maxInFlight, err = conf.FieldMaxInFlight(); err != nil {
return
}
if batchPolicy, err = conf.FieldBatchPolicy(csoFieldBatching); err != nil {
return
}
var pConf csoConfig
if pConf, err = csoConfigFromParsed(conf); err != nil {
return
}
out, err = newGCPCloudStorageOutput(pConf, mgr)
return
})
if err != nil {
panic(err)
}
}
// gcpCloudStorageOutput is a benthos writer.Type implementation that writes
// messages to a GCP Cloud Storage bucket.
type gcpCloudStorageOutput struct {
conf csoConfig
client *storage.Client
connMut sync.RWMutex
log *service.Logger
}
// newGCPCloudStorageOutput creates a new GCP Cloud Storage bucket writer.Type.
func newGCPCloudStorageOutput(conf csoConfig, res *service.Resources) (*gcpCloudStorageOutput, error) {
g := &gcpCloudStorageOutput{
conf: conf,
log: res.Logger(),
}
return g, nil
}
// Connect attempts to establish a connection to the target Google
// Cloud Storage bucket.
func (g *gcpCloudStorageOutput) Connect(ctx context.Context) error {
g.connMut.Lock()
defer g.connMut.Unlock()
var err error
g.client, err = storage.NewClient(context.Background())
if err != nil {
return err
}
return nil
}
func (g *gcpCloudStorageOutput) WriteBatch(ctx context.Context, batch service.MessageBatch) error {
g.connMut.RLock()
client := g.client
g.connMut.RUnlock()
if client == nil {
return service.ErrNotConnected
}
ctx, cancel := context.WithTimeout(ctx, g.conf.Timeout)
defer cancel()
return batch.WalkWithBatchedErrors(func(i int, msg *service.Message) error {
metadata := map[string]string{}
_ = msg.MetaWalk(func(k, v string) error {
metadata[k] = v
return nil
})
outputPath, err := g.conf.Path.TryString(msg)
if err != nil {
return fmt.Errorf("path interpolation error: %w", err)
}
if g.conf.CollisionMode != GCPCloudStorageOverwriteCollisionMode {
_, err = client.Bucket(g.conf.Bucket).Object(outputPath).Attrs(ctx)
}
isMerge := false
var tempPath string
if errors.Is(err, storage.ErrObjectNotExist) || g.conf.CollisionMode == GCPCloudStorageOverwriteCollisionMode {
tempPath = outputPath
} else {
isMerge = true
if g.conf.CollisionMode == GCPCloudStorageErrorIfExistsCollisionMode {
if err == nil {
err = fmt.Errorf("file at path already exists: %s", outputPath)
}
return err
} else if g.conf.CollisionMode == GCPCloudStorageIgnoreCollisionMode {
return nil
}
tempUUID, err := uuid.NewV4()
if err != nil {
return err
}
dir := path.Dir(outputPath)
tempFileName := tempUUID.String() + ".tmp"
tempPath = path.Join(dir, tempFileName)
g.log.Tracef("creating temporary file for the merge %q", tempPath)
}
src := client.Bucket(g.conf.Bucket).Object(tempPath)
w := src.NewWriter(ctx)
w.ChunkSize = g.conf.ChunkSize
if w.ContentType, err = g.conf.ContentType.TryString(msg); err != nil {
return fmt.Errorf("content type interpolation error: %w", err)
}
if w.ContentEncoding, err = g.conf.ContentEncoding.TryString(msg); err != nil {
return fmt.Errorf("content encoding interpolation error: %w", err)
}
w.Metadata = metadata
mBytes, err := msg.AsBytes()
if err != nil {
return err
}
var errs error
if _, werr := w.Write(mBytes); werr != nil {
errs = multierr.Append(errs, werr)
}
if cerr := w.Close(); cerr != nil {
errs = multierr.Append(errs, cerr)
}
if isMerge {
defer g.removeTempFile(ctx, src)
}
if errs != nil {
return errs
}
if isMerge {
dst := client.Bucket(g.conf.Bucket).Object(outputPath)
if aerr := g.appendToFile(ctx, src, dst); aerr != nil {
return aerr
}
}
return nil
})
}
// Close begins cleaning up resources used by this reader asynchronously.
func (g *gcpCloudStorageOutput) Close(context.Context) error {
g.connMut.Lock()
defer g.connMut.Unlock()
var err error
if g.client != nil {
err = g.client.Close()
g.client = nil
}
return err
}
func (g *gcpCloudStorageOutput) appendToFile(ctx context.Context, src, dst *storage.ObjectHandle) error {
_, err := dst.ComposerFrom(dst, src).Run(ctx)
return err
}
func (g *gcpCloudStorageOutput) removeTempFile(ctx context.Context, src *storage.ObjectHandle) {
// Remove the temporary file used for the merge
g.log.Tracef("remove the temporary file used for the merge %q", src.ObjectName())
if err := src.Delete(ctx); err != nil {
g.log.Errorf("Failed to delete temporary file used for merging: %v", err)
}
}