Search in sources :

Example 26 with Trigger

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

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

the class DataService method sendSQL.

public String sendSQL(String nodeId, String catalogName, String schemaName, String tableName, String sql) {
    Node sourceNode = engine.getNodeService().findIdentity();
    Node targetNode = engine.getNodeService().findNode(nodeId);
    if (targetNode == null) {
        return "Unknown node " + nodeId;
    }
    ITriggerRouterService triggerRouterService = engine.getTriggerRouterService();
    TriggerHistory triggerHistory = triggerRouterService.findTriggerHistory(catalogName, schemaName, tableName);
    if (triggerHistory == null) {
        return "Trigger for table " + tableName + " does not exist from node " + sourceNode.getNodeGroupId();
    } else {
        Trigger trigger = triggerRouterService.getTriggerById(triggerHistory.getTriggerId());
        if (trigger != null) {
            ISqlTransaction transaction = null;
            try {
                transaction = sqlTemplate.startSqlTransaction();
                insertSqlEvent(transaction, triggerHistory, trigger.getChannelId(), targetNode, sql, false, -1, null);
                transaction.commit();
                return "Successfully create SQL event for node " + targetNode.getNodeId();
            } catch (Error ex) {
                if (transaction != null) {
                    transaction.rollback();
                }
                throw ex;
            } catch (RuntimeException ex) {
                if (transaction != null) {
                    transaction.rollback();
                }
                throw ex;
            } finally {
                close(transaction);
            }
        } else {
            return "Trigger for table " + tableName + " does not exist from node " + sourceNode.getNodeGroupId();
        }
    }
}
Also used : ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) Trigger(org.jumpmind.symmetric.model.Trigger) ITriggerRouterService(org.jumpmind.symmetric.service.ITriggerRouterService) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) Node(org.jumpmind.symmetric.model.Node)

Example 28 with Trigger

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

the class TriggerRouterService method updateOrCreateDatabaseTriggers.

protected void updateOrCreateDatabaseTriggers(final List<Trigger> triggers, final StringBuilder sqlBuffer, final boolean force, final boolean verifyInDatabase, final List<TriggerHistory> activeTriggerHistories, final boolean useTableCache) {
    int numThreads = getNumberOfThreadsToUseForSyncTriggers();
    ExecutorService executor = Executors.newFixedThreadPool(numThreads, new SyncTriggersThreadFactory());
    List<Future<?>> futures = new ArrayList<Future<?>>();
    for (final Trigger trigger : triggers) {
        Runnable task = new Runnable() {

            public void run() {
                updateOrCreateDatabaseTrigger(trigger, triggers, sqlBuffer, force, verifyInDatabase, activeTriggerHistories, useTableCache);
            }
        };
        futures.add(executor.submit(task));
    }
    awaitTermination(executor, futures);
}
Also used : Trigger(org.jumpmind.symmetric.model.Trigger) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future)

Example 29 with Trigger

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

the class TriggerRouterService method syncTriggers.

public void syncTriggers(StringBuilder sqlBuffer, boolean force) {
    if ((parameterService.is(ParameterConstants.AUTO_SYNC_TRIGGERS) || isCalledFromSymmetricAdminTool())) {
        synchronized (this) {
            if (clusterService.lock(ClusterConstants.SYNCTRIGGERS)) {
                try {
                    String additionalMessage = "";
                    if (isCalledFromSymmetricAdminTool() && !parameterService.is(ParameterConstants.AUTO_SYNC_TRIGGERS)) {
                        additionalMessage = " " + ParameterConstants.AUTO_SYNC_TRIGGERS + " is set to false, but the sync triggers process will run so that needed changes can be written to a file so they can be applied manually.  Once all of the triggers have been successfully applied this process should not show triggers being created";
                    }
                    log.info("Synchronizing triggers{}", additionalMessage);
                    // make sure all tables are freshly read in
                    platform.resetCachedTableModel();
                    clearCache();
                    // make sure channels are read from the database
                    configurationService.clearCache();
                    List<Trigger> triggersForCurrentNode = getTriggersForCurrentNode();
                    boolean createTriggersForTables = false;
                    String nodeId = nodeService.findIdentityNodeId();
                    if (StringUtils.isNotBlank(nodeId)) {
                        NodeSecurity nodeSecurity = nodeService.findNodeSecurity(nodeId);
                        if (nodeSecurity != null && (nodeSecurity.isInitialLoadEnabled() || nodeSecurity.getInitialLoadTime() == null)) {
                            createTriggersForTables = parameterService.is(ParameterConstants.TRIGGER_CREATE_BEFORE_INITIAL_LOAD);
                            if (!createTriggersForTables) {
                                log.info("Trigger creation has been disabled by " + ParameterConstants.TRIGGER_CREATE_BEFORE_INITIAL_LOAD + " because an initial load is in progress or has not yet been requested");
                            }
                        } else {
                            createTriggersForTables = true;
                        }
                    }
                    if (!createTriggersForTables) {
                        triggersForCurrentNode.clear();
                    }
                    List<TriggerHistory> activeTriggerHistories = getActiveTriggerHistories();
                    inactivateTriggers(triggersForCurrentNode, sqlBuffer, activeTriggerHistories);
                    updateOrCreateDatabaseTriggers(triggersForCurrentNode, sqlBuffer, force, true, activeTriggerHistories, true);
                    resetTriggerRouterCacheByNodeGroupId();
                    if (createTriggersForTables) {
                        updateOrCreateDdlTriggers(sqlBuffer);
                    }
                } finally {
                    clusterService.unlock(ClusterConstants.SYNCTRIGGERS);
                    log.info("Done synchronizing triggers");
                }
            } else {
                Lock lock = clusterService.findLocks().get(ClusterConstants.SYNCTRIGGERS);
                if (lock != null) {
                    log.info("Sync triggers was locked by the cluster service.  The locking server id was: {}.  The lock time was: {}", lock.getLockingServerId(), lock.getLockTime());
                } else {
                    log.info("Sync triggers was locked by something but lock details were found");
                }
            }
        }
    } else {
        log.info("Not synchronizing triggers.  {} is set to false", ParameterConstants.AUTO_SYNC_TRIGGERS);
    }
}
Also used : Trigger(org.jumpmind.symmetric.model.Trigger) NodeSecurity(org.jumpmind.symmetric.model.NodeSecurity) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) Lock(org.jumpmind.symmetric.model.Lock)

Example 30 with Trigger

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

the class TriggerRouterService method enhanceTriggerRouters.

private List<TriggerRouter> enhanceTriggerRouters(List<TriggerRouter> triggerRouters) {
    HashMap<String, Router> routersById = new HashMap<String, Router>();
    for (Router router : getRouters()) {
        routersById.put(router.getRouterId().trim().toUpperCase(), router);
    }
    HashMap<String, Trigger> triggersById = new HashMap<String, Trigger>();
    for (Trigger trigger : getTriggers()) {
        triggersById.put(trigger.getTriggerId().trim().toUpperCase(), trigger);
    }
    for (TriggerRouter triggerRouter : triggerRouters) {
        triggerRouter.setTrigger(triggersById.get(triggerRouter.getTrigger().getTriggerId().trim().toUpperCase()));
        triggerRouter.setRouter(routersById.get(triggerRouter.getRouter().getRouterId().trim().toUpperCase()));
    }
    return triggerRouters;
}
Also used : Trigger(org.jumpmind.symmetric.model.Trigger) HashMap(java.util.HashMap) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) ConfigurationChangedDataRouter(org.jumpmind.symmetric.route.ConfigurationChangedDataRouter) FileSyncDataRouter(org.jumpmind.symmetric.route.FileSyncDataRouter) Router(org.jumpmind.symmetric.model.Router)

Aggregations

Trigger (org.jumpmind.symmetric.model.Trigger)42 TriggerRouter (org.jumpmind.symmetric.model.TriggerRouter)20 ArrayList (java.util.ArrayList)17 TriggerHistory (org.jumpmind.symmetric.model.TriggerHistory)14 Router (org.jumpmind.symmetric.model.Router)13 ITriggerRouterService (org.jumpmind.symmetric.service.ITriggerRouterService)11 Test (org.junit.Test)11 Date (java.util.Date)5 Table (org.jumpmind.db.model.Table)5 CsvData (org.jumpmind.symmetric.io.data.CsvData)5 Data (org.jumpmind.symmetric.model.Data)5 Node (org.jumpmind.symmetric.model.Node)5 HashSet (java.util.HashSet)4 ISqlTransaction (org.jumpmind.db.sql.ISqlTransaction)4 HashMap (java.util.HashMap)3 Set (java.util.Set)3 ISymmetricDialect (org.jumpmind.symmetric.db.ISymmetricDialect)3 NodeGroupLink (org.jumpmind.symmetric.model.NodeGroupLink)3 INodeService (org.jumpmind.symmetric.service.INodeService)3 List (java.util.List)2