| 1 | import { createHash } from 'node:crypto'; |
| 2 | import { pipeline } |
| 3 | import { setTimeout as |
| 4 | |
| 5 | import { Injectable, Logger |
| 6 | |
| 7 | import type { GitRequestBody |
| 8 | import { |
| 9 | directoryConflict, |
| 10 | isWellFormedRef, |
| 11 | } from '../../../lib/git/refs/is-valid-ref-name.js'; |
| 12 | import { createUlid } |
| 13 | import { |
| 14 | NonFastForwardError, |
| 15 | RepositoryDeletedError, |
| 16 | UnreplayableRefError, |
| 17 | WalContentionError, |
| 18 | } from '../../../lib/git/wal/wal.errors.js'; |
| 19 | import { |
| 20 | applyTransitions, |
| 21 | emptyIndex, |
| 22 | type RefTransition, |
| 23 | type WalIndex, |
| 24 | ZERO_OID, |
| 25 | } from '../../../lib/git/wal/wal.types.js'; |
| 26 | import { encodeEntryHeader } |
| 27 | import { |
| 28 | pushBytesTotal, |
| 29 | pushesTotal, |
| 30 | refUpdatesTotal, |
| 31 | } from '../../../lib/metrics.js'; |
| 32 | import { WalStoreService } |
| 33 | |
| 34 | const MAX_ATTEMPTS = 8 |
| 35 | const BASE_BACKOFF_MS = 50 |
| 36 | const MAX_BACKOFF_MS = 1000 |
| 37 | |
| 38 | export interface CommitPushOptions |
| 39 | /** Preallocated by callers that durably track this write before committing it. */ |
| 40 | ulid?: string |
| 41 | repoId: string; |
| 42 | transitions: RefTransition |
| 43 | /** The whole request body; the packfile starts at `packOffset`. */ |
| 44 | body: GitRequestBody |
| 45 | packOffset: number; |
| 46 | pushedBy?: string |
| 47 | } |
| 48 | |
| 49 | export interface CommitPushResult |
| 50 | seq: number; |
| 51 | ulid: string; |
| 52 | } |
| 53 | |
| 54 | @Injectable() |
| 55 | export class PushTransactionService |
| 56 | private readonly logger |
| 57 | |
| 58 | constructor(private readonly |
| 59 | |
| 60 | async commitPush({ |
| 61 | repoId, |
| 62 | transitions, |
| 63 | body, |
| 64 | packOffset, |
| 65 | pushedBy = null, |
| 66 | ulid = createUlid() |
| 67 | }: CommitPushOptions |
| 68 | for (const { |
| 69 | if (!newOid.equals |
| 70 | throw new UnreplayableRefError( |
| 71 | } |
| 72 | } |
| 73 | const packSize = |
| 74 | const packHash = |
| 75 | await pipeline(body |
| 76 | const packSha = |
| 77 | |
| 78 | // Durable before the loop: expensive, idempotent, and unaffected by ordering. A rejected push removes it again; one orphaned by a crash is garbage, never corruption. |
| 79 | await this.store |
| 80 | repoId, |
| 81 | ulid, |
| 82 | encodeEntryHeader({ ulid, |
| 83 | body, |
| 84 | packOffset, |
| 85 | ); |
| 86 | |
| 87 | try { |
| 88 | return await this |
| 89 | repoId, |
| 90 | transitions, |
| 91 | ulid, |
| 92 | packSha, |
| 93 | packSize, |
| 94 | }); |
| 95 | } catch (error) |
| 96 | // Only a verdict that the entry was not committed: a failed CAS request may still have landed, and deleting its entry would corrupt the log. |
| 97 | if ( |
| 98 | error instanceof NonFastForwardError |
| 99 | error instanceof UnreplayableRefError |
| 100 | error instanceof WalContentionError |
| 101 | error instanceof RepositoryDeletedError |
| 102 | ) |
| 103 | await this.store |
| 104 | throw error; |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private async commitIndex |
| 109 | repoId, |
| 110 | transitions, |
| 111 | ulid, |
| 112 | packSha, |
| 113 | packSize, |
| 114 | }: { |
| 115 | repoId: string |
| 116 | transitions: RefTransition |
| 117 | ulid: string |
| 118 | packSha: Buffer |
| 119 | packSize: number |
| 120 | }): Promise |
| 121 | for (let attempt |
| 122 | const current = |
| 123 | const index = |
| 124 | |
| 125 | if (index.layers |
| 126 | return { seq: |
| 127 | } |
| 128 | |
| 129 | this.assertFastForward(index |
| 130 | const refs = |
| 131 | // Checked against the index this CAS replaces, never a cache: two pushes creating `a` and `a/b` each pass against a view that lacks the other. |
| 132 | for (const { |
| 133 | const conflict = |
| 134 | ? null |
| 135 | : directoryConflict(refs, |
| 136 | if (conflict) { |
| 137 | throw new UnreplayableRefError( |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | const next: |
| 142 | seq: index.seq |
| 143 | compactedThroughSeq: index.compactedThroughSeq |
| 144 | refs, |
| 145 | layers: [...index |
| 146 | deleted: false, |
| 147 | }; |
| 148 | |
| 149 | if (await this |
| 150 | pushesTotal.add(1) |
| 151 | pushBytesTotal.add(packSize |
| 152 | refUpdatesTotal.add(transitions |
| 153 | return { seq: |
| 154 | } |
| 155 | |
| 156 | this.logger.debug |
| 157 | const ceiling = |
| 158 | await sleep(Math |
| 159 | } |
| 160 | |
| 161 | throw new WalContentionError( |
| 162 | } |
| 163 | |
| 164 | private assertFastForward(index |
| 165 | for (const transition |
| 166 | const live = |
| 167 | if (!live.equals |
| 168 | throw new NonFastForwardError( |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | function seqOfLayer(index: |
| 175 | return ( |
| 176 | index.compactedThroughSeq + |
| 177 | index.layers.findIndex |
| 178 | 1 |
| 179 | ); |
| 180 | } |