Search in sources :

Example 36 with TriggerRouter

use of org.jumpmind.symmetric.model.TriggerRouter 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 37 with TriggerRouter

use of org.jumpmind.symmetric.model.TriggerRouter 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)

Example 38 with TriggerRouter

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

the class DataService method createData.

public Data createData(ISqlTransaction transaction, String catalogName, String schemaName, String tableName, String whereClause) {
    Data data = null;
    Set<TriggerRouter> triggerRouters = engine.getTriggerRouterService().getTriggerRouterForTableForCurrentNode(catalogName, schemaName, tableName, false);
    if (triggerRouters != null && triggerRouters.size() > 0) {
        data = createData(transaction, triggerRouters.iterator().next().getTrigger(), whereClause);
    }
    return data;
}
Also used : TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) Data(org.jumpmind.symmetric.model.Data) CsvData(org.jumpmind.symmetric.io.data.CsvData)

Example 39 with TriggerRouter

use of org.jumpmind.symmetric.model.TriggerRouter 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 40 with TriggerRouter

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

the class DataService method insertHeartbeatEvent.

/**
     * Because we can't add a trigger on the _node table, we are artificially
     * generating heartbeat events.
     * 
     * @param node
     */
public void insertHeartbeatEvent(Node node, boolean isReload) {
    ISqlTransaction transaction = null;
    try {
        transaction = sqlTemplate.startSqlTransaction();
        String tableName = TableConstants.getTableName(tablePrefix, TableConstants.SYM_NODE_HOST);
        List<NodeGroupLink> links = engine.getConfigurationService().getNodeGroupLinksFor(parameterService.getNodeGroupId(), false);
        for (NodeGroupLink nodeGroupLink : links) {
            if (nodeGroupLink.getDataEventAction() == NodeGroupLinkAction.P) {
                Set<TriggerRouter> triggerRouters = engine.getTriggerRouterService().getTriggerRouterForTableForCurrentNode(nodeGroupLink, null, null, tableName, false);
                if (triggerRouters != null && triggerRouters.size() > 0) {
                    Data data = createData(transaction, triggerRouters.iterator().next().getTrigger(), String.format(" t.node_id = '%s'", node.getNodeId()));
                    if (data != null) {
                        insertData(transaction, data);
                    } else {
                        log.warn("Not generating data/data events for table {} " + "because a trigger or trigger hist is not created yet.", tableName);
                    }
                } else {
                    log.warn("Not generating data/data events for table {} " + "because a trigger or trigger hist is not created yet.", tableName);
                }
            }
        }
        transaction.commit();
    } catch (Error ex) {
        if (transaction != null) {
            transaction.rollback();
        }
        throw ex;
    } catch (RuntimeException ex) {
        if (transaction != null) {
            transaction.rollback();
        }
        throw ex;
    } finally {
        close(transaction);
    }
}
Also used : ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) Data(org.jumpmind.symmetric.model.Data) CsvData(org.jumpmind.symmetric.io.data.CsvData) TransformTableNodeGroupLink(org.jumpmind.symmetric.service.impl.TransformService.TransformTableNodeGroupLink) NodeGroupLink(org.jumpmind.symmetric.model.NodeGroupLink)

Aggregations

TriggerRouter (org.jumpmind.symmetric.model.TriggerRouter)64 ArrayList (java.util.ArrayList)22 Trigger (org.jumpmind.symmetric.model.Trigger)20 Router (org.jumpmind.symmetric.model.Router)18 NodeChannel (org.jumpmind.symmetric.model.NodeChannel)14 TriggerHistory (org.jumpmind.symmetric.model.TriggerHistory)13 ITriggerRouterService (org.jumpmind.symmetric.service.ITriggerRouterService)12 Test (org.junit.Test)12 List (java.util.List)10 Node (org.jumpmind.symmetric.model.Node)8 NodeGroupLink (org.jumpmind.symmetric.model.NodeGroupLink)7 OutgoingBatches (org.jumpmind.symmetric.model.OutgoingBatches)7 Table (org.jumpmind.db.model.Table)6 ISqlTransaction (org.jumpmind.db.sql.ISqlTransaction)6 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 Data (org.jumpmind.symmetric.model.Data)5 TableReloadRequest (org.jumpmind.symmetric.model.TableReloadRequest)5 ConfigurationChangedDataRouter (org.jumpmind.symmetric.route.ConfigurationChangedDataRouter)4 FileSyncDataRouter (org.jumpmind.symmetric.route.FileSyncDataRouter)4