Skip to content

Commit

Permalink
ZENKO-1051 Add redis sentinel support
Browse files Browse the repository at this point in the history
  • Loading branch information
tmacro committed Aug 24, 2018
1 parent 8c94b5e commit e06584a
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 38 deletions.
20 changes: 15 additions & 5 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ JQ_FILTERS_CONFIG="."
# for multiple endpoint locations
if [[ "$ENDPOINT" ]]; then
IFS="," read -ra HOST_NAMES <<< "$ENDPOINT"
for host in "${HOST_NAMES[@]}"; do
for host in "${HOST_NAMES[@]}"; do
JQ_FILTERS_CONFIG="$JQ_FILTERS_CONFIG | .restEndpoints[\"$host\"]=\"us-east-1\""
done
echo "Host name has been modified to ${HOST_NAMES[@]}"
Expand Down Expand Up @@ -94,21 +94,31 @@ if [[ "$MONGODB_DATABASE" ]]; then
JQ_FILTERS_CONFIG="$JQ_FILTERS_CONFIG | .mongodb.database=\"$MONGODB_DATABASE\""
fi

if [[ "$REDIS_HOST" ]]; then
if [ -z "$REDIS_HA_NAME" ]; then
REDIS_HA_NAME='mymaster'
fi

if [[ "$REDIS_SENTINELS" ]]; then
JQ_FILTERS_CONFIG="$JQ_FILTERS_CONFIG | .localCache.name=\"$REDIS_HA_NAME\""
JQ_FILTERS_CONFIG="$JQ_FILTERS_CONFIG | .localCache.sentinels=\"$REDIS_SENTINELS\""
elif [[ "$REDIS_HOST" ]]; then
JQ_FILTERS_CONFIG="$JQ_FILTERS_CONFIG | .localCache.host=\"$REDIS_HOST\""
JQ_FILTERS_CONFIG="$JQ_FILTERS_CONFIG | .localCache.port=6379"
fi

if [[ "$REDIS_PORT" ]]; then
if [[ "$REDIS_PORT" ]] && [[ ! "$REDIS_SENTINELS" ]]; then
JQ_FILTERS_CONFIG="$JQ_FILTERS_CONFIG | .localCache.port=$REDIS_PORT"
fi

if [[ "$REDIS_HA_HOST" ]]; then
if [[ "$REDIS_SENTINELS" ]]; then
JQ_FILTERS_CONFIG="$JQ_FILTERS_CONFIG | .redis.name=\"$REDIS_HA_NAME\""
JQ_FILTERS_CONFIG="$JQ_FILTERS_CONFIG | .redis.sentinels=\"$REDIS_SENTINELS\""
elif [[ "$REDIS_HA_HOST" ]]; then
JQ_FILTERS_CONFIG="$JQ_FILTERS_CONFIG | .redis.host=\"$REDIS_HA_HOST\""
JQ_FILTERS_CONFIG="$JQ_FILTERS_CONFIG | .redis.port=6379"
fi

if [[ "$REDIS_HA_PORT" ]]; then
if [[ "$REDIS_HA_PORT" ]] && [[ ! "$REDIS_SENTINELS" ]]; then
JQ_FILTERS_CONFIG="$JQ_FILTERS_CONFIG | .redis.port=$REDIS_HA_PORT"
fi

Expand Down
97 changes: 69 additions & 28 deletions lib/Config.js
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ class Config extends EventEmitter {
item.substr(0, lastColon);
// the port should not include the colon
const port = item.substr(lastColon + 1);
assert(parseInt(port, 10),
assert(Number.parseInt(port, 10),
'bad config: listenOn port must be a positive integer');
this.listenOn.push({ ip: ipAddress, port });
});
Expand Down Expand Up @@ -444,9 +444,9 @@ class Config extends EventEmitter {
'`replicationEndpoints` must be an array');
servers.forEach(item => {
assert(typeof item === 'string' && item !== '',
'bad config: each item of ' +
'`replicationEndpoints:servers` must be a non-empty ' +
'string');
'bad config: each item of ' +
'`replicationEndpoints:servers` must be a ' +
'non-empty string');
});
}
});
Expand Down Expand Up @@ -651,20 +651,53 @@ class Config extends EventEmitter {
assert(typeof config.localCache === 'object',
'config: invalid local cache configuration. localCache must ' +
'be an object');
assert(typeof config.localCache.host === 'string',
'config: invalid host for localCache. host must be a string');
assert(typeof config.localCache.port === 'number',
'config: invalid port for localCache. port must be a number');
if (config.localCache.password !== undefined) {
assert(this._verifyRedisPassword(config.localCache.password),
'config: invalid password for localCache. password must' +
if (config.localCache.sentinels) {
this.localCache = { sentinels: [], name: null };

assert(typeof config.localCache.name === 'string',
'bad config: localCache sentinel name must be a string');
this.localCache.name = config.localCache.name;

assert(Array.isArray(config.localCache.sentinels) ||
typeof config.localCache.sentinels === 'string',
'bad config: localCache sentinels' +
'must be an array or string');

if (typeof config.localCache.sentinels === 'string') {
config.localCache.sentinels.split(',').forEach(item => {
const [host, port] = item.split(':');
this.localCache.sentinels.push({ host,
port: Number.parseInt(port, 10) });
});
} else if (Array.isArray(config.localCache.sentinels)) {
config.localCache.sentinels.forEach(item => {
const { host, port } = item;
assert(typeof host === 'string',
'bad config: localCache' +
'sentinel host must be a string');
assert(typeof port === 'number',
'bad config: localCache' +
'sentinel port must be a number');
this.localCache.sentinels.push({ host, port });
});
}
} else {
assert(typeof config.localCache.host === 'string',
'config: bad host for localCache. host must be a string');
assert(typeof config.localCache.port === 'number',
'config: bad port for localCache. port must be a number');
if (config.localCache.password !== undefined) {
assert(
this._verifyRedisPassword(config.localCache.password),
'config: vad password for localCache. password must' +
' be a string');
}
this.localCache = {
host: config.localCache.host,
port: config.localCache.port,
password: config.localCache.password,
};
}
this.localCache = {
host: config.localCache.host,
port: config.localCache.port,
password: config.localCache.password,
};
}

if (config.mongodb) {
Expand All @@ -680,17 +713,26 @@ class Config extends EventEmitter {
assert(typeof config.redis.name === 'string',
'bad config: redis sentinel name must be a string');
this.redis.name = config.redis.name;

assert(Array.isArray(config.redis.sentinels),
'bad config: redis sentinels must be an array');
config.redis.sentinels.forEach(item => {
const { host, port } = item;
assert(typeof host === 'string',
'bad config: redis sentinel host must be a string');
assert(typeof port === 'number',
'bad config: redis sentinel port must be a number');
this.redis.sentinels.push({ host, port });
});
assert(Array.isArray(config.redis.sentinels) ||
typeof config.redis.sentinels === 'string',
'bad config: redis sentinels must be an array or string');

if (typeof config.redis.sentinels === 'string') {
config.redis.sentinels.split(',').forEach(item => {
const [host, port] = item.split(':');
this.redis.sentinels.push({ host,
port: Number.parseInt(port, 10) });
});
} else if (Array.isArray(config.redis.sentinels)) {
config.redis.sentinels.forEach(item => {
const { host, port } = item;
assert(typeof host === 'string',
'bad config: redis sentinel host must be a string');
assert(typeof port === 'number',
'bad config: redis sentinel port must be a number');
this.redis.sentinels.push({ host, port });
});
}
} else {
// check for standalone configuration
this.redis = {};
Expand All @@ -709,7 +751,6 @@ class Config extends EventEmitter {
this.redis.password = config.redis.password;
}
}

if (config.utapi) {
this.utapi = { component: 's3' };
if (config.utapi.port) {
Expand Down
6 changes: 1 addition & 5 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@ updateAllEndpoints();
// redis client
let localCacheClient;
if (_config.localCache) {
localCacheClient = new RedisClient({
host: _config.localCache.host,
port: _config.localCache.port,
password: _config.localCache.password,
}, logger);
localCacheClient = new RedisClient(_config.localCache, logger);
}
// stats client
const STATS_INTERVAL = 5; // 5 seconds
Expand Down

0 comments on commit e06584a

Please sign in to comment.