use of org.wso2.siddhi.core.config.SiddhiAppContext in project siddhi by wso2.
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.wso2.siddhi.annotation.Extension sinkExt = sink.getClass().getAnnotation(org.wso2.siddhi.annotation.Extension.class);
OptionHolder transportOptionHolder = constructOptionHolder(streamDefinition, sinkAnnotation, sinkExt, supportedDynamicOptions);
OptionHolder mapOptionHolder = constructOptionHolder(streamDefinition, mapAnnotation, sinkMapper.getClass().getAnnotation(org.wso2.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());
}
}
}
}
use of org.wso2.siddhi.core.config.SiddhiAppContext in project siddhi by wso2.
the class QueryParserHelper method createThroughputTracker.
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;
}
use of org.wso2.siddhi.core.config.SiddhiAppContext in project siddhi by wso2.
the class SiddhiAppRuntimeBuilder method defineWindow.
public void defineWindow(WindowDefinition windowDefinition) {
DefinitionParserHelper.validateDefinition(windowDefinition, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap);
DefinitionParserHelper.addStreamJunction(windowDefinition, streamJunctionMap, siddhiAppContext);
AbstractDefinition currentDefinition = windowDefinitionMap.putIfAbsent(windowDefinition.getId(), windowDefinition);
if (currentDefinition != null) {
windowDefinition = (WindowDefinition) currentDefinition;
}
DefinitionParserHelper.addWindow(windowDefinition, windowMap, siddhiAppContext);
// defineStream(windowDefinition);
// DefinitionParserHelper.addStreamJunction(windowDefinition, streamJunctionMap, siddhiAppContext);
}
use of org.wso2.siddhi.core.config.SiddhiAppContext in project siddhi by wso2.
the class SiddhiAppRuntimeBuilder method defineTable.
public void defineTable(TableDefinition tableDefinition) {
DefinitionParserHelper.validateDefinition(tableDefinition, streamDefinitionMap, tableDefinitionMap, windowDefinitionMap, aggregationDefinitionMap);
AbstractDefinition currentDefinition = tableDefinitionMap.putIfAbsent(tableDefinition.getId(), tableDefinition);
if (currentDefinition != null) {
tableDefinition = (TableDefinition) currentDefinition;
}
DefinitionParserHelper.addTable(tableDefinition, tableMap, siddhiAppContext);
}
use of org.wso2.siddhi.core.config.SiddhiAppContext in project siddhi by wso2.
the class MultiClientDistributedSink method connect.
/**
* Will be called to connect to the backend before events are published
*
* @throws ConnectionUnavailableException if it cannot connect to the backend
*/
@Override
public void connect() throws ConnectionUnavailableException {
StringBuilder errorMessages = null;
int errorCount = 0;
for (int i = 0; i < transports.size(); i++) {
try {
Sink transport = transports.get(i);
if (!transport.isConnected()) {
transport.connect();
transport.setConnected(true);
strategy.destinationAvailable(i);
log.info("Connected to destination Id " + i);
}
} catch (ConnectionUnavailableException e) {
errorCount++;
if (errorMessages == null) {
errorMessages = new StringBuilder();
}
errorMessages.append("[Destination").append(i).append("]:").append(e.getMessage());
log.warn(ExceptionUtil.getMessageWithContext(e, siddhiAppContext) + " Failed to Connect to destination ID " + i);
}
}
if (errorCount > 0) {
throw new ConnectionUnavailableException("Error on '" + siddhiAppContext.getName() + "'. " + errorCount + "/" + transports.size() + " connections failed while trying to connect with following error " + "messages:" + errorMessages.toString());
}
}
Aggregations