pdf-caching-transport.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. import OError from '@overleaf/o-error'
  2. import { fallbackRequest, fetchRange, preprocessFileOnce } from './pdf-caching'
  3. import { captureException } from '@/infrastructure/error-reporter'
  4. import { EDITOR_SESSION_ID, getPdfCachingMetrics } from './metrics'
  5. import {
  6. cachedUrlLookupEnabled,
  7. enablePdfCaching,
  8. prefetchingEnabled,
  9. prefetchLargeEnabled,
  10. trackPdfDownloadEnabled,
  11. canUseClsiCache,
  12. } from './pdf-caching-flags'
  13. import { isNetworkError } from '@/utils/is-network-error'
  14. import { debugConsole } from '@/utils/debugging'
  15. import { PDFJS } from './pdf-js'
  16. import { PDFFile, PDFRange, ProcessedPDFFile } from '@ol-types/compile'
  17. import { PdfCachingMetricsFull } from './types'
  18. // 30 seconds: The shutdown grace period of a clsi pre-emp instance.
  19. const STALE_OUTPUT_REQUEST_THRESHOLD_MS = 30 * 1000
  20. export function generatePdfCachingTransportFactory() {
  21. // NOTE: The custom transport can be used for tracking download volume.
  22. if (!enablePdfCaching && !trackPdfDownloadEnabled) {
  23. return () => undefined
  24. }
  25. const usageScore = new Map()
  26. const cachedUrls = new Map()
  27. const metrics: PdfCachingMetricsFull = Object.assign(getPdfCachingMetrics(), {
  28. failedCount: 0,
  29. failedOnce: false,
  30. tooMuchBandwidthCount: 0,
  31. tooManyRequestsCount: 0,
  32. cachedCount: 0,
  33. cachedBytes: 0,
  34. fetchedCount: 0,
  35. fetchedBytes: 0,
  36. latencyComputeMax: 0,
  37. latencyComputeTotal: 0,
  38. requestedCount: 0,
  39. requestedBytes: 0,
  40. oldUrlHitCount: 0,
  41. oldUrlMissCount: 0,
  42. enablePdfCaching,
  43. prefetchingEnabled,
  44. prefetchLargeEnabled,
  45. cachedUrlLookupEnabled,
  46. })
  47. const verifyChunks =
  48. new URLSearchParams(window.location.search).get('verify_chunks') === 'true'
  49. class PDFDataRangeTransport extends PDFJS.PDFDataRangeTransport {
  50. url: string
  51. pdfFile: ProcessedPDFFile
  52. abortController: AbortController
  53. leanPdfRanges: PDFRange[]
  54. handleFetchError: (error: any) => void
  55. startTime: number
  56. queryForChunks: string
  57. constructor({
  58. url,
  59. pdfFile,
  60. abortController,
  61. handleFetchError,
  62. }: {
  63. url: string
  64. pdfFile: PDFFile
  65. abortController: AbortController
  66. handleFetchError: (error: any) => void
  67. }) {
  68. super(pdfFile.size, new Uint8Array())
  69. this.url = url
  70. pdfFile.ranges = pdfFile.ranges || []
  71. pdfFile.editorId = pdfFile.editorId || EDITOR_SESSION_ID
  72. preprocessFileOnce({
  73. file: pdfFile,
  74. usageScore,
  75. cachedUrls,
  76. })
  77. // We can safely cast as preprocessFileOnce mutates the file object into a ProcessedPDFFile
  78. this.pdfFile = pdfFile as unknown as ProcessedPDFFile
  79. // Clone the chunks as the objectId field is encoded to a Uint8Array.
  80. this.leanPdfRanges = pdfFile.ranges.map(r => Object.assign({}, r))
  81. this.handleFetchError = handleFetchError
  82. this.abortController = abortController
  83. this.startTime = performance.now()
  84. const params = new URL(url).searchParams
  85. // drop no needed params
  86. params.delete('enable_pdf_caching')
  87. params.delete('verify_chunks')
  88. this.queryForChunks = params.toString()
  89. }
  90. abort() {
  91. this.abortController.abort()
  92. }
  93. requestDataRange(start: number, end: number) {
  94. const abortSignal = this.abortController.signal
  95. const getDebugInfo = () => ({
  96. // Sentry does not serialize objects in twice nested objects.
  97. // Move the ranges to the root level to see them in Sentry.
  98. pdfRanges: this.leanPdfRanges,
  99. pdfFile: Object.assign({}, this.pdfFile, {
  100. ranges: '[extracted]',
  101. // Hide prefetched chunks as these include binary blobs.
  102. prefetched: this.pdfFile.prefetched?.length,
  103. }),
  104. pdfUrl: this.url,
  105. start,
  106. end,
  107. metrics,
  108. })
  109. const isStaleOutputRequest = () =>
  110. performance.now() - this.startTime > STALE_OUTPUT_REQUEST_THRESHOLD_MS
  111. const is404 = (err: any) =>
  112. (OError.getFullInfo(err) as { statusCode: number }).statusCode === 404
  113. const isFromOutputPDFRequest = (err: any) =>
  114. (OError.getFullInfo(err) as { url?: string }).url?.includes?.(
  115. '/output.pdf'
  116. ) === true
  117. // Do not consider "expected 404s" and network errors as pdf caching
  118. // failures.
  119. // "expected 404s" here include:
  120. // - any stale download request
  121. // Example: The user returns to a browser tab after 1h and scrolls.
  122. // - requests for the main output.pdf file
  123. // A fallback request would not be able to retrieve the PDF either.
  124. const isExpectedError = (err: any) =>
  125. (is404(err) || isNetworkError(err)) &&
  126. (isStaleOutputRequest() || isFromOutputPDFRequest(err))
  127. const usesCache = (url: string) => {
  128. if (!url) return false
  129. const u = new URL(url)
  130. return (
  131. u.pathname.endsWith(
  132. `build/${this.pdfFile.editorId}-${this.pdfFile.build}/output/output.pdf`
  133. ) &&
  134. (u.searchParams.get('clsiserverid') === 'cache' ||
  135. u.searchParams.get('clsiserverid')?.startsWith('clsi-cache-'))
  136. )
  137. }
  138. const canTryFromCache = (err: any) => {
  139. if (!canUseClsiCache) return false
  140. if (!is404(err)) return false
  141. return !usesCache((OError.getFullInfo(err) as { url: string }).url)
  142. }
  143. const getOutputPDFURLFromCache = () => {
  144. if (usesCache(this.url)) return this.url
  145. const u = new URL(this.url)
  146. u.searchParams.set('clsiserverid', this.pdfFile.clsiCacheShard)
  147. u.pathname = u.pathname.replace(
  148. /build\/[a-f0-9-]+\//,
  149. `build/${this.pdfFile.editorId}-${this.pdfFile.build}/`
  150. )
  151. return u.href
  152. }
  153. const fetchFromCache = async () => {
  154. // Try fetching the chunk from clsi-cache
  155. const url = getOutputPDFURLFromCache()
  156. return fallbackRequest({
  157. file: this.pdfFile,
  158. url,
  159. start,
  160. end,
  161. abortSignal,
  162. })
  163. .then(blob => {
  164. // Send the next output.pdf request directly to the cache.
  165. this.url = url
  166. // Only try downloading chunks that were cached previously
  167. this.pdfFile.ranges = this.pdfFile.ranges.filter(r =>
  168. cachedUrls.has(r.hash)
  169. )
  170. return blob
  171. })
  172. .catch(err => {
  173. const { statusCode, url } = OError.getFullInfo(err) as {
  174. statusCode: number
  175. url: string
  176. }
  177. throw OError.tag(
  178. new PDFJS.ResponseException(undefined, statusCode, true),
  179. 'cache-fallback',
  180. { statusCode, url, err }
  181. )
  182. })
  183. }
  184. fetchRange({
  185. url: this.url,
  186. start,
  187. end,
  188. file: this.pdfFile,
  189. queryForChunks: this.queryForChunks,
  190. metrics,
  191. usageScore,
  192. cachedUrls,
  193. verifyChunks,
  194. prefetchingEnabled,
  195. prefetchLargeEnabled,
  196. cachedUrlLookupEnabled,
  197. abortSignal,
  198. canTryFromCache,
  199. fallbackToCacheURL: getOutputPDFURLFromCache(),
  200. })
  201. .catch(err => {
  202. if (abortSignal.aborted) return
  203. if (canTryFromCache(err)) return fetchFromCache()
  204. if (isExpectedError(err)) {
  205. if (is404(err)) {
  206. // A regular pdf-js request would have seen this 404 as well.
  207. } else {
  208. // Flaky network, switch back to regular pdf-js requests.
  209. metrics.failedCount++
  210. metrics.failedOnce = true
  211. }
  212. const { statusCode, url } = OError.getFullInfo(err) as {
  213. statusCode: number
  214. url: string
  215. }
  216. throw OError.tag(
  217. new PDFJS.ResponseException(undefined, statusCode, true),
  218. 'caching',
  219. { statusCode, url, err }
  220. )
  221. }
  222. metrics.failedCount++
  223. metrics.failedOnce = true
  224. if (!enablePdfCaching) {
  225. throw err // This was a fallback request already. Do not retry.
  226. }
  227. err = OError.tag(err, 'optimized pdf download error', getDebugInfo())
  228. debugConsole.error(err)
  229. captureException(err, {
  230. tags: {
  231. fromPdfCaching: true,
  232. isFromOutputPDFRequest: isFromOutputPDFRequest(err),
  233. },
  234. })
  235. return fallbackRequest({
  236. file: this.pdfFile,
  237. url: this.url,
  238. start,
  239. end,
  240. abortSignal,
  241. }).catch(err => {
  242. if (canTryFromCache(err)) return fetchFromCache()
  243. if (isExpectedError(err)) {
  244. const { statusCode, url } = OError.getFullInfo(err) as {
  245. statusCode: number
  246. url: string
  247. }
  248. throw OError.tag(
  249. new PDFJS.ResponseException(undefined, statusCode, true),
  250. 'fallback',
  251. { statusCode, url, err }
  252. )
  253. }
  254. throw err
  255. })
  256. })
  257. .then(blob => {
  258. if (abortSignal.aborted) return
  259. this.onDataRange(start, blob ? new Uint8Array(blob) : null)
  260. })
  261. .catch(err => {
  262. if (abortSignal.aborted) return
  263. err = OError.tag(err, 'fatal pdf download error', getDebugInfo())
  264. debugConsole.error(err)
  265. if (!(err instanceof PDFJS.ResponseException && err.missing)) {
  266. captureException(err, {
  267. tags: {
  268. fromPdfCaching: true,
  269. isFromOutputPDFRequest: isFromOutputPDFRequest(err),
  270. },
  271. })
  272. }
  273. // Signal error for (subsequent) page load.
  274. this.handleFetchError(err)
  275. })
  276. }
  277. }
  278. return function ({
  279. url,
  280. pdfFile,
  281. abortController,
  282. handleFetchError,
  283. }: {
  284. url: string
  285. pdfFile: PDFFile
  286. abortController: AbortController
  287. handleFetchError: (error: any) => void
  288. }) {
  289. if (metrics.failedOnce) {
  290. // Disable pdf caching once any fetch request failed.
  291. // Be trigger-happy here until we reached a stable state of the feature.
  292. return undefined
  293. }
  294. // Latency is collected per preview cycle.
  295. metrics.latencyComputeMax = 0
  296. metrics.latencyComputeTotal = 0
  297. return new PDFDataRangeTransport({
  298. url,
  299. pdfFile,
  300. abortController,
  301. handleFetchError,
  302. })
  303. }
  304. }