Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5eec376c2f | ||
|
|
99d7dbe669 | ||
|
|
a61705dd95 | ||
|
|
16144589f2 | ||
|
|
a25b25f4ef | ||
|
|
ab55180883 | ||
|
|
a03c32702b | ||
|
|
2164523244 | ||
|
|
c3ff470889 | ||
|
|
0e25c0ccf4 | ||
|
|
f4eb4b3352 | ||
|
|
500dddc264 | ||
|
|
a6305f13d5 | ||
|
|
99c3338c23 | ||
|
|
05e06e1543 | ||
|
|
e2f2dab3ef | ||
|
|
e480c279bc | ||
|
|
0c19880ce6 | ||
|
|
2ba962714a | ||
|
|
151b606c11 | ||
|
|
f1466e11cd | ||
|
|
e061d797d7 | ||
|
|
04779bdfe1 | ||
|
|
e04be06e89 | ||
|
|
c4cbffa3a1 | ||
|
|
dfb7194cba | ||
|
|
b8ce9a84c5 | ||
|
|
1f8bd82332 | ||
|
|
ef792fc950 | ||
|
|
de499f7f7e | ||
|
|
b65f3c2176 | ||
|
|
161e137c55 | ||
|
|
d480b9ce7f | ||
|
|
fa23fc3363 | ||
|
|
fc0a0d4aa7 | ||
|
|
ea9bec50c6 | ||
|
|
26a340095d | ||
|
|
524b79d231 | ||
|
|
201ed6d42e | ||
|
|
7c9bfe1036 | ||
|
|
3da75e2ca4 | ||
|
|
e328e41a3a | ||
|
|
8a8e560b87 | ||
|
|
0d0975f2b6 | ||
|
|
af41f6cffb | ||
|
|
999fe0d352 | ||
|
|
f1057534e7 | ||
|
|
fcc8bf9974 | ||
|
|
374eed7d2a | ||
|
|
2085fdb1f4 | ||
|
|
f8a19e32e2 | ||
|
|
7c39b55d42 | ||
|
|
e47236d8d4 | ||
|
|
8ab7e998d1 | ||
|
|
e01a415d37 | ||
|
|
d26a8fb4db | ||
|
|
5b5eb192f5 | ||
|
|
120ba3922d | ||
|
|
2812b945f0 | ||
|
|
31fe053a62 | ||
|
|
dd94771578 | ||
|
|
8b6b1b27cc | ||
|
|
4403862623 | ||
|
|
174518ffb7 | ||
|
|
f444e2afdc | ||
|
|
b58f03a0e8 | ||
|
|
efb54f0593 | ||
|
|
58889867bb | ||
|
|
4c3d419013 | ||
|
|
0671d88cab | ||
|
|
5ddee9b7d5 | ||
|
|
fd2a8951bf | ||
|
|
42a239b888 | ||
|
|
851ac9d072 | ||
|
|
48d26a483d | ||
|
|
edfaa64f49 | ||
|
|
13732168a5 | ||
|
|
06bd3b9acd | ||
|
|
5de75120ba | ||
|
|
32095c4057 | ||
|
|
7fcba9fd9a | ||
|
|
2f44452612 | ||
|
|
89bf6f58fd | ||
|
|
013189e7db | ||
|
|
73d3e8751b | ||
|
|
628890e43e | ||
|
|
4c0c1fff5a | ||
|
|
f00b0844c9 | ||
|
|
889e443ce5 | ||
|
|
3bb3f12539 | ||
|
|
82675c86a6 | ||
|
|
8a8e01f4a7 | ||
|
|
d3d567428a | ||
|
|
dbd8c753c4 | ||
|
|
b3f28b90d2 | ||
|
|
4b3865cbdb | ||
|
|
0c9cf45270 | ||
|
|
6fe9c402a2 | ||
|
|
8361d6c406 | ||
|
|
e612508d38 | ||
|
|
1364395f18 | ||
|
|
0b8441c1b2 | ||
|
|
a85c89854f | ||
|
|
0ea1a9c452 |
+2
-6
@@ -2,11 +2,7 @@
|
||||
*.o
|
||||
*.rdb
|
||||
*.log
|
||||
redis-cli
|
||||
redis-server
|
||||
redis-benchmark
|
||||
redis-check-dump
|
||||
redis-check-aof
|
||||
redis-*
|
||||
doc-tools
|
||||
release
|
||||
misc/*
|
||||
@@ -16,7 +12,6 @@ SHORT_TERM_TODO
|
||||
release.h
|
||||
src/transfer.sh
|
||||
src/configs
|
||||
src/redis-server.dSYM
|
||||
redis.ds
|
||||
src/redis.conf
|
||||
deps/lua/src/lua
|
||||
@@ -24,3 +19,4 @@ deps/lua/src/luac
|
||||
deps/lua/src/liblua.a
|
||||
.make-*
|
||||
.prerequisites
|
||||
*.dSYM
|
||||
|
||||
+82
-2
@@ -10,10 +10,90 @@ Upgrade urgency levels:
|
||||
|
||||
LOW: No need to upgrade unless there are new features you want to use.
|
||||
MODERATE: Program an upgrade of the server, but it's not urgent.
|
||||
HIGH: There is a critical bug that may affect some part of users. Upgrade!
|
||||
HIGH: There is a critical bug that may affect a subset of users. Upgrade!
|
||||
CRITICAL: There is a critical bug affecting MOST USERS. Upgrade ASAP.
|
||||
--------------------------------------------------------------------------------
|
||||
|
||||
---[ Redis 2.6.0 ]
|
||||
|
||||
* [BUGFIX] Allow AUTH when server is in -BUSY state because of a slow script.
|
||||
* [BUGFIX] MULTI/EXEC flow now makes sense when observed in MONITOR
|
||||
* [BUGFIX] SCRIPT KILL now uses different error prefixes for different errors.
|
||||
* [BUGFIX] Default memory limit for 32bit archs lowered from 3.5 to 3 GB.
|
||||
* [BUGFIX] redis-check-dump is now compatible with RDB files generated by 2.6.
|
||||
* [IMPROVED] New field in INFO: slave_read_only.
|
||||
|
||||
---[ Redis 2.5.14 (2.6 Release Candidate 8) ]
|
||||
|
||||
* [BUGFIX] Fixed compilation on FreeBSD.
|
||||
* [IMPROVED] SRANDMEMBER <count> that returns multiple random elements.
|
||||
* [IMPROVED] Sentinel backported to 2.6. It will be taken in sync with 2.8.
|
||||
* [IMPROVED] Helper function for scripting to return errors and status replies.
|
||||
* [IMPROVED] SORT by nosort [ASC|DESC] uses sorted set elements ordering.
|
||||
* [BUGFIX] Better resistence to system clock skew.
|
||||
* [IMPROVED] Warn the user when the configured maxmemory seems odd.
|
||||
* [BUGFIX] Hashing function is now murmurhash2 for security purposes.
|
||||
* [IMPROVED] Install script no longer uses a template but redis.conf itself.
|
||||
|
||||
---[ Redis 2.5.13 (2.6 Release Candidate 7) ]
|
||||
|
||||
UPGRADE URGENCY: HIGH
|
||||
|
||||
* [BUGFIX] Theoretical bug in ziplist fixed.
|
||||
* [BUGFIX] Better out of memory handling (Log produced in log file).
|
||||
* [BUGFIX] Incrementally flush RDB file on slave side while performing the
|
||||
first synchronization with the master. This makes Redis less
|
||||
blocking in environments where disk I/O is slow.
|
||||
* [BUGFIX] Don't crash with Lua's redis.call() without arguments.
|
||||
* [BUGFIX] Don't crash after a big number of Lua calls on 32 bit systems
|
||||
because of a failed assertion.
|
||||
* [BUGFIX] Fix SORT behaviour when called from scripting.
|
||||
* [BUGFIX] Adjust slave PING period accordingly to REDIS_HZ define.
|
||||
* [BUGFIX] BITCOUNT: fix crash on overflowing arguments.
|
||||
* [BUGFIX] Return an error when SELECT argument is not an integer.
|
||||
* [BUGFIX] Blocking operations on lists were completely reimplemented for
|
||||
correctness. Now blocking list ops and pushes originated from
|
||||
Lua scripts will play well together and will be replicated
|
||||
and transmitted to the AOF correctly.
|
||||
* [IMPROVED] Send async PING before starting replication to avoid blocking if
|
||||
master allows us to connect but it is actually not able to reply.
|
||||
* [IMPROVED] Support slave-priority for Redis Sentinel.
|
||||
* [IMPROVED] Hiredis library updated.
|
||||
|
||||
---[ Redis 2.5.12 (2.6 Release Candidate 6) ]
|
||||
|
||||
UPGRADE URGENCY: MODERATE.
|
||||
|
||||
* [BUGFIX] Fixed a timing attack on AUTH (Issue #560).
|
||||
* [BUGFIX] Don't assume that "char" is signed.
|
||||
* [BUGFIX] Check that we have connection before enabling pipe mode.
|
||||
* [BUGFIX] Use the optimized version of the function to convert a double to
|
||||
its string representation. Compilation was disabled because of
|
||||
a typo in the #if statement.
|
||||
* [IMPROVED} REPLCONF internal command introduced, now INFO shows slaves with
|
||||
correct port numbers. This makes 2.5.12 Redis Sentinel compatible.
|
||||
* [IMPROVED] Truncate short write from the AOF for a cleaner restart. On short
|
||||
writes (for instance out of space) Redis will now try to remove
|
||||
the half-written data so that the next restart will work without
|
||||
the need for the "redis-check-aof" utility.
|
||||
* [IMPROVED] New in INFO: aof_last_bgrewrite_status
|
||||
* [IMPROVED] Allow Pub/Sub in contexts where other commands are blocked.
|
||||
* [BUGFIX] mark fd as writable when EPOLLERR or EPOLLHUP is returned by
|
||||
epoll_wait.
|
||||
|
||||
---[ Redis 2.5.11 (2.6 Release Candidate 5) ]
|
||||
|
||||
UPGRADE URGENCY: HIGH.
|
||||
|
||||
* [BUGFIX] Fixed Hash corruption when loading an RDB file generated by
|
||||
previous versions of Redis that encoded hashes using
|
||||
a different ziplist encoding format for small integers.
|
||||
All the fileds that are integers in the range 0-255 may not
|
||||
be recognized, or duplicated un updates, causing a crash
|
||||
when the ziplist is converted to a real hash. (Issue #547).
|
||||
* [BUGFIX] Fixed the count of memory used by output buffers in the
|
||||
setDeferredMultiBulkLength() function.
|
||||
|
||||
---[ Redis 2.5.10 (2.6 Release Candidate 4) ]
|
||||
|
||||
UPGRADE URGENCY: HIGH.
|
||||
@@ -123,7 +203,7 @@ Redis 2.4 is mostly a strict subset of 2.6. However there are a few things
|
||||
that you should be aware of:
|
||||
|
||||
* You can't use .rdb and AOF files generated with 2.6 into a 2.4 instance.
|
||||
* 2.4 slaves can be attached to 2.6 masters, but not the contrary, and only
|
||||
* 2.6 slaves can be attached to 2.4 masters, but not the contrary, and only
|
||||
for the time needed to perform the version upgrade.
|
||||
|
||||
There are also a few API differences, that are unlikely to cause problems,
|
||||
|
||||
+17
-3
@@ -1,8 +1,22 @@
|
||||
1. Enter irc.freenode.org #redis and start talking with 'antirez' and/or 'pietern' to check if there is interest for such a feature and to understand the probability of it being merged. We'll try hard to keep Redis simple... so you'll likely encounter high resistance.
|
||||
# IMPORTANT: HOW TO USE REDIS GITHUB ISSUES
|
||||
|
||||
2. Drop a message to the Redis Google Group with a proposal of semantics/API.
|
||||
* Github issues SHOULD ONLY BE USED to report bugs, and for DETAILED feature
|
||||
requests. Everything else belongs to the Redis Google Group.
|
||||
|
||||
3. If steps 1 and 2 are ok, use the following procedure to submit a patch:
|
||||
PLEASE DO NOT POST GENERAL QUESTIONS that are not about bugs or suspected
|
||||
bugs in the Github issues system. We'll be very happy to help you and provide
|
||||
all the support in the Redis Google Group.
|
||||
|
||||
Redis Google Group address:
|
||||
|
||||
https://groups.google.com/forum/?fromgroups#!forum/redis-db
|
||||
|
||||
# How to provide a patch for a new feature
|
||||
|
||||
1. Drop a message to the Redis Google Group with a proposal of semantics/API.
|
||||
|
||||
2. If in steps 1 you get an acknowledge from the project leaders, use the
|
||||
following procedure to submit a patch:
|
||||
|
||||
a. Fork Redis on github ( http://help.github.com/fork-a-repo/ )
|
||||
b. Create a topic branch (git checkout -b my_branch)
|
||||
|
||||
Vendored
+28
-1
@@ -73,7 +73,7 @@ convert it to the protocol used to communicate with Redis.
|
||||
One or more spaces separates arguments, so you can use the specifiers
|
||||
anywhere in an argument:
|
||||
|
||||
reply = redisCommand("SET key:%s %s", myid, value);
|
||||
reply = redisCommand(context, "SET key:%s %s", myid, value);
|
||||
|
||||
### Using replies
|
||||
|
||||
@@ -320,6 +320,10 @@ The reply parsing API consists of the following functions:
|
||||
int redisReaderFeed(redisReader *reader, const char *buf, size_t len);
|
||||
int redisReaderGetReply(redisReader *reader, void **reply);
|
||||
|
||||
The same set of functions are used internally by hiredis when creating a
|
||||
normal Redis context, the above API just exposes it to the user for a direct
|
||||
usage.
|
||||
|
||||
### Usage
|
||||
|
||||
The function `redisReaderCreate` creates a `redisReader` structure that holds a
|
||||
@@ -346,6 +350,29 @@ immediately after creating the `redisReader`.
|
||||
For example, [hiredis-rb](https://github.com/pietern/hiredis-rb/blob/master/ext/hiredis_ext/reader.c)
|
||||
uses customized reply object functions to create Ruby objects.
|
||||
|
||||
### Reader max buffer
|
||||
|
||||
Both when using the Reader API directly or when using it indirectly via a
|
||||
normal Redis context, the redisReader structure uses a buffer in order to
|
||||
accumulate data from the server.
|
||||
Usually this buffer is destroyed when it is empty and is larger than 16
|
||||
kb in order to avoid wasting memory in unused buffers
|
||||
|
||||
However when working with very big payloads destroying the buffer may slow
|
||||
down performances considerably, so it is possible to modify the max size of
|
||||
an idle buffer changing the value of the `maxbuf` field of the reader structure
|
||||
to the desired value. The special value of 0 means that there is no maximum
|
||||
value for an idle buffer, so the buffer will never get freed.
|
||||
|
||||
For instance if you have a normal Redis context you can set the maximum idle
|
||||
buffer to zero (unlimited) just with:
|
||||
|
||||
context->reader->maxbuf = 0;
|
||||
|
||||
This should be done only in order to maximize performances when working with
|
||||
large payloads. The context should be set back to `REDIS_READER_MAX_BUF` again
|
||||
as soon as possible in order to prevent allocation of useless memory.
|
||||
|
||||
## AUTHORS
|
||||
|
||||
Hiredis was written by Salvatore Sanfilippo (antirez at gmail) and
|
||||
|
||||
Vendored
+29
-11
@@ -372,6 +372,11 @@ void redisProcessCallbacks(redisAsyncContext *ac) {
|
||||
__redisAsyncDisconnect(ac);
|
||||
return;
|
||||
}
|
||||
|
||||
/* If monitor mode, repush callback */
|
||||
if(c->flags & REDIS_MONITORING) {
|
||||
__redisPushCallback(&ac->replies,&cb);
|
||||
}
|
||||
|
||||
/* When the connection is not being disconnected, simply stop
|
||||
* trying to get replies and wait for the next loop tick. */
|
||||
@@ -381,22 +386,31 @@ void redisProcessCallbacks(redisAsyncContext *ac) {
|
||||
/* Even if the context is subscribed, pending regular callbacks will
|
||||
* get a reply before pub/sub messages arrive. */
|
||||
if (__redisShiftCallback(&ac->replies,&cb) != REDIS_OK) {
|
||||
/* A spontaneous reply in a not-subscribed context can only be the
|
||||
* error reply that is sent when a new connection exceeds the
|
||||
* maximum number of allowed connections on the server side. This
|
||||
* is seen as an error instead of a regular reply because the
|
||||
* server closes the connection after sending it. To prevent the
|
||||
* error from being overwritten by an EOF error the connection is
|
||||
* closed here. See issue #43. */
|
||||
if ( !(c->flags & REDIS_SUBSCRIBED) && ((redisReply*)reply)->type == REDIS_REPLY_ERROR ) {
|
||||
/*
|
||||
* A spontaneous reply in a not-subscribed context can be the error
|
||||
* reply that is sent when a new connection exceeds the maximum
|
||||
* number of allowed connections on the server side.
|
||||
*
|
||||
* This is seen as an error instead of a regular reply because the
|
||||
* server closes the connection after sending it.
|
||||
*
|
||||
* To prevent the error from being overwritten by an EOF error the
|
||||
* connection is closed here. See issue #43.
|
||||
*
|
||||
* Another possibility is that the server is loading its dataset.
|
||||
* In this case we also want to close the connection, and have the
|
||||
* user wait until the server is ready to take our request.
|
||||
*/
|
||||
if (((redisReply*)reply)->type == REDIS_REPLY_ERROR) {
|
||||
c->err = REDIS_ERR_OTHER;
|
||||
snprintf(c->errstr,sizeof(c->errstr),"%s",((redisReply*)reply)->str);
|
||||
__redisAsyncDisconnect(ac);
|
||||
return;
|
||||
}
|
||||
/* No more regular callbacks and no errors, the context *must* be subscribed. */
|
||||
assert(c->flags & REDIS_SUBSCRIBED);
|
||||
__redisGetSubscribeCallback(ac,reply,&cb);
|
||||
/* No more regular callbacks and no errors, the context *must* be subscribed or monitoring. */
|
||||
assert((c->flags & REDIS_SUBSCRIBED || c->flags & REDIS_MONITORING));
|
||||
if(c->flags & REDIS_SUBSCRIBED)
|
||||
__redisGetSubscribeCallback(ac,reply,&cb);
|
||||
}
|
||||
|
||||
if (cb.fn != NULL) {
|
||||
@@ -557,6 +571,10 @@ static int __redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void
|
||||
/* (P)UNSUBSCRIBE does not have its own response: every channel or
|
||||
* pattern that is unsubscribed will receive a message. This means we
|
||||
* should not append a callback function for this command. */
|
||||
} else if(strncasecmp(cstr,"monitor\r\n",9) == 0) {
|
||||
/* Set monitor flag and push callback */
|
||||
c->flags |= REDIS_MONITORING;
|
||||
__redisPushCallback(&ac->replies,&cb);
|
||||
} else {
|
||||
if (c->flags & REDIS_SUBSCRIBED)
|
||||
/* This will likely result in an error reply, but it needs to be
|
||||
|
||||
Vendored
+3
-4
@@ -446,7 +446,7 @@ static int processMultiBulkItem(redisReader *r) {
|
||||
long elements;
|
||||
int root = 0;
|
||||
|
||||
/* Set error for nested multi bulks with depth > 2 */
|
||||
/* Set error for nested multi bulks with depth > 7 */
|
||||
if (r->ridx == 8) {
|
||||
__redisReaderSetError(r,REDIS_ERR_PROTOCOL,
|
||||
"No support for nested multi bulk replies with depth > 7");
|
||||
@@ -564,6 +564,7 @@ redisReader *redisReaderCreate(void) {
|
||||
r->errstr[0] = '\0';
|
||||
r->fn = &defaultFunctions;
|
||||
r->buf = sdsempty();
|
||||
r->maxbuf = REDIS_READER_MAX_BUF;
|
||||
if (r->buf == NULL) {
|
||||
free(r);
|
||||
return NULL;
|
||||
@@ -590,9 +591,8 @@ int redisReaderFeed(redisReader *r, const char *buf, size_t len) {
|
||||
|
||||
/* Copy the provided buffer. */
|
||||
if (buf != NULL && len >= 1) {
|
||||
#if 0
|
||||
/* Destroy internal buffer when it is empty and is quite large. */
|
||||
if (r->len == 0 && sdsavail(r->buf) > 16*1024) {
|
||||
if (r->len == 0 && r->maxbuf != 0 && sdsavail(r->buf) > r->maxbuf) {
|
||||
sdsfree(r->buf);
|
||||
r->buf = sdsempty();
|
||||
r->pos = 0;
|
||||
@@ -600,7 +600,6 @@ int redisReaderFeed(redisReader *r, const char *buf, size_t len) {
|
||||
/* r->buf should not be NULL since we just free'd a larger one. */
|
||||
assert(r->buf != NULL);
|
||||
}
|
||||
#endif
|
||||
|
||||
newbuf = sdscatlen(r->buf,buf,len);
|
||||
if (newbuf == NULL) {
|
||||
|
||||
Vendored
+6
@@ -76,6 +76,9 @@
|
||||
/* Flag that is set when the async context has one or more subscriptions. */
|
||||
#define REDIS_SUBSCRIBED 0x20
|
||||
|
||||
/* Flag that is set when monitor mode is active */
|
||||
#define REDIS_MONITORING 0x40
|
||||
|
||||
#define REDIS_REPLY_STRING 1
|
||||
#define REDIS_REPLY_ARRAY 2
|
||||
#define REDIS_REPLY_INTEGER 3
|
||||
@@ -83,6 +86,8 @@
|
||||
#define REDIS_REPLY_STATUS 5
|
||||
#define REDIS_REPLY_ERROR 6
|
||||
|
||||
#define REDIS_READER_MAX_BUF (1024*16) /* Default max unused reader buffer. */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
@@ -122,6 +127,7 @@ typedef struct redisReader {
|
||||
char *buf; /* Read buffer */
|
||||
size_t pos; /* Buffer cursor */
|
||||
size_t len; /* Buffer length */
|
||||
size_t maxbuf; /* Max length of unused buffer */
|
||||
|
||||
redisReadTask rstack[9];
|
||||
int ridx; /* Index of current read task */
|
||||
|
||||
Vendored
+26
-14
@@ -45,6 +45,8 @@
|
||||
#include <errno.h>
|
||||
#include <stdarg.h>
|
||||
#include <stdio.h>
|
||||
#include <poll.h>
|
||||
#include <limits.h>
|
||||
|
||||
#include "net.h"
|
||||
#include "sds.h"
|
||||
@@ -121,28 +123,38 @@ static int redisSetTcpNoDelay(redisContext *c, int fd) {
|
||||
return REDIS_OK;
|
||||
}
|
||||
|
||||
#define __MAX_MSEC (((LONG_MAX) - 999) / 1000)
|
||||
|
||||
static int redisContextWaitReady(redisContext *c, int fd, const struct timeval *timeout) {
|
||||
struct timeval to;
|
||||
struct timeval *toptr = NULL;
|
||||
fd_set wfd;
|
||||
struct pollfd wfd[1];
|
||||
long msec;
|
||||
|
||||
msec = -1;
|
||||
wfd[0].fd = fd;
|
||||
wfd[0].events = POLLOUT;
|
||||
|
||||
/* Only use timeout when not NULL. */
|
||||
if (timeout != NULL) {
|
||||
to = *timeout;
|
||||
toptr = &to;
|
||||
}
|
||||
|
||||
if (errno == EINPROGRESS) {
|
||||
FD_ZERO(&wfd);
|
||||
FD_SET(fd, &wfd);
|
||||
|
||||
if (select(FD_SETSIZE, NULL, &wfd, NULL, toptr) == -1) {
|
||||
__redisSetErrorFromErrno(c,REDIS_ERR_IO,"select(2)");
|
||||
if (timeout->tv_usec > 1000000 || timeout->tv_sec > __MAX_MSEC) {
|
||||
close(fd);
|
||||
return REDIS_ERR;
|
||||
}
|
||||
|
||||
if (!FD_ISSET(fd, &wfd)) {
|
||||
msec = (timeout->tv_sec * 1000) + ((timeout->tv_usec + 999) / 1000);
|
||||
|
||||
if (msec < 0 || msec > INT_MAX) {
|
||||
msec = INT_MAX;
|
||||
}
|
||||
}
|
||||
|
||||
if (errno == EINPROGRESS) {
|
||||
int res;
|
||||
|
||||
if ((res = poll(wfd, 1, msec)) == -1) {
|
||||
__redisSetErrorFromErrno(c, REDIS_ERR_IO, "poll(2)");
|
||||
close(fd);
|
||||
return REDIS_ERR;
|
||||
} else if (res == 0) {
|
||||
errno = ETIMEDOUT;
|
||||
__redisSetErrorFromErrno(c,REDIS_ERR_IO,NULL);
|
||||
close(fd);
|
||||
|
||||
+15
@@ -196,6 +196,21 @@ slave-read-only yes
|
||||
#
|
||||
# repl-timeout 60
|
||||
|
||||
# The slave priority is an integer number published by Redis in the INFO output.
|
||||
# It is used by Redis Sentinel in order to select a slave to promote into a
|
||||
# master if the master is no longer working correctly.
|
||||
#
|
||||
# A slave with a low priority number is considered better for promotion, so
|
||||
# for instance if there are three slaves with priority 10, 100, 25 Sentinel will
|
||||
# pick the one wtih priority 10, that is the lowest.
|
||||
#
|
||||
# However a special priority of 0 marks the slave as not able to perform the
|
||||
# role of master, so a slave with priority of 0 will never be selected by
|
||||
# Redis Sentinel for promotion.
|
||||
#
|
||||
# By default the priority is 100.
|
||||
slave-priority 100
|
||||
|
||||
################################## SECURITY ###################################
|
||||
|
||||
# Require clients to issue AUTH <PASSWORD> before processing any other
|
||||
|
||||
+150
@@ -0,0 +1,150 @@
|
||||
# Example sentinel.conf
|
||||
|
||||
# port <sentinel-port>
|
||||
# The port that this sentinel instance will run on
|
||||
port 26379
|
||||
|
||||
# sentinel monitor <master-name> <ip> <redis-port> <quorum>
|
||||
#
|
||||
# Tells Sentinel to monitor this slave, and to consider it in O_DOWN
|
||||
# (Objectively Down) state only if at least <quorum> sentinels agree.
|
||||
#
|
||||
# Note: master name should not include special characters or spaces.
|
||||
# The valid charset is A-z 0-9 and the three characters ".-_".
|
||||
sentinel monitor mymaster 127.0.0.1 6379 2
|
||||
|
||||
# sentinel auth-pass <master-name> <password>
|
||||
#
|
||||
# Set the password to use to authenticate with the master and slaves.
|
||||
# Useful if there is a password set in the Redis instances to monitor.
|
||||
#
|
||||
# Note that the master password is also used for slaves, so it is not
|
||||
# possible to set a different password in masters and slaves instances
|
||||
# if you want to be able to monitor these instances with Sentinel.
|
||||
#
|
||||
# However you can have Redis instances without the authentication enabled
|
||||
# mixed with Redis instances requiring the authentication (as long as the
|
||||
# password set is the same for all the instances requiring the password) as
|
||||
# the AUTH command will have no effect in Redis instances with authentication
|
||||
# switched off.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# sentinel auth-pass mymaster MySUPER--secret-0123passw0rd
|
||||
|
||||
# sentinel down-after-milliseconds <master-name> <milliseconds>
|
||||
#
|
||||
# Number of milliseconds the master (or any attached slave or sentinel) should
|
||||
# be unreachable (as in, not acceptable reply to PING, continuously, for the
|
||||
# specified period) in order to consider it in S_DOWN state (Subjectively
|
||||
# Down).
|
||||
#
|
||||
# Default is 30 seconds.
|
||||
sentinel down-after-milliseconds mymaster 30000
|
||||
|
||||
# sentinel can-failover <master-name> <yes|no>
|
||||
#
|
||||
# Specify if this Sentinel can start the failover for this master.
|
||||
sentinel can-failover mymaster yes
|
||||
|
||||
# sentinel parallel-syncs <master-name> <numslaves>
|
||||
#
|
||||
# How many slaves we can reconfigure to point to the new slave simultaneously
|
||||
# during the failover. Use a low number if you use the slaves to serve query
|
||||
# to avoid that all the slaves will be unreachable at about the same
|
||||
# time while performing the synchronization with the master.
|
||||
sentinel parallel-syncs mymaster 1
|
||||
|
||||
# sentinel failover-timeout <master-name> <milliseconds>
|
||||
#
|
||||
# Specifies the failover timeout in milliseconds. When this time has elapsed
|
||||
# without any progress in the failover process, it is considered concluded by
|
||||
# the sentinel even if not all the attached slaves were correctly configured
|
||||
# to replicate with the new master (however a "best effort" SLAVEOF command
|
||||
# is sent to all the slaves before).
|
||||
#
|
||||
# Also when 25% of this time has elapsed without any advancement, and there
|
||||
# is a leader switch (the sentinel did not started the failover but is now
|
||||
# elected as leader), the sentinel will continue the failover doing a
|
||||
# "takeover".
|
||||
#
|
||||
# Default is 15 minutes.
|
||||
sentinel failover-timeout mymaster 900000
|
||||
|
||||
# SCRIPTS EXECTION
|
||||
#
|
||||
# sentinel notification-script and sentinel reconfig-script are used in order
|
||||
# to configure scripts that are called to notify the system administrator
|
||||
# or to reconfigure clients after a failover. The scripts are executed
|
||||
# with the following rules for error handling:
|
||||
#
|
||||
# If script exists with "1" the execution is retried later (up to a maximum
|
||||
# number of times currently set to 10).
|
||||
#
|
||||
# If script exists with "2" (or an higher value) the script execution is
|
||||
# not retried.
|
||||
#
|
||||
# If script terminates because it receives a signal the behavior is the same
|
||||
# as exit code 1.
|
||||
#
|
||||
# A script has a maximum running time of 60 seconds. After this limit is
|
||||
# reached the script is terminated with a SIGKILL and the execution retried.
|
||||
|
||||
# NOTIFICATION SCRIPT
|
||||
#
|
||||
# sentinel notification-script <master-name> <script-path>
|
||||
#
|
||||
# Call the specified notification script for any sentienl event that is
|
||||
# generated in the WARNING level (for instance -sdown, -odown, and so forth).
|
||||
# This script should notify the system administrator via email, SMS, or any
|
||||
# other messaging system, that there is something wrong with the monitored
|
||||
# Redis systems.
|
||||
#
|
||||
# The script is called with just two arguments: the first is the event type
|
||||
# and the second the event description.
|
||||
#
|
||||
# The script must exist and be executable in order for sentinel to start if
|
||||
# this option is provided.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# sentinel notification-script mymaster /var/redis/notify.sh
|
||||
|
||||
# CLIENTS RECONFIGURATION SCRIPT
|
||||
#
|
||||
# sentinel client-reconfig-script <master-name> <script-path>
|
||||
#
|
||||
# When the failover starts, ends, or is aborted, a script can be called in
|
||||
# order to perform application-specific tasks to notify the clients that the
|
||||
# configuration has changed and the master is at a different address.
|
||||
#
|
||||
# The script is called in the following cases:
|
||||
#
|
||||
# Failover started (a slave is already promoted)
|
||||
# Failover finished (all the additional slaves already reconfigured)
|
||||
# Failover aborted (in that case the script was previously called when the
|
||||
# failover started, and now gets called again with swapped
|
||||
# addresses).
|
||||
#
|
||||
# The following arguments are passed to the script:
|
||||
#
|
||||
# <master-name> <role> <state> <from-ip> <from-port> <to-ip> <to-port>
|
||||
#
|
||||
# <state> is "start", "end" or "abort"
|
||||
# <role> is either "leader" or "observer"
|
||||
#
|
||||
# The arguments from-ip, from-port, to-ip, to-port are used to communicate
|
||||
# the old address of the master and the new address of the elected slave
|
||||
# (now a master) in the case state is "start" or "end".
|
||||
#
|
||||
# For abort instead the "from" is the address of the promoted slave and
|
||||
# "to" is the address of the original master address, since the failover
|
||||
# was aborted.
|
||||
#
|
||||
# This script should be resistant to multiple invocations.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# sentinel client-reconfig-script mymaster /var/redis/reconfig.sh
|
||||
|
||||
|
||||
+16
-9
@@ -78,6 +78,7 @@ endif
|
||||
|
||||
REDIS_CC=$(QUIET_CC)$(CC) $(FINAL_CFLAGS)
|
||||
REDIS_LD=$(QUIET_LINK)$(CC) $(FINAL_LDFLAGS)
|
||||
REDIS_INSTALL=$(QUIET_INSTALL)$(INSTALL)
|
||||
|
||||
PREFIX?=/usr/local
|
||||
INSTALL_BIN= $(PREFIX)/bin
|
||||
@@ -93,10 +94,12 @@ ENDCOLOR="\033[0m"
|
||||
ifndef V
|
||||
QUIET_CC = @printf ' %b %b\n' $(CCCOLOR)CC$(ENDCOLOR) $(SRCCOLOR)$@$(ENDCOLOR) 1>&2;
|
||||
QUIET_LINK = @printf ' %b %b\n' $(LINKCOLOR)LINK$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR) 1>&2;
|
||||
QUIET_INSTALL = @printf ' %b %b\n' $(LINKCOLOR)INSTALL$(ENDCOLOR) $(BINCOLOR)$@$(ENDCOLOR) 1>&2;
|
||||
endif
|
||||
|
||||
REDIS_SERVER_NAME= redis-server
|
||||
REDIS_SERVER_OBJ= adlist.o ae.o anet.o dict.o redis.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o migrate.o endianconv.o slowlog.o scripting.o bio.o rio.o rand.o memtest.o crc64.o bitops.o
|
||||
REDIS_SENTINEL_NAME= redis-sentinel
|
||||
REDIS_SERVER_OBJ= adlist.o ae.o anet.o dict.o redis.o sds.o zmalloc.o lzf_c.o lzf_d.o pqsort.o zipmap.o sha1.o ziplist.o release.o networking.o util.o object.o db.o replication.o rdb.o t_string.o t_list.o t_set.o t_zset.o t_hash.o config.o aof.o pubsub.o multi.o debug.o sort.o intset.o syncio.o migrate.o endianconv.o slowlog.o scripting.o bio.o rio.o rand.o memtest.o crc64.o bitops.o sentinel.o
|
||||
REDIS_CLI_NAME= redis-cli
|
||||
REDIS_CLI_OBJ= anet.o sds.o adlist.o redis-cli.o zmalloc.o release.o anet.o ae.o
|
||||
REDIS_BENCHMARK_NAME= redis-benchmark
|
||||
@@ -106,7 +109,7 @@ REDIS_CHECK_DUMP_OBJ= redis-check-dump.o lzf_c.o lzf_d.o crc64.o
|
||||
REDIS_CHECK_AOF_NAME= redis-check-aof
|
||||
REDIS_CHECK_AOF_OBJ= redis-check-aof.o
|
||||
|
||||
all: $(REDIS_SERVER_NAME) $(REDIS_CLI_NAME) $(REDIS_BENCHMARK_NAME) $(REDIS_CHECK_DUMP_NAME) $(REDIS_CHECK_AOF_NAME)
|
||||
all: $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME) $(REDIS_CLI_NAME) $(REDIS_BENCHMARK_NAME) $(REDIS_CHECK_DUMP_NAME) $(REDIS_CHECK_AOF_NAME)
|
||||
@echo ""
|
||||
@echo "Hint: To run 'make test' is a good idea ;)"
|
||||
@echo ""
|
||||
@@ -151,7 +154,11 @@ endif
|
||||
|
||||
# redis-server
|
||||
$(REDIS_SERVER_NAME): $(REDIS_SERVER_OBJ)
|
||||
$(REDIS_LD) -o $@ $^ ../deps/lua/src/liblua.a $(FINAL_LIBS)
|
||||
$(REDIS_LD) -o $@ $^ ../deps/hiredis/libhiredis.a ../deps/lua/src/liblua.a $(FINAL_LIBS)
|
||||
|
||||
# redis-sentinel
|
||||
$(REDIS_SENTINEL_NAME): $(REDIS_SERVER_NAME)
|
||||
$(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME)
|
||||
|
||||
# redis-cli
|
||||
$(REDIS_CLI_NAME): $(REDIS_CLI_OBJ)
|
||||
@@ -176,7 +183,7 @@ $(REDIS_CHECK_AOF_NAME): $(REDIS_CHECK_AOF_OBJ)
|
||||
$(REDIS_CC) -c $<
|
||||
|
||||
clean:
|
||||
rm -rf $(REDIS_SERVER_NAME) $(REDIS_CLI_NAME) $(REDIS_BENCHMARK_NAME) $(REDIS_CHECK_DUMP_NAME) $(REDIS_CHECK_AOF_NAME) *.o *.gcda *.gcno *.gcov redis.info lcov-html
|
||||
rm -rf $(REDIS_SERVER_NAME) $(REDIS_SENTINEL_NAME) $(REDIS_CLI_NAME) $(REDIS_BENCHMARK_NAME) $(REDIS_CHECK_DUMP_NAME) $(REDIS_CHECK_AOF_NAME) *.o *.gcda *.gcno *.gcov redis.info lcov-html
|
||||
|
||||
.PHONY: clean
|
||||
|
||||
@@ -217,8 +224,8 @@ src/help.h:
|
||||
|
||||
install: all
|
||||
mkdir -p $(INSTALL_BIN)
|
||||
$(INSTALL) $(REDIS_SERVER_NAME) $(INSTALL_BIN)
|
||||
$(INSTALL) $(REDIS_BENCHMARK_NAME) $(INSTALL_BIN)
|
||||
$(INSTALL) $(REDIS_CLI_NAME) $(INSTALL_BIN)
|
||||
$(INSTALL) $(REDIS_CHECK_DUMP_NAME) $(INSTALL_BIN)
|
||||
$(INSTALL) $(REDIS_CHECK_AOF_NAME) $(INSTALL_BIN)
|
||||
$(REDIS_INSTALL) $(REDIS_SERVER_NAME) $(INSTALL_BIN)
|
||||
$(REDIS_INSTALL) $(REDIS_BENCHMARK_NAME) $(INSTALL_BIN)
|
||||
$(REDIS_INSTALL) $(REDIS_CLI_NAME) $(INSTALL_BIN)
|
||||
$(REDIS_INSTALL) $(REDIS_CHECK_DUMP_NAME) $(INSTALL_BIN)
|
||||
$(REDIS_INSTALL) $(REDIS_CHECK_AOF_NAME) $(INSTALL_BIN)
|
||||
|
||||
@@ -37,6 +37,7 @@
|
||||
#include <stdlib.h>
|
||||
#include <poll.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#include "ae.h"
|
||||
#include "zmalloc.h"
|
||||
@@ -67,6 +68,7 @@ aeEventLoop *aeCreateEventLoop(int setsize) {
|
||||
eventLoop->fired = zmalloc(sizeof(aeFiredEvent)*setsize);
|
||||
if (eventLoop->events == NULL || eventLoop->fired == NULL) goto err;
|
||||
eventLoop->setsize = setsize;
|
||||
eventLoop->lastTime = time(NULL);
|
||||
eventLoop->timeEventHead = NULL;
|
||||
eventLoop->timeEventNextId = 0;
|
||||
eventLoop->stop = 0;
|
||||
@@ -236,6 +238,24 @@ static int processTimeEvents(aeEventLoop *eventLoop) {
|
||||
int processed = 0;
|
||||
aeTimeEvent *te;
|
||||
long long maxId;
|
||||
time_t now = time(NULL);
|
||||
|
||||
/* If the system clock is moved to the future, and then set back to the
|
||||
* right value, time events may be delayed in a random way. Often this
|
||||
* means that scheduled operations will not be performed soon enough.
|
||||
*
|
||||
* Here we try to detect system clock skews, and force all the time
|
||||
* events to be processed ASAP when this happens: the idea is that
|
||||
* processing events earlier is less dangerous than delaying them
|
||||
* indefinitely, and practice suggests it is. */
|
||||
if (now < eventLoop->lastTime) {
|
||||
te = eventLoop->timeEventHead;
|
||||
while(te) {
|
||||
te->when_sec = 0;
|
||||
te = te->next;
|
||||
}
|
||||
}
|
||||
eventLoop->lastTime = now;
|
||||
|
||||
te = eventLoop->timeEventHead;
|
||||
maxId = eventLoop->timeEventNextId-1;
|
||||
|
||||
@@ -88,6 +88,7 @@ typedef struct aeEventLoop {
|
||||
int maxfd; /* highest file descriptor currently registered */
|
||||
int setsize; /* max number of file descriptors tracked */
|
||||
long long timeEventNextId;
|
||||
time_t lastTime; /* Used to detect system clock skew */
|
||||
aeFileEvent *events; /* Registered events */
|
||||
aeFiredEvent *fired; /* Fired events */
|
||||
aeTimeEvent *timeEventHead;
|
||||
|
||||
@@ -89,6 +89,8 @@ static int aeApiPoll(aeEventLoop *eventLoop, struct timeval *tvp) {
|
||||
|
||||
if (e->events & EPOLLIN) mask |= AE_READABLE;
|
||||
if (e->events & EPOLLOUT) mask |= AE_WRITABLE;
|
||||
if (e->events & EPOLLERR) mask |= AE_WRITABLE;
|
||||
if (e->events & EPOLLHUP) mask |= AE_WRITABLE;
|
||||
eventLoop->fired[j].fd = e->data.fd;
|
||||
eventLoop->fired[j].mask = mask;
|
||||
}
|
||||
|
||||
+15
@@ -367,3 +367,18 @@ int anetPeerToString(int fd, char *ip, int *port) {
|
||||
if (port) *port = ntohs(sa.sin_port);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int anetSockName(int fd, char *ip, int *port) {
|
||||
struct sockaddr_in sa;
|
||||
socklen_t salen = sizeof(sa);
|
||||
|
||||
if (getsockname(fd,(struct sockaddr*)&sa,&salen) == -1) {
|
||||
*port = 0;
|
||||
ip[0] = '?';
|
||||
ip[1] = '\0';
|
||||
return -1;
|
||||
}
|
||||
if (ip) strcpy(ip,inet_ntoa(sa.sin_addr));
|
||||
if (port) *port = ntohs(sa.sin_port);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -250,6 +250,13 @@ void flushAppendOnlyFile(int force) {
|
||||
strerror(errno),
|
||||
(long)nwritten,
|
||||
(long)sdslen(server.aof_buf));
|
||||
|
||||
if (ftruncate(server.aof_fd, server.aof_current_size) == -1) {
|
||||
redisLog(REDIS_WARNING, "Could not remove short write "
|
||||
"from the append-only file. Redis may refuse "
|
||||
"to load the AOF the next time it starts. "
|
||||
"ftruncate: %s", strerror(errno));
|
||||
}
|
||||
}
|
||||
exit(1);
|
||||
}
|
||||
@@ -1093,6 +1100,8 @@ void backgroundRewriteDoneHandler(int exitcode, int bysignal) {
|
||||
server.aof_buf = sdsempty();
|
||||
}
|
||||
|
||||
server.aof_lastbgrewrite_status = REDIS_OK;
|
||||
|
||||
redisLog(REDIS_NOTICE, "Background AOF rewrite finished successfully");
|
||||
/* Change state from WAIT_REWRITE to ON if needed */
|
||||
if (server.aof_state == REDIS_AOF_WAIT_REWRITE)
|
||||
@@ -1104,9 +1113,13 @@ void backgroundRewriteDoneHandler(int exitcode, int bysignal) {
|
||||
redisLog(REDIS_VERBOSE,
|
||||
"Background AOF rewrite signal handler took %lldus", ustime()-now);
|
||||
} else if (!bysignal && exitcode != 0) {
|
||||
server.aof_lastbgrewrite_status = REDIS_ERR;
|
||||
|
||||
redisLog(REDIS_WARNING,
|
||||
"Background AOF rewrite terminated with error");
|
||||
} else {
|
||||
server.aof_lastbgrewrite_status = REDIS_ERR;
|
||||
|
||||
redisLog(REDIS_WARNING,
|
||||
"Background AOF rewrite terminated by signal %d", bysignal);
|
||||
}
|
||||
|
||||
+5
-6
@@ -114,14 +114,14 @@ void setbitCommand(redisClient *c) {
|
||||
o->ptr = sdsgrowzero(o->ptr,byte+1);
|
||||
|
||||
/* Get current values */
|
||||
byteval = ((char*)o->ptr)[byte];
|
||||
byteval = ((uint8_t*)o->ptr)[byte];
|
||||
bit = 7 - (bitoffset & 0x7);
|
||||
bitval = byteval & (1 << bit);
|
||||
|
||||
/* Update byte with new bit value and return original value */
|
||||
byteval &= ~(1 << bit);
|
||||
byteval |= ((on & 0x1) << bit);
|
||||
((char*)o->ptr)[byte] = byteval;
|
||||
((uint8_t*)o->ptr)[byte] = byteval;
|
||||
signalModifiedKey(c->db,c->argv[1]);
|
||||
server.dirty++;
|
||||
addReply(c, bitval ? shared.cone : shared.czero);
|
||||
@@ -148,7 +148,7 @@ void getbitCommand(redisClient *c) {
|
||||
bitval = llbuf[byte] & (1 << bit);
|
||||
} else {
|
||||
if (byte < sdslen(o->ptr))
|
||||
bitval = ((char*)o->ptr)[byte] & (1 << bit);
|
||||
bitval = ((uint8_t*)o->ptr)[byte] & (1 << bit);
|
||||
}
|
||||
|
||||
addReply(c, bitval ? shared.cone : shared.czero);
|
||||
@@ -327,10 +327,9 @@ void bitopCommand(redisClient *c) {
|
||||
/* BITCOUNT key [start end] */
|
||||
void bitcountCommand(redisClient *c) {
|
||||
robj *o;
|
||||
long start, end;
|
||||
long start, end, strlen;
|
||||
unsigned char *p;
|
||||
char llbuf[32];
|
||||
size_t strlen;
|
||||
|
||||
/* Lookup, check for type, and return 0 for non existing keys. */
|
||||
if ((o = lookupKeyReadOrReply(c,c->argv[1],shared.czero)) == NULL ||
|
||||
@@ -357,7 +356,7 @@ void bitcountCommand(redisClient *c) {
|
||||
if (end < 0) end = strlen+end;
|
||||
if (start < 0) start = 0;
|
||||
if (end < 0) end = 0;
|
||||
if ((unsigned)end >= strlen) end = strlen-1;
|
||||
if (end >= strlen) end = strlen-1;
|
||||
} else if (c->argc == 2) {
|
||||
/* The whole string. */
|
||||
start = 0;
|
||||
|
||||
+29
-1
@@ -264,6 +264,10 @@ void loadServerConfigFromString(char *config) {
|
||||
{
|
||||
server.aof_rewrite_min_size = memtoll(argv[1],NULL);
|
||||
} else if (!strcasecmp(argv[0],"requirepass") && argc == 2) {
|
||||
if (strlen(argv[1]) > REDIS_AUTHPASS_MAX_LEN) {
|
||||
err = "Password is longer than REDIS_AUTHPASS_MAX_LEN";
|
||||
goto loaderr;
|
||||
}
|
||||
server.requirepass = zstrdup(argv[1]);
|
||||
} else if (!strcasecmp(argv[0],"pidfile") && argc == 2) {
|
||||
zfree(server.pidfile);
|
||||
@@ -343,6 +347,19 @@ void loadServerConfigFromString(char *config) {
|
||||
if ((server.stop_writes_on_bgsave_err = yesnotoi(argv[1])) == -1) {
|
||||
err = "argument must be 'yes' or 'no'"; goto loaderr;
|
||||
}
|
||||
} else if (!strcasecmp(argv[0],"slave-priority") && argc == 2) {
|
||||
server.slave_priority = atoi(argv[1]);
|
||||
} else if (!strcasecmp(argv[0],"sentinel")) {
|
||||
/* argc == 1 is handled by main() as we need to enter the sentinel
|
||||
* mode ASAP. */
|
||||
if (argc != 1) {
|
||||
if (!server.sentinel_mode) {
|
||||
err = "sentinel directive while not in sentinel mode";
|
||||
goto loaderr;
|
||||
}
|
||||
err = sentinelHandleConfiguration(argv+1,argc-1);
|
||||
if (err) goto loaderr;
|
||||
}
|
||||
} else {
|
||||
err = "Bad directive or wrong number of arguments"; goto loaderr;
|
||||
}
|
||||
@@ -411,6 +428,7 @@ void configSetCommand(redisClient *c) {
|
||||
zfree(server.rdb_filename);
|
||||
server.rdb_filename = zstrdup(o->ptr);
|
||||
} else if (!strcasecmp(c->argv[2]->ptr,"requirepass")) {
|
||||
if (sdslen(o->ptr) > REDIS_AUTHPASS_MAX_LEN) goto badfmt;
|
||||
zfree(server.requirepass);
|
||||
server.requirepass = ((char*)o->ptr)[0] ? zstrdup(o->ptr) : NULL;
|
||||
} else if (!strcasecmp(c->argv[2]->ptr,"masterauth")) {
|
||||
@@ -420,7 +438,12 @@ void configSetCommand(redisClient *c) {
|
||||
if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
|
||||
ll < 0) goto badfmt;
|
||||
server.maxmemory = ll;
|
||||
if (server.maxmemory) freeMemoryIfNeeded();
|
||||
if (server.maxmemory) {
|
||||
if (server.maxmemory < zmalloc_used_memory()) {
|
||||
redisLog(REDIS_WARNING,"WARNING: the new maxmemory value set via CONFIG SET is smaller than the current memory usage. This will result in keys eviction and/or inability to accept new write commands depending on the maxmemory-policy.");
|
||||
}
|
||||
freeMemoryIfNeeded();
|
||||
}
|
||||
} else if (!strcasecmp(c->argv[2]->ptr,"maxmemory-policy")) {
|
||||
if (!strcasecmp(o->ptr,"volatile-lru")) {
|
||||
server.maxmemory_policy = REDIS_MAXMEMORY_VOLATILE_LRU;
|
||||
@@ -643,6 +666,10 @@ void configSetCommand(redisClient *c) {
|
||||
|
||||
if (yn == -1) goto badfmt;
|
||||
server.rdb_checksum = yn;
|
||||
} else if (!strcasecmp(c->argv[2]->ptr,"slave-priority")) {
|
||||
if (getLongLongFromObject(o,&ll) == REDIS_ERR ||
|
||||
ll <= 0) goto badfmt;
|
||||
server.slave_priority = ll;
|
||||
} else {
|
||||
addReplyErrorFormat(c,"Unsupported CONFIG parameter: %s",
|
||||
(char*)c->argv[2]->ptr);
|
||||
@@ -732,6 +759,7 @@ void configGetCommand(redisClient *c) {
|
||||
config_get_numerical_field("repl-timeout",server.repl_timeout);
|
||||
config_get_numerical_field("maxclients",server.maxclients);
|
||||
config_get_numerical_field("watchdog-period",server.watchdog_period);
|
||||
config_get_numerical_field("slave-priority",server.slave_priority);
|
||||
|
||||
/* Bool (yes/no) values */
|
||||
config_get_bool_field("no-appendfsync-on-rewrite",
|
||||
|
||||
@@ -52,6 +52,14 @@
|
||||
#define aof_fsync fsync
|
||||
#endif
|
||||
|
||||
/* Define rdb_fsync_range to sync_file_range() on Linux, otherwise we use
|
||||
* the plain fsync() call. */
|
||||
#ifdef __linux__
|
||||
#define rdb_fsync_range(fd,off,size) sync_file_range(fd,off,size,SYNC_FILE_RANGE_WAIT_BEFORE|SYNC_FILE_RANGE_WRITE)
|
||||
#else
|
||||
#define rdb_fsync_range(fd,off,size) fsync(fd)
|
||||
#endif
|
||||
|
||||
/* Byte ordering detection */
|
||||
#include <sys/types.h> /* This will likely define BYTE_ORDER */
|
||||
|
||||
|
||||
@@ -226,7 +226,11 @@ void existsCommand(redisClient *c) {
|
||||
}
|
||||
|
||||
void selectCommand(redisClient *c) {
|
||||
int id = atoi(c->argv[1]->ptr);
|
||||
long id;
|
||||
|
||||
if (getLongFromObjectOrReply(c, c->argv[1], &id,
|
||||
"invalid DB index") != REDIS_OK)
|
||||
return;
|
||||
|
||||
if (selectDb(c,id) == REDIS_ERR) {
|
||||
addReplyError(c,"invalid DB index");
|
||||
|
||||
+28
@@ -218,6 +218,10 @@ void computeDatasetDigest(unsigned char *final) {
|
||||
void debugCommand(redisClient *c) {
|
||||
if (!strcasecmp(c->argv[1]->ptr,"segfault")) {
|
||||
*((char*)-1) = 'x';
|
||||
} else if (!strcasecmp(c->argv[1]->ptr,"oom")) {
|
||||
void *ptr = zmalloc(ULONG_MAX); /* Should trigger an out of memory. */
|
||||
zfree(ptr);
|
||||
addReply(c,shared.ok);
|
||||
} else if (!strcasecmp(c->argv[1]->ptr,"assert")) {
|
||||
if (c->argc >= 3) c->argv[2] = tryObjectEncoding(c->argv[2]);
|
||||
redisAssertWithInfo(c,c->argv[0],1 == 2);
|
||||
@@ -686,6 +690,30 @@ void sigsegvHandler(int sig, siginfo_t *info, void *secret) {
|
||||
}
|
||||
#endif /* HAVE_BACKTRACE */
|
||||
|
||||
/* ==================== Logging functions for debugging ===================== */
|
||||
|
||||
void redisLogHexDump(int level, char *descr, void *value, size_t len) {
|
||||
char buf[65], *b;
|
||||
unsigned char *v = value;
|
||||
char charset[] = "0123456789abcdef";
|
||||
|
||||
redisLog(level,"%s (hexdump):", descr);
|
||||
b = buf;
|
||||
while(len) {
|
||||
b[0] = charset[(*v)>>4];
|
||||
b[1] = charset[(*v)&0xf];
|
||||
b[2] = '\0';
|
||||
b += 2;
|
||||
len--;
|
||||
v++;
|
||||
if (b-buf == 64 || len == 0) {
|
||||
redisLogRaw(level|REDIS_LOG_RAW,buf);
|
||||
b = buf;
|
||||
}
|
||||
}
|
||||
redisLogRaw(level|REDIS_LOG_RAW,"\n");
|
||||
}
|
||||
|
||||
/* =========================== Software Watchdog ============================ */
|
||||
#include <sys/time.h>
|
||||
|
||||
|
||||
+64
-20
@@ -85,29 +85,73 @@ unsigned int dictIdentityHashFunction(unsigned int key)
|
||||
return key;
|
||||
}
|
||||
|
||||
static int dict_hash_function_seed = 5381;
|
||||
static uint32_t dict_hash_function_seed = 5381;
|
||||
|
||||
void dictSetHashFunctionSeed(unsigned int seed) {
|
||||
void dictSetHashFunctionSeed(uint32_t seed) {
|
||||
dict_hash_function_seed = seed;
|
||||
}
|
||||
|
||||
unsigned int dictGetHashFunctionSeed(void) {
|
||||
uint32_t dictGetHashFunctionSeed(void) {
|
||||
return dict_hash_function_seed;
|
||||
}
|
||||
|
||||
/* Generic hash function (a popular one from Bernstein).
|
||||
* I tested a few and this was the best. */
|
||||
unsigned int dictGenHashFunction(const unsigned char *buf, int len) {
|
||||
unsigned int hash = dict_hash_function_seed;
|
||||
/* MurmurHash2, by Austin Appleby
|
||||
* Note - This code makes a few assumptions about how your machine behaves -
|
||||
* 1. We can read a 4-byte value from any address without crashing
|
||||
* 2. sizeof(int) == 4
|
||||
*
|
||||
* And it has a few limitations -
|
||||
*
|
||||
* 1. It will not work incrementally.
|
||||
* 2. It will not produce the same results on little-endian and big-endian
|
||||
* machines.
|
||||
*/
|
||||
unsigned int dictGenHashFunction(const void *key, int len) {
|
||||
/* 'm' and 'r' are mixing constants generated offline.
|
||||
They're not really 'magic', they just happen to work well. */
|
||||
uint32_t seed = dict_hash_function_seed;
|
||||
const uint32_t m = 0x5bd1e995;
|
||||
const int r = 24;
|
||||
|
||||
while (len--)
|
||||
hash = ((hash << 5) + hash) + (*buf++); /* hash * 33 + c */
|
||||
return hash;
|
||||
/* Initialize the hash to a 'random' value */
|
||||
uint32_t h = seed ^ len;
|
||||
|
||||
/* Mix 4 bytes at a time into the hash */
|
||||
const unsigned char *data = (const unsigned char *)key;
|
||||
|
||||
while(len >= 4) {
|
||||
uint32_t k = *(uint32_t*)data;
|
||||
|
||||
k *= m;
|
||||
k ^= k >> r;
|
||||
k *= m;
|
||||
|
||||
h *= m;
|
||||
h ^= k;
|
||||
|
||||
data += 4;
|
||||
len -= 4;
|
||||
}
|
||||
|
||||
/* Handle the last few bytes of the input array */
|
||||
switch(len) {
|
||||
case 3: h ^= data[2] << 16;
|
||||
case 2: h ^= data[1] << 8;
|
||||
case 1: h ^= data[0]; h *= m;
|
||||
};
|
||||
|
||||
/* Do a few final mixes of the hash to ensure the last few
|
||||
* bytes are well-incorporated. */
|
||||
h ^= h >> 13;
|
||||
h *= m;
|
||||
h ^= h >> 15;
|
||||
|
||||
return (unsigned int)h;
|
||||
}
|
||||
|
||||
/* And a case insensitive version */
|
||||
/* And a case insensitive hash function (based on djb hash) */
|
||||
unsigned int dictGenCaseHashFunction(const unsigned char *buf, int len) {
|
||||
unsigned int hash = dict_hash_function_seed;
|
||||
unsigned int hash = (unsigned int)dict_hash_function_seed;
|
||||
|
||||
while (len--)
|
||||
hash = ((hash << 5) + hash) + (tolower(*buf++)); /* hash * 33 + c */
|
||||
@@ -116,8 +160,8 @@ unsigned int dictGenCaseHashFunction(const unsigned char *buf, int len) {
|
||||
|
||||
/* ----------------------------- API implementation ------------------------- */
|
||||
|
||||
/* Reset an hashtable already initialized with ht_init().
|
||||
* NOTE: This function should only called by ht_destroy(). */
|
||||
/* Reset a hash table already initialized with ht_init().
|
||||
* NOTE: This function should only be called by ht_destroy(). */
|
||||
static void _dictReset(dictht *ht)
|
||||
{
|
||||
ht->table = NULL;
|
||||
@@ -162,18 +206,18 @@ int dictResize(dict *d)
|
||||
return dictExpand(d, minimal);
|
||||
}
|
||||
|
||||
/* Expand or create the hashtable */
|
||||
/* Expand or create the hash table */
|
||||
int dictExpand(dict *d, unsigned long size)
|
||||
{
|
||||
dictht n; /* the new hashtable */
|
||||
dictht n; /* the new hash table */
|
||||
unsigned long realsize = _dictNextPower(size);
|
||||
|
||||
/* the size is invalid if it is smaller than the number of
|
||||
* elements already inside the hashtable */
|
||||
* elements already inside the hash table */
|
||||
if (dictIsRehashing(d) || d->ht[0].used > size)
|
||||
return DICT_ERR;
|
||||
|
||||
/* Allocate the new hashtable and initialize all pointers to NULL */
|
||||
/* Allocate the new hash table and initialize all pointers to NULL */
|
||||
n.size = realsize;
|
||||
n.sizemask = realsize-1;
|
||||
n.table = zcalloc(realsize*sizeof(dictEntry*));
|
||||
@@ -280,7 +324,7 @@ int dictAdd(dict *d, void *key, void *val)
|
||||
* a value returns the dictEntry structure to the user, that will make
|
||||
* sure to fill the value field as he wishes.
|
||||
*
|
||||
* This function is also directly expoed to user API to be called
|
||||
* This function is also directly exposed to user API to be called
|
||||
* mainly in order to store non-pointers inside the hash value, example:
|
||||
*
|
||||
* entry = dictAddRaw(dict,mykey);
|
||||
@@ -607,7 +651,7 @@ static int _dictKeyIndex(dict *d, const void *key)
|
||||
unsigned int h, idx, table;
|
||||
dictEntry *he;
|
||||
|
||||
/* Expand the hashtable if needed */
|
||||
/* Expand the hash table if needed */
|
||||
if (_dictExpandIfNeeded(d) == DICT_ERR)
|
||||
return -1;
|
||||
/* Compute the key hash value */
|
||||
|
||||
+5
-5
@@ -1,11 +1,11 @@
|
||||
/* Hash Tables Implementation.
|
||||
*
|
||||
* This file implements in memory hash tables with insert/del/replace/find/
|
||||
* get-random-element operations. Hash tables will auto resize if needed
|
||||
* This file implements in-memory hash tables with insert/del/replace/find/
|
||||
* get-random-element operations. Hash tables will auto-resize if needed
|
||||
* tables of power of two in size are used, collisions are handled by
|
||||
* chaining. See the source code for more information... :)
|
||||
*
|
||||
* Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
|
||||
* Copyright (c) 2006-2012, Salvatore Sanfilippo <antirez at gmail dot com>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@@ -80,7 +80,7 @@ typedef struct dict {
|
||||
int iterators; /* number of iterators currently running */
|
||||
} dict;
|
||||
|
||||
/* If safe is set to 1 this is a safe iteartor, that means, you can call
|
||||
/* If safe is set to 1 this is a safe iterator, that means, you can call
|
||||
* dictAdd, dictFind, and other functions against the dictionary even while
|
||||
* iterating. Otherwise it is a non safe iterator, and only dictNext()
|
||||
* should be called while iterating. */
|
||||
@@ -155,7 +155,7 @@ dictEntry *dictNext(dictIterator *iter);
|
||||
void dictReleaseIterator(dictIterator *iter);
|
||||
dictEntry *dictGetRandomKey(dict *d);
|
||||
void dictPrintStats(dict *d);
|
||||
unsigned int dictGenHashFunction(const unsigned char *buf, int len);
|
||||
unsigned int dictGenHashFunction(const void *key, int len);
|
||||
unsigned int dictGenCaseHashFunction(const unsigned char *buf, int len);
|
||||
void dictEmpty(dict *d);
|
||||
void dictEnableResize(void);
|
||||
|
||||
@@ -3,6 +3,10 @@
|
||||
|
||||
#define _BSD_SOURCE
|
||||
|
||||
#if defined(__linux__)
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
|
||||
#if defined(__linux__) || defined(__OpenBSD__)
|
||||
#define _XOPEN_SOURCE 700
|
||||
#else
|
||||
|
||||
+10
-1
@@ -96,7 +96,7 @@ void execCommand(redisClient *c) {
|
||||
c->flags &= ~(REDIS_MULTI|REDIS_DIRTY_CAS);
|
||||
unwatchAllKeys(c);
|
||||
addReply(c,shared.nullmultibulk);
|
||||
return;
|
||||
goto handle_monitor;
|
||||
}
|
||||
|
||||
/* Replicate a MULTI request now that we are sure the block is executed.
|
||||
@@ -132,6 +132,15 @@ void execCommand(redisClient *c) {
|
||||
* always send the MULTI command (we can't know beforehand if the
|
||||
* next operations will contain at least a modification to the DB). */
|
||||
server.dirty++;
|
||||
|
||||
handle_monitor:
|
||||
/* Send EXEC to clients waiting data from MONITOR. We do it here
|
||||
* since the natural order of commands execution is actually:
|
||||
* MUTLI, EXEC, ... commands inside transaction ...
|
||||
* Instead EXEC is flagged as REDIS_CMD_SKIP_MONITOR in the command
|
||||
* table, and we do it here with correct ordering. */
|
||||
if (listLength(server.monitors) && !server.loading)
|
||||
replicationFeedMonitors(c,server.monitors,c->db->id,c->argv,c->argc);
|
||||
}
|
||||
|
||||
/* ===================== WATCH (CAS alike for MULTI/EXEC) ===================
|
||||
|
||||
@@ -55,6 +55,7 @@ redisClient *createClient(int fd) {
|
||||
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;
|
||||
@@ -364,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);
|
||||
}
|
||||
}
|
||||
@@ -1302,6 +1306,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);
|
||||
|
||||
@@ -803,7 +803,7 @@ robj *rdbLoadObject(int rdbtype, rio *rdb) {
|
||||
}
|
||||
|
||||
/* This will also be called when the set was just converted
|
||||
* to regular hashtable encoded set */
|
||||
* to regular hash table encoded set */
|
||||
if (o->encoding == REDIS_ENCODING_HT) {
|
||||
dictAdd((dict*)o->ptr,ele,NULL);
|
||||
} else {
|
||||
|
||||
@@ -201,7 +201,10 @@ static void readHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
|
||||
if (config.requests_finished < config.requests)
|
||||
config.latency[config.requests_finished++] = c->latency;
|
||||
c->pending--;
|
||||
if (c->pending == 0) clientDone(c);
|
||||
if (c->pending == 0) {
|
||||
clientDone(c);
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
@@ -263,6 +266,8 @@ static client createClient(char *cmd, size_t len) {
|
||||
fprintf(stderr,"%s: %s\n",config.hostsocket,c->context->errstr);
|
||||
exit(1);
|
||||
}
|
||||
/* Suppress hiredis cleanup of unused buffers for max speed. */
|
||||
c->context->reader->maxbuf = 0;
|
||||
/* Queue N requests accordingly to the pipeline size. */
|
||||
c->obuf = sdsempty();
|
||||
for (j = 0; j < config.pipeline; j++)
|
||||
|
||||
+21
-7
@@ -31,6 +31,7 @@
|
||||
#define REDIS_ENCODING_HT 3 /* Encoded as an hash table */
|
||||
|
||||
/* Object types only used for dumping to disk */
|
||||
#define REDIS_EXPIRETIME_MS 252
|
||||
#define REDIS_EXPIRETIME 253
|
||||
#define REDIS_SELECTDB 254
|
||||
#define REDIS_EOF 255
|
||||
@@ -111,6 +112,16 @@ static char types[256][16];
|
||||
/* Prototypes */
|
||||
uint64_t crc64(uint64_t crc, const unsigned char *s, uint64_t l);
|
||||
|
||||
/* Return true if 't' is a valid object type. */
|
||||
int checkType(unsigned char t) {
|
||||
/* In case a new object type is added, update the following
|
||||
* condition as necessary. */
|
||||
return
|
||||
(t >= REDIS_HASH_ZIPMAP && t <= REDIS_HASH_ZIPLIST) ||
|
||||
t <= REDIS_HASH ||
|
||||
t >= REDIS_EXPIRETIME_MS;
|
||||
}
|
||||
|
||||
/* when number of bytes to read is negative, do a peek */
|
||||
int readBytes(void *target, long num) {
|
||||
char peek = (num < 0) ? 1 : 0;
|
||||
@@ -152,7 +163,7 @@ int loadType(entry *e) {
|
||||
/* this byte needs to qualify as type */
|
||||
unsigned char t;
|
||||
if (readBytes(&t, 1)) {
|
||||
if (t <= 4 || (t >=9 && t <= 12) || t >= 253) {
|
||||
if (checkType(t)) {
|
||||
e->type = t;
|
||||
return 1;
|
||||
} else {
|
||||
@@ -168,16 +179,18 @@ int loadType(entry *e) {
|
||||
|
||||
int peekType() {
|
||||
unsigned char t;
|
||||
if (readBytes(&t, -1) && (t <= 4 || (t >=9 && t <= 12) || t >= 253))
|
||||
if (readBytes(&t, -1) && (checkType(t)))
|
||||
return t;
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* discard time, just consume the bytes */
|
||||
int processTime() {
|
||||
int processTime(int type) {
|
||||
uint32_t offset = CURR_OFFSET;
|
||||
unsigned char t[4];
|
||||
if (readBytes(t, 4)) {
|
||||
unsigned char t[8];
|
||||
int timelen = (type == REDIS_EXPIRETIME_MS) ? 8 : 4;
|
||||
|
||||
if (readBytes(t,timelen)) {
|
||||
return 1;
|
||||
} else {
|
||||
SHIFT_ERROR(offset, "Could not read time");
|
||||
@@ -472,8 +485,9 @@ entry loadEntry() {
|
||||
return e;
|
||||
} else {
|
||||
/* optionally consume expire */
|
||||
if (e.type == REDIS_EXPIRETIME) {
|
||||
if (!processTime()) return e;
|
||||
if (e.type == REDIS_EXPIRETIME ||
|
||||
e.type == REDIS_EXPIRETIME_MS) {
|
||||
if (!processTime(e.type)) return e;
|
||||
if (!loadType(&e)) return e;
|
||||
}
|
||||
|
||||
|
||||
+7
-7
@@ -712,17 +712,17 @@ static void usage() {
|
||||
" -a <password> Password to use when connecting to the server\n"
|
||||
" -r <repeat> Execute specified command N times\n"
|
||||
" -i <interval> When -r is used, waits <interval> seconds per command.\n"
|
||||
" It is possible to specify sub-second times like -i 0.1.\n"
|
||||
" It is possible to specify sub-second times like -i 0.1\n"
|
||||
" -n <db> Database number\n"
|
||||
" -x Read last argument from STDIN\n"
|
||||
" -d <delimiter> Multi-bulk delimiter in for raw formatting (default: \\n)\n"
|
||||
" -c Enable cluster mode (follow -ASK and -MOVED redirections)\n"
|
||||
" --raw Use raw formatting for replies (default when STDOUT is not a tty)\n"
|
||||
" --latency Enter a special mode continuously sampling latency.\n"
|
||||
" --slave Simulate a slave showing commands received from the master.\n"
|
||||
" --pipe Transfer raw Redis protocol from stdin to server.\n"
|
||||
" --bigkeys Sample Redis keys looking for big keys.\n"
|
||||
" --eval <file> Send an EVAL command using the Lua script at <file>.\n"
|
||||
" --latency Enter a special mode continuously sampling latency\n"
|
||||
" --slave Simulate a slave showing commands received from the master\n"
|
||||
" --pipe Transfer raw Redis protocol from stdin to server\n"
|
||||
" --bigkeys Sample Redis keys looking for big keys\n"
|
||||
" --eval <file> Send an EVAL command using the Lua script at <file>\n"
|
||||
" --help Output this help and exit\n"
|
||||
" --version Output version and exit\n"
|
||||
"\n"
|
||||
@@ -1233,7 +1233,7 @@ int main(int argc, char **argv) {
|
||||
|
||||
/* Pipe mode */
|
||||
if (config.pipe_mode) {
|
||||
cliConnect(0);
|
||||
if (cliConnect(0) == REDIS_ERR) exit(1);
|
||||
pipeMode();
|
||||
}
|
||||
|
||||
|
||||
+196
-79
@@ -106,6 +106,11 @@ struct redisCommand *commandTable;
|
||||
* results. For instance SPOP and RANDOMKEY are two random commands.
|
||||
* S: Sort command output array if called from script, so that the output
|
||||
* is deterministic.
|
||||
* l: Allow command while loading the database.
|
||||
* t: Allow command while a slave has stale data but is not allowed to
|
||||
* server this data. Normally no command is accepted in this condition
|
||||
* but just a few.
|
||||
* M: Do not automatically propagate the command on MONITOR.
|
||||
*/
|
||||
struct redisCommand redisCommandTable[] = {
|
||||
{"get",getCommand,2,"r",0,NULL,1,1,1,0,0},
|
||||
@@ -148,7 +153,7 @@ struct redisCommand redisCommandTable[] = {
|
||||
{"sismember",sismemberCommand,3,"r",0,NULL,1,1,1,0,0},
|
||||
{"scard",scardCommand,2,"r",0,NULL,1,1,1,0,0},
|
||||
{"spop",spopCommand,2,"wRs",0,NULL,1,1,1,0,0},
|
||||
{"srandmember",srandmemberCommand,2,"rR",0,NULL,1,1,1,0,0},
|
||||
{"srandmember",srandmemberCommand,-2,"rR",0,NULL,1,1,1,0,0},
|
||||
{"sinter",sinterCommand,-2,"rS",0,NULL,1,-1,1,0,0},
|
||||
{"sinterstore",sinterstoreCommand,-3,"wm",0,NULL,1,-1,1,0,0},
|
||||
{"sunion",sunionCommand,-2,"rS",0,NULL,1,-1,1,0,0},
|
||||
@@ -212,25 +217,26 @@ struct redisCommand redisCommandTable[] = {
|
||||
{"lastsave",lastsaveCommand,1,"r",0,NULL,0,0,0,0,0},
|
||||
{"type",typeCommand,2,"r",0,NULL,1,1,1,0,0},
|
||||
{"multi",multiCommand,1,"rs",0,NULL,0,0,0,0,0},
|
||||
{"exec",execCommand,1,"s",0,NULL,0,0,0,0,0},
|
||||
{"exec",execCommand,1,"sM",0,NULL,0,0,0,0,0},
|
||||
{"discard",discardCommand,1,"rs",0,NULL,0,0,0,0,0},
|
||||
{"sync",syncCommand,1,"ars",0,NULL,0,0,0,0,0},
|
||||
{"replconf",replconfCommand,-1,"ars",0,NULL,0,0,0,0,0},
|
||||
{"flushdb",flushdbCommand,1,"w",0,NULL,0,0,0,0,0},
|
||||
{"flushall",flushallCommand,1,"w",0,NULL,0,0,0,0,0},
|
||||
{"sort",sortCommand,-2,"wmS",0,NULL,1,1,1,0,0},
|
||||
{"info",infoCommand,-1,"r",0,NULL,0,0,0,0,0},
|
||||
{"sort",sortCommand,-2,"wm",0,NULL,1,1,1,0,0},
|
||||
{"info",infoCommand,-1,"rlt",0,NULL,0,0,0,0,0},
|
||||
{"monitor",monitorCommand,1,"ars",0,NULL,0,0,0,0,0},
|
||||
{"ttl",ttlCommand,2,"r",0,NULL,1,1,1,0,0},
|
||||
{"pttl",pttlCommand,2,"r",0,NULL,1,1,1,0,0},
|
||||
{"persist",persistCommand,2,"w",0,NULL,1,1,1,0,0},
|
||||
{"slaveof",slaveofCommand,3,"as",0,NULL,0,0,0,0,0},
|
||||
{"slaveof",slaveofCommand,3,"ast",0,NULL,0,0,0,0,0},
|
||||
{"debug",debugCommand,-2,"as",0,NULL,0,0,0,0,0},
|
||||
{"config",configCommand,-2,"ar",0,NULL,0,0,0,0,0},
|
||||
{"subscribe",subscribeCommand,-2,"rps",0,NULL,0,0,0,0,0},
|
||||
{"unsubscribe",unsubscribeCommand,-1,"rps",0,NULL,0,0,0,0,0},
|
||||
{"psubscribe",psubscribeCommand,-2,"rps",0,NULL,0,0,0,0,0},
|
||||
{"punsubscribe",punsubscribeCommand,-1,"rps",0,NULL,0,0,0,0,0},
|
||||
{"publish",publishCommand,3,"pf",0,NULL,0,0,0,0,0},
|
||||
{"subscribe",subscribeCommand,-2,"rpslt",0,NULL,0,0,0,0,0},
|
||||
{"unsubscribe",unsubscribeCommand,-1,"rpslt",0,NULL,0,0,0,0,0},
|
||||
{"psubscribe",psubscribeCommand,-2,"rpslt",0,NULL,0,0,0,0,0},
|
||||
{"punsubscribe",punsubscribeCommand,-1,"rpslt",0,NULL,0,0,0,0,0},
|
||||
{"publish",publishCommand,3,"pflt",0,NULL,0,0,0,0,0},
|
||||
{"watch",watchCommand,-2,"rs",0,noPreloadGetKeys,1,-1,1,0,0},
|
||||
{"unwatch",unwatchCommand,1,"rs",0,NULL,0,0,0,0,0},
|
||||
{"restore",restoreCommand,4,"awm",0,NULL,1,1,1,0,0},
|
||||
@@ -327,17 +333,6 @@ err:
|
||||
if (server.logfile) close(fd);
|
||||
}
|
||||
|
||||
/* Redis generally does not try to recover from out of memory conditions
|
||||
* when allocating objects or strings, it is not clear if it will be possible
|
||||
* to report this condition to the client since the networking layer itself
|
||||
* is based on heap allocation for send buffers, so we simply abort.
|
||||
* At least the code will be simpler to read... */
|
||||
void oom(const char *msg) {
|
||||
redisLog(REDIS_WARNING, "%s: Out of memory\n",msg);
|
||||
sleep(1);
|
||||
abort();
|
||||
}
|
||||
|
||||
/* Return the UNIX time in microseconds */
|
||||
long long ustime(void) {
|
||||
struct timeval tv;
|
||||
@@ -803,13 +798,8 @@ void clientsCron(void) {
|
||||
* a macro is used: run_with_period(milliseconds) { .... }
|
||||
*/
|
||||
|
||||
/* Using the following macro you can run code inside serverCron() with the
|
||||
* specified period, specified in milliseconds.
|
||||
* The actual resolution depends on REDIS_HZ. */
|
||||
#define run_with_period(_ms_) if (!(loops % ((_ms_)/(1000/REDIS_HZ))))
|
||||
|
||||
int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
|
||||
int j, loops = server.cronloops;
|
||||
int j;
|
||||
REDIS_NOTUSED(eventLoop);
|
||||
REDIS_NOTUSED(id);
|
||||
REDIS_NOTUSED(clientData);
|
||||
@@ -878,11 +868,14 @@ int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
|
||||
}
|
||||
|
||||
/* Show information about connected clients */
|
||||
run_with_period(5000) {
|
||||
redisLog(REDIS_VERBOSE,"%d clients connected (%d slaves), %zu bytes in use",
|
||||
listLength(server.clients)-listLength(server.slaves),
|
||||
listLength(server.slaves),
|
||||
zmalloc_used_memory());
|
||||
if (!server.sentinel_mode) {
|
||||
run_with_period(5000) {
|
||||
redisLog(REDIS_VERBOSE,
|
||||
"%d clients connected (%d slaves), %zu bytes in use",
|
||||
listLength(server.clients)-listLength(server.slaves),
|
||||
listLength(server.slaves),
|
||||
zmalloc_used_memory());
|
||||
}
|
||||
}
|
||||
|
||||
/* We need to do a few operations on clients asynchronously. */
|
||||
@@ -962,6 +955,11 @@ int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
|
||||
* to detect transfer failures. */
|
||||
run_with_period(1000) replicationCron();
|
||||
|
||||
/* Run the sentinel timer if we are in sentinel mode. */
|
||||
run_with_period(100) {
|
||||
if (server.sentinel_mode) sentinelTimer();
|
||||
}
|
||||
|
||||
server.cronloops++;
|
||||
return 1000/REDIS_HZ;
|
||||
}
|
||||
@@ -1052,6 +1050,7 @@ void createSharedObjects(void) {
|
||||
shared.del = createStringObject("DEL",3);
|
||||
shared.rpop = createStringObject("RPOP",4);
|
||||
shared.lpop = createStringObject("LPOP",4);
|
||||
shared.lpush = createStringObject("LPUSH",5);
|
||||
for (j = 0; j < REDIS_SHARED_INTEGERS; j++) {
|
||||
shared.integers[j] = createObject(REDIS_STRING,(void*)(long)j);
|
||||
shared.integers[j]->encoding = REDIS_ENCODING_INT;
|
||||
@@ -1095,6 +1094,7 @@ void initServerConfig() {
|
||||
server.aof_last_fsync = time(NULL);
|
||||
server.aof_rewrite_time_last = -1;
|
||||
server.aof_rewrite_time_start = -1;
|
||||
server.aof_lastbgrewrite_status = REDIS_OK;
|
||||
server.aof_delayed_fsync = 0;
|
||||
server.aof_fd = -1;
|
||||
server.aof_selected_db = -1; /* Make sure the first time will not match */
|
||||
@@ -1142,6 +1142,7 @@ void initServerConfig() {
|
||||
server.repl_serve_stale_data = 1;
|
||||
server.repl_slave_ro = 1;
|
||||
server.repl_down_since = time(NULL);
|
||||
server.slave_priority = REDIS_DEFAULT_SLAVE_PRIORITY;
|
||||
|
||||
/* Client output buffer limits */
|
||||
server.client_obuf_limits[REDIS_CLIENT_LIMIT_CLASS_NORMAL].hard_limit_bytes = 0;
|
||||
@@ -1168,6 +1169,8 @@ void initServerConfig() {
|
||||
server.delCommand = lookupCommandByCString("del");
|
||||
server.multiCommand = lookupCommandByCString("multi");
|
||||
server.lpushCommand = lookupCommandByCString("lpush");
|
||||
server.lpopCommand = lookupCommandByCString("lpop");
|
||||
server.rpopCommand = lookupCommandByCString("rpop");
|
||||
|
||||
/* Slow log */
|
||||
server.slowlog_log_slower_than = REDIS_SLOWLOG_LOG_SLOWER_THAN;
|
||||
@@ -1243,6 +1246,7 @@ void initServer() {
|
||||
server.slaves = listCreate();
|
||||
server.monitors = listCreate();
|
||||
server.unblocked_clients = listCreate();
|
||||
server.ready_keys = listCreate();
|
||||
|
||||
createSharedObjects();
|
||||
adjustOpenFilesLimit();
|
||||
@@ -1273,6 +1277,7 @@ void initServer() {
|
||||
server.db[j].dict = dictCreate(&dbDictType,NULL);
|
||||
server.db[j].expires = dictCreate(&keyptrDictType,NULL);
|
||||
server.db[j].blocking_keys = dictCreate(&keylistDictType,NULL);
|
||||
server.db[j].ready_keys = dictCreate(&setDictType,NULL);
|
||||
server.db[j].watched_keys = dictCreate(&keylistDictType,NULL);
|
||||
server.db[j].id = j;
|
||||
}
|
||||
@@ -1308,9 +1313,9 @@ void initServer() {
|
||||
server.stop_writes_on_bgsave_err = 1;
|
||||
aeCreateTimeEvent(server.el, 1, serverCron, NULL, NULL);
|
||||
if (server.ipfd > 0 && aeCreateFileEvent(server.el,server.ipfd,AE_READABLE,
|
||||
acceptTcpHandler,NULL) == AE_ERR) oom("creating file event");
|
||||
acceptTcpHandler,NULL) == AE_ERR) redisPanic("Unrecoverable error creating server.ipfd file event.");
|
||||
if (server.sofd > 0 && aeCreateFileEvent(server.el,server.sofd,AE_READABLE,
|
||||
acceptUnixHandler,NULL) == AE_ERR) oom("creating file event");
|
||||
acceptUnixHandler,NULL) == AE_ERR) redisPanic("Unrecoverable error creating server.sofd file event.");
|
||||
|
||||
if (server.aof_state == REDIS_AOF_ON) {
|
||||
server.aof_fd = open(server.aof_filename,
|
||||
@@ -1324,11 +1329,11 @@ void initServer() {
|
||||
|
||||
/* 32 bit instances are limited to 4GB of address space, so if there is
|
||||
* no explicit limit in the user provided configuration we set a limit
|
||||
* at 3.5GB using maxmemory with 'noeviction' policy'. This saves
|
||||
* useless crashes of the Redis instance. */
|
||||
* at 3 GB using maxmemory with 'noeviction' policy'. This avoids
|
||||
* useless crashes of the Redis instance for out of memory. */
|
||||
if (server.arch_bits == 32 && server.maxmemory == 0) {
|
||||
redisLog(REDIS_WARNING,"Warning: 32 bit instance detected but no memory limit set. Setting 3.5 GB maxmemory limit with 'noeviction' policy now.");
|
||||
server.maxmemory = 3584LL*(1024*1024); /* 3584 MB = 3.5 GB */
|
||||
redisLog(REDIS_WARNING,"Warning: 32 bit instance detected but no memory limit set. Setting 3 GB maxmemory limit with 'noeviction' policy now.");
|
||||
server.maxmemory = 3072LL*(1024*1024); /* 3 GB */
|
||||
server.maxmemory_policy = REDIS_MAXMEMORY_NO_EVICTION;
|
||||
}
|
||||
|
||||
@@ -1359,6 +1364,9 @@ void populateCommandTable(void) {
|
||||
case 's': c->flags |= REDIS_CMD_NOSCRIPT; break;
|
||||
case 'R': c->flags |= REDIS_CMD_RANDOM; break;
|
||||
case 'S': c->flags |= REDIS_CMD_SORT_FOR_SCRIPT; break;
|
||||
case 'l': c->flags |= REDIS_CMD_LOADING; break;
|
||||
case 't': c->flags |= REDIS_CMD_STALE; break;
|
||||
case 'M': c->flags |= REDIS_CMD_SKIP_MONITOR; break;
|
||||
default: redisPanic("Unsupported command flag"); break;
|
||||
}
|
||||
f++;
|
||||
@@ -1434,7 +1442,7 @@ struct redisCommand *lookupCommandByCString(char *s) {
|
||||
}
|
||||
|
||||
/* Propagate the specified command (in the context of the specified database id)
|
||||
* to AOF, Slaves and Monitors.
|
||||
* to AOF and Slaves.
|
||||
*
|
||||
* flags are an xor between:
|
||||
* + REDIS_PROPAGATE_NONE (no propagation of command at all)
|
||||
@@ -1464,8 +1472,12 @@ void call(redisClient *c, int flags) {
|
||||
|
||||
/* 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)
|
||||
if (listLength(server.monitors) &&
|
||||
!server.loading &&
|
||||
!(c->cmd->flags & REDIS_CMD_SKIP_MONITOR))
|
||||
{
|
||||
replicationFeedMonitors(c,server.monitors,c->db->id,c->argv,c->argc);
|
||||
}
|
||||
|
||||
/* Call the command. */
|
||||
redisOpArrayInit(&server.also_propagate);
|
||||
@@ -1577,7 +1589,7 @@ int processCommand(redisClient *c) {
|
||||
return REDIS_OK;
|
||||
}
|
||||
|
||||
/* Don't accept wirte commands if this is a read only slave. But
|
||||
/* Don't accept write commands if this is a read only slave. But
|
||||
* accept write commands if this is our master. */
|
||||
if (server.masterhost && server.repl_slave_ro &&
|
||||
!(c->flags & REDIS_MASTER) &&
|
||||
@@ -1602,20 +1614,22 @@ int processCommand(redisClient *c) {
|
||||
* we are a slave with a broken link with master. */
|
||||
if (server.masterhost && server.repl_state != REDIS_REPL_CONNECTED &&
|
||||
server.repl_serve_stale_data == 0 &&
|
||||
c->cmd->proc != infoCommand && c->cmd->proc != slaveofCommand)
|
||||
!(c->cmd->flags & REDIS_CMD_STALE))
|
||||
{
|
||||
addReply(c, shared.masterdownerr);
|
||||
return REDIS_OK;
|
||||
}
|
||||
|
||||
/* Loading DB? Return an error if the command is not INFO */
|
||||
if (server.loading && c->cmd->proc != infoCommand) {
|
||||
/* Loading DB? Return an error if the command has not the
|
||||
* REDIS_CMD_LOADING flag. */
|
||||
if (server.loading && !(c->cmd->flags & REDIS_CMD_LOADING)) {
|
||||
addReply(c, shared.loadingerr);
|
||||
return REDIS_OK;
|
||||
}
|
||||
|
||||
/* Lua script too slow? Only allow SHUTDOWN NOSAVE and SCRIPT KILL. */
|
||||
/* Lua script too slow? Only allow commands with REDIS_CMD_STALE flag. */
|
||||
if (server.lua_timedout &&
|
||||
c->cmd->proc != authCommand &&
|
||||
!(c->cmd->proc == shutdownCommand &&
|
||||
c->argc == 2 &&
|
||||
tolower(((char*)c->argv[1]->ptr)[0]) == 'n') &&
|
||||
@@ -1636,6 +1650,8 @@ int processCommand(redisClient *c) {
|
||||
addReply(c,shared.queued);
|
||||
} else {
|
||||
call(c,REDIS_CALL_FULL);
|
||||
if (listLength(server.ready_keys))
|
||||
handleClientsBlockedOnLists();
|
||||
}
|
||||
return REDIS_OK;
|
||||
}
|
||||
@@ -1698,10 +1714,52 @@ int prepareForShutdown(int flags) {
|
||||
|
||||
/*================================== Commands =============================== */
|
||||
|
||||
/* Return zero if strings are the same, non-zero if they are not.
|
||||
* The comparison is performed in a way that prevents an attacker to obtain
|
||||
* information about the nature of the strings just monitoring the execution
|
||||
* time of the function.
|
||||
*
|
||||
* Note that limiting the comparison length to strings up to 512 bytes we
|
||||
* can avoid leaking any information about the password length and any
|
||||
* possible branch misprediction related leak.
|
||||
*/
|
||||
int time_independent_strcmp(char *a, char *b) {
|
||||
char bufa[REDIS_AUTHPASS_MAX_LEN], bufb[REDIS_AUTHPASS_MAX_LEN];
|
||||
/* The above two strlen perform len(a) + len(b) operations where either
|
||||
* a or b are fixed (our password) length, and the difference is only
|
||||
* relative to the length of the user provided string, so no information
|
||||
* leak is possible in the following two lines of code. */
|
||||
int alen = strlen(a);
|
||||
int blen = strlen(b);
|
||||
int j;
|
||||
int diff = 0;
|
||||
|
||||
/* We can't compare strings longer than our static buffers.
|
||||
* Note that this will never pass the first test in practical circumstances
|
||||
* so there is no info leak. */
|
||||
if (alen > sizeof(bufa) || blen > sizeof(bufb)) return 1;
|
||||
|
||||
memset(bufa,0,sizeof(bufa)); /* Constant time. */
|
||||
memset(bufb,0,sizeof(bufb)); /* Constant time. */
|
||||
/* Again the time of the following two copies is proportional to
|
||||
* len(a) + len(b) so no info is leaked. */
|
||||
memcpy(bufa,a,alen);
|
||||
memcpy(bufb,b,blen);
|
||||
|
||||
/* Always compare all the chars in the two buffers without
|
||||
* conditional expressions. */
|
||||
for (j = 0; j < sizeof(bufa); j++) {
|
||||
diff |= (bufa[j] ^ bufb[j]);
|
||||
}
|
||||
/* Length must be equal as well. */
|
||||
diff |= alen ^ blen;
|
||||
return diff; /* If zero strings are the same. */
|
||||
}
|
||||
|
||||
void authCommand(redisClient *c) {
|
||||
if (!server.requirepass) {
|
||||
addReplyError(c,"Client sent AUTH, but no password is set");
|
||||
} else if (!strcmp(c->argv[1]->ptr, server.requirepass)) {
|
||||
} else if (!time_independent_strcmp(c->argv[1]->ptr, server.requirepass)) {
|
||||
c->authenticated = 1;
|
||||
addReply(c,shared.ok);
|
||||
} else {
|
||||
@@ -1761,7 +1819,7 @@ sds genRedisInfoString(char *section) {
|
||||
unsigned long lol, bib;
|
||||
int allsections = 0, defsections = 0;
|
||||
int sections = 0;
|
||||
|
||||
|
||||
if (section) {
|
||||
allsections = strcasecmp(section,"all") == 0;
|
||||
defsections = strcasecmp(section,"default") == 0;
|
||||
@@ -1774,7 +1832,11 @@ sds genRedisInfoString(char *section) {
|
||||
/* Server */
|
||||
if (allsections || defsections || !strcasecmp(section,"server")) {
|
||||
struct utsname name;
|
||||
char *mode;
|
||||
|
||||
if (server.sentinel_mode) mode = "sentinel";
|
||||
else mode = "standalone";
|
||||
|
||||
if (sections++) info = sdscat(info,"\r\n");
|
||||
uname(&name);
|
||||
info = sdscatprintf(info,
|
||||
@@ -1782,6 +1844,7 @@ sds genRedisInfoString(char *section) {
|
||||
"redis_version:%s\r\n"
|
||||
"redis_git_sha1:%s\r\n"
|
||||
"redis_git_dirty:%d\r\n"
|
||||
"redis_mode:%s\r\n"
|
||||
"os:%s %s %s\r\n"
|
||||
"arch_bits:%d\r\n"
|
||||
"multiplexing_api:%s\r\n"
|
||||
@@ -1795,6 +1858,7 @@ sds genRedisInfoString(char *section) {
|
||||
REDIS_VERSION,
|
||||
redisGitSHA1(),
|
||||
strtol(redisGitDirty(),NULL,10) > 0,
|
||||
mode,
|
||||
name.sysname, name.release, name.machine,
|
||||
server.arch_bits,
|
||||
aeGetApiName(),
|
||||
@@ -1870,12 +1934,13 @@ sds genRedisInfoString(char *section) {
|
||||
"aof_rewrite_in_progress:%d\r\n"
|
||||
"aof_rewrite_scheduled:%d\r\n"
|
||||
"aof_last_rewrite_time_sec:%ld\r\n"
|
||||
"aof_current_rewrite_time_sec:%ld\r\n",
|
||||
"aof_current_rewrite_time_sec:%ld\r\n"
|
||||
"aof_last_bgrewrite_status:%s\r\n",
|
||||
server.loading,
|
||||
server.dirty,
|
||||
server.rdb_child_pid != -1,
|
||||
server.lastsave,
|
||||
server.lastbgsave_status == REDIS_OK ? "ok" : "err",
|
||||
(server.lastbgsave_status == REDIS_OK) ? "ok" : "err",
|
||||
server.rdb_save_time_last,
|
||||
(server.rdb_child_pid == -1) ?
|
||||
-1 : time(NULL)-server.rdb_save_time_start,
|
||||
@@ -1884,7 +1949,8 @@ sds genRedisInfoString(char *section) {
|
||||
server.aof_rewrite_scheduled,
|
||||
server.aof_rewrite_time_last,
|
||||
(server.aof_child_pid == -1) ?
|
||||
-1 : time(NULL)-server.aof_rewrite_time_start);
|
||||
-1 : time(NULL)-server.aof_rewrite_time_start,
|
||||
(server.aof_lastbgrewrite_status == REDIS_OK) ? "ok" : "err");
|
||||
|
||||
if (server.aof_state != REDIS_AOF_OFF) {
|
||||
info = sdscatprintf(info,
|
||||
@@ -1892,7 +1958,7 @@ sds genRedisInfoString(char *section) {
|
||||
"aof_base_size:%lld\r\n"
|
||||
"aof_pending_rewrite:%d\r\n"
|
||||
"aof_buffer_length:%zu\r\n"
|
||||
"aof_rewrite_buffer_length:%zu\r\n"
|
||||
"aof_rewrite_buffer_length:%lu\r\n"
|
||||
"aof_pending_bio_fsync:%llu\r\n"
|
||||
"aof_delayed_fsync:%lu\r\n",
|
||||
(long long) server.aof_current_size,
|
||||
@@ -1990,9 +2056,10 @@ sds genRedisInfoString(char *section) {
|
||||
|
||||
if (server.repl_state == REDIS_REPL_TRANSFER) {
|
||||
info = sdscatprintf(info,
|
||||
"master_sync_left_bytes:%ld\r\n"
|
||||
"master_sync_left_bytes:%lld\r\n"
|
||||
"master_sync_last_io_seconds_ago:%d\r\n"
|
||||
,(long)server.repl_transfer_left,
|
||||
, (long long)
|
||||
(server.repl_transfer_size - server.repl_transfer_read),
|
||||
(int)(server.unixtime-server.repl_transfer_lastio)
|
||||
);
|
||||
}
|
||||
@@ -2002,6 +2069,11 @@ sds genRedisInfoString(char *section) {
|
||||
"master_link_down_since_seconds:%ld\r\n",
|
||||
(long)server.unixtime-server.repl_down_since);
|
||||
}
|
||||
info = sdscatprintf(info,
|
||||
"slave_priority:%d\r\n"
|
||||
"slave_read_only:%d\r\n",
|
||||
server.slave_priority,
|
||||
server.repl_slave_ro);
|
||||
}
|
||||
info = sdscatprintf(info,
|
||||
"connected_slaves:%lu\r\n",
|
||||
@@ -2033,7 +2105,7 @@ sds genRedisInfoString(char *section) {
|
||||
}
|
||||
if (state == NULL) continue;
|
||||
info = sdscatprintf(info,"slave%d:%s,%d,%s\r\n",
|
||||
slaveid,ip,port,state);
|
||||
slaveid,ip,slave->slave_listening_port,state);
|
||||
slaveid++;
|
||||
}
|
||||
}
|
||||
@@ -2341,21 +2413,25 @@ void usage() {
|
||||
fprintf(stderr," ./redis-server /etc/redis/6379.conf\n");
|
||||
fprintf(stderr," ./redis-server --port 7777\n");
|
||||
fprintf(stderr," ./redis-server --port 7777 --slaveof 127.0.0.1 8888\n");
|
||||
fprintf(stderr," ./redis-server /etc/myredis.conf --loglevel verbose\n");
|
||||
fprintf(stderr," ./redis-server /etc/myredis.conf --loglevel verbose\n\n");
|
||||
fprintf(stderr,"Sentinel mode:\n");
|
||||
fprintf(stderr," ./redis-server /etc/sentinel.conf --sentinel\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void redisAsciiArt(void) {
|
||||
#include "asciilogo.h"
|
||||
char *buf = zmalloc(1024*16);
|
||||
char *mode = "stand alone";
|
||||
|
||||
if (server.sentinel_mode) mode = "sentinel";
|
||||
|
||||
snprintf(buf,1024*16,ascii_logo,
|
||||
REDIS_VERSION,
|
||||
redisGitSHA1(),
|
||||
strtol(redisGitDirty(),NULL,10) > 0,
|
||||
(sizeof(long) == 8) ? "64" : "32",
|
||||
"stand alone",
|
||||
server.port,
|
||||
mode, server.port,
|
||||
(long) getpid()
|
||||
);
|
||||
redisLogRaw(REDIS_NOTICE|REDIS_LOG_RAW,buf);
|
||||
@@ -2393,17 +2469,60 @@ void setupSignalHandlers(void) {
|
||||
|
||||
void memtest(size_t megabytes, int passes);
|
||||
|
||||
/* Returns 1 if there is --sentinel among the arguments or if
|
||||
* argv[0] is exactly "redis-sentinel". */
|
||||
int checkForSentinelMode(int argc, char **argv) {
|
||||
int j;
|
||||
|
||||
if (strstr(argv[0],"redis-sentinel") != NULL) return 1;
|
||||
for (j = 1; j < argc; j++)
|
||||
if (!strcmp(argv[j],"--sentinel")) return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Function called at startup to load RDB or AOF file in memory. */
|
||||
void loadDataFromDisk(void) {
|
||||
long long start = ustime();
|
||||
if (server.aof_state == REDIS_AOF_ON) {
|
||||
if (loadAppendOnlyFile(server.aof_filename) == REDIS_OK)
|
||||
redisLog(REDIS_NOTICE,"DB loaded from append only file: %.3f seconds",(float)(ustime()-start)/1000000);
|
||||
} else {
|
||||
if (rdbLoad(server.rdb_filename) == REDIS_OK) {
|
||||
redisLog(REDIS_NOTICE,"DB loaded from disk: %.3f seconds",
|
||||
(float)(ustime()-start)/1000000);
|
||||
} else if (errno != ENOENT) {
|
||||
redisLog(REDIS_WARNING,"Fatal error loading the DB. Exiting.");
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void redisOutOfMemoryHandler(size_t allocation_size) {
|
||||
redisLog(REDIS_WARNING,"Out Of Memory allocating %zu bytes!",
|
||||
allocation_size);
|
||||
redisPanic("OOM");
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
long long start;
|
||||
struct timeval tv;
|
||||
|
||||
/* We need to initialize our libraries, and the server configuration. */
|
||||
zmalloc_enable_thread_safeness();
|
||||
zmalloc_set_oom_handler(redisOutOfMemoryHandler);
|
||||
srand(time(NULL)^getpid());
|
||||
gettimeofday(&tv,NULL);
|
||||
dictSetHashFunctionSeed(tv.tv_sec^tv.tv_usec^getpid());
|
||||
server.sentinel_mode = checkForSentinelMode(argc,argv);
|
||||
initServerConfig();
|
||||
|
||||
/* We need to init sentinel right now as parsing the configuration file
|
||||
* in sentinel mode will have the effect of populating the sentinel
|
||||
* data structures with master nodes to monitor. */
|
||||
if (server.sentinel_mode) {
|
||||
initSentinelConfig();
|
||||
initSentinel();
|
||||
}
|
||||
|
||||
if (argc >= 2) {
|
||||
int j = 1; /* First option to parse in argv[] */
|
||||
sds options = sdsempty();
|
||||
@@ -2449,33 +2568,31 @@ int main(int argc, char **argv) {
|
||||
loadServerConfig(configfile,options);
|
||||
sdsfree(options);
|
||||
} else {
|
||||
redisLog(REDIS_WARNING,"Warning: no config file specified, using the default config. In order to specify a config file use 'redis-server /path/to/redis.conf'");
|
||||
redisLog(REDIS_WARNING, "Warning: no config file specified, using the default config. In order to specify a config file use %s /path/to/%s.conf", argv[0], server.sentinel_mode ? "sentinel" : "redis");
|
||||
}
|
||||
if (server.daemonize) daemonize();
|
||||
initServer();
|
||||
if (server.daemonize) createPidFile();
|
||||
redisAsciiArt();
|
||||
redisLog(REDIS_WARNING,"Server started, Redis version " REDIS_VERSION);
|
||||
#ifdef __linux__
|
||||
linuxOvercommitMemoryWarning();
|
||||
#endif
|
||||
start = ustime();
|
||||
if (server.aof_state == REDIS_AOF_ON) {
|
||||
if (loadAppendOnlyFile(server.aof_filename) == REDIS_OK)
|
||||
redisLog(REDIS_NOTICE,"DB loaded from append only file: %.3f seconds",(float)(ustime()-start)/1000000);
|
||||
} else {
|
||||
if (rdbLoad(server.rdb_filename) == REDIS_OK) {
|
||||
redisLog(REDIS_NOTICE,"DB loaded from disk: %.3f seconds",
|
||||
(float)(ustime()-start)/1000000);
|
||||
} else if (errno != ENOENT) {
|
||||
redisLog(REDIS_WARNING,"Fatal error loading the DB. Exiting.");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (!server.sentinel_mode) {
|
||||
/* Things only needed when not runnign in Sentinel mode. */
|
||||
redisLog(REDIS_WARNING,"Server started, Redis version " REDIS_VERSION);
|
||||
#ifdef __linux__
|
||||
linuxOvercommitMemoryWarning();
|
||||
#endif
|
||||
loadDataFromDisk();
|
||||
if (server.ipfd > 0)
|
||||
redisLog(REDIS_NOTICE,"The server is now ready to accept connections on port %d", server.port);
|
||||
if (server.sofd > 0)
|
||||
redisLog(REDIS_NOTICE,"The server is now ready to accept connections at %s", server.unixsocket);
|
||||
}
|
||||
if (server.ipfd > 0)
|
||||
redisLog(REDIS_NOTICE,"The server is now ready to accept connections on port %d", server.port);
|
||||
if (server.sofd > 0)
|
||||
redisLog(REDIS_NOTICE,"The server is now ready to accept connections at %s", server.unixsocket);
|
||||
|
||||
/* Warning the user about suspicious maxmemory setting. */
|
||||
if (server.maxmemory > 0 && server.maxmemory < 1024*1024) {
|
||||
redisLog(REDIS_WARNING,"WARNING: You specified a maxmemory value that is less than 1MB (current value is %llu bytes). Are you sure this is what you really want?", server.maxmemory);
|
||||
}
|
||||
|
||||
aeSetBeforeSleepProc(server.el,beforeSleep);
|
||||
aeMain(server.el);
|
||||
aeDeleteEventLoop(server.el);
|
||||
|
||||
+57
-9
@@ -56,10 +56,10 @@
|
||||
#define REDIS_SLOWLOG_LOG_SLOWER_THAN 10000
|
||||
#define REDIS_SLOWLOG_MAX_LEN 128
|
||||
#define REDIS_MAX_CLIENTS 10000
|
||||
|
||||
#define REDIS_AUTHPASS_MAX_LEN 512
|
||||
#define REDIS_DEFAULT_SLAVE_PRIORITY 100
|
||||
#define REDIS_REPL_TIMEOUT 60
|
||||
#define REDIS_REPL_PING_SLAVE_PERIOD 10
|
||||
|
||||
#define REDIS_RUN_ID_SIZE 40
|
||||
#define REDIS_OPS_SEC_SAMPLES 16
|
||||
|
||||
@@ -84,6 +84,9 @@
|
||||
#define REDIS_CMD_NOSCRIPT 64 /* "s" flag */
|
||||
#define REDIS_CMD_RANDOM 128 /* "R" flag */
|
||||
#define REDIS_CMD_SORT_FOR_SCRIPT 256 /* "S" flag */
|
||||
#define REDIS_CMD_LOADING 512 /* "l" flag */
|
||||
#define REDIS_CMD_STALE 1024 /* "t" flag */
|
||||
#define REDIS_CMD_SKIP_MONITOR 2048 /* "M" flag */
|
||||
|
||||
/* Object types */
|
||||
#define REDIS_STRING 0
|
||||
@@ -165,8 +168,9 @@
|
||||
#define REDIS_REPL_NONE 0 /* No active replication */
|
||||
#define REDIS_REPL_CONNECT 1 /* Must connect to master */
|
||||
#define REDIS_REPL_CONNECTING 2 /* Connecting to master */
|
||||
#define REDIS_REPL_TRANSFER 3 /* Receiving .rdb from master */
|
||||
#define REDIS_REPL_CONNECTED 4 /* Connected to master */
|
||||
#define REDIS_REPL_RECEIVE_PONG 3 /* Wait for PING reply */
|
||||
#define REDIS_REPL_TRANSFER 4 /* Receiving .rdb from master */
|
||||
#define REDIS_REPL_CONNECTED 5 /* Connected to master */
|
||||
|
||||
/* Synchronous read timeout - slave side */
|
||||
#define REDIS_REPL_SYNCIO_TIMEOUT 5
|
||||
@@ -254,6 +258,11 @@
|
||||
#define REDIS_PROPAGATE_AOF 1
|
||||
#define REDIS_PROPAGATE_REPL 2
|
||||
|
||||
/* Using the following macro you can run code inside serverCron() with the
|
||||
* specified period, specified in milliseconds.
|
||||
* The actual resolution depends on REDIS_HZ. */
|
||||
#define run_with_period(_ms_) if (!(server.cronloops%((_ms_)/(1000/REDIS_HZ))))
|
||||
|
||||
/* We can print the stacktrace, so our assert is defined this way: */
|
||||
#define redisAssertWithInfo(_c,_o,_e) ((_e)?(void)0 : (_redisAssertWithInfo(_c,_o,#_e,__FILE__,__LINE__),_exit(1)))
|
||||
#define redisAssert(_e) ((_e)?(void)0 : (_redisAssert(#_e,__FILE__,__LINE__),_exit(1)))
|
||||
@@ -292,6 +301,7 @@ typedef struct redisDb {
|
||||
dict *dict; /* The keyspace for this DB */
|
||||
dict *expires; /* Timeout of keys with a timeout set */
|
||||
dict *blocking_keys; /* Keys with clients waiting for data (BLPOP) */
|
||||
dict *ready_keys; /* Blocked keys that received a PUSH */
|
||||
dict *watched_keys; /* WATCHED keys for MULTI/EXEC CAS */
|
||||
int id;
|
||||
} redisDb;
|
||||
@@ -318,6 +328,22 @@ typedef struct blockingState {
|
||||
* for BRPOPLPUSH. */
|
||||
} blockingState;
|
||||
|
||||
/* The following structure represents a node in the server.ready_keys list,
|
||||
* where we accumulate all the keys that had clients blocked with a blocking
|
||||
* operation such as B[LR]POP, but received new data in the context of the
|
||||
* last executed command.
|
||||
*
|
||||
* After the execution of every command or script, we run this list to check
|
||||
* if as a result we should serve data to clients blocked, unblocking them.
|
||||
* Note that server.ready_keys will not have duplicates as there dictionary
|
||||
* also called ready_keys in every structure representing a Redis database,
|
||||
* where we make sure to remember if a given key was already added in the
|
||||
* server.ready_keys list. */
|
||||
typedef struct readyList {
|
||||
redisDb *db;
|
||||
robj *key;
|
||||
} readyList;
|
||||
|
||||
/* With multiplexing we need to take per-clinet state.
|
||||
* Clients are taken in a liked list. */
|
||||
typedef struct redisClient {
|
||||
@@ -345,6 +371,7 @@ typedef struct redisClient {
|
||||
int repldbfd; /* replication DB file descriptor */
|
||||
long repldboff; /* replication DB file offset */
|
||||
off_t repldbsize; /* replication DB file size */
|
||||
int slave_listening_port; /* As configured with: SLAVECONF listening-port */
|
||||
multiState mstate; /* MULTI/EXEC state */
|
||||
blockingState bpop; /* blocking state */
|
||||
list *io_keys; /* Keys this client is waiting to be loaded from the
|
||||
@@ -371,6 +398,7 @@ struct sharedObjectsStruct {
|
||||
*masterdownerr, *roslaveerr,
|
||||
*oomerr, *plus, *messagebulk, *pmessagebulk, *subscribebulk,
|
||||
*unsubscribebulk, *psubscribebulk, *punsubscribebulk, *del, *rpop, *lpop,
|
||||
*lpush,
|
||||
*select[REDIS_SHARED_SELECT_CMDS],
|
||||
*integers[REDIS_SHARED_INTEGERS],
|
||||
*mbulkhdr[REDIS_SHARED_BULKHDR_LEN], /* "*<value>\r\n" */
|
||||
@@ -447,6 +475,7 @@ struct redisServer {
|
||||
int arch_bits; /* 32 or 64 depending on sizeof(long) */
|
||||
int cronloops; /* Number of times the cron function run */
|
||||
char runid[REDIS_RUN_ID_SIZE+1]; /* ID always different at every exec. */
|
||||
int sentinel_mode; /* True if this instance is a Sentinel. */
|
||||
/* Networking */
|
||||
int port; /* TCP listening port */
|
||||
char *bindaddr; /* Bind address or NULL */
|
||||
@@ -465,7 +494,8 @@ struct redisServer {
|
||||
off_t loading_loaded_bytes;
|
||||
time_t loading_start_time;
|
||||
/* Fast pointers to often looked up command */
|
||||
struct redisCommand *delCommand, *multiCommand, *lpushCommand;
|
||||
struct redisCommand *delCommand, *multiCommand, *lpushCommand, *lpopCommand,
|
||||
*rpopCommand;
|
||||
/* Fields used only for stats */
|
||||
time_t stat_starttime; /* Server start time */
|
||||
long long stat_numcommands; /* Number of processed commands */
|
||||
@@ -513,6 +543,7 @@ struct redisServer {
|
||||
time_t aof_last_fsync; /* UNIX time of last fsync() */
|
||||
time_t aof_rewrite_time_last; /* Time used by last AOF rewrite run. */
|
||||
time_t aof_rewrite_time_start; /* Current AOF rewrite start time. */
|
||||
int aof_lastbgrewrite_status; /* REDIS_OK or REDIS_ERR */
|
||||
unsigned long aof_delayed_fsync; /* delayed AOF fsync() counter */
|
||||
/* RDB persistence */
|
||||
long long dirty; /* Changes to DB from the last save */
|
||||
@@ -539,12 +570,14 @@ struct redisServer {
|
||||
char *masterauth; /* AUTH with this password with master */
|
||||
char *masterhost; /* Hostname of master */
|
||||
int masterport; /* Port of master */
|
||||
int repl_ping_slave_period; /* Master pings the salve every N seconds */
|
||||
int repl_ping_slave_period; /* Master pings the slave every N seconds */
|
||||
int repl_timeout; /* Timeout after N seconds of master idle */
|
||||
redisClient *master; /* Client that is master for this slave */
|
||||
int repl_syncio_timeout; /* Timeout for synchronous I/O calls */
|
||||
int repl_state; /* Replication status if the instance is a slave */
|
||||
off_t repl_transfer_left; /* Bytes left reading .rdb */
|
||||
off_t repl_transfer_size; /* Size of RDB to read from master during sync. */
|
||||
off_t repl_transfer_read; /* Amount of RDB read from master during sync. */
|
||||
off_t repl_transfer_last_fsync_off; /* Offset when we fsync-ed last time. */
|
||||
int repl_transfer_s; /* Slave -> Master SYNC socket */
|
||||
int repl_transfer_fd; /* Slave -> Master SYNC temp file descriptor */
|
||||
char *repl_transfer_tmpfile; /* Slave-> master SYNC temp file name */
|
||||
@@ -552,6 +585,7 @@ struct redisServer {
|
||||
int repl_serve_stale_data; /* Serve stale data when link is down? */
|
||||
int repl_slave_ro; /* Slave is read only? */
|
||||
time_t repl_down_since; /* Unix time at which link with master went down */
|
||||
int slave_priority; /* Reported in INFO and used by Sentinel. */
|
||||
/* Limits */
|
||||
unsigned int maxclients; /* Max number of simultaneous clients */
|
||||
unsigned long long maxmemory; /* Max number of memory bytes to use */
|
||||
@@ -560,9 +594,9 @@ struct redisServer {
|
||||
/* Blocked clients */
|
||||
unsigned int bpop_blocked_clients; /* Number of clients blocked by lists */
|
||||
list *unblocked_clients; /* list of clients to unblock before next loop */
|
||||
list *ready_keys; /* List of readyList structures for BLPOP & co */
|
||||
/* Sort parameters - qsort_r() is only available under BSD so we
|
||||
* have to take this state global, in order to pass it to sortCompare() */
|
||||
int sort_dontsort;
|
||||
int sort_desc;
|
||||
int sort_alpha;
|
||||
int sort_bypattern;
|
||||
@@ -770,7 +804,7 @@ int listTypeEqual(listTypeEntry *entry, robj *o);
|
||||
void listTypeDelete(listTypeEntry *entry);
|
||||
void listTypeConvert(robj *subject, int enc);
|
||||
void unblockClientWaitingData(redisClient *c);
|
||||
int handleClientsWaitingListPush(redisClient *c, robj *key, robj *ele);
|
||||
void handleClientsBlockedOnLists(void);
|
||||
void popGenericCommand(redisClient *c, int where);
|
||||
|
||||
/* MULTI/EXEC/WATCH... */
|
||||
@@ -967,6 +1001,12 @@ int *noPreloadGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numke
|
||||
int *renameGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags);
|
||||
int *zunionInterGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags);
|
||||
|
||||
/* Sentinel */
|
||||
void initSentinelConfig(void);
|
||||
void initSentinel(void);
|
||||
void sentinelTimer(void);
|
||||
char *sentinelHandleConfiguration(char **argv, int argc);
|
||||
|
||||
/* Scripting */
|
||||
void scriptingInit(void);
|
||||
|
||||
@@ -1109,6 +1149,7 @@ void scriptCommand(redisClient *c);
|
||||
void timeCommand(redisClient *c);
|
||||
void bitopCommand(redisClient *c);
|
||||
void bitcountCommand(redisClient *c);
|
||||
void replconfCommand(redisClient *c);
|
||||
|
||||
#if defined(__GNUC__)
|
||||
void *calloc(size_t count, size_t size) __attribute__ ((deprecated));
|
||||
@@ -1128,4 +1169,11 @@ sds genRedisInfoString(char *section);
|
||||
void enableWatchdog(int period);
|
||||
void disableWatchdog(void);
|
||||
void watchdogScheduleSignal(int period);
|
||||
void redisLogHexDump(int level, char *descr, void *value, size_t len);
|
||||
|
||||
#define redisDebug(fmt, ...) \
|
||||
printf("DEBUG %s:%d > " fmt "\n", __FILE__, __LINE__, __VA_ARGS__)
|
||||
#define redisDebugMark() \
|
||||
printf("-- MARK %s:%d --\n", __FILE__, __LINE__)
|
||||
|
||||
#endif
|
||||
|
||||
+203
-38
@@ -3,6 +3,7 @@
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
/* ---------------------------------- MASTER -------------------------------- */
|
||||
@@ -145,6 +146,46 @@ void syncCommand(redisClient *c) {
|
||||
return;
|
||||
}
|
||||
|
||||
/* REPLCONF <option> <value> <option> <value> ...
|
||||
* This command is used by a slave in order to configure the replication
|
||||
* process before starting it with the SYNC command.
|
||||
*
|
||||
* Currently the only use of this command is to communicate to the master
|
||||
* what is the listening port of the Slave redis instance, so that the
|
||||
* master can accurately list slaves and their listening ports in
|
||||
* the INFO output.
|
||||
*
|
||||
* In the future the same command can be used in order to configure
|
||||
* the replication to initiate an incremental replication instead of a
|
||||
* full resync. */
|
||||
void replconfCommand(redisClient *c) {
|
||||
int j;
|
||||
|
||||
if ((c->argc % 2) == 0) {
|
||||
/* Number of arguments must be odd to make sure that every
|
||||
* option has a corresponding value. */
|
||||
addReply(c,shared.syntaxerr);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Process every option-value pair. */
|
||||
for (j = 1; j < c->argc; j+=2) {
|
||||
if (!strcasecmp(c->argv[j]->ptr,"listening-port")) {
|
||||
long port;
|
||||
|
||||
if ((getLongFromObjectOrReply(c,c->argv[j+1],
|
||||
&port,NULL) != REDIS_OK))
|
||||
return;
|
||||
c->slave_listening_port = port;
|
||||
} else {
|
||||
addReplyErrorFormat(c,"Unrecognized REPLCONF option: %s",
|
||||
(char*)c->argv[j]->ptr);
|
||||
return;
|
||||
}
|
||||
}
|
||||
addReply(c,shared.ok);
|
||||
}
|
||||
|
||||
void sendBulkToSlave(aeEventLoop *el, int fd, void *privdata, int mask) {
|
||||
redisClient *slave = privdata;
|
||||
REDIS_NOTUSED(el);
|
||||
@@ -271,16 +312,18 @@ void replicationAbortSyncTransfer(void) {
|
||||
}
|
||||
|
||||
/* Asynchronously read the SYNC payload we receive from a master */
|
||||
#define REPL_MAX_WRITTEN_BEFORE_FSYNC (1024*1024*8) /* 8 MB */
|
||||
void readSyncBulkPayload(aeEventLoop *el, int fd, void *privdata, int mask) {
|
||||
char buf[4096];
|
||||
ssize_t nread, readlen;
|
||||
off_t left;
|
||||
REDIS_NOTUSED(el);
|
||||
REDIS_NOTUSED(privdata);
|
||||
REDIS_NOTUSED(mask);
|
||||
|
||||
/* If repl_transfer_left == -1 we still have to read the bulk length
|
||||
/* If repl_transfer_size == -1 we still have to read the bulk length
|
||||
* from the master reply. */
|
||||
if (server.repl_transfer_left == -1) {
|
||||
if (server.repl_transfer_size == -1) {
|
||||
if (syncReadLine(fd,buf,1024,server.repl_syncio_timeout*1000) == -1) {
|
||||
redisLog(REDIS_WARNING,
|
||||
"I/O error reading bulk count from MASTER: %s",
|
||||
@@ -303,16 +346,16 @@ void readSyncBulkPayload(aeEventLoop *el, int fd, void *privdata, int mask) {
|
||||
redisLog(REDIS_WARNING,"Bad protocol from MASTER, the first byte is not '$', are you sure the host and port are right?");
|
||||
goto error;
|
||||
}
|
||||
server.repl_transfer_left = strtol(buf+1,NULL,10);
|
||||
server.repl_transfer_size = strtol(buf+1,NULL,10);
|
||||
redisLog(REDIS_NOTICE,
|
||||
"MASTER <-> SLAVE sync: receiving %ld bytes from master",
|
||||
server.repl_transfer_left);
|
||||
server.repl_transfer_size);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Read bulk data */
|
||||
readlen = (server.repl_transfer_left < (signed)sizeof(buf)) ?
|
||||
server.repl_transfer_left : (signed)sizeof(buf);
|
||||
left = server.repl_transfer_size - server.repl_transfer_read;
|
||||
readlen = (left < (signed)sizeof(buf)) ? left : (signed)sizeof(buf);
|
||||
nread = read(fd,buf,readlen);
|
||||
if (nread <= 0) {
|
||||
redisLog(REDIS_WARNING,"I/O error trying to sync with MASTER: %s",
|
||||
@@ -325,9 +368,23 @@ void readSyncBulkPayload(aeEventLoop *el, int fd, void *privdata, int mask) {
|
||||
redisLog(REDIS_WARNING,"Write error or short write writing to the DB dump file needed for MASTER <-> SLAVE synchronization: %s", strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
server.repl_transfer_left -= nread;
|
||||
server.repl_transfer_read += nread;
|
||||
|
||||
/* Sync data on disk from time to time, otherwise at the end of the transfer
|
||||
* we may suffer a big delay as the memory buffers are copied into the
|
||||
* actual disk. */
|
||||
if (server.repl_transfer_read >=
|
||||
server.repl_transfer_last_fsync_off + REPL_MAX_WRITTEN_BEFORE_FSYNC)
|
||||
{
|
||||
off_t sync_size = server.repl_transfer_read -
|
||||
server.repl_transfer_last_fsync_off;
|
||||
rdb_fsync_range(server.repl_transfer_fd,
|
||||
server.repl_transfer_last_fsync_off, sync_size);
|
||||
server.repl_transfer_last_fsync_off += sync_size;
|
||||
}
|
||||
|
||||
/* Check if the transfer is now complete */
|
||||
if (server.repl_transfer_left == 0) {
|
||||
if (server.repl_transfer_read == server.repl_transfer_size) {
|
||||
if (rename(server.repl_transfer_tmpfile,server.rdb_filename) == -1) {
|
||||
redisLog(REDIS_WARNING,"Failed trying to rename the temp DB into dump.rdb in MASTER <-> SLAVE synchronization: %s", strerror(errno));
|
||||
replicationAbortSyncTransfer();
|
||||
@@ -378,9 +435,57 @@ error:
|
||||
return;
|
||||
}
|
||||
|
||||
/* Send a synchronous command to the master. Used to send AUTH and
|
||||
* REPLCONF commands before starting the replication with SYNC.
|
||||
*
|
||||
* On success NULL is returned.
|
||||
* On error an sds string describing the error is returned.
|
||||
*/
|
||||
char *sendSynchronousCommand(int fd, ...) {
|
||||
va_list ap;
|
||||
sds cmd = sdsempty();
|
||||
char *arg, buf[256];
|
||||
|
||||
/* Create the command to send to the master, we use simple inline
|
||||
* protocol for simplicity as currently we only send simple strings. */
|
||||
va_start(ap,fd);
|
||||
while(1) {
|
||||
arg = va_arg(ap, char*);
|
||||
if (arg == NULL) break;
|
||||
|
||||
if (sdslen(cmd) != 0) cmd = sdscatlen(cmd," ",1);
|
||||
cmd = sdscat(cmd,arg);
|
||||
}
|
||||
cmd = sdscatlen(cmd,"\r\n",2);
|
||||
|
||||
/* Transfer command to the server. */
|
||||
if (syncWrite(fd,cmd,sdslen(cmd),server.repl_syncio_timeout*1000) == -1) {
|
||||
sdsfree(cmd);
|
||||
return sdscatprintf(sdsempty(),"Writing to master: %s",
|
||||
strerror(errno));
|
||||
}
|
||||
sdsfree(cmd);
|
||||
|
||||
/* Read the reply from the server. */
|
||||
if (syncReadLine(fd,buf,sizeof(buf),server.repl_syncio_timeout*1000) == -1)
|
||||
{
|
||||
return sdscatprintf(sdsempty(),"Reading from master: %s",
|
||||
strerror(errno));
|
||||
}
|
||||
|
||||
/* Check for errors from the server. */
|
||||
if (buf[0] != '+') {
|
||||
return sdscatprintf(sdsempty(),"Error from master: %s", buf);
|
||||
}
|
||||
|
||||
return NULL; /* No errors. */
|
||||
}
|
||||
|
||||
void syncWithMaster(aeEventLoop *el, int fd, void *privdata, int mask) {
|
||||
char buf[1024], tmpfile[256];
|
||||
char tmpfile[256], *err;
|
||||
int dfd, maxtries = 5;
|
||||
int sockerr = 0;
|
||||
socklen_t errlen = sizeof(sockerr);
|
||||
REDIS_NOTUSED(el);
|
||||
REDIS_NOTUSED(privdata);
|
||||
REDIS_NOTUSED(mask);
|
||||
@@ -392,32 +497,85 @@ void syncWithMaster(aeEventLoop *el, int fd, void *privdata, int mask) {
|
||||
return;
|
||||
}
|
||||
|
||||
redisLog(REDIS_NOTICE,"Non blocking connect for SYNC fired the event.");
|
||||
/* This event should only be triggered once since it is used to have a
|
||||
* non-blocking connect(2) to the master. It has been triggered when this
|
||||
* function is called, so we can delete it. */
|
||||
aeDeleteFileEvent(server.el,fd,AE_READABLE|AE_WRITABLE);
|
||||
/* Check for errors in the socket. */
|
||||
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &sockerr, &errlen) == -1)
|
||||
sockerr = errno;
|
||||
if (sockerr) {
|
||||
aeDeleteFileEvent(server.el,fd,AE_READABLE|AE_WRITABLE);
|
||||
redisLog(REDIS_WARNING,"Error condition on socket for SYNC: %s",
|
||||
strerror(sockerr));
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* If we were connecting, it's time to send a non blocking PING, we want to
|
||||
* make sure the master is able to reply before going into the actual
|
||||
* replication process where we have long timeouts in the order of
|
||||
* seconds (in the meantime the slave would block). */
|
||||
if (server.repl_state == REDIS_REPL_CONNECTING) {
|
||||
redisLog(REDIS_NOTICE,"Non blocking connect for SYNC fired the event.");
|
||||
/* Delete the writable event so that the readable event remains
|
||||
* registered and we can wait for the PONG reply. */
|
||||
aeDeleteFileEvent(server.el,fd,AE_WRITABLE);
|
||||
server.repl_state = REDIS_REPL_RECEIVE_PONG;
|
||||
/* Send the PING, don't check for errors at all, we have the timeout
|
||||
* that will take care about this. */
|
||||
syncWrite(fd,"PING\r\n",6,100);
|
||||
return;
|
||||
}
|
||||
|
||||
/* Receive the PONG command. */
|
||||
if (server.repl_state == REDIS_REPL_RECEIVE_PONG) {
|
||||
char buf[1024];
|
||||
|
||||
/* Delete the readable event, we no longer need it now that there is
|
||||
* the PING reply to read. */
|
||||
aeDeleteFileEvent(server.el,fd,AE_READABLE);
|
||||
|
||||
/* Read the reply with explicit timeout. */
|
||||
buf[0] = '\0';
|
||||
if (syncReadLine(fd,buf,sizeof(buf),
|
||||
server.repl_syncio_timeout*1000) == -1)
|
||||
{
|
||||
redisLog(REDIS_WARNING,
|
||||
"I/O error reading PING reply from master: %s",
|
||||
strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
|
||||
/* We don't care about the reply, it can be +PONG or an error since
|
||||
* the server requires AUTH. As long as it replies correctly, it's
|
||||
* fine from our point of view. */
|
||||
if (buf[0] != '-' && buf[0] != '+') {
|
||||
redisLog(REDIS_WARNING,"Unexpected reply to PING from master.");
|
||||
goto error;
|
||||
} else {
|
||||
redisLog(REDIS_NOTICE,
|
||||
"Master replied to PING, replication can continue...");
|
||||
}
|
||||
}
|
||||
|
||||
/* AUTH with the master if required. */
|
||||
if(server.masterauth) {
|
||||
char authcmd[1024];
|
||||
size_t authlen;
|
||||
err = sendSynchronousCommand(fd,"AUTH",server.masterauth,NULL);
|
||||
if (err) {
|
||||
redisLog(REDIS_WARNING,"Unable to AUTH to MASTER: %s",err);
|
||||
sdsfree(err);
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
authlen = snprintf(authcmd,sizeof(authcmd),"AUTH %s\r\n",server.masterauth);
|
||||
if (syncWrite(fd,authcmd,authlen,server.repl_syncio_timeout*1000) == -1) {
|
||||
redisLog(REDIS_WARNING,"Unable to AUTH to MASTER: %s",
|
||||
strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
/* Read the AUTH result. */
|
||||
if (syncReadLine(fd,buf,1024,server.repl_syncio_timeout*1000) == -1) {
|
||||
redisLog(REDIS_WARNING,"I/O error reading auth result from MASTER: %s",
|
||||
strerror(errno));
|
||||
goto error;
|
||||
}
|
||||
if (buf[0] != '+') {
|
||||
redisLog(REDIS_WARNING,"Cannot AUTH to MASTER, is the masterauth password correct?");
|
||||
goto error;
|
||||
/* Set the slave port, so that Master's INFO command can list the
|
||||
* slave listening port correctly. */
|
||||
{
|
||||
sds port = sdsfromlonglong(server.port);
|
||||
err = sendSynchronousCommand(fd,"REPLCONF","listening-port",port,
|
||||
NULL);
|
||||
sdsfree(port);
|
||||
/* Ignore the error if any, not all the Redis versions support
|
||||
* REPLCONF listening-port. */
|
||||
if (err) {
|
||||
redisLog(REDIS_NOTICE,"(non critical): Master does not understand REPLCONF listening-port: %s", err);
|
||||
sdsfree(err);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -450,15 +608,18 @@ void syncWithMaster(aeEventLoop *el, int fd, void *privdata, int mask) {
|
||||
}
|
||||
|
||||
server.repl_state = REDIS_REPL_TRANSFER;
|
||||
server.repl_transfer_left = -1;
|
||||
server.repl_transfer_size = -1;
|
||||
server.repl_transfer_read = 0;
|
||||
server.repl_transfer_last_fsync_off = 0;
|
||||
server.repl_transfer_fd = dfd;
|
||||
server.repl_transfer_lastio = server.unixtime;
|
||||
server.repl_transfer_tmpfile = zstrdup(tmpfile);
|
||||
return;
|
||||
|
||||
error:
|
||||
server.repl_state = REDIS_REPL_CONNECT;
|
||||
close(fd);
|
||||
server.repl_transfer_s = -1;
|
||||
server.repl_state = REDIS_REPL_CONNECT;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -491,7 +652,8 @@ int connectWithMaster(void) {
|
||||
void undoConnectWithMaster(void) {
|
||||
int fd = server.repl_transfer_s;
|
||||
|
||||
redisAssert(server.repl_state == REDIS_REPL_CONNECTING);
|
||||
redisAssert(server.repl_state == REDIS_REPL_CONNECTING ||
|
||||
server.repl_state == REDIS_REPL_RECEIVE_PONG);
|
||||
aeDeleteFileEvent(server.el,fd,AE_READABLE|AE_WRITABLE);
|
||||
close(fd);
|
||||
server.repl_transfer_s = -1;
|
||||
@@ -507,7 +669,8 @@ void slaveofCommand(redisClient *c) {
|
||||
if (server.master) freeClient(server.master);
|
||||
if (server.repl_state == REDIS_REPL_TRANSFER)
|
||||
replicationAbortSyncTransfer();
|
||||
else if (server.repl_state == REDIS_REPL_CONNECTING)
|
||||
else if (server.repl_state == REDIS_REPL_CONNECTING ||
|
||||
server.repl_state == REDIS_REPL_RECEIVE_PONG)
|
||||
undoConnectWithMaster();
|
||||
server.repl_state = REDIS_REPL_NONE;
|
||||
redisLog(REDIS_NOTICE,"MASTER MODE enabled (user request)");
|
||||
@@ -545,7 +708,9 @@ void slaveofCommand(redisClient *c) {
|
||||
|
||||
void replicationCron(void) {
|
||||
/* Non blocking connection timeout? */
|
||||
if (server.masterhost && server.repl_state == REDIS_REPL_CONNECTING &&
|
||||
if (server.masterhost &&
|
||||
(server.repl_state == REDIS_REPL_CONNECTING ||
|
||||
server.repl_state == REDIS_REPL_RECEIVE_PONG) &&
|
||||
(time(NULL)-server.repl_transfer_lastio) > server.repl_timeout)
|
||||
{
|
||||
redisLog(REDIS_WARNING,"Timeout connecting to the MASTER...");
|
||||
@@ -556,7 +721,7 @@ void replicationCron(void) {
|
||||
if (server.masterhost && server.repl_state == REDIS_REPL_TRANSFER &&
|
||||
(time(NULL)-server.repl_transfer_lastio) > server.repl_timeout)
|
||||
{
|
||||
redisLog(REDIS_WARNING,"Timeout receiving bulk data from MASTER...");
|
||||
redisLog(REDIS_WARNING,"Timeout receiving bulk data from MASTER... If the problem persists try to set the 'repl-timeout' parameter in redis.conf to a larger value.");
|
||||
replicationAbortSyncTransfer();
|
||||
}
|
||||
|
||||
@@ -580,7 +745,7 @@ void replicationCron(void) {
|
||||
* So slaves can implement an explicit timeout to masters, and will
|
||||
* be able to detect a link disconnection even if the TCP connection
|
||||
* will not actually go down. */
|
||||
if (!(server.cronloops % (server.repl_ping_slave_period*10))) {
|
||||
if (!(server.cronloops % (server.repl_ping_slave_period * REDIS_HZ))) {
|
||||
listIter li;
|
||||
listNode *ln;
|
||||
|
||||
|
||||
+46
-4
@@ -167,6 +167,13 @@ int luaRedisGenericCommand(lua_State *lua, int raise_error) {
|
||||
redisClient *c = server.lua_client;
|
||||
sds reply;
|
||||
|
||||
/* Require at least one argument */
|
||||
if (argc == 0) {
|
||||
luaPushError(lua,
|
||||
"Please specify at least one argument for redis.call()");
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Build the arguments vector */
|
||||
argv = zmalloc(sizeof(robj*)*argc);
|
||||
for (j = 0; j < argc; j++) {
|
||||
@@ -275,11 +282,10 @@ int luaRedisGenericCommand(lua_State *lua, int raise_error) {
|
||||
* reply as expected. */
|
||||
if ((cmd->flags & REDIS_CMD_SORT_FOR_SCRIPT) &&
|
||||
(reply[0] == '*' && reply[1] != '-')) {
|
||||
/* Skip this step if command is SORT but output was already sorted */
|
||||
if (cmd->proc != sortCommand || server.sort_dontsort)
|
||||
luaSortArray(lua);
|
||||
}
|
||||
sdsfree(reply);
|
||||
c->reply_bytes = 0;
|
||||
|
||||
cleanup:
|
||||
/* Clean up. Command code may have changed argv/argc so we use the
|
||||
@@ -326,6 +332,34 @@ int luaRedisSha1hexCommand(lua_State *lua) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Returns a table with a single field 'field' set to the string value
|
||||
* passed as argument. This helper function is handy when returning
|
||||
* a Redis Protocol error or status reply from Lua:
|
||||
*
|
||||
* return redis.error_reply("ERR Some Error")
|
||||
* return redis.status_reply("ERR Some Error")
|
||||
*/
|
||||
int luaRedisReturnSingleFieldTable(lua_State *lua, char *field) {
|
||||
if (lua_gettop(lua) != 1 || lua_type(lua,-1) != LUA_TSTRING) {
|
||||
luaPushError(lua, "wrong number or type of arguments");
|
||||
return 1;
|
||||
}
|
||||
|
||||
lua_newtable(lua);
|
||||
lua_pushstring(lua, field);
|
||||
lua_pushvalue(lua, -3);
|
||||
lua_settable(lua, -3);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int luaRedisErrorReplyCommand(lua_State *lua) {
|
||||
return luaRedisReturnSingleFieldTable(lua,"err");
|
||||
}
|
||||
|
||||
int luaRedisStatusReplyCommand(lua_State *lua) {
|
||||
return luaRedisReturnSingleFieldTable(lua,"ok");
|
||||
}
|
||||
|
||||
int luaLogCommand(lua_State *lua) {
|
||||
int j, argc = lua_gettop(lua);
|
||||
int level;
|
||||
@@ -510,6 +544,14 @@ void scriptingInit(void) {
|
||||
lua_pushcfunction(lua, luaRedisSha1hexCommand);
|
||||
lua_settable(lua, -3);
|
||||
|
||||
/* redis.error_reply and redis.status_reply */
|
||||
lua_pushstring(lua, "error_reply");
|
||||
lua_pushcfunction(lua, luaRedisErrorReplyCommand);
|
||||
lua_settable(lua, -3);
|
||||
lua_pushstring(lua, "status_reply");
|
||||
lua_pushcfunction(lua, luaRedisStatusReplyCommand);
|
||||
lua_settable(lua, -3);
|
||||
|
||||
/* Finally set the table as 'redis' global var. */
|
||||
lua_setglobal(lua,"redis");
|
||||
|
||||
@@ -935,9 +977,9 @@ void scriptCommand(redisClient *c) {
|
||||
sdsfree(sha);
|
||||
} else if (c->argc == 2 && !strcasecmp(c->argv[1]->ptr,"kill")) {
|
||||
if (server.lua_caller == NULL) {
|
||||
addReplyError(c,"No scripts in execution right now.");
|
||||
addReplySds(c,sdsnew("-NOTBUSY No scripts in execution right now.\r\n"));
|
||||
} else if (server.lua_write_dirty) {
|
||||
addReplyError(c, "Sorry the script already executed write commands against the dataset. You can either wait the script termination or kill the server in an hard way using the SHUTDOWN NOSAVE command.");
|
||||
addReplySds(c,sdsnew("-UNKILLABLE Sorry the script already executed write commands against the dataset. You can either wait the script termination or kill the server in an hard way using the SHUTDOWN NOSAVE command.\r\n"));
|
||||
} else {
|
||||
server.lua_kill = 1;
|
||||
addReply(c,shared.ok);
|
||||
|
||||
@@ -179,7 +179,7 @@ sds sdsgrowzero(sds s, size_t len) {
|
||||
return s;
|
||||
}
|
||||
|
||||
sds sdscatlen(sds s, void *t, size_t len) {
|
||||
sds sdscatlen(sds s, const void *t, size_t len) {
|
||||
struct sdshdr *sh;
|
||||
size_t curlen = sdslen(s);
|
||||
|
||||
@@ -193,15 +193,15 @@ sds sdscatlen(sds s, void *t, size_t len) {
|
||||
return s;
|
||||
}
|
||||
|
||||
sds sdscat(sds s, char *t) {
|
||||
sds sdscat(sds s, const char *t) {
|
||||
return sdscatlen(s, t, strlen(t));
|
||||
}
|
||||
|
||||
sds sdscatsds(sds s, sds t) {
|
||||
sds sdscatsds(sds s, const sds t) {
|
||||
return sdscatlen(s, t, sdslen(t));
|
||||
}
|
||||
|
||||
sds sdscpylen(sds s, char *t, size_t len) {
|
||||
sds sdscpylen(sds s, const char *t, size_t len) {
|
||||
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
|
||||
size_t totlen = sh->free+sh->len;
|
||||
|
||||
@@ -218,7 +218,7 @@ sds sdscpylen(sds s, char *t, size_t len) {
|
||||
return s;
|
||||
}
|
||||
|
||||
sds sdscpy(sds s, char *t) {
|
||||
sds sdscpy(sds s, const char *t) {
|
||||
return sdscpylen(s, t, strlen(t));
|
||||
}
|
||||
|
||||
@@ -314,7 +314,7 @@ void sdstoupper(sds s) {
|
||||
for (j = 0; j < len; j++) s[j] = toupper(s[j]);
|
||||
}
|
||||
|
||||
int sdscmp(sds s1, sds s2) {
|
||||
int sdscmp(const sds s1, const sds s2) {
|
||||
size_t l1, l2, minlen;
|
||||
int cmp;
|
||||
|
||||
@@ -342,7 +342,7 @@ int sdscmp(sds s1, sds s2) {
|
||||
* requires length arguments. sdssplit() is just the
|
||||
* same function but for zero-terminated strings.
|
||||
*/
|
||||
sds *sdssplitlen(char *s, int len, char *sep, int seplen, int *count) {
|
||||
sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count) {
|
||||
int elements = 0, slots = 5, start = 0, j;
|
||||
sds *tokens;
|
||||
|
||||
@@ -413,7 +413,7 @@ sds sdsfromlonglong(long long value) {
|
||||
return sdsnewlen(p,32-(p-buf));
|
||||
}
|
||||
|
||||
sds sdscatrepr(sds s, char *p, size_t len) {
|
||||
sds sdscatrepr(sds s, const char *p, size_t len) {
|
||||
s = sdscatlen(s,"\"",1);
|
||||
while(len--) {
|
||||
switch(*p) {
|
||||
@@ -481,8 +481,8 @@ int hex_digit_to_int(char c) {
|
||||
* Note that sdscatrepr() is able to convert back a string into
|
||||
* a quoted string in the same format sdssplitargs() is able to parse.
|
||||
*/
|
||||
sds *sdssplitargs(char *line, int *argc) {
|
||||
char *p = line;
|
||||
sds *sdssplitargs(const char *line, int *argc) {
|
||||
const char *p = line;
|
||||
char *current = NULL;
|
||||
char **vector = NULL;
|
||||
|
||||
@@ -604,7 +604,7 @@ void sdssplitargs_free(sds *argv, int argc) {
|
||||
*
|
||||
* The function returns the sds string pointer, that is always the same
|
||||
* as the input pointer since no resize is needed. */
|
||||
sds sdsmapchars(sds s, char *from, char *to, size_t setlen) {
|
||||
sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen) {
|
||||
size_t j, i, l = sdslen(s);
|
||||
|
||||
for (j = 0; j < l; j++) {
|
||||
|
||||
@@ -60,13 +60,13 @@ sds sdsempty();
|
||||
size_t sdslen(const sds s);
|
||||
sds sdsdup(const sds s);
|
||||
void sdsfree(sds s);
|
||||
size_t sdsavail(sds s);
|
||||
size_t sdsavail(const sds s);
|
||||
sds sdsgrowzero(sds s, size_t len);
|
||||
sds sdscatlen(sds s, void *t, size_t len);
|
||||
sds sdscat(sds s, char *t);
|
||||
sds sdscatsds(sds s, sds t);
|
||||
sds sdscpylen(sds s, char *t, size_t len);
|
||||
sds sdscpy(sds s, char *t);
|
||||
sds sdscatlen(sds s, const void *t, size_t len);
|
||||
sds sdscat(sds s, const char *t);
|
||||
sds sdscatsds(sds s, const sds t);
|
||||
sds sdscpylen(sds s, const char *t, size_t len);
|
||||
sds sdscpy(sds s, const char *t);
|
||||
|
||||
sds sdscatvprintf(sds s, const char *fmt, va_list ap);
|
||||
#ifdef __GNUC__
|
||||
@@ -80,16 +80,16 @@ sds sdstrim(sds s, const char *cset);
|
||||
sds sdsrange(sds s, int start, int end);
|
||||
void sdsupdatelen(sds s);
|
||||
void sdsclear(sds s);
|
||||
int sdscmp(sds s1, sds s2);
|
||||
sds *sdssplitlen(char *s, int len, char *sep, int seplen, int *count);
|
||||
int sdscmp(const sds s1, const sds s2);
|
||||
sds *sdssplitlen(const char *s, int len, const char *sep, int seplen, int *count);
|
||||
void sdsfreesplitres(sds *tokens, int count);
|
||||
void sdstolower(sds s);
|
||||
void sdstoupper(sds s);
|
||||
sds sdsfromlonglong(long long value);
|
||||
sds sdscatrepr(sds s, char *p, size_t len);
|
||||
sds *sdssplitargs(char *line, int *argc);
|
||||
sds sdscatrepr(sds s, const char *p, size_t len);
|
||||
sds *sdssplitargs(const char *line, int *argc);
|
||||
void sdssplitargs_free(sds *argv, int argc);
|
||||
sds sdsmapchars(sds s, char *from, char *to, size_t setlen);
|
||||
sds sdsmapchars(sds s, const char *from, const char *to, size_t setlen);
|
||||
|
||||
/* Low level functions exposed to the user API */
|
||||
sds sdsMakeRoomFor(sds s, size_t addlen);
|
||||
|
||||
+3058
File diff suppressed because it is too large
Load Diff
+89
-19
@@ -2,6 +2,8 @@
|
||||
#include "pqsort.h" /* Partial qsort for SORT+LIMIT */
|
||||
#include <math.h> /* isnan() */
|
||||
|
||||
zskiplistNode* zslGetElementByRank(zskiplist *zsl, unsigned long rank);
|
||||
|
||||
redisSortOperation *createSortOperation(int type, robj *pattern) {
|
||||
redisSortOperation *so = zmalloc(sizeof(*so));
|
||||
so->type = type;
|
||||
@@ -156,8 +158,9 @@ void sortCommand(redisClient *c) {
|
||||
|
||||
/* Lookup the key to sort. It must be of the right types */
|
||||
sortval = lookupKeyRead(c->db,c->argv[1]);
|
||||
if (sortval && sortval->type != REDIS_SET && sortval->type != REDIS_LIST &&
|
||||
sortval->type != REDIS_ZSET)
|
||||
if (sortval && sortval->type != REDIS_SET &&
|
||||
sortval->type != REDIS_LIST &&
|
||||
sortval->type != REDIS_ZSET)
|
||||
{
|
||||
addReply(c,shared.wrongtypeerr);
|
||||
return;
|
||||
@@ -167,7 +170,7 @@ void sortCommand(redisClient *c) {
|
||||
* Operations can be GET/DEL/INCR/DECR */
|
||||
operations = listCreate();
|
||||
listSetFreeMethod(operations,zfree);
|
||||
j = 2;
|
||||
j = 2; /* options start at argv[2] */
|
||||
|
||||
/* Now we need to protect sortval incrementing its count, in the future
|
||||
* SORT may have options able to overwrite/delete keys during the sorting
|
||||
@@ -213,10 +216,18 @@ void sortCommand(redisClient *c) {
|
||||
j++;
|
||||
}
|
||||
|
||||
/* If we have STORE we need to force sorting for deterministic output
|
||||
* and replication. We use alpha sorting since this is guaranteed to
|
||||
* work with any input. */
|
||||
if (storekey && dontsort) {
|
||||
/* For the STORE option, or when SORT is called from a Lua script,
|
||||
* we want to force a specific ordering even when no explicit ordering
|
||||
* was asked (SORT BY nosort). This guarantees that replication / AOF
|
||||
* is deterministic.
|
||||
*
|
||||
* However in the case 'dontsort' is true, but the type to sort is a
|
||||
* sorted set, we don't need to do anything as ordering is guaranteed
|
||||
* in this special case. */
|
||||
if ((storekey || c->flags & REDIS_LUA_CLIENT) &&
|
||||
(dontsort && sortval->type != REDIS_ZSET))
|
||||
{
|
||||
/* Force ALPHA sorting */
|
||||
dontsort = 0;
|
||||
alpha = 1;
|
||||
sortby = NULL;
|
||||
@@ -226,13 +237,41 @@ void sortCommand(redisClient *c) {
|
||||
if (sortval->type == REDIS_ZSET)
|
||||
zsetConvert(sortval, REDIS_ENCODING_SKIPLIST);
|
||||
|
||||
/* Load the sorting vector with all the objects to sort */
|
||||
/* Objtain the length of the object to sort. */
|
||||
switch(sortval->type) {
|
||||
case REDIS_LIST: vectorlen = listTypeLength(sortval); break;
|
||||
case REDIS_SET: vectorlen = setTypeSize(sortval); break;
|
||||
case REDIS_ZSET: vectorlen = dictSize(((zset*)sortval->ptr)->dict); break;
|
||||
default: vectorlen = 0; redisPanic("Bad SORT type"); /* Avoid GCC warning */
|
||||
}
|
||||
|
||||
/* Perform LIMIT start,count sanity checking. */
|
||||
start = (limit_start < 0) ? 0 : limit_start;
|
||||
end = (limit_count < 0) ? vectorlen-1 : start+limit_count-1;
|
||||
if (start >= vectorlen) {
|
||||
start = vectorlen-1;
|
||||
end = vectorlen-2;
|
||||
}
|
||||
if (end >= vectorlen) end = vectorlen-1;
|
||||
|
||||
/* Optimization:
|
||||
*
|
||||
* 1) if the object to sort is a sorted set.
|
||||
* 2) There is nothing to sort as dontsort is true (BY <constant string>).
|
||||
* 3) We have a LIMIT option that actually reduces the number of elements
|
||||
* to fetch.
|
||||
*
|
||||
* In this case to load all the objects in the vector is a huge waste of
|
||||
* resources. We just allocate a vector that is big enough for the selected
|
||||
* range length, and make sure to load just this part in the vector. */
|
||||
if (sortval->type == REDIS_ZSET &&
|
||||
dontsort &&
|
||||
(start != 0 || end != vectorlen-1))
|
||||
{
|
||||
vectorlen = end-start+1;
|
||||
}
|
||||
|
||||
/* Load the sorting vector with all the objects to sort */
|
||||
vector = zmalloc(sizeof(redisSortObject)*vectorlen);
|
||||
j = 0;
|
||||
|
||||
@@ -256,6 +295,48 @@ void sortCommand(redisClient *c) {
|
||||
j++;
|
||||
}
|
||||
setTypeReleaseIterator(si);
|
||||
} else if (sortval->type == REDIS_ZSET && dontsort) {
|
||||
/* Special handling for a sorted set, if 'dontsort' is true.
|
||||
* This makes sure we return elements in the sorted set original
|
||||
* ordering, accordingly to DESC / ASC options.
|
||||
*
|
||||
* Note that in this case we also handle LIMIT here in a direct
|
||||
* way, just getting the required range, as an optimization. */
|
||||
|
||||
zset *zs = sortval->ptr;
|
||||
zskiplist *zsl = zs->zsl;
|
||||
zskiplistNode *ln;
|
||||
robj *ele;
|
||||
int rangelen = vectorlen;
|
||||
|
||||
/* Check if starting point is trivial, before doing log(N) lookup. */
|
||||
if (desc) {
|
||||
long zsetlen = dictSize(((zset*)sortval->ptr)->dict);
|
||||
|
||||
ln = zsl->tail;
|
||||
if (start > 0)
|
||||
ln = zslGetElementByRank(zsl,zsetlen-start);
|
||||
} else {
|
||||
ln = zsl->header->level[0].forward;
|
||||
if (start > 0)
|
||||
ln = zslGetElementByRank(zsl,start+1);
|
||||
}
|
||||
|
||||
while(rangelen--) {
|
||||
redisAssertWithInfo(c,sortval,ln != NULL);
|
||||
ele = ln->obj;
|
||||
vector[j].obj = ele;
|
||||
vector[j].u.score = 0;
|
||||
vector[j].u.cmpobj = NULL;
|
||||
j++;
|
||||
ln = desc ? ln->backward : ln->level[0].forward;
|
||||
}
|
||||
/* The code producing the output does not know that in the case of
|
||||
* sorted set, 'dontsort', and LIMIT, we are able to get just the
|
||||
* range, already sorted, so we need to adjust "start" and "end"
|
||||
* to make sure start is set to 0. */
|
||||
end -= start;
|
||||
start = 0;
|
||||
} else if (sortval->type == REDIS_ZSET) {
|
||||
dict *set = ((zset*)sortval->ptr)->dict;
|
||||
dictIterator *di;
|
||||
@@ -316,17 +397,6 @@ void sortCommand(redisClient *c) {
|
||||
}
|
||||
}
|
||||
|
||||
/* We are ready to sort the vector... perform a bit of sanity check
|
||||
* on the LIMIT option too. We'll use a partial version of quicksort. */
|
||||
start = (limit_start < 0) ? 0 : limit_start;
|
||||
end = (limit_count < 0) ? vectorlen-1 : start+limit_count-1;
|
||||
if (start >= vectorlen) {
|
||||
start = vectorlen-1;
|
||||
end = vectorlen-2;
|
||||
}
|
||||
if (end >= vectorlen) end = vectorlen-1;
|
||||
|
||||
server.sort_dontsort = dontsort;
|
||||
if (dontsort == 0) {
|
||||
server.sort_desc = desc;
|
||||
server.sort_alpha = alpha;
|
||||
|
||||
+5
-1
@@ -403,7 +403,11 @@ void hashTypeConvertZiplist(robj *o, int enc) {
|
||||
value = hashTypeCurrentObject(hi, REDIS_HASH_VALUE);
|
||||
value = tryObjectEncoding(value);
|
||||
ret = dictAdd(dict, field, value);
|
||||
redisAssert(ret == DICT_OK);
|
||||
if (ret != DICT_OK) {
|
||||
redisLogHexDump(REDIS_WARNING,"ziplist with dup elements dump",
|
||||
o->ptr,ziplistBlobLen(o->ptr));
|
||||
redisAssert(ret == DICT_OK);
|
||||
}
|
||||
}
|
||||
|
||||
hashTypeReleaseIterator(hi);
|
||||
|
||||
+204
-125
@@ -1,5 +1,7 @@
|
||||
#include "redis.h"
|
||||
|
||||
void signalListAsReady(redisClient *c, robj *key);
|
||||
|
||||
/*-----------------------------------------------------------------------------
|
||||
* List API
|
||||
*----------------------------------------------------------------------------*/
|
||||
@@ -14,6 +16,11 @@ void listTypeTryConversion(robj *subject, robj *value) {
|
||||
listTypeConvert(subject,REDIS_ENCODING_LINKEDLIST);
|
||||
}
|
||||
|
||||
/* The function pushes an elmenet to the specified list object 'subject',
|
||||
* at head or tail position as specified by 'where'.
|
||||
*
|
||||
* There is no need for the caller to incremnet the refcount of 'value' as
|
||||
* the function takes care of it if needed. */
|
||||
void listTypePush(robj *subject, robj *value, int where) {
|
||||
/* Check if we need to convert the ziplist */
|
||||
listTypeTryConversion(subject,value);
|
||||
@@ -268,16 +275,10 @@ void pushGenericCommand(redisClient *c, int where) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (may_have_waiting_clients) signalListAsReady(c,c->argv[1]);
|
||||
|
||||
for (j = 2; j < c->argc; j++) {
|
||||
c->argv[j] = tryObjectEncoding(c->argv[j]);
|
||||
if (may_have_waiting_clients) {
|
||||
if (handleClientsWaitingListPush(c,c->argv[1],c->argv[j])) {
|
||||
waiting++;
|
||||
continue;
|
||||
} else {
|
||||
may_have_waiting_clients = 0;
|
||||
}
|
||||
}
|
||||
if (!lobj) {
|
||||
lobj = createZiplistObject();
|
||||
dbAdd(c->db,c->argv[1],lobj);
|
||||
@@ -288,18 +289,6 @@ void pushGenericCommand(redisClient *c, int where) {
|
||||
addReplyLongLong(c, waiting + (lobj ? listTypeLength(lobj) : 0));
|
||||
if (pushed) signalModifiedKey(c->db,c->argv[1]);
|
||||
server.dirty += pushed;
|
||||
|
||||
/* Alter the replication of the command accordingly to the number of
|
||||
* list elements delivered to clients waiting into a blocking operation.
|
||||
* We do that only if there were waiting clients, and only if still some
|
||||
* element was pushed into the list (othewise dirty is 0 and nothign will
|
||||
* be propagated). */
|
||||
if (waiting && pushed) {
|
||||
/* CMD KEY a b C D E */
|
||||
for (j = 0; j < waiting; j++) decrRefCount(c->argv[j+2]);
|
||||
memmove(c->argv+2,c->argv+2+waiting,sizeof(robj*)*pushed);
|
||||
c->argc -= waiting;
|
||||
}
|
||||
}
|
||||
|
||||
void lpushCommand(redisClient *c) {
|
||||
@@ -666,29 +655,15 @@ void lremCommand(redisClient *c) {
|
||||
* as well. This command was originally proposed by Ezra Zygmuntowicz.
|
||||
*/
|
||||
|
||||
void rpoplpushHandlePush(redisClient *origclient, redisClient *c, robj *dstkey, robj *dstobj, robj *value) {
|
||||
if (!handleClientsWaitingListPush(origclient,dstkey,value)) {
|
||||
/* Create the list if the key does not exist */
|
||||
if (!dstobj) {
|
||||
dstobj = createZiplistObject();
|
||||
dbAdd(c->db,dstkey,dstobj);
|
||||
} else {
|
||||
signalModifiedKey(c->db,dstkey);
|
||||
}
|
||||
listTypePush(dstobj,value,REDIS_HEAD);
|
||||
/* Additionally propagate this PUSH operation together with
|
||||
* the operation performed by the command. */
|
||||
{
|
||||
robj **argv = zmalloc(sizeof(robj*)*3);
|
||||
argv[0] = createStringObject("LPUSH",5);
|
||||
argv[1] = dstkey;
|
||||
argv[2] = value;
|
||||
incrRefCount(argv[1]);
|
||||
incrRefCount(argv[2]);
|
||||
alsoPropagate(server.lpushCommand,c->db->id,argv,3,
|
||||
REDIS_PROPAGATE_AOF|REDIS_PROPAGATE_REPL);
|
||||
}
|
||||
void rpoplpushHandlePush(redisClient *c, robj *dstkey, robj *dstobj, robj *value) {
|
||||
/* Create the list if the key does not exist */
|
||||
if (!dstobj) {
|
||||
dstobj = createZiplistObject();
|
||||
dbAdd(c->db,dstkey,dstobj);
|
||||
signalListAsReady(c,dstkey);
|
||||
}
|
||||
signalModifiedKey(c->db,dstkey);
|
||||
listTypePush(dstobj,value,REDIS_HEAD);
|
||||
/* Always send the pushed value to the client. */
|
||||
addReplyBulk(c,value);
|
||||
}
|
||||
@@ -709,9 +684,10 @@ void rpoplpushCommand(redisClient *c) {
|
||||
if (dobj && checkType(c,dobj,REDIS_LIST)) return;
|
||||
value = listTypePop(sobj,REDIS_TAIL);
|
||||
/* We saved touched key, and protect it, since rpoplpushHandlePush
|
||||
* may change the client command argument vector. */
|
||||
* may change the client command argument vector (it does not
|
||||
* currently). */
|
||||
incrRefCount(touchedkey);
|
||||
rpoplpushHandlePush(c,c,c->argv[2],dobj,value);
|
||||
rpoplpushHandlePush(c,c->argv[2],dobj,value);
|
||||
|
||||
/* listTypePop returns an object with its refcount incremented */
|
||||
decrRefCount(value);
|
||||
@@ -721,13 +697,6 @@ void rpoplpushCommand(redisClient *c) {
|
||||
signalModifiedKey(c->db,touchedkey);
|
||||
decrRefCount(touchedkey);
|
||||
server.dirty++;
|
||||
|
||||
/* Replicate this as a simple RPOP since the LPUSH side is replicated
|
||||
* by rpoplpushHandlePush() call if needed (it may not be needed
|
||||
* if a client is blocking wait a push against the list). */
|
||||
rewriteClientCommandVector(c,2,
|
||||
resetRefCount(createStringObject("RPOP",4)),
|
||||
c->argv[1]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -735,20 +704,10 @@ void rpoplpushCommand(redisClient *c) {
|
||||
* Blocking POP operations
|
||||
*----------------------------------------------------------------------------*/
|
||||
|
||||
/* Currently Redis blocking operations support is limited to list POP ops,
|
||||
* so the current implementation is not fully generic, but it is also not
|
||||
* completely specific so it will not require a rewrite to support new
|
||||
* kind of blocking operations in the future.
|
||||
*
|
||||
* Still it's important to note that list blocking operations can be already
|
||||
* used as a notification mechanism in order to implement other blocking
|
||||
* operations at application level, so there must be a very strong evidence
|
||||
* of usefulness and generality before new blocking operations are implemented.
|
||||
*
|
||||
* This is how the current blocking POP works, we use BLPOP as example:
|
||||
/* This is how the current blocking POP works, we use BLPOP as example:
|
||||
* - If the user calls BLPOP and the key exists and contains a non empty list
|
||||
* then LPOP is called instead. So BLPOP is semantically the same as LPOP
|
||||
* if there is not to block.
|
||||
* if blocking is not required.
|
||||
* - If instead BLPOP is called and the key does not exists or the list is
|
||||
* empty we need to block. In order to do so we remove the notification for
|
||||
* new data to read in the client socket (so that we'll not serve new
|
||||
@@ -756,12 +715,10 @@ void rpoplpushCommand(redisClient *c) {
|
||||
* in a dictionary (db->blocking_keys) mapping keys to a list of clients
|
||||
* blocking for this keys.
|
||||
* - If a PUSH operation against a key with blocked clients waiting is
|
||||
* performed, we serve the first in the list: basically instead to push
|
||||
* the new element inside the list we return it to the (first / oldest)
|
||||
* blocking client, unblock the client, and remove it form the list.
|
||||
*
|
||||
* The above comment and the source code should be enough in order to understand
|
||||
* the implementation and modify / fix it later.
|
||||
* performed, we mark this key as "ready", and after the current command,
|
||||
* MULTI/EXEC block, or script, is executed, we serve all the clients waiting
|
||||
* for this list, from the one that blocked first, to the last, accordingly
|
||||
* to the number of elements we have in the ready list.
|
||||
*/
|
||||
|
||||
/* Set a client in blocking mode for the specified key, with the specified
|
||||
@@ -836,68 +793,192 @@ void unblockClientWaitingData(redisClient *c) {
|
||||
listAddNodeTail(server.unblocked_clients,c);
|
||||
}
|
||||
|
||||
/* This should be called from any function PUSHing into lists.
|
||||
* 'c' is the "pushing client", 'key' is the key it is pushing data against,
|
||||
* 'ele' is the element pushed.
|
||||
/* If the specified key has clients blocked waiting for list pushes, this
|
||||
* function will put the key reference into the server.ready_keys list.
|
||||
* Note that db->ready_keys is an hash table that allows us to avoid putting
|
||||
* the same key agains and again in the list in case of multiple pushes
|
||||
* made by a script or in the context of MULTI/EXEC.
|
||||
*
|
||||
* If the function returns 0 there was no client waiting for a list push
|
||||
* against this key.
|
||||
* The list will be finally processed by handleClientsBlockedOnLists() */
|
||||
void signalListAsReady(redisClient *c, robj *key) {
|
||||
readyList *rl;
|
||||
|
||||
/* No clients blocking for this key? No need to queue it. */
|
||||
if (dictFind(c->db->blocking_keys,key) == NULL) return;
|
||||
|
||||
/* Key was already signaled? No need to queue it again. */
|
||||
if (dictFind(c->db->ready_keys,key) != NULL) return;
|
||||
|
||||
/* Ok, we need to queue this key into server.ready_keys. */
|
||||
rl = zmalloc(sizeof(*rl));
|
||||
rl->key = key;
|
||||
rl->db = c->db;
|
||||
incrRefCount(key);
|
||||
listAddNodeTail(server.ready_keys,rl);
|
||||
|
||||
/* We also add the key in the db->ready_keys dictionary in order
|
||||
* to avoid adding it multiple times into a list with a simple O(1)
|
||||
* check. */
|
||||
incrRefCount(key);
|
||||
redisAssert(dictAdd(c->db->ready_keys,key,NULL) == DICT_OK);
|
||||
}
|
||||
|
||||
/* This is an helper function for handleClientsBlockedOnLists(). It's work
|
||||
* is to serve a specific client (receiver) that is blocked on 'key'
|
||||
* in the context of the specified 'db', doing the following:
|
||||
*
|
||||
* If the function returns 1 there was a client waiting for a list push
|
||||
* against this key, the element was passed to this client thus it's not
|
||||
* needed to actually add it to the list and the caller should return asap. */
|
||||
int handleClientsWaitingListPush(redisClient *c, robj *key, robj *ele) {
|
||||
struct dictEntry *de;
|
||||
redisClient *receiver;
|
||||
int numclients;
|
||||
list *clients;
|
||||
listNode *ln;
|
||||
robj *dstkey, *dstobj;
|
||||
* 1) Provide the client with the 'value' element.
|
||||
* 2) If the dstkey is not NULL (we are serving a BRPOPLPUSH) also push the
|
||||
* 'value' element on the destionation list (the LPUSH side of the command).
|
||||
* 3) Propagate the resulting BRPOP, BLPOP and additional LPUSH if any into
|
||||
* the AOF and replication channel.
|
||||
*
|
||||
* The argument 'where' is REDIS_TAIL or REDIS_HEAD, and indicates if the
|
||||
* 'value' element was popped fron the head (BLPOP) or tail (BRPOP) so that
|
||||
* we can propagate the command properly.
|
||||
*
|
||||
* The function returns REDIS_OK if we are able to serve the client, otherwise
|
||||
* REDIS_ERR is returned to signal the caller that the list POP operation
|
||||
* should be undoed as the client was not served: This only happens for
|
||||
* BRPOPLPUSH that fails to push the value to the destination key as it is
|
||||
* of the wrong type. */
|
||||
int serveClientBlockedOnList(redisClient *receiver, robj *key, robj *dstkey, redisDb *db, robj *value, int where)
|
||||
{
|
||||
robj *argv[3];
|
||||
|
||||
de = dictFind(c->db->blocking_keys,key);
|
||||
if (de == NULL) return 0;
|
||||
clients = dictGetVal(de);
|
||||
numclients = listLength(clients);
|
||||
if (dstkey == NULL) {
|
||||
/* Propagate the [LR]POP operation. */
|
||||
argv[0] = (where == REDIS_HEAD) ? shared.lpop :
|
||||
shared.rpop;
|
||||
argv[1] = key;
|
||||
propagate((where == REDIS_HEAD) ?
|
||||
server.lpopCommand : server.rpopCommand,
|
||||
db->id,argv,2,REDIS_PROPAGATE_AOF|REDIS_PROPAGATE_REPL);
|
||||
|
||||
/* Try to handle the push as long as there are clients waiting for a push.
|
||||
* Note that "numclients" is used because the list of clients waiting for a
|
||||
* push on "key" is deleted by unblockClient() when empty.
|
||||
*
|
||||
* This loop will have more than 1 iteration when there is a BRPOPLPUSH
|
||||
* that cannot push the target list because it does not contain a list. If
|
||||
* this happens, it simply tries the next client waiting for a push. */
|
||||
while (numclients--) {
|
||||
ln = listFirst(clients);
|
||||
redisAssertWithInfo(c,key,ln != NULL);
|
||||
receiver = ln->value;
|
||||
dstkey = receiver->bpop.target;
|
||||
|
||||
/* Protect receiver->bpop.target, that will be freed by
|
||||
* the next unblockClientWaitingData() call. */
|
||||
if (dstkey) incrRefCount(dstkey);
|
||||
|
||||
/* This should remove the first element of the "clients" list. */
|
||||
unblockClientWaitingData(receiver);
|
||||
|
||||
if (dstkey == NULL) {
|
||||
/* BRPOP/BLPOP */
|
||||
addReplyMultiBulkLen(receiver,2);
|
||||
addReplyBulk(receiver,key);
|
||||
addReplyBulk(receiver,ele);
|
||||
return 1; /* Serve just the first client as in B[RL]POP semantics */
|
||||
/* BRPOP/BLPOP */
|
||||
addReplyMultiBulkLen(receiver,2);
|
||||
addReplyBulk(receiver,key);
|
||||
addReplyBulk(receiver,value);
|
||||
} else {
|
||||
/* BRPOPLPUSH */
|
||||
robj *dstobj =
|
||||
lookupKeyWrite(receiver->db,dstkey);
|
||||
if (!(dstobj &&
|
||||
checkType(receiver,dstobj,REDIS_LIST)))
|
||||
{
|
||||
/* Propagate the RPOP operation. */
|
||||
argv[0] = shared.rpop;
|
||||
argv[1] = key;
|
||||
propagate(server.rpopCommand,
|
||||
db->id,argv,2,
|
||||
REDIS_PROPAGATE_AOF|
|
||||
REDIS_PROPAGATE_REPL);
|
||||
rpoplpushHandlePush(receiver,dstkey,dstobj,
|
||||
value);
|
||||
/* Propagate the LPUSH operation. */
|
||||
argv[0] = shared.lpush;
|
||||
argv[1] = dstkey;
|
||||
argv[2] = value;
|
||||
propagate(server.lpushCommand,
|
||||
db->id,argv,3,
|
||||
REDIS_PROPAGATE_AOF|
|
||||
REDIS_PROPAGATE_REPL);
|
||||
} else {
|
||||
/* BRPOPLPUSH, note that receiver->db is always equal to c->db. */
|
||||
dstobj = lookupKeyWrite(receiver->db,dstkey);
|
||||
if (!(dstobj && checkType(receiver,dstobj,REDIS_LIST))) {
|
||||
rpoplpushHandlePush(c,receiver,dstkey,dstobj,ele);
|
||||
decrRefCount(dstkey);
|
||||
return 1;
|
||||
}
|
||||
decrRefCount(dstkey);
|
||||
/* BRPOPLPUSH failed because of wrong
|
||||
* destination type. */
|
||||
return REDIS_ERR;
|
||||
}
|
||||
}
|
||||
return REDIS_OK;
|
||||
}
|
||||
|
||||
return 0;
|
||||
/* This function should be called by Redis every time a single command,
|
||||
* a MULTI/EXEC block, or a Lua script, terminated its execution after
|
||||
* being called by a client.
|
||||
*
|
||||
* All the keys with at least one client blocked that received at least
|
||||
* one new element via some PUSH operation are accumulated into
|
||||
* the server.ready_keys list. This function will run the list and will
|
||||
* serve clients accordingly. Note that the function will iterate again and
|
||||
* again as a result of serving BRPOPLPUSH we can have new blocking clients
|
||||
* to serve because of the PUSH side of BRPOPLPUSH. */
|
||||
void handleClientsBlockedOnLists(void) {
|
||||
while(listLength(server.ready_keys) != 0) {
|
||||
list *l;
|
||||
|
||||
/* Point server.ready_keys to a fresh list and save the current one
|
||||
* locally. This way as we run the old list we are free to call
|
||||
* signalListAsReady() that may push new elements in server.ready_keys
|
||||
* when handling clients blocked into BRPOPLPUSH. */
|
||||
l = server.ready_keys;
|
||||
server.ready_keys = listCreate();
|
||||
|
||||
while(listLength(l) != 0) {
|
||||
listNode *ln = listFirst(l);
|
||||
readyList *rl = ln->value;
|
||||
|
||||
/* First of all remove this key from db->ready_keys so that
|
||||
* we can safely call signalListAsReady() against this key. */
|
||||
dictDelete(rl->db->ready_keys,rl->key);
|
||||
|
||||
/* If the key exists and it's a list, serve blocked clients
|
||||
* with data. */
|
||||
robj *o = lookupKeyWrite(rl->db,rl->key);
|
||||
if (o != NULL && o->type == REDIS_LIST) {
|
||||
dictEntry *de;
|
||||
|
||||
/* We serve clients in the same order they blocked for
|
||||
* this key, from the first blocked to the last. */
|
||||
de = dictFind(rl->db->blocking_keys,rl->key);
|
||||
if (de) {
|
||||
list *clients = dictGetVal(de);
|
||||
int numclients = listLength(clients);
|
||||
|
||||
while(numclients--) {
|
||||
listNode *clientnode = listFirst(clients);
|
||||
redisClient *receiver = clientnode->value;
|
||||
robj *dstkey = receiver->bpop.target;
|
||||
int where = (receiver->lastcmd &&
|
||||
receiver->lastcmd->proc == blpopCommand) ?
|
||||
REDIS_HEAD : REDIS_TAIL;
|
||||
robj *value = listTypePop(o,where);
|
||||
|
||||
if (value) {
|
||||
/* Protect receiver->bpop.target, that will be
|
||||
* freed by the next unblockClientWaitingData()
|
||||
* call. */
|
||||
if (dstkey) incrRefCount(dstkey);
|
||||
unblockClientWaitingData(receiver);
|
||||
|
||||
if (serveClientBlockedOnList(receiver,
|
||||
rl->key,dstkey,rl->db,value,
|
||||
where) == REDIS_ERR)
|
||||
{
|
||||
/* If we failed serving the client we need
|
||||
* to also undo the POP operation. */
|
||||
listTypePush(o,value,where);
|
||||
}
|
||||
|
||||
if (dstkey) decrRefCount(dstkey);
|
||||
decrRefCount(value);
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (listTypeLength(o) == 0) dbDelete(rl->db,rl->key);
|
||||
/* We don't call signalModifiedKey() as it was already called
|
||||
* when an element was pushed on the list. */
|
||||
}
|
||||
|
||||
/* Free this item. */
|
||||
decrRefCount(rl->key);
|
||||
zfree(rl);
|
||||
listDelNode(l,ln);
|
||||
}
|
||||
listRelease(l); /* We have the new list on place at this point. */
|
||||
}
|
||||
}
|
||||
|
||||
int getTimeoutFromObjectOrReply(redisClient *c, robj *object, time_t *timeout) {
|
||||
@@ -986,7 +1067,6 @@ void brpoplpushCommand(redisClient *c) {
|
||||
|
||||
if (key == NULL) {
|
||||
if (c->flags & REDIS_MULTI) {
|
||||
|
||||
/* Blocking against an empty list in a multi state
|
||||
* returns immediately. */
|
||||
addReply(c, shared.nullbulk);
|
||||
@@ -998,7 +1078,6 @@ void brpoplpushCommand(redisClient *c) {
|
||||
if (key->type != REDIS_LIST) {
|
||||
addReply(c, shared.wrongtypeerr);
|
||||
} else {
|
||||
|
||||
/* The list exists and has elements, so
|
||||
* the regular rpoplpushCommand is executed. */
|
||||
redisAssertWithInfo(c,key,listTypeLength(key) > 0);
|
||||
|
||||
+157
-1
@@ -4,6 +4,8 @@
|
||||
* Set Commands
|
||||
*----------------------------------------------------------------------------*/
|
||||
|
||||
void sunionDiffGenericCommand(redisClient *c, robj **setkeys, int setnum, robj *dstkey, int op);
|
||||
|
||||
/* Factory method to return a set that *can* hold "value". When the object has
|
||||
* an integer-encodable value, an intset will be returned. Otherwise a regular
|
||||
* hash table. */
|
||||
@@ -185,7 +187,7 @@ unsigned long setTypeSize(robj *subject) {
|
||||
}
|
||||
|
||||
/* Convert the set to specified encoding. The resulting dict (when converting
|
||||
* to a hashtable) is presized to hold the number of elements in the original
|
||||
* to a hash table) is presized to hold the number of elements in the original
|
||||
* set. */
|
||||
void setTypeConvert(robj *setobj, int enc) {
|
||||
setTypeIterator *si;
|
||||
@@ -360,11 +362,165 @@ void spopCommand(redisClient *c) {
|
||||
server.dirty++;
|
||||
}
|
||||
|
||||
/* handle the "SRANDMEMBER key <count>" variant. The normal version of the
|
||||
* command is handled by the srandmemberCommand() function itself. */
|
||||
|
||||
/* How many times bigger should be the set compared to the requested size
|
||||
* for us to don't use the "remove elements" strategy? Read later in the
|
||||
* implementation for more info. */
|
||||
#define SRANDMEMBER_SUB_STRATEGY_MUL 3
|
||||
|
||||
void srandmemberWithCountCommand(redisClient *c) {
|
||||
long l;
|
||||
unsigned long count, size;
|
||||
int uniq = 1;
|
||||
robj *set, *ele;
|
||||
int64_t llele;
|
||||
int encoding;
|
||||
|
||||
dict *d;
|
||||
|
||||
if (getLongFromObjectOrReply(c,c->argv[2],&l,NULL) != REDIS_OK) return;
|
||||
if (l >= 0) {
|
||||
count = (unsigned) l;
|
||||
} else {
|
||||
/* A negative count means: return the same elements multiple times
|
||||
* (i.e. don't remove the extracted element after every extraction). */
|
||||
count = -l;
|
||||
uniq = 0;
|
||||
}
|
||||
|
||||
if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.emptymultibulk))
|
||||
== NULL || checkType(c,set,REDIS_SET)) return;
|
||||
size = setTypeSize(set);
|
||||
|
||||
/* If count is zero, serve it ASAP to avoid special cases later. */
|
||||
if (count == 0) {
|
||||
addReply(c,shared.emptymultibulk);
|
||||
return;
|
||||
}
|
||||
|
||||
/* CASE 1: The count was negative, so the extraction method is just:
|
||||
* "return N random elements" sampling the whole set every time.
|
||||
* This case is trivial and can be served without auxiliary data
|
||||
* structures. */
|
||||
if (!uniq) {
|
||||
addReplyMultiBulkLen(c,count);
|
||||
while(count--) {
|
||||
encoding = setTypeRandomElement(set,&ele,&llele);
|
||||
if (encoding == REDIS_ENCODING_INTSET) {
|
||||
addReplyBulkLongLong(c,llele);
|
||||
} else {
|
||||
addReplyBulk(c,ele);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
/* CASE 2:
|
||||
* The number of requested elements is greater than the number of
|
||||
* elements inside the set: simply return the whole set. */
|
||||
if (count >= size) {
|
||||
sunionDiffGenericCommand(c,c->argv,c->argc-1,NULL,REDIS_OP_UNION);
|
||||
return;
|
||||
}
|
||||
|
||||
/* For CASE 3 and CASE 4 we need an auxiliary dictionary. */
|
||||
d = dictCreate(&setDictType,NULL);
|
||||
|
||||
/* CASE 3:
|
||||
* The number of elements inside the set is not greater than
|
||||
* SRANDMEMBER_SUB_STRATEGY_MUL times the number of requested elements.
|
||||
* In this case we create a set from scratch with all the elements, and
|
||||
* subtract random elements to reach the requested number of elements.
|
||||
*
|
||||
* This is done because if the number of requsted elements is just
|
||||
* a bit less than the number of elements in the set, the natural approach
|
||||
* used into CASE 3 is highly inefficient. */
|
||||
if (count*SRANDMEMBER_SUB_STRATEGY_MUL > size) {
|
||||
setTypeIterator *si;
|
||||
|
||||
/* Add all the elements into the temporary dictionary. */
|
||||
si = setTypeInitIterator(set);
|
||||
while((encoding = setTypeNext(si,&ele,&llele)) != -1) {
|
||||
int retval;
|
||||
|
||||
if (encoding == REDIS_ENCODING_INTSET) {
|
||||
retval = dictAdd(d,createStringObjectFromLongLong(llele),NULL);
|
||||
} else if (ele->encoding == REDIS_ENCODING_RAW) {
|
||||
retval = dictAdd(d,dupStringObject(ele),NULL);
|
||||
} else if (ele->encoding == REDIS_ENCODING_INT) {
|
||||
retval = dictAdd(d,
|
||||
createStringObjectFromLongLong((long)ele->ptr),NULL);
|
||||
}
|
||||
redisAssert(retval == DICT_OK);
|
||||
}
|
||||
setTypeReleaseIterator(si);
|
||||
redisAssert(dictSize(d) == size);
|
||||
|
||||
/* Remove random elements to reach the right count. */
|
||||
while(size > count) {
|
||||
dictEntry *de;
|
||||
|
||||
de = dictGetRandomKey(d);
|
||||
dictDelete(d,dictGetKey(de));
|
||||
size--;
|
||||
}
|
||||
}
|
||||
|
||||
/* CASE 4: We have a big set compared to the requested number of elements.
|
||||
* In this case we can simply get random elements from the set and add
|
||||
* to the temporary set, trying to eventually get enough unique elements
|
||||
* to reach the specified count. */
|
||||
else {
|
||||
unsigned long added = 0;
|
||||
|
||||
while(added < count) {
|
||||
encoding = setTypeRandomElement(set,&ele,&llele);
|
||||
if (encoding == REDIS_ENCODING_INTSET) {
|
||||
ele = createStringObjectFromLongLong(llele);
|
||||
} else if (ele->encoding == REDIS_ENCODING_RAW) {
|
||||
ele = dupStringObject(ele);
|
||||
} else if (ele->encoding == REDIS_ENCODING_INT) {
|
||||
ele = createStringObjectFromLongLong((long)ele->ptr);
|
||||
}
|
||||
/* Try to add the object to the dictionary. If it already exists
|
||||
* free it, otherwise increment the number of objects we have
|
||||
* in the result dictionary. */
|
||||
if (dictAdd(d,ele,NULL) == DICT_OK)
|
||||
added++;
|
||||
else
|
||||
decrRefCount(ele);
|
||||
}
|
||||
}
|
||||
|
||||
/* CASE 3 & 4: send the result to the user. */
|
||||
{
|
||||
dictIterator *di;
|
||||
dictEntry *de;
|
||||
|
||||
addReplyMultiBulkLen(c,count);
|
||||
di = dictGetIterator(d);
|
||||
while((de = dictNext(di)) != NULL)
|
||||
addReplyBulk(c,dictGetKey(de));
|
||||
dictReleaseIterator(di);
|
||||
dictRelease(d);
|
||||
}
|
||||
}
|
||||
|
||||
void srandmemberCommand(redisClient *c) {
|
||||
robj *set, *ele;
|
||||
int64_t llele;
|
||||
int encoding;
|
||||
|
||||
if (c->argc == 3) {
|
||||
srandmemberWithCountCommand(c);
|
||||
return;
|
||||
} else if (c->argc > 3) {
|
||||
addReply(c,shared.syntaxerr);
|
||||
return;
|
||||
}
|
||||
|
||||
if ((set = lookupKeyReadOrReply(c,c->argv[1],shared.nullbulk)) == NULL ||
|
||||
checkType(c,set,REDIS_SET)) return;
|
||||
|
||||
|
||||
+2
-1
@@ -7,6 +7,7 @@
|
||||
#include <math.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
#include <float.h>
|
||||
|
||||
#include "util.h"
|
||||
|
||||
@@ -319,7 +320,7 @@ int d2string(char *buf, size_t len, double value) {
|
||||
* integer printing function that is much faster. */
|
||||
double min = -4503599627370495; /* (2^52)-1 */
|
||||
double max = 4503599627370496; /* -(2^52) */
|
||||
if (val > min && val < max && value == ((double)((long long)value)))
|
||||
if (value > min && value < max && value == ((double)((long long)value)))
|
||||
len = ll2string(buf,len,(long long)value);
|
||||
else
|
||||
#endif
|
||||
|
||||
+1
-1
@@ -1 +1 @@
|
||||
#define REDIS_VERSION "2.5.10"
|
||||
#define REDIS_VERSION "2.6.0"
|
||||
|
||||
+75
-16
@@ -317,7 +317,7 @@ static void zipSaveInteger(unsigned char *p, int64_t value, unsigned char encodi
|
||||
int32_t i32;
|
||||
int64_t i64;
|
||||
if (encoding == ZIP_INT_8B) {
|
||||
((char*)p)[0] = (char)value;
|
||||
((int8_t*)p)[0] = (int8_t)value;
|
||||
} else if (encoding == ZIP_INT_16B) {
|
||||
i16 = value;
|
||||
memcpy(p,&i16,sizeof(i16));
|
||||
@@ -325,7 +325,7 @@ static void zipSaveInteger(unsigned char *p, int64_t value, unsigned char encodi
|
||||
} else if (encoding == ZIP_INT_24B) {
|
||||
i32 = value<<8;
|
||||
memrev32ifbe(&i32);
|
||||
memcpy(p,((unsigned char*)&i32)+1,sizeof(i32)-sizeof(int8_t));
|
||||
memcpy(p,((uint8_t*)&i32)+1,sizeof(i32)-sizeof(uint8_t));
|
||||
} else if (encoding == ZIP_INT_32B) {
|
||||
i32 = value;
|
||||
memcpy(p,&i32,sizeof(i32));
|
||||
@@ -347,7 +347,7 @@ static int64_t zipLoadInteger(unsigned char *p, unsigned char encoding) {
|
||||
int32_t i32;
|
||||
int64_t i64, ret = 0;
|
||||
if (encoding == ZIP_INT_8B) {
|
||||
ret = ((char*)p)[0];
|
||||
ret = ((int8_t*)p)[0];
|
||||
} else if (encoding == ZIP_INT_16B) {
|
||||
memcpy(&i16,p,sizeof(i16));
|
||||
memrev16ifbe(&i16);
|
||||
@@ -358,7 +358,7 @@ static int64_t zipLoadInteger(unsigned char *p, unsigned char encoding) {
|
||||
ret = i32;
|
||||
} else if (encoding == ZIP_INT_24B) {
|
||||
i32 = 0;
|
||||
memcpy(((unsigned char*)&i32)+1,p,sizeof(i32)-sizeof(int8_t));
|
||||
memcpy(((uint8_t*)&i32)+1,p,sizeof(i32)-sizeof(uint8_t));
|
||||
memrev32ifbe(&i32);
|
||||
ret = i32>>8;
|
||||
} else if (encoding == ZIP_INT_64B) {
|
||||
@@ -500,12 +500,13 @@ static unsigned char *__ziplistDelete(unsigned char *zl, unsigned char *p, unsig
|
||||
totlen = p-first.p;
|
||||
if (totlen > 0) {
|
||||
if (p[0] != ZIP_END) {
|
||||
/* Tricky: storing the prevlen in this entry might reduce or
|
||||
* increase the number of bytes needed, compared to the current
|
||||
* prevlen. Note that we can always store this length because
|
||||
* it was previously stored by an entry that is being deleted. */
|
||||
/* Storing `prevrawlen` in this entry may increase or decrease the
|
||||
* number of bytes required compare to the current `prevrawlen`.
|
||||
* There always is room to store this, because it was previously
|
||||
* stored by an entry that is now being deleted. */
|
||||
nextdiff = zipPrevLenByteDiff(p,first.prevrawlen);
|
||||
zipPrevEncodeLength(p-nextdiff,first.prevrawlen);
|
||||
p -= nextdiff;
|
||||
zipPrevEncodeLength(p,first.prevrawlen);
|
||||
|
||||
/* Update offset for tail */
|
||||
ZIPLIST_TAIL_OFFSET(zl) =
|
||||
@@ -521,8 +522,8 @@ static unsigned char *__ziplistDelete(unsigned char *zl, unsigned char *p, unsig
|
||||
}
|
||||
|
||||
/* Move tail to the front of the ziplist */
|
||||
memmove(first.p,p-nextdiff,
|
||||
intrev32ifbe(ZIPLIST_BYTES(zl))-(p-zl)-1+nextdiff);
|
||||
memmove(first.p,p,
|
||||
intrev32ifbe(ZIPLIST_BYTES(zl))-(p-zl)-1);
|
||||
} else {
|
||||
/* The entire tail was deleted. No need to move memory. */
|
||||
ZIPLIST_TAIL_OFFSET(zl) =
|
||||
@@ -805,19 +806,24 @@ unsigned char *ziplistFind(unsigned char *p, unsigned char *vstr, unsigned int v
|
||||
return p;
|
||||
}
|
||||
} else {
|
||||
/* Find out if the specified entry can be encoded */
|
||||
/* Find out if the searched field can be encoded. Note that
|
||||
* we do it only the first time, once done vencoding is set
|
||||
* to non-zero and vll is set to the integer value. */
|
||||
if (vencoding == 0) {
|
||||
/* UINT_MAX when the entry CANNOT be encoded */
|
||||
if (!zipTryEncoding(vstr, vlen, &vll, &vencoding)) {
|
||||
/* If the entry can't be encoded we set it to
|
||||
* UCHAR_MAX so that we don't retry again the next
|
||||
* time. */
|
||||
vencoding = UCHAR_MAX;
|
||||
}
|
||||
|
||||
/* Must be non-zero by now */
|
||||
assert(vencoding);
|
||||
}
|
||||
|
||||
/* Compare current entry with specified entry */
|
||||
if (encoding == vencoding) {
|
||||
/* Compare current entry with specified entry, do it only
|
||||
* if vencoding != UCHAR_MAX because if there is no encoding
|
||||
* possible for the field it can't be a valid integer. */
|
||||
if (vencoding != UCHAR_MAX) {
|
||||
long long ll = zipLoadInteger(q, encoding);
|
||||
if (ll == vll) {
|
||||
return p;
|
||||
@@ -1029,6 +1035,22 @@ int randstring(char *target, unsigned int min, unsigned int max) {
|
||||
return len;
|
||||
}
|
||||
|
||||
void verify(unsigned char *zl, zlentry *e) {
|
||||
int i;
|
||||
int len = ziplistLen(zl);
|
||||
zlentry _e;
|
||||
|
||||
for (i = 0; i < len; i++) {
|
||||
memset(&e[i], 0, sizeof(zlentry));
|
||||
e[i] = zipEntry(ziplistIndex(zl, i));
|
||||
|
||||
memset(&_e, 0, sizeof(zlentry));
|
||||
_e = zipEntry(ziplistIndex(zl, -len+i));
|
||||
|
||||
assert(memcmp(&e[i], &_e, sizeof(zlentry)) == 0);
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
unsigned char *zl, *p;
|
||||
unsigned char *entry;
|
||||
@@ -1310,6 +1332,43 @@ int main(int argc, char **argv) {
|
||||
printf("SUCCESS\n\n");
|
||||
}
|
||||
|
||||
printf("Regression test deleting next to last entries:\n");
|
||||
{
|
||||
char v[3][257];
|
||||
zlentry e[3];
|
||||
int i;
|
||||
|
||||
for (i = 0; i < (sizeof(v)/sizeof(v[0])); i++) {
|
||||
memset(v[i], 'a' + i, sizeof(v[0]));
|
||||
}
|
||||
|
||||
v[0][256] = '\0';
|
||||
v[1][ 1] = '\0';
|
||||
v[2][256] = '\0';
|
||||
|
||||
zl = ziplistNew();
|
||||
for (i = 0; i < (sizeof(v)/sizeof(v[0])); i++) {
|
||||
zl = ziplistPush(zl, (unsigned char *) v[i], strlen(v[i]), ZIPLIST_TAIL);
|
||||
}
|
||||
|
||||
verify(zl, e);
|
||||
|
||||
assert(e[0].prevrawlensize == 1);
|
||||
assert(e[1].prevrawlensize == 5);
|
||||
assert(e[2].prevrawlensize == 1);
|
||||
|
||||
/* Deleting entry 1 will increase `prevrawlensize` for entry 2 */
|
||||
unsigned char *p = e[1].p;
|
||||
zl = ziplistDelete(zl, &p);
|
||||
|
||||
verify(zl, e);
|
||||
|
||||
assert(e[0].prevrawlensize == 1);
|
||||
assert(e[1].prevrawlensize == 5);
|
||||
|
||||
printf("SUCCESS\n\n");
|
||||
}
|
||||
|
||||
printf("Create long list and check indices:\n");
|
||||
{
|
||||
zl = ziplistNew();
|
||||
|
||||
+6
-6
@@ -52,15 +52,15 @@
|
||||
* <len> lengths are encoded in a single value or in a 5 bytes value.
|
||||
* If the first byte value (as an unsigned 8 bit value) is between 0 and
|
||||
* 252, it's a single-byte length. If it is 253 then a four bytes unsigned
|
||||
* integer follows (in the host byte ordering). A value fo 255 is used to
|
||||
* integer follows (in the host byte ordering). A value of 255 is used to
|
||||
* signal the end of the hash. The special value 254 is used to mark
|
||||
* empty space that can be used to add new key/value pairs.
|
||||
*
|
||||
* <free> is the number of free unused bytes
|
||||
* after the string, resulting from modification of values associated to a
|
||||
* key (for instance if "foo" is set to "bar', and later "foo" will be se to
|
||||
* "hi", I'll have a free byte to use if the value will enlarge again later,
|
||||
* or even in order to add a key/value pair if it fits.
|
||||
* <free> is the number of free unused bytes after the string, resulting
|
||||
* from modification of values associated to a key. For instance if "foo"
|
||||
* is set to "bar", and later "foo" will be set to "hi", it will have a
|
||||
* free byte to use if the value will enlarge again later, or even in
|
||||
* order to add a key/value pair if it fits.
|
||||
*
|
||||
* <free> is always an unsigned 8 bit number, because if after an
|
||||
* update operation there are more than a few free bytes, the zipmap will be
|
||||
|
||||
+11
-5
@@ -109,17 +109,19 @@ static size_t used_memory = 0;
|
||||
static int zmalloc_thread_safe = 0;
|
||||
pthread_mutex_t used_memory_mutex = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
static void zmalloc_oom(size_t size) {
|
||||
static void zmalloc_default_oom(size_t size) {
|
||||
fprintf(stderr, "zmalloc: Out of memory trying to allocate %zu bytes\n",
|
||||
size);
|
||||
fflush(stderr);
|
||||
abort();
|
||||
}
|
||||
|
||||
static void (*zmalloc_oom_handler)(size_t) = zmalloc_default_oom;
|
||||
|
||||
void *zmalloc(size_t size) {
|
||||
void *ptr = malloc(size+PREFIX_SIZE);
|
||||
|
||||
if (!ptr) zmalloc_oom(size);
|
||||
if (!ptr) zmalloc_oom_handler(size);
|
||||
#ifdef HAVE_MALLOC_SIZE
|
||||
update_zmalloc_stat_alloc(zmalloc_size(ptr),size);
|
||||
return ptr;
|
||||
@@ -133,7 +135,7 @@ void *zmalloc(size_t size) {
|
||||
void *zcalloc(size_t size) {
|
||||
void *ptr = calloc(1, size+PREFIX_SIZE);
|
||||
|
||||
if (!ptr) zmalloc_oom(size);
|
||||
if (!ptr) zmalloc_oom_handler(size);
|
||||
#ifdef HAVE_MALLOC_SIZE
|
||||
update_zmalloc_stat_alloc(zmalloc_size(ptr),size);
|
||||
return ptr;
|
||||
@@ -155,7 +157,7 @@ void *zrealloc(void *ptr, size_t size) {
|
||||
#ifdef HAVE_MALLOC_SIZE
|
||||
oldsize = zmalloc_size(ptr);
|
||||
newptr = realloc(ptr,size);
|
||||
if (!newptr) zmalloc_oom(size);
|
||||
if (!newptr) zmalloc_oom_handler(size);
|
||||
|
||||
update_zmalloc_stat_free(oldsize);
|
||||
update_zmalloc_stat_alloc(zmalloc_size(newptr),size);
|
||||
@@ -164,7 +166,7 @@ void *zrealloc(void *ptr, size_t size) {
|
||||
realptr = (char*)ptr-PREFIX_SIZE;
|
||||
oldsize = *((size_t*)realptr);
|
||||
newptr = realloc(realptr,size+PREFIX_SIZE);
|
||||
if (!newptr) zmalloc_oom(size);
|
||||
if (!newptr) zmalloc_oom_handler(size);
|
||||
|
||||
*((size_t*)newptr) = size;
|
||||
update_zmalloc_stat_free(oldsize);
|
||||
@@ -236,6 +238,10 @@ void zmalloc_enable_thread_safeness(void) {
|
||||
zmalloc_thread_safe = 1;
|
||||
}
|
||||
|
||||
void zmalloc_set_oom_handler(void (*oom_handler)(size_t)) {
|
||||
zmalloc_oom_handler = oom_handler;
|
||||
}
|
||||
|
||||
/* Get the RSS information in an OS-specific way.
|
||||
*
|
||||
* WARNING: the function zmalloc_get_rss() is not designed to be fast
|
||||
|
||||
@@ -72,6 +72,7 @@ void zfree(void *ptr);
|
||||
char *zstrdup(const char *s);
|
||||
size_t zmalloc_used_memory(void);
|
||||
void zmalloc_enable_thread_safeness(void);
|
||||
void zmalloc_set_oom_handler(void (*oom_handler)(size_t));
|
||||
float zmalloc_get_fragmentation_ratio(void);
|
||||
size_t zmalloc_get_rss(void);
|
||||
void zlibc_free(void *ptr);
|
||||
|
||||
@@ -61,9 +61,13 @@ start_server {tags {"repl"}} {
|
||||
|
||||
test {SET on the master should immediately propagate} {
|
||||
r -1 set mykey bar
|
||||
if {$::valgrind} {after 2000}
|
||||
r 0 get mykey
|
||||
} {bar}
|
||||
|
||||
wait_for_condition 500 100 {
|
||||
[r 0 get mykey] eq {bar}
|
||||
} else {
|
||||
fail "SET on master did not propagated on slave"
|
||||
}
|
||||
}
|
||||
|
||||
test {FLUSHALL should replicate} {
|
||||
r -1 flushall
|
||||
|
||||
+11
-3
@@ -95,6 +95,14 @@ proc randomInt {max} {
|
||||
expr {int(rand()*$max)}
|
||||
}
|
||||
|
||||
proc randomSignedInt {max} {
|
||||
set i [randomInt $max]
|
||||
if {rand() > 0.5} {
|
||||
set i -$i
|
||||
}
|
||||
return $i
|
||||
}
|
||||
|
||||
proc randpath args {
|
||||
set path [expr {int(rand()*[llength $args])}]
|
||||
uplevel 1 [lindex $args $path]
|
||||
@@ -103,13 +111,13 @@ proc randpath args {
|
||||
proc randomValue {} {
|
||||
randpath {
|
||||
# Small enough to likely collide
|
||||
randomInt 1000
|
||||
randomSignedInt 1000
|
||||
} {
|
||||
# 32 bit compressible signed/unsigned
|
||||
randpath {randomInt 2000000000} {randomInt 4000000000}
|
||||
randpath {randomSignedInt 2000000000} {randomSignedInt 4000000000}
|
||||
} {
|
||||
# 64 bit
|
||||
randpath {randomInt 1000000000000}
|
||||
randpath {randomSignedInt 1000000000000}
|
||||
} {
|
||||
# Random string
|
||||
randpath {randstring 0 256 alpha} \
|
||||
|
||||
@@ -73,6 +73,17 @@ start_server {tags {"bitops"}} {
|
||||
set e
|
||||
} {ERR*syntax*}
|
||||
|
||||
test {BITCOUNT regression test for github issue #582} {
|
||||
r del str
|
||||
r setbit foo 0 1
|
||||
if {[catch {r bitcount foo 0 4294967296} e]} {
|
||||
assert_match {*ERR*out of range*} $e
|
||||
set _ 1
|
||||
} else {
|
||||
set e
|
||||
}
|
||||
} {1}
|
||||
|
||||
test {BITOP NOT (empty string)} {
|
||||
r set s ""
|
||||
r bitop not dest s
|
||||
|
||||
@@ -145,6 +145,12 @@ start_server {tags {"scripting"}} {
|
||||
set e
|
||||
} {*not allowed after*}
|
||||
|
||||
test {EVAL - No arguments to redis.call/pcall is considered an error} {
|
||||
set e {}
|
||||
catch {r eval {return redis.call()} 0} e
|
||||
set e
|
||||
} {*one argument*}
|
||||
|
||||
test {EVAL - redis.call variant raises a Lua error on Redis cmd error (1)} {
|
||||
set e {}
|
||||
catch {
|
||||
@@ -197,23 +203,23 @@ start_server {tags {"scripting"}} {
|
||||
r eval {return redis.call('smembers','myset')} 0
|
||||
} {a aa aaa azz b c d e f g h i l m n o p q r s t u v z}
|
||||
|
||||
test "SORT is normally not re-ordered by the scripting engine" {
|
||||
test "SORT is normally not alpha re-ordered for the scripting engine" {
|
||||
r del myset
|
||||
r sadd myset 1 2 3 4 10
|
||||
r eval {return redis.call('sort','myset','desc')} 0
|
||||
} {10 4 3 2 1}
|
||||
|
||||
test "SORT BY <constant> output gets ordered by scripting" {
|
||||
test "SORT BY <constant> output gets ordered for scripting" {
|
||||
r del myset
|
||||
r sadd myset a b c d e f g h i l m n o p q r s t u v z aa aaa azz
|
||||
r eval {return redis.call('sort','myset','by','_')} 0
|
||||
} {a aa aaa azz b c d e f g h i l m n o p q r s t u v z}
|
||||
|
||||
test "SORT output containing NULLs is well handled by scripting" {
|
||||
test "SORT BY <constant> with GET gets ordered for scripting" {
|
||||
r del myset
|
||||
r sadd myset a b c
|
||||
r eval {return redis.call('sort','myset','by','_','get','#','get','_:*')} 0
|
||||
} {{} {} {} a b c}
|
||||
} {a {} b {} c {}}
|
||||
|
||||
test "redis.sha1hex() implementation" {
|
||||
list [r eval {return redis.sha1hex('')} 0] \
|
||||
@@ -295,7 +301,7 @@ start_server {tags {"scripting"}} {
|
||||
catch {r ping} e
|
||||
assert_match {BUSY*} $e
|
||||
catch {r script kill} e
|
||||
assert_match {ERR*} $e
|
||||
assert_match {UNKILLABLE*} $e
|
||||
catch {r ping} e
|
||||
assert_match {BUSY*} $e
|
||||
}
|
||||
@@ -338,5 +344,22 @@ start_server {tags {"scripting repl"}} {
|
||||
fail "Expected 2 in x, but value is '[r -1 get x]'"
|
||||
}
|
||||
}
|
||||
|
||||
test {Replication of script multiple pushes to list with BLPOP} {
|
||||
set rd [redis_deferring_client]
|
||||
$rd brpop a 0
|
||||
r eval {
|
||||
redis.call("lpush","a","1");
|
||||
redis.call("lpush","a","2");
|
||||
} 0
|
||||
set res [$rd read]
|
||||
$rd close
|
||||
wait_for_condition 50 100 {
|
||||
[r -1 lrange a 0 -1] eq [r lrange a 0 -1]
|
||||
} else {
|
||||
fail "Expected list 'a' in slave and master to be the same, but they are respectively '[r -1 lrange a 0 -1]' and '[r lrange a 0 -1]'"
|
||||
}
|
||||
set res
|
||||
} {a 1}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -118,6 +118,47 @@ start_server {
|
||||
r sort zset alpha desc
|
||||
} {e d c b a}
|
||||
|
||||
test "SORT sorted set BY nosort should retain ordering" {
|
||||
r del zset
|
||||
r zadd zset 1 a
|
||||
r zadd zset 5 b
|
||||
r zadd zset 2 c
|
||||
r zadd zset 10 d
|
||||
r zadd zset 3 e
|
||||
r multi
|
||||
r sort zset by nosort asc
|
||||
r sort zset by nosort desc
|
||||
r exec
|
||||
} {{a c e b d} {d b e c a}}
|
||||
|
||||
test "SORT sorted set BY nosort + LIMIT" {
|
||||
r del zset
|
||||
r zadd zset 1 a
|
||||
r zadd zset 5 b
|
||||
r zadd zset 2 c
|
||||
r zadd zset 10 d
|
||||
r zadd zset 3 e
|
||||
assert_equal [r sort zset by nosort asc limit 0 1] {a}
|
||||
assert_equal [r sort zset by nosort desc limit 0 1] {d}
|
||||
assert_equal [r sort zset by nosort asc limit 0 2] {a c}
|
||||
assert_equal [r sort zset by nosort desc limit 0 2] {d b}
|
||||
assert_equal [r sort zset by nosort limit 5 10] {}
|
||||
assert_equal [r sort zset by nosort limit -10 100] {a c e b d}
|
||||
}
|
||||
|
||||
test "SORT sorted set BY nosort works as expected from scripts" {
|
||||
r del zset
|
||||
r zadd zset 1 a
|
||||
r zadd zset 5 b
|
||||
r zadd zset 2 c
|
||||
r zadd zset 10 d
|
||||
r zadd zset 3 e
|
||||
r eval {
|
||||
return {redis.call('sort','zset','by','nosort','asc'),
|
||||
redis.call('sort','zset','by','nosort','desc')}
|
||||
} 0
|
||||
} {{a c e b d} {d b e c a}}
|
||||
|
||||
test "SORT sorted set: +inf and -inf handling" {
|
||||
r del zset
|
||||
r zadd zset -100 a
|
||||
|
||||
@@ -397,7 +397,7 @@ start_server {tags {"hash"}} {
|
||||
} {b}
|
||||
|
||||
foreach size {10 512} {
|
||||
test "Hash fuzzing - $size fields" {
|
||||
test "Hash fuzzing #1 - $size fields" {
|
||||
for {set times 0} {$times < 10} {incr times} {
|
||||
catch {unset hash}
|
||||
array set hash {}
|
||||
@@ -418,5 +418,53 @@ start_server {tags {"hash"}} {
|
||||
assert_equal [array size hash] [r hlen hash]
|
||||
}
|
||||
}
|
||||
|
||||
test "Hash fuzzing #2 - $size fields" {
|
||||
for {set times 0} {$times < 10} {incr times} {
|
||||
catch {unset hash}
|
||||
array set hash {}
|
||||
r del hash
|
||||
|
||||
# Create
|
||||
for {set j 0} {$j < $size} {incr j} {
|
||||
randpath {
|
||||
set field [randomValue]
|
||||
set value [randomValue]
|
||||
r hset hash $field $value
|
||||
set hash($field) $value
|
||||
} {
|
||||
set field [randomSignedInt 512]
|
||||
set value [randomSignedInt 512]
|
||||
r hset hash $field $value
|
||||
set hash($field) $value
|
||||
} {
|
||||
randpath {
|
||||
set field [randomValue]
|
||||
} {
|
||||
set field [randomSignedInt 512]
|
||||
}
|
||||
r hdel hash $field
|
||||
unset -nocomplain hash($field)
|
||||
}
|
||||
}
|
||||
|
||||
# Verify
|
||||
foreach {k v} [array get hash] {
|
||||
assert_equal $v [r hget hash $k]
|
||||
}
|
||||
assert_equal [array size hash] [r hlen hash]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
test {Stress test the hash ziplist -> hashtable encoding conversion} {
|
||||
r config set hash-max-ziplist-entries 32
|
||||
for {set j 0} {$j < 100} {incr j} {
|
||||
r del myhash
|
||||
for {set i 0} {$i < 64} {incr i} {
|
||||
r hset myhash [randomValue] [randomValue]
|
||||
}
|
||||
assert {[r object encoding myhash] eq {hashtable}}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,6 +161,47 @@ start_server {
|
||||
}
|
||||
}
|
||||
|
||||
test "BLPOP, LPUSH + DEL should not awake blocked client" {
|
||||
set rd [redis_deferring_client]
|
||||
r del list
|
||||
|
||||
$rd blpop list 0
|
||||
r multi
|
||||
r lpush list a
|
||||
r del list
|
||||
r exec
|
||||
r del list
|
||||
r lpush list b
|
||||
$rd read
|
||||
} {list b}
|
||||
|
||||
test "BLPOP, LPUSH + DEL + SET should not awake blocked client" {
|
||||
set rd [redis_deferring_client]
|
||||
r del list
|
||||
|
||||
$rd blpop list 0
|
||||
r multi
|
||||
r lpush list a
|
||||
r del list
|
||||
r set list foo
|
||||
r exec
|
||||
r del list
|
||||
r lpush list b
|
||||
$rd read
|
||||
} {list b}
|
||||
|
||||
test "MULTI/EXEC is isolated from the point of view of BLPOP" {
|
||||
set rd [redis_deferring_client]
|
||||
r del list
|
||||
$rd blpop list 0
|
||||
r multi
|
||||
r lpush list a
|
||||
r lpush list b
|
||||
r lpush list c
|
||||
r exec
|
||||
$rd read
|
||||
} {list c}
|
||||
|
||||
test "BLPOP with variadic LPUSH" {
|
||||
set rd [redis_deferring_client]
|
||||
r del blist target
|
||||
@@ -169,8 +210,8 @@ start_server {
|
||||
if {$::valgrind} {after 100}
|
||||
assert_equal 2 [r lpush blist foo bar]
|
||||
if {$::valgrind} {after 100}
|
||||
assert_equal {blist foo} [$rd read]
|
||||
assert_equal bar [lindex [r lrange blist 0 -1] 0]
|
||||
assert_equal {blist bar} [$rd read]
|
||||
assert_equal foo [lindex [r lrange blist 0 -1] 0]
|
||||
}
|
||||
|
||||
test "BRPOPLPUSH with zero timeout should block indefinitely" {
|
||||
@@ -222,6 +263,16 @@ start_server {
|
||||
assert_equal {foo} [r lrange blist 0 -1]
|
||||
}
|
||||
|
||||
test "BRPOPLPUSH maintains order of elements after failure" {
|
||||
set rd [redis_deferring_client]
|
||||
r del blist target
|
||||
r set target nolist
|
||||
$rd brpoplpush blist target 0
|
||||
r rpush blist a b c
|
||||
assert_error "ERR*wrong kind*" {$rd read}
|
||||
r lrange blist 0 -1
|
||||
} {a b c}
|
||||
|
||||
test "BRPOPLPUSH with multiple blocked clients" {
|
||||
set rd1 [redis_deferring_client]
|
||||
set rd2 [redis_deferring_client]
|
||||
@@ -293,6 +344,41 @@ start_server {
|
||||
r exec
|
||||
} {foo bar {} {} {bar foo}}
|
||||
|
||||
test "PUSH resulting from BRPOPLPUSH affect WATCH" {
|
||||
set blocked_client [redis_deferring_client]
|
||||
set watching_client [redis_deferring_client]
|
||||
r del srclist dstlist somekey
|
||||
r set somekey somevalue
|
||||
$blocked_client brpoplpush srclist dstlist 0
|
||||
$watching_client watch dstlist
|
||||
$watching_client read
|
||||
$watching_client multi
|
||||
$watching_client read
|
||||
$watching_client get somekey
|
||||
$watching_client read
|
||||
r lpush srclist element
|
||||
$watching_client exec
|
||||
$watching_client read
|
||||
} {}
|
||||
|
||||
test "BRPOPLPUSH does not affect WATCH while still blocked" {
|
||||
set blocked_client [redis_deferring_client]
|
||||
set watching_client [redis_deferring_client]
|
||||
r del srclist dstlist somekey
|
||||
r set somekey somevalue
|
||||
$blocked_client brpoplpush srclist dstlist 0
|
||||
$watching_client watch dstlist
|
||||
$watching_client read
|
||||
$watching_client multi
|
||||
$watching_client read
|
||||
$watching_client get somekey
|
||||
$watching_client read
|
||||
$watching_client exec
|
||||
# Blocked BLPOPLPUSH may create problems, unblock it.
|
||||
r lpush srclist element
|
||||
$watching_client read
|
||||
} {somevalue}
|
||||
|
||||
test {BRPOPLPUSH timeout} {
|
||||
set rd [redis_deferring_client]
|
||||
|
||||
|
||||
@@ -271,6 +271,118 @@ start_server {
|
||||
}
|
||||
}
|
||||
|
||||
test "SRANDMEMBER with <count> against non existing key" {
|
||||
r srandmember nonexisting_key 100
|
||||
} {}
|
||||
|
||||
foreach {type contents} {
|
||||
hashtable {
|
||||
1 5 10 50 125 50000 33959417 4775547 65434162
|
||||
12098459 427716 483706 2726473884 72615637475
|
||||
MARY PATRICIA LINDA BARBARA ELIZABETH JENNIFER MARIA
|
||||
SUSAN MARGARET DOROTHY LISA NANCY KAREN BETTY HELEN
|
||||
SANDRA DONNA CAROL RUTH SHARON MICHELLE LAURA SARAH
|
||||
KIMBERLY DEBORAH JESSICA SHIRLEY CYNTHIA ANGELA MELISSA
|
||||
BRENDA AMY ANNA REBECCA VIRGINIA KATHLEEN
|
||||
}
|
||||
intset {
|
||||
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
||||
20 21 22 23 24 25 26 27 28 29
|
||||
30 31 32 33 34 35 36 37 38 39
|
||||
40 41 42 43 44 45 46 47 48 49
|
||||
}
|
||||
} {
|
||||
test "SRANDMEMBER with <count> - $type" {
|
||||
create_set myset $contents
|
||||
unset -nocomplain myset
|
||||
array set myset {}
|
||||
foreach ele [r smembers myset] {
|
||||
set myset($ele) 1
|
||||
}
|
||||
assert_equal [lsort $contents] [lsort [array names myset]]
|
||||
|
||||
# Make sure that a count of 0 is handled correctly.
|
||||
assert_equal [r srandmember myset 0] {}
|
||||
|
||||
# We'll stress different parts of the code, see the implementation
|
||||
# of SRANDMEMBER for more information, but basically there are
|
||||
# four different code paths.
|
||||
#
|
||||
# PATH 1: Use negative count.
|
||||
#
|
||||
# 1) Check that it returns repeated elements.
|
||||
set res [r srandmember myset -100]
|
||||
assert_equal [llength $res] 100
|
||||
|
||||
# 2) Check that all the elements actually belong to the
|
||||
# original set.
|
||||
foreach ele $res {
|
||||
assert {[info exists myset($ele)]}
|
||||
}
|
||||
|
||||
# 3) Check that eventually all the elements are returned.
|
||||
unset -nocomplain auxset
|
||||
set iterations 1000
|
||||
while {$iterations != 0} {
|
||||
incr iterations -1
|
||||
set res [r srandmember myset -10]
|
||||
foreach ele $res {
|
||||
set auxset($ele) 1
|
||||
}
|
||||
if {[lsort [array names myset]] eq
|
||||
[lsort [array names auxset]]} {
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert {$iterations != 0}
|
||||
|
||||
# PATH 2: positive count (unique behavior) with requested size
|
||||
# equal or greater than set size.
|
||||
foreach size {50 100} {
|
||||
set res [r srandmember myset $size]
|
||||
assert_equal [llength $res] 50
|
||||
assert_equal [lsort $res] [lsort [array names myset]]
|
||||
}
|
||||
|
||||
# PATH 3: Ask almost as elements as there are in the set.
|
||||
# In this case the implementation will duplicate the original
|
||||
# set and will remove random elements up to the requested size.
|
||||
#
|
||||
# PATH 4: Ask a number of elements definitely smaller than
|
||||
# the set size.
|
||||
#
|
||||
# We can test both the code paths just changing the size but
|
||||
# using the same code.
|
||||
|
||||
foreach size {45 5} {
|
||||
set res [r srandmember myset $size]
|
||||
assert_equal [llength $res] $size
|
||||
|
||||
# 1) Check that all the elements actually belong to the
|
||||
# original set.
|
||||
foreach ele $res {
|
||||
assert {[info exists myset($ele)]}
|
||||
}
|
||||
|
||||
# 2) Check that eventually all the elements are returned.
|
||||
unset -nocomplain auxset
|
||||
set iterations 1000
|
||||
while {$iterations != 0} {
|
||||
incr iterations -1
|
||||
set res [r srandmember myset -10]
|
||||
foreach ele $res {
|
||||
set auxset($ele) 1
|
||||
}
|
||||
if {[lsort [array names myset]] eq
|
||||
[lsort [array names auxset]]} {
|
||||
break;
|
||||
}
|
||||
}
|
||||
assert {$iterations != 0}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
proc setup_move {} {
|
||||
r del myset3 myset4
|
||||
create_set myset1 {1 a b}
|
||||
|
||||
+18
-7
@@ -1,4 +1,4 @@
|
||||
#!/bin/bash
|
||||
#!/bin/sh
|
||||
|
||||
# Copyright 2011 Dvir Volk <dvirsk at gmail dot com>. All rights reserved.
|
||||
#
|
||||
@@ -48,6 +48,7 @@ if [ `whoami` != "root" ] ; then
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
#Read the redis port
|
||||
read -p "Please select the redis port for this instance: [$_REDIS_PORT] " REDIS_PORT
|
||||
if [ ! `echo $REDIS_PORT | egrep "^[0-9]+\$"` ] ; then
|
||||
@@ -99,7 +100,7 @@ fi
|
||||
|
||||
#render the tmplates
|
||||
TMP_FILE="/tmp/$REDIS_PORT.conf"
|
||||
TPL_FILE="./redis.conf.tpl"
|
||||
DEFAULT_CONFIG="../redis.conf"
|
||||
INIT_TPL_FILE="./redis_init_script.tpl"
|
||||
INIT_SCRIPT_DEST="/etc/init.d/redis_$REDIS_PORT"
|
||||
PIDFILE="/var/run/redis_$REDIS_PORT.pid"
|
||||
@@ -112,9 +113,19 @@ if [ ! "$CLI_EXEC" ] ; then
|
||||
CLI_EXEC=`dirname $REDIS_EXECUTABLE`"/redis-cli"
|
||||
fi
|
||||
|
||||
#Generate config file from template
|
||||
#Generate config file from the default config file as template
|
||||
#changing only the stuff we're controlling from this script
|
||||
echo "## Generated by install_server.sh ##" > $TMP_FILE
|
||||
cat $TPL_FILE | while read line; do eval "echo \"$line\"" >> $TMP_FILE; done
|
||||
|
||||
SED_EXPR="s#^port [0-9]{4}\$#port ${REDIS_PORT}#;\
|
||||
s#^logfile .+\$#logfile ${REDIS_LOG_FILE}#;\
|
||||
s#^dir .+\$#dir ${REDIS_DATA_DIR}#;\
|
||||
s#^pidfile .+\$#pidfile ${PIDFILE}#;\
|
||||
s#^daemonize no\$#daemonize yes#;"
|
||||
echo $SED_EXPR
|
||||
sed -r "$SED_EXPR" $DEFAULT_CONFIG >> $TMP_FILE
|
||||
|
||||
#cat $TPL_FILE | while read line; do eval "echo \"$line\"" >> $TMP_FILE; done
|
||||
cp -f $TMP_FILE $REDIS_CONFIG_FILE || exit 1
|
||||
|
||||
#Generate sample script from template file
|
||||
@@ -146,9 +157,9 @@ REDIS_CHKCONFIG_INFO=\
|
||||
# Description: Redis daemon\n
|
||||
### END INIT INFO\n\n"
|
||||
|
||||
if [[ ! `which chkconfig` ]] ; then
|
||||
if [ !`which chkconfig` ] ; then
|
||||
#combine the header and the template (which is actually a static footer)
|
||||
echo -e $REDIS_INIT_HEADER > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE"
|
||||
echo $REDIS_INIT_HEADER > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE"
|
||||
else
|
||||
#if we're a box with chkconfig on it we want to include info for chkconfig
|
||||
echo -e $REDIS_INIT_HEADER $REDIS_CHKCONFIG_INFO > $TMP_FILE && cat $INIT_TPL_FILE >> $TMP_FILE || die "Could not write init script to $TMP_FILE"
|
||||
@@ -160,7 +171,7 @@ echo "Copied $TMP_FILE => $INIT_SCRIPT_DEST"
|
||||
|
||||
#Install the service
|
||||
echo "Installing service..."
|
||||
if [[ ! `which chkconfig` ]] ; then
|
||||
if [ !`which chkconfig` ] ; then
|
||||
#if we're not a chkconfig box assume we're able to use update-rc.d
|
||||
update-rc.d redis_$REDIS_PORT defaults && echo "Success!"
|
||||
else
|
||||
|
||||
@@ -1,495 +0,0 @@
|
||||
# Redis configuration file example
|
||||
|
||||
# Note on units: when memory size is needed, it is possible to specify
|
||||
# it in the usual form of 1k 5GB 4M and so forth:
|
||||
#
|
||||
# 1k => 1000 bytes
|
||||
# 1kb => 1024 bytes
|
||||
# 1m => 1000000 bytes
|
||||
# 1mb => 1024*1024 bytes
|
||||
# 1g => 1000000000 bytes
|
||||
# 1gb => 1024*1024*1024 bytes
|
||||
#
|
||||
# units are case insensitive so 1GB 1Gb 1gB are all the same.
|
||||
|
||||
# By default Redis does not run as a daemon. Use 'yes' if you need it.
|
||||
# Note that Redis will write a pid file in /var/run/redis.pid when daemonized.
|
||||
daemonize yes
|
||||
|
||||
# When running daemonized, Redis writes a pid file in /var/run/redis.pid by
|
||||
# default. You can specify a custom pid file location here.
|
||||
pidfile $PIDFILE
|
||||
|
||||
# Accept connections on the specified port, default is 6379.
|
||||
# If port 0 is specified Redis will not listen on a TCP socket.
|
||||
port $REDIS_PORT
|
||||
|
||||
# If you want you can bind a single interface, if the bind option is not
|
||||
# specified all the interfaces will listen for incoming connections.
|
||||
#
|
||||
# bind 127.0.0.1
|
||||
|
||||
# Specify the path for the unix socket that will be used to listen for
|
||||
# incoming connections. There is no default, so Redis will not listen
|
||||
# on a unix socket when not specified.
|
||||
#
|
||||
# unixsocket /tmp/redis.sock
|
||||
# unixsocketperm 755
|
||||
|
||||
# Close the connection after a client is idle for N seconds (0 to disable)
|
||||
timeout 0
|
||||
|
||||
# Set server verbosity to 'debug'
|
||||
# it can be one of:
|
||||
# debug (a lot of information, useful for development/testing)
|
||||
# verbose (many rarely useful info, but not a mess like the debug level)
|
||||
# notice (moderately verbose, what you want in production probably)
|
||||
# warning (only very important / critical messages are logged)
|
||||
loglevel notice
|
||||
|
||||
# Specify the log file name. Also 'stdout' can be used to force
|
||||
# Redis to log on the standard output. Note that if you use standard
|
||||
# output for logging but daemonize, logs will be sent to /dev/null
|
||||
logfile $REDIS_LOG_FILE
|
||||
|
||||
# To enable logging to the system logger, just set 'syslog-enabled' to yes,
|
||||
# and optionally update the other syslog parameters to suit your needs.
|
||||
# syslog-enabled no
|
||||
|
||||
# Specify the syslog identity.
|
||||
# syslog-ident redis
|
||||
|
||||
# Specify the syslog facility. Must be USER or between LOCAL0-LOCAL7.
|
||||
# syslog-facility local0
|
||||
|
||||
# Set the number of databases. The default database is DB 0, you can select
|
||||
# a different one on a per-connection basis using SELECT <dbid> where
|
||||
# dbid is a number between 0 and 'databases'-1
|
||||
databases 16
|
||||
|
||||
################################ SNAPSHOTTING #################################
|
||||
#
|
||||
# Save the DB on disk:
|
||||
#
|
||||
# save <seconds> <changes>
|
||||
#
|
||||
# Will save the DB if both the given number of seconds and the given
|
||||
# number of write operations against the DB occurred.
|
||||
#
|
||||
# In the example below the behaviour will be to save:
|
||||
# after 900 sec (15 min) if at least 1 key changed
|
||||
# after 300 sec (5 min) if at least 10 keys changed
|
||||
# after 60 sec if at least 10000 keys changed
|
||||
#
|
||||
# Note: you can disable saving at all commenting all the "save" lines.
|
||||
#
|
||||
# It is also possible to remove all the previously configured save
|
||||
# points by adding a save directive with a single empty string argument
|
||||
# like in the following example:
|
||||
#
|
||||
# save ""
|
||||
|
||||
save 900 1
|
||||
save 300 10
|
||||
save 60 10000
|
||||
|
||||
# By default Redis will stop accepting writes if RDB snapshots are enabled
|
||||
# (at least one save point) and the latest background save failed.
|
||||
# This will make the user aware (in an hard way) that data is not persisting
|
||||
# on disk properly, otherwise chances are that no one will notice and some
|
||||
# distater will happen.
|
||||
#
|
||||
# If the background saving process will start working again Redis will
|
||||
# automatically allow writes again.
|
||||
#
|
||||
# However if you have setup your proper monitoring of the Redis server
|
||||
# and persistence, you may want to disable this feature so that Redis will
|
||||
# continue to work as usually even if there are problems with disk,
|
||||
# permissions, and so forth.
|
||||
stop-writes-on-bgsave-error yes
|
||||
|
||||
# Compress string objects using LZF when dump .rdb databases?
|
||||
# For default that's set to 'yes' as it's almost always a win.
|
||||
# If you want to save some CPU in the saving child set it to 'no' but
|
||||
# the dataset will likely be bigger if you have compressible values or keys.
|
||||
rdbcompression yes
|
||||
|
||||
# The filename where to dump the DB
|
||||
dbfilename dump.rdb
|
||||
|
||||
# The working directory.
|
||||
#
|
||||
# The DB will be written inside this directory, with the filename specified
|
||||
# above using the 'dbfilename' configuration directive.
|
||||
#
|
||||
# Also the Append Only File will be created inside this directory.
|
||||
#
|
||||
# Note that you must specify a directory here, not a file name.
|
||||
dir $REDIS_DATA_DIR
|
||||
|
||||
################################# REPLICATION #################################
|
||||
|
||||
# Master-Slave replication. Use slaveof to make a Redis instance a copy of
|
||||
# another Redis server. Note that the configuration is local to the slave
|
||||
# so for example it is possible to configure the slave to save the DB with a
|
||||
# different interval, or to listen to another port, and so on.
|
||||
#
|
||||
# slaveof <masterip> <masterport>
|
||||
|
||||
# If the master is password protected (using the "requirepass" configuration
|
||||
# directive below) it is possible to tell the slave to authenticate before
|
||||
# starting the replication synchronization process, otherwise the master will
|
||||
# refuse the slave request.
|
||||
#
|
||||
# masterauth <master-password>
|
||||
|
||||
# When a slave lost the connection with the master, or when the replication
|
||||
# is still in progress, the slave can act in two different ways:
|
||||
#
|
||||
# 1) if slave-serve-stale-data is set to 'yes' (the default) the slave will
|
||||
# still reply to client requests, possibly with out of date data, or the
|
||||
# data set may just be empty if this is the first synchronization.
|
||||
#
|
||||
# 2) if slave-serve-stale data is set to 'no' the slave will reply with
|
||||
# an error "SYNC with master in progress" to all the kind of commands
|
||||
# but to INFO and SLAVEOF.
|
||||
#
|
||||
slave-serve-stale-data yes
|
||||
|
||||
# Slaves send PINGs to server in a predefined interval. It's possible to change
|
||||
# this interval with the repl_ping_slave_period option. The default value is 10
|
||||
# seconds.
|
||||
#
|
||||
# repl-ping-slave-period 10
|
||||
|
||||
# The following option sets a timeout for both Bulk transfer I/O timeout and
|
||||
# master data or ping response timeout. The default value is 60 seconds.
|
||||
#
|
||||
# It is important to make sure that this value is greater than the value
|
||||
# specified for repl-ping-slave-period otherwise a timeout will be detected
|
||||
# every time there is low traffic between the master and the slave.
|
||||
#
|
||||
# repl-timeout 60
|
||||
|
||||
################################## SECURITY ###################################
|
||||
|
||||
# Require clients to issue AUTH <PASSWORD> before processing any other
|
||||
# commands. This might be useful in environments in which you do not trust
|
||||
# others with access to the host running redis-server.
|
||||
#
|
||||
# This should stay commented out for backward compatibility and because most
|
||||
# people do not need auth (e.g. they run their own servers).
|
||||
#
|
||||
# Warning: since Redis is pretty fast an outside user can try up to
|
||||
# 150k passwords per second against a good box. This means that you should
|
||||
# use a very strong password otherwise it will be very easy to break.
|
||||
#
|
||||
# requirepass foobared
|
||||
|
||||
# Command renaming.
|
||||
#
|
||||
# It is possible to change the name of dangerous commands in a shared
|
||||
# environment. For instance the CONFIG command may be renamed into something
|
||||
# of hard to guess so that it will be still available for internal-use
|
||||
# tools but not available for general clients.
|
||||
#
|
||||
# Example:
|
||||
#
|
||||
# rename-command CONFIG b840fc02d524045429941cc15f59e41cb7be6c52
|
||||
#
|
||||
# It is also possible to completely kill a command renaming it into
|
||||
# an empty string:
|
||||
#
|
||||
# rename-command CONFIG ""
|
||||
|
||||
################################### LIMITS ####################################
|
||||
|
||||
# Set the max number of connected clients at the same time. By default
|
||||
# this limit is set to 10000 clients, however if the Redis server is not
|
||||
# able ot configure the process file limit to allow for the specified limit
|
||||
# the max number of allowed clients is set to the current file limit
|
||||
# minus 32 (as Redis reserves a few file descriptors for internal uses).
|
||||
#
|
||||
# Once the limit is reached Redis will close all the new connections sending
|
||||
# an error 'max number of clients reached'.
|
||||
#
|
||||
# maxclients 10000
|
||||
|
||||
# Don't use more memory than the specified amount of bytes.
|
||||
# When the memory limit is reached Redis will try to remove keys
|
||||
# accordingly to the eviction policy selected (see maxmemmory-policy).
|
||||
#
|
||||
# If Redis can't remove keys according to the policy, or if the policy is
|
||||
# set to 'noeviction', Redis will start to reply with errors to commands
|
||||
# that would use more memory, like SET, LPUSH, and so on, and will continue
|
||||
# to reply to read-only commands like GET.
|
||||
#
|
||||
# This option is usually useful when using Redis as an LRU cache, or to set
|
||||
# an hard memory limit for an instance (using the 'noeviction' policy).
|
||||
#
|
||||
# WARNING: If you have slaves attached to an instance with maxmemory on,
|
||||
# the size of the output buffers needed to feed the slaves are subtracted
|
||||
# from the used memory count, so that network problems / resyncs will
|
||||
# not trigger a loop where keys are evicted, and in turn the output
|
||||
# buffer of slaves is full with DELs of keys evicted triggering the deletion
|
||||
# of more keys, and so forth until the database is completely emptied.
|
||||
#
|
||||
# In short... if you have slaves attached it is suggested that you set a lower
|
||||
# limit for maxmemory so that there is some free RAM on the system for slave
|
||||
# output buffers (but this is not needed if the policy is 'noeviction').
|
||||
#
|
||||
# maxmemory <bytes>
|
||||
|
||||
# MAXMEMORY POLICY: how Redis will select what to remove when maxmemory
|
||||
# is reached? You can select among five behavior:
|
||||
#
|
||||
# volatile-lru -> remove the key with an expire set using an LRU algorithm
|
||||
# allkeys-lru -> remove any key accordingly to the LRU algorithm
|
||||
# volatile-random -> remove a random key with an expire set
|
||||
# allkeys-random -> remove a random key, any key
|
||||
# volatile-ttl -> remove the key with the nearest expire time (minor TTL)
|
||||
# noeviction -> don't expire at all, just return an error on write operations
|
||||
#
|
||||
# Note: with all the kind of policies, Redis will return an error on write
|
||||
# operations, when there are not suitable keys for eviction.
|
||||
#
|
||||
# At the date of writing this commands are: set setnx setex append
|
||||
# incr decr rpush lpush rpushx lpushx linsert lset rpoplpush sadd
|
||||
# sinter sinterstore sunion sunionstore sdiff sdiffstore zadd zincrby
|
||||
# zunionstore zinterstore hset hsetnx hmset hincrby incrby decrby
|
||||
# getset mset msetnx exec sort
|
||||
#
|
||||
# The default is:
|
||||
#
|
||||
# maxmemory-policy volatile-lru
|
||||
|
||||
# LRU and minimal TTL algorithms are not precise algorithms but approximated
|
||||
# algorithms (in order to save memory), so you can select as well the sample
|
||||
# size to check. For instance for default Redis will check three keys and
|
||||
# pick the one that was used less recently, you can change the sample size
|
||||
# using the following configuration directive.
|
||||
#
|
||||
# maxmemory-samples 3
|
||||
|
||||
############################## APPEND ONLY MODE ###############################
|
||||
|
||||
# By default Redis asynchronously dumps the dataset on disk. If you can live
|
||||
# with the idea that the latest records will be lost if something like a crash
|
||||
# happens this is the preferred way to run Redis. If instead you care a lot
|
||||
# about your data and don't want to that a single record can get lost you should
|
||||
# enable the append only mode: when this mode is enabled Redis will append
|
||||
# every write operation received in the file appendonly.aof. This file will
|
||||
# be read on startup in order to rebuild the full dataset in memory.
|
||||
#
|
||||
# Note that you can have both the async dumps and the append only file if you
|
||||
# like (you have to comment the "save" statements above to disable the dumps).
|
||||
# Still if append only mode is enabled Redis will load the data from the
|
||||
# log file at startup ignoring the dump.rdb file.
|
||||
#
|
||||
# IMPORTANT: Check the BGREWRITEAOF to check how to rewrite the append
|
||||
# log file in background when it gets too big.
|
||||
|
||||
appendonly no
|
||||
|
||||
# The name of the append only file (default: "appendonly.aof")
|
||||
# appendfilename appendonly.aof
|
||||
|
||||
# The fsync() call tells the Operating System to actually write data on disk
|
||||
# instead to wait for more data in the output buffer. Some OS will really flush
|
||||
# data on disk, some other OS will just try to do it ASAP.
|
||||
#
|
||||
# Redis supports three different modes:
|
||||
#
|
||||
# no: don't fsync, just let the OS flush the data when it wants. Faster.
|
||||
# always: fsync after every write to the append only log . Slow, Safest.
|
||||
# everysec: fsync only if one second passed since the last fsync. Compromise.
|
||||
#
|
||||
# The default is "everysec" that's usually the right compromise between
|
||||
# speed and data safety. It's up to you to understand if you can relax this to
|
||||
# "no" that will let the operating system flush the output buffer when
|
||||
# it wants, for better performances (but if you can live with the idea of
|
||||
# some data loss consider the default persistence mode that's snapshotting),
|
||||
# or on the contrary, use "always" that's very slow but a bit safer than
|
||||
# everysec.
|
||||
#
|
||||
# If unsure, use "everysec".
|
||||
|
||||
# appendfsync always
|
||||
appendfsync everysec
|
||||
# appendfsync no
|
||||
|
||||
# When the AOF fsync policy is set to always or everysec, and a background
|
||||
# saving process (a background save or AOF log background rewriting) is
|
||||
# performing a lot of I/O against the disk, in some Linux configurations
|
||||
# Redis may block too long on the fsync() call. Note that there is no fix for
|
||||
# this currently, as even performing fsync in a different thread will block
|
||||
# our synchronous write(2) call.
|
||||
#
|
||||
# In order to mitigate this problem it's possible to use the following option
|
||||
# that will prevent fsync() from being called in the main process while a
|
||||
# BGSAVE or BGREWRITEAOF is in progress.
|
||||
#
|
||||
# This means that while another child is saving the durability of Redis is
|
||||
# the same as "appendfsync none", that in practical terms means that it is
|
||||
# possible to lost up to 30 seconds of log in the worst scenario (with the
|
||||
# default Linux settings).
|
||||
#
|
||||
# If you have latency problems turn this to "yes". Otherwise leave it as
|
||||
# "no" that is the safest pick from the point of view of durability.
|
||||
no-appendfsync-on-rewrite no
|
||||
|
||||
# Automatic rewrite of the append only file.
|
||||
# Redis is able to automatically rewrite the log file implicitly calling
|
||||
# BGREWRITEAOF when the AOF log size will growth by the specified percentage.
|
||||
#
|
||||
# This is how it works: Redis remembers the size of the AOF file after the
|
||||
# latest rewrite (or if no rewrite happened since the restart, the size of
|
||||
# the AOF at startup is used).
|
||||
#
|
||||
# This base size is compared to the current size. If the current size is
|
||||
# bigger than the specified percentage, the rewrite is triggered. Also
|
||||
# you need to specify a minimal size for the AOF file to be rewritten, this
|
||||
# is useful to avoid rewriting the AOF file even if the percentage increase
|
||||
# is reached but it is still pretty small.
|
||||
#
|
||||
# Specify a percentage of zero in order to disable the automatic AOF
|
||||
# rewrite feature.
|
||||
|
||||
auto-aof-rewrite-percentage 100
|
||||
auto-aof-rewrite-min-size 64mb
|
||||
|
||||
################################ LUA SCRIPTING ###############################
|
||||
|
||||
# Max execution time of a Lua script in milliseconds.
|
||||
#
|
||||
# If the maximum execution time is reached Redis will log that a script is
|
||||
# still in execution after the maximum allowed time and will start to
|
||||
# reply to queries with an error.
|
||||
#
|
||||
# When a long running script exceed the maximum execution time only the
|
||||
# SCRIPT KILL and SHUTDOWN NOSAVE commands are available. The first can be
|
||||
# used to stop a script that did not yet called write commands. The second
|
||||
# is the only way to shut down the server in the case a write commands was
|
||||
# already issue by the script but the user don't want to wait for the natural
|
||||
# termination of the script.
|
||||
#
|
||||
# Set it to 0 or a negative value for unlimited execution without warnings.
|
||||
lua-time-limit 5000
|
||||
|
||||
################################## SLOW LOG ###################################
|
||||
|
||||
# The Redis Slow Log is a system to log queries that exceeded a specified
|
||||
# execution time. The execution time does not include the I/O operations
|
||||
# like talking with the client, sending the reply and so forth,
|
||||
# but just the time needed to actually execute the command (this is the only
|
||||
# stage of command execution where the thread is blocked and can not serve
|
||||
# other requests in the meantime).
|
||||
#
|
||||
# You can configure the slow log with two parameters: one tells Redis
|
||||
# what is the execution time, in microseconds, to exceed in order for the
|
||||
# command to get logged, and the other parameter is the length of the
|
||||
# slow log. When a new command is logged the oldest one is removed from the
|
||||
# queue of logged commands.
|
||||
|
||||
# The following time is expressed in microseconds, so 1000000 is equivalent
|
||||
# to one second. Note that a negative number disables the slow log, while
|
||||
# a value of zero forces the logging of every command.
|
||||
slowlog-log-slower-than 10000
|
||||
|
||||
# There is no limit to this length. Just be aware that it will consume memory.
|
||||
# You can reclaim memory used by the slow log with SLOWLOG RESET.
|
||||
slowlog-max-len 1024
|
||||
|
||||
############################### ADVANCED CONFIG ###############################
|
||||
|
||||
# Hashes are encoded using a memory efficient data structure when they have a
|
||||
# small number of entries, and the biggest entry does not exceed a given
|
||||
# threshold. These thresholds can be configured using the following directives.
|
||||
hash-max-ziplist-entries 512
|
||||
hash-max-ziplist-value 64
|
||||
|
||||
# Similarly to hashes, small lists are also encoded in a special way in order
|
||||
# to save a lot of space. The special representation is only used when
|
||||
# you are under the following limits:
|
||||
list-max-ziplist-entries 512
|
||||
list-max-ziplist-value 64
|
||||
|
||||
# Sets have a special encoding in just one case: when a set is composed
|
||||
# of just strings that happens to be integers in radix 10 in the range
|
||||
# of 64 bit signed integers.
|
||||
# The following configuration setting sets the limit in the size of the
|
||||
# set in order to use this special memory saving encoding.
|
||||
set-max-intset-entries 512
|
||||
|
||||
# Similarly to hashes and lists, sorted sets are also specially encoded in
|
||||
# order to save a lot of space. This encoding is only used when the length and
|
||||
# elements of a sorted set are below the following limits:
|
||||
zset-max-ziplist-entries 128
|
||||
zset-max-ziplist-value 64
|
||||
|
||||
# Active rehashing uses 1 millisecond every 100 milliseconds of CPU time in
|
||||
# order to help rehashing the main Redis hash table (the one mapping top-level
|
||||
# keys to values). The hash table implementation Redis uses (see dict.c)
|
||||
# performs a lazy rehashing: the more operation you run into an hash table
|
||||
# that is rehashing, the more rehashing "steps" are performed, so if the
|
||||
# server is idle the rehashing is never complete and some more memory is used
|
||||
# by the hash table.
|
||||
#
|
||||
# The default is to use this millisecond 10 times every second in order to
|
||||
# active rehashing the main dictionaries, freeing memory when possible.
|
||||
#
|
||||
# If unsure:
|
||||
# use "activerehashing no" if you have hard latency requirements and it is
|
||||
# not a good thing in your environment that Redis can reply form time to time
|
||||
# to queries with 2 milliseconds delay.
|
||||
#
|
||||
# use "activerehashing yes" if you don't have such hard requirements but
|
||||
# want to free memory asap when possible.
|
||||
activerehashing yes
|
||||
|
||||
# The client output buffer limits can be used to force disconnection of clients
|
||||
# that are not reading data from the server fast enough for some reason (a
|
||||
# common reason is that a Pub/Sub client can't consume messages as fast as the
|
||||
# publisher can produce them).
|
||||
#
|
||||
# The limit can be set differently for the three different classes of clients:
|
||||
#
|
||||
# normal -> normal clients
|
||||
# slave -> slave clients and MONITOR clients
|
||||
# pubsub -> clients subcribed to at least one pubsub channel or pattern
|
||||
#
|
||||
# The syntax of every client-output-buffer-limit directive is the following:
|
||||
#
|
||||
# client-output-buffer-limit <class> <hard limit> <soft limit> <soft seconds>
|
||||
#
|
||||
# A client is immediately disconnected once the hard limit is reached, or if
|
||||
# the soft limit is reached and remains reached for the specified number of
|
||||
# seconds (continuously).
|
||||
# So for instance if the hard limit is 32 megabytes and the soft limit is
|
||||
# 16 megabytes / 10 seconds, the client will get disconnected immediately
|
||||
# if the size of the output buffers reach 32 megabytes, but will also get
|
||||
# disconnected if the client reaches 16 megabytes and continuously overcomes
|
||||
# the limit for 10 seconds.
|
||||
#
|
||||
# By default normal clients are not limited because they don't receive data
|
||||
# without asking (in a push way), but just after a request, so only
|
||||
# asynchronous clients may create a scenario where data is requested faster
|
||||
# than it can read.
|
||||
#
|
||||
# Instead there is a default limit for pubsub and slave clients, since
|
||||
# subscribers and slaves receive data in a push fashion.
|
||||
#
|
||||
# Both the hard or the soft limit can be disabled just setting it to zero.
|
||||
client-output-buffer-limit normal 0 0 0
|
||||
client-output-buffer-limit slave 256mb 64mb 60
|
||||
client-output-buffer-limit pubsub 32mb 8mb 60
|
||||
|
||||
################################## INCLUDES ###################################
|
||||
|
||||
# Include one or more other config files here. This is useful if you
|
||||
# have a standard template that goes to all Redis server but also need
|
||||
# to customize a few per-server settings. Include files can include
|
||||
# other files, so use this wisely.
|
||||
#
|
||||
# include /path/to/local.conf
|
||||
# include /path/to/other.conf
|
||||
Reference in New Issue
Block a user