Java Code Examples for io.siddhi.core.config.SiddhiAppContext#getName()

The following examples show how to use io.siddhi.core.config.SiddhiAppContext#getName() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryParserHelper.java    From siddhi with Apache License 2.0 6 votes vote down vote up
public static void registerMemoryUsageTracking(String name, Object value, String metricInfixQueries,
                                               SiddhiAppContext siddhiAppContext,
                                               MemoryUsageTracker memoryUsageTracker) {
    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 +
            metricInfixQueries + SiddhiConstants.METRIC_DELIMITER +
            name + SiddhiConstants.METRIC_DELIMITER + "memory";
    boolean matchExist = false;
    for (String regex : siddhiAppContext.getIncludedMetrics()) {
        if (metricName.matches(regex)) {
            matchExist = true;
            break;
        }
    }
    if (matchExist) {
        memoryUsageTracker.registerObject(value, metricName);
    }
}
 
Example 2
Source File: LogSink.java    From siddhi with Apache License 2.0 5 votes vote down vote up
@Override
protected StateFactory<State> init(StreamDefinition outputStreamDefinition, OptionHolder optionHolder,
                                   ConfigReader sinkConfigReader, SiddhiAppContext siddhiAppContext) {
    String defaultPrefix = siddhiAppContext.getName() + " : " + outputStreamDefinition.getId();
    logPrefix = optionHolder.validateAndGetStaticValue(PREFIX, defaultPrefix);
    logPriority = LogPriority.valueOf(optionHolder.validateAndGetStaticValue(PRIORITY, "INFO")
            .toUpperCase());
    return null;
}
 
Example 3
Source File: RecordTableHandler.java    From siddhi with Apache License 2.0 5 votes vote down vote up
protected final void init(TableDefinition tableDefinition, RecordTableHandlerCallback recordTableHandlerCallback,
                          SiddhiAppContext siddhiAppContext) {
    this.recordTableHandlerCallback = recordTableHandlerCallback;

    id = siddhiAppContext.getName() + "-" + tableDefinition.getId() + "-" + this.getClass().getName();
    StateFactory<S> stateFactory = init(id, tableDefinition);
    stateHolder = siddhiAppContext.generateStateHolder(
            tableDefinition.getId() + "-" + this.getClass().getName(),
            stateFactory);

}
 
Example 4
Source File: QueryParserHelper.java    From siddhi with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 5
Source File: QueryParserHelper.java    From siddhi with Apache License 2.0 5 votes vote down vote up
public static ThroughputTracker createThroughputTracker(SiddhiAppContext siddhiAppContext, String name,
                                                        String type, String function) {
    ThroughputTracker throughputTracker = 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 + "throughput";
        boolean matchExist = false;
        for (String regex : siddhiAppContext.getIncludedMetrics()) {
            if (metricName.matches(regex)) {
                matchExist = true;
                break;
            }
        }
        if (matchExist) {
            throughputTracker = siddhiAppContext
                    .getSiddhiContext()
                    .getStatisticsConfiguration()
                    .getFactory()
                    .createThroughputTracker(metricName, siddhiAppContext.getStatisticsManager());
        }
    }
    return throughputTracker;
}
 
Example 6
Source File: PersistenceHelper.java    From siddhi with Apache License 2.0 5 votes vote down vote up
public static PersistenceReference persist(byte[] serializeObj, SiddhiAppContext siddhiAppContext) {
    long revisionTime = System.currentTimeMillis();
    // start the snapshot persisting task asynchronously
    AsyncSnapshotPersistor asyncSnapshotPersistor = new AsyncSnapshotPersistor(serializeObj,
            siddhiAppContext.getSiddhiContext().getPersistenceStore(), siddhiAppContext.getName(),
            revisionTime);
    Future future = siddhiAppContext.getExecutorService().submit(asyncSnapshotPersistor);
    return new PersistenceReference(future, asyncSnapshotPersistor.getRevision());
}
 
Example 7
Source File: StreamJunction.java    From siddhi with Apache License 2.0 4 votes vote down vote up
public StreamJunction(StreamDefinition streamDefinition, ExecutorService executorService, int bufferSize,
                      StreamJunction faultStreamJunction, SiddhiAppContext siddhiAppContext) {
    this.streamDefinition = streamDefinition;
    this.bufferSize = bufferSize;
    this.batchSize = bufferSize;
    this.executorService = executorService;
    this.siddhiAppContext = siddhiAppContext;
    if (siddhiAppContext.getStatisticsManager() != null) {
        this.throughputTracker = QueryParserHelper.createThroughputTracker(siddhiAppContext,
                streamDefinition.getId(),
                SiddhiConstants.METRIC_INFIX_STREAMS, null);
    }
    this.faultStreamJunction = faultStreamJunction;
    if (faultStreamJunction != null) {
        StreamDefinition faultStreamDefinition = faultStreamJunction.getStreamDefinition();
        StreamEventFactory faultStreamEventFactory = new StreamEventFactory(0, 0,
                faultStreamDefinition.getAttributeList().size());
        faultStreamEventConverter = new FaultStreamEventConverter(faultStreamEventFactory);
    }
    try {
        Annotation asyncAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_ASYNC,
                streamDefinition.getAnnotations());
        if (asyncAnnotation != null) {
            async = true;
            String bufferSizeString = asyncAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_BUFFER_SIZE);
            if (bufferSizeString != null) {
                this.bufferSize = Integer.parseInt(bufferSizeString);
            }
            String workersString = asyncAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_WORKERS);
            if (workersString != null) {
                this.workers = Integer.parseInt(workersString);
                if (workers <= 0) {
                    throw new SiddhiAppCreationException("Annotation element '" +
                            SiddhiConstants.ANNOTATION_ELEMENT_WORKERS + "' cannot be negative or zero, " +
                            "but found, '" + workers + "'.", asyncAnnotation.getQueryContextStartIndex(),
                            asyncAnnotation.getQueryContextEndIndex(), siddhiAppContext.getName(),
                            siddhiAppContext.getSiddhiAppString());
                }
            }
            String batchSizeString = asyncAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_MAX_BATCH_SIZE);
            if (batchSizeString != null) {
                this.batchSize = Integer.parseInt(batchSizeString);
                if (batchSize <= 0) {
                    throw new SiddhiAppCreationException("Annotation element '" +
                            SiddhiConstants.ANNOTATION_ELEMENT_MAX_BATCH_SIZE + "' cannot be negative or zero, " +
                            "but found, '" + batchSize + "'.", asyncAnnotation.getQueryContextStartIndex(),
                            asyncAnnotation.getQueryContextEndIndex(), siddhiAppContext.getName(),
                            siddhiAppContext.getSiddhiAppString());
                }
            }
        }
        Annotation onErrorAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_ON_ERROR,
                streamDefinition.getAnnotations());
        if (onErrorAnnotation != null) {
            this.onErrorAction = OnErrorAction.valueOf(onErrorAnnotation
                    .getElement(SiddhiConstants.ANNOTATION_ELEMENT_ACTION).toUpperCase());
        }
    } catch (DuplicateAnnotationException e) {
        throw new DuplicateAnnotationException(e.getMessageWithOutContext() + " for the same Stream " +
                streamDefinition.getId(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(),
                siddhiAppContext.getName(), siddhiAppContext.getSiddhiAppString());
    }
    isTraceEnabled = log.isTraceEnabled();
}
 
Example 8
Source File: PartitionRuntimeImpl.java    From siddhi with Apache License 2.0 4 votes vote down vote up
public PartitionRuntimeImpl(ConcurrentMap<String, AbstractDefinition> streamDefinitionMap,
                            ConcurrentMap<String, AbstractDefinition> windowDefinitionMap,
                            ConcurrentMap<String, StreamJunction> streamJunctionMap,
                            Partition partition, int partitionIndex, SiddhiAppContext siddhiAppContext) {
    this.siddhiAppContext = siddhiAppContext;
    if (partition.getPartitionTypeMap().isEmpty()) {
        throw new SiddhiAppCreationException("Partition must have at least one partition executor. " +
                "But found none.");
    }
    try {
        Element element = AnnotationHelper.getAnnotationElement("info", "name",
                partition.getAnnotations());
        if (element != null) {
            this.partitionName = element.getValue();
        }
    } catch (DuplicateAnnotationException e) {
        throw new DuplicateAnnotationException(e.getMessageWithOutContext() + " for the same Query " +
                partition.toString(), e, e.getQueryContextStartIndex(), e.getQueryContextEndIndex(),
                siddhiAppContext.getName(), siddhiAppContext.getSiddhiAppString());
    }
    if (partitionName == null) {
        this.partitionName = "partition_" + partitionIndex;
    }

    Annotation purge = AnnotationHelper.getAnnotation(SiddhiConstants.NAMESPACE_PURGE, partition.getAnnotations());
    if (purge != null) {
        if (purge.getElement(SiddhiConstants.ANNOTATION_ELEMENT_ENABLE) != null) {
            String purgeEnable = purge.getElement(SiddhiConstants.ANNOTATION_ELEMENT_ENABLE);
            if (!("true".equalsIgnoreCase(purgeEnable) || "false".equalsIgnoreCase(purgeEnable))) {
                throw new SiddhiAppCreationException("Invalid value for enable: " + purgeEnable + "." +
                        " Please use 'true' or 'false'");
            } else {
                purgingEnabled = Boolean.parseBoolean(purgeEnable);
            }
        } else {
            throw new SiddhiAppCreationException("Annotation @" + SiddhiConstants.NAMESPACE_PURGE +
                    " is missing element '" + SiddhiConstants.ANNOTATION_ELEMENT_ENABLE + "'");
        }
        if (purge.getElement(SiddhiConstants.ANNOTATION_ELEMENT_IDLE_PERIOD) != null) {
            String purgeIdle = purge.getElement(SiddhiConstants.ANNOTATION_ELEMENT_IDLE_PERIOD);
            purgeIdlePeriod = Expression.Time.timeToLong(purgeIdle);

        } else {
            throw new SiddhiAppCreationException("Annotation @" + SiddhiConstants.NAMESPACE_PURGE +
                    " is missing element '" + SiddhiConstants.ANNOTATION_ELEMENT_IDLE_PERIOD + "'");
        }

        if (purge.getElement(SiddhiConstants.ANNOTATION_ELEMENT_INTERVAL) != null) {
            String interval = purge.getElement(SiddhiConstants.ANNOTATION_ELEMENT_INTERVAL);
            purgeExecutionInterval = Expression.Time.timeToLong(interval);
        }
    }
    this.partition = partition;
    this.streamDefinitionMap = streamDefinitionMap;
    this.windowDefinitionMap = windowDefinitionMap;
    this.streamJunctionMap = streamJunctionMap;

    this.stateHolder = siddhiAppContext.generateStateHolder(partitionName, () -> new PartitionState());
}
 
Example 9
Source File: IndexEventHolder.java    From siddhi with Apache License 2.0 4 votes vote down vote up
public IndexEventHolder(StreamEventFactory tableStreamEventFactory, StreamEventConverter eventConverter,
                        PrimaryKeyReferenceHolder[] primaryKeyReferenceHolders,
                        boolean isPrimaryNumeric, Map<String, Integer> indexMetaData,
                        AbstractDefinition tableDefinition, SiddhiAppContext siddhiAppContext) {
    this.tableStreamEventFactory = tableStreamEventFactory;
    this.eventConverter = eventConverter;
    this.primaryKeyReferenceHolders = primaryKeyReferenceHolders;
    this.indexMetaData = indexMetaData;
    this.tableName = tableDefinition.getId();
    this.siddhiAppName = siddhiAppContext.getName();
    this.siddhiAppContext = siddhiAppContext;

    if (primaryKeyReferenceHolders != null) {
        if (isPrimaryNumeric) {
            primaryKeyData = new TreeMap<Object, StreamEvent>();
        } else {
            primaryKeyData = new HashMap<Object, StreamEvent>();
        }
        if (primaryKeyReferenceHolders.length == 1) {
            allIndexMetaData.put(primaryKeyReferenceHolders[0].getPrimaryKeyAttribute(),
                    primaryKeyReferenceHolders[0].getPrimaryKeyPosition());
            primaryKeyAttributes = primaryKeyReferenceHolders[0].getPrimaryKeyAttribute();
        } else {
            StringBuilder primaryKeyAttributesBuilder = new StringBuilder();
            for (PrimaryKeyReferenceHolder primaryKeyReferenceHolder : primaryKeyReferenceHolders) {
                multiPrimaryKeyMetaData.put(primaryKeyReferenceHolder.getPrimaryKeyAttribute(),
                        primaryKeyReferenceHolder.getPrimaryKeyPosition());
                primaryKeyAttributesBuilder.append(primaryKeyReferenceHolder.getPrimaryKeyAttribute())
                        .append(SiddhiConstants.KEY_DELIMITER);
            }
            primaryKeyAttributes = primaryKeyAttributesBuilder.toString();
        }
    } else {
        primaryKeyData = null;
    }
    if (indexMetaData.size() > 0) {
        indexData = new HashMap<String, TreeMap<Object, Set<StreamEvent>>>();
        for (String indexAttributeName : indexMetaData.keySet()) {
            indexData.put(indexAttributeName, new TreeMap<Object, Set<StreamEvent>>());
        }
        allIndexMetaData.putAll(indexMetaData);
    } else {
        indexData = null;
    }

}
 
Example 10
Source File: AbstractQueryableRecordTable.java    From siddhi with Apache License 2.0 4 votes vote down vote up
@Override
public void initCache(TableDefinition tableDefinition, SiddhiAppContext siddhiAppContext,
                      StreamEventCloner storeEventCloner, ConfigReader configReader) {
    String[] annotationNames = {ANNOTATION_STORE, ANNOTATION_CACHE};
    Annotation cacheTableAnnotation = getAnnotation(annotationNames, tableDefinition.getAnnotations());
    if (cacheTableAnnotation != null) {
        cacheEnabled = true;
        maxCacheSize = Integer.parseInt(cacheTableAnnotation.getElement(CACHE_TABLE_SIZE));
        TableDefinition cacheTableDefinition = TableDefinition.id(tableDefinition.getId());
        for (Attribute attribute : tableDefinition.getAttributeList()) {
            cacheTableDefinition.attribute(attribute.getName(), attribute.getType());
        }
        for (Annotation annotation : tableDefinition.getAnnotations()) {
            if (!annotation.getName().equalsIgnoreCase("Store")) {
                cacheTableDefinition.annotation(annotation);
            }
        }

        String cachePolicy = cacheTableAnnotation.getElement(ANNOTATION_CACHE_POLICY);

        if (cachePolicy == null || cachePolicy.equalsIgnoreCase("FIFO")) {
            cachePolicy = "FIFO";
            cacheTable = new CacheTableFIFO();
        } else if (cachePolicy.equalsIgnoreCase("LRU")) {
            cacheTable = new CacheTableLRU();
        } else if (cachePolicy.equalsIgnoreCase("LFU")) {
            cacheTable = new CacheTableLFU();
        } else {
            throw new SiddhiAppCreationException(siddhiAppContext.getName() + " : Cache policy can only be one " +
                    "of FIFO, LRU, and LFU but given as " + cachePolicy);
        }

        // check if cache expiry enabled and initialize relevant parameters
        if (cacheTableAnnotation.getElement(ANNOTATION_CACHE_RETENTION_PERIOD) != null) {
            cacheExpiryEnabled = true;
            retentionPeriod = Expression.Time.timeToLong(cacheTableAnnotation.
                    getElement(ANNOTATION_CACHE_RETENTION_PERIOD));
            if (cacheTableAnnotation.getElement(ANNOTATION_CACHE_PURGE_INTERVAL) == null) {
                purgeInterval = retentionPeriod;
            } else {
                purgeInterval = Expression.Time.timeToLong(cacheTableAnnotation.
                        getElement(ANNOTATION_CACHE_PURGE_INTERVAL));
            }
            storeSizeCheckInterval = purgeInterval * 5;
        } else {
            storeSizeCheckInterval = 10000;
        }

        ((CacheTable) cacheTable).initCacheTable(cacheTableDefinition, configReader, siddhiAppContext,
                recordTableHandler, cacheExpiryEnabled, maxCacheSize, cachePolicy);

        // creating objects needed to load cache
        SiddhiQueryContext siddhiQueryContext = new SiddhiQueryContext(siddhiAppContext,
                CACHE_QUERY_NAME + tableDefinition.getId());
        MatchingMetaInfoHolder matchingMetaInfoHolder =
                generateMatchingMetaInfoHolderForCacheTable(tableDefinition);
        OnDemandQuery onDemandQuery = OnDemandQuery.query().
                from(
                        InputStore.store(tableDefinition.getId())).
                select(
                        Selector.selector().
                                limit(Expression.value((maxCacheSize + 1)))
                );
        List<VariableExpressionExecutor> variableExpressionExecutors = new ArrayList<>();

        compiledConditionForCaching = compileCondition(Expression.value(true), matchingMetaInfoHolder,
                variableExpressionExecutors, tableMap, siddhiQueryContext);
        List<Attribute> expectedOutputAttributes = buildExpectedOutputAttributes(onDemandQuery,
                tableMap, SiddhiConstants.UNKNOWN_STATE, matchingMetaInfoHolder, siddhiQueryContext);

        compiledSelectionForCaching = compileSelection(onDemandQuery.getSelector(), expectedOutputAttributes,
                matchingMetaInfoHolder, variableExpressionExecutors, tableMap, siddhiQueryContext);

        outputAttributesForCaching = expectedOutputAttributes.toArray(new Attribute[0]);
        QueryParserHelper.reduceMetaComplexEvent(matchingMetaInfoHolder.getMetaStateEvent());
        QueryParserHelper.updateVariablePosition(matchingMetaInfoHolder.getMetaStateEvent(),
                variableExpressionExecutors);
        compiledSelectionForSelectAll = generateCSForSelectAll();
    }
}
 
Example 11
Source File: ExceptionUtil.java    From siddhi with Apache License 2.0 4 votes vote down vote up
public static void populateQueryContext(Throwable t, SiddhiElement siddhiElement,
                                        SiddhiAppContext siddhiAppContext,
                                        SiddhiQueryContext siddhiQueryContext) {
    String siddhiAppString = null;
    if (siddhiQueryContext instanceof SiddhiOnDemandQueryContext &&
            ((SiddhiOnDemandQueryContext) siddhiQueryContext).getOnDemandQueryString() != null) {
        siddhiAppString = ((SiddhiOnDemandQueryContext) siddhiQueryContext).getOnDemandQueryString();
    } else if (siddhiAppContext != null) {
        siddhiAppString = siddhiAppContext.getSiddhiAppString();
    }

    if (siddhiElement != null) {
        if (siddhiAppContext != null) {
            if (t instanceof SiddhiAppContextException) {
                ((SiddhiAppContextException) t).setQueryContextIndexIfAbsent(
                        siddhiElement.getQueryContextStartIndex(),
                        siddhiElement.getQueryContextEndIndex(), siddhiAppContext.getName(),
                        siddhiAppString);
            } else {
                throw new SiddhiAppCreationException(t.getMessage(), t, siddhiElement.getQueryContextStartIndex(),
                        siddhiElement.getQueryContextEndIndex(), siddhiAppContext.getName(),
                        siddhiAppString);
            }
        } else {
            if (t instanceof SiddhiAppContextException) {
                ((SiddhiAppContextException) t).setQueryContextIndexIfAbsent(
                        siddhiElement.getQueryContextStartIndex(),
                        siddhiElement.getQueryContextEndIndex(), null, null);
            } else {
                throw new SiddhiAppCreationException(t.getMessage(), t, siddhiElement.getQueryContextStartIndex(),
                        siddhiElement.getQueryContextEndIndex(), null, null);
            }
        }
    } else {
        if (siddhiAppContext != null) {
            if (t instanceof SiddhiAppContextException) {
                ((SiddhiAppContextException) t).setQueryContextIndexIfAbsent(
                        null, null, siddhiAppContext.getName(),
                        siddhiAppString);
            } else {
                throw new SiddhiAppCreationException(t.getMessage(), t, null, null, siddhiAppContext.getName(),
                        siddhiAppString);
            }
        } else {
            if (!(t instanceof SiddhiAppContextException)) {
                throw new SiddhiAppCreationException(t.getMessage(), t, null, null, null, null);
            }
        }
    }
}