diff options
Diffstat (limited to 'src/python')
10 files changed, 98 insertions, 49 deletions
diff --git a/src/python/grpcio/grpc_core_dependencies.py b/src/python/grpcio/grpc_core_dependencies.py index 5fc748483a..cb6ca34dc6 100644 --- a/src/python/grpcio/grpc_core_dependencies.py +++ b/src/python/grpcio/grpc_core_dependencies.py @@ -34,6 +34,7 @@ CORE_SOURCE_FILES = [ 'src/core/lib/profiling/stap_timers.c', 'src/core/lib/support/alloc.c', 'src/core/lib/support/arena.c', + 'src/core/lib/support/atm.c', 'src/core/lib/support/avl.c', 'src/core/lib/support/backoff.c', 'src/core/lib/support/cmdline.c', @@ -254,10 +255,8 @@ CORE_SOURCE_FILES = [ 'src/core/ext/client_channel/client_channel_factory.c', 'src/core/ext/client_channel/client_channel_plugin.c', 'src/core/ext/client_channel/connector.c', - 'src/core/ext/client_channel/default_initial_connect_string.c', 'src/core/ext/client_channel/http_connect_handshaker.c', 'src/core/ext/client_channel/http_proxy.c', - 'src/core/ext/client_channel/initial_connect_string.c', 'src/core/ext/client_channel/lb_policy.c', 'src/core/ext/client_channel/lb_policy_factory.c', 'src/core/ext/client_channel/lb_policy_registry.c', @@ -267,6 +266,7 @@ CORE_SOURCE_FILES = [ 'src/core/ext/client_channel/resolver.c', 'src/core/ext/client_channel/resolver_factory.c', 'src/core/ext/client_channel/resolver_registry.c', + 'src/core/ext/client_channel/retry_throttle.c', 'src/core/ext/client_channel/subchannel.c', 'src/core/ext/client_channel/subchannel_index.c', 'src/core/ext/client_channel/uri_parser.c', diff --git a/src/python/grpcio_reflection/grpc_reflection/v1alpha/reflection.py b/src/python/grpcio_reflection/grpc_reflection/v1alpha/reflection.py index 87f28396ce..c8ad9668ac 100644 --- a/src/python/grpcio_reflection/grpc_reflection/v1alpha/reflection.py +++ b/src/python/grpcio_reflection/grpc_reflection/v1alpha/reflection.py @@ -35,6 +35,7 @@ from google.protobuf import descriptor_pb2 from google.protobuf import descriptor_pool from grpc_reflection.v1alpha import reflection_pb2 +from grpc_reflection.v1alpha import reflection_pb2_grpc _POOL = descriptor_pool.Default() @@ -64,7 +65,7 @@ class ReflectionServicer(reflection_pb2.ServerReflectionServicer): Args: service_names: Iterable of fully-qualified service names available. """ - self._service_names = list(service_names) + self._service_names = tuple(sorted(service_names)) self._pool = _POOL if pool is None else pool def _file_by_filename(self, filename): @@ -84,23 +85,32 @@ class ReflectionServicer(reflection_pb2.ServerReflectionServicer): else: return _file_descriptor_response(descriptor) - def _file_containing_extension(containing_type, extension_number): - # TODO(atash) Python protobuf currently doesn't support querying extensions. - # https://github.com/google/protobuf/issues/2248 - return reflection_pb2.ServerReflectionResponse( - error_response=reflection_pb2.ErrorResponse( - error_code=grpc.StatusCode.UNIMPLEMENTED.value[0], - error_message=grpc.StatusCode.UNIMPLMENTED.value[1].encode(),)) - - def _extension_numbers_of_type(fully_qualified_name): - # TODO(atash) We're allowed to leave this unsupported according to the - # protocol, but we should still eventually implement it. Hits the same issue - # as `_file_containing_extension`, however. - # https://github.com/google/protobuf/issues/2248 - return reflection_pb2.ServerReflectionResponse( - error_response=reflection_pb2.ErrorResponse( - error_code=grpc.StatusCode.UNIMPLEMENTED.value[0], - error_message=grpc.StatusCode.UNIMPLMENTED.value[1].encode(),)) + def _file_containing_extension(self, containing_type, extension_number): + try: + message_descriptor = self._pool.FindMessageTypeByName(containing_type) + extension_descriptor = self._pool.FindExtensionByNumber( + message_descriptor, extension_number) + descriptor = self._pool.FindFileContainingSymbol( + extension_descriptor.full_name) + except KeyError: + return _not_found_error() + else: + return _file_descriptor_response(descriptor) + + def _all_extension_numbers_of_type(self, containing_type): + try: + message_descriptor = self._pool.FindMessageTypeByName(containing_type) + extension_numbers = tuple(sorted( + extension.number + for extension in self._pool.FindAllExtensions(message_descriptor))) + except KeyError: + return _not_found_error() + else: + return reflection_pb2.ServerReflectionResponse( + all_extension_numbers_response=reflection_pb2. + ExtensionNumberResponse( + base_type_name=message_descriptor.full_name, + extension_number=extension_numbers)) def _list_services(self): return reflection_pb2.ServerReflectionResponse( @@ -121,7 +131,7 @@ class ReflectionServicer(reflection_pb2.ServerReflectionServicer): request.file_containing_extension.containing_type, request.file_containing_extension.extension_number) elif request.HasField('all_extension_numbers_of_type'): - yield _all_extension_numbers_of_type( + yield self._all_extension_numbers_of_type( request.all_extension_numbers_of_type) elif request.HasField('list_services'): yield self._list_services() @@ -131,3 +141,14 @@ class ReflectionServicer(reflection_pb2.ServerReflectionServicer): error_code=grpc.StatusCode.INVALID_ARGUMENT.value[0], error_message=grpc.StatusCode.INVALID_ARGUMENT.value[1] .encode(),)) + + +def enable_server_reflection(service_names, server): + """Enables server reflection on a server. + + Args: + service_names: Iterable of fully-qualified service names available. + server: grpc.Server to which reflection service will be added. + """ + reflection_pb2_grpc.add_ServerReflectionServicer_to_server( + ReflectionServicer(service_names), server) diff --git a/src/python/grpcio_tests/tests/interop/_insecure_intraop_test.py b/src/python/grpcio_tests/tests/interop/_insecure_intraop_test.py index 58f3b364ba..3325d54375 100644 --- a/src/python/grpcio_tests/tests/interop/_insecure_intraop_test.py +++ b/src/python/grpcio_tests/tests/interop/_insecure_intraop_test.py @@ -32,7 +32,7 @@ from concurrent import futures import unittest import grpc -from src.proto.grpc.testing import test_pb2 +from src.proto.grpc.testing import test_pb2_grpc from tests.interop import _intraop_test_case from tests.interop import methods @@ -44,11 +44,11 @@ class InsecureIntraopTest(_intraop_test_case.IntraopTestCase, def setUp(self): self.server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) - test_pb2.add_TestServiceServicer_to_server(methods.TestService(), - self.server) + test_pb2_grpc.add_TestServiceServicer_to_server(methods.TestService(), + self.server) port = self.server.add_insecure_port('[::]:0') self.server.start() - self.stub = test_pb2.TestServiceStub( + self.stub = test_pb2_grpc.TestServiceStub( grpc.insecure_channel('localhost:{}'.format(port))) diff --git a/src/python/grpcio_tests/tests/interop/_secure_intraop_test.py b/src/python/grpcio_tests/tests/interop/_secure_intraop_test.py index 5fe929b99e..857e00efb3 100644 --- a/src/python/grpcio_tests/tests/interop/_secure_intraop_test.py +++ b/src/python/grpcio_tests/tests/interop/_secure_intraop_test.py @@ -32,7 +32,7 @@ from concurrent import futures import unittest import grpc -from src.proto.grpc.testing import test_pb2 +from src.proto.grpc.testing import test_pb2_grpc from tests.interop import _intraop_test_case from tests.interop import methods @@ -45,14 +45,14 @@ class SecureIntraopTest(_intraop_test_case.IntraopTestCase, unittest.TestCase): def setUp(self): self.server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) - test_pb2.add_TestServiceServicer_to_server(methods.TestService(), - self.server) + test_pb2_grpc.add_TestServiceServicer_to_server(methods.TestService(), + self.server) port = self.server.add_secure_port( '[::]:0', grpc.ssl_server_credentials( [(resources.private_key(), resources.certificate_chain())])) self.server.start() - self.stub = test_pb2.TestServiceStub( + self.stub = test_pb2_grpc.TestServiceStub( grpc.secure_channel('localhost:{}'.format(port), grpc.ssl_channel_credentials( resources.test_root_certificates()), ( diff --git a/src/python/grpcio_tests/tests/interop/methods.py b/src/python/grpcio_tests/tests/interop/methods.py index 662ea9ce57..e1016f7c0d 100644 --- a/src/python/grpcio_tests/tests/interop/methods.py +++ b/src/python/grpcio_tests/tests/interop/methods.py @@ -40,7 +40,7 @@ from grpc.beta import implementations from src.proto.grpc.testing import empty_pb2 from src.proto.grpc.testing import messages_pb2 -from src.proto.grpc.testing import test_pb2 +from src.proto.grpc.testing import test_pb2_grpc _INITIAL_METADATA_KEY = "x-grpc-test-echo-initial" _TRAILING_METADATA_KEY = "x-grpc-test-echo-trailing-bin" @@ -66,7 +66,7 @@ def _maybe_echo_status_and_message(request, servicer_context): servicer_context.set_details(request.response_status.message) -class TestService(test_pb2.TestServiceServicer): +class TestService(test_pb2_grpc.TestServiceServicer): def EmptyCall(self, request, context): _maybe_echo_metadata(context) diff --git a/src/python/grpcio_tests/tests/interop/server.py b/src/python/grpcio_tests/tests/interop/server.py index 65f1604eb8..0ae2c97b42 100644 --- a/src/python/grpcio_tests/tests/interop/server.py +++ b/src/python/grpcio_tests/tests/interop/server.py @@ -34,7 +34,7 @@ import logging import time import grpc -from src.proto.grpc.testing import test_pb2 +from src.proto.grpc.testing import test_pb2_grpc from tests.interop import methods from tests.interop import resources @@ -53,7 +53,8 @@ def serve(): args = parser.parse_args() server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) - test_pb2.add_TestServiceServicer_to_server(methods.TestService(), server) + test_pb2_grpc.add_TestServiceServicer_to_server(methods.TestService(), + server) if args.use_tls: private_key = resources.private_key() certificate_chain = resources.certificate_chain() diff --git a/src/python/grpcio_tests/tests/qps/qps_worker.py b/src/python/grpcio_tests/tests/qps/qps_worker.py index 025dfb9d4a..7cd53e7bd9 100644 --- a/src/python/grpcio_tests/tests/qps/qps_worker.py +++ b/src/python/grpcio_tests/tests/qps/qps_worker.py @@ -33,7 +33,7 @@ import time from concurrent import futures import grpc -from src.proto.grpc.testing import services_pb2 +from src.proto.grpc.testing import services_pb2_grpc from tests.qps import worker_server @@ -41,7 +41,7 @@ from tests.qps import worker_server def run_worker_server(port): server = grpc.server(futures.ThreadPoolExecutor(max_workers=5)) servicer = worker_server.WorkerServer() - services_pb2.add_WorkerServiceServicer_to_server(servicer, server) + services_pb2_grpc.add_WorkerServiceServicer_to_server(servicer, server) server.add_insecure_port('[::]:{}'.format(port)) server.start() servicer.wait_for_quit() diff --git a/src/python/grpcio_tests/tests/qps/worker_server.py b/src/python/grpcio_tests/tests/qps/worker_server.py index ca1a777611..de9535f46e 100644 --- a/src/python/grpcio_tests/tests/qps/worker_server.py +++ b/src/python/grpcio_tests/tests/qps/worker_server.py @@ -35,7 +35,7 @@ import time from concurrent import futures import grpc from src.proto.grpc.testing import control_pb2 -from src.proto.grpc.testing import services_pb2 +from src.proto.grpc.testing import services_pb2_grpc from src.proto.grpc.testing import stats_pb2 from tests.qps import benchmark_client @@ -45,7 +45,7 @@ from tests.qps import histogram from tests.unit import resources -class WorkerServer(services_pb2.WorkerServiceServicer): +class WorkerServer(services_pb2_grpc.WorkerServiceServicer): """Python Worker Server implementation.""" def __init__(self): @@ -87,8 +87,8 @@ class WorkerServer(services_pb2.WorkerServiceServicer): futures.ThreadPoolExecutor(max_workers=server_threads)) if config.server_type == control_pb2.ASYNC_SERVER: servicer = benchmark_server.BenchmarkServer() - services_pb2.add_BenchmarkServiceServicer_to_server(servicer, - server) + services_pb2_grpc.add_BenchmarkServiceServicer_to_server(servicer, + server) elif config.server_type == control_pb2.ASYNC_GENERIC_SERVER: resp_size = config.payload_config.bytebuf_params.resp_size servicer = benchmark_server.GenericBenchmarkServer(resp_size) diff --git a/src/python/grpcio_tests/tests/reflection/_reflection_servicer_test.py b/src/python/grpcio_tests/tests/reflection/_reflection_servicer_test.py index d06ff064e2..14e6d64c66 100644 --- a/src/python/grpcio_tests/tests/reflection/_reflection_servicer_test.py +++ b/src/python/grpcio_tests/tests/reflection/_reflection_servicer_test.py @@ -34,18 +34,24 @@ import grpc from grpc.framework.foundation import logging_pool from grpc_reflection.v1alpha import reflection from grpc_reflection.v1alpha import reflection_pb2 +from grpc_reflection.v1alpha import reflection_pb2_grpc from google.protobuf import descriptor_pool from google.protobuf import descriptor_pb2 -from src.proto.grpc.testing.proto2 import empty2_extensions_pb2 from src.proto.grpc.testing import empty_pb2 +#empty2_pb2 is imported for import-consequent side-effects. +from src.proto.grpc.testing.proto2 import empty2_pb2 # pylint: disable=unused-import +from src.proto.grpc.testing.proto2 import empty2_extensions_pb2 + from tests.unit.framework.common import test_constants _EMPTY_PROTO_FILE_NAME = 'src/proto/grpc/testing/empty.proto' _EMPTY_PROTO_SYMBOL_NAME = 'grpc.testing.Empty' _SERVICE_NAMES = ('Angstrom', 'Bohr', 'Curie', 'Dyson', 'Einstein', 'Feynman', 'Galilei') +_EMPTY_EXTENSIONS_SYMBOL_NAME = 'grpc.testing.proto2.EmptyWithExtensions' +_EMPTY_EXTENSIONS_NUMBERS = (124, 125, 126, 127, 128,) def _file_descriptor_to_proto(descriptor): @@ -61,12 +67,12 @@ class ReflectionServicerTest(unittest.TestCase): server_pool = logging_pool.pool(test_constants.THREAD_CONCURRENCY) self._server = grpc.server(server_pool) port = self._server.add_insecure_port('[::]:0') - reflection_pb2.add_ServerReflectionServicer_to_server(servicer, - self._server) + reflection_pb2_grpc.add_ServerReflectionServicer_to_server(servicer, + self._server) self._server.start() channel = grpc.insecure_channel('localhost:%d' % port) - self._stub = reflection_pb2.ServerReflectionStub(channel) + self._stub = reflection_pb2_grpc.ServerReflectionStub(channel) def testFileByName(self): requests = (reflection_pb2.ServerReflectionRequest( @@ -109,12 +115,12 @@ class ReflectionServicerTest(unittest.TestCase): self.assertSequenceEqual(expected_responses, responses) @unittest.skip( - 'TODO(atash): implement file-containing-extension reflection ' - '(see https://github.com/google/protobuf/issues/2248)') + 'TODO(mmx): enable when (pure) python protobuf issue is fixed' + '(see https://github.com/google/protobuf/issues/2882)') def testFileContainingExtension(self): requests = (reflection_pb2.ServerReflectionRequest( file_containing_extension=reflection_pb2.ExtensionRequest( - containing_type='grpc.testing.proto2.Empty', + containing_type=_EMPTY_EXTENSIONS_SYMBOL_NAME, extension_number=125,), ), reflection_pb2.ServerReflectionRequest( file_containing_extension=reflection_pb2.ExtensionRequest( @@ -126,7 +132,28 @@ class ReflectionServicerTest(unittest.TestCase): valid_host='', file_descriptor_response=reflection_pb2.FileDescriptorResponse( file_descriptor_proto=(_file_descriptor_to_proto( - empty_extensions_pb2.DESCRIPTOR),))), + empty2_extensions_pb2.DESCRIPTOR),))), + reflection_pb2.ServerReflectionResponse( + valid_host='', + error_response=reflection_pb2.ErrorResponse( + error_code=grpc.StatusCode.NOT_FOUND.value[0], + error_message=grpc.StatusCode.NOT_FOUND.value[1].encode(), + )),) + self.assertSequenceEqual(expected_responses, responses) + + def testExtensionNumbersOfType(self): + requests = (reflection_pb2.ServerReflectionRequest( + all_extension_numbers_of_type=_EMPTY_EXTENSIONS_SYMBOL_NAME + ), reflection_pb2.ServerReflectionRequest( + all_extension_numbers_of_type='i.donut.exist.co.uk.net.name.foo'),) + responses = tuple(self._stub.ServerReflectionInfo(iter(requests))) + expected_responses = ( + reflection_pb2.ServerReflectionResponse( + valid_host='', + all_extension_numbers_response=reflection_pb2. + ExtensionNumberResponse( + base_type_name=_EMPTY_EXTENSIONS_SYMBOL_NAME, + extension_number=_EMPTY_EXTENSIONS_NUMBERS)), reflection_pb2.ServerReflectionResponse( valid_host='', error_response=reflection_pb2.ErrorResponse( diff --git a/src/python/grpcio_tests/tests/stress/client.py b/src/python/grpcio_tests/tests/stress/client.py index b9dbe61d44..b7eb12bff8 100644 --- a/src/python/grpcio_tests/tests/stress/client.py +++ b/src/python/grpcio_tests/tests/stress/client.py @@ -34,7 +34,7 @@ import threading import grpc from six.moves import queue -from src.proto.grpc.testing import metrics_pb2 +from src.proto.grpc.testing import metrics_pb2_grpc from src.proto.grpc.testing import test_pb2 from tests.interop import methods @@ -139,7 +139,7 @@ def run_test(args): runners = [] server = grpc.server(futures.ThreadPoolExecutor(max_workers=25)) - metrics_pb2.add_MetricsServiceServicer_to_server( + metrics_pb2_grpc.add_MetricsServiceServicer_to_server( metrics_server.MetricsServer(hist), server) server.add_insecure_port('[::]:{}'.format(args.metrics_port)) server.start() |