Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 1 | # |
Zsolt Haraszti | accad4a | 2017-01-03 21:56:48 -0800 | [diff] [blame] | 2 | # Copyright 2017 the original author or authors. |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | # |
| 16 | |
| 17 | """ |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 18 | gRPC client meant to connect to a gRPC server endpoint, and query the |
Zsolt Haraszti | cba96de | 2016-11-06 14:04:55 -0800 | [diff] [blame] | 19 | end-point's schema by calling SchemaService.Schema(Empty) and all of its |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 20 | semantics are derived from the recovered schema. |
| 21 | """ |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 22 | |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 23 | import os |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 24 | import sys |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 25 | from random import randint |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 26 | from zlib import decompress |
| 27 | |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 28 | import grpc |
| 29 | from consul import Consul |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 30 | from grpc._channel import _Rendezvous |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 31 | from structlog import get_logger |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 32 | from twisted.internet import reactor |
Zsolt Haraszti | 2aac623 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 33 | from twisted.internet.defer import inlineCallbacks, returnValue |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 34 | from werkzeug.exceptions import ServiceUnavailable |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 35 | |
Zsolt Haraszti | 2198076 | 2016-11-08 10:57:19 -0800 | [diff] [blame] | 36 | from chameleon.protos import third_party |
Zsolt Haraszti | cba96de | 2016-11-06 14:04:55 -0800 | [diff] [blame] | 37 | from chameleon.protos.schema_pb2 import SchemaServiceStub |
| 38 | from google.protobuf.empty_pb2 import Empty |
| 39 | |
alshabib | 6ca0562 | 2017-01-31 14:08:36 -0800 | [diff] [blame] | 40 | from chameleon.utils.asleep import asleep |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 41 | |
| 42 | log = get_logger() |
| 43 | |
| 44 | |
| 45 | class GrpcClient(object): |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 46 | """ |
| 47 | Connect to a gRPC server, fetch its schema, and process the downloaded |
| 48 | schema files to drive the customization of the north-bound interface(s) |
| 49 | of Chameleon. |
| 50 | """ |
| 51 | RETRY_BACKOFF = [0.05, 0.1, 0.2, 0.5, 1, 2, 5] |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 52 | |
Zsolt Haraszti | dca6fa1 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 53 | def __init__(self, consul_endpoint, work_dir, endpoint='localhost:50055', |
Scott Baker | 74548db | 2017-02-15 14:16:13 -0800 | [diff] [blame] | 54 | reconnect_callback=None, credentials=None): |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 55 | self.consul_endpoint = consul_endpoint |
| 56 | self.endpoint = endpoint |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 57 | self.work_dir = work_dir |
Zsolt Haraszti | dca6fa1 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 58 | self.reconnect_callback = reconnect_callback |
Scott Baker | 74548db | 2017-02-15 14:16:13 -0800 | [diff] [blame] | 59 | self.credentials = credentials |
Zsolt Haraszti | dca6fa1 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 60 | |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 61 | self.plugin_dir = os.path.abspath(os.path.join( |
| 62 | os.path.dirname(__file__), '../protoc_plugins')) |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 63 | |
| 64 | self.channel = None |
| 65 | self.schema = None |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 66 | self.retries = 0 |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 67 | self.shutting_down = False |
Zsolt Haraszti | 3cf3634 | 2016-10-05 20:40:19 -0700 | [diff] [blame] | 68 | self.connected = False |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 69 | |
Zsolt Haraszti | dca6fa1 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 70 | def start(self): |
| 71 | log.debug('starting') |
Zsolt Haraszti | 3cf3634 | 2016-10-05 20:40:19 -0700 | [diff] [blame] | 72 | if not self.connected: |
| 73 | reactor.callLater(0, self.connect) |
Zsolt Haraszti | dca6fa1 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 74 | log.info('started') |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 75 | return self |
| 76 | |
Zsolt Haraszti | dca6fa1 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 77 | def stop(self): |
| 78 | log.debug('stopping') |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 79 | if self.shutting_down: |
| 80 | return |
| 81 | self.shutting_down = True |
Zsolt Haraszti | dca6fa1 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 82 | log.info('stopped') |
| 83 | |
| 84 | def set_reconnect_callback(self, reconnect_callback): |
| 85 | self.reconnect_callback = reconnect_callback |
| 86 | return self |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 87 | |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 88 | @inlineCallbacks |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 89 | def connect(self): |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 90 | """ |
| 91 | (Re-)Connect to end-point |
| 92 | """ |
| 93 | |
Zsolt Haraszti | 3cf3634 | 2016-10-05 20:40:19 -0700 | [diff] [blame] | 94 | if self.shutting_down or self.connected: |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 95 | return |
| 96 | |
| 97 | try: |
| 98 | if self.endpoint.startswith('@'): |
Zsolt Haraszti | 267a906 | 2016-12-26 23:11:15 -0800 | [diff] [blame] | 99 | _endpoint = yield self._get_endpoint_from_consul( |
| 100 | self.endpoint[1:]) |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 101 | else: |
| 102 | _endpoint = self.endpoint |
| 103 | |
Scott Baker | 74548db | 2017-02-15 14:16:13 -0800 | [diff] [blame] | 104 | if self.credentials: |
| 105 | log.info('securely connecting', endpoint=_endpoint) |
| 106 | self.channel = grpc.secure_channel(_endpoint, self.credentials) |
| 107 | else: |
| 108 | log.info('insecurely connecting', endpoint=_endpoint) |
| 109 | self.channel = grpc.insecure_channel(_endpoint) |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 110 | |
Zsolt Haraszti | 2198076 | 2016-11-08 10:57:19 -0800 | [diff] [blame] | 111 | swagger_from = self._retrieve_schema() |
| 112 | self._compile_proto_files(swagger_from) |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 113 | self._clear_backoff() |
| 114 | |
Zsolt Haraszti | 3cf3634 | 2016-10-05 20:40:19 -0700 | [diff] [blame] | 115 | self.connected = True |
Zsolt Haraszti | dca6fa1 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 116 | if self.reconnect_callback is not None: |
| 117 | reactor.callLater(0, self.reconnect_callback) |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 118 | |
| 119 | return |
| 120 | |
| 121 | except _Rendezvous, e: |
| 122 | if e.code() == grpc.StatusCode.UNAVAILABLE: |
| 123 | log.info('grpc-endpoint-not-available') |
| 124 | else: |
| 125 | log.exception(e) |
| 126 | yield self._backoff('not-available') |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 127 | |
| 128 | except Exception, e: |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 129 | if not self.shutting_down: |
| 130 | log.exception('cannot-connect', endpoint=_endpoint) |
| 131 | yield self._backoff('unknown-error') |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 132 | |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 133 | reactor.callLater(0, self.connect) |
| 134 | |
| 135 | def _backoff(self, msg): |
| 136 | wait_time = self.RETRY_BACKOFF[min(self.retries, |
| 137 | len(self.RETRY_BACKOFF) - 1)] |
| 138 | self.retries += 1 |
| 139 | log.error(msg, retry_in=wait_time) |
| 140 | return asleep(wait_time) |
| 141 | |
| 142 | def _clear_backoff(self): |
| 143 | if self.retries: |
| 144 | log.info('reconnected', after_retries=self.retries) |
| 145 | self.retries = 0 |
| 146 | |
Zsolt Haraszti | 267a906 | 2016-12-26 23:11:15 -0800 | [diff] [blame] | 147 | @inlineCallbacks |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 148 | def _get_endpoint_from_consul(self, service_name): |
| 149 | """ |
| 150 | Look up an appropriate grpc endpoint (host, port) from |
| 151 | consul, under the service name specified by service-name |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 152 | """ |
| 153 | host = self.consul_endpoint.split(':')[0].strip() |
| 154 | port = int(self.consul_endpoint.split(':')[1].strip()) |
| 155 | |
Zsolt Haraszti | 267a906 | 2016-12-26 23:11:15 -0800 | [diff] [blame] | 156 | while True: |
| 157 | log.debug('consul-lookup', host=host, port=port) |
| 158 | consul = Consul(host=host, port=port) |
| 159 | _, services = consul.catalog.service(service_name) |
| 160 | log.debug('consul-response', services=services) |
| 161 | if services: |
| 162 | break |
| 163 | log.warning('no-service', consul_host=host, consul_port=port, |
| 164 | service_name=service_name) |
| 165 | yield asleep(1.0) |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 166 | |
alshabib | c822490 | 2017-01-26 11:59:52 -0800 | [diff] [blame] | 167 | # pick local addresses when resolving a service via consul |
| 168 | # see CORD-815 (https://jira.opencord.org/browse/CORD-815) |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 169 | |
| 170 | service = services[randint(0, len(services) - 1)] |
| 171 | endpoint = '{}:{}'.format(service['ServiceAddress'], |
| 172 | service['ServicePort']) |
Zsolt Haraszti | 267a906 | 2016-12-26 23:11:15 -0800 | [diff] [blame] | 173 | returnValue(endpoint) |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 174 | |
| 175 | def _retrieve_schema(self): |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 176 | """ |
| 177 | Retrieve schema from gRPC end-point, and save all *.proto files in |
| 178 | the work directory. |
| 179 | """ |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 180 | assert isinstance(self.channel, grpc.Channel) |
| 181 | stub = SchemaServiceStub(self.channel) |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 182 | # try: |
Zsolt Haraszti | cba96de | 2016-11-06 14:04:55 -0800 | [diff] [blame] | 183 | schemas = stub.GetSchema(Empty()) |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 184 | # except _Rendezvous, e: |
| 185 | # if e.code == grpc.StatusCode.UNAVAILABLE: |
| 186 | # |
| 187 | # else: |
| 188 | # raise e |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 189 | |
| 190 | os.system('mkdir -p %s' % self.work_dir) |
| 191 | os.system('rm -fr /tmp/%s/*' % |
| 192 | self.work_dir.replace('/tmp/', '')) # safer |
| 193 | |
Zsolt Haraszti | dca6fa1 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 194 | for proto_file in schemas.protos: |
| 195 | proto_fname = proto_file.file_name |
| 196 | proto_content = proto_file.proto |
| 197 | log.debug('saving-proto', fname=proto_fname, dir=self.work_dir, |
| 198 | length=len(proto_content)) |
| 199 | with open(os.path.join(self.work_dir, proto_fname), 'w') as f: |
| 200 | f.write(proto_content) |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 201 | |
Zsolt Haraszti | dca6fa1 | 2016-11-03 16:56:17 -0700 | [diff] [blame] | 202 | desc_content = decompress(proto_file.descriptor) |
| 203 | desc_fname = proto_fname.replace('.proto', '.desc') |
| 204 | log.debug('saving-descriptor', fname=desc_fname, dir=self.work_dir, |
| 205 | length=len(desc_content)) |
| 206 | with open(os.path.join(self.work_dir, desc_fname), 'wb') as f: |
| 207 | f.write(desc_content) |
Zsolt Haraszti | 2198076 | 2016-11-08 10:57:19 -0800 | [diff] [blame] | 208 | return schemas.swagger_from |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 209 | |
Zsolt Haraszti | 2198076 | 2016-11-08 10:57:19 -0800 | [diff] [blame] | 210 | def _compile_proto_files(self, swagger_from): |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 211 | """ |
| 212 | For each *.proto file in the work directory, compile the proto |
| 213 | file into the respective *_pb2.py file as well as generate the |
| 214 | web server gateway python file *_gw.py. |
| 215 | :return: None |
| 216 | """ |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 217 | google_api_dir = os.path.abspath(os.path.join( |
Zsolt Haraszti | 46c7200 | 2016-10-10 09:55:30 -0700 | [diff] [blame] | 218 | os.path.dirname(__file__), '../protos/third_party' |
| 219 | )) |
| 220 | |
| 221 | chameleon_base_dir = os.path.abspath(os.path.join( |
| 222 | os.path.dirname(__file__), '../..' |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 223 | )) |
| 224 | |
| 225 | for fname in [f for f in os.listdir(self.work_dir) |
| 226 | if f.endswith('.proto')]: |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 227 | |
Zsolt Haraszti | 2198076 | 2016-11-08 10:57:19 -0800 | [diff] [blame] | 228 | need_swagger = fname == swagger_from |
| 229 | log.debug('compiling', file=fname, need_swagger=need_swagger) |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 230 | cmd = ( |
| 231 | 'cd %s && ' |
Zsolt Haraszti | 05b837a | 2016-10-05 00:18:57 -0700 | [diff] [blame] | 232 | 'env PATH=%s PYTHONPATH=%s ' |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 233 | 'python -m grpc.tools.protoc ' |
| 234 | '-I. ' |
| 235 | '-I%s ' |
| 236 | '--python_out=. ' |
| 237 | '--grpc_python_out=. ' |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 238 | '--plugin=protoc-gen-gw=%s/gw_gen.py ' |
| 239 | '--gw_out=. ' |
Zsolt Haraszti | 46c7200 | 2016-10-10 09:55:30 -0700 | [diff] [blame] | 240 | '--plugin=protoc-gen-swagger=%s/swagger_gen.py ' |
Zsolt Haraszti | 2198076 | 2016-11-08 10:57:19 -0800 | [diff] [blame] | 241 | '%s' |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 242 | '%s' % ( |
| 243 | self.work_dir, |
| 244 | ':'.join([os.environ['PATH'], self.plugin_dir]), |
Zsolt Haraszti | 46c7200 | 2016-10-10 09:55:30 -0700 | [diff] [blame] | 245 | ':'.join([google_api_dir, chameleon_base_dir]), |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 246 | google_api_dir, |
Zsolt Haraszti | 46c7200 | 2016-10-10 09:55:30 -0700 | [diff] [blame] | 247 | self.plugin_dir, |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 248 | self.plugin_dir, |
Zsolt Haraszti | 2198076 | 2016-11-08 10:57:19 -0800 | [diff] [blame] | 249 | '--swagger_out=. ' if need_swagger else '', |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 250 | fname) |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 251 | ) |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 252 | log.debug('executing', cmd=cmd, file=fname) |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 253 | os.system(cmd) |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 254 | log.info('compiled', file=fname) |
Zsolt Haraszti | a9a12dc | 2016-09-27 13:48:35 -0700 | [diff] [blame] | 255 | |
| 256 | # test-load each _pb2 file to see all is right |
| 257 | if self.work_dir not in sys.path: |
| 258 | sys.path.insert(0, self.work_dir) |
| 259 | |
| 260 | for fname in [f for f in os.listdir(self.work_dir) |
| 261 | if f.endswith('_pb2.py')]: |
| 262 | modname = fname[:-len('.py')] |
| 263 | log.debug('test-import', modname=modname) |
| 264 | _ = __import__(modname) |
Zsolt Haraszti | 3d55ffc | 2016-10-03 22:26:41 -0700 | [diff] [blame] | 265 | |
Zsolt Haraszti | 2aac623 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 266 | @inlineCallbacks |
Zsolt Haraszti | c8cfdf3 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 267 | def invoke(self, stub, method_name, request, metadata, retry=1): |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 268 | """ |
| 269 | Invoke a gRPC call to the remote server and return the response. |
| 270 | :param stub: Reference to the *_pb2 service stub |
| 271 | :param method_name: The method name inside the service stub |
| 272 | :param request: The request protobuf message |
Zsolt Haraszti | c8cfdf3 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 273 | :param metadata: [(str, str), (str, str), ...] |
| 274 | :return: The response protobuf message and returned trailing metadata |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 275 | """ |
| 276 | |
Zsolt Haraszti | 3cf3634 | 2016-10-05 20:40:19 -0700 | [diff] [blame] | 277 | if not self.connected: |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 278 | raise ServiceUnavailable() |
| 279 | |
| 280 | try: |
Zsolt Haraszti | c8cfdf3 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 281 | method = getattr(stub(self.channel), method_name) |
| 282 | response, rendezvous = method.with_call(request, metadata=metadata) |
| 283 | returnValue((response, rendezvous.trailing_metadata())) |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 284 | |
| 285 | except grpc._channel._Rendezvous, e: |
Zsolt Haraszti | c8cfdf3 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 286 | code = e.code() |
| 287 | if code == grpc.StatusCode.UNAVAILABLE: |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 288 | e = ServiceUnavailable() |
Zsolt Haraszti | 2aac623 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 289 | |
| 290 | if self.connected: |
| 291 | self.connected = False |
| 292 | yield self.connect() |
| 293 | if retry > 0: |
| 294 | response = yield self.invoke(stub, method_name, |
Zsolt Haraszti | c8cfdf3 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 295 | request, metadata, |
Zsolt Haraszti | 2aac623 | 2016-11-23 11:18:23 -0800 | [diff] [blame] | 296 | retry=retry - 1) |
| 297 | returnValue(response) |
| 298 | |
Zsolt Haraszti | c8cfdf3 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 299 | elif code in ( |
| 300 | grpc.StatusCode.NOT_FOUND, |
| 301 | grpc.StatusCode.INVALID_ARGUMENT, |
Matteo Scandolo | 92f7f1f | 2017-04-28 15:56:29 -0700 | [diff] [blame^] | 302 | grpc.StatusCode.ALREADY_EXISTS, |
| 303 | grpc.StatusCode.UNAUTHENTICATED, |
| 304 | grpc.StatusCode.PERMISSION_DENIED): |
Zsolt Haraszti | c8cfdf3 | 2016-11-28 14:28:39 -0800 | [diff] [blame] | 305 | |
| 306 | pass # don't log error, these occur naturally |
| 307 | |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 308 | else: |
| 309 | log.exception(e) |
| 310 | |
Zsolt Haraszti | e7b6076 | 2016-10-05 17:49:27 -0700 | [diff] [blame] | 311 | raise e |