Пример #1
0
def serve(port, secret):
    link = JavaLink(port, secret)
    # initiate connection
    link.connect()

    # get work to do
    try:
        # retrieve the initialization info and initiate serving
        command = link.get_command()

        function_name = command.get('functionName')
        code_file = command.get('codeFilePath')
        data_folders = command.get('resourceFolderPaths', [])

        loaded_function = LoadedFunction(code_file, function_name,
                                         data_folders)

        logging.info("Predictor ready")
        link.send_json({"ok": True})

        stored_exception = None
        # loop and process commands
        while True:
            request = link.read_json()
            if request is None:
                break

            used_api_key = request.get("usedAPIKey", None)
            if used_api_key is not None:
                os.environ["DKU_CURRENT_REQUEST_USED_API_KEY"] = used_api_key

            before = time.time()
            response = loaded_function.predict(request["params"])
            after = time.time()
            link.send_json({
                'ok': True,
                'resp': response,
                'execTimeUS': int(1000000 * (after - before))
            })

            if used_api_key is not None:
                del os.environ["DKU_CURRENT_REQUEST_USED_API_KEY"]

        # send end of stream
        logging.info("Work done")
        link.send_string('')
    except:
        logging.exception("Function user code failed")
        link.send_string('')  # send null to mark failure
        link.send_json(get_json_friendly_error())
    finally:
        # done
        link.close()
Пример #2
0
def serve(port, secret):
    link = JavaLink(port, secret)
    # initiate connection
    link.connect()
    # get work to do
    try:
        command = link.get_command()

        config = command.get("config", {})
        plugin_config = command.get("pluginConfig", {})
        code = command["code"]
        root = command["root"]

        # get the fs provider object
        clazz = get_clazz_in_code(code, FSProvider)
        arg_count = len(inspect.getargspec(clazz.__init__).args)
        fsprovider = None
        if arg_count == 1:
            fsprovider = clazz()
        elif arg_count == 2:
            fsprovider = clazz(root)
        elif arg_count == 3:
            fsprovider = clazz(root, config)
        elif arg_count == 4:
            fsprovider = clazz(root, config, plugin_config)
        else:
            reason = "Wrong signature of the FSProvider subclass: %i args" % arg_count
            link.send_json({'ok': False, 'reason': reason})
            raise Exception(reason)
        link.send_json({'ok': True})

        # loop and process commands
        closed = False
        while not closed:
            request = link.read_json()
            if request is None:
                break

            closed, response = handle_request(request, fsprovider, closed)

            link.send_json(response)

        # send end of stream
        link.send_string('')
    except:
        link.send_string('')  # mark failure
        traceback.print_exc()
        link.send_json(get_json_friendly_error())
    finally:
        # done
        link.close()
Пример #3
0
def serve(port, secret):
    link = JavaLink(port, secret)
    # initiate connection
    link.connect()
    
    # get work to do
    try:
        # retrieve the initialization info and initiate serving
        command = link.get_command()
        
        model_type = command.get('modelType')
        code_file = command.get('codeFilePath')
        data_folder = command.get('resourceFolderPath', None)
        
        loaded_model = LoadedModel(model_type, code_file, data_folder)
        
        logging.info("Predictor ready")
        link.send_json({"ok":True})

        stored_exception = None
        # loop and process commands
        while True:
            request = link.read_json()
            if request is None:
                break

            used_api_key =  request.get("usedAPIKey", None)
            if used_api_key is not None:
                os.environ["DKU_CURRENT_REQUEST_USED_API_KEY"] = used_api_key

            before = time.time()
            response = handle_predict(loaded_model, request["obj"])
            after = time.time()
            response["execTimeUS"] = int(1000000 * (after-before))
            link.send_json(response)

            if used_api_key is not None:
                del os.environ["DKU_CURRENT_REQUEST_USED_API_KEY"]
            
        # send end of stream
        logging.info("Work done")
        link.send_string('')
    except:
        ex_type, ex, tb = sys.exc_info()
        traceback.print_exc()
        link.send_string('') # send null to mark failure
        link.send_json({'errorType': str(ex_type), 'message':str(ex), 'traceback':traceback.extract_tb(tb)})
    finally:
        # done
        link.close()
Пример #4
0
def serve(port, secret):
    link = JavaLink(port, secret)
    # initiate connection
    link.connect()
    
    # get work to do
    try:
        # retrieve the initialization info and initiate serving
        command = link.get_command()
        model_folder = command.get('modelFolder')
        try:
            conditional_outputs = load_from_filepath(osp.join(model_folder, "conditional_outputs.json"))
        except Exception as e:
            logging.exception("Can't load conditional outputs: " + str(e))
            conditional_outputs = []
        predictor = build_predictor_for_saved_model(model_folder, "PREDICTION", conditional_outputs)
        logging.info("Predictor ready")
        link.send_json({"ok":True})

        stored_exception = None
        # loop and process commands
        while True:
            request = link.read_json()
            if request is None:
                break

            before = time.time()
            response = handle_predict(predictor, request)
            after = time.time()
            response["execTimeUS"] = int(1000000 * (after-before))
            link.send_json(response)
            
        # send end of stream
        logging.info("Work done")
        link.send_string('')
    except:
        logging.exception("Prediction user code failed")
        link.send_string('') # send null to mark failure
        link.send_json(get_json_friendly_error())
    finally:
        # done
        link.close()
Пример #5
0
def serve(port, secret):
    link = JavaLink(port, secret)
    # initiate connection
    link.connect()
    # get work to do
    try:
        command = link.get_command()
        config = command.get("config", {})
        plugin_config = command.get("pluginConfig", {})
        code = command["code"]

        # get the connector object
        clazz = get_clazz_in_code(code, Connector)
        arg_count = len(inspect.getargspec(clazz.__init__).args)
        connector = None
        if arg_count == 1:
            connector = clazz()
        elif arg_count == 2:
            connector = clazz(config)
        elif arg_count == 3:
            connector = clazz(config, plugin_config)
        else:
            raise Exception(
                "Wrong signature of the Connector subclass: %i args" %
                arg_count)

        link.send_json({'ok': True})

        stored_error = None
        # loop and process commands
        while True:
            request = link.read_json()
            if request is None:
                break

            response = None
            task = request["task"]
            logging.info("Processing task: %s" % task)
            if task == "read_rows":
                schema = request.get("schema", None)
                partitioning = request.get("partitioning", None)
                partition_id = request.get("partitionId", None)
                limit = request.get("limit", None)
                stored_error = None
                try:
                    with link.get_output() as output:
                        read_rows(connector, schema, partitioning,
                                  partition_id, limit, output)
                except:
                    logging.exception("Connector send fail, storing exception")
                    stored_error = get_json_friendly_error()
                link.send_string('')
            elif task == "finish_read_session":
                if stored_error is None:
                    link.send_json({"ok": True})
                else:
                    link.send_json({"ok": False, "error": stored_error})
            elif task == "write_rows":
                schema = request.get("schema", None)
                partitioning = request.get("partitioning", None)
                partition_id = request.get("partitionId", None)
                with link.get_input() as input:
                    write_rows(connector, schema, partitioning, partition_id,
                               input)
                link.send_json({'ok': True})
            elif task == "get_schema":
                link.send_json({'schema': connector.get_read_schema()})
            elif task == "get_partitioning_scheme":
                link.send_json({'partitioning': connector.get_partitioning()})
            elif task == "list_partitions":
                partitioning = request.get("partitioning", None)
                link.send_json(
                    {'partitions': connector.list_partitions(partitioning)})
            elif task == "partition_exists":
                partitioning = request.get("partitioning", None)
                partition_id = request.get("partitionId", None)
                link.send_json({
                    "exists":
                    connector.partition_exists(partitioning, partition_id)
                })
            elif task == "records_count":
                partitioning = request.get("partitioning", None)
                partition_id = request.get("partitionId", None)
                link.send_json({
                    "count":
                    connector.get_records_count(partitioning, partition_id)
                })
            else:
                raise Exception("Unexpected task %s" % task)

        # send end of stream
        logging.info("Work done")
        link.send_string('')
    except:
        link.send_string('')  # mark failure
        traceback.print_exc()
        link.send_json(get_json_friendly_error())
    finally:
        # done
        link.close()
Пример #6
0
def serve(port, secret):
    link = JavaLink(port, secret)
    # initiate connection
    link.connect()
    is_serving = False
    # get work to do
    try:
        command = link.get_command()
        plugin_config = command.get("pluginConfig", {})
        code = command["code"]

        # get the helper function
        ctx = {}
        python2_friendly_exec(code, ctx, ctx)

        functions = [o for o in ctx.values() if inspect.isfunction(o)]
        f = functions[0] if len(functions) == 1 else ctx.get('do', None)

        if f is None:
            raise Exception('No function "do" defined')
        f_args_count = len(inspect.getargspec(f).args)
        if f_args_count >= 5:
            reason = "Too many arguments for the do() function : %i args" % f_args_count
            raise Exception(reason)
        link.send_json({'ok': True})

        def call_do(payload, config, plugin_config, inputs):
            result = None
            if f_args_count == 0:
                result = f()
            if f_args_count == 1:
                result = f(payload)
            if f_args_count == 2:
                result = f(payload, config)
            if f_args_count == 3:
                result = f(payload, config, plugin_config)
            if f_args_count == 4:
                result = f(payload, config, plugin_config, inputs)
            return result

        is_serving = True
        # loop and process commands
        while True:
            request = link.read_json()
            if request is None:
                break

            response = call_do(request.get('payload', None),
                               request.get('config', {}), plugin_config,
                               request.get('inputs', []))
            if response is None:
                raise Exception("Empty response to %s" % json.dumps(request))

            link.send_json(response)

        # send end of stream
        link.send_string('')
    except:
        error = get_json_friendly_error()
        link.send_string('')  # mark failure
        traceback.print_exc()
        if not is_serving:
            link.send_json(error)
        else:
            link.send_json({'ok': False, 'error': error})
    finally:
        # done
        link.close()