Search in sources :

Example 86 with SiddhiAppContext

use of org.wso2.siddhi.core.config.SiddhiAppContext in project siddhi by wso2.

the class DefinitionParserHelper method addTable.

public static void addTable(TableDefinition tableDefinition, ConcurrentMap<String, Table> tableMap, SiddhiAppContext siddhiAppContext) {
    if (!tableMap.containsKey(tableDefinition.getId())) {
        MetaStreamEvent tableMetaStreamEvent = new MetaStreamEvent();
        tableMetaStreamEvent.addInputDefinition(tableDefinition);
        for (Attribute attribute : tableDefinition.getAttributeList()) {
            tableMetaStreamEvent.addOutputData(attribute);
        }
        StreamEventPool tableStreamEventPool = new StreamEventPool(tableMetaStreamEvent, 10);
        StreamEventCloner tableStreamEventCloner = new StreamEventCloner(tableMetaStreamEvent, tableStreamEventPool);
        Annotation annotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_STORE, tableDefinition.getAnnotations());
        Table table;
        ConfigReader configReader = null;
        RecordTableHandlerManager recordTableHandlerManager = null;
        RecordTableHandler recordTableHandler = null;
        if (annotation != null) {
            annotation = updateAnnotationRef(annotation, SiddhiConstants.NAMESPACE_STORE, siddhiAppContext);
            String tableType = annotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
            Extension extension = new Extension() {

                @Override
                public String getNamespace() {
                    return SiddhiConstants.NAMESPACE_STORE;
                }

                @Override
                public String getName() {
                    return tableType;
                }
            };
            recordTableHandlerManager = siddhiAppContext.getSiddhiContext().getRecordTableHandlerManager();
            if (recordTableHandlerManager != null) {
                recordTableHandler = recordTableHandlerManager.generateRecordTableHandler();
            }
            table = (Table) SiddhiClassLoader.loadExtensionImplementation(extension, TableExtensionHolder.getInstance(siddhiAppContext));
            configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(extension.getNamespace(), extension.getName());
        } else {
            table = new InMemoryTable();
        }
        table.initTable(tableDefinition, tableStreamEventPool, tableStreamEventCloner, configReader, siddhiAppContext, recordTableHandler);
        if (recordTableHandler != null) {
            recordTableHandlerManager.registerRecordTableHandler(recordTableHandler.getElementId(), recordTableHandler);
        }
        tableMap.putIfAbsent(tableDefinition.getId(), table);
    }
}
Also used : Extension(org.wso2.siddhi.query.api.extension.Extension) InMemoryTable(org.wso2.siddhi.core.table.InMemoryTable) Table(org.wso2.siddhi.core.table.Table) InMemoryTable(org.wso2.siddhi.core.table.InMemoryTable) Attribute(org.wso2.siddhi.query.api.definition.Attribute) ConfigReader(org.wso2.siddhi.core.util.config.ConfigReader) StreamEventPool(org.wso2.siddhi.core.event.stream.StreamEventPool) StreamEventCloner(org.wso2.siddhi.core.event.stream.StreamEventCloner) RecordTableHandler(org.wso2.siddhi.core.table.record.RecordTableHandler) RecordTableHandlerManager(org.wso2.siddhi.core.table.record.RecordTableHandlerManager) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) Annotation(org.wso2.siddhi.query.api.annotation.Annotation)

Example 87 with SiddhiAppContext

use of org.wso2.siddhi.core.config.SiddhiAppContext in project siddhi by wso2.

the class DefinitionParserHelper method addWindow.

public static void addWindow(WindowDefinition windowDefinition, ConcurrentMap<String, Window> eventWindowMap, SiddhiAppContext siddhiAppContext) {
    if (!eventWindowMap.containsKey(windowDefinition.getId())) {
        Window table = new Window(windowDefinition, siddhiAppContext);
        eventWindowMap.putIfAbsent(windowDefinition.getId(), table);
    }
}
Also used : Window(org.wso2.siddhi.core.window.Window)

Example 88 with SiddhiAppContext

use of org.wso2.siddhi.core.config.SiddhiAppContext in project siddhi by wso2.

the class DefinitionParserHelper method addFunction.

public static void addFunction(SiddhiAppContext siddhiAppContext, final FunctionDefinition functionDefinition) {
    Extension extension = new Extension() {

        @Override
        public String getNamespace() {
            return "script";
        }

        @Override
        public String getName() {
            return functionDefinition.getLanguage().toLowerCase();
        }
    };
    try {
        Script script = (Script) SiddhiClassLoader.loadExtensionImplementation(extension, ScriptExtensionHolder.getInstance(siddhiAppContext));
        ConfigReader configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(extension.getNamespace(), extension.getName());
        script.setReturnType(functionDefinition.getReturnType());
        script.init(functionDefinition.getId(), functionDefinition.getBody(), configReader);
        siddhiAppContext.getScriptFunctionMap().put(functionDefinition.getId(), script);
    } catch (Throwable t) {
        ExceptionUtil.populateQueryContext(t, functionDefinition, siddhiAppContext);
        throw t;
    }
}
Also used : Extension(org.wso2.siddhi.query.api.extension.Extension) Script(org.wso2.siddhi.core.function.Script) ConfigReader(org.wso2.siddhi.core.util.config.ConfigReader)

Example 89 with SiddhiAppContext

use of org.wso2.siddhi.core.config.SiddhiAppContext in project siddhi by wso2.

the class QueryParserHelper method createLatencyTracker.

public static LatencyTracker createLatencyTracker(SiddhiAppContext siddhiAppContext, String name, String type, String function) {
    LatencyTracker latencyTracker = null;
    if (siddhiAppContext.getStatisticsManager() != null) {
        String metricName = siddhiAppContext.getSiddhiContext().getStatisticsConfiguration().getMetricPrefix() + SiddhiConstants.METRIC_DELIMITER + SiddhiConstants.METRIC_INFIX_SIDDHI_APPS + SiddhiConstants.METRIC_DELIMITER + siddhiAppContext.getName() + SiddhiConstants.METRIC_DELIMITER + SiddhiConstants.METRIC_INFIX_SIDDHI + SiddhiConstants.METRIC_DELIMITER + type + SiddhiConstants.METRIC_DELIMITER + name;
        if (function != null) {
            metricName += SiddhiConstants.METRIC_DELIMITER + function;
        }
        metricName += SiddhiConstants.METRIC_DELIMITER + "latency";
        boolean matchExist = false;
        for (String regex : siddhiAppContext.getIncludedMetrics()) {
            if (metricName.matches(regex)) {
                matchExist = true;
                break;
            }
        }
        if (matchExist) {
            latencyTracker = siddhiAppContext.getSiddhiContext().getStatisticsConfiguration().getFactory().createLatencyTracker(metricName, siddhiAppContext.getStatisticsManager());
        }
    }
    return latencyTracker;
}
Also used : LatencyTracker(org.wso2.siddhi.core.util.statistics.LatencyTracker)

Example 90 with SiddhiAppContext

use of org.wso2.siddhi.core.config.SiddhiAppContext in project siddhi by wso2.

the class SnapshotService method restore.

public void restore(byte[] snapshot) throws CannotRestoreSiddhiAppStateException {
    Map<String, Map<String, Object>> snapshots = (Map<String, Map<String, Object>>) ByteSerializer.byteToObject(snapshot, siddhiAppContext);
    List<Snapshotable> snapshotableList;
    try {
        threadBarrier.lock();
        List<Snapshotable> partitionSnapshotables = snapshotableMap.get("partition");
        try {
            if (partitionSnapshotables != null) {
                for (Snapshotable snapshotable : partitionSnapshotables) {
                    snapshotable.restoreState(snapshots.get(snapshotable.getElementId()));
                }
            }
        } catch (Throwable t) {
            throw new CannotRestoreSiddhiAppStateException("Restoring of Siddhi app " + siddhiAppContext.getName() + " not completed properly because content of Siddhi app has changed since " + "last state persistence. Clean persistence store for a fresh deployment.", t);
        }
        for (Map.Entry<String, List<Snapshotable>> entry : snapshotableMap.entrySet()) {
            if (entry.getKey().equals("partition")) {
                continue;
            }
            snapshotableList = entry.getValue();
            try {
                for (Snapshotable snapshotable : snapshotableList) {
                    snapshotable.restoreState(snapshots.get(snapshotable.getElementId()));
                }
            } catch (Throwable t) {
                throw new CannotRestoreSiddhiAppStateException("Restoring of Siddhi app " + siddhiAppContext.getName() + " not completed properly because content of Siddhi app has changed since " + "last state persistence. Clean persistence store for a fresh deployment.", t);
            }
        }
    } finally {
        threadBarrier.unlock();
    }
}
Also used : CannotRestoreSiddhiAppStateException(org.wso2.siddhi.core.exception.CannotRestoreSiddhiAppStateException) List(java.util.List) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)30 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)28 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)24 ArrayList (java.util.ArrayList)22 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)20 Attribute (org.wso2.siddhi.query.api.definition.Attribute)18 StreamEventPool (org.wso2.siddhi.core.event.stream.StreamEventPool)17 ConstantExpressionExecutor (org.wso2.siddhi.core.executor.ConstantExpressionExecutor)17 SiddhiAppValidationException (org.wso2.siddhi.query.api.exception.SiddhiAppValidationException)13 MetaStateEvent (org.wso2.siddhi.core.event.state.MetaStateEvent)12 AbstractDefinition (org.wso2.siddhi.query.api.definition.AbstractDefinition)12 StreamDefinition (org.wso2.siddhi.query.api.definition.StreamDefinition)12 HashMap (java.util.HashMap)11 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)11 SingleStreamRuntime (org.wso2.siddhi.core.query.input.stream.single.SingleStreamRuntime)11 Expression (org.wso2.siddhi.query.api.expression.Expression)11 Table (org.wso2.siddhi.core.table.Table)10 Variable (org.wso2.siddhi.query.api.expression.Variable)10 QueryRuntime (org.wso2.siddhi.core.query.QueryRuntime)8 StreamJunction (org.wso2.siddhi.core.stream.StreamJunction)8