Search in sources :

Example 11 with Element

use of org.ballerinalang.siddhi.query.api.annotation.Element in project ballerina by ballerina-lang.

the class DefinitionParserHelper method addEventSink.

public static void addEventSink(StreamDefinition streamDefinition, ConcurrentMap<String, List<Sink>> eventSinkMap, SiddhiAppContext siddhiAppContext) {
    for (Annotation sinkAnnotation : streamDefinition.getAnnotations()) {
        if (SiddhiConstants.ANNOTATION_SINK.equalsIgnoreCase(sinkAnnotation.getName())) {
            sinkAnnotation = updateAnnotationRef(sinkAnnotation, SiddhiConstants.NAMESPACE_SINK, siddhiAppContext);
            Annotation mapAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_MAP, sinkAnnotation.getAnnotations());
            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<>();
                String sinkType = sinkAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
                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);
                }
                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));
                    org.ballerinalang.siddhi.annotation.Extension sinkExt = sink.getClass().getAnnotation(org.ballerinalang.siddhi.annotation.Extension.class);
                    OptionHolder transportOptionHolder = constructOptionHolder(streamDefinition, sinkAnnotation, sinkExt, supportedDynamicOptions);
                    OptionHolder mapOptionHolder = constructOptionHolder(streamDefinition, mapAnnotation, sinkMapper.getClass().getAnnotation(org.ballerinalang.siddhi.annotation.Extension.class), sinkMapper.getSupportedDynamicOptions());
                    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);
                        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));
                        try {
                            distributionStrategy.init(streamDefinition, transportOptionHolder, distributionOptHolder, destinationOptHolders, configReader);
                            ((DistributedTransport) sink).init(streamDefinition, sinkType, transportOptionHolder, sinkConfigReader, sinkMapper, mapType, mapOptionHolder, sinkHandler, payloadElementList, mapperConfigReader, siddhiAppContext, destinationOptHolders, sinkAnnotation, distributionStrategy, supportedDynamicOptions);
                        } catch (Throwable t) {
                            ExceptionUtil.populateQueryContext(t, sinkAnnotation, siddhiAppContext);
                            throw t;
                        }
                    } else {
                        try {
                            sink.init(streamDefinition, sinkType, transportOptionHolder, sinkConfigReader, sinkMapper, mapType, mapOptionHolder, sinkHandler, payloadElementList, mapperConfigReader, siddhiAppContext);
                        } catch (Throwable t) {
                            ExceptionUtil.populateQueryContext(t, sinkAnnotation, siddhiAppContext);
                            throw t;
                        }
                    }
                    if (sinkHandlerManager != null) {
                        sinkHandlerManager.registerSinkHandler(sinkHandler.getElementId(), sinkHandler);
                        siddhiAppContext.getSnapshotService().addSnapshotable(streamDefinition.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);
                    }
                    siddhiAppContext.getSnapshotService().addSnapshotable(sink.getStreamDefinition().getId(), sink);
                    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("Both @sink(type=) and @map(type=) are required.", sinkAnnotation.getQueryContextStartIndex(), sinkAnnotation.getQueryContextEndIndex());
            }
        }
    }
}
Also used : Element(org.ballerinalang.siddhi.query.api.annotation.Element) ArrayList(java.util.ArrayList) OutputGroupDeterminer(org.ballerinalang.siddhi.core.stream.output.sink.OutputGroupDeterminer) SingleClientDistributedSink(org.ballerinalang.siddhi.core.util.transport.SingleClientDistributedSink) Sink(org.ballerinalang.siddhi.core.stream.output.sink.Sink) MultiClientDistributedSink(org.ballerinalang.siddhi.core.util.transport.MultiClientDistributedSink) SingleClientDistributedSink(org.ballerinalang.siddhi.core.util.transport.SingleClientDistributedSink) OptionHolder(org.ballerinalang.siddhi.core.util.transport.OptionHolder) SinkMapper(org.ballerinalang.siddhi.core.stream.output.sink.SinkMapper) SinkHandlerManager(org.ballerinalang.siddhi.core.stream.output.sink.SinkHandlerManager) SinkHandler(org.ballerinalang.siddhi.core.stream.output.sink.SinkHandler) DistributedTransport(org.ballerinalang.siddhi.core.stream.output.sink.distributed.DistributedTransport) DistributionStrategy(org.ballerinalang.siddhi.core.stream.output.sink.distributed.DistributionStrategy) SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException) ConfigReader(org.ballerinalang.siddhi.core.util.config.ConfigReader) Annotation(org.ballerinalang.siddhi.query.api.annotation.Annotation) Extension(org.ballerinalang.siddhi.query.api.extension.Extension) MultiClientDistributedSink(org.ballerinalang.siddhi.core.util.transport.MultiClientDistributedSink)

Example 12 with Element

use of org.ballerinalang.siddhi.query.api.annotation.Element in project ballerina by ballerina-lang.

the class SinkMapper method init.

public final void init(StreamDefinition streamDefinition, String type, OptionHolder mapOptionHolder, List<Element> unmappedPayloadList, Sink sink, ConfigReader mapperConfigReader, LatencyTracker mapperLatencyTracker, SiddhiAppContext siddhiAppContext) {
    this.mapperLatencyTracker = mapperLatencyTracker;
    this.siddhiAppContext = siddhiAppContext;
    sink.setTrpDynamicOptions(trpDynamicOptions);
    this.sinkListener = sink;
    this.optionHolder = mapOptionHolder;
    this.type = type;
    if (unmappedPayloadList != null && !unmappedPayloadList.isEmpty()) {
        templateBuilderMap = new HashMap<>();
        for (Element e : unmappedPayloadList) {
            TemplateBuilder templateBuilder = new TemplateBuilder(streamDefinition, e.getValue());
            if (templateBuilderMap.containsKey(e.getKey())) {
                throw new SiddhiAppCreationException("Duplicate Keys, " + e.getKey() + ", in @payload() ");
            }
            templateBuilderMap.put(e.getKey(), templateBuilder);
        }
    }
    init(streamDefinition, mapOptionHolder, templateBuilderMap, mapperConfigReader, siddhiAppContext);
}
Also used : SiddhiAppCreationException(org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException) Element(org.ballerinalang.siddhi.query.api.annotation.Element) TemplateBuilder(org.ballerinalang.siddhi.core.util.transport.TemplateBuilder)

Aggregations

Element (org.ballerinalang.siddhi.query.api.annotation.Element)12 SiddhiAppCreationException (org.ballerinalang.siddhi.core.exception.SiddhiAppCreationException)7 ArrayList (java.util.ArrayList)6 SiddhiAppValidationException (org.ballerinalang.siddhi.query.api.exception.SiddhiAppValidationException)5 ExecutionElement (org.ballerinalang.siddhi.query.api.execution.ExecutionElement)5 Annotation (org.ballerinalang.siddhi.query.api.annotation.Annotation)4 HashMap (java.util.HashMap)3 Attribute (org.ballerinalang.siddhi.query.api.definition.Attribute)3 ReentrantLock (java.util.concurrent.locks.ReentrantLock)2 SiddhiAppContext (org.ballerinalang.siddhi.core.config.SiddhiAppContext)2 MetaStreamEvent (org.ballerinalang.siddhi.core.event.stream.MetaStreamEvent)2 StreamEventPool (org.ballerinalang.siddhi.core.event.stream.StreamEventPool)2 VariableExpressionExecutor (org.ballerinalang.siddhi.core.executor.VariableExpressionExecutor)2 QueryRuntime (org.ballerinalang.siddhi.core.query.QueryRuntime)2 StreamRuntime (org.ballerinalang.siddhi.core.query.input.stream.StreamRuntime)2 SingleStreamRuntime (org.ballerinalang.siddhi.core.query.input.stream.single.SingleStreamRuntime)2 LockWrapper (org.ballerinalang.siddhi.core.util.lock.LockWrapper)2 LatencyTracker (org.ballerinalang.siddhi.core.util.statistics.LatencyTracker)2 Window (org.ballerinalang.siddhi.core.window.Window)2 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)1