def assert_config_works(config): config_json = config_to_dict_with_type(config) json_str = json.dumps(config_json) config = InferenceConfigurationJava.fromJson(StringJava(json_str)) test_json = config.toJson() test_json_dict = json.loads(test_json) config_json_dict = config_json test_pipeline_steps = test_json_dict["steps"] config_pipeline_steps = config_json_dict["steps"] assert len(test_pipeline_steps) == len(config_pipeline_steps)
def test_build_tp(output_format): schema = pydatavec.Schema() schema.add_string_column("first") tp = pydatavec.TransformProcess(schema) tp.append_string("first", "two") java_tp = tp.to_java() tp_json = java_tp.toJson() load_java_tp(tp_json) _ = json.dumps(tp_json) as_python_json = json.loads(tp_json) transform_process = (TransformProcessStep().set_input( column_names=["first"], types=["String" ]).set_output(column_names=["first"], types=["String" ]).transform_process(as_python_json)) port = random.randint(1000, 65535) serving_config = ServingConfig( http_port=port, input_data_format="JSON", output_data_format=output_format, log_timings=True, ) inference_config = InferenceConfiguration(serving_config=serving_config, steps=[transform_process]) as_json = config_to_dict_with_type(inference_config) inference_from_json(as_json) server = Server( inference_config=inference_config, extra_start_args="-Xmx8g", jar_path="konduit.jar", ) server.start() client = server.get_client() assert is_port_in_use(port) try: predicted = client.predict({"first": "value"}) print(predicted) server.stop() except Exception as e: print(e) server.stop()
def test_python_serde(): python_configuration = PythonConfig(python_code="first += 2", python_inputs=["first"], python_outputs=["first"]) port = random.randint(1000, 65535) serving_config = ServingConfig( http_port=port, output_data_format="NUMPY", log_timings=True, ) python_pipeline_step = PythonStep().step(python_configuration) inference_config = InferenceConfiguration(serving_config=serving_config, steps=[python_pipeline_step]) json.dumps(config_to_dict_with_type(inference_config))
def start(self, server_id=None): """Start the Konduit server :param server_id the server will be started with this id. """ if not server_id: server_id = self.server_id else: self.server_id = server_id json_config = config_to_dict_with_type(self.config) with open(self.config_path, "w") as f: abs_path = os.path.abspath(self.config_path) logging.info("Wrote config.yaml to path " + abs_path) json.dump(json_config, f) args = self._process_extra_args(abs_path, server_id) process = subprocess.Popen(args=args, stdout=subprocess.PIPE) self.process = process # Check if the server is up or not. request_timeout = 5 started = False print("Starting server") port = -1 while True: output = process.stdout.readline() if output == b'' and process.poll() is not None: break else: print(output.decode("utf-8"), end="") if b'Inference server started on port' in output: m = re.search('port (.+?) with', str(output)) if m: port = int(m.group(1)) self.port = port break if port != -1: try: r = requests.get( "http://localhost:{}/healthcheck".format(port), timeout=request_timeout, ) if r.status_code == 204: started = True except Exception as ex: logging.debug("Server health checks failed...\n{}".format( str(ex))) process = process.poll() if started: print( "\n\nServer has started successfully on port {}".format(port)) else: print("\n\nThe server wasn't able to start.") self.stop() return process, port, started
def start(self, kill_existing_server=True): """Start the Konduit server :param kill_existing_server: whether to kill any previously started server if it wasn't stop. """ if kill_existing_server: if os.path.exists(self.pid_file_path): with open(self.pid_file_path, "rb") as pid_file: pid = int(pid_file.readline().strip()) try: stop_server_by_pid(pid) except OSError: logging.debug( "Attempt to kill existing process by pid: '{}' failed. The process might not " "exist. ".format(pid)) os.remove(self.pid_file_path) json_config = config_to_dict_with_type(self.config) with open(self.config_path, "w") as f: abs_path = os.path.abspath(self.config_path) logging.info("Wrote config.json to path " + abs_path) json.dump(json_config, f) args = self._process_extra_args(abs_path) process = subprocess.Popen(args=args) self.process = process # Check if the server is up or not. request_timeout = 5 tries = int(self.start_timeout / request_timeout + 1) started = False print("Starting server", end="") for i in range(tries): start_time = time.time() # This url returns status 204 with no content when the server is up. try: """ This would look like the following on success: ------ Starting server... Server has started successfully. ------ and like this on failure ------ Starting server... The server wasn't able to start. ------ """ print(".", end="") logging.debug( "Checking server integrity. Tries: {} of {}".format( i + 1, tries)) r = requests.get( "http://localhost:{}/healthcheck".format( self.config.serving_config.http_port), timeout=request_timeout, ) if r.status_code == 204: started = True break except Exception as ex: logging.debug("{}\nChecking server integrity again...".format( str(ex))) time_taken = time.time() - start_time if time_taken < request_timeout: time.sleep( request_timeout - time_taken ) # Making sure the loop takes exactly "request_timeout" seconds if started: print("\n\nServer has started successfully.") else: print("\n\nThe server wasn't able to start.") self.stop() return process
def start(self, kill_existing_server=True): """Start the Konduit server :param kill_existing_server: whether to kill any previously started server if it wasn't stop. """ if kill_existing_server: if os.path.exists(self.pid_file_path): with open(self.pid_file_path, "rb") as pid_file: pid = int(pid_file.readline().strip()) try: stop_server_by_pid(pid) except OSError: logging.debug( "Attempt to kill existing process by pid: '{}' failed. The process might not " "exist. ".format(pid)) os.remove(self.pid_file_path) json_config = config_to_dict_with_type(self.config) with open(self.config_path, "w") as f: abs_path = os.path.abspath(self.config_path) logging.info("Wrote config.json to path " + abs_path) json.dump(json_config, f) args = self._process_extra_args(abs_path) process = subprocess.Popen(args=args, stdout=subprocess.PIPE) self.process = process # Check if the server is up or not. request_timeout = 5 started = False print("Starting server") port = -1 while True: output = process.stdout.readline() if output == b'' and process.poll() is not None: break else: print(output.decode("utf-8"), end="") if b'Inference server started on port' in output: m = re.search('port (.+?) with', str(output)) if m: port = int(m.group(1)) self.port = port break if port != -1: try: r = requests.get( "http://localhost:{}/healthcheck".format(port), timeout=request_timeout, ) if r.status_code == 204: started = True except Exception as ex: logging.debug("Server health checks failed...\n{}".format( str(ex))) process = process.poll() if started: print( "\n\nServer has started successfully on port {}".format(port)) else: print("\n\nThe server wasn't able to start.") self.stop() return process, port, started