Skip to content

Commit e7bd22a

Browse files
committed
Merge pull request #46 from jcoleman/sentinel-support
Sentinel support
2 parents 344aa5a + e8167a8 commit e7bd22a

File tree

2 files changed

+206
-5
lines changed

2 files changed

+206
-5
lines changed

README.markdown

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ Add the following into your Tomcat context.xml (or the context block of the serv
5858
host="localhost" <!-- optional: defaults to "localhost" -->
5959
port="6379" <!-- optional: defaults to "6379" -->
6060
database="0" <!-- optional: defaults to "0" -->
61-
maxInactiveInterval="60" <!-- optional: defaults to "60" (in seconds) --> />
61+
maxInactiveInterval="60" <!-- optional: defaults to "60" (in seconds) -->
62+
sentinelMaster="SentinelMasterName" <!-- optional -->
63+
sentinels="sentinel-host-1:port,sentinel-host-2:port,.." <!-- optional --> />
6264

6365
The Valve must be declared before the Manager.
6466

@@ -70,6 +72,11 @@ Copy the following files into the `TOMCAT_BASE/lib` directory:
7072

7173
Reboot the server, and sessions should now be stored in Redis.
7274

75+
Connection Pool Configuration
76+
-----------------------------
77+
78+
All of the configuration options from both `org.apache.commons.pool2.impl.GenericObjectPoolConfig` and `org.apache.commons.pool2.impl.BaseObjectPoolConfig` are also configurable for the Redis connection pool used by the session manager. To configure any of these attributes (e.g., `maxIdle` and `testOnBorrow`) just use the config attribute name prefixed with `connectionPool` (e.g., `connectionPoolMaxIdle` and `connectionPoolTestOnBorrow`) and set the desired value in the `<Manager>` declaration in your Tomcat context.xml.
79+
7380
Session Change Tracking
7481
-----------------------
7582

src/main/java/com/radiadesign/catalina/session/RedisSessionManager.java

Lines changed: 198 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
import org.apache.catalina.Session;
1111
import org.apache.catalina.session.ManagerBase;
1212

13+
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
14+
import org.apache.commons.pool2.impl.BaseObjectPoolConfig;
15+
16+
import redis.clients.util.Pool;
1317
import redis.clients.jedis.JedisPool;
18+
import redis.clients.jedis.JedisSentinelPool;
1419
import redis.clients.jedis.JedisPoolConfig;
1520
import redis.clients.jedis.Jedis;
1621
import redis.clients.jedis.Protocol;
@@ -20,6 +25,7 @@
2025
import java.util.Collections;
2126
import java.util.Enumeration;
2227
import java.util.Set;
28+
import java.util.HashSet;
2329

2430
import org.apache.juli.logging.Log;
2531
import org.apache.juli.logging.LogFactory;
@@ -36,7 +42,12 @@ public class RedisSessionManager extends ManagerBase implements Lifecycle {
3642
protected int database = 0;
3743
protected String password = null;
3844
protected int timeout = Protocol.DEFAULT_TIMEOUT;
39-
protected JedisPool connectionPool;
45+
protected String sentinelMaster = null;
46+
protected String sentinels = null;
47+
Set<String> sentinelSet = null;
48+
49+
protected Pool<Jedis> connectionPool;
50+
protected JedisPoolConfig connectionPoolConfig = new JedisPoolConfig();
4051

4152
protected RedisSessionHandlerValve handlerValve;
4253
protected ThreadLocal<RedisSession> currentSession = new ThreadLocal<>();
@@ -107,6 +118,33 @@ public void setSaveOnChange(boolean saveOnChange) {
107118
this.saveOnChange = saveOnChange;
108119
}
109120

121+
public String getSentinels() {
122+
return sentinels;
123+
}
124+
125+
public void setSentinels(String sentinels) {
126+
if (null == sentinels) {
127+
sentinels = "";
128+
}
129+
130+
String[] sentinelArray = getSentinels().split(",");
131+
this.sentinelSet = new HashSet<String>(Arrays.asList(sentinelArray));
132+
133+
this.sentinels = sentinels;
134+
}
135+
136+
public Set<String> getSentinelSet() {
137+
return this.sentinelSet;
138+
}
139+
140+
public String getSentinelMaster() {
141+
return this.sentinelMaster;
142+
}
143+
144+
public void setSentinelMaster(String master) {
145+
this.sentinelMaster = sentinelMaster;
146+
}
147+
110148
@Override
111149
public int getRejectedSessions() {
112150
// Essentially do nothing.
@@ -545,11 +583,19 @@ public void processExpires() {
545583

546584
private void initializeDatabaseConnection() throws LifecycleException {
547585
try {
548-
// TODO: Allow configuration of pool (such as size...)
549-
connectionPool = new JedisPool(new JedisPoolConfig(), getHost(), getPort(), getTimeout(), getPassword());
586+
if (getSentinelMaster() != null) {
587+
Set<String> sentinelSet = getSentinelSet();
588+
if (sentinelSet != null && sentinelSet.size() > 0) {
589+
connectionPool = new JedisSentinelPool(getSentinelMaster(), sentinelSet, this.connectionPoolConfig, getTimeout(), getPassword());
590+
} else {
591+
throw new LifecycleException("Error configuring Redis Sentinel connection pool: expected both `sentinelMaster` and `sentiels` to be configured");
592+
}
593+
} else {
594+
connectionPool = new JedisPool(this.connectionPoolConfig, getHost(), getPort(), getTimeout(), getPassword());
595+
}
550596
} catch (Exception e) {
551597
e.printStackTrace();
552-
throw new LifecycleException("Error Connecting to Redis", e);
598+
throw new LifecycleException("Error connecting to Redis", e);
553599
}
554600
}
555601

@@ -570,4 +616,152 @@ private void initializeSerializer() throws ClassNotFoundException, IllegalAccess
570616
}
571617
serializer.setClassLoader(classLoader);
572618
}
619+
620+
621+
// Connection Pool Config Accessors
622+
623+
// - from org.apache.commons.pool2.impl.GenericObjectPoolConfig
624+
625+
public int getConnectionPoolMaxTotal() {
626+
return this.connectionPoolConfig.getMaxTotal();
627+
}
628+
629+
public void setConnectionPoolMaxTotal(int connectionPoolMaxTotal) {
630+
this.connectionPoolConfig.setMaxTotal(connectionPoolMaxTotal);
631+
}
632+
633+
public int getConnectionPoolMaxIdle() {
634+
return this.connectionPoolConfig.getMaxIdle();
635+
}
636+
637+
public void setConnectionPoolMaxIdle(int connectionPoolMaxIdle) {
638+
this.connectionPoolConfig.setMaxIdle(connectionPoolMaxIdle);
639+
}
640+
641+
public int getConnectionPoolMinIdle() {
642+
return this.connectionPoolConfig.getMinIdle();
643+
}
644+
645+
public void setConnectionPoolMinIdle(int connectionPoolMinIdle) {
646+
this.connectionPoolConfig.setMinIdle(connectionPoolMinIdle);
647+
}
648+
649+
650+
// - from org.apache.commons.pool2.impl.BaseObjectPoolConfig
651+
652+
public boolean getLifo() {
653+
return this.connectionPoolConfig.getLifo();
654+
}
655+
public void setLifo(boolean lifo) {
656+
this.connectionPoolConfig.setLifo(lifo);
657+
}
658+
public long getMaxWaitMillis() {
659+
return this.connectionPoolConfig.getMaxWaitMillis();
660+
}
661+
662+
public void setMaxWaitMillis(long maxWaitMillis) {
663+
this.connectionPoolConfig.setMaxWaitMillis(maxWaitMillis);
664+
}
665+
666+
public long getMinEvictableIdleTimeMillis() {
667+
return this.connectionPoolConfig.getMinEvictableIdleTimeMillis();
668+
}
669+
670+
public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
671+
this.connectionPoolConfig.setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);
672+
}
673+
674+
public long getSoftMinEvictableIdleTimeMillis() {
675+
return this.connectionPoolConfig.getSoftMinEvictableIdleTimeMillis();
676+
}
677+
678+
public void setSoftMinEvictableIdleTimeMillis(long softMinEvictableIdleTimeMillis) {
679+
this.connectionPoolConfig.setSoftMinEvictableIdleTimeMillis(softMinEvictableIdleTimeMillis);
680+
}
681+
682+
public int getNumTestsPerEvictionRun() {
683+
return this.connectionPoolConfig.getNumTestsPerEvictionRun();
684+
}
685+
686+
public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
687+
this.connectionPoolConfig.setNumTestsPerEvictionRun(numTestsPerEvictionRun);
688+
}
689+
690+
public boolean getTestOnCreate() {
691+
return this.connectionPoolConfig.getTestOnCreate();
692+
}
693+
694+
public void setTestOnCreate(boolean testOnCreate) {
695+
this.connectionPoolConfig.setTestOnCreate(testOnCreate);
696+
}
697+
698+
public boolean getTestOnBorrow() {
699+
return this.connectionPoolConfig.getTestOnBorrow();
700+
}
701+
702+
public void setTestOnBorrow(boolean testOnBorrow) {
703+
this.connectionPoolConfig.setTestOnBorrow(testOnBorrow);
704+
}
705+
706+
public boolean getTestOnReturn() {
707+
return this.connectionPoolConfig.getTestOnReturn();
708+
}
709+
710+
public void setTestOnReturn(boolean testOnReturn) {
711+
this.connectionPoolConfig.setTestOnReturn(testOnReturn);
712+
}
713+
714+
public boolean getTestWhileIdle() {
715+
return this.connectionPoolConfig.getTestWhileIdle();
716+
}
717+
718+
public void setTestWhileIdle(boolean testWhileIdle) {
719+
this.connectionPoolConfig.setTestWhileIdle(testWhileIdle);
720+
}
721+
722+
public long getTimeBetweenEvictionRunsMillis() {
723+
return this.connectionPoolConfig.getTimeBetweenEvictionRunsMillis();
724+
}
725+
726+
public void setTimeBetweenEvictionRunsMillis(long timeBetweenEvictionRunsMillis) {
727+
this.connectionPoolConfig.setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);
728+
}
729+
730+
public String getEvictionPolicyClassName() {
731+
return this.connectionPoolConfig.getEvictionPolicyClassName();
732+
}
733+
734+
public void setEvictionPolicyClassName(String evictionPolicyClassName) {
735+
this.connectionPoolConfig.setEvictionPolicyClassName(evictionPolicyClassName);
736+
}
737+
738+
public boolean getBlockWhenExhausted() {
739+
return this.connectionPoolConfig.getBlockWhenExhausted();
740+
}
741+
742+
public void setBlockWhenExhausted(boolean blockWhenExhausted) {
743+
this.connectionPoolConfig.setBlockWhenExhausted(blockWhenExhausted);
744+
}
745+
746+
public boolean getJmxEnabled() {
747+
return this.connectionPoolConfig.getJmxEnabled();
748+
}
749+
750+
public void setJmxEnabled(boolean jmxEnabled) {
751+
this.connectionPoolConfig.setJmxEnabled(jmxEnabled);
752+
}
753+
public String getJmxNameBase() {
754+
return this.connectionPoolConfig.getJmxNameBase();
755+
}
756+
public void setJmxNameBase(String jmxNameBase) {
757+
this.connectionPoolConfig.setJmxNameBase(jmxNameBase);
758+
}
759+
760+
public String getJmxNamePrefix() {
761+
return this.connectionPoolConfig.getJmxNamePrefix();
762+
}
763+
764+
public void setJmxNamePrefix(String jmxNamePrefix) {
765+
this.connectionPoolConfig.setJmxNamePrefix(jmxNamePrefix);
766+
}
573767
}

0 commit comments

Comments
 (0)