Search in sources :

Example 26 with TriggerHistory

use of org.jumpmind.symmetric.model.TriggerHistory in project symmetric-ds by JumpMind.

the class AuditTableDataRouter method routeToNodes.

public Set<String> routeToNodes(SimpleRouterContext context, DataMetaData dataMetaData, Set<Node> nodes, boolean initialLoad, boolean initialLoadSelectUsed, TriggerRouter triggerRouter) {
    DataEventType eventType = dataMetaData.getData().getDataEventType();
    if (eventType == DataEventType.INSERT || eventType == DataEventType.UPDATE || eventType == DataEventType.DELETE) {
        IParameterService parameterService = engine.getParameterService();
        IDatabasePlatform platform = engine.getDatabasePlatform();
        TriggerHistory triggerHistory = dataMetaData.getTriggerHistory();
        Table table = dataMetaData.getTable().copyAndFilterColumns(triggerHistory.getParsedColumnNames(), triggerHistory.getParsedPkColumnNames(), true);
        String tableName = table.getFullyQualifiedTableName();
        Table auditTable = auditTables.get(tableName);
        if (auditTable == null) {
            auditTable = toAuditTable(table);
            if (parameterService.is(ParameterConstants.AUTO_CONFIGURE_DATABASE)) {
                platform.alterTables(true, auditTable);
            }
            auditTable = platform.getTableFromCache(auditTable.getCatalog(), auditTable.getSchema(), auditTable.getName(), false);
            auditTables.put(tableName, auditTable);
        }
        DatabaseInfo dbInfo = platform.getDatabaseInfo();
        String auditTableName = auditTable.getQualifiedTableName(dbInfo.getDelimiterToken(), dbInfo.getCatalogSeparator(), dbInfo.getSchemaSeparator());
        ISqlTemplate template = platform.getSqlTemplate();
        Map<String, Object> values = null;
        if (eventType != DataEventType.DELETE) {
            values = new HashMap<String, Object>(getNewDataAsObject(null, dataMetaData, engine.getSymmetricDialect(), false));
        } else {
            values = new HashMap<String, Object>(getOldDataAsObject(null, dataMetaData, engine.getSymmetricDialect(), false));
        }
        Long sequence = (Long) context.get(auditTableName);
        if (sequence == null) {
            sequence = 1l + template.queryForLong(String.format("select max(%s) from %s", auditTable.getColumnWithName(COLUMN_AUDIT_ID).getName(), auditTableName));
        } else {
            sequence = 1l + sequence;
        }
        context.put(auditTable.getName(), sequence);
        values.put(auditTable.getColumnWithName(COLUMN_AUDIT_ID).getName(), sequence);
        values.put(auditTable.getColumnWithName(COLUMN_AUDIT_TIME).getName(), new Date());
        values.put(auditTable.getColumnWithName(COLUMN_AUDIT_EVENT).getName(), eventType.getCode());
        DmlStatement statement = platform.createDmlStatement(DmlType.INSERT, auditTable, null);
        int[] types = statement.getTypes();
        Object[] args = statement.getValueArray(values);
        String sql = statement.getSql();
        template.update(sql, args, types);
    }
    return null;
}
Also used : IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) Table(org.jumpmind.db.model.Table) DatabaseInfo(org.jumpmind.db.platform.DatabaseInfo) DataEventType(org.jumpmind.symmetric.io.data.DataEventType) IParameterService(org.jumpmind.symmetric.service.IParameterService) Date(java.util.Date) ISqlTemplate(org.jumpmind.db.sql.ISqlTemplate) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) DmlStatement(org.jumpmind.db.sql.DmlStatement)

Example 27 with TriggerHistory

use of org.jumpmind.symmetric.model.TriggerHistory in project symmetric-ds by JumpMind.

the class DataService method insertSqlEvent.

public void insertSqlEvent(Node targetNode, String sql, boolean isLoad, long loadId, String createBy) {
    TriggerHistory history = engine.getTriggerRouterService().findTriggerHistoryForGenericSync();
    Trigger trigger = engine.getTriggerRouterService().getTriggerById(history.getTriggerId(), false);
    String reloadChannelId = getReloadChannelIdForTrigger(trigger, engine.getConfigurationService().getChannels(false));
    Data data = new Data(history.getSourceTableName(), DataEventType.SQL, CsvUtils.escapeCsvData(sql), null, history, isLoad ? reloadChannelId : Constants.CHANNEL_CONFIG, null, null);
    if (isLoad) {
        insertDataAndDataEventAndOutgoingBatch(data, targetNode.getNodeId(), Constants.UNKNOWN_ROUTER_ID, isLoad, loadId, createBy);
    } else {
        data.setNodeList(targetNode.getNodeId());
        insertData(data);
    }
}
Also used : Trigger(org.jumpmind.symmetric.model.Trigger) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) Data(org.jumpmind.symmetric.model.Data) CsvData(org.jumpmind.symmetric.io.data.CsvData)

Example 28 with TriggerHistory

use of org.jumpmind.symmetric.model.TriggerHistory in project symmetric-ds by JumpMind.

the class DataService method insertLoadBatchesForReload.

private void insertLoadBatchesForReload(Node targetNode, long loadId, String createBy, List<TriggerHistory> triggerHistories, Map<Integer, List<TriggerRouter>> triggerRoutersByHistoryId, boolean transactional, ISqlTransaction transaction, Map<String, TableReloadRequest> reloadRequests, ProcessInfo processInfo) {
    Map<String, Channel> channels = engine.getConfigurationService().getChannels(false);
    for (TriggerHistory triggerHistory : triggerHistories) {
        List<TriggerRouter> triggerRouters = triggerRoutersByHistoryId.get(triggerHistory.getTriggerHistoryId());
        processInfo.incrementCurrentDataCount();
        for (TriggerRouter triggerRouter : triggerRouters) {
            if (triggerRouter.getInitialLoadOrder() >= 0 && engine.getGroupletService().isTargetEnabled(triggerRouter, targetNode)) {
                String selectSql = null;
                if (reloadRequests != null) {
                    TableReloadRequest reloadRequest = reloadRequests.get(triggerRouter.getTriggerId() + triggerRouter.getRouterId());
                    selectSql = reloadRequest != null ? reloadRequest.getReloadSelect() : null;
                }
                if (StringUtils.isBlank(selectSql)) {
                    selectSql = StringUtils.isBlank(triggerRouter.getInitialLoadSelect()) ? Constants.ALWAYS_TRUE_CONDITION : triggerRouter.getInitialLoadSelect();
                }
                if (parameterService.is(ParameterConstants.INITIAL_LOAD_USE_EXTRACT_JOB)) {
                    Trigger trigger = triggerRouter.getTrigger();
                    String reloadChannel = getReloadChannelIdForTrigger(trigger, channels);
                    Channel channel = channels.get(reloadChannel);
                    Table table = platform.getTableFromCache(triggerHistory.getSourceCatalogName(), triggerHistory.getSourceSchemaName(), triggerHistory.getSourceTableName(), false);
                    processInfo.setCurrentTableName(table.getName());
                    int numberOfBatches = getNumberOfReloadBatches(table, triggerRouter, channel, targetNode, selectSql);
                    long startBatchId = -1;
                    long endBatchId = -1;
                    for (int i = 0; i < numberOfBatches; i++) {
                        // needs to grab the start and end batch id
                        endBatchId = insertReloadEvent(transaction, targetNode, triggerRouter, triggerHistory, selectSql, true, loadId, createBy, Status.RQ);
                        if (startBatchId == -1) {
                            startBatchId = endBatchId;
                        }
                    }
                    engine.getDataExtractorService().requestExtractRequest(transaction, targetNode.getNodeId(), channel.getQueue(), triggerRouter, startBatchId, endBatchId);
                } else {
                    insertReloadEvent(transaction, targetNode, triggerRouter, triggerHistory, selectSql, true, loadId, createBy, Status.NE);
                }
                if (!transactional) {
                    transaction.commit();
                }
            }
        }
    }
}
Also used : Trigger(org.jumpmind.symmetric.model.Trigger) Table(org.jumpmind.db.model.Table) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) Channel(org.jumpmind.symmetric.model.Channel) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) TableReloadRequest(org.jumpmind.symmetric.model.TableReloadRequest)

Example 29 with TriggerHistory

use of org.jumpmind.symmetric.model.TriggerHistory in project symmetric-ds by JumpMind.

the class DataService method insertSQLBatchesForReload.

private void insertSQLBatchesForReload(Node targetNode, long loadId, String createBy, List<TriggerHistory> triggerHistories, Map<Integer, List<TriggerRouter>> triggerRoutersByHistoryId, boolean transactional, ISqlTransaction transaction, Map<String, TableReloadRequest> reloadRequests) {
    if (reloadRequests != null && reloadRequests.size() > 0) {
        int sqlEventsSent = 0;
        for (TriggerHistory triggerHistory : triggerHistories) {
            List<TriggerRouter> triggerRouters = triggerRoutersByHistoryId.get(triggerHistory.getTriggerHistoryId());
            TableReloadRequest currentRequest = reloadRequests.get(ParameterConstants.ALL + ParameterConstants.ALL);
            boolean fullLoad = currentRequest == null ? false : true;
            for (TriggerRouter triggerRouter : triggerRouters) {
                if (!fullLoad) {
                    currentRequest = reloadRequests.get(triggerRouter.getTriggerId() + triggerRouter.getRouterId());
                }
                //Check the before custom sql is present on the specific table reload request
                if (currentRequest != null && currentRequest.getBeforeCustomSql() != null && currentRequest.getBeforeCustomSql().length() > 0 && engine.getGroupletService().isTargetEnabled(triggerRouter, targetNode)) {
                    String tableName = triggerRouter.qualifiedTargetTableName(triggerHistory);
                    String formattedBeforeSql = String.format(currentRequest.getBeforeCustomSql(), tableName) + ";";
                    insertSqlEvent(transaction, triggerHistory, triggerRouter.getTrigger().getChannelId(), targetNode, formattedBeforeSql, true, loadId, createBy);
                    sqlEventsSent++;
                    if (!transactional) {
                        transaction.commit();
                    }
                }
            }
        }
        if (sqlEventsSent > 0) {
            log.info("Before sending load {} to target node {} SQL data events were sent for {} tables", new Object[] { loadId, targetNode, sqlEventsSent });
        }
    }
}
Also used : TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) TableReloadRequest(org.jumpmind.symmetric.model.TableReloadRequest)

Example 30 with TriggerHistory

use of org.jumpmind.symmetric.model.TriggerHistory in project symmetric-ds by JumpMind.

the class DataService method insertSqlEventsPriorToReload.

private void insertSqlEventsPriorToReload(Node targetNode, String nodeIdRecord, long loadId, String createBy, boolean transactional, ISqlTransaction transaction, boolean reverse, List<TriggerHistory> triggerHistories, Map<Integer, List<TriggerRouter>> triggerRoutersByHistoryId, Map<String, TableReloadRequest> reloadRequests, boolean isFullLoad, String channelId) {
    if (!Constants.DEPLOYMENT_TYPE_REST.equals(targetNode.getDeploymentType())) {
        /*
                 * Insert node security so the client doing the initial load knows
                 * that an initial load is currently happening
                 */
        insertNodeSecurityUpdate(transaction, nodeIdRecord, targetNode.getNodeId(), true, loadId, createBy, channelId);
        if (isFullLoad) {
            /*
                     * Mark incoming batches as OK at the target node because we marked
                     * outgoing batches as OK at the source
                     */
            insertSqlEvent(transaction, targetNode, String.format("update %s_incoming_batch set status='OK', error_flag=0 where node_id='%s' and status != 'OK'", tablePrefix, engine.getNodeService().findIdentityNodeId()), true, loadId, createBy);
        }
    }
    if (!isFullLoad && reloadRequests != null && reloadRequests.size() > 0) {
        String beforeSql = "";
        int beforeSqlSent = 0;
        for (TriggerHistory triggerHistory : triggerHistories) {
            List<TriggerRouter> triggerRouters = triggerRoutersByHistoryId.get(triggerHistory.getTriggerHistoryId());
            for (TriggerRouter triggerRouter : triggerRouters) {
                String key = triggerRouter.getTriggerId() + triggerRouter.getRouterId();
                TableReloadRequest currentRequest = reloadRequests.get(key);
                if (currentRequest == null) {
                    throw new SymmetricException("Could not locate table reload request for key '" + key + "'. Available requests are: " + reloadRequests.keySet());
                }
                beforeSql = currentRequest.getBeforeCustomSql();
                if (isNotBlank(beforeSql)) {
                    String tableName = triggerRouter.qualifiedTargetTableName(triggerHistory);
                    String formattedBeforeSql = String.format(beforeSql, tableName) + ";";
                    insertSqlEvent(transaction, targetNode, formattedBeforeSql, true, loadId, createBy);
                    beforeSqlSent++;
                }
            }
        }
        if (beforeSqlSent > 0) {
            log.info("Before sending load {} to target node {} the before sql [{}] was sent for {} tables", new Object[] { loadId, targetNode, beforeSql, beforeSqlSent });
        }
    } else {
        String beforeSql = parameterService.getString(reverse ? ParameterConstants.INITIAL_LOAD_REVERSE_BEFORE_SQL : ParameterConstants.INITIAL_LOAD_BEFORE_SQL);
        if (isNotBlank(beforeSql)) {
            insertSqlEvent(transaction, targetNode, beforeSql, true, loadId, createBy);
        }
    }
}
Also used : TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) SymmetricException(org.jumpmind.symmetric.SymmetricException) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) TableReloadRequest(org.jumpmind.symmetric.model.TableReloadRequest)

Aggregations

TriggerHistory (org.jumpmind.symmetric.model.TriggerHistory)50 Table (org.jumpmind.db.model.Table)19 Data (org.jumpmind.symmetric.model.Data)19 Node (org.jumpmind.symmetric.model.Node)17 DataMetaData (org.jumpmind.symmetric.model.DataMetaData)14 Trigger (org.jumpmind.symmetric.model.Trigger)14 TriggerRouter (org.jumpmind.symmetric.model.TriggerRouter)13 HashSet (java.util.HashSet)12 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)10 Router (org.jumpmind.symmetric.model.Router)10 NodeChannel (org.jumpmind.symmetric.model.NodeChannel)9 ITriggerRouterService (org.jumpmind.symmetric.service.ITriggerRouterService)8 List (java.util.List)6 CsvData (org.jumpmind.symmetric.io.data.CsvData)6 SymmetricException (org.jumpmind.symmetric.SymmetricException)5 TableReloadRequest (org.jumpmind.symmetric.model.TableReloadRequest)5 Date (java.util.Date)4 ISqlTransaction (org.jumpmind.db.sql.ISqlTransaction)4 HashMap (java.util.HashMap)3