| 1 | import { Injectable } from '@nestjs/common'; |
| 2 | |
| 3 | import type { Readable |
| 4 | |
| 5 | import { S3Service } |
| 6 | import { isPullRef } |
| 7 | import { isNotFound, statusOf |
| 8 | import { |
| 9 | prefixed, |
| 10 | type GitRequestBody, |
| 11 | } from '../../../lib/git/protocol/git-request-body.js'; |
| 12 | import { |
| 13 | decodeEntryHeader, |
| 14 | decodeIndex, |
| 15 | encodeIndex, |
| 16 | ENTRY_CONTENT_TYPE, |
| 17 | ENTRY_HEADER_PROBE_BYTES, |
| 18 | entryHeaderLength, |
| 19 | INDEX_CONTENT_TYPE, |
| 20 | } from '../../../lib/git/wal/wal-codec.js'; |
| 21 | import { |
| 22 | emptyIndex, |
| 23 | type WalEntryHeader, |
| 24 | type WalIndex, |
| 25 | } from '../../../lib/git/wal/wal.types.js'; |
| 26 | import { |
| 27 | RepositoryDeletedError, |
| 28 | WalContentionError, |
| 29 | } from '../../../lib/git/wal/wal.errors.js'; |
| 30 | |
| 31 | // Each lost CAS is a push that committed in between; only a stream of pushes could outlast this, and the caller retries. |
| 32 | const MAX_TOMBSTONE_ATTEMPTS = 8 |
| 33 | |
| 34 | export interface StoredIndex |
| 35 | index: WalIndex |
| 36 | etag: string; |
| 37 | } |
| 38 | |
| 39 | @Injectable() |
| 40 | export class WalStoreService |
| 41 | constructor(private readonly |
| 42 | |
| 43 | indexKey(repoId: |
| 44 | return `repos/${repoId |
| 45 | } |
| 46 | |
| 47 | entryKey(repoId: |
| 48 | return `repos/${repoId |
| 49 | } |
| 50 | |
| 51 | // null when the repository has no index yet. |
| 52 | async readIndex(repoId |
| 53 | const stored = |
| 54 | if (stored?.index |
| 55 | return stored; |
| 56 | } |
| 57 | |
| 58 | private async readStoredIndex |
| 59 | try { |
| 60 | const response = |
| 61 | Bucket: this.s3 |
| 62 | Key: this.indexKey |
| 63 | }); |
| 64 | const body = |
| 65 | return { index: |
| 66 | } catch (error) |
| 67 | if (isNotFound(error |
| 68 | throw error; |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /** Swaps the index for an empty one flagged deleted, so a push racing the delete loses its CAS and then reads the repository as gone. The tombstone is never removed: a push authorized just before the delete could otherwise recreate the index from nothing. */ |
| 73 | async tombstone(repoId |
| 74 | for (let attempt |
| 75 | const current = |
| 76 | if (current?.index |
| 77 | |
| 78 | const next = |
| 79 | ...emptyIndex(), |
| 80 | seq: (current?.index |
| 81 | deleted: true, |
| 82 | }; |
| 83 | if (await this |
| 84 | } |
| 85 | throw new WalContentionError( |
| 86 | } |
| 87 | |
| 88 | /** Every entry of the log. Only safe behind a tombstone, which stops new entries from being committed. */ |
| 89 | async purgeEntries(repoId |
| 90 | await this.s3 |
| 91 | } |
| 92 | |
| 93 | async deleteEntry(repoId |
| 94 | await this.s3 |
| 95 | Bucket: this.s3 |
| 96 | Key: this.entryKey |
| 97 | }); |
| 98 | } |
| 99 | |
| 100 | /** Copies the log `index` describes to a new id: the packs first, then the index that names them, so a fork is never pointed at objects that have not landed yet. Layers are immutable once written, so this needs no lock. */ |
| 101 | async copyLog(fromRepoId |
| 102 | for (const layer |
| 103 | await this.s3 |
| 104 | Bucket: this.s3 |
| 105 | Key: this.entryKey |
| 106 | CopySource: `${this. |
| 107 | }); |
| 108 | } |
| 109 | |
| 110 | // A fork starts with the parent's branches and tags, not its pull requests: those refs belong to the repository the requests were opened against. |
| 111 | await this.casIndex |
| 112 | toRepoId, |
| 113 | { |
| 114 | ...index, |
| 115 | refs: new Map |
| 116 | }, |
| 117 | null, |
| 118 | ); |
| 119 | } |
| 120 | |
| 121 | /** The commit point of a push. A null etag means create-if-absent. Returns false when another writer won the race. */ |
| 122 | async casIndex( |
| 123 | repoId: string, |
| 124 | next: WalIndex |
| 125 | etag: string |
| 126 | ): Promise<boolean |
| 127 | try { |
| 128 | await this.s3 |
| 129 | Bucket: this.s3 |
| 130 | Key: this.indexKey |
| 131 | Body: encodeIndex(next |
| 132 | ContentType: INDEX_CONTENT_TYPE, |
| 133 | ...(etag ? |
| 134 | }); |
| 135 | return true; |
| 136 | } catch (error) |
| 137 | const status = |
| 138 | if (status === |
| 139 | throw error; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | /** ContentLength is mandatory: the body is a stream, so the SDK cannot measure it, and concatenating header and pack in memory is exactly what a large push cannot afford. */ |
| 144 | async putEntry( |
| 145 | repoId: string, |
| 146 | ulid: string, |
| 147 | header: Buffer |
| 148 | pack: GitRequestBody |
| 149 | packStart = 0, |
| 150 | ): Promise<void |
| 151 | await this.s3 |
| 152 | Bucket: this.s3 |
| 153 | Key: this.entryKey |
| 154 | Body: prefixed(header |
| 155 | ContentLength: header.length |
| 156 | ContentType: ENTRY_CONTENT_TYPE, |
| 157 | }); |
| 158 | } |
| 159 | |
| 160 | async readEntryHeader( |
| 161 | repoId: string, |
| 162 | ulid: string, |
| 163 | ): Promise<WalEntryHeader |
| 164 | try { |
| 165 | return decodeEntryHeader(await |
| 166 | .header; |
| 167 | } catch (error) |
| 168 | if (isNotFound(error |
| 169 | throw error; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** One ranged GET for an ordinary header; a header past the probe, from a push of thousands of refs, takes a second one sized to fit. */ |
| 174 | private async readEntryHeaderBytes |
| 175 | const read = |
| 176 | const response = |
| 177 | Bucket: this.s3 |
| 178 | Key: this.entryKey |
| 179 | Range: `bytes=0-${length |
| 180 | }); |
| 181 | return Buffer.from |
| 182 | }; |
| 183 | |
| 184 | const probe = |
| 185 | const length = |
| 186 | return length > |
| 187 | } |
| 188 | |
| 189 | /** A ranged GET that skips the header, so the packfile never becomes a Buffer. */ |
| 190 | async openEntryPack(repoId |
| 191 | const { packOffset |
| 192 | await this.readEntryHeaderBytes |
| 193 | ); |
| 194 | |
| 195 | const response = |
| 196 | Bucket: this.s3 |
| 197 | Key: this.entryKey |
| 198 | Range: `bytes=${packOffset |
| 199 | }); |
| 200 | return response.Body |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | /** 412 lost the race outright, 409 collided with a concurrent conditional write. */ |