use of org.jumpmind.symmetric.model.TriggerHistory 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();
}
}
}
}
}
}
}
use of org.jumpmind.symmetric.model.TriggerHistory in project symmetric-ds by JumpMind.
the class DataService method sendSQL.
public String sendSQL(String nodeId, String catalogName, String schemaName, String tableName, String sql) {
Node sourceNode = engine.getNodeService().findIdentity();
Node targetNode = engine.getNodeService().findNode(nodeId);
if (targetNode == null) {
return "Unknown node " + nodeId;
}
ITriggerRouterService triggerRouterService = engine.getTriggerRouterService();
TriggerHistory triggerHistory = triggerRouterService.findTriggerHistory(catalogName, schemaName, tableName);
if (triggerHistory == null) {
return "Trigger for table " + tableName + " does not exist from node " + sourceNode.getNodeGroupId();
} else {
Trigger trigger = triggerRouterService.getTriggerById(triggerHistory.getTriggerId());
if (trigger != null) {
ISqlTransaction transaction = null;
try {
transaction = sqlTemplate.startSqlTransaction();
insertSqlEvent(transaction, triggerHistory, trigger.getChannelId(), targetNode, sql, false, -1, null);
transaction.commit();
return "Successfully create SQL event for node " + targetNode.getNodeId();
} catch (Error ex) {
if (transaction != null) {
transaction.rollback();
}
throw ex;
} catch (RuntimeException ex) {
if (transaction != null) {
transaction.rollback();
}
throw ex;
} finally {
close(transaction);
}
} else {
return "Trigger for table " + tableName + " does not exist from node " + sourceNode.getNodeGroupId();
}
}
}
use of org.jumpmind.symmetric.model.TriggerHistory in project symmetric-ds by JumpMind.
the class DataService method lookupTriggerHistory.
private TriggerHistory lookupTriggerHistory(Trigger trigger) {
TriggerHistory history = engine.getTriggerRouterService().getNewestTriggerHistoryForTrigger(trigger.getTriggerId(), trigger.getSourceCatalogName(), trigger.getSourceSchemaName(), trigger.getSourceTableName());
if (history == null) {
engine.getTriggerRouterService().syncTriggers();
history = engine.getTriggerRouterService().getNewestTriggerHistoryForTrigger(trigger.getTriggerId(), null, null, null);
}
if (history == null) {
throw new RuntimeException("Cannot find history for trigger " + trigger.getTriggerId() + ", " + trigger.getSourceTableName());
}
return history;
}
use of org.jumpmind.symmetric.model.TriggerHistory in project symmetric-ds by JumpMind.
the class TriggerRouterService method updateOrCreateDatabaseTriggers.
protected void updateOrCreateDatabaseTriggers(Trigger trigger, Table table, StringBuilder sqlBuffer, boolean force, boolean verifyInDatabase, List<TriggerHistory> activeTriggerHistories) {
TriggerHistory newestHistory = null;
TriggerReBuildReason reason = TriggerReBuildReason.NEW_TRIGGERS;
String errorMessage = null;
if (verifyInDatabase) {
Channel channel = configurationService.getChannel(trigger.getChannelId());
if (channel == null) {
errorMessage = String.format("Trigger %s had an unrecognized channel_id of '%s'. Please check to make sure the channel exists. Creating trigger on the '%s' channel", trigger.getTriggerId(), trigger.getChannelId(), Constants.CHANNEL_DEFAULT);
log.error(errorMessage);
trigger.setChannelId(Constants.CHANNEL_DEFAULT);
}
}
try {
boolean foundPk = false;
Column[] columns = trigger.filterExcludedAndIncludedColumns(table.getColumns());
for (Column column : columns) {
foundPk |= column.isPrimaryKey();
if (foundPk) {
break;
}
}
if (!foundPk) {
table = platform.makeAllColumnsPrimaryKeys(table);
}
TriggerHistory latestHistoryBeforeRebuild = getNewestTriggerHistoryForTrigger(trigger.getTriggerId(), trigger.isSourceCatalogNameWildCarded() ? table.getCatalog() : trigger.getSourceCatalogName(), trigger.isSourceSchemaNameWildCarded() ? table.getSchema() : trigger.getSourceSchemaName(), trigger.isSourceTableNameWildCarded() ? table.getName() : trigger.getSourceTableName());
boolean forceRebuildOfTriggers = false;
if (latestHistoryBeforeRebuild == null) {
reason = TriggerReBuildReason.NEW_TRIGGERS;
forceRebuildOfTriggers = true;
} else if (table.calculateTableHashcode() != latestHistoryBeforeRebuild.getTableHash()) {
reason = TriggerReBuildReason.TABLE_SCHEMA_CHANGED;
forceRebuildOfTriggers = true;
} else if (trigger.hasChangedSinceLastTriggerBuild(latestHistoryBeforeRebuild.getCreateTime()) || trigger.toHashedValue() != latestHistoryBeforeRebuild.getTriggerRowHash()) {
reason = TriggerReBuildReason.TABLE_SYNC_CONFIGURATION_CHANGED;
forceRebuildOfTriggers = true;
} else if (symmetricDialect.getTriggerTemplate().toHashedValue() != latestHistoryBeforeRebuild.getTriggerTemplateHash()) {
reason = TriggerReBuildReason.TRIGGER_TEMPLATE_CHANGED;
forceRebuildOfTriggers = true;
} else if (force) {
reason = TriggerReBuildReason.FORCED;
forceRebuildOfTriggers = true;
}
boolean supportsTriggers = symmetricDialect.getPlatform().getDatabaseInfo().isTriggersSupported();
newestHistory = rebuildTriggerIfNecessary(sqlBuffer, forceRebuildOfTriggers, trigger, DataEventType.INSERT, reason, latestHistoryBeforeRebuild, null, trigger.isSyncOnInsert() && supportsTriggers, table, activeTriggerHistories);
newestHistory = rebuildTriggerIfNecessary(sqlBuffer, forceRebuildOfTriggers, trigger, DataEventType.UPDATE, reason, latestHistoryBeforeRebuild, newestHistory, trigger.isSyncOnUpdate() && supportsTriggers, table, activeTriggerHistories);
newestHistory = rebuildTriggerIfNecessary(sqlBuffer, forceRebuildOfTriggers, trigger, DataEventType.DELETE, reason, latestHistoryBeforeRebuild, newestHistory, trigger.isSyncOnDelete() && supportsTriggers, table, activeTriggerHistories);
if (latestHistoryBeforeRebuild != null && newestHistory != null) {
inactivateTriggerHistory(latestHistoryBeforeRebuild);
}
if (newestHistory != null) {
synchronized (activeTriggerHistories) {
activeTriggerHistories.add(newestHistory);
}
newestHistory.setErrorMessage(errorMessage);
if (parameterService.is(ParameterConstants.AUTO_SYNC_TRIGGERS)) {
for (ITriggerCreationListener l : extensionService.getExtensionPointList(ITriggerCreationListener.class)) {
l.triggerCreated(trigger, newestHistory);
}
}
}
} catch (Exception ex) {
log.error(String.format("Failed to create triggers for %s", trigger.qualifiedSourceTableName()), ex);
if (newestHistory != null) {
// Make sure all the triggers are removed from the
// table
symmetricDialect.removeTrigger(null, trigger.getSourceCatalogName(), trigger.getSourceSchemaName(), newestHistory.getNameForInsertTrigger(), trigger.getSourceTableName());
symmetricDialect.removeTrigger(null, trigger.getSourceCatalogName(), trigger.getSourceSchemaName(), newestHistory.getNameForUpdateTrigger(), trigger.getSourceTableName());
symmetricDialect.removeTrigger(null, trigger.getSourceCatalogName(), trigger.getSourceSchemaName(), newestHistory.getNameForDeleteTrigger(), trigger.getSourceTableName());
}
for (ITriggerCreationListener l : extensionService.getExtensionPointList(ITriggerCreationListener.class)) {
l.triggerFailed(trigger, ex);
}
}
}
use of org.jumpmind.symmetric.model.TriggerHistory in project symmetric-ds by JumpMind.
the class TriggerRouterService method syncTriggers.
public void syncTriggers(StringBuilder sqlBuffer, boolean force) {
if ((parameterService.is(ParameterConstants.AUTO_SYNC_TRIGGERS) || isCalledFromSymmetricAdminTool())) {
synchronized (this) {
if (clusterService.lock(ClusterConstants.SYNCTRIGGERS)) {
try {
String additionalMessage = "";
if (isCalledFromSymmetricAdminTool() && !parameterService.is(ParameterConstants.AUTO_SYNC_TRIGGERS)) {
additionalMessage = " " + ParameterConstants.AUTO_SYNC_TRIGGERS + " is set to false, but the sync triggers process will run so that needed changes can be written to a file so they can be applied manually. Once all of the triggers have been successfully applied this process should not show triggers being created";
}
log.info("Synchronizing triggers{}", additionalMessage);
// make sure all tables are freshly read in
platform.resetCachedTableModel();
clearCache();
// make sure channels are read from the database
configurationService.clearCache();
List<Trigger> triggersForCurrentNode = getTriggersForCurrentNode();
boolean createTriggersForTables = false;
String nodeId = nodeService.findIdentityNodeId();
if (StringUtils.isNotBlank(nodeId)) {
NodeSecurity nodeSecurity = nodeService.findNodeSecurity(nodeId);
if (nodeSecurity != null && (nodeSecurity.isInitialLoadEnabled() || nodeSecurity.getInitialLoadTime() == null)) {
createTriggersForTables = parameterService.is(ParameterConstants.TRIGGER_CREATE_BEFORE_INITIAL_LOAD);
if (!createTriggersForTables) {
log.info("Trigger creation has been disabled by " + ParameterConstants.TRIGGER_CREATE_BEFORE_INITIAL_LOAD + " because an initial load is in progress or has not yet been requested");
}
} else {
createTriggersForTables = true;
}
}
if (!createTriggersForTables) {
triggersForCurrentNode.clear();
}
List<TriggerHistory> activeTriggerHistories = getActiveTriggerHistories();
inactivateTriggers(triggersForCurrentNode, sqlBuffer, activeTriggerHistories);
updateOrCreateDatabaseTriggers(triggersForCurrentNode, sqlBuffer, force, true, activeTriggerHistories, true);
resetTriggerRouterCacheByNodeGroupId();
if (createTriggersForTables) {
updateOrCreateDdlTriggers(sqlBuffer);
}
} finally {
clusterService.unlock(ClusterConstants.SYNCTRIGGERS);
log.info("Done synchronizing triggers");
}
} else {
Lock lock = clusterService.findLocks().get(ClusterConstants.SYNCTRIGGERS);
if (lock != null) {
log.info("Sync triggers was locked by the cluster service. The locking server id was: {}. The lock time was: {}", lock.getLockingServerId(), lock.getLockTime());
} else {
log.info("Sync triggers was locked by something but lock details were found");
}
}
}
} else {
log.info("Not synchronizing triggers. {} is set to false", ParameterConstants.AUTO_SYNC_TRIGGERS);
}
}
Aggregations