Search in sources :

Example 36 with Trigger

use of org.jumpmind.symmetric.model.Trigger 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);
}
Also used : Table(org.jumpmind.db.model.Table) Set(java.util.Set) HashSet(java.util.HashSet) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Trigger(org.jumpmind.symmetric.model.Trigger) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future)

Example 37 with Trigger

use of org.jumpmind.symmetric.model.Trigger in project symmetric-ds by JumpMind.

the class TriggerRouterService method buildTriggerForSymmetricTable.

protected Trigger buildTriggerForSymmetricTable(String tableName) {
    boolean syncChanges = !TableConstants.getTablesThatDoNotSync(tablePrefix).contains(tableName) && parameterService.is(ParameterConstants.AUTO_SYNC_CONFIGURATION);
    boolean syncOnIncoming = !configurationService.isMasterToMaster() && (parameterService.is(ParameterConstants.AUTO_SYNC_CONFIGURATION_ON_INCOMING, true) || tableName.equals(TableConstants.getTableName(tablePrefix, TableConstants.SYM_TABLE_RELOAD_REQUEST)));
    Trigger trigger = new Trigger();
    trigger.setUseHandleKeyUpdates(false);
    trigger.setTriggerId(tableName);
    trigger.setSyncOnDelete(syncChanges);
    trigger.setSyncOnInsert(syncChanges);
    trigger.setSyncOnUpdate(syncChanges);
    trigger.setSyncOnIncomingBatch(syncOnIncoming);
    trigger.setSourceTableName(tableName);
    trigger.setUseCaptureOldData(false);
    if (TableConstants.getTableName(tablePrefix, TableConstants.SYM_NODE_HOST).equals(tableName) || TableConstants.getTableName(tablePrefix, TableConstants.SYM_MONITOR_EVENT).equals(tableName)) {
        trigger.setChannelId(Constants.CHANNEL_HEARTBEAT);
    } else if (TableConstants.getTableName(tablePrefix, TableConstants.SYM_FILE_SNAPSHOT).equals(tableName)) {
        trigger.setChannelId(Constants.CHANNEL_DYNAMIC);
        trigger.setChannelExpression("$(curTriggerValue).$(curColumnPrefix)" + platform.alterCaseToMatchDatabaseDefaultCase("channel_id"));
        trigger.setReloadChannelId(Constants.CHANNEL_FILESYNC_RELOAD);
        trigger.setUseCaptureOldData(true);
        trigger.setSyncOnIncomingBatch(false);
        boolean syncEnabled = parameterService.is(ParameterConstants.FILE_SYNC_ENABLE);
        trigger.setSyncOnInsert(syncEnabled);
        // Changed to false because of issues with the traffic file
        trigger.setSyncOnUpdate(syncEnabled);
        trigger.setSyncOnDelete(false);
    } else {
        trigger.setChannelId(Constants.CHANNEL_CONFIG);
    }
    if (TableConstants.getTableName(tablePrefix, TableConstants.SYM_MONITOR_EVENT).equals(tableName) && !parameterService.is(ParameterConstants.MONITOR_EVENTS_CAPTURE_ENABLED)) {
        trigger.setSyncOnInsert(false);
        trigger.setSyncOnUpdate(false);
        trigger.setSyncOnDelete(false);
    }
    if (!TableConstants.getTableName(tablePrefix, TableConstants.SYM_NODE_HOST).equals(tableName) && !TableConstants.getTableName(tablePrefix, TableConstants.SYM_NODE).equals(tableName) && !TableConstants.getTableName(tablePrefix, TableConstants.SYM_NODE_SECURITY).equals(tableName) && !TableConstants.getTableName(tablePrefix, TableConstants.SYM_TABLE_RELOAD_REQUEST).equals(tableName)) {
        trigger.setUseCaptureLobs(true);
    }
    // little trick to force the rebuild of SymmetricDS triggers every time
    // there is a new version of SymmetricDS
    trigger.setLastUpdateTime(new Date(Version.version().hashCode()));
    return trigger;
}
Also used : Trigger(org.jumpmind.symmetric.model.Trigger) Date(java.util.Date)

Example 38 with Trigger

use of org.jumpmind.symmetric.model.Trigger in project symmetric-ds by JumpMind.

the class AbstractTriggerRouterServiceTest method test13BinaryColumnTypesForDerby.

@Test
public void test13BinaryColumnTypesForDerby() {
    ISymmetricDialect dialect = getDbDialect();
    if (DatabaseNamesConstants.DERBY.equals(dialect.getName())) {
        try {
            getSqlTemplate().update("drop table test_derby_binary_types");
        } catch (Exception e) {
        }
        getSqlTemplate().update("create table test_derby_binary_types (id integer, data VARCHAR (100) FOR BIT DATA, data2 CHAR(12) FOR BIT DATA)");
        TriggerRouter trouter = new TriggerRouter();
        Trigger trigger = trouter.getTrigger();
        trigger.setSourceTableName("test_derby_binary_types");
        trigger.setChannelId(TestConstants.TEST_CHANNEL_ID);
        Router router = trouter.getRouter();
        router.getNodeGroupLink().setSourceNodeGroupId(TestConstants.TEST_ROOT_NODE_GROUP);
        router.getNodeGroupLink().setTargetNodeGroupId(TestConstants.TEST_ROOT_NODE_GROUP);
        getTriggerRouterService().saveTriggerRouter(trouter);
        ITriggerRouterService triggerService = getTriggerRouterService();
        triggerService.syncTriggers();
        Assert.assertEquals("Some triggers must have failed to build.", 0, triggerService.getFailedTriggers().size());
        getSqlTemplate().update("insert into test_derby_binary_types values (?, ?, ?)", new Object[] { 23, "test 1 2 3".getBytes(), "test 1 2 3".getBytes() });
        String csvString = getNextDataRow();
        Assert.assertEquals("\"23\",\"dGVzdCAxIDIgMw==\",\"dGVzdCAxIDIgMyAg\"", csvString);
    }
}
Also used : ISymmetricDialect(org.jumpmind.symmetric.db.ISymmetricDialect) Trigger(org.jumpmind.symmetric.model.Trigger) ITriggerRouterService(org.jumpmind.symmetric.service.ITriggerRouterService) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) Router(org.jumpmind.symmetric.model.Router) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) Test(org.junit.Test)

Example 39 with Trigger

use of org.jumpmind.symmetric.model.Trigger in project symmetric-ds by JumpMind.

the class AbstractTriggerRouterServiceTest method test06InitialLoadSql.

@Test
public void test06InitialLoadSql() throws Exception {
    ITriggerRouterService triggerRouterService = getTriggerRouterService();
    IParameterService parameterService = getParameterService();
    parameterService.saveParameter(ParameterConstants.INITIAL_LOAD_CONCAT_CSV_IN_SQL_ENABLED, true, "unittest");
    TriggerRouter triggerRouter = triggerRouterService.getTriggerRouterForTableForCurrentNode(null, null, TEST_TRIGGERS_TABLE, true).iterator().next();
    Table table = getDbDialect().getPlatform().getTableFromCache(triggerRouter.getTrigger().getSourceTableName(), true);
    Trigger trigger = triggerRouter.getTrigger();
    String sql = getDbDialect().createInitialLoadSqlFor(new Node("1", null, "1.0"), triggerRouter, table, triggerRouterService.getNewestTriggerHistoryForTrigger(trigger.getTriggerId(), trigger.getSourceCatalogName(), trigger.getSourceSchemaName(), trigger.getSourceTableName()), getConfigurationService().getChannel(triggerRouter.getTrigger().getChannelId()), null);
    List<String> csvStrings = getSqlTemplate().query(sql, new StringMapper());
    assertTrue(csvStrings.size() > 0);
    String csvString = csvStrings.get(0);
    // DB2 captures decimal differently
    csvString = csvString.replaceFirst("\"00001\\.\"", "\"1\"");
    // Informix captures decimal differently
    csvString = csvString.replaceFirst("\"1.0000000000000000\"", "\"1\"");
    // ASA captures decimal differently
    csvString = csvString.replaceFirst("\"1.000000\"", "\"1\"");
    assertTrue(csvString.endsWith(EXPECTED_INSERT1_CSV_ENDSWITH), "Received " + csvString + ", Expected the string to end with " + EXPECTED_INSERT1_CSV_ENDSWITH);
}
Also used : StringMapper(org.jumpmind.db.sql.mapper.StringMapper) Table(org.jumpmind.db.model.Table) Trigger(org.jumpmind.symmetric.model.Trigger) ITriggerRouterService(org.jumpmind.symmetric.service.ITriggerRouterService) Node(org.jumpmind.symmetric.model.Node) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) IParameterService(org.jumpmind.symmetric.service.IParameterService) Test(org.junit.Test)

Example 40 with Trigger

use of org.jumpmind.symmetric.model.Trigger in project symmetric-ds by JumpMind.

the class RouterServiceTest method testNotProducesCommonBatchesSameTablesTwoChannelsMultipleRoutersSameTableIncomingOnAnotherChannel.

@Test
public void testNotProducesCommonBatchesSameTablesTwoChannelsMultipleRoutersSameTableIncomingOnAnotherChannel() {
    List<TriggerRouter> triggerRouters = new ArrayList<TriggerRouter>();
    Trigger tableTrigger1 = new Trigger("a", CHANNEL_2_TEST.getChannelId(), true);
    Trigger tableTrigger2 = new Trigger("a", "anotherchannel");
    triggerRouters.add(new TriggerRouter(tableTrigger1, new Router("test", SOURCE_NODE_GROUP, TARGET_NODE_GROUP, "default")));
    triggerRouters.add(new TriggerRouter(tableTrigger2, new Router("test", TARGET_NODE_GROUP, SOURCE_NODE_GROUP, "default")));
    assertTrue(!routerService.producesCommonBatches(CHANNEL_2_TEST, SOURCE_NODE_GROUP, triggerRouters));
}
Also used : Trigger(org.jumpmind.symmetric.model.Trigger) ArrayList(java.util.ArrayList) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) Router(org.jumpmind.symmetric.model.Router) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter) Test(org.junit.Test)

Aggregations

Trigger (org.jumpmind.symmetric.model.Trigger)42 TriggerRouter (org.jumpmind.symmetric.model.TriggerRouter)20 ArrayList (java.util.ArrayList)17 TriggerHistory (org.jumpmind.symmetric.model.TriggerHistory)14 Router (org.jumpmind.symmetric.model.Router)13 ITriggerRouterService (org.jumpmind.symmetric.service.ITriggerRouterService)11 Test (org.junit.Test)11 Date (java.util.Date)5 Table (org.jumpmind.db.model.Table)5 CsvData (org.jumpmind.symmetric.io.data.CsvData)5 Data (org.jumpmind.symmetric.model.Data)5 Node (org.jumpmind.symmetric.model.Node)5 HashSet (java.util.HashSet)4 ISqlTransaction (org.jumpmind.db.sql.ISqlTransaction)4 HashMap (java.util.HashMap)3 Set (java.util.Set)3 ISymmetricDialect (org.jumpmind.symmetric.db.ISymmetricDialect)3 NodeGroupLink (org.jumpmind.symmetric.model.NodeGroupLink)3 INodeService (org.jumpmind.symmetric.service.INodeService)3 List (java.util.List)2