-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
285 lines (238 loc) · 8.88 KB
/
main.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
package main
import (
"compress/gzip"
"context"
"errors"
"flag"
"fmt"
"log"
"os"
"os/signal"
"runtime"
"strings"
"syscall"
"time"
"cloud.google.com/go/pubsub"
"github.com/mrbuk/gcs-compressor/core"
)
var (
compressionLevel int
sourceBucketName string
sourceObjectName string
destinationBucketName string
destinationObjectName string
subscriptionName string
topicName string
projectId string
subscription *pubsub.Subscription
topic *pubsub.Topic
mainCtx context.Context
mainCancel context.CancelFunc
)
const WORKFLOW_TIMEOUT = 60 * time.Minute
func init() {
flag.IntVar(&compressionLevel, "compressionLevel", gzip.DefaultCompression, "NoCompression = 0, BestSpeed = 1, BestCompression = 9, DefaultCompression = -1, HuffmanOnly = -2")
flag.StringVar(&sourceBucketName, "sourceBucket", "", "name of bucket to read from: e.g. gcs-source-bucket [required]")
flag.StringVar(&destinationBucketName, "destinationBucket", "", "name of bucket to write to: e.g. gcs-destination bucket [required]")
flag.StringVar(&sourceObjectName, "sourceObjectName", "", "name of uncompressed source object [cli-driven]")
flag.StringVar(&destinationObjectName, "destinationObjectName", "", "name of compressed destination object [cli-driven]")
flag.StringVar(&subscriptionName, "subscription", "", "name of the PubSub subscription to listen for storage notifications [event-driven]")
flag.StringVar(&topicName, "topic", "", "name of the PubSub topic used to republish messages in case of a shutdown mid-processing [event-driven]")
flag.StringVar(&projectId, "projectId", pubsub.DetectProjectID, "Google Cloud project id used for the PubSub client")
flag.Parse()
}
func validateFlags() {
// check for required values
if sourceBucketName == "" || destinationBucketName == "" {
fmt.Fprintf(flag.CommandLine.Output(), "error: -sourceBucket and -destinationBucket are required\n\n")
flag.PrintDefaults()
os.Exit(1)
}
// ensure that only one sourceObjectName or subscription is set
if (sourceObjectName == "" && subscriptionName == "") || (sourceObjectName != "" && subscriptionName != "") {
fmt.Fprintf(flag.CommandLine.Output(), "error: provide either -sourceObjectName for cli xor -subscription\n\n")
flag.PrintDefaults()
os.Exit(1)
}
if destinationObjectName == "" {
destinationObjectName = sourceObjectName
}
if sourceBucketName == destinationBucketName && sourceObjectName == destinationObjectName {
fmt.Fprintf(flag.CommandLine.Output(),
"error: when using the same -sourceBucket and -destinationBucket, -subscription cannot be used.\n"+
" when using the same -sourceBucket and -destinationBucket, -destinationObjectName must be different from -sourceObjectName\n\n")
flag.PrintDefaults()
os.Exit(1)
}
if subscriptionName != "" && topicName == "" {
fmt.Fprintf(flag.CommandLine.Output(), "error: when usign -subscription -topic needs to be provided.\n\n")
flag.PrintDefaults()
os.Exit(1)
}
}
func main() {
validateFlags()
// use two different context to allow to cancel workers and giving them
// time to cleanup / republish messages that have not been fully processed
mainCtx, mainCancel = context.WithCancel(context.Background())
workerCtx, workerCancel := context.WithCancel(mainCtx)
// single file should be compressed
if sourceObjectName != "" {
wf, err := core.NewWorkflow(mainCtx, compressionLevel, sourceBucketName, sourceObjectName, destinationBucketName, destinationObjectName)
if err != nil {
log.Fatalf("error with storage client: %v", err)
}
defer wf.Close()
err = wf.Compress(mainCtx)
if err != nil {
log.Fatalf("error compressing object: %v", err)
}
err = wf.Delete(mainCtx)
if err != nil {
log.Fatalf("error deleting source object: %v", err)
}
return
}
// event driven
pubSubClient, err := pubsub.NewClient(workerCtx, projectId)
if err != nil {
log.Fatal(err)
}
noOfConcurrentJob := runtime.NumCPU() - 1
if noOfConcurrentJob <= 0 {
noOfConcurrentJob = 1
}
// create a worker pool to paralellize compression
jobs := make(chan core.WorkflowContext, noOfConcurrentJob)
for w := 1; w <= noOfConcurrentJob; w++ {
go worker(workerCtx, w, jobs)
}
log.Printf("topic used for republishing '%s'", topicName)
topic = pubSubClient.Topic(topicName)
log.Printf("subscribing to '%s'\n", subscriptionName)
subscription = pubSubClient.Subscription(subscriptionName)
c := shutdownSignal(mainCancel, workerCancel)
defer func() {
signal.Stop(c)
close(jobs)
topic.Stop()
pubSubClient.Close()
}()
log.Printf("waiting for messages on '%s'\n", subscriptionName)
err = subscription.Receive(workerCtx, func(_ context.Context, msg *pubsub.Message) {
bucketId := msg.Attributes["bucketId"]
if bucketId != sourceBucketName {
log.Printf("ignoring event - received for bucket '%s' but expected to get it for bucket '%s'. Potentially storage notification misconfigured.\n", bucketId, sourceBucketName)
msg.Ack()
return
}
objectId := msg.Attributes["objectId"]
if objectId == "" {
log.Printf("ignoring event for empty object: %v\n", msg.Attributes)
msg.Ack()
return
}
// ingore files containing 'dax-tmp'
if objectId == "" || strings.Contains(objectId, "dax-tmp") {
log.Printf("ignoring event for temp object: '%s'\n", objectId)
msg.Ack()
return
}
// ingore events other than finalize (e.g. delete)
eventType := msg.Attributes["eventType"]
if eventType != "OBJECT_FINALIZE" {
log.Printf("ignoring event of type '%s' for objectId '%s'\n", eventType, objectId)
msg.Ack()
return
}
// write into event into BQ and ack the message directly
// the max allowed ack deadline for Pubsub is 600s
// compressing large files takes than 600s resulting into
// potential duplicates if not acked directly
// TODO ensure to write to BQ before we ACK
msg.Ack()
jobs <- core.WorkflowContext{
ObjectName: objectId,
OriginalMessageAttributes: msg.Attributes,
OriginalMessageData: msg.Data,
}
})
if err != nil {
log.Fatal("sub.Receive: %w", err)
}
<-mainCtx.Done()
}
func worker(ctx context.Context, id int, jobs <-chan core.WorkflowContext) {
for cdata := range jobs {
workerName := fmt.Sprintf("[worker-%d]", id)
objectName := cdata.ObjectName
newContextData := core.WorkflowContext{
WorkerName: workerName,
ObjectName: cdata.ObjectName,
OriginalMessageAttributes: cdata.OriginalMessageAttributes,
OriginalMessageData: cdata.OriginalMessageData,
}
log.Printf("%s - '%s' compressing from bucket / '%s' -> bucket '%s' / '%s'", workerName, objectName, sourceBucketName, destinationBucketName, objectName)
func() {
lctx, lcancel := context.WithTimeout(context.WithValue(ctx, core.ContextData, newContextData), WORKFLOW_TIMEOUT)
defer lcancel()
wf, err := core.NewWorkflow(lctx, compressionLevel, sourceBucketName, objectName, destinationBucketName, objectName)
if err != nil {
handleWorkerError(lctx, "failed with error with storage client", err)
return
}
defer wf.Close()
err = wf.Compress(lctx)
if err != nil {
handleWorkerError(lctx, "failed with error compressing object", err)
return
}
err = wf.Delete(lctx)
if err != nil {
handleWorkerError(lctx, "failed with error deleting source object", err)
return
}
log.Printf("%s - finished job for %s\n", workerName, objectName)
}()
}
}
func handleWorkerError(ctx context.Context, errMsg string, cause error) {
cdata, err := core.GetContextData(ctx)
if err != nil {
log.Printf("cannot republish message due to issue with context: %v", err)
}
workerName := cdata.WorkerName
objectName := cdata.ObjectName
log.Printf("%s - '%s' %s: %v", workerName, objectName, errMsg, cause)
if cause == context.Canceled || errors.Unwrap(cause) == context.Canceled {
log.Printf("%s - '%s' context canceled. re-publishing message for reprocessing", workerName, objectName)
nCtx, _ := context.WithTimeout(mainCtx, 5*time.Second)
r := topic.Publish(nCtx, &pubsub.Message{
Attributes: cdata.OriginalMessageAttributes,
Data: cdata.OriginalMessageData,
})
msgId, err := r.Get(nCtx)
if err != nil {
log.Printf("'%s' - error republishing message on topic: %v", objectName, err)
return
}
log.Printf("'%s' - republished message with id '%s'", objectName, msgId)
}
}
func shutdownSignal(mainCancel, workerCancel context.CancelFunc) chan<- os.Signal {
// catch SIGINT and properly cancel and cleanup
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
go func() {
sig := <-c
signal.Stop(c)
log.Printf("received signal %v", sig)
// cancel the workers, wait 7s - the docker default timeout before forefully killing is 10s -
// to allow for cleanup / republishing of messages
log.Printf("canceling all workers and waiting 7s before stopping - issue another signal to kill immediatlely")
workerCancel()
time.Sleep(7 * time.Second)
mainCancel()
}()
return c
}