| 1 | import { Injectable, Logger } from '@nestjs/common'; |
| 2 | import { createHash } |
| 3 | import { pipeline } |
| 4 | import { setTimeout as |
| 5 | |
| 6 | import type { GitRequestBody |
| 7 | |
| 8 | import { encodeEntryHeader } |
| 9 | import { WalStoreService } |
| 10 | import { |
| 11 | NonFastForwardError, |
| 12 | WalContentionError, |
| 13 | } from '../../../lib/git/wal/wal.errors.js'; |
| 14 | import { |
| 15 | applyTransitions, |
| 16 | emptyIndex, |
| 17 | ZERO_OID, |
| 18 | type RefTransition, |
| 19 | type WalIndex, |
| 20 | } from '../../../lib/git/wal/wal.types.js'; |
| 21 | import { createUlid } |
| 22 | import { |
| 23 | pushBytesTotal, |
| 24 | pushesTotal, |
| 25 | refUpdatesTotal, |
| 26 | } from '../../../lib/metrics.js'; |
| 27 | |
| 28 | const MAX_ATTEMPTS = 8 |
| 29 | const BASE_BACKOFF_MS = 50 |
| 30 | const MAX_BACKOFF_MS = 1000 |
| 31 | |
| 32 | export interface CommitPushOptions |
| 33 | repoId: string; |
| 34 | transitions: RefTransition |
| 35 | /** The whole request body; the packfile starts at `packOffset`. */ |
| 36 | body: GitRequestBody |
| 37 | packOffset: number; |
| 38 | pushedBy?: string |
| 39 | } |
| 40 | |
| 41 | export interface CommitPushResult |
| 42 | seq: number; |
| 43 | ulid: string; |
| 44 | } |
| 45 | |
| 46 | @Injectable() |
| 47 | export class PushTransactionService |
| 48 | private readonly logger |
| 49 | |
| 50 | constructor(private readonly |
| 51 | |
| 52 | async commitPush({ |
| 53 | repoId, |
| 54 | transitions, |
| 55 | body, |
| 56 | packOffset, |
| 57 | pushedBy = null, |
| 58 | }: CommitPushOptions |
| 59 | const ulid = |
| 60 | const packSize = |
| 61 | const packHash = |
| 62 | await pipeline(body |
| 63 | const packSha = |
| 64 | |
| 65 | // Durable before the loop: expensive, idempotent, and unaffected by ordering. An entry orphaned by a failed transaction is garbage, never corruption. |
| 66 | await this.store |
| 67 | repoId, |
| 68 | ulid, |
| 69 | encodeEntryHeader({ ulid, |
| 70 | body, |
| 71 | packOffset, |
| 72 | ); |
| 73 | |
| 74 | for (let attempt |
| 75 | const current = |
| 76 | const index = |
| 77 | |
| 78 | if (index.layers |
| 79 | return { seq: |
| 80 | } |
| 81 | |
| 82 | this.assertFastForward(index |
| 83 | |
| 84 | const next: |
| 85 | seq: index.seq |
| 86 | compactedThroughSeq: index.compactedThroughSeq |
| 87 | refs: applyTransitions(index |
| 88 | layers: [...index |
| 89 | }; |
| 90 | |
| 91 | if (await this |
| 92 | pushesTotal.add(1) |
| 93 | pushBytesTotal.add(packSize |
| 94 | refUpdatesTotal.add(transitions |
| 95 | return { seq: |
| 96 | } |
| 97 | |
| 98 | this.logger.debug |
| 99 | await sleep(backoffMs |
| 100 | } |
| 101 | |
| 102 | throw new WalContentionError( |
| 103 | } |
| 104 | |
| 105 | private assertFastForward(index |
| 106 | for (const transition |
| 107 | const live = |
| 108 | if (!live.equals |
| 109 | throw new NonFastForwardError( |
| 110 | } |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | function seqOfLayer(index: |
| 116 | return ( |
| 117 | index.compactedThroughSeq + |
| 118 | index.layers.findIndex |
| 119 | 1 |
| 120 | ); |
| 121 | } |
| 122 | |
| 123 | function backoffMs(attempt: |
| 124 | const ceiling = |
| 125 | return Math.round |
| 126 | } |