Search in sources :

Example 1 with ExtractRequest

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

the class DataExtractorService method execute.

/**
 * This is a callback method used by the NodeCommunicationService that
 * extracts an initial load in the background.
 */
public void execute(NodeCommunication nodeCommunication, RemoteNodeStatus status) {
    long ts = System.currentTimeMillis();
    List<ExtractRequest> requests = getExtractRequestsForNode(nodeCommunication.getNodeId());
    /*
         * Process extract requests until it has taken longer than 30 seconds,
         * and then allow the process to return so progress status can be seen.
         */
    for (int i = 0; i < requests.size() && (System.currentTimeMillis() - ts) <= Constants.LONG_OPERATION_THRESHOLD; i++) {
        ExtractRequest request = requests.get(i);
        Node identity = nodeService.findIdentity();
        Node targetNode = nodeService.findNode(nodeCommunication.getNodeId());
        log.info("Extracting batches for request {}. Starting at batch {}.  Ending at batch {}", new Object[] { request.getRequestId(), request.getStartBatchId(), request.getEndBatchId() });
        List<OutgoingBatch> batches = outgoingBatchService.getOutgoingBatchRange(request.getStartBatchId(), request.getEndBatchId()).getBatches();
        ProcessInfo processInfo = statisticManager.newProcessInfo(new ProcessInfoKey(identity.getNodeId(), nodeCommunication.getNodeId(), ProcessType.INITIAL_LOAD_EXTRACT_JOB));
        try {
            boolean areBatchesOk = true;
            /*
                 * check to see if batches have been OK'd by another reload
                 * request
                 */
            for (OutgoingBatch outgoingBatch : batches) {
                if (outgoingBatch.getStatus() != Status.OK) {
                    areBatchesOk = false;
                }
            }
            if (!areBatchesOk) {
                Channel channel = configurationService.getChannel(batches.get(0).getChannelId());
                /*
                     * "Trick" the extractor to extract one reload batch, but we
                     * will split it across the N batches when writing it
                     */
                extractOutgoingBatch(processInfo, targetNode, new MultiBatchStagingWriter(identity.getNodeId(), stagingManager, batches, channel.getMaxBatchSize()), batches.get(0), false, false, ExtractMode.FOR_SYM_CLIENT);
            } else {
                log.info("Batches already had an OK status for request {}, batches {} to {}.  Not extracting", new Object[] { request.getRequestId(), request.getStartBatchId(), request.getEndBatchId() });
            }
            /*
                 * re-query the batches to see if they have been OK'd while
                 * extracting
                 */
            List<OutgoingBatch> checkBatches = outgoingBatchService.getOutgoingBatchRange(request.getStartBatchId(), request.getEndBatchId()).getBatches();
            areBatchesOk = true;
            /*
                 * check to see if batches have been OK'd by another reload
                 * request while extracting
                 */
            for (OutgoingBatch outgoingBatch : checkBatches) {
                if (outgoingBatch.getStatus() != Status.OK) {
                    areBatchesOk = false;
                }
            }
            ISqlTransaction transaction = null;
            try {
                transaction = sqlTemplate.startSqlTransaction();
                updateExtractRequestStatus(transaction, request.getRequestId(), ExtractStatus.OK);
                if (!areBatchesOk) {
                    for (OutgoingBatch outgoingBatch : batches) {
                        outgoingBatch.setStatus(Status.NE);
                        outgoingBatchService.updateOutgoingBatch(transaction, outgoingBatch);
                    }
                } else {
                    log.info("Batches already had an OK status for request {}, batches {} to {}.  Not updating the status to NE", new Object[] { request.getRequestId(), request.getStartBatchId(), request.getEndBatchId() });
                }
                transaction.commit();
            } catch (Error ex) {
                if (transaction != null) {
                    transaction.rollback();
                }
                throw ex;
            } catch (RuntimeException ex) {
                if (transaction != null) {
                    transaction.rollback();
                }
                throw ex;
            } finally {
                close(transaction);
            }
            processInfo.setStatus(org.jumpmind.symmetric.model.ProcessInfo.Status.OK);
        } catch (RuntimeException ex) {
            log.debug("Failed to extract batches for request {}. Starting at batch {}.  Ending at batch {}", new Object[] { request.getRequestId(), request.getStartBatchId(), request.getEndBatchId() });
            processInfo.setStatus(org.jumpmind.symmetric.model.ProcessInfo.Status.ERROR);
            throw ex;
        }
    }
}
Also used : Node(org.jumpmind.symmetric.model.Node) NodeChannel(org.jumpmind.symmetric.model.NodeChannel) Channel(org.jumpmind.symmetric.model.Channel) ProcessInfoKey(org.jumpmind.symmetric.model.ProcessInfoKey) ProcessInfo(org.jumpmind.symmetric.model.ProcessInfo) ExtractRequest(org.jumpmind.symmetric.model.ExtractRequest) TransformPoint(org.jumpmind.symmetric.io.data.transform.TransformPoint) ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) OutgoingBatch(org.jumpmind.symmetric.model.OutgoingBatch)

Aggregations

ISqlTransaction (org.jumpmind.db.sql.ISqlTransaction)1 TransformPoint (org.jumpmind.symmetric.io.data.transform.TransformPoint)1 Channel (org.jumpmind.symmetric.model.Channel)1 ExtractRequest (org.jumpmind.symmetric.model.ExtractRequest)1 Node (org.jumpmind.symmetric.model.Node)1 NodeChannel (org.jumpmind.symmetric.model.NodeChannel)1 OutgoingBatch (org.jumpmind.symmetric.model.OutgoingBatch)1 ProcessInfo (org.jumpmind.symmetric.model.ProcessInfo)1 ProcessInfoKey (org.jumpmind.symmetric.model.ProcessInfoKey)1