google-cloud-bigquery-v2 1.0.0

Google Cloud Client Libraries for Rust - BigQuery API
Documentation
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
// Copyright 2026 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Helpers to await the completion of an [InsertJob] request

use crate::builder::job_service::InsertJob;
use crate::model::Job;
use google_cloud_gax::backoff_policy::BackoffPolicy;
use google_cloud_gax::error::Error as GaxError;
use google_cloud_gax::error::rpc::{Code, Status};
use google_cloud_gax::exponential_backoff::ExponentialBackoff;
use google_cloud_gax::retry_state::RetryState;
use google_cloud_lro::Poller;

impl google_cloud_lro::internal::DiscoveryOperation for Job {
    fn name(&self) -> Option<&String> {
        self.job_reference.as_ref().map(|r| &r.job_id)
    }

    fn done(&self) -> bool {
        self.status
            .as_ref()
            .map(|s| s.state == "DONE")
            .unwrap_or(false)
    }

    fn error(&self) -> Option<Status> {
        self.status.as_ref().and_then(|s| {
            s.error_result.as_ref().map(|e| {
                Status::default()
                    .set_code(Code::Unknown)
                    .set_message(e.message.clone())
            })
        })
    }
}

/// Determines if a BigQuery job failure reason is transient and eligible for
/// job-level retry.
///
/// Returns `true` for retryable reasons (`jobBackendError`,
/// `jobInternalError`, `jobRateLimitExceeded`, `tableUnavailable`) per
/// BigQuery error handling specification.
#[allow(dead_code)]
pub(crate) fn is_retryable_job_error(reason: &str) -> bool {
    matches!(
        reason,
        "jobBackendError" | "jobInternalError" | "jobRateLimitExceeded" | "tableUnavailable"
    )
}

/// Prepares a `Job` instance for retry by assigning a new synthetic job ID
/// and clearing existing execution status.
///
/// To preserve idempotency and avoid job execution collisions, each job-level
/// retry must use a unique job ID while retaining original reference details
/// (project ID, location) and configuration settings.
#[allow(dead_code)]
pub(crate) fn prepare_job_for_retry(mut job: Job) -> Job {
    job.job_reference.get_or_insert_default().job_id = uuid::Uuid::new_v4().to_string();
    job.status = None;
    job
}

/// Configuration policy for BigQuery job-level retries.
#[derive(Debug)]
pub(crate) struct JobRetryPolicy {
    /// Maximum number of general job-level attempts for retryable job errors.
    pub job_level_attempt_limit: u32,
    /// Backoff strategy between retry attempts.
    pub backoff: ExponentialBackoff,
}

impl Default for JobRetryPolicy {
    fn default() -> Self {
        Self {
            job_level_attempt_limit: 3,
            backoff: ExponentialBackoff::default(),
        }
    }
}

/// Errors returned by the JobPoller.
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum JobPollerError {
    /// An error occurred during the RPC or LRO polling.
    #[error(transparent)]
    Rpc(#[from] GaxError),
    /// The job completed, but the BigQuery service reported an internal error.
    #[error("BigQuery job failed ({}): {}", .error_result.reason, .error_result.message)]
    JobFailed {
        /// Final error result of the job.
        error_result: Box<crate::model::ErrorProto>,
        /// Errors and warnings encountered during the running of the job.
        errors: Vec<crate::model::ErrorProto>,
    },
}

/// A poller that monitors the status of an inserted BigQuery job and handles retries.
#[derive(Debug)]
pub struct JobPoller {
    policy: JobRetryPolicy,
    // Because the builder holds a `Job` which is >7kB, we ought to store it
    // on the heap.
    //
    // This is important across `await` points where stack variables
    // are captured in the async fn's state machine. This bloats the size of
    // the returned `Future`, which can potentially overflow the stack.
    //
    // See #6391.
    builder: Box<InsertJob>,
}

impl JobPoller {
    pub(crate) fn new(builder: InsertJob) -> Self {
        Self {
            policy: JobRetryPolicy::default(),
            builder: Box::new(builder),
        }
    }

    /// Sets the maximum number of job-level attempts.
    pub fn with_attempt_limit(mut self, limit: u32) -> Self {
        self.policy.job_level_attempt_limit = limit;
        self
    }

    /// Sets the exponential backoff policy for job-level retries.
    pub fn with_job_retry_backoff(mut self, backoff: ExponentialBackoff) -> Self {
        self.policy.backoff = backoff;
        self
    }

    /// Polls the job until it is done, returning the final Job status.
    pub async fn until_done(self) -> Result<Job, JobPollerError> {
        let mut attempts = 0_u32;
        let mut builder = self.builder;
        let backoff = self.policy.backoff;
        let start_time = std::time::Instant::now();

        loop {
            // NOTE: the client library intercepts errors and retries internally
            // according to the policies set on `builder`.
            {
                let poller = (*builder).clone().poller();
                let job = poller.until_done().await?;
                let Some(status) = &job.status else {
                    return Ok(job);
                };
                let Some(err) = &status.error_result else {
                    return Ok(job);
                };

                attempts += 1;
                if !is_retryable_job_error(&err.reason)
                    || attempts >= self.policy.job_level_attempt_limit
                {
                    return Err(JobPollerError::JobFailed {
                        error_result: Box::new(err.clone()),
                        errors: status.errors.clone(),
                    });
                }

                let job = prepare_job_for_retry(job);
                *builder = (*builder).set_job(job);

                // We use a block so that `job` (~7kB) is not allocated on the
                // stack across the sleep `await` point.
            }

            let retry_state = RetryState::new(true)
                .set_start(start_time)
                .set_attempt_count(attempts);
            let delay = backoff.on_failure(&retry_state);
            tokio::time::sleep(delay).await;
        }
    }
}

impl InsertJob {
    /// Returns a `JobPoller`, which can retry on [job-level errors].
    ///
    /// If the job fails with an internal error, the `JobPoller` will retry the
    /// `InsertJob` operation. Note that the client library will supply a
    /// synthetic job ID for any retries.
    ///
    /// ```no_run
    /// # async fn example(builder: google_cloud_bigquery_v2::builder::job_service::InsertJob) -> Result<(), Box<dyn std::error::Error>> {
    /// let job = builder.into_job_poller().until_done().await?;
    /// # Ok(())
    /// # }
    /// ```
    ///
    /// [job-level errors]: https://docs.cloud.google.com/bigquery/docs/error-messages#errortable
    pub fn into_job_poller(self) -> JobPoller {
        JobPoller::new(self)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::{
        ErrorProto, JobConfiguration, JobConfigurationQuery, JobReference, JobStatus,
    };
    use google_cloud_lro::internal::DiscoveryOperation;

    #[test]
    fn name_none() {
        let job = Job::default();
        assert_eq!(job.name(), None);
    }

    #[test]
    fn name_some() {
        let job = Job::new().set_job_reference(JobReference::new().set_job_id("test-id"));
        assert_eq!(job.name().map(|s| s.as_str()), Some("test-id"));
    }

    #[test]
    fn done_none() {
        let job = Job::default();
        assert!(!job.done());
    }

    #[test]
    fn done_false() {
        let job = Job::new().set_status(JobStatus::new().set_state("RUNNING"));
        assert!(!job.done());
    }

    #[test]
    fn done_true() {
        let job = Job::new().set_status(JobStatus::new().set_state("DONE"));
        assert!(job.done());
    }

    #[test]
    fn error_none() {
        let job = Job::default();
        assert!(job.error().is_none());

        let job_no_error = Job::new().set_status(JobStatus::new().set_state("DONE"));
        assert!(job_no_error.error().is_none());
    }

    #[test]
    fn error_some() {
        let job = Job::new()
            .set_status(JobStatus::new().set_error_result(ErrorProto::new().set_message("failed")));
        let err = job.error().expect("should have error");
        assert_eq!(err.code, Code::Unknown);
        assert_eq!(err.message, "failed");
    }

    #[test]
    fn retryable_job_errors() {
        assert!(is_retryable_job_error("jobBackendError"));
        assert!(is_retryable_job_error("jobInternalError"));
        assert!(is_retryable_job_error("jobRateLimitExceeded"));
        assert!(is_retryable_job_error("tableUnavailable"));

        assert!(!is_retryable_job_error("invalidQuery"));
        assert!(!is_retryable_job_error("accessDenied"));
        assert!(!is_retryable_job_error("notFound"));
        assert!(!is_retryable_job_error("backendError"));
        assert!(!is_retryable_job_error(""));
    }

    #[test]
    fn job_retry_policy_defaults() {
        let policy = JobRetryPolicy::default();
        assert_eq!(policy.job_level_attempt_limit, 3);
    }

    #[test]
    fn prepare_job_for_retry_generates_new_id_and_resets_status() {
        let original_job = Job::new()
            .set_job_reference(
                JobReference::new()
                    .set_project_id("test-project")
                    .set_job_id("original-job-id")
                    .set_location("US"),
            )
            .set_status(
                JobStatus::new().set_state("DONE").set_error_result(
                    ErrorProto::new()
                        .set_reason("jobBackendError")
                        .set_message("backend failed"),
                ),
            );

        let retried_job = prepare_job_for_retry(original_job);

        assert!(retried_job.status.is_none());

        let ref_data = retried_job
            .job_reference
            .expect("should have job reference");
        assert_eq!(ref_data.project_id, "test-project");
        assert_eq!(ref_data.location.as_deref(), Some("US"));
        assert_ne!(ref_data.job_id, "original-job-id");
        assert!(uuid::Uuid::parse_str(&ref_data.job_id).is_ok());
    }

    #[test]
    fn prepare_job_for_retry_handles_none_job_reference() {
        let original_job = Job::new().set_status(JobStatus::new().set_state("DONE"));

        let retried_job = prepare_job_for_retry(original_job);
        assert!(retried_job.status.is_none());

        let ref_data = retried_job
            .job_reference
            .expect("should create job reference when missing");
        assert!(uuid::Uuid::parse_str(&ref_data.job_id).is_ok());
    }

    #[test]
    fn prepare_job_for_retry_preserves_job_configuration_and_metadata() {
        let original_job = Job::new()
            .set_job_reference(
                JobReference::new()
                    .set_project_id("my-project")
                    .set_job_id("initial-id")
                    .set_location("EU"),
            )
            .set_configuration(
                JobConfiguration::new()
                    .set_query(JobConfigurationQuery::new().set_query("SELECT 42"))
                    .set_labels([("env".to_string(), "test".to_string())]),
            )
            .set_user_email("user@example.com")
            .set_status(
                JobStatus::new().set_state("DONE").set_error_result(
                    ErrorProto::new()
                        .set_reason("jobInternalError")
                        .set_message("internal error"),
                ),
            );

        let retried = prepare_job_for_retry(original_job);

        // Status must be reset to None for retry submission
        assert!(retried.status.is_none());

        // Configuration and user_email must be preserved
        assert_eq!(
            retried
                .configuration
                .as_ref()
                .and_then(|c| c.query.as_ref())
                .map(|q| q.query.as_str()),
            Some("SELECT 42")
        );
        assert_eq!(
            retried
                .configuration
                .as_ref()
                .and_then(|c| c.labels.get("env").map(|s| s.as_str())),
            Some("test")
        );
        assert_eq!(retried.user_email.as_str(), "user@example.com");

        // JobReference metadata preserved, but job_id replaced with a new valid UUID
        let ref_data = retried.job_reference.expect("must have reference");
        assert_eq!(ref_data.project_id, "my-project");
        assert_eq!(ref_data.location.as_deref(), Some("EU"));
        assert_ne!(ref_data.job_id, "initial-id");
        assert!(uuid::Uuid::parse_str(&ref_data.job_id).is_ok());
    }

    #[test]
    fn custom_retry_policy_builder() {
        let mut policy = JobRetryPolicy::default();
        assert_eq!(policy.job_level_attempt_limit, 3);

        policy.job_level_attempt_limit = 5;
        assert_eq!(policy.job_level_attempt_limit, 5);
    }

    #[test]
    fn job_poller_error_job_failed_display() {
        let err_proto = ErrorProto::new()
            .set_reason("invalidQuery")
            .set_message("syntax error");
        let sub_error = ErrorProto::new()
            .set_reason("invalid")
            .set_message("detailed error");

        let poller_err = JobPollerError::JobFailed {
            error_result: Box::new(err_proto),
            errors: vec![sub_error],
        };

        let JobPollerError::JobFailed {
            error_result,
            errors,
        } = &poller_err
        else {
            panic!("expected JobPollerError::JobFailed");
        };
        assert_eq!(error_result.reason, "invalidQuery");
        assert_eq!(errors.len(), 1);
        assert_eq!(errors[0].reason, "invalid");
        assert_eq!(
            poller_err.to_string(),
            "BigQuery job failed (invalidQuery): syntax error"
        );
    }
}