| 1 | import { Injectable } from '@nestjs/common'; |
| 2 | |
| 3 | import type { Readable |
| 4 | |
| 5 | import { S3Service } |
| 6 | import { isNotFound, statusOf |
| 7 | import { |
| 8 | prefixed, |
| 9 | type GitRequestBody, |
| 10 | } from '../../../lib/git/protocol/git-request-body.js'; |
| 11 | import { |
| 12 | decodeEntryHeader, |
| 13 | decodeIndex, |
| 14 | encodeIndex, |
| 15 | ENTRY_CONTENT_TYPE, |
| 16 | ENTRY_HEADER_PROBE_BYTES, |
| 17 | INDEX_CONTENT_TYPE, |
| 18 | } from '../../../lib/git/wal/wal-codec.js'; |
| 19 | import { |
| 20 | emptyIndex, |
| 21 | type WalEntryHeader, |
| 22 | type WalIndex, |
| 23 | } from '../../../lib/git/wal/wal.types.js'; |
| 24 | import { |
| 25 | RepositoryDeletedError, |
| 26 | WalContentionError, |
| 27 | } from '../../../lib/git/wal/wal.errors.js'; |
| 28 | |
| 29 | // Each lost CAS is a push that committed in between; only a stream of pushes could outlast this, and the caller retries. |
| 30 | const MAX_TOMBSTONE_ATTEMPTS = 8 |
| 31 | |
| 32 | export interface StoredIndex |
| 33 | index: WalIndex |
| 34 | etag: string; |
| 35 | } |
| 36 | |
| 37 | @Injectable() |
| 38 | export class WalStoreService |
| 39 | constructor(private readonly |
| 40 | |
| 41 | indexKey(repoId: |
| 42 | return `repos/${repoId |
| 43 | } |
| 44 | |
| 45 | entryKey(repoId: |
| 46 | return `repos/${repoId |
| 47 | } |
| 48 | |
| 49 | // null when the repository has no index yet. |
| 50 | async readIndex(repoId |
| 51 | const stored = |
| 52 | if (stored?.index |
| 53 | return stored; |
| 54 | } |
| 55 | |
| 56 | private async readStoredIndex |
| 57 | try { |
| 58 | const response = |
| 59 | Bucket: this.s3 |
| 60 | Key: this.indexKey |
| 61 | }); |
| 62 | const body = |
| 63 | return { index: |
| 64 | } catch (error) |
| 65 | if (isNotFound(error |
| 66 | throw error; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | /** 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. */ |
| 71 | async tombstone(repoId |
| 72 | for (let attempt |
| 73 | const current = |
| 74 | if (current?.index |
| 75 | |
| 76 | const next = |
| 77 | ...emptyIndex(), |
| 78 | seq: (current?.index |
| 79 | deleted: true, |
| 80 | }; |
| 81 | if (await this |
| 82 | } |
| 83 | throw new WalContentionError( |
| 84 | } |
| 85 | |
| 86 | /** Every entry of the log. Only safe behind a tombstone, which stops new entries from being committed. */ |
| 87 | async purgeEntries(repoId |
| 88 | await this.s3 |
| 89 | } |
| 90 | |
| 91 | async deleteEntry(repoId |
| 92 | await this.s3 |
| 93 | Bucket: this.s3 |
| 94 | Key: this.entryKey |
| 95 | }); |
| 96 | } |
| 97 | |
| 98 | /** Copies a repository's log 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. */ |
| 99 | async copyLog(fromRepoId |
| 100 | const stored = |
| 101 | if (!stored) |
| 102 | |
| 103 | for (const layer |
| 104 | await this.s3 |
| 105 | Bucket: this.s3 |
| 106 | Key: this.entryKey |
| 107 | CopySource: `${this. |
| 108 | }); |
| 109 | } |
| 110 | |
| 111 | await this.casIndex |
| 112 | } |
| 113 | |
| 114 | /** The commit point of a push. A null etag means create-if-absent. Returns false when another writer won the race. */ |
| 115 | async casIndex( |
| 116 | repoId: string, |
| 117 | next: WalIndex |
| 118 | etag: string |
| 119 | ): Promise<boolean |
| 120 | try { |
| 121 | await this.s3 |
| 122 | Bucket: this.s3 |
| 123 | Key: this.indexKey |
| 124 | Body: encodeIndex(next |
| 125 | ContentType: INDEX_CONTENT_TYPE, |
| 126 | ...(etag ? |
| 127 | }); |
| 128 | return true; |
| 129 | } catch (error) |
| 130 | const status = |
| 131 | if (status === |
| 132 | throw error; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | /** 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. */ |
| 137 | async putEntry( |
| 138 | repoId: string, |
| 139 | ulid: string, |
| 140 | header: Buffer |
| 141 | pack: GitRequestBody |
| 142 | packStart = 0, |
| 143 | ): Promise<void |
| 144 | await this.s3 |
| 145 | Bucket: this.s3 |
| 146 | Key: this.entryKey |
| 147 | Body: prefixed(header |
| 148 | ContentLength: header.length |
| 149 | ContentType: ENTRY_CONTENT_TYPE, |
| 150 | }); |
| 151 | } |
| 152 | |
| 153 | async readEntryHeader( |
| 154 | repoId: string, |
| 155 | ulid: string, |
| 156 | ): Promise<WalEntryHeader |
| 157 | try { |
| 158 | const response = |
| 159 | Bucket: this.s3 |
| 160 | Key: this.entryKey |
| 161 | Range: `bytes=0-${ENTRY_HEADER_PROBE_BYTES |
| 162 | }); |
| 163 | const body = |
| 164 | return decodeEntryHeader(body |
| 165 | } catch (error) |
| 166 | if (isNotFound(error |
| 167 | throw error; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /** A ranged GET that skips the header, so the packfile never becomes a Buffer. */ |
| 172 | async openEntryPack(repoId |
| 173 | const head = |
| 174 | Bucket: this.s3 |
| 175 | Key: this.entryKey |
| 176 | Range: `bytes=0-${ENTRY_HEADER_PROBE_BYTES |
| 177 | }); |
| 178 | const { packOffset |
| 179 | Buffer.from(await |
| 180 | ); |
| 181 | |
| 182 | const response = |
| 183 | Bucket: this.s3 |
| 184 | Key: this.entryKey |
| 185 | Range: `bytes=${packOffset |
| 186 | }); |
| 187 | return response.Body |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /** 412 lost the race outright, 409 collided with a concurrent conditional write. */ |