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();
}
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();
}
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);
}
}
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);
}
}
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);
}
Aggregations