mirror of
https://github.com/fluencelabs/redis
synced 2025-03-18 16:40:50 +00:00
Merge branch 'unstable'
This commit is contained in:
commit
bfe85f7ca9
156
CLUSTER
Normal file
156
CLUSTER
Normal file
@ -0,0 +1,156 @@
|
||||
CLUSTER README
|
||||
==============
|
||||
|
||||
Redis Cluster is currenty a work in progress, however there are a few things
|
||||
that you can do already with it to see how it works.
|
||||
|
||||
The following guide show you how to setup a three nodes cluster and issue some
|
||||
basic command against it.
|
||||
|
||||
... WORK IN PROGRESS ...
|
||||
|
||||
1) Show MIGRATE
|
||||
2) Show CLUSTER MEET
|
||||
3) Show link status detection with CLUSTER NODES
|
||||
4) Show how to add slots with CLUSTER ADDSLOTS
|
||||
5) Show redirection
|
||||
6) Show cluster down
|
||||
|
||||
... WORK IN PROGRESS ...
|
||||
|
||||
TODO
|
||||
====
|
||||
|
||||
*** WARNING: all the following problably has some meaning only for
|
||||
*** me (antirez), most info are not updated, so please consider this file
|
||||
*** as a private TODO list / brainstorming.
|
||||
|
||||
- disconnect FAIL clients after some pong idle time.
|
||||
|
||||
---------------------------------
|
||||
|
||||
* Majority rule: the cluster con continue when there are all the hash slots covered AND when there are the majority of masters.
|
||||
* Shutdown on request rule: when a node sees many connections closed or even a timeout longer than usual on almost all the other nodes, it will usually wait for the normal timeout before to change the state, unless it receives a query from a client: in such a case it will put itself into error status.
|
||||
|
||||
--------------------------------
|
||||
|
||||
* When asked for a key that is not in a node's business it will reply:
|
||||
|
||||
-ASK 1.2.3.4:6379 (in case we want the client to ask just one time)
|
||||
-MOVED <slotid> 1.2.3.4:6379 (in case the hash slot is permanently moved)
|
||||
|
||||
So with -ASK a client should just retry the query against this new node, a single time.
|
||||
|
||||
With -MOVED the client should update its hash slots table to reflect the fact that now the specified node is the one to contact for the specified hash slot.
|
||||
|
||||
* Nodes communicate using a binary protocol.
|
||||
|
||||
* Node failure detection.
|
||||
|
||||
1) Every node contains information about all the other nodes:
|
||||
- If this node is believed to work ok or not
|
||||
- The hash slots for which this node is responsible
|
||||
- If the node is a master or a slave
|
||||
- If it is a slave, the slave of which node
|
||||
- if it is a master, the list of slave nodes
|
||||
- The slaves are ordered for "<ip>:<port>" string from lower to higher
|
||||
ordered lexicographically. When a master is down, the cluster will
|
||||
try to elect the first slave in the list.
|
||||
|
||||
2) Every node also contains the unix time where every other node was
|
||||
reported to work properly (that is, it replied to a ping or any other
|
||||
protocol request correctly). For every node we also store the timestamp
|
||||
at which we sent the latest ping, so we can easily compute the current
|
||||
lag.
|
||||
|
||||
3) From time to time a node pings a random node, selected among the nodes
|
||||
with the least recent "alive" time stamp. Three random nodes are selected
|
||||
and the one with lower alive time stamp is pinged.
|
||||
|
||||
4) The ping packet contains also information about a few random nodes
|
||||
alive time stamp. So that the receiver of the ping will update the
|
||||
alive table if the received alive timestamp is more recent the
|
||||
one present in the node local table.
|
||||
|
||||
In the ping packet every node "gossip" information is somethig like
|
||||
this:
|
||||
|
||||
<ip>:<port>:<status>:<pingsent_timestamp>:<pongreceived_timestamp>
|
||||
|
||||
status is OK, POSSIBLE_FAILURE, FAILURE.
|
||||
|
||||
5) The node replies to ping with a pong packet, that also contains a random
|
||||
selections of nodes timestamps.
|
||||
|
||||
A given node thinks another node may be in a failure state once there is a
|
||||
ping timeout bigger than 30 seconds (configurable).
|
||||
|
||||
When a possible failure is detected the node performs the following action:
|
||||
|
||||
1) Is the average between all the other nodes big? For instance bigger
|
||||
than 30 seconds / 2 = 15 seconds? Probably *we* are disconnected.
|
||||
In such a case we don't trust our lag data, and reset all the
|
||||
timestamps of sent ping to zero. This way when we'll reconnect there
|
||||
is no risk that we'll claim many nodes are down, taking inappropriate
|
||||
actions.
|
||||
|
||||
2) Messages from nodes marked as failed are *always* ignored by the other
|
||||
nodes. A new node needs to be "introduced" by a good online node.
|
||||
|
||||
3) If we are well connected (that is, condition "1" is not true) and a
|
||||
node timeout is > 30 seconds, we mark the node as POSSIBLE_FAILURE
|
||||
(a flat in the cluster node structure). Every time we sent a ping
|
||||
to another node we inform this other nodes that we detected this
|
||||
condition, as already stated.
|
||||
|
||||
4) Once a node receives a POSSIBLE_FAILURE status for a node that is
|
||||
already marked as POSSIBLE_FAILURE locally, it sends a message
|
||||
to all the other nodes of type NODE_FAILURE_DETECTED, communicating the
|
||||
ip/port of the specified node.
|
||||
|
||||
All the nodes need to update the status of this node setting it into
|
||||
FAILURE.
|
||||
|
||||
5) If the computer in FAILURE state is a master node, what is needed is
|
||||
to perform a Slave Election.
|
||||
|
||||
SLAVE ELECTION
|
||||
|
||||
1) The slave election is performed by the first slave (with slaves ordered
|
||||
lexicographically). Actually it is the first functioning slave, so if
|
||||
the first slave is marked as failing the next slave will perform the
|
||||
election and so forth. Such a slave is called the "Successor".
|
||||
|
||||
2) The Successor starts checking that all the nodes in the cluster already
|
||||
marked the master in FAILURE state. If at least one node does not agree
|
||||
no action is performed.
|
||||
|
||||
3) If all the nodes agree that the master is failing, the Successor does
|
||||
the following:
|
||||
|
||||
a) It will send a SUCCESSION message to all the other nodes, that will
|
||||
upgrade the hash slot tables accordingly. It will make sure that all
|
||||
the nodes are updated and if some node did not received the message
|
||||
it will keep trying.
|
||||
b) Once all nodes not marked as FAILURE accepted the SUCCESSION message
|
||||
it will update his own table and will start acting as a master
|
||||
accepting write queries.
|
||||
c) Every node receiving the succession message, if not already informed
|
||||
of the change will broadcast the same message to other three random
|
||||
nodes. No action is performed if the specified host was already marked
|
||||
as the master node.
|
||||
d) A node that was a slave of the original master that failed will
|
||||
switch master to the new one once the SUCCESSION message is received.
|
||||
|
||||
RANDOM
|
||||
|
||||
1) When selecting a slave, the system will try to pick one with an IP different than the master and other slaves, if possible.
|
||||
|
||||
2) The PING packet also contains information about the local configuration checksum. This is the SHA1 of the current configuration, without the bits that normally change form one node to another (like latest ping reply, failure status of nodes, and so forth). From time to time the local config SHA1 is checked against the list of the other nodes, and if there is a mismatch between our configuration and the most common one that lasts for more than N seconds, the most common configuration is asked and retrieved from another node. The event is logged.
|
||||
|
||||
3) Every time a node updates its internal cluster configuration, it dumps such a config in the cluster.conf file. On startup the configuration is reloaded.
|
||||
Nodes can share the cluster configuration when needed (for instance if SHA1 does not match) using this exact same format.
|
||||
|
||||
CLIENTS
|
||||
|
||||
- Clients may be configured to use slaves to perform reads, when read-after-write consistency is not required.
|
45
TODO
45
TODO
@ -9,18 +9,24 @@ WARNING: are you a possible Redis contributor?
|
||||
us, and *how* exactly this can be implemented to have good changes
|
||||
of a merge. Otherwise it is probably wasted work! Thank you
|
||||
|
||||
DISKSTORE TODO
|
||||
==============
|
||||
|
||||
* Check that 00/00 and ff/ff exist at startup, otherwise exit with error.
|
||||
* Implement sync flush option, where data is written synchronously on disk when a command is executed.
|
||||
* Implement MULTI/EXEC as transaction abstract API to diskstore.c, with transaction_start, transaction_end, and a journal to recover.
|
||||
* Stop BGSAVE thread on shutdown and any other condition where the child is killed during normal bgsave.
|
||||
* Fix RANDOMKEY to really do something interesting
|
||||
* Fix DBSIZE to really do something interesting
|
||||
* Add a DEBUG command to check if an entry is or not in memory currently
|
||||
API CHANGES
|
||||
===========
|
||||
|
||||
* dscache.c near 236, kobj = createStringObject... we could use static obj.
|
||||
* Turn commands into variadic versions when it makes sense, that is, when
|
||||
the variable number of arguments represent values, and there is no conflict
|
||||
with the return value of the command.
|
||||
|
||||
CLUSTER
|
||||
=======
|
||||
|
||||
* Implement rehashing and cluster check in redis-trib.
|
||||
* Reimplement MIGRATE / RESTORE to use just in memory buffers (no disk at
|
||||
all). This will require touching a lot of the RDB stuff around, but we may
|
||||
hand with faster persistence for RDB.
|
||||
* Implement the slave nodes semantics and election.
|
||||
* Allow redis-trib to create a cluster-wide snapshot (using SYNC).
|
||||
* Allow redis-trib to restore a cluster-wide snapshot (implement UPLOAD?).
|
||||
|
||||
APPEND ONLY FILE
|
||||
================
|
||||
@ -35,6 +41,8 @@ OPTIMIZATIONS
|
||||
* SORT: Don't copy the list into a vector when BY argument is constant.
|
||||
* Write the hash table size of every db in the dump, so that Redis can resize the hash table just one time when loading a big DB.
|
||||
* Read-only mode for slaves.
|
||||
* Redis big lists as linked lists of small ziplists?
|
||||
Possibly a simple heuristic that join near nodes when some node gets smaller than the low_level, and split it into two if gets bigger than high_level.
|
||||
|
||||
REPORTING
|
||||
=========
|
||||
@ -57,4 +65,21 @@ KNOWN BUGS
|
||||
What happens if between 1 and 2 for some reason (system under huge load
|
||||
or alike) too many time passes? We should prevent expires while the
|
||||
AOF is loading.
|
||||
* #519: Slave may have expired keys that were never read in the master (so a DEL
|
||||
is not sent in the replication channel) but are already expired since
|
||||
a lot of time. Maybe after a given delay that is undoubltly greater than
|
||||
the replication link latency we should expire this key on the slave on
|
||||
access?
|
||||
|
||||
DISKSTORE TODO
|
||||
==============
|
||||
|
||||
* Fix FLUSHALL/FLUSHDB: the queue of pending reads/writes should be handled.
|
||||
* Check that 00/00 and ff/ff exist at startup, otherwise exit with error.
|
||||
* Implement sync flush option, where data is written synchronously on disk when a command is executed.
|
||||
* Implement MULTI/EXEC as transaction abstract API to diskstore.c, with transaction_start, transaction_end, and a journal to recover.
|
||||
* Stop BGSAVE thread on shutdown and any other condition where the child is killed during normal bgsave.
|
||||
* Fix RANDOMKEY to really do something interesting
|
||||
* Fix DBSIZE to really do something interesting
|
||||
* Add a DEBUG command to check if an entry is or not in memory currently
|
||||
* dscache.c near 236, kobj = createStringObject... we could use static obj.
|
||||
|
11
deps/hiredis/Makefile
vendored
11
deps/hiredis/Makefile
vendored
@ -15,8 +15,9 @@ ifeq ($(uname_S),SunOS)
|
||||
DYLIB_MAKE_CMD?=$(CC) -G -o ${DYLIBNAME} ${OBJ}
|
||||
STLIBNAME?=libhiredis.a
|
||||
STLIB_MAKE_CMD?=ar rcs ${STLIBNAME} ${OBJ}
|
||||
else ifeq ($(uname_S),Darwin)
|
||||
CFLAGS?=-std=c99 -pedantic $(OPTIMIZATION) -fPIC -Wall -W -Wwrite-strings $(ARCH) $(PROF)
|
||||
else
|
||||
ifeq ($(uname_S),Darwin)
|
||||
CFLAGS?=-std=c99 -pedantic $(OPTIMIZATION) -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings $(ARCH) $(PROF)
|
||||
CCLINK?=-lm -pthread
|
||||
LDFLAGS?=-L. -Wl,-rpath,.
|
||||
OBJARCH?=-arch i386 -arch x86_64
|
||||
@ -25,7 +26,7 @@ else ifeq ($(uname_S),Darwin)
|
||||
STLIBNAME?=libhiredis.a
|
||||
STLIB_MAKE_CMD?=libtool -static -o ${STLIBNAME} - ${OBJ}
|
||||
else
|
||||
CFLAGS?=-std=c99 -pedantic $(OPTIMIZATION) -fPIC -Wall -W -Wwrite-strings $(ARCH) $(PROF)
|
||||
CFLAGS?=-std=c99 -pedantic $(OPTIMIZATION) -fPIC -Wall -W -Wstrict-prototypes -Wwrite-strings $(ARCH) $(PROF)
|
||||
CCLINK?=-lm -pthread
|
||||
LDFLAGS?=-L. -Wl,-rpath,.
|
||||
DYLIBNAME?=libhiredis.so
|
||||
@ -33,6 +34,8 @@ else
|
||||
STLIBNAME?=libhiredis.a
|
||||
STLIB_MAKE_CMD?=ar rcs ${STLIBNAME} ${OBJ}
|
||||
endif
|
||||
endif
|
||||
|
||||
CCOPT= $(CFLAGS) $(CCLINK)
|
||||
DEBUG?= -g -ggdb
|
||||
|
||||
@ -45,7 +48,7 @@ all: ${DYLIBNAME} ${BINS}
|
||||
|
||||
# Deps (use make dep to generate this)
|
||||
net.o: net.c fmacros.h net.h
|
||||
async.o: async.c async.h hiredis.h sds.h util.h
|
||||
async.o: async.c async.h hiredis.h sds.h util.h dict.c dict.h
|
||||
example.o: example.c hiredis.h
|
||||
hiredis.o: hiredis.c hiredis.h net.h sds.h util.h
|
||||
sds.o: sds.c sds.h
|
||||
|
4
deps/hiredis/README.md
vendored
4
deps/hiredis/README.md
vendored
@ -108,7 +108,7 @@ was received:
|
||||
* **`REDIS_REPLY_ARRAY`**:
|
||||
* A multi bulk reply. The number of elements in the multi bulk reply is stored in
|
||||
`reply->elements`. Every element in the multi bulk reply is a `redisReply` object as well
|
||||
and can be accessed via `reply->elements[..index..]`.
|
||||
and can be accessed via `reply->element[..index..]`.
|
||||
Redis may reply with nested arrays but this is fully supported.
|
||||
|
||||
Replies should be freed using the `freeReplyObject()` function.
|
||||
@ -171,7 +171,7 @@ the latter means an error occurred while reading a reply. Just as with the other
|
||||
the `err` field in the context can be used to find out what the cause of this error is.
|
||||
|
||||
The following examples shows a simple pipeline (resulting in only a single call to `write(2)` and
|
||||
a single call to `write(2)`):
|
||||
a single call to `read(2)`):
|
||||
|
||||
redisReply *reply;
|
||||
redisAppendCommand(context,"SET foo bar");
|
||||
|
334
deps/hiredis/async.c
vendored
334
deps/hiredis/async.c
vendored
@ -30,14 +30,58 @@
|
||||
*/
|
||||
|
||||
#include <string.h>
|
||||
#include <strings.h>
|
||||
#include <assert.h>
|
||||
#include <ctype.h>
|
||||
#include "async.h"
|
||||
#include "dict.c"
|
||||
#include "sds.h"
|
||||
#include "util.h"
|
||||
|
||||
/* Forward declaration of function in hiredis.c */
|
||||
void __redisAppendCommand(redisContext *c, char *cmd, size_t len);
|
||||
|
||||
/* Functions managing dictionary of callbacks for pub/sub. */
|
||||
static unsigned int callbackHash(const void *key) {
|
||||
return dictGenHashFunction((unsigned char*)key,sdslen((char*)key));
|
||||
}
|
||||
|
||||
static void *callbackValDup(void *privdata, const void *src) {
|
||||
((void) privdata);
|
||||
redisCallback *dup = malloc(sizeof(*dup));
|
||||
memcpy(dup,src,sizeof(*dup));
|
||||
return dup;
|
||||
}
|
||||
|
||||
static int callbackKeyCompare(void *privdata, const void *key1, const void *key2) {
|
||||
int l1, l2;
|
||||
((void) privdata);
|
||||
|
||||
l1 = sdslen((sds)key1);
|
||||
l2 = sdslen((sds)key2);
|
||||
if (l1 != l2) return 0;
|
||||
return memcmp(key1,key2,l1) == 0;
|
||||
}
|
||||
|
||||
static void callbackKeyDestructor(void *privdata, void *key) {
|
||||
((void) privdata);
|
||||
sdsfree((sds)key);
|
||||
}
|
||||
|
||||
static void callbackValDestructor(void *privdata, void *val) {
|
||||
((void) privdata);
|
||||
free(val);
|
||||
}
|
||||
|
||||
static dictType callbackDict = {
|
||||
callbackHash,
|
||||
NULL,
|
||||
callbackValDup,
|
||||
callbackKeyCompare,
|
||||
callbackKeyDestructor,
|
||||
callbackValDestructor
|
||||
};
|
||||
|
||||
static redisAsyncContext *redisAsyncInitialize(redisContext *c) {
|
||||
redisAsyncContext *ac = realloc(c,sizeof(redisAsyncContext));
|
||||
c = &(ac->c);
|
||||
@ -50,19 +94,23 @@ static redisAsyncContext *redisAsyncInitialize(redisContext *c) {
|
||||
ac->err = 0;
|
||||
ac->errstr = NULL;
|
||||
ac->data = NULL;
|
||||
ac->_adapter_data = NULL;
|
||||
|
||||
ac->evAddRead = NULL;
|
||||
ac->evDelRead = NULL;
|
||||
ac->evAddWrite = NULL;
|
||||
ac->evDelWrite = NULL;
|
||||
ac->evCleanup = NULL;
|
||||
ac->ev.data = NULL;
|
||||
ac->ev.addRead = NULL;
|
||||
ac->ev.delRead = NULL;
|
||||
ac->ev.addWrite = NULL;
|
||||
ac->ev.delWrite = NULL;
|
||||
ac->ev.cleanup = NULL;
|
||||
|
||||
ac->onConnect = NULL;
|
||||
ac->onDisconnect = NULL;
|
||||
|
||||
ac->replies.head = NULL;
|
||||
ac->replies.tail = NULL;
|
||||
ac->sub.invalid.head = NULL;
|
||||
ac->sub.invalid.tail = NULL;
|
||||
ac->sub.channels = dictCreate(&callbackDict,NULL);
|
||||
ac->sub.patterns = dictCreate(&callbackDict,NULL);
|
||||
return ac;
|
||||
}
|
||||
|
||||
@ -96,6 +144,11 @@ int redisAsyncSetReplyObjectFunctions(redisAsyncContext *ac, redisReplyObjectFun
|
||||
int redisAsyncSetConnectCallback(redisAsyncContext *ac, redisConnectCallback *fn) {
|
||||
if (ac->onConnect == NULL) {
|
||||
ac->onConnect = fn;
|
||||
|
||||
/* The common way to detect an established connection is to wait for
|
||||
* the first write event to be fired. This assumes the related event
|
||||
* library functions are already set. */
|
||||
if (ac->ev.addWrite) ac->ev.addWrite(ac->ev.data);
|
||||
return REDIS_OK;
|
||||
}
|
||||
return REDIS_ERR;
|
||||
@ -114,11 +167,11 @@ static int __redisPushCallback(redisCallbackList *list, redisCallback *source) {
|
||||
redisCallback *cb;
|
||||
|
||||
/* Copy callback from stack to heap */
|
||||
cb = calloc(1,sizeof(*cb));
|
||||
cb = malloc(sizeof(*cb));
|
||||
if (!cb) redisOOM();
|
||||
if (source != NULL) {
|
||||
cb->fn = source->fn;
|
||||
cb->privdata = source->privdata;
|
||||
memcpy(cb,source,sizeof(*cb));
|
||||
cb->next = NULL;
|
||||
}
|
||||
|
||||
/* Store callback in list */
|
||||
@ -146,51 +199,150 @@ static int __redisShiftCallback(redisCallbackList *list, redisCallback *target)
|
||||
return REDIS_ERR;
|
||||
}
|
||||
|
||||
/* Tries to do a clean disconnect from Redis, meaning it stops new commands
|
||||
* from being issued, but tries to flush the output buffer and execute
|
||||
* callbacks for all remaining replies.
|
||||
*
|
||||
* This functions is generally called from within a callback, so the
|
||||
* processCallbacks function will pick up the flag when there are no
|
||||
* more replies. */
|
||||
void redisAsyncDisconnect(redisAsyncContext *ac) {
|
||||
static void __redisRunCallback(redisAsyncContext *ac, redisCallback *cb, redisReply *reply) {
|
||||
redisContext *c = &(ac->c);
|
||||
c->flags |= REDIS_DISCONNECTING;
|
||||
if (cb->fn != NULL) {
|
||||
c->flags |= REDIS_IN_CALLBACK;
|
||||
cb->fn(ac,reply,cb->privdata);
|
||||
c->flags &= ~REDIS_IN_CALLBACK;
|
||||
}
|
||||
}
|
||||
|
||||
/* Helper function to free the context. */
|
||||
static void __redisAsyncFree(redisAsyncContext *ac) {
|
||||
redisContext *c = &(ac->c);
|
||||
redisCallback cb;
|
||||
dictIterator *it;
|
||||
dictEntry *de;
|
||||
|
||||
/* Execute pending callbacks with NULL reply. */
|
||||
while (__redisShiftCallback(&ac->replies,&cb) == REDIS_OK)
|
||||
__redisRunCallback(ac,&cb,NULL);
|
||||
|
||||
/* Execute callbacks for invalid commands */
|
||||
while (__redisShiftCallback(&ac->sub.invalid,&cb) == REDIS_OK)
|
||||
__redisRunCallback(ac,&cb,NULL);
|
||||
|
||||
/* Run subscription callbacks callbacks with NULL reply */
|
||||
it = dictGetIterator(ac->sub.channels);
|
||||
while ((de = dictNext(it)) != NULL)
|
||||
__redisRunCallback(ac,dictGetEntryVal(de),NULL);
|
||||
dictReleaseIterator(it);
|
||||
dictRelease(ac->sub.channels);
|
||||
|
||||
it = dictGetIterator(ac->sub.patterns);
|
||||
while ((de = dictNext(it)) != NULL)
|
||||
__redisRunCallback(ac,dictGetEntryVal(de),NULL);
|
||||
dictReleaseIterator(it);
|
||||
dictRelease(ac->sub.patterns);
|
||||
|
||||
/* Signal event lib to clean up */
|
||||
if (ac->ev.cleanup) ac->ev.cleanup(ac->ev.data);
|
||||
|
||||
/* Execute disconnect callback. When redisAsyncFree() initiated destroying
|
||||
* this context, the status will always be REDIS_OK. */
|
||||
if (ac->onDisconnect && (c->flags & REDIS_CONNECTED)) {
|
||||
if (c->flags & REDIS_FREEING) {
|
||||
ac->onDisconnect(ac,REDIS_OK);
|
||||
} else {
|
||||
ac->onDisconnect(ac,(ac->err == 0) ? REDIS_OK : REDIS_ERR);
|
||||
}
|
||||
}
|
||||
|
||||
/* Cleanup self */
|
||||
redisFree(c);
|
||||
}
|
||||
|
||||
/* Free the async context. When this function is called from a callback,
|
||||
* control needs to be returned to redisProcessCallbacks() before actual
|
||||
* free'ing. To do so, a flag is set on the context which is picked up by
|
||||
* redisProcessCallbacks(). Otherwise, the context is immediately free'd. */
|
||||
void redisAsyncFree(redisAsyncContext *ac) {
|
||||
redisContext *c = &(ac->c);
|
||||
c->flags |= REDIS_FREEING;
|
||||
if (!(c->flags & REDIS_IN_CALLBACK))
|
||||
__redisAsyncFree(ac);
|
||||
}
|
||||
|
||||
/* Helper function to make the disconnect happen and clean up. */
|
||||
static void __redisAsyncDisconnect(redisAsyncContext *ac) {
|
||||
redisContext *c = &(ac->c);
|
||||
redisCallback cb;
|
||||
int status;
|
||||
|
||||
/* Make sure error is accessible if there is any */
|
||||
__redisAsyncCopyError(ac);
|
||||
status = (ac->err == 0) ? REDIS_OK : REDIS_ERR;
|
||||
|
||||
if (status == REDIS_OK) {
|
||||
/* When the connection is cleanly disconnected, there should not
|
||||
* be pending callbacks. */
|
||||
if (ac->err == 0) {
|
||||
/* For clean disconnects, there should be no pending callbacks. */
|
||||
assert(__redisShiftCallback(&ac->replies,NULL) == REDIS_ERR);
|
||||
} else {
|
||||
/* Callbacks should not be able to issue new commands. */
|
||||
/* Disconnection is caused by an error, make sure that pending
|
||||
* callbacks cannot call new commands. */
|
||||
c->flags |= REDIS_DISCONNECTING;
|
||||
|
||||
/* Execute pending callbacks with NULL reply. */
|
||||
while (__redisShiftCallback(&ac->replies,&cb) == REDIS_OK) {
|
||||
if (cb.fn != NULL)
|
||||
cb.fn(ac,NULL,cb.privdata);
|
||||
}
|
||||
}
|
||||
|
||||
/* Signal event lib to clean up */
|
||||
if (ac->evCleanup) ac->evCleanup(ac->_adapter_data);
|
||||
/* For non-clean disconnects, __redisAsyncFree() will execute pending
|
||||
* callbacks with a NULL-reply. */
|
||||
__redisAsyncFree(ac);
|
||||
}
|
||||
|
||||
/* Execute callback with proper status */
|
||||
if (ac->onDisconnect) ac->onDisconnect(ac,status);
|
||||
/* Tries to do a clean disconnect from Redis, meaning it stops new commands
|
||||
* from being issued, but tries to flush the output buffer and execute
|
||||
* callbacks for all remaining replies. When this function is called from a
|
||||
* callback, there might be more replies and we can safely defer disconnecting
|
||||
* to redisProcessCallbacks(). Otherwise, we can only disconnect immediately
|
||||
* when there are no pending callbacks. */
|
||||
void redisAsyncDisconnect(redisAsyncContext *ac) {
|
||||
redisContext *c = &(ac->c);
|
||||
c->flags |= REDIS_DISCONNECTING;
|
||||
if (!(c->flags & REDIS_IN_CALLBACK) && ac->replies.head == NULL)
|
||||
__redisAsyncDisconnect(ac);
|
||||
}
|
||||
|
||||
/* Cleanup self */
|
||||
redisFree(c);
|
||||
static int __redisGetSubscribeCallback(redisAsyncContext *ac, redisReply *reply, redisCallback *dstcb) {
|
||||
redisContext *c = &(ac->c);
|
||||
dict *callbacks;
|
||||
dictEntry *de;
|
||||
int pvariant;
|
||||
char *stype;
|
||||
sds sname;
|
||||
|
||||
/* Custom reply functions are not supported for pub/sub. This will fail
|
||||
* very hard when they are used... */
|
||||
if (reply->type == REDIS_REPLY_ARRAY) {
|
||||
assert(reply->elements >= 2);
|
||||
assert(reply->element[0]->type == REDIS_REPLY_STRING);
|
||||
stype = reply->element[0]->str;
|
||||
pvariant = (tolower(stype[0]) == 'p') ? 1 : 0;
|
||||
|
||||
if (pvariant)
|
||||
callbacks = ac->sub.patterns;
|
||||
else
|
||||
callbacks = ac->sub.channels;
|
||||
|
||||
/* Locate the right callback */
|
||||
assert(reply->element[1]->type == REDIS_REPLY_STRING);
|
||||
sname = sdsnewlen(reply->element[1]->str,reply->element[1]->len);
|
||||
de = dictFind(callbacks,sname);
|
||||
if (de != NULL) {
|
||||
memcpy(dstcb,dictGetEntryVal(de),sizeof(*dstcb));
|
||||
|
||||
/* If this is an unsubscribe message, remove it. */
|
||||
if (strcasecmp(stype+pvariant,"unsubscribe") == 0) {
|
||||
dictDelete(callbacks,sname);
|
||||
|
||||
/* If this was the last unsubscribe message, revert to
|
||||
* non-subscribe mode. */
|
||||
assert(reply->element[2]->type == REDIS_REPLY_INTEGER);
|
||||
if (reply->element[2]->integer == 0)
|
||||
c->flags &= ~REDIS_SUBSCRIBED;
|
||||
}
|
||||
}
|
||||
sdsfree(sname);
|
||||
} else {
|
||||
/* Shift callback for invalid commands. */
|
||||
__redisShiftCallback(&ac->sub.invalid,dstcb);
|
||||
}
|
||||
return REDIS_OK;
|
||||
}
|
||||
|
||||
void redisProcessCallbacks(redisAsyncContext *ac) {
|
||||
@ -213,11 +365,28 @@ void redisProcessCallbacks(redisAsyncContext *ac) {
|
||||
break;
|
||||
}
|
||||
|
||||
/* Shift callback and execute it */
|
||||
assert(__redisShiftCallback(&ac->replies,&cb) == REDIS_OK);
|
||||
/* 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) {
|
||||
/* No more regular callbacks, the context *must* be subscribed. */
|
||||
assert(c->flags & REDIS_SUBSCRIBED);
|
||||
__redisGetSubscribeCallback(ac,reply,&cb);
|
||||
}
|
||||
|
||||
if (cb.fn != NULL) {
|
||||
cb.fn(ac,reply,cb.privdata);
|
||||
__redisRunCallback(ac,&cb,reply);
|
||||
c->fn->freeObject(reply);
|
||||
|
||||
/* Proceed with free'ing when redisAsyncFree() was called. */
|
||||
if (c->flags & REDIS_FREEING) {
|
||||
__redisAsyncFree(ac);
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
/* No callback for this reply. This can either be a NULL callback,
|
||||
* or there were no callbacks to begin with. Either way, don't
|
||||
* abort with an error, but simply ignore it because the client
|
||||
* doesn't know what the server will spit out over the wire. */
|
||||
c->fn->freeObject(reply);
|
||||
}
|
||||
}
|
||||
@ -237,7 +406,7 @@ void redisAsyncHandleRead(redisAsyncContext *ac) {
|
||||
__redisAsyncDisconnect(ac);
|
||||
} else {
|
||||
/* Always re-schedule reads */
|
||||
if (ac->evAddRead) ac->evAddRead(ac->_adapter_data);
|
||||
if (ac->ev.addRead) ac->ev.addRead(ac->ev.data);
|
||||
redisProcessCallbacks(ac);
|
||||
}
|
||||
}
|
||||
@ -251,13 +420,13 @@ void redisAsyncHandleWrite(redisAsyncContext *ac) {
|
||||
} else {
|
||||
/* Continue writing when not done, stop writing otherwise */
|
||||
if (!done) {
|
||||
if (ac->evAddWrite) ac->evAddWrite(ac->_adapter_data);
|
||||
if (ac->ev.addWrite) ac->ev.addWrite(ac->ev.data);
|
||||
} else {
|
||||
if (ac->evDelWrite) ac->evDelWrite(ac->_adapter_data);
|
||||
if (ac->ev.delWrite) ac->ev.delWrite(ac->ev.data);
|
||||
}
|
||||
|
||||
/* Always schedule reads after writes */
|
||||
if (ac->evAddRead) ac->evAddRead(ac->_adapter_data);
|
||||
if (ac->ev.addRead) ac->ev.addRead(ac->ev.data);
|
||||
|
||||
/* Fire onConnect when this is the first write event. */
|
||||
if (!(c->flags & REDIS_CONNECTED)) {
|
||||
@ -267,26 +436,81 @@ void redisAsyncHandleWrite(redisAsyncContext *ac) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Helper function for the redisAsyncCommand* family of functions.
|
||||
*
|
||||
* Write a formatted command to the output buffer and register the provided
|
||||
* callback function with the context.
|
||||
*/
|
||||
/* Sets a pointer to the first argument and its length starting at p. Returns
|
||||
* the number of bytes to skip to get to the following argument. */
|
||||
static char *nextArgument(char *start, char **str, size_t *len) {
|
||||
char *p = start;
|
||||
if (p[0] != '$') {
|
||||
p = strchr(p,'$');
|
||||
if (p == NULL) return NULL;
|
||||
}
|
||||
|
||||
*len = (int)strtol(p+1,NULL,10);
|
||||
p = strchr(p,'\r');
|
||||
assert(p);
|
||||
*str = p+2;
|
||||
return p+2+(*len)+2;
|
||||
}
|
||||
|
||||
/* Helper function for the redisAsyncCommand* family of functions. Writes a
|
||||
* formatted command to the output buffer and registers the provided callback
|
||||
* function with the context. */
|
||||
static int __redisAsyncCommand(redisAsyncContext *ac, redisCallbackFn *fn, void *privdata, char *cmd, size_t len) {
|
||||
redisContext *c = &(ac->c);
|
||||
redisCallback cb;
|
||||
int pvariant, hasnext;
|
||||
char *cstr, *astr;
|
||||
size_t clen, alen;
|
||||
char *p;
|
||||
sds sname;
|
||||
|
||||
/* Don't accept new commands when the connection is lazily closed. */
|
||||
if (c->flags & REDIS_DISCONNECTING) return REDIS_ERR;
|
||||
__redisAppendCommand(c,cmd,len);
|
||||
/* Don't accept new commands when the connection is about to be closed. */
|
||||
if (c->flags & (REDIS_DISCONNECTING | REDIS_FREEING)) return REDIS_ERR;
|
||||
|
||||
/* Store callback */
|
||||
/* Setup callback */
|
||||
cb.fn = fn;
|
||||
cb.privdata = privdata;
|
||||
__redisPushCallback(&ac->replies,&cb);
|
||||
|
||||
/* Find out which command will be appended. */
|
||||
p = nextArgument(cmd,&cstr,&clen);
|
||||
assert(p != NULL);
|
||||
hasnext = (p[0] == '$');
|
||||
pvariant = (tolower(cstr[0]) == 'p') ? 1 : 0;
|
||||
cstr += pvariant;
|
||||
clen -= pvariant;
|
||||
|
||||
if (hasnext && strncasecmp(cstr,"subscribe\r\n",11) == 0) {
|
||||
c->flags |= REDIS_SUBSCRIBED;
|
||||
|
||||
/* Add every channel/pattern to the list of subscription callbacks. */
|
||||
while ((p = nextArgument(p,&astr,&alen)) != NULL) {
|
||||
sname = sdsnewlen(astr,alen);
|
||||
if (pvariant)
|
||||
dictReplace(ac->sub.patterns,sname,&cb);
|
||||
else
|
||||
dictReplace(ac->sub.channels,sname,&cb);
|
||||
}
|
||||
} else if (strncasecmp(cstr,"unsubscribe\r\n",13) == 0) {
|
||||
/* It is only useful to call (P)UNSUBSCRIBE when the context is
|
||||
* subscribed to one or more channels or patterns. */
|
||||
if (!(c->flags & REDIS_SUBSCRIBED)) return REDIS_ERR;
|
||||
|
||||
/* (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 (c->flags & REDIS_SUBSCRIBED)
|
||||
/* This will likely result in an error reply, but it needs to be
|
||||
* received and passed to the callback. */
|
||||
__redisPushCallback(&ac->sub.invalid,&cb);
|
||||
else
|
||||
__redisPushCallback(&ac->replies,&cb);
|
||||
}
|
||||
|
||||
__redisAppendCommand(c,cmd,len);
|
||||
|
||||
/* Always schedule a write when the write buffer is non-empty */
|
||||
if (ac->evAddWrite) ac->evAddWrite(ac->_adapter_data);
|
||||
if (ac->ev.addWrite) ac->ev.addWrite(ac->ev.data);
|
||||
|
||||
return REDIS_OK;
|
||||
}
|
||||
|
34
deps/hiredis/async.h
vendored
34
deps/hiredis/async.h
vendored
@ -1,5 +1,7 @@
|
||||
/*
|
||||
* Copyright (c) 2009-2010, Salvatore Sanfilippo <antirez at gmail dot com>
|
||||
* Copyright (c) 2010, Pieter Noordhuis <pcnoordhuis at gmail dot com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@ -36,6 +38,7 @@ extern "C" {
|
||||
#endif
|
||||
|
||||
struct redisAsyncContext; /* need forward declaration of redisAsyncContext */
|
||||
struct dict; /* dictionary header is included in async.c */
|
||||
|
||||
/* Reply callback prototype and container */
|
||||
typedef void (redisCallbackFn)(struct redisAsyncContext*, void*, void*);
|
||||
@ -66,16 +69,18 @@ typedef struct redisAsyncContext {
|
||||
/* Not used by hiredis */
|
||||
void *data;
|
||||
|
||||
/* Used by the different event lib adapters to store their private data */
|
||||
void *_adapter_data;
|
||||
/* Event library data and hooks */
|
||||
struct {
|
||||
void *data;
|
||||
|
||||
/* Called when the library expects to start reading/writing.
|
||||
* The supplied functions should be idempotent. */
|
||||
void (*evAddRead)(void *privdata);
|
||||
void (*evDelRead)(void *privdata);
|
||||
void (*evAddWrite)(void *privdata);
|
||||
void (*evDelWrite)(void *privdata);
|
||||
void (*evCleanup)(void *privdata);
|
||||
/* Hooks that are called when the library expects to start
|
||||
* reading/writing. These functions should be idempotent. */
|
||||
void (*addRead)(void *privdata);
|
||||
void (*delRead)(void *privdata);
|
||||
void (*addWrite)(void *privdata);
|
||||
void (*delWrite)(void *privdata);
|
||||
void (*cleanup)(void *privdata);
|
||||
} ev;
|
||||
|
||||
/* Called when either the connection is terminated due to an error or per
|
||||
* user request. The status is set accordingly (REDIS_OK, REDIS_ERR). */
|
||||
@ -84,16 +89,25 @@ typedef struct redisAsyncContext {
|
||||
/* Called when the first write event was received. */
|
||||
redisConnectCallback *onConnect;
|
||||
|
||||
/* Reply callbacks */
|
||||
/* Regular command callbacks */
|
||||
redisCallbackList replies;
|
||||
|
||||
/* Subscription callbacks */
|
||||
struct {
|
||||
redisCallbackList invalid;
|
||||
struct dict *channels;
|
||||
struct dict *patterns;
|
||||
} sub;
|
||||
} redisAsyncContext;
|
||||
|
||||
/* Functions that proxy to hiredis */
|
||||
redisAsyncContext *redisAsyncConnect(const char *ip, int port);
|
||||
redisAsyncContext *redisAsyncConnectUnix(const char *path);
|
||||
int redisAsyncSetReplyObjectFunctions(redisAsyncContext *ac, redisReplyObjectFunctions *fn);
|
||||
int redisAsyncSetConnectCallback(redisAsyncContext *ac, redisConnectCallback *fn);
|
||||
int redisAsyncSetDisconnectCallback(redisAsyncContext *ac, redisDisconnectCallback *fn);
|
||||
void redisAsyncDisconnect(redisAsyncContext *ac);
|
||||
void redisAsyncFree(redisAsyncContext *ac);
|
||||
|
||||
/* Handle read/write events */
|
||||
void redisAsyncHandleRead(redisAsyncContext *ac);
|
||||
|
338
deps/hiredis/dict.c
vendored
Normal file
338
deps/hiredis/dict.c
vendored
Normal file
@ -0,0 +1,338 @@
|
||||
/* Hash table implementation.
|
||||
*
|
||||
* 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>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of Redis nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "fmacros.h"
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include "dict.h"
|
||||
|
||||
/* -------------------------- private prototypes ---------------------------- */
|
||||
|
||||
static int _dictExpandIfNeeded(dict *ht);
|
||||
static unsigned long _dictNextPower(unsigned long size);
|
||||
static int _dictKeyIndex(dict *ht, const void *key);
|
||||
static int _dictInit(dict *ht, dictType *type, void *privDataPtr);
|
||||
|
||||
/* -------------------------- hash functions -------------------------------- */
|
||||
|
||||
/* Generic hash function (a popular one from Bernstein).
|
||||
* I tested a few and this was the best. */
|
||||
static unsigned int dictGenHashFunction(const unsigned char *buf, int len) {
|
||||
unsigned int hash = 5381;
|
||||
|
||||
while (len--)
|
||||
hash = ((hash << 5) + hash) + (*buf++); /* hash * 33 + c */
|
||||
return hash;
|
||||
}
|
||||
|
||||
/* ----------------------------- API implementation ------------------------- */
|
||||
|
||||
/* Reset an hashtable already initialized with ht_init().
|
||||
* NOTE: This function should only called by ht_destroy(). */
|
||||
static void _dictReset(dict *ht) {
|
||||
ht->table = NULL;
|
||||
ht->size = 0;
|
||||
ht->sizemask = 0;
|
||||
ht->used = 0;
|
||||
}
|
||||
|
||||
/* Create a new hash table */
|
||||
static dict *dictCreate(dictType *type, void *privDataPtr) {
|
||||
dict *ht = malloc(sizeof(*ht));
|
||||
_dictInit(ht,type,privDataPtr);
|
||||
return ht;
|
||||
}
|
||||
|
||||
/* Initialize the hash table */
|
||||
static int _dictInit(dict *ht, dictType *type, void *privDataPtr) {
|
||||
_dictReset(ht);
|
||||
ht->type = type;
|
||||
ht->privdata = privDataPtr;
|
||||
return DICT_OK;
|
||||
}
|
||||
|
||||
/* Expand or create the hashtable */
|
||||
static int dictExpand(dict *ht, unsigned long size) {
|
||||
dict n; /* the new hashtable */
|
||||
unsigned long realsize = _dictNextPower(size), i;
|
||||
|
||||
/* the size is invalid if it is smaller than the number of
|
||||
* elements already inside the hashtable */
|
||||
if (ht->used > size)
|
||||
return DICT_ERR;
|
||||
|
||||
_dictInit(&n, ht->type, ht->privdata);
|
||||
n.size = realsize;
|
||||
n.sizemask = realsize-1;
|
||||
n.table = calloc(realsize,sizeof(dictEntry*));
|
||||
|
||||
/* Copy all the elements from the old to the new table:
|
||||
* note that if the old hash table is empty ht->size is zero,
|
||||
* so dictExpand just creates an hash table. */
|
||||
n.used = ht->used;
|
||||
for (i = 0; i < ht->size && ht->used > 0; i++) {
|
||||
dictEntry *he, *nextHe;
|
||||
|
||||
if (ht->table[i] == NULL) continue;
|
||||
|
||||
/* For each hash entry on this slot... */
|
||||
he = ht->table[i];
|
||||
while(he) {
|
||||
unsigned int h;
|
||||
|
||||
nextHe = he->next;
|
||||
/* Get the new element index */
|
||||
h = dictHashKey(ht, he->key) & n.sizemask;
|
||||
he->next = n.table[h];
|
||||
n.table[h] = he;
|
||||
ht->used--;
|
||||
/* Pass to the next element */
|
||||
he = nextHe;
|
||||
}
|
||||
}
|
||||
assert(ht->used == 0);
|
||||
free(ht->table);
|
||||
|
||||
/* Remap the new hashtable in the old */
|
||||
*ht = n;
|
||||
return DICT_OK;
|
||||
}
|
||||
|
||||
/* Add an element to the target hash table */
|
||||
static int dictAdd(dict *ht, void *key, void *val) {
|
||||
int index;
|
||||
dictEntry *entry;
|
||||
|
||||
/* Get the index of the new element, or -1 if
|
||||
* the element already exists. */
|
||||
if ((index = _dictKeyIndex(ht, key)) == -1)
|
||||
return DICT_ERR;
|
||||
|
||||
/* Allocates the memory and stores key */
|
||||
entry = malloc(sizeof(*entry));
|
||||
entry->next = ht->table[index];
|
||||
ht->table[index] = entry;
|
||||
|
||||
/* Set the hash entry fields. */
|
||||
dictSetHashKey(ht, entry, key);
|
||||
dictSetHashVal(ht, entry, val);
|
||||
ht->used++;
|
||||
return DICT_OK;
|
||||
}
|
||||
|
||||
/* Add an element, discarding the old if the key already exists.
|
||||
* Return 1 if the key was added from scratch, 0 if there was already an
|
||||
* element with such key and dictReplace() just performed a value update
|
||||
* operation. */
|
||||
static int dictReplace(dict *ht, void *key, void *val) {
|
||||
dictEntry *entry, auxentry;
|
||||
|
||||
/* Try to add the element. If the key
|
||||
* does not exists dictAdd will suceed. */
|
||||
if (dictAdd(ht, key, val) == DICT_OK)
|
||||
return 1;
|
||||
/* It already exists, get the entry */
|
||||
entry = dictFind(ht, key);
|
||||
/* Free the old value and set the new one */
|
||||
/* Set the new value and free the old one. Note that it is important
|
||||
* to do that in this order, as the value may just be exactly the same
|
||||
* as the previous one. In this context, think to reference counting,
|
||||
* you want to increment (set), and then decrement (free), and not the
|
||||
* reverse. */
|
||||
auxentry = *entry;
|
||||
dictSetHashVal(ht, entry, val);
|
||||
dictFreeEntryVal(ht, &auxentry);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Search and remove an element */
|
||||
static int dictDelete(dict *ht, const void *key) {
|
||||
unsigned int h;
|
||||
dictEntry *de, *prevde;
|
||||
|
||||
if (ht->size == 0)
|
||||
return DICT_ERR;
|
||||
h = dictHashKey(ht, key) & ht->sizemask;
|
||||
de = ht->table[h];
|
||||
|
||||
prevde = NULL;
|
||||
while(de) {
|
||||
if (dictCompareHashKeys(ht,key,de->key)) {
|
||||
/* Unlink the element from the list */
|
||||
if (prevde)
|
||||
prevde->next = de->next;
|
||||
else
|
||||
ht->table[h] = de->next;
|
||||
|
||||
dictFreeEntryKey(ht,de);
|
||||
dictFreeEntryVal(ht,de);
|
||||
free(de);
|
||||
ht->used--;
|
||||
return DICT_OK;
|
||||
}
|
||||
prevde = de;
|
||||
de = de->next;
|
||||
}
|
||||
return DICT_ERR; /* not found */
|
||||
}
|
||||
|
||||
/* Destroy an entire hash table */
|
||||
static int _dictClear(dict *ht) {
|
||||
unsigned long i;
|
||||
|
||||
/* Free all the elements */
|
||||
for (i = 0; i < ht->size && ht->used > 0; i++) {
|
||||
dictEntry *he, *nextHe;
|
||||
|
||||
if ((he = ht->table[i]) == NULL) continue;
|
||||
while(he) {
|
||||
nextHe = he->next;
|
||||
dictFreeEntryKey(ht, he);
|
||||
dictFreeEntryVal(ht, he);
|
||||
free(he);
|
||||
ht->used--;
|
||||
he = nextHe;
|
||||
}
|
||||
}
|
||||
/* Free the table and the allocated cache structure */
|
||||
free(ht->table);
|
||||
/* Re-initialize the table */
|
||||
_dictReset(ht);
|
||||
return DICT_OK; /* never fails */
|
||||
}
|
||||
|
||||
/* Clear & Release the hash table */
|
||||
static void dictRelease(dict *ht) {
|
||||
_dictClear(ht);
|
||||
free(ht);
|
||||
}
|
||||
|
||||
static dictEntry *dictFind(dict *ht, const void *key) {
|
||||
dictEntry *he;
|
||||
unsigned int h;
|
||||
|
||||
if (ht->size == 0) return NULL;
|
||||
h = dictHashKey(ht, key) & ht->sizemask;
|
||||
he = ht->table[h];
|
||||
while(he) {
|
||||
if (dictCompareHashKeys(ht, key, he->key))
|
||||
return he;
|
||||
he = he->next;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static dictIterator *dictGetIterator(dict *ht) {
|
||||
dictIterator *iter = malloc(sizeof(*iter));
|
||||
|
||||
iter->ht = ht;
|
||||
iter->index = -1;
|
||||
iter->entry = NULL;
|
||||
iter->nextEntry = NULL;
|
||||
return iter;
|
||||
}
|
||||
|
||||
static dictEntry *dictNext(dictIterator *iter) {
|
||||
while (1) {
|
||||
if (iter->entry == NULL) {
|
||||
iter->index++;
|
||||
if (iter->index >=
|
||||
(signed)iter->ht->size) break;
|
||||
iter->entry = iter->ht->table[iter->index];
|
||||
} else {
|
||||
iter->entry = iter->nextEntry;
|
||||
}
|
||||
if (iter->entry) {
|
||||
/* We need to save the 'next' here, the iterator user
|
||||
* may delete the entry we are returning. */
|
||||
iter->nextEntry = iter->entry->next;
|
||||
return iter->entry;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void dictReleaseIterator(dictIterator *iter) {
|
||||
free(iter);
|
||||
}
|
||||
|
||||
/* ------------------------- private functions ------------------------------ */
|
||||
|
||||
/* Expand the hash table if needed */
|
||||
static int _dictExpandIfNeeded(dict *ht) {
|
||||
/* If the hash table is empty expand it to the intial size,
|
||||
* if the table is "full" dobule its size. */
|
||||
if (ht->size == 0)
|
||||
return dictExpand(ht, DICT_HT_INITIAL_SIZE);
|
||||
if (ht->used == ht->size)
|
||||
return dictExpand(ht, ht->size*2);
|
||||
return DICT_OK;
|
||||
}
|
||||
|
||||
/* Our hash table capability is a power of two */
|
||||
static unsigned long _dictNextPower(unsigned long size) {
|
||||
unsigned long i = DICT_HT_INITIAL_SIZE;
|
||||
|
||||
if (size >= LONG_MAX) return LONG_MAX;
|
||||
while(1) {
|
||||
if (i >= size)
|
||||
return i;
|
||||
i *= 2;
|
||||
}
|
||||
}
|
||||
|
||||
/* Returns the index of a free slot that can be populated with
|
||||
* an hash entry for the given 'key'.
|
||||
* If the key already exists, -1 is returned. */
|
||||
static int _dictKeyIndex(dict *ht, const void *key) {
|
||||
unsigned int h;
|
||||
dictEntry *he;
|
||||
|
||||
/* Expand the hashtable if needed */
|
||||
if (_dictExpandIfNeeded(ht) == DICT_ERR)
|
||||
return -1;
|
||||
/* Compute the key hash value */
|
||||
h = dictHashKey(ht, key) & ht->sizemask;
|
||||
/* Search if this slot does not already contain the given key */
|
||||
he = ht->table[h];
|
||||
while(he) {
|
||||
if (dictCompareHashKeys(ht, key, he->key))
|
||||
return -1;
|
||||
he = he->next;
|
||||
}
|
||||
return h;
|
||||
}
|
||||
|
126
deps/hiredis/dict.h
vendored
Normal file
126
deps/hiredis/dict.h
vendored
Normal file
@ -0,0 +1,126 @@
|
||||
/* Hash table implementation.
|
||||
*
|
||||
* 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>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of Redis nor the names of its contributors may be used
|
||||
* to endorse or promote products derived from this software without
|
||||
* specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef __DICT_H
|
||||
#define __DICT_H
|
||||
|
||||
#define DICT_OK 0
|
||||
#define DICT_ERR 1
|
||||
|
||||
/* Unused arguments generate annoying warnings... */
|
||||
#define DICT_NOTUSED(V) ((void) V)
|
||||
|
||||
typedef struct dictEntry {
|
||||
void *key;
|
||||
void *val;
|
||||
struct dictEntry *next;
|
||||
} dictEntry;
|
||||
|
||||
typedef struct dictType {
|
||||
unsigned int (*hashFunction)(const void *key);
|
||||
void *(*keyDup)(void *privdata, const void *key);
|
||||
void *(*valDup)(void *privdata, const void *obj);
|
||||
int (*keyCompare)(void *privdata, const void *key1, const void *key2);
|
||||
void (*keyDestructor)(void *privdata, void *key);
|
||||
void (*valDestructor)(void *privdata, void *obj);
|
||||
} dictType;
|
||||
|
||||
typedef struct dict {
|
||||
dictEntry **table;
|
||||
dictType *type;
|
||||
unsigned long size;
|
||||
unsigned long sizemask;
|
||||
unsigned long used;
|
||||
void *privdata;
|
||||
} dict;
|
||||
|
||||
typedef struct dictIterator {
|
||||
dict *ht;
|
||||
int index;
|
||||
dictEntry *entry, *nextEntry;
|
||||
} dictIterator;
|
||||
|
||||
/* This is the initial size of every hash table */
|
||||
#define DICT_HT_INITIAL_SIZE 4
|
||||
|
||||
/* ------------------------------- Macros ------------------------------------*/
|
||||
#define dictFreeEntryVal(ht, entry) \
|
||||
if ((ht)->type->valDestructor) \
|
||||
(ht)->type->valDestructor((ht)->privdata, (entry)->val)
|
||||
|
||||
#define dictSetHashVal(ht, entry, _val_) do { \
|
||||
if ((ht)->type->valDup) \
|
||||
entry->val = (ht)->type->valDup((ht)->privdata, _val_); \
|
||||
else \
|
||||
entry->val = (_val_); \
|
||||
} while(0)
|
||||
|
||||
#define dictFreeEntryKey(ht, entry) \
|
||||
if ((ht)->type->keyDestructor) \
|
||||
(ht)->type->keyDestructor((ht)->privdata, (entry)->key)
|
||||
|
||||
#define dictSetHashKey(ht, entry, _key_) do { \
|
||||
if ((ht)->type->keyDup) \
|
||||
entry->key = (ht)->type->keyDup((ht)->privdata, _key_); \
|
||||
else \
|
||||
entry->key = (_key_); \
|
||||
} while(0)
|
||||
|
||||
#define dictCompareHashKeys(ht, key1, key2) \
|
||||
(((ht)->type->keyCompare) ? \
|
||||
(ht)->type->keyCompare((ht)->privdata, key1, key2) : \
|
||||
(key1) == (key2))
|
||||
|
||||
#define dictHashKey(ht, key) (ht)->type->hashFunction(key)
|
||||
|
||||
#define dictGetEntryKey(he) ((he)->key)
|
||||
#define dictGetEntryVal(he) ((he)->val)
|
||||
#define dictSlots(ht) ((ht)->size)
|
||||
#define dictSize(ht) ((ht)->used)
|
||||
|
||||
/* API */
|
||||
static unsigned int dictGenHashFunction(const unsigned char *buf, int len);
|
||||
static dict *dictCreate(dictType *type, void *privDataPtr);
|
||||
static int dictExpand(dict *ht, unsigned long size);
|
||||
static int dictAdd(dict *ht, void *key, void *val);
|
||||
static int dictReplace(dict *ht, void *key, void *val);
|
||||
static int dictDelete(dict *ht, const void *key);
|
||||
static void dictRelease(dict *ht);
|
||||
static dictEntry * dictFind(dict *ht, const void *key);
|
||||
static dictIterator *dictGetIterator(dict *ht);
|
||||
static dictEntry *dictNext(dictIterator *iter);
|
||||
static void dictReleaseIterator(dictIterator *iter);
|
||||
|
||||
#endif /* __DICT_H */
|
3
deps/hiredis/example.c
vendored
3
deps/hiredis/example.c
vendored
@ -9,7 +9,8 @@ int main(void) {
|
||||
redisContext *c;
|
||||
redisReply *reply;
|
||||
|
||||
c = redisConnect((char*)"127.0.0.1", 6379);
|
||||
struct timeval timeout = { 1, 500000 }; // 1.5 seconds
|
||||
c = redisConnectWithTimeout((char*)"127.0.0.2", 6379, timeout);
|
||||
if (c->err) {
|
||||
printf("Connection error: %s\n", c->errstr);
|
||||
exit(1);
|
||||
|
9
deps/hiredis/fmacros.h
vendored
9
deps/hiredis/fmacros.h
vendored
@ -1,7 +1,9 @@
|
||||
#ifndef _REDIS_FMACRO_H
|
||||
#define _REDIS_FMACRO_H
|
||||
#ifndef __HIREDIS_FMACRO_H
|
||||
#define __HIREDIS_FMACRO_H
|
||||
|
||||
#ifndef _BSD_SOURCE
|
||||
#define _BSD_SOURCE
|
||||
#endif
|
||||
|
||||
#ifdef __linux__
|
||||
#define _XOPEN_SOURCE 700
|
||||
@ -9,7 +11,4 @@
|
||||
#define _XOPEN_SOURCE
|
||||
#endif
|
||||
|
||||
#define _LARGEFILE_SOURCE
|
||||
#define _FILE_OFFSET_BITS 64
|
||||
|
||||
#endif
|
||||
|
123
deps/hiredis/hiredis.c
vendored
123
deps/hiredis/hiredis.c
vendored
@ -271,14 +271,17 @@ static int processLineItem(redisReader *r) {
|
||||
int len;
|
||||
|
||||
if ((p = readLine(r,&len)) != NULL) {
|
||||
if (r->fn) {
|
||||
if (cur->type == REDIS_REPLY_INTEGER) {
|
||||
if (cur->type == REDIS_REPLY_INTEGER) {
|
||||
if (r->fn && r->fn->createInteger)
|
||||
obj = r->fn->createInteger(cur,readLongLong(p));
|
||||
} else {
|
||||
obj = r->fn->createString(cur,p,len);
|
||||
}
|
||||
else
|
||||
obj = (void*)REDIS_REPLY_INTEGER;
|
||||
} else {
|
||||
obj = (void*)(size_t)(cur->type);
|
||||
/* Type will be error or status. */
|
||||
if (r->fn && r->fn->createString)
|
||||
obj = r->fn->createString(cur,p,len);
|
||||
else
|
||||
obj = (void*)(size_t)(cur->type);
|
||||
}
|
||||
|
||||
/* Set reply if this is the root object. */
|
||||
@ -306,15 +309,19 @@ static int processBulkItem(redisReader *r) {
|
||||
|
||||
if (len < 0) {
|
||||
/* The nil object can always be created. */
|
||||
obj = r->fn ? r->fn->createNil(cur) :
|
||||
(void*)REDIS_REPLY_NIL;
|
||||
if (r->fn && r->fn->createNil)
|
||||
obj = r->fn->createNil(cur);
|
||||
else
|
||||
obj = (void*)REDIS_REPLY_NIL;
|
||||
success = 1;
|
||||
} else {
|
||||
/* Only continue when the buffer contains the entire bulk item. */
|
||||
bytelen += len+2; /* include \r\n */
|
||||
if (r->pos+bytelen <= r->len) {
|
||||
obj = r->fn ? r->fn->createString(cur,s+2,len) :
|
||||
(void*)REDIS_REPLY_STRING;
|
||||
if (r->fn && r->fn->createString)
|
||||
obj = r->fn->createString(cur,s+2,len);
|
||||
else
|
||||
obj = (void*)REDIS_REPLY_STRING;
|
||||
success = 1;
|
||||
}
|
||||
}
|
||||
@ -351,12 +358,16 @@ static int processMultiBulkItem(redisReader *r) {
|
||||
root = (r->ridx == 0);
|
||||
|
||||
if (elements == -1) {
|
||||
obj = r->fn ? r->fn->createNil(cur) :
|
||||
(void*)REDIS_REPLY_NIL;
|
||||
if (r->fn && r->fn->createNil)
|
||||
obj = r->fn->createNil(cur);
|
||||
else
|
||||
obj = (void*)REDIS_REPLY_NIL;
|
||||
moveToNextTask(r);
|
||||
} else {
|
||||
obj = r->fn ? r->fn->createArray(cur,elements) :
|
||||
(void*)REDIS_REPLY_ARRAY;
|
||||
if (r->fn && r->fn->createArray)
|
||||
obj = r->fn->createArray(cur,elements);
|
||||
else
|
||||
obj = (void*)REDIS_REPLY_ARRAY;
|
||||
|
||||
/* Modify task stack when there are more than 0 elements. */
|
||||
if (elements > 0) {
|
||||
@ -434,7 +445,7 @@ static int processItem(redisReader *r) {
|
||||
}
|
||||
}
|
||||
|
||||
void *redisReplyReaderCreate() {
|
||||
void *redisReplyReaderCreate(void) {
|
||||
redisReader *r = calloc(sizeof(redisReader),1);
|
||||
r->error = NULL;
|
||||
r->fn = &defaultFunctions;
|
||||
@ -493,7 +504,7 @@ static void redisSetReplyReaderError(redisReader *r, sds err) {
|
||||
if (r->buf != NULL) {
|
||||
sdsfree(r->buf);
|
||||
r->buf = sdsempty();
|
||||
r->pos = 0;
|
||||
r->pos = r->len = 0;
|
||||
}
|
||||
r->ridx = -1;
|
||||
r->error = err;
|
||||
@ -504,11 +515,18 @@ char *redisReplyReaderGetError(void *reader) {
|
||||
return r->error;
|
||||
}
|
||||
|
||||
void redisReplyReaderFeed(void *reader, char *buf, size_t len) {
|
||||
void redisReplyReaderFeed(void *reader, const char *buf, size_t len) {
|
||||
redisReader *r = reader;
|
||||
|
||||
/* Copy the provided buffer. */
|
||||
if (buf != NULL && len >= 1) {
|
||||
/* Destroy internal buffer when it is empty and is quite large. */
|
||||
if (r->len == 0 && sdsavail(r->buf) > 16*1024) {
|
||||
sdsfree(r->buf);
|
||||
r->buf = sdsempty();
|
||||
r->pos = 0;
|
||||
}
|
||||
|
||||
r->buf = sdscatlen(r->buf,buf,len);
|
||||
r->len = sdslen(r->buf);
|
||||
}
|
||||
@ -538,15 +556,10 @@ int redisReplyReaderGetReply(void *reader, void **reply) {
|
||||
if (processItem(r) < 0)
|
||||
break;
|
||||
|
||||
/* Discard the consumed part of the buffer. */
|
||||
if (r->pos > 0) {
|
||||
if (r->pos == r->len) {
|
||||
/* sdsrange has a quirck on this edge case. */
|
||||
sdsfree(r->buf);
|
||||
r->buf = sdsempty();
|
||||
} else {
|
||||
r->buf = sdsrange(r->buf,r->pos,r->len);
|
||||
}
|
||||
/* Discard part of the buffer when we've consumed at least 1k, to avoid
|
||||
* doing unnecessary calls to memmove() in sds.c. */
|
||||
if (r->pos >= 1024) {
|
||||
r->buf = sdsrange(r->buf,r->pos,-1);
|
||||
r->pos = 0;
|
||||
r->len = sdslen(r->buf);
|
||||
}
|
||||
@ -556,13 +569,6 @@ int redisReplyReaderGetReply(void *reader, void **reply) {
|
||||
void *aux = r->reply;
|
||||
r->reply = NULL;
|
||||
|
||||
/* Destroy the buffer when it is empty and is quite large. */
|
||||
if (r->len == 0 && sdsavail(r->buf) > 16*1024) {
|
||||
sdsfree(r->buf);
|
||||
r->buf = sdsempty();
|
||||
r->pos = 0;
|
||||
}
|
||||
|
||||
/* Check if there actually *is* a reply. */
|
||||
if (r->error != NULL) {
|
||||
return REDIS_ERR;
|
||||
@ -601,7 +607,7 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) {
|
||||
char *cmd = NULL; /* final command */
|
||||
int pos; /* position in final command */
|
||||
sds current; /* current argument */
|
||||
int interpolated = 0; /* did we do interpolation on an argument? */
|
||||
int touched = 0; /* was the current argument touched? */
|
||||
char **argv = NULL;
|
||||
int argc = 0, j;
|
||||
int totlen = 0;
|
||||
@ -615,13 +621,14 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) {
|
||||
while(*c != '\0') {
|
||||
if (*c != '%' || c[1] == '\0') {
|
||||
if (*c == ' ') {
|
||||
if (sdslen(current) != 0) {
|
||||
if (touched) {
|
||||
addArgument(current, &argv, &argc, &totlen);
|
||||
current = sdsempty();
|
||||
interpolated = 0;
|
||||
touched = 0;
|
||||
}
|
||||
} else {
|
||||
current = sdscatlen(current,c,1);
|
||||
touched = 1;
|
||||
}
|
||||
} else {
|
||||
switch(c[1]) {
|
||||
@ -630,14 +637,12 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) {
|
||||
size = strlen(arg);
|
||||
if (size > 0)
|
||||
current = sdscatlen(current,arg,size);
|
||||
interpolated = 1;
|
||||
break;
|
||||
case 'b':
|
||||
arg = va_arg(ap,char*);
|
||||
size = va_arg(ap,size_t);
|
||||
if (size > 0)
|
||||
current = sdscatlen(current,arg,size);
|
||||
interpolated = 1;
|
||||
break;
|
||||
case '%':
|
||||
current = sdscat(current,"%");
|
||||
@ -683,7 +688,6 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) {
|
||||
_format[_l] = '\0';
|
||||
va_copy(_cpy,ap);
|
||||
current = sdscatvprintf(current,_format,_cpy);
|
||||
interpolated = 1;
|
||||
va_end(_cpy);
|
||||
|
||||
/* Update current position (note: outer blocks
|
||||
@ -696,13 +700,14 @@ int redisvFormatCommand(char **target, const char *format, va_list ap) {
|
||||
va_arg(ap,void);
|
||||
}
|
||||
}
|
||||
touched = 1;
|
||||
c++;
|
||||
}
|
||||
c++;
|
||||
}
|
||||
|
||||
/* Add the last argument if needed */
|
||||
if (interpolated || sdslen(current) != 0) {
|
||||
if (touched) {
|
||||
addArgument(current, &argv, &argc, &totlen);
|
||||
} else {
|
||||
sdsfree(current);
|
||||
@ -798,7 +803,7 @@ void __redisSetError(redisContext *c, int type, const sds errstr) {
|
||||
}
|
||||
}
|
||||
|
||||
static redisContext *redisContextInit() {
|
||||
static redisContext *redisContextInit(void) {
|
||||
redisContext *c = calloc(sizeof(redisContext),1);
|
||||
c->err = 0;
|
||||
c->errstr = NULL;
|
||||
@ -809,8 +814,7 @@ static redisContext *redisContextInit() {
|
||||
}
|
||||
|
||||
void redisFree(redisContext *c) {
|
||||
/* Disconnect before free'ing if not yet disconnected. */
|
||||
if (c->flags & REDIS_CONNECTED)
|
||||
if (c->fd > 0)
|
||||
close(c->fd);
|
||||
if (c->errstr != NULL)
|
||||
sdsfree(c->errstr);
|
||||
@ -827,31 +831,52 @@ void redisFree(redisContext *c) {
|
||||
redisContext *redisConnect(const char *ip, int port) {
|
||||
redisContext *c = redisContextInit();
|
||||
c->flags |= REDIS_BLOCK;
|
||||
redisContextConnectTcp(c,ip,port);
|
||||
redisContextConnectTcp(c,ip,port,NULL);
|
||||
return c;
|
||||
}
|
||||
|
||||
redisContext *redisConnectWithTimeout(const char *ip, int port, struct timeval tv) {
|
||||
redisContext *c = redisContextInit();
|
||||
c->flags |= REDIS_BLOCK;
|
||||
redisContextConnectTcp(c,ip,port,&tv);
|
||||
return c;
|
||||
}
|
||||
|
||||
redisContext *redisConnectNonBlock(const char *ip, int port) {
|
||||
redisContext *c = redisContextInit();
|
||||
c->flags &= ~REDIS_BLOCK;
|
||||
redisContextConnectTcp(c,ip,port);
|
||||
redisContextConnectTcp(c,ip,port,NULL);
|
||||
return c;
|
||||
}
|
||||
|
||||
redisContext *redisConnectUnix(const char *path) {
|
||||
redisContext *c = redisContextInit();
|
||||
c->flags |= REDIS_BLOCK;
|
||||
redisContextConnectUnix(c,path);
|
||||
redisContextConnectUnix(c,path,NULL);
|
||||
return c;
|
||||
}
|
||||
|
||||
redisContext *redisConnectUnixWithTimeout(const char *path, struct timeval tv) {
|
||||
redisContext *c = redisContextInit();
|
||||
c->flags |= REDIS_BLOCK;
|
||||
redisContextConnectUnix(c,path,&tv);
|
||||
return c;
|
||||
}
|
||||
|
||||
redisContext *redisConnectUnixNonBlock(const char *path) {
|
||||
redisContext *c = redisContextInit();
|
||||
c->flags &= ~REDIS_BLOCK;
|
||||
redisContextConnectUnix(c,path);
|
||||
redisContextConnectUnix(c,path,NULL);
|
||||
return c;
|
||||
}
|
||||
|
||||
/* Set read/write timeout on a blocking socket. */
|
||||
int redisSetTimeout(redisContext *c, struct timeval tv) {
|
||||
if (c->flags & REDIS_BLOCK)
|
||||
return redisContextSetTimeout(c,tv);
|
||||
return REDIS_ERR;
|
||||
}
|
||||
|
||||
/* Set the replyObjectFunctions to use. Returns REDIS_ERR when the reader
|
||||
* was already initialized and the function set could not be re-set.
|
||||
* Return REDIS_OK when they could be set. */
|
||||
@ -879,7 +904,7 @@ int redisBufferRead(redisContext *c) {
|
||||
char buf[2048];
|
||||
int nread = read(c->fd,buf,sizeof(buf));
|
||||
if (nread == -1) {
|
||||
if (errno == EAGAIN) {
|
||||
if (errno == EAGAIN && !(c->flags & REDIS_BLOCK)) {
|
||||
/* Try again later */
|
||||
} else {
|
||||
__redisSetError(c,REDIS_ERR_IO,NULL);
|
||||
@ -910,7 +935,7 @@ int redisBufferWrite(redisContext *c, int *done) {
|
||||
if (sdslen(c->obuf) > 0) {
|
||||
nwritten = write(c->fd,c->obuf,sdslen(c->obuf));
|
||||
if (nwritten == -1) {
|
||||
if (errno == EAGAIN) {
|
||||
if (errno == EAGAIN && !(c->flags & REDIS_BLOCK)) {
|
||||
/* Try again later */
|
||||
} else {
|
||||
__redisSetError(c,REDIS_ERR_IO,NULL);
|
||||
|
18
deps/hiredis/hiredis.h
vendored
18
deps/hiredis/hiredis.h
vendored
@ -33,6 +33,7 @@
|
||||
#define __HIREDIS_H
|
||||
#include <stdio.h> /* for size_t */
|
||||
#include <stdarg.h> /* for va_list */
|
||||
#include <sys/time.h> /* for struct timeval */
|
||||
|
||||
#define HIREDIS_MAJOR 0
|
||||
#define HIREDIS_MINOR 9
|
||||
@ -64,6 +65,16 @@
|
||||
* should be terminated once all replies have been read. */
|
||||
#define REDIS_DISCONNECTING 0x4
|
||||
|
||||
/* Flag specific to the async API which means that the context should be clean
|
||||
* up as soon as possible. */
|
||||
#define REDIS_FREEING 0x8
|
||||
|
||||
/* Flag that is set when an async callback is executed. */
|
||||
#define REDIS_IN_CALLBACK 0x10
|
||||
|
||||
/* Flag that is set when the async context has one or more subscriptions. */
|
||||
#define REDIS_SUBSCRIBED 0x20
|
||||
|
||||
#define REDIS_REPLY_STRING 1
|
||||
#define REDIS_REPLY_ARRAY 2
|
||||
#define REDIS_REPLY_INTEGER 3
|
||||
@ -118,13 +129,13 @@ typedef struct redisContext {
|
||||
} redisContext;
|
||||
|
||||
void freeReplyObject(void *reply);
|
||||
void *redisReplyReaderCreate();
|
||||
void *redisReplyReaderCreate(void);
|
||||
int redisReplyReaderSetReplyObjectFunctions(void *reader, redisReplyObjectFunctions *fn);
|
||||
int redisReplyReaderSetPrivdata(void *reader, void *privdata);
|
||||
void *redisReplyReaderGetObject(void *reader);
|
||||
char *redisReplyReaderGetError(void *reader);
|
||||
void redisReplyReaderFree(void *ptr);
|
||||
void redisReplyReaderFeed(void *reader, char *buf, size_t len);
|
||||
void redisReplyReaderFeed(void *reader, const char *buf, size_t len);
|
||||
int redisReplyReaderGetReply(void *reader, void **reply);
|
||||
|
||||
/* Functions to format a command according to the protocol. */
|
||||
@ -133,9 +144,12 @@ int redisFormatCommand(char **target, const char *format, ...);
|
||||
int redisFormatCommandArgv(char **target, int argc, const char **argv, const size_t *argvlen);
|
||||
|
||||
redisContext *redisConnect(const char *ip, int port);
|
||||
redisContext *redisConnectWithTimeout(const char *ip, int port, struct timeval tv);
|
||||
redisContext *redisConnectNonBlock(const char *ip, int port);
|
||||
redisContext *redisConnectUnix(const char *path);
|
||||
redisContext *redisConnectUnixWithTimeout(const char *path, struct timeval tv);
|
||||
redisContext *redisConnectUnixNonBlock(const char *path);
|
||||
int redisSetTimeout(redisContext *c, struct timeval tv);
|
||||
int redisSetReplyObjectFunctions(redisContext *c, redisReplyObjectFunctions *fn);
|
||||
void redisFree(redisContext *c);
|
||||
int redisBufferRead(redisContext *c);
|
||||
|
118
deps/hiredis/net.c
vendored
118
deps/hiredis/net.c
vendored
@ -4,6 +4,7 @@
|
||||
* Copyright (c) 2010, Pieter Noordhuis <pcnoordhuis at gmail dot com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
@ -32,6 +33,7 @@
|
||||
#include "fmacros.h"
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <sys/select.h>
|
||||
#include <sys/un.h>
|
||||
#include <netinet/in.h>
|
||||
#include <netinet/tcp.h>
|
||||
@ -66,7 +68,7 @@ static int redisCreateSocket(redisContext *c, int type) {
|
||||
return s;
|
||||
}
|
||||
|
||||
static int redisSetNonBlock(redisContext *c, int fd) {
|
||||
static int redisSetBlocking(redisContext *c, int fd, int blocking) {
|
||||
int flags;
|
||||
|
||||
/* Set the socket nonblocking.
|
||||
@ -78,9 +80,15 @@ static int redisSetNonBlock(redisContext *c, int fd) {
|
||||
close(fd);
|
||||
return REDIS_ERR;
|
||||
}
|
||||
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) {
|
||||
|
||||
if (blocking)
|
||||
flags &= ~O_NONBLOCK;
|
||||
else
|
||||
flags |= O_NONBLOCK;
|
||||
|
||||
if (fcntl(fd, F_SETFL, flags) == -1) {
|
||||
__redisSetError(c,REDIS_ERR_IO,
|
||||
sdscatprintf(sdsempty(), "fcntl(F_SETFL,O_NONBLOCK): %s", strerror(errno)));
|
||||
sdscatprintf(sdsempty(), "fcntl(F_SETFL): %s", strerror(errno)));
|
||||
close(fd);
|
||||
return REDIS_ERR;
|
||||
}
|
||||
@ -92,19 +100,89 @@ static int redisSetTcpNoDelay(redisContext *c, int fd) {
|
||||
if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) == -1) {
|
||||
__redisSetError(c,REDIS_ERR_IO,
|
||||
sdscatprintf(sdsempty(), "setsockopt(TCP_NODELAY): %s", strerror(errno)));
|
||||
close(fd);
|
||||
return REDIS_ERR;
|
||||
}
|
||||
return REDIS_OK;
|
||||
}
|
||||
|
||||
int redisContextConnectTcp(redisContext *c, const char *addr, int port) {
|
||||
static int redisContextWaitReady(redisContext *c, int fd, const struct timeval *timeout) {
|
||||
struct timeval to;
|
||||
struct timeval *toptr = NULL;
|
||||
fd_set wfd;
|
||||
int err;
|
||||
socklen_t errlen;
|
||||
|
||||
/* 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) {
|
||||
__redisSetError(c,REDIS_ERR_IO,
|
||||
sdscatprintf(sdsempty(), "select(2): %s", strerror(errno)));
|
||||
close(fd);
|
||||
return REDIS_ERR;
|
||||
}
|
||||
|
||||
if (!FD_ISSET(fd, &wfd)) {
|
||||
errno = ETIMEDOUT;
|
||||
__redisSetError(c,REDIS_ERR_IO,NULL);
|
||||
close(fd);
|
||||
return REDIS_ERR;
|
||||
}
|
||||
|
||||
err = 0;
|
||||
errlen = sizeof(err);
|
||||
if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &err, &errlen) == -1) {
|
||||
__redisSetError(c,REDIS_ERR_IO,
|
||||
sdscatprintf(sdsempty(), "getsockopt(SO_ERROR): %s", strerror(errno)));
|
||||
close(fd);
|
||||
return REDIS_ERR;
|
||||
}
|
||||
|
||||
if (err) {
|
||||
errno = err;
|
||||
__redisSetError(c,REDIS_ERR_IO,NULL);
|
||||
close(fd);
|
||||
return REDIS_ERR;
|
||||
}
|
||||
|
||||
return REDIS_OK;
|
||||
}
|
||||
|
||||
__redisSetError(c,REDIS_ERR_IO,NULL);
|
||||
close(fd);
|
||||
return REDIS_ERR;
|
||||
}
|
||||
|
||||
int redisContextSetTimeout(redisContext *c, struct timeval tv) {
|
||||
if (setsockopt(c->fd,SOL_SOCKET,SO_RCVTIMEO,&tv,sizeof(tv)) == -1) {
|
||||
__redisSetError(c,REDIS_ERR_IO,
|
||||
sdscatprintf(sdsempty(), "setsockopt(SO_RCVTIMEO): %s", strerror(errno)));
|
||||
return REDIS_ERR;
|
||||
}
|
||||
if (setsockopt(c->fd,SOL_SOCKET,SO_SNDTIMEO,&tv,sizeof(tv)) == -1) {
|
||||
__redisSetError(c,REDIS_ERR_IO,
|
||||
sdscatprintf(sdsempty(), "setsockopt(SO_SNDTIMEO): %s", strerror(errno)));
|
||||
return REDIS_ERR;
|
||||
}
|
||||
return REDIS_OK;
|
||||
}
|
||||
|
||||
int redisContextConnectTcp(redisContext *c, const char *addr, int port, struct timeval *timeout) {
|
||||
int s;
|
||||
int blocking = (c->flags & REDIS_BLOCK);
|
||||
struct sockaddr_in sa;
|
||||
|
||||
if ((s = redisCreateSocket(c,AF_INET)) == REDIS_ERR)
|
||||
if ((s = redisCreateSocket(c,AF_INET)) < 0)
|
||||
return REDIS_ERR;
|
||||
if (!blocking && redisSetNonBlock(c,s) == REDIS_ERR)
|
||||
if (redisSetBlocking(c,s,0) != REDIS_OK)
|
||||
return REDIS_ERR;
|
||||
|
||||
sa.sin_family = AF_INET;
|
||||
@ -126,30 +204,31 @@ int redisContextConnectTcp(redisContext *c, const char *addr, int port) {
|
||||
if (errno == EINPROGRESS && !blocking) {
|
||||
/* This is ok. */
|
||||
} else {
|
||||
__redisSetError(c,REDIS_ERR_IO,NULL);
|
||||
close(s);
|
||||
return REDIS_ERR;
|
||||
if (redisContextWaitReady(c,s,timeout) != REDIS_OK)
|
||||
return REDIS_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
if (redisSetTcpNoDelay(c,s) != REDIS_OK) {
|
||||
close(s);
|
||||
/* Reset socket to be blocking after connect(2). */
|
||||
if (blocking && redisSetBlocking(c,s,1) != REDIS_OK)
|
||||
return REDIS_ERR;
|
||||
|
||||
if (redisSetTcpNoDelay(c,s) != REDIS_OK)
|
||||
return REDIS_ERR;
|
||||
}
|
||||
|
||||
c->fd = s;
|
||||
c->flags |= REDIS_CONNECTED;
|
||||
return REDIS_OK;
|
||||
}
|
||||
|
||||
int redisContextConnectUnix(redisContext *c, const char *path) {
|
||||
int redisContextConnectUnix(redisContext *c, const char *path, struct timeval *timeout) {
|
||||
int s;
|
||||
int blocking = (c->flags & REDIS_BLOCK);
|
||||
struct sockaddr_un sa;
|
||||
|
||||
if ((s = redisCreateSocket(c,AF_LOCAL)) == REDIS_ERR)
|
||||
if ((s = redisCreateSocket(c,AF_LOCAL)) < 0)
|
||||
return REDIS_ERR;
|
||||
if (!blocking && redisSetNonBlock(c,s) != REDIS_OK)
|
||||
if (redisSetBlocking(c,s,0) != REDIS_OK)
|
||||
return REDIS_ERR;
|
||||
|
||||
sa.sun_family = AF_LOCAL;
|
||||
@ -158,12 +237,15 @@ int redisContextConnectUnix(redisContext *c, const char *path) {
|
||||
if (errno == EINPROGRESS && !blocking) {
|
||||
/* This is ok. */
|
||||
} else {
|
||||
__redisSetError(c,REDIS_ERR_IO,NULL);
|
||||
close(s);
|
||||
return REDIS_ERR;
|
||||
if (redisContextWaitReady(c,s,timeout) != REDIS_OK)
|
||||
return REDIS_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
/* Reset socket to be blocking after connect(2). */
|
||||
if (blocking && redisSetBlocking(c,s,1) != REDIS_OK)
|
||||
return REDIS_ERR;
|
||||
|
||||
c->fd = s;
|
||||
c->flags |= REDIS_CONNECTED;
|
||||
return REDIS_OK;
|
||||
|
7
deps/hiredis/net.h
vendored
7
deps/hiredis/net.h
vendored
@ -1,6 +1,8 @@
|
||||
/* Extracted from anet.c to work properly with Hiredis error reporting.
|
||||
*
|
||||
* Copyright (c) 2006-2010, Salvatore Sanfilippo <antirez at gmail dot com>
|
||||
* Copyright (c) 2010, Pieter Noordhuis <pcnoordhuis at gmail dot com>
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
@ -37,7 +39,8 @@
|
||||
#define AF_LOCAL AF_UNIX
|
||||
#endif
|
||||
|
||||
int redisContextConnectTcp(redisContext *c, const char *addr, int port);
|
||||
int redisContextConnectUnix(redisContext *c, const char *path);
|
||||
int redisContextSetTimeout(redisContext *c, struct timeval tv);
|
||||
int redisContextConnectTcp(redisContext *c, const char *addr, int port, struct timeval *timeout);
|
||||
int redisContextConnectUnix(redisContext *c, const char *path, struct timeval *timeout);
|
||||
|
||||
#endif
|
||||
|
145
deps/hiredis/sds.c
vendored
145
deps/hiredis/sds.c
vendored
@ -30,11 +30,11 @@
|
||||
|
||||
#define SDS_ABORT_ON_OOM
|
||||
|
||||
#include "sds.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include "sds.h"
|
||||
|
||||
static void sdsOomAbort(void) {
|
||||
fprintf(stderr,"SDS: Out Of Memory (SDS_ABORT_ON_OOM defined)\n");
|
||||
@ -69,11 +69,6 @@ sds sdsnew(const char *init) {
|
||||
return sdsnewlen(init, initlen);
|
||||
}
|
||||
|
||||
size_t sdslen(const sds s) {
|
||||
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
|
||||
return sh->len;
|
||||
}
|
||||
|
||||
sds sdsdup(const sds s) {
|
||||
return sdsnewlen(s, sdslen(s));
|
||||
}
|
||||
@ -83,11 +78,6 @@ void sdsfree(sds s) {
|
||||
free(s-sizeof(struct sdshdr));
|
||||
}
|
||||
|
||||
size_t sdsavail(sds s) {
|
||||
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
|
||||
return sh->free;
|
||||
}
|
||||
|
||||
void sdsupdatelen(sds s) {
|
||||
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
|
||||
int reallen = strlen(s);
|
||||
@ -115,6 +105,25 @@ static sds sdsMakeRoomFor(sds s, size_t addlen) {
|
||||
return newsh->buf;
|
||||
}
|
||||
|
||||
/* Grow the sds to have the specified length. Bytes that were not part of
|
||||
* the original length of the sds will be set to zero. */
|
||||
sds sdsgrowzero(sds s, size_t len) {
|
||||
struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
|
||||
size_t totlen, curlen = sh->len;
|
||||
|
||||
if (len <= curlen) return s;
|
||||
s = sdsMakeRoomFor(s,len-curlen);
|
||||
if (s == NULL) return NULL;
|
||||
|
||||
/* Make sure added region doesn't contain garbage */
|
||||
sh = (void*)(s-(sizeof(struct sdshdr)));
|
||||
memset(s+curlen,0,(len-curlen+1)); /* also set trailing \0 byte */
|
||||
totlen = sh->len+sh->free;
|
||||
sh->len = len;
|
||||
sh->free = totlen-sh->len;
|
||||
return s;
|
||||
}
|
||||
|
||||
sds sdscatlen(sds s, const void *t, size_t len) {
|
||||
struct sdshdr *sh;
|
||||
size_t curlen = sdslen(s);
|
||||
@ -222,13 +231,16 @@ sds sdsrange(sds s, int start, int end) {
|
||||
}
|
||||
newlen = (start > end) ? 0 : (end-start)+1;
|
||||
if (newlen != 0) {
|
||||
if (start >= (signed)len) start = len-1;
|
||||
if (end >= (signed)len) end = len-1;
|
||||
newlen = (start > end) ? 0 : (end-start)+1;
|
||||
if (start >= (signed)len) {
|
||||
newlen = 0;
|
||||
} else if (end >= (signed)len) {
|
||||
end = len-1;
|
||||
newlen = (start > end) ? 0 : (end-start)+1;
|
||||
}
|
||||
} else {
|
||||
start = 0;
|
||||
}
|
||||
if (start != 0) memmove(sh->buf, sh->buf+start, newlen);
|
||||
if (start && newlen) memmove(sh->buf, sh->buf+start, newlen);
|
||||
sh->buf[newlen] = 0;
|
||||
sh->free = sh->free+(sh->len-newlen);
|
||||
sh->len = newlen;
|
||||
@ -477,3 +489,106 @@ err:
|
||||
if (current) sdsfree(current);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
#ifdef SDS_TEST_MAIN
|
||||
#include <stdio.h>
|
||||
|
||||
int __failed_tests = 0;
|
||||
int __test_num = 0;
|
||||
#define test_cond(descr,_c) do { \
|
||||
__test_num++; printf("%d - %s: ", __test_num, descr); \
|
||||
if(_c) printf("PASSED\n"); else {printf("FAILED\n"); __failed_tests++;} \
|
||||
} while(0);
|
||||
#define test_report() do { \
|
||||
printf("%d tests, %d passed, %d failed\n", __test_num, \
|
||||
__test_num-__failed_tests, __failed_tests); \
|
||||
if (__failed_tests) { \
|
||||
printf("=== WARNING === We have failed tests here...\n"); \
|
||||
} \
|
||||
} while(0);
|
||||
|
||||
int main(void) {
|
||||
{
|
||||
sds x = sdsnew("foo"), y;
|
||||
|
||||
test_cond("Create a string and obtain the length",
|
||||
sdslen(x) == 3 && memcmp(x,"foo\0",4) == 0)
|
||||
|
||||
sdsfree(x);
|
||||
x = sdsnewlen("foo",2);
|
||||
test_cond("Create a string with specified length",
|
||||
sdslen(x) == 2 && memcmp(x,"fo\0",3) == 0)
|
||||
|
||||
x = sdscat(x,"bar");
|
||||
test_cond("Strings concatenation",
|
||||
sdslen(x) == 5 && memcmp(x,"fobar\0",6) == 0);
|
||||
|
||||
x = sdscpy(x,"a");
|
||||
test_cond("sdscpy() against an originally longer string",
|
||||
sdslen(x) == 1 && memcmp(x,"a\0",2) == 0)
|
||||
|
||||
x = sdscpy(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk");
|
||||
test_cond("sdscpy() against an originally shorter string",
|
||||
sdslen(x) == 33 &&
|
||||
memcmp(x,"xyzxxxxxxxxxxyyyyyyyyyykkkkkkkkkk\0",33) == 0)
|
||||
|
||||
sdsfree(x);
|
||||
x = sdscatprintf(sdsempty(),"%d",123);
|
||||
test_cond("sdscatprintf() seems working in the base case",
|
||||
sdslen(x) == 3 && memcmp(x,"123\0",4) ==0)
|
||||
|
||||
sdsfree(x);
|
||||
x = sdstrim(sdsnew("xxciaoyyy"),"xy");
|
||||
test_cond("sdstrim() correctly trims characters",
|
||||
sdslen(x) == 4 && memcmp(x,"ciao\0",5) == 0)
|
||||
|
||||
y = sdsrange(sdsdup(x),1,1);
|
||||
test_cond("sdsrange(...,1,1)",
|
||||
sdslen(y) == 1 && memcmp(y,"i\0",2) == 0)
|
||||
|
||||
sdsfree(y);
|
||||
y = sdsrange(sdsdup(x),1,-1);
|
||||
test_cond("sdsrange(...,1,-1)",
|
||||
sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)
|
||||
|
||||
sdsfree(y);
|
||||
y = sdsrange(sdsdup(x),-2,-1);
|
||||
test_cond("sdsrange(...,-2,-1)",
|
||||
sdslen(y) == 2 && memcmp(y,"ao\0",3) == 0)
|
||||
|
||||
sdsfree(y);
|
||||
y = sdsrange(sdsdup(x),2,1);
|
||||
test_cond("sdsrange(...,2,1)",
|
||||
sdslen(y) == 0 && memcmp(y,"\0",1) == 0)
|
||||
|
||||
sdsfree(y);
|
||||
y = sdsrange(sdsdup(x),1,100);
|
||||
test_cond("sdsrange(...,1,100)",
|
||||
sdslen(y) == 3 && memcmp(y,"iao\0",4) == 0)
|
||||
|
||||
sdsfree(y);
|
||||
y = sdsrange(sdsdup(x),100,100);
|
||||
test_cond("sdsrange(...,100,100)",
|
||||
sdslen(y) == 0 && memcmp(y,"\0",1) == 0)
|
||||
|
||||
sdsfree(y);
|
||||
sdsfree(x);
|
||||
x = sdsnew("foo");
|
||||
y = sdsnew("foa");
|
||||
test_cond("sdscmp(foo,foa)", sdscmp(x,y) > 0)
|
||||
|
||||
sdsfree(y);
|
||||
sdsfree(x);
|
||||
x = sdsnew("bar");
|
||||
y = sdsnew("bar");
|
||||
test_cond("sdscmp(bar,bar)", sdscmp(x,y) == 0)
|
||||
|
||||
sdsfree(y);
|
||||
sdsfree(x);
|
||||
x = sdsnew("aar");
|
||||
y = sdsnew("bar");
|
||||
test_cond("sdscmp(bar,bar)", sdscmp(x,y) < 0)
|
||||
}
|
||||
test_report()
|
||||
}
|
||||
#endif
|
||||
|
13
deps/hiredis/sds.h
vendored
13
deps/hiredis/sds.h
vendored
@ -42,13 +42,24 @@ struct sdshdr {
|
||||
char buf[];
|
||||
};
|
||||
|
||||
static inline size_t sdslen(const sds s) {
|
||||
struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
|
||||
return sh->len;
|
||||
}
|
||||
|
||||
static inline size_t sdsavail(const sds s) {
|
||||
struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
|
||||
return sh->free;
|
||||
}
|
||||
|
||||
sds sdsnewlen(const void *init, size_t initlen);
|
||||
sds sdsnew(const char *init);
|
||||
sds sdsempty();
|
||||
sds sdsempty(void);
|
||||
size_t sdslen(const sds s);
|
||||
sds sdsdup(const sds s);
|
||||
void sdsfree(sds s);
|
||||
size_t sdsavail(sds s);
|
||||
sds sdsgrowzero(sds s, size_t len);
|
||||
sds sdscatlen(sds s, const void *t, size_t len);
|
||||
sds sdscat(sds s, const char *t);
|
||||
sds sdscpylen(sds s, char *t, size_t len);
|
||||
|
85
deps/hiredis/test.c
vendored
85
deps/hiredis/test.c
vendored
@ -6,6 +6,7 @@
|
||||
#include <assert.h>
|
||||
#include <unistd.h>
|
||||
#include <signal.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "hiredis.h"
|
||||
|
||||
@ -31,7 +32,7 @@ static void __connect(redisContext **target) {
|
||||
}
|
||||
}
|
||||
|
||||
static void test_format_commands() {
|
||||
static void test_format_commands(void) {
|
||||
char *cmd;
|
||||
int len;
|
||||
|
||||
@ -53,6 +54,12 @@ static void test_format_commands() {
|
||||
len == 4+4+(3+2)+4+(3+2)+4+(0+2));
|
||||
free(cmd);
|
||||
|
||||
test("Format command with an empty string in between proper interpolations: ");
|
||||
len = redisFormatCommand(&cmd,"SET %s %s","","foo");
|
||||
test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$0\r\n\r\n$3\r\nfoo\r\n",len) == 0 &&
|
||||
len == 4+4+(3+2)+4+(0+2)+4+(3+2));
|
||||
free(cmd);
|
||||
|
||||
test("Format command with %%b string interpolation: ");
|
||||
len = redisFormatCommand(&cmd,"SET %b %b","foo",3,"b\0r",3);
|
||||
test_cond(strncmp(cmd,"*3\r\n$3\r\nSET\r\n$3\r\nfoo\r\n$3\r\nb\0r\r\n",len) == 0 &&
|
||||
@ -115,7 +122,7 @@ static void test_format_commands() {
|
||||
free(cmd);
|
||||
}
|
||||
|
||||
static void test_blocking_connection() {
|
||||
static void test_blocking_connection(void) {
|
||||
redisContext *c;
|
||||
redisReply *reply;
|
||||
int major, minor;
|
||||
@ -246,13 +253,21 @@ static void test_blocking_connection() {
|
||||
* conditions, the error will be set to EOF. */
|
||||
assert(c->err == REDIS_ERR_EOF &&
|
||||
strcmp(c->errstr,"Server closed the connection") == 0);
|
||||
|
||||
/* Clean up context and reconnect again */
|
||||
redisFree(c);
|
||||
|
||||
__connect(&c);
|
||||
test("Returns I/O error on socket timeout: ");
|
||||
struct timeval tv = { 0, 1000 };
|
||||
assert(redisSetTimeout(c,tv) == REDIS_OK);
|
||||
test_cond(redisGetReply(c,(void**)&reply) == REDIS_ERR &&
|
||||
c->err == REDIS_ERR_IO && errno == EAGAIN);
|
||||
redisFree(c);
|
||||
|
||||
/* Context should be connected */
|
||||
__connect(&c);
|
||||
}
|
||||
|
||||
static void test_reply_reader() {
|
||||
static void test_reply_reader(void) {
|
||||
void *reader;
|
||||
void *reply;
|
||||
char *err;
|
||||
@ -309,10 +324,19 @@ static void test_reply_reader() {
|
||||
ret = redisReplyReaderGetReply(reader,&reply);
|
||||
test_cond(ret == REDIS_OK && reply == (void*)REDIS_REPLY_STATUS);
|
||||
redisReplyReaderFree(reader);
|
||||
|
||||
test("Properly reset state after protocol error: ");
|
||||
reader = redisReplyReaderCreate();
|
||||
redisReplyReaderSetReplyObjectFunctions(reader,NULL);
|
||||
redisReplyReaderFeed(reader,(char*)"x",1);
|
||||
ret = redisReplyReaderGetReply(reader,&reply);
|
||||
assert(ret == REDIS_ERR);
|
||||
ret = redisReplyReaderGetReply(reader,&reply);
|
||||
test_cond(ret == REDIS_OK && reply == NULL)
|
||||
}
|
||||
|
||||
static void test_throughput() {
|
||||
int i;
|
||||
static void test_throughput(void) {
|
||||
int i, num;
|
||||
long long t1, t2;
|
||||
redisContext *c = blocking_context;
|
||||
redisReply **replies;
|
||||
@ -321,31 +345,60 @@ static void test_throughput() {
|
||||
for (i = 0; i < 500; i++)
|
||||
freeReplyObject(redisCommand(c,"LPUSH mylist foo"));
|
||||
|
||||
replies = malloc(sizeof(redisReply*)*1000);
|
||||
num = 1000;
|
||||
replies = malloc(sizeof(redisReply*)*num);
|
||||
t1 = usec();
|
||||
for (i = 0; i < 1000; i++) {
|
||||
for (i = 0; i < num; i++) {
|
||||
replies[i] = redisCommand(c,"PING");
|
||||
assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS);
|
||||
}
|
||||
t2 = usec();
|
||||
for (i = 0; i < 1000; i++) freeReplyObject(replies[i]);
|
||||
for (i = 0; i < num; i++) freeReplyObject(replies[i]);
|
||||
free(replies);
|
||||
printf("\t(1000x PING: %.2fs)\n", (t2-t1)/1000000.0);
|
||||
printf("\t(%dx PING: %.3fs)\n", num, (t2-t1)/1000000.0);
|
||||
|
||||
replies = malloc(sizeof(redisReply*)*1000);
|
||||
replies = malloc(sizeof(redisReply*)*num);
|
||||
t1 = usec();
|
||||
for (i = 0; i < 1000; i++) {
|
||||
for (i = 0; i < num; i++) {
|
||||
replies[i] = redisCommand(c,"LRANGE mylist 0 499");
|
||||
assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_ARRAY);
|
||||
assert(replies[i] != NULL && replies[i]->elements == 500);
|
||||
}
|
||||
t2 = usec();
|
||||
for (i = 0; i < 1000; i++) freeReplyObject(replies[i]);
|
||||
for (i = 0; i < num; i++) freeReplyObject(replies[i]);
|
||||
free(replies);
|
||||
printf("\t(1000x LRANGE with 500 elements: %.2fs)\n", (t2-t1)/1000000.0);
|
||||
printf("\t(%dx LRANGE with 500 elements: %.3fs)\n", num, (t2-t1)/1000000.0);
|
||||
|
||||
num = 10000;
|
||||
replies = malloc(sizeof(redisReply*)*num);
|
||||
for (i = 0; i < num; i++)
|
||||
redisAppendCommand(c,"PING");
|
||||
t1 = usec();
|
||||
for (i = 0; i < num; i++) {
|
||||
assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK);
|
||||
assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_STATUS);
|
||||
}
|
||||
t2 = usec();
|
||||
for (i = 0; i < num; i++) freeReplyObject(replies[i]);
|
||||
free(replies);
|
||||
printf("\t(%dx PING (pipelined): %.3fs)\n", num, (t2-t1)/1000000.0);
|
||||
|
||||
replies = malloc(sizeof(redisReply*)*num);
|
||||
for (i = 0; i < num; i++)
|
||||
redisAppendCommand(c,"LRANGE mylist 0 499");
|
||||
t1 = usec();
|
||||
for (i = 0; i < num; i++) {
|
||||
assert(redisGetReply(c, (void*)&replies[i]) == REDIS_OK);
|
||||
assert(replies[i] != NULL && replies[i]->type == REDIS_REPLY_ARRAY);
|
||||
assert(replies[i] != NULL && replies[i]->elements == 500);
|
||||
}
|
||||
t2 = usec();
|
||||
for (i = 0; i < num; i++) freeReplyObject(replies[i]);
|
||||
free(replies);
|
||||
printf("\t(%dx LRANGE with 500 elements (pipelined): %.3fs)\n", num, (t2-t1)/1000000.0);
|
||||
}
|
||||
|
||||
static void cleanup() {
|
||||
static void cleanup(void) {
|
||||
redisContext *c = blocking_context;
|
||||
redisReply *reply;
|
||||
|
||||
|
@ -340,6 +340,12 @@ list-max-ziplist-value 64
|
||||
# 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)
|
||||
|
65
src/Makefile
65
src/Makefile
@ -16,16 +16,38 @@ else
|
||||
endif
|
||||
|
||||
ifeq ($(USE_TCMALLOC),yes)
|
||||
CCLINK+= -ltcmalloc
|
||||
CFLAGS+= -DUSE_TCMALLOC
|
||||
ALLOC_LINK=-ltcmalloc
|
||||
ALLOC_FLAGS=-DUSE_TCMALLOC
|
||||
endif
|
||||
|
||||
ifeq ($(USE_TCMALLOC_MINIMAL),yes)
|
||||
ALLOC_LINK=-ltcmalloc_minimal
|
||||
ALLOC_FLAGS=-DUSE_TCMALLOC
|
||||
endif
|
||||
|
||||
ifeq ($(USE_JEMALLOC),yes)
|
||||
ALLOC_LINK=-ljemalloc
|
||||
ALLOC_FLAGS=-DUSE_JEMALLOC
|
||||
endif
|
||||
|
||||
CCLINK+= $(ALLOC_LINK)
|
||||
CFLAGS+= $(ALLOC_FLAGS)
|
||||
|
||||
CCOPT= $(CFLAGS) $(CCLINK) $(ARCH) $(PROF)
|
||||
|
||||
PREFIX= /usr/local
|
||||
INSTALL_BIN= $(PREFIX)/bin
|
||||
INSTALL= cp -p
|
||||
|
||||
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 dscache.o pubsub.o multi.o debug.o sort.o intset.o syncio.o diskstore.o endian.o
|
||||
|
||||
CCCOLOR="\033[34m"
|
||||
LINKCOLOR="\033[34;1m"
|
||||
SRCCOLOR="\033[33m"
|
||||
BINCOLOR="\033[37;1m"
|
||||
MAKECOLOR="\033[32;1m"
|
||||
ENDCOLOR="\033[0m"
|
||||
|
||||
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 dscache.o pubsub.o multi.o debug.o sort.o intset.o syncio.o diskstore.o cluster.o crc16.o endian.o
|
||||
BENCHOBJ = ae.o anet.o redis-benchmark.o sds.o adlist.o zmalloc.o
|
||||
CLIOBJ = anet.o sds.o adlist.o redis-cli.o zmalloc.o release.o
|
||||
CHECKDUMPOBJ = redis-check-dump.o lzf_c.o lzf_d.o
|
||||
@ -83,7 +105,7 @@ redis-check-dump.o: redis-check-dump.c lzf.h
|
||||
redis-cli.o: redis-cli.c fmacros.h version.h ../deps/hiredis/hiredis.h \
|
||||
sds.h zmalloc.h ../deps/linenoise/linenoise.h help.h
|
||||
redis.o: redis.c redis.h fmacros.h config.h ae.h sds.h dict.h adlist.h \
|
||||
zmalloc.h anet.h zipmap.h ziplist.h intset.h version.h
|
||||
zmalloc.h anet.h zipmap.h ziplist.h intset.h version.h asciilogo.h
|
||||
release.o: release.c release.h
|
||||
replication.o: replication.c redis.h fmacros.h config.h ae.h sds.h dict.h \
|
||||
adlist.h zmalloc.h anet.h zipmap.h ziplist.h intset.h version.h
|
||||
@ -103,40 +125,51 @@ t_string.o: t_string.c redis.h fmacros.h config.h ae.h sds.h dict.h \
|
||||
adlist.h zmalloc.h anet.h zipmap.h ziplist.h intset.h version.h
|
||||
t_zset.o: t_zset.c redis.h fmacros.h config.h ae.h sds.h dict.h adlist.h \
|
||||
zmalloc.h anet.h zipmap.h ziplist.h intset.h version.h
|
||||
util.o: util.c redis.h fmacros.h config.h ae.h sds.h dict.h adlist.h \
|
||||
util.o: util.c util.h
|
||||
cluster.o: redis.h fmacros.h config.h ae.h sds.h dict.h adlist.h \
|
||||
zmalloc.h anet.h zipmap.h ziplist.h intset.h version.h
|
||||
ziplist.o: ziplist.c zmalloc.h ziplist.h
|
||||
zipmap.o: zipmap.c zmalloc.h
|
||||
zmalloc.o: zmalloc.c config.h
|
||||
|
||||
dependencies:
|
||||
cd ../deps/hiredis && $(MAKE) static ARCH="$(ARCH)"
|
||||
cd ../deps/linenoise && $(MAKE) ARCH="$(ARCH)"
|
||||
@echo $(MAKECOLOR)MAKE$(ENDCOLOR) $(BINCOLOR)hiredis$(ENDCOLOR)
|
||||
@cd ../deps/hiredis && $(MAKE) static ARCH="$(ARCH)"
|
||||
@echo $(MAKECOLOR)MAKE$(ENDCOLOR) $(BINCOLOR)linenoise$(ENDCOLOR)
|
||||
@cd ../deps/linenoise && $(MAKE) ARCH="$(ARCH)"
|
||||
|
||||
redis-server: $(OBJ)
|
||||
$(CC) -o $(PRGNAME) $(CCOPT) $(DEBUG) $(OBJ)
|
||||
@$(CC) -o $(PRGNAME) $(CCOPT) $(DEBUG) $(OBJ)
|
||||
@echo $(LINKCOLOR)LINK$(ENDCOLOR) $(BINCOLOR)$(@)$(ENDCOLOR)
|
||||
|
||||
redis-benchmark: dependencies $(BENCHOBJ)
|
||||
cd ../deps/hiredis && $(MAKE) static
|
||||
$(CC) -o $(BENCHPRGNAME) $(CCOPT) $(DEBUG) $(BENCHOBJ) ../deps/hiredis/libhiredis.a
|
||||
@cd ../deps/hiredis && $(MAKE) static
|
||||
@$(CC) -o $(BENCHPRGNAME) $(CCOPT) $(DEBUG) $(BENCHOBJ) ../deps/hiredis/libhiredis.a
|
||||
@echo $(LINKCOLOR)LINK$(ENDCOLOR) $(BINCOLOR)$(@)$(ENDCOLOR)
|
||||
|
||||
redis-benchmark.o:
|
||||
$(CC) -c $(CFLAGS) -I../deps/hiredis $(DEBUG) $(COMPILE_TIME) $<
|
||||
@$(CC) -c $(CFLAGS) -I../deps/hiredis $(DEBUG) $(COMPILE_TIME) $<
|
||||
@echo $(CCCOLOR)CC$(ENDCOLOR) $(SRCCOLOR)$(<)$(ENDCOLOR)
|
||||
|
||||
redis-cli: dependencies $(CLIOBJ)
|
||||
$(CC) -o $(CLIPRGNAME) $(CCOPT) $(DEBUG) $(CLIOBJ) ../deps/hiredis/libhiredis.a ../deps/linenoise/linenoise.o
|
||||
@$(CC) -o $(CLIPRGNAME) $(CCOPT) $(DEBUG) $(CLIOBJ) ../deps/hiredis/libhiredis.a ../deps/linenoise/linenoise.o
|
||||
@echo $(LINKCOLOR)LINK$(ENDCOLOR) $(BINCOLOR)$(@)$(ENDCOLOR)
|
||||
|
||||
redis-cli.o:
|
||||
$(CC) -c $(CFLAGS) -I../deps/hiredis -I../deps/linenoise $(DEBUG) $(COMPILE_TIME) $<
|
||||
@$(CC) -c $(CFLAGS) -I../deps/hiredis -I../deps/linenoise $(DEBUG) $(COMPILE_TIME) $<
|
||||
@echo $(CCCOLOR)CC$(ENDCOLOR) $(SRCCOLOR)$(<)$(ENDCOLOR)
|
||||
|
||||
redis-check-dump: $(CHECKDUMPOBJ)
|
||||
$(CC) -o $(CHECKDUMPPRGNAME) $(CCOPT) $(DEBUG) $(CHECKDUMPOBJ)
|
||||
@$(CC) -o $(CHECKDUMPPRGNAME) $(CCOPT) $(DEBUG) $(CHECKDUMPOBJ)
|
||||
@echo $(LINKCOLOR)LINK$(ENDCOLOR) $(BINCOLOR)$(@)$(ENDCOLOR)
|
||||
|
||||
redis-check-aof: $(CHECKAOFOBJ)
|
||||
$(CC) -o $(CHECKAOFPRGNAME) $(CCOPT) $(DEBUG) $(CHECKAOFOBJ)
|
||||
@$(CC) -o $(CHECKAOFPRGNAME) $(CCOPT) $(DEBUG) $(CHECKAOFOBJ)
|
||||
@echo $(LINKCOLOR)LINK$(ENDCOLOR) $(BINCOLOR)$(@)$(ENDCOLOR)
|
||||
|
||||
.c.o:
|
||||
$(CC) -c $(CFLAGS) $(DEBUG) $(COMPILE_TIME) $<
|
||||
@$(CC) -c $(CFLAGS) $(DEBUG) $(COMPILE_TIME) $<
|
||||
@echo $(CCCOLOR)CC$(ENDCOLOR) $(SRCCOLOR)$(<)$(ENDCOLOR)
|
||||
|
||||
clean:
|
||||
rm -rf $(PRGNAME) $(BENCHPRGNAME) $(CLIPRGNAME) $(CHECKDUMPPRGNAME) $(CHECKAOFPRGNAME) *.o *.gcda *.gcno *.gcov
|
||||
|
10
src/anet.c
10
src/anet.c
@ -345,3 +345,13 @@ int anetUnixAccept(char *err, int s) {
|
||||
|
||||
return fd;
|
||||
}
|
||||
|
||||
int anetPeerToString(int fd, char *ip, int *port) {
|
||||
struct sockaddr_in sa;
|
||||
socklen_t salen = sizeof(sa);
|
||||
|
||||
if (getpeername(fd,(struct sockaddr*)&sa,&salen) == -1) return -1;
|
||||
if (ip) strcpy(ip,inet_ntoa(sa.sin_addr));
|
||||
if (port) *port = ntohs(sa.sin_port);
|
||||
return 0;
|
||||
}
|
||||
|
@ -53,5 +53,6 @@ int anetWrite(int fd, char *buf, int count);
|
||||
int anetNonBlock(char *err, int fd);
|
||||
int anetTcpNoDelay(char *err, int fd);
|
||||
int anetTcpKeepAlive(char *err, int fd);
|
||||
int anetPeerToString(int fd, char *ip, int *port);
|
||||
|
||||
#endif
|
||||
|
68
src/aof.c
68
src/aof.c
@ -284,9 +284,11 @@ int loadAppendOnlyFile(char *filename) {
|
||||
/* The fake client should not have a reply */
|
||||
redisAssert(fakeClient->bufpos == 0 && listLength(fakeClient->reply) == 0);
|
||||
|
||||
/* Clean up, ready for the next command */
|
||||
for (j = 0; j < argc; j++) decrRefCount(argv[j]);
|
||||
zfree(argv);
|
||||
/* Clean up. Command code may have changed argv/argc so we use the
|
||||
* argv/argc of the client instead of the local variables. */
|
||||
for (j = 0; j < fakeClient->argc; j++)
|
||||
decrRefCount(fakeClient->argv[j]);
|
||||
zfree(fakeClient->argv);
|
||||
}
|
||||
|
||||
/* This point can only be reached when EOF is reached without errors.
|
||||
@ -346,7 +348,7 @@ int rewriteAppendOnlyFile(char *filename) {
|
||||
|
||||
/* Iterate this DB writing every entry */
|
||||
while((de = dictNext(di)) != NULL) {
|
||||
sds keystr = dictGetEntryKey(de);
|
||||
sds keystr;
|
||||
robj key, *o;
|
||||
time_t expiretime;
|
||||
|
||||
@ -429,21 +431,55 @@ int rewriteAppendOnlyFile(char *filename) {
|
||||
}
|
||||
} else if (o->type == REDIS_ZSET) {
|
||||
/* Emit the ZADDs needed to rebuild the sorted set */
|
||||
zset *zs = o->ptr;
|
||||
dictIterator *di = dictGetIterator(zs->dict);
|
||||
dictEntry *de;
|
||||
char cmd[]="*4\r\n$4\r\nZADD\r\n";
|
||||
|
||||
while((de = dictNext(di)) != NULL) {
|
||||
char cmd[]="*4\r\n$4\r\nZADD\r\n";
|
||||
robj *eleobj = dictGetEntryKey(de);
|
||||
double *score = dictGetEntryVal(de);
|
||||
if (o->encoding == REDIS_ENCODING_ZIPLIST) {
|
||||
unsigned char *zl = o->ptr;
|
||||
unsigned char *eptr, *sptr;
|
||||
unsigned char *vstr;
|
||||
unsigned int vlen;
|
||||
long long vll;
|
||||
double score;
|
||||
|
||||
if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
|
||||
if (fwriteBulkObject(fp,&key) == 0) goto werr;
|
||||
if (fwriteBulkDouble(fp,*score) == 0) goto werr;
|
||||
if (fwriteBulkObject(fp,eleobj) == 0) goto werr;
|
||||
eptr = ziplistIndex(zl,0);
|
||||
redisAssert(eptr != NULL);
|
||||
sptr = ziplistNext(zl,eptr);
|
||||
redisAssert(sptr != NULL);
|
||||
|
||||
while (eptr != NULL) {
|
||||
redisAssert(ziplistGet(eptr,&vstr,&vlen,&vll));
|
||||
score = zzlGetScore(sptr);
|
||||
|
||||
if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
|
||||
if (fwriteBulkObject(fp,&key) == 0) goto werr;
|
||||
if (fwriteBulkDouble(fp,score) == 0) goto werr;
|
||||
if (vstr != NULL) {
|
||||
if (fwriteBulkString(fp,(char*)vstr,vlen) == 0)
|
||||
goto werr;
|
||||
} else {
|
||||
if (fwriteBulkLongLong(fp,vll) == 0)
|
||||
goto werr;
|
||||
}
|
||||
zzlNext(zl,&eptr,&sptr);
|
||||
}
|
||||
} else if (o->encoding == REDIS_ENCODING_SKIPLIST) {
|
||||
zset *zs = o->ptr;
|
||||
dictIterator *di = dictGetIterator(zs->dict);
|
||||
dictEntry *de;
|
||||
|
||||
while((de = dictNext(di)) != NULL) {
|
||||
robj *eleobj = dictGetEntryKey(de);
|
||||
double *score = dictGetEntryVal(de);
|
||||
|
||||
if (fwrite(cmd,sizeof(cmd)-1,1,fp) == 0) goto werr;
|
||||
if (fwriteBulkObject(fp,&key) == 0) goto werr;
|
||||
if (fwriteBulkDouble(fp,*score) == 0) goto werr;
|
||||
if (fwriteBulkObject(fp,eleobj) == 0) goto werr;
|
||||
}
|
||||
dictReleaseIterator(di);
|
||||
} else {
|
||||
redisPanic("Unknown sorted set encoding");
|
||||
}
|
||||
dictReleaseIterator(di);
|
||||
} else if (o->type == REDIS_HASH) {
|
||||
char cmd[]="*4\r\n$4\r\nHSET\r\n";
|
||||
|
||||
|
18
src/asciilogo.h
Normal file
18
src/asciilogo.h
Normal file
@ -0,0 +1,18 @@
|
||||
char *ascii_logo =
|
||||
" _._ \n"
|
||||
" _.-``__ ''-._ \n"
|
||||
" _.-`` `. `_. ''-._ Redis %s (%s/%d) %s bit\n"
|
||||
" .-`` .-```. ```\\/ _.,_ ''-._ \n"
|
||||
" ( ' , .-` | `, ) Running in %s mode\n"
|
||||
" |`-._`-...-` __...-.``-._|'` _.-'| Port: %d\n"
|
||||
" | `-._ `._ / _.-' | PID: %ld\n"
|
||||
" `-._ `-._ `-./ _.-' _.-' \n"
|
||||
" |`-._`-._ `-.__.-' _.-'_.-'| \n"
|
||||
" | `-._`-._ _.-'_.-' | http://redis.io \n"
|
||||
" `-._ `-._`-.__.-'_.-' _.-' \n"
|
||||
" |`-._`-._ `-.__.-' _.-'_.-'| \n"
|
||||
" | `-._`-._ _.-'_.-' | \n"
|
||||
" `-._ `-._`-.__.-'_.-' _.-' \n"
|
||||
" `-._ `-.__.-' _.-' \n"
|
||||
" `-._ _.-' \n"
|
||||
" `-.__.-' \n\n";
|
1766
src/cluster.c
Normal file
1766
src/cluster.c
Normal file
File diff suppressed because it is too large
Load Diff
27
src/config.c
27
src/config.c
@ -261,6 +261,10 @@ void loadServerConfig(char *filename) {
|
||||
server.list_max_ziplist_value = memtoll(argv[1], NULL);
|
||||
} else if (!strcasecmp(argv[0],"set-max-intset-entries") && argc == 2) {
|
||||
server.set_max_intset_entries = memtoll(argv[1], NULL);
|
||||
} else if (!strcasecmp(argv[0],"zset-max-ziplist-entries") && argc == 2) {
|
||||
server.zset_max_ziplist_entries = memtoll(argv[1], NULL);
|
||||
} else if (!strcasecmp(argv[0],"zset-max-ziplist-value") && argc == 2) {
|
||||
server.zset_max_ziplist_value = memtoll(argv[1], NULL);
|
||||
} else if (!strcasecmp(argv[0],"rename-command") && argc == 3) {
|
||||
struct redisCommand *cmd = lookupCommand(argv[1]);
|
||||
int retval;
|
||||
@ -285,6 +289,13 @@ void loadServerConfig(char *filename) {
|
||||
err = "Target command name already exists"; goto loaderr;
|
||||
}
|
||||
}
|
||||
} else if (!strcasecmp(argv[0],"cluster-enabled") && argc == 2) {
|
||||
if ((server.cluster_enabled = yesnotoi(argv[1])) == -1) {
|
||||
err = "argument must be 'yes' or 'no'"; goto loaderr;
|
||||
}
|
||||
} else if (!strcasecmp(argv[0],"cluster-config-file") && argc == 2) {
|
||||
zfree(server.cluster.configfile);
|
||||
server.cluster.configfile = zstrdup(argv[1]);
|
||||
} else {
|
||||
err = "Bad directive or wrong number of arguments"; goto loaderr;
|
||||
}
|
||||
@ -443,6 +454,12 @@ void configSetCommand(redisClient *c) {
|
||||
} else if (!strcasecmp(c->argv[2]->ptr,"set-max-intset-entries")) {
|
||||
if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
|
||||
server.set_max_intset_entries = ll;
|
||||
} else if (!strcasecmp(c->argv[2]->ptr,"zset-max-ziplist-entries")) {
|
||||
if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
|
||||
server.zset_max_ziplist_entries = ll;
|
||||
} else if (!strcasecmp(c->argv[2]->ptr,"zset-max-ziplist-value")) {
|
||||
if (getLongLongFromObject(o,&ll) == REDIS_ERR || ll < 0) goto badfmt;
|
||||
server.zset_max_ziplist_value = ll;
|
||||
} else {
|
||||
addReplyErrorFormat(c,"Unsupported CONFIG parameter: %s",
|
||||
(char*)c->argv[2]->ptr);
|
||||
@ -594,6 +611,16 @@ void configGetCommand(redisClient *c) {
|
||||
addReplyBulkLongLong(c,server.set_max_intset_entries);
|
||||
matches++;
|
||||
}
|
||||
if (stringmatch(pattern,"zset-max-ziplist-entries",0)) {
|
||||
addReplyBulkCString(c,"zset-max-ziplist-entries");
|
||||
addReplyBulkLongLong(c,server.zset_max_ziplist_entries);
|
||||
matches++;
|
||||
}
|
||||
if (stringmatch(pattern,"zset-max-ziplist-value",0)) {
|
||||
addReplyBulkCString(c,"zset-max-ziplist-value");
|
||||
addReplyBulkLongLong(c,server.zset_max_ziplist_value);
|
||||
matches++;
|
||||
}
|
||||
setDeferredMultiBulkLength(c,replylen,matches*2);
|
||||
}
|
||||
|
||||
|
15
src/config.h
15
src/config.h
@ -10,18 +10,31 @@
|
||||
* this expects a different allocation scheme. Therefore, *exclusively* use
|
||||
* either tcmalloc or OSX's malloc_size()! */
|
||||
#if defined(USE_TCMALLOC)
|
||||
#define REDIS_MALLOC "tcmalloc"
|
||||
#include <google/tcmalloc.h>
|
||||
#if TC_VERSION_MAJOR >= 1 && TC_VERSION_MINOR >= 6
|
||||
#define HAVE_MALLOC_SIZE 1
|
||||
#define redis_malloc_size(p) tc_malloc_size(p)
|
||||
#endif
|
||||
#elif defined(USE_JEMALLOC)
|
||||
#define REDIS_MALLOC "jemalloc"
|
||||
#define JEMALLOC_MANGLE
|
||||
#include <jemalloc/jemalloc.h>
|
||||
#if JEMALLOC_VERSION_MAJOR >= 2 && JEMALLOC_VERSION_MINOR >= 1
|
||||
#define HAVE_MALLOC_SIZE 1
|
||||
#define redis_malloc_size(p) JEMALLOC_P(malloc_usable_size)(p)
|
||||
#endif
|
||||
#elif defined(__APPLE__)
|
||||
#include <malloc/malloc.h>
|
||||
#define HAVE_MALLOC_SIZE 1
|
||||
#define redis_malloc_size(p) malloc_size(p)
|
||||
#endif
|
||||
|
||||
/* Tefine redis_fstat to fstat or fstat64() */
|
||||
#ifndef REDIS_MALLOC
|
||||
#define REDIS_MALLOC "libc"
|
||||
#endif
|
||||
|
||||
/* Define redis_fstat to fstat or fstat64() */
|
||||
#if defined(__APPLE__) && !defined(MAC_OS_X_VERSION_10_6)
|
||||
#define redis_fstat fstat64
|
||||
#define redis_stat stat64
|
||||
|
74
src/crc16.c
Normal file
74
src/crc16.c
Normal file
@ -0,0 +1,74 @@
|
||||
#include "redis.h"
|
||||
|
||||
/*
|
||||
* Copyright 2001-2010 Georges Menie (www.menie.org)
|
||||
* Copyright 2010 Salvatore Sanfilippo (adapted to Redis coding style)
|
||||
* All rights reserved.
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of the University of California, Berkeley nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
/* CRC16 implementation acording to CCITT standards */
|
||||
|
||||
static const uint16_t crc16tab[256]= {
|
||||
0x0000,0x1021,0x2042,0x3063,0x4084,0x50a5,0x60c6,0x70e7,
|
||||
0x8108,0x9129,0xa14a,0xb16b,0xc18c,0xd1ad,0xe1ce,0xf1ef,
|
||||
0x1231,0x0210,0x3273,0x2252,0x52b5,0x4294,0x72f7,0x62d6,
|
||||
0x9339,0x8318,0xb37b,0xa35a,0xd3bd,0xc39c,0xf3ff,0xe3de,
|
||||
0x2462,0x3443,0x0420,0x1401,0x64e6,0x74c7,0x44a4,0x5485,
|
||||
0xa56a,0xb54b,0x8528,0x9509,0xe5ee,0xf5cf,0xc5ac,0xd58d,
|
||||
0x3653,0x2672,0x1611,0x0630,0x76d7,0x66f6,0x5695,0x46b4,
|
||||
0xb75b,0xa77a,0x9719,0x8738,0xf7df,0xe7fe,0xd79d,0xc7bc,
|
||||
0x48c4,0x58e5,0x6886,0x78a7,0x0840,0x1861,0x2802,0x3823,
|
||||
0xc9cc,0xd9ed,0xe98e,0xf9af,0x8948,0x9969,0xa90a,0xb92b,
|
||||
0x5af5,0x4ad4,0x7ab7,0x6a96,0x1a71,0x0a50,0x3a33,0x2a12,
|
||||
0xdbfd,0xcbdc,0xfbbf,0xeb9e,0x9b79,0x8b58,0xbb3b,0xab1a,
|
||||
0x6ca6,0x7c87,0x4ce4,0x5cc5,0x2c22,0x3c03,0x0c60,0x1c41,
|
||||
0xedae,0xfd8f,0xcdec,0xddcd,0xad2a,0xbd0b,0x8d68,0x9d49,
|
||||
0x7e97,0x6eb6,0x5ed5,0x4ef4,0x3e13,0x2e32,0x1e51,0x0e70,
|
||||
0xff9f,0xefbe,0xdfdd,0xcffc,0xbf1b,0xaf3a,0x9f59,0x8f78,
|
||||
0x9188,0x81a9,0xb1ca,0xa1eb,0xd10c,0xc12d,0xf14e,0xe16f,
|
||||
0x1080,0x00a1,0x30c2,0x20e3,0x5004,0x4025,0x7046,0x6067,
|
||||
0x83b9,0x9398,0xa3fb,0xb3da,0xc33d,0xd31c,0xe37f,0xf35e,
|
||||
0x02b1,0x1290,0x22f3,0x32d2,0x4235,0x5214,0x6277,0x7256,
|
||||
0xb5ea,0xa5cb,0x95a8,0x8589,0xf56e,0xe54f,0xd52c,0xc50d,
|
||||
0x34e2,0x24c3,0x14a0,0x0481,0x7466,0x6447,0x5424,0x4405,
|
||||
0xa7db,0xb7fa,0x8799,0x97b8,0xe75f,0xf77e,0xc71d,0xd73c,
|
||||
0x26d3,0x36f2,0x0691,0x16b0,0x6657,0x7676,0x4615,0x5634,
|
||||
0xd94c,0xc96d,0xf90e,0xe92f,0x99c8,0x89e9,0xb98a,0xa9ab,
|
||||
0x5844,0x4865,0x7806,0x6827,0x18c0,0x08e1,0x3882,0x28a3,
|
||||
0xcb7d,0xdb5c,0xeb3f,0xfb1e,0x8bf9,0x9bd8,0xabbb,0xbb9a,
|
||||
0x4a75,0x5a54,0x6a37,0x7a16,0x0af1,0x1ad0,0x2ab3,0x3a92,
|
||||
0xfd2e,0xed0f,0xdd6c,0xcd4d,0xbdaa,0xad8b,0x9de8,0x8dc9,
|
||||
0x7c26,0x6c07,0x5c64,0x4c45,0x3ca2,0x2c83,0x1ce0,0x0cc1,
|
||||
0xef1f,0xff3e,0xcf5d,0xdf7c,0xaf9b,0xbfba,0x8fd9,0x9ff8,
|
||||
0x6e17,0x7e36,0x4e55,0x5e74,0x2e93,0x3eb2,0x0ed1,0x1ef0
|
||||
};
|
||||
|
||||
uint16_t crc16(const char *buf, int len) {
|
||||
int counter;
|
||||
uint16_t crc = 0;
|
||||
for (counter = 0; counter < len; counter++)
|
||||
crc = (crc<<8) ^ crc16tab[((crc>>8) ^ *buf++)&0x00FF];
|
||||
return crc;
|
||||
}
|
126
src/db.c
126
src/db.c
@ -2,6 +2,9 @@
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
void SlotToKeyAdd(robj *key);
|
||||
void SlotToKeyDel(robj *key);
|
||||
|
||||
/*-----------------------------------------------------------------------------
|
||||
* C-level DB API
|
||||
*----------------------------------------------------------------------------*/
|
||||
@ -131,6 +134,7 @@ int dbAdd(redisDb *db, robj *key, robj *val) {
|
||||
sds copy = sdsdup(key->ptr);
|
||||
dictAdd(db->dict, copy, val);
|
||||
if (server.ds_enabled) cacheSetKeyMayExist(db,key);
|
||||
if (server.cluster_enabled) SlotToKeyAdd(key);
|
||||
return REDIS_OK;
|
||||
}
|
||||
}
|
||||
@ -146,6 +150,7 @@ int dbReplace(redisDb *db, robj *key, robj *val) {
|
||||
if ((oldval = dictFetchValue(db->dict,key->ptr)) == NULL) {
|
||||
sds copy = sdsdup(key->ptr);
|
||||
dictAdd(db->dict, copy, val);
|
||||
if (server.cluster_enabled) SlotToKeyAdd(key);
|
||||
retval = 1;
|
||||
} else {
|
||||
dictReplace(db->dict, key->ptr, val);
|
||||
@ -198,7 +203,12 @@ int dbDelete(redisDb *db, robj *key) {
|
||||
/* Deleting an entry from the expires dict will not free the sds of
|
||||
* the key, because it is shared with the main dictionary. */
|
||||
if (dictSize(db->expires) > 0) dictDelete(db->expires,key->ptr);
|
||||
return dictDelete(db->dict,key->ptr) == DICT_OK;
|
||||
if (dictDelete(db->dict,key->ptr) == DICT_OK) {
|
||||
if (server.cluster_enabled) SlotToKeyDel(key);
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
/* Empty the whole database.
|
||||
@ -307,6 +317,10 @@ void existsCommand(redisClient *c) {
|
||||
void selectCommand(redisClient *c) {
|
||||
int id = atoi(c->argv[1]->ptr);
|
||||
|
||||
if (server.cluster_enabled && id != 0) {
|
||||
addReplyError(c,"SELECT is not allowed in cluster mode");
|
||||
return;
|
||||
}
|
||||
if (selectDb(c,id) == REDIS_ERR) {
|
||||
addReplyError(c,"invalid DB index");
|
||||
} else {
|
||||
@ -428,6 +442,11 @@ void moveCommand(redisClient *c) {
|
||||
redisDb *src, *dst;
|
||||
int srcid;
|
||||
|
||||
if (server.cluster_enabled) {
|
||||
addReplyError(c,"MOVE is not allowed in cluster mode");
|
||||
return;
|
||||
}
|
||||
|
||||
/* Obtain source and target DB pointers */
|
||||
src = c->db;
|
||||
srcid = c->db->id;
|
||||
@ -616,3 +635,108 @@ void persistCommand(redisClient *c) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
* API to get key arguments from commands
|
||||
* ---------------------------------------------------------------------------*/
|
||||
|
||||
int *getKeysUsingCommandTable(struct redisCommand *cmd,robj **argv, int argc, int *numkeys) {
|
||||
int j, i = 0, last, *keys;
|
||||
REDIS_NOTUSED(argv);
|
||||
|
||||
if (cmd->firstkey == 0) {
|
||||
*numkeys = 0;
|
||||
return NULL;
|
||||
}
|
||||
last = cmd->lastkey;
|
||||
if (last < 0) last = argc+last;
|
||||
keys = zmalloc(sizeof(int)*((last - cmd->firstkey)+1));
|
||||
for (j = cmd->firstkey; j <= last; j += cmd->keystep) {
|
||||
redisAssert(j < argc);
|
||||
keys[i++] = j;
|
||||
}
|
||||
*numkeys = i;
|
||||
return keys;
|
||||
}
|
||||
|
||||
int *getKeysFromCommand(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags) {
|
||||
if (cmd->getkeys_proc) {
|
||||
return cmd->getkeys_proc(cmd,argv,argc,numkeys,flags);
|
||||
} else {
|
||||
return getKeysUsingCommandTable(cmd,argv,argc,numkeys);
|
||||
}
|
||||
}
|
||||
|
||||
void getKeysFreeResult(int *result) {
|
||||
zfree(result);
|
||||
}
|
||||
|
||||
int *noPreloadGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags) {
|
||||
if (flags & REDIS_GETKEYS_PRELOAD) {
|
||||
*numkeys = 0;
|
||||
return NULL;
|
||||
} else {
|
||||
return getKeysUsingCommandTable(cmd,argv,argc,numkeys);
|
||||
}
|
||||
}
|
||||
|
||||
int *renameGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags) {
|
||||
if (flags & REDIS_GETKEYS_PRELOAD) {
|
||||
int *keys = zmalloc(sizeof(int));
|
||||
*numkeys = 1;
|
||||
keys[0] = 1;
|
||||
return keys;
|
||||
} else {
|
||||
return getKeysUsingCommandTable(cmd,argv,argc,numkeys);
|
||||
}
|
||||
}
|
||||
|
||||
int *zunionInterGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags) {
|
||||
int i, num, *keys;
|
||||
REDIS_NOTUSED(cmd);
|
||||
REDIS_NOTUSED(flags);
|
||||
|
||||
num = atoi(argv[2]->ptr);
|
||||
/* Sanity check. Don't return any key if the command is going to
|
||||
* reply with syntax error. */
|
||||
if (num > (argc-3)) {
|
||||
*numkeys = 0;
|
||||
return NULL;
|
||||
}
|
||||
keys = zmalloc(sizeof(int)*num);
|
||||
for (i = 0; i < num; i++) keys[i] = 3+i;
|
||||
*numkeys = num;
|
||||
return keys;
|
||||
}
|
||||
|
||||
/* Slot to Key API. This is used by Redis Cluster in order to obtain in
|
||||
* a fast way a key that belongs to a specified hash slot. This is useful
|
||||
* while rehashing the cluster. */
|
||||
void SlotToKeyAdd(robj *key) {
|
||||
unsigned int hashslot = keyHashSlot(key->ptr,sdslen(key->ptr));
|
||||
|
||||
zslInsert(server.cluster.slots_to_keys,hashslot,key);
|
||||
incrRefCount(key);
|
||||
}
|
||||
|
||||
void SlotToKeyDel(robj *key) {
|
||||
unsigned int hashslot = keyHashSlot(key->ptr,sdslen(key->ptr));
|
||||
|
||||
zslDelete(server.cluster.slots_to_keys,hashslot,key);
|
||||
}
|
||||
|
||||
unsigned int GetKeysInSlot(unsigned int hashslot, robj **keys, unsigned int count) {
|
||||
zskiplistNode *n;
|
||||
zrangespec range;
|
||||
int j = 0;
|
||||
|
||||
range.min = range.max = hashslot;
|
||||
range.minex = range.maxex = 0;
|
||||
|
||||
n = zslFirstInRange(server.cluster.slots_to_keys, range);
|
||||
while(n && n->score == hashslot && count--) {
|
||||
keys[j++] = n->obj;
|
||||
n = n->level[0].forward;
|
||||
}
|
||||
return j;
|
||||
}
|
||||
|
63
src/debug.c
63
src/debug.c
@ -100,7 +100,7 @@ void computeDatasetDigest(unsigned char *final) {
|
||||
mixDigest(digest,key,sdslen(key));
|
||||
|
||||
/* Make sure the key is loaded if VM is active */
|
||||
o = lookupKeyRead(db,keyobj);
|
||||
o = dictGetEntryVal(de);
|
||||
|
||||
aux = htonl(o->type);
|
||||
mixDigest(digest,&aux,sizeof(aux));
|
||||
@ -127,22 +127,57 @@ void computeDatasetDigest(unsigned char *final) {
|
||||
}
|
||||
setTypeReleaseIterator(si);
|
||||
} else if (o->type == REDIS_ZSET) {
|
||||
zset *zs = o->ptr;
|
||||
dictIterator *di = dictGetIterator(zs->dict);
|
||||
dictEntry *de;
|
||||
unsigned char eledigest[20];
|
||||
|
||||
while((de = dictNext(di)) != NULL) {
|
||||
robj *eleobj = dictGetEntryKey(de);
|
||||
double *score = dictGetEntryVal(de);
|
||||
unsigned char eledigest[20];
|
||||
if (o->encoding == REDIS_ENCODING_ZIPLIST) {
|
||||
unsigned char *zl = o->ptr;
|
||||
unsigned char *eptr, *sptr;
|
||||
unsigned char *vstr;
|
||||
unsigned int vlen;
|
||||
long long vll;
|
||||
double score;
|
||||
|
||||
snprintf(buf,sizeof(buf),"%.17g",*score);
|
||||
memset(eledigest,0,20);
|
||||
mixObjectDigest(eledigest,eleobj);
|
||||
mixDigest(eledigest,buf,strlen(buf));
|
||||
xorDigest(digest,eledigest,20);
|
||||
eptr = ziplistIndex(zl,0);
|
||||
redisAssert(eptr != NULL);
|
||||
sptr = ziplistNext(zl,eptr);
|
||||
redisAssert(sptr != NULL);
|
||||
|
||||
while (eptr != NULL) {
|
||||
redisAssert(ziplistGet(eptr,&vstr,&vlen,&vll));
|
||||
score = zzlGetScore(sptr);
|
||||
|
||||
memset(eledigest,0,20);
|
||||
if (vstr != NULL) {
|
||||
mixDigest(eledigest,vstr,vlen);
|
||||
} else {
|
||||
ll2string(buf,sizeof(buf),vll);
|
||||
mixDigest(eledigest,buf,strlen(buf));
|
||||
}
|
||||
|
||||
snprintf(buf,sizeof(buf),"%.17g",score);
|
||||
mixDigest(eledigest,buf,strlen(buf));
|
||||
xorDigest(digest,eledigest,20);
|
||||
zzlNext(zl,&eptr,&sptr);
|
||||
}
|
||||
} else if (o->encoding == REDIS_ENCODING_SKIPLIST) {
|
||||
zset *zs = o->ptr;
|
||||
dictIterator *di = dictGetIterator(zs->dict);
|
||||
dictEntry *de;
|
||||
|
||||
while((de = dictNext(di)) != NULL) {
|
||||
robj *eleobj = dictGetEntryKey(de);
|
||||
double *score = dictGetEntryVal(de);
|
||||
|
||||
snprintf(buf,sizeof(buf),"%.17g",*score);
|
||||
memset(eledigest,0,20);
|
||||
mixObjectDigest(eledigest,eleobj);
|
||||
mixDigest(eledigest,buf,strlen(buf));
|
||||
xorDigest(digest,eledigest,20);
|
||||
}
|
||||
dictReleaseIterator(di);
|
||||
} else {
|
||||
redisPanic("Unknown sorted set encoding");
|
||||
}
|
||||
dictReleaseIterator(di);
|
||||
} else if (o->type == REDIS_HASH) {
|
||||
hashTypeIterator *hi;
|
||||
robj *obj;
|
||||
|
20
src/dict.c
20
src/dict.c
@ -245,9 +245,9 @@ int dictRehashMilliseconds(dict *d, int ms) {
|
||||
}
|
||||
|
||||
/* This function performs just a step of rehashing, and only if there are
|
||||
* not iterators bound to our hash table. When we have iterators in the middle
|
||||
* of a rehashing we can't mess with the two hash tables otherwise some element
|
||||
* can be missed or duplicated.
|
||||
* no safe iterators bound to our hash table. When we have iterators in the
|
||||
* middle of a rehashing we can't mess with the two hash tables otherwise
|
||||
* some element can be missed or duplicated.
|
||||
*
|
||||
* This function is called by common lookup or update operations in the
|
||||
* dictionary so that the hash table automatically migrates from H1 to H2
|
||||
@ -424,17 +424,26 @@ dictIterator *dictGetIterator(dict *d)
|
||||
iter->d = d;
|
||||
iter->table = 0;
|
||||
iter->index = -1;
|
||||
iter->safe = 0;
|
||||
iter->entry = NULL;
|
||||
iter->nextEntry = NULL;
|
||||
return iter;
|
||||
}
|
||||
|
||||
dictIterator *dictGetSafeIterator(dict *d) {
|
||||
dictIterator *i = dictGetIterator(d);
|
||||
|
||||
i->safe = 1;
|
||||
return i;
|
||||
}
|
||||
|
||||
dictEntry *dictNext(dictIterator *iter)
|
||||
{
|
||||
while (1) {
|
||||
if (iter->entry == NULL) {
|
||||
dictht *ht = &iter->d->ht[iter->table];
|
||||
if (iter->index == -1 && iter->table == 0) iter->d->iterators++;
|
||||
if (iter->safe && iter->index == -1 && iter->table == 0)
|
||||
iter->d->iterators++;
|
||||
iter->index++;
|
||||
if (iter->index >= (signed) ht->size) {
|
||||
if (dictIsRehashing(iter->d) && iter->table == 0) {
|
||||
@ -461,7 +470,8 @@ dictEntry *dictNext(dictIterator *iter)
|
||||
|
||||
void dictReleaseIterator(dictIterator *iter)
|
||||
{
|
||||
if (!(iter->index == -1 && iter->table == 0)) iter->d->iterators--;
|
||||
if (iter->safe && !(iter->index == -1 && iter->table == 0))
|
||||
iter->d->iterators--;
|
||||
zfree(iter);
|
||||
}
|
||||
|
||||
|
@ -74,10 +74,13 @@ 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
|
||||
* 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. */
|
||||
typedef struct dictIterator {
|
||||
dict *d;
|
||||
int table;
|
||||
int index;
|
||||
int table, index, safe;
|
||||
dictEntry *entry, *nextEntry;
|
||||
} dictIterator;
|
||||
|
||||
@ -132,6 +135,7 @@ dictEntry * dictFind(dict *d, const void *key);
|
||||
void *dictFetchValue(dict *d, const void *key);
|
||||
int dictResize(dict *d);
|
||||
dictIterator *dictGetIterator(dict *d);
|
||||
dictIterator *dictGetSafeIterator(dict *d);
|
||||
dictEntry *dictNext(dictIterator *iter);
|
||||
void dictReleaseIterator(dictIterator *iter);
|
||||
dictEntry *dictGetRandomKey(dict *d);
|
||||
|
@ -903,59 +903,6 @@ int waitForSwappedKey(redisClient *c, robj *key) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Preload keys for any command with first, last and step values for
|
||||
* the command keys prototype, as defined in the command table. */
|
||||
void waitForMultipleSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv) {
|
||||
int j, last;
|
||||
if (cmd->vm_firstkey == 0) return;
|
||||
last = cmd->vm_lastkey;
|
||||
if (last < 0) last = argc+last;
|
||||
for (j = cmd->vm_firstkey; j <= last; j += cmd->vm_keystep) {
|
||||
redisAssert(j < argc);
|
||||
waitForSwappedKey(c,argv[j]);
|
||||
}
|
||||
}
|
||||
|
||||
/* Preload keys needed for the ZUNIONSTORE and ZINTERSTORE commands.
|
||||
* Note that the number of keys to preload is user-defined, so we need to
|
||||
* apply a sanity check against argc. */
|
||||
void zunionInterBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv) {
|
||||
int i, num;
|
||||
REDIS_NOTUSED(cmd);
|
||||
|
||||
num = atoi(argv[2]->ptr);
|
||||
if (num > (argc-3)) return;
|
||||
for (i = 0; i < num; i++) {
|
||||
waitForSwappedKey(c,argv[3+i]);
|
||||
}
|
||||
}
|
||||
|
||||
/* Preload keys needed to execute the entire MULTI/EXEC block.
|
||||
*
|
||||
* This function is called by blockClientOnSwappedKeys when EXEC is issued,
|
||||
* and will block the client when any command requires a swapped out value. */
|
||||
void execBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv) {
|
||||
int i, margc;
|
||||
struct redisCommand *mcmd;
|
||||
robj **margv;
|
||||
REDIS_NOTUSED(cmd);
|
||||
REDIS_NOTUSED(argc);
|
||||
REDIS_NOTUSED(argv);
|
||||
|
||||
if (!(c->flags & REDIS_MULTI)) return;
|
||||
for (i = 0; i < c->mstate.count; i++) {
|
||||
mcmd = c->mstate.commands[i].cmd;
|
||||
margc = c->mstate.commands[i].argc;
|
||||
margv = c->mstate.commands[i].argv;
|
||||
|
||||
if (mcmd->vm_preload_proc != NULL) {
|
||||
mcmd->vm_preload_proc(c,mcmd,margc,margv);
|
||||
} else {
|
||||
waitForMultipleSwappedKeys(c,mcmd,margc,margv);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Is this client attempting to run a command against swapped keys?
|
||||
* If so, block it ASAP, load the keys in background, then resume it.
|
||||
*
|
||||
@ -967,10 +914,39 @@ void execBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int
|
||||
* Return 1 if the client is marked as blocked, 0 if the client can
|
||||
* continue as the keys it is going to access appear to be in memory. */
|
||||
int blockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd) {
|
||||
if (cmd->vm_preload_proc != NULL) {
|
||||
cmd->vm_preload_proc(c,cmd,c->argc,c->argv);
|
||||
int *keyindex, numkeys, j, i;
|
||||
|
||||
/* EXEC is a special case, we need to preload all the commands
|
||||
* queued into the transaction */
|
||||
if (cmd->proc == execCommand) {
|
||||
struct redisCommand *mcmd;
|
||||
robj **margv;
|
||||
int margc;
|
||||
|
||||
if (!(c->flags & REDIS_MULTI)) return 0;
|
||||
for (i = 0; i < c->mstate.count; i++) {
|
||||
mcmd = c->mstate.commands[i].cmd;
|
||||
margc = c->mstate.commands[i].argc;
|
||||
margv = c->mstate.commands[i].argv;
|
||||
|
||||
keyindex = getKeysFromCommand(mcmd,margv,margc,&numkeys,
|
||||
REDIS_GETKEYS_PRELOAD);
|
||||
for (j = 0; j < numkeys; j++) {
|
||||
redisLog(REDIS_DEBUG,"Preloading %s",
|
||||
(char*)margv[keyindex[j]]->ptr);
|
||||
waitForSwappedKey(c,margv[keyindex[j]]);
|
||||
}
|
||||
getKeysFreeResult(keyindex);
|
||||
}
|
||||
} else {
|
||||
waitForMultipleSwappedKeys(c,cmd,c->argc,c->argv);
|
||||
keyindex = getKeysFromCommand(cmd,c->argv,c->argc,&numkeys,
|
||||
REDIS_GETKEYS_PRELOAD);
|
||||
for (j = 0; j < numkeys; j++) {
|
||||
redisLog(REDIS_DEBUG,"Preloading %s",
|
||||
(char*)c->argv[keyindex[j]]->ptr);
|
||||
waitForSwappedKey(c,c->argv[keyindex[j]]);
|
||||
}
|
||||
getKeysFreeResult(keyindex);
|
||||
}
|
||||
|
||||
/* If the client was blocked for at least one key, mark it as blocked. */
|
||||
|
195
src/networking.c
195
src/networking.c
@ -16,7 +16,6 @@ redisClient *createClient(int fd) {
|
||||
|
||||
anetNonBlock(NULL,fd);
|
||||
anetTcpNoDelay(NULL,fd);
|
||||
if (!c) return NULL;
|
||||
if (aeCreateFileEvent(server.el,fd,AE_READABLE,
|
||||
readQueryFromClient, c) == AE_ERR)
|
||||
{
|
||||
@ -60,9 +59,6 @@ redisClient *createClient(int fd) {
|
||||
/* Set the event loop to listen for write events on the client's socket.
|
||||
* Typically gets called every time a reply is built. */
|
||||
int _installWriteEvent(redisClient *c) {
|
||||
/* When CLOSE_AFTER_REPLY is set, no more replies may be added! */
|
||||
redisAssert(!(c->flags & REDIS_CLOSE_AFTER_REPLY));
|
||||
|
||||
if (c->fd <= 0) return REDIS_ERR;
|
||||
if (c->bufpos == 0 && listLength(c->reply) == 0 &&
|
||||
(c->replstate == REDIS_REPL_NONE ||
|
||||
@ -88,9 +84,15 @@ robj *dupLastObjectIfNeeded(list *reply) {
|
||||
return listNodeValue(ln);
|
||||
}
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
* Low level functions to add more data to output buffers.
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
int _addReplyToBuffer(redisClient *c, char *s, size_t len) {
|
||||
size_t available = sizeof(c->buf)-c->bufpos;
|
||||
|
||||
if (c->flags & REDIS_CLOSE_AFTER_REPLY) return REDIS_OK;
|
||||
|
||||
/* If there already are entries in the reply list, we cannot
|
||||
* add anything more to the static buffer. */
|
||||
if (listLength(c->reply) > 0) return REDIS_ERR;
|
||||
@ -105,6 +107,9 @@ int _addReplyToBuffer(redisClient *c, char *s, size_t len) {
|
||||
|
||||
void _addReplyObjectToList(redisClient *c, robj *o) {
|
||||
robj *tail;
|
||||
|
||||
if (c->flags & REDIS_CLOSE_AFTER_REPLY) return;
|
||||
|
||||
if (listLength(c->reply) == 0) {
|
||||
incrRefCount(o);
|
||||
listAddNodeTail(c->reply,o);
|
||||
@ -128,6 +133,12 @@ void _addReplyObjectToList(redisClient *c, robj *o) {
|
||||
* needed it will be free'd, otherwise it ends up in a robj. */
|
||||
void _addReplySdsToList(redisClient *c, sds s) {
|
||||
robj *tail;
|
||||
|
||||
if (c->flags & REDIS_CLOSE_AFTER_REPLY) {
|
||||
sdsfree(s);
|
||||
return;
|
||||
}
|
||||
|
||||
if (listLength(c->reply) == 0) {
|
||||
listAddNodeTail(c->reply,createObject(REDIS_STRING,s));
|
||||
} else {
|
||||
@ -148,6 +159,9 @@ void _addReplySdsToList(redisClient *c, sds s) {
|
||||
|
||||
void _addReplyStringToList(redisClient *c, char *s, size_t len) {
|
||||
robj *tail;
|
||||
|
||||
if (c->flags & REDIS_CLOSE_AFTER_REPLY) return;
|
||||
|
||||
if (listLength(c->reply) == 0) {
|
||||
listAddNodeTail(c->reply,createStringObject(s,len));
|
||||
} else {
|
||||
@ -165,6 +179,11 @@ void _addReplyStringToList(redisClient *c, char *s, size_t len) {
|
||||
}
|
||||
}
|
||||
|
||||
/* -----------------------------------------------------------------------------
|
||||
* Higher level functions to queue data on the client output buffer.
|
||||
* The following functions are the ones that commands implementations will call.
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
void addReply(redisClient *c, robj *obj) {
|
||||
if (_installWriteEvent(c) != REDIS_OK) return;
|
||||
|
||||
@ -301,7 +320,12 @@ void _addReplyLongLong(redisClient *c, long long ll, char prefix) {
|
||||
}
|
||||
|
||||
void addReplyLongLong(redisClient *c, long long ll) {
|
||||
_addReplyLongLong(c,ll,':');
|
||||
if (ll == 0)
|
||||
addReply(c,shared.czero);
|
||||
else if (ll == 1)
|
||||
addReply(c,shared.cone);
|
||||
else
|
||||
_addReplyLongLong(c,ll,':');
|
||||
}
|
||||
|
||||
void addReplyMultiBulkLen(redisClient *c, long length) {
|
||||
@ -395,7 +419,7 @@ void acceptTcpHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
|
||||
|
||||
cfd = anetTcpAccept(server.neterr, fd, cip, &cport);
|
||||
if (cfd == AE_ERR) {
|
||||
redisLog(REDIS_VERBOSE,"Accepting client connection: %s", server.neterr);
|
||||
redisLog(REDIS_WARNING,"Accepting client connection: %s", server.neterr);
|
||||
return;
|
||||
}
|
||||
redisLog(REDIS_VERBOSE,"Accepted %s:%d", cip, cport);
|
||||
@ -410,7 +434,7 @@ void acceptUnixHandler(aeEventLoop *el, int fd, void *privdata, int mask) {
|
||||
|
||||
cfd = anetUnixAccept(server.neterr, fd);
|
||||
if (cfd == AE_ERR) {
|
||||
redisLog(REDIS_VERBOSE,"Accepting client connection: %s", server.neterr);
|
||||
redisLog(REDIS_WARNING,"Accepting client connection: %s", server.neterr);
|
||||
return;
|
||||
}
|
||||
redisLog(REDIS_VERBOSE,"Accepted connection to %s", server.unixsocket);
|
||||
@ -502,10 +526,16 @@ void freeClient(redisClient *c) {
|
||||
* close the connection with all our slaves if we have any, so
|
||||
* when we'll resync with the master the other slaves will sync again
|
||||
* with us as well. Note that also when the slave is not connected
|
||||
* to the master it will keep refusing connections by other slaves. */
|
||||
while (listLength(server.slaves)) {
|
||||
ln = listFirst(server.slaves);
|
||||
freeClient((redisClient*)ln->value);
|
||||
* to the master it will keep refusing connections by other slaves.
|
||||
*
|
||||
* We do this only if server.masterhost != NULL. If it is NULL this
|
||||
* means the user called SLAVEOF NO ONE and we are freeing our
|
||||
* link with the master, so no need to close link with slaves. */
|
||||
if (server.masterhost != NULL) {
|
||||
while (listLength(server.slaves)) {
|
||||
ln = listFirst(server.slaves);
|
||||
freeClient((redisClient*)ln->value);
|
||||
}
|
||||
}
|
||||
}
|
||||
/* Release memory */
|
||||
@ -670,70 +700,74 @@ static void setProtocolError(redisClient *c, int pos) {
|
||||
|
||||
int processMultibulkBuffer(redisClient *c) {
|
||||
char *newline = NULL;
|
||||
char *eptr;
|
||||
int pos = 0, tolerr;
|
||||
long bulklen;
|
||||
int pos = 0, ok;
|
||||
long long ll;
|
||||
|
||||
if (c->multibulklen == 0) {
|
||||
/* The client should have been reset */
|
||||
redisAssert(c->argc == 0);
|
||||
|
||||
/* Multi bulk length cannot be read without a \r\n */
|
||||
newline = strstr(c->querybuf,"\r\n");
|
||||
newline = strchr(c->querybuf,'\r');
|
||||
if (newline == NULL)
|
||||
return REDIS_ERR;
|
||||
|
||||
/* Buffer should also contain \n */
|
||||
if (newline-(c->querybuf) > ((signed)sdslen(c->querybuf)-2))
|
||||
return REDIS_ERR;
|
||||
|
||||
/* We know for sure there is a whole line since newline != NULL,
|
||||
* so go ahead and find out the multi bulk length. */
|
||||
redisAssert(c->querybuf[0] == '*');
|
||||
c->multibulklen = strtol(c->querybuf+1,&eptr,10);
|
||||
pos = (newline-c->querybuf)+2;
|
||||
if (c->multibulklen <= 0) {
|
||||
c->querybuf = sdsrange(c->querybuf,pos,-1);
|
||||
return REDIS_OK;
|
||||
} else if (c->multibulklen > 1024*1024) {
|
||||
ok = string2ll(c->querybuf+1,newline-(c->querybuf+1),&ll);
|
||||
if (!ok || ll > 1024*1024) {
|
||||
addReplyError(c,"Protocol error: invalid multibulk length");
|
||||
setProtocolError(c,pos);
|
||||
return REDIS_ERR;
|
||||
}
|
||||
|
||||
pos = (newline-c->querybuf)+2;
|
||||
if (ll <= 0) {
|
||||
c->querybuf = sdsrange(c->querybuf,pos,-1);
|
||||
return REDIS_OK;
|
||||
}
|
||||
|
||||
c->multibulklen = ll;
|
||||
|
||||
/* Setup argv array on client structure */
|
||||
if (c->argv) zfree(c->argv);
|
||||
c->argv = zmalloc(sizeof(robj*)*c->multibulklen);
|
||||
|
||||
/* Search new newline */
|
||||
newline = strstr(c->querybuf+pos,"\r\n");
|
||||
}
|
||||
|
||||
redisAssert(c->multibulklen > 0);
|
||||
while(c->multibulklen) {
|
||||
/* Read bulk length if unknown */
|
||||
if (c->bulklen == -1) {
|
||||
newline = strstr(c->querybuf+pos,"\r\n");
|
||||
if (newline != NULL) {
|
||||
if (c->querybuf[pos] != '$') {
|
||||
addReplyErrorFormat(c,
|
||||
"Protocol error: expected '$', got '%c'",
|
||||
c->querybuf[pos]);
|
||||
setProtocolError(c,pos);
|
||||
return REDIS_ERR;
|
||||
}
|
||||
|
||||
bulklen = strtol(c->querybuf+pos+1,&eptr,10);
|
||||
tolerr = (eptr[0] != '\r');
|
||||
if (tolerr || bulklen == LONG_MIN || bulklen == LONG_MAX ||
|
||||
bulklen < 0 || bulklen > 512*1024*1024)
|
||||
{
|
||||
addReplyError(c,"Protocol error: invalid bulk length");
|
||||
setProtocolError(c,pos);
|
||||
return REDIS_ERR;
|
||||
}
|
||||
pos += eptr-(c->querybuf+pos)+2;
|
||||
c->bulklen = bulklen;
|
||||
} else {
|
||||
/* No newline in current buffer, so wait for more data */
|
||||
newline = strchr(c->querybuf+pos,'\r');
|
||||
if (newline == NULL)
|
||||
break;
|
||||
|
||||
/* Buffer should also contain \n */
|
||||
if (newline-(c->querybuf) > ((signed)sdslen(c->querybuf)-2))
|
||||
break;
|
||||
|
||||
if (c->querybuf[pos] != '$') {
|
||||
addReplyErrorFormat(c,
|
||||
"Protocol error: expected '$', got '%c'",
|
||||
c->querybuf[pos]);
|
||||
setProtocolError(c,pos);
|
||||
return REDIS_ERR;
|
||||
}
|
||||
|
||||
ok = string2ll(c->querybuf+pos+1,newline-(c->querybuf+pos+1),&ll);
|
||||
if (!ok || ll < 0 || ll > 512*1024*1024) {
|
||||
addReplyError(c,"Protocol error: invalid bulk length");
|
||||
setProtocolError(c,pos);
|
||||
return REDIS_ERR;
|
||||
}
|
||||
|
||||
pos += newline-(c->querybuf+pos)+2;
|
||||
c->bulklen = ll;
|
||||
}
|
||||
|
||||
/* Read bulk argument */
|
||||
@ -845,3 +879,70 @@ void getClientsMaxBuffers(unsigned long *longest_output_list,
|
||||
*biggest_input_buffer = bib;
|
||||
}
|
||||
|
||||
void clientCommand(redisClient *c) {
|
||||
listNode *ln;
|
||||
listIter li;
|
||||
redisClient *client;
|
||||
|
||||
if (!strcasecmp(c->argv[1]->ptr,"list") && c->argc == 2) {
|
||||
sds o = sdsempty();
|
||||
time_t now = time(NULL);
|
||||
|
||||
listRewind(server.clients,&li);
|
||||
while ((ln = listNext(&li)) != NULL) {
|
||||
char ip[32], flags[16], *p;
|
||||
int port;
|
||||
|
||||
client = listNodeValue(ln);
|
||||
if (anetPeerToString(client->fd,ip,&port) == -1) continue;
|
||||
p = flags;
|
||||
if (client->flags & REDIS_SLAVE) {
|
||||
if (client->flags & REDIS_MONITOR)
|
||||
*p++ = 'O';
|
||||
else
|
||||
*p++ = 'S';
|
||||
}
|
||||
if (client->flags & REDIS_MASTER) *p++ = 'M';
|
||||
if (p == flags) *p++ = 'N';
|
||||
if (client->flags & REDIS_MULTI) *p++ = 'x';
|
||||
if (client->flags & REDIS_BLOCKED) *p++ = 'b';
|
||||
if (client->flags & REDIS_IO_WAIT) *p++ = 'i';
|
||||
if (client->flags & REDIS_DIRTY_CAS) *p++ = 'd';
|
||||
if (client->flags & REDIS_CLOSE_AFTER_REPLY) *p++ = 'c';
|
||||
if (client->flags & REDIS_UNBLOCKED) *p++ = 'u';
|
||||
*p++ = '\0';
|
||||
o = sdscatprintf(o,
|
||||
"addr=%s:%d fd=%d idle=%ld flags=%s db=%d sub=%d psub=%d\n",
|
||||
ip,port,client->fd,
|
||||
(long)(now - client->lastinteraction),
|
||||
flags,
|
||||
client->db->id,
|
||||
(int) dictSize(client->pubsub_channels),
|
||||
(int) listLength(client->pubsub_patterns));
|
||||
}
|
||||
addReplyBulkCBuffer(c,o,sdslen(o));
|
||||
sdsfree(o);
|
||||
} else if (!strcasecmp(c->argv[1]->ptr,"kill") && c->argc == 3) {
|
||||
listRewind(server.clients,&li);
|
||||
while ((ln = listNext(&li)) != NULL) {
|
||||
char ip[32], addr[64];
|
||||
int port;
|
||||
|
||||
client = listNodeValue(ln);
|
||||
if (anetPeerToString(client->fd,ip,&port) == -1) continue;
|
||||
snprintf(addr,sizeof(addr),"%s:%d",ip,port);
|
||||
if (strcmp(addr,c->argv[2]->ptr) == 0) {
|
||||
addReply(c,shared.ok);
|
||||
if (c == client) {
|
||||
client->flags |= REDIS_CLOSE_AFTER_REPLY;
|
||||
} else {
|
||||
freeClient(client);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
addReplyError(c,"No such client");
|
||||
} else {
|
||||
addReplyError(c, "Syntax error, try CLIENT (LIST | KILL ip:port)");
|
||||
}
|
||||
}
|
||||
|
83
src/object.c
83
src/object.c
@ -93,10 +93,20 @@ robj *createHashObject(void) {
|
||||
|
||||
robj *createZsetObject(void) {
|
||||
zset *zs = zmalloc(sizeof(*zs));
|
||||
robj *o;
|
||||
|
||||
zs->dict = dictCreate(&zsetDictType,NULL);
|
||||
zs->zsl = zslCreate();
|
||||
return createObject(REDIS_ZSET,zs);
|
||||
o = createObject(REDIS_ZSET,zs);
|
||||
o->encoding = REDIS_ENCODING_SKIPLIST;
|
||||
return o;
|
||||
}
|
||||
|
||||
robj *createZsetZiplistObject(void) {
|
||||
unsigned char *zl = ziplistNew();
|
||||
robj *o = createObject(REDIS_ZSET,zl);
|
||||
o->encoding = REDIS_ENCODING_ZIPLIST;
|
||||
return o;
|
||||
}
|
||||
|
||||
void freeStringObject(robj *o) {
|
||||
@ -132,11 +142,20 @@ void freeSetObject(robj *o) {
|
||||
}
|
||||
|
||||
void freeZsetObject(robj *o) {
|
||||
zset *zs = o->ptr;
|
||||
|
||||
dictRelease(zs->dict);
|
||||
zslFree(zs->zsl);
|
||||
zfree(zs);
|
||||
zset *zs;
|
||||
switch (o->encoding) {
|
||||
case REDIS_ENCODING_SKIPLIST:
|
||||
zs = o->ptr;
|
||||
dictRelease(zs->dict);
|
||||
zslFree(zs->zsl);
|
||||
zfree(zs);
|
||||
break;
|
||||
case REDIS_ENCODING_ZIPLIST:
|
||||
zfree(o->ptr);
|
||||
break;
|
||||
default:
|
||||
redisPanic("Unknown sorted set encoding");
|
||||
}
|
||||
}
|
||||
|
||||
void freeHashObject(robj *o) {
|
||||
@ -183,6 +202,16 @@ int checkType(redisClient *c, robj *o, int type) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int isObjectRepresentableAsLongLong(robj *o, long long *llval) {
|
||||
redisAssert(o->type == REDIS_STRING);
|
||||
if (o->encoding == REDIS_ENCODING_INT) {
|
||||
if (llval) *llval = (long) o->ptr;
|
||||
return REDIS_OK;
|
||||
} else {
|
||||
return string2ll(o->ptr,sdslen(o->ptr),llval) ? REDIS_OK : REDIS_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
/* Try to encode a string object in order to save space */
|
||||
robj *tryObjectEncoding(robj *o) {
|
||||
long value;
|
||||
@ -200,7 +229,7 @@ robj *tryObjectEncoding(robj *o) {
|
||||
redisAssert(o->type == REDIS_STRING);
|
||||
|
||||
/* Check if we can represent this string as a long integer */
|
||||
if (isStringRepresentableAsLong(s,&value) == REDIS_ERR) return o;
|
||||
if (!string2l(s,sdslen(s),&value)) return o;
|
||||
|
||||
/* Ok, this object can be encoded...
|
||||
*
|
||||
@ -402,6 +431,7 @@ char *strEncoding(int encoding) {
|
||||
case REDIS_ENCODING_LINKEDLIST: return "linkedlist";
|
||||
case REDIS_ENCODING_ZIPLIST: return "ziplist";
|
||||
case REDIS_ENCODING_INTSET: return "intset";
|
||||
case REDIS_ENCODING_SKIPLIST: return "skiplist";
|
||||
default: return "unknown";
|
||||
}
|
||||
}
|
||||
@ -416,3 +446,42 @@ unsigned long estimateObjectIdleTime(robj *o) {
|
||||
REDIS_LRU_CLOCK_RESOLUTION;
|
||||
}
|
||||
}
|
||||
|
||||
/* This is an helper function for the DEBUG command. We need to lookup keys
|
||||
* without any modification of LRU or other parameters. */
|
||||
robj *objectCommandLookup(redisClient *c, robj *key) {
|
||||
dictEntry *de;
|
||||
|
||||
if ((de = dictFind(c->db->dict,key->ptr)) == NULL) return NULL;
|
||||
return (robj*) dictGetEntryVal(de);
|
||||
}
|
||||
|
||||
robj *objectCommandLookupOrReply(redisClient *c, robj *key, robj *reply) {
|
||||
robj *o = objectCommandLookup(c,key);
|
||||
|
||||
if (!o) addReply(c, reply);
|
||||
return o;
|
||||
}
|
||||
|
||||
/* Object command allows to inspect the internals of an Redis Object.
|
||||
* Usage: OBJECT <verb> ... arguments ... */
|
||||
void objectCommand(redisClient *c) {
|
||||
robj *o;
|
||||
|
||||
if (!strcasecmp(c->argv[1]->ptr,"refcount") && c->argc == 3) {
|
||||
if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nullbulk))
|
||||
== NULL) return;
|
||||
addReplyLongLong(c,o->refcount);
|
||||
} else if (!strcasecmp(c->argv[1]->ptr,"encoding") && c->argc == 3) {
|
||||
if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nullbulk))
|
||||
== NULL) return;
|
||||
addReplyBulkCString(c,strEncoding(o->encoding));
|
||||
} else if (!strcasecmp(c->argv[1]->ptr,"idletime") && c->argc == 3) {
|
||||
if ((o = objectCommandLookupOrReply(c,c->argv[2],shared.nullbulk))
|
||||
== NULL) return;
|
||||
addReplyLongLong(c,estimateObjectIdleTime(o));
|
||||
} else {
|
||||
addReplyError(c,"Syntax error. Try OBJECT (refcount|encoding|idletime)");
|
||||
}
|
||||
}
|
||||
|
||||
|
69
src/rdb.c
69
src/rdb.c
@ -245,7 +245,7 @@ int rdbSaveDoubleValue(FILE *fp, double val) {
|
||||
return rdbWriteRaw(fp,buf,len);
|
||||
}
|
||||
|
||||
/* Save a Redis object. */
|
||||
/* Save a Redis object. Returns -1 on error, 0 on success. */
|
||||
int rdbSaveObject(FILE *fp, robj *o) {
|
||||
int n, nwritten = 0;
|
||||
|
||||
@ -302,24 +302,33 @@ int rdbSaveObject(FILE *fp, robj *o) {
|
||||
redisPanic("Unknown set encoding");
|
||||
}
|
||||
} else if (o->type == REDIS_ZSET) {
|
||||
/* Save a set value */
|
||||
zset *zs = o->ptr;
|
||||
dictIterator *di = dictGetIterator(zs->dict);
|
||||
dictEntry *de;
|
||||
/* Save a sorted set value */
|
||||
if (o->encoding == REDIS_ENCODING_ZIPLIST) {
|
||||
size_t l = ziplistBlobLen((unsigned char*)o->ptr);
|
||||
|
||||
if ((n = rdbSaveLen(fp,dictSize(zs->dict))) == -1) return -1;
|
||||
nwritten += n;
|
||||
|
||||
while((de = dictNext(di)) != NULL) {
|
||||
robj *eleobj = dictGetEntryKey(de);
|
||||
double *score = dictGetEntryVal(de);
|
||||
|
||||
if ((n = rdbSaveStringObject(fp,eleobj)) == -1) return -1;
|
||||
if ((n = rdbSaveRawString(fp,o->ptr,l)) == -1) return -1;
|
||||
nwritten += n;
|
||||
if ((n = rdbSaveDoubleValue(fp,*score)) == -1) return -1;
|
||||
} else if (o->encoding == REDIS_ENCODING_SKIPLIST) {
|
||||
zset *zs = o->ptr;
|
||||
dictIterator *di = dictGetIterator(zs->dict);
|
||||
dictEntry *de;
|
||||
|
||||
if ((n = rdbSaveLen(fp,dictSize(zs->dict))) == -1) return -1;
|
||||
nwritten += n;
|
||||
|
||||
while((de = dictNext(di)) != NULL) {
|
||||
robj *eleobj = dictGetEntryKey(de);
|
||||
double *score = dictGetEntryVal(de);
|
||||
|
||||
if ((n = rdbSaveStringObject(fp,eleobj)) == -1) return -1;
|
||||
nwritten += n;
|
||||
if ((n = rdbSaveDoubleValue(fp,*score)) == -1) return -1;
|
||||
nwritten += n;
|
||||
}
|
||||
dictReleaseIterator(di);
|
||||
} else {
|
||||
redisPanic("Unknown sorted set encoding");
|
||||
}
|
||||
dictReleaseIterator(di);
|
||||
} else if (o->type == REDIS_HASH) {
|
||||
/* Save a hash value */
|
||||
if (o->encoding == REDIS_ENCODING_ZIPMAP) {
|
||||
@ -386,6 +395,8 @@ int rdbSaveKeyValuePair(FILE *fp, robj *key, robj *val,
|
||||
vtype = REDIS_LIST_ZIPLIST;
|
||||
else if (vtype == REDIS_SET && val->encoding == REDIS_ENCODING_INTSET)
|
||||
vtype = REDIS_SET_INTSET;
|
||||
else if (vtype == REDIS_ZSET && val->encoding == REDIS_ENCODING_ZIPLIST)
|
||||
vtype = REDIS_ZSET_ZIPLIST;
|
||||
/* Save type, key, value */
|
||||
if (rdbSaveType(fp,vtype) == -1) return -1;
|
||||
if (rdbSaveStringObject(fp,key) == -1) return -1;
|
||||
@ -414,7 +425,7 @@ int rdbSave(char *filename) {
|
||||
strerror(errno));
|
||||
return REDIS_ERR;
|
||||
}
|
||||
if (fwrite("REDIS0001",9,1,fp) == 0) goto werr;
|
||||
if (fwrite("REDIS0002",9,1,fp) == 0) goto werr;
|
||||
for (j = 0; j < server.dbnum; j++) {
|
||||
redisDb *db = server.db+j;
|
||||
dict *d = db->dict;
|
||||
@ -745,11 +756,13 @@ robj *rdbLoadObject(int type, FILE *fp) {
|
||||
} else if (type == REDIS_ZSET) {
|
||||
/* Read list/set value */
|
||||
size_t zsetlen;
|
||||
size_t maxelelen = 0;
|
||||
zset *zs;
|
||||
|
||||
if ((zsetlen = rdbLoadLen(fp,NULL)) == REDIS_RDB_LENERR) return NULL;
|
||||
o = createZsetObject();
|
||||
zs = o->ptr;
|
||||
|
||||
/* Load every single element of the list/set */
|
||||
while(zsetlen--) {
|
||||
robj *ele;
|
||||
@ -759,10 +772,21 @@ robj *rdbLoadObject(int type, FILE *fp) {
|
||||
if ((ele = rdbLoadEncodedStringObject(fp)) == NULL) return NULL;
|
||||
ele = tryObjectEncoding(ele);
|
||||
if (rdbLoadDoubleValue(fp,&score) == -1) return NULL;
|
||||
|
||||
/* Don't care about integer-encoded strings. */
|
||||
if (ele->encoding == REDIS_ENCODING_RAW &&
|
||||
sdslen(ele->ptr) > maxelelen)
|
||||
maxelelen = sdslen(ele->ptr);
|
||||
|
||||
znode = zslInsert(zs->zsl,score,ele);
|
||||
dictAdd(zs->dict,ele,&znode->score);
|
||||
incrRefCount(ele); /* added to skiplist */
|
||||
}
|
||||
|
||||
/* Convert *after* loading, since sorted sets are not stored ordered. */
|
||||
if (zsetLength(o) <= server.zset_max_ziplist_entries &&
|
||||
maxelelen <= server.zset_max_ziplist_value)
|
||||
zsetConvert(o,REDIS_ENCODING_ZIPLIST);
|
||||
} else if (type == REDIS_HASH) {
|
||||
size_t hashlen;
|
||||
|
||||
@ -811,7 +835,8 @@ robj *rdbLoadObject(int type, FILE *fp) {
|
||||
}
|
||||
} else if (type == REDIS_HASH_ZIPMAP ||
|
||||
type == REDIS_LIST_ZIPLIST ||
|
||||
type == REDIS_SET_INTSET)
|
||||
type == REDIS_SET_INTSET ||
|
||||
type == REDIS_ZSET_ZIPLIST)
|
||||
{
|
||||
robj *aux = rdbLoadStringObject(fp);
|
||||
|
||||
@ -846,8 +871,14 @@ robj *rdbLoadObject(int type, FILE *fp) {
|
||||
if (intsetLen(o->ptr) > server.set_max_intset_entries)
|
||||
setTypeConvert(o,REDIS_ENCODING_HT);
|
||||
break;
|
||||
case REDIS_ZSET_ZIPLIST:
|
||||
o->type = REDIS_ZSET;
|
||||
o->encoding = REDIS_ENCODING_ZIPLIST;
|
||||
if (zsetLength(o) > server.zset_max_ziplist_entries)
|
||||
zsetConvert(o,REDIS_ENCODING_SKIPLIST);
|
||||
break;
|
||||
default:
|
||||
redisPanic("Unknown enoding");
|
||||
redisPanic("Unknown encoding");
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
@ -900,7 +931,7 @@ int rdbLoad(char *filename) {
|
||||
return REDIS_ERR;
|
||||
}
|
||||
rdbver = atoi(buf+5);
|
||||
if (rdbver != 1) {
|
||||
if (rdbver < 1 || rdbver > 2) {
|
||||
fclose(fp);
|
||||
redisLog(REDIS_WARNING,"Can't handle RDB format version %d",rdbver);
|
||||
return REDIS_ERR;
|
||||
|
@ -394,15 +394,18 @@ static sds cliFormatReplyRaw(redisReply *r) {
|
||||
switch (r->type) {
|
||||
case REDIS_REPLY_NIL:
|
||||
/* Nothing... */
|
||||
break;
|
||||
break;
|
||||
case REDIS_REPLY_ERROR:
|
||||
out = sdscatlen(out,r->str,r->len);
|
||||
out = sdscatlen(out,"\n",1);
|
||||
break;
|
||||
case REDIS_REPLY_STATUS:
|
||||
case REDIS_REPLY_STRING:
|
||||
out = sdscatlen(out,r->str,r->len);
|
||||
break;
|
||||
break;
|
||||
case REDIS_REPLY_INTEGER:
|
||||
out = sdscatprintf(out,"%lld",r->integer);
|
||||
break;
|
||||
break;
|
||||
case REDIS_REPLY_ARRAY:
|
||||
for (i = 0; i < r->elements; i++) {
|
||||
if (i > 0) out = sdscat(out,config.mb_delim);
|
||||
@ -410,7 +413,7 @@ static sds cliFormatReplyRaw(redisReply *r) {
|
||||
out = sdscatlen(out,tmp,sdslen(tmp));
|
||||
sdsfree(tmp);
|
||||
}
|
||||
break;
|
||||
break;
|
||||
default:
|
||||
fprintf(stderr,"Unknown reply type: %d\n", r->type);
|
||||
exit(1);
|
||||
@ -464,7 +467,18 @@ static int cliSendCommand(int argc, char **argv, int repeat) {
|
||||
return REDIS_OK;
|
||||
}
|
||||
|
||||
output_raw = !strcasecmp(command,"info");
|
||||
output_raw = 0;
|
||||
if (!strcasecmp(command,"info") ||
|
||||
(argc == 2 && !strcasecmp(command,"cluster") &&
|
||||
(!strcasecmp(argv[1],"nodes") ||
|
||||
!strcasecmp(argv[1],"info"))) ||
|
||||
(argc == 2 && !strcasecmp(command,"client") &&
|
||||
!strcasecmp(argv[1],"list")))
|
||||
|
||||
{
|
||||
output_raw = 1;
|
||||
}
|
||||
|
||||
if (!strcasecmp(command,"help") || !strcasecmp(command,"?")) {
|
||||
cliOutputHelp(--argc, ++argv);
|
||||
return REDIS_OK;
|
||||
|
258
src/redis-trib.rb
Executable file
258
src/redis-trib.rb
Executable file
@ -0,0 +1,258 @@
|
||||
#!/usr/bin/env ruby
|
||||
|
||||
require 'rubygems'
|
||||
require 'redis'
|
||||
|
||||
ClusterHashSlots = 4096
|
||||
|
||||
def xputs(s)
|
||||
printf s
|
||||
STDOUT.flush
|
||||
end
|
||||
|
||||
class ClusterNode
|
||||
def initialize(addr)
|
||||
s = addr.split(":")
|
||||
if s.length != 2
|
||||
puts "Invalid node name #{addr}"
|
||||
exit 1
|
||||
end
|
||||
@r = nil
|
||||
@host = s[0]
|
||||
@port = s[1]
|
||||
@slots = {}
|
||||
@dirty = false
|
||||
end
|
||||
|
||||
def to_s
|
||||
"#{@host}:#{@port}"
|
||||
end
|
||||
|
||||
def connect(o={})
|
||||
xputs "Connecting to node #{self}: "
|
||||
begin
|
||||
@r = Redis.new(:host => @host, :port => @port)
|
||||
@r.ping
|
||||
rescue
|
||||
puts "ERROR"
|
||||
puts "Sorry, can't connect to node #{self}"
|
||||
exit 1 if o[:abort]
|
||||
@r = nil
|
||||
end
|
||||
puts "OK"
|
||||
end
|
||||
|
||||
def assert_cluster
|
||||
info = @r.info
|
||||
if !info["cluster_enabled"] || info["cluster_enabled"].to_i == 0
|
||||
puts "Error: Node #{self} is not configured as a cluster node."
|
||||
exit 1
|
||||
end
|
||||
end
|
||||
|
||||
def assert_empty
|
||||
if !(@r.cluster("info").split("\r\n").index("cluster_known_nodes:1")) ||
|
||||
(@r.info['db0'])
|
||||
puts "Error: Node #{self} is not empty. Either the node already knows other nodes (check with nodes-info) or contains some key in database 0."
|
||||
exit 1
|
||||
end
|
||||
end
|
||||
|
||||
def add_slots(slots)
|
||||
slots.each{|s|
|
||||
@slots[s] = :new
|
||||
}
|
||||
@dirty = true
|
||||
end
|
||||
|
||||
def flush_node_config
|
||||
return if !@dirty
|
||||
new = []
|
||||
@slots.each{|s,val|
|
||||
if val == :new
|
||||
new << s
|
||||
@slots[s] = true
|
||||
end
|
||||
}
|
||||
@r.cluster("addslots",*new)
|
||||
@dirty = false
|
||||
end
|
||||
|
||||
def info_string
|
||||
# We want to display the hash slots assigned to this node
|
||||
# as ranges, like in: "1-5,8-9,20-25,30"
|
||||
#
|
||||
# Note: this could be easily written without side effects,
|
||||
# we use 'slots' just to split the computation into steps.
|
||||
|
||||
# First step: we want an increasing array of integers
|
||||
# for instance: [1,2,3,4,5,8,9,20,21,22,23,24,25,30]
|
||||
slots = @slots.keys.sort
|
||||
|
||||
# As we want to aggregate adiacent slots we convert all the
|
||||
# slot integers into ranges (with just one element)
|
||||
# So we have something like [1..1,2..2, ... and so forth.
|
||||
slots.map!{|x| x..x}
|
||||
|
||||
# Finally we group ranges with adiacent elements.
|
||||
slots = slots.reduce([]) {|a,b|
|
||||
if !a.empty? && b.first == (a[-1].last)+1
|
||||
a[0..-2] + [(a[-1].first)..(b.last)]
|
||||
else
|
||||
a + [b]
|
||||
end
|
||||
}
|
||||
|
||||
# Now our task is easy, we just convert ranges with just one
|
||||
# element into a number, and a real range into a start-end format.
|
||||
# Finally we join the array using the comma as separator.
|
||||
slots = slots.map{|x|
|
||||
x.count == 1 ? x.first.to_s : "#{x.first}-#{x.last}"
|
||||
}.join(",")
|
||||
|
||||
"#{self.to_s.ljust(25)} slots:#{slots}"
|
||||
end
|
||||
|
||||
def info
|
||||
{
|
||||
:host => @host,
|
||||
:port => @port,
|
||||
:slots => @slots,
|
||||
:dirty => @dirty
|
||||
}
|
||||
end
|
||||
|
||||
def is_dirty?
|
||||
@dirty
|
||||
end
|
||||
|
||||
def r
|
||||
@r
|
||||
end
|
||||
end
|
||||
|
||||
class RedisTrib
|
||||
def initialize
|
||||
@nodes = []
|
||||
end
|
||||
|
||||
def check_arity(req_args, num_args)
|
||||
if ((req_args > 0 and num_args != req_args) ||
|
||||
(req_args < 0 and num_args < req_args.abs))
|
||||
puts "Wrong number of arguments for specified sub command"
|
||||
exit 1
|
||||
end
|
||||
end
|
||||
|
||||
def add_node(node)
|
||||
@nodes << node
|
||||
end
|
||||
|
||||
def check_cluster
|
||||
puts "Performing Cluster Check (using node #{@nodes[0]})"
|
||||
show_nodes
|
||||
end
|
||||
|
||||
def alloc_slots
|
||||
slots_per_node = ClusterHashSlots/@nodes.length
|
||||
i = 0
|
||||
@nodes.each{|n|
|
||||
first = i*slots_per_node
|
||||
last = first+slots_per_node-1
|
||||
last = ClusterHashSlots-1 if i == @nodes.length-1
|
||||
n.add_slots first..last
|
||||
i += 1
|
||||
}
|
||||
end
|
||||
|
||||
def flush_nodes_config
|
||||
@nodes.each{|n|
|
||||
n.flush_node_config
|
||||
}
|
||||
end
|
||||
|
||||
def show_nodes
|
||||
@nodes.each{|n|
|
||||
puts n.info_string
|
||||
}
|
||||
end
|
||||
|
||||
def join_cluster
|
||||
# We use a brute force approach to make sure the node will meet
|
||||
# each other, that is, sending CLUSTER MEET messages to all the nodes
|
||||
# about the very same node.
|
||||
# Thanks to gossip this information should propagate across all the
|
||||
# cluster in a matter of seconds.
|
||||
first = false
|
||||
@nodes.each{|n|
|
||||
if !first then first = n.info; next; end # Skip the first node
|
||||
n.r.cluster("meet",first[:host],first[:port])
|
||||
}
|
||||
end
|
||||
|
||||
def yes_or_die(msg)
|
||||
print "#{msg} (type 'yes' to accept): "
|
||||
STDOUT.flush
|
||||
if !(STDIN.gets.chomp.downcase == "yes")
|
||||
puts "Aborting..."
|
||||
exit 1
|
||||
end
|
||||
end
|
||||
|
||||
# redis-trib subcommands implementations
|
||||
|
||||
def check_cluster_cmd
|
||||
node = ClusterNode.new(ARGV[1])
|
||||
node.connect(:abort => true)
|
||||
node.assert_cluster
|
||||
add_node(node)
|
||||
check_cluster
|
||||
end
|
||||
|
||||
def create_cluster_cmd
|
||||
puts "Creating cluster"
|
||||
ARGV[1..-1].each{|n|
|
||||
node = ClusterNode.new(n)
|
||||
node.connect(:abort => true)
|
||||
node.assert_cluster
|
||||
node.assert_empty
|
||||
add_node(node)
|
||||
}
|
||||
puts "Performing hash slots allocation on #{@nodes.length} nodes..."
|
||||
alloc_slots
|
||||
show_nodes
|
||||
yes_or_die "Can I set the above configuration?"
|
||||
flush_nodes_config
|
||||
puts "** Nodes configuration updated"
|
||||
puts "** Sending CLUSTER MEET messages to join the cluster"
|
||||
join_cluster
|
||||
check_cluster
|
||||
end
|
||||
end
|
||||
|
||||
COMMANDS={
|
||||
"create" => ["create_cluster_cmd", -2, "host1:port host2:port ... hostN:port"],
|
||||
"check" => ["check_cluster_cmd", 2, "host:port"]
|
||||
}
|
||||
|
||||
# Sanity check
|
||||
if ARGV.length == 0
|
||||
puts "Usage: redis-trib <command> <arguments ...>"
|
||||
puts
|
||||
COMMANDS.each{|k,v|
|
||||
puts " #{k.ljust(20)} #{v[2]}"
|
||||
}
|
||||
puts
|
||||
exit 1
|
||||
end
|
||||
|
||||
rt = RedisTrib.new
|
||||
cmd_spec = COMMANDS[ARGV[0].downcase]
|
||||
if !cmd_spec
|
||||
puts "Unknown redis-trib subcommand '#{ARGV[0]}'"
|
||||
exit 1
|
||||
end
|
||||
rt.check_arity(cmd_spec[1],ARGV.length)
|
||||
|
||||
# Dispatch
|
||||
rt.send(cmd_spec[0])
|
149
src/redis.c
149
src/redis.c
@ -70,12 +70,12 @@ struct redisServer server; /* server global state */
|
||||
struct redisCommand *commandTable;
|
||||
struct redisCommand redisCommandTable[] = {
|
||||
{"get",getCommand,2,0,NULL,1,1,1,0,0},
|
||||
{"set",setCommand,3,REDIS_CMD_DENYOOM,NULL,0,0,0,0,0},
|
||||
{"setnx",setnxCommand,3,REDIS_CMD_DENYOOM,NULL,0,0,0,0,0},
|
||||
{"setex",setexCommand,4,REDIS_CMD_DENYOOM,NULL,0,0,0,0,0},
|
||||
{"set",setCommand,3,REDIS_CMD_DENYOOM,noPreloadGetKeys,1,1,1,0,0},
|
||||
{"setnx",setnxCommand,3,REDIS_CMD_DENYOOM,noPreloadGetKeys,1,1,1,0,0},
|
||||
{"setex",setexCommand,4,REDIS_CMD_DENYOOM,noPreloadGetKeys,2,2,1,0,0},
|
||||
{"append",appendCommand,3,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0},
|
||||
{"strlen",strlenCommand,2,0,NULL,1,1,1,0,0},
|
||||
{"del",delCommand,-2,0,NULL,0,0,0,0,0},
|
||||
{"del",delCommand,-2,0,noPreloadGetKeys,1,-1,1,0,0},
|
||||
{"exists",existsCommand,2,0,NULL,1,1,1,0,0},
|
||||
{"setbit",setbitCommand,4,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0},
|
||||
{"getbit",getbitCommand,3,0,NULL,1,1,1,0,0},
|
||||
@ -85,8 +85,8 @@ struct redisCommand redisCommandTable[] = {
|
||||
{"incr",incrCommand,2,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0},
|
||||
{"decr",decrCommand,2,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0},
|
||||
{"mget",mgetCommand,-2,0,NULL,1,-1,1,0,0},
|
||||
{"rpush",rpushCommand,3,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0},
|
||||
{"lpush",lpushCommand,3,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0},
|
||||
{"rpush",rpushCommand,-3,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0},
|
||||
{"lpush",lpushCommand,-3,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0},
|
||||
{"rpushx",rpushxCommand,3,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0},
|
||||
{"lpushx",lpushxCommand,3,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0},
|
||||
{"linsert",linsertCommand,5,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0},
|
||||
@ -94,7 +94,7 @@ struct redisCommand redisCommandTable[] = {
|
||||
{"lpop",lpopCommand,2,0,NULL,1,1,1,0,0},
|
||||
{"brpop",brpopCommand,-3,0,NULL,1,1,1,0,0},
|
||||
{"brpoplpush",brpoplpushCommand,4,REDIS_CMD_DENYOOM,NULL,1,2,1,0,0},
|
||||
{"blpop",blpopCommand,-3,0,NULL,1,1,1,0,0},
|
||||
{"blpop",blpopCommand,-3,0,NULL,1,-2,1,0,0},
|
||||
{"llen",llenCommand,2,0,NULL,1,1,1,0,0},
|
||||
{"lindex",lindexCommand,3,0,NULL,1,1,1,0,0},
|
||||
{"lset",lsetCommand,4,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0},
|
||||
@ -102,8 +102,8 @@ struct redisCommand redisCommandTable[] = {
|
||||
{"ltrim",ltrimCommand,4,0,NULL,1,1,1,0,0},
|
||||
{"lrem",lremCommand,4,0,NULL,1,1,1,0,0},
|
||||
{"rpoplpush",rpoplpushCommand,3,REDIS_CMD_DENYOOM,NULL,1,2,1,0,0},
|
||||
{"sadd",saddCommand,3,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0},
|
||||
{"srem",sremCommand,3,0,NULL,1,1,1,0,0},
|
||||
{"sadd",saddCommand,-3,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0},
|
||||
{"srem",sremCommand,-3,0,NULL,1,1,1,0,0},
|
||||
{"smove",smoveCommand,4,0,NULL,1,2,1,0,0},
|
||||
{"sismember",sismemberCommand,3,0,NULL,1,1,1,0,0},
|
||||
{"scard",scardCommand,2,0,NULL,1,1,1,0,0},
|
||||
@ -121,8 +121,8 @@ struct redisCommand redisCommandTable[] = {
|
||||
{"zrem",zremCommand,3,0,NULL,1,1,1,0,0},
|
||||
{"zremrangebyscore",zremrangebyscoreCommand,4,0,NULL,1,1,1,0,0},
|
||||
{"zremrangebyrank",zremrangebyrankCommand,4,0,NULL,1,1,1,0,0},
|
||||
{"zunionstore",zunionstoreCommand,-4,REDIS_CMD_DENYOOM,zunionInterBlockClientOnSwappedKeys,0,0,0,0,0},
|
||||
{"zinterstore",zinterstoreCommand,-4,REDIS_CMD_DENYOOM,zunionInterBlockClientOnSwappedKeys,0,0,0,0,0},
|
||||
{"zunionstore",zunionstoreCommand,-4,REDIS_CMD_DENYOOM,zunionInterGetKeys,0,0,0,0,0},
|
||||
{"zinterstore",zinterstoreCommand,-4,REDIS_CMD_DENYOOM,zunionInterGetKeys,0,0,0,0,0},
|
||||
{"zrange",zrangeCommand,-4,0,NULL,1,1,1,0,0},
|
||||
{"zrangebyscore",zrangebyscoreCommand,-4,0,NULL,1,1,1,0,0},
|
||||
{"zrevrangebyscore",zrevrangebyscoreCommand,-4,0,NULL,1,1,1,0,0},
|
||||
@ -138,7 +138,7 @@ struct redisCommand redisCommandTable[] = {
|
||||
{"hmset",hmsetCommand,-4,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0},
|
||||
{"hmget",hmgetCommand,-3,0,NULL,1,1,1,0,0},
|
||||
{"hincrby",hincrbyCommand,4,REDIS_CMD_DENYOOM,NULL,1,1,1,0,0},
|
||||
{"hdel",hdelCommand,3,0,NULL,1,1,1,0,0},
|
||||
{"hdel",hdelCommand,-3,0,NULL,1,1,1,0,0},
|
||||
{"hlen",hlenCommand,2,0,NULL,1,1,1,0,0},
|
||||
{"hkeys",hkeysCommand,2,0,NULL,1,1,1,0,0},
|
||||
{"hvals",hvalsCommand,2,0,NULL,1,1,1,0,0},
|
||||
@ -152,10 +152,10 @@ struct redisCommand redisCommandTable[] = {
|
||||
{"randomkey",randomkeyCommand,1,0,NULL,0,0,0,0,0},
|
||||
{"select",selectCommand,2,0,NULL,0,0,0,0,0},
|
||||
{"move",moveCommand,3,0,NULL,1,1,1,0,0},
|
||||
{"rename",renameCommand,3,0,NULL,1,1,1,0,0},
|
||||
{"renamenx",renamenxCommand,3,0,NULL,1,1,1,0,0},
|
||||
{"expire",expireCommand,3,0,NULL,0,0,0,0,0},
|
||||
{"expireat",expireatCommand,3,0,NULL,0,0,0,0,0},
|
||||
{"rename",renameCommand,3,0,renameGetKeys,1,2,1,0,0},
|
||||
{"renamenx",renamenxCommand,3,0,renameGetKeys,1,2,1,0,0},
|
||||
{"expire",expireCommand,3,0,NULL,1,1,1,0,0},
|
||||
{"expireat",expireatCommand,3,0,NULL,1,1,1,0,0},
|
||||
{"keys",keysCommand,2,0,NULL,0,0,0,0,0},
|
||||
{"dbsize",dbsizeCommand,1,0,NULL,0,0,0,0,0},
|
||||
{"auth",authCommand,2,0,NULL,0,0,0,0,0},
|
||||
@ -168,7 +168,7 @@ struct redisCommand redisCommandTable[] = {
|
||||
{"lastsave",lastsaveCommand,1,0,NULL,0,0,0,0,0},
|
||||
{"type",typeCommand,2,0,NULL,1,1,1,0,0},
|
||||
{"multi",multiCommand,1,0,NULL,0,0,0,0,0},
|
||||
{"exec",execCommand,1,REDIS_CMD_DENYOOM,execBlockClientOnSwappedKeys,0,0,0,0,0},
|
||||
{"exec",execCommand,1,REDIS_CMD_DENYOOM,NULL,0,0,0,0,0},
|
||||
{"discard",discardCommand,1,0,NULL,0,0,0,0,0},
|
||||
{"sync",syncCommand,1,0,NULL,0,0,0,0,0},
|
||||
{"flushdb",flushdbCommand,1,0,NULL,0,0,0,0,0},
|
||||
@ -186,8 +186,14 @@ struct redisCommand redisCommandTable[] = {
|
||||
{"psubscribe",psubscribeCommand,-2,0,NULL,0,0,0,0,0},
|
||||
{"punsubscribe",punsubscribeCommand,-1,0,NULL,0,0,0,0,0},
|
||||
{"publish",publishCommand,3,REDIS_CMD_FORCE_REPLICATION,NULL,0,0,0,0,0},
|
||||
{"watch",watchCommand,-2,0,NULL,0,0,0,0,0},
|
||||
{"unwatch",unwatchCommand,1,0,NULL,0,0,0,0,0}
|
||||
{"watch",watchCommand,-2,0,noPreloadGetKeys,1,-1,1,0,0},
|
||||
{"unwatch",unwatchCommand,1,0,NULL,0,0,0,0,0},
|
||||
{"cluster",clusterCommand,-2,0,NULL,0,0,0,0,0},
|
||||
{"restore",restoreCommand,4,0,NULL,0,0,0,0,0},
|
||||
{"migrate",migrateCommand,6,0,NULL,0,0,0,0,0},
|
||||
{"dump",dumpCommand,2,0,NULL,0,0,0,0,0},
|
||||
{"object",objectCommand,-2,0,NULL,0,0,0,0,0},
|
||||
{"client",clientCommand,-2,0,NULL,0,0,0,0,0}
|
||||
};
|
||||
|
||||
/*============================ Utility functions ============================ */
|
||||
@ -200,14 +206,20 @@ void redisLogRaw(int level, const char *msg) {
|
||||
time_t now = time(NULL);
|
||||
FILE *fp;
|
||||
char buf[64];
|
||||
int rawmode = (level & REDIS_LOG_RAW);
|
||||
|
||||
level &= 0xff; /* clear flags */
|
||||
if (level < server.verbosity) return;
|
||||
|
||||
fp = (server.logfile == NULL) ? stdout : fopen(server.logfile,"a");
|
||||
if (!fp) return;
|
||||
|
||||
strftime(buf,sizeof(buf),"%d %b %H:%M:%S",localtime(&now));
|
||||
fprintf(fp,"[%d] %s %c %s\n",(int)getpid(),buf,c[level],msg);
|
||||
if (rawmode) {
|
||||
fprintf(fp,"%s",msg);
|
||||
} else {
|
||||
strftime(buf,sizeof(buf),"%d %b %H:%M:%S",localtime(&now));
|
||||
fprintf(fp,"[%d] %s %c %s\n",(int)getpid(),buf,c[level],msg);
|
||||
}
|
||||
fflush(fp);
|
||||
|
||||
if (server.logfile) fclose(fp);
|
||||
@ -222,7 +234,7 @@ void redisLog(int level, const char *fmt, ...) {
|
||||
va_list ap;
|
||||
char msg[REDIS_MAX_LOGMSG_LEN];
|
||||
|
||||
if (level < server.verbosity) return;
|
||||
if ((level&0xff) < server.verbosity) return;
|
||||
|
||||
va_start(ap, fmt);
|
||||
vsnprintf(msg, sizeof(msg), fmt, ap);
|
||||
@ -440,6 +452,17 @@ dictType keylistDictType = {
|
||||
dictListDestructor /* val destructor */
|
||||
};
|
||||
|
||||
/* Cluster nodes hash table, mapping nodes addresses 1.2.3.4:6379 to
|
||||
* clusterNode structures. */
|
||||
dictType clusterNodesDictType = {
|
||||
dictSdsHash, /* hash function */
|
||||
NULL, /* key dup */
|
||||
NULL, /* val dup */
|
||||
dictSdsKeyCompare, /* key compare */
|
||||
dictSdsDestructor, /* key destructor */
|
||||
NULL /* val destructor */
|
||||
};
|
||||
|
||||
int htNeedsResize(dict *dict) {
|
||||
long long size, used;
|
||||
|
||||
@ -563,6 +586,10 @@ int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
|
||||
*/
|
||||
updateLRUClock();
|
||||
|
||||
/* Record the max memory used since the server was started. */
|
||||
if (zmalloc_used_memory() > server.stat_peak_memory)
|
||||
server.stat_peak_memory = zmalloc_used_memory();
|
||||
|
||||
/* We received a SIGTERM, shutting down here in a safe way, as it is
|
||||
* not ok doing so inside the signal handler. */
|
||||
if (server.shutdown_asap) {
|
||||
@ -669,6 +696,9 @@ int serverCron(struct aeEventLoop *eventLoop, long long id, void *clientData) {
|
||||
* to detect transfer failures. */
|
||||
if (!(loops % 10)) replicationCron();
|
||||
|
||||
/* Run other sub-systems specific cron jobs */
|
||||
if (server.cluster_enabled && !(loops % 10)) clusterCron();
|
||||
|
||||
server.cronloops++;
|
||||
return 100;
|
||||
}
|
||||
@ -821,8 +851,12 @@ void initServerConfig() {
|
||||
server.list_max_ziplist_entries = REDIS_LIST_MAX_ZIPLIST_ENTRIES;
|
||||
server.list_max_ziplist_value = REDIS_LIST_MAX_ZIPLIST_VALUE;
|
||||
server.set_max_intset_entries = REDIS_SET_MAX_INTSET_ENTRIES;
|
||||
server.zset_max_ziplist_entries = REDIS_ZSET_MAX_ZIPLIST_ENTRIES;
|
||||
server.zset_max_ziplist_value = REDIS_ZSET_MAX_ZIPLIST_VALUE;
|
||||
server.shutdown_asap = 0;
|
||||
server.cache_flush_delay = 0;
|
||||
server.cluster_enabled = 0;
|
||||
server.cluster.configfile = zstrdup("nodes.conf");
|
||||
|
||||
updateLRUClock();
|
||||
resetServerSaveParams();
|
||||
@ -928,6 +962,7 @@ void initServer() {
|
||||
server.stat_starttime = time(NULL);
|
||||
server.stat_keyspace_misses = 0;
|
||||
server.stat_keyspace_hits = 0;
|
||||
server.stat_peak_memory = 0;
|
||||
server.unixtime = time(NULL);
|
||||
aeCreateTimeEvent(server.el, 1, serverCron, NULL, NULL);
|
||||
if (server.ipfd > 0 && aeCreateFileEvent(server.el,server.ipfd,AE_READABLE,
|
||||
@ -945,6 +980,7 @@ void initServer() {
|
||||
}
|
||||
|
||||
if (server.ds_enabled) dsInit();
|
||||
if (server.cluster_enabled) clusterInit();
|
||||
srand(time(NULL)^getpid());
|
||||
}
|
||||
|
||||
@ -1051,6 +1087,29 @@ int processCommand(redisClient *c) {
|
||||
return REDIS_OK;
|
||||
}
|
||||
|
||||
/* If cluster is enabled, redirect here */
|
||||
if (server.cluster_enabled &&
|
||||
!(cmd->getkeys_proc == NULL && cmd->firstkey == 0)) {
|
||||
int hashslot;
|
||||
|
||||
if (server.cluster.state != REDIS_CLUSTER_OK) {
|
||||
addReplyError(c,"The cluster is down. Check with CLUSTER INFO for more information");
|
||||
return REDIS_OK;
|
||||
} else {
|
||||
int ask;
|
||||
clusterNode *n = getNodeByQuery(c,cmd,c->argv,c->argc,&hashslot,&ask);
|
||||
if (n == NULL) {
|
||||
addReplyError(c,"Multi keys request invalid in cluster");
|
||||
return REDIS_OK;
|
||||
} else if (n != server.cluster.myself) {
|
||||
addReplySds(c,sdscatprintf(sdsempty(),
|
||||
"-%s %d %s:%d\r\n", ask ? "ASK" : "MOVED",
|
||||
hashslot,n->ip,n->port));
|
||||
return REDIS_OK;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* Handle the maxmemory directive.
|
||||
*
|
||||
* First we try to free some memory if possible (if there are volatile
|
||||
@ -1189,7 +1248,6 @@ sds genRedisInfoString(char *section) {
|
||||
sds info = sdsempty();
|
||||
time_t uptime = time(NULL)-server.stat_starttime;
|
||||
int j, numcommands;
|
||||
char hmem[64];
|
||||
struct rusage self_ru, c_ru;
|
||||
unsigned long lol, bib;
|
||||
int allsections = 0, defsections = 0;
|
||||
@ -1203,7 +1261,6 @@ sds genRedisInfoString(char *section) {
|
||||
getrusage(RUSAGE_SELF, &self_ru);
|
||||
getrusage(RUSAGE_CHILDREN, &c_ru);
|
||||
getClientsMaxBuffers(&lol,&bib);
|
||||
bytesToHuman(hmem,zmalloc_used_memory());
|
||||
|
||||
/* Server */
|
||||
if (allsections || defsections || !strcasecmp(section,"server")) {
|
||||
@ -1248,23 +1305,28 @@ sds genRedisInfoString(char *section) {
|
||||
|
||||
/* Memory */
|
||||
if (allsections || defsections || !strcasecmp(section,"memory")) {
|
||||
char hmem[64];
|
||||
char peak_hmem[64];
|
||||
|
||||
bytesToHuman(hmem,zmalloc_used_memory());
|
||||
bytesToHuman(peak_hmem,server.stat_peak_memory);
|
||||
if (sections++) info = sdscat(info,"\r\n");
|
||||
info = sdscatprintf(info,
|
||||
"# Memory\r\n"
|
||||
"used_memory:%zu\r\n"
|
||||
"used_memory_human:%s\r\n"
|
||||
"used_memory_rss:%zu\r\n"
|
||||
"used_memory_peak:%zu\r\n"
|
||||
"used_memory_peak_human:%s\r\n"
|
||||
"mem_fragmentation_ratio:%.2f\r\n"
|
||||
"use_tcmalloc:%d\r\n",
|
||||
"mem_allocator:%s\r\n",
|
||||
zmalloc_used_memory(),
|
||||
hmem,
|
||||
zmalloc_get_rss(),
|
||||
server.stat_peak_memory,
|
||||
peak_hmem,
|
||||
zmalloc_get_fragmentation_ratio(),
|
||||
#ifdef USE_TCMALLOC
|
||||
1
|
||||
#else
|
||||
0
|
||||
#endif
|
||||
REDIS_MALLOC
|
||||
);
|
||||
}
|
||||
|
||||
@ -1455,6 +1517,15 @@ sds genRedisInfoString(char *section) {
|
||||
}
|
||||
}
|
||||
|
||||
/* Clusetr */
|
||||
if (allsections || defsections || !strcasecmp(section,"cluster")) {
|
||||
if (sections++) info = sdscat(info,"\r\n");
|
||||
info = sdscatprintf(info,
|
||||
"# Cluster\r\n"
|
||||
"cluster_enabled:%d\r\n",
|
||||
server.cluster_enabled);
|
||||
}
|
||||
|
||||
/* Key space */
|
||||
if (allsections || defsections || !strcasecmp(section,"keyspace")) {
|
||||
if (sections++) info = sdscat(info,"\r\n");
|
||||
@ -1663,6 +1734,23 @@ void usage() {
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void redisAsciiArt(void) {
|
||||
#include "asciilogo.h"
|
||||
char *buf = zmalloc(1024*16);
|
||||
|
||||
snprintf(buf,1024*16,ascii_logo,
|
||||
REDIS_VERSION,
|
||||
redisGitSHA1(),
|
||||
strtol(redisGitDirty(),NULL,10) > 0,
|
||||
(sizeof(long) == 8) ? "64" : "32",
|
||||
server.cluster_enabled ? "cluster" : "stand alone",
|
||||
server.port,
|
||||
(long) getpid()
|
||||
);
|
||||
redisLogRaw(REDIS_NOTICE|REDIS_LOG_RAW,buf);
|
||||
zfree(buf);
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
long long start;
|
||||
|
||||
@ -1681,6 +1769,7 @@ int main(int argc, char **argv) {
|
||||
if (server.daemonize) daemonize();
|
||||
initServer();
|
||||
if (server.daemonize) createPidFile();
|
||||
redisAsciiArt();
|
||||
redisLog(REDIS_NOTICE,"Server started, Redis version " REDIS_VERSION);
|
||||
#ifdef __linux__
|
||||
linuxOvercommitMemoryWarning();
|
||||
|
250
src/redis.h
250
src/redis.h
@ -18,6 +18,7 @@
|
||||
#include <inttypes.h>
|
||||
#include <pthread.h>
|
||||
#include <syslog.h>
|
||||
#include <netinet/in.h>
|
||||
|
||||
#include "ae.h" /* Event driven programming library */
|
||||
#include "sds.h" /* Dynamic safe strings */
|
||||
@ -29,6 +30,7 @@
|
||||
#include "ziplist.h" /* Compact list data structure */
|
||||
#include "intset.h" /* Compact integer set structure */
|
||||
#include "version.h"
|
||||
#include "util.h"
|
||||
|
||||
/* Error codes */
|
||||
#define REDIS_OK 0
|
||||
@ -70,10 +72,12 @@
|
||||
#define REDIS_ZSET 3
|
||||
#define REDIS_HASH 4
|
||||
#define REDIS_VMPOINTER 8
|
||||
|
||||
/* Object types only used for persistence in .rdb files */
|
||||
#define REDIS_HASH_ZIPMAP 9
|
||||
#define REDIS_LIST_ZIPLIST 10
|
||||
#define REDIS_SET_INTSET 11
|
||||
#define REDIS_ZSET_ZIPLIST 12
|
||||
|
||||
/* Objects encoding. Some kind of objects like Strings and Hashes can be
|
||||
* internally represented in multiple ways. The 'encoding' field of the object
|
||||
@ -85,6 +89,7 @@
|
||||
#define REDIS_ENCODING_LINKEDLIST 4 /* Encoded as regular linked list */
|
||||
#define REDIS_ENCODING_ZIPLIST 5 /* Encoded as ziplist */
|
||||
#define REDIS_ENCODING_INTSET 6 /* Encoded as intset */
|
||||
#define REDIS_ENCODING_SKIPLIST 7 /* Encoded as skiplist */
|
||||
|
||||
/* Object types only used for dumping to disk */
|
||||
#define REDIS_EXPIRETIME 253
|
||||
@ -177,6 +182,7 @@
|
||||
#define REDIS_VERBOSE 1
|
||||
#define REDIS_NOTICE 2
|
||||
#define REDIS_WARNING 3
|
||||
#define REDIS_LOG_RAW (1<<10) /* Modifier to log without timestamp */
|
||||
|
||||
/* Anti-warning macro... */
|
||||
#define REDIS_NOTUSED(V) ((void) V)
|
||||
@ -195,6 +201,8 @@
|
||||
#define REDIS_LIST_MAX_ZIPLIST_ENTRIES 512
|
||||
#define REDIS_LIST_MAX_ZIPLIST_VALUE 64
|
||||
#define REDIS_SET_MAX_INTSET_ENTRIES 512
|
||||
#define REDIS_ZSET_MAX_ZIPLIST_ENTRIES 128
|
||||
#define REDIS_ZSET_MAX_ZIPLIST_VALUE 64
|
||||
|
||||
/* Sets operations codes */
|
||||
#define REDIS_OP_UNION 0
|
||||
@ -360,7 +368,147 @@ struct sharedObjectsStruct {
|
||||
*integers[REDIS_SHARED_INTEGERS];
|
||||
};
|
||||
|
||||
/* Global server state structure */
|
||||
/* ZSETs use a specialized version of Skiplists */
|
||||
typedef struct zskiplistNode {
|
||||
robj *obj;
|
||||
double score;
|
||||
struct zskiplistNode *backward;
|
||||
struct zskiplistLevel {
|
||||
struct zskiplistNode *forward;
|
||||
unsigned int span;
|
||||
} level[];
|
||||
} zskiplistNode;
|
||||
|
||||
typedef struct zskiplist {
|
||||
struct zskiplistNode *header, *tail;
|
||||
unsigned long length;
|
||||
int level;
|
||||
} zskiplist;
|
||||
|
||||
typedef struct zset {
|
||||
dict *dict;
|
||||
zskiplist *zsl;
|
||||
} zset;
|
||||
|
||||
/*-----------------------------------------------------------------------------
|
||||
* Redis cluster data structures
|
||||
*----------------------------------------------------------------------------*/
|
||||
|
||||
#define REDIS_CLUSTER_SLOTS 4096
|
||||
#define REDIS_CLUSTER_OK 0 /* Everything looks ok */
|
||||
#define REDIS_CLUSTER_FAIL 1 /* The cluster can't work */
|
||||
#define REDIS_CLUSTER_NEEDHELP 2 /* The cluster works, but needs some help */
|
||||
#define REDIS_CLUSTER_NAMELEN 40 /* sha1 hex length */
|
||||
#define REDIS_CLUSTER_PORT_INCR 10000 /* Cluster port = baseport + PORT_INCR */
|
||||
|
||||
struct clusterNode;
|
||||
|
||||
/* clusterLink encapsulates everything needed to talk with a remote node. */
|
||||
typedef struct clusterLink {
|
||||
int fd; /* TCP socket file descriptor */
|
||||
sds sndbuf; /* Packet send buffer */
|
||||
sds rcvbuf; /* Packet reception buffer */
|
||||
struct clusterNode *node; /* Node related to this link if any, or NULL */
|
||||
} clusterLink;
|
||||
|
||||
/* Node flags */
|
||||
#define REDIS_NODE_MASTER 1 /* The node is a master */
|
||||
#define REDIS_NODE_SLAVE 2 /* The node is a slave */
|
||||
#define REDIS_NODE_PFAIL 4 /* Failure? Need acknowledge */
|
||||
#define REDIS_NODE_FAIL 8 /* The node is believed to be malfunctioning */
|
||||
#define REDIS_NODE_MYSELF 16 /* This node is myself */
|
||||
#define REDIS_NODE_HANDSHAKE 32 /* We have still to exchange the first ping */
|
||||
#define REDIS_NODE_NOADDR 64 /* We don't know the address of this node */
|
||||
#define REDIS_NODE_MEET 128 /* Send a MEET message to this node */
|
||||
#define REDIS_NODE_NULL_NAME "\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
|
||||
|
||||
struct clusterNode {
|
||||
char name[REDIS_CLUSTER_NAMELEN]; /* Node name, hex string, sha1-size */
|
||||
int flags; /* REDIS_NODE_... */
|
||||
unsigned char slots[REDIS_CLUSTER_SLOTS/8]; /* slots handled by this node */
|
||||
int numslaves; /* Number of slave nodes, if this is a master */
|
||||
struct clusterNode **slaves; /* pointers to slave nodes */
|
||||
struct clusterNode *slaveof; /* pointer to the master node */
|
||||
time_t ping_sent; /* Unix time we sent latest ping */
|
||||
time_t pong_received; /* Unix time we received the pong */
|
||||
char *configdigest; /* Configuration digest of this node */
|
||||
time_t configdigest_ts; /* Configuration digest timestamp */
|
||||
char ip[16]; /* Latest known IP address of this node */
|
||||
int port; /* Latest known port of this node */
|
||||
clusterLink *link; /* TCP/IP link with this node */
|
||||
};
|
||||
typedef struct clusterNode clusterNode;
|
||||
|
||||
typedef struct {
|
||||
char *configfile;
|
||||
clusterNode *myself; /* This node */
|
||||
int state; /* REDIS_CLUSTER_OK, REDIS_CLUSTER_FAIL, ... */
|
||||
int node_timeout;
|
||||
dict *nodes; /* Hash table of name -> clusterNode structures */
|
||||
clusterNode *migrating_slots_to[REDIS_CLUSTER_SLOTS];
|
||||
clusterNode *importing_slots_from[REDIS_CLUSTER_SLOTS];
|
||||
clusterNode *slots[REDIS_CLUSTER_SLOTS];
|
||||
zskiplist *slots_to_keys;
|
||||
} clusterState;
|
||||
|
||||
/* Redis cluster messages header */
|
||||
|
||||
/* Note that the PING, PONG and MEET messages are actually the same exact
|
||||
* kind of packet. PONG is the reply to ping, in the extact format as a PING,
|
||||
* while MEET is a special PING that forces the receiver to add the sender
|
||||
* as a node (if it is not already in the list). */
|
||||
#define CLUSTERMSG_TYPE_PING 0 /* Ping */
|
||||
#define CLUSTERMSG_TYPE_PONG 1 /* Pong (reply to Ping) */
|
||||
#define CLUSTERMSG_TYPE_MEET 2 /* Meet "let's join" message */
|
||||
#define CLUSTERMSG_TYPE_FAIL 3 /* Mark node xxx as failing */
|
||||
|
||||
/* Initially we don't know our "name", but we'll find it once we connect
|
||||
* to the first node, using the getsockname() function. Then we'll use this
|
||||
* address for all the next messages. */
|
||||
typedef struct {
|
||||
char nodename[REDIS_CLUSTER_NAMELEN];
|
||||
uint32_t ping_sent;
|
||||
uint32_t pong_received;
|
||||
char ip[16]; /* IP address last time it was seen */
|
||||
uint16_t port; /* port last time it was seen */
|
||||
uint16_t flags;
|
||||
uint32_t notused; /* for 64 bit alignment */
|
||||
} clusterMsgDataGossip;
|
||||
|
||||
typedef struct {
|
||||
char nodename[REDIS_CLUSTER_NAMELEN];
|
||||
} clusterMsgDataFail;
|
||||
|
||||
union clusterMsgData {
|
||||
/* PING, MEET and PONG */
|
||||
struct {
|
||||
/* Array of N clusterMsgDataGossip structures */
|
||||
clusterMsgDataGossip gossip[1];
|
||||
} ping;
|
||||
/* FAIL */
|
||||
struct {
|
||||
clusterMsgDataFail about;
|
||||
} fail;
|
||||
};
|
||||
|
||||
typedef struct {
|
||||
uint32_t totlen; /* Total length of this message */
|
||||
uint16_t type; /* Message type */
|
||||
uint16_t count; /* Only used for some kind of messages. */
|
||||
char sender[REDIS_CLUSTER_NAMELEN]; /* Name of the sender node */
|
||||
unsigned char myslots[REDIS_CLUSTER_SLOTS/8];
|
||||
char slaveof[REDIS_CLUSTER_NAMELEN];
|
||||
char configdigest[32];
|
||||
uint16_t port; /* Sender TCP base port */
|
||||
unsigned char state; /* Cluster state from the POV of the sender */
|
||||
unsigned char notused[5]; /* Reserved for future use. For alignment. */
|
||||
union clusterMsgData data;
|
||||
} clusterMsg;
|
||||
|
||||
/*-----------------------------------------------------------------------------
|
||||
* Global server state
|
||||
*----------------------------------------------------------------------------*/
|
||||
|
||||
struct redisServer {
|
||||
/* General */
|
||||
pthread_t mainthread;
|
||||
@ -373,6 +521,7 @@ struct redisServer {
|
||||
char *unixsocket;
|
||||
int ipfd;
|
||||
int sofd;
|
||||
int cfd;
|
||||
list *clients;
|
||||
list *slaves, *monitors;
|
||||
char neterr[ANET_ERR_LEN];
|
||||
@ -393,6 +542,7 @@ struct redisServer {
|
||||
long long stat_evictedkeys; /* number of evicted keys (maxmemory) */
|
||||
long long stat_keyspace_hits; /* number of successful lookups of keys */
|
||||
long long stat_keyspace_misses; /* number of failed lookups of keys */
|
||||
size_t stat_peak_memory; /* max used memory record */
|
||||
/* Configuration */
|
||||
int verbosity;
|
||||
int maxidletime;
|
||||
@ -468,6 +618,8 @@ struct redisServer {
|
||||
size_t list_max_ziplist_entries;
|
||||
size_t list_max_ziplist_value;
|
||||
size_t set_max_intset_entries;
|
||||
size_t zset_max_ziplist_entries;
|
||||
size_t zset_max_ziplist_value;
|
||||
time_t unixtime; /* Unix time sampled every second. */
|
||||
/* Virtual memory I/O threads stuff */
|
||||
/* An I/O thread process an element taken from the io_jobs queue and
|
||||
@ -499,6 +651,9 @@ struct redisServer {
|
||||
/* Misc */
|
||||
unsigned lruclock:22; /* clock incrementing every minute, for LRU */
|
||||
unsigned lruclock_padding:10;
|
||||
/* Cluster */
|
||||
int cluster_enabled;
|
||||
clusterState cluster;
|
||||
};
|
||||
|
||||
typedef struct pubsubPattern {
|
||||
@ -507,20 +662,19 @@ typedef struct pubsubPattern {
|
||||
} pubsubPattern;
|
||||
|
||||
typedef void redisCommandProc(redisClient *c);
|
||||
typedef void redisVmPreloadProc(redisClient *c, struct redisCommand *cmd, int argc, robj **argv);
|
||||
typedef int *redisGetKeysProc(struct redisCommand *cmd, robj **argv, int argc, int *numkeys, int flags);
|
||||
struct redisCommand {
|
||||
char *name;
|
||||
redisCommandProc *proc;
|
||||
int arity;
|
||||
int flags;
|
||||
/* Use a function to determine which keys need to be loaded
|
||||
* in the background prior to executing this command. Takes precedence
|
||||
* over vm_firstkey and others, ignored when NULL */
|
||||
redisVmPreloadProc *vm_preload_proc;
|
||||
/* Use a function to determine keys arguments in a command line.
|
||||
* Used both for diskstore preloading and Redis Cluster. */
|
||||
redisGetKeysProc *getkeys_proc;
|
||||
/* What keys should be loaded in background when calling this command? */
|
||||
int vm_firstkey; /* The first argument that's a key (0 = no keys) */
|
||||
int vm_lastkey; /* THe last argument that's a key */
|
||||
int vm_keystep; /* The step between first and last key */
|
||||
int firstkey; /* The first argument that's a key (0 = no keys) */
|
||||
int lastkey; /* THe last argument that's a key */
|
||||
int keystep; /* The step between first and last key */
|
||||
long long microseconds, calls;
|
||||
};
|
||||
|
||||
@ -542,28 +696,6 @@ typedef struct _redisSortOperation {
|
||||
robj *pattern;
|
||||
} redisSortOperation;
|
||||
|
||||
/* ZSETs use a specialized version of Skiplists */
|
||||
typedef struct zskiplistNode {
|
||||
robj *obj;
|
||||
double score;
|
||||
struct zskiplistNode *backward;
|
||||
struct zskiplistLevel {
|
||||
struct zskiplistNode *forward;
|
||||
unsigned int span;
|
||||
} level[];
|
||||
} zskiplistNode;
|
||||
|
||||
typedef struct zskiplist {
|
||||
struct zskiplistNode *header, *tail;
|
||||
unsigned long length;
|
||||
int level;
|
||||
} zskiplist;
|
||||
|
||||
typedef struct zset {
|
||||
dict *dict;
|
||||
zskiplist *zsl;
|
||||
} zset;
|
||||
|
||||
/* DIsk store threaded I/O request message */
|
||||
#define REDIS_IOJOB_LOAD 0
|
||||
#define REDIS_IOJOB_SAVE 1
|
||||
@ -634,6 +766,7 @@ extern struct redisServer server;
|
||||
extern struct sharedObjectsStruct shared;
|
||||
extern dictType setDictType;
|
||||
extern dictType zsetDictType;
|
||||
extern dictType clusterNodesDictType;
|
||||
extern double R_Zero, R_PosInf, R_NegInf, R_Nan;
|
||||
dictType hashDictType;
|
||||
|
||||
@ -720,6 +853,7 @@ void freeHashObject(robj *o);
|
||||
robj *createObject(int type, void *ptr);
|
||||
robj *createStringObject(char *ptr, size_t len);
|
||||
robj *dupStringObject(robj *o);
|
||||
int isObjectRepresentableAsLongLong(robj *o, long long *llongval);
|
||||
robj *tryObjectEncoding(robj *o);
|
||||
robj *getDecodedObject(robj *o);
|
||||
size_t stringObjectLen(robj *o);
|
||||
@ -730,6 +864,7 @@ robj *createSetObject(void);
|
||||
robj *createIntsetObject(void);
|
||||
robj *createHashObject(void);
|
||||
robj *createZsetObject(void);
|
||||
robj *createZsetZiplistObject(void);
|
||||
int getLongFromObjectOrReply(redisClient *c, robj *o, long *target, const char *msg);
|
||||
int checkType(redisClient *c, robj *o, int type);
|
||||
int getLongLongFromObjectOrReply(redisClient *c, robj *o, long long *target, const char *msg);
|
||||
@ -748,6 +883,7 @@ int fwriteBulkString(FILE *fp, char *s, unsigned long len);
|
||||
int fwriteBulkDouble(FILE *fp, double d);
|
||||
int fwriteBulkLongLong(FILE *fp, long long l);
|
||||
int fwriteBulkObject(FILE *fp, robj *obj);
|
||||
int fwriteBulkCount(FILE *fp, char prefix, int count);
|
||||
|
||||
/* Replication */
|
||||
void replicationFeedSlaves(list *slaves, int dictid, robj **argv, int argc);
|
||||
@ -789,9 +925,24 @@ int startAppendOnly(void);
|
||||
void backgroundRewriteDoneHandler(int exitcode, int bysignal);
|
||||
|
||||
/* Sorted sets data type */
|
||||
|
||||
/* Struct to hold a inclusive/exclusive range spec. */
|
||||
typedef struct {
|
||||
double min, max;
|
||||
int minex, maxex; /* are min or max exclusive? */
|
||||
} zrangespec;
|
||||
|
||||
zskiplist *zslCreate(void);
|
||||
void zslFree(zskiplist *zsl);
|
||||
zskiplistNode *zslInsert(zskiplist *zsl, double score, robj *obj);
|
||||
unsigned char *zzlInsert(unsigned char *zl, robj *ele, double score);
|
||||
int zslDelete(zskiplist *zsl, double score, robj *obj);
|
||||
zskiplistNode *zslFirstInRange(zskiplist *zsl, zrangespec range);
|
||||
double zzlGetScore(unsigned char *sptr);
|
||||
void zzlNext(unsigned char *zl, unsigned char **eptr, unsigned char **sptr);
|
||||
void zzlPrev(unsigned char *zl, unsigned char **eptr, unsigned char **sptr);
|
||||
unsigned int zsetLength(robj *zobj);
|
||||
void zsetConvert(robj *zobj, int encoding);
|
||||
|
||||
/* Core functions */
|
||||
void freeMemoryIfNeeded(void);
|
||||
@ -829,8 +980,6 @@ void freeIOJob(iojob *j);
|
||||
void queueIOJob(iojob *j);
|
||||
void waitEmptyIOJobsQueue(void);
|
||||
void processAllPendingIOJobs(void);
|
||||
void zunionInterBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv);
|
||||
void execBlockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd, int argc, robj **argv);
|
||||
int blockClientOnSwappedKeys(redisClient *c, struct redisCommand *cmd);
|
||||
int dontWaitForSwappedKey(redisClient *c, robj *key);
|
||||
void handleClientsBlockedOnSwappedKey(redisDb *db, robj *key);
|
||||
@ -881,16 +1030,6 @@ int pubsubUnsubscribeAllPatterns(redisClient *c, int notify);
|
||||
void freePubsubPattern(void *p);
|
||||
int listMatchPubsubPattern(void *a, void *b);
|
||||
|
||||
/* Utility functions */
|
||||
int stringmatchlen(const char *pattern, int patternLen,
|
||||
const char *string, int stringLen, int nocase);
|
||||
int stringmatch(const char *pattern, const char *string, int nocase);
|
||||
long long memtoll(const char *p, int *err);
|
||||
int ll2string(char *s, size_t len, long long value);
|
||||
int isStringRepresentableAsLong(sds s, long *longval);
|
||||
int isStringRepresentableAsLongLong(sds s, long long *longval);
|
||||
int isObjectRepresentableAsLongLong(robj *o, long long *llongval);
|
||||
|
||||
/* Configuration */
|
||||
void loadServerConfig(char *filename);
|
||||
void appendServerSaveParams(time_t seconds, int changes);
|
||||
@ -916,6 +1055,25 @@ long long emptyDb();
|
||||
int selectDb(redisClient *c, int id);
|
||||
void signalModifiedKey(redisDb *db, robj *key);
|
||||
void signalFlushedDb(int dbid);
|
||||
unsigned int GetKeysInSlot(unsigned int hashslot, robj **keys, unsigned int count);
|
||||
|
||||
/* API to get key arguments from commands */
|
||||
#define REDIS_GETKEYS_ALL 0
|
||||
#define REDIS_GETKEYS_PRELOAD 1
|
||||
int *getKeysFromCommand(struct redisCommand *cmd, robj **argv, int argc, int *numkeys, int flags);
|
||||
void getKeysFreeResult(int *result);
|
||||
int *noPreloadGetKeys(struct redisCommand *cmd,robj **argv, int argc, int *numkeys, int flags);
|
||||
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);
|
||||
|
||||
/* Cluster */
|
||||
void clusterInit(void);
|
||||
unsigned short crc16(const char *buf, int len);
|
||||
unsigned int keyHashSlot(char *key, int keylen);
|
||||
clusterNode *createClusterNode(char *nodename, int flags);
|
||||
int clusterAddNode(clusterNode *node);
|
||||
void clusterCron(void);
|
||||
clusterNode *getNodeByQuery(redisClient *c, struct redisCommand *cmd, robj **argv, int argc, int *hashslot, int *ask);
|
||||
|
||||
/* Git SHA1 */
|
||||
char *redisGitSHA1(void);
|
||||
@ -1039,6 +1197,12 @@ void punsubscribeCommand(redisClient *c);
|
||||
void publishCommand(redisClient *c);
|
||||
void watchCommand(redisClient *c);
|
||||
void unwatchCommand(redisClient *c);
|
||||
void clusterCommand(redisClient *c);
|
||||
void restoreCommand(redisClient *c);
|
||||
void migrateCommand(redisClient *c);
|
||||
void dumpCommand(redisClient *c);
|
||||
void objectCommand(redisClient *c);
|
||||
void clientCommand(redisClient *c);
|
||||
|
||||
#if defined(__GNUC__)
|
||||
void *calloc(size_t count, size_t size) __attribute__ ((deprecated));
|
||||
|
43
src/sds.c
43
src/sds.c
@ -36,11 +36,11 @@
|
||||
|
||||
#define SDS_ABORT_ON_OOM
|
||||
|
||||
#include "sds.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include "sds.h"
|
||||
#include "zmalloc.h"
|
||||
|
||||
static void sdsOomAbort(void) {
|
||||
@ -51,7 +51,11 @@ static void sdsOomAbort(void) {
|
||||
sds sdsnewlen(const void *init, size_t initlen) {
|
||||
struct sdshdr *sh;
|
||||
|
||||
sh = zmalloc(sizeof(struct sdshdr)+initlen+1);
|
||||
if (init) {
|
||||
sh = zmalloc(sizeof(struct sdshdr)+initlen+1);
|
||||
} else {
|
||||
sh = zcalloc(sizeof(struct sdshdr)+initlen+1);
|
||||
}
|
||||
#ifdef SDS_ABORT_ON_OOM
|
||||
if (sh == NULL) sdsOomAbort();
|
||||
#else
|
||||
@ -59,10 +63,8 @@ sds sdsnewlen(const void *init, size_t initlen) {
|
||||
#endif
|
||||
sh->len = initlen;
|
||||
sh->free = 0;
|
||||
if (initlen) {
|
||||
if (init) memcpy(sh->buf, init, initlen);
|
||||
else memset(sh->buf,0,initlen);
|
||||
}
|
||||
if (initlen && init)
|
||||
memcpy(sh->buf, init, initlen);
|
||||
sh->buf[initlen] = '\0';
|
||||
return (char*)sh->buf;
|
||||
}
|
||||
@ -76,11 +78,6 @@ sds sdsnew(const char *init) {
|
||||
return sdsnewlen(init, initlen);
|
||||
}
|
||||
|
||||
size_t sdslen(const sds s) {
|
||||
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
|
||||
return sh->len;
|
||||
}
|
||||
|
||||
sds sdsdup(const sds s) {
|
||||
return sdsnewlen(s, sdslen(s));
|
||||
}
|
||||
@ -90,11 +87,6 @@ void sdsfree(sds s) {
|
||||
zfree(s-sizeof(struct sdshdr));
|
||||
}
|
||||
|
||||
size_t sdsavail(sds s) {
|
||||
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
|
||||
return sh->free;
|
||||
}
|
||||
|
||||
void sdsupdatelen(sds s) {
|
||||
struct sdshdr *sh = (void*) (s-(sizeof(struct sdshdr)));
|
||||
int reallen = strlen(s);
|
||||
@ -306,15 +298,17 @@ int sdscmp(sds s1, sds s2) {
|
||||
*/
|
||||
sds *sdssplitlen(char *s, int len, char *sep, int seplen, int *count) {
|
||||
int elements = 0, slots = 5, start = 0, j;
|
||||
sds *tokens;
|
||||
|
||||
sds *tokens = zmalloc(sizeof(sds)*slots);
|
||||
if (seplen < 1 || len < 0) return NULL;
|
||||
|
||||
tokens = zmalloc(sizeof(sds)*slots);
|
||||
#ifdef SDS_ABORT_ON_OOM
|
||||
if (tokens == NULL) sdsOomAbort();
|
||||
#else
|
||||
if (tokens == NULL) return NULL;
|
||||
#endif
|
||||
if (seplen < 1 || len < 0 || tokens == NULL) {
|
||||
*count = 0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (len == 0) {
|
||||
*count = 0;
|
||||
return tokens;
|
||||
@ -552,6 +546,13 @@ err:
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void sdssplitargs_free(sds *argv, int argc) {
|
||||
int j;
|
||||
|
||||
for (j = 0 ;j < argc; j++) sdsfree(argv[j]);
|
||||
zfree(argv);
|
||||
}
|
||||
|
||||
#ifdef SDS_TEST_MAIN
|
||||
#include <stdio.h>
|
||||
#include "testhelp.h"
|
||||
|
11
src/sds.h
11
src/sds.h
@ -42,6 +42,16 @@ struct sdshdr {
|
||||
char buf[];
|
||||
};
|
||||
|
||||
static inline size_t sdslen(const sds s) {
|
||||
struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
|
||||
return sh->len;
|
||||
}
|
||||
|
||||
static inline size_t sdsavail(const sds s) {
|
||||
struct sdshdr *sh = (void*)(s-(sizeof(struct sdshdr)));
|
||||
return sh->free;
|
||||
}
|
||||
|
||||
sds sdsnewlen(const void *init, size_t initlen);
|
||||
sds sdsnew(const char *init);
|
||||
sds sdsempty();
|
||||
@ -74,5 +84,6 @@ void sdstoupper(sds s);
|
||||
sds sdsfromlonglong(long long value);
|
||||
sds sdscatrepr(sds s, char *p, size_t len);
|
||||
sds *sdssplitargs(char *line, int *argc);
|
||||
void sdssplitargs_free(sds *argv, int argc);
|
||||
|
||||
#endif
|
||||
|
@ -199,6 +199,9 @@ void sortCommand(redisClient *c) {
|
||||
j++;
|
||||
}
|
||||
|
||||
/* Destructively convert encoded sorted sets for SORT. */
|
||||
if (sortval->type == REDIS_ZSET) zsetConvert(sortval, REDIS_ENCODING_SKIPLIST);
|
||||
|
||||
/* Load the sorting vector with all the objects to sort */
|
||||
switch(sortval->type) {
|
||||
case REDIS_LIST: vectorlen = listTypeLength(sortval); break;
|
||||
|
14
src/syncio.c
14
src/syncio.c
@ -107,6 +107,7 @@ int syncReadLine(int fd, char *ptr, ssize_t size, int timeout) {
|
||||
int fwriteBulkString(FILE *fp, char *s, unsigned long len) {
|
||||
char cbuf[128];
|
||||
int clen;
|
||||
|
||||
cbuf[0] = '$';
|
||||
clen = 1+ll2string(cbuf+1,sizeof(cbuf)-1,len);
|
||||
cbuf[clen++] = '\r';
|
||||
@ -117,6 +118,19 @@ int fwriteBulkString(FILE *fp, char *s, unsigned long len) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Write a multi bulk count in the form "*<count>\r\n" */
|
||||
int fwriteBulkCount(FILE *fp, char prefix, int count) {
|
||||
char cbuf[128];
|
||||
int clen;
|
||||
|
||||
cbuf[0] = prefix;
|
||||
clen = 1+ll2string(cbuf+1,sizeof(cbuf)-1,count);
|
||||
cbuf[clen++] = '\r';
|
||||
cbuf[clen++] = '\n';
|
||||
if (fwrite(cbuf,clen,1,fp) == 0) return 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Write a double value in bulk format $<count>\r\n<payload>\r\n */
|
||||
int fwriteBulkDouble(FILE *fp, double d) {
|
||||
char buf[128], dbuf[128];
|
||||
|
19
src/t_hash.c
19
src/t_hash.c
@ -396,17 +396,22 @@ void hmgetCommand(redisClient *c) {
|
||||
|
||||
void hdelCommand(redisClient *c) {
|
||||
robj *o;
|
||||
int j, deleted = 0;
|
||||
|
||||
if ((o = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
|
||||
checkType(c,o,REDIS_HASH)) return;
|
||||
|
||||
if (hashTypeDelete(o,c->argv[2])) {
|
||||
if (hashTypeLength(o) == 0) dbDelete(c->db,c->argv[1]);
|
||||
addReply(c,shared.cone);
|
||||
signalModifiedKey(c->db,c->argv[1]);
|
||||
server.dirty++;
|
||||
} else {
|
||||
addReply(c,shared.czero);
|
||||
for (j = 2; j < c->argc; j++) {
|
||||
if (hashTypeDelete(o,c->argv[j])) {
|
||||
if (hashTypeLength(o) == 0) dbDelete(c->db,c->argv[1]);
|
||||
deleted++;
|
||||
}
|
||||
}
|
||||
if (deleted) {
|
||||
signalModifiedKey(c->db,c->argv[1]);
|
||||
server.dirty += deleted;
|
||||
}
|
||||
addReplyLongLong(c,deleted);
|
||||
}
|
||||
|
||||
void hlenCommand(redisClient *c) {
|
||||
|
49
src/t_list.c
49
src/t_list.c
@ -259,30 +259,35 @@ void listTypeConvert(robj *subject, int enc) {
|
||||
*----------------------------------------------------------------------------*/
|
||||
|
||||
void pushGenericCommand(redisClient *c, int where) {
|
||||
int j, addlen = 0, pushed = 0;
|
||||
robj *lobj = lookupKeyWrite(c->db,c->argv[1]);
|
||||
c->argv[2] = tryObjectEncoding(c->argv[2]);
|
||||
if (lobj == NULL) {
|
||||
if (handleClientsWaitingListPush(c,c->argv[1],c->argv[2])) {
|
||||
addReply(c,shared.cone);
|
||||
return;
|
||||
}
|
||||
lobj = createZiplistObject();
|
||||
dbAdd(c->db,c->argv[1],lobj);
|
||||
} else {
|
||||
if (lobj->type != REDIS_LIST) {
|
||||
addReply(c,shared.wrongtypeerr);
|
||||
return;
|
||||
}
|
||||
if (handleClientsWaitingListPush(c,c->argv[1],c->argv[2])) {
|
||||
signalModifiedKey(c->db,c->argv[1]);
|
||||
addReply(c,shared.cone);
|
||||
return;
|
||||
}
|
||||
int may_have_waiting_clients = (lobj == NULL);
|
||||
|
||||
if (lobj && lobj->type != REDIS_LIST) {
|
||||
addReply(c,shared.wrongtypeerr);
|
||||
return;
|
||||
}
|
||||
listTypePush(lobj,c->argv[2],where);
|
||||
addReplyLongLong(c,listTypeLength(lobj));
|
||||
signalModifiedKey(c->db,c->argv[1]);
|
||||
server.dirty++;
|
||||
|
||||
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])) {
|
||||
addlen++;
|
||||
continue;
|
||||
} else {
|
||||
may_have_waiting_clients = 0;
|
||||
}
|
||||
}
|
||||
if (!lobj) {
|
||||
lobj = createZiplistObject();
|
||||
dbAdd(c->db,c->argv[1],lobj);
|
||||
}
|
||||
listTypePush(lobj,c->argv[j],where);
|
||||
pushed++;
|
||||
}
|
||||
addReplyLongLong(c,addlen + (lobj ? listTypeLength(lobj) : 0));
|
||||
if (pushed) signalModifiedKey(c->db,c->argv[1]);
|
||||
server.dirty += pushed;
|
||||
}
|
||||
|
||||
void lpushCommand(redisClient *c) {
|
||||
|
34
src/t_set.c
34
src/t_set.c
@ -218,9 +218,9 @@ void setTypeConvert(robj *setobj, int enc) {
|
||||
|
||||
void saddCommand(redisClient *c) {
|
||||
robj *set;
|
||||
int j, added = 0;
|
||||
|
||||
set = lookupKeyWrite(c->db,c->argv[1]);
|
||||
c->argv[2] = tryObjectEncoding(c->argv[2]);
|
||||
if (set == NULL) {
|
||||
set = setTypeCreate(c->argv[2]);
|
||||
dbAdd(c->db,c->argv[1],set);
|
||||
@ -230,30 +230,34 @@ void saddCommand(redisClient *c) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (setTypeAdd(set,c->argv[2])) {
|
||||
signalModifiedKey(c->db,c->argv[1]);
|
||||
server.dirty++;
|
||||
addReply(c,shared.cone);
|
||||
} else {
|
||||
addReply(c,shared.czero);
|
||||
|
||||
for (j = 2; j < c->argc; j++) {
|
||||
c->argv[j] = tryObjectEncoding(c->argv[j]);
|
||||
if (setTypeAdd(set,c->argv[j])) added++;
|
||||
}
|
||||
if (added) signalModifiedKey(c->db,c->argv[1]);
|
||||
server.dirty += added;
|
||||
addReplyLongLong(c,added);
|
||||
}
|
||||
|
||||
void sremCommand(redisClient *c) {
|
||||
robj *set;
|
||||
int j, deleted = 0;
|
||||
|
||||
if ((set = lookupKeyWriteOrReply(c,c->argv[1],shared.czero)) == NULL ||
|
||||
checkType(c,set,REDIS_SET)) return;
|
||||
|
||||
c->argv[2] = tryObjectEncoding(c->argv[2]);
|
||||
if (setTypeRemove(set,c->argv[2])) {
|
||||
if (setTypeSize(set) == 0) dbDelete(c->db,c->argv[1]);
|
||||
signalModifiedKey(c->db,c->argv[1]);
|
||||
server.dirty++;
|
||||
addReply(c,shared.cone);
|
||||
} else {
|
||||
addReply(c,shared.czero);
|
||||
for (j = 2; j < c->argc; j++) {
|
||||
if (setTypeRemove(set,c->argv[j])) {
|
||||
if (setTypeSize(set) == 0) dbDelete(c->db,c->argv[1]);
|
||||
deleted++;
|
||||
}
|
||||
}
|
||||
if (deleted) {
|
||||
signalModifiedKey(c->db,c->argv[1]);
|
||||
server.dirty += deleted;
|
||||
}
|
||||
addReplyLongLong(c,deleted);
|
||||
}
|
||||
|
||||
void smoveCommand(redisClient *c) {
|
||||
|
1701
src/t_zset.c
1701
src/t_zset.c
File diff suppressed because it is too large
Load Diff
273
src/util.c
273
src/util.c
@ -1,6 +1,10 @@
|
||||
#include "redis.h"
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <ctype.h>
|
||||
#include <limits.h>
|
||||
#include <math.h>
|
||||
#include "util.h"
|
||||
|
||||
/* Glob-style pattern matching. */
|
||||
int stringmatchlen(const char *pattern, int patternLen,
|
||||
@ -200,44 +204,237 @@ int ll2string(char *s, size_t len, long long value) {
|
||||
return l;
|
||||
}
|
||||
|
||||
/* Check if the sds string 's' can be represented by a long long
|
||||
* (that is, is a number that fits into long without any other space or
|
||||
* character before or after the digits, so that converting this number
|
||||
* back to a string will result in the same bytes as the original string).
|
||||
*
|
||||
* If so, the function returns REDIS_OK and *llongval is set to the value
|
||||
* of the number. Otherwise REDIS_ERR is returned */
|
||||
int isStringRepresentableAsLongLong(sds s, long long *llongval) {
|
||||
char buf[32], *endptr;
|
||||
long long value;
|
||||
int slen;
|
||||
/* Convert a string into a long long. Returns 1 if the string could be parsed
|
||||
* into a (non-overflowing) long long, 0 otherwise. The value will be set to
|
||||
* the parsed value when appropriate. */
|
||||
int string2ll(char *s, size_t slen, long long *value) {
|
||||
char *p = s;
|
||||
size_t plen = 0;
|
||||
int negative = 0;
|
||||
unsigned long long v;
|
||||
|
||||
value = strtoll(s, &endptr, 10);
|
||||
if (endptr[0] != '\0') return REDIS_ERR;
|
||||
slen = ll2string(buf,32,value);
|
||||
if (plen == slen)
|
||||
return 0;
|
||||
|
||||
/* If the number converted back into a string is not identical
|
||||
* then it's not possible to encode the string as integer */
|
||||
if (sdslen(s) != (unsigned)slen || memcmp(buf,s,slen)) return REDIS_ERR;
|
||||
if (llongval) *llongval = value;
|
||||
return REDIS_OK;
|
||||
}
|
||||
|
||||
int isStringRepresentableAsLong(sds s, long *longval) {
|
||||
long long ll;
|
||||
|
||||
if (isStringRepresentableAsLongLong(s,&ll) == REDIS_ERR) return REDIS_ERR;
|
||||
if (ll < LONG_MIN || ll > LONG_MAX) return REDIS_ERR;
|
||||
*longval = (long)ll;
|
||||
return REDIS_OK;
|
||||
}
|
||||
|
||||
int isObjectRepresentableAsLongLong(robj *o, long long *llongval) {
|
||||
redisAssert(o->type == REDIS_STRING);
|
||||
if (o->encoding == REDIS_ENCODING_INT) {
|
||||
if (llongval) *llongval = (long) o->ptr;
|
||||
return REDIS_OK;
|
||||
} else {
|
||||
return isStringRepresentableAsLongLong(o->ptr,llongval);
|
||||
/* Special case: first and only digit is 0. */
|
||||
if (slen == 1 && p[0] == '0') {
|
||||
if (value != NULL) *value = 0;
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (p[0] == '-') {
|
||||
negative = 1;
|
||||
p++; plen++;
|
||||
|
||||
/* Abort on only a negative sign. */
|
||||
if (plen == slen)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* First digit should be 1-9, otherwise the string should just be 0. */
|
||||
if (p[0] >= '1' && p[0] <= '9') {
|
||||
v = p[0]-'0';
|
||||
p++; plen++;
|
||||
} else if (p[0] == '0' && slen == 1) {
|
||||
*value = 0;
|
||||
return 1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
|
||||
while (plen < slen && p[0] >= '0' && p[0] <= '9') {
|
||||
if (v > (ULLONG_MAX / 10)) /* Overflow. */
|
||||
return 0;
|
||||
v *= 10;
|
||||
|
||||
if (v > (ULLONG_MAX - (p[0]-'0'))) /* Overflow. */
|
||||
return 0;
|
||||
v += p[0]-'0';
|
||||
|
||||
p++; plen++;
|
||||
}
|
||||
|
||||
/* Return if not all bytes were used. */
|
||||
if (plen < slen)
|
||||
return 0;
|
||||
|
||||
if (negative) {
|
||||
if (v > ((unsigned long long)(-(LLONG_MIN+1))+1)) /* Overflow. */
|
||||
return 0;
|
||||
if (value != NULL) *value = -v;
|
||||
} else {
|
||||
if (v > LLONG_MAX) /* Overflow. */
|
||||
return 0;
|
||||
if (value != NULL) *value = v;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Convert a string into a long. Returns 1 if the string could be parsed into a
|
||||
* (non-overflowing) long, 0 otherwise. The value will be set to the parsed
|
||||
* value when appropriate. */
|
||||
int string2l(char *s, size_t slen, long *lval) {
|
||||
long long llval;
|
||||
|
||||
if (!string2ll(s,slen,&llval))
|
||||
return 0;
|
||||
|
||||
if (llval < LONG_MIN || llval > LONG_MAX)
|
||||
return 0;
|
||||
|
||||
*lval = (long)llval;
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* Convert a double to a string representation. Returns the number of bytes
|
||||
* required. The representation should always be parsable by stdtod(3). */
|
||||
int d2string(char *buf, size_t len, double value) {
|
||||
if (isnan(value)) {
|
||||
len = snprintf(buf,len,"nan");
|
||||
} else if (isinf(value)) {
|
||||
if (value < 0)
|
||||
len = snprintf(buf,len,"-inf");
|
||||
else
|
||||
len = snprintf(buf,len,"inf");
|
||||
} else if (value == 0) {
|
||||
/* See: http://en.wikipedia.org/wiki/Signed_zero, "Comparisons". */
|
||||
if (1.0/value < 0)
|
||||
len = snprintf(buf,len,"-0");
|
||||
else
|
||||
len = snprintf(buf,len,"0");
|
||||
} else {
|
||||
#if (DBL_MANT_DIG >= 52) && (LLONG_MAX == 0x7fffffffffffffffLL)
|
||||
/* Check if the float is in a safe range to be casted into a
|
||||
* long long. We are assuming that long long is 64 bit here.
|
||||
* Also we are assuming that there are no implementations around where
|
||||
* double has precision < 52 bit.
|
||||
*
|
||||
* Under this assumptions we test if a double is inside an interval
|
||||
* where casting to long long is safe. Then using two castings we
|
||||
* make sure the decimal part is zero. If all this is true we use
|
||||
* 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)))
|
||||
len = ll2string(buf,len,(long long)value);
|
||||
else
|
||||
#endif
|
||||
len = snprintf(buf,len,"%.17g",value);
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
#ifdef UTIL_TEST_MAIN
|
||||
#include <assert.h>
|
||||
|
||||
void test_string2ll(void) {
|
||||
char buf[32];
|
||||
long long v;
|
||||
|
||||
/* May not start with +. */
|
||||
strcpy(buf,"+1");
|
||||
assert(string2ll(buf,strlen(buf),&v) == 0);
|
||||
|
||||
/* Leading space. */
|
||||
strcpy(buf," 1");
|
||||
assert(string2ll(buf,strlen(buf),&v) == 0);
|
||||
|
||||
/* Trailing space. */
|
||||
strcpy(buf,"1 ");
|
||||
assert(string2ll(buf,strlen(buf),&v) == 0);
|
||||
|
||||
/* May not start with 0. */
|
||||
strcpy(buf,"01");
|
||||
assert(string2ll(buf,strlen(buf),&v) == 0);
|
||||
|
||||
strcpy(buf,"-1");
|
||||
assert(string2ll(buf,strlen(buf),&v) == 1);
|
||||
assert(v == -1);
|
||||
|
||||
strcpy(buf,"0");
|
||||
assert(string2ll(buf,strlen(buf),&v) == 1);
|
||||
assert(v == 0);
|
||||
|
||||
strcpy(buf,"1");
|
||||
assert(string2ll(buf,strlen(buf),&v) == 1);
|
||||
assert(v == 1);
|
||||
|
||||
strcpy(buf,"99");
|
||||
assert(string2ll(buf,strlen(buf),&v) == 1);
|
||||
assert(v == 99);
|
||||
|
||||
strcpy(buf,"-99");
|
||||
assert(string2ll(buf,strlen(buf),&v) == 1);
|
||||
assert(v == -99);
|
||||
|
||||
strcpy(buf,"-9223372036854775808");
|
||||
assert(string2ll(buf,strlen(buf),&v) == 1);
|
||||
assert(v == LLONG_MIN);
|
||||
|
||||
strcpy(buf,"-9223372036854775809"); /* overflow */
|
||||
assert(string2ll(buf,strlen(buf),&v) == 0);
|
||||
|
||||
strcpy(buf,"9223372036854775807");
|
||||
assert(string2ll(buf,strlen(buf),&v) == 1);
|
||||
assert(v == LLONG_MAX);
|
||||
|
||||
strcpy(buf,"9223372036854775808"); /* overflow */
|
||||
assert(string2ll(buf,strlen(buf),&v) == 0);
|
||||
}
|
||||
|
||||
void test_string2l(void) {
|
||||
char buf[32];
|
||||
long v;
|
||||
|
||||
/* May not start with +. */
|
||||
strcpy(buf,"+1");
|
||||
assert(string2l(buf,strlen(buf),&v) == 0);
|
||||
|
||||
/* May not start with 0. */
|
||||
strcpy(buf,"01");
|
||||
assert(string2l(buf,strlen(buf),&v) == 0);
|
||||
|
||||
strcpy(buf,"-1");
|
||||
assert(string2l(buf,strlen(buf),&v) == 1);
|
||||
assert(v == -1);
|
||||
|
||||
strcpy(buf,"0");
|
||||
assert(string2l(buf,strlen(buf),&v) == 1);
|
||||
assert(v == 0);
|
||||
|
||||
strcpy(buf,"1");
|
||||
assert(string2l(buf,strlen(buf),&v) == 1);
|
||||
assert(v == 1);
|
||||
|
||||
strcpy(buf,"99");
|
||||
assert(string2l(buf,strlen(buf),&v) == 1);
|
||||
assert(v == 99);
|
||||
|
||||
strcpy(buf,"-99");
|
||||
assert(string2l(buf,strlen(buf),&v) == 1);
|
||||
assert(v == -99);
|
||||
|
||||
#if LONG_MAX != LLONG_MAX
|
||||
strcpy(buf,"-2147483648");
|
||||
assert(string2l(buf,strlen(buf),&v) == 1);
|
||||
assert(v == LONG_MIN);
|
||||
|
||||
strcpy(buf,"-2147483649"); /* overflow */
|
||||
assert(string2l(buf,strlen(buf),&v) == 0);
|
||||
|
||||
strcpy(buf,"2147483647");
|
||||
assert(string2l(buf,strlen(buf),&v) == 1);
|
||||
assert(v == LONG_MAX);
|
||||
|
||||
strcpy(buf,"2147483648"); /* overflow */
|
||||
assert(string2l(buf,strlen(buf),&v) == 0);
|
||||
#endif
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
test_string2ll();
|
||||
test_string2l();
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
12
src/util.h
Normal file
12
src/util.h
Normal file
@ -0,0 +1,12 @@
|
||||
#ifndef __REDIS_UTIL_H
|
||||
#define __REDIS_UTIL_H
|
||||
|
||||
int stringmatchlen(const char *p, int plen, const char *s, int slen, int nocase);
|
||||
int stringmatch(const char *p, const char *s, int nocase);
|
||||
long long memtoll(const char *p, int *err);
|
||||
int ll2string(char *s, size_t len, long long value);
|
||||
int string2ll(char *s, size_t slen, long long *value);
|
||||
int string2l(char *s, size_t slen, long *value);
|
||||
int d2string(char *buf, size_t len, double value);
|
||||
|
||||
#endif
|
@ -1 +1 @@
|
||||
#define REDIS_VERSION "2.3.0"
|
||||
#define REDIS_VERSION "2.9.0"
|
||||
|
@ -67,11 +67,10 @@
|
||||
#include <assert.h>
|
||||
#include <limits.h>
|
||||
#include "zmalloc.h"
|
||||
#include "util.h"
|
||||
#include "ziplist.h"
|
||||
#include "endian.h"
|
||||
|
||||
int ll2string(char *s, size_t len, long long value);
|
||||
|
||||
#define ZIP_END 255
|
||||
#define ZIP_BIGLEN 254
|
||||
|
||||
@ -252,22 +251,9 @@ static int zipPrevLenByteDiff(unsigned char *p, unsigned int len) {
|
||||
* Stores the integer value in 'v' and its encoding in 'encoding'. */
|
||||
static int zipTryEncoding(unsigned char *entry, unsigned int entrylen, long long *v, unsigned char *encoding) {
|
||||
long long value;
|
||||
char *eptr;
|
||||
char buf[32];
|
||||
|
||||
if (entrylen >= 32 || entrylen == 0) return 0;
|
||||
if (entry[0] == '-' || (entry[0] >= '0' && entry[0] <= '9')) {
|
||||
int slen;
|
||||
|
||||
/* Perform a back-and-forth conversion to make sure that
|
||||
* the string turned into an integer is not losing any info. */
|
||||
memcpy(buf,entry,entrylen);
|
||||
buf[entrylen] = '\0';
|
||||
value = strtoll(buf,&eptr,10);
|
||||
if (eptr[0] != '\0') return 0;
|
||||
slen = ll2string(buf,32,value);
|
||||
if (entrylen != (unsigned)slen || memcmp(buf,entry,slen)) return 0;
|
||||
|
||||
if (string2ll((char*)entry,entrylen,&value)) {
|
||||
/* Great, the string can be encoded. Check what's the smallest
|
||||
* of our encoding types that can hold this value. */
|
||||
if (value >= INT16_MIN && value <= INT16_MAX) {
|
||||
@ -385,8 +371,8 @@ static unsigned char *ziplistResize(unsigned char *zl, unsigned int len) {
|
||||
* The pointer "p" points to the first entry that does NOT need to be
|
||||
* updated, i.e. consecutive fields MAY need an update. */
|
||||
static unsigned char *__ziplistCascadeUpdate(unsigned char *zl, unsigned char *p) {
|
||||
unsigned int curlen = ZIPLIST_BYTES(zl), rawlen, rawlensize;
|
||||
unsigned int offset, noffset, extra;
|
||||
size_t curlen = ZIPLIST_BYTES(zl), rawlen, rawlensize;
|
||||
size_t offset, noffset, extra;
|
||||
unsigned char *np;
|
||||
zlentry cur, next;
|
||||
|
||||
@ -408,12 +394,17 @@ static unsigned char *__ziplistCascadeUpdate(unsigned char *zl, unsigned char *p
|
||||
offset = p-zl;
|
||||
extra = rawlensize-next.prevrawlensize;
|
||||
zl = ziplistResize(zl,curlen+extra);
|
||||
ZIPLIST_TAIL_OFFSET(zl) += extra;
|
||||
p = zl+offset;
|
||||
|
||||
/* Move the tail to the back. */
|
||||
/* Current pointer and offset for next element. */
|
||||
np = p+rawlen;
|
||||
noffset = np-zl;
|
||||
|
||||
/* Update tail offset when next element is not the tail element. */
|
||||
if ((zl+ZIPLIST_TAIL_OFFSET(zl)) != np)
|
||||
ZIPLIST_TAIL_OFFSET(zl) += extra;
|
||||
|
||||
/* Move the tail to the back. */
|
||||
memmove(np+rawlensize,
|
||||
np+next.prevrawlensize,
|
||||
curlen-noffset-next.prevrawlensize-1);
|
||||
@ -441,7 +432,8 @@ static unsigned char *__ziplistCascadeUpdate(unsigned char *zl, unsigned char *p
|
||||
/* Delete "num" entries, starting at "p". Returns pointer to the ziplist. */
|
||||
static unsigned char *__ziplistDelete(unsigned char *zl, unsigned char *p, unsigned int num) {
|
||||
unsigned int i, totlen, deleted = 0;
|
||||
int offset, nextdiff = 0;
|
||||
size_t offset;
|
||||
int nextdiff = 0;
|
||||
zlentry first, tail;
|
||||
|
||||
first = zipEntry(p);
|
||||
@ -493,8 +485,9 @@ static unsigned char *__ziplistDelete(unsigned char *zl, unsigned char *p, unsig
|
||||
|
||||
/* Insert item at "p". */
|
||||
static unsigned char *__ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char *s, unsigned int slen) {
|
||||
unsigned int curlen = ZIPLIST_BYTES(zl), reqlen, prevlen = 0;
|
||||
unsigned int offset, nextdiff = 0;
|
||||
size_t curlen = ZIPLIST_BYTES(zl), reqlen, prevlen = 0;
|
||||
size_t offset;
|
||||
int nextdiff = 0;
|
||||
unsigned char encoding = 0;
|
||||
long long value;
|
||||
zlentry entry, tail;
|
||||
@ -678,7 +671,7 @@ unsigned char *ziplistInsert(unsigned char *zl, unsigned char *p, unsigned char
|
||||
* Also update *p in place, to be able to iterate over the
|
||||
* ziplist, while deleting entries. */
|
||||
unsigned char *ziplistDelete(unsigned char *zl, unsigned char **p) {
|
||||
unsigned int offset = *p-zl;
|
||||
size_t offset = *p-zl;
|
||||
zl = __ziplistDelete(zl,*p,1);
|
||||
|
||||
/* Store pointer to current element in p, because ziplistDelete will
|
||||
@ -887,7 +880,7 @@ void pop(unsigned char *zl, int where) {
|
||||
}
|
||||
}
|
||||
|
||||
void randstring(char *target, unsigned int min, unsigned int max) {
|
||||
int randstring(char *target, unsigned int min, unsigned int max) {
|
||||
int p, len = min+rand()%(max-min+1);
|
||||
int minval, maxval;
|
||||
switch(rand() % 3) {
|
||||
@ -909,10 +902,9 @@ void randstring(char *target, unsigned int min, unsigned int max) {
|
||||
|
||||
while(p < len)
|
||||
target[p++] = minval+rand()%(maxval-minval+1);
|
||||
return;
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
unsigned char *zl, *p;
|
||||
unsigned char *entry;
|
||||
@ -1245,6 +1237,7 @@ int main(int argc, char **argv) {
|
||||
int i,j,len,where;
|
||||
unsigned char *p;
|
||||
char buf[1024];
|
||||
int buflen;
|
||||
list *ref;
|
||||
listNode *refnode;
|
||||
|
||||
@ -1253,10 +1246,6 @@ int main(int argc, char **argv) {
|
||||
unsigned int slen;
|
||||
long long sval;
|
||||
|
||||
/* In the regression for the cascade bug, it was triggered
|
||||
* with a random seed of 2. */
|
||||
srand(2);
|
||||
|
||||
for (i = 0; i < 20000; i++) {
|
||||
zl = ziplistNew();
|
||||
ref = listCreate();
|
||||
@ -1266,31 +1255,32 @@ int main(int argc, char **argv) {
|
||||
/* Create lists */
|
||||
for (j = 0; j < len; j++) {
|
||||
where = (rand() & 1) ? ZIPLIST_HEAD : ZIPLIST_TAIL;
|
||||
switch(rand() % 4) {
|
||||
case 0:
|
||||
sprintf(buf,"%lld",(0LL + rand()) >> 20);
|
||||
break;
|
||||
case 1:
|
||||
sprintf(buf,"%lld",(0LL + rand()));
|
||||
break;
|
||||
case 2:
|
||||
sprintf(buf,"%lld",(0LL + rand()) << 20);
|
||||
break;
|
||||
case 3:
|
||||
randstring(buf,0,256);
|
||||
break;
|
||||
default:
|
||||
assert(NULL);
|
||||
if (rand() % 2) {
|
||||
buflen = randstring(buf,1,sizeof(buf)-1);
|
||||
} else {
|
||||
switch(rand() % 3) {
|
||||
case 0:
|
||||
buflen = sprintf(buf,"%lld",(0LL + rand()) >> 20);
|
||||
break;
|
||||
case 1:
|
||||
buflen = sprintf(buf,"%lld",(0LL + rand()));
|
||||
break;
|
||||
case 2:
|
||||
buflen = sprintf(buf,"%lld",(0LL + rand()) << 20);
|
||||
break;
|
||||
default:
|
||||
assert(NULL);
|
||||
}
|
||||
}
|
||||
|
||||
/* Add to ziplist */
|
||||
zl = ziplistPush(zl, (unsigned char*)buf, strlen(buf), where);
|
||||
zl = ziplistPush(zl, (unsigned char*)buf, buflen, where);
|
||||
|
||||
/* Add to reference list */
|
||||
if (where == ZIPLIST_HEAD) {
|
||||
listAddNodeHead(ref,sdsnew(buf));
|
||||
listAddNodeHead(ref,sdsnewlen(buf, buflen));
|
||||
} else if (where == ZIPLIST_TAIL) {
|
||||
listAddNodeTail(ref,sdsnew(buf));
|
||||
listAddNodeTail(ref,sdsnewlen(buf, buflen));
|
||||
} else {
|
||||
assert(NULL);
|
||||
}
|
||||
@ -1305,12 +1295,13 @@ int main(int argc, char **argv) {
|
||||
|
||||
assert(ziplistGet(p,&sstr,&slen,&sval));
|
||||
if (sstr == NULL) {
|
||||
sprintf(buf,"%lld",sval);
|
||||
buflen = sprintf(buf,"%lld",sval);
|
||||
} else {
|
||||
memcpy(buf,sstr,slen);
|
||||
buf[slen] = '\0';
|
||||
buflen = slen;
|
||||
memcpy(buf,sstr,buflen);
|
||||
buf[buflen] = '\0';
|
||||
}
|
||||
assert(strcmp(buf,listNodeValue(refnode)) == 0);
|
||||
assert(memcmp(buf,listNodeValue(refnode),buflen) == 0);
|
||||
}
|
||||
zfree(zl);
|
||||
listRelease(ref);
|
||||
|
@ -31,13 +31,14 @@ tags {"aof"} {
|
||||
}
|
||||
|
||||
start_server_aof [list dir $server_path] {
|
||||
test {Unfinished MULTI: Server should not have been started} {
|
||||
is_alive $srv
|
||||
} {0}
|
||||
test "Unfinished MULTI: Server should not have been started" {
|
||||
assert_equal 0 [is_alive $srv]
|
||||
}
|
||||
|
||||
test {Unfinished MULTI: Server should have logged an error} {
|
||||
exec cat [dict get $srv stdout] | tail -n1
|
||||
} {*Unexpected end of file reading the append only file*}
|
||||
test "Unfinished MULTI: Server should have logged an error" {
|
||||
set result [exec cat [dict get $srv stdout] | tail -n1]
|
||||
assert_match "*Unexpected end of file reading the append only file*" $result
|
||||
}
|
||||
}
|
||||
|
||||
## Test that the server exits when the AOF contains a short read
|
||||
@ -47,36 +48,57 @@ tags {"aof"} {
|
||||
}
|
||||
|
||||
start_server_aof [list dir $server_path] {
|
||||
test {Short read: Server should not have been started} {
|
||||
is_alive $srv
|
||||
} {0}
|
||||
test "Short read: Server should not have been started" {
|
||||
assert_equal 0 [is_alive $srv]
|
||||
}
|
||||
|
||||
test {Short read: Server should have logged an error} {
|
||||
exec cat [dict get $srv stdout] | tail -n1
|
||||
} {*Bad file format reading the append only file*}
|
||||
test "Short read: Server should have logged an error" {
|
||||
set result [exec cat [dict get $srv stdout] | tail -n1]
|
||||
assert_match "*Bad file format reading the append only file*" $result
|
||||
}
|
||||
}
|
||||
|
||||
## Test that redis-check-aof indeed sees this AOF is not valid
|
||||
test {Short read: Utility should confirm the AOF is not valid} {
|
||||
test "Short read: Utility should confirm the AOF is not valid" {
|
||||
catch {
|
||||
exec src/redis-check-aof $aof_path
|
||||
} str
|
||||
set _ $str
|
||||
} {*not valid*}
|
||||
} result
|
||||
assert_match "*not valid*" $result
|
||||
}
|
||||
|
||||
test {Short read: Utility should be able to fix the AOF} {
|
||||
exec echo y | src/redis-check-aof --fix $aof_path
|
||||
} {*Successfully truncated AOF*}
|
||||
test "Short read: Utility should be able to fix the AOF" {
|
||||
set result [exec echo y | src/redis-check-aof --fix $aof_path]
|
||||
assert_match "*Successfully truncated AOF*" $result
|
||||
}
|
||||
|
||||
## Test that the server can be started using the truncated AOF
|
||||
start_server_aof [list dir $server_path] {
|
||||
test {Fixed AOF: Server should have been started} {
|
||||
is_alive $srv
|
||||
} {1}
|
||||
test "Fixed AOF: Server should have been started" {
|
||||
assert_equal 1 [is_alive $srv]
|
||||
}
|
||||
|
||||
test {Fixed AOF: Keyspace should contain values that were parsable} {
|
||||
test "Fixed AOF: Keyspace should contain values that were parsable" {
|
||||
set client [redis [dict get $srv host] [dict get $srv port]]
|
||||
list [$client get foo] [$client get bar]
|
||||
} {hello {}}
|
||||
assert_equal "hello" [$client get foo]
|
||||
assert_equal "" [$client get bar]
|
||||
}
|
||||
}
|
||||
|
||||
## Test that SPOP (that modifies the client its argc/argv) is correctly free'd
|
||||
create_aof {
|
||||
append_to_aof [formatCommand sadd set foo]
|
||||
append_to_aof [formatCommand sadd set bar]
|
||||
append_to_aof [formatCommand spop set]
|
||||
}
|
||||
|
||||
start_server_aof [list dir $server_path] {
|
||||
test "AOF+SPOP: Server should have been started" {
|
||||
assert_equal 1 [is_alive $srv]
|
||||
}
|
||||
|
||||
test "AOF+SPOP: Set should have 1 member" {
|
||||
set client [redis [dict get $srv host] [dict get $srv port]]
|
||||
assert_equal 1 [$client scard set]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -214,7 +214,7 @@ proc start_server {options {code undefined}} {
|
||||
|
||||
# find out the pid
|
||||
while {![info exists pid]} {
|
||||
regexp {^\[(\d+)\]} [exec head -n1 $stdout] _ pid
|
||||
regexp {\[(\d+)\]} [exec cat $stdout] _ pid
|
||||
after 100
|
||||
}
|
||||
|
||||
|
@ -127,6 +127,7 @@ proc execute_everything {} {
|
||||
# execute_tests "integration/redis-cli"
|
||||
execute_tests "unit/pubsub"
|
||||
|
||||
return; # No diskstore tests for now...
|
||||
# run tests with diskstore enabled
|
||||
puts "\nRunning diskstore tests... this is slow, press Ctrl+C if not interested.."
|
||||
set ::diskstore 1
|
||||
|
@ -226,6 +226,15 @@ start_server {tags {"hash"}} {
|
||||
set _ $rv
|
||||
} {0 0 1 0 {} 1 0 {}}
|
||||
|
||||
test {HDEL - more than a single value} {
|
||||
set rv {}
|
||||
r del myhash
|
||||
r hmset myhash a 1 b 2 c 3
|
||||
assert_equal 0 [r hdel myhash x y]
|
||||
assert_equal 2 [r hdel myhash a c f]
|
||||
r hgetall myhash
|
||||
} {b 2}
|
||||
|
||||
test {HEXISTS} {
|
||||
set rv {}
|
||||
set k [lindex [array names smallhash *] 0]
|
||||
|
@ -55,6 +55,13 @@ start_server {
|
||||
assert_equal $largevalue(linkedlist) [r lindex mylist2 2]
|
||||
}
|
||||
|
||||
test {Variadic RPUSH/LPUSH} {
|
||||
r del mylist
|
||||
assert_equal 4 [r lpush mylist a b c d]
|
||||
assert_equal 8 [r rpush mylist 0 1 2 3]
|
||||
assert_equal {d c b a 0 1 2 3} [r lrange mylist 0 -1]
|
||||
}
|
||||
|
||||
test {DEL a list - ziplist} {
|
||||
assert_equal 1 [r del myziplist2]
|
||||
assert_equal 0 [r exists myziplist2]
|
||||
@ -142,6 +149,15 @@ start_server {
|
||||
}
|
||||
}
|
||||
|
||||
test "BLPOP with variadic LPUSH" {
|
||||
set rd [redis_deferring_client]
|
||||
r del blist target
|
||||
$rd blpop blist 0
|
||||
assert_equal 2 [r lpush blist foo bar]
|
||||
assert_equal {blist foo} [$rd read]
|
||||
assert_equal bar [lindex [r lrange blist 0 -1] 0]
|
||||
}
|
||||
|
||||
test "BRPOPLPUSH with zero timeout should block indefinitely" {
|
||||
set rd [redis_deferring_client]
|
||||
r del blist target
|
||||
|
@ -59,6 +59,13 @@ start_server {
|
||||
assert_encoding hashtable myset
|
||||
}
|
||||
|
||||
test {Variadic SADD} {
|
||||
r del myset
|
||||
assert_equal 3 [r sadd myset a b c]
|
||||
assert_equal 2 [r sadd myset A a b c B]
|
||||
assert_equal [lsort {A a b c B}] [lsort [r smembers myset]]
|
||||
}
|
||||
|
||||
test "Set encoding after DEBUG RELOAD" {
|
||||
r del myintset myhashset mylargeintset
|
||||
for {set i 0} {$i < 100} {incr i} { r sadd myintset $i }
|
||||
@ -90,6 +97,14 @@ start_server {
|
||||
assert_equal {3 5} [lsort [r smembers myset]]
|
||||
}
|
||||
|
||||
test {SREM with multiple arguments} {
|
||||
r del myset
|
||||
r sadd myset a b c d
|
||||
assert_equal 0 [r srem myset k k k]
|
||||
assert_equal 2 [r srem myset b d x y]
|
||||
lsort [r smembers myset]
|
||||
} {a c}
|
||||
|
||||
foreach {type} {hashtable intset} {
|
||||
for {set i 1} {$i <= 5} {incr i} {
|
||||
r del [format "set%d" $i]
|
||||
|
File diff suppressed because it is too large
Load Diff
Loading…
x
Reference in New Issue
Block a user