X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/5d46e370b7da346801e01066e3ed077069d73d3d..9ad853ccde374ed371efee3b1654c6ebb39e06fa:/src/dscache.c diff --git a/src/dscache.c b/src/dscache.c index 3a06b03e..15134cc7 100644 --- a/src/dscache.c +++ b/src/dscache.c @@ -73,6 +73,16 @@ * impossible since anyway the io_keys stuff will work as lock? * * - Serialize special encoded things in a raw form. + * + * - When putting IO read operations on top of the queue, do this only if + * the already-on-top operation is not a save or if it is a save that + * is scheduled for later execution. If there is a save that is ready to + * fire, let's insert the load operation just before the first save that + * is scheduled for later exection for instance. + * + * - Support MULTI/EXEC transactions via a journal file, that is played on + * startup to check if there is cleanup to do. This way we can implement + * transactions with our simple file based KV store. */ /* Virtual Memory is composed mainly of two subsystems: @@ -97,6 +107,8 @@ */ void spawnIOThread(void); +int cacheScheduleIOPushJobs(int flags); +int processActiveIOJobs(int max); /* =================== Virtual Memory - Blocking Side ====================== */ @@ -120,6 +132,7 @@ void dsInit(void) { server.io_ready_clients = listCreate(); pthread_mutex_init(&server.io_mutex,NULL); pthread_cond_init(&server.io_condvar,NULL); + pthread_mutex_init(&server.bgsavethread_mutex,NULL); server.io_active_threads = 0; if (pipe(pipefds) == -1) { redisLog(REDIS_WARNING,"Unable to intialized DS: pipe(2): %s. Exiting." @@ -172,8 +185,7 @@ int cacheFreeOneEntry(void) { * are swappable objects */ int maxtries = 100; - if (dictSize(db->dict) == 0) continue; - for (i = 0; i < 5; i++) { + for (i = 0; i < 5 && dictSize(db->dict); i++) { dictEntry *de; double swappability; robj keyobj; @@ -200,10 +212,17 @@ int cacheFreeOneEntry(void) { } } if (best == NULL) { - /* FIXME: If there are objects that are in the write queue - * so we can't delete them we should block here, at the cost of - * slowness as the object cache memory limit is considered - * n hard limit. */ + /* Was not able to fix a single object... we should check if our + * IO queues have stuff in queue, and try to consume the queue + * otherwise we'll use an infinite amount of memory if changes to + * the dataset are faster than I/O */ + if (listLength(server.cache_io_queue) > 0) { + redisLog(REDIS_DEBUG,"--- Busy waiting IO to reclaim memory"); + cacheScheduleIOPushJobs(REDIS_IO_ASAP); + processActiveIOJobs(1); + return REDIS_OK; + } + /* Nothing to free at all... */ return REDIS_ERR; } key = dictGetEntryKey(best); @@ -304,7 +323,10 @@ void freeIOJob(iojob *j) { /* Every time a thread finished a Job, it writes a byte into the write side * of an unix pipe in order to "awake" the main thread, and this function - * is called. */ + * is called. + * + * If privdata != NULL the function will try to put more jobs in the queue + * of IO jobs to process as more room is made. */ void vmThreadedIOCompletedJob(aeEventLoop *el, int fd, void *privdata, int mask) { @@ -312,7 +334,6 @@ void vmThreadedIOCompletedJob(aeEventLoop *el, int fd, void *privdata, int retval, processed = 0, toprocess = -1; REDIS_NOTUSED(el); REDIS_NOTUSED(mask); - REDIS_NOTUSED(privdata); /* For every byte we read in the read side of the pipe, there is one * I/O job completed to process. */ @@ -356,7 +377,7 @@ void vmThreadedIOCompletedJob(aeEventLoop *el, int fd, void *privdata, * * So we set a negative cache entry avoiding that the * resumed client will block load what does not exist... */ - if (dictFind(j->db->dict,j->key) == NULL && + if (dictFind(j->db->dict,j->key->ptr) == NULL && (cacheScheduleIOGetFlags(j->db,j->key) & (REDIS_IO_SAVE|REDIS_IO_SAVEINPROG)) == 0) { @@ -371,6 +392,7 @@ void vmThreadedIOCompletedJob(aeEventLoop *el, int fd, void *privdata, freeIOJob(j); } processed++; + if (privdata != NULL) cacheScheduleIOPushJobs(0); if (processed == toprocess) return; } if (retval < 0 && errno != EAGAIN) { @@ -392,6 +414,7 @@ void *IOThreadEntryPoint(void *arg) { iojob *j; listNode *ln; REDIS_NOTUSED(arg); + long long start; pthread_detach(pthread_self()); lockThreadedIO(); @@ -399,10 +422,13 @@ void *IOThreadEntryPoint(void *arg) { /* Get a new job to process */ if (listLength(server.io_newjobs) == 0) { /* Wait for more work to do */ + redisLog(REDIS_DEBUG,"[T] wait for signal"); pthread_cond_wait(&server.io_condvar,&server.io_mutex); + redisLog(REDIS_DEBUG,"[T] signal received"); continue; } - redisLog(REDIS_DEBUG,"%ld IO jobs to process", + start = ustime(); + redisLog(REDIS_DEBUG,"[T] %ld IO jobs to process", listLength(server.io_newjobs)); ln = listFirst(server.io_newjobs); j = ln->value; @@ -412,7 +438,7 @@ void *IOThreadEntryPoint(void *arg) { ln = listLast(server.io_processing); /* We use ln later to remove it */ unlockThreadedIO(); - redisLog(REDIS_DEBUG,"Thread %ld: new job type %s: %p about key '%s'", + redisLog(REDIS_DEBUG,"[T] %ld: new job type %s: %p about key '%s'", (long) pthread_self(), (j->type == REDIS_IOJOB_LOAD) ? "load" : "save", (void*)j, (char*)j->key->ptr); @@ -425,22 +451,25 @@ void *IOThreadEntryPoint(void *arg) { if (j->val) j->expire = expire; } else if (j->type == REDIS_IOJOB_SAVE) { if (j->val) { - dsSet(j->db,j->key,j->val); + dsSet(j->db,j->key,j->val,j->expire); } else { dsDel(j->db,j->key); } } /* Done: insert the job into the processed queue */ - redisLog(REDIS_DEBUG,"Thread %ld completed the job: %p (key %s)", + redisLog(REDIS_DEBUG,"[T] %ld completed the job: %p (key %s)", (long) pthread_self(), (void*)j, (char*)j->key->ptr); + redisLog(REDIS_DEBUG,"[T] lock IO"); lockThreadedIO(); + redisLog(REDIS_DEBUG,"[T] IO locked"); listDelNode(server.io_processing,ln); listAddNodeTail(server.io_processed,j); /* Signal the main thread there is new stuff to process */ redisAssert(write(server.io_ready_pipe_write,"x",1) == 1); + redisLog(REDIS_WARNING,"TIME (%c): %lld\n", j->type == REDIS_IOJOB_LOAD ? 'L' : 'S', ustime()-start); } /* never reached, but that's the full pattern... */ unlockThreadedIO(); @@ -466,62 +495,95 @@ void spawnIOThread(void) { server.io_active_threads++; } -/* Wait that all the pending IO Jobs are processed */ -void waitEmptyIOJobsQueue(void) { - while(1) { +/* Wait that up to 'max' pending IO Jobs are processed by the I/O thread. + * From our point of view an IO job processed means that the count of + * server.io_processed must increase by one. + * + * If max is -1, all the pending IO jobs will be processed. + * + * Returns the number of IO jobs processed. + * + * NOTE: while this may appear like a busy loop, we are actually blocked + * by IO since we continuously acquire/release the IO lock. */ +int processActiveIOJobs(int max) { + int processed = 0; + + while(max == -1 || max > 0) { int io_processed_len; + redisLog(REDIS_DEBUG,"[P] lock IO"); lockThreadedIO(); + 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)); + if (listLength(server.io_newjobs) == 0 && listLength(server.io_processing) == 0) { + /* There is nothing more to process */ + redisLog(REDIS_DEBUG,"[P] Nothing to process, unlock IO, return"); unlockThreadedIO(); - return; + break; } + +#if 1 /* If there are new jobs we need to signal the thread to - * process the next one. */ - redisLog(REDIS_DEBUG,"waitEmptyIOJobsQueue: new %d, processing %d", + * process the next one. FIXME: drop this if useless. */ + redisLog(REDIS_DEBUG,"[P] waitEmptyIOJobsQueue: new %d, processing %d, processed %d", listLength(server.io_newjobs), - listLength(server.io_processing)); + listLength(server.io_processing), + listLength(server.io_processed)); - /* FIXME: signal or not? if (listLength(server.io_newjobs)) { + redisLog(REDIS_DEBUG,"[P] There are new jobs, signal"); pthread_cond_signal(&server.io_condvar); } - */ - /* While waiting for empty jobs queue condition we post-process some - * finshed job, as I/O threads may be hanging trying to write against - * the io_ready_pipe_write FD but there are so much pending jobs that - * it's blocking. */ +#endif + + /* Check if we can process some finished job */ io_processed_len = listLength(server.io_processed); + redisLog(REDIS_DEBUG,"[P] Unblock IO"); unlockThreadedIO(); + redisLog(REDIS_DEBUG,"[P] Wait"); + usleep(10000); if (io_processed_len) { vmThreadedIOCompletedJob(NULL,server.io_ready_pipe_read, (void*)0xdeadbeef,0); - /* FIXME: probably wiser to drop this sleeps. Anyway - * the contention on the IO thread will avoid we to loop - * too fast here. */ - usleep(1000); /* 1 millisecond */ - } else { - /* FIXME: same as fixme above. */ - usleep(10000); /* 10 milliseconds */ + processed++; + if (max != -1) max--; } } + return processed; } -/* Process all the IO Jobs already completed by threads but still waiting - * processing from the main thread. */ -void processAllPendingIOJobs(void) { - while(1) { +void waitEmptyIOJobsQueue(void) { + processActiveIOJobs(-1); +} + +/* Process up to 'max' IO Jobs already completed by threads but still waiting + * processing from the main thread. + * + * If max == -1 all the pending jobs are processed. + * + * The number of processed jobs is returned. */ +int processPendingIOJobs(int max) { + int processed = 0; + + while(max == -1 || max > 0) { int io_processed_len; lockThreadedIO(); io_processed_len = listLength(server.io_processed); unlockThreadedIO(); - if (io_processed_len == 0) return; + if (io_processed_len == 0) break; vmThreadedIOCompletedJob(NULL,server.io_ready_pipe_read, (void*)0xdeadbeef,0); + if (max != -1) max--; + processed++; } + return processed; +} + +void processAllPendingIOJobs(void) { + processPendingIOJobs(-1); } /* This function must be called while with threaded IO locked */ @@ -533,7 +595,21 @@ void queueIOJob(iojob *j) { spawnIOThread(); } -void dsCreateIOJob(int type, redisDb *db, robj *key, robj *val) { +/* Consume all the IO scheduled operations, and all the thread IO jobs + * so that eventually the state of diskstore is a point-in-time snapshot. + * + * This is useful when we need to BGSAVE with diskstore enabled. */ +void cacheForcePointInTime(void) { + redisLog(REDIS_NOTICE,"Diskstore: synching on disk to reach point-in-time state."); + while (listLength(server.cache_io_queue) != 0) { + cacheScheduleIOPushJobs(REDIS_IO_ASAP); + processActiveIOJobs(1); + } + waitEmptyIOJobsQueue(); + processAllPendingIOJobs(); +} + +void cacheCreateIOJob(int type, redisDb *db, robj *key, robj *val, time_t expire) { iojob *j; j = zmalloc(sizeof(*j)); @@ -543,6 +619,7 @@ void dsCreateIOJob(int type, redisDb *db, robj *key, robj *val) { incrRefCount(key); j->val = val; if (val) incrRefCount(val); + j->expire = expire; lockThreadedIO(); queueIOJob(j); @@ -647,6 +724,7 @@ void cacheScheduleIO(redisDb *db, robj *key, int type) { * in queue for the same key. */ if (type == REDIS_IO_LOAD && !(flags & REDIS_IO_SAVE)) { listAddNodeHead(server.cache_io_queue, op); + cacheScheduleIOPushJobs(REDIS_IO_ONLYLOADS); } else { /* FIXME: probably when this happens we want to at least move * the write job about this queue on top, and set the creation time @@ -655,83 +733,109 @@ void cacheScheduleIO(redisDb *db, robj *key, int type) { } } -void cacheCron(void) { +/* Push scheduled IO operations into IO Jobs that the IO thread can process. + * + * If flags include REDIS_IO_ONLYLOADS only load jobs are processed:this is + * useful since it's safe to push LOAD IO jobs from any place of the code, while + * SAVE io jobs should never be pushed while we are processing a command + * (not protected by lookupKey() that will block on keys in IO_SAVEINPROG + * state. + * + * The REDIS_IO_ASAP flag tells the function to don't wait for the IO job + * scheduled completion time, but just do the operation ASAP. This is useful + * when we need to reclaim memory from the IO queue. + */ +#define MAX_IO_JOBS_QUEUE 10 +int cacheScheduleIOPushJobs(int flags) { time_t now = time(NULL); listNode *ln; - int jobs, topush = 0; + int jobs, topush = 0, pushed = 0; + + /* Don't push new jobs if there is a threaded BGSAVE in progress. */ + if (server.bgsavethread != (pthread_t) -1) return 0; - /* Sync stuff on disk, but only if we have less than 100 IO jobs */ + /* Sync stuff on disk, but only if we have less + * than MAX_IO_JOBS_QUEUE IO jobs. */ lockThreadedIO(); jobs = listLength(server.io_newjobs); unlockThreadedIO(); - topush = 100-jobs; + topush = MAX_IO_JOBS_QUEUE-jobs; if (topush < 0) topush = 0; if (topush > (signed)listLength(server.cache_io_queue)) topush = listLength(server.cache_io_queue); while((ln = listFirst(server.cache_io_queue)) != NULL) { ioop *op = ln->value; + struct dictEntry *de; + robj *val; if (!topush) break; topush--; - if (op->type == REDIS_IO_LOAD || - (now - op->ctime) >= server.cache_flush_delay) + if (op->type != REDIS_IO_LOAD && flags & REDIS_IO_ONLYLOADS) break; + + /* Don't execute SAVE before the scheduled time for completion */ + if (op->type == REDIS_IO_SAVE && !(flags & REDIS_IO_ASAP) && + (now - op->ctime) < server.cache_flush_delay) break; + + /* Don't add a SAVE job in the IO thread queue if there is already + * a save in progress for the same key. */ + if (op->type == REDIS_IO_SAVE && + cacheScheduleIOGetFlags(op->db,op->key) & REDIS_IO_SAVEINPROG) { - struct dictEntry *de; - robj *val; - - /* Don't add a SAVE job in queue if there is already - * a save in progress for the same key. */ - if (op->type == REDIS_IO_SAVE && - cacheScheduleIOGetFlags(op->db,op->key) & REDIS_IO_SAVEINPROG) - { - /* Move the operation at the end of the list of there - * are other operations. Otherwise break, nothing to do - * here. */ - if (listLength(server.cache_io_queue) > 1) { - listDelNode(server.cache_io_queue,ln); - listAddNodeTail(server.cache_io_queue,op); - continue; - } else { - break; - } + /* Move the operation at the end of the list if there + * are other operations, so we can try to process the next one. + * Otherwise break, nothing to do here. */ + if (listLength(server.cache_io_queue) > 1) { + listDelNode(server.cache_io_queue,ln); + listAddNodeTail(server.cache_io_queue,op); + continue; + } else { + break; } + } - redisLog(REDIS_DEBUG,"Creating IO %s Job for key %s", - op->type == REDIS_IO_LOAD ? "load" : "save", op->key->ptr); + redisLog(REDIS_DEBUG,"Creating IO %s Job for key %s", + op->type == REDIS_IO_LOAD ? "load" : "save", op->key->ptr); - if (op->type == REDIS_IO_LOAD) { - dsCreateIOJob(REDIS_IOJOB_LOAD,op->db,op->key,NULL); + if (op->type == REDIS_IO_LOAD) { + cacheCreateIOJob(REDIS_IOJOB_LOAD,op->db,op->key,NULL,0); + } else { + time_t expire = -1; + + /* Lookup the key, in order to put the current value in the IO + * Job. Otherwise if the key does not exists we schedule a disk + * store delete operation, setting the value to NULL. */ + de = dictFind(op->db->dict,op->key->ptr); + if (de) { + val = dictGetEntryVal(de); + expire = getExpire(op->db,op->key); } else { - /* Lookup the key, in order to put the current value in the IO - * Job. Otherwise if the key does not exists we schedule a disk - * store delete operation, setting the value to NULL. */ - de = dictFind(op->db->dict,op->key->ptr); - if (de) { - val = dictGetEntryVal(de); - } else { - /* Setting the value to NULL tells the IO thread to delete - * the key on disk. */ - val = NULL; - } - dsCreateIOJob(REDIS_IOJOB_SAVE,op->db,op->key,val); + /* Setting the value to NULL tells the IO thread to delete + * the key on disk. */ + val = NULL; } - /* Mark the operation as in progress. */ - cacheScheduleIODelFlag(op->db,op->key,op->type); - cacheScheduleIOAddFlag(op->db,op->key, - (op->type == REDIS_IO_LOAD) ? REDIS_IO_LOADINPROG : - REDIS_IO_SAVEINPROG); - /* Finally remove the operation from the queue. - * But we'll have trace of it in the hash table. */ - listDelNode(server.cache_io_queue,ln); - decrRefCount(op->key); - zfree(op); - } else { - break; /* too early */ + cacheCreateIOJob(REDIS_IOJOB_SAVE,op->db,op->key,val,expire); } + /* Mark the operation as in progress. */ + cacheScheduleIODelFlag(op->db,op->key,op->type); + cacheScheduleIOAddFlag(op->db,op->key, + (op->type == REDIS_IO_LOAD) ? REDIS_IO_LOADINPROG : + REDIS_IO_SAVEINPROG); + /* Finally remove the operation from the queue. + * But we'll have trace of it in the hash table. */ + listDelNode(server.cache_io_queue,ln); + decrRefCount(op->key); + zfree(op); + pushed++; } + return pushed; +} + +void cacheCron(void) { + /* Push jobs */ + cacheScheduleIOPushJobs(0); /* Reclaim memory from the object cache */ while (server.ds_enabled && zmalloc_used_memory() >