Code Monkey home page Code Monkey logo

jedis's Introduction

This README is just a fast quick start document. You can find more detailed documentation at redis.io.

What is Redis?

Redis is often referred to as a data structures server. What this means is that Redis provides access to mutable data structures via a set of commands, which are sent using a server-client model with TCP sockets and a simple protocol. So different processes can query and modify the same data structures in a shared way.

Data structures implemented into Redis have a few special properties:

  • Redis cares to store them on disk, even if they are always served and modified into the server memory. This means that Redis is fast, but that it is also non-volatile.
  • The implementation of data structures emphasizes memory efficiency, so data structures inside Redis will likely use less memory compared to the same data structure modelled using a high-level programming language.
  • Redis offers a number of features that are natural to find in a database, like replication, tunable levels of durability, clustering, and high availability.

Another good example is to think of Redis as a more complex version of memcached, where the operations are not just SETs and GETs, but operations that work with complex data types like Lists, Sets, ordered data structures, and so forth.

If you want to know more, this is a list of selected starting points:

Building Redis

Redis can be compiled and used on Linux, OSX, OpenBSD, NetBSD, FreeBSD. We support big endian and little endian architectures, and both 32 bit and 64 bit systems.

It may compile on Solaris derived systems (for instance SmartOS) but our support for this platform is best effort and Redis is not guaranteed to work as well as in Linux, OSX, and *BSD.

It is as simple as:

% make

To build with TLS support, you'll need OpenSSL development libraries (e.g. libssl-dev on Debian/Ubuntu) and run:

% make BUILD_TLS=yes

To build with systemd support, you'll need systemd development libraries (such as libsystemd-dev on Debian/Ubuntu or systemd-devel on CentOS) and run:

% make USE_SYSTEMD=yes

To append a suffix to Redis program names, use:

% make PROG_SUFFIX="-alt"

You can build a 32 bit Redis binary using:

% make 32bit

After building Redis, it is a good idea to test it using:

% make test

If TLS is built, running the tests with TLS enabled (you will need tcl-tls installed):

% ./utils/gen-test-certs.sh
% ./runtest --tls

Fixing build problems with dependencies or cached build options

Redis has some dependencies which are included in the deps directory. make does not automatically rebuild dependencies even if something in the source code of dependencies changes.

When you update the source code with git pull or when code inside the dependencies tree is modified in any other way, make sure to use the following command in order to really clean everything and rebuild from scratch:

% make distclean

This will clean: jemalloc, lua, hiredis, linenoise and other dependencies.

Also if you force certain build options like 32bit target, no C compiler optimizations (for debugging purposes), and other similar build time options, those options are cached indefinitely until you issue a make distclean command.

Fixing problems building 32 bit binaries

If after building Redis with a 32 bit target you need to rebuild it with a 64 bit target, or the other way around, you need to perform a make distclean in the root directory of the Redis distribution.

In case of build errors when trying to build a 32 bit binary of Redis, try the following steps:

  • Install the package libc6-dev-i386 (also try g++-multilib).
  • Try using the following command line instead of make 32bit: make CFLAGS="-m32 -march=native" LDFLAGS="-m32"

Allocator

Selecting a non-default memory allocator when building Redis is done by setting the MALLOC environment variable. Redis is compiled and linked against libc malloc by default, with the exception of jemalloc being the default on Linux systems. This default was picked because jemalloc has proven to have fewer fragmentation problems than libc malloc.

To force compiling against libc malloc, use:

% make MALLOC=libc

To compile against jemalloc on Mac OS X systems, use:

% make MALLOC=jemalloc

Monotonic clock

By default, Redis will build using the POSIX clock_gettime function as the monotonic clock source. On most modern systems, the internal processor clock can be used to improve performance. Cautions can be found here: http://oliveryang.net/2015/09/pitfalls-of-TSC-usage/

To build with support for the processor's internal instruction clock, use:

% make CFLAGS="-DUSE_PROCESSOR_CLOCK"

Verbose build

Redis will build with a user-friendly colorized output by default. If you want to see a more verbose output, use the following:

% make V=1

Running Redis

To run Redis with the default configuration, just type:

% cd src
% ./redis-server

If you want to provide your redis.conf, you have to run it using an additional parameter (the path of the configuration file):

% cd src
% ./redis-server /path/to/redis.conf

It is possible to alter the Redis configuration by passing parameters directly as options using the command line. Examples:

% ./redis-server --port 9999 --replicaof 127.0.0.1 6379
% ./redis-server /etc/redis/6379.conf --loglevel debug

All the options in redis.conf are also supported as options using the command line, with exactly the same name.

Running Redis with TLS:

Please consult the TLS.md file for more information on how to use Redis with TLS.

Playing with Redis

You can use redis-cli to play with Redis. Start a redis-server instance, then in another terminal try the following:

% cd src
% ./redis-cli
redis> ping
PONG
redis> set foo bar
OK
redis> get foo
"bar"
redis> incr mycounter
(integer) 1
redis> incr mycounter
(integer) 2
redis>

You can find the list of all the available commands at https://redis.io/commands.

Installing Redis

In order to install Redis binaries into /usr/local/bin, just use:

% make install

You can use make PREFIX=/some/other/directory install if you wish to use a different destination.

make install will just install binaries in your system, but will not configure init scripts and configuration files in the appropriate place. This is not needed if you just want to play a bit with Redis, but if you are installing it the proper way for a production system, we have a script that does this for Ubuntu and Debian systems:

% cd utils
% ./install_server.sh

Note: install_server.sh will not work on Mac OSX; it is built for Linux only.

The script will ask you a few questions and will setup everything you need to run Redis properly as a background daemon that will start again on system reboots.

You'll be able to stop and start Redis using the script named /etc/init.d/redis_<portnumber>, for instance /etc/init.d/redis_6379.

Code contributions

By contributing code to the Redis project in any form, including sending a pull request via GitHub, a code fragment or patch via private email or public discussion groups, you agree to release your code under the terms of the Redis Software Grant and Contributor License Agreement. Redis software contains contributions to the original Redis core project, which are owned by their contributors and licensed under the 3BSD license. Any copy of that license in this repository applies only to those contributions. Redis releases all Redis project versions from 7.4.x and thereafter under the RSALv2/SSPL dual-license as described in the LICENSE.txt file included in the Redis source distribution.

Please see the CONTRIBUTING.md file in this source distribution for more information. For security bugs and vulnerabilities, please see SECURITY.md.

Redis Trademarks

The purpose of a trademark is to identify the goods and services of a person or company without causing confusion. As the registered owner of its name and logo, Redis accepts certain limited uses of its trademarks but it has requirements that must be followed as described in its Trademark Guidelines available at: https://redis.com/legal/trademark-guidelines/.

Redis internals

If you are reading this README you are likely in front of a Github page or you just untarred the Redis distribution tar ball. In both the cases you are basically one step away from the source code, so here we explain the Redis source code layout, what is in each file as a general idea, the most important functions and structures inside the Redis server and so forth. We keep all the discussion at a high level without digging into the details since this document would be huge otherwise and our code base changes continuously, but a general idea should be a good starting point to understand more. Moreover most of the code is heavily commented and easy to follow.

Source code layout

The Redis root directory just contains this README, the Makefile which calls the real Makefile inside the src directory and an example configuration for Redis and Sentinel. You can find a few shell scripts that are used in order to execute the Redis, Redis Cluster and Redis Sentinel unit tests, which are implemented inside the tests directory.

Inside the root are the following important directories:

  • src: contains the Redis implementation, written in C.
  • tests: contains the unit tests, implemented in Tcl.
  • deps: contains libraries Redis uses. Everything needed to compile Redis is inside this directory; your system just needs to provide libc, a POSIX compatible interface and a C compiler. Notably deps contains a copy of jemalloc, which is the default allocator of Redis under Linux. Note that under deps there are also things which started with the Redis project, but for which the main repository is not redis/redis.

There are a few more directories but they are not very important for our goals here. We'll focus mostly on src, where the Redis implementation is contained, exploring what there is inside each file. The order in which files are exposed is the logical one to follow in order to disclose different layers of complexity incrementally.

Note: lately Redis was refactored quite a bit. Function names and file names have been changed, so you may find that this documentation reflects the unstable branch more closely. For instance, in Redis 3.0 the server.c and server.h files were named redis.c and redis.h. However the overall structure is the same. Keep in mind that all the new developments and pull requests should be performed against the unstable branch.

server.h

The simplest way to understand how a program works is to understand the data structures it uses. So we'll start from the main header file of Redis, which is server.h.

All the server configuration and in general all the shared state is defined in a global structure called server, of type struct redisServer. A few important fields in this structure are:

  • server.db is an array of Redis databases, where data is stored.
  • server.commands is the command table.
  • server.clients is a linked list of clients connected to the server.
  • server.master is a special client, the master, if the instance is a replica.

There are tons of other fields. Most fields are commented directly inside the structure definition.

Another important Redis data structure is the one defining a client. In the past it was called redisClient, now just client. The structure has many fields, here we'll just show the main ones:

struct client {
    int fd;
    sds querybuf;
    int argc;
    robj **argv;
    redisDb *db;
    int flags;
    list *reply;
    // ... many other fields ...
    char buf[PROTO_REPLY_CHUNK_BYTES];
}

The client structure defines a connected client:

  • The fd field is the client socket file descriptor.
  • argc and argv are populated with the command the client is executing, so that functions implementing a given Redis command can read the arguments.
  • querybuf accumulates the requests from the client, which are parsed by the Redis server according to the Redis protocol and executed by calling the implementations of the commands the client is executing.
  • reply and buf are dynamic and static buffers that accumulate the replies the server sends to the client. These buffers are incrementally written to the socket as soon as the file descriptor is writable.

As you can see in the client structure above, arguments in a command are described as robj structures. The following is the full robj structure, which defines a Redis object:

struct redisObject {
    unsigned type:4;
    unsigned encoding:4;
    unsigned lru:LRU_BITS; /* LRU time (relative to global lru_clock) or
                            * LFU data (least significant 8 bits frequency
                            * and most significant 16 bits access time). */
    int refcount;
    void *ptr;
};

Basically this structure can represent all the basic Redis data types like strings, lists, sets, sorted sets and so forth. The interesting thing is that it has a type field, so that it is possible to know what type a given object has, and a refcount, so that the same object can be referenced in multiple places without allocating it multiple times. Finally the ptr field points to the actual representation of the object, which might vary even for the same type, depending on the encoding used.

Redis objects are used extensively in the Redis internals, however in order to avoid the overhead of indirect accesses, recently in many places we just use plain dynamic strings not wrapped inside a Redis object.

server.c

This is the entry point of the Redis server, where the main() function is defined. The following are the most important steps in order to startup the Redis server.

  • initServerConfig() sets up the default values of the server structure.
  • initServer() allocates the data structures needed to operate, setup the listening socket, and so forth.
  • aeMain() starts the event loop which listens for new connections.

There are two special functions called periodically by the event loop:

  1. serverCron() is called periodically (according to server.hz frequency), and performs tasks that must be performed from time to time, like checking for timed out clients.
  2. beforeSleep() is called every time the event loop fired, Redis served a few requests, and is returning back into the event loop.

Inside server.c you can find code that handles other vital things of the Redis server:

  • call() is used in order to call a given command in the context of a given client.
  • activeExpireCycle() handles eviction of keys with a time to live set via the EXPIRE command.
  • performEvictions() is called when a new write command should be performed but Redis is out of memory according to the maxmemory directive.
  • The global variable redisCommandTable defines all the Redis commands, specifying the name of the command, the function implementing the command, the number of arguments required, and other properties of each command.

commands.c

This file is auto generated by utils/generate-command-code.py, the content is based on the JSON files in the src/commands folder. These are meant to be the single source of truth about the Redis commands, and all the metadata about them. These JSON files are not meant to be used by anyone directly, instead that metadata can be obtained via the COMMAND command.

networking.c

This file defines all the I/O functions with clients, masters and replicas (which in Redis are just special clients):

  • createClient() allocates and initializes a new client.
  • The addReply*() family of functions are used by command implementations in order to append data to the client structure, that will be transmitted to the client as a reply for a given command executed.
  • writeToClient() transmits the data pending in the output buffers to the client and is called by the writable event handler sendReplyToClient().
  • readQueryFromClient() is the readable event handler and accumulates data read from the client into the query buffer.
  • processInputBuffer() is the entry point in order to parse the client query buffer according to the Redis protocol. Once commands are ready to be processed, it calls processCommand() which is defined inside server.c in order to actually execute the command.
  • freeClient() deallocates, disconnects and removes a client.

aof.c and rdb.c

As you can guess from the names, these files implement the RDB and AOF persistence for Redis. Redis uses a persistence model based on the fork() system call in order to create a process with the same (shared) memory content of the main Redis process. This secondary process dumps the content of the memory on disk. This is used by rdb.c to create the snapshots on disk and by aof.c in order to perform the AOF rewrite when the append only file gets too big.

The implementation inside aof.c has additional functions in order to implement an API that allows commands to append new commands into the AOF file as clients execute them.

The call() function defined inside server.c is responsible for calling the functions that in turn will write the commands into the AOF.

db.c

Certain Redis commands operate on specific data types; others are general. Examples of generic commands are DEL and EXPIRE. They operate on keys and not on their values specifically. All those generic commands are defined inside db.c.

Moreover db.c implements an API in order to perform certain operations on the Redis dataset without directly accessing the internal data structures.

The most important functions inside db.c which are used in many command implementations are the following:

  • lookupKeyRead() and lookupKeyWrite() are used in order to get a pointer to the value associated to a given key, or NULL if the key does not exist.
  • dbAdd() and its higher level counterpart setKey() create a new key in a Redis database.
  • dbDelete() removes a key and its associated value.
  • emptyData() removes an entire single database or all the databases defined.

The rest of the file implements the generic commands exposed to the client.

object.c

The robj structure defining Redis objects was already described. Inside object.c there are all the functions that operate with Redis objects at a basic level, like functions to allocate new objects, handle the reference counting and so forth. Notable functions inside this file:

  • incrRefCount() and decrRefCount() are used in order to increment or decrement an object reference count. When it drops to 0 the object is finally freed.
  • createObject() allocates a new object. There are also specialized functions to allocate string objects having a specific content, like createStringObjectFromLongLong() and similar functions.

This file also implements the OBJECT command.

replication.c

This is one of the most complex files inside Redis, it is recommended to approach it only after getting a bit familiar with the rest of the code base. In this file there is the implementation of both the master and replica role of Redis.

One of the most important functions inside this file is replicationFeedSlaves() that writes commands to the clients representing replica instances connected to our master, so that the replicas can get the writes performed by the clients: this way their data set will remain synchronized with the one in the master.

This file also implements both the SYNC and PSYNC commands that are used in order to perform the first synchronization between masters and replicas, or to continue the replication after a disconnection.

Script

The script unit is composed of 3 units:

  • script.c - integration of scripts with Redis (commands execution, set replication/resp, ...)
  • script_lua.c - responsible to execute Lua code, uses script.c to interact with Redis from within the Lua code.
  • function_lua.c - contains the Lua engine implementation, uses script_lua.c to execute the Lua code.
  • functions.c - contains Redis Functions implementation (FUNCTION command), uses functions_lua.c if the function it wants to invoke needs the Lua engine.
  • eval.c - contains the eval implementation using script_lua.c to invoke the Lua code.

Other C files

  • t_hash.c, t_list.c, t_set.c, t_string.c, t_zset.c and t_stream.c contains the implementation of the Redis data types. They implement both an API to access a given data type, and the client command implementations for these data types.
  • ae.c implements the Redis event loop, it's a self contained library which is simple to read and understand.
  • sds.c is the Redis string library, check https://github.com/antirez/sds for more information.
  • anet.c is a library to use POSIX networking in a simpler way compared to the raw interface exposed by the kernel.
  • dict.c is an implementation of a non-blocking hash table which rehashes incrementally.
  • cluster.c implements the Redis Cluster. Probably a good read only after being very familiar with the rest of the Redis code base. If you want to read cluster.c make sure to read the Redis Cluster specification.

Anatomy of a Redis command

All the Redis commands are defined in the following way:

void foobarCommand(client *c) {
    printf("%s",c->argv[1]->ptr); /* Do something with the argument. */
    addReply(c,shared.ok); /* Reply something to the client. */
}

The command function is referenced by a JSON file, together with its metadata, see commands.c described above for details. The command flags are documented in the comment above the struct redisCommand in server.h. For other details, please refer to the COMMAND command. https://redis.io/commands/command/

After the command operates in some way, it returns a reply to the client, usually using addReply() or a similar function defined inside networking.c.

There are tons of command implementations inside the Redis source code that can serve as examples of actual commands implementations (e.g. pingCommand). Writing a few toy commands can be a good exercise to get familiar with the code base.

There are also many other files not described here, but it is useless to cover everything. We just want to help you with the first steps. Eventually you'll find your way inside the Redis code base :-)

Enjoy!

jedis's People

Contributors

alextkachman avatar avital-fine avatar avitalfineredis avatar chayim avatar dengliming avatar dependabot[bot] avatar devfozgul avatar dvirdukhan avatar ewhauser avatar gerzse avatar gkorland avatar grdmitro avatar heartsavior avatar ivowiblo avatar kevinsawicki avatar marcosnils avatar mardambey avatar mayankdang avatar mindwind avatar nrodrigues avatar nykolaslima avatar phufool avatar raszi avatar samhendley avatar sazzad16 avatar taer avatar xetorthio avatar yangbodong22011 avatar yaourt avatar zeekling avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

jedis's Issues

Using JedisPool, seeing at random a "PONG" as a response to a get

We are using Jedis with JedisPool providing the Jedis connections. In our code, we are rapidly getting Jedis connections from the JedisPool, putting a value into Redis, and releasing this resource back to the JedisPool. We are processing over 100,000 items that need to be stored. With that writting code we have exposed end points that let us read from Redis via the same JedisPool of connections. Occasionally when reading, we will get a response of "PONG" back.
After the reviewing the code, I only see one spot that deals with a Redis "ping", which would bring about a "PONG" response. This code is a thread detailing with the validation and clean-up of Jedis connections.

support for upcoming Redis 2.2

Hi,

As Redis 2.2 is approaching RC phase, it would be nice to get support for the new commands in Jedis. I'll gladly test them (once the windows port completes :) ).

API doesn't support byte[]

I'm trying to use this to store serialized objects but the lack of raw byte[] support makes that difficult. Are you planning on support raw byte[]?

Pipelined HMSET throws ClassCastException

I'm attempting to a do a series HMSET in a pipeline for batch inserts and getting this exception

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:121)
at redis.clients.jedis.Jedis.hmset(Jedis.java:728)

The commands I'm executing are as follows:

+1289381390.666655 "HMSET" "grails.gorm.tests.Person:1" "lastName" "Builder" "firstName" "Bob"
+1289381390.670692 "SADD" "grails.gorm.tests.Person.all" "1"
+1289381391.967739 "HMSET" "grails.gorm.tests.Person:2" "lastName" "Flintstone" "firstName" "Fred"

This issue is preventing me from supporting batch writes (using a pipeline) in GORM for Redis

ClassCastException: Integer cannot be cast to String

I'm happy to submit copies of whatever code you'd like, but I'm fairly convinced this is a Jedis issue and not an issue with my setup. The two classes listed are here, http://paste.ly/x7L . I don't see any issues with my redis server at the moment. At the same time as I receive this stack trace, I can use redis-cli to get keys successfully.

    java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:121)
at redis.clients.jedis.Jedis.setex(Jedis.java:442)
at com.tedpennings.blog.cache.redis.RedisCacheRepository.cacheString(RedisCacheRepository.java:89)
at com.tedpennings.blog.foursquare.FoursquareLocationProvider.getLocation(FoursquareLocationProvider.java:44)
at com.tedpennings.blog.foursquare.FoursquareLocationProvider.getLocation(FoursquareLocationProvider.java:62)
at com.tedpennings.blog.controllers.BaseController.getFoursquareLocation(BaseController.java:104)
at sun.reflect.GeneratedMethodAccessor33.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:162)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.invokeHandlerMethod(AnnotationMethodHandlerAdapter.java:427)
at org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter.handle(AnnotationMethodHandlerAdapter.java:415)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:788)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:717)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:327)
at org.mortbay.jetty.servlet.Dispatcher.forward(Dispatcher.java:126)
at org.tuckey.web.filters.urlrewrite.NormalRewrittenUrl.doRewrite(NormalRewrittenUrl.java:195)
at org.tuckey.web.filters.urlrewrite.RuleChain.handleRewrite(RuleChain.java:159)
at org.tuckey.web.filters.urlrewrite.RuleChain.doRules(RuleChain.java:141)
at org.tuckey.web.filters.urlrewrite.UrlRewriter.processRequest(UrlRewriter.java:90)
at org.tuckey.web.filters.urlrewrite.UrlRewriteFilter.doFilter(UrlRewriteFilter.java:417)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at org.mortbay.jetty.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:230)
at org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:547)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)

ArrayIndexOutOfBoundsException when buffer gets full and isn't flushed properly

I got this exception from some tests I was running:

java.lang.ArrayIndexOutOfBoundsException: 8192
at redis.clients.util.RedisOutputStream.write(RedisOutputStream.java:37)
at redis.clients.jedis.Protocol.sendCommand(Protocol.java:24)
at redis.clients.jedis.Connection.sendCommand(Connection.java:60)
at redis.clients.jedis.Jedis.get(Jedis.java:54)

Looking at the code, I'm thinking maybe the Jedis client isn't thread-safe, so there should be one instance per thread?

thanks for any feedback,
-Z

binary pub/sub support

Hi,

JedisPubSub still uses Strings instead of byte[] without providing a binary equivalent (like BinaryJedis).

Set default JedisPool configuration to work smoothly with Redis default configuration

Redis comes configured with a default connection timeout of 300s. This can have a negative interaction with the current default configuration of the JedisPool such that in normal usage you can end up in a bad state (see issue 68). I propose that we set some default configuration for the pool that will alleviate this problem and perhaps save some developers some frustration when their pool stops working as expected:

GenericObjectPool.Config poolConfig = new GenericObjectPool.Config();
poolConfig.testWhileIdle = true;
poolConfig.minEvictableIdleTimeMillis = 60000;
poolConfig.timeBetweenEvictionRunsMillis = 30000;
poolConfig.numTestsPerEvictionRun = -1;

My feeling is that this would have a much lower impact than checking every connection on borrow which would also work. Further, we should document additional configurations and best practices for accessing the pool and recovering from error conditions.

no way to stop repair threads

It seems that FixedResourcePool does not provide a method to clean up nicely, including stopping the repair threads. (This is from a quick look at the source, I could be wrong...) A destroy() method or similar should be provided.

I'm investigating why my Grails application does not terminate correctly when I press Ctrl-C, this looks like a prime suspect.

Exclusion operators in ZxxxRANGEBYSCORE

Sorted sets allow you to retrieve ranges with an exclusive rather than inclusive start or end value. These could be optional boolean parameters to the z...rangeByScore() methods.

FixedResourcePool failed

I'm using jedis version on your git repository, and i have some problem with its ConnectionPool, I think RepairThread should catch exception that throw by createResource. I make a test like this:

  1. new JedisPool() <--> RedisServer did not started
  2. getResource() <--> RedisServer still down.
  3. getResource() <--> RedisServer up.
  4. getResource() <--> RedisServer down.
  5. getResource() <--> RedisServer up.

in summary, a loop try to get connection to RedisServer, put an item, server side will up/down redis-server for several times, and I hope JedisPool will wait for connection available. in my test, JedisPool turn frozen after some restart of redis-server.

"Unknown reply" exception.

Hello,

I'm using redis 2.1.7 (for compiled for Win64 bit) with jedis 1.4.0.jar file.
I started to randomly get this exception:

Caused by: redis.clients.jedis.JedisException: Unknown reply:  
    at redis.clients.jedis.Protocol.process(Protocol.java:69)
    at redis.clients.jedis.Protocol.read(Protocol.java:120)
    at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:162)
    at redis.clients.jedis.Connection.getBulkReply(Connection.java:152)
    at redis.clients.jedis.Jedis.get(Jedis.java:68)
    at com.hexbinama.hdyf.service.impl.PostsServiceJedisImpl.getRecentPosts(PostsServiceJedisImpl.java:75)
    ... 44 more

I've only started to get this exception few minutes ago, and I did change nothing in the jedis service part. The only think that I can think of right now is that the number of keys in the database is over 200 for some minutes.

If I restart the server, it starts to work again, but is now second time that I get it without a reason.

PS: there are two spaces after "Unknown reply: " reported in the console of my IDE.

rich exceptions

It would be useful to have rich exceptions, to distinguish between recoverable and unrecoverable errors. I don't have a lot of insight into the 'driver' to pinpoint some examples but from a top-level view, it would be useful to differentiate between:

a. network exception (unrecoverable) - the connection to the db went down
b. protocol exception (recoverable?) - the data request/reply was corrupted/invalid for some reason:

  • incorrect number of arguments - unsupported operation (issuing an illegal command during multi).

The API shields one from doing too many mistakes at the protocol level but I assume there are some corner cases that cannot be prevented. Anyway, having a difference between A and B would be a good start.

Bad processBulkReply implementation ...

The current implementation doesn't take care about TCP fragmentation ...

Little patch :

diff --git a/src/main/java/redis/clients/jedis/Protocol.java b/src/main/java/redis/clients/jedis/Protocol.java
index ff96e8b..3c1ff30 100644
--- a/src/main/java/redis/clients/jedis/Protocol.java
+++ b/src/main/java/redis/clients/jedis/Protocol.java
@@ -139,8 +139,11 @@ public class Protocol {
return null;
}
byte[] read = new byte[len];

  •   int offset = 0;
    try {
    
  •       is.read(read);
    
  •           while(offset < len) {
    
  •               offset += is.read(read, offset, (len - offset));
    
  •           }
        // read 2 more bytes for the command delimiter
        is.read();
        is.read();
    

Improve pipelining by queueing commands

Currently in jedis pipelining is done by reading all the replies at once. Performance could be further improved by issuing multiple commands in one go as mentioned in the protocol spec:
Pipelining is supported so multiple commands can be sent with a single write operation by the client

Can you consider adding some sort of pipelining mode to the Jedis client? In it, all commands could be added to the queue and issued at once, in bulk.
The current approach of using an abstract class requires clients to aggregate commands themselves which is difficult.

MD5 hashing and sharding fails

I have a ShardedJedisPool with 3 shards configured to use MD5 hashing.
I'm getting this error:
java.lang.ArrayIndexOutOfBoundsException
at sun.security.provider.DigestBase.engineUpdate(DigestBase.java:110)
at sun.security.provider.MD5.implDigest(MD5.java:88)
at sun.security.provider.DigestBase.engineDigest(DigestBase.java:169)
at sun.security.provider.DigestBase.engineDigest(DigestBase.java:148)
at java.security.MessageDigest$Delegate.engineDigest(MessageDigest.java:546)
at java.security.MessageDigest.digest(MessageDigest.java:323)
at redis.clients.util.Hashing$1.hash(Hashing.java:28)
at redis.clients.util.Sharded.getShardInfo(Sharded.java:86)
at redis.clients.util.Sharded.getShardInfo(Sharded.java:96)
at redis.clients.util.Sharded.getShard(Sharded.java:82)
at redis.clients.jedis.ShardedJedisPipeline.hgetAll(ShardedJedisPipeline.java:192)

Also all keys end up in the second shard, probably related to the above error.

It seems like server has closed the connection

Hi,

We are using redis in a web application. The Jedis poll is retreived through JNDI. Using Jetty, we have configured it like this:

<New class="org.eclipse.jetty.plus.jndi.Resource">
    <Arg>redis/jaxspot</Arg>
    <Arg>
        <New class="redis.clients.jedis.JedisPool">
            <Arg>
                <New class="org.apache.commons.pool.impl.GenericObjectPool$Config">
                    <Set type="int" name="minIdle">4</Set>
                    <Set type="int" name="maxActive">10</Set>
                </New>
            </Arg>
            <Arg>127.0.0.1</Arg>
            <Arg type="int">6379</Arg>
        </New>
    </Arg>
</New>

So this is basically a pool of 4 to 10 connections. In our application the first requests goes well. But when we wait about 2-3 minutes and we refresh the page, redis crashed. The following code:

Jedis jedis = pool.getResource();
[...]
String v = jedis.get(s);

throws the exception:

Caused by: redis.clients.jedis.JedisException: It seems like server has closed the connection.
at redis.clients.util.RedisInputStream.readLine(RedisInputStream.java:90)
at redis.clients.jedis.Protocol.processBulkReply(Protocol.java:83)
at redis.clients.jedis.Protocol.process(Protocol.java:66)
at redis.clients.jedis.Protocol.read(Protocol.java:121)
at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:163)
at redis.clients.jedis.Connection.getBulkReply(Connection.java:153)
at redis.clients.jedis.Jedis.get(Jedis.java:68)
at net.playtouch.jaxspot.module.caching.redis.RedisTransactionalCache$1.execute(RedisTransactionalCache.java:28)

isResourceValid failed

Hi!
I'm testing on Redis 2.0 RC4 and it return "PONG" on ping() instead of "OK", so the "isResourceValid" will failed on checking connection properly.

Use slf4j instead of Java util logger

I think it would be better for Jedis to use slf4j instead of the java util logger. This would enable apps to determine which logger to use in production.

upload artifacts (including javadoc and sources) into Maven central

Hi,
It would be useful to have the versions uploaded into Maven central for easier consumption. The last version 1.4.0 is not there as far as I can see which requires us to create our own copy which is cumbersome.

It would be nice to avoid having to do this in the future.
Thanks

Maven repo

It would be amazing if you created a maven repository for this project.

Thanks for everything you do!

JedisException: Unknown reply: O

Dealing with a data set of about 100,000 key/values I am getting the attached exception when trying to

jedis.set("MyObj:MyId:label", label);

This happens roughly around the 50,000th entry. The data prior to this is stored correctly and the data after this is lost.
This is running in a grails/groovy app.

[ 30.09.2010 11:27:55] [http-8080-5] ERROR errors.GrailsExceptionResolver : 72 - Unknown reply: O
redis.clients.jedis.JedisException: Unknown reply: O
at redis.clients.jedis.Protocol.process(Protocol.java:65)
at redis.clients.jedis.Protocol.read(Protocol.java:116)
at redis.clients.jedis.Connection.getStatusCodeReply(Connection.java:121)
at redis.clients.jedis.Jedis.set(Jedis.java:49)
at TestController$_closure22.doCall(TestController:494)
at TestController$_closure22.doCall(TestController)
at com.frequency.utils.PageCacheFilter.doFilter(PageCacheFilter.java:138)
at java.lang.Thread.run(Thread.java:637)
[ 30.09.2010 11:27:55] [http-8080-5] ERROR [/].[grails] : 678 - Servlet.service() for servlet grails threw exception
java.lang.NullPointerException
at org.codehaus.groovy.grails.web.errors.GrailsExceptionResolver.resolveException(GrailsExceptionResolver.java:75)
at com.frequency.utils.GenericExceptionHandler.resolveException(GenericExceptionHandler.java:20)
at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1007)
at org.codehaus.groovy.grails.web.servlet.GrailsDispatcherServlet.doDispatch(GrailsDispatcherServlet.java:319)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:292)
at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:260)
at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:251)
at org.codehaus.groovy.grails.web.mapping.filter.UrlMappingsFilter.doFilterInternal(UrlMappingsFilter.java:183)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.obtainContent(GrailsPageFilter.java:245)
at org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.doFilter(GrailsPageFilter.java:134)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
at org.springframework.security.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
at org.springframework.security.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
at org.springframework.security.ui.ExceptionTranslationFilter.doFilterHttp(ExceptionTranslationFilter.java:101)
at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
at org.springframework.security.providers.anonymous.AnonymousProcessingFilter.doFilterHttp(AnonymousProcessingFilter.java:105)
at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
at org.springframework.security.ui.rememberme.RememberMeProcessingFilter.doFilterHttp(RememberMeProcessingFilter.java:116)
at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
at org.springframework.security.wrapper.SecurityContextHolderAwareRequestFilter.doFilterHttp(SecurityContextHolderAwareRequestFilter.java:91)
at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
at org.springframework.security.ui.AbstractProcessingFilter.doFilterHttp(AbstractProcessingFilter.java:277)
at org.codehaus.groovy.grails.plugins.springsecurity.GrailsAuthenticationProcessingFilter.super$3$doFilterHttp(GrailsAuthenticationProcessingFilter.groovy)
at sun.reflect.GeneratedMethodAccessor666.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1058)
at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:1070)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnSuperN(ScriptBytecodeAdapter.java:127)
at org.codehaus.groovy.grails.plugins.springsecurity.GrailsAuthenticationProcessingFilter.doFilterHttp(GrailsAuthenticationProcessingFilter.groovy:56)
at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
at org.springframework.security.ui.logout.LogoutFilter.doFilterHttp(LogoutFilter.java:89)
at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
at org.springframework.security.context.HttpSessionContextIntegrationFilter.doFilterHttp(HttpSessionContextIntegrationFilter.java:235)
at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
at org.springframework.security.util.FilterChainProxy.doFilter(FilterChainProxy.java:175)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:67)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.codehaus.groovy.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:69)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.codehaus.groovy.grails.plugins.resourcesfirst.ResourcesFirstFilter.doFilter(ResourcesFirstFilter.java:45)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.frequency.utils.PageCacheFilter.doFilter(PageCacheFilter.java:138)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:637)
[ 30.09.2010 11:27:55] [http-8080-5] ERROR [/].[default] : 274 - Servlet.service() for servlet default threw exception
java.lang.NullPointerException
at org.codehaus.groovy.grails.web.errors.GrailsExceptionResolver.resolveException(GrailsExceptionResolver.java:75)
at com.frequency.utils.GenericExceptionHandler.resolveException(GenericExceptionHandler.java:20)
at org.springframework.web.servlet.DispatcherServlet.processHandlerException(DispatcherServlet.java:1007)
at org.codehaus.groovy.grails.web.servlet.GrailsDispatcherServlet.doDispatch(GrailsDispatcherServlet.java:319)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:820)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:646)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:436)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:374)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:302)
at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:292)
at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:260)
at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:251)
at org.codehaus.groovy.grails.web.mapping.filter.UrlMappingsFilter.doFilterInternal(UrlMappingsFilter.java:183)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.obtainContent(GrailsPageFilter.java:245)
at org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.doFilter(GrailsPageFilter.java:134)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:378)
at org.springframework.security.intercept.web.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:109)
at org.springframework.security.intercept.web.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:83)
at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
at org.springframework.security.ui.ExceptionTranslationFilter.doFilterHttp(ExceptionTranslationFilter.java:101)
at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
at org.springframework.security.providers.anonymous.AnonymousProcessingFilter.doFilterHttp(AnonymousProcessingFilter.java:105)
at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
at org.springframework.security.ui.rememberme.RememberMeProcessingFilter.doFilterHttp(RememberMeProcessingFilter.java:116)
at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
at org.springframework.security.wrapper.SecurityContextHolderAwareRequestFilter.doFilterHttp(SecurityContextHolderAwareRequestFilter.java:91)
at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
at org.springframework.security.ui.AbstractProcessingFilter.doFilterHttp(AbstractProcessingFilter.java:277)
at org.codehaus.groovy.grails.plugins.springsecurity.GrailsAuthenticationProcessingFilter.super$3$doFilterHttp(GrailsAuthenticationProcessingFilter.groovy)
at sun.reflect.GeneratedMethodAccessor666.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.codehaus.groovy.reflection.CachedMethod.invoke(CachedMethod.java:88)
at groovy.lang.MetaMethod.doMethodInvoke(MetaMethod.java:233)
at groovy.lang.MetaClassImpl.invokeMethod(MetaClassImpl.java:1058)
at groovy.lang.ExpandoMetaClass.invokeMethod(ExpandoMetaClass.java:1070)
at org.codehaus.groovy.runtime.ScriptBytecodeAdapter.invokeMethodOnSuperN(ScriptBytecodeAdapter.java:127)
at org.codehaus.groovy.grails.plugins.springsecurity.GrailsAuthenticationProcessingFilter.doFilterHttp(GrailsAuthenticationProcessingFilter.groovy:56)
at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
at org.springframework.security.ui.logout.LogoutFilter.doFilterHttp(LogoutFilter.java:89)
at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
at org.springframework.security.context.HttpSessionContextIntegrationFilter.doFilterHttp(HttpSessionContextIntegrationFilter.java:235)
at org.springframework.security.ui.SpringSecurityFilter.doFilter(SpringSecurityFilter.java:53)
at org.springframework.security.util.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:390)
at org.springframework.security.util.FilterChainProxy.doFilter(FilterChainProxy.java:175)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:67)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.codehaus.groovy.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:69)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.codehaus.groovy.grails.plugins.resourcesfirst.ResourcesFirstFilter.doFilter(ResourcesFirstFilter.java:45)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at com.frequency.utils.PageCacheFilter.doFilter(PageCacheFilter.java:138)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Thread.java:637)

NullPointerException in zscore

Hi, i get NullPointerException in zscore method, when key does not exist in set.
java.lang.NullPointerException
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:991)
at java.lang.Double.valueOf(Double.java:475)
at redis.clients.jedis.Jedis.zscore(Jedis.java:516)

Seems that there is no check for null response in code, or am I missing something?

Thanks

Add OSGi Metadata

It would be nice if this library would ship with a valid MANIFEST.MF for OSGi Bundle compatibility.
This is quite simple to achieve and would make jedis OSGi compatible (broaden the audience, make it a first class citizen in Java App Severs that leverage OSGi as Runtime already.. and so on).

(fork commit coming)

poor performance when using utf8 characters

Hi,
I'm new to redis (only tested it yesterday) and then to jedis.
I used hmset to write 300000 entries but performance tests were really disappointing : more than 2 minutes to write only 10000 entries.

Today, I've checked the jedis code and found an issue in the RedisOuputStream class, when using utf8 characters. This is due to wrong conditions for flushing the buffer, where jedis flushed it for each chars.

Here is a small test to reproduce it :

import java.util.*;
import redis.clients.jedis.*;

public class JedisPerf {
    public static void main(String[] args) {
        String v = "éééééééééééééééééééééééééééééééééééééééééééé";
        Map<String, String> map = new HashMap<String, String>();
        for (int i = 0; i < 30; i++) {
            map.put("k" + i, v);
        }

        Jedis jedis = new Jedis("localhost");
        jedis.flushDB();
    
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            jedis.hmset("id:" + i, map);
        }
        long end = System.currentTimeMillis();
        System.out.println((end - start) + " ms");
    }
}

With the current git snapshot, this is done in 401299 ms.
With the patch bellow, it took only 6452 ms :

diff --git a/src/main/java/redis/clients/util/RedisOutputStream.java b/src/main/java/redis/clients/util/RedisOutputStream.java
index d9233e9..c366e02 100644
--- a/src/main/java/redis/clients/util/RedisOutputStream.java
+++ b/src/main/java/redis/clients/util/RedisOutputStream.java
@@ -119,13 +119,13 @@ public final class RedisOutputStream extends FilterOutputStream {
                     flushBuffer();
                 }
             } else if (c < 0x800) {
-                if(2 < buf.length - count) {
+                if(2 >= buf.length - count) {
                     flushBuffer();
                 }
                 buf[count++] = (byte)(0xc0 | (c >> 6));
                 buf[count++] = (byte)(0x80 | (c & 0x3f));
             } else if (isSurrogate(c)) {
-                if(4 < buf.length - count) {
+                if(4 >= buf.length - count) {
                     flushBuffer();
                 }
                 int uc = Character.toCodePoint(c, str.charAt(i++));
@@ -134,7 +134,7 @@ public final class RedisOutputStream extends FilterOutputStream {
                 buf[count++] = ((byte)(0x80 | ((uc >> 6) & 0x3f)));
                 buf[count++] = ((byte)(0x80 | (uc & 0x3f)));
             } else {
-                if(3 < buf.length - count) {
+                if(3 >= buf.length - count) {
                     flushBuffer();
                 }
                 buf[count++] =((byte)(0xe0 | ((c >> 12))));

JedisPool with one arg constructor fails

When i try to setup a JedisPool with:
JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT);
the pool will fail to create connection. At least i have to set the port:
JedisPool pool = new JedisPool("localhost", Protocol.DEFAULT_PORT);
So my guess is that the function JedisPool(String host) is not setting the default port.

automaticall return Jedis instances to the pool when closed

Would it be possible to automatically return Jedis instances to the pool when they are 'closed' (disconnected/quit)? Currently one needs to keep track of the connection usage if JedisPool is used which makes it hard to properly do cleanup.
It's a lot easier and consistent to simply ask the client to work with a Jedis instance (no matter if JedisPool is used or not).

Pipelined smembers return value.

I'm calling smembers from a ShardedJedisPipeline and instead of getting a Set it looks like I'm getting an ArrayList.
This is different from calling smembers without pipelining, which returns a Set.

I'm using jedis-1.3.2-jdk1.5.jar

ArrayIndexOutOfBoundsException in RedisOutputStream using JedisPool in 1.5

We're using pooling like so:

// Get pool from app context
JedisPool pool = (JedisPool) context.getAttribute("jedis-pool");
if (pool == null) {
    log.error("Cannot get plays today, unable to get jedis pool!");
    return 0; // if pool is down, just get out
}

Jedis jedis = null;
try {
    jedis = pool.getResource();
    return Long.parseLong( jedis.hget(hash, hashKey) );
} catch (Exception e) {
    log.error("Error getting plays today", e);
} finally {
    if (jedis != null) pool.returnResource(jedis);
}

We never saw this when developing / staging, and when we first deployed the app with this code, it ran fine for maybe 10 minutes, then the following stack trace started appearing in our logs from the hget line in the code above:

java.lang.ArrayIndexOutOfBoundsException: 8208
at redis.clients.util.RedisOutputStream.write(RedisOutputStream.java:35)
at redis.clients.jedis.Protocol.sendCommand(Protocol.java:32)
at redis.clients.jedis.Protocol.sendCommand(Protocol.java:26)
at redis.clients.jedis.Connection.sendCommand(Connection.java:70)
at redis.clients.jedis.BinaryClient.hget(BinaryClient.java:172)
at redis.clients.jedis.Client.hget(Client.java:136)
at redis.clients.jedis.Jedis.hget(Jedis.java:693)

Afterwards, it seems every successive call of that line caused the same exception with varying values for the actual array index (all of which were above the 8192 length buffer in RedisOutputStream).

tests documentation

Hi,

I apologise if I'm missing something obvious, but my tests are failing.

Do I need to start my own redis instance? Presumably I have to start it with the configs found in "conf", but it fails with:

redis-server conf/redis.conf

*** FATAL CONFIG FILE ERROR ***
Reading the configuration file, at line 216
>>> 'no-appendfsync-on-rewrite no'
Bad directive or wrong number of arguments

I'm using redis 2.0.2 (via macports)

Thanks,
Juan

Nice project

Hi!
I'm using Jredis for a long time, but now I need an other one that support connection pool.
I was surprised that this project seem nice, but it's hard to find it on search engine like google.
I think it's good to create your homepage on google code or something like it, some more documents...
thank you for your work.

Jedis incr returns Integer instead of Long

I noticed that Jedis's (v1.4.0) 'incr' implementation returns a java.lang.Integer, instead of a java.lang.Long. Since redis's incr is a 64-bit signed 'integer', I think this should be switched to a Long to not loose bits.

Thanks!

Murmur hashing fails with more than 2 shards

I have a ShardedJedisPool configured to use Murmur Hashing.
If I use it with two shards it splits the keys evenly among them.
If I add a third shard the first shard gets around 25% of they keys, the second one around 75% and the third one none.

MurmurHash instead of MD5 as sharding hashing algorithm ...

May I propose to use MurmurHash instead of MD5 for Ketama hash algorithm ?

MD5 avalanche behavior is OK, but under heavy load, it could become slow and / or too consuming ...
MurmurHash is not a crypto hash function, but is really fast and had a very good avalanche behavior.

There is already an open source (public domain) Java implementation here : http://dmy999.com/article/50/murmurhash-2-java-port

General info available here : http://en.wikipedia.org/wiki/MurmurHash

Regards

Jedis preventing application to close

Hi,

We are using Jedis for a very large website. When integrating it, we saw an issue with the way Repair threads are used.

if no Redis daemon is available, the following commands block the shutdown of the application:

jedisPool.init()
jedisPool.destroy();

The destroy thread block at:

repairThreads[i].interrupt();
repairThreads[i].join();

in the destroy method. The cause is that the repair threads never stops and thus the join does not finish.

I saw that the repair threads are not stoppable because of the following code in JedisPool.createResource() method:

while (!done) {
    try {
    jedis.connect();
    if (password != null) {
        jedis.auth(password);
    }
    done = true;
    } catch (Exception e) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException e1) {
        }
    }
}

When no Redis daemon is available, the while loop keeps looping and the Interrupted exception is simply ignored.

As you may know, as a library developper, it is really not recommanded to catch Interrupted exception. If it is necessary, you should restore the interrupt status that the exception throwing cleared.

The following code fixes the issue:

try {
    Thread.sleep(100);
} catch (InterruptedException ignored) {
    // The repair thread can be interrupted on shutdown
    Thread.currentThread().interrupt();
    return null;
}

Note that the return null; may be acceptable there since we are in the case where the shutdown method has been called to interrupt the repair threads. So we are exiting the program.

This enables Repair threads to stop and then unblock the program shutdown.

For the moment, a quick hugly fix for those who needs the patch is to override the createResource() method and apply the above patch in it.

Thanks,

Mathieu Carbou
[email protected]

Getting Exception while trying to do a jedis.get() command - ClassCastException

I have pasted the Exception that I'm getting below using version 1.5.0. I'm attempting to use the pool to pull jedis objects out of to use:

jedisPool = new JedisPool(new GenericObjectPool.Config(), "cache01");

and here is the thread code that I'm using to check the cache:
http://pastebin.com/icTDwFNH

it happens intermittently but when it does it floods the logs with these error messages. Any ideas?

SEVERE: java.lang.ClassCastException: [B cannot be cast to java.lang.Long ::--> om.mysite.something.getCache updateCache
java.lang.ClassCastException: java.lang.Long cannot be cast to [B
at redis.clients.jedis.Connection.getBinaryBulkReply(Connection.java:163)
at redis.clients.jedis.Connection.getBulkReply(Connection.java:153)
at redis.clients.jedis.Jedis.get(Jedis.java:68)
at com.mysite.something.getCache(Cache.java:19)

incrBy and hincrBy returns long but takes int

Hi, just updated a app from 1.3.2 to 1.5-RC2 and found that incr/decr and incrBy/decrBy methods returns long instead of int, as should be in the first time. But as parameter the [h][in|de]crBy methods takes still int, should be Long also. Just in case someone needs to make an in/decrBy with a value of x with x: Integer.MAX_VALUE < x < Long.MAX_VALUE or MIN_VALUE.

greets
mheuser

Test failures on master

Is that expected (due to some refactoring) ??
Running "mvn clean test" with a off-the-shelve Redis 2.0.2 installation:

Results :

Failed tests:
bgsave(redis.clients.jedis.tests.commands.ControlCommandsTest)

Tests in error:
persist(redis.clients.jedis.tests.commands.AllKindOfValuesCommandsTest)
lpushx(redis.clients.jedis.tests.commands.ListCommandsTest)
rpushx(redis.clients.jedis.tests.commands.ListCommandsTest)
linsert(redis.clients.jedis.tests.commands.ListCommandsTest)
trySharding(redis.clients.jedis.tests.ShardedJedisTest)
tryShardingWithMurmure(redis.clients.jedis.tests.ShardedJedisTest)
watch(redis.clients.jedis.tests.commands.TransactionCommandsTest)
unwatch(redis.clients.jedis.tests.commands.TransactionCommandsTest)
strlen(redis.clients.jedis.tests.commands.StringValuesCommandsTest)

Tests run: 151, Failures: 1, Errors: 9, Skipped: 0

Overriding of interface method is a Java 1.6 feature

Class JedisByteHashMap is annotating the methods implemented from Map<byte[], byte[]> interface as override.
Annotating interface methods as override is only possible since Java 1.6 and the project requires Java 1.5 compatibility at the moment.

Java 1.5

Any idea what would need to be done to support this under Java 1.5? We use Java 1.5 on all our production servers and would like to use your library, but aren't so exciting about moving to Java 1.6 in order to do so. We might, however, be able to help make the requisite changes to support Java 1.5.

BinaryJedisCommands does not define #select, #mget, #mset, or #msetnx

Being a newcomer to Redis, as far as I know, the commands select, mget, mset, and msetnx are valid in the binary protocol. However, they are absent from the interface BinaryJedisCommand. A corollary is that neither the shared binary interface nor the binary pipeline supper mget, etc. But, maybe a separate issue, there actually is no binary pipeline ... is this planned for a future version?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.