use of org.syncany.plugins.transfer.files.TransactionRemoteFile in project syncany by syncany.
the class UpOperation method attemptResumeTransactionRemoteFile.
private TransactionRemoteFile attemptResumeTransactionRemoteFile() throws StorageException, BlockingTransfersException {
TransactionRemoteFile transactionRemoteFile = null;
// They look for the matching transaction on the remote.
List<TransactionRemoteFile> transactions = transferManager.getTransactionsByClient(config.getMachineName());
// Not sure yet what these blocking structures are.
if (transactions == null) {
// We have blocking transactions
stopBecauseOfBlockingTransactions();
throw new BlockingTransfersException();
}
// There is no sign of the transaction on the remote. Clean up the local transaction.
if (transactions.size() != 1) {
logger.log(Level.INFO, "Unable to find (unique) transactionRemoteFile. Not resuming.");
transferManager.clearResumableTransactions();
} else // Remote transaction file found.
{
transactionRemoteFile = transactions.get(0);
}
return transactionRemoteFile;
}
use of org.syncany.plugins.transfer.files.TransactionRemoteFile in project syncany by syncany.
the class RemoteTransaction method commit.
/**
* Commits this transaction by performing the required upload and
* delete operations. The method first moves all files to the temporary
* remote location. If no errors occur, all files are moved to their
* final location.
*
* <p>The method first writes a {@link TransactionRemoteFile} containing
* all actions to be performed and then uploads this file. Then it uploads
* new files (added by {@link #upload(File, RemoteFile) upload()} and moves
* deleted files to a temporary location (deleted by {@link #delete(RemoteFile) delete()}.
*
* <p>If this was successful, the transaction file is deleted and the
* temporary files. After deleting the transaction file, the transaction
* is successfully committed.
*/
public void commit() throws StorageException {
logger.log(Level.INFO, "Starting TX.commit() ...");
if (isEmpty()) {
logger.log(Level.INFO, "- Empty transaction, not committing anything.");
return;
}
File localTransactionFile = writeLocalTransactionFile();
TransactionRemoteFile remoteTransactionFile = uploadTransactionFile(localTransactionFile);
commit(localTransactionFile, remoteTransactionFile);
}
use of org.syncany.plugins.transfer.files.TransactionRemoteFile in project syncany by syncany.
the class TransactionAwareFeatureTransferManager method removeUnreferencedTemporaryFiles.
/**
* Removes temporary files on the offsite storage that are not listed in any
* of the {@link TransactionRemoteFile}s available remotely.
*
* <p>Temporary files might be left over from unfinished transactions.
*/
public void removeUnreferencedTemporaryFiles() throws StorageException {
// Retrieve all transactions
Map<TransactionTO, TransactionRemoteFile> transactions = retrieveRemoteTransactions();
Collection<TempRemoteFile> tempRemoteFiles = list(TempRemoteFile.class).values();
// Find all remoteFiles that are referenced in a transaction
Set<TempRemoteFile> tempRemoteFilesInTransactions = new HashSet<TempRemoteFile>();
for (TransactionTO transaction : transactions.keySet()) {
for (ActionTO action : transaction.getActions()) {
tempRemoteFilesInTransactions.add(action.getTempRemoteFile());
}
}
// Consider just those files that are not referenced and delete them.
tempRemoteFiles.removeAll(tempRemoteFilesInTransactions);
for (TempRemoteFile unreferencedTempRemoteFile : tempRemoteFiles) {
logger.log(Level.INFO, "Unreferenced temporary file found. Deleting {0}", unreferencedTempRemoteFile);
underlyingTransferManager.delete(unreferencedTempRemoteFile);
}
}
use of org.syncany.plugins.transfer.files.TransactionRemoteFile 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();
}
}
use of org.syncany.plugins.transfer.files.TransactionRemoteFile in project syncany by syncany.
the class TransactionAwareFeatureTransferManager method getTransactionsByClient.
/**
* This function returns a list of all remote transaction files that belong to the client. If blocking transactions exist,
* this methods returns null, because we are not allowed to proceed.
*/
public List<TransactionRemoteFile> getTransactionsByClient(String client) throws StorageException {
Objects.requireNonNull(config, "Cannot get transactions if config is null.");
Map<TransactionTO, TransactionRemoteFile> transactions = retrieveRemoteTransactions();
List<TransactionRemoteFile> transactionsByClient = new ArrayList<TransactionRemoteFile>();
for (TransactionTO potentiallyResumableTransaction : transactions.keySet()) {
boolean isCancelledOwnTransaction = potentiallyResumableTransaction.getMachineName().equals(config.getMachineName());
if (isCancelledOwnTransaction) {
transactionsByClient.add(transactions.get(potentiallyResumableTransaction));
} else {
// Check for blocking transactions
for (ActionTO action : potentiallyResumableTransaction.getActions()) {
if (action.getType().equals(ActionType.DELETE)) {
return null;
}
}
}
}
return transactionsByClient;
}
Aggregations