class UwsgiServing(RESTfulComponent, PredictMixin): def __init__(self, engine): super(UwsgiServing, self).__init__(engine) self._show_perf = False self._stats_collector = None self._memory_monitor = None self._run_language = None self._predictor = None self._target_type = None self._code_dir = None self._deployment_config = None self._predict_calls_count = 0 self._verbose = self._logger.isEnabledFor(logging.DEBUG) self._total_predict_requests = Metric( "mlpiper.restful.predict_requests", title="Total number of stat requests", metric_type=MetricType.COUNTER, value_type=int, metric_relation=MetricRelation.SUM_OF, ) self._error_response = None def get_info(self): return { "python": "{}.{}.{}".format(sys.version_info[0], sys.version_info[1], sys.version_info[2]), "worker_id": self.get_wid(), } def configure(self, params): """ @brief It is called in within the 'deputy' context """ super(UwsgiServing, self).configure(params) self._code_dir = self._params.get("__custom_model_path__") self._show_perf = self._params.get("show_perf") self._run_language = RunLanguage(params.get("run_language")) self._target_type = TargetType(params[TARGET_TYPE_ARG_KEYWORD]) self._stats_collector = StatsCollector( disable_instance=not self._show_perf) self._stats_collector.register_report("run_predictor_total", "finish", StatsOperation.SUB, "start") self._memory_monitor = MemoryMonitor() self._deployment_config = parse_validate_deployment_config_file( self._params["deployment_config"]) self._logger.info( "Configure component with input params, name: {}, params: {}". format(self.name(), params)) def load_model_callback(self, model_path, stream, version): self._logger.info(self.get_info()) try: if self._run_language == RunLanguage.PYTHON: from datarobot_drum.drum.language_predictors.python_predictor.python_predictor import ( PythonPredictor, ) self._predictor = PythonPredictor() elif self._run_language == RunLanguage.JAVA: from datarobot_drum.drum.language_predictors.java_predictor.java_predictor import ( JavaPredictor, ) self._predictor = JavaPredictor() elif self._run_language == RunLanguage.R: from datarobot_drum.drum.language_predictors.r_predictor.r_predictor import ( RPredictor, ) self._predictor = RPredictor() self._predictor.configure(self._params) except Exception as e: self._error_response = {"message": "ERROR: {}".format(e)} @FlaskRoute("{}/".format(os.environ.get(URL_PREFIX_ENV_VAR_NAME, "")), methods=["GET"]) def ping(self, url_params, form_params): return HTTP_200_OK, {"message": "OK"} @FlaskRoute("{}/ping/".format(os.environ.get(URL_PREFIX_ENV_VAR_NAME, "")), methods=["GET"]) def ping2(self, url_params, form_params): return HTTP_200_OK, {"message": "OK"} @FlaskRoute("{}/capabilities/".format( os.environ.get(URL_PREFIX_ENV_VAR_NAME, "")), methods=["GET"]) def capabilities(self, url_params, form_params): return HTTP_200_OK, make_predictor_capabilities( self._predictor.supported_payload_formats) @FlaskRoute("{}/info/".format(os.environ.get(URL_PREFIX_ENV_VAR_NAME, "")), methods=["GET"]) def info(self, url_params, form_params): model_info = self._predictor.model_info() model_info.update({ModelInfoKeys.LANGUAGE: self._run_language.value}) model_info.update({ModelInfoKeys.DRUM_VERSION: drum_version}) model_info.update({ModelInfoKeys.DRUM_SERVER: "nginx + uwsgi"}) model_info.update({ ModelInfoKeys.MODEL_METADATA: read_model_metadata_yaml(self._code_dir) }) return HTTP_200_OK, model_info @FlaskRoute("{}/health/".format(os.environ.get(URL_PREFIX_ENV_VAR_NAME, "")), methods=["GET"]) def health(self, url_params, form_params): if self._error_response: return HTTP_513_DRUM_PIPELINE_ERROR, self._error_response else: return HTTP_200_OK, {"message": "OK"} @FlaskRoute("{}/stats/".format(os.environ.get(URL_PREFIX_ENV_VAR_NAME, "")), methods=["GET"]) def prediction_server_stats(self, url_params, form_params): mem_info = self._memory_monitor.collect_memory_info() ret_dict = {"mem_info": mem_info._asdict()} self._stats_collector.round() ret_dict["time_info"] = {} for name in self._stats_collector.get_report_names(): d = self._stats_collector.dict_report(name) ret_dict["time_info"][name] = d self._stats_collector.stats_reset() return HTTP_200_OK, ret_dict @FlaskRoute("{}/predictions/".format( os.environ.get(URL_PREFIX_ENV_VAR_NAME, "")), methods=["POST"]) def predictions(self, url_params, form_params): return self.predict(url_params, form_params) @FlaskRoute("{}/predict/".format( os.environ.get(URL_PREFIX_ENV_VAR_NAME, "")), methods=["POST"]) def predict(self, url_params, form_params): if self._error_response: return HTTP_513_DRUM_PIPELINE_ERROR, self._error_response self._stats_collector.enable() self._stats_collector.mark("start") try: response, response_status = self.do_predict_structured() if response_status == HTTP_200_OK: # this counter is managed by uwsgi self._total_predict_requests.increase() self._predict_calls_count += 1 except Exception as ex: response_status, response = self._handle_exception(ex) finally: self._stats_collector.mark("finish") self._stats_collector.disable() return response_status, response @FlaskRoute( "{}/predictionsUnstructured/".format( os.environ.get(URL_PREFIX_ENV_VAR_NAME, "")), methods=["POST"], ) def predictions_unstructured(self, url_params, form_params): return self.predict_unstructured(url_params, form_params) @FlaskRoute( "{}/predictUnstructured/".format( os.environ.get(URL_PREFIX_ENV_VAR_NAME, "")), methods=["POST"], ) def predict_unstructured(self, url_params, form_params): if self._error_response: return HTTP_513_DRUM_PIPELINE_ERROR, self._error_response self._stats_collector.enable() self._stats_collector.mark("start") try: response, response_status = self.do_predict_unstructured() if response_status == HTTP_200_OK: # this counter is managed by uwsgi self._total_predict_requests.increase() self._predict_calls_count += 1 except Exception as ex: response_status, response = self._handle_exception(ex) finally: self._stats_collector.mark("finish") self._stats_collector.disable() return response_status, response @FlaskRoute("{}/transform/".format( os.environ.get(URL_PREFIX_ENV_VAR_NAME, "")), methods=["POST"]) def transform(self, url_params, form_params): if self._error_response: return HTTP_513_DRUM_PIPELINE_ERROR, self._error_response self._stats_collector.enable() self._stats_collector.mark("start") try: response, response_status = self.do_transform() if response_status == HTTP_200_OK: # this counter is managed by uwsgi self._total_predict_requests.increase() self._predict_calls_count += 1 except Exception as ex: response_status, response = self._handle_exception(ex) finally: self._stats_collector.mark("finish") self._stats_collector.disable() return response_status, response def _handle_exception(self, ex): self._logger.error(ex) response_status = HTTP_500_INTERNAL_SERVER_ERROR response = {"message": "ERROR: {}".format(ex)} return response_status, response def _get_stats_dict(self): return { "predict_calls_per_worker": self._predict_calls_count, "predict_calls_total": self._total_predict_requests.get(), }
class PredictionServer(ConnectableComponent, PredictMixin): def __init__(self, engine): super(PredictionServer, self).__init__(engine) self._show_perf = False self._stats_collector = None self._memory_monitor = None self._run_language = None self._predictor = None self._target_type = None self._code_dir = None self._deployment_config = None def configure(self, params): super(PredictionServer, self).configure(params) self._code_dir = self._params.get("__custom_model_path__") self._show_perf = self._params.get("show_perf") self._run_language = RunLanguage(params.get("run_language")) self._target_type = TargetType(params[TARGET_TYPE_ARG_KEYWORD]) self._stats_collector = StatsCollector(disable_instance=not self._show_perf) self._stats_collector.register_report( "run_predictor_total", "finish", StatsOperation.SUB, "start" ) self._memory_monitor = MemoryMonitor(monitor_current_process=True) self._deployment_config = parse_validate_deployment_config_file( self._params["deployment_config"] ) if self._run_language == RunLanguage.PYTHON: from datarobot_drum.drum.language_predictors.python_predictor.python_predictor import ( PythonPredictor, ) self._predictor = PythonPredictor() elif self._run_language == RunLanguage.JAVA: from datarobot_drum.drum.language_predictors.java_predictor.java_predictor import ( JavaPredictor, ) self._predictor = JavaPredictor() elif self._run_language == RunLanguage.JULIA: from datarobot_drum.drum.language_predictors.julia_predictor.julia_predictor import ( JlPredictor, ) self._predictor = JlPredictor() elif self._run_language == RunLanguage.R: # this import is here, because RPredictor imports rpy library, # which is not installed for Java and Python cases. from datarobot_drum.drum.language_predictors.r_predictor.r_predictor import RPredictor self._predictor = RPredictor() else: raise DrumCommonException( "Prediction server doesn't support language: {} ".format(self._run_language) ) self._predictor.configure(params) def _materialize(self, parent_data_objs, user_data): model_api = base_api_blueprint() @model_api.route("/capabilities/", methods=["GET"]) def capabilities(): return make_predictor_capabilities(self._predictor.supported_payload_formats) @model_api.route("/info/", methods=["GET"]) def info(): model_info = self._predictor.model_info() model_info.update({ModelInfoKeys.LANGUAGE: self._run_language.value}) model_info.update({ModelInfoKeys.DRUM_VERSION: drum_version}) model_info.update({ModelInfoKeys.DRUM_SERVER: "flask"}) model_info.update( {ModelInfoKeys.MODEL_METADATA: read_model_metadata_yaml(self._code_dir)} ) return model_info, HTTP_200_OK @model_api.route("/health/", methods=["GET"]) def health(): return {"message": "OK"}, HTTP_200_OK @model_api.route("/predictions/", methods=["POST"]) @model_api.route("/predict/", methods=["POST"]) def predict(): logger.debug("Entering predict() endpoint") self._stats_collector.enable() self._stats_collector.mark("start") try: response, response_status = self.do_predict_structured(logger=logger) finally: self._stats_collector.mark("finish") self._stats_collector.disable() return response, response_status @model_api.route("/transform/", methods=["POST"]) def transform(): logger.debug("Entering transform() endpoint") self._stats_collector.enable() self._stats_collector.mark("start") try: response, response_status = self.do_transform(logger=logger) finally: self._stats_collector.mark("finish") self._stats_collector.disable() return response, response_status @model_api.route("/predictionsUnstructured/", methods=["POST"]) @model_api.route("/predictUnstructured/", methods=["POST"]) def predict_unstructured(): logger.debug("Entering predict() endpoint") self._stats_collector.enable() self._stats_collector.mark("start") try: response, response_status = self.do_predict_unstructured(logger=logger) finally: self._stats_collector.mark("finish") self._stats_collector.disable() return response, response_status @model_api.route("/stats/", methods=["GET"]) def stats(): mem_info = self._memory_monitor.collect_memory_info() ret_dict = {"mem_info": mem_info._asdict()} self._stats_collector.round() ret_dict["time_info"] = {} for name in self._stats_collector.get_report_names(): d = self._stats_collector.dict_report(name) ret_dict["time_info"][name] = d self._stats_collector.stats_reset() return ret_dict, HTTP_200_OK @model_api.errorhandler(Exception) def handle_exception(e): logger.exception(e) return {"message": "ERROR: {}".format(e)}, HTTP_500_INTERNAL_SERVER_ERROR # Disables warning for development server cli = sys.modules["flask.cli"] cli.show_server_banner = lambda *x: None app = get_flask_app(model_api) host = self._params.get("host", None) port = self._params.get("port", None) try: app.run(host, port, threaded=False) except OSError as e: raise DrumCommonException("{}: host: {}; port: {}".format(e, host, port)) if self._stats_collector: self._stats_collector.print_reports() return []