ZipManager.coffee 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. Settings = require('settings-sharelatex')
  2. logger = require('logger-sharelatex')
  3. metrics = require('./Metrics')
  4. zlib = require('zlib')
  5. # Compress and uncompress data sent to Redis using the node 'zlib'
  6. # module, to reduce load on Redis.
  7. #
  8. # Measurements show that most of the load on Redis comes from a very
  9. # large documents. We can shift some of that CPU load from redis to
  10. # the docupdaters (which are scalable) by compressing the data in the
  11. # docupdater first.
  12. #
  13. # To avoid overloading the docupdater clients we impose a minimum size
  14. # on data we will compress, so we only catch the large ones.
  15. #
  16. # The optimimum size for the cutoff is about 10K, below this we do
  17. # more work but don't really gain any extra reduction in Redis CPU
  18. #
  19. # |--------------------+-----------+--------------------------|
  20. # | Compression cutoff | Redis CPU | Extra doc updater CPU(*) |
  21. # |--------------------+-----------+--------------------------|
  22. # | N/A | 100% | 0% |
  23. # | 100k | 80% | 10% |
  24. # | 10k | 50% | 30% |
  25. # |--------------------+-----------+--------------------------|
  26. #
  27. # (*) percentage of a single core, because node zlib runs in multiple
  28. # threads.
  29. ZIP_WRITES_ENABLED = Settings.redis.zip?.writesEnabled
  30. ZIP_MINSIZE = Settings.redis.zip?.minSize || 64*1024
  31. module.exports = ZipManager =
  32. uncompressIfNeeded: (doc_id, result, callback) ->
  33. # result is an array of [text, version]. Each entry is a node
  34. # Buffer object which we need to convert to strings on output
  35. # first make sure the version (result[1]) is returned as a string
  36. if result?[1]?.toString?
  37. result[1] = result[1].toString()
  38. # now uncompress the text (result[0]) if needed
  39. buf = result?[0]
  40. # Check if we have a GZIP file (magic numbers in header)
  41. if buf? and buf[0] == 0x1F and buf[1] == 0x8B
  42. zlib.gunzip buf, (err, newbuf) ->
  43. if err?
  44. logger.err doc_id:doc_id, err:err, "error uncompressing doc"
  45. callback(err, null)
  46. else
  47. logger.log doc_id:doc_id, fromBytes: buf.length, toChars: newbuf.length, factor: buf.length/newbuf.length, "uncompressed successfully"
  48. result[0] = newbuf.toString()
  49. callback(null, result)
  50. else
  51. # if we don't have a GZIP file it's just a buffer of text, convert it back to a string
  52. if buf?.toString?
  53. result[0] = buf.toString()
  54. callback(null, result)
  55. compressIfNeeded: (doc_id, text, callback) ->
  56. if ZIP_WRITES_ENABLED and ZIP_MINSIZE > 1024 and text.length > ZIP_MINSIZE
  57. # N.B. skip files of 1k or less, because gzip increases the size
  58. zlib.gzip text, (err, buf) ->
  59. if err?
  60. logger.err doc_id:doc_id, err:err, "error compressing doc"
  61. callback(err, null)
  62. else
  63. logger.log doc_id:doc_id, fromChars: text.length, toBytes: buf.length, factor: buf.length/text.length , "compressed successfully"
  64. callback(null, buf)
  65. else
  66. callback(null, text)