Skip to content
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

Add support for clientUnblock command #2424

Merged
merged 7 commits into from
Mar 17, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 9 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.net.ssl.SSLSocketFactory;

import redis.clients.jedis.Protocol.Keyword;
import redis.clients.jedis.args.UnblockType;
import redis.clients.jedis.params.ClientKillParams;
import redis.clients.jedis.params.GeoAddParams;
import redis.clients.jedis.params.GeoRadiusParam;
Expand Down Expand Up @@ -1229,6 +1230,14 @@ public void clientId() {
sendCommand(CLIENT, Keyword.ID.getRaw());
}

public void clientUnblock(final long clientId, final UnblockType unblockType) {
if (unblockType == null) {
sendCommand(CLIENT, Keyword.UNBLOCK.getRaw(), toByteArray(clientId));
} else {
sendCommand(CLIENT, Keyword.UNBLOCK.getRaw(), toByteArray(clientId), unblockType.getRaw());
}
}

public void time() {
sendCommand(TIME);
}
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/redis/clients/jedis/BinaryJedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocketFactory;

import redis.clients.jedis.args.UnblockType;
import redis.clients.jedis.commands.AdvancedBinaryJedisCommands;
import redis.clients.jedis.commands.BasicCommands;
import redis.clients.jedis.commands.BinaryJedisCommands;
Expand Down Expand Up @@ -4034,6 +4035,13 @@ public Long clientId() {
return client.getIntegerReply();
}

@Override
public Long clientUnblock(final long clientId, final UnblockType unblockType) {
checkIsInMultiOrPipeline();
client.clientUnblock(clientId, unblockType);
return client.getIntegerReply();
}

public String clientPause(final long timeout) {
checkIsInMultiOrPipeline();
client.clientPause(timeout);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/redis/clients/jedis/Jedis.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javax.net.ssl.SSLParameters;
import javax.net.ssl.SSLSocketFactory;

import redis.clients.jedis.args.UnblockType;
import redis.clients.jedis.commands.AdvancedJedisCommands;
import redis.clients.jedis.commands.BasicCommands;
import redis.clients.jedis.commands.ClusterCommands;
Expand Down Expand Up @@ -3386,6 +3387,13 @@ public Long clientId() {
return client.getIntegerReply();
}

@Override
public Long clientUnblock(final long clientId, final UnblockType unblockType) {
checkIsInMultiOrPipeline();
client.clientUnblock(clientId, unblockType);
return client.getIntegerReply();
}

@Override
public String migrate(final String host, final int port, final String key,
final int destinationDb, final int timeout) {
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 @@ -283,7 +283,7 @@ public static enum Keyword implements Rawable {
GETNAME, SETNAME, LIST, MATCH, COUNT, PING, PONG, UNLOAD, REPLACE, KEYS, PAUSE, DOCTOR, BLOCK,
NOACK, STREAMS, KEY, CREATE, MKSTREAM, SETID, DESTROY, DELCONSUMER, MAXLEN, GROUP, ID, IDLE,
TIME, RETRYCOUNT, FORCE, USAGE, SAMPLES, STREAM, GROUPS, CONSUMERS, HELP, FREQ, SETUSER,
GETUSER, DELUSER, WHOAMI, CAT, GENPASS, USERS, LOG, INCR, SAVE, JUSTID;
GETUSER, DELUSER, WHOAMI, CAT, GENPASS, USERS, LOG, INCR, SAVE, JUSTID, UNBLOCK;

/**
* @deprecated This will be private in future. Use {@link #getRaw()}.
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/redis/clients/jedis/args/UnblockType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package redis.clients.jedis.args;

import redis.clients.jedis.util.SafeEncoder;

/**
* Unblock type for {@code CLIENT UNBLOCK} command.
*/
public enum UnblockType implements Rawable {
TIMEOUT, ERROR;

private final byte[] raw;

UnblockType() {
raw = SafeEncoder.encode(this.name().toLowerCase());
sazzad16 marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
public byte[] getRaw() {
return new byte[0];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

import redis.clients.jedis.AccessControlUser;
import redis.clients.jedis.args.UnblockType;
import redis.clients.jedis.params.MigrateParams;
import redis.clients.jedis.params.ClientKillParams;

Expand Down Expand Up @@ -41,6 +42,8 @@ String migrate(String host, int port, int destinationDB, int timeout, MigratePar

Long clientKill(ClientKillParams params);

Long clientUnblock(long clientId, UnblockType unblockType);

byte[] clientGetnameBinary();

byte[] clientListBinary();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import redis.clients.jedis.AccessControlLogEntry;
import redis.clients.jedis.AccessControlUser;
import redis.clients.jedis.args.UnblockType;
import redis.clients.jedis.params.MigrateParams;
import redis.clients.jedis.params.ClientKillParams;
import redis.clients.jedis.util.Slowlog;
Expand Down Expand Up @@ -50,6 +51,8 @@ String migrate(String host, int port, int destinationDB, int timeout, MigratePar

Long clientId();

Long clientUnblock(long clientId, UnblockType unblockType);

String memoryDoctor();

Long memoryUsage(String key);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/redis/clients/jedis/commands/Commands.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import redis.clients.jedis.ListPosition;
import redis.clients.jedis.ScanParams;
import redis.clients.jedis.SortingParams;
import redis.clients.jedis.args.UnblockType;
import redis.clients.jedis.ZParams;
import redis.clients.jedis.params.GetExParams;
import redis.clients.jedis.params.MigrateParams;
Expand Down Expand Up @@ -424,6 +425,8 @@ default void restoreReplace(String key, int ttl, byte[] serializedValue) {

void clientId();

void clientUnblock(long clientId, UnblockType unblockType);

void memoryDoctor();

void xadd(String key, StreamEntryID id, Map<String, String> hash, long maxLen, boolean approximateLength);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
import static redis.clients.jedis.params.ClientKillParams.Type;
import static redis.clients.jedis.params.ClientKillParams.SkipMe;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -16,6 +21,7 @@

import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.args.UnblockType;
import redis.clients.jedis.exceptions.JedisConnectionException;
import redis.clients.jedis.params.ClientKillParams;

Expand Down Expand Up @@ -93,6 +99,22 @@ public void clientIdReconnect() {
assertTrue(clientIdInitial < clientIdAfterReconnect);
}

@Test
public void clientUnblock() throws InterruptedException, TimeoutException {
long clientId = client.clientId();
assertEquals(0, jedis.clientUnblock(clientId, UnblockType.ERROR).longValue());
Future<?> future = Executors.newSingleThreadExecutor().submit(() -> client.brpop(100000, "foo"));

try {
// to make true command already executed
TimeUnit.MILLISECONDS.sleep(500);
assertEquals(1, jedis.clientUnblock(clientId, UnblockType.ERROR).longValue());
future.get(1, TimeUnit.SECONDS);
} catch (ExecutionException e) {
assertEquals("redis.clients.jedis.exceptions.JedisDataException: UNBLOCKED client unblocked via CLIENT UNBLOCK", e.getMessage());
}
}

@Test
public void killIdString() {
String info = findInClientList();
Expand Down