Skip to content

Commit

Permalink
SLOWLOG GET for Redis 4.0+ (#2084)
Browse files Browse the repository at this point in the history
  • Loading branch information
sazzad16 committed Dec 6, 2020
1 parent a07f707 commit b54ce7d
Show file tree
Hide file tree
Showing 6 changed files with 113 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/main/java/redis/clients/jedis/BinaryJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -3516,15 +3516,15 @@ public Long slowlogLen() {
}

@Override
public List<byte[]> slowlogGetBinary() {
public List<Object> slowlogGetBinary() {
client.slowlogGet();
return client.getBinaryMultiBulkReply();
return client.getObjectMultiBulkReply();
}

@Override
public List<byte[]> slowlogGetBinary(final long entries) {
public List<Object> slowlogGetBinary(final long entries) {
client.slowlogGet(entries);
return client.getBinaryMultiBulkReply();
return client.getObjectMultiBulkReply();
}

@Override
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/redis/clients/jedis/HostAndPort.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ public String toString() {
return host + ":" + port;
}

/**
* Creates HostAndPort with <i>unconverted</i> host.
*
* @param string String to parse. Must be in <b>"host:port"</b> format. Port is mandatory.
* @return parsed HostAndPort
*/
public static HostAndPort from(String string) {
int lastColon = string.lastIndexOf(":");
String host = string.substring(0, lastColon);
int port = Integer.parseInt(string.substring(lastColon + 1));
return new HostAndPort(host, port);
}

/**
* Splits String into host and port parts.
* String must be in ( host + ":" + port ) format.
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/redis/clients/jedis/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +283,17 @@ public static enum Keyword {
ID, IDLE, TIME, RETRYCOUNT, FORCE, USAGE, SAMPLES, STREAM, GROUPS, CONSUMERS, HELP, FREQ,
SETUSER, GETUSER, DELUSER, WHOAMI, CAT, GENPASS, USERS;

/**
* @deprecated This will be private in future. Use {@link #getRaw()}.
*/
public final byte[] raw;

Keyword() {
raw = SafeEncoder.encode(this.name().toLowerCase(Locale.ENGLISH));
}

public byte[] getRaw() {
return raw;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public interface AdvancedBinaryJedisCommands {

Long slowlogLen();

List<byte[]> slowlogGetBinary();
List<Object> slowlogGetBinary();

List<byte[]> slowlogGetBinary(long entries);
List<Object> slowlogGetBinary(long entries);

Long objectRefcount(byte[] key);

Expand Down
20 changes: 19 additions & 1 deletion src/main/java/redis/clients/jedis/util/Slowlog.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@
import java.util.ArrayList;
import java.util.List;

import redis.clients.jedis.HostAndPort;

public class Slowlog {

private final long id;
private final long timeStamp;
private final long executionTime;
private final List<String> args;
private HostAndPort clientIpPort;
private String clientName;

private static final String COMMA = ",";

@SuppressWarnings("unchecked")
Expand All @@ -23,6 +29,10 @@ private Slowlog(List<Object> properties) {
for (byte[] barg : bargs) {
this.args.add(SafeEncoder.encode(barg));
}
if (properties.size() == 4) return;

this.clientIpPort = HostAndPort.from(SafeEncoder.encode((byte[]) properties.get(4)));
this.clientName = SafeEncoder.encode((byte[]) properties.get(5));
}

@SuppressWarnings("unchecked")
Expand All @@ -35,7 +45,7 @@ public static List<Slowlog> from(List<Object> nestedMultiBulkReply) {

return logs;
}

public long getId() {
return id;
}
Expand All @@ -52,6 +62,14 @@ public List<String> getArgs() {
return args;
}

public HostAndPort getClientIpPort() {
return clientIpPort;
}

public String getClientName() {
return clientName;
}

@Override
public String toString() {
return new StringBuilder().append(id).append(COMMA).append(timeStamp).append(COMMA)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
package redis.clients.jedis.tests.commands;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.util.List;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import redis.clients.jedis.Protocol;
import redis.clients.jedis.util.SafeEncoder;
import redis.clients.jedis.util.Slowlog;

public class SlowlogCommandsTest extends JedisCommandTestBase {

private static final String SLOWLOG_TIME_PARAM = "slowlog-log-slower-than";
private static final String ZERO = "0";
private String slowlogTimeValue;

@Before
@Override
public void setUp() throws Exception {
super.setUp();
slowlogTimeValue = jedis.configGet(SLOWLOG_TIME_PARAM).get(1);
}

@After
@Override
public void tearDown() throws Exception {
jedis.configSet(SLOWLOG_TIME_PARAM, slowlogTimeValue);
super.tearDown();
}

@Test
public void slowlog() {
final String slowlogTimeParam = "slowlog-log-slower-than";
final String slowlogTimeValue = jedis.configGet(slowlogTimeParam).get(1);

jedis.configSet("slowlog-log-slower-than", "0");
jedis.configSet(SLOWLOG_TIME_PARAM, ZERO);
jedis.set("foo", "bar");
jedis.set("foo2", "bar2");

Expand All @@ -30,27 +49,59 @@ public void slowlog() {
assertTrue(log.getExecutionTime() >= 0);
assertNotNull(log.getArgs());

List<byte[]> breducedLog = jedis.slowlogGetBinary(1);
List<Object> breducedLog = jedis.slowlogGetBinary(1);
assertEquals(1, breducedLog.size());

List<Slowlog> log1 = jedis.slowlogGet();
List<byte[]> blog1 = jedis.slowlogGetBinary();
List<Object> blog1 = jedis.slowlogGetBinary();

assertNotNull(log1);
assertNotNull(blog1);
}

long len1 = jedis.slowlogLen();

@Test
public void slowlogObjectDetails() {
final String clientName = "slowlog-object-client";
jedis.clientSetname(clientName);
jedis.slowlogReset();
jedis.configSet(SLOWLOG_TIME_PARAM, ZERO);

List<Slowlog> log2 = jedis.slowlogGet();
List<byte[]> blog2 = jedis.slowlogGetBinary();
long len2 = jedis.slowlogLen();

assertTrue(len1 > len2);
assertTrue(log1.size() > log2.size());
assertTrue(blog1.size() > blog2.size());
List<Slowlog> logs = jedis.slowlogGet(); // Get only 'CONFIG SET'
assertEquals(1, logs.size());
Slowlog log = logs.get(0);
assertTrue(log.getId() > 0);
assertTrue(log.getTimeStamp() > 0);
assertTrue(log.getExecutionTime() > 0);
assertEquals(4, log.getArgs().size());
assertEquals(SafeEncoder.encode(Protocol.Command.CONFIG.getRaw()), log.getArgs().get(0));
assertEquals(SafeEncoder.encode(Protocol.Keyword.SET.getRaw()), log.getArgs().get(1));
assertEquals(SLOWLOG_TIME_PARAM, log.getArgs().get(2));
assertEquals(ZERO, log.getArgs().get(3));
assertEquals("127.0.0.1", log.getClientIpPort().getHost());
assertTrue(log.getClientIpPort().getPort() > 0);
assertEquals(clientName, log.getClientName());
}

jedis.configSet(slowlogTimeParam, slowlogTimeValue);
@Test
public void slowlogBinaryDetails() {
final byte[] clientName = SafeEncoder.encode("slowlog-binary-client");
jedis.clientSetname(clientName);
jedis.slowlogReset();
jedis.configSet(SafeEncoder.encode(SLOWLOG_TIME_PARAM), SafeEncoder.encode(ZERO));

List<Object> logs = jedis.slowlogGetBinary(); // Get only 'CONFIG SET'
assertEquals(1, logs.size());
List<Object> log = (List<Object>) logs.get(0);
assertTrue((Long) log.get(0) > 0);
assertTrue((Long) log.get(1) > 0);
assertTrue((Long) log.get(2) > 0);
List<Object> args = (List<Object>) log.get(3);
assertEquals(4, args.size());
assertArrayEquals(Protocol.Command.CONFIG.getRaw(), (byte[]) args.get(0));
assertArrayEquals(Protocol.Keyword.SET.getRaw(), (byte[]) args.get(1));
assertArrayEquals(SafeEncoder.encode(SLOWLOG_TIME_PARAM), (byte[]) args.get(2));
assertArrayEquals(Protocol.toByteArray(0), (byte[]) args.get(3));
assertTrue(SafeEncoder.encode((byte[]) log.get(4)).startsWith("127.0.0.1:"));
assertArrayEquals(clientName, (byte[]) log.get(5));
}
}

0 comments on commit b54ce7d

Please sign in to comment.