]> git.saurik.com Git - redis.git/blobdiff - src/redis.c
Var renamed into pushGenericCommand() to better reflect what it means.
[redis.git] / src / redis.c
index 4b9ba618d570562a9c73c633d4d4fb6bcba2010a..647c586736e3961796ddc4cc3fdb51c6672764ac 100644 (file)
@@ -1175,10 +1175,33 @@ struct redisCommand *lookupCommandByCString(char *s) {
     return cmd;
 }
 
+/* Propagate the specified command (in the context of the specified database id)
+ * to AOF, Slaves and Monitors.
+ *
+ * flags are an xor between:
+ * + REDIS_PROPAGATE_NONE (no propagation of command at all)
+ * + REDIS_PROPAGATE_AOF (propagate into the AOF file if is enabled)
+ * + REDIS_PROPAGATE_REPL (propagate into the replication link)
+ */
+void propagate(struct redisCommand *cmd, int dbid, robj **argv, int argc,
+               int flags)
+{
+    if (server.aof_state != REDIS_AOF_OFF && flags & REDIS_PROPAGATE_AOF)
+        feedAppendOnlyFile(cmd,dbid,argv,argc);
+    if (flags & REDIS_PROPAGATE_REPL && listLength(server.slaves))
+        replicationFeedSlaves(server.slaves,dbid,argv,argc);
+}
+
 /* Call() is the core of Redis execution of a command */
 void call(redisClient *c, int flags) {
     long long dirty, start = ustime(), duration;
 
+    /* Sent the command to clients in MONITOR mode, only if the commands are
+     * not geneated from reading an AOF. */
+    if (listLength(server.monitors) && !server.loading)
+        replicationFeedMonitors(server.monitors,c->db->id,c->argv,c->argc);
+
+    /* Call the command. */
     dirty = server.dirty;
     c->cmd->proc(c);
     dirty = server.dirty-dirty;
@@ -1189,20 +1212,25 @@ void call(redisClient *c, int flags) {
     if (server.loading && c->flags & REDIS_LUA_CLIENT)
         flags &= ~(REDIS_CALL_SLOWLOG | REDIS_CALL_STATS);
 
+    /* Log the command into the Slow log if needed, and populate the
+     * per-command statistics that we show in INFO commandstats. */
     if (flags & REDIS_CALL_SLOWLOG)
         slowlogPushEntryIfNeeded(c->argv,c->argc,duration);
     if (flags & REDIS_CALL_STATS) {
         c->cmd->microseconds += duration;
         c->cmd->calls++;
     }
+
+    /* Propagate the command into the AOF and replication link */
     if (flags & REDIS_CALL_PROPAGATE) {
-        if (server.aof_state != REDIS_AOF_OFF && dirty > 0)
-            feedAppendOnlyFile(c->cmd,c->db->id,c->argv,c->argc);
-        if ((dirty > 0 || c->cmd->flags & REDIS_CMD_FORCE_REPLICATION) &&
-            listLength(server.slaves))
-            replicationFeedSlaves(server.slaves,c->db->id,c->argv,c->argc);
-        if (listLength(server.monitors))
-            replicationFeedMonitors(server.monitors,c->db->id,c->argv,c->argc);
+        int flags = REDIS_PROPAGATE_NONE;
+
+        if (c->cmd->flags & REDIS_CMD_FORCE_REPLICATION)
+            flags |= REDIS_PROPAGATE_REPL;
+        if (dirty)
+            flags |= (REDIS_PROPAGATE_REPL | REDIS_PROPAGATE_AOF);
+        if (flags != REDIS_PROPAGATE_NONE)
+            propagate(c->cmd,c->db->id,c->argv,c->argc,flags);
     }
     server.stat_numcommands++;
 }
@@ -1832,7 +1860,6 @@ int freeMemoryIfNeeded(void) {
 
     /* Compute how much memory we need to free. */
     mem_tofree = mem_used - server.maxmemory;
-    printf("USED: %zu, TOFREE: %zu\n", mem_used, mem_tofree);
     mem_freed = 0;
     while (mem_freed < mem_tofree) {
         int j, k, keys_freed = 0;
@@ -1923,7 +1950,6 @@ int freeMemoryIfNeeded(void) {
                 delta = (long long) zmalloc_used_memory();
                 dbDelete(db,keyobj);
                 delta -= (long long) zmalloc_used_memory();
-                // printf("%lld\n",delta);
                 mem_freed += delta;
                 server.stat_evictedkeys++;
                 decrRefCount(keyobj);
@@ -1933,27 +1959,7 @@ int freeMemoryIfNeeded(void) {
                  * start spending so much time here that is impossible to
                  * deliver data to the slaves fast enough, so we force the
                  * transmission here inside the loop. */
-                if (slaves) {
-                    listIter li;
-                    listNode *ln;
-
-                    listRewind(server.slaves,&li);
-                    while((ln = listNext(&li))) {
-                        redisClient *slave = listNodeValue(ln);
-                        int events;
-
-                        events = aeGetFileEvents(server.el,slave->fd);
-                        printf("EVENTS: %d\n", events);
-                        if (events & AE_WRITABLE &&
-                            slave->replstate == REDIS_REPL_ONLINE &&
-                            listLength(slave->reply))
-                        {
-                            printf("SLAVE %d -> %d\n",
-                                slave->fd, (int) listLength(slave->reply));
-                            sendReplyToClient(server.el,slave->fd,slave,0);
-                        }
-                    }
-                }
+                if (slaves) flushSlavesOutputBuffers();
             }
         }
         if (!keys_freed) return REDIS_ERR; /* nothing to free... */