Search in sources :

Example 36 with Sink

use of org.wso2.siddhi.core.stream.output.sink.Sink in project siddhi by wso2.

the class SiddhiAppRuntime method shutdown.

public synchronized void shutdown() {
    SourceHandlerManager sourceHandlerManager = siddhiAppContext.getSiddhiContext().getSourceHandlerManager();
    for (List<Source> sources : sourceMap.values()) {
        for (Source source : sources) {
            try {
                if (sourceHandlerManager != null) {
                    sourceHandlerManager.unregisterSourceHandler(source.getMapper().getHandler().getElementId());
                }
                source.shutdown();
            } catch (Throwable t) {
                log.error(StringUtil.removeCRLFCharacters(ExceptionUtil.getMessageWithContext(t, siddhiAppContext)) + " Error in shutting down source '" + StringUtil.removeCRLFCharacters(source.getType()) + "' at '" + StringUtil.removeCRLFCharacters(source.getStreamDefinition().getId()) + "' on Siddhi App '" + siddhiAppContext.getName() + "'.", t);
            }
        }
    }
    for (Table table : tableMap.values()) {
        try {
            table.shutdown();
        } catch (Throwable t) {
            log.error(StringUtil.removeCRLFCharacters(ExceptionUtil.getMessageWithContext(t, siddhiAppContext)) + " Error in shutting down table '" + StringUtil.removeCRLFCharacters(table.getTableDefinition().getId()) + "' on Siddhi App '" + StringUtil.removeCRLFCharacters(siddhiAppContext.getName()) + "'.", t);
        }
    }
    SinkHandlerManager sinkHandlerManager = siddhiAppContext.getSiddhiContext().getSinkHandlerManager();
    for (List<Sink> sinks : sinkMap.values()) {
        for (Sink sink : sinks) {
            try {
                if (sinkHandlerManager != null) {
                    sinkHandlerManager.unregisterSinkHandler(sink.getHandler().getElementId());
                }
                sink.shutdown();
            } catch (Throwable t) {
                log.error(StringUtil.removeCRLFCharacters(ExceptionUtil.getMessageWithContext(t, siddhiAppContext)) + " Error in shutting down sink '" + StringUtil.removeCRLFCharacters(sink.getType()) + "' at '" + StringUtil.removeCRLFCharacters(sink.getStreamDefinition().getId()) + "' on Siddhi App '" + StringUtil.removeCRLFCharacters(siddhiAppContext.getName()) + "'.", t);
            }
        }
    }
    for (Table table : tableMap.values()) {
        RecordTableHandlerManager recordTableHandlerManager = siddhiAppContext.getSiddhiContext().getRecordTableHandlerManager();
        if (recordTableHandlerManager != null) {
            String elementId = null;
            RecordTableHandler recordTableHandler = table.getHandler();
            if (recordTableHandler != null) {
                elementId = recordTableHandler.getElementId();
            }
            if (elementId != null) {
                recordTableHandlerManager.unregisterRecordTableHandler(elementId);
            }
        }
        table.shutdown();
    }
    for (EternalReferencedHolder eternalReferencedHolder : siddhiAppContext.getEternalReferencedHolders()) {
        try {
            eternalReferencedHolder.stop();
        } catch (Throwable t) {
            log.error(StringUtil.removeCRLFCharacters(ExceptionUtil.getMessageWithContext(t, siddhiAppContext)) + " Error while stopping EternalReferencedHolder '" + StringUtil.removeCRLFCharacters(eternalReferencedHolder.toString()) + "' down Siddhi app '" + StringUtil.removeCRLFCharacters(siddhiAppContext.getName()) + "'.", t);
        }
    }
    inputManager.disconnect();
    Thread thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            for (StreamJunction streamJunction : streamJunctionMap.values()) {
                streamJunction.stopProcessing();
            }
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
            }
            siddhiAppContext.getScheduledExecutorService().shutdownNow();
            siddhiAppContext.getExecutorService().shutdownNow();
        }
    }, "Siddhi-SiddhiApp-" + siddhiAppContext.getName() + "-Shutdown-Cleaner");
    thread.start();
    if (siddhiAppRuntimeMap != null) {
        siddhiAppRuntimeMap.remove(siddhiAppContext.getName());
    }
    if (siddhiAppContext.getStatisticsManager() != null) {
        if (siddhiAppContext.isStatsEnabled()) {
            siddhiAppContext.getStatisticsManager().stopReporting();
        }
        siddhiAppContext.getStatisticsManager().cleanup();
    }
    running = false;
}
Also used : Table(org.wso2.siddhi.core.table.Table) SourceHandlerManager(org.wso2.siddhi.core.stream.input.source.SourceHandlerManager) RecordTableHandler(org.wso2.siddhi.core.table.record.RecordTableHandler) EternalReferencedHolder(org.wso2.siddhi.core.util.extension.holder.EternalReferencedHolder) Source(org.wso2.siddhi.core.stream.input.source.Source) Sink(org.wso2.siddhi.core.stream.output.sink.Sink) StreamJunction(org.wso2.siddhi.core.stream.StreamJunction) SinkHandlerManager(org.wso2.siddhi.core.stream.output.sink.SinkHandlerManager) RecordTableHandlerManager(org.wso2.siddhi.core.table.record.RecordTableHandlerManager)

Example 37 with Sink

use of org.wso2.siddhi.core.stream.output.sink.Sink in project siddhi by wso2.

the class DefinitionParserHelper method addEventSource.

public static void addEventSource(StreamDefinition streamDefinition, ConcurrentMap<String, List<Source>> eventSourceMap, SiddhiAppContext siddhiAppContext) {
    for (Annotation sourceAnnotation : streamDefinition.getAnnotations()) {
        if (SiddhiConstants.ANNOTATION_SOURCE.equalsIgnoreCase(sourceAnnotation.getName())) {
            sourceAnnotation = updateAnnotationRef(sourceAnnotation, SiddhiConstants.NAMESPACE_SOURCE, siddhiAppContext);
            Annotation mapAnnotation = AnnotationHelper.getAnnotation(SiddhiConstants.ANNOTATION_MAP, sourceAnnotation.getAnnotations());
            if (mapAnnotation == null) {
                mapAnnotation = Annotation.annotation(SiddhiConstants.ANNOTATION_MAP).element(SiddhiConstants.ANNOTATION_ELEMENT_TYPE, "passThrough");
            }
            final String sourceType = sourceAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
            final String mapType = mapAnnotation.getElement(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
            if (sourceType != null && mapType != null) {
                SourceHandlerManager sourceHandlerManager = siddhiAppContext.getSiddhiContext().getSourceHandlerManager();
                SourceHandler sourceHandler = null;
                if (sourceHandlerManager != null) {
                    sourceHandler = sourceHandlerManager.generateSourceHandler();
                }
                // load input transport extension
                Extension sourceExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_SOURCE, sourceType, sourceAnnotation, SiddhiConstants.NAMESPACE_SOURCE);
                Source source = (Source) SiddhiClassLoader.loadExtensionImplementation(sourceExtension, SourceExecutorExtensionHolder.getInstance(siddhiAppContext));
                ConfigReader configReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(sourceExtension.getNamespace(), sourceExtension.getName());
                // load input mapper extension
                Extension mapperExtension = constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_MAP, mapType, sourceAnnotation, SiddhiConstants.NAMESPACE_SOURCE_MAPPER);
                SourceMapper sourceMapper = (SourceMapper) SiddhiClassLoader.loadExtensionImplementation(mapperExtension, SourceMapperExecutorExtensionHolder.getInstance(siddhiAppContext));
                ConfigReader mapperConfigReader = siddhiAppContext.getSiddhiContext().getConfigManager().generateConfigReader(mapperExtension.getNamespace(), mapperExtension.getName());
                validateSourceMapperCompatibility(streamDefinition, sourceType, mapType, source, sourceMapper, sourceAnnotation);
                OptionHolder sourceOptionHolder = constructOptionHolder(streamDefinition, sourceAnnotation, source.getClass().getAnnotation(org.wso2.siddhi.annotation.Extension.class), null);
                OptionHolder mapOptionHolder = constructOptionHolder(streamDefinition, mapAnnotation, sourceMapper.getClass().getAnnotation(org.wso2.siddhi.annotation.Extension.class), null);
                AttributesHolder attributesHolder = getAttributeMappings(mapAnnotation, mapType, streamDefinition);
                String[] transportPropertyNames = getTransportPropertyNames(attributesHolder);
                try {
                    source.init(sourceType, sourceOptionHolder, sourceMapper, transportPropertyNames, configReader, mapType, mapOptionHolder, attributesHolder.payloadMappings, attributesHolder.transportMappings, mapperConfigReader, sourceHandler, streamDefinition, siddhiAppContext);
                } catch (Throwable t) {
                    ExceptionUtil.populateQueryContext(t, sourceAnnotation, siddhiAppContext);
                    throw t;
                }
                siddhiAppContext.getSnapshotService().addSnapshotable(source.getStreamDefinition().getId(), source);
                if (sourceHandlerManager != null) {
                    sourceHandlerManager.registerSourceHandler(sourceHandler.getElementId(), sourceHandler);
                    siddhiAppContext.getSnapshotService().addSnapshotable(streamDefinition.getId(), sourceHandler);
                }
                List<Source> eventSources = eventSourceMap.get(streamDefinition.getId());
                if (eventSources == null) {
                    eventSources = new ArrayList<>();
                    eventSources.add(source);
                    eventSourceMap.put(streamDefinition.getId(), eventSources);
                } else {
                    eventSources.add(source);
                }
            } else {
                throw new SiddhiAppCreationException("Both @Sink(type=) and @map(type=) are required.", sourceAnnotation.getQueryContextStartIndex(), sourceAnnotation.getQueryContextEndIndex());
            }
        }
    }
}
Also used : SourceHandler(org.wso2.siddhi.core.stream.input.source.SourceHandler) SourceHandlerManager(org.wso2.siddhi.core.stream.input.source.SourceHandlerManager) SiddhiAppCreationException(org.wso2.siddhi.core.exception.SiddhiAppCreationException) ConfigReader(org.wso2.siddhi.core.util.config.ConfigReader) Annotation(org.wso2.siddhi.query.api.annotation.Annotation) Source(org.wso2.siddhi.core.stream.input.source.Source) Extension(org.wso2.siddhi.query.api.extension.Extension) OptionHolder(org.wso2.siddhi.core.util.transport.OptionHolder) SourceMapper(org.wso2.siddhi.core.stream.input.source.SourceMapper)

Example 38 with Sink

use of org.wso2.siddhi.core.stream.output.sink.Sink in project siddhi by wso2.

the class MultiClientDistributedSink method initTransport.

@Override
public void initTransport(OptionHolder sinkOptionHolder, List<OptionHolder> destinationOptionHolders, Annotation sinkAnnotation, ConfigReader sinkConfigReader, SiddhiAppContext siddhiAppContext) {
    String transportType = sinkOptionHolder.validateAndGetStaticValue(SiddhiConstants.ANNOTATION_ELEMENT_TYPE);
    Extension sinkExtension = DefinitionParserHelper.constructExtension(streamDefinition, SiddhiConstants.ANNOTATION_SINK, transportType, sinkAnnotation, SiddhiConstants.NAMESPACE_SINK);
    destinationOptionHolders.forEach(destinationOption -> {
        Sink sink = (Sink) SiddhiClassLoader.loadExtensionImplementation(sinkExtension, SinkExecutorExtensionHolder.getInstance(siddhiAppContext));
        destinationOption.merge(sinkOptionHolder);
        sink.initOnlyTransport(streamDefinition, destinationOption, sinkConfigReader, siddhiAppContext);
        transports.add(sink);
    });
}
Also used : Extension(org.wso2.siddhi.query.api.extension.Extension) Sink(org.wso2.siddhi.core.stream.output.sink.Sink)

Example 39 with Sink

use of org.wso2.siddhi.core.stream.output.sink.Sink in project siddhi by wso2.

the class PartitionTestCase2 method partitionStreamValidationTest.

@Test(expectedExceptions = SiddhiAppCreationException.class)
public void partitionStreamValidationTest() throws InterruptedException {
    log.info("filter test1");
    SiddhiManager siddhiManager = new SiddhiManager();
    String siddhiApp = "" + "@App:name(\"ExtremaBottomKLeng\")\n" + "@App:Description('Demonstrates how to use the siddhi-execution-extrema with " + "bottomKLengthBatch function')\n" + "\n" + "define stream inputStream (item string, price long);\n" + "\n" + "@sink(type='log') \n" + "define stream outputStream(item string, price long);\n" + "\n" + "partition with (itemsss of inputStreamssss)\n" + "begin \n" + "    from inputStream select item\n" + "    insert into s\n" + "end;\n" + "from inputStream\n" + "insert all events into outputStream; ";
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(siddhiApp);
    try {
        log.info("Running : " + siddhiAppRuntime.getName());
        siddhiAppRuntime.start();
    } finally {
        siddhiAppRuntime.shutdown();
    }
}
Also used : SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) Test(org.testng.annotations.Test)

Example 40 with Sink

use of org.wso2.siddhi.core.stream.output.sink.Sink in project siddhi by wso2.

the class Sink method publish.

@Override
public final void publish(Object payload) {
    if (mapperLatencyTracker != null && siddhiAppContext.isStatsEnabled()) {
        mapperLatencyTracker.markOut();
    }
    if (isConnected.get()) {
        try {
            DynamicOptions dynamicOptions = trpDynamicOptions.get();
            publish(payload, dynamicOptions);
            if (throughputTracker != null && siddhiAppContext.isStatsEnabled()) {
                throughputTracker.eventIn();
            }
        } catch (ConnectionUnavailableException e) {
            isConnected.set(false);
            LOG.error(ExceptionUtil.getMessageWithContext(e, siddhiAppContext) + " Connection unavailable at Sink '" + type + "' at '" + streamDefinition.getId() + "', will retry connection immediately.", e);
            connectWithRetry();
            publish(payload);
        }
    } else if (isTryingToConnect.get()) {
        LOG.error("Error on '" + siddhiAppContext.getName() + "'. Dropping event at Sink '" + type + "' at '" + streamDefinition.getId() + "' as its still trying to reconnect!, events dropped '" + payload + "'");
    } else {
        connectWithRetry();
        publish(payload);
    }
}
Also used : DynamicOptions(org.wso2.siddhi.core.util.transport.DynamicOptions) ConnectionUnavailableException(org.wso2.siddhi.core.exception.ConnectionUnavailableException)

Aggregations

Test (org.testng.annotations.Test)27 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)25 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)25 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)17 InMemoryBroker (org.wso2.siddhi.core.util.transport.InMemoryBroker)16 Sink (org.wso2.siddhi.core.stream.output.sink.Sink)5 HashMap (java.util.HashMap)4 Logger (org.apache.log4j.Logger)4 UnitTestAppender (org.wso2.siddhi.core.UnitTestAppender)4 Extension (org.wso2.siddhi.query.api.extension.Extension)4 Event (org.wso2.siddhi.core.event.Event)3 SiddhiAppCreationException (org.wso2.siddhi.core.exception.SiddhiAppCreationException)3 Source (org.wso2.siddhi.core.stream.input.source.Source)3 InMemoryConfigManager (org.wso2.siddhi.core.util.config.InMemoryConfigManager)3 OptionHolder (org.wso2.siddhi.core.util.transport.OptionHolder)3 APIPolicy (org.wso2.carbon.apimgt.core.models.policy.APIPolicy)2 RequestCountLimit (org.wso2.carbon.apimgt.core.models.policy.RequestCountLimit)2 ConnectionUnavailableException (org.wso2.siddhi.core.exception.ConnectionUnavailableException)2 SourceHandlerManager (org.wso2.siddhi.core.stream.input.source.SourceHandlerManager)2 SinkHandlerManager (org.wso2.siddhi.core.stream.output.sink.SinkHandlerManager)2