Skip to content

Add cluster-config-file locking - #1594

Closed
mattsta wants to merge 1 commit into
redis:unstablefrom
mattsta:add-flock-cluster-configfile
Closed

mattsta wants to merge 1 commit into
redis:unstablefrom
mattsta:add-flock-cluster-configfile

Conversation

@mattsta

@mattsta mattsta commented Mar 11, 2014

Copy link
Copy Markdown
Contributor

This is a first attempt at fixing the ongoing problem of people
launching multiple Redis Cluster instances in the same directory
with default options (which makes all instances use the same
"nodes.conf" which breaks their cluster).

This commit uses a flock approach to the problem. Another
valid solution would be to use an external lock file. Another
valid pattern would be to obtain the lock in clusterInit
instead of where we read/create the config in clusterLoadConfig.

Do we allow runtime moving of cluster-config-file? If so,
this commit doesn't handle that case yet. Actually, I just
checked, and the 'set' command doesn't work at all in
a cluster because:

127.0.0.1:37446> set cluster-config-file poo.conf
(error) MOVED 13508 127.0.0.1:19029
127.0.0.1:37446> debug cmdkeys set cluster-config-file poo.conf
1) "cluster-config-file"

@mattsta

mattsta commented Mar 11, 2014

Copy link
Copy Markdown
Contributor Author

Ha. Of course I meant "config set." Ignore unintentional dumbs.

Looks like we don't allow reading or setting of cluster-config-file:

127.0.0.1:37446> config get cluster-config-file
(empty list or set)
127.0.0.1:37446> config set cluster-config-file poo.conf
(error) ERR Unsupported CONFIG parameter: cluster-config-file

Prevent multiple Redis Cluster processes from using the same
node configuration file, since if that happens, your entire
cluster becomes unusable (and it confuses people who are
just getting started by giving them no feedback as to the failure).

This commit changes the functionality of reading and saving
cluster configs.  Now we keep the cluster config file open
as long as the redis-server process is alive.  If we close
the config file, we invite a race condition where another
node could start without our lock in place.

Instead of closing the configuration file after reading it, we
retain the FILE* globally.  Also, we don't close the file after
writing anymore.

Updates:
  - Retain FILE* in server struct
  - Centralized file opening and file closing for cluster-config-file so
    we can open the file without parsing it.
  - Improved(?)/added specific error messages
  - Started using REDIS_OK/REDIS_ERR in more places.

Example output on failure to obtain lock:
    matt@ununoctium:~/repos/redis/src% ./redis-server --cluster-enabled yes --port 3333
    [67969] 27 Mar 12:26:46.153 # Unable to start Redis Cluster. Your cluster-config-file "nodes.conf" is being used by another instance.  Please provide a unique cluster-config-file.
@mattsta

mattsta commented Mar 27, 2014

Copy link
Copy Markdown
Contributor Author

[Refactored this and rebased it against current unstable to resolve a few merge conflicts.]

@antirez

antirez commented Apr 24, 2014

Copy link
Copy Markdown
Contributor

Thanks matt, looks like to use flock() is preferred since we are guaranteed about the auto-release semantics when the process exists?

About where the lock is obtained, more or less it is probably the same thing, the interesting stuff here is that no two instances can run with the same file, as long as there is no race condition possible.

nodes.conf will hardly be something we can reconfigure at runtime, but, if we'll change idea, we'll just have to handle the logic by creating a new file in the dest path, lock it, unlock the old one, switch config. No need currently.

@antirez

antirez commented Apr 24, 2014

Copy link
Copy Markdown
Contributor

Question: why are you retaining the file descriptor? It looks like there is a simpler way to obtain the same effect, that is just open the file, lock it, and leak the file descriptor (never close it again, ever). This way we'll always take our lock to the file, whatever we do in the next calls, and we are free to open/close the file an infinite number of times.

@antirez

antirez commented Apr 24, 2014

Copy link
Copy Markdown
Contributor

p.s. I'm not sure what I suggested work after we rewrite the file, but it should in theory since the way we rewrite it we don't actually unlink / create it again, but merely truncate to zero length. Btw I'm investigating using fcntl() locks instead that have a less surprising semantics across operating systems apparently...

@antirez

antirez commented Apr 24, 2014

Copy link
Copy Markdown
Contributor

Ok, that's the idea, and works great -> https://gist.github.com/antirez/11255064
This closely simulate what Redis does.

if you run the test you'll notice that if you delete the file with rm /tmp/foo.txt, two processes will be able to lock the file at the same time, which is expected since they are two different files even if they have the same path (the first file will exist as long as one process still has a file descriptor about it).

Sounds like very acceptable with Redis, since we never unlink(2) the nodes.conf file but we just rewrite it in place.

I'm implementing the above give that's just a handful lines of code.

@mattsta

mattsta commented Apr 24, 2014

Copy link
Copy Markdown
Contributor Author

Great change! The approach of "leak a file descriptor, but in a good way" didn't enter my mind :) Most of my version was dealing with saving the same FILE* throughout the server's lifetime. Your way is much easier.

This will hopefully stop our weekly bug report of "cluster doesn't work -> oh, it's a nodes.conf conflict."

@antirez

antirez commented Apr 24, 2014

Copy link
Copy Markdown
Contributor

Hello Matt, I implemented this approach here -> db06108
Fortunately it appears to work... as you said (and the reason why you opened this PR to start with), this is likely a source of confusion less for Cluster newcomers.

Thanks,
Salvatore

@antirez antirez closed this Apr 24, 2014
@0xffea

0xffea commented Nov 30, 2014

Copy link
Copy Markdown

@badboy

badboy commented Dec 1, 2014

Copy link
Copy Markdown
Contributor

@mattsta

mattsta commented Dec 1, 2014

Copy link
Copy Markdown
Contributor Author

Two different possibilities to incorporate the use of fnctl:

Neat!

Though, isn't the main advantage of using fctnl that it works everywhere, so we don't need special cases? :)

We could just name it redis_flock() in util.c and use it instead of flock().

@badboy

badboy commented Dec 1, 2014

Copy link
Copy Markdown
Contributor

That's what I got from that article as well, but I didn't test it (yet). I can do so and would implement the redis_flock method then.

@badboy

badboy commented Dec 1, 2014

Copy link
Copy Markdown
Contributor

After writing a small sample application and then porting it into a redisFlock method I can say: fcntl locking won't help us here.

If you re-read perkin's post:

The semantics of fcntl() are such that any closure of a file descriptor in your application will release the locks held against that file.

Redis frequently opens and closes the provided cluster_configfile (nodes.conf), thus completely breaking the fcntl locking.
More info: On the Brokenness of File Locking.

Our best shot: Keep the flock on Linux/BSD (it works for the Redis use case) and disable it completely on machines without flock (it's not worth the hassle to have a wrapper if it doesn't work and changing the access to the nodes.conf is not an option either)

@mattsta

mattsta commented Dec 2, 2014

Copy link
Copy Markdown
Contributor Author

fcntl locking won't help us here.

you're quite right!

Just removing our flock calls for Solaris should be good enough.

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