RedisBackendTests.coffee 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  1. sinon = require('sinon')
  2. chai = require('chai')
  3. should = chai.should()
  4. modulePath = "../../../../app/js/RedisBackend.js"
  5. SandboxedModule = require('sandboxed-module')
  6. RedisKeyBuilder = require "../../../../app/js/RedisKeyBuilder"
  7. describe "RedisBackend", ->
  8. beforeEach ->
  9. @Settings =
  10. redis:
  11. documentupdater: [{
  12. primary: true
  13. port: "6379"
  14. host: "localhost"
  15. password: "single-password"
  16. key_schema:
  17. blockingKey: ({doc_id}) -> "Blocking:#{doc_id}"
  18. docLines: ({doc_id}) -> "doclines:#{doc_id}"
  19. docOps: ({doc_id}) -> "DocOps:#{doc_id}"
  20. docVersion: ({doc_id}) -> "DocVersion:#{doc_id}"
  21. docHash: ({doc_id}) -> "DocHash:#{doc_id}"
  22. projectKey: ({doc_id}) -> "ProjectId:#{doc_id}"
  23. pendingUpdates: ({doc_id}) -> "PendingUpdates:#{doc_id}"
  24. docsInProject: ({project_id}) -> "DocsIn:#{project_id}"
  25. }, {
  26. cluster: [{
  27. port: "7000"
  28. host: "localhost"
  29. }]
  30. password: "cluster-password"
  31. key_schema:
  32. blockingKey: ({doc_id}) -> "Blocking:{#{doc_id}}"
  33. docLines: ({doc_id}) -> "doclines:{#{doc_id}}"
  34. docOps: ({doc_id}) -> "DocOps:{#{doc_id}}"
  35. docVersion: ({doc_id}) -> "DocVersion:{#{doc_id}}"
  36. docHash: ({doc_id}) -> "DocHash:{#{doc_id}}"
  37. projectKey: ({doc_id}) -> "ProjectId:{#{doc_id}}"
  38. pendingUpdates: ({doc_id}) -> "PendingUpdates:{#{doc_id}}"
  39. docsInProject: ({project_id}) -> "DocsIn:{#{project_id}}"
  40. }]
  41. test_context = @
  42. class Cluster
  43. constructor: (@config) ->
  44. test_context.rclient_ioredis = @
  45. nodes: sinon.stub()
  46. @timer = timer = sinon.stub()
  47. class Timer
  48. constructor: (args...) -> timer(args...)
  49. done: () ->
  50. @RedisBackend = SandboxedModule.require modulePath, requires:
  51. "settings-sharelatex": @Settings
  52. "logger-sharelatex": @logger = { error: sinon.stub(), log: sinon.stub(), warn: sinon.stub() }
  53. "redis-sharelatex": @redis =
  54. createClient: sinon.stub().returns @rclient_redis = {}
  55. activeHealthCheck: sinon.stub()
  56. "ioredis": @ioredis =
  57. Cluster: Cluster
  58. "metrics-sharelatex":
  59. @Metrics =
  60. inc: sinon.stub()
  61. Timer: Timer
  62. @client = @RedisBackend.createClient()
  63. @doc_id = "mock-doc-id"
  64. @project_id = "mock-project-id"
  65. it "should create a redis client", ->
  66. @redis.createClient
  67. .calledWith({
  68. port: "6379"
  69. host: "localhost"
  70. password: "single-password"
  71. })
  72. .should.equal true
  73. it "should create an ioredis cluster client", ->
  74. @rclient_ioredis.config.should.deep.equal [{
  75. port: "7000"
  76. host: "localhost"
  77. }]
  78. describe "individual commands", ->
  79. describe "with the same results", ->
  80. beforeEach (done) ->
  81. @content = "bar"
  82. @rclient_redis.get = sinon.stub()
  83. @rclient_redis.get.withArgs("doclines:#{@doc_id}").yields(null, @content)
  84. @rclient_ioredis.get = sinon.stub()
  85. @rclient_ioredis.get.withArgs("doclines:{#{@doc_id}}").yields(null, @content)
  86. @client.get RedisKeyBuilder.docLines({doc_id: @doc_id}), (error, @result) =>
  87. setTimeout () -> # Let all background requests complete
  88. done(error)
  89. it "should return the result", ->
  90. @result.should.equal @content
  91. it "should have called the redis client with the appropriate key", ->
  92. @rclient_redis.get
  93. .calledWith("doclines:#{@doc_id}")
  94. .should.equal true
  95. it "should have called the ioredis cluster client with the appropriate key", ->
  96. @rclient_ioredis.get
  97. .calledWith("doclines:{#{@doc_id}}")
  98. .should.equal true
  99. it "should send a metric", ->
  100. @Metrics.inc
  101. .calledWith("backend-match")
  102. .should.equal true
  103. it "should time the commands", ->
  104. @timer
  105. .calledWith("redis.ioredis.get")
  106. .should.equal true
  107. @timer
  108. .calledWith("redis.noderedis.get")
  109. .should.equal true
  110. describe "with different results", ->
  111. beforeEach (done) ->
  112. @rclient_redis.get = sinon.stub()
  113. @rclient_redis.get.withArgs("doclines:#{@doc_id}").yields(null, "primary-result")
  114. @rclient_ioredis.get = sinon.stub()
  115. @rclient_ioredis.get.withArgs("doclines:{#{@doc_id}}").yields(null, "secondary-result")
  116. @client.get RedisKeyBuilder.docLines({doc_id: @doc_id}), (error, @result) =>
  117. setTimeout () -> # Let all background requests complete
  118. done(error)
  119. it "should return the primary result", ->
  120. @result.should.equal "primary-result"
  121. it "should send a metric", ->
  122. @Metrics.inc
  123. .calledWith("backend-conflict")
  124. .should.equal true
  125. describe "with differently ordered results from smembers", ->
  126. beforeEach (done) ->
  127. @rclient_redis.smembers = sinon.stub()
  128. @rclient_redis.smembers.withArgs("DocsIn:#{@project_id}").yields(null, ["one", "two"])
  129. @rclient_ioredis.smembers = sinon.stub()
  130. @rclient_ioredis.smembers.withArgs("DocsIn:{#{@project_id}}").yields(null, ["two", "one"])
  131. @client.smembers RedisKeyBuilder.docsInProject({project_id: @project_id}), (error, @result) =>
  132. setTimeout () -> # Let all background requests complete
  133. done(error)
  134. it "should return the primary result", ->
  135. @result.should.deep.equal ["one", "two"]
  136. it "should send a metric indicating a match", ->
  137. @Metrics.inc
  138. .calledWith("backend-match")
  139. .should.equal true
  140. describe "when the secondary errors", ->
  141. beforeEach (done) ->
  142. @rclient_redis.get = sinon.stub()
  143. @rclient_redis.get.withArgs("doclines:#{@doc_id}").yields(null, "primary-result")
  144. @rclient_ioredis.get = sinon.stub()
  145. @rclient_ioredis.get.withArgs("doclines:{#{@doc_id}}").yields(@error = new Error("oops"))
  146. @client.get RedisKeyBuilder.docLines({doc_id: @doc_id}), (error, @result) =>
  147. setTimeout () -> # Let all background requests complete
  148. done(error)
  149. it "should return the primary result", ->
  150. @result.should.equal "primary-result"
  151. it "should log out the secondary error", ->
  152. @logger.error
  153. .calledWith({
  154. err: @error
  155. }, "error in redis backend")
  156. .should.equal true
  157. describe "when the primary errors", ->
  158. beforeEach (done) ->
  159. @rclient_redis.get = sinon.stub()
  160. @rclient_redis.get.withArgs("doclines:#{@doc_id}").yields(@error = new Error("oops"))
  161. @rclient_ioredis.get = sinon.stub()
  162. @rclient_ioredis.get.withArgs("doclines:{#{@doc_id}}").yields(null, "secondary-result")
  163. @client.get RedisKeyBuilder.docLines({doc_id: @doc_id}), (@returned_error, @result) =>
  164. setTimeout () -> # Let all background requests complete
  165. done()
  166. it "should return the error", ->
  167. @returned_error.should.equal @error
  168. it "should log out the error", ->
  169. @logger.error
  170. .calledWith({
  171. err: @error
  172. }, "error in redis backend")
  173. .should.equal true
  174. describe "when the command has the key in a non-zero argument index", ->
  175. beforeEach (done) ->
  176. @script = "mock-script"
  177. @key_count = 1
  178. @value = "mock-value"
  179. @rclient_redis.eval = sinon.stub()
  180. @rclient_redis.eval.withArgs(@script, @key_count, "Blocking:#{@doc_id}", @value).yields(null)
  181. @rclient_ioredis.eval = sinon.stub()
  182. @rclient_ioredis.eval.withArgs(@script, @key_count, "Blocking:{#{@doc_id}}", @value).yields(null, @content)
  183. @client.eval @script, @key_count, RedisKeyBuilder.blockingKey({doc_id: @doc_id}), @value, (error) =>
  184. setTimeout () -> # Let all background requests complete
  185. done(error)
  186. it "should have called the redis client with the appropriate key", ->
  187. @rclient_redis.eval
  188. .calledWith(@script, @key_count, "Blocking:#{@doc_id}", @value)
  189. .should.equal true
  190. it "should have called the ioredis cluster client with the appropriate key", ->
  191. @rclient_ioredis.eval
  192. .calledWith(@script, @key_count, "Blocking:{#{@doc_id}}", @value)
  193. .should.equal true
  194. describe "when the secondary takes longer than SECONDARY_TIMEOUT", ->
  195. beforeEach (done) ->
  196. @client.SECONDARY_TIMEOUT = 10
  197. @content = "bar"
  198. @rclient_redis.get = (key, cb) =>
  199. key.should.equal "doclines:#{@doc_id}"
  200. setTimeout () =>
  201. cb(null, @content)
  202. , @client.SECONDARY_TIMEOUT * 3 # If the secondary errors first, don't affect the primary result
  203. @rclient_ioredis.get = (key, cb) =>
  204. key.should.equal "doclines:{#{@doc_id}}"
  205. setTimeout () =>
  206. cb(null, @content)
  207. , @client.SECONDARY_TIMEOUT * 2
  208. @client.get RedisKeyBuilder.docLines({doc_id: @doc_id}), (error, @result) =>
  209. done(error)
  210. it "should log out an error for the backend", ->
  211. @logger.error
  212. .calledWith({err: new Error("backend timed out")}, "backend timed out")
  213. .should.equal true
  214. it "should return the primary result", ->
  215. @result.should.equal @content
  216. describe "when the primary takes longer than SECONDARY_TIMEOUT", ->
  217. beforeEach (done) ->
  218. @client.SECONDARY_TIMEOUT = 10
  219. @content = "bar"
  220. @rclient_ioredis.get = sinon.stub()
  221. @rclient_ioredis.get.withArgs("doclines:{#{@doc_id}}").yields(null, @content)
  222. @rclient_redis.get = (key, cb) =>
  223. key.should.equal "doclines:#{@doc_id}"
  224. setTimeout () =>
  225. cb(null, @content)
  226. , @client.SECONDARY_TIMEOUT * 2
  227. @client.get RedisKeyBuilder.docLines({doc_id: @doc_id}), (error, @result) =>
  228. done(error)
  229. it "should not consider this an error", ->
  230. @logger.error
  231. .called
  232. .should.equal false
  233. describe "multi commands", ->
  234. beforeEach ->
  235. # We will test with:
  236. # rclient.multi()
  237. # .get("doclines:foo")
  238. # .get("DocVersion:foo")
  239. # .exec (...) ->
  240. @doclines = "mock-doclines"
  241. @version = "42"
  242. @rclient_redis.multi = sinon.stub().returns @rclient_redis
  243. @rclient_ioredis.multi = sinon.stub().returns @rclient_ioredis
  244. describe "with the same results", ->
  245. beforeEach (done) ->
  246. @rclient_redis.get = sinon.stub()
  247. @rclient_redis.exec = sinon.stub().yields(null, [@doclines, @version])
  248. @rclient_ioredis.get = sinon.stub()
  249. @rclient_ioredis.exec = sinon.stub().yields(null, [ [null, @doclines], [null, @version] ])
  250. multi = @client.multi()
  251. multi.get RedisKeyBuilder.docLines({doc_id: @doc_id})
  252. multi.get RedisKeyBuilder.docVersion({doc_id: @doc_id})
  253. multi.exec (error, @result) =>
  254. setTimeout () ->
  255. done(error)
  256. it "should return the result", ->
  257. @result.should.deep.equal [@doclines, @version]
  258. it "should have called the redis client with the appropriate keys", ->
  259. @rclient_redis.get
  260. .calledWith("doclines:#{@doc_id}")
  261. .should.equal true
  262. @rclient_redis.get
  263. .calledWith("DocVersion:#{@doc_id}")
  264. .should.equal true
  265. @rclient_ioredis.exec
  266. .called
  267. .should.equal true
  268. it "should have called the ioredis cluster client with the appropriate keys", ->
  269. @rclient_ioredis.get
  270. .calledWith("doclines:{#{@doc_id}}")
  271. .should.equal true
  272. @rclient_ioredis.get
  273. .calledWith("DocVersion:{#{@doc_id}}")
  274. .should.equal true
  275. @rclient_ioredis.exec
  276. .called
  277. .should.equal true
  278. it "should send a metric", ->
  279. @Metrics.inc
  280. .calledWith("backend-match")
  281. .should.equal true
  282. it "should time the exec", ->
  283. @timer
  284. .calledWith("redis.ioredis.exec")
  285. .should.equal true
  286. @timer
  287. .calledWith("redis.noderedis.exec")
  288. .should.equal true
  289. describe "with different results", ->
  290. beforeEach (done) ->
  291. @rclient_redis.get = sinon.stub()
  292. @rclient_redis.exec = sinon.stub().yields(null, [@doclines, @version])
  293. @rclient_ioredis.get = sinon.stub()
  294. @rclient_ioredis.exec = sinon.stub().yields(null, [ [null, "different-doc-lines"], [null, @version] ])
  295. multi = @client.multi()
  296. multi.get RedisKeyBuilder.docLines({doc_id: @doc_id})
  297. multi.get RedisKeyBuilder.docVersion({doc_id: @doc_id})
  298. multi.exec (error, @result) =>
  299. setTimeout () ->
  300. done(error)
  301. it "should return the primary result", ->
  302. @result.should.deep.equal [@doclines, @version]
  303. it "should send a metric", ->
  304. @Metrics.inc
  305. .calledWith("backend-conflict")
  306. .should.equal true
  307. describe "when the secondary errors", ->
  308. beforeEach (done) ->
  309. @rclient_redis.get = sinon.stub()
  310. @rclient_redis.exec = sinon.stub().yields(null, [@doclines, @version])
  311. @rclient_ioredis.get = sinon.stub()
  312. @rclient_ioredis.exec = sinon.stub().yields(@error = new Error("oops"))
  313. multi = @client.multi()
  314. multi.get RedisKeyBuilder.docLines({doc_id: @doc_id})
  315. multi.get RedisKeyBuilder.docVersion({doc_id: @doc_id})
  316. multi.exec (error, @result) =>
  317. setTimeout () ->
  318. done(error)
  319. it "should return the primary result", ->
  320. @result.should.deep.equal [@doclines, @version]
  321. it "should log out the secondary error", ->
  322. @logger.error
  323. .calledWith({
  324. err: @error
  325. }, "error in redis backend")
  326. .should.equal true
  327. describe "when the secondary errors", ->
  328. beforeEach (done) ->
  329. @rclient_redis.get = sinon.stub()
  330. @rclient_redis.exec = sinon.stub().yields(@error = new Error("oops"))
  331. @rclient_ioredis.get = sinon.stub()
  332. @rclient_ioredis.exec = sinon.stub().yields([ [null, @doclines], [null, @version] ])
  333. multi = @client.multi()
  334. multi.get RedisKeyBuilder.docLines({doc_id: @doc_id})
  335. multi.get RedisKeyBuilder.docVersion({doc_id: @doc_id})
  336. multi.exec (@returned_error) =>
  337. setTimeout () -> done()
  338. it "should return the error", ->
  339. @returned_error.should.equal @error
  340. it "should log out the error", ->
  341. @logger.error
  342. .calledWith({
  343. err: @error
  344. }, "error in redis backend")
  345. .should.equal true
  346. describe "when the secondary takes longer than SECONDARY_TIMEOUT", ->
  347. beforeEach (done) ->
  348. @rclient_redis.get = sinon.stub()
  349. @rclient_redis.exec = (cb) =>
  350. setTimeout () =>
  351. cb(null, [@doclines, @version])
  352. , 30 # If secondary errors first, don't affect the primary result
  353. @rclient_ioredis.get = sinon.stub()
  354. @rclient_ioredis.exec = (cb) =>
  355. setTimeout () =>
  356. cb(null, [ [null, @doclines], [null, @version] ])
  357. , 20
  358. multi = @client.multi()
  359. multi.SECONDARY_TIMEOUT = 10
  360. multi.get RedisKeyBuilder.docLines({doc_id: @doc_id})
  361. multi.get RedisKeyBuilder.docVersion({doc_id: @doc_id})
  362. multi.exec (error, @result) =>
  363. done(error)
  364. it "should log out an error for the backend", ->
  365. @logger.error
  366. .calledWith({err: new Error("backend timed out")}, "backend timed out")
  367. .should.equal true
  368. it "should return the primary result", ->
  369. @result.should.deep.equal [@doclines, @version]
  370. describe "when the primary takes longer than SECONDARY_TIMEOUT", ->
  371. beforeEach (done) ->
  372. @rclient_redis.get = sinon.stub()
  373. @rclient_redis.exec = (cb) =>
  374. setTimeout () =>
  375. cb(null, [@doclines, @version])
  376. , 20
  377. @rclient_ioredis.get = sinon.stub()
  378. @rclient_ioredis.exec = sinon.stub().yields(null, [ [null, @doclines], [null, @version] ])
  379. multi = @client.multi()
  380. multi.SECONDARY_TIMEOUT = 10
  381. multi.get RedisKeyBuilder.docLines({doc_id: @doc_id})
  382. multi.get RedisKeyBuilder.docVersion({doc_id: @doc_id})
  383. multi.exec (error, @result) =>
  384. done(error)
  385. it "should not consider this an error", ->
  386. @logger.error
  387. .called
  388. .should.equal false
  389. describe "_healthCheckNodeRedisClient", ->
  390. beforeEach ->
  391. @redis.activeHealthCheckRedis = sinon.stub().returns @healthCheck = {
  392. isAlive: sinon.stub()
  393. }
  394. describe "successfully", ->
  395. beforeEach (done) ->
  396. @healthCheck.isAlive.returns true
  397. @redis_client = {}
  398. @client._healthCheckNodeRedisClient(@redis_client, done)
  399. it "should check the status of the node redis client", ->
  400. @healthCheck.isAlive.called.should.equal true
  401. it "should only create one health check when called multiple times", (done) ->
  402. @client._healthCheckNodeRedisClient @redis_client, () =>
  403. @redis.activeHealthCheckRedis.calledOnce.should.equal true
  404. @healthCheck.isAlive.calledTwice.should.equal true
  405. done()
  406. describe "when failing", ->
  407. beforeEach ->
  408. @healthCheck.isAlive.returns false
  409. @redis_client = {}
  410. it "should return an error", (done) ->
  411. @client._healthCheckNodeRedisClient @redis_client, (error) ->
  412. error.message.should.equal "node-redis client failed health check"
  413. done()
  414. describe "_healthCheckClusterClient", ->
  415. beforeEach ->
  416. @client.HEARTBEAT_TIMEOUT = 10
  417. @nodes = [{
  418. options: key: "node-0"
  419. stream: destroy: sinon.stub()
  420. }, {
  421. options: key: "node-1"
  422. stream: destroy: sinon.stub()
  423. }]
  424. @rclient_ioredis.nodes = sinon.stub().returns(@nodes)
  425. describe "when both clients are successful", ->
  426. beforeEach (done) ->
  427. @nodes[0].ping = sinon.stub().yields()
  428. @nodes[1].ping = sinon.stub().yields()
  429. @client._healthCheckClusterClient({ rclient: @rclient_ioredis }, done)
  430. it "should get all cluster nodes", ->
  431. @rclient_ioredis.nodes
  432. .calledWith("all")
  433. .should.equal true
  434. it "should ping each cluster node", ->
  435. for node in @nodes
  436. node.ping.called.should.equal true
  437. describe "when ping fails to a node", ->
  438. beforeEach ->
  439. @nodes[0].ping = (cb) -> cb()
  440. @nodes[1].ping = (cb) -> # Just hang
  441. it "should return an error", (done) ->
  442. @client._healthCheckClusterClient { rclient: @rclient_ioredis }, (error) ->
  443. error.message.should.equal "ioredis node ping check timed out"
  444. done()