Skip to content

DATAREDIS-268 - Add support for 'CLIENT LIST'. #53

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
<row><entry><code>BRPOPLPUSH</code></entry><entry>X</entry></row>
<row><entry><code>CLIENT KILL</code></entry><entry>X</entry></row>
<row><entry><code>CLIENT GETNAME</code></entry><entry>X</entry></row>
<row><entry><code>CLIENT LIST</code></entry><entry>-</entry></row>
<row><entry><code>CLIENT LIST</code></entry><entry>X</entry></row>
<row><entry><code>CLIENT SETNAME</code></entry><entry>X</entry></row>
<row><entry><code>CONFIG GET</code></entry><entry>X</entry></row>
<row><entry><code>CONFIG RESETSTAT</code></entry><entry>X</entry></row>
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jredisVersion=06052013
jedisVersion=2.4.1
springVersion=3.2.8.RELEASE
log4jVersion=1.2.17
version=1.3.0.BUILD-SNAPSHOT
version=1.3.0.DATAREDIS-268-SNAPSHOT
srpVersion=0.7
jacksonVersion=1.8.8
fasterXmlJacksonVersion=2.2.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.springframework.data.redis.connection.convert.ListConverter;
import org.springframework.data.redis.connection.convert.MapConverter;
import org.springframework.data.redis.connection.convert.SetConverter;
import org.springframework.data.redis.core.types.RedisClientInfo;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import org.springframework.util.Assert;
Expand Down Expand Up @@ -2191,6 +2192,11 @@ public Long time() {
return this.delegate.time();
}

@Override
public List<RedisClientInfo> getClientList() {
return this.delegate.getClientList();
}

/**
* Specifies if pipelined and tx results should be deserialized to Strings. If false, results of
* {@link #closePipeline()} and {@link #exec()} will be of the type returned by the underlying connection
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.util.List;
import java.util.Properties;

import org.springframework.data.redis.core.types.RedisClientInfo;

/**
* Server-specific commands supported by Redis.
*
Expand Down Expand Up @@ -186,4 +188,13 @@ public enum ShutdownOption {
* @since 1.3
*/
String getClientName();

/**
* Request information and statistics about connected clients.
*
* @return {@link List} of {@link RedisClientInfo} objects.
* @since 1.3
* @see http://redis.io/commands/client-list
*/
List<RedisClientInfo> getClientList();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.types.RedisClientInfo;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
Expand Down Expand Up @@ -301,4 +302,10 @@ public interface StringTuple extends Tuple {
* @sice 1.3
*/
void setClientName(String name);

/**
* @see RedisConnection#getClientList()
* @since 1.3
*/
List<RedisClientInfo> getClientList();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.redis.connection.convert;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.springframework.core.convert.converter.Converter;
import org.springframework.data.redis.core.types.RedisClientInfo;
import org.springframework.data.redis.core.types.RedisClientInfo.RedisClientInfoBuilder;

/**
* {@link Converter} implementation to create one {@link RedisClientInfo} per line entry in given {@link String} array.
*
* <pre>
* ## sample of single line
* addr=127.0.0.1:60311 fd=6 name= age=4059 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=0 qbuf-free=32768 obl=0 oll=0 omem=0 events=r cmd=client
* </pre>
*
* @author Christoph Strobl
* @since 1.3
*/
public class StringToRedisClientInfoConverter implements Converter<String[], List<RedisClientInfo>> {

@Override
public List<RedisClientInfo> convert(String[] lines) {

if (lines == null) {
return Collections.emptyList();
}
List<RedisClientInfo> infos = new ArrayList<RedisClientInfo>(lines.length);
for (String line : lines) {
infos.add(RedisClientInfoBuilder.fromString(line));
}
return infos;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.springframework.data.redis.connection.Subscription;
import org.springframework.data.redis.connection.convert.Converters;
import org.springframework.data.redis.connection.convert.TransactionResultConverter;
import org.springframework.data.redis.core.types.RedisClientInfo;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
Expand Down Expand Up @@ -2854,6 +2855,18 @@ public String getClientName() {
return jedis.clientGetname();
}

/*
* @see org.springframework.data.redis.connection.RedisServerCommands#getClientName()
*/
@Override
public List<RedisClientInfo> getClientList() {

if (isQueueing() || isPipelined()) {
throw new UnsupportedOperationException("'CLIENT LIST' is not supported in in pipeline / multi mode.");
}
return JedisConverters.toListOfRedisClientInformation(this.jedis.clientList());
}

/**
* Specifies if pipelined results should be converted to the expected data type. If false, results of
* {@link #closePipeline()} and {@link #exec()} will be of the type returned by the Jedis driver
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013 the original author or authors.
* Copyright 2013-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,8 @@
*/
package org.springframework.data.redis.connection.jedis;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand All @@ -31,7 +33,10 @@
import org.springframework.data.redis.connection.convert.ListConverter;
import org.springframework.data.redis.connection.convert.MapConverter;
import org.springframework.data.redis.connection.convert.SetConverter;
import org.springframework.data.redis.connection.convert.StringToRedisClientInfoConverter;
import org.springframework.data.redis.core.types.RedisClientInfo;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import redis.clients.jedis.BinaryClient.LIST_POSITION;
import redis.clients.jedis.BitOP;
Expand All @@ -42,6 +47,7 @@
* Jedis type converters
*
* @author Jennifer Hickey
* @author Christoph Strobl
*/
abstract public class JedisConverters extends Converters {

Expand All @@ -51,6 +57,7 @@ abstract public class JedisConverters extends Converters {
private static final MapConverter<String, byte[]> STRING_MAP_TO_BYTE_MAP;
private static final SetConverter<redis.clients.jedis.Tuple, Tuple> TUPLE_SET_TO_TUPLE_SET;
private static final Converter<Exception, DataAccessException> EXCEPTION_CONVERTER = new JedisExceptionConverter();
private static final Converter<String[], List<RedisClientInfo>> STRING_TO_CLIENT_INFO_CONVERTER = new StringToRedisClientInfoConverter();

static {
STRING_TO_BYTES = new Converter<String, byte[]>() {
Expand Down Expand Up @@ -114,6 +121,19 @@ public static String toString(byte[] source) {
return source == null ? null : SafeEncoder.encode(source);
}

/**
* @param source
* @return
* @since 1.3
*/
public static List<RedisClientInfo> toListOfRedisClientInformation(String source) {

if (!StringUtils.hasText(source)) {
return Collections.emptyList();
}
return STRING_TO_CLIENT_INFO_CONVERTER.convert(source.split("\\r?\\n"));
}

public static DataAccessException toDataAccessException(Exception ex) {
return EXCEPTION_CONVERTER.convert(ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.springframework.data.redis.connection.ReturnType;
import org.springframework.data.redis.connection.SortParameters;
import org.springframework.data.redis.connection.Subscription;
import org.springframework.data.redis.core.types.RedisClientInfo;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import org.springframework.util.ReflectionUtils;
Expand Down Expand Up @@ -1204,4 +1205,8 @@ public void setClientName(byte[] name) {
public String getClientName() {
throw new UnsupportedOperationException("The 'CLIENT GETNAME' command is not supported by the JRedis driver.");
}

public List<RedisClientInfo> getClientList() {
throw new UnsupportedOperationException();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import org.springframework.data.redis.connection.Subscription;
import org.springframework.data.redis.connection.convert.Converters;
import org.springframework.data.redis.connection.convert.TransactionResultConverter;
import org.springframework.data.redis.core.types.RedisClientInfo;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
Expand Down Expand Up @@ -2959,6 +2960,21 @@ public String getClientName() {
}
}

@Override
public List<RedisClientInfo> getClientList() {

if (isPipelined()) {
throw new UnsupportedOperationException("Cannot be called in pipeline mode.");
}
if (isQueueing()) {
transaction(new LettuceTxResult(getAsyncConnection().clientList(),
LettuceConverters.stringToRedisClientListConverter()));
return null;
}

return LettuceConverters.toListOfRedisClientInformation(getConnection().clientList());
}

/**
* Specifies if pipelined and transaction results should be converted to the expected data type. If false, results of
* {@link #closePipeline()} and {@link #exec()} will be of the type returned by the Lettuce driver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedHashSet;
import java.util.List;
Expand All @@ -31,7 +32,10 @@
import org.springframework.data.redis.connection.SortParameters;
import org.springframework.data.redis.connection.SortParameters.Order;
import org.springframework.data.redis.connection.convert.Converters;
import org.springframework.data.redis.connection.convert.StringToRedisClientInfoConverter;
import org.springframework.data.redis.core.types.RedisClientInfo;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import com.lambdaworks.redis.KeyValue;
import com.lambdaworks.redis.ScoredValue;
Expand All @@ -55,6 +59,8 @@ abstract public class LettuceConverters extends Converters {
private static final Converter<ScoredValue<byte[]>, Tuple> SCORED_VALUE_TO_TUPLE;
private static final Converter<Exception, DataAccessException> EXCEPTION_CONVERTER = new LettuceExceptionConverter();

private static final Converter<String[], List<RedisClientInfo>> STRING_TO_LIST_OF_CLIENT_INFO = new StringToRedisClientInfoConverter();

static {
DATE_TO_LONG = new Converter<Date, Long>() {
public Long convert(Date source) {
Expand Down Expand Up @@ -115,6 +121,20 @@ public Tuple convert(ScoredValue<byte[]> source) {
};
}

public static Converter<String, List<RedisClientInfo>> stringToRedisClientListConverter() {
return new Converter<String, List<RedisClientInfo>>() {

@Override
public List<RedisClientInfo> convert(String source) {
if (!StringUtils.hasText(source)) {
return Collections.emptyList();
}

return STRING_TO_LIST_OF_CLIENT_INFO.convert(source.split("\\r?\\n"));
}
};
}

public static Converter<Date, Long> dateToLong() {
return DATE_TO_LONG;
}
Expand Down Expand Up @@ -235,4 +255,8 @@ public static SortArgs toSortArgs(SortParameters params) {
}
return args;
}

public static List<RedisClientInfo> toListOfRedisClientInformation(String clientList) {
return stringToRedisClientListConverter().convert(clientList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import org.springframework.data.redis.connection.ReturnType;
import org.springframework.data.redis.connection.SortParameters;
import org.springframework.data.redis.connection.Subscription;
import org.springframework.data.redis.core.types.RedisClientInfo;
import org.springframework.util.Assert;

import redis.Command;
Expand Down Expand Up @@ -2280,6 +2281,19 @@ public String getClientName() {
}
}

@Override
public List<RedisClientInfo> getClientList() {
if (isQueueing()) {
throw new UnsupportedOperationException();
}
if (isPipelined()) {
pipeline(new SrpGenericResult(pipeline.client_list(), SrpConverters.replyToListOfRedisClientInfo()));
return null;
}

return SrpConverters.toListOfRedisClientInformation(this.client.client_list());
}

private List<Object> closeTransaction() {
List<Object> results = Collections.emptyList();
if (txTracker != null) {
Expand Down
Loading