-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Datastore: add retry param to page iterator. #8547
Changes from all commits
225fa0d
095d12c
e7cbd0a
425e731
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
""" | ||
|
||
import datetime | ||
import functools | ||
import itertools | ||
|
||
from google.protobuf import struct_pb2 | ||
|
@@ -471,6 +472,12 @@ def _set_protobuf_value(value_pb, val): | |
setattr(value_pb, attr, val) | ||
|
||
|
||
def _call_api(fnc_call, retry, *args, **kwargs): | ||
if retry: | ||
return retry(functools.partial(fnc_call, *args, **kwargs))() | ||
return fnc_call(*args, **kwargs) | ||
|
||
|
||
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. Please add explicit unit tests for |
||
class GeoPoint(object): | ||
"""Simple container for a geo point value. | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Copyright 2019 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 | ||
# | ||
# http://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. | ||
|
||
from google.api_core import exceptions | ||
from google.api_core import retry | ||
|
||
|
||
_RETRYABLE_REASONS = frozenset(["DEADLINE_EXCEEDED", "UNAVAILABLE"]) | ||
|
||
_UNSTRUCTURED_RETRYABLE_TYPES = ( | ||
exceptions.TooManyRequests, | ||
exceptions.InternalServerError, | ||
) | ||
|
||
|
||
def _should_retry(exc): | ||
"""Predicate for determining when to retry. | ||
|
||
We retry if and only if the 'error status' is 'DEADLINE_EXCEEDED' | ||
or 'UNAVAILABLE'. | ||
""" | ||
if hasattr(exc, "errors"): | ||
# Check for unstructured error returns | ||
return isinstance(exc, _UNSTRUCTURED_RETRYABLE_TYPES) | ||
|
||
if hasattr(exc, "error"): | ||
reason = exc.error["status"] | ||
return reason in _RETRYABLE_REASONS | ||
return False | ||
|
||
|
||
DEFAULT_RETRY = retry.Retry(predicate=_should_retry) | ||
"""The default retry object. | ||
|
||
Any method with a ``retry`` parameter will be retried automatically, | ||
with reasonable defaults. To disable retry, pass ``retry=None``. | ||
To modify the default retry behavior, call a ``with_XXX`` method | ||
on ``DEFAULT_RETRY``. For example, to change the deadline to 30 seconds, | ||
pass ``retry=datastore.DEFAULT_RETRY.with_deadline(30)``. | ||
""" | ||
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. Just copying this file in wholesale from bigquery isn't appropriate: we should be refactoring to share this implementation. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# Copyright 2019 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 | ||
# | ||
# http://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. | ||
|
||
import unittest | ||
|
||
import mock | ||
|
||
|
||
class Test_should_retry(unittest.TestCase): | ||
def _call_fut(self, exc): | ||
from google.cloud.datastore.retry import _should_retry | ||
|
||
return _should_retry(exc) | ||
|
||
def test_wo_errors_attribute(self): | ||
self.assertFalse(self._call_fut(object())) | ||
|
||
def test_w_empty_errors(self): | ||
exc = mock.Mock(errors=[], spec=["errors"]) | ||
self.assertFalse(self._call_fut(exc)) | ||
|
||
def test_w_non_matching_reason(self): | ||
exc = mock.Mock(error={"status": "bogus"}, spec=["error"]) | ||
self.assertFalse(self._call_fut(exc)) | ||
|
||
def test_w_DEADLINE_EXCEEDED(self): | ||
exc = mock.Mock(error={"status": "DEADLINE_EXCEEDED"}, spec=["error"]) | ||
self.assertTrue(self._call_fut(exc)) | ||
|
||
def test_w_UNAVAILABLE(self): | ||
exc = mock.Mock(error={"status": "UNAVAILABLE"}, spec=["error"]) | ||
self.assertTrue(self._call_fut(exc)) | ||
|
||
def test_w_unstructured_too_many_requests(self): | ||
from google.api_core.exceptions import TooManyRequests | ||
|
||
exc = TooManyRequests("testing") | ||
self.assertTrue(self._call_fut(exc)) | ||
|
||
def test_w_unstructured_internal_server_error(self): | ||
from google.api_core.exceptions import InternalServerError | ||
|
||
exc = InternalServerError("testing") | ||
self.assertTrue(self._call_fut(exc)) |
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.
If
retry
is None, then avoide the partial and justreturn fnc_call(*args, **kwargs)
.