def mockreadfail(self, rack, board, device): """Mock method to monkeypatch the client read method to fail.""" raise grpc.RpcError()
def testUnknownExceptionParser(self): response = grpc.RpcError() response.code = lambda: "unknown" response.details = lambda: "unknown" self.assertRaises(opencue.exception.CueException, lambda: testRaise(response))
def ReportStates(self, request, context): if request.states[0].type == "fail": raise grpc.RpcError("Test Exception") return ReportStatesResponse(unreportedStates=[], )
def testNotFoundExceptionParser(self): response = grpc.RpcError() response.code = lambda: grpc.StatusCode.NOT_FOUND response.details = lambda: "testing not found" self.assertRaises(opencue.exception.EntityNotFoundException, lambda: testRaise(response))
def testDeadlineExceptionParser(self): response = grpc.RpcError() response.code = lambda: grpc.StatusCode.DEADLINE_EXCEEDED response.details = lambda: "deadline exceeded" self.assertRaises(opencue.exception.DeadlineExceededException, lambda: testRaise(response))
def Recv(): error = grpc.RpcError() error.code = lambda: grpc.StatusCode.PERMISSION_DENIED error.details = lambda: 'Did not work...' raise error
def _raise_grpc_error_unavailable(*args): del args # Unused error = grpc.RpcError() error.code = lambda: grpc.StatusCode.UNAVAILABLE raise error
def MinNodesToRemove(self, request, context): print('>>>>>>>>>>>>>>In endpoint MinNodesToRemove') print(time.strftime("%c")) graph = request.graph source_nodes = request.source_node target_nodes = request.target_node g = robustness.Robustness() try: edges_list = [] for edges_proto in graph.edges: edges_list.append(list(edges_proto.edge)) graph_in = {"nodes": list(graph.nodes), "edges": edges_list} source_nodes_in = str(source_nodes) target_nodes_in = str(target_nodes) ret = g.min_nodes_to_remove(graph_in, source_nodes_in, target_nodes_in) resp = network_analytics_robustness_pb2.MinNodesToRemoveResponse( status=ret[0], message=ret[1]) if resp.status: nodes_resp = ret[2]["nodes"] edges_resp = [] for edge_ret in ret[2]["edges"]: edges_resp.append( network_analytics_robustness_pb2.Edge(edge=edge_ret)) resp = network_analytics_robustness_pb2.MinNodesToRemoveResponse( status=ret[0], message=ret[1], nodes_output=nodes_resp, edges_output=edges_resp) else: print(time.strftime("%c")) print('Waiting for next call on port 5002.') raise grpc.RpcError(grpc.StatusCode.UNKNOWN, ret[1]) print('status:', resp.status) print('message:', resp.message) print(time.strftime("%c")) print('Waiting for next call on port 5002.') return resp except Exception as e: logging.exception("message") print(time.strftime("%c")) print('Waiting for next call on port 5002.') raise grpc.RpcError(grpc.StatusCode.UNKNOWN, str(e))
def MostImportantNodesEdgesSubset(self, request, context): print('>>>>>>>>>>>>>>In endpoint MostImportantNodesEdgesSubset') print(time.strftime("%c")) graph = request.graph source_nodes = request.source_nodes target_nodes = request.target_nodes T = request.Type g = robustness.Robustness() try: edges_list = [] for edges_proto in graph.edges: edges_list.append(list(edges_proto.edge)) weights_list = list(graph.weights) nodes_list = list(graph.nodes) if len(weights_list) > 0: graph_in = { "nodes": nodes_list, "edges": edges_list, "weights": weights_list } else: graph_in = {"nodes": nodes_list, "edges": edges_list} source_nodes_in = list(source_nodes) target_nodes_in = list(target_nodes) ret = g.most_important_nodes_edges_subset( graph_in, source_nodes_in, target_nodes_in, T, request.normalized, request.directed, request.weight) resp = network_analytics_robustness_pb2.MostImportantNodesEdgesSubsetResponse( status=ret[0], message=ret[1]) if resp.status: betweenness_centrality = ret[2]["betweenness_centrality"] if (T == 0): node_resp = network_analytics_robustness_pb2.node_betweenness( node=betweenness_centrality[0], node_centrality_value=betweenness_centrality[1]) resp = network_analytics_robustness_pb2.MostImportantNodesEdgesSubsetResponse( status=ret[0], message=ret[1], node_betweenness_centrality=node_resp) elif (T == 1): edges_resp = [] for edge_ret in betweenness_centrality[0]: edges_resp.append( network_analytics_robustness_pb2.Edge( edge=list(edge_ret))) graph_resp = network_analytics_robustness_pb2.edge_betweenness( edge=edges_resp, edge_centrality_value=betweenness_centrality[1]) resp = network_analytics_robustness_pb2.MostImportantNodesEdgesSubsetResponse( status=ret[0], message=ret[1], edge_betweenness_centrality=graph_resp) else: print(time.strftime("%c")) print('Waiting for next call on port 5002.') raise grpc.RpcError(grpc.StatusCode.UNKNOWN, ret[1]) print('status:', resp.status) print('message:', resp.message) print(time.strftime("%c")) print('Waiting for next call on port 5002.') return resp except Exception as e: logging.exception("message") print(time.strftime("%c")) print('Waiting for next call on port 5002.') raise grpc.RpcError(grpc.StatusCode.UNKNOWN, str(e))
def Classify(self, request, context): try: example = request.input.example_list.examples[0] start_time_millis = self.getCurrentTimeinMillis() time_1 = datetime.now() if self._Validations(request, context) is True: time_2 = datetime.now() example = request.input.example_list.examples[0] idx = 0 final_input = [] final_labels = [] for k in example.features.feature.keys(): v = example.features.feature[k] int_list_value = list(v.int64_list.value) float_list_value = list(v.float_list.value) byte_list_value = list(v.bytes_list.value) if len(int_list_value) > 0: final_input.append(int_list_value) final_labels.append(k) elif len(float_list_value) > 0: final_input.append(float_list_value) final_labels.append(k) elif len(byte_list_value) > 0: final_input.append(byte_list_value) final_labels.append(k) else: LOG.info("Input param empty, ignoring it") idx = idx + 1 classification_request = pd.DataFrame.from_records( list(zip(*final_input)), columns=final_labels) try: classification_outputs = self.model.wrapper_classification_func( classification_request) LOG.info( "the output from the model's classification function %s", classification_outputs) response = tensorflow__serving_dot_apis_dot_classification__pb2.ClassificationResponse( ) except grpc.RpcError as grpc_error: end_time_millis = self.getCurrentTimeinMillis() LOG.error("Error while doing Classification") requestDurationInMillis = str(end_time_millis - start_time_millis) self.logRequests(self.model.model_name, requestDurationInMillis, str(0)) LOG.error("grpc error : %s", str(grpc_error)) s = getattr(grpc_error, 'message', str(grpc_error)) raise grpc.RpcError(grpc_error) return None classifications = [] for idx in range(0, classification_request.shape[0]): classes = [] classification = tensorflow__serving_dot_apis_dot_classification__pb2.Classifications( ) for k, v in classification_outputs.items(): class_data = tensorflow__serving_dot_apis_dot_classification__pb2.Class( ) class_data.label = "Class-" + k class_data.score = v[idx] classes.append(class_data) classification.classes.extend(classes) classifications.append(classification) response.result.classifications.extend(classifications) return response else: return None except Exception as ex: s = getattr(ex, 'message', str(ex)) raise Exception(s) return None
def raise_grpc_error(context, status_code, msg): # Make sure the error handling, including logging, works as intended in the client context.set_code(status_code) context.set_details(msg) # Raise error on the plugin-side raise grpc.RpcError(status_code, msg)
def Predict(self, request, context): try: start_time_millis = self.getCurrentTimeinMillis() time_1 = datetime.now() if self._Validations(request, context): time_2 = datetime.now() predict_request = {} for k, v in request.inputs.items(): predict_request[k] = tensor_util.MakeNdarray( request.inputs[k]) if v.dtype == dtypes.string: predict_request[k] = predict_request[k].astype(str) time_3 = datetime.now() for k, v in predict_request.items(): LOG.info("the key :%s", k) LOG.info("the value :%s", v) # Add the more specific try catch request for predict request try: predict_outputs = self.model.wrapper_predict_func( predict_request) LOG.info("the output from the model's predict function %s", predict_outputs) response = tensorflow__serving_dot_apis_dot_predict__pb2.PredictResponse( ) except grpc.RpcError as grpc_error: end_time_millis = self.getCurrentTimeinMillis() LOG.error("Error while doing Prediction") requestDurationInMillis = str(end_time_millis - start_time_millis) self.logRequests(self.model.model_name, requestDurationInMillis, str(0)) LOG.error("grpc error : %s", str(grpc_error)) s = getattr(grpc_error, 'message', str(grpc_error)) raise grpc.RpcError(grpc_error) return None for k, v in predict_outputs.items(): # Disabling this pylint error after checking that this line adheres to # tensorflow documentation # pylint: disable=E1101 response.outputs[k].CopyFrom( tf.contrib.util.make_tensor_proto(v)) ''' here print the response time taken to serve the request ''' time_4 = datetime.now() end_time_millis = self.getCurrentTimeinMillis() validation_time_ms = (time_2 - time_1).microseconds parse_time = (time_3 - time_2).microseconds handle_time = (time_4 - time_3).microseconds model_metrics = { "mlf_mc_model_name": self.model.model_name, "mlf_mc_token_validation_time_ms": validation_time_ms / 1000, "mlf_mc_parse_time_ms": parse_time / 1000, "mlf_mc_handle_time_ms": handle_time / 1000 } self.collect_metrics(self.model.model_name, model_metrics) requestDurationInMillis = str(end_time_millis - start_time_millis) self.logRequests(self.model.model_name, requestDurationInMillis, str(1)) return response else: requestDurationInMillis = self.getCurrentTimeinMillis() LOG.error( "Error while validating JWT token, token not validated successfully" ) self.logRequests(self.model.model_name, requestDurationInMillis, str(0)) return None except Exception as ex: s = getattr(ex, 'message', str(ex)) raise Exception(s) return None
def test_ctor_w_rpc_error_on_prefetch(self): wrapped = mock.MagicMock() wrapped.__next__.side_effect = grpc.RpcError() with pytest.raises(grpc.RpcError): self._make_one(wrapped)
def DisableStaticRules(self, _: DisableStaticRuleRequest) -> Void: raise grpc.RpcError()
def PLSA(self,request,context): print('>>>>>>>>>>>>>>In endpoint plsa') print(time.strftime("%c")) docs = request.docs num_topics = request.num_topics topic_divider = request.topic_divider maxiter = request.maxiter beta = request.beta param_error = False message = '' try : if len(docs) < 2: message = 'Length of docs should be at least two' param_error =True if topic_divider < 0: param_error = True message = 'topic_divider parameter can not be a negative nubmer' if topic_divider == 0 and num_topics < 2: param_error = True message = 'Number of topics should be at least two' if maxiter < 0: param_error = True message = 'maxiter should be greater than zero' if beta < 0 or beta > 1: param_error = True message = 'beta should have value of (0,1]' if param_error: print(time.strftime("%c")) print('Waiting for next call on port 5000.') raise grpc.RpcError(grpc.StatusCode.UNKNOWN, message) except Exception as e: logging.exception("message") print(time.strftime("%c")) print('Waiting for next call on port 5000.') raise grpc.RpcError(grpc.StatusCode.UNKNOWN, str(e)) try: unique_folder_naming = str(datetime.datetime.now()).replace(':', '-').replace('.', '-') + '^' + str(random.randint(100000000000, 999999999999)) + '/' thread1 = threading.Thread(target=generate_topics_plsa, args=(docs,unique_folder_naming,num_topics,topic_divider,maxiter,beta)) thread1.start() resp = topic_analysis_pb2.PLSAResponse(status=True, message='success', handle=unique_folder_naming[:-1].replace('-','e').replace(' ','d').replace('^','y')) print('status:',resp.status) print('message:',resp.message) print('Waiting for next call on port 5000.') return resp except Exception as e: logging.exception("message") print(time.strftime("%c")) print('Waiting for next call on port 5000.') raise grpc.RpcError(grpc.StatusCode.UNKNOWN, str(e))
def fake_add_done(_): grpc_err = grpc.RpcError() grpc_err.code = lambda: grpc.StatusCode.UNKNOWN grpc_err.details = lambda: "Test Exception" raise grpc_err
def Recv(): error = grpc.RpcError() error.code = lambda: grpc.StatusCode.OUT_OF_RANGE error.details = lambda: 'Did not work...' raise error
def mockwritefail(self, rack, board, device, data): """Mock method to monkeypatch the client write method to fail.""" raise grpc.RpcError()
def Recv(): error = grpc.RpcError() error.code = lambda: grpc.StatusCode.INVALID_ARGUMENT error.details = lambda: 'Did not work...' raise error
def push(self, item, push_state, content=None): assert isinstance(item, Item) assert item.digest is not None assert item.size is not None assert isinstance(push_state, _IsolateServerGrpcPushState) # Default to item.content(). content = item.content() if content is None else content guard_memory_use(self, content, item.size) self._num_pushes += 1 try: def chunker(): # Returns one bit of content at a time if (isinstance(content, str) or not isinstance(content, collections.Iterable)): yield content else: for chunk in content: yield chunk def slicer(): # Ensures every bit of content is under the gRPC max size; yields # proto messages to send via gRPC. request = bytestream_pb2.WriteRequest() u = uuid.uuid4() request.resource_name = '%s/uploads/%s/blobs/%s/%d' % ( self._proxy.prefix, u, item.digest, item.size) request.write_offset = 0 for chunk in chunker(): # Make sure we send at least one chunk for zero-length blobs has_sent_anything = False while chunk or not has_sent_anything: has_sent_anything = True slice_len = min(len(chunk), NET_IO_FILE_CHUNK) request.data = chunk[:slice_len] if request.write_offset + slice_len == item.size: request.finish_write = True yield request request.write_offset += slice_len chunk = chunk[slice_len:] response = None try: response = self._proxy.call_no_retries('Write', slicer()) except grpc.RpcError as r: if r.code() == grpc.StatusCode.ALREADY_EXISTS: # This is legit - we didn't check before we pushed so no problem if # it's already there. self._already_exists += 1 if self._already_exists % 100 == 0: logging.info('unnecessarily pushed %d/%d blobs (%.1f%%)' % ( self._already_exists, self._num_pushes, 100.0 * self._already_exists / self._num_pushes)) else: logging.error('gRPC error during push: throwing as IOError (%s)' % r) raise IOError(r) except Exception as e: logging.error('error during push: throwing as IOError (%s)' % e) raise IOError(e) if response is not None and response.committed_size != item.size: raise IOError('%s/%d: incorrect size written (%d)' % ( item.digest, item.size, response.committed_size)) elif response is None and item.size > 0: # This happens when the content generator is exhausted and the gRPC call # simply returns None. Throw gRPC error as this is not recoverable. raise grpc.RpcError('None gRPC response on uploading %s' % item.digest) finally: with self._lock: self._memory_use -= item.size
def _raise_non_retryable_grpc_error(*args): del args # Unused error = grpc.RpcError() error.code = lambda: grpc.StatusCode.ABORTED raise error
def DeleteStates(self, request, context): for state_id in request.ids: if state_id.type == LOG_TYPE: raise grpc.RpcError("Test Exception") return Void()
def testExistsExceptionParser(self): response = grpc.RpcError() response.code = lambda: grpc.StatusCode.ALREADY_EXISTS response.details = lambda: "already exists" self.assertRaises(opencue.exception.EntityAlreadyExistsException, lambda: testRaise(response))
def get_online_features( self, feature_refs: List[str], entity_rows: List[GetOnlineFeaturesRequest.EntityRow], project: Optional[str] = None, ) -> GetOnlineFeaturesResponse: """ Retrieves the latest online feature data from Feast Serving Args: feature_refs: List of feature references that will be returned for each entity. Each feature reference should have the following format: "feature_set:feature" where "feature_set" & "feature" refer to the feature and feature set names respectively. Only the feature name is required. entity_rows: List of GetFeaturesRequest.EntityRow where each row contains entities. Timestamp should not be set for online retrieval. All entity types within a feature project: Specifies the project which contain the FeatureSets which the requested features belong to. Returns: Returns a list of maps where each item in the list contains the latest feature values for the provided entities """ self._connect_serving() try: response = self._serving_service_stub.GetOnlineFeatures( GetOnlineFeaturesRequest( features=_build_feature_references( feature_ref_strs=feature_refs, project=project if project is not None else self.project, ), entity_rows=entity_rows, )) # collect entity row refs entity_refs = set() for entity_row in entity_rows: entity_refs.update(entity_row.fields.keys()) strip_field_values = [] for field_value in response.field_values: # strip the project part the string feature references returned from serving strip_fields = {} for ref_str, value in field_value.fields.items(): # find and ignore entities if ref_str in entity_refs: strip_fields[ref_str] = value else: strip_ref_str = repr( FeatureRef.from_str(ref_str, ignore_project=True)) strip_fields[strip_ref_str] = value strip_field_values.append( GetOnlineFeaturesResponse.FieldValues(fields=strip_fields)) del response.field_values[:] response.field_values.extend(strip_field_values) except grpc.RpcError as e: raise grpc.RpcError(e.details()) return response
def testInternalExceptionParser(self): response = grpc.RpcError() response.code = lambda: grpc.StatusCode.INTERNAL response.details = lambda: "Internal Error" self.assertRaises(opencue.exception.CueInternalErrorException, lambda: testRaise(response))
def get_historical_features( self, feature_refs: List[str], entity_rows: Union[pd.DataFrame, str], compute_statistics: bool = False, project: str = None, ) -> RetrievalJob: """ Retrieves historical features from a Feast Serving deployment. Args: feature_refs: List of feature references that will be returned for each entity. Each feature reference should have the following format: "feature_set:feature" where "feature_set" & "feature" refer to the feature and feature set names respectively. Only the feature name is required. entity_rows (Union[pd.DataFrame, str]): Pandas dataframe containing entities and a 'datetime' column. Each entity in a feature set must be present as a column in this dataframe. The datetime column must contain timestamps in datetime64 format. compute_statistics (bool): Indicates whether Feast should compute statistics over the retrieved dataset. project: Specifies the project which contain the FeatureSets which the requested features belong to. Returns: feast.job.RetrievalJob: Returns a retrival job object that can be used to monitor retrieval progress asynchronously, and can be used to materialize the results. Examples: >>> from feast import Client >>> from datetime import datetime >>> >>> feast_client = Client(core_url="localhost:6565", serving_url="localhost:6566") >>> feature_refs = ["my_project/bookings_7d", "booking_14d"] >>> entity_rows = pd.DataFrame( >>> { >>> "datetime": [pd.datetime.now() for _ in range(3)], >>> "customer": [1001, 1002, 1003], >>> } >>> ) >>> feature_retrieval_job = feast_client.get_historical_features( >>> feature_refs, entity_rows, project="my_project") >>> df = feature_retrieval_job.to_dataframe() >>> print(df) """ # Retrieve serving information to determine store type and # staging location serving_info = self._serving_service.GetFeastServingInfo( GetFeastServingInfoRequest(), timeout=self._config.getint( CONFIG_GRPC_CONNECTION_TIMEOUT_DEFAULT_KEY), metadata=self._get_grpc_metadata(), ) # type: GetFeastServingInfoResponse if serving_info.type != FeastServingType.FEAST_SERVING_TYPE_BATCH: raise Exception( f'You are connected to a store "{self.serving_url}" which ' f"does not support batch retrieval ") if isinstance(entity_rows, pd.DataFrame): # Pandas DataFrame detected # Remove timezone from datetime column if isinstance(entity_rows["datetime"].dtype, pd.core.dtypes.dtypes.DatetimeTZDtype): entity_rows["datetime"] = pd.DatetimeIndex( entity_rows["datetime"]).tz_localize(None) elif isinstance(entity_rows, str): # String based source if not entity_rows.endswith((".avro", "*")): raise Exception( "Only .avro and wildcard paths are accepted as entity_rows" ) else: raise Exception(f"Only pandas.DataFrame and str types are allowed" f" as entity_rows, but got {type(entity_rows)}.") # Export and upload entity row DataFrame to staging location # provided by Feast staged_files = export_source_to_staging_location( entity_rows, serving_info.job_staging_location) # type: List[str] request = GetBatchFeaturesRequest( features=_build_feature_references( feature_ref_strs=feature_refs, project=project if project is not None else self.project, ), dataset_source=DatasetSource(file_source=DatasetSource.FileSource( file_uris=staged_files, data_format=DataFormat.DATA_FORMAT_AVRO)), compute_statistics=compute_statistics, ) # Retrieve Feast Job object to manage life cycle of retrieval try: response = self._serving_service.GetBatchFeatures( request, metadata=self._get_grpc_metadata()) except grpc.RpcError as e: raise grpc.RpcError(e.details()) return RetrievalJob( response.job, self._serving_service, auth_metadata_plugin=self._auth_metadata, )
def EvaluateScript(self, header, request, context, func_type): """ Evaluates script provided in the header, given the arguments provided in the sequence of RowData objects, the request. :param header: :param request: an iterable sequence of RowData. :param context: the context sent from client :param func_type: function type. :return: an iterable sequence of RowData. """ # Retrieve data types from header arg_types = self.get_arg_types(header) ret_type = self.get_return_type(header) logging.info('EvaluateScript: {} ({} {}) {}'.format( header.script, arg_types, ret_type, func_type)) aggr = (func_type == FunctionType.Aggregation) # Check if parameters are provided if header.params: # Verify argument type if arg_types == ArgType.String: # Create an empty list if tensor function if aggr: all_rows = [] # Iterate over bundled rows for request_rows in request: # Iterate over rows for row in request_rows.rows: # Retrieve numerical data from duals params = self.get_arguments(context, arg_types, row.duals) if aggr: # Append value to list, for later aggregation all_rows.append(params) else: # Evaluate script row wise yield self.evaluate(context, header.script, ret_type, params=params) # Evaluate script based on data from all rows if aggr: params = [list(param) for param in zip(*all_rows)] yield self.evaluate(context, header.script, ret_type, params=params) else: # This plugin does not support other argument types than string. # Make sure the error handling, including logging, works as intended in the client msg = 'Argument type: {} not supported in this plugin.'.format( arg_types) context.set_code(grpc.StatusCode.UNIMPLEMENTED) context.set_details(msg) # Raise error on the plugin-side raise grpc.RpcError(grpc.StatusCode.UNIMPLEMENTED, msg) else: # This plugin does not support script evaluation without parameters # Make sure the error handling, including logging, works as intended in the client msg = 'Script evaluation with no parameters is not supported in this plugin.' context.set_code(grpc.StatusCode.UNIMPLEMENTED) context.set_details(msg) # Raise error on the plugin-side raise grpc.RpcError(grpc.StatusCode.UNIMPLEMENTED, msg)
def grpc_error(code, details): # Monkey patch insertion for the methods a real grpc.RpcError would have. error = grpc.RpcError("RPC error %r: %s" % (code, details)) error.code = lambda: code error.details = lambda: details return error
def _raise_rpc_error(state): rpc_error = grpc.RpcError() state.rpc_errors.append(rpc_error) raise rpc_error
def _raise_grpc_error_deadline_exceeded(*args): del args # Unused error = grpc.RpcError() error.code = lambda: grpc.StatusCode.DEADLINE_EXCEEDED raise error