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;
}
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));
}
}
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));
}
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();
}
}
}
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;
}
Aggregations