-
Notifications
You must be signed in to change notification settings - Fork 14
/
workflow.yaml
395 lines (377 loc) · 14 KB
/
workflow.yaml
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
main:
params: [args]
steps:
- init:
assign:
- project: ${sys.get_env("GOOGLE_CLOUD_PROJECT_ID")}
- cloud_function: ${default(map.get(args, "cloud_function"), "gaarf")}
- function_location: ${default(map.get(args, "location"), "us-central1")}
- bq_dataset_location: ${if(default(map.get(args, "bq_dataset_location"), "us") == "", "us", default(map.get(args, "bq_dataset_location"), "us"))}
- gcs_bucket: ${default(map.get(args,"gcs_bucket"), project)}
- only_run_bq: ${default(map.get(args,"only_run_bq"), false)}
- completion_topic: ${default(map.get(args, "completion_topic"), "gaarf_wf_completed")}
# 20 is the default for "Concurrent branches and iterations" - https://cloud.google.com/workflows/quotas#parallel_steps
- concurrency_limit: ${default(map.get(args, "concurrency_limit"), 20)}
- workflow_ads_id: ${default(map.get(args, "workflow_ads_id"), sys.get_env("GOOGLE_CLOUD_WORKFLOW_ID") + "-ads")}
#- recreate_dataset: ${default(map.get(args,"recreate_dataset"), false)}
- disable_strict_views: ${default(map.get(args,"disable_strict_views"), false)}
- check_shortcut_arg:
switch:
- condition: ${only_run_bq}
next: run_bq_workflow
next: run_ads_workflow
- run_ads_workflow:
call: runAdsQueries
args:
project: ${project}
function_location: ${function_location}
function_name: ${cloud_function}
gcs_bucket: ${gcs_bucket}
queries_path: ${args.ads_queries_path}
ads_config_path: ${args.ads_config_path}
cid: ${args.cid}
cid_ignore: ${map.get(args, "cid_ignore")}
customer_ids_query: ${map.get(args, "customer_ids_query")}
customer_ids_offset: ${map.get(args, "customer_ids_offset")}
customer_ids_batchsize: ${map.get(args, "customer_ids_batchsize")}
writer: ${default(map.get(args, "writer"), "bq")}
bq_dataset: ${default(map.get(args,"dataset"), "")}
bq_dataset_location: ${bq_dataset_location}
macros: ${map.get(args, "ads_macro")}
writer_options: ${default(map.get(args, "writer_options"), map.get(args, "bq_writer_options"))}
output_path: ${map.get(args, "output_path")}
concurrency_limit: ${concurrency_limit}
workflow_ads_id: ${workflow_ads_id}
disable_strict_views: ${disable_strict_views}
result: accounts
- run_bq_workflow:
call: runBigQueryQueries
args:
project: ${project}
function_name: ${cloud_function + "-bq"}
function_location: ${function_location}
gcs_bucket: ${gcs_bucket}
queries_path: ${args.bq_queries_path}
dataset_location: ${bq_dataset_location}
macros: ${map.get(args, "bq_macro")}
sqlParams: ${map.get(args, "bq_sql")}
- create_completion_message:
assign:
- message:
accounts: ${len(accounts)}
- base64Msg: ${base64.encode(json.encode(message))}
- publish_completion_message:
call: googleapis.pubsub.v1.projects.topics.publish
args:
topic: ${"projects/" + project + "/topics/" + completion_topic}
body:
messages:
- data: ${base64Msg}
result: publishResult
- return_result:
return: ${accounts}
runAdsQueries:
params:
[
project,
function_name,
function_location,
gcs_bucket,
queries_path,
ads_config_path,
cid,
cid_ignore,
customer_ids_query,
customer_ids_offset,
customer_ids_batchsize,
writer,
bq_dataset,
bq_dataset_location,
macros,
writer_options,
output_path,
concurrency_limit,
workflow_ads_id,
disable_strict_views,
]
# NOTE: currently it's assumed that CF's project is the same as project for BQ datasets
steps:
# get CF 'gaarf-getcids' function's URL
- get_function_cids:
call: http.get
args:
url: ${"https://cloudfunctions.googleapis.com/v2/projects/" + project + "/locations/" + function_location + "/functions/" + function_name + "-getcids"}
auth:
type: OAuth2
result: function_cids
# get CF 'gaarf-bq-view' function's URL
- get_function_view:
call: http.get
args:
url: ${"https://cloudfunctions.googleapis.com/v2/projects/" + project + "/locations/" + function_location + "/functions/" + function_name + "-bq-view"}
auth:
type: OAuth2
result: function_view
#call 'gaarf-getcids' CF to get a list of customer ids for further processing
- call_gaarf_cids_cf:
call: http.post
args:
url: ${function_cids.body.serviceConfig.uri}
timeout: 1800 # maximum allowed timeout in Workflows is 1800 (even though CF gen2 support 3600)
query:
ads_config_path: ${ads_config_path}
customer_id: ${cid}
customer_ids_ignore: ${default(cid_ignore, "")}
customer_ids_query: ${default(customer_ids_query, "")}
customer_ids_offset: ${default(customer_ids_offset, "")}
customer_ids_batchsize: ${default(customer_ids_batchsize, "")}
auth:
type: OIDC
result: accounts_response
- set_accounts_from_cf_response:
assign:
- accounts: ${accounts_response.body.accounts}
- log_cids:
call: sys.log
args:
json:
text: "accounts to process"
batches: ${len(accounts)}
accounts: ${accounts}
severity: "INFO"
# fetch script from GCS
- get_ads_scripts_from_gcs:
call: googleapis.storage.v1.objects.list
args:
bucket: ${gcs_bucket}
prefix: ${queries_path}
result: scripts_raw
- initialize_filtered_list:
assign:
- scripts: []
- filter_sql_files:
for:
value: script_item
in: ${scripts_raw.items}
steps:
- check_sql_extension:
switch:
- condition: ${text.match_regex(script_item.name, "[.]sql$")}
assign:
- scripts: ${list.concat(scripts, "gs://" + script_item.bucket + "/" + script_item.name)}
# now `scripts` is a list of GCS uris of SQL scripts
- log_ads_scripts:
call: sys.log
args:
json:
count: ${len(scripts)}
scripts: ${scripts}
severity: "INFO"
# loop over accounts grouped by batches (to overcome the limit of max steps in workflow)
- iteration_over_batches:
for:
value: accounts_batch
in: ${accounts}
index: index
steps:
- log_cids_batch:
call: sys.log
args:
json:
text: "Executing Ads subworkflow"
index: ${index}
count: ${len(accounts_batch)}
accounts: ${accounts_batch}
severity: "INFO"
- run_ads_workflow:
call: googleapis.workflowexecutions.v1.projects.locations.workflows.executions.run
args:
workflow_id: ${workflow_ads_id}
connector_params:
timeout: 86400 # 24h=60*60*24 seconds
argument:
scripts: ${scripts}
accounts: ${accounts_batch}
function_name: ${function_name}
function_location: ${function_location}
ads_config_path: ${ads_config_path}
writer: ${writer}
bq_dataset: ${bq_dataset}
bq_dataset_location: ${bq_dataset_location}
macros: ${macros}
writer_options: ${writer_options}
output_path: ${output_path}
concurrency_limit: ${concurrency_limit}
result: execution_result
# collecting account ids from `accounts` where they are slit into batched into a flatten list
- initialize_accounts_flatten:
assign:
- accounts_flatten: []
- flatten_accounts:
for:
value: accounts_batch
in: ${accounts}
steps:
- loop_over_batch:
for:
value: account
in: ${accounts_batch}
steps:
- append:
assign:
- accounts_flatten: ${list.concat(accounts_flatten, account)}
- check_for_bq_writer:
switch:
- condition: ${writer == "bq" or writer == "bigquery"}
next: create_views
next: return_result
# for each script (excluding constants) create a unified view
- create_views:
for:
value: script_item
in: ${scripts}
steps:
- check_for_constant:
switch:
- condition: ${text.match_regex(script_item, "_constant")}
next: continue
next: call_create_view_cf
- call_create_view_cf:
call: http.post
args:
url: ${function_view.body.serviceConfig.uri}
timeout: 1800
query:
project_id: ${project}
dataset: ${bq_dataset}
dataset_location: ${bq_dataset_location}
script_path: ${script_item}
body:
accounts: ${if(disable_strict_views, [], accounts_flatten)}
auth:
type: OIDC
result: create_view_response
- return_result:
return: ${accounts}
runBigQueryQueries:
params:
[
project,
function_location,
function_name,
gcs_bucket,
queries_path,
macros,
sqlParams,
dataset_location,
]
steps:
- get_bq_scripts_from_gcs:
call: googleapis.storage.v1.objects.list
args:
bucket: ${gcs_bucket}
prefix: ${queries_path}
result: bq_scripts_raw
# check if there are any bq scripts on GCS
- check_scripts:
switch:
- condition: ${map.get(bq_scripts_raw, "items") != null and len(map.get(bq_scripts_raw, "items")) > 0}
next: initialize_filtered_list
next: end
- initialize_filtered_list:
assign:
- bq_scripts: []
- filter_sql_files:
for:
value: bq_script_item
in: ${bq_scripts_raw.items}
steps:
- check_sql_extension:
switch:
- condition: ${text.match_regex(bq_script_item.name, "[.]sql$")}
assign:
- bq_scripts: ${list.concat(bq_scripts, bq_script_item.name)}
- log_bq_scripts:
call: sys.log
args:
data: ${bq_scripts}
severity: "INFO"
# check if there are any bq scripts on GCS
- check_scripts2:
switch:
- condition: ${len(bq_scripts) > 0}
next: get_function_bq
next: end
# get clound function's uri
- get_function_bq:
call: http.get
args:
url: ${"https://cloudfunctions.googleapis.com/v2/projects/" + project + "/locations/" + function_location + "/functions/" + function_name}
auth:
type: OAuth2
result: function_bq
# TODO: move to using CF adapter when it's ready (currently only v1 supported):
#call: googleapis.cloudfunctions.v2.projects.locations.functions.get
#args:
# name: ${"projects/" + project + "/locations/" + function_location + "/functions/" + function_name}
#result: function_bq
- runBqQueries:
for:
value: bq_script_item
in: ${bq_scripts}
steps:
- call_gaarf_bq_cf:
try:
call: http.post
args:
url: ${function_bq.body.serviceConfig.uri}
timeout: 1800
query:
script_path: ${"gs://" + gcs_bucket + "/" + bq_script_item}
project_id: ${project}
dataset_location: ${dataset_location}
body:
macro: ${macros}
sql: ${sqlParams}
auth:
type: OIDC
result: script_results
retry:
predicate: ${custom_retry_predicate}
max_retries: 3
backoff:
initial_delay: 2
max_delay: 60
multiplier: 2
- log_script_bq_result:
call: sys.log
args:
data:
script: ${bq_script_item}
result: ${script_results.body}
severity: "INFO"
custom_retry_predicate:
params: [e]
steps:
- log_call_gaarf_cf_failure:
call: sys.log
args:
data: ${e}
severity: "WARNING"
- normalize_fields:
assign:
- tags: ${default(map.get(e, "tags"), [])}
- code: ${default(map.get(e, "code"),0)}
- what_to_repeat:
switch:
# We'll repeat if it's a ConnectionError, TimeoutError or http statuses:
# 429 - Too Many Requests
# 502 - Bad Gateway
# 503 - Service Unavailable
# 504 - Gateway Timeout
# NOTE: sometime errors happen inside Workflow and there's no any code
# (i.e. "code" can be null, so DO NOT use operand ==,<,>,>=,<= without wrapping with `default`
- condition: ${"ConnectionFailedError" in tags or "ConnectionError" in tags or "TimeoutError" in tags or code == 429 or code == 502 or code == 503 or code == 504}
return: true
- otherwise:
return: false