2016-06-07 12:58:18 -04:00
|
|
|
Settings = require "settings-sharelatex"
|
|
|
|
async = require "async"
|
2016-06-08 07:18:37 -04:00
|
|
|
_ = require "underscore"
|
|
|
|
logger = require "logger-sharelatex"
|
2016-06-28 11:49:15 -04:00
|
|
|
Metrics = require "metrics-sharelatex"
|
2016-06-07 12:58:18 -04:00
|
|
|
|
|
|
|
class Client
|
|
|
|
constructor: (@clients) ->
|
2016-07-12 06:04:37 -04:00
|
|
|
@SECONDARY_TIMEOUT = 600
|
2016-06-23 10:38:51 -04:00
|
|
|
@HEARTBEAT_TIMEOUT = 2000
|
2016-06-07 12:58:18 -04:00
|
|
|
|
|
|
|
multi: () ->
|
|
|
|
return new MultiClient(
|
|
|
|
@clients.map (client) -> {
|
|
|
|
rclient: client.rclient.multi()
|
|
|
|
key_schema: client.key_schema
|
|
|
|
primary: client.primary
|
2016-06-08 07:18:37 -04:00
|
|
|
driver: client.driver
|
2016-06-07 12:58:18 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-06-23 13:00:03 -04:00
|
|
|
healthCheck: (callback) ->
|
|
|
|
jobs = @clients.map (client) =>
|
|
|
|
(cb) => @_healthCheckClient(client, cb)
|
|
|
|
async.parallel jobs, callback
|
2016-06-23 10:38:51 -04:00
|
|
|
|
2016-06-23 13:00:03 -04:00
|
|
|
_healthCheckClient: (client, callback) ->
|
|
|
|
if client.driver == "ioredis"
|
|
|
|
@_healthCheckClusterClient(client, callback)
|
|
|
|
else
|
|
|
|
@_healthCheckNodeRedisClient(client, callback)
|
2016-06-23 10:59:25 -04:00
|
|
|
|
2016-06-23 13:00:03 -04:00
|
|
|
_healthCheckNodeRedisClient: (client, callback) ->
|
|
|
|
client.healthCheck ?= require("redis-sharelatex").activeHealthCheckRedis(Settings.redis.web)
|
|
|
|
if client.healthCheck.isAlive()
|
|
|
|
return callback()
|
|
|
|
else
|
|
|
|
return callback(new Error("node-redis client failed health check"))
|
|
|
|
|
|
|
|
_healthCheckClusterClient: (client, callback) ->
|
|
|
|
jobs = client.rclient.nodes("all").map (n) =>
|
|
|
|
(cb) => @_checkNode(n, cb)
|
|
|
|
async.parallel jobs, callback
|
|
|
|
|
|
|
|
_checkNode: (node, _callback) ->
|
|
|
|
callback = (args...) ->
|
|
|
|
_callback(args...)
|
|
|
|
_callback = () ->
|
2016-06-23 10:59:25 -04:00
|
|
|
timer = setTimeout () ->
|
2016-06-23 13:00:03 -04:00
|
|
|
error = new Error("ioredis node ping check timed out")
|
|
|
|
logger.error {err: error, key: node.options.key}, "node timed out"
|
|
|
|
callback(error)
|
2016-06-23 10:59:25 -04:00
|
|
|
, @HEARTBEAT_TIMEOUT
|
|
|
|
node.ping (err) ->
|
2016-06-23 13:00:03 -04:00
|
|
|
clearTimeout timer
|
|
|
|
callback(err)
|
2016-06-23 10:38:51 -04:00
|
|
|
|
2016-06-07 12:58:18 -04:00
|
|
|
class MultiClient
|
|
|
|
constructor: (@clients) ->
|
2016-07-12 06:45:10 -04:00
|
|
|
@SECONDARY_TIMEOUT = 600
|
2016-06-07 12:58:18 -04:00
|
|
|
|
|
|
|
exec: (callback) ->
|
2016-07-05 11:07:47 -04:00
|
|
|
primaryError = null
|
|
|
|
primaryResult = null
|
2016-07-06 07:25:36 -04:00
|
|
|
jobs = @clients.map (client) =>
|
|
|
|
(cb) =>
|
|
|
|
cb = _.once(cb)
|
2016-07-04 06:14:23 -04:00
|
|
|
timer = new Metrics.Timer("redis.#{client.driver}.exec")
|
2016-07-06 07:25:36 -04:00
|
|
|
|
|
|
|
timeout = null
|
|
|
|
if !client.primary
|
|
|
|
timeout = setTimeout () ->
|
2016-07-07 04:45:55 -04:00
|
|
|
logger.error {err: new Error("#{client.driver} backend timed out")}, "backend timed out"
|
|
|
|
cb()
|
2016-07-06 07:25:36 -04:00
|
|
|
, @SECONDARY_TIMEOUT
|
|
|
|
|
|
|
|
client.rclient.exec (error, result) =>
|
2016-07-04 06:14:23 -04:00
|
|
|
timer.done()
|
2016-06-08 07:18:37 -04:00
|
|
|
if client.driver == "ioredis"
|
|
|
|
# ioredis returns an results like:
|
|
|
|
# [ [null, 42], [null, "foo"] ]
|
|
|
|
# where the first entries in each 2-tuple are
|
|
|
|
# presumably errors for each individual command,
|
|
|
|
# and the second entry is the result. We need to transform
|
|
|
|
# this into the same result as the old redis driver:
|
|
|
|
# [ 42, "foo" ]
|
|
|
|
filtered_result = []
|
|
|
|
for entry in result or []
|
|
|
|
if entry[0]?
|
|
|
|
return cb(entry[0])
|
|
|
|
else
|
|
|
|
filtered_result.push entry[1]
|
|
|
|
result = filtered_result
|
|
|
|
|
2016-06-07 12:58:18 -04:00
|
|
|
if client.primary
|
2016-07-05 11:07:47 -04:00
|
|
|
primaryError = error
|
|
|
|
primaryResult = result
|
2016-07-06 07:25:36 -04:00
|
|
|
if timeout?
|
|
|
|
clearTimeout(timeout)
|
2016-06-08 07:18:37 -04:00
|
|
|
cb(error, result)
|
2016-06-07 12:58:18 -04:00
|
|
|
async.parallel jobs, (error, results) ->
|
2016-06-08 07:18:37 -04:00
|
|
|
if error?
|
|
|
|
logger.error {err: error}, "error in redis backend"
|
|
|
|
else
|
2016-07-06 09:24:27 -04:00
|
|
|
compareResults(results, "exec")
|
2016-07-05 11:07:47 -04:00
|
|
|
callback(primaryError, primaryResult)
|
2016-06-07 12:58:18 -04:00
|
|
|
|
2016-06-08 07:18:37 -04:00
|
|
|
COMMANDS = {
|
|
|
|
"get": 0,
|
|
|
|
"smembers": 0,
|
|
|
|
"set": 0,
|
|
|
|
"srem": 0,
|
|
|
|
"sadd": 0,
|
|
|
|
"del": 0,
|
|
|
|
"lrange": 0,
|
|
|
|
"llen": 0,
|
|
|
|
"rpush": 0,
|
|
|
|
"expire": 0,
|
|
|
|
"ltrim": 0,
|
|
|
|
"incr": 0,
|
|
|
|
"eval": 2
|
|
|
|
}
|
|
|
|
for command, key_pos of COMMANDS
|
|
|
|
do (command, key_pos) ->
|
|
|
|
Client.prototype[command] = (args..., callback) ->
|
2016-07-05 11:07:47 -04:00
|
|
|
primaryError = null
|
2016-07-06 10:02:05 -04:00
|
|
|
primaryResult = []
|
2016-07-06 07:25:36 -04:00
|
|
|
jobs = @clients.map (client) =>
|
|
|
|
(cb) =>
|
|
|
|
cb = _.once(cb)
|
2016-06-08 07:18:37 -04:00
|
|
|
key_builder = args[key_pos]
|
2016-06-07 12:58:18 -04:00
|
|
|
key = key_builder(client.key_schema)
|
2016-06-08 07:18:37 -04:00
|
|
|
args_with_key = args.slice(0)
|
|
|
|
args_with_key[key_pos] = key
|
2016-07-04 06:14:23 -04:00
|
|
|
timer = new Metrics.Timer("redis.#{client.driver}.#{command}")
|
2016-07-06 07:25:36 -04:00
|
|
|
|
|
|
|
timeout = null
|
|
|
|
if !client.primary
|
|
|
|
timeout = setTimeout () ->
|
2016-07-07 04:45:55 -04:00
|
|
|
logger.error {err: new Error("#{client.driver} backend timed out")}, "backend timed out"
|
|
|
|
cb()
|
2016-07-06 07:25:36 -04:00
|
|
|
, @SECONDARY_TIMEOUT
|
|
|
|
|
|
|
|
client.rclient[command] args_with_key..., (error, result...) =>
|
2016-07-04 06:14:23 -04:00
|
|
|
timer.done()
|
2016-06-07 12:58:18 -04:00
|
|
|
if client.primary
|
2016-07-05 11:07:47 -04:00
|
|
|
primaryError = error
|
|
|
|
primaryResult = result
|
2016-07-06 07:25:36 -04:00
|
|
|
if timeout?
|
|
|
|
clearTimeout(timeout)
|
2016-06-08 07:18:37 -04:00
|
|
|
cb(error, result...)
|
|
|
|
async.parallel jobs, (error, results) ->
|
|
|
|
if error?
|
|
|
|
logger.error {err: error}, "error in redis backend"
|
|
|
|
else
|
2016-07-06 09:24:27 -04:00
|
|
|
compareResults(results, command)
|
2016-07-05 11:07:47 -04:00
|
|
|
callback(primaryError, primaryResult...)
|
2016-06-07 12:58:18 -04:00
|
|
|
|
2016-06-08 07:18:37 -04:00
|
|
|
MultiClient.prototype[command] = (args...) ->
|
2016-06-07 12:58:18 -04:00
|
|
|
for client in @clients
|
2016-06-08 07:18:37 -04:00
|
|
|
key_builder = args[key_pos]
|
2016-06-07 12:58:18 -04:00
|
|
|
key = key_builder(client.key_schema)
|
2016-06-08 07:18:37 -04:00
|
|
|
args_with_key = args.slice(0)
|
|
|
|
args_with_key[key_pos] = key
|
2016-06-08 11:41:58 -04:00
|
|
|
client.rclient[command] args_with_key...
|
2016-06-07 12:58:18 -04:00
|
|
|
|
2016-07-06 09:24:27 -04:00
|
|
|
compareResults = (results, command) ->
|
2016-06-08 07:18:37 -04:00
|
|
|
return if results.length < 2
|
|
|
|
first = results[0]
|
2016-07-07 06:24:33 -04:00
|
|
|
if command == "smembers" and first?
|
2016-07-06 09:24:27 -04:00
|
|
|
first = first.slice().sort()
|
2016-06-08 07:18:37 -04:00
|
|
|
for result in results.slice(1)
|
2016-07-07 06:24:33 -04:00
|
|
|
if command == "smembers" and result?
|
2016-07-06 09:24:27 -04:00
|
|
|
result = result.slice().sort()
|
2016-06-08 07:18:37 -04:00
|
|
|
if not _.isEqual(first, result)
|
2016-07-05 11:07:47 -04:00
|
|
|
logger.error results: results, "redis backend conflict"
|
2016-06-28 11:49:15 -04:00
|
|
|
Metrics.inc "backend-conflict"
|
|
|
|
else
|
|
|
|
Metrics.inc "backend-match"
|
2016-06-07 12:58:18 -04:00
|
|
|
|
|
|
|
module.exports =
|
|
|
|
createClient: () ->
|
|
|
|
client_configs = Settings.redis.documentupdater
|
|
|
|
unless client_configs instanceof Array
|
|
|
|
client_configs.primary = true
|
|
|
|
client_configs = [client_configs]
|
|
|
|
clients = client_configs.map (config) ->
|
2016-06-07 13:38:32 -04:00
|
|
|
if config.cluster?
|
|
|
|
Redis = require("ioredis")
|
|
|
|
rclient = new Redis.Cluster(config.cluster)
|
2016-06-08 07:18:37 -04:00
|
|
|
driver = "ioredis"
|
2016-06-07 13:38:32 -04:00
|
|
|
else
|
2016-06-17 09:11:22 -04:00
|
|
|
redis_config = {}
|
|
|
|
for key in ["host", "port", "password", "endpoints", "masterName"]
|
|
|
|
if config[key]?
|
|
|
|
redis_config[key] = config[key]
|
|
|
|
rclient = require("redis-sharelatex").createClient(redis_config)
|
2016-07-04 06:14:23 -04:00
|
|
|
driver = "noderedis"
|
2016-06-08 07:18:37 -04:00
|
|
|
return {
|
|
|
|
rclient: rclient
|
|
|
|
key_schema: config.key_schema
|
|
|
|
primary: config.primary
|
|
|
|
driver: driver
|
|
|
|
}
|
2016-06-07 12:58:18 -04:00
|
|
|
return new Client(clients)
|