use of org.jumpmind.symmetric.service.IParameterService in project symmetric-ds by JumpMind.
the class AbstractTriggerTemplate method createInitalLoadSql.
public String createInitalLoadSql(Node node, TriggerRouter triggerRouter, Table originalTable, TriggerHistory triggerHistory, Channel channel, String overrideSelectSql) {
IParameterService parameterService = symmetricDialect.getParameterService();
boolean dateTimeAsString = parameterService.is(ParameterConstants.DATA_LOADER_TREAT_DATETIME_AS_VARCHAR);
boolean concatInCsv = parameterService.is(ParameterConstants.INITIAL_LOAD_CONCAT_CSV_IN_SQL_ENABLED);
Table table = originalTable.copyAndFilterColumns(triggerHistory.getParsedColumnNames(), triggerHistory.getParsedPkColumnNames(), true);
Column[] columns = table.getColumns();
String textColumnExpression = parameterService.getString(ParameterConstants.DATA_EXTRACTOR_TEXT_COLUMN_EXPRESSION);
String sql = null;
String tableAlias = symmetricDialect.getInitialLoadTableAlias();
if (concatInCsv) {
sql = sqlTemplates.get(INITIAL_LOAD_SQL_TEMPLATE);
String columnsText = buildColumnsString(tableAlias, tableAlias, "", table, columns, DataEventType.INSERT, false, channel, triggerRouter.getTrigger()).columnString;
if (isNotBlank(textColumnExpression)) {
columnsText = textColumnExpression.replace("$(columnName)", columnsText);
}
sql = FormatUtils.replace("columns", columnsText, sql);
} else {
sql = "select $(columns) from $(schemaName)$(tableName) t where $(whereClause)";
StringBuilder columnList = new StringBuilder();
for (int i = 0; i < columns.length; i++) {
Column column = columns[i];
if (column != null) {
if (i > 0) {
columnList.append(",");
}
boolean isLob = symmetricDialect.getPlatform().isLob(column.getMappedTypeCode());
if (!(isLob && triggerRouter.getTrigger().isUseStreamLobs())) {
String columnExpression = null;
if (useTriggerTemplateForColumnTemplatesDuringInitialLoad()) {
ColumnString columnString = fillOutColumnTemplate(tableAlias, tableAlias, "", table, column, DataEventType.INSERT, false, channel, triggerRouter.getTrigger());
columnExpression = columnString.columnString;
if (isNotBlank(textColumnExpression) && TypeMap.isTextType(column.getMappedTypeCode())) {
columnExpression = textColumnExpression.replace("$(columnName)", columnExpression);
}
} else {
columnExpression = SymmetricUtils.quote(symmetricDialect, column.getName());
if (dateTimeAsString && TypeMap.isDateTimeType(column.getMappedTypeCode())) {
columnExpression = castDatetimeColumnToString(column.getName());
} else if (isNotBlank(textColumnExpression) && TypeMap.isTextType(column.getMappedTypeCode())) {
columnExpression = textColumnExpression.replace("$(columnName)", columnExpression);
}
}
columnList.append(columnExpression).append(" as ").append("x__").append(i);
} else {
columnList.append(" ").append(emptyColumnTemplate).append(" as ").append("x__").append(i);
}
}
}
sql = FormatUtils.replace("columns", columnList.toString(), sql);
}
String initialLoadSelect = StringUtils.isBlank(triggerRouter.getInitialLoadSelect()) ? Constants.ALWAYS_TRUE_CONDITION : triggerRouter.getInitialLoadSelect();
if (StringUtils.isNotBlank(overrideSelectSql)) {
initialLoadSelect = overrideSelectSql;
}
sql = FormatUtils.replace("whereClause", initialLoadSelect, sql);
sql = FormatUtils.replace("tableName", SymmetricUtils.quote(symmetricDialect, table.getName()), sql);
sql = FormatUtils.replace("schemaName", triggerHistory == null ? getSourceTablePrefix(originalTable) : getSourceTablePrefix(triggerHistory), sql);
sql = FormatUtils.replace("primaryKeyWhereString", getPrimaryKeyWhereString(symmetricDialect.getInitialLoadTableAlias(), table.hasPrimaryKey() ? table.getPrimaryKeyColumns() : table.getColumns()), sql);
// Replace these parameters to give the initiaLoadContition a chance to
// reference the node that is being loaded
sql = FormatUtils.replace("groupId", node.getNodeGroupId(), sql);
sql = FormatUtils.replace("externalId", node.getExternalId(), sql);
sql = FormatUtils.replace("nodeId", node.getNodeId(), sql);
sql = replaceDefaultSchemaAndCatalog(sql);
sql = FormatUtils.replace("prefixName", symmetricDialect.getTablePrefix(), sql);
sql = FormatUtils.replace("oracleToClob", triggerRouter.getTrigger().isUseCaptureLobs() ? toClobExpression(table) : "", sql);
return sql;
}
use of org.jumpmind.symmetric.service.IParameterService in project symmetric-ds by JumpMind.
the class RestServiceTest method testRestPullApi.
protected void testRestPullApi() throws Exception {
loadConfigAtRegistrationServer();
RestService restService = getRegServer().getRestService();
ISymmetricEngine engine = getRegServer().getEngine();
IParameterService parameterService = engine.getParameterService();
INodeService nodeService = engine.getNodeService();
parameterService.saveParameter(ParameterConstants.REST_API_ENABLED, Boolean.TRUE, "unit_test");
assertNotNull("Could not find the rest service in the application context", restService);
List<Node> nodes = nodeService.findAllNodes();
assertEquals("Expected there to only be one node registered", 1, nodes.size());
assertEquals("The only node we expected to be registered is a server node", "server", nodes.get(0).getNodeGroupId());
RegistrationInfo registrationInfo = restService.postRegisterNode("client", "client", DatabaseNamesConstants.SQLITE, "3.0", "hostName");
assertNotNull("Registration should have returned a result object", registrationInfo);
assertFalse("Registration should not have been open", registrationInfo.isRegistered());
assertEquals("Expected there to only be one node registered", 1, nodes.size());
engine.openRegistration("client", "client");
registrationInfo = restService.postRegisterNode("client", "client", DatabaseNamesConstants.SQLITE, "3.0", "hostName");
assertNotNull("Registration should have returned a result object", registrationInfo);
assertTrue("Registration should have been open", registrationInfo.isRegistered());
assertEquals("client", registrationInfo.getNodeId());
try {
restService.getPullData(registrationInfo.getNodeId(), "wrong password", false, false, true, null);
fail("We should have received an exception");
} catch (NotAllowedException ex) {
}
PullDataResults results = null;
assertPullReturnsNoData(restService, registrationInfo);
engine.getSqlTemplate().update("insert into a values(?, ?, ?)", 1, "this is a test", FormatUtils.parseDate("2013-06-08 00:00:00.000", FormatUtils.TIMESTAMP_PATTERNS));
engine.route();
results = restService.getPullData("server", registrationInfo.getNodeId(), registrationInfo.getNodePassword(), false, false, true, null);
assertNotNull("Should have a non null results object", results);
assertEquals(1, results.getNbrBatches());
assertEquals(4, results.getBatches().get(0).getBatchId());
log.info(results.getBatches().get(0).getSqlStatements().get(0));
// pull a second time without acking. should get the same results
results = restService.getPullData("server", registrationInfo.getNodeId(), registrationInfo.getNodePassword(), false, false, false, null);
assertNotNull("Should have a non null results object", results);
assertEquals(1, results.getNbrBatches());
// test that when we don't request jdbc timestamp format sql statements come back in that format
assertFalse(results.getBatches().get(0).getSqlStatements().get(0).contains("{ts '"));
// make sure we have no delimited identifiers
assertFalse(results.getBatches().get(0).getSqlStatements().get(0).contains("\""));
engine.getSqlTemplate().update("update a set notes=? where id=?", "changed", 1);
engine.getSqlTemplate().update("update a set notes=? where id=?", "changed again", 1);
engine.route();
results = restService.getPullData("server", registrationInfo.getNodeId(), registrationInfo.getNodePassword(), true, false, true, null);
assertNotNull("Should have a non null results object", results);
assertEquals(2, results.getNbrBatches());
assertNotSame(results.getBatches().get(1).getBatchId(), results.getBatches().get(0).getBatchId());
assertEquals(2, results.getBatches().get(1).getSqlStatements().size());
// test that when we request jdbc timestamp format sql statements come back in that format
String testSql = results.getBatches().get(1).getSqlStatements().get(0);
assertTrue("The following sql was supposed to contain '{ts '" + testSql, testSql.contains("{ts '"));
// make sure we have delimited identifiers
assertTrue(results.getBatches().get(1).getSqlStatements().get(0).contains("\""));
log.info(results.getBatches().get(1).getSqlStatements().get(0));
log.info(results.getBatches().get(1).getSqlStatements().get(1));
ackBatches(restService, registrationInfo, results, buildBatchResults(registrationInfo, results));
engine.getSqlTemplate().update("insert into a values(?, ?, ?)", 2, "this is a test", FormatUtils.parseDate("2073-06-08 00:00:00.000", FormatUtils.TIMESTAMP_PATTERNS));
engine.getSqlTemplate().update("insert into a values(?, ?, ?)", 3, "this is a test", FormatUtils.parseDate("2073-06-08 00:00:00.000", FormatUtils.TIMESTAMP_PATTERNS));
engine.getSqlTemplate().update("update a set notes=? where id=?", "update to 2", 2);
engine.getSqlTemplate().update("update a set notes=? where id=?", "update to 3", 3);
engine.getSqlTemplate().update("update a set notes=? where id=?", "update 2 again", 2);
engine.route();
results = restService.getPullData("server", registrationInfo.getNodeId(), registrationInfo.getNodePassword(), false, true, true, null);
assertNotNull("Should have a non null results object", results);
assertEquals(1, results.getNbrBatches());
List<String> sqls = results.getBatches().get(0).getSqlStatements();
assertEquals(5, sqls.size());
for (String sql : sqls) {
log.info(sql);
assertTrue(sql, sql.toLowerCase().startsWith("insert or replace"));
}
ackBatches(restService, registrationInfo, results, buildBatchResults(registrationInfo, results));
Channel channel = engine.getConfigurationService().getChannel("default");
channel.setBatchAlgorithm("nontransactional");
channel.setMaxBatchSize(1);
engine.getConfigurationService().saveChannel(channel, true);
engine.getSqlTemplate().update("delete from a");
engine.route();
results = restService.getPullData("server", registrationInfo.getNodeId(), registrationInfo.getNodePassword(), false, false, true, null);
assertNotNull("Should have a non null results object", results);
assertEquals(3, results.getNbrBatches());
List<Batch> batches = results.getBatches();
for (Batch batch : batches) {
assertEquals(1, batch.getSqlStatements().size());
assertTrue(batch.getSqlStatements().get(0).toLowerCase().startsWith("delete from"));
}
ackBatches(restService, registrationInfo, results, buildBatchResults(registrationInfo, results));
}
use of org.jumpmind.symmetric.service.IParameterService in project symmetric-ds by JumpMind.
the class DbFillCommand method executeWithOptions.
@Override
protected boolean executeWithOptions(CommandLine line) throws Exception {
DbFill dbFill = new DbFill(getDatabasePlatform(false));
if (line.hasOption(OPTION_SCHEMA)) {
dbFill.setSchema(line.getOptionValue(OPTION_SCHEMA));
}
if (line.hasOption(OPTION_CATALOG)) {
dbFill.setCatalog(line.getOptionValue(OPTION_CATALOG));
}
if (line.hasOption(OPTION_COUNT)) {
dbFill.setRecordCount(Integer.parseInt(line.getOptionValue(OPTION_COUNT)));
}
if (line.hasOption(OPTION_CASCADE)) {
dbFill.setCascading(true);
}
if (line.hasOption(OPTION_CASCADE_SELECT)) {
dbFill.setCascadingSelect(true);
}
if (line.hasOption(OPTION_INTERVAL)) {
dbFill.setInterval(Integer.parseInt(line.getOptionValue(OPTION_INTERVAL)));
}
if (line.hasOption(OPTION_WEIGHTS)) {
int[] dmlWeight = { 0, 0, 0 };
String[] strWeight = line.getOptionValue(OPTION_WEIGHTS).split(",");
if (strWeight != null && strWeight.length == 3) {
for (int i = 0; i < 3; i++) {
dmlWeight[i] = new Integer(strWeight[i]);
}
dbFill.setDmlWeight(dmlWeight);
}
}
if (line.hasOption(OPTION_DEBUG)) {
dbFill.setDebug(true);
}
if (line.hasOption(OPTION_VERBOSE_CONSOLE)) {
dbFill.setVerbose(true);
}
String[] ignore = null;
if (line.hasOption(OPTION_IGNORE_TABLES)) {
ignore = line.getOptionValue(OPTION_IGNORE_TABLES).split(",");
}
String[] prefixed = null;
if (line.hasOption(OPTION_PREFIXED_TABLES)) {
prefixed = line.getOptionValue(OPTION_PREFIXED_TABLES).split(",");
}
if (line.hasOption(OPTION_CONTINUE)) {
dbFill.setContinueOnError(true);
}
if (line.hasOption(OPTION_PRINT)) {
dbFill.setPrint(true);
}
if (line.hasOption(OPTION_RAND)) {
dbFill.setUseRandomCount(true);
}
if (line.hasOption(OPTION_REPEAT)) {
dbFill.setRepeat(Integer.parseInt(line.getOptionValue(OPTION_REPEAT)));
}
if (line.hasOption(OPTION_COMMIT)) {
dbFill.setMaxRowsCommit(Integer.parseInt(line.getOptionValue(OPTION_COMMIT)));
}
if (line.hasOption(OPTION_COMMIT_DELAY)) {
dbFill.setCommitDelay(Integer.parseInt(line.getOptionValue(OPTION_COMMIT_DELAY)));
}
if (line.hasOption(OPTION_ROLLBACK)) {
dbFill.setPercentRollback(Integer.parseInt(line.getOptionValue(OPTION_ROLLBACK)));
}
// Ignore the Symmetric config tables.
getSymmetricEngine();
IParameterService parameterService = engine.getParameterService();
String cfgPrefix = parameterService.getString(ParameterConstants.RUNTIME_CONFIG_TABLE_PREFIX);
dbFill.setIgnore((String[]) ArrayUtils.add(ignore, cfgPrefix));
dbFill.setPrefixed(prefixed);
Map<String, int[]> tableProperties = parseTableProperties();
// If tables are provided in the property file, ignore the tables provided at the command line.
String[] tableNames = null;
if (tableProperties.size() != 0) {
tableNames = tableProperties.keySet().toArray(new String[0]);
} else {
tableNames = line.getArgs();
}
if (!dbFill.getPrint()) {
dbFill.fillTables(tableNames, tableProperties);
} else {
for (String tableName : tableNames) {
Table table = platform.readTableFromDatabase(dbFill.getCatalogToUse(), dbFill.getSchemaToUse(), tableName);
if (table != null) {
for (int i = 0; i < dbFill.getRecordCount(); i++) {
for (int j = 0; j < dbFill.getInsertWeight(); j++) {
String sql = dbFill.createDynamicRandomInsertSql(table);
System.out.println(sql);
}
for (int j = 0; j < dbFill.getUpdateWeight(); j++) {
String sql = dbFill.createDynamicRandomUpdateSql(table);
System.out.println(sql);
}
for (int j = 0; j < dbFill.getDeleteWeight(); j++) {
String sql = dbFill.createDynamicRandomDeleteSql(table);
System.out.println(sql);
}
}
}
}
}
return true;
}
use of org.jumpmind.symmetric.service.IParameterService in project symmetric-ds by JumpMind.
the class ConfigurationChangedDatabaseWriterFilter method syncEnded.
public void syncEnded(DataContext context, List<IncomingBatch> batchesProcessed, Throwable ex) {
IParameterService parameterService = engine.getParameterService();
if (context.get(CTX_KEY_FLUSH_TRANSFORMS_NEEDED) != null) {
log.info("About to refresh the cache of transformation because new configuration came through the data loader");
engine.getTransformService().clearCache();
log.info("About to clear the staging area because new transform configuration came through the data loader");
engine.getStagingManager().clean(0);
context.remove(CTX_KEY_FLUSH_TRANSFORMS_NEEDED);
}
if (context.get(CTX_KEY_RESTART_JOBMANAGER_NEEDED) != null) {
IJobManager jobManager = engine.getJobManager();
if (jobManager != null && jobManager.isStarted()) {
log.info("About to restart jobs because a new schedule came through the data loader");
jobManager.stopJobs();
jobManager.startJobs();
}
context.remove(CTX_KEY_RESTART_JOBMANAGER_NEEDED);
}
/**
* No need to sync triggers until the entire sync process has finished just in case there
* are multiple batches that contain configuration changes
*/
if (context.get(CTX_KEY_RESYNC_NEEDED) != null && parameterService.is(ParameterConstants.AUTO_SYNC_TRIGGERS)) {
log.info("About to syncTriggers because new configuration came through the data loader");
engine.getTriggerRouterService().syncTriggers();
context.remove(CTX_KEY_RESYNC_NEEDED);
}
if (context.get(CTX_KEY_RESYNC_TABLE_NEEDED) != null && parameterService.is(ParameterConstants.AUTO_SYNC_TRIGGERS)) {
@SuppressWarnings("unchecked") Set<Table> tables = (Set<Table>) context.get(CTX_KEY_RESYNC_TABLE_NEEDED);
for (Table table : tables) {
engine.getTriggerRouterService().syncTriggers(table, false);
}
context.remove(CTX_KEY_RESYNC_TABLE_NEEDED);
}
}
use of org.jumpmind.symmetric.service.IParameterService in project symmetric-ds by JumpMind.
the class PushHeartbeatListener method heartbeat.
public void heartbeat(Node me) {
IParameterService parameterService = engine.getParameterService();
if (parameterService.is(ParameterConstants.HEARTBEAT_ENABLED)) {
ISymmetricDialect symmetricDialect = engine.getSymmetricDialect();
boolean updateWithBatchStatus = parameterService.is(ParameterConstants.HEARTBEAT_UPDATE_NODE_WITH_BATCH_STATUS, false);
int outgoingErrorCount = -1;
int outgoingUnsentCount = -1;
if (updateWithBatchStatus) {
outgoingUnsentCount = engine.getOutgoingBatchService().countOutgoingBatchesUnsent();
outgoingErrorCount = engine.getOutgoingBatchService().countOutgoingBatchesInError();
}
if (!parameterService.getExternalId().equals(me.getExternalId()) || !parameterService.getNodeGroupId().equals(me.getNodeGroupId()) || (parameterService.getSyncUrl() != null && !parameterService.getSyncUrl().equals(me.getSyncUrl())) || !parameterService.getString(ParameterConstants.SCHEMA_VERSION, "").equals(me.getSchemaVersion()) || (engine.getDeploymentType() != null && !engine.getDeploymentType().equals(me.getDeploymentType())) || !Version.version().equals(me.getSymmetricVersion()) || !symmetricDialect.getName().equals(me.getDatabaseType()) || !symmetricDialect.getVersion().equals(me.getDatabaseVersion()) || me.getBatchInErrorCount() != outgoingErrorCount || me.getBatchToSendCount() != outgoingUnsentCount) {
log.info("Some attribute(s) of node changed. Recording changes");
me.setDeploymentType(engine.getDeploymentType());
me.setSymmetricVersion(Version.version());
me.setDatabaseType(symmetricDialect.getName());
me.setDatabaseVersion(symmetricDialect.getVersion());
me.setBatchInErrorCount(outgoingErrorCount);
me.setBatchToSendCount(outgoingUnsentCount);
me.setSchemaVersion(parameterService.getString(ParameterConstants.SCHEMA_VERSION));
if (parameterService.is(ParameterConstants.AUTO_UPDATE_NODE_VALUES)) {
log.info("Updating my node configuration info according to the symmetric properties");
me.setExternalId(parameterService.getExternalId());
me.setNodeGroupId(parameterService.getNodeGroupId());
if (!StringUtils.isBlank(parameterService.getSyncUrl())) {
me.setSyncUrl(parameterService.getSyncUrl());
}
}
engine.getNodeService().save(me);
}
log.debug("Updating my node info");
engine.getOutgoingBatchService().markAllChannelAsSent(Constants.CHANNEL_HEARTBEAT, getTableName());
engine.getNodeService().updateNodeHostForCurrentNode();
log.debug("Done updating my node info");
if (!engine.getNodeService().isRegistrationServer()) {
if (!symmetricDialect.getPlatform().getDatabaseInfo().isTriggersSupported()) {
engine.getDataService().insertHeartbeatEvent(me, false);
Set<Node> children = engine.getNodeService().findNodesThatOriginatedFromNodeId(me.getNodeId());
for (Node node : children) {
engine.getDataService().insertHeartbeatEvent(node, false);
}
}
}
}
}
Aggregations