From 43599facf4c8b42b8a14d0601556f4231d9d10f8 Mon Sep 17 00:00:00 2001 From: Lidi Zheng Date: Tue, 20 Nov 2018 16:57:08 -0800 Subject: Channelz Python wrapper implementation * Expose the C-Core API in Cython layer * Handle the object translation * Create a separate package for Channelz specifically * Handle nullptr and raise exception if seen one * Translate C++ Channelz unit tests * Adding 5 more invalid query unit tests Adding peripheral utility for grpcio-channelz package * Add to `pylint_code.sh` * Add to Python build script * Add to artifact build script * Add to Bazel * Add to Sphinx module list --- tools/distrib/pylint_code.sh | 1 + tools/run_tests/artifacts/build_artifact_python.sh | 5 +++++ tools/run_tests/helper_scripts/build_python.sh | 5 +++++ 3 files changed, 11 insertions(+) (limited to 'tools') diff --git a/tools/distrib/pylint_code.sh b/tools/distrib/pylint_code.sh index 82a818cce5..d17eb9fdb8 100755 --- a/tools/distrib/pylint_code.sh +++ b/tools/distrib/pylint_code.sh @@ -20,6 +20,7 @@ cd "$(dirname "$0")/../.." DIRS=( 'src/python/grpcio/grpc' + 'src/python/grpcio_channelz/grpc_channelz' 'src/python/grpcio_health_checking/grpc_health' 'src/python/grpcio_reflection/grpc_reflection' 'src/python/grpcio_testing/grpc_testing' diff --git a/tools/run_tests/artifacts/build_artifact_python.sh b/tools/run_tests/artifacts/build_artifact_python.sh index 65f2d1c765..bc6e558204 100755 --- a/tools/run_tests/artifacts/build_artifact_python.sh +++ b/tools/run_tests/artifacts/build_artifact_python.sh @@ -108,6 +108,11 @@ then ${SETARCH_CMD} "${PYTHON}" src/python/grpcio_testing/setup.py sdist cp -r src/python/grpcio_testing/dist/* "$ARTIFACT_DIR" + # Build grpcio_channelz source distribution + ${SETARCH_CMD} "${PYTHON}" src/python/grpcio_channelz/setup.py \ + preprocess build_package_protos sdist + cp -r src/python/grpcio_channelz/dist/* "$ARTIFACT_DIR" + # Build grpcio_health_checking source distribution ${SETARCH_CMD} "${PYTHON}" src/python/grpcio_health_checking/setup.py \ preprocess build_package_protos sdist diff --git a/tools/run_tests/helper_scripts/build_python.sh b/tools/run_tests/helper_scripts/build_python.sh index 8394f07e51..27acd9664b 100755 --- a/tools/run_tests/helper_scripts/build_python.sh +++ b/tools/run_tests/helper_scripts/build_python.sh @@ -189,6 +189,11 @@ pip_install_dir "$ROOT" $VENV_PYTHON "$ROOT/tools/distrib/python/make_grpcio_tools.py" pip_install_dir "$ROOT/tools/distrib/python/grpcio_tools" +# Build/install Chaneelz +$VENV_PYTHON "$ROOT/src/python/grpcio_channelz/setup.py" preprocess +$VENV_PYTHON "$ROOT/src/python/grpcio_channelz/setup.py" build_package_protos +pip_install_dir "$ROOT/src/python/grpcio_channelz" + # Build/install health checking $VENV_PYTHON "$ROOT/src/python/grpcio_health_checking/setup.py" preprocess $VENV_PYTHON "$ROOT/src/python/grpcio_health_checking/setup.py" build_package_protos -- cgit v1.2.3 From 53476eced45e9a8a4dd57b9519f42328d763f907 Mon Sep 17 00:00:00 2001 From: Lidi Zheng Date: Wed, 28 Nov 2018 10:30:39 -0800 Subject: Adopt reviewer's suggestions * Correct the StatusCode * Format code * Use @staticmethod * Fix typo --- .../grpcio/grpc/_cython/_cygrpc/channelz.pyx.pxi | 31 +++++++---- .../grpcio_channelz/grpc_channelz/v1/channelz.py | 61 ++++++++++++++-------- .../tests/channelz/_channelz_servicer_test.py | 10 ++-- tools/run_tests/helper_scripts/build_python.sh | 2 +- 4 files changed, 66 insertions(+), 38 deletions(-) (limited to 'tools') diff --git a/src/python/grpcio/grpc/_cython/_cygrpc/channelz.pyx.pxi b/src/python/grpcio/grpc/_cython/_cygrpc/channelz.pyx.pxi index 572a473d3e..113f7976dd 100644 --- a/src/python/grpcio/grpc/_cython/_cygrpc/channelz.pyx.pxi +++ b/src/python/grpcio/grpc/_cython/_cygrpc/channelz.pyx.pxi @@ -14,43 +14,56 @@ def channelz_get_top_channels(start_channel_id): - cdef char *c_returned_str = grpc_channelz_get_top_channels(start_channel_id) + cdef char *c_returned_str = grpc_channelz_get_top_channels( + start_channel_id, + ) if c_returned_str == NULL: - raise ValueError('Failed to get top channels, please ensure your start_channel_id==%s is valid' % start_channel_id) + raise ValueError('Failed to get top channels, please ensure your' \ + ' start_channel_id==%s is valid' % start_channel_id) return c_returned_str def channelz_get_servers(start_server_id): cdef char *c_returned_str = grpc_channelz_get_servers(start_server_id) if c_returned_str == NULL: - raise ValueError('Failed to get servers, please ensure your start_server_id==%s is valid' % start_server_id) + raise ValueError('Failed to get servers, please ensure your' \ + ' start_server_id==%s is valid' % start_server_id) return c_returned_str def channelz_get_server(server_id): cdef char *c_returned_str = grpc_channelz_get_server(server_id) if c_returned_str == NULL: - raise ValueError('Failed to get the server, please ensure your server_id==%s is valid' % server_id) + raise ValueError('Failed to get the server, please ensure your' \ + ' server_id==%s is valid' % server_id) return c_returned_str def channelz_get_server_sockets(server_id, start_socket_id): - cdef char *c_returned_str = grpc_channelz_get_server_sockets(server_id, start_socket_id) + cdef char *c_returned_str = grpc_channelz_get_server_sockets( + server_id, + start_socket_id, + ) if c_returned_str == NULL: - raise ValueError('Failed to get server sockets, please ensure your server_id==%s and start_socket_id==%s is valid' % (server_id, start_socket_id)) + raise ValueError('Failed to get server sockets, please ensure your' \ + ' server_id==%s and start_socket_id==%s is valid' % + (server_id, start_socket_id)) return c_returned_str def channelz_get_channel(channel_id): cdef char *c_returned_str = grpc_channelz_get_channel(channel_id) if c_returned_str == NULL: - raise ValueError('Failed to get the channel, please ensure your channel_id==%s is valid' % (channel_id)) + raise ValueError('Failed to get the channel, please ensure your' \ + ' channel_id==%s is valid' % (channel_id)) return c_returned_str def channelz_get_subchannel(subchannel_id): cdef char *c_returned_str = grpc_channelz_get_subchannel(subchannel_id) if c_returned_str == NULL: - raise ValueError('Failed to get the subchannel, please ensure your subchannel_id==%s is valid' % (subchannel_id)) + raise ValueError('Failed to get the subchannel, please ensure your' \ + ' subchannel_id==%s is valid' % (subchannel_id)) return c_returned_str def channelz_get_socket(socket_id): cdef char *c_returned_str = grpc_channelz_get_socket(socket_id) if c_returned_str == NULL: - raise ValueError('Failed to get the socket, please ensure your socket_id==%s is valid' % (socket_id)) + raise ValueError('Failed to get the socket, please ensure your' \ + ' socket_id==%s is valid' % (socket_id)) return c_returned_str diff --git a/src/python/grpcio_channelz/grpc_channelz/v1/channelz.py b/src/python/grpcio_channelz/grpc_channelz/v1/channelz.py index bc08562b7b..1ae5e8c140 100644 --- a/src/python/grpcio_channelz/grpc_channelz/v1/channelz.py +++ b/src/python/grpcio_channelz/grpc_channelz/v1/channelz.py @@ -25,41 +25,44 @@ from google.protobuf import json_format class ChannelzServicer(_channelz_pb2_grpc.ChannelzServicer): """Servicer handling RPCs for service statuses.""" - # pylint: disable=no-self-use - def GetTopChannels(self, request, context): + @staticmethod + def GetTopChannels(request, context): try: return json_format.Parse( cygrpc.channelz_get_top_channels(request.start_channel_id), _channelz_pb2.GetTopChannelsResponse(), ) - except ValueError as e: - context.set_code(grpc.StatusCode.INVALID_ARGUMENT) + except (ValueError, json_format.ParseError) as e: + context.set_code(grpc.StatusCode.INTERNAL) context.set_details(str(e)) - # pylint: disable=no-self-use - def GetServers(self, request, context): + @staticmethod + def GetServers(request, context): try: return json_format.Parse( cygrpc.channelz_get_servers(request.start_server_id), _channelz_pb2.GetServersResponse(), ) - except ValueError as e: - context.set_code(grpc.StatusCode.INVALID_ARGUMENT) + except (ValueError, json_format.ParseError) as e: + context.set_code(grpc.StatusCode.INTERNAL) context.set_details(str(e)) - # pylint: disable=no-self-use - def GetServer(self, request, context): + @staticmethod + def GetServer(request, context): try: return json_format.Parse( cygrpc.channelz_get_server(request.server_id), _channelz_pb2.GetServerResponse(), ) except ValueError as e: - context.set_code(grpc.StatusCode.INVALID_ARGUMENT) + context.set_code(grpc.StatusCode.NOT_FOUND) + context.set_details(str(e)) + except json_format.ParseError as e: + context.set_code(grpc.StatusCode.INTERNAL) context.set_details(str(e)) - # pylint: disable=no-self-use - def GetServerSockets(self, request, context): + @staticmethod + def GetServerSockets(request, context): try: return json_format.Parse( cygrpc.channelz_get_server_sockets(request.server_id, @@ -67,40 +70,52 @@ class ChannelzServicer(_channelz_pb2_grpc.ChannelzServicer): _channelz_pb2.GetServerSocketsResponse(), ) except ValueError as e: - context.set_code(grpc.StatusCode.INVALID_ARGUMENT) + context.set_code(grpc.StatusCode.NOT_FOUND) + context.set_details(str(e)) + except json_format.ParseError as e: + context.set_code(grpc.StatusCode.INTERNAL) context.set_details(str(e)) - # pylint: disable=no-self-use - def GetChannel(self, request, context): + @staticmethod + def GetChannel(request, context): try: return json_format.Parse( cygrpc.channelz_get_channel(request.channel_id), _channelz_pb2.GetChannelResponse(), ) except ValueError as e: - context.set_code(grpc.StatusCode.INVALID_ARGUMENT) + context.set_code(grpc.StatusCode.NOT_FOUND) + context.set_details(str(e)) + except json_format.ParseError as e: + context.set_code(grpc.StatusCode.INTERNAL) context.set_details(str(e)) - # pylint: disable=no-self-use - def GetSubchannel(self, request, context): + @staticmethod + def GetSubchannel(request, context): try: return json_format.Parse( cygrpc.channelz_get_subchannel(request.subchannel_id), _channelz_pb2.GetSubchannelResponse(), ) except ValueError as e: - context.set_code(grpc.StatusCode.INVALID_ARGUMENT) + context.set_code(grpc.StatusCode.NOT_FOUND) + context.set_details(str(e)) + except json_format.ParseError as e: + context.set_code(grpc.StatusCode.INTERNAL) context.set_details(str(e)) - # pylint: disable=no-self-use - def GetSocket(self, request, context): + @staticmethod + def GetSocket(request, context): try: return json_format.Parse( cygrpc.channelz_get_socket(request.socket_id), _channelz_pb2.GetSocketResponse(), ) except ValueError as e: - context.set_code(grpc.StatusCode.INVALID_ARGUMENT) + context.set_code(grpc.StatusCode.NOT_FOUND) + context.set_details(str(e)) + except json_format.ParseError as e: + context.set_code(grpc.StatusCode.INTERNAL) context.set_details(str(e)) diff --git a/src/python/grpcio_tests/tests/channelz/_channelz_servicer_test.py b/src/python/grpcio_tests/tests/channelz/_channelz_servicer_test.py index 103609441e..2549ed76be 100644 --- a/src/python/grpcio_tests/tests/channelz/_channelz_servicer_test.py +++ b/src/python/grpcio_tests/tests/channelz/_channelz_servicer_test.py @@ -428,7 +428,7 @@ class ChannelzServicerTest(unittest.TestCase): self._channelz_stub.GetServer( channelz_pb2.GetServerRequest(server_id=10000)) except BaseException as e: - self.assertIn('StatusCode.INVALID_ARGUMENT', str(e)) + self.assertIn('StatusCode.NOT_FOUND', str(e)) else: self.fail('Invalid query not detected') @@ -437,7 +437,7 @@ class ChannelzServicerTest(unittest.TestCase): self._channelz_stub.GetChannel( channelz_pb2.GetChannelRequest(channel_id=10000)) except BaseException as e: - self.assertIn('StatusCode.INVALID_ARGUMENT', str(e)) + self.assertIn('StatusCode.NOT_FOUND', str(e)) else: self.fail('Invalid query not detected') @@ -446,7 +446,7 @@ class ChannelzServicerTest(unittest.TestCase): self._channelz_stub.GetSubchannel( channelz_pb2.GetSubchannelRequest(subchannel_id=10000)) except BaseException as e: - self.assertIn('StatusCode.INVALID_ARGUMENT', str(e)) + self.assertIn('StatusCode.NOT_FOUND', str(e)) else: self.fail('Invalid query not detected') @@ -455,7 +455,7 @@ class ChannelzServicerTest(unittest.TestCase): self._channelz_stub.GetSocket( channelz_pb2.GetSocketRequest(socket_id=10000)) except BaseException as e: - self.assertIn('StatusCode.INVALID_ARGUMENT', str(e)) + self.assertIn('StatusCode.NOT_FOUND', str(e)) else: self.fail('Invalid query not detected') @@ -467,7 +467,7 @@ class ChannelzServicerTest(unittest.TestCase): start_socket_id=0, )) except BaseException as e: - self.assertIn('StatusCode.INVALID_ARGUMENT', str(e)) + self.assertIn('StatusCode.NOT_FOUND', str(e)) else: self.fail('Invalid query not detected') diff --git a/tools/run_tests/helper_scripts/build_python.sh b/tools/run_tests/helper_scripts/build_python.sh index 27acd9664b..e43f1fb429 100755 --- a/tools/run_tests/helper_scripts/build_python.sh +++ b/tools/run_tests/helper_scripts/build_python.sh @@ -189,7 +189,7 @@ pip_install_dir "$ROOT" $VENV_PYTHON "$ROOT/tools/distrib/python/make_grpcio_tools.py" pip_install_dir "$ROOT/tools/distrib/python/grpcio_tools" -# Build/install Chaneelz +# Build/install Channelz $VENV_PYTHON "$ROOT/src/python/grpcio_channelz/setup.py" preprocess $VENV_PYTHON "$ROOT/src/python/grpcio_channelz/setup.py" build_package_protos pip_install_dir "$ROOT/src/python/grpcio_channelz" -- cgit v1.2.3