Cloud spanner client does not seem to be retrying broken streams
Documentation for snapshot transactions mentions:
If streaming a chunk fails due to a “resumable” error, Session.read() retries the StreamingRead API reqeust, passing the resume_token from the last partial result streamed.
This is the correct thing to do but the client actually does not seem to be doing this. I looked around a little bit in the code and I could find anyplace where it is retrying the read with the resume token. This is the place where the stream would throw an error if it broke: https://github.com/GoogleCloudPlatform/google-cloud-python/blob/9d9b6c0708ae6553458ad6107e5ad8efb23762e8/spanner/google/cloud/spanner/streamed.py#L132
I dont see any exception handling happening here and it looks like that the error would just be propagated back to the user.
@jonparrott Are we doing anything about "sensible defaults" for the initial and maximum arguments to google.api.core.retry.exponential_sleep_generator?
@tseaver the defaults to the high-level Retry are sensible. The expectation for gapic clients is that they'll populate those values based on the gapic client config json file. I haven't yet sent a PR for that code, but here's a gist from my prototype.
@lukesneeringer can help with the gapic config part.
One possibly valid approach in the near term is to use Retry's defaults until I'm done replacing gax, in which case using the gapic config should be trivial. WDYT, @lukesneeringer?
@jonparrott I missed seeing the Retry class, which looks to have been added after #3819 (update, added in #3835).
Also, I'm unsure how GAPIC config fits in with retries for the streaming-result-set iteration, where code in this library is responsible for tracking / passing back the resume_token to "restart" the result set after a transient failure.
Also, I'm unsure how GAPIC config fits in with retries for the streaming-result-set iteration, where code in this library is responsible for tracking / passing back the resume_token to "restart" the result set after a transient failure.
Yeah in general I wouldn't sweat that for your purposes here- sorry to mislead you on that (it's monday..)
Basically, two levels of retries will be involved here: the "iterator" retry and the "method" retry. e.g.: calling next(iter) will catch errors and and retry calling the method to re-establish the stream. The method itself is wrapped with its own retry.
It's turtles all the way down.
I don't think you need to work about the gapic config for the iterator retry- you can just use the default retry or customize as you feel is appropriate.
So I need to know what are the"transient" errors which may be propagated from the "iterator" retry, and which the "method" (StreamedResultSet.consume_next) will need to trap in order to restart the API, passing the previously-captured resume_token. I'm also unclear whether there is supposed to be any backoff / jitter assocated with the "restart" (vs. invisible-to-me retries of the "iterator" API calls).
Per @blowmage: the Ruby implementation appears to only do the "restart" for UNAVAILABLE.
Yes you should try only for UNAVAILABLE with exponential backoff.
On Mon, Aug 28, 2017 at 9:57 AM, Tres Seaver [email protected] wrote:
Per @blowmage https://github.com/blowmage: the Ruby implementation https://github.com/GoogleCloudPlatform/google-cloud-ruby/blob/master/google-cloud-spanner/lib/google/cloud/spanner/results.rb#L143 appears to only do the "restart" for UNAVAILABLE.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/GoogleCloudPlatform/google-cloud-python/issues/3775#issuecomment-325411015, or mute the thread https://github.com/notifications/unsubscribe-auth/ATdef5aCSTCk6do3cbvM_cAhx4zqEl6Xks5scvFjgaJpZM4OyakU .
@jonparrott the "stock" bits in google.api.core.retry don't really fit this case: I need to inject a "fixup" when the appropriate exception (UNAVAILABLE) is raised, before retrying the stream iteration. Something like:
def consume_next(self):
"""Consume the next partial result set from the stream.
Parse the result set into new/existing rows in :attr:`_rows`
"""
sleep_generator = retry.exponential_sleep_generator(
initial=retry._DEFAULT_INITIAL_DELAY,
maximum=retry._DEFAULT_MAXIMUM_DELAY)
for sleep in sleep_generator:
try:
response = six.next(self._response_iterator)
except exceptions.ServiceUnavailable:
if self._resume_token in (None, b''):
raise
self._response_iterator = self._retry(
resume_token=self._resume_token)
else:
break
@tseaver that seems pretty reasonable. (it's one of the reasons I made exponential_sleep_generator public)
You should set a deadline of some sort, though.
Does the Python client allow users to specify deadline? If it does then the same deadline should be set adjusting for the time time elapsed so far.
On Mon, Aug 28, 2017 at 10:43 AM, Jon Wayne Parrott < [email protected]> wrote:
@tseaver https://github.com/tseaver that seems pretty reasonable. (it's one of the reasons I made exponential_sleep_generator public)
You should set a deadline of some sort, though.
— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/GoogleCloudPlatform/google-cloud-python/issues/3775#issuecomment-325423984, or mute the thread https://github.com/notifications/unsubscribe-auth/ATdef1-qD59qWEW1G7iw2pW8ElDJyjstks5scvxNgaJpZM4OyakU .
And of course I have to add my own deadline handling.
BTW, why does google.api.core.retry.retry_target use the more convoluted datetime stuff to compute the deadline, rather than just time.time plus a constant number of seconds?
BTW, why does google.api.core.retry.retry_target use the more convoluted datetime stuff to compute the deadline, rather than just time.time plus a constant number of seconds?
No particular reason other than being functionally equivalent and having on single source of truth for time in tests (datetime_helpers.utcnow()).
And of course I have to add my own deadline handling.
How so?
@jonparrott:
And of course I have to add my own deadline handling.
How so?
As you pointed out above, I'm doing the low level work myself.
ISTM that the retry_target (and therefore Retry) could grow some feature to signal a "fixup" to happen between the retries, to accommodate this kind of stateful behavior.
I'm still not sure what you mean? Just pass the user's deadline into retry_target?
I can't use retry_target: it doesn't let me issue the "restart" request and capture the new iterator. And so I need to do my own deadline handling.
Thanks for the PR - I think I see now, so it seems like if you did this:
retry_target(functools.partial(six.next, self._response_iterator))
It wouldn't let you catch the error and replace self._response_iterator.
I could imagine a hack using the predicate and some getattr magic, but that sounds gross to me.
Is there a minimal change you could make to retry_target to make it work for you- perhaps an optional on_exception callback? If so, I'm happy to review and merge that.
Also - I've thought about a retry_iterator function - maybe it makes more sense to just go ahead and add that to core?
Woo hoo! Thanks @tseaver!