Search in sources :

Example 1 with Batch

use of org.jumpmind.symmetric.web.rest.model.Batch 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));
}
Also used : RegistrationInfo(org.jumpmind.symmetric.web.rest.model.RegistrationInfo) NotAllowedException(org.jumpmind.symmetric.web.rest.NotAllowedException) Node(org.jumpmind.symmetric.model.Node) Channel(org.jumpmind.symmetric.model.Channel) ISymmetricEngine(org.jumpmind.symmetric.ISymmetricEngine) IParameterService(org.jumpmind.symmetric.service.IParameterService) PullDataResults(org.jumpmind.symmetric.web.rest.model.PullDataResults) Batch(org.jumpmind.symmetric.web.rest.model.Batch) INodeService(org.jumpmind.symmetric.service.INodeService) RestService(org.jumpmind.symmetric.web.rest.RestService)

Example 2 with Batch

use of org.jumpmind.symmetric.web.rest.model.Batch in project symmetric-ds by JumpMind.

the class RestService method getPullData.

@ApiOperation(value = "Pull pending batches for the specified node for the specified engine")
@RequestMapping(value = "/engine/{engine}/pulldata", method = RequestMethod.GET)
@ResponseStatus(HttpStatus.OK)
@ResponseBody
public final PullDataResults getPullData(@PathVariable("engine") String engineName, @RequestParam(value = WebConstants.NODE_ID) String nodeId, @ApiParam(value = "This the password for the nodeId being passed in.  The password is stored in the node_security table.") @RequestParam(value = WebConstants.SECURITY_TOKEN) String securityToken, @RequestParam(value = "useJdbcTimestampFormat", required = false, defaultValue = "true") boolean useJdbcTimestampFormat, @RequestParam(value = "useUpsertStatements", required = false, defaultValue = "false") boolean useUpsertStatements, @RequestParam(value = "useDelimitedIdentifiers", required = false, defaultValue = "true") boolean useDelimitedIdentifiers, @RequestParam(value = "hostName", required = false) String hostName) {
    ISymmetricEngine engine = getSymmetricEngine(engineName);
    IDataExtractorService dataExtractorService = engine.getDataExtractorService();
    IStatisticManager statisticManager = engine.getStatisticManager();
    INodeService nodeService = engine.getNodeService();
    org.jumpmind.symmetric.model.Node targetNode = nodeService.findNode(nodeId);
    if (securityVerified(nodeId, engine, securityToken)) {
        ProcessInfo processInfo = statisticManager.newProcessInfo(new ProcessInfoKey(nodeService.findIdentityNodeId(), nodeId, ProcessType.REST_PULL_HANLDER));
        try {
            PullDataResults results = new PullDataResults();
            List<OutgoingBatchWithPayload> extractedBatches = dataExtractorService.extractToPayload(processInfo, targetNode, PayloadType.SQL, useJdbcTimestampFormat, useUpsertStatements, useDelimitedIdentifiers);
            List<Batch> batches = new ArrayList<Batch>();
            for (OutgoingBatchWithPayload outgoingBatchWithPayload : extractedBatches) {
                if (outgoingBatchWithPayload.getStatus() == org.jumpmind.symmetric.model.OutgoingBatch.Status.LD || outgoingBatchWithPayload.getStatus() == org.jumpmind.symmetric.model.OutgoingBatch.Status.IG) {
                    Batch batch = new Batch();
                    batch.setBatchId(outgoingBatchWithPayload.getBatchId());
                    batch.setChannelId(outgoingBatchWithPayload.getChannelId());
                    batch.setSqlStatements(outgoingBatchWithPayload.getPayload());
                    batches.add(batch);
                }
            }
            results.setBatches(batches);
            results.setNbrBatches(batches.size());
            processInfo.setStatus(org.jumpmind.symmetric.model.ProcessInfo.Status.OK);
            if (engine.getParameterService().is(ParameterConstants.REST_HEARTBEAT_ON_PULL) && hostName != null) {
                Heartbeat heartbeat = new Heartbeat();
                heartbeat.setNodeId(nodeId);
                heartbeat.setHeartbeatTime(new Date());
                heartbeat.setHostName(hostName);
                this.heartbeatImpl(engine, heartbeat);
            }
            return results;
        } finally {
            if (processInfo.getStatus() != org.jumpmind.symmetric.model.ProcessInfo.Status.OK) {
                processInfo.setStatus(org.jumpmind.symmetric.model.ProcessInfo.Status.ERROR);
            }
        }
    } else {
        throw new NotAllowedException();
    }
}
Also used : OutgoingBatchWithPayload(org.jumpmind.symmetric.model.OutgoingBatchWithPayload) ArrayList(java.util.ArrayList) ISymmetricEngine(org.jumpmind.symmetric.ISymmetricEngine) ProcessInfoKey(org.jumpmind.symmetric.model.ProcessInfoKey) ProcessInfo(org.jumpmind.symmetric.model.ProcessInfo) Date(java.util.Date) IStatisticManager(org.jumpmind.symmetric.statistic.IStatisticManager) PullDataResults(org.jumpmind.symmetric.web.rest.model.PullDataResults) IncomingBatch(org.jumpmind.symmetric.model.IncomingBatch) Batch(org.jumpmind.symmetric.web.rest.model.Batch) OutgoingBatch(org.jumpmind.symmetric.model.OutgoingBatch) INodeService(org.jumpmind.symmetric.service.INodeService) Heartbeat(org.jumpmind.symmetric.web.rest.model.Heartbeat) IDataExtractorService(org.jumpmind.symmetric.service.IDataExtractorService) ResponseStatus(org.springframework.web.bind.annotation.ResponseStatus) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Example 3 with Batch

use of org.jumpmind.symmetric.web.rest.model.Batch in project symmetric-ds by JumpMind.

the class RestServiceTest method buildBatchResults.

protected BatchResults buildBatchResults(RegistrationInfo registrationInfo, PullDataResults results) {
    BatchResults batchResults = new BatchResults();
    batchResults.setNodeId(registrationInfo.getNodeId());
    for (Batch batch : results.getBatches()) {
        batchResults.getBatchResults().add(new BatchResult(batch.getBatchId(), true));
    }
    return batchResults;
}
Also used : Batch(org.jumpmind.symmetric.web.rest.model.Batch) BatchResults(org.jumpmind.symmetric.web.rest.model.BatchResults) BatchResult(org.jumpmind.symmetric.web.rest.model.BatchResult)

Aggregations

Batch (org.jumpmind.symmetric.web.rest.model.Batch)3 ISymmetricEngine (org.jumpmind.symmetric.ISymmetricEngine)2 INodeService (org.jumpmind.symmetric.service.INodeService)2 PullDataResults (org.jumpmind.symmetric.web.rest.model.PullDataResults)2 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Channel (org.jumpmind.symmetric.model.Channel)1 IncomingBatch (org.jumpmind.symmetric.model.IncomingBatch)1 Node (org.jumpmind.symmetric.model.Node)1 OutgoingBatch (org.jumpmind.symmetric.model.OutgoingBatch)1 OutgoingBatchWithPayload (org.jumpmind.symmetric.model.OutgoingBatchWithPayload)1 ProcessInfo (org.jumpmind.symmetric.model.ProcessInfo)1 ProcessInfoKey (org.jumpmind.symmetric.model.ProcessInfoKey)1 IDataExtractorService (org.jumpmind.symmetric.service.IDataExtractorService)1 IParameterService (org.jumpmind.symmetric.service.IParameterService)1 IStatisticManager (org.jumpmind.symmetric.statistic.IStatisticManager)1 NotAllowedException (org.jumpmind.symmetric.web.rest.NotAllowedException)1 RestService (org.jumpmind.symmetric.web.rest.RestService)1 BatchResult (org.jumpmind.symmetric.web.rest.model.BatchResult)1