-
Notifications
You must be signed in to change notification settings - Fork 137
feat(bigquery): heap-box futures and builder in JobPoller to prevent stack overflow #6237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
haphungw
wants to merge
2
commits into
googleapis:main
Choose a base branch
from
haphungw:fix-job-poller-stack-overflow
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -104,14 +104,14 @@ pub enum JobPollerError { | |||||
| #[derive(Debug)] | ||||||
| pub struct JobPoller { | ||||||
| policy: JobRetryPolicy, | ||||||
| builder: InsertJob, | ||||||
| builder: Box<InsertJob>, | ||||||
| } | ||||||
|
|
||||||
| impl JobPoller { | ||||||
| pub(crate) fn new(builder: InsertJob) -> Self { | ||||||
| Self { | ||||||
| policy: JobRetryPolicy::default(), | ||||||
| builder, | ||||||
| builder: Box::new(builder), | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -128,38 +128,44 @@ impl JobPoller { | |||||
| } | ||||||
|
|
||||||
| /// 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 job_result = builder.clone().poller().until_done().await?; | ||||||
| attempts += 1; | ||||||
|
|
||||||
| if let Some(status) = &job_result.status | ||||||
| && let Some(err) = &status.error_result | ||||||
| { | ||||||
| if is_retryable_job_error(&err.reason) | ||||||
| && attempts < self.policy.job_level_attempt_limit | ||||||
| pub fn until_done( | ||||||
| self, | ||||||
| ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<Job, JobPollerError>> + Send>> | ||||||
| { | ||||||
| Box::pin(async move { | ||||||
| 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 = Box::new(builder.clone().poller()); | ||||||
| let job_result = poller.until_done().await?; | ||||||
| attempts += 1; | ||||||
|
|
||||||
| if let Some(status) = &job_result.status | ||||||
| && let Some(err) = &status.error_result | ||||||
| { | ||||||
| let retry_job = prepare_job_for_retry(job_result); | ||||||
| builder = builder.set_job(retry_job); | ||||||
|
|
||||||
| 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; | ||||||
| continue; | ||||||
| if is_retryable_job_error(&err.reason) | ||||||
| && attempts < self.policy.job_level_attempt_limit | ||||||
| { | ||||||
| let retry_job = prepare_job_for_retry(job_result); | ||||||
| *builder = builder.set_job(retry_job); | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This assignment will likely cause a compilation error because calling
Suggested change
|
||||||
|
|
||||||
| 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; | ||||||
| continue; | ||||||
| } | ||||||
| return Err(JobPollerError::ErrorProto(err.clone())); | ||||||
| } | ||||||
| return Err(JobPollerError::ErrorProto(err.clone())); | ||||||
| return Ok(job_result); | ||||||
| } | ||||||
| return Ok(job_result); | ||||||
| } | ||||||
| }) | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why does the signature has to change that much ? isn't this gonna diverge from other LROs that are async and return just a Result ?
Maybe I don't understand Pin/Box enough to understand what is going on here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was struggling to get this test pass due to stack overflow
https://github.com/googleapis/google-cloud-rust/pull/6232/changes#diff-824e62dc84d6a981c4c62f1f254e61712feacc64d410a3a1b0744f981cb657edR267
https://pantheon.corp.google.com/cloud-build/builds;region=us-central1/6916363e-91b1-4147-81c4-2bf87a03abd7?project=rust-sdk-testing&e=SqlStudioMysqlLaunch::SqlStudioMysqlEnabled
The builder and polling loop state machine get pretty huge on the stack, so boxing it offloads that to the heap.
The
the .until_done().awaitcaller syntax is still the same, but it seems like I haven't been able to fix the issue.