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

Support all RedisClientTypes #141

Merged
merged 4 commits into from
Oct 12, 2023
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
42 changes: 25 additions & 17 deletions src/main/java/org/swisspush/reststorage/DefaultRedisProvider.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
import io.vertx.redis.client.RedisConnection;
import io.vertx.redis.client.RedisClientType;
import io.vertx.redis.client.RedisOptions;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.swisspush.reststorage.redis.RedisProvider;
import org.swisspush.reststorage.util.ModuleConfiguration;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -95,19 +96,20 @@ private Future<RedisAPI> connectToRedis() {
.setMaxPoolSize(redisMaxPoolSize)
.setMaxPoolWaiting(redisMaxPoolWaitingSize)
.setPoolRecycleTimeout(redisPoolRecycleTimeoutMs)
.setMaxWaitingHandlers(redisMaxPipelineWaitingSize);
if (configuration.isRedisClustered()) {
redisOptions.setType(RedisClientType.CLUSTER);
redisOptions.addConnectionString(createConnectString());
} else {
redisOptions.setConnectionString(createConnectString());
}
.setMaxWaitingHandlers(redisMaxPipelineWaitingSize)
.setType(configuration.getRedisClientType());

createConnectStrings().forEach(redisOptions::addConnectionString);

redis = Redis.createClient(vertx, redisOptions);

redis.connect().onSuccess(conn -> {
log.info("Successfully connected to redis");
client = conn;
client.close();

if (configuration.getRedisClientType() == RedisClientType.STANDALONE) {
client.close();
}

// make sure the client is reconnected on error
// eg, the underlying TCP connection is closed but the client side doesn't know it yet
Expand Down Expand Up @@ -138,16 +140,22 @@ private Future<RedisAPI> connectToRedis() {
return promise.future();
}

private String createConnectString() {
StringBuilder connectionStringBuilder = new StringBuilder();
connectionStringBuilder.append(configuration.isRedisEnableTls() ? "rediss://" : "redis://");
String redisUser = configuration.getRedisUser();
private List<String> createConnectStrings() {
String redisPassword = configuration.getRedisPassword();
if (StringUtils.isNotEmpty(redisUser) && StringUtils.isNotEmpty(redisPassword)) {
connectionStringBuilder.append(configuration.getRedisUser()).append(":").append(redisPassword).append("@");
String redisUser = configuration.getRedisUser();
StringBuilder connectionStringPrefixBuilder = new StringBuilder();
connectionStringPrefixBuilder.append(configuration.isRedisEnableTls() ? "rediss://" : "redis://");
if (redisUser != null && !redisUser.isEmpty()) {
connectionStringPrefixBuilder.append(redisUser).append(":").append((redisPassword == null ? "" : redisPassword)).append("@");
}
List<String> connectionString = new ArrayList<>();
String connectionStringPrefix = connectionStringPrefixBuilder.toString();
for (int i = 0; i < configuration.getRedisHosts().size(); i++) {
String host = configuration.getRedisHosts().get(i);
int port = configuration.getRedisPorts().get(i);
connectionString.add(connectionStringPrefix + host + ":" + port);
}
connectionStringBuilder.append(configuration.getRedisHost()).append(":").append(configuration.getRedisPort());
return connectionStringBuilder.toString();
return connectionString;
}

private void attemptReconnect(int retry) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package org.swisspush.reststorage.util;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import io.vertx.core.json.JsonObject;
import io.vertx.redis.client.RedisClientType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* Utility class to configure the RestStorageModule.
*
* @author https://github.com/mcweba [Marc-Andre Weber]
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class ModuleConfiguration {

private static final Logger log = LoggerFactory.getLogger(ModuleConfiguration.class);
Expand All @@ -29,10 +34,10 @@
private String prefix = "";
private String storageAddress = "resource-storage";
private Map<String, String> editorConfig = null;
private String redisHost = "localhost";
private int redisPort = 6379;
private List<String> redisHosts = Collections.singletonList("localhost");
private List<Integer> redisPorts = Collections.singletonList(6379);
private boolean redisEnableTls;
private boolean redisClustered;
private RedisClientType redisClientType = RedisClientType.STANDALONE;
/**
* @deprecated Instance authentication is considered as legacy. With Redis from 6.x on the ACL authentication method should be used.
*/
Expand Down Expand Up @@ -116,12 +121,22 @@
}

public ModuleConfiguration redisHost(String redisHost) {
this.redisHost = redisHost;
this.redisHosts = Collections.singletonList(redisHost);
return this;
}

public ModuleConfiguration redisPort(int redisPort) {
this.redisPort = redisPort;
this.redisPorts = Collections.singletonList(redisPort);
return this;
}

public ModuleConfiguration redisHosts(List<String> redisHosts) {
this.redisHosts = redisHosts;
return this;

Check warning on line 135 in src/main/java/org/swisspush/reststorage/util/ModuleConfiguration.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/swisspush/reststorage/util/ModuleConfiguration.java#L134-L135

Added lines #L134 - L135 were not covered by tests
}

public ModuleConfiguration redisPorts(List<Integer> redisPorts) {
this.redisPorts = redisPorts;

Check warning on line 139 in src/main/java/org/swisspush/reststorage/util/ModuleConfiguration.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/swisspush/reststorage/util/ModuleConfiguration.java#L139

Added line #L139 was not covered by tests
return this;
}

Expand All @@ -130,9 +145,9 @@
return this;
}

public ModuleConfiguration redisClustered(boolean redisClustered) {
this.redisClustered = redisClustered;
public ModuleConfiguration redisClientType(RedisClientType redisClientType) {
this.redisClientType = redisClientType;
return this;

Check warning on line 150 in src/main/java/org/swisspush/reststorage/util/ModuleConfiguration.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/swisspush/reststorage/util/ModuleConfiguration.java#L149-L150

Added lines #L149 - L150 were not covered by tests
}

public ModuleConfiguration redisReconnectAttempts(int redisReconnectAttempts) {
Expand Down Expand Up @@ -287,11 +302,18 @@
}

public String getRedisHost() {
return redisHost;
return redisHosts.get(0);
}

public List<String> getRedisHosts() {
return redisHosts;
}

public int getRedisPort() {
return redisPort;
return redisPorts.get(0);
}
public List<Integer> getRedisPorts() {
return redisPorts;
}

public int getRedisReconnectAttempts() {
Expand All @@ -314,10 +336,6 @@
return redisEnableTls;
}

public boolean isRedisClustered() {
return redisClustered;
}

public String getRedisAuth() {
return redisAuth;
}
Expand All @@ -329,7 +347,9 @@
public String getRedisUser() {
return redisUser;
}

public RedisClientType getRedisClientType() {
return redisClientType;
}
public String getExpirablePrefix() {
return expirablePrefix;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.Collections;
import java.util.HashMap;

import static org.swisspush.reststorage.util.ModuleConfiguration.StorageType;
Expand Down Expand Up @@ -272,8 +273,8 @@ public void testGetOverriddenFromJsonObject(TestContext testContext){
json.put("prefix", "newprefix");
json.put("storageAddress", "newStorageAddress");
json.put("editorConfig", new JsonObject().put("myKey", "myValue"));
json.put("redisHost", "newredishost");
json.put("redisPort", 4321);
json.put("redisHosts", Collections.singletonList("newredishost"));
json.put("redisPorts", Collections.singletonList(4321));
json.put("maxRedisWaitingHandlers", 4096);
json.put("expirablePrefix", "newExpirablePrefix");
json.put("resourcesPrefix", "newResourcesPrefix");
Expand Down