X-Git-Url: https://git.saurik.com/redis.git/blobdiff_plain/bbaeda402cac1b66b52a869bfc419001d79ffcc0..2ea41242f669c1afe03af7c8befb140a7a80fc4e:/src/networking.c diff --git a/src/networking.c b/src/networking.c index 06097b58..84413463 100644 --- a/src/networking.c +++ b/src/networking.c @@ -43,6 +43,7 @@ redisClient *createClient(int fd) { c->fd = fd; c->bufpos = 0; c->querybuf = sdsempty(); + c->querybuf_peak = 0; c->reqtype = 0; c->argc = 0; c->argv = NULL; @@ -51,9 +52,10 @@ redisClient *createClient(int fd) { c->bulklen = -1; c->sentlen = 0; c->flags = 0; - c->ctime = c->lastinteraction = time(NULL); + c->ctime = c->lastinteraction = server.unixtime; c->authenticated = 0; c->replstate = REDIS_REPL_NONE; + c->slave_listening_port = 0; c->reply = listCreate(); c->reply_bytes = 0; c->obuf_soft_limit_reached_time = 0; @@ -363,7 +365,10 @@ void setDeferredMultiBulkLength(redisClient *c, void *node, long length) { /* Only glue when the next node is non-NULL (an sds in this case) */ if (next->ptr != NULL) { + c->reply_bytes -= zmalloc_size_sds(len->ptr); + c->reply_bytes -= zmalloc_size_sds(next->ptr); len->ptr = sdscatlen(len->ptr,next->ptr,sdslen(next->ptr)); + c->reply_bytes += zmalloc_size_sds(len->ptr); listDelNode(c->reply,ln->next); } } @@ -481,7 +486,7 @@ void copyClientOutputBuffer(redisClient *dst, redisClient *src) { dst->reply_bytes = src->reply_bytes; } -static void acceptCommonHandler(int fd) { +static void acceptCommonHandler(int fd, int flags) { redisClient *c; if ((c = createClient(fd)) == NULL) { redisLog(REDIS_WARNING,"Error allocating resoures for the client"); @@ -504,6 +509,7 @@ static void acceptCommonHandler(int fd) { return; } server.stat_numconnections++; + c->flags |= flags; } void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) { @@ -519,7 +525,7 @@ void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) { return; } redisLog(REDIS_VERBOSE,"Accepted %s:%d", cip, cport); - acceptCommonHandler(cfd); + acceptCommonHandler(cfd,0); } void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask) { @@ -534,7 +540,7 @@ void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask) { return; } redisLog(REDIS_VERBOSE,"Accepted connection to %s", server.unixsocket); - acceptCommonHandler(cfd); + acceptCommonHandler(cfd,REDIS_UNIX_SOCKET); } @@ -546,6 +552,16 @@ static void freeClientArgv(redisClient *c) { c->cmd = NULL; } +/* Close all the slaves connections. This is useful in chained replication + * when we resync with our own master and want to force all our slaves to + * resync with us as well. */ +void disconnectSlaves(void) { + while (listLength(server.slaves)) { + listNode *ln = listFirst(server.slaves); + freeClient((redisClient*)ln->value); + } +} + void freeClient(redisClient *c) { listNode *ln; @@ -603,22 +619,13 @@ void freeClient(redisClient *c) { if (c->flags & REDIS_MASTER) { server.master = NULL; server.repl_state = REDIS_REPL_CONNECT; - server.repl_down_since = time(NULL); - /* Since we lost the connection with the master, we should also - * close the connection with all our slaves if we have any, so - * when we'll resync with the master the other slaves will sync again - * with us as well. Note that also when the slave is not connected - * to the master it will keep refusing connections by other slaves. + server.repl_down_since = server.unixtime; + /* We lost connection with our master, force our slaves to resync + * with us as well to load the new data set. * - * We do this only if server.masterhost != NULL. If it is NULL this - * means the user called SLAVEOF NO ONE and we are freeing our - * link with the master, so no need to close link with slaves. */ - if (server.masterhost != NULL) { - while (listLength(server.slaves)) { - ln = listFirst(server.slaves); - freeClient((redisClient*)ln->value); - } - } + * If server.masterhost is NULL the user called SLAVEOF NO ONE so + * slave resync is not needed. */ + if (server.masterhost != NULL) disconnectSlaves(); } /* If this client was scheduled for async freeing we need to remove it @@ -731,7 +738,7 @@ void sendReplyToClient(aeEventLoop *el, int fd, void *privdata, int mask) { return; } } - if (totwritten > 0) c->lastinteraction = time(NULL); + if (totwritten > 0) c->lastinteraction = server.unixtime; if (c->bufpos == 0 && listLength(c->reply) == 0) { c->sentlen = 0; aeDeleteFileEvent(server.el,c->fd,AE_WRITABLE); @@ -751,34 +758,6 @@ void resetClient(redisClient *c) { if (!(c->flags & REDIS_MULTI)) c->flags &= (~REDIS_ASKING); } -void closeTimedoutClients(void) { - redisClient *c; - listNode *ln; - time_t now = time(NULL); - listIter li; - - listRewind(server.clients,&li); - while ((ln = listNext(&li)) != NULL) { - c = listNodeValue(ln); - if (server.maxidletime && - !(c->flags & REDIS_SLAVE) && /* no timeout for slaves */ - !(c->flags & REDIS_MASTER) && /* no timeout for masters */ - !(c->flags & REDIS_BLOCKED) && /* no timeout for BLPOP */ - dictSize(c->pubsub_channels) == 0 && /* no timeout for pubsub */ - listLength(c->pubsub_patterns) == 0 && - (now - c->lastinteraction > server.maxidletime)) - { - redisLog(REDIS_VERBOSE,"Closing idle client"); - freeClient(c); - } else if (c->flags & REDIS_BLOCKED) { - if (c->bpop.timeout != 0 && c->bpop.timeout < now) { - addReply(c,shared.nullmultibulk); - unblockClientWaitingData(c); - } - } - } -} - int processInlineBuffer(redisClient *c) { char *newline = strstr(c->querybuf,"\r\n"); int argc, j; @@ -1026,6 +1005,7 @@ void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) { } qblen = sdslen(c->querybuf); + if (c->querybuf_peak < qblen) c->querybuf_peak = qblen; c->querybuf = sdsMakeRoomFor(c->querybuf, readlen); nread = read(fd, c->querybuf+qblen, readlen); if (nread == -1) { @@ -1043,7 +1023,7 @@ void readQueryFromClient(aeEventLoop *el, int fd, void *privdata, int mask) { } if (nread) { sdsIncrLen(c->querybuf,nread); - c->lastinteraction = time(NULL); + c->lastinteraction = server.unixtime; } else { server.current_client = NULL; return; @@ -1083,11 +1063,11 @@ void getClientsMaxBuffers(unsigned long *longest_output_list, /* Turn a Redis client into an sds string representing its state. */ sds getClientInfoString(redisClient *client) { char ip[32], flags[16], events[3], *p; - int port; - time_t now = time(NULL); + int port = 0; /* initialized to zero for the unix socket case. */ int emask; - anetPeerToString(client->fd,ip,&port); + if (!(client->flags & REDIS_UNIX_SOCKET)) + anetPeerToString(client->fd,ip,&port); p = flags; if (client->flags & REDIS_SLAVE) { if (client->flags & REDIS_MONITOR) @@ -1102,6 +1082,7 @@ sds getClientInfoString(redisClient *client) { if (client->flags & REDIS_CLOSE_AFTER_REPLY) *p++ = 'c'; if (client->flags & REDIS_UNBLOCKED) *p++ = 'u'; if (client->flags & REDIS_CLOSE_ASAP) *p++ = 'A'; + if (client->flags & REDIS_UNIX_SOCKET) *p++ = 'U'; if (p == flags) *p++ = 'N'; *p++ = '\0'; @@ -1111,14 +1092,16 @@ sds getClientInfoString(redisClient *client) { if (emask & AE_WRITABLE) *p++ = 'w'; *p = '\0'; return sdscatprintf(sdsempty(), - "addr=%s:%d fd=%d age=%ld idle=%ld flags=%s db=%d sub=%d psub=%d qbuf=%lu qbuf-free=%lu obl=%lu oll=%lu omem=%lu events=%s cmd=%s", - ip,port,client->fd, - (long)(now - client->ctime), - (long)(now - client->lastinteraction), + "addr=%s:%d fd=%d age=%ld idle=%ld flags=%s db=%d sub=%d psub=%d multi=%d qbuf=%lu qbuf-free=%lu obl=%lu oll=%lu omem=%lu events=%s cmd=%s", + (client->flags & REDIS_UNIX_SOCKET) ? server.unixsocket : ip, + port,client->fd, + (long)(server.unixtime - client->ctime), + (long)(server.unixtime - client->lastinteraction), flags, client->db->id, (int) dictSize(client->pubsub_channels), (int) listLength(client->pubsub_patterns), + (client->flags & REDIS_MULTI) ? client->mstate.count : -1, (unsigned long) sdslen(client->querybuf), (unsigned long) sdsavail(client->querybuf), (unsigned long) client->bufpos, @@ -1327,6 +1310,7 @@ int checkClientOutputBufferLimits(redisClient *c) { * called from contexts where the client can't be freed safely, i.e. from the * lower level functions pushing data inside the client output buffers. */ void asyncCloseClientOnOutputBufferLimitReached(redisClient *c) { + redisAssert(c->reply_bytes < ULONG_MAX-(1024*64)); if (c->reply_bytes == 0 || c->flags & REDIS_CLOSE_ASAP) return; if (checkClientOutputBufferLimits(c)) { sds client = getClientInfoString(c);