コード例 #1
0
    def create_channel(
        cls, address="spanner.googleapis.com:443", credentials=None, **kwargs
    ):
        """Create and return a gRPC channel object.

        Args:
            address (str): The host for the channel to use.
            credentials (~.Credentials): The
                authorization credentials to attach to requests. These
                credentials identify this application to the service. If
                none are specified, the client will attempt to ascertain
                the credentials from the environment.
            kwargs (dict): Keyword arguments, which are passed to the
                channel creation.

        Returns:
            grpc.Channel: A gRPC channel object.
        """
        grpc_gcp_config = grpc_gcp.api_config_from_text_pb(
            pkg_resources.resource_string(__name__, _SPANNER_GRPC_CONFIG)
        )
        options = [(grpc_gcp.API_CONFIG_CHANNEL_ARG, grpc_gcp_config)]
        return google.api_core.grpc_helpers.create_channel(
            address, credentials=credentials, scopes=cls._OAUTH_SCOPES, **kwargs
        )
コード例 #2
0
    def test_reconnect(self):
        server_pool = logging_pool.pool(test_constants.THREAD_CONCURRENCY)
        handler = grpc.method_handlers_generic_handler('test', {
            'UnaryUnary':
            grpc.unary_unary_rpc_method_handler(_handle_unary_unary)
        })
        sock_opt = _get_reuse_socket_option()
        port = _pick_and_bind_port(sock_opt)
        self.assertIsNotNone(port)

        server = grpc.server(server_pool, (handler, ))
        server.add_insecure_port('[::]:{}'.format(port))
        server.start()

        channel_config = grpc_gcp.api_config_from_text_pb('')
        channel = grpc_gcp.insecure_channel(
            'localhost:{}'.format(port),
            options=((grpc_gcp.API_CONFIG_CHANNEL_ARG, channel_config), ))

        multi_callable = channel.unary_unary(_UNARY_UNARY)
        self.assertEqual(_RESPONSE, multi_callable(_REQUEST))
        server.stop(None)
        # By default, the channel connectivity is checked every 5s
        # GRPC_CLIENT_CHANNEL_BACKUP_POLL_INTERVAL_MS can be set to change
        # this.
        time.sleep(5.1)
        server = grpc.server(server_pool, (handler, ))
        server.add_insecure_port('[::]:{}'.format(port))
        server.start()
        self.assertEqual(_RESPONSE, multi_callable(_REQUEST))
コード例 #3
0
    def test_immediately_connectable_channel_connectivity(self):
        thread_pool = _thread_pool.RecordingThreadPool(max_workers=None)
        server = grpc.server(thread_pool, options=(('grpc.so_reuseport', 0), ))
        port = server.add_insecure_port('[::]:0')
        server.start()
        callback = _Callback()

        channel_config = grpc_gcp.api_config_from_text_pb('')
        channel = grpc_gcp.insecure_channel(
            'localhost:{}'.format(port),
            options=((grpc_gcp.API_CONFIG_CHANNEL_ARG, channel_config), ))

        ready_future = grpc.channel_ready_future(channel)
        ready_future.add_done_callback(callback.accept_value)
        self.assertIsNone(
            ready_future.result(timeout=test_constants.LONG_TIMEOUT))
        value_passed_to_callback = callback.block_until_called()
        self.assertIs(ready_future, value_passed_to_callback)
        self.assertFalse(ready_future.cancelled())
        self.assertTrue(ready_future.done())
        self.assertFalse(ready_future.running())
        # Cancellation after maturity has no effect.
        ready_future.cancel()
        self.assertFalse(ready_future.cancelled())
        self.assertTrue(ready_future.done())
        self.assertFalse(ready_future.running())
        self.assertFalse(thread_pool.was_used())
コード例 #4
0
    def test_lonely_channel_connectivity(self):
        callback = _Callback()

        channel_config = grpc_gcp.api_config_from_text_pb('')
        channel = grpc_gcp.insecure_channel(
            'localhost:12345',
            options=((grpc_gcp.API_CONFIG_CHANNEL_ARG, channel_config), ))
        channel.subscribe(callback.update, try_to_connect=False)
        time.sleep(4)
        first_connectivities = callback.block_until_connectivities_satisfy(
            bool)
        channel.subscribe(callback.update, try_to_connect=True)
        time.sleep(4)
        second_connectivities = callback.block_until_connectivities_satisfy(
            lambda connectivities: 2 <= len(connectivities))
        # Wait for a connection that will never happen.
        time.sleep(test_constants.SHORT_TIMEOUT)
        third_connectivities = callback.connectivities()
        channel.unsubscribe(callback.update)
        fourth_connectivities = callback.connectivities()
        channel.unsubscribe(callback.update)
        fifth_connectivities = callback.connectivities()

        self.assertSequenceEqual((grpc.ChannelConnectivity.IDLE, ),
                                 first_connectivities)
        self.assertNotIn(grpc.ChannelConnectivity.READY, second_connectivities)
        self.assertNotIn(grpc.ChannelConnectivity.READY, third_connectivities)
        self.assertNotIn(grpc.ChannelConnectivity.READY, fourth_connectivities)
        self.assertNotIn(grpc.ChannelConnectivity.READY, fifth_connectivities)
コード例 #5
0
ファイル: prober.py プロジェクト: WeiranFang/grpc-gcp-python
def _get_stub_channel(target, use_extension=False):
  cred, _ = auth.default([_OAUTH_SCOPE])
  if not use_extension:
    return _secure_authorized_channel(cred, Request(), target)
  config = grpc_gcp.api_config_from_text_pb(
      pkg_resources.resource_string(__name__, 'spanner.grpc.config'))
  options = [(grpc_gcp.API_CONFIG_CHANNEL_ARG, config)]
  return _secure_authorized_channel(cred, Request(), target, options=options)
コード例 #6
0
 def setUp(self):
     self._server = test_common.test_server()
     self._server.add_generic_rpc_handlers((_GenericHandler(), ))
     port = self._server.add_insecure_port('[::]:0')
     self._server.start()
     channel_config = grpc_gcp.api_config_from_text_pb('')
     self._channel = grpc_gcp.insecure_channel(
         'localhost:{}'.format(port),
         options=((grpc_gcp.API_CONFIG_CHANNEL_ARG, channel_config), ))
コード例 #7
0
 def setUp(self):
     config = grpc_gcp.api_config_from_text_pb(
         pkg_resources.resource_string(__name__, 'spanner.grpc.config'))
     http_request = Request()
     credentials, _ = google.auth.default([_OAUTH_SCOPE], http_request)
     self.channel = self._create_secure_gcp_channel(
         credentials,
         http_request,
         _TARGET,
         options=[(grpc_gcp.API_CONFIG_CHANNEL_ARG, config)])
     self.assertIsInstance(self.channel, grpc_gcp._channel.Channel)
     self.assertEqual(self.channel._max_concurrent_streams_low_watermark, 1)
     self.assertEqual(self.channel._max_size, 10)
コード例 #8
0
    def setUp(self):
        self._server_pool = logging_pool.pool(test_constants.THREAD_CONCURRENCY)
        self._trigger = _TestTrigger(test_constants.THREAD_CONCURRENCY)
        self._server = grpc.server(
            self._server_pool,
            handlers=(_GenericHandler(self._trigger),),
            options=(('grpc.so_reuseport', 0),),
            maximum_concurrent_rpcs=test_constants.THREAD_CONCURRENCY)
        port = self._server.add_insecure_port('[::]:0')
        self._server.start()

        channel_config = grpc_gcp.api_config_from_text_pb('')
        self._channel = grpc_gcp.insecure_channel(
            'localhost:{}'.format(port),
            options=((grpc_gcp.API_CONFIG_CHANNEL_ARG, channel_config),)
        )
コード例 #9
0
    def test_immediately_connectable_channel_connectivity(self):
        thread_pool = _thread_pool.RecordingThreadPool(max_workers=None)
        server = grpc.server(thread_pool, options=(('grpc.so_reuseport', 0), ))
        port = server.add_insecure_port('[::]:0')
        server.start()
        first_callback = _Callback()
        second_callback = _Callback()

        channel_config = grpc_gcp.api_config_from_text_pb('')
        channel = grpc_gcp.insecure_channel(
            'localhost:{}'.format(port),
            options=((grpc_gcp.API_CONFIG_CHANNEL_ARG, channel_config), ))
        channel.subscribe(first_callback.update, try_to_connect=False)
        first_connectivities = first_callback.block_until_connectivities_satisfy(
            bool)
        # Wait for a connection that will never happen because try_to_connect=True
        # has not yet been passed.
        time.sleep(test_constants.SHORT_TIMEOUT)
        second_connectivities = first_callback.connectivities()
        channel.subscribe(second_callback.update, try_to_connect=True)
        third_connectivities = first_callback.block_until_connectivities_satisfy(
            lambda connectivities: 2 <= len(connectivities))
        fourth_connectivities = second_callback.block_until_connectivities_satisfy(
            bool)
        # Wait for a connection that will happen (or may already have happened).
        first_callback.block_until_connectivities_satisfy(
            _ready_in_connectivities)
        second_callback.block_until_connectivities_satisfy(
            _ready_in_connectivities)
        del channel

        self.assertSequenceEqual((grpc.ChannelConnectivity.IDLE, ),
                                 first_connectivities)
        self.assertSequenceEqual((grpc.ChannelConnectivity.IDLE, ),
                                 second_connectivities)
        self.assertNotIn(grpc.ChannelConnectivity.TRANSIENT_FAILURE,
                         third_connectivities)
        self.assertNotIn(grpc.ChannelConnectivity.SHUTDOWN,
                         third_connectivities)
        self.assertNotIn(grpc.ChannelConnectivity.TRANSIENT_FAILURE,
                         fourth_connectivities)
        self.assertNotIn(grpc.ChannelConnectivity.SHUTDOWN,
                         fourth_connectivities)
        self.assertFalse(thread_pool.was_used())
コード例 #10
0
 def test_lonely_channel_connectivity(self):
     callback = _Callback()
     channel_config = grpc_gcp.api_config_from_text_pb('')
     channel = grpc_gcp.insecure_channel(
         'localhost:12345',
         options=((grpc_gcp.API_CONFIG_CHANNEL_ARG, channel_config), ))
     ready_future = grpc.channel_ready_future(channel)
     ready_future.add_done_callback(callback.accept_value)
     with self.assertRaises(grpc.FutureTimeoutError):
         ready_future.result(timeout=test_constants.SHORT_TIMEOUT)
     self.assertFalse(ready_future.cancelled())
     self.assertFalse(ready_future.done())
     self.assertTrue(ready_future.running())
     ready_future.cancel()
     value_passed_to_callback = callback.block_until_called()
     self.assertIs(ready_future, value_passed_to_callback)
     self.assertTrue(ready_future.cancelled())
     self.assertTrue(ready_future.done())
     self.assertFalse(ready_future.running())
コード例 #11
0
def _create_channel():
    http_request = Request()
    credentials, _ = google.auth.default([_OAUTH_SCOPE], http_request)

    if _GRPC_GCP:
        config = grpc_gcp.api_config_from_text_pb(
            pkg_resources.resource_string(__name__, 'spanner.grpc.config'))
        channel = _create_secure_gcp_channel(
            credentials,
            http_request,
            _TARGET,
            options=[(grpc_gcp.API_CONFIG_CHANNEL_ARG, config)])
    else:
        channel = _create_secure_gcp_channel(credentials, http_request,
                                             _TARGET)

    print('\nUsing gRPC-GCP extension: {}'.format(_is_gcp_channel(channel)))

    return channel
コード例 #12
0
    def setUp(self):
        self._servicer = _Servicer()
        self._server = test_common.test_server()
        self._server.add_generic_rpc_handlers(
            (_generic_handler(self._servicer), ))
        port = self._server.add_insecure_port('[::]:0')
        self._server.start()

        channel_config = grpc_gcp.api_config_from_text_pb('')
        channel = grpc_gcp.insecure_channel(
            'localhost:{}'.format(port),
            options=((grpc_gcp.API_CONFIG_CHANNEL_ARG, channel_config), ))

        self._unary_unary = channel.unary_unary(
            '/'.join((
                '',
                _SERVICE,
                _UNARY_UNARY,
            )),
            request_serializer=_REQUEST_SERIALIZER,
            response_deserializer=_RESPONSE_DESERIALIZER,
        )
        self._unary_stream = channel.unary_stream(
            '/'.join((
                '',
                _SERVICE,
                _UNARY_STREAM,
            )), )
        self._stream_unary = channel.stream_unary(
            '/'.join((
                '',
                _SERVICE,
                _STREAM_UNARY,
            )), )
        self._stream_stream = channel.stream_stream(
            '/'.join((
                '',
                _SERVICE,
                _STREAM_STREAM,
            )),
            request_serializer=_REQUEST_SERIALIZER,
            response_deserializer=_RESPONSE_DESERIALIZER,
        )
コード例 #13
0
    def test_reachable_then_unreachable_channel_connectivity(self):
        thread_pool = _thread_pool.RecordingThreadPool(max_workers=None)
        server = grpc.server(thread_pool, options=(('grpc.so_reuseport', 0), ))
        port = server.add_insecure_port('[::]:0')
        server.start()
        callback = _Callback()

        channel_config = grpc_gcp.api_config_from_text_pb('')
        channel = grpc_gcp.insecure_channel(
            'localhost:{}'.format(port),
            options=((grpc_gcp.API_CONFIG_CHANNEL_ARG, channel_config), ))
        channel.subscribe(callback.update, try_to_connect=True)
        callback.block_until_connectivities_satisfy(_ready_in_connectivities)
        # Now take down the server and confirm that channel readiness is repudiated.
        server.stop(None)
        callback.block_until_connectivities_satisfy(
            _last_connectivity_is_not_ready)
        channel.unsubscribe(callback.update)
        self.assertFalse(thread_pool.was_used())
コード例 #14
0
    def test_channel_connectivity_invalid_target(self):
        config = config = grpc_gcp.api_config_from_text_pb(
            pkg_resources.resource_string(__name__, 'spanner.grpc.config'))
        http_request = Request()
        credentials, _ = google.auth.default([_OAUTH_SCOPE], http_request)
        invalid_channel = self._create_secure_gcp_channel(
            credentials,
            http_request,
            'localhost:1234',
            options=[(grpc_gcp.API_CONFIG_CHANNEL_ARG, config)])

        callback = _Callback()
        invalid_channel.subscribe(callback.update_first, try_to_connect=False)

        stub = spanner_pb2_grpc.SpannerStub(invalid_channel)
        with self.assertRaises(Exception) as context:
            stub.CreateSession(
                spanner_pb2.CreateSessionRequest(database=_DATABASE))
        self.assertEqual(grpc.StatusCode.UNAVAILABLE, context.exception.code())
        first_connectivities = callback.block_until_connectivities_satisfy(
            lambda connectivities: len(connectivities) >= 3)
        self.assertEqual(grpc.ChannelConnectivity.IDLE,
                         first_connectivities[0])
        self.assertIn(grpc.ChannelConnectivity.CONNECTING,
                      first_connectivities)
        self.assertIn(grpc.ChannelConnectivity.TRANSIENT_FAILURE,
                      first_connectivities)

        invalid_channel.subscribe(callback.update_second, try_to_connect=True)
        second_connectivities = callback.block_until_connectivities_satisfy(
            lambda connectivities: len(connectivities) >= 3, False)
        self.assertNotIn(grpc.ChannelConnectivity.IDLE, second_connectivities)
        self.assertIn(grpc.ChannelConnectivity.CONNECTING,
                      second_connectivities)
        self.assertIn(grpc.ChannelConnectivity.TRANSIENT_FAILURE,
                      second_connectivities)

        self.assertEqual(2, len(invalid_channel._subscribers))
        invalid_channel.unsubscribe(callback.update_first)
        invalid_channel.unsubscribe(callback.update_second)
        self.assertEqual(0, len(invalid_channel._subscribers))
コード例 #15
0
    def create_channel(cls, address="spanner.googleapis.com:443", credentials=None):
        """Create and return a gRPC channel object.

        Args:
            address (str): The host for the channel to use.
            credentials (~.Credentials): The
                authorization credentials to attach to requests. These
                credentials identify this application to the service. If
                none are specified, the client will attempt to ascertain
                the credentials from the environment.

        Returns:
            grpc.Channel: A gRPC channel object.
        """
        grpc_gcp_config = grpc_gcp.api_config_from_text_pb(
            pkg_resources.resource_string(__name__, _SPANNER_GRPC_CONFIG)
        )
        options = [(grpc_gcp.API_CONFIG_CHANNEL_ARG, grpc_gcp_config)]
        return google.api_core.grpc_helpers.create_channel(
            address, credentials=credentials, scopes=cls._OAUTH_SCOPES
        )
コード例 #16
0
    args = parser.parse_args()

    if args.scenario == UNSTARTED_SERVER:
        server = grpc.server(DaemonPool(),
                             options=(('grpc.so_reuseport', 0), ))
        if args.wait_for_interrupt:
            time.sleep(WAIT_TIME)
    elif args.scenario == RUNNING_SERVER:
        server = grpc.server(DaemonPool(),
                             options=(('grpc.so_reuseport', 0), ))
        port = server.add_insecure_port('[::]:0')
        server.start()
        if args.wait_for_interrupt:
            time.sleep(WAIT_TIME)
    elif args.scenario == POLL_CONNECTIVITY_NO_SERVER:
        channel_config = grpc_gcp.api_config_from_text_pb('')
        channel = grpc_gcp.insecure_channel(
            'localhost:12345',
            options=((grpc_gcp.API_CONFIG_CHANNEL_ARG, channel_config), ))

        def connectivity_callback(connectivity):
            pass

        channel.subscribe(connectivity_callback, try_to_connect=True)
        if args.wait_for_interrupt:
            time.sleep(WAIT_TIME)
    elif args.scenario == POLL_CONNECTIVITY:
        server = grpc.server(DaemonPool(),
                             options=(('grpc.so_reuseport', 0), ))
        port = server.add_insecure_port('[::]:0')
        server.start()