pdf-caching-transport.ts 10 KB

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