Search in sources :

Example 1 with FunctionMetaDataManager

use of org.apache.pulsar.functions.worker.FunctionMetaDataManager in project incubator-pulsar by apache.

the class FunctionsImpl method getFunctionInstanceStatus.

@GET
@Path("/{tenant}/{namespace}/{functionName}/{instanceId}/status")
public Response getFunctionInstanceStatus(@PathParam("tenant") final String tenant, @PathParam("namespace") final String namespace, @PathParam("functionName") final String functionName, @PathParam("instanceId") final String instanceId) throws IOException {
    // validate parameters
    try {
        validateGetFunctionInstanceRequestParams(tenant, namespace, functionName, instanceId);
    } catch (IllegalArgumentException e) {
        log.error("Invalid getFunctionStatus request @ /{}/{}/{}", tenant, namespace, functionName, e);
        return Response.status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity(new ErrorData(e.getMessage())).build();
    }
    FunctionMetaDataManager functionMetaDataManager = worker().getFunctionMetaDataManager();
    if (!functionMetaDataManager.containsFunction(tenant, namespace, functionName)) {
        log.error("Function in getFunctionStatus does not exist @ /{}/{}/{}", tenant, namespace, functionName);
        return Response.status(Status.NOT_FOUND).type(MediaType.APPLICATION_JSON).entity(new ErrorData(String.format("Function %s doesn't exist", functionName))).build();
    }
    FunctionRuntimeManager functionRuntimeManager = worker().getFunctionRuntimeManager();
    FunctionStatus functionStatus = null;
    try {
        functionStatus = functionRuntimeManager.getFunctionInstanceStatus(tenant, namespace, functionName, Integer.parseInt(instanceId));
    } catch (Exception e) {
        log.error("Got Exception Getting Status", e);
        FunctionStatus.Builder functionStatusBuilder = FunctionStatus.newBuilder();
        functionStatusBuilder.setRunning(false);
        String functionConfigJson = org.apache.pulsar.functions.utils.Utils.printJson(functionStatusBuilder.build());
        return Response.status(Status.OK).entity(functionConfigJson).build();
    }
    String jsonResponse = org.apache.pulsar.functions.utils.Utils.printJson(functionStatus);
    return Response.status(Status.OK).entity(jsonResponse).build();
}
Also used : FunctionStatus(org.apache.pulsar.functions.proto.InstanceCommunication.FunctionStatus) FunctionMetaDataManager(org.apache.pulsar.functions.worker.FunctionMetaDataManager) ErrorData(org.apache.pulsar.common.policies.data.ErrorData) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) FunctionRuntimeManager(org.apache.pulsar.functions.worker.FunctionRuntimeManager) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 2 with FunctionMetaDataManager

use of org.apache.pulsar.functions.worker.FunctionMetaDataManager in project incubator-pulsar by apache.

the class FunctionsImpl method getFunctionInfo.

@GET
@Path("/{tenant}/{namespace}/{functionName}")
public Response getFunctionInfo(@PathParam("tenant") final String tenant, @PathParam("namespace") final String namespace, @PathParam("functionName") final String functionName) throws IOException {
    // validate parameters
    try {
        validateGetFunctionRequestParams(tenant, namespace, functionName);
    } catch (IllegalArgumentException e) {
        log.error("Invalid getFunction request @ /{}/{}/{}", tenant, namespace, functionName, e);
        return Response.status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity(new ErrorData(e.getMessage())).build();
    }
    FunctionMetaDataManager functionMetaDataManager = worker().getFunctionMetaDataManager();
    if (!functionMetaDataManager.containsFunction(tenant, namespace, functionName)) {
        log.error("Function in getFunction does not exist @ /{}/{}/{}", tenant, namespace, functionName);
        return Response.status(Status.NOT_FOUND).type(MediaType.APPLICATION_JSON).entity(new ErrorData(String.format("Function %s doesn't exist", functionName))).build();
    }
    FunctionMetaData functionMetaData = functionMetaDataManager.getFunctionMetaData(tenant, namespace, functionName);
    String functionConfigJson = org.apache.pulsar.functions.utils.Utils.printJson(functionMetaData.getFunctionConfig());
    return Response.status(Status.OK).entity(functionConfigJson).build();
}
Also used : FunctionMetaData(org.apache.pulsar.functions.proto.Function.FunctionMetaData) FunctionMetaDataManager(org.apache.pulsar.functions.worker.FunctionMetaDataManager) ErrorData(org.apache.pulsar.common.policies.data.ErrorData) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 3 with FunctionMetaDataManager

use of org.apache.pulsar.functions.worker.FunctionMetaDataManager in project incubator-pulsar by apache.

the class FunctionsImpl method triggerFunction.

@POST
@Path("/{tenant}/{namespace}/{functionName}/trigger")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response triggerFunction(@PathParam("tenant") final String tenant, @PathParam("namespace") final String namespace, @PathParam("name") final String functionName, @FormDataParam("data") final String input, @FormDataParam("dataStream") final InputStream uploadedInputStream) {
    FunctionConfig functionConfig;
    // validate parameters
    try {
        validateTriggerRequestParams(tenant, namespace, functionName, input, uploadedInputStream);
    } catch (IllegalArgumentException e) {
        log.error("Invalid trigger function request @ /{}/{}/{}", tenant, namespace, functionName, e);
        return Response.status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity(new ErrorData(e.getMessage())).build();
    }
    FunctionMetaDataManager functionMetaDataManager = worker().getFunctionMetaDataManager();
    if (!functionMetaDataManager.containsFunction(tenant, namespace, functionName)) {
        log.error("Function in getFunction does not exist @ /{}/{}/{}", tenant, namespace, functionName);
        return Response.status(Status.NOT_FOUND).type(MediaType.APPLICATION_JSON).entity(new ErrorData(String.format("Function %s doesn't exist", functionName))).build();
    }
    FunctionMetaData functionMetaData = functionMetaDataManager.getFunctionMetaData(tenant, namespace, functionName);
    String inputTopicToWrite;
    if (functionMetaData.getFunctionConfig().getInputsList().size() > 0) {
        inputTopicToWrite = functionMetaData.getFunctionConfig().getInputsList().get(0);
    } else {
        inputTopicToWrite = functionMetaData.getFunctionConfig().getCustomSerdeInputs().entrySet().iterator().next().getKey();
    }
    String outputTopic = functionMetaData.getFunctionConfig().getOutput();
    Reader reader = null;
    Producer producer = null;
    try {
        if (outputTopic != null && !outputTopic.isEmpty()) {
            reader = worker().getClient().newReader().topic(outputTopic).startMessageId(MessageId.latest).create();
        }
        producer = worker().getClient().newProducer().topic(inputTopicToWrite).create();
        byte[] targetArray;
        if (uploadedInputStream != null) {
            targetArray = new byte[uploadedInputStream.available()];
            uploadedInputStream.read(targetArray);
        } else {
            targetArray = input.getBytes();
        }
        MessageId msgId = producer.send(targetArray);
        if (reader == null) {
            return Response.status(Status.OK).build();
        }
        long curTime = System.currentTimeMillis();
        long maxTime = curTime + 1000;
        while (curTime < maxTime) {
            Message msg = reader.readNext(10000, TimeUnit.MILLISECONDS);
            if (msg == null)
                break;
            if (msg.getProperties().containsKey("__pfn_input_msg_id__") && msg.getProperties().containsKey("__pfn_input_topic__")) {
                MessageId newMsgId = MessageId.fromByteArray(Base64.getDecoder().decode((String) msg.getProperties().get("__pfn_input_msg_id__")));
                if (msgId.equals(newMsgId) && msg.getProperties().get("__pfn_input_topic__").equals(inputTopicToWrite)) {
                    return Response.status(Status.OK).entity(msg.getData()).build();
                }
            }
            curTime = System.currentTimeMillis();
        }
        return Response.status(Status.REQUEST_TIMEOUT).build();
    } catch (Exception e) {
        return Response.status(Status.INTERNAL_SERVER_ERROR).build();
    } finally {
        if (reader != null) {
            reader.closeAsync();
        }
        if (producer != null) {
            producer.closeAsync();
        }
    }
}
Also used : FunctionConfig(org.apache.pulsar.functions.proto.Function.FunctionConfig) FunctionMetaData(org.apache.pulsar.functions.proto.Function.FunctionMetaData) Message(org.apache.pulsar.client.api.Message) Reader(org.apache.pulsar.client.api.Reader) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Producer(org.apache.pulsar.client.api.Producer) FunctionMetaDataManager(org.apache.pulsar.functions.worker.FunctionMetaDataManager) ErrorData(org.apache.pulsar.common.policies.data.ErrorData) MessageId(org.apache.pulsar.client.api.MessageId) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes)

Example 4 with FunctionMetaDataManager

use of org.apache.pulsar.functions.worker.FunctionMetaDataManager in project incubator-pulsar by apache.

the class FunctionsImpl method updateRequest.

private Response updateRequest(FunctionMetaData functionMetaData, InputStream uploadedInputStream) {
    // Upload to bookkeeper
    try {
        log.info("Uploading function package to {}", functionMetaData.getPackageLocation());
        Utils.uploadToBookeeper(worker().getDlogNamespace(), uploadedInputStream, functionMetaData.getPackageLocation().getPackagePath());
    } catch (IOException e) {
        log.error("Error uploading file {}", functionMetaData.getPackageLocation(), e);
        return Response.serverError().type(MediaType.APPLICATION_JSON).entity(new ErrorData(e.getMessage())).build();
    }
    // Submit to FMT
    FunctionMetaDataManager functionMetaDataManager = worker().getFunctionMetaDataManager();
    CompletableFuture<RequestResult> completableFuture = functionMetaDataManager.updateFunction(functionMetaData);
    RequestResult requestResult = null;
    try {
        requestResult = completableFuture.get();
        if (!requestResult.isSuccess()) {
            return Response.status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity(new ErrorData(requestResult.getMessage())).build();
        }
    } catch (ExecutionException e) {
        return Response.serverError().type(MediaType.APPLICATION_JSON).entity(new ErrorData(e.getCause().getMessage())).build();
    } catch (InterruptedException e) {
        return Response.status(Status.REQUEST_TIMEOUT).type(MediaType.APPLICATION_JSON).entity(new ErrorData(e.getCause().getMessage())).build();
    }
    return Response.status(Status.OK).build();
}
Also used : RequestResult(org.apache.pulsar.functions.worker.request.RequestResult) FunctionMetaDataManager(org.apache.pulsar.functions.worker.FunctionMetaDataManager) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ErrorData(org.apache.pulsar.common.policies.data.ErrorData)

Example 5 with FunctionMetaDataManager

use of org.apache.pulsar.functions.worker.FunctionMetaDataManager in project incubator-pulsar by apache.

the class FunctionsImpl method updateFunction.

@PUT
@Path("/{tenant}/{namespace}/{functionName}")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response updateFunction(@PathParam("tenant") final String tenant, @PathParam("namespace") final String namespace, @PathParam("functionName") final String functionName, @FormDataParam("data") final InputStream uploadedInputStream, @FormDataParam("data") final FormDataContentDisposition fileDetail, @FormDataParam("functionConfig") final String functionConfigJson) {
    FunctionConfig functionConfig;
    // validate parameters
    try {
        functionConfig = validateUpdateRequestParams(tenant, namespace, functionName, uploadedInputStream, fileDetail, functionConfigJson);
    } catch (IllegalArgumentException e) {
        log.error("Invalid update function request @ /{}/{}/{}", tenant, namespace, functionName, e);
        return Response.status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity(new ErrorData(e.getMessage())).build();
    }
    FunctionMetaDataManager functionMetaDataManager = worker().getFunctionMetaDataManager();
    if (!functionMetaDataManager.containsFunction(tenant, namespace, functionName)) {
        return Response.status(Status.BAD_REQUEST).type(MediaType.APPLICATION_JSON).entity(new ErrorData(String.format("Function %s doesn't exist", functionName))).build();
    }
    // function state
    FunctionMetaData.Builder functionMetaDataBuilder = FunctionMetaData.newBuilder().setFunctionConfig(functionConfig).setCreateTime(System.currentTimeMillis()).setVersion(0);
    PackageLocationMetaData.Builder packageLocationMetaDataBuilder = PackageLocationMetaData.newBuilder().setPackagePath(String.format("%s/%s/%s/%s", tenant, namespace, functionName, Utils.getUniquePackageName(fileDetail.getFileName())));
    functionMetaDataBuilder.setPackageLocation(packageLocationMetaDataBuilder);
    return updateRequest(functionMetaDataBuilder.build(), uploadedInputStream);
}
Also used : FunctionConfig(org.apache.pulsar.functions.proto.Function.FunctionConfig) FunctionMetaData(org.apache.pulsar.functions.proto.Function.FunctionMetaData) FunctionMetaDataManager(org.apache.pulsar.functions.worker.FunctionMetaDataManager) PackageLocationMetaData(org.apache.pulsar.functions.proto.Function.PackageLocationMetaData) ErrorData(org.apache.pulsar.common.policies.data.ErrorData) Path(javax.ws.rs.Path) Consumes(javax.ws.rs.Consumes) PUT(javax.ws.rs.PUT)

Aggregations

ErrorData (org.apache.pulsar.common.policies.data.ErrorData)9 FunctionMetaDataManager (org.apache.pulsar.functions.worker.FunctionMetaDataManager)9 Path (javax.ws.rs.Path)8 ExecutionException (java.util.concurrent.ExecutionException)5 IOException (java.io.IOException)4 GET (javax.ws.rs.GET)4 FunctionMetaData (org.apache.pulsar.functions.proto.Function.FunctionMetaData)4 Consumes (javax.ws.rs.Consumes)3 FunctionConfig (org.apache.pulsar.functions.proto.Function.FunctionConfig)3 POST (javax.ws.rs.POST)2 PackageLocationMetaData (org.apache.pulsar.functions.proto.Function.PackageLocationMetaData)2 FunctionRuntimeManager (org.apache.pulsar.functions.worker.FunctionRuntimeManager)2 RequestResult (org.apache.pulsar.functions.worker.request.RequestResult)2 Gson (com.google.gson.Gson)1 DELETE (javax.ws.rs.DELETE)1 PUT (javax.ws.rs.PUT)1 Message (org.apache.pulsar.client.api.Message)1 MessageId (org.apache.pulsar.client.api.MessageId)1 Producer (org.apache.pulsar.client.api.Producer)1 Reader (org.apache.pulsar.client.api.Reader)1