Skip to content

Cancel SHUTDOWN if initial AOF is being written - #1829

Merged
antirez merged 1 commit into
redis:unstablefrom
mattsta:stop-aof-dataloss
Jun 23, 2014
Merged

antirez merged 1 commit into
redis:unstablefrom
mattsta:stop-aof-dataloss

Conversation

@mattsta

@mattsta mattsta commented Jun 21, 2014

Copy link
Copy Markdown
Contributor

If we're writing the initial AOF, we shouldn't exit because Redis will load the empty (or partial) AOF on restart.

@antirez

antirez commented Jun 21, 2014

Copy link
Copy Markdown
Contributor

👍 Matt, thanks. If the program works the same with server.aof_current_size = 0 that could be better since it's the value the var is initialized to on normal startups.

@mattsta

mattsta commented Jun 21, 2014

Copy link
Copy Markdown
Contributor Author

I did look for an initial value, but it looks to be undefined until set in aofUpdateCurrentSize, so I created an initial undefined value when AOF is enabled.

The only other place aof_current_size is set directly is when loadAppendOnlyFile() fails (then it's set to 0).

If I missed something, let me know. :)

@mattsta

mattsta commented Jun 21, 2014

Copy link
Copy Markdown
Contributor Author

Oh, I see now. Obviously loadAppendOnly() is called on startup, it's just out of the regular flow of setting initial values in redis.c. I'll switch things around.

@antirez

antirez commented Jun 21, 2014

Copy link
Copy Markdown
Contributor

@mattsta exactly, your code looks perfect just the idea is to use the same init value as before. Thinking a bit about additional failure modes we could fix and replying...

@antirez

antirez commented Jun 21, 2014

Copy link
Copy Markdown
Contributor

So.. the only code path I can imagine where the value is set to zero but it was not set via CONFIG SET is when we start the server without an AOF file. One could argue that it should be possible to shutdown the server under this special condition if a rewrite is in progress (manually triggered.... for the file to be zero length and a rewrite to be in progress). But, if the server is empty, is not a big deal to wait for the children to exit before the shutdown, since the rewrite in the worst condition can write the data written in the current event loop before flushAppendOnlyFile() is called (otherwise the aof_current_size would no longer be zero).

TL;DR: if we use a value of zero, start a server, trigger a manual rewrite with BGREWRITEAOF, and send SHUTDOWN, all at the same time, the server will not shutdown if the rewrite is still in progress.

@mattsta

mattsta commented Jun 21, 2014

Copy link
Copy Markdown
Contributor Author

That sounds like a good thing. If the user requests data saving, the server shouldn't stop it unless requested.

Though, there's not a current way to cancel an AOF rewrite (e.g. DEBUG CANCEL BGREWRITEAOF), and the NOSAVE flag isn't respected in the AOF shutdown flow (because previously, the rewrite was just killed).

@antirez

antirez commented Jun 21, 2014

Copy link
Copy Markdown
Contributor

AOF rewrite does not need to be preserved actually because the server is writing to the old AOF at the same time, so it is technically strange you can't cancel the first BGREWRITE that starts after a server is started up with an empty file. That was the point of my probably bad exposed comment above :-)

About the fact AOF rewrites can't be aborted, that's true but never found an use for it... maybe there is, like, you start a rewrite but the disk is too busy and is putting too much pressure?

About NOSAVE, this is specifically intended for the rdb storage because AOFs will always flush pending buffers to the currently active AOF. However the old one is killed because there is no point in waiting for the rerwite since all the data is already in the old AOF anyway.

@mattsta

mattsta commented Jun 21, 2014

Copy link
Copy Markdown
Contributor Author

AOF rewrite does not need to be preserved actually because the server is writing to the old AOF at the same time,

Correct! That's also where an initial value of -1 helps because it makes it clear "file has never existed" (meaning: we must wait for it to complete before exiting) versus "file exists but is empty" (meaning: the user has no data, so we can still exit).

@antirez

antirez commented Jun 21, 2014

Copy link
Copy Markdown
Contributor

Exactly, -1 is semantically more correct. My only concern is if this negative value is used (causing issues) in the AOF code while a rewrite is already in progress.

@antirez

antirez commented Jun 21, 2014

Copy link
Copy Markdown
Contributor

p.s. grepping to check this.

@antirez

antirez commented Jun 21, 2014

Copy link
Copy Markdown
Contributor

The var is used while possibly at -1:

  1. In INFO output.

Should be never touched in the context of flushAppendOnlyFile() since the buffer should always be empty in this case, given that we append to the buffer only if the value is REDIS_AOF_ON (and it is switched to ON only after the first rewrite).

Ok, then we have the key :-) Instead of initializing the value to -1, we should just check for the value of server.aof_state. If it is REDIS_AOF_WAIT_REWRITE, we know the AOF was just turned on. Makes sense?

Fixes redis#1826 (and many other reports of the same problem)
@mattsta

mattsta commented Jun 21, 2014

Copy link
Copy Markdown
Contributor Author

Even better! Didn't notice that flag the first time around.

antirez added a commit that referenced this pull request Jun 23, 2014
Cancel SHUTDOWN if initial AOF is being written
@antirez
antirez merged commit af7647b into redis:unstable Jun 23, 2014
@antirez

antirez commented Jun 23, 2014

Copy link
Copy Markdown
Contributor

Merged, thanks!

@mattsta
mattsta deleted the stop-aof-dataloss branch June 23, 2014 12:37
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.

2 participants