Search in sources :

Example 1 with FunctionMetaData

use of org.apache.pulsar.functions.proto.Function.FunctionMetaData in project incubator-pulsar by apache.

the class FunctionApiV2ResourceTest method testGetFunctionSuccess.

@Test
public void testGetFunctionSuccess() throws Exception {
    when(mockedManager.containsFunction(eq(tenant), eq(namespace), eq(function))).thenReturn(true);
    FunctionConfig functionConfig = FunctionConfig.newBuilder().setClassName(className).putCustomSerdeInputs(inputTopic, inputSerdeClassName).setOutputSerdeClassName(outputSerdeClassName).setName(function).setNamespace(namespace).setProcessingGuarantees(FunctionConfig.ProcessingGuarantees.ATMOST_ONCE).setOutput(outputTopic).setTenant(tenant).setParallelism(parallelism).build();
    FunctionMetaData metaData = FunctionMetaData.newBuilder().setCreateTime(System.currentTimeMillis()).setFunctionConfig(functionConfig).setPackageLocation(PackageLocationMetaData.newBuilder().setPackagePath("/path/to/package")).setVersion(1234).build();
    when(mockedManager.getFunctionMetaData(eq(tenant), eq(namespace), eq(function))).thenReturn(metaData);
    Response response = getDefaultFunctionInfo();
    assertEquals(Status.OK.getStatusCode(), response.getStatus());
    assertEquals(org.apache.pulsar.functions.utils.Utils.printJson(functionConfig), response.getEntity());
}
Also used : FunctionConfig(org.apache.pulsar.functions.proto.Function.FunctionConfig) FunctionMetaData(org.apache.pulsar.functions.proto.Function.FunctionMetaData) Response(javax.ws.rs.core.Response) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 2 with FunctionMetaData

use of org.apache.pulsar.functions.proto.Function.FunctionMetaData in project incubator-pulsar by apache.

the class FunctionMetaDataManager method updateFunction.

/**
 * Sends an update request to the FMT (Function Metadata Topic)
 * @param functionMetaData The function metadata that needs to be updated
 * @return a completable future of when the update has been applied
 */
public synchronized CompletableFuture<RequestResult> updateFunction(FunctionMetaData functionMetaData) {
    long version = 0;
    String tenant = functionMetaData.getFunctionConfig().getTenant();
    if (!this.functionMetaDataMap.containsKey(tenant)) {
        this.functionMetaDataMap.put(tenant, new ConcurrentHashMap<>());
    }
    Map<String, Map<String, FunctionMetaData>> namespaces = this.functionMetaDataMap.get(tenant);
    String namespace = functionMetaData.getFunctionConfig().getNamespace();
    if (!namespaces.containsKey(namespace)) {
        namespaces.put(namespace, new ConcurrentHashMap<>());
    }
    Map<String, FunctionMetaData> functionMetaDatas = namespaces.get(namespace);
    String functionName = functionMetaData.getFunctionConfig().getName();
    if (functionMetaDatas.containsKey(functionName)) {
        version = functionMetaDatas.get(functionName).getVersion() + 1;
    }
    FunctionMetaData newFunctionMetaData = functionMetaData.toBuilder().setVersion(version).build();
    Request.ServiceRequest updateRequest = ServiceRequestUtils.getUpdateRequest(this.workerConfig.getWorkerId(), newFunctionMetaData);
    return submit(updateRequest);
}
Also used : FunctionMetaData(org.apache.pulsar.functions.proto.Function.FunctionMetaData) Request(org.apache.pulsar.functions.proto.Request) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Map(java.util.Map)

Example 3 with FunctionMetaData

use of org.apache.pulsar.functions.proto.Function.FunctionMetaData in project incubator-pulsar by apache.

the class FunctionMetaDataManager method isRequestOutdated.

private boolean isRequestOutdated(Request.ServiceRequest serviceRequest) {
    FunctionMetaData requestFunctionMetaData = serviceRequest.getFunctionMetaData();
    Function.FunctionConfig functionConfig = requestFunctionMetaData.getFunctionConfig();
    FunctionMetaData currentFunctionMetaData = this.functionMetaDataMap.get(functionConfig.getTenant()).get(functionConfig.getNamespace()).get(functionConfig.getName());
    return currentFunctionMetaData.getVersion() >= requestFunctionMetaData.getVersion();
}
Also used : FunctionMetaData(org.apache.pulsar.functions.proto.Function.FunctionMetaData) Function(org.apache.pulsar.functions.proto.Function)

Example 4 with FunctionMetaData

use of org.apache.pulsar.functions.proto.Function.FunctionMetaData 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 5 with FunctionMetaData

use of org.apache.pulsar.functions.proto.Function.FunctionMetaData 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)

Aggregations

FunctionMetaData (org.apache.pulsar.functions.proto.Function.FunctionMetaData)11 Function (org.apache.pulsar.functions.proto.Function)4 Request (org.apache.pulsar.functions.proto.Request)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 File (java.io.File)2 IOException (java.io.IOException)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 Path (javax.ws.rs.Path)2 MessageId (org.apache.pulsar.client.api.MessageId)2 Producer (org.apache.pulsar.client.api.Producer)2 ErrorData (org.apache.pulsar.common.policies.data.ErrorData)2 FunctionConfig (org.apache.pulsar.functions.proto.Function.FunctionConfig)2 FunctionMetaDataManager (org.apache.pulsar.functions.worker.FunctionMetaDataManager)2 FileOutputStream (java.io.FileOutputStream)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1