use of org.jumpmind.symmetric.model.Node 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.model.Node in project symmetric-ds by JumpMind.
the class WildcardTest method test.
@Override
protected void test(ISymmetricEngine rootServer, ISymmetricEngine clientServer) throws Exception {
for (int i = 0; i < 10; i++) {
rootServer.getSqlTemplate().update("insert into a (id, notes) values(?,?)", i, "test 12345");
}
for (int i = 0; i < 100; i++) {
rootServer.getSqlTemplate().update("insert into b (id, notes) values(?,?)", i, "test 54321");
}
for (int i = 0; i < 5; i++) {
rootServer.getSqlTemplate().update("insert into c (id, notes) values(?,?)", i, "no sync");
}
loadConfigAtRegistrationServer();
assertEquals(0, clientServer.getSqlTemplate().queryForInt("select count(*) from a"));
assertEquals(0, clientServer.getSqlTemplate().queryForInt("select count(*) from b"));
assertEquals(0, clientServer.getSqlTemplate().queryForInt("select count(*) from c"));
rootServer.openRegistration("client", "client");
pull("client");
Node clientNode = clientServer.getNodeService().findIdentity();
assertNotNull(clientNode);
rootServer.getDataService().reloadNode(clientNode.getNodeId(), false, "unit test");
pull("client");
// load succeeded
assertEquals(10, clientServer.getSqlTemplate().queryForInt("select count(*) from a"));
assertEquals(100, clientServer.getSqlTemplate().queryForInt("select count(*) from b"));
// c was excluded
assertEquals(0, clientServer.getSqlTemplate().queryForInt("select count(*) from c"));
for (int i = 100; i < 200; i++) {
rootServer.getSqlTemplate().update("insert into b (id, notes) values(?,?)", i, "test 54321");
}
for (int i = 5; i < 10; i++) {
rootServer.getSqlTemplate().update("insert into c (id, notes) values(?,?)", i, "no sync");
}
pull("client");
assertEquals(200, clientServer.getSqlTemplate().queryForInt("select count(*) from b"));
assertEquals(0, clientServer.getSqlTemplate().queryForInt("select count(*) from c"));
}
use of org.jumpmind.symmetric.model.Node in project symmetric-ds by JumpMind.
the class SymmetricAdmin method sendSql.
private void sendSql(CommandLine line, List<String> args) {
String tableName = popArg(args, "Table Name");
String sql = popArg(args, "SQL");
String catalogName = line.getOptionValue(OPTION_CATALOG);
String schemaName = line.getOptionValue(OPTION_SCHEMA);
for (Node node : getNodes(line)) {
System.out.println("Sending SQL to node '" + node.getNodeId() + "'");
getSymmetricEngine().getDataService().sendSQL(node.getNodeId(), catalogName, schemaName, tableName, sql);
}
}
use of org.jumpmind.symmetric.model.Node in project symmetric-ds by JumpMind.
the class SymmetricAdmin method reloadTable.
private void reloadTable(CommandLine line, List<String> args) {
String catalogName = line.getOptionValue(OPTION_CATALOG);
String schemaName = line.getOptionValue(OPTION_SCHEMA);
if (args.size() == 0) {
System.out.println("ERROR: Expected argument for: Table Name");
System.exit(1);
}
for (String tableName : args) {
for (Node node : getNodes(line)) {
System.out.println("Reloading table '" + tableName + "' to node '" + node.getNodeId() + "'");
if (line.hasOption(OPTION_WHERE)) {
System.out.println(getSymmetricEngine().getDataService().reloadTable(node.getNodeId(), catalogName, schemaName, tableName, line.getOptionValue(OPTION_WHERE)));
} else {
System.out.println(getSymmetricEngine().getDataService().reloadTable(node.getNodeId(), catalogName, schemaName, tableName));
}
}
}
}
use of org.jumpmind.symmetric.model.Node in project symmetric-ds by JumpMind.
the class AbstractSymmetricEngine method isConfigured.
public boolean isConfigured() {
boolean configurationValid = false;
boolean isRegistrationServer = getNodeService().isRegistrationServer();
boolean isSelfConfigurable = isRegistrationServer && (getParameterService().is(ParameterConstants.AUTO_INSERT_REG_SVR_IF_NOT_FOUND, false) || StringUtils.isNotBlank(getParameterService().getString(ParameterConstants.AUTO_CONFIGURE_REG_SVR_SQL_SCRIPT)));
Table symNodeTable = symmetricDialect.getPlatform().readTableFromDatabase(null, null, TableConstants.getTableName(parameterService.getTablePrefix(), TableConstants.SYM_NODE));
Node node = symNodeTable != null ? getNodeService().findIdentity() : null;
long offlineNodeDetectionPeriodSeconds = getParameterService().getLong(ParameterConstants.OFFLINE_NODE_DETECTION_PERIOD_MINUTES) * 60;
long heartbeatSeconds = getParameterService().getLong(ParameterConstants.HEARTBEAT_SYNC_ON_PUSH_PERIOD_SEC);
String registrationUrl = getParameterService().getRegistrationUrl();
if (!isSelfConfigurable && node == null && isRegistrationServer) {
log.warn("This node is configured as a registration server, but it is missing its node_identity. It probably needs configured.", ParameterConstants.REGISTRATION_URL);
} else if (!isSelfConfigurable && node == null && StringUtils.isBlank(getParameterService().getRegistrationUrl())) {
log.warn("Please set the property {} so this node may pull registration or manually insert configuration into the configuration tables", ParameterConstants.REGISTRATION_URL);
} else if (Constants.PLEASE_SET_ME.equals(registrationUrl)) {
log.warn("Please set the registration.url for the node");
} else if (Constants.PLEASE_SET_ME.equals(getParameterService().getNodeGroupId())) {
log.warn("Please set the group.id for the node");
} else if (Constants.PLEASE_SET_ME.equals(getParameterService().getExternalId())) {
log.warn("Please set the external.id for the node");
} else if (offlineNodeDetectionPeriodSeconds > 0 && offlineNodeDetectionPeriodSeconds <= heartbeatSeconds) {
// Offline node detection is not disabled (-1) and the value is too
// small (less than the heartbeat)
log.warn("The {} property must be a longer period of time than the {} property. Otherwise, nodes will be taken offline before the heartbeat job has a chance to run", ParameterConstants.OFFLINE_NODE_DETECTION_PERIOD_MINUTES, ParameterConstants.HEARTBEAT_SYNC_ON_PUSH_PERIOD_SEC);
} else if (node != null && Version.isOlderMinorVersion(Version.version(), node.getSymmetricVersion())) {
log.warn("SymmetricDS does not support automatic downgrading. The current version running version of {} is older than the last running version of {}", Version.version(), node.getSymmetricVersion());
} else {
if (node != null && Version.isOlderMinorVersion(node.getSymmetricVersion(), Version.version())) {
log.debug("The current version of {} is newer than the last running version of {}", Version.version(), node.getSymmetricVersion());
}
configurationValid = true;
}
return configurationValid;
}
Aggregations