Search in sources :

Example 21 with SiddhiAppCreationException

use of io.siddhi.core.exception.SiddhiAppCreationException in project siddhi by wso2.

the class DefinitionParserHelper method addEventSink.

public static void addEventSink(StreamDefinition streamDefinition, StreamJunction streamJunction, ConcurrentMap<String, List<Sink>> eventSinkMap, SiddhiAppContext siddhiAppContext) {
    for (Annotation sinkAnnotation : streamDefinition.getAnnotations()) {
        if (SiddhiConstants.ANNOTATION_SINK.equalsIgnoreCase(sinkAnnotation.getName())) {
            try {
                sinkAnnotation = updateAnnotationRef(sinkAnnotation, SiddhiConstants.NAMESPACE_SINK, siddhiAppContext);
                Annotation mapAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_MAP, sinkAnnotation.getAnnotations());
                String sinkType = sinkAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
                if (sinkType == null) {
                    throw new SiddhiAppCreationException("Attribute 'type' does not exist for annotation '" + sinkAnnotation + "'", sinkAnnotation, siddhiAppContext);
                }
                if (mapAnnotation == null) {
                    mapAnnotation = Annotation.annotation(SiddhiConstants.ANNOTATION_MAP).element(SiddhiConstants.ANNOTATION_ELEMENT_TYPE, "passThrough");
                }
                Annotation distributionAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_DISTRIBUTION, sinkAnnotation.getAnnotations());
                if (mapAnnotation != null) {
                    String[] supportedDynamicOptions = null;
                    List<OptionHolder> destinationOptHolders = new ArrayList<>();
                    List<Map<String, String>> destinationDeploymentProperties = new ArrayList<>();
                    Extension sinkExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_SINK, sinkType, sinkAnnotation, SiddhiConstants.NAMESPACE_SINK);
                    ConfigReader sinkConfigReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(sinkExtension.getNamespace(), sinkExtension.getName());
                    final boolean isDistributedTransport = (distributionAnnotation != null);
                    boolean isMultiClient = false;
                    if (isDistributedTransport) {
                        Sink sink = createSink(sinkExtension, siddhiAppContext);
                        isMultiClient = isMultiClientDistributedTransport(sink, streamDefinition, distributionAnnotation, siddhiAppContext);
                        supportedDynamicOptions = sink.getSupportedDynamicOptions();
                        destinationOptHolders = createDestinationOptionHolders(distributionAnnotation, streamDefinition, sink, siddhiAppContext);
                        destinationDeploymentProperties = createDestinationDeploymentProperties(distributionAnnotation, sink);
                    }
                    final String mapType = mapAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
                    if (mapType != null) {
                        Sink sink;
                        if (isDistributedTransport) {
                            sink = (isMultiClient) ? new MultiClientDistributedSink() : new SingleClientDistributedSink();
                        } else {
                            sink = createSink(sinkExtension, siddhiAppContext);
                        }
                        if (supportedDynamicOptions == null) {
                            supportedDynamicOptions = sink.getSupportedDynamicOptions();
                        }
                        // load output mapper extension
                        Extension mapperExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_MAP, mapType, sinkAnnotation, SiddhiConstants.NAMESPACE_SINK_MAPPER);
                        ConfigReader mapperConfigReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(sinkExtension.getNamespace(), sinkExtension.getName());
                        SinkMapper sinkMapper = (SinkMapper) SiddhiClassLoader.loadExtensionImplementation(mapperExtension, SinkMapperExecutorExtensionHolder.getInstance(siddhiAppContext));
                        io.siddhi.annotation.Extension sinkExt = sink.getClass().getAnnotation(io.siddhi.annotation.Extension.class);
                        OptionHolder transportOptionHolder = constructOptionHolder(streamDefinition, sinkAnnotation, sinkExt, supportedDynamicOptions, true);
                        Map<String, String> deploymentProperties = createDeploymentProperties(sinkAnnotation, sinkExt);
                        OptionHolder mapOptionHolder = constructOptionHolder(streamDefinition, mapAnnotation, sinkMapper.getClass().getAnnotation(io.siddhi.annotation.Extension.class), sinkMapper.getSupportedDynamicOptions(), false);
                        List<Element> payloadElementList = getPayload(mapAnnotation);
                        OptionHolder distributionOptHolder = null;
                        SinkHandlerManager sinkHandlerManager = siddhiAppContext.getSiddhiContext().getSinkHandlerManager();
                        SinkHandler sinkHandler = null;
                        if (sinkHandlerManager != null) {
                            sinkHandler = sinkHandlerManager.generateSinkHandler();
                        }
                        if (isDistributedTransport) {
                            distributionOptHolder = constructOptionHolder(streamDefinition, distributionAnnotation, sinkExt, supportedDynamicOptions, true);
                            String strategyType = distributionOptHolder.validateAndGetStaticValue(SiddhiConstants.DISTRIBUTION_STRATEGY_KEY);
                            Extension strategyExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_SINK, strategyType, sinkAnnotation, SiddhiConstants.NAMESPACE_DISTRIBUTION_STRATEGY);
                            ConfigReader configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(strategyExtension.getNamespace(), strategyExtension.getName());
                            DistributionStrategy distributionStrategy = (DistributionStrategy) SiddhiClassLoader.loadExtensionImplementation(strategyExtension, DistributionStrategyExtensionHolder.getInstance(siddhiAppContext));
                            distributionStrategy.init(streamDefinition, transportOptionHolder, distributionOptHolder, destinationOptHolders, configReader);
                            ((DistributedTransport) sink).init(streamDefinition, sinkType, transportOptionHolder, sinkConfigReader, sinkMapper, mapType, mapOptionHolder, sinkHandler, payloadElementList, mapperConfigReader, streamJunction, siddhiAppContext, destinationOptHolders, sinkAnnotation, distributionStrategy, supportedDynamicOptions, deploymentProperties, destinationDeploymentProperties);
                        } else {
                            sink.init(streamDefinition, sinkType, transportOptionHolder, sinkConfigReader, sinkMapper, mapType, mapOptionHolder, sinkHandler, payloadElementList, mapperConfigReader, deploymentProperties, streamJunction, siddhiAppContext);
                        }
                        if (sinkHandlerManager != null) {
                            sinkHandlerManager.registerSinkHandler(sinkHandler.getId(), sinkHandler);
                        }
                        validateSinkMapperCompatibility(streamDefinition, sinkType, mapType, sink, sinkMapper, sinkAnnotation);
                        // Setting the output group determiner
                        OutputGroupDeterminer groupDeterminer = constructOutputGroupDeterminer(transportOptionHolder, distributionOptHolder, streamDefinition, destinationOptHolders.size());
                        if (groupDeterminer != null) {
                            sink.getMapper().setGroupDeterminer(groupDeterminer);
                        }
                        List<Sink> eventSinks = eventSinkMap.get(streamDefinition.getId());
                        if (eventSinks == null) {
                            eventSinks = new ArrayList<>();
                            eventSinks.add(sink);
                            eventSinkMap.put(streamDefinition.getId(), eventSinks);
                        } else {
                            eventSinks.add(sink);
                        }
                    } else {
                        throw new SiddhiAppCreationException("Attribute 'type' does not exist for annotation '" + mapAnnotation + "'", mapAnnotation, siddhiAppContext);
                    }
                } else {
                    throw new SiddhiAppCreationException("Both @sink(type=) and @map(type=) are required.", sinkAnnotation.getQueryContextStartIndex(), sinkAnnotation.getQueryContextEndIndex());
                }
            } catch (Throwable t) {
                ExceptionUtil.populateQueryContext(t, sinkAnnotation, siddhiAppContext);
                throw t;
            }
        }
    }
}
Also used : Element(io.siddhi.query.api.annotation.Element) ArrayList(java.util.ArrayList) OutputGroupDeterminer(io.siddhi.core.stream.output.sink.OutputGroupDeterminer) SingleClientDistributedSink(io.siddhi.core.util.transport.SingleClientDistributedSink) SingleClientDistributedSink(io.siddhi.core.util.transport.SingleClientDistributedSink) MultiClientDistributedSink(io.siddhi.core.util.transport.MultiClientDistributedSink) Sink(io.siddhi.core.stream.output.sink.Sink) OptionHolder(io.siddhi.core.util.transport.OptionHolder) SinkMapper(io.siddhi.core.stream.output.sink.SinkMapper) SinkHandlerManager(io.siddhi.core.stream.output.sink.SinkHandlerManager) SinkHandler(io.siddhi.core.stream.output.sink.SinkHandler) DistributedTransport(io.siddhi.core.stream.output.sink.distributed.DistributedTransport) DistributionStrategy(io.siddhi.core.stream.output.sink.distributed.DistributionStrategy) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) ConfigReader(io.siddhi.core.util.config.ConfigReader) Annotation(io.siddhi.query.api.annotation.Annotation) Extension(io.siddhi.query.api.extension.Extension) MultiClientDistributedSink(io.siddhi.core.util.transport.MultiClientDistributedSink) Map(java.util.Map) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 22 with SiddhiAppCreationException

use of io.siddhi.core.exception.SiddhiAppCreationException in project siddhi by wso2.

the class DefinitionParserHelper method constructOptionHolder.

private static OptionHolder constructOptionHolder(StreamDefinition streamDefinition, Annotation annotation, io.siddhi.annotation.Extension extension, String[] supportedDynamicOptions, boolean supportDeploymentOptions) {
    List<String> supportedDynamicOptionList = new ArrayList<>();
    if (supportedDynamicOptions != null) {
        supportedDynamicOptionList = Arrays.asList(supportedDynamicOptions);
    }
    Map<String, String> options = new HashMap<>();
    Map<String, String> dynamicOptions = new HashMap<>();
    for (Element element : annotation.getElements()) {
        if (element.getKey().startsWith("dep:")) {
            if (!supportDeploymentOptions) {
                throw new SiddhiAppCreationException("DeploymentOption is not supported by '" + extension.namespace() + ":" + extension.name() + "', but a deployment property '" + element.getKey() + "' is configured", annotation.getQueryContextStartIndex(), annotation.getQueryContextEndIndex());
            }
        }
        if (Pattern.matches("(.*?)\\{\\{.*?\\}\\}(.*?)", element.getValue())) {
            if (supportedDynamicOptionList.contains(element.getKey())) {
                dynamicOptions.put(element.getKey(), element.getValue());
            } else {
                throw new SiddhiAppCreationException("'" + element.getKey() + "' is not a supported " + "DynamicOption " + "for the Extension '" + extension.namespace() + ":" + extension.name() + "', it only supports following as its DynamicOptions: " + supportedDynamicOptionList, annotation.getQueryContextStartIndex(), annotation.getQueryContextEndIndex());
            }
        } else {
            options.put(element.getKey(), element.getValue());
        }
    }
    return new OptionHolder(streamDefinition, options, dynamicOptions, extension);
}
Also used : HashMap(java.util.HashMap) OptionHolder(io.siddhi.core.util.transport.OptionHolder) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) Element(io.siddhi.query.api.annotation.Element) ArrayList(java.util.ArrayList)

Example 23 with SiddhiAppCreationException

use of io.siddhi.core.exception.SiddhiAppCreationException in project siddhi by wso2.

the class TemplateBuilder method assignTemplateArrayAttributePositions.

private void assignTemplateArrayAttributePositions(String[] splitTemplateArray, StreamDefinition streamDefinition) {
    this.positionArray = new int[splitTemplateArray.length / 2];
    int positionCount = 0;
    for (int i = 0; i < splitTemplateArray.length; i++) {
        if (i % 2 != 0) {
            try {
                positionArray[positionCount++] = Integer.parseInt(splitTemplateArray[i]);
            } catch (NumberFormatException e) {
                throw new SiddhiAppCreationException(String.format("Invalid mapping configuration provided in " + "%s. Mapping parameter should be surrounded only with '{{' and '}}'.", streamDefinition));
            }
        }
    }
}
Also used : SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException)

Example 24 with SiddhiAppCreationException

use of io.siddhi.core.exception.SiddhiAppCreationException in project siddhi by wso2.

the class AttributeStreamFunction method init.

@Override
protected StateFactory init(AbstractDefinition inputDefinition, ExpressionExecutor[] attributeExpressionExecutors, ConfigReader configReader, boolean outputExpectsExpiredEvents, SiddhiQueryContext siddhiQueryContext) {
    if (attributeExpressionExecutors.length != 1) {
        throw new SiddhiAppCreationException("Only one attribute is expected but found " + attributeExpressionExecutors.length);
    }
    if (!(attributeExpressionExecutors[0] instanceof ConstantExpressionExecutor)) {
        throw new SiddhiAppCreationException("Attribute is expected to be constant, but its not!");
    }
    newAttributes = new ArrayList<>();
    newAttributes.add(new Attribute(((ConstantExpressionExecutor) attributeExpressionExecutors[0]).getValue().toString(), inputDefinition.getAttributeList().get(0).getType()));
    return null;
}
Also used : Attribute(io.siddhi.query.api.definition.Attribute) SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) ConstantExpressionExecutor(io.siddhi.core.executor.ConstantExpressionExecutor)

Example 25 with SiddhiAppCreationException

use of io.siddhi.core.exception.SiddhiAppCreationException in project siddhi by wso2.

the class SessionWindowTestCase method testSessionWindow10.

@Test(description = "This test checks whether the allowedLatency parameter value is greater" + " than the session gap value", expectedExceptions = SiddhiAppCreationException.class)
public void testSessionWindow10() {
    log.info("SessionWindow Test10: Testing session window " + "with providing greater allowedLatency value than session gap value");
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = null;
    String purchaseEventStream = "" + "define stream purchaseEventStream (user string, item_number int, price float, quantity int);";
    String query = "" + "@info(name = 'query0') " + "from purchaseEventStream#window.session(5 sec, 6 sec) " + "select * " + "insert all events into outputStream ;";
    try {
        siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(purchaseEventStream + query);
    } catch (SiddhiAppCreationException e) {
        AssertJUnit.assertEquals("There is no parameterOverload for 'session' that matches attribute types " + "'<LONG, LONG>'. Supported parameter overloads are (<INT|LONG|TIME> session.gap), " + "(<INT|LONG|TIME> session.gap, <STRING> session.key), (<INT|LONG|TIME> session.gap, " + "<STRING> session.key, <INT|LONG|TIME> allowed.latency).", e.getCause().getMessage());
        throw e;
    } finally {
        if (siddhiAppRuntime != null) {
            siddhiAppRuntime.shutdown();
        }
    }
}
Also used : SiddhiAppCreationException(io.siddhi.core.exception.SiddhiAppCreationException) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) SiddhiManager(io.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Aggregations

SiddhiAppCreationException (io.siddhi.core.exception.SiddhiAppCreationException)54 Attribute (io.siddhi.query.api.definition.Attribute)23 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)16 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)15 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)12 SiddhiManager (io.siddhi.core.SiddhiManager)12 ArrayList (java.util.ArrayList)12 Expression (io.siddhi.query.api.expression.Expression)11 MetaStateEvent (io.siddhi.core.event.state.MetaStateEvent)10 ConstantExpressionExecutor (io.siddhi.core.executor.ConstantExpressionExecutor)10 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)10 Annotation (io.siddhi.query.api.annotation.Annotation)10 Variable (io.siddhi.query.api.expression.Variable)10 ConfigReader (io.siddhi.core.util.config.ConfigReader)9 HashMap (java.util.HashMap)9 Test (org.testng.annotations.Test)9 Element (io.siddhi.query.api.annotation.Element)8 OutputAttribute (io.siddhi.query.api.execution.query.selection.OutputAttribute)8 Table (io.siddhi.core.table.Table)7 MatchingMetaInfoHolder (io.siddhi.core.util.collection.operator.MatchingMetaInfoHolder)7