Skip to content

Commit

Permalink
Merge in changes from redis#1455 and push to our nexus
Browse files Browse the repository at this point in the history
  • Loading branch information
akshay-gavagai committed Nov 17, 2020
1 parent 818dc9d commit 7440511
Show file tree
Hide file tree
Showing 9 changed files with 795 additions and 32 deletions.
41 changes: 9 additions & 32 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<packaging>jar</packaging>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.4.0-SNAPSHOT</version>
<version>3.4.0-Gavagai</version>
<name>Jedis</name>
<description>Jedis is a blazingly small and sane Redis java client.</description>
<url>https://github.com/xetorthio/jedis</url>
Expand Down Expand Up @@ -93,14 +93,16 @@
</dependencies>

<distributionManagement>
<snapshotRepository>
<id>ossrh</id>
<url>https://oss.sonatype.org/content/repositories/snapshots</url>
</snapshotRepository>
<repository>
<id>ossrh</id>
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
<id>deployment</id>
<name>Internal Releases</name>
<url>http://nexus.gavagai.se/nexus3/content/repositories/releases/</url>
</repository>
<snapshotRepository>
<id>deployment-snapshots</id>
<name>Internal Snapshots</name>
<url>http://nexus.gavagai.se/nexus3/content/repositories/snapshots/</url>
</snapshotRepository>
</distributionManagement>

<build>
Expand Down Expand Up @@ -162,17 +164,6 @@
<artifactId>maven-release-plugin</artifactId>
<version>2.4.2</version>
</plugin>
<plugin>
<groupId>org.sonatype.plugins</groupId>
<artifactId>nexus-staging-maven-plugin</artifactId>
<version>1.6.7</version>
<extensions>true</extensions>
<configuration>
<serverId>ossrh</serverId>
<nexusUrl>https://oss.sonatype.org/</nexusUrl>
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugin>
<groupId>com.googlecode.maven-java-formatter-plugin</groupId>
<artifactId>maven-java-formatter-plugin</artifactId>
Expand All @@ -189,20 +180,6 @@
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-gpg-plugin</artifactId>
<version>1.5</version>
<executions>
<execution>
<id>sign-artifacts</id>
<phase>verify</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.felix</groupId>
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/redis/clients/jedis/CommandListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package redis.clients.jedis;

import redis.clients.jedis.commands.ProtocolCommand;

public interface CommandListener {

void afterCommand(Connection conn, ProtocolCommand cmd, final byte[][] args);

}
13 changes: 13 additions & 0 deletions src/main/java/redis/clients/jedis/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class Connection implements Closeable {
private RedisOutputStream outputStream;
private RedisInputStream inputStream;
private boolean broken = false;
private CommandListener commandListener;

public Connection() {
this(Protocol.DEFAULT_HOST);
Expand Down Expand Up @@ -97,6 +98,14 @@ public void rollbackTimeout() {
}
}

public void setCommandListener(CommandListener listener) {
this.commandListener = listener;
}

public void clearCommandListener() {
this.commandListener = null;
}

public void sendCommand(final ProtocolCommand cmd, final String... args) {
final byte[][] bargs = new byte[args.length][];
for (int i = 0; i < args.length; i++) {
Expand All @@ -113,6 +122,10 @@ public void sendCommand(final ProtocolCommand cmd, final byte[]... args) {
try {
connect();
Protocol.sendCommand(outputStream, cmd, args);
// Record commands sent, for retrying in cluster pipeline.
if (commandListener != null) {
commandListener.afterCommand(this, cmd, args);
}
} catch (JedisConnectionException ex) {
/*
* When client send request which formed by invalid protocol, Redis send back error message
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/redis/clients/jedis/JedisCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ public JedisCluster(Set<HostAndPort> jedisClusterNode, int connectionTimeout, in
ssl, sslSocketFactory, sslParameters, hostnameVerifier, hostAndPortMap);
}

public JedisClusterPipeline pipelined() {
return new JedisClusterPipeline(connectionHandler);
}

@Override
public String set(final String key, final String value) {
return new JedisClusterCommand<String>(connectionHandler, maxAttempts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ public Map<String, JedisPool> getNodes() {
return cache.getNodes();
}

public HostAndPort getSlotNode(int slot) {
return cache.getSlotNode(slot);
}

public void assignSlotToNode(int slot, HostAndPort targetNode) {
cache.assignSlotToNode(slot, targetNode);
}

private void initializeSlotsCache(Set<HostAndPort> startNodes,
int connectionTimeout, int soTimeout, String user, String password, String clientName,
boolean ssl, SSLSocketFactory sslSocketFactory, SSLParameters sslParameters, HostnameVerifier hostnameVerifier) {
Expand Down
13 changes: 13 additions & 0 deletions src/main/java/redis/clients/jedis/JedisClusterInfoCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
public class JedisClusterInfoCache {
private final Map<String, JedisPool> nodes = new HashMap<String, JedisPool>();
private final Map<Integer, JedisPool> slots = new HashMap<Integer, JedisPool>();
private Map<Integer, HostAndPort> mapping = new HashMap<Integer, HostAndPort>();

private final ReentrantReadWriteLock rwl = new ReentrantReadWriteLock();
private final Lock r = rwl.readLock();
Expand Down Expand Up @@ -160,6 +161,7 @@ public void renewClusterSlots(Jedis jedis) {
private void discoverClusterSlots(Jedis jedis) {
List<Object> slots = jedis.clusterSlots();
this.slots.clear();
this.mapping.clear();

for (Object slotInfoObj : slots) {
List<Object> slotInfo = (List<Object>) slotInfoObj;
Expand Down Expand Up @@ -227,6 +229,7 @@ public void assignSlotsToNode(List<Integer> targetSlots, HostAndPort targetNode)
JedisPool targetPool = setupNodeIfNotExist(targetNode);
for (Integer slot : targetSlots) {
slots.put(slot, targetPool);
mapping.put(slot, targetNode);
}
} finally {
w.unlock();
Expand Down Expand Up @@ -288,6 +291,7 @@ public void reset() {
}
nodes.clear();
slots.clear();
mapping.clear();
} finally {
w.unlock();
}
Expand All @@ -313,4 +317,13 @@ private List<Integer> getAssignedSlotArray(List<Object> slotInfo) {
}
return slotNums;
}

public HostAndPort getSlotNode(int slot) {
r.lock();
try {
return mapping.get(slot);
} finally {
r.unlock();
}
}
}
Loading

0 comments on commit 7440511

Please sign in to comment.