Search in sources :

Example 31 with TriggerHistory

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

the class DataService method sendScript.

public void sendScript(String nodeId, String script, boolean isLoad) {
    Node targetNode = engine.getNodeService().findNode(nodeId);
    TriggerHistory history = engine.getTriggerRouterService().findTriggerHistoryForGenericSync();
    Data data = new Data(history.getSourceTableName(), DataEventType.BSH, CsvUtils.escapeCsvData(script), null, history, Constants.CHANNEL_CONFIG, null, null);
    data.setNodeList(nodeId);
    if (!isLoad) {
        insertData(data);
    } else {
        insertDataAndDataEventAndOutgoingBatch(data, targetNode.getNodeId(), Constants.UNKNOWN_ROUTER_ID, isLoad, -1, null);
    }
}
Also used : TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) Node(org.jumpmind.symmetric.model.Node) Data(org.jumpmind.symmetric.model.Data) CsvData(org.jumpmind.symmetric.io.data.CsvData)

Example 32 with TriggerHistory

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

the class DataService method insertDataAndDataEventAndOutgoingBatch.

/**
     * @return The inserted batch id
     */
public long insertDataAndDataEventAndOutgoingBatch(ISqlTransaction transaction, Data data, String nodeId, String routerId, boolean isLoad, long loadId, String createBy, Status status, String overrideChannelId) {
    long dataId = insertData(transaction, data);
    String channelId = null;
    if (isLoad) {
        if (overrideChannelId != null) {
            channelId = overrideChannelId;
        } else {
            TriggerHistory history = data.getTriggerHistory();
            if (history != null && channelId == null) {
                Trigger trigger = engine.getTriggerRouterService().getTriggerById(history.getTriggerId());
                channelId = getReloadChannelIdForTrigger(trigger, engine.getConfigurationService().getChannels(false));
            }
        }
    } else {
        channelId = data.getChannelId();
    }
    return insertDataEventAndOutgoingBatch(transaction, dataId, channelId, nodeId, data.getDataEventType(), routerId, isLoad, loadId, createBy, status, data.getTableName());
}
Also used : Trigger(org.jumpmind.symmetric.model.Trigger) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory)

Example 33 with TriggerHistory

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

the class DataService method reloadMissingForeignKeyRows.

public void reloadMissingForeignKeyRows(String nodeId, long dataId) {
    try {
        Data data = findData(dataId);
        log.debug("reloadMissingForeignKeyRows for nodeId '{}' dataId '{}' table '{}'", nodeId, dataId, data.getTableName());
        TriggerHistory hist = data.getTriggerHistory();
        Table table = platform.getTableFromCache(hist.getSourceCatalogName(), hist.getSourceSchemaName(), hist.getSourceTableName(), false);
        Map<String, String> dataMap = data.toColumnNameValuePairs(table.getColumnNames(), CsvData.ROW_DATA);
        List<TableRow> tableRows = new ArrayList<TableRow>();
        Row row = new Row(dataMap.size());
        row.putAll(dataMap);
        tableRows.add(new TableRow(table, row, null, null, null));
        List<TableRow> foreignTableRows;
        try {
            foreignTableRows = getForeignTableRows(tableRows, new HashSet<TableRow>());
        } catch (CloneNotSupportedException e) {
            throw new RuntimeException(e);
        }
        if (foreignTableRows.isEmpty()) {
            log.info("Could not determine foreign table rows to fix foreign key violation for " + "nodeId '{}' dataId '{}' table '{}'", nodeId, dataId, data.getTableName());
        }
        Collections.reverse(foreignTableRows);
        Set<TableRow> visited = new HashSet<TableRow>();
        for (TableRow foreignTableRow : foreignTableRows) {
            if (visited.add(foreignTableRow)) {
                Table foreignTable = foreignTableRow.getTable();
                String catalog = foreignTable.getCatalog();
                String schema = foreignTable.getSchema();
                if (StringUtils.equals(platform.getDefaultCatalog(), catalog)) {
                    catalog = null;
                }
                if (StringUtils.equals(platform.getDefaultSchema(), schema)) {
                    schema = null;
                }
                log.info("Issuing foreign key correction reload " + "nodeId {} catalog '{}' schema '{}' foreign table name '{}' fk name '{}' where sql '{}' " + "to correct dataId '{}' table '{}' for column '{}'", nodeId, catalog, schema, foreignTable.getName(), foreignTableRow.getFkName(), foreignTableRow.getWhereSql(), dataId, data.getTableName(), foreignTableRow.getReferenceColumnName());
                reloadTable(nodeId, catalog, schema, foreignTable.getName(), foreignTableRow.getWhereSql());
            }
        }
    } catch (Exception e) {
        log.error("Unknown exception while processing foreign key for node id: " + nodeId + " data id " + dataId, e);
    }
}
Also used : Table(org.jumpmind.db.model.Table) ArrayList(java.util.ArrayList) Data(org.jumpmind.symmetric.model.Data) CsvData(org.jumpmind.symmetric.io.data.CsvData) UniqueKeyException(org.jumpmind.db.sql.UniqueKeyException) NotImplementedException(org.apache.commons.lang.NotImplementedException) SymmetricException(org.jumpmind.symmetric.SymmetricException) IoException(org.jumpmind.exception.IoException) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) Row(org.jumpmind.db.sql.Row) HashSet(java.util.HashSet)

Example 34 with TriggerHistory

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

the class DataService method sendSchema.

public boolean sendSchema(String nodeId, String catalogName, String schemaName, String tableName, boolean isLoad) {
    Node sourceNode = engine.getNodeService().findIdentity();
    Node targetNode = engine.getNodeService().findNode(nodeId);
    if (targetNode == null) {
        log.error("Could not send schema to the node {}.  The target node does not exist", nodeId);
        return false;
    }
    ITriggerRouterService triggerRouterService = engine.getTriggerRouterService();
    List<TriggerHistory> triggerHistories = triggerRouterService.findTriggerHistories(catalogName, schemaName, tableName);
    Map<Integer, List<TriggerRouter>> triggerRoutersByHistoryId = triggerRouterService.fillTriggerRoutersByHistIdAndSortHist(sourceNode.getNodeGroupId(), targetNode.getNodeGroupId(), triggerHistories);
    int eventCount = 0;
    for (TriggerHistory triggerHistory : triggerHistories) {
        List<TriggerRouter> triggerRouters = triggerRoutersByHistoryId.get(triggerHistory.getTriggerHistoryId());
        for (TriggerRouter triggerRouter : triggerRouters) {
            eventCount++;
            insertCreateEvent(targetNode, triggerHistory, triggerRouter.getRouter().getRouterId(), false, -1, null);
        }
    }
    if (eventCount > 0) {
        return true;
    } else {
        return false;
    }
}
Also used : ITriggerRouterService(org.jumpmind.symmetric.service.ITriggerRouterService) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) Node(org.jumpmind.symmetric.model.Node) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) List(java.util.List) ArrayList(java.util.ArrayList)

Example 35 with TriggerHistory

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

the class DataService method insertDeleteBatchesForReload.

private void insertDeleteBatchesForReload(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 deleteEventsSent = 0;
        for (ListIterator<TriggerHistory> triggerHistoryIterator = triggerHistories.listIterator(triggerHistories.size()); triggerHistoryIterator.hasPrevious(); ) {
            TriggerHistory triggerHistory = triggerHistoryIterator.previous();
            List<TriggerRouter> triggerRouters = triggerRoutersByHistoryId.get(triggerHistory.getTriggerHistoryId());
            TableReloadRequest currentRequest = reloadRequests.get(ParameterConstants.ALL + ParameterConstants.ALL);
            boolean fullLoad = currentRequest == null ? false : true;
            for (ListIterator<TriggerRouter> iterator = triggerRouters.listIterator(triggerRouters.size()); iterator.hasPrevious(); ) {
                TriggerRouter triggerRouter = iterator.previous();
                if (!fullLoad) {
                    currentRequest = reloadRequests.get(triggerRouter.getTriggerId() + triggerRouter.getRouterId());
                }
                //Check the delete flag on the specific table reload request
                if (currentRequest != null && currentRequest.isDeleteFirst() && engine.getGroupletService().isTargetEnabled(triggerRouter, targetNode)) {
                    insertPurgeEvent(transaction, targetNode, triggerRouter, triggerHistory, true, currentRequest.getBeforeCustomSql(), loadId, createBy);
                    deleteEventsSent++;
                    if (!transactional) {
                        transaction.commit();
                    }
                }
            }
        }
        if (deleteEventsSent > 0) {
            log.info("Before sending load {} to target node {} delete data events were sent for {} tables", new Object[] { loadId, targetNode, deleteEventsSent });
        }
    } else {
        if (parameterService.is(ParameterConstants.INITIAL_LOAD_DELETE_BEFORE_RELOAD)) {
            for (ListIterator<TriggerHistory> triggerHistoryIterator = triggerHistories.listIterator(triggerHistories.size()); triggerHistoryIterator.hasPrevious(); ) {
                TriggerHistory triggerHistory = triggerHistoryIterator.previous();
                List<TriggerRouter> triggerRouters = triggerRoutersByHistoryId.get(triggerHistory.getTriggerHistoryId());
                for (ListIterator<TriggerRouter> iterator = triggerRouters.listIterator(triggerRouters.size()); iterator.hasPrevious(); ) {
                    TriggerRouter triggerRouter = iterator.previous();
                    if (triggerRouter.getInitialLoadOrder() >= 0 && engine.getGroupletService().isTargetEnabled(triggerRouter, targetNode) && (!StringUtils.isBlank(parameterService.getString(ParameterConstants.INITIAL_LOAD_DELETE_FIRST_SQL)) || !StringUtils.isEmpty(triggerRouter.getInitialLoadDeleteStmt()))) {
                        insertPurgeEvent(transaction, targetNode, triggerRouter, triggerHistory, true, null, loadId, createBy);
                        if (!transactional) {
                            transaction.commit();
                        }
                    }
                }
            }
        }
    }
}
Also used : TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) 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