use of org.alfresco.service.cmr.transfer.TransferEventError in project alfresco-repository by Alfresco.
the class TransferServiceCallbackTest method testErrorDuringCommit.
public void testErrorDuringCommit() {
Exception error = new TransferException("Commit failed");
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.ERROR);
status4.setCurrentPosition(8);
status4.setEndPosition(8);
status4.setError(error);
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);
try {
transferService.transfer(TRANSFER_TARGET_NAME, transferDef, mockedCallback);
fail();
} catch (TransferFailureException ex) {
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 TransferEventEndState();
event.setTransferState(TransferState.COMMITTING);
expectedEvents.add(event);
event = new TransferEventEnterState();
event.setTransferState(TransferState.ERROR);
expectedEvents.add(event);
event = new TransferEventReport();
event.setTransferState(TransferState.ERROR);
expectedEvents.add(event);
event = new TransferEventReport();
event.setTransferState(TransferState.ERROR);
expectedEvents.add(event);
event = new TransferEventError();
event.setTransferState(TransferState.ERROR);
((TransferEventError) event).setException(error);
expectedEvents.add(event);
verifyCallback(expectedEvents);
}
}
use of org.alfresco.service.cmr.transfer.TransferEventError in project alfresco-repository by Alfresco.
the class TransferServiceCallbackTest method testSendContentFailed.
public void testSendContentFailed() {
TransferProgress status0 = new TransferProgress();
status0.setStatus(Status.CANCELLED);
status0.setCurrentPosition(0);
status0.setEndPosition(0);
TransferProgress[] statuses = new TransferProgress[] { status0 };
configureBasicMockTransmitter(statuses);
when(mockedTransferTransmitter.begin(target, localRepositoryId, version)).thenReturn(transfer);
doThrow(new TransferException("Simulate failure to write content")).when(mockedTransferTransmitter).sendManifest(any(Transfer.class), any(File.class), any(OutputStream.class));
when(mockedTransferTransmitter.getStatus(transfer)).thenReturn(statuses[0]);
TransferDefinition transferDef = new TransferDefinition();
transferDef.setNodes(folder1, file1, file2, file3);
try {
transferService.transfer(TRANSFER_TARGET_NAME, transferDef, mockedCallback);
fail("Transfer expected to throw an exception, but it didn't.");
} catch (TransferFailureException ex) {
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.ERROR);
expectedEvents.add(event);
event = new TransferEventReport();
event.setTransferState(TransferState.ERROR);
expectedEvents.add(event);
event = new TransferEventReport();
event.setTransferState(TransferState.ERROR);
expectedEvents.add(event);
event = new TransferEventError();
event.setTransferState(TransferState.ERROR);
((TransferEventError) event).setException((Exception) ex.getCause());
expectedEvents.add(event);
verifyCallback(expectedEvents);
}
}
use of org.alfresco.service.cmr.transfer.TransferEventError in project alfresco-repository by Alfresco.
the class TransferServiceCallbackTest method testTargetAlreadyLocked.
public void testTargetAlreadyLocked() {
configureBasicMockTransmitter(null);
when(mockedTransferTransmitter.begin(target, "localRepositoryId", version)).thenThrow(new TransferException("Simulate lock unavailable"));
TransferDefinition transferDef = new TransferDefinition();
transferDef.setNodes(folder1, file1, file2, file3);
try {
transferService.transfer(TRANSFER_TARGET_NAME, transferDef, mockedCallback);
fail("Transfer expected to throw an exception, but it didn't.");
} catch (TransferFailureException ex) {
List<TransferEvent> expectedEvents = new ArrayList<TransferEvent>();
TransferEventImpl event;
event = new TransferEventEnterState();
event.setTransferState(TransferState.START);
expectedEvents.add(event);
event = new TransferEventEndState();
event.setTransferState(TransferState.START);
expectedEvents.add(event);
event = new TransferEventEnterState();
event.setTransferState(TransferState.ERROR);
expectedEvents.add(event);
event = new TransferEventReport();
event.setTransferState(TransferState.ERROR);
expectedEvents.add(event);
event = new TransferEventError();
event.setTransferState(TransferState.ERROR);
((TransferEventError) event).setException((Exception) ex.getCause());
expectedEvents.add(event);
verifyCallback(expectedEvents);
}
}
use of org.alfresco.service.cmr.transfer.TransferEventError in project alfresco-repository by Alfresco.
the class ReplicationActionExecutor method executeImpl.
@Override
protected void executeImpl(Action action, NodeRef actionedUponNodeRef) {
if (action instanceof ReplicationDefinition) {
// Already of the correct type
} else if (action.getActionDefinitionName().equals(ReplicationDefinitionImpl.EXECUTOR_NAME)) {
// Specialise the action if needed, eg when loaded directly from
// the NodeRef without going via the replication service
action = new ReplicationDefinitionImpl(action);
}
// Off we go
final ReplicationDefinition replicationDef = (ReplicationDefinition) action;
if (replicationDef.getTargetName() == null || replicationDef.getTargetName().equals("")) {
throw new ReplicationServiceException(I18NUtil.getMessage(MSG_ERR_TARGET_NOT_GIVEN));
}
if (replicationDef.getPayload().size() == 0) {
throw new ReplicationServiceException(I18NUtil.getMessage(MSG_ERR_NO_PAYLOADS_SPECIFIED));
}
if (!replicationDef.isEnabled()) {
throw new DisabledReplicationJobException(I18NUtil.getMessage(MSG_ERR_REPLICATION_DEF_DISABLED));
}
if (!replicationParams.isEnabled()) {
throw new ReplicationServiceException(I18NUtil.getMessage(MSG_ERR_UNABLE_TO_REPLICATE));
}
// Lock the service - only one instance of the replication
// should occur at a time
ReplicationDefinitionLockExtender lock = new ReplicationDefinitionLockExtender(replicationDef);
// Turn our payload list of root nodes into something that
// the transfer service can work with
Set<NodeRef> toTransfer;
try {
toTransfer = expandPayload(replicationDef);
} catch (Exception e) {
lock.close();
throw new ReplicationServiceException(I18NUtil.getMessage(MSG_ERR_PROCESSING_PAYLOAD, e.getMessage()), e);
}
// Ask the transfer service to do the replication
// work for us
TransferEndEvent endEvent = null;
try {
// Build the definition
TransferDefinition transferDefinition = buildTransferDefinition(replicationDef, toTransfer);
// Off we go
endEvent = transferService.transfer(replicationDef.getTargetName(), transferDefinition, lock);
if (endEvent instanceof TransferEventCancelled) {
if (logger.isDebugEnabled())
logger.debug("Cancelling replication job");
// that this is correctly recorded
throw new ActionCancelledException(replicationDef);
}
// Record details of the transfer reports (in success case)
replicationDef.setLocalTransferReport(endEvent.getSourceReport());
replicationDef.setRemoteTransferReport(endEvent.getDestinationReport());
replicationDefinitionPersister.saveReplicationDefinition(replicationDef);
} catch (Exception e) {
if (e instanceof ActionCancelledException) {
writeDefinitionReports(replicationDef, endEvent.getSourceReport(), endEvent.getDestinationReport());
throw (ActionCancelledException) e;
}
if (e instanceof TransferFailureException) {
TransferEventError failureEndEvent = ((TransferFailureException) e).getErrorEvent();
writeDefinitionReports(replicationDef, failureEndEvent.getSourceReport(), failureEndEvent.getDestinationReport());
Throwable cause = (e.getCause() == null) ? e : e.getCause();
throw new ReplicationServiceException(I18NUtil.getMessage(MSG_ERR_EXECUTING_TRANSFER, cause.getMessage()), cause);
}
writeDefinitionReports(replicationDef, null, null);
throw new ReplicationServiceException(I18NUtil.getMessage(MSG_ERR_EXECUTING_TRANSFER, e.getMessage()), e);
} finally {
lock.close();
}
}
use of org.alfresco.service.cmr.transfer.TransferEventError 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");
}
}
}
Aggregations