Search in sources :

Example 41 with TriggerRouter

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

Example 42 with TriggerRouter

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

the class DataService method insertCreateBatchesForReload.

private void insertCreateBatchesForReload(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 createEventsSent = 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 create flag on the specific table reload request
                if (currentRequest != null && currentRequest.isCreateTable() && engine.getGroupletService().isTargetEnabled(triggerRouter, targetNode)) {
                    insertCreateEvent(transaction, targetNode, triggerHistory, triggerRouter.getRouter().getRouterId(), true, loadId, createBy);
                    createEventsSent++;
                    if (!transactional) {
                        transaction.commit();
                    }
                }
            }
        }
        if (createEventsSent > 0) {
            log.info("Before sending load {} to target node {} create table events were sent for {} tables", new Object[] { loadId, targetNode, createEventsSent });
        }
    } else {
        if (parameterService.is(ParameterConstants.INITIAL_LOAD_CREATE_SCHEMA_BEFORE_RELOAD)) {
            for (TriggerHistory triggerHistory : triggerHistories) {
                List<TriggerRouter> triggerRouters = triggerRoutersByHistoryId.get(triggerHistory.getTriggerHistoryId());
                for (TriggerRouter triggerRouter : triggerRouters) {
                    if (triggerRouter.getInitialLoadOrder() >= 0 && engine.getGroupletService().isTargetEnabled(triggerRouter, targetNode)) {
                        insertCreateEvent(transaction, targetNode, triggerHistory, triggerRouter.getRouter().getRouterId(), true, 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)

Example 43 with TriggerRouter

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

the class RouterService method routeDataForChannel.

protected int routeDataForChannel(ProcessInfo processInfo, final NodeChannel nodeChannel, final Node sourceNode) {
    ChannelRouterContext context = null;
    long ts = System.currentTimeMillis();
    int dataCount = -1;
    try {
        List<TriggerRouter> triggerRouters = engine.getTriggerRouterService().getTriggerRouters(false);
        boolean producesCommonBatches = producesCommonBatches(nodeChannel.getChannel(), parameterService.getNodeGroupId(), triggerRouters);
        boolean onlyDefaultRoutersAssigned = onlyDefaultRoutersAssigned(nodeChannel.getChannel(), parameterService.getNodeGroupId(), triggerRouters);
        context = new ChannelRouterContext(sourceNode.getNodeId(), nodeChannel, symmetricDialect.getPlatform().getSqlTemplate().startSqlTransaction());
        context.setProduceCommonBatches(producesCommonBatches);
        context.setOnlyDefaultRoutersAssigned(onlyDefaultRoutersAssigned);
        context.setDataGaps(gapDetector.getDataGaps());
        dataCount = selectDataAndRoute(processInfo, context);
        return dataCount;
    } catch (DelayRoutingException ex) {
        log.info("The routing process for the {} channel is being delayed.  {}", nodeChannel.getChannelId(), isNotBlank(ex.getMessage()) ? ex.getMessage() : "");
        if (context != null) {
            context.rollback();
        }
        return 0;
    } catch (InterruptedException ex) {
        log.warn("The routing process was interrupted.  Rolling back changes");
        if (context != null) {
            context.rollback();
        }
        return 0;
    } catch (SyntaxParsingException ex) {
        log.error(String.format("Failed to route and batch data on '%s' channel due to an invalid router expression", nodeChannel.getChannelId()), ex);
        if (context != null) {
            context.rollback();
        }
        return 0;
    } catch (Throwable ex) {
        log.error(String.format("Failed to route and batch data on '%s' channel", nodeChannel.getChannelId()), ex);
        if (context != null) {
            context.rollback();
        }
        return 0;
    } finally {
        try {
            if (dataCount > 0) {
                long insertTs = System.currentTimeMillis();
                engine.getDataService().insertDataEvents(context.getSqlTransaction(), context.getDataEventList());
                context.clearDataEventsList();
                completeBatchesAndCommit(context);
                gapDetector.addDataIds(context.getDataIds());
                gapDetector.setIsAllDataRead(context.getDataIds().size() < context.getChannel().getMaxDataToRoute());
                context.incrementStat(System.currentTimeMillis() - insertTs, ChannelRouterContext.STAT_INSERT_DATA_EVENTS_MS);
                if (parameterService.is(ParameterConstants.ROUTING_COLLECT_STATS_UNROUTED)) {
                    Data lastDataProcessed = context.getLastDataProcessed();
                    if (lastDataProcessed != null && lastDataProcessed.getDataId() > 0) {
                        String channelId = nodeChannel.getChannelId();
                        long queryTs = System.currentTimeMillis();
                        long dataLeftToRoute = sqlTemplate.queryForInt(getSql("selectUnroutedCountForChannelSql"), channelId, lastDataProcessed.getDataId());
                        queryTs = System.currentTimeMillis() - queryTs;
                        if (queryTs > Constants.LONG_OPERATION_THRESHOLD) {
                            log.warn("Unrouted query for channel {} took longer than expected. The query took {} ms.", channelId, queryTs);
                        }
                        engine.getStatisticManager().setDataUnRouted(channelId, dataLeftToRoute);
                    }
                }
            }
        } catch (Exception e) {
            if (context != null) {
                context.rollback();
            }
            log.error("", e);
        } finally {
            long totalTime = System.currentTimeMillis() - ts;
            context.incrementStat(totalTime, ChannelRouterContext.STAT_ROUTE_TOTAL_TIME);
            context.logStats(log, totalTime);
            context.cleanup();
        }
    }
}
Also used : ChannelRouterContext(org.jumpmind.symmetric.route.ChannelRouterContext) Data(org.jumpmind.symmetric.model.Data) DataMetaData(org.jumpmind.symmetric.model.DataMetaData) SyntaxParsingException(org.jumpmind.symmetric.SyntaxParsingException) SymmetricException(org.jumpmind.symmetric.SymmetricException) DelayRoutingException(org.jumpmind.symmetric.route.DelayRoutingException) SyntaxParsingException(org.jumpmind.symmetric.SyntaxParsingException) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) DelayRoutingException(org.jumpmind.symmetric.route.DelayRoutingException)

Example 44 with TriggerRouter

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

the class TriggerRouterService method getTriggerRoutersCacheForCurrentNode.

protected TriggerRoutersCache getTriggerRoutersCacheForCurrentNode(boolean refreshCache) {
    String myNodeGroupId = parameterService.getNodeGroupId();
    long triggerRouterCacheTimeoutInMs = parameterService.getLong(ParameterConstants.CACHE_TIMEOUT_TRIGGER_ROUTER_IN_MS);
    TriggerRoutersCache cache = triggerRouterCacheByNodeGroupId == null ? null : triggerRouterCacheByNodeGroupId.get(myNodeGroupId);
    if (cache == null || refreshCache || System.currentTimeMillis() - this.triggerRouterPerNodeCacheTime > triggerRouterCacheTimeoutInMs) {
        synchronized (cacheLock) {
            this.triggerRouterPerNodeCacheTime = System.currentTimeMillis();
            Map<String, TriggerRoutersCache> newTriggerRouterCacheByNodeGroupId = new HashMap<String, TriggerRoutersCache>();
            List<TriggerRouter> triggerRouters = getAllTriggerRoutersForCurrentNode(myNodeGroupId);
            Map<String, List<TriggerRouter>> triggerRoutersByTriggerId = new HashMap<String, List<TriggerRouter>>(triggerRouters.size());
            Map<String, Router> routers = new HashMap<String, Router>(triggerRouters.size());
            for (TriggerRouter triggerRouter : triggerRouters) {
                if (triggerRouter.isEnabled()) {
                    boolean sourceEnabled = groupletService.isSourceEnabled(triggerRouter);
                    if (sourceEnabled) {
                        String triggerId = triggerRouter.getTrigger().getTriggerId();
                        List<TriggerRouter> list = triggerRoutersByTriggerId.get(triggerId);
                        if (list == null) {
                            list = new ArrayList<TriggerRouter>();
                            triggerRoutersByTriggerId.put(triggerId, list);
                        }
                        list.add(triggerRouter);
                        routers.put(triggerRouter.getRouter().getRouterId(), triggerRouter.getRouter());
                    }
                }
            }
            newTriggerRouterCacheByNodeGroupId.put(myNodeGroupId, new TriggerRoutersCache(triggerRoutersByTriggerId, routers));
            this.triggerRouterCacheByNodeGroupId = newTriggerRouterCacheByNodeGroupId;
            cache = triggerRouterCacheByNodeGroupId == null ? null : triggerRouterCacheByNodeGroupId.get(myNodeGroupId);
        }
    }
    return cache;
}
Also used : HashMap(java.util.HashMap) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) ConfigurationChangedDataRouter(org.jumpmind.symmetric.route.ConfigurationChangedDataRouter) FileSyncDataRouter(org.jumpmind.symmetric.route.FileSyncDataRouter) Router(org.jumpmind.symmetric.model.Router) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) List(java.util.List) ArrayList(java.util.ArrayList)

Example 45 with TriggerRouter

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

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