Skip to content

Commit e646bc3

Browse files
authored
use the script when validating the BQ table (#34617)
1 parent 76708d5 commit e646bc3

File tree

2 files changed

+126
-96
lines changed

2 files changed

+126
-96
lines changed

.github/workflows/run_rc_validation_python_mobile_gaming.yml

Lines changed: 9 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -86,89 +86,6 @@ jobs:
8686
GAME_STATS_WINDOW_DURATION: 20
8787
SUBMISSION_TIMEOUT_SECONDS: 120 # Timeout for the python submission script itself
8888
# --- Define the validation function with enhanced debugging (FIXED QUOTING) ---
89-
VALIDATE_TABLE_FUNC: |
90-
validate_table() {
91-
local table_name=$1
92-
echo "DEBUG: ===== Starting validate_table for table: $table_name ====="
93-
# Ensure required env vars are set (GCP_PROJECT_ID, BQ_DATASET are inherited)
94-
if [[ -z "$GCP_PROJECT_ID" || -z "$BQ_DATASET" ]]; then
95-
echo "ERROR: GCP_PROJECT_ID and BQ_DATASET must be set in the environment."
96-
exit 1
97-
fi
98-
99-
local full_table_id="${GCP_PROJECT_ID}.${BQ_DATASET}.${table_name}"
100-
local full_table_id_show="${GCP_PROJECT_ID}:${BQ_DATASET}.${table_name}"
101-
local count=""
102-
local exit_code=1
103-
local retries=10
104-
local delay=60 # Default seconds between retries
105-
106-
# Allow overriding delay via second argument (optional)
107-
if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then
108-
delay=$2
109-
echo "DEBUG: Using custom retry delay: ${delay}s for table ${table_name}"
110-
else
111-
echo "DEBUG: Using default retry delay: ${delay}s for table ${table_name}"
112-
fi
113-
echo "DEBUG: Full table ID: ${full_table_id}, Max retries: ${retries}"
114-
115-
for i in $(seq 1 $retries); do
116-
echo "DEBUG: Starting attempt $i/$retries..."
117-
local query_output
118-
119-
echo "DEBUG: Executing: bq query --project_id=${GCP_PROJECT_ID} --use_legacy_sql=false --format=sparse --max_rows=1 \"SELECT COUNT(*) FROM \`${full_table_id}\`\""
120-
query_output=$(bq query --project_id=${GCP_PROJECT_ID} \
121-
--use_legacy_sql=false \
122-
--format=sparse \
123-
--max_rows=1 \
124-
"SELECT COUNT(*) FROM \`${full_table_id}\`" 2>&1)
125-
exit_code=$?
126-
127-
echo "DEBUG: bq query exit code: $exit_code"
128-
echo "DEBUG: bq query raw output: [$query_output]"
129-
130-
if [ $exit_code -eq 0 ]; then
131-
echo "DEBUG: bq query exited successfully (code 0)."
132-
count=$(echo "$query_output" | tail -n 1 | tr -d '[:space:]')
133-
echo "DEBUG: Processed count after removing whitespace (from last line): [$count]"
134-
if [[ "$count" =~ ^[0-9]+$ ]] && [ "$count" -gt 0 ]; then
135-
echo "DEBUG: Count [$count] is a positive integer. Validation successful for this attempt."
136-
break # Success! Found non-zero rows
137-
else
138-
echo "DEBUG: Count [$count] is zero or not a positive integer."
139-
if [[ "$count" == "0" ]]; then
140-
echo "DEBUG: Explicit count of 0 received."
141-
fi
142-
fi
143-
else
144-
echo "DEBUG: bq query failed (exit code: $exit_code)."
145-
echo "DEBUG: Checking table existence with bq show..."
146-
if ! bq show --project_id=${GCP_PROJECT_ID} "${full_table_id_show}" > /dev/null 2>&1; then
147-
echo "DEBUG: Table ${full_table_id_show} appears not to exist (bq show failed)."
148-
else
149-
echo "DEBUG: Table ${full_table_id_show} appears to exist (bq show succeeded), but query failed."
150-
fi
151-
fi
152-
153-
if [ $i -lt $retries ]; then
154-
echo "DEBUG: Validation condition not met on attempt $i. Retrying in $delay seconds..."
155-
sleep $delay
156-
else
157-
echo "DEBUG: Final attempt ($i) failed."
158-
fi
159-
done
160-
161-
echo "DEBUG: ===== Final validation check for table: $table_name ====="
162-
if [[ "$count" =~ ^[0-9]+$ ]] && [ "$count" -gt 0 ]; then
163-
echo "SUCCESS: Table ${table_name} has ${count} rows. Final validation OK."
164-
echo "DEBUG: validate_table returning 0 (success)."
165-
return 0 # Indicate success
166-
else
167-
echo "ERROR: Failed to get a non-zero row count for table ${table_name} after $retries retries (Last exit code: $exit_code, Last processed count: '$count')."
168-
echo "DEBUG: validate_table returning 1 (failure)."
169-
return 1 # Indicate failure
170-
fi
171-
}
17289

17390
steps:
17491
- name: Checkout code at RC tag
@@ -347,11 +264,10 @@ jobs:
347264
- name: Validate Leaderboard Results (Direct Runner)
348265
run: |
349266
source beam_env/bin/activate
350-
eval "$VALIDATE_TABLE_FUNC"
351267
echo "Validating BigQuery results for Leaderboard (DirectRunner)..."
352268
sleep 90
353-
validate_table "leader_board_users" || exit 1
354-
validate_table "leader_board_teams" || exit 1
269+
./scripts/tools/validate_table.sh "leader_board_users" || exit 1
270+
./scripts/tools/validate_table.sh "leader_board_teams" || exit 1
355271
echo "Leaderboard (Direct Runner) BQ validation finished successfully."
356272
shell: bash
357273

@@ -426,11 +342,10 @@ jobs:
426342
exit 0 # Exit step successfully to allow cancellation/cleanup
427343
fi
428344
source beam_env/bin/activate
429-
eval "$VALIDATE_TABLE_FUNC"
430345
echo "Validating BigQuery results for Leaderboard (DataflowRunner)..."
431346
sleep 240
432-
validate_table "leader_board_users" 15 || exit 1 # Use 15s retry delay
433-
validate_table "leader_board_teams" 15 || exit 1 # Use 15s retry delay
347+
./scripts/tools/validate_table.sh "leader_board_users" 15 || exit 1 # Use 15s retry delay
348+
./scripts/tools/validate_table.sh "leader_board_teams" 15 || exit 1 # Use 15s retry delay
434349
echo "Leaderboard (Dataflow Runner) BQ validation finished successfully."
435350
shell: bash
436351

@@ -451,7 +366,7 @@ jobs:
451366
echo "leaderboard_dataflow_jobid.txt not found, cannot cancel job (it might have failed before ID extraction)."
452367
fi
453368
shell: bash
454-
369+
455370
# ================== GameStats Tests ==================
456371
- name: Run GameStats (Direct Runner) in Background
457372
run: |
@@ -471,12 +386,11 @@ jobs:
471386
- name: Validate GameStats Results (Direct Runner)
472387
run: |
473388
source beam_env/bin/activate
474-
eval "$VALIDATE_TABLE_FUNC"
475389
echo "Validating BigQuery results for GameStats (DirectRunner)..."
476390
echo "* Sleeping for 25mins"
477391
sleep 25m
478-
validate_table "game_stats_teams" || exit 1
479-
validate_table "game_stats_sessions" || exit 1
392+
./scripts/tools/validate_table.sh "game_stats_teams" || exit 1
393+
./scripts/tools/validate_table.sh "game_stats_sessions" || exit 1
480394
echo "GameStats (Direct Runner) BQ validation finished successfully."
481395
shell: bash
482396

@@ -552,12 +466,11 @@ jobs:
552466
exit 0 # Exit step successfully to allow cleanup
553467
fi
554468
source beam_env/bin/activate
555-
eval "$VALIDATE_TABLE_FUNC"
556469
echo "Validating BigQuery results for GameStats (DataflowRunner)..."
557470
echo "* Sleeping for 25mins"
558471
sleep 25m
559-
validate_table "game_stats_teams" 15 || exit 1 # Use 15s retry delay
560-
validate_table "game_stats_sessions" 15 || exit 1 # Use 15s retry delay
472+
./scripts/tools/validate_table.sh "game_stats_teams" 15 || exit 1 # Use 15s retry delay
473+
./scripts/tools/validate_table.sh "game_stats_sessions" 15 || exit 1 # Use 15s retry delay
561474
echo "GameStats (Dataflow Runner) BQ validation finished successfully."
562475
shell: bash
563476

scripts/tools/validate_table.sh

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
#!/bin/bash
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
19+
# Function to validate if a BigQuery table exists and has rows.
20+
# Usage: validate_table <table_name> [retry_delay_seconds]
21+
# Exits with 0 if validation succeeds, 1 otherwise.
22+
# Requires GCP_PROJECT_ID and BQ_DATASET to be set in the environment.
23+
24+
validate_table() {
25+
local table_name=$1
26+
echo "DEBUG: ===== Starting validate_table for table: $table_name ====="
27+
# Ensure required env vars are set (GCP_PROJECT_ID, BQ_DATASET are inherited)
28+
if [[ -z "$GCP_PROJECT_ID" || -z "$BQ_DATASET" ]]; then
29+
echo "ERROR: GCP_PROJECT_ID and BQ_DATASET must be set in the environment."
30+
exit 1 # Exit script if env vars missing
31+
fi
32+
33+
local full_table_id="${GCP_PROJECT_ID}.${BQ_DATASET}.${table_name}"
34+
local full_table_id_show="${GCP_PROJECT_ID}:${BQ_DATASET}.${table_name}"
35+
local count=""
36+
local exit_code=1
37+
local retries=10
38+
local delay=60 # Default seconds between retries
39+
40+
# Allow overriding delay via second argument (optional)
41+
if [[ -n "$2" && "$2" =~ ^[0-9]+$ ]]; then
42+
delay=$2
43+
echo "DEBUG: Using custom retry delay: ${delay}s for table ${table_name}"
44+
else
45+
echo "DEBUG: Using default retry delay: ${delay}s for table ${table_name}"
46+
fi
47+
echo "DEBUG: Full table ID: ${full_table_id}, Max retries: ${retries}"
48+
49+
for i in $(seq 1 $retries); do
50+
echo "DEBUG: Starting attempt $i/$retries..."
51+
local query_output
52+
53+
echo "DEBUG: Executing: bq query --project_id=${GCP_PROJECT_ID} --use_legacy_sql=false --format=sparse --max_rows=1 \"SELECT COUNT(*) FROM \`${full_table_id}\`\""
54+
query_output=$(bq query --project_id=${GCP_PROJECT_ID} \
55+
--use_legacy_sql=false \
56+
--format=sparse \
57+
--max_rows=1 \
58+
"SELECT COUNT(*) FROM \`${full_table_id}\`" 2>&1)
59+
exit_code=$?
60+
61+
echo "DEBUG: bq query exit code: $exit_code"
62+
echo "DEBUG: bq query raw output: [$query_output]"
63+
64+
if [ $exit_code -eq 0 ]; then
65+
echo "DEBUG: bq query exited successfully (code 0)."
66+
count=$(echo "$query_output" | tail -n 1 | tr -d '[:space:]')
67+
echo "DEBUG: Processed count after removing whitespace (from last line): [$count]"
68+
if [[ "$count" =~ ^[0-9]+$ ]] && [ "$count" -gt 0 ]; then
69+
echo "DEBUG: Count [$count] is a positive integer. Validation successful for this attempt."
70+
break # Success! Found non-zero rows
71+
else
72+
echo "DEBUG: Count [$count] is zero or not a positive integer."
73+
if [[ "$count" == "0" ]]; then
74+
echo "DEBUG: Explicit count of 0 received."
75+
fi
76+
fi
77+
else
78+
echo "DEBUG: bq query failed (exit code: $exit_code)."
79+
echo "DEBUG: Checking table existence with bq show..."
80+
if ! bq show --project_id=${GCP_PROJECT_ID} "${full_table_id_show}" > /dev/null 2>&1; then
81+
echo "DEBUG: Table ${full_table_id_show} appears not to exist (bq show failed)."
82+
else
83+
echo "DEBUG: Table ${full_table_id_show} appears to exist (bq show succeeded), but query failed."
84+
fi
85+
fi
86+
87+
if [ $i -lt $retries ]; then
88+
echo "DEBUG: Validation condition not met on attempt $i. Retrying in $delay seconds..."
89+
sleep $delay
90+
else
91+
echo "DEBUG: Final attempt ($i) failed."
92+
fi
93+
done
94+
95+
echo "DEBUG: ===== Final validation check for table: $table_name ====="
96+
if [[ "$count" =~ ^[0-9]+$ ]] && [ "$count" -gt 0 ]; then
97+
echo "SUCCESS: Table ${table_name} has ${count} rows. Final validation OK."
98+
echo "DEBUG: validate_table returning 0 (success)."
99+
return 0 # Indicate success
100+
else
101+
echo "ERROR: Failed to get a non-zero row count for table ${table_name} after $retries retries (Last exit code: $exit_code, Last processed count: '$count')."
102+
echo "DEBUG: validate_table returning 1 (failure)."
103+
return 1 # Indicate failure
104+
fi
105+
}
106+
107+
# Allow the script to be sourced using "source ./script.sh"
108+
# and then call the function directly: "validate_table my_table 30"
109+
# If the script is executed directly, check if arguments are provided and call the function.
110+
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
111+
if [[ $# -eq 0 ]]; then
112+
echo "Usage: $0 <table_name> [retry_delay_seconds]"
113+
echo "Requires GCP_PROJECT_ID and BQ_DATASET env vars."
114+
exit 1
115+
fi
116+
validate_table "$@"
117+
fi

0 commit comments

Comments
 (0)