Skip to content

Implement 'atomic downloads' with copy-to-temp + rename - #6913

Merged
ncw merged 5 commits into
rclone:masterfrom
nurpax:copy-rename
May 9, 2023
Merged

ncw merged 5 commits into
rclone:masterfrom
nurpax:copy-rename

Conversation

@nurpax

@nurpax nurpax commented Apr 4, 2023

Copy link
Copy Markdown
Contributor

Download a local copy into a temporary file first. Once the download is complete, rename the temp file to the real destination.

This prevents other processes from seeing partially downloaded copies of files being downloaded.

for #3770

What is the purpose of this change?

Support "atomic downloads" by copy-to-temp/rename. This prevents other processes from seeing partially downloaded files while rclone is downloading.

Was the change discussed in an issue or in the forum before?

#3770

Checklist

  • I have read the contribution guidelines.
  • I have added tests for all changes in this PR if appropriate.
  • I have added documentation for the changes if appropriate.
  • All commit messages are in house style.
  • I'm done, this Pull Request is ready for review :-)

Checklist incomplete, pushing PR for review. If the approach looks sensible, I'll update the PR with tests & other required items.

Comment thread fs/operations/operations.go Resolved

@ncw ncw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are some initial thoughts. I think this might need a few iterations as operations.Copy is very complicated!

Comment thread fs/operations/operations.go Resolved
Comment thread fs/operations/operations.go Resolved
Comment thread fs/operations/operations.go Resolved
Comment thread fs/operations/operations.go Resolved
@nurpax

nurpax commented Apr 5, 2023 •

Copy link
Copy Markdown
Contributor Author

If it's starting to look better, I can move on to tests & CLI options in the upcoming iterations.

Comment thread fs/operations/operations.go Outdated

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little confused why there's dst and newDst in this function. It might be because there are a lot of assignments to dst and newDst is updated to "carry the newly created file handle forward" when there were no errors.

I think the error handling here is correct and the debug prints print sensible output too. I'm just not 100% sure what should be returned from this path and how the newDst is used by the caller.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks correct.

The return from this function should be the final object in its final resting place. We try not to update newDst unless the function returned err = nil which is why I think there is dst and newDst.

I think the whole dst and newDst needs a careful review, but let's not do it in this PR! I'll do it after we merge it.

@ncw ncw left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is looking about right now :-)

Do you want to carry on with the other bits?

Comment thread fs/operations/operations.go Resolved
Comment thread fs/operations/operations.go Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this looks correct.

The return from this function should be the final object in its final resting place. We try not to update newDst unless the function returned err = nil which is why I think there is dst and newDst.

I think the whole dst and newDst needs a careful review, but let's not do it in this PR! I'll do it after we merge it.

@nurpax

nurpax commented Apr 5, 2023

Copy link
Copy Markdown
Contributor Author

@ncw Should the --inplace flag be a global flag and ignored on backends where this is not needed/unsupported, or should each supporting backend have their on flag like --local-inplace? I'd vote for the former: single global --inplace option.

@ncw

ncw commented Apr 5, 2023

Copy link
Copy Markdown
Member

@ncw Should the --inplace flag be a global flag and ignored on backends where this is not needed/unsupported, or should each supporting backend have their on flag like --local-inplace? I'd vote for the former: single global --inplace option.

Yes this needs to be a global flag as it affects operations.Copy which is used by everything.

BTW You can disable a feature for a backend, eg --disable PartialWrites which would be equivalent to --inplace=true.

@nurpax

nurpax commented Apr 5, 2023 •

Copy link
Copy Markdown
Contributor Author

OK, I added --inplace and a couple of tests. I did notice that some tests were failing at least TestSyncUTFNorm, I didn't check yet if those were earlier failures or caused by my change.

Not entirely sure if my added tests add a lot of value as they're copied from other tests, so the coverage should be similar to existing coverage.

EDIT: yes, my change definitely breaks TestSyncUTFNorm. Investigating further.

@nurpax

nurpax commented Apr 5, 2023 •

Copy link
Copy Markdown
Contributor Author

I find that TestSyncUTFNorm break goes away with this change:

diff --git a/fs/operations/operations.go b/fs/operations/operations.go
index 11c4b4795..fbe39cbd6 100644
--- a/fs/operations/operations.go
+++ b/fs/operations/operations.go
@@ -337,7 +337,7 @@ func Copy(ctx context.Context, f fs.Fs, dst fs.Object, remote string, src fs.Obj
        hashType, hashOption := CommonHash(ctx, f, src.Fs())

        remotePartial := remote
-       if !ci.Inplace && f.Features().Move != nil && f.Features().PartialUploads {
+       if !ci.Inplace && f.Features().Move != nil && f.Features().PartialUploads && dst == nil {
                // Avoid making the leaf name longer if it's already lengthy to avoid
                // trouble with file name length limits.
                suffix := ".partial-" + random.String(8)

At least this Update() path is broken when remotePartial has a suffix:

if doUpdate {
	actionTaken = "Copied (replaced existing)"
	err = dst.Update(ctx, in, wrappedSrc, options...)
} else {
	actionTaken = "Copied (new)"
	dst, err = f.Put(ctx, in, wrappedSrc, options...)
}

Argument dst is passed as an input to Copy() so it points to non-suffix destination path and dst.Update will write the source there.

I thought this would've fixed it:

if doUpdate && remotePartial == remote {
	actionTaken = "Copied (replaced existing)"
	err = dst.Update(ctx, in, wrappedSrc, options...)
} else {
	actionTaken = "Copied (new)"
	dst, err = f.Put(ctx, in, wrappedSrc, options...)
}

But it still breaks with some weirdness in the UTF-8 filenames:

image

(copy&pasting from the shell removes the spaces that I see in the terminal :))

@ncw

ncw commented Apr 7, 2023

Copy link
Copy Markdown
Member

I think the problem is this...

If dst != nil then you want to be using dst.Remote() to name the file, not src.Remote().

Thats a subtle point also... Some backends do different things on Update vs Put. If you use Update when you should have used Put on Google Drive you'll make a duplicate file.

Now on the backends we are talking about, local, sftp, ftp etc they don't do anything special. Though perhaps we'll be missing out on special permissions on the file which would have been preserved if we updated the existing file rather than made a new one.

@nurpax

nurpax commented Apr 11, 2023

Copy link
Copy Markdown
Contributor Author

I can get tests (ie., cd fs/sync && go test) to pass with this change: 4d31ccf

But to be fair, I don't completely understand this function so it's hard to tell if that's the right thing to do. :)

@ncw

ncw commented Apr 20, 2023

Copy link
Copy Markdown
Member

I can get tests (ie., cd fs/sync && go test) to pass with this change: 4d31ccf

But to be fair, I don't completely understand this function so it's hard to tell if that's the right thing to do. :)

I think that patch is nearly right - I commented on it what I think it should be.

@nurpax

nurpax commented Apr 21, 2023

Copy link
Copy Markdown
Contributor Author

Added a comment here: 4d31ccf#commitcomment-109940910 (I'm finding it really hard to keep track of review comments in GitHub PRs :))

@jakanaka

Copy link
Copy Markdown

How this is supposed to work? I mean what flags to use?

@nurpax

nurpax commented Apr 30, 2023

Copy link
Copy Markdown
Contributor Author

The patch implements this so that it's enabled by default. If you want to revert to old behavior, you can turn it off with the "--inplace" flag. This is the same default and same flag as in rsync.

@jakanaka

Copy link
Copy Markdown

The patch implements this so that it's enabled by default. If you want to revert to old behavior, you can turn it off with the "--inplace" flag. This is the same default and same flag as in rsync.

What does it do exactly? what will happen if i do this?

rclone copy "/mnt/folder1" "sftp:/folder1"

here sftp is a sftp remote

@ncw

ncw commented Apr 30, 2023

Copy link
Copy Markdown
Member

@nurpax I think next week I will pull this locally and see if I can find out what is going on. If I manage to fix it I'll push a commit on top of yours.

@ncw

ncw commented May 4, 2023 •

Copy link
Copy Markdown
Member

@nurpax I've spent quite a long time working on this. I've

  • rebased the commits and fixed the conflicts
  • squashed the commits into two main commits
  • fixed the problems (these were to do with calling the Update method)
  • added some more commits with more fixes

So I think this is nearly done!

It needs

  • documentation (I'm happy to write this)
  • decision on whether to set the feature flag for FTP/SFTP

Can you pull what I've done (I'd suggest into a new branch since I rebased it) and see how it is working for you?

PS the Copy function is too complicated now! It needs refactoring badly. I'll probably do that after we merge this.

@nurpax

nurpax commented May 5, 2023

Copy link
Copy Markdown
Contributor Author

@ncw, thank you so much for helping out with this change! I read through your modifications and it locally and it works as I'd expect it to work: copying new and replacing existing files works, I tried with a bunch of different "rclone copy/sync" scenarios where the source was modified. Tests also passed.

As far as my original needs for this feature go, it's done. 👍 I don't have an opinion on ftp/sftp. We don't use that path and I have little recent experience on using ftp, so I can't tell what downsides enabling this might have.


For the record, there are some requests in #3770 (comment) that are not implemented in this change. I think they could hide the partial files by masking out *.partial in their web server config. The .partial would have to become part of the "official rclone interface" then. A simple workaround (at the cost of extra diskspace) would be to rclone into a local directory (outside of the web server dirs) first, then perform another rclone sync from the local directory to the web server dir.

The comment mentions --partial-dir as a way to specify a separate directory for the temp downloads but it seems rsync's --partial-dir doesn't quite work like that. See e.g., this thread: like copying --partial-dir to rclone

It's possible to revert back to old behavior by using the --inplace flag, so IMO merging what's done now is definitely forward progress anyway. :)

ncw and others added 5 commits May 9, 2023 15:56
Before this fix it was returning the base objects string rather than
the overridden remote.
Before this change rclone used a normal SFTP rename if present to
implement Move.

However the normal SFTP rename won't overwrite existing files.

This fixes it to either use the POSIX rename extension
("[email protected]") or to delete the source first before
renaming using the normal SFTP rename.

This isn't normally a problem as rclone always removes any existing
objects first, however to implement non --inplace operations we do
require overwriting an existing file.
Implement a Partialuploads feature flag to mark backends for which
uploads are not atomic.

This is set for the following backends

- local
- ftp
- sftp

See rclone#3770
When copying to a backend which has the PartialUploads feature flag
set and can Move files the file is copied into a temporary name first.
Once the copy is complete, the file is renamed to the real
destination.

This prevents other processes from seeing partially downloaded copies
of files being downloaded and prevents overwriting the old file until
the new one is complete.

This also adds --inplace flag that can be used to disable the partial
file copy/rename feature.

See rclone#3770

Co-authored-by: Nick Craig-Wood <[email protected]>
@ncw

ncw commented May 9, 2023

Copy link
Copy Markdown
Member

@nurpax

thank you so much for helping out with this change! I read through your modifications and it locally and it works as I'd expect it to work: copying new and replacing existing files works, I tried with a bunch of different "rclone copy/sync" scenarios where the source was modified. Tests also passed.

Great!

As far as my original needs for this feature go, it's done. +1 I don't have an opinion on ftp/sftp. We don't use that path and I have little recent experience on using ftp, so I can't tell what downsides enabling this might have.

I think enabling this on SFTP is pretty safe. Enabling this on FTP is probably less safe as there are a lot of extremely crusty FTP servers out there.

I'm going to enable it for FTP and SFTP since the integration tests all pass. I had to patch the SFTP backend to allow overwriting files when renaming.

For the record, there are some requests in #3770 (comment) that are not implemented in this change. I think they could hide the partial files by masking out *.partial in their web server config. The .partial would have to become part of the "official rclone interface" then. A simple workaround (at the cost of extra diskspace) would be to rclone into a local directory (outside of the web server dirs) first, then perform another rclone sync from the local directory to the web server dir.

I'm happy for *.partial to become part of the official rclone interface - I've written it into the docs.

The comment mentions --partial-dir as a way to specify a separate directory for the temp downloads but it seems rsync's --partial-dir doesn't quite work like that. See e.g., this thread: like copying --partial-dir to rclone

Yes --partial-dir on rsync does something a little different. However I think it is probably close enough if we wanted to use it for the purpose of storing *.partial files.

It's possible to revert back to old behavior by using the --inplace flag, so IMO merging what's done now is definitely forward progress anyway. :)

Yes!

I've just pushed an update with docs, sftp and ftp support. If that builds OK then I'll merge :-)

Thank you for doing this, and in particular thank you for untangling the confusion re rsync as to exactly what this feature should do.

@ncw
ncw merged commit 5c594fe into rclone:master May 9, 2023
@nurpax

nurpax commented May 9, 2023

Copy link
Copy Markdown
Contributor Author

Excellent! Thanks for the help, I'm glad it's merged! :)

@Jwink3101

Copy link
Copy Markdown
Contributor

This can potentially close #6206 and #6284

@kapitainsky

Copy link
Copy Markdown
Contributor

@ncw

ncw commented Jul 1, 2023

Copy link
Copy Markdown
Member

I will look at this shortly!

@nielash nielash mentioned this pull request Sep 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants