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

[OPIK-493] Fix Redis setup to properly config database indexes #779

Merged
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.comet.opik.infrastructure;

import com.comet.opik.infrastructure.redis.RedisUrl;
import com.comet.opik.utils.JsonUtils;
import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.Valid;
Expand All @@ -19,10 +20,13 @@ public class RedisConfig {
public Config build() {
Config config = new Config();

var redisUrl = RedisUrl.parse(singleNodeUrl);

Objects.requireNonNull(singleNodeUrl, "singleNodeUrl must not be null");

config.useSingleServer()
.setAddress(singleNodeUrl);
.setAddress(singleNodeUrl)
.setDatabase(redisUrl.database());

config.setCodec(new JsonJacksonCodec(JsonUtils.MAPPER));
return config;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.comet.opik.infrastructure.redis;

import lombok.NonNull;

import java.net.URI;
import java.net.URISyntaxException;

public record RedisUrl(String scheme, String host, int port, int database) {

public static RedisUrl parse(@NonNull String redisUrl) {
try {
URI uri = new URI(redisUrl.trim());
String scheme = uri.getScheme();
String host = uri.getHost();
int port = uri.getPort() == -1 ? 6379 : uri.getPort();
String path = uri.getPath();
int database = 0;

if (path != null && path.length() > 1) {
idoberko2 marked this conversation as resolved.
Show resolved Hide resolved
database = getDatabase(path);
}

return new RedisUrl(scheme, host, port, database);
} catch (URISyntaxException e) {
throw new IllegalArgumentException(e);
}
}

private static int getDatabase(String path) {
try {
return Integer.parseInt(path.substring(1));
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid database index in Redis URL: " + path);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.comet.opik.infrastructure.redis;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.util.stream.Stream;

import static org.assertj.core.api.Assertions.assertThat;

@DisplayName("Redis URL parser Unit Test")
class RedisUrlTest {

public static Stream<Arguments> testRedisUrlParser() {
return Stream.of(
Arguments.of("redis://localhost:6379/0", "redis", "localhost", 6379, 0),
Arguments.of("redis://localhost:6379/1", "redis", "localhost", 6379, 1),
Arguments.of("redis://localhost:6379", "redis", "localhost", 6379, 0),
Arguments.of("rediss://localhost:6379", "rediss", "localhost", 6379, 0),
Arguments.of("rediss://master.redis.cache.amazonaws.com:7000/3", "rediss",
"master.redis.cache.amazonaws.com", 7000, 3),
Arguments.of("rediss://:xxxxxxxxxxx@redis-16379.hosted.com:16379/16", "rediss",
"redis-16379.hosted.com", 16379, 16)
);
}

@ParameterizedTest
@MethodSource
@DisplayName("Test parse method with different URLs")
void testRedisUrlParser(String url, String scheme, String host, int port, int database) {
var redisUrl = RedisUrl.parse(url);

assertThat(scheme).isEqualTo(redisUrl.scheme());
assertThat(host).isEqualTo(redisUrl.host());
assertThat(port).isEqualTo(redisUrl.port());
assertThat(database).isEqualTo(redisUrl.database());
}

}