Search in sources :

Example 96 with RestException

use of org.apache.pulsar.common.util.RestException in project incubator-pulsar by apache.

the class ComponentImpl method getFunctionState.

@Override
public FunctionState getFunctionState(final String tenant, final String namespace, final String functionName, final String key, final String clientRole, final AuthenticationDataSource clientAuthenticationDataHttps) {
    if (!isWorkerServiceAvailable()) {
        throwUnavailableException();
    }
    try {
        if (!isAuthorizedRole(tenant, namespace, clientRole, clientAuthenticationDataHttps)) {
            log.warn("{}/{}/{} Client [{}] is not authorized to get state for {}", tenant, namespace, functionName, clientRole, ComponentTypeUtils.toString(componentType));
            throw new RestException(Status.UNAUTHORIZED, "Client is not authorized to perform operation");
        }
    } catch (PulsarAdminException e) {
        log.error("{}/{}/{} Failed to authorize [{}]", tenant, namespace, functionName, e);
        throw new RestException(Status.INTERNAL_SERVER_ERROR, e.getMessage());
    }
    if (null == worker().getStateStoreAdminClient()) {
        throwStateStoreUnvailableResponse();
    }
    // validate parameters
    try {
        validateFunctionStateParams(tenant, namespace, functionName, key);
    } catch (IllegalArgumentException e) {
        log.error("Invalid getFunctionState request @ /{}/{}/{}/{}", tenant, namespace, functionName, key, e);
        throw new RestException(Status.BAD_REQUEST, e.getMessage());
    }
    String tableNs = getStateNamespace(tenant, namespace);
    String tableName = functionName;
    String stateStorageServiceUrl = worker().getWorkerConfig().getStateStorageServiceUrl();
    if (storageClient.get() == null) {
        storageClient.compareAndSet(null, StorageClientBuilder.newBuilder().withSettings(StorageClientSettings.newBuilder().serviceUri(stateStorageServiceUrl).clientName("functions-admin").build()).withNamespace(tableNs).build());
    }
    FunctionState value;
    try (Table<ByteBuf, ByteBuf> table = result(storageClient.get().openTable(tableName))) {
        try (KeyValue<ByteBuf, ByteBuf> kv = result(table.getKv(Unpooled.wrappedBuffer(key.getBytes(UTF_8))))) {
            if (null == kv) {
                throw new RestException(Status.NOT_FOUND, "key '" + key + "' doesn't exist.");
            } else {
                if (kv.isNumber()) {
                    value = new FunctionState(key, null, null, kv.numberValue(), kv.version());
                } else {
                    try {
                        value = new FunctionState(key, new String(ByteBufUtil.getBytes(kv.value(), kv.value().readerIndex(), kv.value().readableBytes()), UTF_8), null, null, kv.version());
                    } catch (Exception e) {
                        value = new FunctionState(key, null, ByteBufUtil.getBytes(kv.value()), null, kv.version());
                    }
                }
            }
        }
    } catch (RestException e) {
        throw e;
    } catch (org.apache.bookkeeper.clients.exceptions.NamespaceNotFoundException | StreamNotFoundException e) {
        log.debug("State not found while processing getFunctionState request @ /{}/{}/{}/{}", tenant, namespace, functionName, key, e);
        throw new RestException(Status.NOT_FOUND, e.getMessage());
    } catch (Exception e) {
        log.error("Error while getFunctionState request @ /{}/{}/{}/{}", tenant, namespace, functionName, key, e);
        throw new RestException(Status.INTERNAL_SERVER_ERROR, e.getMessage());
    }
    return value;
}
Also used : FunctionState(org.apache.pulsar.common.functions.FunctionState) RestException(org.apache.pulsar.common.util.RestException) ByteBuf(io.netty.buffer.ByteBuf) RestUtils.throwUnavailableException(org.apache.pulsar.functions.worker.rest.RestUtils.throwUnavailableException) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) WebApplicationException(javax.ws.rs.WebApplicationException) NamespaceNotFoundException(org.apache.bookkeeper.clients.exceptions.NamespaceNotFoundException) RestException(org.apache.pulsar.common.util.RestException) StreamNotFoundException(org.apache.bookkeeper.clients.exceptions.StreamNotFoundException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) NamespaceNotFoundException(org.apache.bookkeeper.clients.exceptions.NamespaceNotFoundException) StreamNotFoundException(org.apache.bookkeeper.clients.exceptions.StreamNotFoundException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException)

Example 97 with RestException

use of org.apache.pulsar.common.util.RestException in project incubator-pulsar by apache.

the class ComponentImpl method componentInstanceStatusRequestValidate.

protected void componentInstanceStatusRequestValidate(final String tenant, final String namespace, final String componentName, final int instanceId, final String clientRole, final AuthenticationDataSource clientAuthenticationDataHttps) {
    componentStatusRequestValidate(tenant, namespace, componentName, clientRole, clientAuthenticationDataHttps);
    FunctionMetaDataManager functionMetaDataManager = worker().getFunctionMetaDataManager();
    FunctionMetaData functionMetaData = functionMetaDataManager.getFunctionMetaData(tenant, namespace, componentName);
    int parallelism = functionMetaData.getFunctionDetails().getParallelism();
    if (instanceId < 0 || instanceId >= parallelism) {
        log.error("instanceId in get {} Status out of bounds @ /{}/{}/{}", ComponentTypeUtils.toString(componentType), tenant, namespace, componentName);
        throw new RestException(Status.BAD_REQUEST, String.format("%s %s doesn't have instance with id %s", ComponentTypeUtils.toString(componentType), componentName, instanceId));
    }
}
Also used : FunctionMetaData(org.apache.pulsar.functions.proto.Function.FunctionMetaData) FunctionMetaDataManager(org.apache.pulsar.functions.worker.FunctionMetaDataManager) RestException(org.apache.pulsar.common.util.RestException)

Example 98 with RestException

use of org.apache.pulsar.common.util.RestException in project incubator-pulsar by apache.

the class ComponentImpl method changeFunctionInstanceStatus.

public void changeFunctionInstanceStatus(final String tenant, final String namespace, final String componentName, final String instanceId, final boolean start, final URI uri, final String clientRole, final AuthenticationDataSource clientAuthenticationDataHttps) {
    if (!isWorkerServiceAvailable()) {
        throwUnavailableException();
    }
    try {
        if (!isAuthorizedRole(tenant, namespace, clientRole, clientAuthenticationDataHttps)) {
            log.warn("{}/{}/{} Client [{}] is not authorized to start/stop {}", tenant, namespace, componentName, clientRole, ComponentTypeUtils.toString(componentType));
            throw new RestException(Status.UNAUTHORIZED, "Client is not authorized to perform operation");
        }
    } catch (PulsarAdminException e) {
        log.error("{}/{}/{} Failed to authorize [{}]", tenant, namespace, componentName, e);
        throw new RestException(Status.INTERNAL_SERVER_ERROR, e.getMessage());
    }
    // validate parameters
    try {
        validateGetFunctionInstanceRequestParams(tenant, namespace, componentName, componentType, instanceId);
    } catch (IllegalArgumentException e) {
        log.error("Invalid start/stop {} request @ /{}/{}/{}", ComponentTypeUtils.toString(componentType), tenant, namespace, componentName, e);
        throw new RestException(Status.BAD_REQUEST, e.getMessage());
    }
    FunctionMetaDataManager functionMetaDataManager = worker().getFunctionMetaDataManager();
    if (!functionMetaDataManager.containsFunction(tenant, namespace, componentName)) {
        log.error("{} does not exist @ /{}/{}/{}", ComponentTypeUtils.toString(componentType), tenant, namespace, componentName);
        throw new RestException(Status.NOT_FOUND, String.format("%s %s doesn't exist", ComponentTypeUtils.toString(componentType), componentName));
    }
    FunctionMetaData functionMetaData = functionMetaDataManager.getFunctionMetaData(tenant, namespace, componentName);
    if (!InstanceUtils.calculateSubjectType(functionMetaData.getFunctionDetails()).equals(componentType)) {
        log.error("{}/{}/{} is not a {}", tenant, namespace, componentName, ComponentTypeUtils.toString(componentType));
        throw new RestException(Status.NOT_FOUND, String.format("%s %s doesn't exist", ComponentTypeUtils.toString(componentType), componentName));
    }
    if (!FunctionMetaDataUtils.canChangeState(functionMetaData, Integer.parseInt(instanceId), start ? Function.FunctionState.RUNNING : Function.FunctionState.STOPPED)) {
        log.error("Operation not permitted on {}/{}/{}", tenant, namespace, componentName);
        throw new RestException(Status.BAD_REQUEST, "Operation not permitted");
    }
    FunctionMetaData newFunctionMetaData = FunctionMetaDataUtils.changeFunctionInstanceStatus(functionMetaData, Integer.parseInt(instanceId), start);
    internalProcessFunctionRequest(tenant, namespace, componentName, newFunctionMetaData, false, String.format("Failed to start/stop %s: %s/%s/%s/%s", ComponentTypeUtils.toString(componentType), tenant, namespace, componentName, instanceId));
}
Also used : FunctionMetaData(org.apache.pulsar.functions.proto.Function.FunctionMetaData) FunctionMetaDataManager(org.apache.pulsar.functions.worker.FunctionMetaDataManager) RestException(org.apache.pulsar.common.util.RestException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException)

Example 99 with RestException

use of org.apache.pulsar.common.util.RestException in project incubator-pulsar by apache.

the class ComponentImpl method triggerFunction.

@Override
public String triggerFunction(final String tenant, final String namespace, final String functionName, final String input, final InputStream uploadedInputStream, final String topic, final String clientRole, final AuthenticationDataSource clientAuthenticationDataHttps) {
    if (!isWorkerServiceAvailable()) {
        throwUnavailableException();
    }
    try {
        if (!isAuthorizedRole(tenant, namespace, clientRole, clientAuthenticationDataHttps)) {
            log.warn("{}/{}/{} Client [{}] is not authorized to trigger {}", tenant, namespace, functionName, clientRole, ComponentTypeUtils.toString(componentType));
            throw new RestException(Status.UNAUTHORIZED, "Client is not authorized to perform operation");
        }
    } catch (PulsarAdminException e) {
        log.error("{}/{}/{} Failed to authorize [{}]", tenant, namespace, functionName, e);
        throw new RestException(Status.INTERNAL_SERVER_ERROR, e.getMessage());
    }
    // validate parameters
    try {
        validateTriggerRequestParams(tenant, namespace, functionName, topic, input, uploadedInputStream);
    } catch (IllegalArgumentException e) {
        log.error("Invalid trigger function request @ /{}/{}/{}", tenant, namespace, functionName, e);
        throw new RestException(Status.BAD_REQUEST, e.getMessage());
    }
    FunctionMetaDataManager functionMetaDataManager = worker().getFunctionMetaDataManager();
    if (!functionMetaDataManager.containsFunction(tenant, namespace, functionName)) {
        log.warn("Function in trigger function does not exist @ /{}/{}/{}", tenant, namespace, functionName);
        throw new RestException(Status.NOT_FOUND, String.format("Function %s doesn't exist", functionName));
    }
    FunctionMetaData functionMetaData = functionMetaDataManager.getFunctionMetaData(tenant, namespace, functionName);
    String inputTopicToWrite;
    if (topic != null) {
        inputTopicToWrite = topic;
    } else if (functionMetaData.getFunctionDetails().getSource().getInputSpecsCount() == 1) {
        inputTopicToWrite = functionMetaData.getFunctionDetails().getSource().getInputSpecsMap().keySet().iterator().next();
    } else {
        log.error("Function in trigger function has more than 1 input topics @ /{}/{}/{}", tenant, namespace, functionName);
        throw new RestException(Status.BAD_REQUEST, "Function in trigger function has more than 1 input topics");
    }
    if (functionMetaData.getFunctionDetails().getSource().getInputSpecsCount() == 0 || !functionMetaData.getFunctionDetails().getSource().getInputSpecsMap().containsKey(inputTopicToWrite)) {
        log.error("Function in trigger function has unidentified topic @ /{}/{}/{} {}", tenant, namespace, functionName, inputTopicToWrite);
        throw new RestException(Status.BAD_REQUEST, "Function in trigger function has unidentified topic");
    }
    try {
        worker().getBrokerAdmin().topics().getSubscriptions(inputTopicToWrite);
    } catch (PulsarAdminException e) {
        log.error("Function in trigger function is not ready @ /{}/{}/{}", tenant, namespace, functionName);
        throw new RestException(Status.BAD_REQUEST, "Function in trigger function is not ready");
    }
    String outputTopic = functionMetaData.getFunctionDetails().getSink().getTopic();
    Reader<byte[]> reader = null;
    Producer<byte[]> producer = null;
    try {
        if (outputTopic != null && !outputTopic.isEmpty()) {
            reader = worker().getClient().newReader().topic(outputTopic).startMessageId(MessageId.latest).readerName(worker().getWorkerConfig().getWorkerId() + "-trigger-" + FunctionCommon.getFullyQualifiedName(tenant, namespace, functionName)).create();
        }
        producer = worker().getClient().newProducer(Schema.AUTO_PRODUCE_BYTES()).topic(inputTopicToWrite).producerName(worker().getWorkerConfig().getWorkerId() + "-trigger-" + FunctionCommon.getFullyQualifiedName(tenant, namespace, functionName)).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 null;
        }
        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(TopicName.get(inputTopicToWrite).toString())) {
                    return new String(msg.getData());
                }
            }
            curTime = System.currentTimeMillis();
        }
        throw new RestException(Status.REQUEST_TIMEOUT, "Request Timed Out");
    } catch (SchemaSerializationException e) {
        throw new RestException(Status.BAD_REQUEST, String.format("Failed to serialize input with error: %s. Please check" + "if input data conforms with the schema of the input topic.", e.getMessage()));
    } catch (IOException e) {
        throw new RestException(Status.INTERNAL_SERVER_ERROR, e.getMessage());
    } finally {
        if (reader != null) {
            reader.closeAsync();
        }
        if (producer != null) {
            producer.closeAsync();
        }
    }
}
Also used : FunctionMetaData(org.apache.pulsar.functions.proto.Function.FunctionMetaData) Message(org.apache.pulsar.client.api.Message) RestException(org.apache.pulsar.common.util.RestException) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) IOException(java.io.IOException) FunctionMetaDataManager(org.apache.pulsar.functions.worker.FunctionMetaDataManager) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) MessageId(org.apache.pulsar.client.api.MessageId)

Example 100 with RestException

use of org.apache.pulsar.common.util.RestException in project incubator-pulsar by apache.

the class ComponentImpl method getFunctionsInstanceStats.

@Override
public FunctionInstanceStatsDataImpl getFunctionsInstanceStats(final String tenant, final String namespace, final String componentName, final String instanceId, final URI uri, final String clientRole, final AuthenticationDataSource clientAuthenticationDataHttps) {
    if (!isWorkerServiceAvailable()) {
        throwUnavailableException();
    }
    try {
        if (!isAuthorizedRole(tenant, namespace, clientRole, clientAuthenticationDataHttps)) {
            log.warn("{}/{}/{} Client [{}] is not authorized to get stats for {}", tenant, namespace, componentName, clientRole, ComponentTypeUtils.toString(componentType));
            throw new RestException(Status.UNAUTHORIZED, "Client is not authorized to perform operation");
        }
    } catch (PulsarAdminException e) {
        log.error("{}/{}/{} Failed to authorize [{}]", tenant, namespace, componentName, e);
        throw new RestException(Status.INTERNAL_SERVER_ERROR, e.getMessage());
    }
    // validate parameters
    try {
        validateGetFunctionInstanceRequestParams(tenant, namespace, componentName, componentType, instanceId);
    } catch (IllegalArgumentException e) {
        log.error("Invalid get {} Stats request @ /{}/{}/{}", ComponentTypeUtils.toString(componentType), tenant, namespace, componentName, e);
        throw new RestException(Status.BAD_REQUEST, e.getMessage());
    }
    FunctionMetaDataManager functionMetaDataManager = worker().getFunctionMetaDataManager();
    if (!functionMetaDataManager.containsFunction(tenant, namespace, componentName)) {
        log.warn("{} in get {} Stats does not exist @ /{}/{}/{}", ComponentTypeUtils.toString(componentType), componentType, tenant, namespace, componentName);
        throw new RestException(Status.NOT_FOUND, String.format("%s %s doesn't exist", ComponentTypeUtils.toString(componentType), componentName));
    }
    FunctionMetaData functionMetaData = functionMetaDataManager.getFunctionMetaData(tenant, namespace, componentName);
    if (!InstanceUtils.calculateSubjectType(functionMetaData.getFunctionDetails()).equals(componentType)) {
        log.error("{}/{}/{} is not a {}", tenant, namespace, componentName, ComponentTypeUtils.toString(componentType));
        throw new RestException(Status.NOT_FOUND, String.format("%s %s doesn't exist", ComponentTypeUtils.toString(componentType), componentName));
    }
    int instanceIdInt = Integer.parseInt(instanceId);
    if (instanceIdInt < 0 || instanceIdInt >= functionMetaData.getFunctionDetails().getParallelism()) {
        log.error("instanceId in get {} Stats out of bounds @ /{}/{}/{}", ComponentTypeUtils.toString(componentType), tenant, namespace, componentName);
        throw new RestException(Status.BAD_REQUEST, String.format("%s %s doesn't have instance with id %s", ComponentTypeUtils.toString(componentType), componentName, instanceId));
    }
    FunctionRuntimeManager functionRuntimeManager = worker().getFunctionRuntimeManager();
    FunctionInstanceStatsDataImpl functionInstanceStatsData;
    try {
        functionInstanceStatsData = functionRuntimeManager.getFunctionInstanceStats(tenant, namespace, componentName, Integer.parseInt(instanceId), uri);
    } catch (WebApplicationException we) {
        throw we;
    } catch (Exception e) {
        log.error("{}/{}/{} Got Exception Getting Stats", tenant, namespace, componentName, e);
        throw new RestException(Status.INTERNAL_SERVER_ERROR, e.getMessage());
    }
    return functionInstanceStatsData;
}
Also used : FunctionMetaData(org.apache.pulsar.functions.proto.Function.FunctionMetaData) WebApplicationException(javax.ws.rs.WebApplicationException) FunctionMetaDataManager(org.apache.pulsar.functions.worker.FunctionMetaDataManager) RestException(org.apache.pulsar.common.util.RestException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) FunctionInstanceStatsDataImpl(org.apache.pulsar.common.policies.data.FunctionInstanceStatsDataImpl) RestUtils.throwUnavailableException(org.apache.pulsar.functions.worker.rest.RestUtils.throwUnavailableException) SchemaSerializationException(org.apache.pulsar.client.api.SchemaSerializationException) WebApplicationException(javax.ws.rs.WebApplicationException) NamespaceNotFoundException(org.apache.bookkeeper.clients.exceptions.NamespaceNotFoundException) RestException(org.apache.pulsar.common.util.RestException) StreamNotFoundException(org.apache.bookkeeper.clients.exceptions.StreamNotFoundException) PulsarAdminException(org.apache.pulsar.client.admin.PulsarAdminException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) FunctionRuntimeManager(org.apache.pulsar.functions.worker.FunctionRuntimeManager)

Aggregations

RestException (org.apache.pulsar.common.util.RestException)178 IOException (java.io.IOException)101 PulsarAdminException (org.apache.pulsar.client.admin.PulsarAdminException)101 WebApplicationException (javax.ws.rs.WebApplicationException)66 FunctionMetaDataManager (org.apache.pulsar.functions.worker.FunctionMetaDataManager)65 RestUtils.throwUnavailableException (org.apache.pulsar.functions.worker.rest.RestUtils.throwUnavailableException)54 Test (org.testng.annotations.Test)45 File (java.io.File)41 FunctionMetaData (org.apache.pulsar.functions.proto.Function.FunctionMetaData)36 Function (org.apache.pulsar.functions.proto.Function)33 Namespace (org.apache.distributedlog.api.namespace.Namespace)21 SchemaSerializationException (org.apache.pulsar.client.api.SchemaSerializationException)21 ExecutionException (java.util.concurrent.ExecutionException)18 NamespaceNotFoundException (org.apache.bookkeeper.clients.exceptions.NamespaceNotFoundException)18 StreamNotFoundException (org.apache.bookkeeper.clients.exceptions.StreamNotFoundException)18 FunctionAuthData (org.apache.pulsar.functions.auth.FunctionAuthData)18 FunctionAuthUtils.getFunctionAuthData (org.apache.pulsar.functions.auth.FunctionAuthUtils.getFunctionAuthData)18 FunctionRuntimeManager (org.apache.pulsar.functions.worker.FunctionRuntimeManager)15 URI (java.net.URI)14 FunctionConfig (org.apache.pulsar.common.functions.FunctionConfig)12