Search in sources :

Example 1 with DuplicateDefinitionException

use of org.ballerinalang.siddhi.query.api.exception.DuplicateDefinitionException in project ballerina by ballerina-lang.

the class DefinitionParserHelper method validateDefinition.

public static void validateDefinition(AbstractDefinition definition, ConcurrentMap<String, AbstractDefinition> streamDefinitionMap, ConcurrentMap<String, AbstractDefinition> tableDefinitionMap, ConcurrentMap<String, AbstractDefinition> windowDefinitionMap, ConcurrentMap<String, AbstractDefinition> aggregationDefinitionMap) {
    AbstractDefinition existingTableDefinition = tableDefinitionMap.get(definition.getId());
    if (existingTableDefinition != null && (!existingTableDefinition.equals(definition) || definition instanceof StreamDefinition)) {
        throw new DuplicateDefinitionException("Table Definition with same Stream Id '" + definition.getId() + "' already exist : " + existingTableDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
    AbstractDefinition existingStreamDefinition = streamDefinitionMap.get(definition.getId());
    if (existingStreamDefinition != null && (!existingStreamDefinition.equals(definition) || definition instanceof TableDefinition)) {
        throw new DuplicateDefinitionException("Stream Definition with same Stream Id '" + definition.getId() + "' already exist : " + existingStreamDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
    AbstractDefinition existingWindowDefinition = windowDefinitionMap.get(definition.getId());
    if (existingWindowDefinition != null && (!existingWindowDefinition.equals(definition) || definition instanceof WindowDefinition)) {
        throw new DuplicateDefinitionException("Window Definition with same Window Id '" + definition.getId() + "' already exist : " + existingWindowDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
    AbstractDefinition existingAggregationDefinition = aggregationDefinitionMap.get(definition.getId());
    if (existingAggregationDefinition != null && (!existingAggregationDefinition.equals(definition) || definition instanceof AggregationDefinition)) {
        throw new DuplicateDefinitionException("Aggregation Definition with same Aggregation Id '" + definition.getId() + "' already exist : " + existingWindowDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
}
Also used : StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) DuplicateDefinitionException(org.ballerinalang.siddhi.query.api.exception.DuplicateDefinitionException) AggregationDefinition(org.ballerinalang.siddhi.query.api.definition.AggregationDefinition) AbstractDefinition(org.ballerinalang.siddhi.query.api.definition.AbstractDefinition) TableDefinition(org.ballerinalang.siddhi.query.api.definition.TableDefinition) WindowDefinition(org.ballerinalang.siddhi.query.api.definition.WindowDefinition)

Example 2 with DuplicateDefinitionException

use of org.ballerinalang.siddhi.query.api.exception.DuplicateDefinitionException in project ballerina by ballerina-lang.

the class SiddhiApp method checkDuplicateDefinition.

private void checkDuplicateDefinition(AbstractDefinition definition) {
    TableDefinition existingTableDefinition = tableDefinitionMap.get(definition.getId());
    if (existingTableDefinition != null && (!existingTableDefinition.equals(definition) || definition instanceof StreamDefinition)) {
        throw new DuplicateDefinitionException("Table Definition with same Stream Id '" + definition.getId() + "' already exist : " + existingTableDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
    StreamDefinition existingStreamDefinition = streamDefinitionMap.get(definition.getId());
    if (existingStreamDefinition != null && (!existingStreamDefinition.equals(definition) || definition instanceof TableDefinition)) {
        throw new DuplicateDefinitionException("Stream Definition with same Stream Id '" + definition.getId() + "' already exist : " + existingStreamDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
    WindowDefinition existingWindowDefinition = windowDefinitionMap.get(definition.getId());
    if (existingWindowDefinition != null && (!existingWindowDefinition.equals(definition) || definition instanceof WindowDefinition)) {
        throw new DuplicateDefinitionException("Stream Definition with same Window Id '" + definition.getId() + "' already exist : " + existingWindowDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
    AggregationDefinition existingAggregationDefinition = aggregationDefinitionMap.get(definition.getId());
    if (existingAggregationDefinition != null && (!existingAggregationDefinition.equals(definition) || definition instanceof AggregationDefinition)) {
        throw new DuplicateDefinitionException("Aggregate Definition with same Aggregate Id '" + definition.getId() + "' already exist : " + existingAggregationDefinition + ", hence cannot add " + definition, definition.getQueryContextStartIndex(), definition.getQueryContextEndIndex());
    }
}
Also used : StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) DuplicateDefinitionException(org.ballerinalang.siddhi.query.api.exception.DuplicateDefinitionException) AggregationDefinition(org.ballerinalang.siddhi.query.api.definition.AggregationDefinition) TableDefinition(org.ballerinalang.siddhi.query.api.definition.TableDefinition) WindowDefinition(org.ballerinalang.siddhi.query.api.definition.WindowDefinition)

Example 3 with DuplicateDefinitionException

use of org.ballerinalang.siddhi.query.api.exception.DuplicateDefinitionException in project ballerina by ballerina-lang.

the class SiddhiApp method defineTrigger.

public SiddhiApp defineTrigger(TriggerDefinition triggerDefinition) {
    if (triggerDefinition == null) {
        throw new SiddhiAppValidationException("Trigger Definition should not be null");
    } else if (triggerDefinition.getId() == null) {
        throw new SiddhiAppValidationException("Trigger Id should not be null for Trigger Definition", triggerDefinition.getQueryContextStartIndex(), triggerDefinition.getQueryContextEndIndex());
    }
    StreamDefinition streamDefinition = StreamDefinition.id(triggerDefinition.getId()).attribute(SiddhiConstants.TRIGGERED_TIME, Attribute.Type.LONG);
    streamDefinition.setQueryContextStartIndex(triggerDefinition.getQueryContextStartIndex());
    streamDefinition.setQueryContextEndIndex(triggerDefinition.getQueryContextEndIndex());
    try {
        checkDuplicateDefinition(streamDefinition);
    } catch (DuplicateDefinitionException e) {
        throw new DuplicateDefinitionException("Trigger '" + triggerDefinition.getId() + "' cannot be defined as," + " " + e.getMessageWithOutContext(), e, triggerDefinition.getQueryContextStartIndex(), triggerDefinition.getQueryContextEndIndex());
    }
    if (triggerDefinitionMap.containsKey(triggerDefinition.getId())) {
        throw new DuplicateDefinitionException("Trigger Definition with same Id '" + triggerDefinition.getId() + "' already exist '" + triggerDefinitionMap.get(triggerDefinition.getId()) + "', hence cannot add '" + triggerDefinition + "'", triggerDefinition.getQueryContextStartIndex(), triggerDefinition.getQueryContextEndIndex());
    }
    this.triggerDefinitionMap.put(triggerDefinition.getId(), triggerDefinition);
    this.streamDefinitionMap.put(streamDefinition.getId(), streamDefinition);
    return this;
}
Also used : StreamDefinition(org.ballerinalang.siddhi.query.api.definition.StreamDefinition) DuplicateDefinitionException(org.ballerinalang.siddhi.query.api.exception.DuplicateDefinitionException) SiddhiAppValidationException(org.ballerinalang.siddhi.query.api.exception.SiddhiAppValidationException)

Example 4 with DuplicateDefinitionException

use of org.ballerinalang.siddhi.query.api.exception.DuplicateDefinitionException in project ballerina by ballerina-lang.

the class QueryParser method parse.

/**
 * Parse a query and return corresponding QueryRuntime.
 *
 * @param query                    query to be parsed.
 * @param siddhiAppContext         associated Siddhi app context.
 * @param streamDefinitionMap      keyvalue containing user given stream definitions.
 * @param tableDefinitionMap       keyvalue containing table definitions.
 * @param windowDefinitionMap      keyvalue containing window definition map.
 * @param aggregationDefinitionMap keyvalue containing aggregation definition map.
 * @param tableMap                 keyvalue containing event tables.
 * @param aggregationMap           keyvalue containing aggrigation runtimes.
 * @param windowMap                keyvalue containing event window map.
 * @param lockSynchronizer         Lock synchronizer for sync the lock across queries.
 * @param queryIndex               query index to identify unknown query by number
 * @return queryRuntime
 */
public static QueryRuntime parse(Query query, SiddhiAppContext siddhiAppContext, Map<String, AbstractDefinition> streamDefinitionMap, Map<String, AbstractDefinition> tableDefinitionMap, Map<String, AbstractDefinition> windowDefinitionMap, Map<String, AbstractDefinition> aggregationDefinitionMap, Map<String, Table> tableMap, Map<String, AggregationRuntime> aggregationMap, Map<String, Window> windowMap, LockSynchronizer lockSynchronizer, String queryIndex) {
    List<VariableExpressionExecutor> executors = new ArrayList<VariableExpressionExecutor>();
    QueryRuntime queryRuntime;
    Element nameElement = null;
    LatencyTracker latencyTracker = null;
    LockWrapper lockWrapper = null;
    try {
        nameElement = AnnotationHelper.getAnnotationElement("info", "name", query.getAnnotations());
        String queryName = null;
        if (nameElement != null) {
            queryName = nameElement.getValue();
        } else {
            queryName = "query_" + queryIndex + "_" + UUID.randomUUID().toString();
        }
        latencyTracker = QueryParserHelper.createLatencyTracker(siddhiAppContext, queryName, SiddhiConstants.METRIC_INFIX_QUERIES, null);
        OutputStream.OutputEventType outputEventType = query.getOutputStream().getOutputEventType();
        boolean outputExpectsExpiredEvents = false;
        if (outputEventType != OutputStream.OutputEventType.CURRENT_EVENTS) {
            outputExpectsExpiredEvents = true;
        }
        StreamRuntime streamRuntime = InputStreamParser.parse(query.getInputStream(), siddhiAppContext, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap, tableMap, windowMap, aggregationMap, executors, latencyTracker, outputExpectsExpiredEvents, queryName);
        QuerySelector selector = SelectorParser.parse(query.getSelector(), query.getOutputStream(), siddhiAppContext, streamRuntime.getMetaComplexEvent(), tableMap, executors, queryName, SiddhiConstants.UNKNOWN_STATE);
        boolean isWindow = query.getInputStream() instanceof JoinInputStream;
        if (!isWindow && query.getInputStream() instanceof SingleInputStream) {
            for (StreamHandler streamHandler : ((SingleInputStream) query.getInputStream()).getStreamHandlers()) {
                if (streamHandler instanceof org.ballerinalang.siddhi.query.api.execution.query.input.handler.Window) {
                    isWindow = true;
                    break;
                }
            }
        }
        Element synchronizedElement = AnnotationHelper.getAnnotationElement("synchronized", null, query.getAnnotations());
        if (synchronizedElement != null) {
            if (!("false".equalsIgnoreCase(synchronizedElement.getValue()))) {
                // Query LockWrapper does not need a unique
                lockWrapper = new LockWrapper("");
                // id since it will
                // not be passed to the LockSynchronizer.
                // LockWrapper does not have a default lock
                lockWrapper.setLock(new ReentrantLock());
            }
        } else {
            if (isWindow || !(streamRuntime instanceof SingleStreamRuntime)) {
                if (streamRuntime instanceof JoinStreamRuntime) {
                    // If at least one Window is involved in the join, use the LockWrapper of that window
                    // for the query as well.
                    // If join is between two EventWindows, sync the locks of the LockWrapper of those windows
                    // and use either of them for query.
                    MetaStateEvent metaStateEvent = (MetaStateEvent) streamRuntime.getMetaComplexEvent();
                    MetaStreamEvent[] metaStreamEvents = metaStateEvent.getMetaStreamEvents();
                    if (metaStreamEvents[0].getEventType() == EventType.WINDOW && metaStreamEvents[1].getEventType() == EventType.WINDOW) {
                        LockWrapper leftLockWrapper = windowMap.get(metaStreamEvents[0].getLastInputDefinition().getId()).getLock();
                        LockWrapper rightLockWrapper = windowMap.get(metaStreamEvents[1].getLastInputDefinition().getId()).getLock();
                        if (!leftLockWrapper.equals(rightLockWrapper)) {
                            // Sync the lock across both wrappers
                            lockSynchronizer.sync(leftLockWrapper, rightLockWrapper);
                        }
                        // Can use either leftLockWrapper or rightLockWrapper since both of them will hold the
                        // same lock internally
                        // If either of their lock is updated later, the other lock also will be update by the
                        // LockSynchronizer.
                        lockWrapper = leftLockWrapper;
                    } else if (metaStreamEvents[0].getEventType() == EventType.WINDOW) {
                        // Share the same wrapper as the query lock wrapper
                        lockWrapper = windowMap.get(metaStreamEvents[0].getLastInputDefinition().getId()).getLock();
                    } else if (metaStreamEvents[1].getEventType() == EventType.WINDOW) {
                        // Share the same wrapper as the query lock wrapper
                        lockWrapper = windowMap.get(metaStreamEvents[1].getLastInputDefinition().getId()).getLock();
                    } else {
                        // Join does not contain any Window
                        // Query LockWrapper does not need a unique
                        lockWrapper = new LockWrapper("");
                        // id since
                        // it will not be passed to the LockSynchronizer.
                        // LockWrapper does not have a default lock
                        lockWrapper.setLock(new ReentrantLock());
                    }
                } else {
                    lockWrapper = new LockWrapper("");
                    lockWrapper.setLock(new ReentrantLock());
                }
            }
        }
        OutputRateLimiter outputRateLimiter = OutputParser.constructOutputRateLimiter(query.getOutputStream().getId(), query.getOutputRate(), query.getSelector().getGroupByList().size() != 0, isWindow, siddhiAppContext.getScheduledExecutorService(), siddhiAppContext, queryName);
        if (outputRateLimiter instanceof WrappedSnapshotOutputRateLimiter) {
            selector.setBatchingEnabled(false);
        }
        siddhiAppContext.addEternalReferencedHolder(outputRateLimiter);
        OutputCallback outputCallback = OutputParser.constructOutputCallback(query.getOutputStream(), streamRuntime.getMetaComplexEvent().getOutputStreamDefinition(), tableMap, windowMap, siddhiAppContext, !(streamRuntime instanceof SingleStreamRuntime), queryName);
        QueryParserHelper.reduceMetaComplexEvent(streamRuntime.getMetaComplexEvent());
        QueryParserHelper.updateVariablePosition(streamRuntime.getMetaComplexEvent(), executors);
        QueryParserHelper.initStreamRuntime(streamRuntime, streamRuntime.getMetaComplexEvent(), lockWrapper, queryName);
        selector.setEventPopulator(StateEventPopulatorFactory.constructEventPopulator(streamRuntime.getMetaComplexEvent()));
        queryRuntime = new QueryRuntime(query, siddhiAppContext, streamRuntime, selector, outputRateLimiter, outputCallback, streamRuntime.getMetaComplexEvent(), lockWrapper != null, queryName);
        if (outputRateLimiter instanceof WrappedSnapshotOutputRateLimiter) {
            selector.setBatchingEnabled(false);
            ((WrappedSnapshotOutputRateLimiter) outputRateLimiter).init(streamRuntime.getMetaComplexEvent().getOutputStreamDefinition().getAttributeList().size(), selector.getAttributeProcessorList(), streamRuntime.getMetaComplexEvent());
        }
        outputRateLimiter.init(siddhiAppContext, lockWrapper, queryName);
    } catch (DuplicateDefinitionException e) {
        if (nameElement != null) {
            throw new DuplicateDefinitionException(e.getMessageWithOutContext() + ", when creating query " + nameElement.getValue(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiAppContext.getName(), siddhiAppContext.getSiddhiAppString());
        } else {
            throw new DuplicateDefinitionException(e.getMessage(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(), siddhiAppContext.getName(), siddhiAppContext.getSiddhiAppString());
        }
    } catch (Throwable t) {
        ExceptionUtil.populateQueryContext(t, query, siddhiAppContext);
        throw t;
    }
    return queryRuntime;
}
Also used : Element(org.ballerinalang.siddhi.query.api.annotation.Element) OutputStream(org.ballerinalang.siddhi.query.api.execution.query.output.stream.OutputStream) ArrayList(java.util.ArrayList) OutputCallback(org.ballerinalang.siddhi.core.query.output.callback.OutputCallback) QueryRuntime(org.ballerinalang.siddhi.core.query.QueryRuntime) JoinInputStream(org.ballerinalang.siddhi.query.api.execution.query.input.stream.JoinInputStream) SingleInputStream(org.ballerinalang.siddhi.query.api.execution.query.input.stream.SingleInputStream) OutputRateLimiter(org.ballerinalang.siddhi.core.query.output.ratelimit.OutputRateLimiter) WrappedSnapshotOutputRateLimiter(org.ballerinalang.siddhi.core.query.output.ratelimit.snapshot.WrappedSnapshotOutputRateLimiter) StreamHandler(org.ballerinalang.siddhi.query.api.execution.query.input.handler.StreamHandler) StreamRuntime(org.ballerinalang.siddhi.core.query.input.stream.StreamRuntime) JoinStreamRuntime(org.ballerinalang.siddhi.core.query.input.stream.join.JoinStreamRuntime) SingleStreamRuntime(org.ballerinalang.siddhi.core.query.input.stream.single.SingleStreamRuntime) Window(org.ballerinalang.siddhi.core.window.Window) ReentrantLock(java.util.concurrent.locks.ReentrantLock) DuplicateDefinitionException(org.ballerinalang.siddhi.query.api.exception.DuplicateDefinitionException) SingleStreamRuntime(org.ballerinalang.siddhi.core.query.input.stream.single.SingleStreamRuntime) JoinStreamRuntime(org.ballerinalang.siddhi.core.query.input.stream.join.JoinStreamRuntime) VariableExpressionExecutor(org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor) QuerySelector(org.ballerinalang.siddhi.core.query.selector.QuerySelector) LockWrapper(org.ballerinalang.siddhi.core.util.lock.LockWrapper) MetaStateEvent(org.ballerinalang.siddhi.core.event.state.MetaStateEvent) LatencyTracker(org.ballerinalang.siddhi.core.util.statistics.LatencyTracker) WrappedSnapshotOutputRateLimiter(org.ballerinalang.siddhi.core.query.output.ratelimit.snapshot.WrappedSnapshotOutputRateLimiter) MetaStreamEvent(org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)

Aggregations

DuplicateDefinitionException (org.ballerinalang.siddhi.query.api.exception.DuplicateDefinitionException)4 StreamDefinition (org.ballerinalang.siddhi.query.api.definition.StreamDefinition)3 AggregationDefinition (org.ballerinalang.siddhi.query.api.definition.AggregationDefinition)2 TableDefinition (org.ballerinalang.siddhi.query.api.definition.TableDefinition)2 WindowDefinition (org.ballerinalang.siddhi.query.api.definition.WindowDefinition)2 ArrayList (java.util.ArrayList)1 ReentrantLock (java.util.concurrent.locks.ReentrantLock)1 MetaStateEvent (org.ballerinalang.siddhi.core.event.state.MetaStateEvent)1 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)1 VariableExpressionExecutor (org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor)1 QueryRuntime (org.ballerinalang.siddhi.core.query.QueryRuntime)1 StreamRuntime (org.ballerinalang.siddhi.core.query.input.stream.StreamRuntime)1 JoinStreamRuntime (org.ballerinalang.siddhi.core.query.input.stream.join.JoinStreamRuntime)1 SingleStreamRuntime (org.ballerinalang.siddhi.core.query.input.stream.single.SingleStreamRuntime)1 OutputCallback (org.ballerinalang.siddhi.core.query.output.callback.OutputCallback)1 OutputRateLimiter (org.ballerinalang.siddhi.core.query.output.ratelimit.OutputRateLimiter)1 WrappedSnapshotOutputRateLimiter (org.ballerinalang.siddhi.core.query.output.ratelimit.snapshot.WrappedSnapshotOutputRateLimiter)1 QuerySelector (org.ballerinalang.siddhi.core.query.selector.QuerySelector)1 LockWrapper (org.ballerinalang.siddhi.core.util.lock.LockWrapper)1 LatencyTracker (org.ballerinalang.siddhi.core.util.statistics.LatencyTracker)1