Skip to content

Commit

Permalink
Upgrading google-gax to 0.14.1.
Browse files Browse the repository at this point in the history
In the process, adding back code changes that were
reverted in the process of reverting PR googleapis#2336. Some of
the changes were necessary, but anything using GAX
(logging and pubsub) could safely be restored.
  • Loading branch information
dhermes committed Sep 22, 2016
1 parent f2ba165 commit 523100f
Show file tree
Hide file tree
Showing 15 changed files with 42 additions and 82 deletions.
19 changes: 0 additions & 19 deletions google/cloud/_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,9 @@
except ImportError:
app_identity = None
try:
from google.gax.grpc import exc_to_code as beta_exc_to_code
import grpc
from grpc._channel import _Rendezvous
except ImportError: # pragma: NO COVER
beta_exc_to_code = None
grpc = None
_Rendezvous = Exception
import six
from six.moves import http_client
from six.moves import configparser
Expand Down Expand Up @@ -685,21 +681,6 @@ def make_insecure_stub(stub_class, host, port=None):
return stub_class(channel)


def exc_to_code(exc):
"""Retrieves the status code from a gRPC exception.
:type exc: :class:`Exception`
:param exc: An exception from gRPC beta or stable.
:rtype: :class:`grpc.StatusCode`
:returns: The status code attached to the exception.
"""
if isinstance(exc, _Rendezvous):
return exc.code()
else:
return beta_exc_to_code(exc)


try:
from pytz import UTC # pylint: disable=unused-import,wrong-import-order
except ImportError:
Expand Down
2 changes: 1 addition & 1 deletion google/cloud/bigtable/row_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ class InvalidChunk(RuntimeError):
class PartialRowsData(object):
"""Convenience wrapper for consuming a ``ReadRows`` streaming response.
:type response_iterator: :class:`grpc._channel._Rendezvous`
:type response_iterator: :class:`~google.cloud.exceptions.GrpcRendezvous`
:param response_iterator: A streaming iterator returned from a
``ReadRows`` request.
"""
Expand Down
2 changes: 1 addition & 1 deletion google/cloud/bigtable/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ def sample_row_keys(self):
samples would require space roughly equal to the difference in their
``offset_bytes`` fields.
:rtype: :class:`grpc._channel._Rendezvous`
:rtype: :class:`~google.cloud.exceptions.GrpcRendezvous`
:returns: A cancel-able iterator. Can be consumed by calling ``next()``
or by casting to a :class:`list` and can be cancelled by
calling ``cancel()``.
Expand Down
5 changes: 2 additions & 3 deletions google/cloud/datastore/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,17 @@
from google.cloud.environment_vars import DISABLE_GRPC
from google.cloud.environment_vars import GCD_HOST
from google.cloud.exceptions import Conflict
from google.cloud.exceptions import GrpcRendezvous
from google.cloud.exceptions import make_exception
from google.cloud.datastore._generated import datastore_pb2 as _datastore_pb2
# pylint: disable=ungrouped-imports
try:
from grpc import StatusCode
from grpc._channel import _Rendezvous
from google.cloud.datastore._generated import datastore_grpc_pb2
except ImportError: # pragma: NO COVER
_HAVE_GRPC = False
datastore_grpc_pb2 = None
StatusCode = None
_Rendezvous = Exception
else:
_HAVE_GRPC = True
# pylint: enable=ungrouped-imports
Expand Down Expand Up @@ -313,7 +312,7 @@ def commit(self, project, request_pb):
request_pb.project_id = project
try:
return self._stub.Commit(request_pb)
except _Rendezvous as exc:
except GrpcRendezvous as exc:
if exc.code() == StatusCode.ABORTED:
raise Conflict(exc.details())
raise
Expand Down
11 changes: 11 additions & 0 deletions google/cloud/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@

_HTTP_CODE_TO_EXCEPTION = {} # populated at end of module

try:
from grpc._channel import _Rendezvous
except ImportError: # pragma: NO COVER
_Rendezvous = None


# pylint: disable=invalid-name
GrpcRendezvous = _Rendezvous
"""Exception class raised by gRPC stable."""
# pylint: enable=invalid-name


class GoogleCloudError(Exception):
"""Base error class for Google Cloud errors (abstract).
Expand Down
2 changes: 1 addition & 1 deletion google/cloud/logging/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from google.gax import CallOptions
from google.gax import INITIAL_PAGE
from google.gax.errors import GaxError
from google.gax.grpc import exc_to_code
from google.logging.type.log_severity_pb2 import LogSeverity
from google.logging.v2.logging_config_pb2 import LogSink
from google.logging.v2.logging_metrics_pb2 import LogMetric
Expand All @@ -29,7 +30,6 @@
# pylint: disable=ungrouped-imports
from google.cloud._helpers import _datetime_to_pb_timestamp
from google.cloud._helpers import _pb_timestamp_to_rfc3339
from google.cloud._helpers import exc_to_code
from google.cloud.exceptions import Conflict
from google.cloud.exceptions import NotFound
# pylint: enable=ungrouped-imports
Expand Down
8 changes: 4 additions & 4 deletions google/cloud/pubsub/_gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@
from google.gax import CallOptions
from google.gax import INITIAL_PAGE
from google.gax.errors import GaxError
from google.gax.grpc import exc_to_code
from google.pubsub.v1.pubsub_pb2 import PubsubMessage
from google.pubsub.v1.pubsub_pb2 import PushConfig
from grpc.beta.implementations import insecure_channel
from grpc import insecure_channel
from grpc import StatusCode

# pylint: disable=ungrouped-imports
from google.cloud._helpers import _to_bytes
from google.cloud._helpers import exc_to_code
from google.cloud._helpers import _pb_timestamp_to_rfc3339
from google.cloud.exceptions import Conflict
from google.cloud.exceptions import NotFound
Expand Down Expand Up @@ -520,7 +520,7 @@ def make_gax_publisher_api(connection):
"""
channel = None
if connection.in_emulator:
channel = insecure_channel(connection.host, None)
channel = insecure_channel(connection.host)
return PublisherApi(channel=channel)


Expand All @@ -540,5 +540,5 @@ def make_gax_subscriber_api(connection):
"""
channel = None
if connection.in_emulator:
channel = insecure_channel(connection.host, None)
channel = insecure_channel(connection.host)
return SubscriberApi(channel=channel)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

GRPC_PACKAGES = [
'grpcio >= 1.0.0',
'google-gax >= 0.13.0, < 0.14dev',
'google-gax >= 0.14.1, < 0.15dev',
'gapic-google-pubsub-v1 >= 0.9.0, < 0.10dev',
'grpc-google-pubsub-v1 >= 0.9.0, < 0.10dev',
'gapic-google-logging-v2 >= 0.9.0, < 0.10dev',
Expand Down
4 changes: 2 additions & 2 deletions system_tests/bigtable.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def _retry_on_unavailable(exc):


def setUpModule():
from grpc._channel import _Rendezvous
from google.cloud.exceptions import GrpcRendezvous

Config.IN_EMULATOR = os.getenv(BIGTABLE_EMULATOR) is not None

Expand All @@ -106,7 +106,7 @@ def setUpModule():
Config.INSTANCE = Config.CLIENT.instance(INSTANCE_ID, LOCATION_ID)

if not Config.IN_EMULATOR:
retry = RetryErrors(_Rendezvous,
retry = RetryErrors(GrpcRendezvous,
error_predicate=_retry_on_unavailable)
instances, failed_locations = retry(Config.CLIENT.list_instances)()

Expand Down
5 changes: 3 additions & 2 deletions system_tests/logging_.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ def _logger_name():
return 'system-tests-logger' + unique_resource_id('-')

def _list_entries(self, logger):
from grpc._channel import _Rendezvous
from google.gax.errors import GaxError

inner = RetryResult(_has_entries)(logger.list_entries)
outer = RetryErrors(_Rendezvous, _retry_on_unavailable)(inner)
outer = RetryErrors(GaxError, _retry_on_unavailable)(inner)
return outer()

def test_log_text(self):
Expand Down
7 changes: 3 additions & 4 deletions system_tests/pubsub.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
import unittest

from google.gax.errors import GaxError
from google.gax.grpc import exc_to_code
from grpc import StatusCode
from grpc._channel import _Rendezvous
import httplib2

# pylint: disable=ungrouped-imports
from google.cloud import _helpers
from google.cloud.environment_vars import PUBSUB_EMULATOR
from google.cloud.pubsub import client
# pylint: enable=ungrouped-imports
Expand All @@ -34,10 +33,10 @@


def _unavailable(exc):
return _helpers.exc_to_code(exc) == StatusCode.UNAVAILABLE
return exc_to_code(exc) == StatusCode.UNAVAILABLE


retry_unavailable = RetryErrors((GaxError, _Rendezvous), _unavailable)
retry_unavailable = RetryErrors(GaxError, _unavailable)


class Config(object):
Expand Down
4 changes: 2 additions & 2 deletions unit_tests/_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ def __init__(self, **kw):
self.__dict__.update(kw)

def _make_grpc_error(self, status_code):
from grpc._channel import _Rendezvous
from grpc._channel import _RPCState
from google.cloud.exceptions import GrpcRendezvous

details = 'Some error details.'
exc_state = _RPCState((), None, None, status_code, details)
return _Rendezvous(exc_state, None, None, None)
return GrpcRendezvous(exc_state, None, None, None)

def _make_grpc_not_found(self):
from grpc import StatusCode
Expand Down
10 changes: 5 additions & 5 deletions unit_tests/datastore/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,24 +243,24 @@ def _commit_failure_helper(self, exc, err_class):
@unittest.skipUnless(_HAVE_GRPC, 'No gRPC')
def test_commit_failure_aborted(self):
from grpc import StatusCode
from grpc._channel import _Rendezvous
from grpc._channel import _RPCState
from google.cloud.exceptions import Conflict
from google.cloud.exceptions import GrpcRendezvous

details = 'Bad things.'
exc_state = _RPCState((), None, None, StatusCode.ABORTED, details)
exc = _Rendezvous(exc_state, None, None, None)
exc = GrpcRendezvous(exc_state, None, None, None)
self._commit_failure_helper(exc, Conflict)

@unittest.skipUnless(_HAVE_GRPC, 'No gRPC')
def test_commit_failure_cancelled(self):
from grpc import StatusCode
from grpc._channel import _Rendezvous
from grpc._channel import _RPCState
from google.cloud.exceptions import GrpcRendezvous

exc_state = _RPCState((), None, None, StatusCode.CANCELLED, None)
exc = _Rendezvous(exc_state, None, None, None)
self._commit_failure_helper(exc, _Rendezvous)
exc = GrpcRendezvous(exc_state, None, None, None)
self._commit_failure_helper(exc, GrpcRendezvous)

@unittest.skipUnless(_HAVE_GRPC, 'No gRPC')
def test_commit_failure_non_grpc_err(self):
Expand Down
12 changes: 6 additions & 6 deletions unit_tests/pubsub/test__gax.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,8 +787,8 @@ def mock_publisher_api(channel):
channels.append(channel)
return mock_result

def mock_insecure_channel(host, port):
insecure_args.append((host, port))
def mock_insecure_channel(host):
insecure_args.append(host)
return mock_channel

host = 'CURR_HOST:1234'
Expand All @@ -799,7 +799,7 @@ def mock_insecure_channel(host, port):

self.assertIs(result, mock_result)
self.assertEqual(channels, [mock_channel])
self.assertEqual(insecure_args, [(host, None)])
self.assertEqual(insecure_args, [host])


@unittest.skipUnless(_HAVE_GAX, 'No gax-python')
Expand Down Expand Up @@ -840,8 +840,8 @@ def mock_subscriber_api(channel):
channels.append(channel)
return mock_result

def mock_insecure_channel(host, port):
insecure_args.append((host, port))
def mock_insecure_channel(host):
insecure_args.append(host)
return mock_channel

host = 'CURR_HOST:1234'
Expand All @@ -852,7 +852,7 @@ def mock_insecure_channel(host, port):

self.assertIs(result, mock_result)
self.assertEqual(channels, [mock_channel])
self.assertEqual(insecure_args, [(host, None)])
self.assertEqual(insecure_args, [host])


class _GAXPublisherAPI(_GAXBaseAPI):
Expand Down
31 changes: 0 additions & 31 deletions unit_tests/test__helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1025,37 +1025,6 @@ def test_without_port_argument(self):
self._helper(host, host)


class Test_exc_to_code(unittest.TestCase):

def _callFUT(self, exc):
from google.cloud._helpers import exc_to_code
return exc_to_code(exc)

def test_with_stable(self):
from grpc._channel import _Rendezvous
from grpc._channel import _RPCState
from grpc import StatusCode

status_code = StatusCode.FAILED_PRECONDITION
exc_state = _RPCState((), None, None, status_code, None)
exc = _Rendezvous(exc_state, None, None, None)
result = self._callFUT(exc)
self.assertEqual(result, status_code)

def test_with_beta(self):
from grpc import StatusCode
from grpc.framework.interfaces.face.face import AbortionError

status_code = StatusCode.UNIMPLEMENTED
exc = AbortionError(None, None, status_code, None)
result = self._callFUT(exc)
self.assertEqual(result, status_code)

def test_with_none(self):
result = self._callFUT(None)
self.assertIsNone(result)


class _AppIdentity(object):

def __init__(self, app_id):
Expand Down

0 comments on commit 523100f

Please sign in to comment.