MigrationPersistor.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. const AbstractPersistor = require('./AbstractPersistor')
  2. const Logger = require('logger-sharelatex')
  3. const Stream = require('stream')
  4. const { promisify } = require('util')
  5. const { NotFoundError, WriteError } = require('./Errors')
  6. const pipeline = promisify(Stream.pipeline)
  7. // Persistor that wraps two other persistors. Talks to the 'primary' by default,
  8. // but will fall back to an older persistor in the case of a not-found error.
  9. // If `Settings.fallback.copyOnMiss` is set, this will copy files from the fallback
  10. // to the primary, in the event that they are missing.
  11. //
  12. // It is unlikely that the bucket/location name will be the same on the fallback
  13. // as the primary. The bucket names should be overridden in `Settings.fallback.buckets`
  14. // e.g.
  15. // Settings.fallback.buckets = {
  16. // myBucketOnS3: 'myBucketOnGCS'
  17. // }
  18. module.exports = class MigrationPersistor extends AbstractPersistor {
  19. constructor(primaryPersistor, fallbackPersistor, settings) {
  20. super()
  21. this.primaryPersistor = primaryPersistor
  22. this.fallbackPersistor = fallbackPersistor
  23. this.settings = settings
  24. }
  25. async sendFile(...args) {
  26. return this.primaryPersistor.sendFile(...args)
  27. }
  28. async sendStream(...args) {
  29. return this.primaryPersistor.sendStream(...args)
  30. }
  31. async getRedirectUrl(...args) {
  32. return this.primaryPersistor.getRedirectUrl(...args)
  33. }
  34. async getObjectMd5Hash(...args) {
  35. return this._runWithFallback('getObjectMd5Hash', ...args)
  36. }
  37. async checkIfObjectExists(...args) {
  38. return this._runWithFallback('checkIfObjectExists', ...args)
  39. }
  40. async getObjectSize(...args) {
  41. return this._runWithFallback('getObjectSize', ...args)
  42. }
  43. async directorySize(...args) {
  44. return this._runWithFallback('directorySize', ...args)
  45. }
  46. async deleteObject(...args) {
  47. return this._runOnBoth('deleteObject', ...args)
  48. }
  49. async deleteDirectory(...args) {
  50. return this._runOnBoth('deleteDirectory', ...args)
  51. }
  52. async getObjectStream(bucket, key, opts = {}) {
  53. const shouldCopy = this.settings.copyOnMiss && !opts.start && !opts.end
  54. try {
  55. // 'return await' so we catch NotFoundError before returning
  56. return await this.primaryPersistor.getObjectStream(bucket, key, opts)
  57. } catch (err) {
  58. if (err instanceof NotFoundError) {
  59. const fallbackBucket = this._getFallbackBucket(bucket)
  60. const fallbackStream = await this.fallbackPersistor.getObjectStream(
  61. fallbackBucket,
  62. key,
  63. opts
  64. )
  65. // tee the stream to the client, and as a copy to the primary (if necessary)
  66. // start listening on both straight away so that we don't consume bytes
  67. // in one place before the other
  68. const returnStream = new Stream.PassThrough()
  69. pipeline(fallbackStream, returnStream).catch((error) => {
  70. Logger.warn({ error }, 'failed to copy object from fallback')
  71. })
  72. if (shouldCopy) {
  73. const copyStream = new Stream.PassThrough()
  74. pipeline(fallbackStream, copyStream).catch((error) => {
  75. Logger.warn({ error }, 'failed to copy object from fallback')
  76. })
  77. this._copyStreamFromFallbackAndVerify(
  78. copyStream,
  79. fallbackBucket,
  80. bucket,
  81. key,
  82. key
  83. ).catch((error) => {
  84. Logger.warn({ error }, 'failed to copy file from fallback')
  85. })
  86. }
  87. return returnStream
  88. }
  89. throw err
  90. }
  91. }
  92. async copyObject(bucket, sourceKey, destKey) {
  93. try {
  94. return await this.primaryPersistor.copyObject(bucket, sourceKey, destKey)
  95. } catch (err) {
  96. if (err instanceof NotFoundError) {
  97. const fallbackBucket = this._getFallbackBucket(bucket)
  98. const fallbackStream = await this.fallbackPersistor.getObjectStream(
  99. fallbackBucket,
  100. sourceKey,
  101. {}
  102. )
  103. const copyStream = new Stream.PassThrough()
  104. pipeline(fallbackStream, copyStream).catch((error) => {
  105. Logger.warn({ error }, 'failed to copy object from fallback')
  106. })
  107. if (this.settings.copyOnMiss) {
  108. const missStream = new Stream.PassThrough()
  109. pipeline(fallbackStream, missStream).catch((error) => {
  110. Logger.warn({ error }, 'failed to copy object from fallback')
  111. })
  112. // copy from sourceKey -> sourceKey
  113. this._copyStreamFromFallbackAndVerify(
  114. missStream,
  115. fallbackBucket,
  116. bucket,
  117. sourceKey,
  118. sourceKey
  119. ).catch(() => {
  120. // swallow errors, as this runs in the background and will log a warning
  121. })
  122. }
  123. // copy from sourceKey -> destKey
  124. return this._copyStreamFromFallbackAndVerify(
  125. copyStream,
  126. fallbackBucket,
  127. bucket,
  128. sourceKey,
  129. destKey
  130. )
  131. }
  132. throw err
  133. }
  134. }
  135. async _copyStreamFromFallbackAndVerify(
  136. stream,
  137. sourceBucket,
  138. destBucket,
  139. sourceKey,
  140. destKey
  141. ) {
  142. try {
  143. let sourceMd5
  144. try {
  145. sourceMd5 = await this.fallbackPersistor.getObjectMd5Hash(
  146. sourceBucket,
  147. sourceKey
  148. )
  149. } catch (err) {
  150. Logger.warn(err, 'error getting md5 hash from fallback persistor')
  151. }
  152. await this.primaryPersistor.sendStream(destBucket, destKey, stream, {
  153. sourceMd5
  154. })
  155. } catch (err) {
  156. const error = new WriteError(
  157. 'unable to copy file to destination persistor',
  158. {
  159. sourceBucket,
  160. destBucket,
  161. sourceKey,
  162. destKey
  163. },
  164. err
  165. )
  166. if (this.settings.Metrics) {
  167. this.settings.Metrics.inc('fallback.copy.failure')
  168. }
  169. try {
  170. await this.primaryPersistor.deleteObject(destBucket, destKey)
  171. } catch (err) {
  172. error.info.cleanupError = new WriteError(
  173. 'unable to clean up destination copy artifact',
  174. {
  175. destBucket,
  176. destKey
  177. },
  178. err
  179. )
  180. }
  181. throw error
  182. }
  183. }
  184. _getFallbackBucket(bucket) {
  185. return (this.settings.buckets && this.settings.buckets[bucket]) || bucket
  186. }
  187. async _runOnBoth(methodName, bucket, ...moreArgs) {
  188. const fallbackBucket = this._getFallbackBucket(bucket)
  189. await Promise.all([
  190. this.primaryPersistor[methodName](bucket, ...moreArgs),
  191. this.fallbackPersistor[methodName](fallbackBucket, ...moreArgs)
  192. ])
  193. }
  194. async _runWithFallback(methodName, bucket, key, ...moreArgs) {
  195. try {
  196. // 'return await' so we catch NotFoundError before returning
  197. return await this.primaryPersistor[methodName](bucket, key, ...moreArgs)
  198. } catch (err) {
  199. if (err instanceof NotFoundError) {
  200. const fallbackBucket = this._getFallbackBucket(bucket)
  201. if (this.settings.copyOnMiss) {
  202. const fallbackStream = await this.fallbackPersistor.getObjectStream(
  203. fallbackBucket,
  204. key,
  205. {}
  206. )
  207. // run in background
  208. this._copyStreamFromFallbackAndVerify(
  209. fallbackStream,
  210. fallbackBucket,
  211. bucket,
  212. key,
  213. key
  214. ).catch((err) => {
  215. Logger.warn({ err }, 'failed to copy file from fallback')
  216. })
  217. }
  218. return this.fallbackPersistor[methodName](
  219. fallbackBucket,
  220. key,
  221. ...moreArgs
  222. )
  223. }
  224. throw err
  225. }
  226. }
  227. }