Search in sources :

Example 1 with ConnectionUnavailableException

use of org.wso2.siddhi.core.exception.ConnectionUnavailableException in project siddhi by wso2.

the class AbstractRecordTable method find.

@Override
public StreamEvent find(CompiledCondition compiledCondition, StateEvent matchingEvent) throws ConnectionUnavailableException {
    RecordStoreCompiledCondition recordStoreCompiledCondition = ((RecordStoreCompiledCondition) compiledCondition);
    Map<String, Object> findConditionParameterMap = new HashMap<>();
    for (Map.Entry<String, ExpressionExecutor> entry : recordStoreCompiledCondition.variableExpressionExecutorMap.entrySet()) {
        findConditionParameterMap.put(entry.getKey(), entry.getValue().execute(matchingEvent));
    }
    Iterator<Object[]> records;
    if (recordTableHandler != null) {
        records = recordTableHandler.find(matchingEvent.getTimestamp(), findConditionParameterMap, recordStoreCompiledCondition.compiledCondition);
    } else {
        records = find(findConditionParameterMap, recordStoreCompiledCondition.compiledCondition);
    }
    ComplexEventChunk<StreamEvent> streamEventComplexEventChunk = new ComplexEventChunk<>(true);
    if (records != null) {
        while (records.hasNext()) {
            Object[] record = records.next();
            StreamEvent streamEvent = storeEventPool.borrowEvent();
            System.arraycopy(record, 0, streamEvent.getOutputData(), 0, record.length);
            streamEventComplexEventChunk.add(streamEvent);
        }
    }
    return streamEventComplexEventChunk.getFirst();
}
Also used : VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) ComplexEventChunk(org.wso2.siddhi.core.event.ComplexEventChunk) HashMap(java.util.HashMap) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with ConnectionUnavailableException

use of org.wso2.siddhi.core.exception.ConnectionUnavailableException 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());
    }
}
Also used : Sink(org.wso2.siddhi.core.stream.output.sink.Sink) ConnectionUnavailableException(org.wso2.siddhi.core.exception.ConnectionUnavailableException)

Example 3 with ConnectionUnavailableException

use of org.wso2.siddhi.core.exception.ConnectionUnavailableException in project siddhi by wso2.

the class InMemoryTransportTestCase method inMemoryWithFailingSource.

@Test
public void inMemoryWithFailingSource() throws InterruptedException {
    log.info("Test failing inMemorySource");
    String streams = "" + "@app:name('TestSiddhiApp')" + "@source(type='testFailingInMemory', topic='WSO2', @map(type='passThrough')) " + "define stream FooStream (symbol string, price float, volume long); " + "define stream BarStream (symbol string, price float, volume long); ";
    String query = "" + "from FooStream " + "select * " + "insert into BarStream; ";
    SiddhiManager siddhiManager = new SiddhiManager();
    SiddhiAppRuntime siddhiAppRuntime = siddhiManager.createSiddhiAppRuntime(streams + query);
    siddhiAppRuntime.addCallback("BarStream", new StreamCallback() {

        @Override
        public void receive(Event[] events) {
            EventPrinter.print(events);
            for (Event event : events) {
                wso2Count.incrementAndGet();
            }
        }
    });
    siddhiAppRuntime.start();
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 55.6f, 100L }));
    InMemoryBroker.publish("IBM", new Event(System.currentTimeMillis(), new Object[] { "IBM", 75.6f, 100L }));
    TestFailingInMemorySource.fail = true;
    TestFailingInMemorySource.connectionCallback.onError(new ConnectionUnavailableException("Connection Lost"));
    Thread.sleep(500);
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 57.6f, 100L }));
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 57.6f, 100L }));
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 57.6f, 100L }));
    TestFailingInMemorySource.fail = false;
    Thread.sleep(5500);
    InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 55.6f, 100L }));
    InMemoryBroker.publish("IBM", new Event(System.currentTimeMillis(), new Object[] { "IBM", 75.6f, 100L }));
    // assert event count
    AssertJUnit.assertEquals("Number of WSO2 events", 2, wso2Count.get());
    AssertJUnit.assertEquals("Number of errors", 1, TestFailingInMemorySource.numberOfErrorOccurred);
    siddhiAppRuntime.shutdown();
}
Also used : SiddhiAppRuntime(org.wso2.siddhi.core.SiddhiAppRuntime) Event(org.wso2.siddhi.core.event.Event) ConnectionUnavailableException(org.wso2.siddhi.core.exception.ConnectionUnavailableException) SiddhiManager(org.wso2.siddhi.core.SiddhiManager) StreamCallback(org.wso2.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 4 with ConnectionUnavailableException

use of org.wso2.siddhi.core.exception.ConnectionUnavailableException 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)

Example 5 with ConnectionUnavailableException

use of org.wso2.siddhi.core.exception.ConnectionUnavailableException in project siddhi by wso2.

the class AbstractQueryableRecordTable method query.

@Override
public StreamEvent query(StateEvent matchingEvent, CompiledCondition compiledCondition, CompiledSelection compiledSelection) throws ConnectionUnavailableException {
    RecordStoreCompiledSelection recordStoreCompiledSelection = ((RecordStoreCompiledSelection) compiledSelection);
    RecordStoreCompiledCondition recordStoreCompiledCondition = ((RecordStoreCompiledCondition) compiledCondition);
    Map<String, Object> parameterMap = new HashMap<>();
    for (Map.Entry<String, ExpressionExecutor> entry : recordStoreCompiledCondition.variableExpressionExecutorMap.entrySet()) {
        parameterMap.put(entry.getKey(), entry.getValue().execute(matchingEvent));
    }
    for (Map.Entry<String, ExpressionExecutor> entry : recordStoreCompiledSelection.variableExpressionExecutorMap.entrySet()) {
        parameterMap.put(entry.getKey(), entry.getValue().execute(matchingEvent));
    }
    Iterator<Object[]> records;
    if (recordTableHandler != null) {
        records = recordTableHandler.query(matchingEvent.getTimestamp(), parameterMap, recordStoreCompiledCondition.compiledCondition, recordStoreCompiledSelection.compiledSelection);
    } else {
        records = query(parameterMap, recordStoreCompiledCondition.compiledCondition, recordStoreCompiledSelection.compiledSelection);
    }
    ComplexEventChunk<StreamEvent> streamEventComplexEventChunk = new ComplexEventChunk<>(true);
    if (records != null) {
        while (records.hasNext()) {
            Object[] record = records.next();
            StreamEvent streamEvent = storeEventPool.borrowEvent();
            System.arraycopy(record, 0, streamEvent.getOutputData(), 0, record.length);
            streamEventComplexEventChunk.add(streamEvent);
        }
    }
    return streamEventComplexEventChunk.getFirst();
}
Also used : VariableExpressionExecutor(org.wso2.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(org.wso2.siddhi.core.executor.ExpressionExecutor) ConstantExpressionExecutor(org.wso2.siddhi.core.executor.ConstantExpressionExecutor) ComplexEventChunk(org.wso2.siddhi.core.event.ComplexEventChunk) HashMap(java.util.HashMap) MetaStreamEvent(org.wso2.siddhi.core.event.stream.MetaStreamEvent) StreamEvent(org.wso2.siddhi.core.event.stream.StreamEvent) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

HashMap (java.util.HashMap)5 Map (java.util.Map)5 ExpressionExecutor (org.wso2.siddhi.core.executor.ExpressionExecutor)5 VariableExpressionExecutor (org.wso2.siddhi.core.executor.VariableExpressionExecutor)5 ArrayList (java.util.ArrayList)4 StateEvent (org.wso2.siddhi.core.event.state.StateEvent)3 StreamEvent (org.wso2.siddhi.core.event.stream.StreamEvent)3 ConnectionUnavailableException (org.wso2.siddhi.core.exception.ConnectionUnavailableException)3 ComplexEventChunk (org.wso2.siddhi.core.event.ComplexEventChunk)2 Test (org.testng.annotations.Test)1 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)1 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)1 Event (org.wso2.siddhi.core.event.Event)1 MetaStreamEvent (org.wso2.siddhi.core.event.stream.MetaStreamEvent)1 ConstantExpressionExecutor (org.wso2.siddhi.core.executor.ConstantExpressionExecutor)1 StreamCallback (org.wso2.siddhi.core.stream.output.StreamCallback)1 Sink (org.wso2.siddhi.core.stream.output.sink.Sink)1 DynamicOptions (org.wso2.siddhi.core.util.transport.DynamicOptions)1