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

Atomic writes for persisting service/check state #2480

Merged
merged 3 commits into from
Nov 7, 2016
Merged

Conversation

kyhavlov
Copy link
Contributor

@kyhavlov kyhavlov commented Nov 7, 2016

Modified #2240 to call file.Sync() when persisting services/checks. We don't want to do it for check state though because that happens a lot more frequently, so instead just handle the case where we load a malformed file.

Fixes #1221

if err := ioutil.WriteFile(file, buf, 0600); err != nil {
return fmt.Errorf("failed writing file %q: %s", file, err)

// Create temp file in same dir, to make more likely atomic
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add a comment here about why we aren't calling writeFileAtomic().

Copy link
Contributor

@slackpad slackpad left a comment

Choose a reason for hiding this comment

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

LGTM - one place we should add a comment then good to merge.

@kyhavlov kyhavlov merged commit 83d2f36 into master Nov 7, 2016
@kyhavlov kyhavlov deleted the b-atomic-writes branch November 7, 2016 20:36
if err != nil {
return err
}
if _, err := fh.Write(contents); err != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Super late on this minor comment but better late than never: Shouldn't we defer the fh.Close() right after we open successfully? We otherwise have an exit path where it remains open. We can still close before we rename too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh that's a good point. We should probably even do a best-effort delete on the temp file if something goes wrong (do a deferred fn that we disarm at the end). We originally used a file name that was 1:1 with the service name but changed that to fix a race condition.

@kyhavlov kyhavlov mentioned this pull request Nov 17, 2016
@camerondavison
Copy link
Contributor

camerondavison commented Dec 29, 2016

just as an fyi. possibly related to this change, recently I just got Error starting agent: failed decoding service file "/var/lib/consul/services/bef2a566bb452d18967ed9d7f574f22a-ad21449f-29ac-c4e0-f185-73bd4791a2be.tmp": unexpected end of JSON input

the name seems extra long.

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