| 1 | import { Injectable, Logger } from '@nestjs/common'; |
| 2 | import { ConfigService } |
| 3 | import { createReadStream } |
| 4 | import { mkdtemp, rm |
| 5 | import { tmpdir } |
| 6 | import path from 'node:path |
| 7 | |
| 8 | import { CodeSearchUnavailableError } |
| 9 | import type { IndexTarget |
| 10 | import { |
| 11 | indexRepository, |
| 12 | repositoryScope, |
| 13 | searchIndex, |
| 14 | } from '../../../lib/git/code-search/zoekt.js'; |
| 15 | import { resolveCommit } |
| 16 | import { isNotFound } |
| 17 | import { S3Service } |
| 18 | |
| 19 | // The search node mirrors this prefix into its index directory, so nothing but shards may live under it. |
| 20 | const SHARD_PREFIX = ' |
| 21 | const MARKER_PREFIX = ' |
| 22 | |
| 23 | /** Indexes repositories as they are opened and publishes the shards to S3, where the zoekt node picks them up. Unset `ZOEKT_URL` turns code search off. */ |
| 24 | @Injectable() |
| 25 | export class CodeSearchService |
| 26 | private readonly logger |
| 27 | private readonly url |
| 28 | private readonly running |
| 29 | private readonly dirty |
| 30 | // Every repository page opens the repository, so the heads this process knows are published spare S3 a marker read per view. |
| 31 | private readonly published |
| 32 | |
| 33 | constructor( |
| 34 | config: ConfigService |
| 35 | private readonly s3 |
| 36 | ) { |
| 37 | this.url = |
| 38 | } |
| 39 | |
| 40 | /** A call while a run is in flight queues exactly one rerun, so the latest HEAD always gets indexed. */ |
| 41 | index(target: |
| 42 | if (!this.url |
| 43 | |
| 44 | const running = |
| 45 | if (running) { |
| 46 | this.dirty.add |
| 47 | return running; |
| 48 | } |
| 49 | |
| 50 | const run = |
| 51 | do { |
| 52 | this.dirty.delete |
| 53 | await this.publish |
| 54 | } while (this. |
| 55 | })().finally(() |
| 56 | |
| 57 | this.running.set |
| 58 | return run; |
| 59 | } |
| 60 | |
| 61 | /** Waits out an index run in flight first, so it cannot publish shards after they are removed. */ |
| 62 | async remove(repositoryId |
| 63 | this.dirty.delete |
| 64 | await this.running |
| 65 | this.published.delete |
| 66 | |
| 67 | await this.s3 |
| 68 | await this.s3 |
| 69 | Bucket: this.s3 |
| 70 | Key: `${MARKER_PREFIX}${ |
| 71 | }); |
| 72 | } |
| 73 | |
| 74 | /** Reads must neither wait on nor fail because of the search index. */ |
| 75 | indexInBackground(target: |
| 76 | this.index(target |
| 77 | this.logger.warn |
| 78 | `Code search index failed for ${target.repositoryId |
| 79 | ), |
| 80 | ); |
| 81 | } |
| 82 | |
| 83 | async searchRepository({ |
| 84 | query, |
| 85 | limit, |
| 86 | ...target |
| 87 | }: IndexTarget |
| 88 | const url = |
| 89 | |
| 90 | const indexing = |
| 91 | this.running.has |
| 92 | (await this.unpublishedStamp |
| 93 | if (indexing) this |
| 94 | |
| 95 | const hits = |
| 96 | url, |
| 97 | query: `r:^${target |
| 98 | limit, |
| 99 | }); |
| 100 | |
| 101 | return { |
| 102 | indexing, |
| 103 | // The query can close the parenthesis and OR in other repositories: the `r:` above only narrows the scan, this filter is what enforces access. |
| 104 | files: hits.filter |
| 105 | }; |
| 106 | } |
| 107 | |
| 108 | /** Narrowed to shards flagged public and, given `repositoryIds`, to those repositories; a query can OR its way out of both, so the caller must still keep only repositories it knows to be public and in scope. */ |
| 109 | async searchPublic({ |
| 110 | query, |
| 111 | limit, |
| 112 | repositoryIds, |
| 113 | }: { |
| 114 | query: string |
| 115 | limit: number |
| 116 | repositoryIds?: string |
| 117 | }) { |
| 118 | return searchIndex({ |
| 119 | url: this.requireUrl |
| 120 | query: `public:yes${repositoryScope |
| 121 | limit, |
| 122 | }); |
| 123 | } |
| 124 | |
| 125 | private requireUrl() { |
| 126 | if (!this.url |
| 127 | return this.url |
| 128 | } |
| 129 | |
| 130 | private async publish |
| 131 | const stamp = |
| 132 | if (!stamp) |
| 133 | |
| 134 | const indexDir = |
| 135 | try { |
| 136 | const shards = |
| 137 | indexDir, |
| 138 | repoDirectory: target.repoDirectory |
| 139 | name: target.repositoryId |
| 140 | isPublic: target.isPublic |
| 141 | }); |
| 142 | const keys = |
| 143 | |
| 144 | for (const [i |
| 145 | const file = |
| 146 | await this.s3 |
| 147 | Bucket: this.s3 |
| 148 | Key: keys[i |
| 149 | Body: createReadStream(file |
| 150 | ContentLength: (await stat |
| 151 | }); |
| 152 | } |
| 153 | // The marker moves last: a crash before it leaves the marker behind, and behind only costs a reindex. |
| 154 | await this.s3 |
| 155 | `${SHARD_PREFIX}${target. |
| 156 | keys, |
| 157 | ); |
| 158 | await this.s3 |
| 159 | Bucket: this.s3 |
| 160 | Key: `${MARKER_PREFIX}${ |
| 161 | Body: stamp, |
| 162 | }); |
| 163 | } finally { |
| 164 | await rm(indexDir |
| 165 | } |
| 166 | |
| 167 | this.published.set |
| 168 | this.logger.log |
| 169 | `Published code search shards for ${target.repositoryId |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | /** HEAD and visibility, as `<sha> public|private`, when the published shards are missing or describe something else; null when they are current. An empty repository has nothing to index. */ |
| 174 | private async unpublishedStamp |
| 175 | repositoryId, |
| 176 | repoDirectory, |
| 177 | isPublic, |
| 178 | }: IndexTarget |
| 179 | const head = |
| 180 | if (!head) |
| 181 | const stamp = |
| 182 | if (this.published |
| 183 | |
| 184 | try { |
| 185 | const marker = |
| 186 | Bucket: this.s3 |
| 187 | Key: `${MARKER_PREFIX}${ |
| 188 | }); |
| 189 | if ((await marker |
| 190 | } catch (error) |
| 191 | if (isNotFound(error |
| 192 | throw error; |
| 193 | } |
| 194 | |
| 195 | this.published.set |
| 196 | return null; |
| 197 | } |
| 198 | } |