Net: improve prepareClientToWrite() error handling and comments.

When we fail to setup the write handler it does not make sense to take
the client around, it is missing writes: whatever is a client or a slave
anyway the connection should terminated ASAP.

Moreover what the function does exactly with its return value, and in
which case the write handler is installed on the socket, was not clear,
so the functions comment are improved to make the goals of the function
more obvious.

Also related to #2485.
This commit is contained in:
antirez 2015-04-01 10:07:08 +02:00
parent d5a35c3946
commit 6c60526db9
2 changed files with 37 additions and 10 deletions

View File

@ -135,23 +135,49 @@ redisClient *createClient(int fd) {
* returns REDIS_OK, and make sure to install the write handler in our event * returns REDIS_OK, and make sure to install the write handler in our event
* loop so that when the socket is writable new data gets written. * loop so that when the socket is writable new data gets written.
* *
* If the client should not receive new data, because it is a fake client, * If the client should not receive new data, because it is a fake client
* a master, a slave not yet online, or because the setup of the write handler * (used to load AOF in memory), a master or because the setup of the write
* failed, the function returns REDIS_ERR. * handler failed, the function returns REDIS_ERR.
*
* The function may return REDIS_OK without actually installing the write
* event handler in the following cases:
*
* 1) The event handler should already be installed since the output buffer
* already contained something.
* 2) The client is a slave but not yet online, so we want to just accumulate
* writes in the buffer but not actually sending them yet.
* *
* Typically gets called every time a reply is built, before adding more * Typically gets called every time a reply is built, before adding more
* data to the clients output buffers. If the function returns REDIS_ERR no * data to the clients output buffers. If the function returns REDIS_ERR no
* data should be appended to the output buffers. */ * data should be appended to the output buffers. */
int prepareClientToWrite(redisClient *c) { int prepareClientToWrite(redisClient *c) {
/* If it's the Lua client we always return ok without installing any
* handler since there is no socket at all. */
if (c->flags & REDIS_LUA_CLIENT) return REDIS_OK; if (c->flags & REDIS_LUA_CLIENT) return REDIS_OK;
/* Masters don't receive replies, unless REDIS_MASTER_FORCE_REPLY flag
* is set. */
if ((c->flags & REDIS_MASTER) && if ((c->flags & REDIS_MASTER) &&
!(c->flags & REDIS_MASTER_FORCE_REPLY)) return REDIS_ERR; !(c->flags & REDIS_MASTER_FORCE_REPLY)) return REDIS_ERR;
if (c->fd <= 0) return REDIS_ERR; /* Fake client */
if (c->fd <= 0) return REDIS_ERR; /* Fake client for AOF loading. */
/* Only install the handler if not already installed and, in case of
* slaves, if the client can actually receive writes. */
if (c->bufpos == 0 && listLength(c->reply) == 0 && if (c->bufpos == 0 && listLength(c->reply) == 0 &&
(c->replstate == REDIS_REPL_NONE || (c->replstate == REDIS_REPL_NONE ||
c->replstate == REDIS_REPL_ONLINE) && !c->repl_put_online_on_ack && (c->replstate == REDIS_REPL_ONLINE && !c->repl_put_online_on_ack)))
aeCreateFileEvent(server.el, c->fd, AE_WRITABLE, {
sendReplyToClient, c) == AE_ERR) return REDIS_ERR; /* Try to install the write handler. */
if (aeCreateFileEvent(server.el, c->fd, AE_WRITABLE,
sendReplyToClient, c) == AE_ERR)
{
freeClientAsync(c);
return REDIS_ERR;
}
}
/* Authorize the caller to queue in the output buffer of this client. */
return REDIS_OK; return REDIS_OK;
} }

View File

@ -652,7 +652,8 @@ void replconfCommand(redisClient *c) {
* *
* It does a few things: * It does a few things:
* *
* 1) Put the slave in ONLINE state. * 1) Put the slave in ONLINE state (useless when the function is called
* because state is already ONLINE but repl_put_online_on_ack is true).
* 2) Make sure the writable event is re-installed, since calling the SYNC * 2) Make sure the writable event is re-installed, since calling the SYNC
* command disables it, so that we can accumulate output buffer without * command disables it, so that we can accumulate output buffer without
* sending it to the slave. * sending it to the slave.
@ -660,7 +661,7 @@ void replconfCommand(redisClient *c) {
void putSlaveOnline(redisClient *slave) { void putSlaveOnline(redisClient *slave) {
slave->replstate = REDIS_REPL_ONLINE; slave->replstate = REDIS_REPL_ONLINE;
slave->repl_put_online_on_ack = 0; slave->repl_put_online_on_ack = 0;
slave->repl_ack_time = server.unixtime; slave->repl_ack_time = server.unixtime; /* Prevent false timeout. */
if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE, if (aeCreateFileEvent(server.el, slave->fd, AE_WRITABLE,
sendReplyToClient, slave) == AE_ERR) { sendReplyToClient, slave) == AE_ERR) {
redisLog(REDIS_WARNING,"Unable to register writable event for slave bulk transfer: %s", strerror(errno)); redisLog(REDIS_WARNING,"Unable to register writable event for slave bulk transfer: %s", strerror(errno));
@ -773,7 +774,7 @@ void updateSlavesWaitingBgsave(int bgsaveerr, int type) {
* is technically online now. */ * is technically online now. */
slave->replstate = REDIS_REPL_ONLINE; slave->replstate = REDIS_REPL_ONLINE;
slave->repl_put_online_on_ack = 1; slave->repl_put_online_on_ack = 1;
slave->repl_ack_time = server.unixtime; slave->repl_ack_time = server.unixtime; /* Timeout otherwise. */
} else { } else {
if (bgsaveerr != REDIS_OK) { if (bgsaveerr != REDIS_OK) {
freeClient(slave); freeClient(slave);