Skip to content

Make 'lookupKeyRead‘ return NULL if key is expired and current client is not master - #1770

Closed
coocood wants to merge 2 commits into
redis:unstablefrom
coocood:slave_expire
Closed

coocood wants to merge 2 commits into
redis:unstablefrom
coocood:slave_expire

Conversation

@coocood

@coocood coocood commented May 22, 2014

Copy link
Copy Markdown

@coocood

coocood commented May 22, 2014

Copy link
Copy Markdown
Author

I made an assumption that commands from master never trigger 'lookupRead', I need to verify it.

@coocood

coocood commented May 22, 2014

Copy link
Copy Markdown
Author

'lookupKeyRead' is called by 'EXPIRE' command, so master will trigger 'lookupKeyRead'.

So checking if the current client is master is required.

Normally if a key is expired, the master should sent DEL command before any further command on that key.

But if slave's clock is faster than master, the key may expire before master, so there is a chance that master send a command that set the key's new expire time, but failed due to the key is expired on slave.

@coocood coocood changed the title Do not lookup key if key is expired Make 'lookupKeyRead‘ return NULL if key is expired and current client is not master May 22, 2014
antirez added a commit that referenced this pull request Dec 10, 2014
Slaves key expire is orchestrated by the master. Sometimes the master
will send the synthesized DEL to expire keys on the slave with a non
trivial delay (when the key is not accessed, only the incremental expiry
algorithm will expire it in background).

During that time, a key is logically expired, but slaves still return
the key if you GET (or whatever) it. This is a bad behavior.

However we can't simply trust the slave view of the key, since we need
the master to be able to send write commands to update the slave data
set, and DELs should only happen when the key is expired in the master
in order to ensure consistency.

However 99.99% of the issues with this behavior is when a client which
is not a master sends a read only command. In this case we are safe and
can consider the key as non existing.

This commit does a few changes in order to make this sane:

1. lookupKeyRead() is modified in order to return NULL if the above
conditions are met.
2. Calls to lookupKeyRead() in commands actually writing to the data set
are repliaced with calls to lookupKeyWrite().

There are redundand checks, so for example, if in "2" something was
overlooked, we should be still safe, since anyway, when the master
writes the behavior is to don't care about what expireIfneeded()
returns.

This commit is related to  #1768, #1770, #2131.
@antirez

antirez commented Dec 10, 2014

Copy link
Copy Markdown
Contributor

Fixed in 06e76bc.

@antirez antirez closed this Dec 10, 2014
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.

Improve expire consistency on slaves

2 participants