Search in sources :

Example 1 with IDataRouter

use of org.jumpmind.symmetric.route.IDataRouter in project symmetric-ds by JumpMind.

the class RouterService method shouldDataBeRouted.

/**
 * For use in data load events
 */
public boolean shouldDataBeRouted(SimpleRouterContext context, DataMetaData dataMetaData, Node node, boolean initialLoad, boolean initialLoadSelectUsed, TriggerRouter triggerRouter) {
    IDataRouter router = getDataRouter(dataMetaData.getRouter());
    Set<Node> oneNodeSet = new HashSet<Node>(1);
    oneNodeSet.add(node);
    Collection<String> nodeIds = router.routeToNodes(context, dataMetaData, oneNodeSet, initialLoad, initialLoadSelectUsed, triggerRouter);
    return nodeIds != null && nodeIds.contains(node.getNodeId());
}
Also used : IDataRouter(org.jumpmind.symmetric.route.IDataRouter) Node(org.jumpmind.symmetric.model.Node) HashSet(java.util.HashSet)

Example 2 with IDataRouter

use of org.jumpmind.symmetric.route.IDataRouter in project symmetric-ds by JumpMind.

the class RouterService method routeData.

@SuppressWarnings("unchecked")
protected int routeData(ProcessInfo processInfo, Data data, ChannelRouterContext context) {
    int numberOfDataEventsInserted = 0;
    List<TriggerRouter> triggerRouters = getTriggerRoutersForData(data);
    Table table = symmetricDialect.getTable(data.getTriggerHistory(), true);
    if (triggerRouters != null && triggerRouters.size() > 0) {
        for (TriggerRouter triggerRouter : triggerRouters) {
            DataMetaData dataMetaData = new DataMetaData(data, table, triggerRouter.getRouter(), context.getChannel());
            Collection<String> nodeIds = null;
            if (!context.getChannel().isIgnoreEnabled() && triggerRouter.isRouted(data.getDataEventType())) {
                String targetNodeIds = data.getNodeList();
                if (StringUtils.isNotBlank(targetNodeIds)) {
                    List<String> targetNodeIdsList = Arrays.asList(targetNodeIds.split(","));
                    nodeIds = CollectionUtils.intersection(targetNodeIdsList, toNodeIds(findAvailableNodes(triggerRouter, context)));
                    if (nodeIds.size() == 0) {
                        log.info("None of the target nodes specified in the data.node_list field ({}) were qualified nodes.  {} will not be routed using the {} router", new Object[] { targetNodeIds, data.getDataId(), triggerRouter.getRouter().getRouterId() });
                    }
                } else {
                    try {
                        IDataRouter dataRouter = getDataRouter(triggerRouter.getRouter());
                        context.addUsedDataRouter(dataRouter);
                        long ts = System.currentTimeMillis();
                        nodeIds = dataRouter.routeToNodes(context, dataMetaData, findAvailableNodes(triggerRouter, context), false, false, triggerRouter);
                        context.incrementStat(System.currentTimeMillis() - ts, ChannelRouterContext.STAT_DATA_ROUTER_MS);
                    } catch (RuntimeException ex) {
                        StringBuilder failureMessage = new StringBuilder("Failed to route data: ");
                        failureMessage.append(data.getDataId());
                        failureMessage.append(" for table: ");
                        failureMessage.append(data.getTableName());
                        failureMessage.append(".\n");
                        data.writeCsvDataDetails(failureMessage);
                        log.error(failureMessage.toString());
                        throw ex;
                    }
                }
                if (nodeIds != null) {
                    if (!triggerRouter.isPingBackEnabled() && data.getSourceNodeId() != null) {
                        nodeIds.remove(data.getSourceNodeId());
                    }
                    // should never route to self
                    nodeIds.remove(engine.getNodeService().findIdentityNodeId());
                }
            }
            numberOfDataEventsInserted += insertDataEvents(processInfo, context, dataMetaData, nodeIds);
        }
    } else {
        log.warn("Could not find trigger routers for trigger history id of {}.  There is a good chance that data was captured and the trigger router link was removed before the data could be routed", data.getTriggerHistory().getTriggerHistoryId());
        log.info("Data with the id of {} will be assigned to an unrouted batch", data.getDataId());
        numberOfDataEventsInserted += insertDataEvents(processInfo, context, new DataMetaData(data, table, null, context.getChannel()), new HashSet<String>(0));
    }
    context.incrementStat(numberOfDataEventsInserted, ChannelRouterContext.STAT_DATA_EVENTS_INSERTED);
    return numberOfDataEventsInserted;
}
Also used : Table(org.jumpmind.db.model.Table) IDataRouter(org.jumpmind.symmetric.route.IDataRouter) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) DataMetaData(org.jumpmind.symmetric.model.DataMetaData) HashSet(java.util.HashSet)

Example 3 with IDataRouter

use of org.jumpmind.symmetric.route.IDataRouter in project symmetric-ds by JumpMind.

the class RouterService method completeBatchesAndCommit.

protected void completeBatchesAndCommit(ChannelRouterContext context) {
    Set<IDataRouter> usedRouters = new HashSet<IDataRouter>(context.getUsedDataRouters());
    List<OutgoingBatch> batches = new ArrayList<OutgoingBatch>(context.getBatchesByNodes().values());
    context.commit();
    if (engine.getParameterService().is(ParameterConstants.ROUTING_LOG_STATS_ON_BATCH_ERROR)) {
        engine.getStatisticManager().addRouterStats(context.getStartDataId(), context.getEndDataId(), context.getDataReadCount(), context.getPeekAheadFillCount(), context.getDataGaps(), context.getTransactions(), batches);
    }
    for (OutgoingBatch batch : batches) {
        batch.setRouterMillis(System.currentTimeMillis() - batch.getCreateTime().getTime());
        for (IDataRouter dataRouter : usedRouters) {
            dataRouter.completeBatch(context, batch);
        }
        if (Constants.UNROUTED_NODE_ID.equals(batch.getNodeId())) {
            batch.setStatus(Status.OK);
        } else {
            batch.setStatus(Status.NE);
        }
        engine.getOutgoingBatchService().updateOutgoingBatch(batch);
        context.getBatchesByNodes().remove(batch.getNodeId());
    }
    for (IDataRouter dataRouter : usedRouters) {
        dataRouter.contextCommitted(context);
    }
    context.setNeedsCommitted(false);
}
Also used : IDataRouter(org.jumpmind.symmetric.route.IDataRouter) ArrayList(java.util.ArrayList) OutgoingBatch(org.jumpmind.symmetric.model.OutgoingBatch) HashSet(java.util.HashSet)

Example 4 with IDataRouter

use of org.jumpmind.symmetric.route.IDataRouter in project symmetric-ds by JumpMind.

the class RouterService method getDataRouter.

protected IDataRouter getDataRouter(Router router) {
    IDataRouter dataRouter = null;
    Map<String, IDataRouter> routers = getRouters();
    if (!StringUtils.isBlank(router.getRouterType())) {
        dataRouter = routers.get(router.getRouterType());
        if (dataRouter == null) {
            log.warn("Could not find configured router type of {} with the id of {}. Defaulting the router", router.getRouterType(), router.getRouterId());
        }
    }
    if (dataRouter == null) {
        return getRouters().get("default");
    }
    return dataRouter;
}
Also used : IDataRouter(org.jumpmind.symmetric.route.IDataRouter)

Aggregations

IDataRouter (org.jumpmind.symmetric.route.IDataRouter)4 HashSet (java.util.HashSet)3 ArrayList (java.util.ArrayList)1 Table (org.jumpmind.db.model.Table)1 DataMetaData (org.jumpmind.symmetric.model.DataMetaData)1 Node (org.jumpmind.symmetric.model.Node)1 OutgoingBatch (org.jumpmind.symmetric.model.OutgoingBatch)1 TriggerRouter (org.jumpmind.symmetric.model.TriggerRouter)1