def StartTraining( self, request: coordinator_pb2.StartTrainingRequest, context: grpc.ServicerContext, ) -> coordinator_pb2.StartTrainingReply: """The StartTraining gRPC method. Once a participant is notified that the :class:`xain_fl.coordinator.coordinator.Coordinator` is in a round (through the state advertised in the :class:`~.coordinator_pb2.HeartbeatReply`), the participant should call this method in order to get the global model weights in order to start the training for the round. Args: request (:class:`~.coordinator_pb2.StartTrainingRequest`): The participant's request. context (:class:`~grpc.ServicerContext`): The context associated with the gRPC request. Returns: :class:`~.coordinator_pb2.StartTrainingReply`: The reply to the participant's request. The reply contains the global model weights. """ try: return self.coordinator.on_message(request, context.peer()) except UnknownParticipantError as error: context.set_details(str(error)) context.set_code(grpc.StatusCode.PERMISSION_DENIED) return coordinator_pb2.StartTrainingReply() except InvalidRequestError as error: context.set_details(str(error)) context.set_Code(grpc.StatusCode.FAILED_PRECONDITION) return coordinator_pb2.StartTrainingReply()
def Heartbeat( self, request: coordinator_pb2.HeartbeatRequest, context: grpc.ServicerContext) -> coordinator_pb2.HeartbeatReply: """The Heartbeat gRPC method. Participants periodically send an heartbeat so that the :class:`Coordinator` can detect failures. Args: request (:class:`~.coordinator_pb2.HeartbeatRequest`): The participant's request. The participant's request contains the current :class:`~.coordinator_pb2.State` and round number the participant is on. context (:class:`~grpc.ServicerContext`): The context associated with the gRPC request. Returns: :class:`~.coordinator_pb2.HeartbeatReply`: The reply to the participant's request. The reply contains both the :class:`~.coordinator_pb2.State` and the current round the coordinator is on. If a training session has not started yet the round number defaults to 0. """ try: return self.coordinator.on_message(request, context.peer()) except UnknownParticipantError as error: context.set_details(str(error)) context.set_code(grpc.StatusCode.PERMISSION_DENIED) return coordinator_pb2.HeartbeatReply()
def EndTraining( self, request: coordinator_pb2.EndTrainingRequest, context: grpc.ServicerContext) -> coordinator_pb2.EndTrainingReply: """The EndTraining gRPC method. Once a participant has finished the training for the round it calls this method in order to submit to the :class:`xain_fl.coordinator.coordinator.Coordinator` the updated weights. Args: request (:class:`~.coordinator_pb2.EndTrainingRequest`): The participant's request. The request contains the updated weights as a result of the training as well as any metrics helpful for the :class:`xain_fl.coordinator.coordinator.Coordinator`. context (:class:`~grpc.ServicerContext`): The context associated with the gRPC request. Returns: :class:`~.coordinator_pb2.EndTrainingReply`: The reply to the participant's request. The reply is just an acknowledgment that the :class:`xain_fl.coordinator.coordinator.Coordinator` successfully received the updated weights. """ try: return self.coordinator.on_message(request, context.peer()) except DuplicatedUpdateError as error: context.set_details(str(error)) context.set_code(grpc.StatusCode.ALREADY_EXISTS) return coordinator_pb2.EndTrainingReply() except UnknownParticipantError as error: context.set_details(str(error)) context.set_code(grpc.StatusCode.PERMISSION_DENIED) return coordinator_pb2.EndTrainingReply()
def GetDeviceStatus(self, request: cast_control_pb2.DeviceID, context: grpc.ServicerContext): print(f"Got request for UUID '{request.uuid}'") # Check if the string is actually a UUID try: uuid = UUID(request.uuid) except ValueError: context.set_code(grpc.StatusCode.INVALID_ARGUMENT) context.set_details(f"Illegal UUID: '{request.uuid}'") return cast_control_pb2.Status() # Check if the ID actually exists on the network chromecast = next( (c for c in self.chromecasts if c.device.uuid == uuid), None) if chromecast == None: context.set_code(grpc.StatusCode.NOT_FOUND) context.set_details( f"No device with UUID '{request.uuid}' found on the network") return cast_control_pb2.Status() chromecast.wait() cc_status = chromecast.status return cast_control_pb2.Status(volume=cc_status.volume_level, muted=cc_status.volume_muted)
def DetectLanguageFromTweetText( self, request: DetectLanguageFromTweetTextRequest, context: grpc.ServicerContext ) -> DetectLanguageFromTweetTextResponse: """ Q: "à partir du texte d’un tweet, est-il possible de deviner la langue dans lequel le tweet a été rédigé ?" Args: request: context: Returns: """ # process feature: detect language from tweet id with StorageDatabase() as db: tweet = db.tweets.find_one({'tweet_id': str(request.tweet_id)}) if tweet is None: context.set_code(grpc.StatusCode.INVALID_ARGUMENT) context.set_details("Can't find a tweet with tweet_id={} !".format(request.tweet_id)) return DetectLanguageFromTweetTextResponse() tweet_text = tweet['text'] logger.debug(f"tweet_id={request.tweet_id} => text: {tweet_text}") detect_language = compute_detect_language(tweet_text) # build message result msg_detect_language = DetectLanguageFromTweetTextResponse(**{**detect_language.dict(), **{'text': tweet_text}}) # send message result (one_to_one) return msg_detect_language
def set_grpc_err(context: grpc.ServicerContext, code: grpc.StatusCode, details: str): """ Sets status code and details for a gRPC context. Removes commas from the details message (see https://github.com/grpc/grpc-node/issues/769) """ context.set_code(code) context.set_details(details.replace(',', ''))
def intercept( self, method: Callable, request: Any, context: grpc.ServicerContext, method_name: str, ) -> Any: """Override this method to implement a custom interceptor. You should call method(request, context) to invoke the next handler (either the RPC method implementation, or the next interceptor in the list). Args: method: The next interceptor, or method implementation. request: The RPC request, as a protobuf message. context: The ServicerContext pass by gRPC to the service. method_name: A string of the form "/protobuf.package.Service/Method" Returns: This should generally return the result of method(request, context), which is typically the RPC method response, as a protobuf message. The interceptor is free to modify this in some way, however. """ try: return method(request, context) except GrpcException as e: context.set_code(e.status_code) context.set_details(e.details) logger.error(e.details) return any_pb2.Any() except marshmallow.ValidationError as e: context.set_code(InvalidArgument.status_code) msg = ' '.join([ "%s: %s" % (key, str(value)) for key, value in e.messages.items() ]) context.set_details(msg) logger.error(msg) return any_pb2.Any() except ApiException as e: context.set_code("KubeError") context.set_details(parse_api_exception(e)) logger.error(parse_api_exception(e)) return kubespawner_pb2.Status(status=e.status, message=parse_api_exception(e)) except ApiValueError as e: context.set_code("KubeError") context.set_details(e) logger.error(e) return kubespawner_pb2.Status(status=400, message=e) except Exception as e: context.set_code(Unknown.status_code) context.set_details(str(e)) logger.error(str(e)) return any_pb2.Any()
def DeleteItem(self, request: api_pb2.DeleteItemRequest, context: grpc.ServicerContext) -> api_pb2.DeleteItemResponse: try: db.models.Item.objects.get(id=request.id).delete() return api_pb2.DeleteItemResponse() except db.models.Item.DoesNotExist: context.set_code(grpc.StatusCode.NOT_FOUND) context.set_details('Item does not exist') return api_pb2.DeleteItemResponse()
def SayHello(self, request: helloworld_pb2.HelloRequest, context: grpc.ServicerContext) -> helloworld_pb2.HelloReply: parts = request.name.split("=") err_length = int(parts[1]) err_msg = "x" * err_length context.set_code(grpc.StatusCode.FAILED_PRECONDITION) context.set_details(err_msg) logging.info("returning message length = %d", len(err_msg)) return helloworld_pb2.HelloReply()
def AddItem(self, request: api_pb2.Item, context: grpc.ServicerContext) -> api_pb2.AddItemResponse: try: item = db.models.Item(**_json_format.MessageToDict(request)) item.full_clean() item.save() return api_pb2.AddItemResponse(item=convert(item)) except ValidationError as e: # TODO: Return correct status code by exception types context.set_code(grpc.StatusCode.INTERNAL) context.set_details(json.dumps(e.messages)) return api_pb2.AddItemResponse()
def GetItem(self, request: api_pb2.GetItemRequest, context: grpc.ServicerContext) -> api_pb2.Item: try: item = db.models.Item.objects.get(id=request.id) item.pv = F('pv') + 1 item.save() item.refresh_from_db() return convert(item) except db.models.Item.DoesNotExist: context.set_code(grpc.StatusCode.NOT_FOUND) context.set_details('Item does not exist') return api_pb2.Item()
def Get(self, request: app_pb2.GetReq, context: grpc.ServicerContext) -> app_pb2.GetResp: # Handle Error # https://github.com/grpc/grpc/blob/master/doc/statuscodes.md if request.key not in self.store: context.set_code(grpc.StatusCode.NOT_FOUND) context.set_details('Key not found {}'.format(request.key)) return app_pb2.GetResp() val = self.store[request.key] return app_pb2.GetResp(val=val)
def login(self, request: service_pb2.LoginRequest, context: grpc.ServicerContext) -> service_pb2.Empty: print(request.email) print(request.password) if not bcrypt.checkpw(request.password, ""): context.set_code(grpc.StatusCode.PERMISSION_DENIED) context.set_details("Invalid password") return service_pb2.Empty() return
def executor(self, request: Any, context: grpc.ServicerContext) -> executor_base.Executor: """Returns the executor which should be used to handle `request`.""" with self._lock: executor = self._executors.get(request.executor.id) if executor is None: message = f'No executor found for executor id: {request.executor.id}' context.set_code(grpc.StatusCode.FAILED_PRECONDITION) context.set_details(message) raise RuntimeError(message) return executor
def GetByToken(self, request: app_pb2.GetByTokenReq, context: grpc.ServicerContext) -> app_pb2.GetByTokenResp: # Handle Error # https://github.com/grpc/grpc/blob/master/doc/statuscodes.md if request.token not in self.maxCacheKeysByToken: context.set_code(grpc.StatusCode.NOT_FOUND) context.set_details('Token not found {}'.format(request.token)) return app_pb2.GetByTokenResp() max_cache_keys = self.maxCacheKeysByToken[request.token] account = app_pb2.Account(max_cache_keys=max_cache_keys) return app_pb2.GetByTokenResp(account=account)
def __init__(self, message: str = None, context: ServicerContext = None, status_code: tuple = None): self.status_code = self.status_code if status_code is None\ else status_code self.message = self.message if message is None\ else message if context: context.set_code(self.status_code) context.set_details(self.message) super().__init__(message)
def wrapped_catch_them_all(self, request, context: grpc.ServicerContext): try: return func(self, request, context) except Exception as e: start_time = getattr(context, "start_time", None) if start_time is not None: delta = time.perf_counter() - start_time self._log.exception("FAIL %.3f", delta) else: self._log.exception("FAIL ?") context.set_code(grpc.StatusCode.INTERNAL) context.set_details("%s: %s" % (type(e), e)) context.error = True submit_event("error", 1) return EventResponse()
def dispatch(self, request_object: Message, context_object: grpc.ServicerContext, func: Callable): ctx = contextvars.copy_context() context.set(context_object) bound_logger = self.logger.bind(peer=context_object.peer()) with bound_logger.catch(): try: return ctx.run(func, request_object) except exceptions.GRPCError as e: context_object.set_code(e.code) if e.details: context_object.set_details(e.details) except NotImplementedError: context_object.set_code(grpc.StatusCode.UNIMPLEMENTED)
def ListItem(self, request: api_pb2.ListItemRequest, context: grpc.ServicerContext) -> api_pb2.ListItemResponse: page = request.page limit = request.limit items = Paginator(db.models.Item.objects.all(), limit) try: return api_pb2.ListItemResponse( items=map(convert, items.page(page).object_list), total=db.models.Item.objects.count(), prevPage=max(1, page - 1), nextPage=min(page + 1, items.num_pages), ) except InvalidPage: context.set_code(grpc.StatusCode.INVALID_ARGUMENT) context.set_details('Invalid page number') return api_pb2.ListItemResponse()
def UpdateItem(self, request: api_pb2.UpdateItemRequest, context: grpc.ServicerContext) -> api_pb2.UpdateItemResponse: try: item = db.models.Item.objects.get(id=request.item.id) item.__dict__.update(_json_format.MessageToDict(request)['item']) item.full_clean() item.save() item.refresh_from_db() return api_pb2.UpdateItemResponse(item=convert(item)) except db.models.Item.DoesNotExist: context.set_code(grpc.StatusCode.NOT_FOUND) context.set_details('Item does not exist') return api_pb2.UpdateItemResponse() except ValidationError as e: # TODO: Return correct status code by exception types context.set_code(grpc.StatusCode.INTERNAL) context.set_details(json.dumps(e.messages)) return api_pb2.UpdateItemResponse()
def wrapper(instance, req: Message, ctx: Context) -> Message: try: res = func(instance, req, ctx) return res except ExitGRPCCallWithCode: return Message() except Exception as e: if logger is not None: logger.error(e) ctx.set_code(grpc.StatusCode.INTERNAL) ctx.set_details( "Unknown error happened during processing request.") return Message()
def Store(self, request: app_pb2.StoreReq, context: grpc.ServicerContext) -> app_pb2.StoreResp: accountsCtx = grpc.ServicerContext() getByTokenResp = self.accounts.GetByToken(app_pb2.GetByTokenReq(token=request.account_token)) if request.account_token not in self.keysByAccount: self.keysByAccount[request.account_token] = 0 if self.keysByAccount[request.account_token] >= getByTokenResp.account.max_cache_keys: context.set_code(grpc.StatusCode.FAILED_PRECONDITION) context.set_details('Account {} exceeds max key limit {}'.format(request.account_token, getByTokenResp.account.max_cache_keys)) return app_pb2.GetResp() if request.key not in self.store: self.keysByAccount[request.account_token] += 1 self.store[request.key] = request.val return app_pb2.StoreResp()
def CreateBeer(self, create_beer_request: bartender_pb2.CreateBeerRequest, context: grpc.ServicerContext) -> bartender_pb2.Beer: beer = create_beer_request.beer if not beer or not beer.name or not create_beer_request.brand: context.set_details("Well ain't that cute? But it's WRONG!") context.set_code(grpc.StatusCode.INVALID_ARGUMENT) return bartender_pb2.Beer() types = self._cache.get(create_beer_request.brand, []) if beer.name in types: context.set_details("Plagiarism!") context.set_code(grpc.StatusCode.ALREADY_EXISTS) return bartender_pb2.Beer() types.append(beer) self._cache[create_beer_request.brand] = types return beer
def intercept( self, method: Callable, request: Any, context: grpc.ServicerContext, method_name: str, ) -> Any: """Do not call this directly; use the interceptor kwarg on grpc.server().""" try: return method(request, context) except GrpcException as e: context.set_code(e.status_code) context.set_details(e.details) raise except Exception as e: if self._status_on_unknown_exception is not None: context.set_code(self._status_on_unknown_exception) context.set_details(repr(e)) raise
def UpdateExecutionInfo( self, request: execution_watcher_pb2.UpdateExecutionInfoRequest, context: grpc.ServicerContext ) -> execution_watcher_pb2.UpdateExecutionInfoResponse: """Updates the `custom_properties` field of Execution object in MLMD.""" logging.info( 'Received request to update execution info: updates %s, ' 'execution_id %s', request.updates, request.execution_id) if request.execution_id != self._execution.id: context.set_code(grpc.StatusCode.NOT_FOUND) context.set_details( 'Execution with given execution_id not tracked by server: ' f'{request.execution_id}') return execution_watcher_pb2.UpdateExecutionInfoResponse() for key, value in request.updates.items(): self._execution.custom_properties[key].CopyFrom(value) # Only the execution is needed with self._mlmd_connection as m: m.store.put_executions((self._execution, )) return execution_watcher_pb2.UpdateExecutionInfoResponse()
def Delete(self, request: crudata.DeleteRequest, context: grpc.ServicerContext) -> crudata.DeleteResponse: if not request.originator or not request.originator.id: context.set_code(grpc.StatusCode.INVALID_ARGUMENT) context.set_details("missing originator.id") return crudata.DeleteResponse() validate_originator(request.originator) if not request.entity_type: context.set_code(grpc.StatusCode.INVALID_ARGUMENT) context.set_details("missing entity_type") return crudata.DeleteResponse() delete_originator = self.crud_store.delete( entity_type=request.entity_type, originator=request.originator, ) return crudata.DeleteResponse( originator=delete_originator, )
def intercept( self, method: Callable, request: Any, context: grpc.ServicerContext, method_name: str, ) -> Any: """Override this method to implement a custom interceptor. You should call method(request, context) to invoke the next handler (either the RPC method implementation, or the next interceptor in the list). Args: method: The next interceptor, or method implementation. request: The RPC request, as a protobuf message. context: The ServicerContext pass by gRPC to the service. method_name: A string of the form "/protobuf.package.Service/Method" Returns: This should generally return the result of method(request, context), which is typically the RPC method response, as a protobuf message. The interceptor is free to modify this in some way, however. """ try: return method(request, context) except GrpcException as e: context.set_code(e.status_code) context.set_details(e.details) logger.error(e.details) return any_pb2.Any() except marshmallow.ValidationError as e: context.set_code(InvalidArgument.status_code) context.set_details(e.__str__()) logger.error(e) return any_pb2.Any() except mongoengine.errors.DoesNotExist as e: context.set_code(NotFound.status_code) context.set_details(str(e)) logger.error(str(e)) return any_pb2.Any() except Exception as e: context.set_code(Unknown.status_code) context.set_details(str(e)) logger.error(str(e)) return any_pb2.Any()
def _set_unknown_err(context: grpc.ServicerContext, err): logging.error(traceback.format_exc()) context.set_code(grpc.StatusCode.UNKNOWN) context.set_details(str(err))
def _set_invalid_arg_err(context: grpc.ServicerContext, err): logging.error(traceback.format_exc()) context.set_code(grpc.StatusCode.INVALID_ARGUMENT) context.set_details(str(err))
def _propagate_grpc_code_err(context: grpc.ServicerContext, err: grpc.RpcError): logging.error(traceback.format_exc()) context.set_code(err.code()) context.set_details(str(err))
def _set_unavailable_error(context: grpc.ServicerContext, err): logging.error(traceback.format_exc()) context.set_code(grpc.StatusCode.UNAVAILABLE) context.set_details(str(err))
def __init__(self, ctx: Context, status_code, details: str = ""): ctx.set_code(status_code) ctx.set_details(details) super().__init__()