use of org.jumpmind.symmetric.model.TriggerHistory in project symmetric-ds by JumpMind.
the class DataService method sendScript.
public void sendScript(String nodeId, String script, boolean isLoad) {
Node targetNode = engine.getNodeService().findNode(nodeId);
TriggerHistory history = engine.getTriggerRouterService().findTriggerHistoryForGenericSync();
Data data = new Data(history.getSourceTableName(), DataEventType.BSH, CsvUtils.escapeCsvData(script), null, history, Constants.CHANNEL_CONFIG, null, null);
data.setNodeList(nodeId);
if (!isLoad) {
insertData(data);
} else {
insertDataAndDataEventAndOutgoingBatch(data, targetNode.getNodeId(), Constants.UNKNOWN_ROUTER_ID, isLoad, -1, null);
}
}
use of org.jumpmind.symmetric.model.TriggerHistory 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());
}
use of org.jumpmind.symmetric.model.TriggerHistory in project symmetric-ds by JumpMind.
the class DataService method reloadMissingForeignKeyRows.
public void reloadMissingForeignKeyRows(String nodeId, long dataId) {
try {
Data data = findData(dataId);
log.debug("reloadMissingForeignKeyRows for nodeId '{}' dataId '{}' table '{}'", nodeId, dataId, data.getTableName());
TriggerHistory hist = data.getTriggerHistory();
Table table = platform.getTableFromCache(hist.getSourceCatalogName(), hist.getSourceSchemaName(), hist.getSourceTableName(), false);
Map<String, String> dataMap = data.toColumnNameValuePairs(table.getColumnNames(), CsvData.ROW_DATA);
List<TableRow> tableRows = new ArrayList<TableRow>();
Row row = new Row(dataMap.size());
row.putAll(dataMap);
tableRows.add(new TableRow(table, row, null, null, null));
List<TableRow> foreignTableRows;
try {
foreignTableRows = getForeignTableRows(tableRows, new HashSet<TableRow>());
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
if (foreignTableRows.isEmpty()) {
log.info("Could not determine foreign table rows to fix foreign key violation for " + "nodeId '{}' dataId '{}' table '{}'", nodeId, dataId, data.getTableName());
}
Collections.reverse(foreignTableRows);
Set<TableRow> visited = new HashSet<TableRow>();
for (TableRow foreignTableRow : foreignTableRows) {
if (visited.add(foreignTableRow)) {
Table foreignTable = foreignTableRow.getTable();
String catalog = foreignTable.getCatalog();
String schema = foreignTable.getSchema();
if (StringUtils.equals(platform.getDefaultCatalog(), catalog)) {
catalog = null;
}
if (StringUtils.equals(platform.getDefaultSchema(), schema)) {
schema = null;
}
log.info("Issuing foreign key correction reload " + "nodeId {} catalog '{}' schema '{}' foreign table name '{}' fk name '{}' where sql '{}' " + "to correct dataId '{}' table '{}' for column '{}'", nodeId, catalog, schema, foreignTable.getName(), foreignTableRow.getFkName(), foreignTableRow.getWhereSql(), dataId, data.getTableName(), foreignTableRow.getReferenceColumnName());
reloadTable(nodeId, catalog, schema, foreignTable.getName(), foreignTableRow.getWhereSql());
}
}
} catch (Exception e) {
log.error("Unknown exception while processing foreign key for node id: " + nodeId + " data id " + dataId, e);
}
}
use of org.jumpmind.symmetric.model.TriggerHistory in project symmetric-ds by JumpMind.
the class DataService method sendSchema.
public boolean sendSchema(String nodeId, String catalogName, String schemaName, String tableName, boolean isLoad) {
Node sourceNode = engine.getNodeService().findIdentity();
Node targetNode = engine.getNodeService().findNode(nodeId);
if (targetNode == null) {
log.error("Could not send schema to the node {}. The target node does not exist", nodeId);
return false;
}
ITriggerRouterService triggerRouterService = engine.getTriggerRouterService();
List<TriggerHistory> triggerHistories = triggerRouterService.findTriggerHistories(catalogName, schemaName, tableName);
Map<Integer, List<TriggerRouter>> triggerRoutersByHistoryId = triggerRouterService.fillTriggerRoutersByHistIdAndSortHist(sourceNode.getNodeGroupId(), targetNode.getNodeGroupId(), triggerHistories);
int eventCount = 0;
for (TriggerHistory triggerHistory : triggerHistories) {
List<TriggerRouter> triggerRouters = triggerRoutersByHistoryId.get(triggerHistory.getTriggerHistoryId());
for (TriggerRouter triggerRouter : triggerRouters) {
eventCount++;
insertCreateEvent(targetNode, triggerHistory, triggerRouter.getRouter().getRouterId(), false, -1, null);
}
}
if (eventCount > 0) {
return true;
} else {
return false;
}
}
use of org.jumpmind.symmetric.model.TriggerHistory 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();
}
}
}
}
}
}
}
Aggregations