| 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 | RepositoryDeletedError, |
| 13 | WalContentionError, |
| 14 | } from '../../../lib/git/wal/wal.errors.js'; |
| 15 | import { |
| 16 | applyTransitions, |
| 17 | emptyIndex, |
| 18 | ZERO_OID, |
| 19 | type RefTransition, |
| 20 | type WalIndex, |
| 21 | } from '../../../lib/git/wal/wal.types.js'; |
| 22 | import { createUlid } |
| 23 | import { |
| 24 | pushBytesTotal, |
| 25 | pushesTotal, |
| 26 | refUpdatesTotal, |
| 27 | } from '../../../lib/metrics.js'; |
| 28 | |
| 29 | const MAX_ATTEMPTS = 8 |
| 30 | const BASE_BACKOFF_MS = 50 |
| 31 | const MAX_BACKOFF_MS = 1000 |
| 32 | |
| 33 | export interface CommitPushOptions |
| 34 | repoId: string; |
| 35 | transitions: RefTransition |
| 36 | /** The whole request body; the packfile starts at `packOffset`. */ |
| 37 | body: GitRequestBody |
| 38 | packOffset: number; |
| 39 | pushedBy?: string |
| 40 | } |
| 41 | |
| 42 | export interface CommitPushResult |
| 43 | seq: number; |
| 44 | ulid: string; |
| 45 | } |
| 46 | |
| 47 | @Injectable() |
| 48 | export class PushTransactionService |
| 49 | private readonly logger |
| 50 | |
| 51 | constructor(private readonly |
| 52 | |
| 53 | async commitPush({ |
| 54 | repoId, |
| 55 | transitions, |
| 56 | body, |
| 57 | packOffset, |
| 58 | pushedBy = null, |
| 59 | }: CommitPushOptions |
| 60 | const ulid = |
| 61 | const packSize = |
| 62 | const packHash = |
| 63 | await pipeline(body |
| 64 | const packSha = |
| 65 | |
| 66 | // 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. |
| 67 | await this.store |
| 68 | repoId, |
| 69 | ulid, |
| 70 | encodeEntryHeader({ ulid, |
| 71 | body, |
| 72 | packOffset, |
| 73 | ); |
| 74 | |
| 75 | try { |
| 76 | return await this |
| 77 | repoId, |
| 78 | transitions, |
| 79 | ulid, |
| 80 | packSha, |
| 81 | packSize, |
| 82 | }); |
| 83 | } catch (error) |
| 84 | // 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. |
| 85 | if ( |
| 86 | error instanceof NonFastForwardError |
| 87 | error instanceof WalContentionError |
| 88 | error instanceof RepositoryDeletedError |
| 89 | ) |
| 90 | await this.store |
| 91 | throw error; |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | private async commitIndex |
| 96 | repoId, |
| 97 | transitions, |
| 98 | ulid, |
| 99 | packSha, |
| 100 | packSize, |
| 101 | }: { |
| 102 | repoId: string |
| 103 | transitions: RefTransition |
| 104 | ulid: string |
| 105 | packSha: Buffer |
| 106 | packSize: number |
| 107 | }): Promise |
| 108 | for (let attempt |
| 109 | const current = |
| 110 | const index = |
| 111 | |
| 112 | if (index.layers |
| 113 | return { seq: |
| 114 | } |
| 115 | |
| 116 | this.assertFastForward(index |
| 117 | |
| 118 | const next: |
| 119 | seq: index.seq |
| 120 | compactedThroughSeq: index.compactedThroughSeq |
| 121 | refs: applyTransitions(index |
| 122 | layers: [...index |
| 123 | deleted: false, |
| 124 | }; |
| 125 | |
| 126 | if (await this |
| 127 | pushesTotal.add(1) |
| 128 | pushBytesTotal.add(packSize |
| 129 | refUpdatesTotal.add(transitions |
| 130 | return { seq: |
| 131 | } |
| 132 | |
| 133 | this.logger.debug |
| 134 | await sleep(backoffMs |
| 135 | } |
| 136 | |
| 137 | throw new WalContentionError( |
| 138 | } |
| 139 | |
| 140 | private assertFastForward(index |
| 141 | for (const transition |
| 142 | const live = |
| 143 | if (!live.equals |
| 144 | throw new NonFastForwardError( |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | function seqOfLayer(index: |
| 151 | return ( |
| 152 | index.compactedThroughSeq + |
| 153 | index.layers.findIndex |
| 154 | 1 |
| 155 | ); |
| 156 | } |
| 157 | |
| 158 | function backoffMs(attempt: |
| 159 | const ceiling = |
| 160 | return Math.round |
| 161 | } |