예제 #1
0
def test_retryable_stream_errors():
    # Make sure the config matches our hard-coded tuple of exceptions.
    interfaces = subscriber_client_config.config["interfaces"]
    retry_codes = interfaces["google.pubsub.v1.Subscriber"]["retry_codes"]
    idempotent = retry_codes["idempotent"]

    status_codes = tuple(getattr(grpc.StatusCode, name, None) for name in idempotent)
    expected = tuple(
        exceptions.exception_class_for_grpc_status(status_code)
        for status_code in status_codes
    )
    assert set(expected).issubset(set(streaming_pull_manager._RETRYABLE_STREAM_ERRORS))
def test_idempotent_retry_codes():
    # Make sure the config matches our hard-coded tuple of exceptions.
    interfaces = subscriber_client_config.config['interfaces']
    retry_codes = interfaces['google.pubsub.v1.Subscriber']['retry_codes']
    idempotent = retry_codes['idempotent']

    status_codes = tuple(
        getattr(grpc.StatusCode, name, None) for name in idempotent)
    expected = tuple(
        exceptions.exception_class_for_grpc_status(status_code)
        for status_code in status_codes)
    assert base.BasePolicy._RETRYABLE_STREAM_ERRORS == expected
def test_retryable_stream_errors():
    # Make sure the config matches our hard-coded tuple of exceptions.
    interfaces = subscriber_client_config.config["interfaces"]
    retry_codes = interfaces["google.pubsub.v1.Subscriber"]["retry_codes"]
    idempotent = retry_codes["idempotent"]

    status_codes = tuple(getattr(grpc.StatusCode, name, None) for name in idempotent)
    expected = tuple(
        exceptions.exception_class_for_grpc_status(status_code)
        for status_code in status_codes
    )
    assert set(expected).issubset(set(streaming_pull_manager._RETRYABLE_STREAM_ERRORS))
예제 #4
0
def _exception_class_for_grpc_status_name(name):
    """Returns the Google API exception class for a gRPC error code name.

    Args:
        name (str): The name of the gRPC status code, for example,
            ``UNAVAILABLE``.

    Returns:
        :func:`type`: The appropriate subclass of
            :class:`google.api_core.exceptions.GoogleAPICallError`.
    """
    return exceptions.exception_class_for_grpc_status(getattr(grpc.StatusCode, name))
예제 #5
0
def _exception_class_for_grpc_status_name(name):
    """Returns the Google API exception class for a gRPC error code name.

    Args:
        name (str): The name of the gRPC status code, for example,
            ``UNAVAILABLE``.

    Returns:
        :func:`type`: The appropriate subclass of
            :class:`google.api_core.exceptions.GoogleAPICallError`.
    """
    return exceptions.exception_class_for_grpc_status(
        getattr(grpc.StatusCode, name))
예제 #6
0
def test_idempotent_retry_codes():
    # Make sure the config matches our hard-coded tuple of exceptions.
    interfaces = subscriber_client_config.config['interfaces']
    retry_codes = interfaces['google.pubsub.v1.Subscriber']['retry_codes']
    idempotent = retry_codes['idempotent']

    status_codes = tuple(
        getattr(grpc.StatusCode, name, None)
        for name in idempotent
    )
    expected = tuple(
        exceptions.exception_class_for_grpc_status(status_code)
        for status_code in status_codes
    )
    assert set(expected).issubset(
        set(base.BasePolicy._RETRYABLE_STREAM_ERRORS))
예제 #7
0
    def _get_retry_and_timeout(
        self, service_address: metadata.Address,
        meth_pb: descriptor_pb2.MethodDescriptorProto
    ) -> Tuple[Optional[wrappers.RetryInfo], Optional[float]]:
        """Returns the retry and timeout configuration of a method if it exists.

        Args:
            service_address (~.metadata.Address): An address object for the
                service, denoting the location of these methods.
            meth_pb (~.descriptor_pb2.MethodDescriptorProto): A
                protobuf method objects.

        Returns:
            Tuple[Optional[~.wrappers.RetryInfo], Optional[float]]: The retry
                and timeout information for the method if it exists.
        """

        # If we got a gRPC service config, get the appropriate retry
        # and timeout information from it.
        retry = None
        timeout = None

        # This object should be a dictionary that conforms to the
        # gRPC service config proto:
        #   Repo: https://github.com/grpc/grpc-proto/
        #   Filename: grpc/service_config/service_config.proto
        #
        # We only care about a small piece, so we are just leaving
        # it as a dictionary and parsing accordingly.
        if self.opts.retry:
            # The gRPC service config uses a repeated `name` field
            # with a particular format, which we match against.
            # This defines the expected selector for *this* method.
            selector = {
                'service':
                '{package}.{service_name}'.format(
                    package='.'.join(service_address.package),
                    service_name=service_address.name,
                ),
                'method':
                meth_pb.name,
            }

            # Find the method config that applies to us, if any.
            mc = next((c for c in self.opts.retry.get('methodConfig', [])
                       if selector in c.get('name')), None)
            if mc:
                # Set the timeout according to this method config.
                if mc.get('timeout'):
                    timeout = self._to_float(mc['timeout'])

                # Set the retry according to this method config.
                if 'retryPolicy' in mc:
                    r = mc['retryPolicy']
                    retry = wrappers.RetryInfo(
                        max_attempts=r.get('maxAttempts', 0),
                        initial_backoff=self._to_float(
                            r.get('initialBackoff', '0s'), ),
                        max_backoff=self._to_float(r.get('maxBackoff', '0s')),
                        backoff_multiplier=r.get('backoffMultiplier', 0.0),
                        retryable_exceptions=frozenset(
                            exceptions.exception_class_for_grpc_status(
                                getattr(grpc.StatusCode, code), )
                            for code in r.get('retryableStatusCodes', [])),
                    )

        return retry, timeout
예제 #8
0
    def _get_methods(
        self,
        methods: Sequence[descriptor_pb2.MethodDescriptorProto],
        service_address: metadata.Address,
        path: Tuple[int, ...],
    ) -> Mapping[str, wrappers.Method]:
        """Return a dictionary of wrapped methods for the given service.

        Args:
            methods (Sequence[~.descriptor_pb2.MethodDescriptorProto]): A
                sequence of protobuf method objects.
            service_address (~.metadata.Address): An address object for the
                service, denoting the location of these methods.
            path (Tuple[int]): The source location path thus far, as understood
                by ``SourceCodeInfo.Location``.

        Returns:
            Mapping[str, ~.wrappers.Method]: A ordered mapping of
                :class:`~.wrappers.Method` objects.
        """
        # Iterate over the methods and collect them into a dictionary.
        answer: Dict[str, wrappers.Method] = collections.OrderedDict()
        for meth_pb, i in zip(methods, range(0, sys.maxsize)):
            lro = None

            # If the output type is google.longrunning.Operation, we use
            # a specialized object in its place.
            if meth_pb.output_type.endswith('google.longrunning.Operation'):
                op = meth_pb.options.Extensions[operations_pb2.operation_info]
                if not op.response_type or not op.metadata_type:
                    raise TypeError(
                        f'rpc {meth_pb.name} returns a google.longrunning.'
                        'Operation, but is missing a response type or '
                        'metadata type.', )
                lro = wrappers.OperationInfo(
                    response_type=self.api_messages[service_address.resolve(
                        op.response_type, )],
                    metadata_type=self.api_messages[service_address.resolve(
                        op.metadata_type, )],
                )

            # If we got a gRPC service config, get the appropriate retry
            # and timeout information from it.
            retry = None
            timeout = None

            # This object should be a dictionary that conforms to the
            # gRPC service config proto:
            #   Repo: https://github.com/grpc/grpc-proto/
            #   Filename: grpc/service_config/service_config.proto
            #
            # We only care about a small piece, so we are just leaving
            # it as a dictionary and parsing accordingly.
            if self.opts.retry:
                # The gRPC service config uses a repeated `name` field
                # with a particular format, which we match against.
                # This defines the expected selector for *this* method.
                selector = {
                    'service':
                    '{package}.{service_name}'.format(
                        package='.'.join(service_address.package),
                        service_name=service_address.name,
                    ),
                    'method':
                    meth_pb.name,
                }

                # Find the method config that applies to us, if any.
                mc = next((i for i in self.opts.retry.get('methodConfig', [])
                           if selector in i.get('name')), None)
                if mc:
                    # Set the timeout according to this method config.
                    if mc.get('timeout'):
                        timeout = self._to_float(mc['timeout'])

                    # Set the retry according to this method config.
                    if 'retryPolicy' in mc:
                        r = mc['retryPolicy']
                        retry = wrappers.RetryInfo(
                            max_attempts=r.get('maxAttempts', 0),
                            initial_backoff=self._to_float(
                                r.get('initialBackoff', '0s'), ),
                            max_backoff=self._to_float(
                                r.get('maxBackoff', '0s'), ),
                            backoff_multiplier=r.get('backoffMultiplier', 0.0),
                            retryable_exceptions=frozenset(
                                exceptions.exception_class_for_grpc_status(
                                    getattr(grpc.StatusCode, code), )
                                for code in r.get('retryableStatusCodes', [])),
                        )

            # Create the method wrapper object.
            answer[meth_pb.name] = wrappers.Method(
                input=self.api_messages[meth_pb.input_type.lstrip('.')],
                lro=lro,
                method_pb=meth_pb,
                meta=metadata.Metadata(
                    address=service_address.child(meth_pb.name, path + (i, )),
                    documentation=self.docs.get(path + (i, ), self.EMPTY),
                ),
                output=self.api_messages[meth_pb.output_type.lstrip('.')],
                retry=retry,
                timeout=timeout,
            )

        # Done; return the answer.
        return answer