MigrationPersistor.js 7.3 KB

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