Search in sources :

Example 11 with ConnectionUnavailableException

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

the class AbstractRecordTable method updateOrAdd.

@Override
public void updateOrAdd(ComplexEventChunk<StateEvent> updateOrAddingEventChunk, CompiledCondition compiledCondition, CompiledUpdateSet compiledUpdateSet, AddingStreamEventExtractor addingStreamEventExtractor) {
    RecordStoreCompiledCondition recordStoreCompiledCondition = ((RecordStoreCompiledCondition) compiledCondition);
    RecordTableCompiledUpdateSet recordTableCompiledUpdateSet = (RecordTableCompiledUpdateSet) compiledUpdateSet;
    List<Map<String, Object>> updateConditionParameterMaps = new ArrayList<>();
    List<Map<String, Object>> updateSetParameterMaps = new ArrayList<>();
    List<Object[]> addingRecords = new ArrayList<>();
    updateOrAddingEventChunk.reset();
    long timestamp = 0L;
    while (updateOrAddingEventChunk.hasNext()) {
        StateEvent stateEvent = updateOrAddingEventChunk.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);
        addingRecords.add(stateEvent.getStreamEvent(0).getOutputData());
        timestamp = stateEvent.getTimestamp();
    }
    try {
        if (recordTableHandler != null) {
            recordTableHandler.updateOrAdd(timestamp, recordStoreCompiledCondition.getCompiledCondition(), updateConditionParameterMaps, recordTableCompiledUpdateSet.getUpdateSetMap(), updateSetParameterMaps, addingRecords);
        } else {
            updateOrAdd(recordStoreCompiledCondition.getCompiledCondition(), updateConditionParameterMaps, recordTableCompiledUpdateSet.getUpdateSetMap(), updateSetParameterMaps, addingRecords);
        }
    } catch (ConnectionUnavailableException | DatabaseRuntimeException e) {
        onUpdateOrAddError(updateOrAddingEventChunk, compiledCondition, compiledUpdateSet, addingStreamEventExtractor, 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 12 with ConnectionUnavailableException

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

the class ErrorHandlerUtils method constructErrorRecordString.

public static String constructErrorRecordString(ComplexEventChunk<StateEvent> eventChunk, boolean isObjectColumnPresent, TableDefinition tableDefinition, Exception e) {
    JsonObject payloadJson = new JsonObject();
    if (isObjectColumnPresent || e instanceof ConnectionUnavailableException) {
        payloadJson.addProperty("isEditable", false);
    } else {
        payloadJson.addProperty("isEditable", true);
    }
    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()) {
        StateEvent stateEvent = eventChunk.next();
        for (StreamEvent streamEvent : stateEvent.getStreamEvents()) {
            if (streamEvent != null) {
                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) StateEvent(io.siddhi.core.event.state.StateEvent) JsonObject(com.google.gson.JsonObject) ConnectionUnavailableException(io.siddhi.core.exception.ConnectionUnavailableException)

Example 13 with ConnectionUnavailableException

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

the class Sink method onError.

public void onError(Object payload, DynamicOptions dynamicOptions, Exception e) {
    OnErrorAction errorAction = onErrorAction;
    if (e instanceof ConnectionUnavailableException) {
        setConnected(false);
        if (connectionCallback != null) {
            connectionCallback.connectionFailed();
        }
    } else if (errorAction == OnErrorAction.WAIT) {
        LOG.error("Error on '" + siddhiAppContext.getName() + "'. Dropping event at Sink '" + type + "' at '" + streamDefinition.getId() + "' as on.error='wait' does not handle " + "'" + e.getClass().getName() + "' error: '" + e.getMessage() + "', events dropped '" + payload + "'", e);
        return;
    }
    try {
        switch(errorAction) {
            case STREAM:
                connectWithRetry();
                streamJunction.handleError(dynamicOptions.getEvent(), e);
                break;
            case WAIT:
                LOG.error(StringUtil.removeCRLFCharacters(ExceptionUtil.getMessageWithContext(e, siddhiAppContext) + ", error while connecting Sink '" + type + "' at '" + streamDefinition.getId() + "', will retry every '5 sec'."), e);
                int count = 0;
                while (!isConnected.get()) {
                    if (isShutdown.get()) {
                        isTryingToConnect.set(false);
                        return;
                    }
                    retryWait(5000);
                    count++;
                    if (!isConnected.get() && !isTryingToConnect.getAndSet(true)) {
                        while (!isConnected.get()) {
                            if (isShutdown.get()) {
                                isTryingToConnect.set(false);
                                return;
                            }
                            S state = stateHolder.getState();
                            try {
                                connectAndPublish(payload, dynamicOptions, state);
                                isTryingToConnect.set(false);
                                return;
                            } catch (ConnectionUnavailableException ignore) {
                            } finally {
                                stateHolder.returnState(state);
                            }
                            if (count % 12 == 0) {
                                LOG.warn(StringUtil.removeCRLFCharacters(ExceptionUtil.getMessageWithContext(e, siddhiAppContext) + ", still waiting to connect Sink '" + type + "' at '" + streamDefinition.getId() + "' retrying every '5 sec'."), e);
                            }
                            retryWait(5000);
                            count++;
                        }
                    }
                    if (count % 12 == 0) {
                        LOG.warn(StringUtil.removeCRLFCharacters(ExceptionUtil.getMessageWithContext(e, siddhiAppContext) + ", still waiting to connect Sink '" + type + "' at '" + streamDefinition.getId() + "."), e);
                    }
                }
                S state = stateHolder.getState();
                try {
                    publish(payload, dynamicOptions, state);
                } catch (ConnectionUnavailableException ignore) {
                    onError(payload, dynamicOptions, e);
                } finally {
                    stateHolder.returnState(state);
                }
                break;
            case STORE:
                connectWithRetry();
                ErroneousEvent erroneousEvent = new ErroneousEvent(dynamicOptions.getEvent(), e, e.getMessage());
                erroneousEvent.setOriginalPayload(payload);
                ErrorStoreHelper.storeErroneousEvent(siddhiAppContext.getSiddhiContext().getErrorStore(), ErrorOccurrence.STORE_ON_SINK_ERROR, siddhiAppContext.getName(), erroneousEvent, streamDefinition.getId());
                break;
            case LOG:
            default:
                connectWithRetry();
                LOG.error("Error on '" + siddhiAppContext.getName() + "'. Dropping event at Sink '" + type + "' at '" + streamDefinition.getId() + "' as its still trying to reconnect!, " + "events dropped '" + payload + "'");
                if (LOG.isDebugEnabled()) {
                    LOG.debug(e);
                }
                break;
        }
    } catch (Throwable t) {
        LOG.error("Error on '" + siddhiAppContext.getName() + "'. Dropping event at Sink '" + type + "' at '" + streamDefinition.getId() + "' as there is an issue when " + "handling the error: '" + t.getMessage() + "', events dropped '" + payload + "'", e);
    }
}
Also used : ConnectionUnavailableException(io.siddhi.core.exception.ConnectionUnavailableException) ErroneousEvent(io.siddhi.core.util.error.handler.model.ErroneousEvent)

Example 14 with ConnectionUnavailableException

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

the class Table method onDeleteError.

protected void onDeleteError(ComplexEventChunk<StateEvent> deletingEventChunk, CompiledCondition compiledCondition, Exception e) {
    OnErrorAction errorAction = onErrorAction;
    if (e instanceof ConnectionUnavailableException) {
        isConnected.set(false);
        if (errorAction == OnErrorAction.STORE) {
            deletingEventChunk.reset();
            ErroneousEvent erroneousEvent = new ErroneousEvent(new ReplayableTableRecord(deletingEventChunk, compiledCondition), e, e.getMessage());
            erroneousEvent.setOriginalPayload(ErrorHandlerUtils.constructErrorRecordString(deletingEventChunk, isObjectColumnPresent, tableDefinition, e));
            ErrorStoreHelper.storeErroneousEvent(siddhiAppContext.getSiddhiContext().getErrorStore(), ErrorOccurrence.STORE_ON_TABLE_DELETE, siddhiAppContext.getName(), erroneousEvent, tableDefinition.getId());
            LOG.error("Error on '" + siddhiAppContext.getName() + "' while performing delete for events  at '" + tableDefinition.getId() + "'. Events saved '" + deletingEventChunk.toString() + "'");
            if (LOG.isDebugEnabled()) {
                LOG.debug(e);
            }
            if (!isTryingToConnect.get()) {
                connectWithRetry();
            }
        } else {
            if (isTryingToConnect.get()) {
                LOG.warn("Error on '" + siddhiAppContext.getName() + "' while performing delete for events '" + deletingEventChunk + "', operation busy waiting at Table '" + tableDefinition.getId() + "' as its trying to reconnect!");
                waitWhileConnect();
                LOG.info("SiddhiApp '" + siddhiAppContext.getName() + "' table '" + tableDefinition.getId() + "' has become available for delete operation for events '" + deletingEventChunk + "'");
                delete(deletingEventChunk, compiledCondition);
            } else {
                connectWithRetry();
                delete(deletingEventChunk, compiledCondition);
            }
        }
    } else if (e instanceof DatabaseRuntimeException) {
        if (errorAction == OnErrorAction.STORE) {
            deletingEventChunk.reset();
            ReplayableTableRecord record = new ReplayableTableRecord(deletingEventChunk, compiledCondition);
            record.setFromConnectionUnavailableException(false);
            ErroneousEvent erroneousEvent = new ErroneousEvent(record, e, e.getMessage());
            erroneousEvent.setOriginalPayload(ErrorHandlerUtils.constructErrorRecordString(deletingEventChunk, isObjectColumnPresent, tableDefinition, e));
            ErrorStoreHelper.storeErroneousEvent(siddhiAppContext.getSiddhiContext().getErrorStore(), ErrorOccurrence.STORE_ON_TABLE_DELETE, siddhiAppContext.getName(), erroneousEvent, tableDefinition.getId());
            LOG.error("Error on '" + siddhiAppContext.getName() + "' while performing delete for events  at '" + tableDefinition.getId() + "'. Events saved '" + deletingEventChunk.toString() + "'");
            if (LOG.isDebugEnabled()) {
                LOG.debug(e);
            }
        }
    }
}
Also used : DatabaseRuntimeException(io.siddhi.core.exception.DatabaseRuntimeException) ReplayableTableRecord(io.siddhi.core.util.error.handler.model.ReplayableTableRecord) ConnectionUnavailableException(io.siddhi.core.exception.ConnectionUnavailableException) ErroneousEvent(io.siddhi.core.util.error.handler.model.ErroneousEvent)

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