Search in sources :

Example 1 with ConnectionUnavailableException

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

the class InMemoryTransportTestCase method inMemoryWithFailingSource.

@Test(dependsOnMethods = { "inMemoryWithFailingSink1" })
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();
    try {
        InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 55.6f, 100L }));
    } catch (SubscriberUnAvailableException e) {
        AssertJUnit.fail();
    }
    TestFailingInMemorySource.fail = true;
    TestFailingInMemorySource.connectionCallback.onError(new ConnectionUnavailableException("Connection Lost"));
    Thread.sleep(6000);
    try {
        InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 57.6f, 100L }));
        AssertJUnit.fail();
    } catch (Throwable e) {
        AssertJUnit.assertTrue(e instanceof SubscriberUnAvailableException);
    }
    TestFailingInMemorySource.fail = false;
    Thread.sleep(10000);
    try {
        InMemoryBroker.publish("WSO2", new Event(System.currentTimeMillis(), new Object[] { "WSO2", 55.6f, 100L }));
    } catch (SubscriberUnAvailableException e) {
        AssertJUnit.fail();
    }
    // assert event count
    AssertJUnit.assertEquals("Number of WSO2 events", 2, wso2Count.get());
    AssertJUnit.assertEquals("Number of errors", 1, TestFailingInMemorySource.numberOfErrorOccurred);
    siddhiAppRuntime.shutdown();
}
Also used : SubscriberUnAvailableException(io.siddhi.core.util.transport.SubscriberUnAvailableException) SiddhiAppRuntime(io.siddhi.core.SiddhiAppRuntime) Event(io.siddhi.core.event.Event) ConnectionUnavailableException(io.siddhi.core.exception.ConnectionUnavailableException) SiddhiManager(io.siddhi.core.SiddhiManager) StreamCallback(io.siddhi.core.stream.output.StreamCallback) Test(org.testng.annotations.Test)

Example 2 with ConnectionUnavailableException

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

the class ErrorHandlerUtils method constructAddErrorRecordString.

public static String constructAddErrorRecordString(ComplexEventChunk<StreamEvent> eventChunk, boolean isObjectColumnPresent, TableDefinition tableDefinition, Exception e) {
    JsonObject payloadJson = new JsonObject();
    payloadJson.addProperty("isEditable", !(isObjectColumnPresent || e instanceof ConnectionUnavailableException));
    JsonArray attributes = new JsonArray();
    JsonArray records = new JsonArray();
    for (Attribute attribute : tableDefinition.getAttributeList()) {
        JsonObject attributeJson = new JsonObject();
        attributeJson.add("name", new JsonPrimitive(attribute.getName()));
        attributeJson.add("type", new JsonPrimitive(String.valueOf(attribute.getType())));
        attributes.add(attributeJson);
    }
    payloadJson.add("attributes", attributes);
    while (eventChunk.hasNext()) {
        StreamEvent streamEvent = eventChunk.next();
        JsonArray record = new JsonArray();
        for (Object item : streamEvent.getOutputData()) {
            if (item == null) {
                record.add(JsonNull.INSTANCE);
            } else {
                record.add(String.valueOf(item));
            }
        }
        records.add(record);
    }
    payloadJson.add("records", records);
    return payloadJson.toString();
}
Also used : JsonArray(com.google.gson.JsonArray) Attribute(io.siddhi.query.api.definition.Attribute) JsonPrimitive(com.google.gson.JsonPrimitive) StreamEvent(io.siddhi.core.event.stream.StreamEvent) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) ConnectionUnavailableException(io.siddhi.core.exception.ConnectionUnavailableException)

Example 3 with ConnectionUnavailableException

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

the class AbstractRecordTable method update.

@Override
public void update(ComplexEventChunk<StateEvent> updatingEventChunk, CompiledCondition compiledCondition, CompiledUpdateSet compiledUpdateSet) {
    RecordStoreCompiledCondition recordStoreCompiledCondition = ((RecordStoreCompiledCondition) compiledCondition);
    RecordTableCompiledUpdateSet recordTableCompiledUpdateSet = (RecordTableCompiledUpdateSet) compiledUpdateSet;
    List<Map<String, Object>> updateConditionParameterMaps = new ArrayList<>();
    List<Map<String, Object>> updateSetParameterMaps = new ArrayList<>();
    updatingEventChunk.reset();
    long timestamp = 0L;
    while (updatingEventChunk.hasNext()) {
        StateEvent stateEvent = updatingEventChunk.next();
        Map<String, Object> variableMap = new HashMap<>();
        for (Map.Entry<String, ExpressionExecutor> entry : recordStoreCompiledCondition.variableExpressionExecutorMap.entrySet()) {
            variableMap.put(entry.getKey(), entry.getValue().execute(stateEvent));
        }
        updateConditionParameterMaps.add(variableMap);
        Map<String, Object> variableMapForUpdateSet = new HashMap<>();
        for (Map.Entry<String, ExpressionExecutor> entry : recordTableCompiledUpdateSet.getExpressionExecutorMap().entrySet()) {
            variableMapForUpdateSet.put(entry.getKey(), entry.getValue().execute(stateEvent));
        }
        updateSetParameterMaps.add(variableMapForUpdateSet);
        timestamp = stateEvent.getTimestamp();
    }
    try {
        if (recordTableHandler != null) {
            recordTableHandler.update(timestamp, recordStoreCompiledCondition.getCompiledCondition(), updateConditionParameterMaps, recordTableCompiledUpdateSet.getUpdateSetMap(), updateSetParameterMaps);
        } else {
            update(recordStoreCompiledCondition.getCompiledCondition(), updateConditionParameterMaps, recordTableCompiledUpdateSet.getUpdateSetMap(), updateSetParameterMaps);
        }
    } catch (ConnectionUnavailableException | DatabaseRuntimeException e) {
        onUpdateError(updatingEventChunk, compiledCondition, compiledUpdateSet, e);
    }
}
Also used : VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) HashMap(java.util.HashMap) DatabaseRuntimeException(io.siddhi.core.exception.DatabaseRuntimeException) ArrayList(java.util.ArrayList) StateEvent(io.siddhi.core.event.state.StateEvent) HashMap(java.util.HashMap) Map(java.util.Map) ConnectionUnavailableException(io.siddhi.core.exception.ConnectionUnavailableException)

Example 4 with ConnectionUnavailableException

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

the class AbstractRecordTable method delete.

@Override
public void delete(ComplexEventChunk<StateEvent> deletingEventChunk, CompiledCondition compiledCondition) {
    RecordStoreCompiledCondition recordStoreCompiledCondition = ((RecordStoreCompiledCondition) compiledCondition);
    List<Map<String, Object>> deleteConditionParameterMaps = new ArrayList<>();
    deletingEventChunk.reset();
    long timestamp = 0L;
    while (deletingEventChunk.hasNext()) {
        StateEvent stateEvent = deletingEventChunk.next();
        Map<String, Object> variableMap = new HashMap<>();
        for (Map.Entry<String, ExpressionExecutor> entry : recordStoreCompiledCondition.variableExpressionExecutorMap.entrySet()) {
            variableMap.put(entry.getKey(), entry.getValue().execute(stateEvent));
        }
        deleteConditionParameterMaps.add(variableMap);
        timestamp = stateEvent.getTimestamp();
    }
    try {
        if (recordTableHandler != null) {
            recordTableHandler.delete(timestamp, deleteConditionParameterMaps, recordStoreCompiledCondition.getCompiledCondition());
        } else {
            delete(deleteConditionParameterMaps, recordStoreCompiledCondition.getCompiledCondition());
        }
    } catch (ConnectionUnavailableException | DatabaseRuntimeException e) {
        onDeleteError(deletingEventChunk, compiledCondition, e);
    }
}
Also used : VariableExpressionExecutor(io.siddhi.core.executor.VariableExpressionExecutor) ExpressionExecutor(io.siddhi.core.executor.ExpressionExecutor) HashMap(java.util.HashMap) DatabaseRuntimeException(io.siddhi.core.exception.DatabaseRuntimeException) ArrayList(java.util.ArrayList) StateEvent(io.siddhi.core.event.state.StateEvent) HashMap(java.util.HashMap) Map(java.util.Map) ConnectionUnavailableException(io.siddhi.core.exception.ConnectionUnavailableException)

Example 5 with ConnectionUnavailableException

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

the class AbstractQueryableRecordTable method find.

@Override
public StreamEvent find(CompiledCondition compiledCondition, StateEvent matchingEvent) throws ConnectionUnavailableException {
    try {
        updateStoreTableSize();
    } catch (ConnectionUnavailableException e) {
        log.error(e);
    }
    // handle compile condition type conv
    RecordStoreCompiledCondition recordStoreCompiledCondition;
    CompiledConditionWithCache compiledConditionWithCache = null;
    findMatchingEvent = matchingEvent;
    if (cacheEnabled) {
        RecordStoreCompiledCondition compiledConditionTemp = (RecordStoreCompiledCondition) compiledCondition;
        compiledConditionWithCache = (CompiledConditionWithCache) compiledConditionTemp.getCompiledCondition();
        recordStoreCompiledCondition = new RecordStoreCompiledCondition(compiledConditionTemp.variableExpressionExecutorMap, compiledConditionWithCache.getStoreCompileCondition(), compiledConditionTemp.getSiddhiQueryContext());
    } else {
        recordStoreCompiledCondition = ((RecordStoreCompiledCondition) compiledCondition);
    }
    StreamEvent cacheResults;
    if (cacheEnabled) {
        readWriteLock.writeLock().lock();
        try {
            // when table is smaller than max cache send results from cache
            if (storeTableSize <= maxCacheSize) {
                if (log.isDebugEnabled()) {
                    log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": store table size is smaller than max cache. Sending results from cache");
                }
                cacheResults = cacheTable.find(compiledConditionWithCache.getCacheCompileCondition(), matchingEvent);
                return cacheResults;
            } else {
                if (log.isDebugEnabled()) {
                    log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": store table size is bigger than cache.");
                }
                if (compiledConditionWithCache.isRouteToCache()) {
                    if (log.isDebugEnabled()) {
                        log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": cache constraints satisfied. Checking cache");
                    }
                    cacheResults = cacheTable.find(compiledConditionWithCache.getCacheCompileCondition(), matchingEvent);
                    if (cacheResults != null) {
                        if (log.isDebugEnabled()) {
                            log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": cache hit. Sending results from cache");
                        }
                        return cacheResults;
                    }
                    // cache miss
                    if (log.isDebugEnabled()) {
                        log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": cache miss. Loading from store");
                    }
                    StreamEvent streamEvent = super.find(recordStoreCompiledCondition, matchingEvent);
                    if (streamEvent == null) {
                        if (log.isDebugEnabled()) {
                            log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": store also miss. sending null");
                        }
                        return null;
                    }
                    if (cacheTable.size() == maxCacheSize) {
                        ((CacheTable) cacheTable).deleteOneEntryUsingCachePolicy();
                    }
                    ((CacheTable) cacheTable).addStreamEventUptoMaxSize(streamEvent);
                    cacheResults = cacheTable.find(compiledConditionWithCache.getCacheCompileCondition(), matchingEvent);
                    if (log.isDebugEnabled()) {
                        log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": sending results from cache after loading from store");
                    }
                    return cacheResults;
                }
            }
        } finally {
            readWriteLock.writeLock().unlock();
        }
    }
    // when cache is not enabled or cache query conditions are not satisfied
    if (log.isDebugEnabled()) {
        log.debug(siddhiAppContext.getName() + "-" + recordStoreCompiledCondition.getSiddhiQueryContext().getName() + ": sending results from store");
    }
    return super.find(recordStoreCompiledCondition, matchingEvent);
}
Also used : OnDemandQueryParser.generateMatchingMetaInfoHolderForCacheTable(io.siddhi.core.util.parser.OnDemandQueryParser.generateMatchingMetaInfoHolderForCacheTable) CacheTable(io.siddhi.core.table.CacheTable) OnDemandQueryRuntimeUtil.executeSelectorAndReturnStreamEvent(io.siddhi.core.util.OnDemandQueryRuntimeUtil.executeSelectorAndReturnStreamEvent) StreamEvent(io.siddhi.core.event.stream.StreamEvent) MetaStreamEvent(io.siddhi.core.event.stream.MetaStreamEvent) ConnectionUnavailableException(io.siddhi.core.exception.ConnectionUnavailableException)

Aggregations

ConnectionUnavailableException (io.siddhi.core.exception.ConnectionUnavailableException)14 DatabaseRuntimeException (io.siddhi.core.exception.DatabaseRuntimeException)7 StateEvent (io.siddhi.core.event.state.StateEvent)5 StreamEvent (io.siddhi.core.event.stream.StreamEvent)5 ErroneousEvent (io.siddhi.core.util.error.handler.model.ErroneousEvent)4 ArrayList (java.util.ArrayList)4 ExpressionExecutor (io.siddhi.core.executor.ExpressionExecutor)3 VariableExpressionExecutor (io.siddhi.core.executor.VariableExpressionExecutor)3 ReplayableTableRecord (io.siddhi.core.util.error.handler.model.ReplayableTableRecord)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 JsonArray (com.google.gson.JsonArray)2 JsonObject (com.google.gson.JsonObject)2 JsonPrimitive (com.google.gson.JsonPrimitive)2 MetaStreamEvent (io.siddhi.core.event.stream.MetaStreamEvent)2 OnDemandQueryRuntimeUtil.executeSelectorAndReturnStreamEvent (io.siddhi.core.util.OnDemandQueryRuntimeUtil.executeSelectorAndReturnStreamEvent)2 Attribute (io.siddhi.query.api.definition.Attribute)2 SiddhiAppRuntime (io.siddhi.core.SiddhiAppRuntime)1 SiddhiManager (io.siddhi.core.SiddhiManager)1 Event (io.siddhi.core.event.Event)1