use of org.jumpmind.symmetric.model.TriggerHistory in project symmetric-ds by JumpMind.
the class TriggerRouterService method syncTrigger.
public void syncTrigger(Trigger trigger, ITriggerCreationListener listener, boolean force, boolean verifyInDatabase) {
StringBuilder sqlBuffer = new StringBuilder();
clearCache();
List<Trigger> triggersForCurrentNode = null;
if (verifyInDatabase) {
triggersForCurrentNode = getTriggersForCurrentNode();
} else {
triggersForCurrentNode = new ArrayList<Trigger>();
triggersForCurrentNode.add(trigger);
}
try {
if (listener != null) {
extensionService.addExtensionPoint(listener);
}
List<TriggerHistory> allHistories = getActiveTriggerHistories();
if (triggersForCurrentNode.contains(trigger)) {
if (!trigger.isSourceTableNameWildCarded()) {
for (TriggerHistory triggerHistory : getActiveTriggerHistories(trigger)) {
if (!triggerHistory.getFullyQualifiedSourceTableName().equals(trigger.getFullyQualifiedSourceTableName())) {
dropTriggers(triggerHistory, sqlBuffer);
}
}
}
updateOrCreateDatabaseTrigger(trigger, triggersForCurrentNode, sqlBuffer, force, verifyInDatabase, allHistories, false);
} else {
for (TriggerHistory triggerHistory : getActiveTriggerHistories(trigger)) {
dropTriggers(triggerHistory, sqlBuffer);
}
}
} finally {
if (listener != null) {
extensionService.removeExtensionPoint(listener);
}
}
}
use of org.jumpmind.symmetric.model.TriggerHistory in project symmetric-ds by JumpMind.
the class TriggerRouterService method fillTriggerRoutersByHistId.
public Map<Integer, List<TriggerRouter>> fillTriggerRoutersByHistId(String sourceNodeGroupId, String targetNodeGroupId, List<TriggerHistory> triggerHistories) {
List<TriggerRouter> triggerRouters = new ArrayList<TriggerRouter>(getAllTriggerRoutersForReloadForCurrentNode(sourceNodeGroupId, targetNodeGroupId));
Map<Integer, List<TriggerRouter>> triggerRoutersByHistoryId = new HashMap<Integer, List<TriggerRouter>>(triggerHistories.size());
for (TriggerHistory triggerHistory : triggerHistories) {
List<TriggerRouter> triggerRoutersForTriggerHistory = new ArrayList<TriggerRouter>();
triggerRoutersByHistoryId.put(triggerHistory.getTriggerHistoryId(), triggerRoutersForTriggerHistory);
String triggerId = triggerHistory.getTriggerId();
for (TriggerRouter triggerRouter : triggerRouters) {
if (triggerRouter.getTrigger().getTriggerId().equals(triggerId)) {
triggerRoutersForTriggerHistory.add(triggerRouter);
}
}
}
return triggerRoutersByHistoryId;
}
use of org.jumpmind.symmetric.model.TriggerHistory in project symmetric-ds by JumpMind.
the class TriggerRouterService method rebuildTriggerIfNecessary.
protected TriggerHistory rebuildTriggerIfNecessary(StringBuilder sqlBuffer, boolean forceRebuild, Trigger trigger, DataEventType dmlType, TriggerReBuildReason reason, TriggerHistory oldhist, TriggerHistory hist, boolean triggerIsActive, Table table, List<TriggerHistory> activeTriggerHistories) {
boolean triggerExists = false;
boolean triggerRemoved = false;
TriggerHistory newTriggerHist = new TriggerHistory(table, trigger, symmetricDialect.getTriggerTemplate(), reason);
int maxTriggerNameLength = symmetricDialect.getMaxTriggerNameLength();
if (trigger.isSyncOnInsert()) {
newTriggerHist.setNameForInsertTrigger(getTriggerName(DataEventType.INSERT, maxTriggerNameLength, trigger, table, activeTriggerHistories).toUpperCase());
}
if (trigger.isSyncOnUpdate()) {
newTriggerHist.setNameForUpdateTrigger(getTriggerName(DataEventType.UPDATE, maxTriggerNameLength, trigger, table, activeTriggerHistories).toUpperCase());
}
if (trigger.isSyncOnDelete()) {
newTriggerHist.setNameForDeleteTrigger(getTriggerName(DataEventType.DELETE, maxTriggerNameLength, trigger, table, activeTriggerHistories).toUpperCase());
}
String oldTriggerName = null;
String oldSourceSchema = null;
String oldCatalogName = null;
if (oldhist != null) {
oldTriggerName = oldhist.getTriggerNameForDmlType(dmlType);
oldSourceSchema = oldhist.getSourceSchemaName();
oldCatalogName = oldhist.getSourceCatalogName();
triggerExists = symmetricDialect.doesTriggerExist(oldCatalogName, oldSourceSchema, oldhist.getSourceTableName(), oldTriggerName);
} else {
// We had no trigger_hist row, lets validate that the trigger as
// defined in the trigger row data does not exist as well.
oldTriggerName = newTriggerHist.getTriggerNameForDmlType(dmlType);
oldSourceSchema = table.getSchema();
oldCatalogName = table.getCatalog();
if (StringUtils.isNotBlank(oldTriggerName)) {
triggerExists = symmetricDialect.doesTriggerExist(oldCatalogName, oldSourceSchema, table.getName(), oldTriggerName);
}
}
if (!triggerExists && forceRebuild) {
reason = TriggerReBuildReason.TRIGGERS_MISSING;
}
if ((forceRebuild || !triggerIsActive) && triggerExists) {
symmetricDialect.removeTrigger(sqlBuffer, oldCatalogName, oldSourceSchema, oldTriggerName, trigger.isSourceTableNameWildCarded() ? table.getName() : trigger.getSourceTableName());
triggerExists = false;
triggerRemoved = true;
}
boolean isDeadTrigger = !trigger.isSyncOnInsert() && !trigger.isSyncOnUpdate() && !trigger.isSyncOnDelete();
if (hist == null && (oldhist == null || (!triggerExists && triggerIsActive) || (isDeadTrigger && forceRebuild))) {
insert(newTriggerHist);
hist = getNewestTriggerHistoryForTrigger(trigger.getTriggerId(), trigger.isSourceCatalogNameWildCarded() ? table.getCatalog() : trigger.getSourceCatalogName(), trigger.isSourceSchemaNameWildCarded() ? table.getSchema() : trigger.getSourceSchemaName(), trigger.isSourceTableNameWildCarded() ? table.getName() : trigger.getSourceTableName());
}
try {
if (!triggerExists && triggerIsActive) {
symmetricDialect.createTrigger(sqlBuffer, dmlType, trigger, hist, configurationService.getChannel(trigger.getChannelId()), tablePrefix, table);
if (triggerRemoved) {
statisticManager.incrementTriggersRebuiltCount(1);
} else {
statisticManager.incrementTriggersCreatedCount(1);
}
} else if (triggerRemoved) {
statisticManager.incrementTriggersRemovedCount(1);
}
} catch (RuntimeException ex) {
if (hist != null) {
log.info("Cleaning up trigger hist row of {} after failing to create the associated trigger", hist.getTriggerHistoryId());
hist.setErrorMessage(ex.getMessage());
inactivateTriggerHistory(hist);
}
throw ex;
}
return hist;
}
use of org.jumpmind.symmetric.model.TriggerHistory in project symmetric-ds by JumpMind.
the class TriggerRouterService method inactivateTriggers.
protected void inactivateTriggers(final List<Trigger> triggersThatShouldBeActive, final StringBuilder sqlBuffer, List<TriggerHistory> activeTriggerHistories) {
final boolean ignoreCase = this.parameterService.is(ParameterConstants.DB_METADATA_IGNORE_CASE);
final Map<String, Set<Table>> tablesByTriggerId = new HashMap<String, Set<Table>>();
int numThreads = getNumberOfThreadsToUseForSyncTriggers();
ExecutorService executor = Executors.newFixedThreadPool(numThreads, new SyncTriggersThreadFactory());
List<Future<?>> futures = new ArrayList<Future<?>>();
for (final TriggerHistory history : activeTriggerHistories) {
Runnable runnable = new Runnable() {
public void run() {
boolean removeTrigger = false;
Set<Table> tables = tablesByTriggerId.get(history.getTriggerId());
Trigger trigger = getTriggerFromList(history.getTriggerId(), triggersThatShouldBeActive);
if (tables == null && trigger != null) {
tables = getTablesForTrigger(trigger, triggersThatShouldBeActive, false);
tablesByTriggerId.put(trigger.getTriggerId(), tables);
}
if (tables == null || tables.size() == 0 || trigger == null) {
removeTrigger = true;
} else {
boolean foundTable = false;
for (Table table : tables) {
boolean matchesCatalog = isEqual(trigger.isSourceCatalogNameWildCarded() ? table.getCatalog() : trigger.getSourceCatalogName(), history.getSourceCatalogName(), ignoreCase);
boolean matchesSchema = isEqual(trigger.isSourceSchemaNameWildCarded() ? table.getSchema() : trigger.getSourceSchemaName(), history.getSourceSchemaName(), ignoreCase);
boolean matchesTable = isEqual(trigger.isSourceTableNameWildCarded() ? table.getName() : trigger.getSourceTableName(), history.getSourceTableName(), ignoreCase);
foundTable |= matchesCatalog && matchesSchema && matchesTable;
}
if (!foundTable) {
removeTrigger = true;
}
}
if (removeTrigger) {
log.info("About to remove triggers for inactivated table: {}", history.getFullyQualifiedSourceTableName());
dropTriggers(history, sqlBuffer);
}
}
};
futures.add(executor.submit(runnable));
}
awaitTermination(executor, futures);
}
use of org.jumpmind.symmetric.model.TriggerHistory in project symmetric-ds by JumpMind.
the class TriggerRouterService method findTriggerHistoryForGenericSync.
public TriggerHistory findTriggerHistoryForGenericSync() {
String triggerTableName = TableConstants.getTableName(tablePrefix, TableConstants.SYM_NODE);
TriggerHistory history = findTriggerHistory(null, null, triggerTableName.toUpperCase());
if (history == null) {
history = findTriggerHistory(null, null, triggerTableName);
}
return history;
}
Aggregations