Search in sources :

Example 1 with ChannelRouterContext

use of org.jumpmind.symmetric.route.ChannelRouterContext 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 2 with ChannelRouterContext

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

the class RouterService method routeDataForChannel.

protected int routeDataForChannel(ProcessInfo processInfo, final NodeChannel nodeChannel, final Node sourceNode, DataGapDetector gapDetector) {
    ChannelRouterContext context = null;
    long ts = System.currentTimeMillis();
    int dataCount = -1;
    try {
        List<TriggerRouter> triggerRouters = engine.getTriggerRouterService().getTriggerRouters(true, 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);
        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);
                context.incrementStat(System.currentTimeMillis() - insertTs, ChannelRouterContext.STAT_INSERT_DATA_EVENTS_MS);
                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", channelId, queryTs);
                        log.info("The query took {} ms", 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);
            boolean detectGaps = context.isRequestGapDetection();
            context.cleanup();
            if (detectGaps) {
                gapDetector.beforeRouting();
            }
        }
    }
}
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) DelayRoutingException(org.jumpmind.symmetric.route.DelayRoutingException) SyntaxParsingException(org.jumpmind.symmetric.SyntaxParsingException) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) DelayRoutingException(org.jumpmind.symmetric.route.DelayRoutingException)

Example 3 with ChannelRouterContext

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

the class AbstractRouterServiceTest method testDontSelectOldDataDuringRouting.

public void testDontSelectOldDataDuringRouting() throws Exception {
    NodeChannel testChannel = getConfigurationService().getNodeChannel(TestConstants.TEST_CHANNEL_ID, false);
    testChannel.setUseOldDataToRoute(false);
    testChannel.setMaxBatchSize(50);
    testChannel.setBatchAlgorithm("nontransactional");
    getConfigurationService().saveChannel(testChannel, true);
    TriggerRouter trigger = getTestRoutingTableTrigger(TEST_TABLE_1);
    trigger.getRouter().setRouterType("column");
    trigger.getRouter().setRouterExpression("ROUTING_VARCHAR=:NODE_ID");
    getTriggerRouterService().saveTriggerRouter(trigger);
    // clean setup
    deleteAll(TEST_TABLE_1);
    insert(TEST_TABLE_1, 100, true);
    getRouterService().routeData(true);
    resetBatches();
    // delete
    deleteAll(TEST_TABLE_1);
    ISqlTransaction transaction = getSqlTemplate().startSqlTransaction();
    ChannelRouterContext context = new ChannelRouterContext(TestConstants.TEST_ROOT_EXTERNAL_ID, testChannel, transaction);
    DataGapRouteReader reader = new DataGapRouteReader(context, engine);
    reader.run();
    List<Data> list = new ArrayList<Data>();
    do {
        Data data = reader.take();
        if (data != null) {
            list.add(data);
        } else {
            break;
        }
    } while (true);
    transaction.close();
    Assert.assertEquals(100, list.size());
    for (Data data : list) {
        Assert.assertNull(data.toParsedOldData());
        Assert.assertNotNull(data.toParsedPkData());
    }
}
Also used : DataGapRouteReader(org.jumpmind.symmetric.route.DataGapRouteReader) ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) ChannelRouterContext(org.jumpmind.symmetric.route.ChannelRouterContext) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) ArrayList(java.util.ArrayList) Data(org.jumpmind.symmetric.model.Data) NodeChannel(org.jumpmind.symmetric.model.NodeChannel)

Aggregations

Data (org.jumpmind.symmetric.model.Data)3 TriggerRouter (org.jumpmind.symmetric.model.TriggerRouter)3 ChannelRouterContext (org.jumpmind.symmetric.route.ChannelRouterContext)3 SyntaxParsingException (org.jumpmind.symmetric.SyntaxParsingException)2 DataMetaData (org.jumpmind.symmetric.model.DataMetaData)2 DelayRoutingException (org.jumpmind.symmetric.route.DelayRoutingException)2 ArrayList (java.util.ArrayList)1 ISqlTransaction (org.jumpmind.db.sql.ISqlTransaction)1 SymmetricException (org.jumpmind.symmetric.SymmetricException)1 NodeChannel (org.jumpmind.symmetric.model.NodeChannel)1 DataGapRouteReader (org.jumpmind.symmetric.route.DataGapRouteReader)1