Search in sources :

Example 51 with TriggerHistory

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

the class DataService method insertLoadBatchesForReload.

private void insertLoadBatchesForReload(Node targetNode, long loadId, String createBy, List<TriggerHistory> triggerHistories, Map<Integer, List<TriggerRouter>> triggerRoutersByHistoryId, boolean transactional, ISqlTransaction transaction) {
    Map<String, Channel> channels = engine.getConfigurationService().getChannels(false);
    DatabaseInfo dbInfo = platform.getDatabaseInfo();
    String quote = dbInfo.getDelimiterToken();
    String catalogSeparator = dbInfo.getCatalogSeparator();
    String schemaSeparator = dbInfo.getSchemaSeparator();
    for (TriggerHistory triggerHistory : triggerHistories) {
        List<TriggerRouter> triggerRouters = triggerRoutersByHistoryId.get(triggerHistory.getTriggerHistoryId());
        for (TriggerRouter triggerRouter : triggerRouters) {
            if (triggerRouter.getInitialLoadOrder() >= 0 && engine.getGroupletService().isTargetEnabled(triggerRouter, targetNode)) {
                if (parameterService.is(ParameterConstants.INITIAL_LOAD_USE_EXTRACT_JOB)) {
                    Trigger trigger = triggerRouter.getTrigger();
                    String reloadChannel = getReloadChannelIdForTrigger(trigger, channels);
                    Channel channel = channels.get(reloadChannel);
                    // calculate the number of batches needed for table.
                    int numberOfBatches = triggerRouter.getInitialLoadBatchCount();
                    if (numberOfBatches <= 0) {
                        Table table = platform.getTableFromCache(triggerHistory.getSourceCatalogName(), triggerHistory.getSourceSchemaName(), triggerHistory.getSourceTableName(), false);
                        String sql = String.format("select count(*) from %s where %s", table.getQualifiedTableName(quote, catalogSeparator, schemaSeparator), StringUtils.isBlank(triggerRouter.getInitialLoadSelect()) ? Constants.ALWAYS_TRUE_CONDITION : triggerRouter.getInitialLoadSelect());
                        sql = FormatUtils.replace("groupId", targetNode.getNodeGroupId(), sql);
                        sql = FormatUtils.replace("externalId", targetNode.getExternalId(), sql);
                        sql = FormatUtils.replace("nodeId", targetNode.getNodeId(), sql);
                        int rowCount = sqlTemplate.queryForInt(sql);
                        if (rowCount > 0) {
                            numberOfBatches = (rowCount / channel.getMaxBatchSize()) + 1;
                        } else {
                            numberOfBatches = 1;
                        }
                    }
                    long startBatchId = -1;
                    long endBatchId = -1;
                    for (int i = 0; i < numberOfBatches; i++) {
                        // needs to grab the start and end batch id
                        endBatchId = insertReloadEvent(transaction, targetNode, triggerRouter, triggerHistory, null, true, loadId, createBy, Status.RQ);
                        if (startBatchId == -1) {
                            startBatchId = endBatchId;
                        }
                    }
                    engine.getDataExtractorService().requestExtractRequest(transaction, targetNode.getNodeId(), triggerRouter, startBatchId, endBatchId);
                } else {
                    insertReloadEvent(transaction, targetNode, triggerRouter, triggerHistory, null, true, loadId, createBy, Status.NE);
                }
                if (!transactional) {
                    transaction.commit();
                }
            }
        }
    }
}
Also used : Trigger(org.jumpmind.symmetric.model.Trigger) Table(org.jumpmind.db.model.Table) DatabaseInfo(org.jumpmind.db.platform.DatabaseInfo) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) Channel(org.jumpmind.symmetric.model.Channel) TriggerRouter(org.jumpmind.symmetric.model.TriggerRouter)

Example 52 with TriggerHistory

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

the class DataService method insertSqlEvent.

public void insertSqlEvent(Node targetNode, String sql, boolean isLoad, long loadId, String createBy) {
    TriggerHistory history = engine.getTriggerRouterService().findTriggerHistoryForGenericSync();
    Trigger trigger = engine.getTriggerRouterService().getTriggerById(true, history.getTriggerId(), false);
    String reloadChannelId = getReloadChannelIdForTrigger(trigger, engine.getConfigurationService().getChannels(false));
    Data data = new Data(history.getSourceTableName(), DataEventType.SQL, CsvUtils.escapeCsvData(sql), null, history, isLoad ? reloadChannelId : Constants.CHANNEL_CONFIG, null, null);
    if (isLoad) {
        insertDataAndDataEventAndOutgoingBatch(data, targetNode.getNodeId(), Constants.UNKNOWN_ROUTER_ID, isLoad, loadId, createBy);
    } else {
        data.setNodeList(targetNode.getNodeId());
        insertData(data);
    }
}
Also used : Trigger(org.jumpmind.symmetric.model.Trigger) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) Data(org.jumpmind.symmetric.model.Data) CsvData(org.jumpmind.symmetric.io.data.CsvData)

Example 53 with TriggerHistory

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(true, 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();
        }
    }
}
Also used : ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) Trigger(org.jumpmind.symmetric.model.Trigger) ITriggerRouterService(org.jumpmind.symmetric.service.ITriggerRouterService) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) Node(org.jumpmind.symmetric.model.Node)

Example 54 with TriggerHistory

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) {
    long dataId = insertData(transaction, data);
    String channelId = null;
    if (isLoad) {
        TriggerHistory history = data.getTriggerHistory();
        if (history != null && channelId == null) {
            Trigger trigger = engine.getTriggerRouterService().getTriggerById(true, 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);
}
Also used : Trigger(org.jumpmind.symmetric.model.Trigger) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory)

Example 55 with TriggerHistory

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

the class NonDmlEventsTest method test.

@Override
protected void test(ISymmetricEngine rootServer, ISymmetricEngine clientServer) throws Exception {
    loadConfigAndRegisterNode("client", "root");
    // pull to clear out any heartbeat events
    pull("client");
    List<TriggerHistory> histories = rootServer.getTriggerRouterService().findTriggerHistories(null, null, testTable.getName());
    assertNotNull(histories);
    assertEquals(1, histories.size());
    String serverQuote = rootServer.getDatabasePlatform().getDatabaseInfo().getDelimiterToken();
    String clientQuote = clientServer.getDatabasePlatform().getDatabaseInfo().getDelimiterToken();
    for (int i = 0; i < 100; i++) {
        rootServer.getSqlTemplate().update(String.format("insert into %sCamelCase%s values (?,?)", serverQuote, serverQuote), i, "this is a test");
    }
    String serverCountSql = String.format("select count(*) from %sCamelCase%s", serverQuote, serverQuote);
    String clientCountSql = String.format("select count(*) from %sCamelCase%s", clientQuote, clientQuote);
    assertEquals(100, rootServer.getDatabasePlatform().getSqlTemplate().queryForInt(serverCountSql));
    assertEquals(0, clientServer.getDatabasePlatform().getSqlTemplate().queryForInt(clientCountSql));
    // we installed a dead trigger, so no data should have been captured
    assertFalse(pull("client"));
    rootServer.getDataService().reloadTable("client", null, null, testTable.getName());
    assertTrue(pull("client"));
    assertEquals(100, clientServer.getDatabasePlatform().getSqlTemplate().queryForInt(clientCountSql));
    rootServer.getDataService().sendSQL("client", null, null, testTable.getName(), String.format("insert into %sCamelCase%s values (101,'direct insert')", clientQuote, clientQuote));
    rootServer.getDataService().sendSQL("client", null, null, testTable.getName(), String.format("insert into %sCamelCase%s values (102,'direct insert')", clientQuote, clientQuote));
    assertTrue(pull("client"));
    assertEquals(102, clientServer.getDatabasePlatform().getSqlTemplate().queryForInt(clientCountSql));
    rootServer.getDataService().sendSQL("client", null, null, testTable.getName(), String.format("delete from %sCamelCase%s", clientQuote, clientQuote));
    assertTrue(pull("client"));
    assertEquals(0, clientServer.getDatabasePlatform().getSqlTemplate().queryForInt(clientCountSql));
    rootServer.getDataService().reloadTable("client", null, null, testTable.getName(), String.format("%sId%s < 50", serverQuote, serverQuote));
    assertTrue(pull("client"));
    assertEquals(50, clientServer.getDatabasePlatform().getSqlTemplate().queryForInt(clientCountSql));
    Table serverTable = rootServer.getDatabasePlatform().readTableFromDatabase(null, null, "A");
    Table clientTable = clientServer.getDatabasePlatform().readTableFromDatabase(null, null, "A");
    // test a wildcard table
    for (int i = 0; i < 10; i++) {
        rootServer.getSqlTemplate().update(String.format("insert into %s values (?)", serverTable.getName()), i);
    }
    assertFalse(pull("client"));
    String msg = rootServer.getDataService().reloadTable("client", null, null, "A");
    assertTrue("Should have pulled data for the reload event for table A.  The reload table method returned the following text: " + msg, pull("client"));
    assertEquals(10, clientServer.getDatabasePlatform().getSqlTemplate().queryForInt(String.format("select count(*) from %s", clientTable.getName())));
    testRoutingOfReloadEvents(rootServer, clientServer);
}
Also used : Table(org.jumpmind.db.model.Table) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory)

Aggregations

TriggerHistory (org.jumpmind.symmetric.model.TriggerHistory)55 Table (org.jumpmind.db.model.Table)20 Data (org.jumpmind.symmetric.model.Data)19 Node (org.jumpmind.symmetric.model.Node)17 Trigger (org.jumpmind.symmetric.model.Trigger)15 TriggerRouter (org.jumpmind.symmetric.model.TriggerRouter)15 DataMetaData (org.jumpmind.symmetric.model.DataMetaData)14 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)11 HashSet (java.util.HashSet)11 Router (org.jumpmind.symmetric.model.Router)10 NodeChannel (org.jumpmind.symmetric.model.NodeChannel)9 ITriggerRouterService (org.jumpmind.symmetric.service.ITriggerRouterService)9 List (java.util.List)7 CsvData (org.jumpmind.symmetric.io.data.CsvData)6 ISqlTransaction (org.jumpmind.db.sql.ISqlTransaction)5 Channel (org.jumpmind.symmetric.model.Channel)5 TableReloadRequest (org.jumpmind.symmetric.model.TableReloadRequest)5 HashMap (java.util.HashMap)4 Date (java.util.Date)3