use of org.jumpmind.symmetric.model.TriggerRouter in project symmetric-ds by JumpMind.
the class AbstractRouterServiceTest method testColumnMatchOnNotNull.
public void testColumnMatchOnNotNull() {
NodeChannel testChannel = getConfigurationService().getNodeChannel(TestConstants.TEST_CHANNEL_ID, false);
TriggerRouter trigger = getTestRoutingTableTrigger(TEST_TABLE_1);
trigger.getRouter().setRouterType("column");
trigger.getRouter().setRouterExpression("ROUTING_VARCHAR!=NULL");
getTriggerRouterService().saveTriggerRouter(trigger);
getTriggerRouterService().syncTriggers();
resetBatches();
update(TEST_TABLE_1, "Not Routed");
Assert.assertEquals(0, countBatchesForChannel(getOutgoingBatchService().getOutgoingBatches(NODE_GROUP_NODE_1.getNodeId(), false), testChannel));
getRouterService().routeData(true);
Assert.assertEquals(1, countBatchesForChannel(getOutgoingBatchService().getOutgoingBatches(NODE_GROUP_NODE_1.getNodeId(), false), testChannel));
resetBatches();
update(TEST_TABLE_1, null);
Assert.assertEquals(0, countBatchesForChannel(getOutgoingBatchService().getOutgoingBatches(NODE_GROUP_NODE_1.getNodeId(), false), testChannel));
getRouterService().routeData(true);
Assert.assertEquals(0, countBatchesForChannel(getOutgoingBatchService().getOutgoingBatches(NODE_GROUP_NODE_1.getNodeId(), false), testChannel));
}
use of org.jumpmind.symmetric.model.TriggerRouter in project symmetric-ds by JumpMind.
the class RestService method sendSchemaImpl.
private SendSchemaResponse sendSchemaImpl(ISymmetricEngine engine, SendSchemaRequest request) {
IConfigurationService configurationService = engine.getConfigurationService();
INodeService nodeService = engine.getNodeService();
ITriggerRouterService triggerRouterService = engine.getTriggerRouterService();
IDataService dataService = engine.getDataService();
SendSchemaResponse response = new SendSchemaResponse();
org.jumpmind.symmetric.model.Node identity = nodeService.findIdentity();
if (identity != null) {
List<org.jumpmind.symmetric.model.Node> nodesToSendTo = new ArrayList<org.jumpmind.symmetric.model.Node>();
List<String> nodeIds = request.getNodeIdsToSendTo();
if (nodeIds == null || nodeIds.size() == 0) {
nodeIds = new ArrayList<String>();
String nodeGroupIdToSendTo = request.getNodeGroupIdToSendTo();
if (isNotBlank(nodeGroupIdToSendTo)) {
NodeGroupLink link = configurationService.getNodeGroupLinkFor(identity.getNodeGroupId(), nodeGroupIdToSendTo, false);
if (link != null) {
Collection<org.jumpmind.symmetric.model.Node> nodes = nodeService.findEnabledNodesFromNodeGroup(nodeGroupIdToSendTo);
nodesToSendTo.addAll(nodes);
} else {
log.warn("Could not send schema to all nodes in the '" + nodeGroupIdToSendTo + "' node group. No node group link exists");
}
} else {
log.warn("Could not send schema to nodes. There are none that were provided and the nodeGroupIdToSendTo was also not provided");
}
} else {
for (String nodeIdToValidate : nodeIds) {
org.jumpmind.symmetric.model.Node node = nodeService.findNode(nodeIdToValidate);
if (node != null) {
NodeGroupLink link = configurationService.getNodeGroupLinkFor(identity.getNodeGroupId(), node.getNodeGroupId(), false);
if (link != null) {
nodesToSendTo.add(node);
} else {
log.warn("Could not send schema to node '" + nodeIdToValidate + "'. No node group link exists");
}
} else {
log.warn("Could not send schema to node '" + nodeIdToValidate + "'. It was not present in the database");
}
}
}
Map<String, List<TableName>> results = response.getNodeIdsSentTo();
List<String> nodeIdsToSendTo = toNodeIds(nodesToSendTo);
for (String nodeId : nodeIdsToSendTo) {
results.put(nodeId, new ArrayList<TableName>());
}
if (nodesToSendTo.size() > 0) {
List<TableName> tablesToSend = request.getTablesToSend();
List<TriggerRouter> triggerRouters = triggerRouterService.getTriggerRouters(false);
for (TriggerRouter triggerRouter : triggerRouters) {
Trigger trigger = triggerRouter.getTrigger();
NodeGroupLink link = triggerRouter.getRouter().getNodeGroupLink();
if (link.getSourceNodeGroupId().equals(identity.getNodeGroupId())) {
for (org.jumpmind.symmetric.model.Node node : nodesToSendTo) {
if (link.getTargetNodeGroupId().equals(node.getNodeGroupId())) {
if (tablesToSend == null || tablesToSend.size() == 0 || contains(trigger, tablesToSend)) {
dataService.sendSchema(node.getNodeId(), trigger.getSourceCatalogName(), trigger.getSourceSchemaName(), trigger.getSourceTableName(), false);
results.get(node.getNodeId()).add(new TableName(trigger.getSourceCatalogName(), trigger.getSourceSchemaName(), trigger.getSourceTableName()));
}
}
}
}
}
}
}
return response;
}
use of org.jumpmind.symmetric.model.TriggerRouter in project symmetric-ds by JumpMind.
the class ConfigurationChangedDataRouter method queueSyncTriggers.
@SuppressWarnings("unchecked")
protected void queueSyncTriggers(SimpleRouterContext routingContext, DataMetaData dataMetaData, Map<String, String> columnValues) {
if ((tableMatches(dataMetaData, TableConstants.SYM_TRIGGER) || tableMatches(dataMetaData, TableConstants.SYM_TRIGGER_ROUTER))) {
Object needResync = routingContext.get(CTX_KEY_RESYNC_NEEDED);
if (needResync == null || needResync instanceof Set) {
if (needResync == null) {
needResync = new HashSet<Trigger>();
routingContext.put(CTX_KEY_RESYNC_NEEDED, needResync);
}
ITriggerRouterService triggerRouterService = engine.getTriggerRouterService();
boolean refreshCache = false;
if (routingContext.get(CTX_KEY_FLUSHED_TRIGGER_ROUTERS) == null) {
triggerRouterService.clearCache();
refreshCache = true;
routingContext.put(CTX_KEY_FLUSHED_TRIGGER_ROUTERS, true);
}
Trigger trigger = null;
Date lastUpdateTime = null;
String triggerId = columnValues.get("TRIGGER_ID");
if (tableMatches(dataMetaData, TableConstants.SYM_TRIGGER_ROUTER)) {
String routerId = columnValues.get("ROUTER_ID");
TriggerRouter tr = triggerRouterService.findTriggerRouterById(triggerId, routerId, refreshCache);
if (tr != null) {
trigger = tr.getTrigger();
lastUpdateTime = tr.getLastUpdateTime();
}
} else {
trigger = triggerRouterService.getTriggerById(triggerId, refreshCache);
if (trigger != null) {
lastUpdateTime = trigger.getLastUpdateTime();
}
}
if (trigger != null) {
List<TriggerHistory> histories = triggerRouterService.getActiveTriggerHistories(trigger);
boolean sync = false;
if (histories != null && histories.size() > 0) {
for (TriggerHistory triggerHistory : histories) {
if (triggerHistory.getCreateTime().before(lastUpdateTime)) {
sync = true;
}
}
} else {
sync = true;
}
if (sync) {
((Set<Trigger>) needResync).add(trigger);
}
}
}
} else if (tableMatches(dataMetaData, TableConstants.SYM_ROUTER) || tableMatches(dataMetaData, TableConstants.SYM_NODE_GROUP_LINK)) {
routingContext.put(CTX_KEY_RESYNC_NEEDED, Boolean.TRUE);
}
}
use of org.jumpmind.symmetric.model.TriggerRouter in project symmetric-ds by JumpMind.
the class FileSyncDataRouter method routeToNodes.
public Set<String> routeToNodes(SimpleRouterContext context, DataMetaData dataMetaData, Set<Node> nodes, boolean initialLoad, boolean initialLoadSelectUsed, TriggerRouter triggerRouter) {
Set<String> nodeIds = new HashSet<String>();
IFileSyncService fileSyncService = engine.getFileSyncService();
IRouterService routerService = engine.getRouterService();
Map<String, String> newData = getNewDataAsString(null, dataMetaData, engine.getSymmetricDialect());
String triggerId = newData.get("TRIGGER_ID");
String routerId = newData.get("ROUTER_ID");
String sourceNodeId = newData.get("LAST_UPDATE_BY");
String lastEventType = newData.get("LAST_EVENT_TYPE");
if (triggerId == null) {
Map<String, String> oldData = getOldDataAsString(null, dataMetaData, engine.getSymmetricDialect());
triggerId = oldData.get("TRIGGER_ID");
routerId = oldData.get("ROUTER_ID");
sourceNodeId = oldData.get("LAST_UPDATE_BY");
lastEventType = oldData.get("LAST_EVENT_TYPE");
}
LastEventType eventType = LastEventType.fromCode(lastEventType);
FileTriggerRouter fileTriggerRouter = fileSyncService.getFileTriggerRouter(triggerId, routerId);
if (fileTriggerRouter != null && fileTriggerRouter.isEnabled()) {
if (fileTriggerRouter.getRouter().getNodeGroupLink().equals(triggerRouter.getRouter().getNodeGroupLink())) {
if (eventType == null || eventType == LastEventType.DELETE && fileTriggerRouter.getFileTrigger().isSyncOnDelete() || eventType == LastEventType.MODIFY && fileTriggerRouter.getFileTrigger().isSyncOnModified() || eventType == LastEventType.CREATE && fileTriggerRouter.getFileTrigger().isSyncOnCreate()) {
Router router = fileTriggerRouter.getRouter();
Map<String, IDataRouter> routers = routerService.getRouters();
IDataRouter dataRouter = null;
if (StringUtils.isNotBlank(router.getRouterType())) {
dataRouter = routers.get(router.getRouterType());
}
if (dataRouter == null) {
dataRouter = routers.get("default");
}
if (context instanceof ChannelRouterContext) {
((ChannelRouterContext) context).addUsedDataRouter(dataRouter);
}
dataMetaData.setRouter(router);
Set<String> dataRouterNodeIds = dataRouter.routeToNodes(context, dataMetaData, nodes, false, false, triggerRouter);
if (dataRouterNodeIds != null) {
nodeIds.addAll(dataRouterNodeIds);
}
nodeIds.remove(sourceNodeId);
}
}
} else {
log.error("Could not find a trigger router with a trigger_id of {} and a router_id of {}. The file snapshot will not be routed", triggerId, routerId);
}
return nodeIds;
}
use of org.jumpmind.symmetric.model.TriggerRouter in project symmetric-ds by JumpMind.
the class RouterService method producesCommonBatches.
protected boolean producesCommonBatches(Channel channel, String nodeGroupId, List<TriggerRouter> triggerRouters) {
String channelId = channel.getChannelId();
Boolean producesCommonBatches = commonBatchesLastKnownState.get(channelId);
long cacheTime = parameterService.getLong(ParameterConstants.CACHE_CHANNEL_COMMON_BATCHES_IN_MS);
if (producesCommonBatches == null || System.currentTimeMillis() - commonBatchesCacheTime > cacheTime) {
producesCommonBatches = !Constants.CHANNEL_CONFIG.equals(channelId) && !channel.isFileSyncFlag() && !channel.isReloadFlag() && !Constants.CHANNEL_HEARTBEAT.equals(channelId) ? true : false;
if (producesCommonBatches && triggerRouters != null) {
List<TriggerRouter> testableTriggerRouters = new ArrayList<TriggerRouter>();
for (TriggerRouter triggerRouter : triggerRouters) {
if (triggerRouter.getTrigger().getChannelId().equals(channel.getChannelId())) {
testableTriggerRouters.add(triggerRouter);
} else {
/*
* Add any trigger router that is in another channel, but is
* for a table that is in the current channel
*/
String anotherChannelTableName = triggerRouter.getTrigger().getFullyQualifiedSourceTableName();
for (TriggerRouter triggerRouter2 : triggerRouters) {
String currentTableName = triggerRouter2.getTrigger().getFullyQualifiedSourceTableName();
String currentChannelId = triggerRouter2.getTrigger().getChannelId();
if (anotherChannelTableName.equals(currentTableName) && currentChannelId.equals(channelId)) {
testableTriggerRouters.add(triggerRouter);
}
}
}
}
for (TriggerRouter triggerRouter : testableTriggerRouters) {
boolean isDefaultRouter = "default".equals(triggerRouter.getRouter().getRouterType());
/*
* If the data router is not a default data router or there will
* be incoming data on the channel where sync_on_incoming_batch
* is on, then we can not do 'optimal' routing. When
* sync_on_incoming_batch is on, then we might not be sending
* data to all nodes in a node_group. We can only do 'optimal'
* routing if data is going to go to all nodes in a group.
*/
if (triggerRouter.getRouter().getNodeGroupLink().getSourceNodeGroupId().equals(nodeGroupId)) {
if (!isDefaultRouter) {
producesCommonBatches = false;
break;
} else {
if (triggerRouter.getTrigger().isSyncOnIncomingBatch()) {
String outgoingTableName = triggerRouter.getTrigger().getFullyQualifiedSourceTableName();
for (TriggerRouter triggerRouter2 : testableTriggerRouters) {
String incomingTableName = triggerRouter2.getTrigger().getFullyQualifiedSourceTableName();
String targetNodeGroupId = triggerRouter2.getRouter().getNodeGroupLink().getTargetNodeGroupId();
if (incomingTableName.equals(outgoingTableName) && targetNodeGroupId.equals(nodeGroupId)) {
producesCommonBatches = false;
break;
}
}
}
}
}
}
}
if (!producesCommonBatches.equals(commonBatchesLastKnownState.get(channelId))) {
if (producesCommonBatches) {
log.info("The '{}' channel is in common batch mode", channelId);
} else {
log.info("The '{}' channel is NOT in common batch mode", channelId);
}
commonBatchesLastKnownState.put(channelId, producesCommonBatches);
}
commonBatchesCacheTime = System.currentTimeMillis();
}
return producesCommonBatches;
}
Aggregations