Bug fixes and added redis sentinel supportability changes.

pull/27/head
Ranjith Manickam 2018-12-04 03:05:05 +05:30
parent 8aa5359453
commit 551a855675
2 changed files with 35 additions and 18 deletions

View File

@ -7,7 +7,7 @@ public interface DataCacheConstants {
String APPLICATION_PROPERTIES_FILE = "redis-data-cache.properties"; String APPLICATION_PROPERTIES_FILE = "redis-data-cache.properties";
// redis properties // redis properties
String REDIS_HOSTS = "redis.hosts:127.0.0.1:6379"; String REDIS_HOSTS = "redis.hosts";
String REDIS_CLUSTER_ENABLED = "redis.cluster.enabled:false"; String REDIS_CLUSTER_ENABLED = "redis.cluster.enabled:false";
String REDIS_SENTINEL_ENABLED = "redis.sentinel.enabled:false"; String REDIS_SENTINEL_ENABLED = "redis.sentinel.enabled:false";
String REDIS_LB_STICKY_SESSION_ENABLED = "redis.load-balancer.sticky-session.enabled:false"; String REDIS_LB_STICKY_SESSION_ENABLED = "redis.load-balancer.sticky-session.enabled:false";
@ -29,6 +29,9 @@ public interface DataCacheConstants {
String REDIS_CONN_FAILED_RETRY_MSG = "Jedis connection failed, retrying..."; String REDIS_CONN_FAILED_RETRY_MSG = "Jedis connection failed, retrying...";
String SESSION_EXPIRY_JOB_INTERVAL = "redis.session.expiry.job.interval:60";
String SESSION_DATA_SYNC_JOB_INTERVAL = "redis.session.data-sync.job.interval:10";
enum RedisConfigType { enum RedisConfigType {
DEFAULT, DEFAULT,
SENTINEL, SENTINEL,

View File

@ -3,6 +3,8 @@ package tomcat.request.session.data.cache.impl;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import tomcat.request.session.data.cache.DataCache; import tomcat.request.session.data.cache.DataCache;
import tomcat.request.session.data.cache.DataCacheConstants;
import tomcat.request.session.data.cache.DataCacheFactory;
import tomcat.request.session.data.cache.impl.redis.RedisCache; import tomcat.request.session.data.cache.impl.redis.RedisCache;
import java.io.Serializable; import java.io.Serializable;
@ -10,12 +12,18 @@ import java.util.Date;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.TimeUnit;
/** author: Ranjith Manickam @ 3 Dec' 2018 */ /** author: Ranjith Manickam @ 3 Dec' 2018 */
public class StandardDataCache extends RedisCache { public class StandardDataCache extends RedisCache {
private Date lastSessionJobRun; private boolean triggerDataSync;
private boolean triggerRedisSync;
private Date expiryJobTriggered;
private Date dataSyncJobTriggered;
private final int expiryJobTriggerInterval;
private final int dataSyncJobTriggerInterval;
private final int sessionExpiryTime; private final int sessionExpiryTime;
private final Map<String, SessionData> sessionData; private final Map<String, SessionData> sessionData;
@ -24,8 +32,11 @@ public class StandardDataCache extends RedisCache {
super(properties); super(properties);
this.sessionExpiryTime = sessionExpiryTime; this.sessionExpiryTime = sessionExpiryTime;
this.sessionData = new ConcurrentHashMap<>(); this.sessionData = new ConcurrentHashMap<>();
this.lastSessionJobRun = new Date(); this.expiryJobTriggered = new Date();
this.triggerRedisSync = false; this.dataSyncJobTriggered = new Date();
this.triggerDataSync = false;
this.expiryJobTriggerInterval = Integer.parseInt(DataCacheFactory.getProperty(properties, DataCacheConstants.SESSION_EXPIRY_JOB_INTERVAL));
this.dataSyncJobTriggerInterval = Integer.parseInt(DataCacheFactory.getProperty(properties, DataCacheConstants.SESSION_DATA_SYNC_JOB_INTERVAL));
} }
/** {@inheritDoc} */ /** {@inheritDoc} */
@ -35,7 +46,7 @@ public class StandardDataCache extends RedisCache {
try { try {
return super.set(key, value); return super.set(key, value);
} catch (RuntimeException ex) { } catch (RuntimeException ex) {
this.triggerRedisSync = true; this.triggerDataSync = true;
} }
return value; return value;
} }
@ -48,7 +59,7 @@ public class StandardDataCache extends RedisCache {
retValue = super.setnx(key, value); retValue = super.setnx(key, value);
} catch (RuntimeException ex) { } catch (RuntimeException ex) {
retValue = this.sessionData.containsKey(key) ? 0L : 1L; retValue = this.sessionData.containsKey(key) ? 0L : 1L;
this.triggerRedisSync = true; this.triggerDataSync = true;
} }
if (retValue == 1L) { if (retValue == 1L) {
@ -63,7 +74,7 @@ public class StandardDataCache extends RedisCache {
try { try {
return super.expire(key, seconds); return super.expire(key, seconds);
} catch (RuntimeException ex) { } catch (RuntimeException ex) {
this.triggerRedisSync = true; this.triggerDataSync = true;
} }
return null; return null;
} }
@ -78,7 +89,7 @@ public class StandardDataCache extends RedisCache {
try { try {
return super.get(key); return super.get(key);
} catch (RuntimeException ex) { } catch (RuntimeException ex) {
this.triggerRedisSync = true; this.triggerDataSync = true;
throw ex; throw ex;
} }
} }
@ -90,7 +101,7 @@ public class StandardDataCache extends RedisCache {
try { try {
return super.delete(key); return super.delete(key);
} catch (RuntimeException ex) { } catch (RuntimeException ex) {
this.triggerRedisSync = true; this.triggerDataSync = true;
} }
return (value == null) ? 0L : 1L; return (value == null) ? 0L : 1L;
} }
@ -122,18 +133,21 @@ public class StandardDataCache extends RedisCache {
/** To handle session data. */ /** To handle session data. */
private synchronized void handleSessionData() { private synchronized void handleSessionData() {
// redis sync // redis sync
if (this.triggerRedisSync) { if (this.triggerDataSync) {
new SessionDataSyncThread(this, this.sessionData, this.sessionExpiryTime); long difference = new Date().getTime() - this.dataSyncJobTriggered.getTime();
this.triggerRedisSync = false;
if (difference >= TimeUnit.MINUTES.toMillis(this.dataSyncJobTriggerInterval)) {
new SessionDataSyncThread(this, this.sessionData, this.sessionExpiryTime);
this.triggerDataSync = false;
this.dataSyncJobTriggered = new Date();
}
} }
// session expiry // session expiry
long diff = new Date().getTime() - this.lastSessionJobRun.getTime(); long difference = new Date().getTime() - this.expiryJobTriggered.getTime();
long diffMinutes = diff / (60 * 1000) % 60; if (difference >= TimeUnit.MINUTES.toMillis(this.expiryJobTriggerInterval)) {
if (diffMinutes > 0L) {
new SessionDataExpiryThread(this.sessionData, this.sessionExpiryTime); new SessionDataExpiryThread(this.sessionData, this.sessionExpiryTime);
this.lastSessionJobRun = new Date(); this.expiryJobTriggered = new Date();
} }
} }