Skip to content

Commit

Permalink
Merge pull request #36 from GDLMadushanka/sentinel
Browse files Browse the repository at this point in the history
Add sentinel password support
  • Loading branch information
GDLMadushanka authored Dec 8, 2022
2 parents 0c1d2f4 + 0561976 commit 744b71d
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 8 deletions.
10 changes: 5 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<groupId>org.wso2.carbon.connector</groupId>
<artifactId>org.wso2.carbon.connector.redis</artifactId>
<packaging>jar</packaging>
<version>2.3.0-SNAPSHOT</version>
<version>2.6.0-SNAPSHOT</version>
<name>WSO2 Carbon - Mediation Library Connector For Redis</name>
<url>http://wso2.org</url>
<properties>
Expand All @@ -48,7 +48,7 @@
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.3.0</version>
<version>3.9.0</version>
</dependency>
<dependency>
<groupId>org.apache.synapse</groupId>
Expand Down Expand Up @@ -350,7 +350,7 @@
<repository>
<id>wso2-nexus</id>
<name>WSO2 internal Repository</name>
<url>http://maven.wso2.org/nexus/content/groups/wso2-public/</url>
<url>https://maven.wso2.org/nexus/content/groups/wso2-public/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
Expand All @@ -360,7 +360,7 @@
<repository>
<id>wso2.releases</id>
<name>WSO2 internal Repository</name>
<url>http://maven.wso2.org/nexus/content/repositories/releases/</url>
<url>https://maven.wso2.org/nexus/content/repositories/releases/</url>
<releases>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
Expand All @@ -370,7 +370,7 @@
<repository>
<id>wso2.snapshots</id>
<name>Apache Snapshot Repository</name>
<url>http://maven.wso2.org/nexus/content/repositories/snapshots/</url>
<url>https://maven.wso2.org/nexus/content/repositories/snapshots/</url>
<snapshots>
<enabled>true</enabled>
<updatePolicy>daily</updatePolicy>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package org.wso2.carbon.connector.operations;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import org.apache.synapse.MessageContext;
import org.apache.synapse.SynapseException;
Expand All @@ -29,6 +30,7 @@
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisSentinelPool;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.Protocol;

import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -260,6 +262,35 @@ private Jedis createSentinel() {
if (masterPasswordProp != null && !masterPasswordProp.isEmpty()) {
masterPassword = masterPasswordProp;
}

String masterUser = (String) messageContext.getProperty(RedisConstants.REDIS_MASTER_USER);
String sentinelUser = (String) messageContext.getProperty(RedisConstants.SENTINEL_USER);
String sentinelPassword = (String) messageContext.getProperty(RedisConstants.SENTINEL_PASSWORD);
String sentinelClientName = (String) messageContext.getProperty(RedisConstants.SENTINEL_CLIENT_NAME);
String clientName = (String) messageContext.getProperty(RedisConstants.CLIENT_NAME);
String sentinelConnTimeoutProp = (String) messageContext.getProperty(RedisConstants.SENTINEL_CONNECTION_TIMEOUT);
int sentinelConnectionTimeout = Protocol.DEFAULT_TIMEOUT;
if (!StringUtils.isEmpty(sentinelConnTimeoutProp)) {
try {
sentinelConnectionTimeout = Integer.parseInt(sentinelConnTimeoutProp);
} catch (NumberFormatException e) {
throw new SynapseException(
"Invalid input for \"" + RedisConstants.SENTINEL_CONNECTION_TIMEOUT + "\". Cannot parse "
+ sentinelConnTimeoutProp + " to an Integer.", e);
}
}
String sentinelSoTimeoutProp = (String) messageContext.getProperty(RedisConstants.SENTINEL_SO_TIMEOUT);
int sentinelSoTimeout = Protocol.DEFAULT_TIMEOUT;
if (!StringUtils.isEmpty(sentinelConnTimeoutProp)) {
try {
sentinelSoTimeout = Integer.parseInt(sentinelConnTimeoutProp);
} catch (NumberFormatException e) {
throw new SynapseException(
"Invalid input for \"" + RedisConstants.SENTINEL_SO_TIMEOUT + "\". Cannot parse "
+ sentinelSoTimeoutProp + " to an Integer.", e);
}
}

//Use double lock to avoid creating a new Jedis Sentinel connection pool for each request.
if (jedisSentinelPoolMap.get(uniquePoolId) == null) {
lock.lock();
Expand All @@ -268,8 +299,11 @@ private Jedis createSentinel() {
GenericObjectPoolConfig config = new GenericObjectPoolConfig();
config.setMaxTotal(maxConnections);
config.setMaxIdle(maxConnections);
jedisSentinelPoolMap.put(uniquePoolId, new JedisSentinelPool(masterName, sentinels, config,
connectionTimeout, soTimeout, masterPassword, dbNumber));
JedisSentinelPool jedisSentinelPool = new JedisSentinelPool(masterName, sentinels, config,
connectionTimeout, soTimeout, masterUser, masterPassword, dbNumber, clientName,
sentinelConnectionTimeout, sentinelSoTimeout, sentinelUser, sentinelPassword,
sentinelClientName);
jedisSentinelPoolMap.put(uniquePoolId, jedisSentinelPool);
}
} finally {
lock.unlock();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ public class RedisConstants {
public static final String MESSAGE = "redisMessage";
public static final String CACHEKEY = "cacheKey";
public static final String CACHEHOSTNAME = "cacheHostname";
public static final String SENTINEL_USER = "sentinelUser";
public static final String SENTINEL_CLIENT_NAME = "sentinelClientName";
public static final String SENTINEL_PASSWORD = "sentinelPassword";
public static final String SENTINEL_CONNECTION_TIMEOUT = "sentinelConnectionTimeout";
public static final String SENTINEL_SO_TIMEOUT = "sentinelSoTimeout";
public static final String REDIS_MASTER_USER = "masterUser";
public static final String USESSL = "useSsl";
public static final String CONNECTION_TIMEOUT = "redisConnectionTimeout";
public static final String REDIS_CLUSTER_ENABLED = "redisClusterEnabled";
Expand Down
44 changes: 43 additions & 1 deletion src/main/resources/config/init.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@
<parameter name="maxAttempts" description="the number of retries"/>

<!-- Redis Sentinel specific parameters-->
<parameter name="sentinelPassword" description="sentinel password"/>
<parameter name="sentinelUser" description="sentinel user name"/>
<parameter name="sentinelClientName" description="sentinel client name"/>
<parameter name="masterUser" description="master user name"/>
<parameter name="sentinelConnectionTimeout" description="connection timeout for sentinel"/>
<parameter name="sentinelSoTimeout" description="socket timeout for sentinel"/>
<parameter name="sentinelEnabled" description="a flag to enable redis sentinel"/>
<parameter name="masterName" description="master name for sentinel"/>
<parameter name="sentinels" description="comma separated list of sentinels"/>
Expand Down Expand Up @@ -135,7 +141,43 @@
<else>
<property name="sentinelEnabled" expression="$func:sentinelEnabled"/>
</else>
</filter>
</filter>
<filter xpath="$func:sentinelPassword = '' or not(string($func:sentinelPassword))">
<then/>
<else>
<property name="sentinelPassword" expression="$func:sentinelPassword"/>
</else>
</filter>
<filter xpath="$func:sentinelUser = '' or not(string($func:sentinelUser))">
<then/>
<else>
<property name="sentinelUser" expression="$func:sentinelUser"/>
</else>
</filter>
<filter xpath="$func:sentinelClientName = '' or not(string($func:sentinelClientName))">
<then/>
<else>
<property name="sentinelClientName" expression="$func:sentinelClientName"/>
</else>
</filter>
<filter xpath="$func:masterUser = '' or not(string($func:masterUser))">
<then/>
<else>
<property name="masterUser" expression="$func:masterUser"/>
</else>
</filter>
<filter xpath="$func:sentinelConnectionTimeout = '' or not(string($func:sentinelConnectionTimeout))">
<then/>
<else>
<property name="sentinelConnectionTimeout" expression="$func:sentinelConnectionTimeout"/>
</else>
</filter>
<filter xpath="$func:sentinelSoTimeout = '' or not(string($func:sentinelSoTimeout))">
<then/>
<else>
<property name="sentinelSoTimeout" expression="$func:sentinelSoTimeout"/>
</else>
</filter>
<filter xpath="$func:masterName = '' or not(string($func:masterName))">
<then/>
<else>
Expand Down

0 comments on commit 744b71d

Please sign in to comment.