Skip to content

Commit

Permalink
Support report libname and libver to Redis (#3356)
Browse files Browse the repository at this point in the history
* Support report libname and libver to Redis

#3338
Co-authored-by: M Sazzadul Hoque <[email protected]>

* Refactor ClientAttributeOption

Co-authored-by: M Sazzadul Hoque <[email protected]>

* Apply suggestions from code review

* Keep select()

* Update src/main/java/redis/clients/jedis/Connection.java

Co-authored-by: M Sazzadul Hoque <[email protected]>

---------

Co-authored-by: M Sazzadul Hoque <[email protected]>
  • Loading branch information
yangbodong22011 and sazzad16 authored Apr 7, 2023
1 parent 58f5761 commit 5192937
Show file tree
Hide file tree
Showing 10 changed files with 164 additions and 9 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@
</distributionManagement>

<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.jacoco</groupId>
Expand Down
47 changes: 39 additions & 8 deletions src/main/java/redis/clients/jedis/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@
import java.util.List;
import java.util.function.Supplier;

import redis.clients.jedis.Protocol.Command;
import redis.clients.jedis.Protocol.Keyword;
import redis.clients.jedis.args.ClientAttributeOption;
import redis.clients.jedis.args.Rawable;
import redis.clients.jedis.commands.ProtocolCommand;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.exceptions.JedisDataException;
import redis.clients.jedis.exceptions.JedisException;
import redis.clients.jedis.util.IOUtils;
import redis.clients.jedis.util.JedisMetaInfo;
import redis.clients.jedis.util.RedisInputStream;
import redis.clients.jedis.util.RedisOutputStream;
import redis.clients.jedis.util.SafeEncoder;
Expand Down Expand Up @@ -353,16 +357,47 @@ private void initializeFromClientConfig(JedisClientConfig config) {
auth(credentialsProvider);
}

List<CommandArguments> fireAndForgetMsg = new ArrayList<>();

int dbIndex = config.getDatabase();
if (dbIndex > 0) {
select(dbIndex);
fireAndForgetMsg.add(new CommandArguments(Command.SELECT).add(Protocol.toByteArray(dbIndex)));
}

String clientName = config.getClientName();
if (clientName != null) {
// TODO: need to figure out something without encoding
clientSetname(clientName);
fireAndForgetMsg.add(new CommandArguments(Command.CLIENT).add(Keyword.SETNAME).add(clientName));
}

String libName = JedisMetaInfo.getArtifactId();
if (libName != null) {
fireAndForgetMsg.add(new CommandArguments(Command.CLIENT).add(Keyword.SETINFO)
.add(ClientAttributeOption.LIB_NAME.getRaw()).add(libName));
}

String libVersion = JedisMetaInfo.getVersion();
if (libVersion != null) {
fireAndForgetMsg.add(new CommandArguments(Command.CLIENT).add(Keyword.SETINFO)
.add(ClientAttributeOption.LIB_VER.getRaw()).add(libVersion));
}

for (CommandArguments arg : fireAndForgetMsg) {
sendCommand(arg);
}

List<Object> objects = getMany(fireAndForgetMsg.size());
for (Object obj : objects) {
if (obj instanceof JedisDataException) {
JedisDataException e = (JedisDataException)obj;
String errorMsg = e.getMessage().toUpperCase();
if (errorMsg.contains("UNKNOWN") ||
errorMsg.contains("NOAUTH")) { // TODO: not filter out NOAUTH
// ignore
} else {
throw e;
}
}
}
} catch (JedisException je) {
try {
setBroken();
Expand Down Expand Up @@ -397,16 +432,12 @@ private void auth(final Supplier<RedisCredentials> credentialsProvider) {
getStatusCodeReply(); // OK
}

@Deprecated
public String select(final int index) {
sendCommand(Protocol.Command.SELECT, Protocol.toByteArray(index));
return getStatusCodeReply();
}

private String clientSetname(final String name) {
sendCommand(Protocol.Command.CLIENT, Protocol.Keyword.SETNAME.name(), name);
return getStatusCodeReply();
}

/**
* @deprecated The QUIT command is deprecated, see <a href="https://github.com/redis/redis/issues/11420">#11420</a>.
* {@link Connection#disconnect()} can be used instead.
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -4231,6 +4231,13 @@ public byte[] clientInfoBinary() {
return connection.getBinaryBulkReply();
}

@Override
public String clientSetInfo(ClientAttributeOption attr, byte[] value) {
checkIsInMultiOrPipeline();
connection.sendCommand(CLIENT, SETINFO.getRaw(), attr.getRaw(), value);
return connection.getStatusCodeReply();
}

@Override
public String clientSetname(final byte[] name) {
checkIsInMultiOrPipeline();
Expand Down Expand Up @@ -8514,6 +8521,13 @@ public String clientInfo() {
return connection.getBulkReply();
}

@Override
public String clientSetInfo(ClientAttributeOption attr, String value) {
checkIsInMultiOrPipeline();
connection.sendCommand(CLIENT, SETINFO.getRaw(), attr.getRaw(), encode(value));
return connection.getStatusCodeReply();
}

@Override
public String clientSetname(final String name) {
checkIsInMultiOrPipeline();
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/redis/clients/jedis/Protocol.java
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ public static enum Keyword implements Rawable {

AGGREGATE, ALPHA, BY, GET, LIMIT, NO, NOSORT, ONE, SET, STORE, WEIGHTS, WITHSCORES, RESETSTAT,
REWRITE, RESET, FLUSH, EXISTS, LOAD, LEN, HELP, SCHEDULE, MATCH, COUNT, TYPE, KEYS, REFCOUNT,
ENCODING, IDLETIME, FREQ, REPLACE, GETNAME, SETNAME, LIST, ID, KILL, PAUSE, UNPAUSE, UNBLOCK,
ENCODING, IDLETIME, FREQ, REPLACE, GETNAME, SETNAME, SETINFO, LIST, ID, KILL, PAUSE, UNPAUSE, UNBLOCK,
STREAMS, CREATE, MKSTREAM, SETID, DESTROY, DELCONSUMER, MAXLEN, GROUP, IDLE, TIME, BLOCK, NOACK,
RETRYCOUNT, STREAM, GROUPS, CONSUMERS, JUSTID, WITHVALUES, NOMKSTREAM, MINID, CREATECONSUMER,
SETUSER, GETUSER, DELUSER, WHOAMI, USERS, CAT, GENPASS, LOG, SAVE, DRYRUN, COPY, AUTH, AUTH2,
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/redis/clients/jedis/args/ClientAttributeOption.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package redis.clients.jedis.args;

import redis.clients.jedis.util.SafeEncoder;

/**
* CLIENT SETINFO command attr option
* since redis 7.2
*/
public enum ClientAttributeOption implements Rawable {
LIB_NAME("LIB-NAME"),
LIB_VER("LIB-VER");

private final byte[] raw;

private ClientAttributeOption(String str) {
this.raw = SafeEncoder.encode(str);
}

@Override
public byte[] getRaw() {
return raw;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package redis.clients.jedis.commands;

import redis.clients.jedis.args.ClientAttributeOption;
import redis.clients.jedis.args.ClientPauseMode;
import redis.clients.jedis.args.ClientType;
import redis.clients.jedis.args.UnblockType;
Expand Down Expand Up @@ -76,6 +77,15 @@ public interface ClientBinaryCommands {
*/
byte[] clientInfoBinary();

/**
* client set info command
* Since redis 7.2
* @param attr the attr option
* @param value the value
* @return OK or error
*/
String clientSetInfo(ClientAttributeOption attr, byte[] value);

/**
* Assigns a name to the current connection.
*
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/redis/clients/jedis/commands/ClientCommands.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package redis.clients.jedis.commands;

import redis.clients.jedis.args.ClientAttributeOption;
import redis.clients.jedis.args.ClientPauseMode;
import redis.clients.jedis.args.ClientType;
import redis.clients.jedis.args.UnblockType;
Expand Down Expand Up @@ -76,6 +77,15 @@ public interface ClientCommands {
*/
String clientInfo();

/**
* client set info command
* Since redis 7.2
* @param attr the attr option
* @param value the value
* @return OK or error
*/
String clientSetInfo(ClientAttributeOption attr, String value);

/**
* Assigns a name to the current connection.
*
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/redis/clients/jedis/util/JedisMetaInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package redis.clients.jedis.util;

import java.io.InputStream;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Jedis Meta info load version groupId
*/
public class JedisMetaInfo {
private static final Logger log = LoggerFactory.getLogger(JedisMetaInfo.class);

private static String groupId;
private static String artifactId;
private static String version;

static {
Properties p = new Properties();
try {
InputStream in = JedisMetaInfo.class.getClassLoader().getResourceAsStream("pom.properties");
p.load(in);

groupId = p.getProperty("groupId", null);
artifactId = p.getProperty("artifactId", null);
version = p.getProperty("version", null);

in.close();
} catch (Exception e) {
log.error("Load Jedis meta info from pom.properties failed", e);
}
}

public static String getGroupId() {
return groupId;
}

public static String getArtifactId() {
return artifactId;
}

public static String getVersion() {
return version;
}
}
3 changes: 3 additions & 0 deletions src/main/resources/pom.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
groupId=${project.groupId}
artifactId=${project.artifactId}
version=${project.version}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.junit.Test;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.args.ClientAttributeOption;
import redis.clients.jedis.args.ClientType;
import redis.clients.jedis.args.UnblockType;
import redis.clients.jedis.exceptions.JedisConnectionException;
Expand Down Expand Up @@ -62,6 +63,17 @@ public void nameBinary() {
assertArrayEquals(name, client.clientGetnameBinary());
}

@Test
public void clientSetInfoDefault() {
String libName = "jedis";
String libVersion = "999.999.999";
assertEquals("OK", client.clientSetInfo(ClientAttributeOption.LIB_NAME, libName));
assertEquals("OK", client.clientSetInfo(ClientAttributeOption.LIB_VER, libVersion));
String info = client.clientInfo();
assertTrue(info.contains("lib-name=jedis"));
assertTrue(info.contains("lib-ver=999.999.999"));
}

@Test
public void clientId() {
long clientId = client.clientId();
Expand Down

0 comments on commit 5192937

Please sign in to comment.