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