Search in sources :

Example 1 with BlockingTransfersException

use of org.syncany.operations.up.BlockingTransfersException in project syncany by syncany.

the class CleanupOperation method execute.

@Override
public CleanupOperationResult execute() throws Exception {
    logger.log(Level.INFO, "");
    logger.log(Level.INFO, "Running 'Cleanup' at client " + config.getMachineName() + " ...");
    logger.log(Level.INFO, "--------------------------------------------");
    // Do initial check out remote repository preconditions
    CleanupResultCode preconditionResult = checkPreconditions();
    fireStartEvent();
    if (preconditionResult != CleanupResultCode.OK) {
        fireEndEvent();
        return new CleanupOperationResult(preconditionResult);
    }
    fireCleanupNeededEvent();
    // At this point, the operation will lock the repository
    startOperation();
    // If other clients have unfinished transactions with deletions, do not proceed.
    try {
        transferManager.cleanTransactions();
    } catch (BlockingTransfersException ignored) {
        finishOperation();
        fireEndEvent();
        return new CleanupOperationResult(CleanupResultCode.NOK_REPO_BLOCKED);
    }
    // Wait two seconds (conservative cleanup, see #104)
    logger.log(Level.INFO, "Cleanup: Waiting a while to be sure that no other actions are running ...");
    Thread.sleep(BEFORE_DOUBLE_CHECK_TIME);
    // Check again. No other clients should be busy, because we waited BEFORE_DOUBLE_CHECK_TIME
    preconditionResult = checkPreconditions();
    if (preconditionResult != CleanupResultCode.OK) {
        finishOperation();
        fireEndEvent();
        return new CleanupOperationResult(preconditionResult);
    }
    // If we do cleanup, we are no longer allowed to resume a transaction
    transferManager.clearResumableTransactions();
    transferManager.clearPendingTransactions();
    // Now do the actual work!
    logger.log(Level.INFO, "Cleanup: Starting transaction.");
    remoteTransaction = new RemoteTransaction(config, transferManager);
    removeOldVersions();
    if (options.isRemoveUnreferencedTemporaryFiles()) {
        transferManager.removeUnreferencedTemporaryFiles();
    }
    mergeRemoteFiles();
    // We went succesfully through the entire operation and checked everything. Hence we update the last cleanup time.
    updateLastCleanupTime();
    finishOperation();
    fireEndEvent();
    return updateResultCode(result);
}
Also used : BlockingTransfersException(org.syncany.operations.up.BlockingTransfersException) RemoteTransaction(org.syncany.plugins.transfer.RemoteTransaction) CleanupResultCode(org.syncany.operations.cleanup.CleanupOperationResult.CleanupResultCode)

Example 2 with BlockingTransfersException

use of org.syncany.operations.up.BlockingTransfersException in project syncany by syncany.

the class TransactionAwareFeatureTransferManager method cleanTransactions.

/**
	 * Checks if any transactions of the local machine were not completed and performs
	 * a rollback if any transactions were found. The rollback itself is performed in
	 * a transaction.
	 *
	 * <p>The method uses {@link #retrieveRemoteTransactions()} to download all transaction
	 * files and then rolls back the local machines's transactions:
	 *
	 * <ul>
	 *  <li>Files in the transaction marked "UPLOAD" are deleted.</li>
	 *  <li>Files in the transaction marked "DELETE" are moved back to their original place.</li>
	 * </ul>
	 *
	 * @throws BlockingTransfersException if we cannot proceed (Deleting transaction by another client exists).
	 */
public void cleanTransactions() throws StorageException, BlockingTransfersException {
    Objects.requireNonNull(config, "Cannot clean transactions if config is null.");
    Map<TransactionTO, TransactionRemoteFile> transactions = retrieveRemoteTransactions();
    boolean noBlockingTransactionsExist = true;
    for (TransactionTO potentiallyCancelledTransaction : transactions.keySet()) {
        boolean isCancelledOwnTransaction = potentiallyCancelledTransaction.getMachineName().equals(config.getMachineName());
        // If this transaction is from our machine, delete all permanent or temporary files in this transaction.
        if (isCancelledOwnTransaction) {
            rollbackSingleTransaction(potentiallyCancelledTransaction, transactions.get(potentiallyCancelledTransaction));
        } else if (noBlockingTransactionsExist) {
            // Only check if we have not yet found deleting transactions by others
            for (ActionTO action : potentiallyCancelledTransaction.getActions()) {
                if (action.getType().equals(ActionType.DELETE)) {
                    noBlockingTransactionsExist = false;
                }
            }
        }
    }
    logger.log(Level.INFO, "Done rolling back previous transactions.");
    if (!noBlockingTransactionsExist) {
        throw new BlockingTransfersException();
    }
}
Also used : ActionTO(org.syncany.plugins.transfer.to.ActionTO) TransactionRemoteFile(org.syncany.plugins.transfer.files.TransactionRemoteFile) BlockingTransfersException(org.syncany.operations.up.BlockingTransfersException) TransactionTO(org.syncany.plugins.transfer.to.TransactionTO)

Aggregations

BlockingTransfersException (org.syncany.operations.up.BlockingTransfersException)2 CleanupResultCode (org.syncany.operations.cleanup.CleanupOperationResult.CleanupResultCode)1 RemoteTransaction (org.syncany.plugins.transfer.RemoteTransaction)1 TransactionRemoteFile (org.syncany.plugins.transfer.files.TransactionRemoteFile)1 ActionTO (org.syncany.plugins.transfer.to.ActionTO)1 TransactionTO (org.syncany.plugins.transfer.to.TransactionTO)1