Skip to content

Commit

Permalink
Merge pull request #27861 from cescoffier/redis-6-compat
Browse files Browse the repository at this point in the history
Fix compatibility issue with Redis 6
  • Loading branch information
cescoffier authored Sep 13, 2022
2 parents 66caddd + 7d64701 commit 8e620fb
Show file tree
Hide file tree
Showing 13 changed files with 99 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public IncrementResource(RedisDataSource ds) {

@GET
public int increment() {
return (int) commands.incrby("counter", INCREMENT);
return (int) commands.incrby("counter-dev-mode", INCREMENT);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -212,12 +212,12 @@ public <T> List<String> toArgs(Codec<T> codec) {
if (radius > 0) {
list.add("BYRADIUS");
list.add(Double.toString(radius));
list.add(unit.name());
list.add(unit.toString());
} else {
list.add("BYBOX");
list.add(Double.toString(width));
list.add(Double.toString(height));
list.add(unit.name());
list.add(unit.toString());
}

if (direction != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,12 @@ public <T> List<String> toArgs(Codec<T> codec) {
if (radius > 0) {
list.add("BYRADIUS");
list.add(Double.toString(radius));
list.add(unit.name());
list.add(unit.toString());
} else {
list.add("BYBOX");
list.add(Double.toString(width));
list.add(Double.toString(height));
list.add(unit.name());
list.add(unit.toString());
}

if (direction != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.quarkus.redis.datasource.geo;

import java.util.Locale;

public enum GeoUnit {

/**
Expand All @@ -21,4 +23,9 @@ public enum GeoUnit {
* mile.
*/
MI;

public String toString() {
// Redis 6 requires lower case, uppercase has only been added in Redis 7.
return name().toLowerCase(Locale.ROOT);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ Uni<Response> _geodist(K key, V from, V to, GeoUnit unit) {
.put(marshaller.encode(key))
.put(marshaller.encode(from))
.put(marshaller.encode(to))
.put(unit.name()));
.put(unit.toString()));
}

Uni<Response> _geohash(K key, V... members) {
Expand Down Expand Up @@ -142,7 +142,7 @@ Uni<Response> _georadius(K key, double longitude, double latitude, double radius

return execute(RedisCommand.of(Command.GEORADIUS)
.put(marshaller.encode(key)).put(longitude).put(latitude).put(radius)
.put(unit.name()));
.put(unit.toString()));
}

Uni<Response> _georadius(K key, double longitude, double latitude, double radius, GeoUnit unit,
Expand All @@ -155,7 +155,7 @@ Uni<Response> _georadius(K key, double longitude, double latitude, double radius
nonNull(geoArgs, "geoArgs");
return execute(RedisCommand.of(Command.GEORADIUS)
.put(marshaller.encode(key)).put(longitude).put(latitude).put(radius)
.put(unit.name())
.put(unit.toString())
.putArgs(geoArgs));
}

Expand All @@ -169,7 +169,7 @@ Uni<Response> _georadius(K key, double longitude, double latitude, double radius
nonNull(geoArgs, "geoArgs");
return execute(RedisCommand.of(Command.GEORADIUS)
.put(marshaller.encode(key)).put(longitude).put(latitude).put(radius)
.put(unit.name())
.put(unit.toString())
.putArgs(geoArgs, keyCodec));
}

Expand All @@ -180,7 +180,7 @@ Uni<Response> _georadiusbymember(K key, V member, double distance, GeoUnit unit,
nonNull(unit, "unit");
nonNull(geoArgs, "geoArgs");
return execute(RedisCommand.of(Command.GEORADIUSBYMEMBER).put(marshaller.encode(key)).put(marshaller.encode(member))
.put(distance).put(unit.name())
.put(distance).put(unit.toString())
.putArgs(geoArgs));
}

Expand All @@ -190,7 +190,7 @@ Uni<Response> _georadiusbymember(K key, V member, double distance, GeoUnit unit)
positive(distance, "distance");
nonNull(unit, "unit");
return execute(RedisCommand.of(Command.GEORADIUSBYMEMBER).put(marshaller.encode(key))
.put(marshaller.encode(member)).put(distance).put(unit.name()));
.put(marshaller.encode(member)).put(distance).put(unit.toString()));
}

Uni<Response> _georadiusbymember(K key, V member, double distance, GeoUnit unit, GeoRadiusStoreArgs<K> geoArgs) {
Expand All @@ -201,7 +201,7 @@ Uni<Response> _georadiusbymember(K key, V member, double distance, GeoUnit unit,
nonNull(geoArgs, "geoArgs");
return execute(RedisCommand.of(Command.GEORADIUSBYMEMBER).put(marshaller.encode(key))
.put(marshaller.encode(member))
.put(distance).put(unit.name())
.put(distance).put(unit.toString())
.putArgs(geoArgs, keyCodec));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
import org.testcontainers.utility.DockerImageName;

import io.vertx.mutiny.core.Vertx;
import io.vertx.mutiny.redis.client.Command;
import io.vertx.mutiny.redis.client.Redis;
import io.vertx.mutiny.redis.client.RedisAPI;
import io.vertx.mutiny.redis.client.Request;

public class DatasourceTestBase {

Expand All @@ -20,7 +22,7 @@ public class DatasourceTestBase {
public static RedisAPI api;

static GenericContainer<?> server = new GenericContainer<>(
DockerImageName.parse("redis:7-alpine"))
DockerImageName.parse(System.getProperty("redis.base.image", "redis:7-alpine")))
.withExposedPorts(6379);

@BeforeAll
Expand All @@ -39,4 +41,12 @@ static void cleanup() {
vertx.closeAndAwait();
}

public static String getRedisVersion() {
String info = redis.send(Request.cmd(Command.INFO)).await().indefinitely().toString();
// Look for the redis_version line
return info.lines().filter(s -> s.startsWith("redis_version")).findAny()
.map(line -> line.split(":")[1])
.orElseThrow();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ void expire() {
}

@Test
@RequiresRedis7OrHigher
void expireWithArgs() {
assertThat(keys.expire(key, 10, new ExpireArgs().xx())).isFalse();
strings.set(key, Person.person7);
Expand All @@ -158,6 +159,7 @@ void expireat() {
}

@Test
@RequiresRedis7OrHigher
void expireatWithArgs() {
Date expiration = new Date(System.currentTimeMillis() + 10000);
assertThat(keys.expireat(key, expiration.toInstant().getEpochSecond(), new ExpireArgs().xx())).isFalse();
Expand Down Expand Up @@ -222,6 +224,7 @@ void pexpire() {
}

@Test
@RequiresRedis7OrHigher
void pexpireWithArgs() {
assertThat(keys.pexpire(key, 5000, new ExpireArgs().xx())).isFalse();
strings.set(key, Person.person7);
Expand All @@ -235,6 +238,7 @@ void pexpireWithArgs() {
}

@Test
@RequiresRedis7OrHigher
void pexpireWithDuration() {
assertThat(keys.pexpire(key, Duration.ofSeconds(5))).isFalse();
strings.set(key, Person.person7);
Expand All @@ -258,6 +262,7 @@ void pexpireat() {
}

@Test
@RequiresRedis7OrHigher
void pexpireatWithArgs() {
Instant expiration = new Date(System.currentTimeMillis() + 5000).toInstant();
assertThat(keys.pexpireat(key, expiration.getEpochSecond(), new ExpireArgs().xx())).isFalse();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ void blpop() {
}

@Test
@RequiresRedis7OrHigher
void blmpop() {
lists.rpush("two", Person.person1, Person.person2, Person.person3);
assertThat(lists.blmpop(Duration.ofSeconds(1), Position.RIGHT, "one", "two"))
Expand All @@ -59,6 +60,7 @@ void blmpop() {
}

@Test
@RequiresRedis7OrHigher
void blmpopMany() {
lists.rpush("two", Person.person1, Person.person2, Person.person3);
assertThat(lists.blmpop(Duration.ofSeconds(1), Position.RIGHT, 2, "one", "two"))
Expand Down Expand Up @@ -135,6 +137,7 @@ void lpop() {
}

@Test
@RequiresRedis7OrHigher
void lmpop() {
assertThat(lists.lmpop(Position.RIGHT, key)).isNull();
lists.rpush(key, Person.person1, Person.person2);
Expand All @@ -143,6 +146,7 @@ void lmpop() {
}

@Test
@RequiresRedis7OrHigher
void lmpopMany() {
assertThat(lists.lmpop(Position.RIGHT, 2, key)).isEmpty();
lists.rpush(key, Person.person1, Person.person2);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package io.quarkus.redis.datasource;

import static org.junit.jupiter.api.extension.ConditionEvaluationResult.disabled;
import static org.junit.jupiter.api.extension.ConditionEvaluationResult.enabled;

import java.util.Optional;

import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;
import org.junit.platform.commons.util.AnnotationUtils;

class Redis7OrHigherCondition implements ExecutionCondition {

private static final ConditionEvaluationResult ENABLED_BY_DEFAULT = enabled("@RequiresRedis7OrHigher is not present");

@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext context) {
Optional<RequiresRedis7OrHigher> optional = AnnotationUtils.findAnnotation(context.getElement(),
RequiresRedis7OrHigher.class);

if (optional.isPresent()) {
String version = DatasourceTestBase.getRedisVersion();

return isRedis7orHigher(version) ? enabled("Redis " + version + " >= 7")
: disabled("Disabled, Redis " + version + " < 7");
}

return ENABLED_BY_DEFAULT;
}

public static boolean isRedis7orHigher(String version) {
return Integer.parseInt(version.split("\\.")[0]) >= 7;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.quarkus.redis.datasource;

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import org.junit.jupiter.api.extension.ExtendWith;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@ExtendWith(Redis7OrHigherCondition.class)
@interface RequiresRedis7OrHigher {

// Important the class must extend DatasourceTestBase.
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ void sdiffstore() {
}

@Test
@RequiresRedis7OrHigher // because of sintercard
void sinter() {
populate();
assertThat(sets.sinter("key1", "key2", "key3")).isEqualTo(Set.of(person3));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ void zincrby() {
}

@Test
@RequiresRedis7OrHigher
void zintercard() {
setOfPlaces.zadd("zset1", Map.of(Place.crussol, 1.0, Place.grignan, 2.0));
setOfPlaces.zadd("zset2", Map.of(Place.crussol, 2.0, Place.grignan, 1.0));
Expand Down Expand Up @@ -734,6 +735,7 @@ public void zmscore() {
}

@Test
@RequiresRedis7OrHigher
public void zmpopMin() {
assertThat(setOfPlaces.zmpopMin("zset1")).isEqualTo(null);

Expand All @@ -754,6 +756,7 @@ public void zmpopMin() {
}

@Test
@RequiresRedis7OrHigher
public void zmpopMax() {
assertThat(setOfPlaces.zmpopMax("zset1")).isEqualTo(null);

Expand All @@ -774,6 +777,7 @@ public void zmpopMax() {
}

@Test
@RequiresRedis7OrHigher
public void bzmpopMin() {
assertThat(setOfPlaces.bzmpopMin(Duration.ofSeconds(1), "zset1")).isEqualTo(null);

Expand All @@ -796,6 +800,7 @@ public void bzmpopMin() {
}

@Test
@RequiresRedis7OrHigher
public void bzmpopMax() {
assertThat(setOfPlaces.bzmpopMax(Duration.ofSeconds(1), "zset1")).isEqualTo(null);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ void strlen() {
}

@Test
@RequiresRedis7OrHigher
void lcs() {
strings.mset(Map.of("key1", "ohmytext", "key2", "mynewtext"));
assertThat(strings.lcs("key1", "key2")).isEqualTo("mytext");
Expand Down

0 comments on commit 8e620fb

Please sign in to comment.