From 8b5bb414f111eeabb0514e686abbc7e0db031221 Mon Sep 17 00:00:00 2001 From: antirez Date: Fri, 7 May 2010 09:32:26 +0200 Subject: [PATCH] Swap file is now locked --- TODO | 5 +---- redis.c | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/TODO b/TODO index 2c119293..bdbe7974 100644 --- a/TODO +++ b/TODO @@ -15,10 +15,7 @@ Virtual Memory sub-TODO: * Check if the page selection algorithm is working well * Divide swappability of objects by refcount * Use multiple open FDs against the VM file, one for thread. -* it should be possible to give the vm-max-memory option in megabyte, gigabyte, ..., just using 2GB, 100MB, and so forth. -* Try to understand what can be moved into I/O threads that currently is instead handled by the main thread. For instance swapping file table scannig to find contiguous page could be a potential candidate (but I'm not convinced it's a good idea, better to improve the algorithm, for instance double the fast forward at every step?). -* Possibly decrRefCount() against swapped objects can be moved into I/O threads, as it's a slow operation against million elements list, and in general consumes CPU time that can be consumed by other threads (and cores). -* EXISTS should avoid loading the object if possible without too make the code too specialized. +* EXISTS should avoid loading the object if possible without making the code too specialized. * vm-min-age option * Make sure objects loaded from the VM are specially encoded when possible. * Check what happens performance-wise if instead to create threads again and again the same threads are reused forever. Note: this requires a way to disable this clients in the child, but waiting for empty new jobs queue can be enough. diff --git a/redis.c b/redis.c index d47dc60b..572bf306 100644 --- a/redis.c +++ b/redis.c @@ -8604,22 +8604,35 @@ static void vmInit(void) { off_t totsize; int pipefds[2]; size_t stacksize; + struct flock fl; if (server.vm_max_threads != 0) zmalloc_enable_thread_safeness(); /* we need thread safe zmalloc() */ expandVmSwapFilename(); redisLog(REDIS_NOTICE,"Using '%s' as swap file",server.vm_swap_file); + /* Try to open the old swap file, otherwise create it */ if ((server.vm_fp = fopen(server.vm_swap_file,"r+b")) == NULL) { server.vm_fp = fopen(server.vm_swap_file,"w+b"); } if (server.vm_fp == NULL) { redisLog(REDIS_WARNING, - "Impossible to open the swap file: %s. Exiting.", + "Can't open the swap file: %s. Exiting.", strerror(errno)); exit(1); } server.vm_fd = fileno(server.vm_fp); + /* Lock the swap file for writing, this is useful in order to avoid + * another instance to use the same swap file for a config error. */ + fl.l_type = F_WRLCK; + fl.l_whence = SEEK_SET; + fl.l_start = fl.l_len = 0; + if (fcntl(server.vm_fd,F_SETLK,&fl) == -1) { + redisLog(REDIS_WARNING, + "Can't lock the swap file at '%s': %s. Make sure it is not used by another Redis instance.", server.vm_swap_file, strerror(errno)); + exit(1); + } + /* Initialize */ server.vm_next_page = 0; server.vm_near_pages = 0; server.vm_stats_used_pages = 0; -- 2.45.2