Skip to content

Commit 6878eb6

Browse files
authored
fix: adding workaround for cloud workflows issue (#310)
1 parent df68cf2 commit 6878eb6

File tree

9 files changed

+251
-88
lines changed

9 files changed

+251
-88
lines changed

modules/data_warehouse/bigquery.tf

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ resource "google_project_iam_member" "bq_connection_iam_object_viewer" {
4444
role = "roles/storage.objectViewer"
4545
member = "serviceAccount:${google_bigquery_connection.ds_connection.cloud_resource[0].service_account_id}"
4646

47-
depends_on = [google_storage_bucket.raw_bucket, google_bigquery_connection.ds_connection]
47+
depends_on = [google_project_iam_member.workflow_manage_sa_roles, google_bigquery_connection.ds_connection]
4848
}
4949

5050
## Create a BigQuery connection for Vertex AI to support GenerativeAI use cases
@@ -372,6 +372,6 @@ resource "google_bigquery_data_transfer_config" "dts_config" {
372372
depends_on = [
373373
google_project_iam_member.dts_roles,
374374
google_bigquery_dataset.ds_edw,
375-
time_sleep.complete_workflow
375+
module.workflow_polling_4
376376
]
377377
}

modules/data_warehouse/metadata.yaml

+2-9
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,22 @@ spec:
3232
description: {}
3333
icon: assets/data_warehouse_icon_v1.png
3434
deploymentDuration:
35-
configurationSecs: 120
36-
deploymentSecs: 420
35+
configurationSecs: "120"
36+
deploymentSecs: "420"
3737
costEstimate:
3838
description: cost of this solution is $0.65
3939
url: https://cloud.google.com/products/calculator/#id=857776c6-49e8-4c6a-adc5-42a15b8fb67d
4040
cloudProducts:
4141
- productId: search_BIGQUERY_SECTION
42-
pageUrl: ""
4342
- productId: WORKFLOWS_SECTION
44-
pageUrl: ""
4543
- productId: STORAGE_SECTION
46-
pageUrl: ""
4744
- productId: ai-platform
48-
pageUrl: ""
4945
- productId: LOOKER_STUDIO_SECTION
5046
pageUrl: lookerstudio.google.com
5147
isExternal: true
5248
- productId: CLOUD_DMS_SECTION
53-
pageUrl: ""
5449
- productId: FUNCTIONS_SECTION
55-
pageUrl: ""
5650
- productId: DATAFORM_SECTION
57-
pageUrl: ""
5851
content:
5952
architecture:
6053
diagramUrl: www.gstatic.com/pantheon/images/solutions/data-warehouse-architecture_v6.svg

modules/data_warehouse/notebook_deployment.tf

+6-1
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,12 @@ resource "google_dataform_repository_iam_member" "workflow_manage_repo" {
155155
count = length(local.notebook_names)
156156
repository = local.notebook_names[count.index]
157157

158-
depends_on = [google_service_account_iam_member.workflow_auth_function, google_dataform_repository_iam_member.function_manage_repo, google_dataform_repository.notebook_repo]
158+
depends_on = [
159+
google_project_iam_member.workflow_manage_sa_roles,
160+
google_service_account_iam_member.workflow_auth_function,
161+
google_dataform_repository_iam_member.function_manage_repo,
162+
google_dataform_repository.notebook_repo
163+
]
159164
}
160165

161166
# Create and deploy a Cloud Function to deploy notebooks

modules/data_warehouse/src/sql/sp_sample_queries.sql

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ SELECT
4747
GROUP BY
4848
1, 2
4949
ORDER BY
50-
WeekdayNumber, 3
50+
WeekdayNumber
5151
;
5252

5353
-- Query: Items with less than 30 days of inventory remaining
@@ -58,7 +58,7 @@ WITH Orders AS (
5858
FROM
5959
`${project_id}.${dataset_id}.order_items` AS order_items
6060
WHERE
61-
order_items.created_at > TIMESTAMP_SUB(CURRENT_TIMESTAMP(), INTERVAL 30 DAY)
61+
order_items.created_at > TIMESTAMP_SUB(TIMESTAMP("2023-09-26 00:00:00"), INTERVAL 30 DAY)
6262
GROUP BY
6363
product_id
6464
),
@@ -99,7 +99,7 @@ SELECT
9999
FROM
100100
End30dInventory
101101
ORDER BY
102-
rank ASC, current_stock DESC
102+
rank ASC, count_sold_30d DESC, current_stock DESC
103103
;
104104

105105
-- Query: data summed by month, then pivoted by department

modules/data_warehouse/versions.tf

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,9 @@ terraform {
2424
source = "hashicorp/google-beta"
2525
version = ">= 4.52, < 6"
2626
}
27-
random = {
28-
source = "hashicorp/random"
29-
version = ">= 2"
30-
}
3127
archive = {
3228
source = "hashicorp/archive"
33-
version = ">= 2"
29+
version = "2.4.2"
3430
}
3531
time = {
3632
source = "hashicorp/time"
@@ -44,6 +40,10 @@ terraform {
4440
source = "hashicorp/local"
4541
version = ">=2.4"
4642
}
43+
random = {
44+
source = "hashicorp/random"
45+
version = "3.6.0"
46+
}
4747
}
4848
required_version = ">= 0.13"
4949

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/**
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
data "google_client_config" "current" {
18+
}
19+
20+
locals {
21+
is_active = var.input_workflow_state == null || var.input_workflow_state == "FAILED" ? "no" : "yes"
22+
}
23+
24+
## Trigger the execution of the setup workflow with an API call
25+
data "http" "call_workflows_setup" {
26+
url = "https://workflowexecutions.googleapis.com/v1/${var.workflow_id}/executions"
27+
method = local.is_active == "no" ? "POST" : "GET"
28+
# method = "POST"
29+
request_headers = {
30+
Accept = "application/json"
31+
Authorization = "Bearer ${data.google_client_config.current.access_token}" }
32+
}
33+
34+
resource "time_sleep" "workflow_execution_wait" {
35+
create_duration = "120s"
36+
depends_on = [
37+
data.http.call_workflows_setup
38+
]
39+
}
40+
41+
# locals {
42+
# is_active = var.input_workflow_state == null || var.input_workflow_state == "FAILED" ? "no" : "yes"
43+
# }
44+
45+
# ## Trigger the execution of the setup workflow with an API call
46+
# data "http" "call_workflows_setup" {
47+
# url = "https://workflowexecutions.googleapis.com/v1/${var.workflow_id}/executions"
48+
# method = local.is_active == "no" ? "POST" : "GET"
49+
# request_headers = {
50+
# Accept = "application/json"
51+
# Authorization = "Bearer ${data.google_client_config.current.access_token}" }
52+
# }
53+
54+
# ## If the workflow last failed, sleep for 30 seconds before checking the workflow execution status.
55+
# ## If last execution did not fail, exit as quickly as possible (1 second)
56+
# resource "time_sleep" "workflow_execution_wait" {
57+
# create_duration = local.is_active == "no" ? "30s" : "1s"
58+
# depends_on = [
59+
# data.http.call_workflows_setup,
60+
# ]
61+
# }
62+
63+
## Check the state of the setup workflow execution with an API call
64+
data "http" "call_workflows_state" {
65+
url = "https://workflowexecutions.googleapis.com/v1/${var.workflow_id}/executions"
66+
method = "GET"
67+
request_headers = {
68+
Accept = "application/json"
69+
Authorization = "Bearer ${data.google_client_config.current.access_token}" }
70+
depends_on = [
71+
data.http.call_workflows_setup,
72+
time_sleep.workflow_execution_wait
73+
]
74+
}
75+
76+
## Parse out the workflow execution state from the API call response
77+
locals {
78+
response_body = jsondecode(data.http.call_workflows_state.response_body)
79+
workflow_state = local.response_body.executions[0].state
80+
}
81+
82+
## Output the workflow state to use as input for subsequent invocations
83+
output "workflow_state" {
84+
description = "State of the most recent workflow execution. Used to determine how to proceed with next polling run."
85+
value = local.workflow_state
86+
}
87+
88+
# ## If workflow execution is actively running, sleep for 90 seconds to allow it to finish
89+
# ## If not, exit as quickly as possible (1 second)
90+
# resource "time_sleep" "complete_workflow" {
91+
# create_duration = local.workflow_state == "ACTIVE" ? "90s" : "1s"
92+
# depends_on = [
93+
# data.http.call_workflows_setup,
94+
# time_sleep.workflow_execution_wait,
95+
# data.http.call_workflows_state,
96+
# local.workflow_state
97+
# ]
98+
# }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
variable "input_workflow_state" {
18+
type = string
19+
description = "Name of the BigQuery ML GenAI remote model that connects to the LLM used for text generation"
20+
}
21+
22+
variable "workflow_id" {
23+
type = string
24+
description = "The identifer of a workflow created by Terraform. Format is projects/{project ID}/locations/{region}/workflows/{workflow name}"
25+
}
26+
27+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
terraform {
18+
required_providers {
19+
google = {
20+
source = "hashicorp/google"
21+
version = ">= 4.52, < 6"
22+
}
23+
google-beta = {
24+
source = "hashicorp/google-beta"
25+
version = ">= 4.52, < 6"
26+
}
27+
archive = {
28+
source = "hashicorp/archive"
29+
version = ">= 2"
30+
}
31+
random = {
32+
source = "hashicorp/random"
33+
version = ">= 2"
34+
}
35+
time = {
36+
source = "hashicorp/time"
37+
version = ">= 0.9.1"
38+
}
39+
http = {
40+
source = "hashicorp/http"
41+
version = ">= 2"
42+
}
43+
local = {
44+
source = "hashicorp/local"
45+
version = ">=2.4"
46+
}
47+
}
48+
required_version = ">= 0.13"
49+
50+
provider_meta "google" {
51+
module_name = "blueprints/terraform/terraform-google-bigquery:data_warehouse/v7.0.0"
52+
}
53+
}

0 commit comments

Comments
 (0)