Search in sources :

Example 1 with TransferEventSuccess

use of org.alfresco.service.cmr.transfer.TransferEventSuccess in project alfresco-repository by Alfresco.

the class TransferServiceCallbackTest method testCompleteSuccess.

public void testCompleteSuccess() {
    TransferProgress status0 = new TransferProgress();
    status0.setStatus(Status.COMMIT_REQUESTED);
    status0.setCurrentPosition(0);
    status0.setEndPosition(0);
    TransferProgress status1 = new TransferProgress();
    status1.setStatus(Status.COMMITTING);
    status1.setCurrentPosition(0);
    status1.setEndPosition(4);
    TransferProgress status2 = new TransferProgress();
    status2.setStatus(Status.COMMITTING);
    status2.setCurrentPosition(3);
    status2.setEndPosition(4);
    TransferProgress status3 = new TransferProgress();
    status3.setStatus(Status.COMMITTING);
    status3.setCurrentPosition(5);
    status3.setEndPosition(8);
    TransferProgress status4 = new TransferProgress();
    status4.setStatus(Status.COMPLETE);
    status4.setCurrentPosition(8);
    status4.setEndPosition(8);
    TransferProgress[] statuses = new TransferProgress[] { status0, status1, status2, status3, status4 };
    configureBasicMockTransmitter(statuses);
    when(mockedTransferTransmitter.begin(target, localRepositoryId, version)).thenReturn(transfer);
    TransferDefinition transferDef = new TransferDefinition();
    transferDef.setNodes(folder1, file1, file2, file3);
    transferService.transfer(TRANSFER_TARGET_NAME, transferDef, mockedCallback);
    List<TransferEvent> expectedEvents = new ArrayList<TransferEvent>();
    TransferEventImpl event;
    event = new TransferEventEnterState();
    event.setTransferState(TransferState.START);
    expectedEvents.add(event);
    event = new TransferEventBegin();
    event.setTransferState(TransferState.START);
    expectedEvents.add(event);
    event = new TransferEventEndState();
    event.setTransferState(TransferState.START);
    expectedEvents.add(event);
    event = new TransferEventEnterState();
    event.setTransferState(TransferState.SENDING_SNAPSHOT);
    expectedEvents.add(event);
    event = new TransferEventSendingSnapshot();
    event.setTransferState(TransferState.SENDING_SNAPSHOT);
    expectedEvents.add(event);
    event = new TransferEventEndState();
    event.setTransferState(TransferState.SENDING_SNAPSHOT);
    expectedEvents.add(event);
    event = new TransferEventEnterState();
    event.setTransferState(TransferState.SENDING_CONTENT);
    expectedEvents.add(event);
    event = new TransferEventSendingContent();
    event.setTransferState(TransferState.SENDING_CONTENT);
    expectedEvents.add(event);
    event = new TransferEventSendingContent();
    event.setTransferState(TransferState.SENDING_CONTENT);
    expectedEvents.add(event);
    event = new TransferEventSendingContent();
    event.setTransferState(TransferState.SENDING_CONTENT);
    expectedEvents.add(event);
    event = new TransferEventEndState();
    event.setTransferState(TransferState.SENDING_CONTENT);
    expectedEvents.add(event);
    event = new TransferEventEnterState();
    event.setTransferState(TransferState.PREPARING);
    expectedEvents.add(event);
    event = new TransferEventEndState();
    event.setTransferState(TransferState.PREPARING);
    expectedEvents.add(event);
    event = new TransferEventEnterState();
    event.setTransferState(TransferState.COMMITTING);
    expectedEvents.add(event);
    event = new TransferEventCommittingStatus();
    event.setTransferState(TransferState.COMMITTING);
    expectedEvents.add(event);
    event = new TransferEventCommittingStatus();
    event.setTransferState(TransferState.COMMITTING);
    expectedEvents.add(event);
    event = new TransferEventCommittingStatus();
    event.setTransferState(TransferState.COMMITTING);
    expectedEvents.add(event);
    event = new TransferEventCommittingStatus();
    event.setTransferState(TransferState.COMMITTING);
    expectedEvents.add(event);
    event = new TransferEventEndState();
    event.setTransferState(TransferState.COMMITTING);
    expectedEvents.add(event);
    event = new TransferEventEnterState();
    event.setTransferState(TransferState.SUCCESS);
    expectedEvents.add(event);
    event = new TransferEventReport();
    event.setTransferState(TransferState.SUCCESS);
    expectedEvents.add(event);
    event = new TransferEventReport();
    event.setTransferState(TransferState.SUCCESS);
    expectedEvents.add(event);
    event = new TransferEventSuccess();
    event.setTransferState(TransferState.SUCCESS);
    expectedEvents.add(event);
    verifyCallback(expectedEvents);
}
Also used : TransferEvent(org.alfresco.service.cmr.transfer.TransferEvent) ArrayList(java.util.ArrayList) TransferEventBegin(org.alfresco.service.cmr.transfer.TransferEventBegin) TransferEventReport(org.alfresco.service.cmr.transfer.TransferEventReport) TransferEventEndState(org.alfresco.service.cmr.transfer.TransferEventEndState) TransferProgress(org.alfresco.service.cmr.transfer.TransferProgress) TransferDefinition(org.alfresco.service.cmr.transfer.TransferDefinition) TransferEventEnterState(org.alfresco.service.cmr.transfer.TransferEventEnterState) TransferEventSendingSnapshot(org.alfresco.service.cmr.transfer.TransferEventSendingSnapshot) TransferEventSuccess(org.alfresco.service.cmr.transfer.TransferEventSuccess) TransferEventCommittingStatus(org.alfresco.service.cmr.transfer.TransferEventCommittingStatus) TransferEventSendingContent(org.alfresco.service.cmr.transfer.TransferEventSendingContent)

Example 2 with TransferEventSuccess

use of org.alfresco.service.cmr.transfer.TransferEventSuccess in project alfresco-repository by Alfresco.

the class TransferServiceImpl2 method transferImpl.

private TransferEndEvent transferImpl(String targetName, final TransferDefinition definition, final TransferEventProcessor eventProcessor) throws TransferFailureException {
    if (logger.isDebugEnabled()) {
        logger.debug("transfer started to :" + targetName);
    }
    // transfer end event
    TransferEndEvent endEvent = null;
    Exception failureException = null;
    TransferTarget target = null;
    Transfer transfer = null;
    final List<TransferEvent> transferReportEvents = new LinkedList<TransferEvent>();
    NodeRef sourceReport = null;
    NodeRef destinationReport = null;
    File manifest = null;
    File requisite = null;
    int pollRetries = 0;
    int pollPosition = -1;
    boolean cancelled = false;
    Descriptor currentDescriptor = descriptorService.getCurrentRepositoryDescriptor();
    Descriptor serverDescriptor = descriptorService.getServerDescriptor();
    final String localRepositoryId = currentDescriptor.getId();
    TransferVersion fromVersion = new TransferVersionImpl(serverDescriptor);
    // Wire in the transferReport - so any callbacks are stored in transferReport
    TransferCallback reportCallback = new TransferCallback() {

        public void processEvent(TransferEvent event) {
            transferReportEvents.add(event);
        }
    };
    eventProcessor.addObserver(reportCallback);
    TransferContext transferContext = new TransferContext();
    // start transfer
    ClientTransferState clientState = ClientTransferState.Begin;
    while (clientState != ClientTransferState.Exit) {
        try {
            switch(clientState) {
                case Begin:
                    {
                        eventProcessor.start();
                        manifest = createManifest(definition, localRepositoryId, fromVersion, transferContext);
                        logger.debug("transfer begin");
                        target = getTransferTarget(targetName);
                        checkTargetEnabled(target);
                        transfer = transmitter.begin(target, localRepositoryId, fromVersion);
                        String transferId = transfer.getTransferId();
                        TransferStatus status = new TransferStatus();
                        transferMonitoring.put(transferId, status);
                        logger.debug("transfer begun transferId:" + transferId);
                        eventProcessor.begin(transferId);
                        checkCancel(transferId);
                        // next state
                        clientState = ClientTransferState.Prepare;
                        break;
                    }
                case Prepare:
                    {
                        // check alfresco versions are compatible
                        TransferVersion toVersion = transfer.getToVersion();
                        if (!this.transferVersionChecker.checkTransferVersions(fromVersion, toVersion)) {
                            throw new TransferException(MSG_INCOMPATIBLE_VERSIONS, new Object[] { transfer.getTransferId(), fromVersion, toVersion });
                        }
                        // send Manifest, get the requsite back.
                        eventProcessor.sendSnapshot(1, 1);
                        requisite = createRequisiteFile();
                        FileOutputStream reqOutput = new FileOutputStream(requisite);
                        transmitter.sendManifest(transfer, manifest, reqOutput);
                        logger.debug("manifest sent");
                        checkCancel(transfer.getTransferId());
                        if (logger.isDebugEnabled()) {
                            logger.debug("requisite file written to local filesystem");
                            try {
                                outputFile(requisite);
                            } catch (IOException error) {
                                // This is debug code - so an exception thrown while debugging
                                logger.debug("error while outputting snapshotFile");
                                error.printStackTrace();
                            }
                        }
                        sendContent(transfer, definition, eventProcessor, manifest, requisite);
                        logger.debug("content sending finished");
                        checkCancel(transfer.getTransferId());
                        // prepare
                        eventProcessor.prepare();
                        transmitter.prepare(transfer);
                        checkCancel(transfer.getTransferId());
                        // next state
                        clientState = ClientTransferState.Commit;
                        break;
                    }
                case Commit:
                    {
                        logger.debug("about to start committing transferId:" + transfer.getTransferId());
                        eventProcessor.commit();
                        transmitter.commit(transfer);
                        logger.debug("committing transferId:" + transfer.getTransferId());
                        checkCancel(transfer.getTransferId());
                        // next state
                        clientState = ClientTransferState.Poll;
                        break;
                    }
                case Poll:
                    {
                        TransferProgress progress = null;
                        try {
                            progress = transmitter.getStatus(transfer);
                            // reset retries for next poll
                            pollRetries = 0;
                        } catch (TransferException e) {
                            pollRetries++;
                            if (pollRetries == 3) {
                                throw new TransferException(MSG_FAILED_TO_GET_TRANSFER_STATUS, new Object[] { target.getName() });
                            }
                        }
                        // check status
                        if (progress.getStatus() == TransferProgress.Status.ERROR) {
                            Throwable targetError = progress.getError();
                            // NOTE: it's possible the error is not returned from pre v3.4 target repositories
                            if (targetError == null) {
                                targetError = new TransferException(MSG_UNKNOWN_TARGET_ERROR);
                            }
                            if (Exception.class.isAssignableFrom(targetError.getClass())) {
                                failureException = (Exception) targetError;
                            } else {
                                failureException = new TransferException(MSG_TARGET_ERROR, new Object[] { targetError.getMessage() }, targetError);
                            }
                            clientState = ClientTransferState.Finished;
                            break;
                        } else if (progress.getStatus() == TransferProgress.Status.CANCELLED) {
                            cancelled = true;
                            clientState = ClientTransferState.Finished;
                            break;
                        }
                        // notify transfer progress
                        if (progress.getCurrentPosition() != pollPosition) {
                            pollPosition = progress.getCurrentPosition();
                            logger.debug("committing :" + pollPosition);
                            eventProcessor.committing(progress.getEndPosition(), pollPosition);
                        }
                        if (progress.getStatus() == TransferProgress.Status.COMPLETE) {
                            clientState = ClientTransferState.Finished;
                            break;
                        }
                        checkCancel(transfer.getTransferId());
                        // sleep before next poll
                        try {
                            Thread.sleep(commitPollDelay);
                        } catch (InterruptedException e) {
                        // carry on
                        }
                        break;
                    }
                case Cancel:
                    {
                        logger.debug("Abort - waiting for target confirmation of cancel");
                        transmitter.abort(transfer);
                        // next state... poll for confirmation of cancel from target
                        clientState = ClientTransferState.Poll;
                        break;
                    }
                case Finished:
                    {
                        try {
                            TransferEndEventImpl endEventImpl = null;
                            String reportName = null;
                            try {
                                if (failureException != null) {
                                    logger.debug("TransferException - unable to transfer", failureException);
                                    TransferEventError errorEvent = new TransferEventError();
                                    errorEvent.setTransferState(TransferEvent.TransferState.ERROR);
                                    errorEvent.setException(failureException);
                                    errorEvent.setMessage(failureException.getMessage());
                                    endEventImpl = errorEvent;
                                    reportName = "error";
                                } else if (cancelled) {
                                    endEventImpl = new TransferEventCancelled();
                                    endEventImpl.setTransferState(TransferEvent.TransferState.CANCELLED);
                                    endEventImpl.setMessage("cancelled");
                                    reportName = "cancelled";
                                } else {
                                    logger.debug("committed transferId:" + transfer.getTransferId());
                                    endEventImpl = new TransferEventSuccess();
                                    endEventImpl.setTransferState(TransferEvent.TransferState.SUCCESS);
                                    endEventImpl.setMessage("success");
                                    reportName = "success";
                                }
                                // manually add the terminal event to the transfer report event list
                                transferReportEvents.add(endEventImpl);
                            } catch (Exception e) {
                                // report this failure as last resort
                                failureException = e;
                                reportName = "error";
                                logger.warn("Exception - unable to notify end transfer state", e);
                            }
                            reportName += "_" + new SimpleDateFormat("yyyyMMddhhmmssSSS").format(new Date());
                            try {
                                if (transfer != null) {
                                    logger.debug("now pull back the destination transfer report");
                                    destinationReport = persistDestinationTransferReport(reportName, transfer, target);
                                    if (destinationReport != null) {
                                        eventProcessor.writeReport(destinationReport, TransferEventReport.ReportType.DESTINATION, endEventImpl.getTransferState());
                                    }
                                }
                                logger.debug("now persist the client side transfer report");
                                sourceReport = persistTransferReport(reportName, transfer, target, definition, transferReportEvents, manifest, failureException);
                                if (sourceReport != null) {
                                    eventProcessor.writeReport(sourceReport, TransferEventReport.ReportType.SOURCE, endEventImpl.getTransferState());
                                }
                            } catch (Exception e) {
                                logger.warn("Exception - unable to write transfer reports", e);
                            }
                            try {
                                endEventImpl.setLast(true);
                                endEventImpl.setSourceReport(sourceReport);
                                endEventImpl.setDestinationReport(destinationReport);
                                endEvent = endEventImpl;
                                eventProcessor.end(endEvent);
                            } catch (Exception e) {
                                // report this failure as last resort
                                failureException = e;
                                logger.warn("Exception - unable to notify end transfer state", e);
                            }
                        } finally {
                            clientState = ClientTransferState.Exit;
                        }
                    }
            }
        } catch (TransferCancelledException e) {
            logger.debug("Interrupted by transfer cancel request from client");
            clientState = ClientTransferState.Cancel;
        } catch (Exception e) {
            logger.debug("Exception - unable to transfer", e);
            /**
             * Save the first exception that we encounter.
             */
            if (failureException == null) {
                failureException = e;
            }
            if (transfer != null && (clientState == ClientTransferState.Begin || clientState == ClientTransferState.Prepare || clientState == ClientTransferState.Commit)) {
                // we must first inform the target repository that a client failure has occurred to allow it to
                // clean up appropriately, too
                clientState = ClientTransferState.Cancel;
            } else {
                clientState = ClientTransferState.Finished;
            }
        }
    }
    try {
        if (endEvent == null) {
            TransferEventError error = new TransferEventError();
            error.setTransferState(TransferEvent.TransferState.ERROR);
            TransferFailureException endException = new TransferFailureException(error);
            error.setMessage(endException.getMessage());
            error.setException(endException);
            error.setSourceReport(sourceReport);
            error.setDestinationReport(destinationReport);
            error.setLast(true);
            endEvent = error;
        }
        if (endEvent instanceof TransferEventError) {
            TransferEventError endError = (TransferEventError) endEvent;
            throw new TransferFailureException(endError);
        }
        return endEvent;
    } finally {
        // clean up
        if (transfer != null) {
            transferMonitoring.remove(transfer.getTransferId());
        }
        if (manifest != null) {
            manifest.delete();
            logger.debug("manifest file deleted");
        }
        if (requisite != null) {
            requisite.delete();
            logger.debug("requisite file deleted");
        }
    }
}
Also used : TransferCancelledException(org.alfresco.service.cmr.transfer.TransferCancelledException) TransferTarget(org.alfresco.service.cmr.transfer.TransferTarget) NodeRef(org.alfresco.service.cmr.repository.NodeRef) TransferEventSuccess(org.alfresco.service.cmr.transfer.TransferEventSuccess) TransferEventError(org.alfresco.service.cmr.transfer.TransferEventError) TransferFailureException(org.alfresco.service.cmr.transfer.TransferFailureException) TransferEndEvent(org.alfresco.service.cmr.transfer.TransferEndEvent) TransferEvent(org.alfresco.service.cmr.transfer.TransferEvent) TransferEventCancelled(org.alfresco.service.cmr.transfer.TransferEventCancelled) IOException(java.io.IOException) TransferCancelledException(org.alfresco.service.cmr.transfer.TransferCancelledException) TransferFailureException(org.alfresco.service.cmr.transfer.TransferFailureException) TransferException(org.alfresco.service.cmr.transfer.TransferException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) AlfrescoRuntimeException(org.alfresco.error.AlfrescoRuntimeException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) LinkedList(java.util.LinkedList) TransferCallback(org.alfresco.service.cmr.transfer.TransferCallback) Date(java.util.Date) TransferProgress(org.alfresco.service.cmr.transfer.TransferProgress) TransferException(org.alfresco.service.cmr.transfer.TransferException) FileOutputStream(java.io.FileOutputStream) Descriptor(org.alfresco.service.descriptor.Descriptor) File(java.io.File) TransferVersion(org.alfresco.service.cmr.transfer.TransferVersion) SimpleDateFormat(java.text.SimpleDateFormat)

Aggregations

TransferEvent (org.alfresco.service.cmr.transfer.TransferEvent)2 TransferEventSuccess (org.alfresco.service.cmr.transfer.TransferEventSuccess)2 TransferProgress (org.alfresco.service.cmr.transfer.TransferProgress)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)1 AlfrescoRuntimeException (org.alfresco.error.AlfrescoRuntimeException)1 NodeRef (org.alfresco.service.cmr.repository.NodeRef)1 TransferCallback (org.alfresco.service.cmr.transfer.TransferCallback)1 TransferCancelledException (org.alfresco.service.cmr.transfer.TransferCancelledException)1 TransferDefinition (org.alfresco.service.cmr.transfer.TransferDefinition)1 TransferEndEvent (org.alfresco.service.cmr.transfer.TransferEndEvent)1 TransferEventBegin (org.alfresco.service.cmr.transfer.TransferEventBegin)1 TransferEventCancelled (org.alfresco.service.cmr.transfer.TransferEventCancelled)1 TransferEventCommittingStatus (org.alfresco.service.cmr.transfer.TransferEventCommittingStatus)1