Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve AtomicFileWriter's support for Win and Mac #1554

Merged
merged 1 commit into from
Apr 6, 2023

Conversation

n1hility
Copy link
Member

@n1hility n1hility commented Apr 5, 2023

As discussed on containers/common#1397, this PR is a step towards addressing corruption issues with container and machine configuration files that can occur with an improper shutdown.

Add ExplicitCommit option to provide calling code the ability to preserve original files when an application error occurs, not just an error that occurs during writing (e.g. a marshaller failure)

Change Close() impl to safely handle redundant calls (for defer usage safety)

Introduces two flush points, one just after data is written that supports the previous code's Linux approach of using fdatasync(), and another for Mac/Win/Non-Linux Unix that needs to full flush after metadata updates are staged.

Using this patch, common's usage for writting the container config file would look like this:

opts := &ioutils.AtomicFileWriterOptions{ExplicitCommit: true}
configFile, err := ioutils.NewAtomicFileWriterWithOpts(path, 0o644, opts)
if err != nil {
	return err
}
defer configFile.Close()

enc := toml.NewEncoder(configFile)
if err := enc.Encode(c); err != nil {
	return err
}

// If no errors commit the changes to the config file
return configFile.Commit()

Add ExplicitCommit option to provide calling code the ability to
preserve original files when an application error occurs, not just
an error that occurs during writing.

Change Close() impl to safely handle redundant calls (for defer
usage safety)

Signed-off-by: Jason T. Greene <jason.greene@redhat.com>
Copy link
Member

@vrothberg vrothberg left a comment

Choose a reason for hiding this comment

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

// to the destination. This allows an application to preserve the original
// file when an error occurs during processing (and not just during write)
// The default is false, which will auto-commit on Close
ExplicitCommit bool
Copy link
Member

Choose a reason for hiding this comment

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

I think I'm missing a point and wonder why ExplicitCommit and Commit() are needed. I somehow expected changes to Close() only. @n1hility, can you elaborate on that?

Copy link
Member

Choose a reason for hiding this comment

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

I think the point is that there is no way to abort this operation.
As shown in the example in the PR description when the encoder returns an error which was not caused by the Write() call, the encoder returns early and we can have a corrupted file once Close() is called. The only way to prevent this would be to not call Close() but this would then leak fds instead and would never allow the use of defer somefile.Close()

Copy link
Member

Choose a reason for hiding this comment

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

Thanks, makes sense 👍

Copy link
Member Author

Choose a reason for hiding this comment

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

Great summary, I couldn't have said it better. I would have preferred to make a hard move and make this commit() approach the only option instead of a flag like in my PR, since the previous auto-commit behavior is likely to cause problems in most usage patterns. However, it would be compatibility breaking, and I wasnt sure how much usage of this API there is in the wild.

Copy link
Member

Choose a reason for hiding this comment

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

I think that's a great way of keeping the API stable, @n1hility. Thanks!

Copy link
Member

@vrothberg vrothberg left a comment

Choose a reason for hiding this comment

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

LGTM

@rhatdan
Copy link
Member

rhatdan commented Apr 6, 2023

LGTM

@rhatdan rhatdan merged commit 945f872 into containers:main Apr 6, 2023
n1hility added a commit to n1hility/common that referenced this pull request Apr 6, 2023
Updates to containers/storage@main
(contains a required fswriters PR: containers/storage#1554)

Signed-off-by: Jason T. Greene <jason.greene@redhat.com>
n1hility added a commit to n1hility/common that referenced this pull request Apr 6, 2023
Updates to containers/storage@main
(contains a required fswriters PR: containers/storage#1554)

Signed-off-by: Jason T. Greene <jason.greene@redhat.com>
n1hility added a commit to n1hility/common that referenced this pull request Apr 6, 2023
Updates to containers/storage@main
(contains a required fswriters PR: containers/storage#1554)

[NO NEW TESTS NEEDED]

Signed-off-by: Jason T. Greene <jason.greene@redhat.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants