]>
Commit | Line | Data |
---|---|---|
1 | #include "redis.h" | |
2 | ||
3 | #include <fcntl.h> | |
4 | #include <pthread.h> | |
5 | #include <math.h> | |
6 | #include <signal.h> | |
7 | ||
8 | /* dscache.c - Disk store cache for disk store backend. | |
9 | * | |
10 | * When Redis is configured for using disk as backend instead of memory, the | |
11 | * memory is used as a cache, so that recently accessed keys are taken in | |
12 | * memory for fast read and write operations. | |
13 | * | |
14 | * Modified keys are marked to be flushed on disk, and will be flushed | |
15 | * as long as the maxium configured flush time elapsed. | |
16 | * | |
17 | * This file implements the whole caching subsystem and contains further | |
18 | * documentation. */ | |
19 | ||
20 | /* TODO: | |
21 | * | |
22 | * WARNING: most of the following todo items and design issues are no | |
23 | * longer relevant with the new design. Here as a checklist to see if | |
24 | * some old ideas still apply. | |
25 | * | |
26 | * - What happens when an object is destroyed? | |
27 | * | |
28 | * If the object is destroyed since semantically it was deleted or | |
29 | * replaced with something new, we don't care if there was a SAVE | |
30 | * job pending for it. Anyway when the IO JOb will be created we'll get | |
31 | * the pointer of the current value. | |
32 | * | |
33 | * If the object is already a REDIS_IO_SAVEINPROG object, then it is | |
34 | * impossible that we get a decrRefCount() that will reach refcount of zero | |
35 | * since the object is both in the dataset and in the io job entry. | |
36 | * | |
37 | * - What happens with MULTI/EXEC? | |
38 | * | |
39 | * Good question. Without some kind of versioning with a global counter | |
40 | * it is not possible to have trasactions on disk, but they are still | |
41 | * useful since from the point of view of memory and client bugs it is | |
42 | * a protection anyway. Also it's useful for WATCH. | |
43 | * | |
44 | * Btw there is to check what happens when WATCH gets combined to keys | |
45 | * that gets removed from the object cache. Should be save but better | |
46 | * to check. | |
47 | * | |
48 | * - Check if/why INCR will not update the LRU info for the object. | |
49 | * | |
50 | * - Fix/Check the following race condition: a key gets a DEL so there is | |
51 | * a write operation scheduled against this key. Later the same key will | |
52 | * be the argument of a GET, but the write operation was still not | |
53 | * completed (to delete the file). If the GET will be for some reason | |
54 | * a blocking loading (via lookup) we can load the old value on memory. | |
55 | * | |
56 | * This problems can be fixed with negative caching. We can use it | |
57 | * to optimize the system, but also when a key is deleted we mark | |
58 | * it as non existing on disk as well (in a way that this cache | |
59 | * entry can't be evicted, setting time to 0), then we avoid looking at | |
60 | * the disk at all if the key can't be there. When an IO Job complete | |
61 | * a deletion, we set the time of the negative caching to a non zero | |
62 | * value so it will be evicted later. | |
63 | * | |
64 | * Are there other patterns like this where we load stale data? | |
65 | * | |
66 | * Also, make sure that key preloading is ONLY done for keys that are | |
67 | * not marked as cacheKeyDoesNotExist(), otherwise, again, we can load | |
68 | * data from disk that should instead be deleted. | |
69 | * | |
70 | * - dsSet() should use rename(2) in order to avoid corruptions. | |
71 | * | |
72 | * - Don't add a LOAD if there is already a LOADINPROGRESS, or is this | |
73 | * impossible since anyway the io_keys stuff will work as lock? | |
74 | * | |
75 | * - Serialize special encoded things in a raw form. | |
76 | * | |
77 | * - When putting IO read operations on top of the queue, do this only if | |
78 | * the already-on-top operation is not a save or if it is a save that | |
79 | * is scheduled for later execution. If there is a save that is ready to | |
80 | * fire, let's insert the load operation just before the first save that | |
81 | * is scheduled for later exection for instance. | |
82 | * | |
83 | * - Support MULTI/EXEC transactions via a journal file, that is played on | |
84 | * startup to check if there is cleanup to do. This way we can implement | |
85 | * transactions with our simple file based KV store. | |
86 | */ | |
87 | ||
88 | /* Virtual Memory is composed mainly of two subsystems: | |
89 | * - Blocking Virutal Memory | |
90 | * - Threaded Virtual Memory I/O | |
91 | * The two parts are not fully decoupled, but functions are split among two | |
92 | * different sections of the source code (delimited by comments) in order to | |
93 | * make more clear what functionality is about the blocking VM and what about | |
94 | * the threaded (not blocking) VM. | |
95 | * | |
96 | * Redis VM design: | |
97 | * | |
98 | * Redis VM is a blocking VM (one that blocks reading swapped values from | |
99 | * disk into memory when a value swapped out is needed in memory) that is made | |
100 | * unblocking by trying to examine the command argument vector in order to | |
101 | * load in background values that will likely be needed in order to exec | |
102 | * the command. The command is executed only once all the relevant keys | |
103 | * are loaded into memory. | |
104 | * | |
105 | * This basically is almost as simple of a blocking VM, but almost as parallel | |
106 | * as a fully non-blocking VM. | |
107 | */ | |
108 | ||
109 | void spawnIOThread(void); | |
110 | int cacheScheduleIOPushJobs(int flags); | |
111 | int processActiveIOJobs(int max); | |
112 | ||
113 | /* =================== Virtual Memory - Blocking Side ====================== */ | |
114 | ||
115 | void dsInit(void) { | |
116 | int pipefds[2]; | |
117 | size_t stacksize; | |
118 | ||
119 | zmalloc_enable_thread_safeness(); /* we need thread safe zmalloc() */ | |
120 | ||
121 | redisLog(REDIS_NOTICE,"Opening Disk Store: %s", server.ds_path); | |
122 | /* Open Disk Store */ | |
123 | if (dsOpen() != REDIS_OK) { | |
124 | redisLog(REDIS_WARNING,"Fatal error opening disk store. Exiting."); | |
125 | exit(1); | |
126 | }; | |
127 | ||
128 | /* Initialize threaded I/O for Object Cache */ | |
129 | server.io_newjobs = listCreate(); | |
130 | server.io_processing = listCreate(); | |
131 | server.io_processed = listCreate(); | |
132 | server.io_ready_clients = listCreate(); | |
133 | pthread_mutex_init(&server.io_mutex,NULL); | |
134 | pthread_cond_init(&server.io_condvar,NULL); | |
135 | pthread_mutex_init(&server.bgsavethread_mutex,NULL); | |
136 | server.io_active_threads = 0; | |
137 | if (pipe(pipefds) == -1) { | |
138 | redisLog(REDIS_WARNING,"Unable to intialized DS: pipe(2): %s. Exiting." | |
139 | ,strerror(errno)); | |
140 | exit(1); | |
141 | } | |
142 | server.io_ready_pipe_read = pipefds[0]; | |
143 | server.io_ready_pipe_write = pipefds[1]; | |
144 | redisAssert(anetNonBlock(NULL,server.io_ready_pipe_read) != ANET_ERR); | |
145 | /* LZF requires a lot of stack */ | |
146 | pthread_attr_init(&server.io_threads_attr); | |
147 | pthread_attr_getstacksize(&server.io_threads_attr, &stacksize); | |
148 | ||
149 | /* Solaris may report a stacksize of 0, let's set it to 1 otherwise | |
150 | * multiplying it by 2 in the while loop later will not really help ;) */ | |
151 | if (!stacksize) stacksize = 1; | |
152 | ||
153 | while (stacksize < REDIS_THREAD_STACK_SIZE) stacksize *= 2; | |
154 | pthread_attr_setstacksize(&server.io_threads_attr, stacksize); | |
155 | /* Listen for events in the threaded I/O pipe */ | |
156 | if (aeCreateFileEvent(server.el, server.io_ready_pipe_read, AE_READABLE, | |
157 | vmThreadedIOCompletedJob, NULL) == AE_ERR) | |
158 | oom("creating file event"); | |
159 | ||
160 | /* Spawn our I/O thread */ | |
161 | spawnIOThread(); | |
162 | } | |
163 | ||
164 | /* Compute how good candidate the specified object is for eviction. | |
165 | * An higher number means a better candidate. */ | |
166 | double computeObjectSwappability(robj *o) { | |
167 | /* actual age can be >= minage, but not < minage. As we use wrapping | |
168 | * 21 bit clocks with minutes resolution for the LRU. */ | |
169 | return (double) estimateObjectIdleTime(o); | |
170 | } | |
171 | ||
172 | /* Try to free one entry from the diskstore object cache */ | |
173 | int cacheFreeOneEntry(void) { | |
174 | int j, i; | |
175 | struct dictEntry *best = NULL; | |
176 | double best_swappability = 0; | |
177 | redisDb *best_db = NULL; | |
178 | robj *val; | |
179 | sds key; | |
180 | ||
181 | for (j = 0; j < server.dbnum; j++) { | |
182 | redisDb *db = server.db+j; | |
183 | /* Why maxtries is set to 100? | |
184 | * Because this way (usually) we'll find 1 object even if just 1% - 2% | |
185 | * are swappable objects */ | |
186 | int maxtries = 100; | |
187 | ||
188 | for (i = 0; i < 5 && dictSize(db->dict); i++) { | |
189 | dictEntry *de; | |
190 | double swappability; | |
191 | robj keyobj; | |
192 | sds keystr; | |
193 | ||
194 | if (maxtries) maxtries--; | |
195 | de = dictGetRandomKey(db->dict); | |
196 | keystr = dictGetEntryKey(de); | |
197 | val = dictGetEntryVal(de); | |
198 | initStaticStringObject(keyobj,keystr); | |
199 | ||
200 | /* Don't remove objects that are currently target of a | |
201 | * read or write operation. */ | |
202 | if (cacheScheduleIOGetFlags(db,&keyobj) != 0) { | |
203 | if (maxtries) i--; /* don't count this try */ | |
204 | continue; | |
205 | } | |
206 | swappability = computeObjectSwappability(val); | |
207 | if (!best || swappability > best_swappability) { | |
208 | best = de; | |
209 | best_swappability = swappability; | |
210 | best_db = db; | |
211 | } | |
212 | } | |
213 | } | |
214 | if (best == NULL) { | |
215 | /* Not able to free a single object? we should check if our | |
216 | * IO queues have stuff in queue, and try to consume the queue | |
217 | * otherwise we'll use an infinite amount of memory if changes to | |
218 | * the dataset are faster than I/O */ | |
219 | if (listLength(server.cache_io_queue) > 0) { | |
220 | redisLog(REDIS_DEBUG,"--- Busy waiting IO to reclaim memory"); | |
221 | cacheScheduleIOPushJobs(REDIS_IO_ASAP); | |
222 | processActiveIOJobs(1); | |
223 | return REDIS_OK; | |
224 | } | |
225 | /* Nothing to free at all... */ | |
226 | return REDIS_ERR; | |
227 | } | |
228 | key = dictGetEntryKey(best); | |
229 | val = dictGetEntryVal(best); | |
230 | ||
231 | redisLog(REDIS_DEBUG,"Key selected for cache eviction: %s swappability:%f", | |
232 | key, best_swappability); | |
233 | ||
234 | /* Delete this key from memory */ | |
235 | { | |
236 | robj *kobj = createStringObject(key,sdslen(key)); | |
237 | dbDelete(best_db,kobj); | |
238 | decrRefCount(kobj); | |
239 | } | |
240 | return REDIS_OK; | |
241 | } | |
242 | ||
243 | /* ==================== Disk store negative caching ======================== | |
244 | * | |
245 | * When disk store is enabled, we need negative caching, that is, to remember | |
246 | * keys that are for sure *not* on the disk key-value store. | |
247 | * | |
248 | * This is usefuls because without negative caching cache misses will cost us | |
249 | * a disk lookup, even if the same non existing key is accessed again and again. | |
250 | * | |
251 | * With negative caching we remember that the key is not on disk, so if it's | |
252 | * not in memory and we have a negative cache entry, we don't try a disk | |
253 | * access at all. | |
254 | */ | |
255 | ||
256 | /* Returns true if the specified key may exists on disk, that is, we don't | |
257 | * have an entry in our negative cache for this key */ | |
258 | int cacheKeyMayExist(redisDb *db, robj *key) { | |
259 | return dictFind(db->io_negcache,key) == NULL; | |
260 | } | |
261 | ||
262 | /* Set the specified key as an entry that may possibily exist on disk, that is, | |
263 | * remove the negative cache entry for this key if any. */ | |
264 | void cacheSetKeyMayExist(redisDb *db, robj *key) { | |
265 | dictDelete(db->io_negcache,key); | |
266 | } | |
267 | ||
268 | /* Set the specified key as non existing on disk, that is, create a negative | |
269 | * cache entry for this key. */ | |
270 | void cacheSetKeyDoesNotExist(redisDb *db, robj *key) { | |
271 | if (dictReplace(db->io_negcache,key,(void*)time(NULL))) { | |
272 | incrRefCount(key); | |
273 | } | |
274 | } | |
275 | ||
276 | /* Remove one entry from negative cache using approximated LRU. */ | |
277 | int negativeCacheEvictOneEntry(void) { | |
278 | struct dictEntry *de; | |
279 | robj *best = NULL; | |
280 | redisDb *best_db = NULL; | |
281 | time_t time, best_time = 0; | |
282 | int j; | |
283 | ||
284 | for (j = 0; j < server.dbnum; j++) { | |
285 | redisDb *db = server.db+j; | |
286 | int i; | |
287 | ||
288 | if (dictSize(db->io_negcache) == 0) continue; | |
289 | for (i = 0; i < 3; i++) { | |
290 | de = dictGetRandomKey(db->io_negcache); | |
291 | time = (time_t) dictGetEntryVal(de); | |
292 | ||
293 | if (best == NULL || time < best_time) { | |
294 | best = dictGetEntryKey(de); | |
295 | best_db = db; | |
296 | best_time = time; | |
297 | } | |
298 | } | |
299 | } | |
300 | if (best) { | |
301 | dictDelete(best_db->io_negcache,best); | |
302 | return REDIS_OK; | |
303 | } else { | |
304 | return REDIS_ERR; | |
305 | } | |
306 | } | |
307 | ||
308 | /* ================== Disk store cache - Threaded I/O ====================== */ | |
309 | ||
310 | void freeIOJob(iojob *j) { | |
311 | decrRefCount(j->key); | |
312 | /* j->val can be NULL if the job is about deleting the key from disk. */ | |
313 | if (j->val) decrRefCount(j->val); | |
314 | zfree(j); | |
315 | } | |
316 | ||
317 | /* Every time a thread finished a Job, it writes a byte into the write side | |
318 | * of an unix pipe in order to "awake" the main thread, and this function | |
319 | * is called. | |
320 | * | |
321 | * If privdata == NULL the function will try to put more jobs in the queue | |
322 | * of IO jobs to process as more room is made. privdata is equal to NULL | |
323 | * when the function is called from the event loop, so we want to push | |
324 | * more IO jobs in the queue. Instead when the function is called by | |
325 | * other functions that want to create a write-barrier to avoid race | |
326 | * conditions we don't push new jobs in the queue. */ | |
327 | void vmThreadedIOCompletedJob(aeEventLoop *el, int fd, void *privdata, | |
328 | int mask) | |
329 | { | |
330 | char buf[1]; | |
331 | int retval, processed = 0, toprocess = -1; | |
332 | REDIS_NOTUSED(el); | |
333 | REDIS_NOTUSED(mask); | |
334 | ||
335 | /* For every byte we read in the read side of the pipe, there is one | |
336 | * I/O job completed to process. */ | |
337 | while((retval = read(fd,buf,1)) == 1) { | |
338 | iojob *j; | |
339 | listNode *ln; | |
340 | ||
341 | redisLog(REDIS_DEBUG,"Processing I/O completed job"); | |
342 | ||
343 | /* Get the processed element (the oldest one) */ | |
344 | lockThreadedIO(); | |
345 | redisAssert(listLength(server.io_processed) != 0); | |
346 | if (toprocess == -1) { | |
347 | toprocess = (listLength(server.io_processed)*REDIS_MAX_COMPLETED_JOBS_PROCESSED)/100; | |
348 | if (toprocess <= 0) toprocess = 1; | |
349 | } | |
350 | ln = listFirst(server.io_processed); | |
351 | j = ln->value; | |
352 | listDelNode(server.io_processed,ln); | |
353 | unlockThreadedIO(); | |
354 | ||
355 | /* Post process it in the main thread, as there are things we | |
356 | * can do just here to avoid race conditions and/or invasive locks */ | |
357 | redisLog(REDIS_DEBUG,"COMPLETED Job type %s, key: %s", | |
358 | (j->type == REDIS_IOJOB_LOAD) ? "load" : "save", | |
359 | (unsigned char*)j->key->ptr); | |
360 | if (j->type == REDIS_IOJOB_LOAD) { | |
361 | /* Create the key-value pair in the in-memory database */ | |
362 | if (j->val != NULL) { | |
363 | /* Note: it's possible that the key is already in memory | |
364 | * due to a blocking load operation. */ | |
365 | if (dictFind(j->db->dict,j->key->ptr) == NULL) { | |
366 | dbAdd(j->db,j->key,j->val); | |
367 | incrRefCount(j->val); | |
368 | if (j->expire != -1) setExpire(j->db,j->key,j->expire); | |
369 | } | |
370 | } else { | |
371 | /* Key not found on disk. If it is also not in memory | |
372 | * as a cached object, nor there is a job writing it | |
373 | * in background, we are sure the key does not exist | |
374 | * currently. | |
375 | * | |
376 | * So we set a negative cache entry avoiding that the | |
377 | * resumed client will block load what does not exist... */ | |
378 | if (dictFind(j->db->dict,j->key->ptr) == NULL && | |
379 | (cacheScheduleIOGetFlags(j->db,j->key) & | |
380 | (REDIS_IO_SAVE|REDIS_IO_SAVEINPROG)) == 0) | |
381 | { | |
382 | cacheSetKeyDoesNotExist(j->db,j->key); | |
383 | } | |
384 | } | |
385 | cacheScheduleIODelFlag(j->db,j->key,REDIS_IO_LOADINPROG); | |
386 | handleClientsBlockedOnSwappedKey(j->db,j->key); | |
387 | } else if (j->type == REDIS_IOJOB_SAVE) { | |
388 | cacheScheduleIODelFlag(j->db,j->key,REDIS_IO_SAVEINPROG); | |
389 | } | |
390 | freeIOJob(j); | |
391 | processed++; | |
392 | if (privdata == NULL) cacheScheduleIOPushJobs(0); | |
393 | if (processed == toprocess) return; | |
394 | } | |
395 | if (retval < 0 && errno != EAGAIN) { | |
396 | redisLog(REDIS_WARNING, | |
397 | "WARNING: read(2) error in vmThreadedIOCompletedJob() %s", | |
398 | strerror(errno)); | |
399 | } | |
400 | } | |
401 | ||
402 | void lockThreadedIO(void) { | |
403 | pthread_mutex_lock(&server.io_mutex); | |
404 | } | |
405 | ||
406 | void unlockThreadedIO(void) { | |
407 | pthread_mutex_unlock(&server.io_mutex); | |
408 | } | |
409 | ||
410 | void *IOThreadEntryPoint(void *arg) { | |
411 | iojob *j; | |
412 | listNode *ln; | |
413 | REDIS_NOTUSED(arg); | |
414 | long long start; | |
415 | ||
416 | pthread_detach(pthread_self()); | |
417 | lockThreadedIO(); | |
418 | while(1) { | |
419 | /* Get a new job to process */ | |
420 | if (listLength(server.io_newjobs) == 0) { | |
421 | /* Wait for more work to do */ | |
422 | redisLog(REDIS_DEBUG,"[T] wait for signal"); | |
423 | pthread_cond_wait(&server.io_condvar,&server.io_mutex); | |
424 | redisLog(REDIS_DEBUG,"[T] signal received"); | |
425 | continue; | |
426 | } | |
427 | start = ustime(); | |
428 | redisLog(REDIS_DEBUG,"[T] %ld IO jobs to process", | |
429 | listLength(server.io_newjobs)); | |
430 | ln = listFirst(server.io_newjobs); | |
431 | j = ln->value; | |
432 | listDelNode(server.io_newjobs,ln); | |
433 | /* Add the job in the processing queue */ | |
434 | listAddNodeTail(server.io_processing,j); | |
435 | ln = listLast(server.io_processing); /* We use ln later to remove it */ | |
436 | unlockThreadedIO(); | |
437 | ||
438 | redisLog(REDIS_DEBUG,"[T] %ld: new job type %s: %p about key '%s'", | |
439 | (long) pthread_self(), | |
440 | (j->type == REDIS_IOJOB_LOAD) ? "load" : "save", | |
441 | (void*)j, (char*)j->key->ptr); | |
442 | ||
443 | /* Process the Job */ | |
444 | if (j->type == REDIS_IOJOB_LOAD) { | |
445 | time_t expire; | |
446 | ||
447 | j->val = dsGet(j->db,j->key,&expire); | |
448 | if (j->val) j->expire = expire; | |
449 | } else if (j->type == REDIS_IOJOB_SAVE) { | |
450 | if (j->val) { | |
451 | dsSet(j->db,j->key,j->val,j->expire); | |
452 | } else { | |
453 | dsDel(j->db,j->key); | |
454 | } | |
455 | } | |
456 | ||
457 | /* Done: insert the job into the processed queue */ | |
458 | redisLog(REDIS_DEBUG,"[T] %ld completed the job: %p (key %s)", | |
459 | (long) pthread_self(), (void*)j, (char*)j->key->ptr); | |
460 | ||
461 | redisLog(REDIS_DEBUG,"[T] lock IO"); | |
462 | lockThreadedIO(); | |
463 | redisLog(REDIS_DEBUG,"[T] IO locked"); | |
464 | listDelNode(server.io_processing,ln); | |
465 | listAddNodeTail(server.io_processed,j); | |
466 | ||
467 | /* Signal the main thread there is new stuff to process */ | |
468 | redisAssert(write(server.io_ready_pipe_write,"x",1) == 1); | |
469 | redisLog(REDIS_DEBUG,"TIME (%c): %lld\n", j->type == REDIS_IOJOB_LOAD ? 'L' : 'S', ustime()-start); | |
470 | } | |
471 | /* never reached, but that's the full pattern... */ | |
472 | unlockThreadedIO(); | |
473 | return NULL; | |
474 | } | |
475 | ||
476 | void spawnIOThread(void) { | |
477 | pthread_t thread; | |
478 | sigset_t mask, omask; | |
479 | int err; | |
480 | ||
481 | sigemptyset(&mask); | |
482 | sigaddset(&mask,SIGCHLD); | |
483 | sigaddset(&mask,SIGHUP); | |
484 | sigaddset(&mask,SIGPIPE); | |
485 | pthread_sigmask(SIG_SETMASK, &mask, &omask); | |
486 | while ((err = pthread_create(&thread,&server.io_threads_attr,IOThreadEntryPoint,NULL)) != 0) { | |
487 | redisLog(REDIS_WARNING,"Unable to spawn an I/O thread: %s", | |
488 | strerror(err)); | |
489 | usleep(1000000); | |
490 | } | |
491 | pthread_sigmask(SIG_SETMASK, &omask, NULL); | |
492 | server.io_active_threads++; | |
493 | } | |
494 | ||
495 | /* Wait that up to 'max' pending IO Jobs are processed by the I/O thread. | |
496 | * From our point of view an IO job processed means that the count of | |
497 | * server.io_processed must increase by one. | |
498 | * | |
499 | * If max is -1, all the pending IO jobs will be processed. | |
500 | * | |
501 | * Returns the number of IO jobs processed. | |
502 | * | |
503 | * NOTE: while this may appear like a busy loop, we are actually blocked | |
504 | * by IO since we continuously acquire/release the IO lock. */ | |
505 | int processActiveIOJobs(int max) { | |
506 | int processed = 0; | |
507 | ||
508 | while(max == -1 || max > 0) { | |
509 | int io_processed_len; | |
510 | ||
511 | redisLog(REDIS_DEBUG,"[P] lock IO"); | |
512 | lockThreadedIO(); | |
513 | redisLog(REDIS_DEBUG,"Waiting IO jobs processing: new:%d proessing:%d processed:%d",listLength(server.io_newjobs),listLength(server.io_processing),listLength(server.io_processed)); | |
514 | ||
515 | if (listLength(server.io_newjobs) == 0 && | |
516 | listLength(server.io_processing) == 0) | |
517 | { | |
518 | /* There is nothing more to process */ | |
519 | redisLog(REDIS_DEBUG,"[P] Nothing to process, unlock IO, return"); | |
520 | unlockThreadedIO(); | |
521 | break; | |
522 | } | |
523 | ||
524 | #if 1 | |
525 | /* If there are new jobs we need to signal the thread to | |
526 | * process the next one. FIXME: drop this if useless. */ | |
527 | redisLog(REDIS_DEBUG,"[P] waitEmptyIOJobsQueue: new %d, processing %d, processed %d", | |
528 | listLength(server.io_newjobs), | |
529 | listLength(server.io_processing), | |
530 | listLength(server.io_processed)); | |
531 | ||
532 | if (listLength(server.io_newjobs)) { | |
533 | redisLog(REDIS_DEBUG,"[P] There are new jobs, signal"); | |
534 | pthread_cond_signal(&server.io_condvar); | |
535 | } | |
536 | #endif | |
537 | ||
538 | /* Check if we can process some finished job */ | |
539 | io_processed_len = listLength(server.io_processed); | |
540 | redisLog(REDIS_DEBUG,"[P] Unblock IO"); | |
541 | unlockThreadedIO(); | |
542 | redisLog(REDIS_DEBUG,"[P] Wait"); | |
543 | usleep(10000); | |
544 | if (io_processed_len) { | |
545 | vmThreadedIOCompletedJob(NULL,server.io_ready_pipe_read, | |
546 | (void*)0xdeadbeef,0); | |
547 | processed++; | |
548 | if (max != -1) max--; | |
549 | } | |
550 | } | |
551 | return processed; | |
552 | } | |
553 | ||
554 | void waitEmptyIOJobsQueue(void) { | |
555 | processActiveIOJobs(-1); | |
556 | } | |
557 | ||
558 | /* Process up to 'max' IO Jobs already completed by threads but still waiting | |
559 | * processing from the main thread. | |
560 | * | |
561 | * If max == -1 all the pending jobs are processed. | |
562 | * | |
563 | * The number of processed jobs is returned. */ | |
564 | int processPendingIOJobs(int max) { | |
565 | int processed = 0; | |
566 | ||
567 | while(max == -1 || max > 0) { | |
568 | int io_processed_len; | |
569 | ||
570 | lockThreadedIO(); | |
571 | io_processed_len = listLength(server.io_processed); | |
572 | unlockThreadedIO(); | |
573 | if (io_processed_len == 0) break; | |
574 | vmThreadedIOCompletedJob(NULL,server.io_ready_pipe_read, | |
575 | (void*)0xdeadbeef,0); | |
576 | if (max != -1) max--; | |
577 | processed++; | |
578 | } | |
579 | return processed; | |
580 | } | |
581 | ||
582 | void processAllPendingIOJobs(void) { | |
583 | processPendingIOJobs(-1); | |
584 | } | |
585 | ||
586 | /* This function must be called while with threaded IO locked */ | |
587 | void queueIOJob(iojob *j) { | |
588 | redisLog(REDIS_DEBUG,"Queued IO Job %p type %d about key '%s'\n", | |
589 | (void*)j, j->type, (char*)j->key->ptr); | |
590 | listAddNodeTail(server.io_newjobs,j); | |
591 | } | |
592 | ||
593 | /* Consume all the IO scheduled operations, and all the thread IO jobs | |
594 | * so that eventually the state of diskstore is a point-in-time snapshot. | |
595 | * | |
596 | * This is useful when we need to BGSAVE with diskstore enabled. */ | |
597 | void cacheForcePointInTime(void) { | |
598 | redisLog(REDIS_NOTICE,"Diskstore: synching on disk to reach point-in-time state."); | |
599 | while (listLength(server.cache_io_queue) != 0) { | |
600 | cacheScheduleIOPushJobs(REDIS_IO_ASAP); | |
601 | processActiveIOJobs(1); | |
602 | } | |
603 | waitEmptyIOJobsQueue(); | |
604 | processAllPendingIOJobs(); | |
605 | } | |
606 | ||
607 | void cacheCreateIOJob(int type, redisDb *db, robj *key, robj *val, time_t expire) { | |
608 | iojob *j; | |
609 | ||
610 | j = zmalloc(sizeof(*j)); | |
611 | j->type = type; | |
612 | j->db = db; | |
613 | j->key = key; | |
614 | incrRefCount(key); | |
615 | j->val = val; | |
616 | if (val) incrRefCount(val); | |
617 | j->expire = expire; | |
618 | ||
619 | lockThreadedIO(); | |
620 | queueIOJob(j); | |
621 | pthread_cond_signal(&server.io_condvar); | |
622 | unlockThreadedIO(); | |
623 | } | |
624 | ||
625 | /* ============= Disk store cache - Scheduling of IO operations ============= | |
626 | * | |
627 | * We use a queue and an hash table to hold the state of IO operations | |
628 | * so that's fast to lookup if there is already an IO operation in queue | |
629 | * for a given key. | |
630 | * | |
631 | * There are two types of IO operations for a given key: | |
632 | * REDIS_IO_LOAD and REDIS_IO_SAVE. | |
633 | * | |
634 | * The function cacheScheduleIO() function pushes the specified IO operation | |
635 | * in the queue, but avoid adding the same key for the same operation | |
636 | * multiple times, thanks to the associated hash table. | |
637 | * | |
638 | * We take a set of flags per every key, so when the scheduled IO operation | |
639 | * gets moved from the scheduled queue to the actual IO Jobs queue that | |
640 | * is processed by the IO thread, we flag it as IO_LOADINPROG or | |
641 | * IO_SAVEINPROG. | |
642 | * | |
643 | * So for every given key we always know if there is some IO operation | |
644 | * scheduled, or in progress, for this key. | |
645 | * | |
646 | * NOTE: all this is very important in order to guarantee correctness of | |
647 | * the Disk Store Cache. Jobs are always queued here. Load jobs are | |
648 | * queued at the head for faster execution only in the case there is not | |
649 | * already a write operation of some kind for this job. | |
650 | * | |
651 | * So we have ordering, but can do exceptions when there are no already | |
652 | * operations for a given key. Also when we need to block load a given | |
653 | * key, for an immediate lookup operation, we can check if the key can | |
654 | * be accessed synchronously without race conditions (no IN PROGRESS | |
655 | * operations for this key), otherwise we blocking wait for completion. */ | |
656 | ||
657 | #define REDIS_IO_LOAD 1 | |
658 | #define REDIS_IO_SAVE 2 | |
659 | #define REDIS_IO_LOADINPROG 4 | |
660 | #define REDIS_IO_SAVEINPROG 8 | |
661 | ||
662 | void cacheScheduleIOAddFlag(redisDb *db, robj *key, long flag) { | |
663 | struct dictEntry *de = dictFind(db->io_queued,key); | |
664 | ||
665 | if (!de) { | |
666 | dictAdd(db->io_queued,key,(void*)flag); | |
667 | incrRefCount(key); | |
668 | return; | |
669 | } else { | |
670 | long flags = (long) dictGetEntryVal(de); | |
671 | ||
672 | if (flags & flag) { | |
673 | redisLog(REDIS_WARNING,"Adding the same flag again: was: %ld, addede: %ld",flags,flag); | |
674 | redisAssert(!(flags & flag)); | |
675 | } | |
676 | flags |= flag; | |
677 | dictGetEntryVal(de) = (void*) flags; | |
678 | } | |
679 | } | |
680 | ||
681 | void cacheScheduleIODelFlag(redisDb *db, robj *key, long flag) { | |
682 | struct dictEntry *de = dictFind(db->io_queued,key); | |
683 | long flags; | |
684 | ||
685 | redisAssert(de != NULL); | |
686 | flags = (long) dictGetEntryVal(de); | |
687 | redisAssert(flags & flag); | |
688 | flags &= ~flag; | |
689 | if (flags == 0) { | |
690 | dictDelete(db->io_queued,key); | |
691 | } else { | |
692 | dictGetEntryVal(de) = (void*) flags; | |
693 | } | |
694 | } | |
695 | ||
696 | int cacheScheduleIOGetFlags(redisDb *db, robj *key) { | |
697 | struct dictEntry *de = dictFind(db->io_queued,key); | |
698 | ||
699 | return (de == NULL) ? 0 : ((long) dictGetEntryVal(de)); | |
700 | } | |
701 | ||
702 | void cacheScheduleIO(redisDb *db, robj *key, int type) { | |
703 | ioop *op; | |
704 | long flags; | |
705 | ||
706 | if ((flags = cacheScheduleIOGetFlags(db,key)) & type) return; | |
707 | ||
708 | redisLog(REDIS_DEBUG,"Scheduling key %s for %s", | |
709 | key->ptr, type == REDIS_IO_LOAD ? "loading" : "saving"); | |
710 | cacheScheduleIOAddFlag(db,key,type); | |
711 | op = zmalloc(sizeof(*op)); | |
712 | op->type = type; | |
713 | op->db = db; | |
714 | op->key = key; | |
715 | incrRefCount(key); | |
716 | op->ctime = time(NULL); | |
717 | ||
718 | /* Give priority to load operations if there are no save already | |
719 | * in queue for the same key. */ | |
720 | if (type == REDIS_IO_LOAD && !(flags & REDIS_IO_SAVE)) { | |
721 | listAddNodeHead(server.cache_io_queue, op); | |
722 | cacheScheduleIOPushJobs(REDIS_IO_ONLYLOADS); | |
723 | } else { | |
724 | /* FIXME: probably when this happens we want to at least move | |
725 | * the write job about this queue on top, and set the creation time | |
726 | * to a value that will force processing ASAP. */ | |
727 | listAddNodeTail(server.cache_io_queue, op); | |
728 | } | |
729 | } | |
730 | ||
731 | /* Push scheduled IO operations into IO Jobs that the IO thread can process. | |
732 | * | |
733 | * If flags include REDIS_IO_ONLYLOADS only load jobs are processed:this is | |
734 | * useful since it's safe to push LOAD IO jobs from any place of the code, while | |
735 | * SAVE io jobs should never be pushed while we are processing a command | |
736 | * (not protected by lookupKey() that will block on keys in IO_SAVEINPROG | |
737 | * state. | |
738 | * | |
739 | * The REDIS_IO_ASAP flag tells the function to don't wait for the IO job | |
740 | * scheduled completion time, but just do the operation ASAP. This is useful | |
741 | * when we need to reclaim memory from the IO queue. | |
742 | */ | |
743 | #define MAX_IO_JOBS_QUEUE 10 | |
744 | int cacheScheduleIOPushJobs(int flags) { | |
745 | time_t now = time(NULL); | |
746 | listNode *ln; | |
747 | int jobs, topush = 0, pushed = 0; | |
748 | ||
749 | /* Don't push new jobs if there is a threaded BGSAVE in progress. */ | |
750 | if (server.bgsavethread != (pthread_t) -1) return 0; | |
751 | ||
752 | /* Sync stuff on disk, but only if we have less | |
753 | * than MAX_IO_JOBS_QUEUE IO jobs. */ | |
754 | lockThreadedIO(); | |
755 | jobs = listLength(server.io_newjobs); | |
756 | unlockThreadedIO(); | |
757 | ||
758 | topush = MAX_IO_JOBS_QUEUE-jobs; | |
759 | if (topush < 0) topush = 0; | |
760 | if (topush > (signed)listLength(server.cache_io_queue)) | |
761 | topush = listLength(server.cache_io_queue); | |
762 | ||
763 | while((ln = listFirst(server.cache_io_queue)) != NULL) { | |
764 | ioop *op = ln->value; | |
765 | struct dictEntry *de; | |
766 | robj *val; | |
767 | ||
768 | if (!topush) break; | |
769 | topush--; | |
770 | ||
771 | if (op->type != REDIS_IO_LOAD && flags & REDIS_IO_ONLYLOADS) break; | |
772 | ||
773 | /* Don't execute SAVE before the scheduled time for completion */ | |
774 | if (op->type == REDIS_IO_SAVE && !(flags & REDIS_IO_ASAP) && | |
775 | (now - op->ctime) < server.cache_flush_delay) break; | |
776 | ||
777 | /* Don't add a SAVE job in the IO thread queue if there is already | |
778 | * a save in progress for the same key. */ | |
779 | if (op->type == REDIS_IO_SAVE && | |
780 | cacheScheduleIOGetFlags(op->db,op->key) & REDIS_IO_SAVEINPROG) | |
781 | { | |
782 | /* Move the operation at the end of the list if there | |
783 | * are other operations, so we can try to process the next one. | |
784 | * Otherwise break, nothing to do here. */ | |
785 | if (listLength(server.cache_io_queue) > 1) { | |
786 | listDelNode(server.cache_io_queue,ln); | |
787 | listAddNodeTail(server.cache_io_queue,op); | |
788 | continue; | |
789 | } else { | |
790 | break; | |
791 | } | |
792 | } | |
793 | ||
794 | redisLog(REDIS_DEBUG,"Creating IO %s Job for key %s", | |
795 | op->type == REDIS_IO_LOAD ? "load" : "save", op->key->ptr); | |
796 | ||
797 | if (op->type == REDIS_IO_LOAD) { | |
798 | cacheCreateIOJob(REDIS_IOJOB_LOAD,op->db,op->key,NULL,0); | |
799 | } else { | |
800 | time_t expire = -1; | |
801 | ||
802 | /* Lookup the key, in order to put the current value in the IO | |
803 | * Job. Otherwise if the key does not exists we schedule a disk | |
804 | * store delete operation, setting the value to NULL. */ | |
805 | de = dictFind(op->db->dict,op->key->ptr); | |
806 | if (de) { | |
807 | val = dictGetEntryVal(de); | |
808 | expire = getExpire(op->db,op->key); | |
809 | } else { | |
810 | /* Setting the value to NULL tells the IO thread to delete | |
811 | * the key on disk. */ | |
812 | val = NULL; | |
813 | } | |
814 | cacheCreateIOJob(REDIS_IOJOB_SAVE,op->db,op->key,val,expire); | |
815 | } | |
816 | /* Mark the operation as in progress. */ | |
817 | cacheScheduleIODelFlag(op->db,op->key,op->type); | |
818 | cacheScheduleIOAddFlag(op->db,op->key, | |
819 | (op->type == REDIS_IO_LOAD) ? REDIS_IO_LOADINPROG : | |
820 | REDIS_IO_SAVEINPROG); | |
821 | /* Finally remove the operation from the queue. | |
822 | * But we'll have trace of it in the hash table. */ | |
823 | listDelNode(server.cache_io_queue,ln); | |
824 | decrRefCount(op->key); | |
825 | zfree(op); | |
826 | pushed++; | |
827 | } | |
828 | return pushed; | |
829 | } | |
830 | ||
831 | void cacheCron(void) { | |
832 | /* Push jobs */ | |
833 | cacheScheduleIOPushJobs(0); | |
834 | ||
835 | /* Reclaim memory from the object cache */ | |
836 | while (server.ds_enabled && zmalloc_used_memory() > | |
837 | server.cache_max_memory) | |
838 | { | |
839 | int done = 0; | |
840 | ||
841 | if (cacheFreeOneEntry() == REDIS_OK) done++; | |
842 | if (negativeCacheEvictOneEntry() == REDIS_OK) done++; | |
843 | if (done == 0) break; /* nothing more to free */ | |
844 | } | |
845 | } | |
846 | ||
847 | /* ========== Disk store cache - Blocking clients on missing keys =========== */ | |
848 | ||
849 | /* This function makes the clinet 'c' waiting for the key 'key' to be loaded. | |
850 | * If the key is already in memory we don't need to block. | |
851 | * | |
852 | * FIXME: we should try if it's actually better to suspend the client | |
853 | * accessing an object that is being saved, and awake it only when | |
854 | * the saving was completed. | |
855 | * | |
856 | * Otherwise if the key is not in memory, we block the client and start | |
857 | * an IO Job to load it: | |
858 | * | |
859 | * the key is added to the io_keys list in the client structure, and also | |
860 | * in the hash table mapping swapped keys to waiting clients, that is, | |
861 | * server.io_waited_keys. */ | |
862 | int waitForSwappedKey(redisClient *c, robj *key) { | |
863 | struct dictEntry *de; | |
864 | list *l; | |
865 | ||
866 | /* Return ASAP if the key is in memory */ | |
867 | de = dictFind(c->db->dict,key->ptr); | |
868 | if (de != NULL) return 0; | |
869 | ||
870 | /* Don't wait for keys we are sure are not on disk either */ | |
871 | if (!cacheKeyMayExist(c->db,key)) return 0; | |
872 | ||
873 | /* Add the key to the list of keys this client is waiting for. | |
874 | * This maps clients to keys they are waiting for. */ | |
875 | listAddNodeTail(c->io_keys,key); | |
876 | incrRefCount(key); | |
877 | ||
878 | /* Add the client to the swapped keys => clients waiting map. */ | |
879 | de = dictFind(c->db->io_keys,key); | |
880 | if (de == NULL) { | |
881 | int retval; | |
882 | ||
883 | /* For every key we take a list of clients blocked for it */ | |
884 | l = listCreate(); | |
885 | retval = dictAdd(c->db->io_keys,key,l); | |
886 | incrRefCount(key); | |
887 | redisAssert(retval == DICT_OK); | |
888 | } else { | |
889 | l = dictGetEntryVal(de); | |
890 | } | |
891 | listAddNodeTail(l,c); | |
892 | ||
893 | /* Are we already loading the key from disk? If not create a job */ | |
894 | if (de == NULL) { | |
895 | int flags = cacheScheduleIOGetFlags(c->db,key); | |
896 | ||
897 | /* It is possible that even if there are no clients waiting for | |
898 | * a load operation, still we have a load operation in progress. | |
899 | * For instance think to a client performing a GET and then | |
900 | * closing the connection */ | |
901 | if ((flags & (REDIS_IO_LOAD|REDIS_IO_LOADINPROG)) == 0) | |
902 | cacheScheduleIO(c->db,key,REDIS_IO_LOAD); | |
903 | } | |
904 | return 1; | |
905 | } | |
906 | ||
907 | /* Is this client attempting to run a command against swapped keys? | |
908 | * If so, block it ASAP, load the keys in background, then resume it. | |
909 | * | |
910 | * The important idea about this function is that it can fail! If keys will | |
911 | * still be swapped when the client is resumed, this key lookups will | |
912 | * just block loading keys from disk. In practical terms this should only | |
913 | * happen with SORT BY command or if there is a bug in this function. | |
914 | * | |
915 | * Return 1 if the client is marked as blocked, 0 if the client can | |
916 | * continue as the keys it is going to access appear to be in memory. */ | |
917 | int blockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd) { | |
918 | int *keyindex, numkeys, j, i; | |
919 | ||
920 | /* EXEC is a special case, we need to preload all the commands | |
921 | * queued into the transaction */ | |
922 | if (cmd->proc == execCommand) { | |
923 | struct redisCommand *mcmd; | |
924 | robj **margv; | |
925 | int margc; | |
926 | ||
927 | if (!(c->flags & REDIS_MULTI)) return 0; | |
928 | for (i = 0; i < c->mstate.count; i++) { | |
929 | mcmd = c->mstate.commands[i].cmd; | |
930 | margc = c->mstate.commands[i].argc; | |
931 | margv = c->mstate.commands[i].argv; | |
932 | ||
933 | keyindex = getKeysFromCommand(mcmd,margv,margc,&numkeys, | |
934 | REDIS_GETKEYS_PRELOAD); | |
935 | for (j = 0; j < numkeys; j++) { | |
936 | redisLog(REDIS_DEBUG,"Preloading %s", | |
937 | (char*)margv[keyindex[j]]->ptr); | |
938 | waitForSwappedKey(c,margv[keyindex[j]]); | |
939 | } | |
940 | getKeysFreeResult(keyindex); | |
941 | } | |
942 | } else { | |
943 | keyindex = getKeysFromCommand(cmd,c->argv,c->argc,&numkeys, | |
944 | REDIS_GETKEYS_PRELOAD); | |
945 | for (j = 0; j < numkeys; j++) { | |
946 | redisLog(REDIS_DEBUG,"Preloading %s", | |
947 | (char*)c->argv[keyindex[j]]->ptr); | |
948 | waitForSwappedKey(c,c->argv[keyindex[j]]); | |
949 | } | |
950 | getKeysFreeResult(keyindex); | |
951 | } | |
952 | ||
953 | /* If the client was blocked for at least one key, mark it as blocked. */ | |
954 | if (listLength(c->io_keys)) { | |
955 | c->flags |= REDIS_IO_WAIT; | |
956 | aeDeleteFileEvent(server.el,c->fd,AE_READABLE); | |
957 | server.cache_blocked_clients++; | |
958 | return 1; | |
959 | } else { | |
960 | return 0; | |
961 | } | |
962 | } | |
963 | ||
964 | /* Remove the 'key' from the list of blocked keys for a given client. | |
965 | * | |
966 | * The function returns 1 when there are no longer blocking keys after | |
967 | * the current one was removed (and the client can be unblocked). */ | |
968 | int dontWaitForSwappedKey(redisClient *c, robj *key) { | |
969 | list *l; | |
970 | listNode *ln; | |
971 | listIter li; | |
972 | struct dictEntry *de; | |
973 | ||
974 | /* The key object might be destroyed when deleted from the c->io_keys | |
975 | * list (and the "key" argument is physically the same object as the | |
976 | * object inside the list), so we need to protect it. */ | |
977 | incrRefCount(key); | |
978 | ||
979 | /* Remove the key from the list of keys this client is waiting for. */ | |
980 | listRewind(c->io_keys,&li); | |
981 | while ((ln = listNext(&li)) != NULL) { | |
982 | if (equalStringObjects(ln->value,key)) { | |
983 | listDelNode(c->io_keys,ln); | |
984 | break; | |
985 | } | |
986 | } | |
987 | redisAssert(ln != NULL); | |
988 | ||
989 | /* Remove the client form the key => waiting clients map. */ | |
990 | de = dictFind(c->db->io_keys,key); | |
991 | redisAssert(de != NULL); | |
992 | l = dictGetEntryVal(de); | |
993 | ln = listSearchKey(l,c); | |
994 | redisAssert(ln != NULL); | |
995 | listDelNode(l,ln); | |
996 | if (listLength(l) == 0) | |
997 | dictDelete(c->db->io_keys,key); | |
998 | ||
999 | decrRefCount(key); | |
1000 | return listLength(c->io_keys) == 0; | |
1001 | } | |
1002 | ||
1003 | /* Every time we now a key was loaded back in memory, we handle clients | |
1004 | * waiting for this key if any. */ | |
1005 | void handleClientsBlockedOnSwappedKey(redisDb *db, robj *key) { | |
1006 | struct dictEntry *de; | |
1007 | list *l; | |
1008 | listNode *ln; | |
1009 | int len; | |
1010 | ||
1011 | de = dictFind(db->io_keys,key); | |
1012 | if (!de) return; | |
1013 | ||
1014 | l = dictGetEntryVal(de); | |
1015 | len = listLength(l); | |
1016 | /* Note: we can't use something like while(listLength(l)) as the list | |
1017 | * can be freed by the calling function when we remove the last element. */ | |
1018 | while (len--) { | |
1019 | ln = listFirst(l); | |
1020 | redisClient *c = ln->value; | |
1021 | ||
1022 | if (dontWaitForSwappedKey(c,key)) { | |
1023 | /* Put the client in the list of clients ready to go as we | |
1024 | * loaded all the keys about it. */ | |
1025 | listAddNodeTail(server.io_ready_clients,c); | |
1026 | } | |
1027 | } | |
1028 | } |